rainmeter-studio/Library/Litestep.cpp

154 lines
3.9 KiB
C++
Raw Normal View History

2009-02-10 18:37:48 +00:00
/*
Copyright (C) 2002 Kimmo Pekkola + few lsapi developers
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2009-02-10 18:37:48 +00:00
*/
#include "StdAfx.h"
2009-02-10 18:37:48 +00:00
#include "Litestep.h"
#include "Rainmeter.h"
#include "DialogAbout.h"
#include "System.h"
2009-02-10 18:37:48 +00:00
extern CRainmeter* Rainmeter;
2012-04-09 19:45:54 +03:00
UINT GetUniqueID()
{
static UINT id = 0;
return id++;
}
2012-05-29 19:02:20 +03:00
void RunCommand(std::wstring command)
{
2009-02-10 18:37:48 +00:00
std::wstring args;
2011-07-18 00:32:09 +00:00
size_t notwhite = command.find_first_not_of(L" \t\r\n");
2009-02-10 18:37:48 +00:00
command.erase(0, notwhite);
size_t quotePos = command.find(L'"');
2009-02-10 18:37:48 +00:00
if (quotePos == 0)
{
size_t quotePos2 = command.find(L'"', quotePos + 1);
2009-02-10 18:37:48 +00:00
if (quotePos2 != std::wstring::npos)
{
2011-07-14 00:26:53 +00:00
args.assign(command, quotePos2 + 1, command.length() - (quotePos2 + 1));
command.assign(command, quotePos + 1, quotePos2 - quotePos - 1);
2009-02-10 18:37:48 +00:00
}
else
{
2011-07-14 00:26:53 +00:00
command.erase(0, 1);
2009-02-10 18:37:48 +00:00
}
}
else
{
size_t spacePos = command.find(L' ');
2009-02-10 18:37:48 +00:00
if (spacePos != std::wstring::npos)
{
2011-07-14 00:26:53 +00:00
args.assign(command, spacePos + 1, command.length() - (spacePos + 1));
command.erase(spacePos);
2009-02-10 18:37:48 +00:00
}
}
2012-06-02 12:55:21 +03:00
if (!command.empty())
{
RunFile(command.c_str(), args.c_str());
}
2012-05-29 19:02:20 +03:00
}
2012-08-18 09:45:16 +03:00
void RunFile(const WCHAR* file, const WCHAR* args)
2012-05-29 19:02:20 +03:00
{
SHELLEXECUTEINFO si = {sizeof(SHELLEXECUTEINFO)};
2012-08-18 09:45:16 +03:00
si.lpVerb = L"open";
2012-05-29 19:02:20 +03:00
si.lpFile = file;
si.nShow = SW_SHOWNORMAL;
DWORD type = GetFileAttributes(si.lpFile);
if (type & FILE_ATTRIBUTE_DIRECTORY && type != 0xFFFFFFFF)
{
ShellExecute(si.hwnd, si.lpVerb, si.lpFile, NULL, NULL, si.nShow);
}
else
{
std::wstring dir = CRainmeter::ExtractPath(file);
si.lpDirectory = dir.c_str();
2012-05-29 19:02:20 +03:00
si.lpParameters = args;
si.fMask = SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI;
ShellExecuteEx(&si);
}
2009-02-10 18:37:48 +00:00
}
2011-09-24 09:13:13 +00:00
WCHAR* GetString(UINT id)
2011-09-23 16:28:38 +00:00
{
LPWSTR pData;
int len = LoadString(Rainmeter->GetResourceInstance(), id, (LPWSTR)&pData, 0);
2011-09-24 09:13:13 +00:00
return len ? pData : L"";
2011-09-23 16:28:38 +00:00
}
std::wstring GetFormattedString(UINT id, ...)
{
LPWSTR pBuffer = NULL;
va_list args = NULL;
va_start(args, id);
DWORD len = FormatMessage(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
GetString(id),
0,
0,
(LPWSTR)&pBuffer,
0,
&args);
2011-09-23 16:28:38 +00:00
va_end(args);
2012-09-08 10:09:14 -07:00
std::wstring tmpSz(len ? pBuffer : L"", len);
if (pBuffer) LocalFree(pBuffer);
2011-09-23 16:28:38 +00:00
return tmpSz;
}
HICON GetIcon(UINT id, bool large)
{
typedef HRESULT (WINAPI * FPLOADICONMETRIC)(HINSTANCE hinst, PCWSTR pszName, int lims, HICON* phico);
HINSTANCE hExe = GetModuleHandle(NULL);
HINSTANCE hComctl = GetModuleHandle(L"Comctl32");
if (hComctl)
{
// Try LoadIconMetric for better quality with high DPI
FPLOADICONMETRIC loadIconMetric = (FPLOADICONMETRIC)GetProcAddress(hComctl, "LoadIconMetric");
if (loadIconMetric)
{
HICON icon;
HRESULT hr = loadIconMetric(hExe, MAKEINTRESOURCE(id), large ? LIM_LARGE : LIM_SMALL, &icon);
if (SUCCEEDED(hr))
{
return icon;
}
}
}
return (HICON)LoadImage(
hExe,
MAKEINTRESOURCE(id),
IMAGE_ICON,
GetSystemMetrics(large ? SM_CXICON : SM_CXSMICON),
GetSystemMetrics(large ? SM_CYICON : SM_CYSMICON),
LR_SHARED);
}
void RmNullCRTInvalidParameterHandler(const wchar_t* expression, const wchar_t* function, const wchar_t* file, unsigned int line, uintptr_t pReserved)
{
// Do nothing.
}