Code: Select all
// -----------------------------------------------------------------------------
// Name: winapi.cpp
// Author: Kenny "Scriptdaemon" Williams
// Created: 3/14/2009
// Description: Windows-specific functions (implementation)
// -----------------------------------------------------------------------------
// TODO: Use enum for desktop styles
// TODO: Should I use a singleton or keep static methods?
#ifndef WX_PRECOMP
# include <wx/msw/wrapwin.h>
#endif
#include "winapi.h"
#include <wx/msw/registry.h>
#include <wx/msw/wrapshl.h>
#include <wx/filename.h>
#include <wx/stdpaths.h>
// -----------------------------------------------------------------------------
// Public methods
// -----------------------------------------------------------------------------
const wxString WinApi::GetDesktopBackground()
{
char buff[MAX_PATH];
SystemParametersInfo(SPI_GETDESKWALLPAPER, MAX_PATH, &buff[0], 0);
return wxString(buff, wxConvUTF8);
}
const wxString WinApi::GetUserPicsDir()
{
LPITEMIDLIST pidl;
const HRESULT hr = SHGetSpecialFolderLocation(NULL, CSIDL_MYPICTURES, &pidl);
// This will also free pidl
return SUCCEEDED(hr) ? wxItemIdList(pidl).GetPath() : *wxEmptyString;
}
void WinApi::SetDesktopBackground(const wxBitmap bmp, const wxString style)
{
// If the bitmap object exists, save to user's pictures directory
const wxString path = bmp.IsOk() ? SaveAsBmp(bmp) : GetDesktopBackground();
// Set the style before updating the background
SetDesktopBackgroundStyle(style);
// Clear the current background first (releases lock on current bitmap)
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (PVOID)wxEmptyString, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (PVOID)path.c_str(), SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
}
void WinApi::SetRunKey(const bool addVal)
{
wxRegKey key(wxRegKey::HKCU, "Software\\Microsoft\\Windows\\CurrentVersion\\Run");
if (addVal)
{
key.SetValue(wxTheApp->GetAppName(), wxStandardPaths::Get().GetExecutablePath());
}
else
{
key.DeleteValue(wxTheApp->GetAppName());
}
}
// -----------------------------------------------------------------------------
// Private methods
// -----------------------------------------------------------------------------
const wxString WinApi::SaveAsBmp(wxBitmap bmp)
{
const wxFileName file(GetUserPicsDir(), wxTheApp->GetAppName(), "bmp");
const wxString path = file.GetFullPath();
bmp.SaveFile(path, wxBITMAP_TYPE_BMP);
return path;
}
void WinApi::SetDesktopBackgroundStyle(const wxString style)
{
wxRegKey key(wxRegKey::HKCU, "Control Panel\\Desktop");
switch (style[0])
{
case 'S': // Stretched
key.SetValue("WallpaperStyle", "2");
key.SetValue("TileWallpaper", "0");
break;
case 'C': // Centered
key.SetValue("WallpaperStyle", "1");
key.SetValue("TileWallpaper", "0");
break;
case 'T': // Tiled
key.SetValue("WallpaperStyle", "1");
key.SetValue("TileWallpaper", "1");
break;
default: ; // Use previous style
}
}