1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
/*
* NotificationHandler.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "NotificationHandler.h"
#include <SDL_video.h>
#include <SDL_syswm.h>
#if defined(VCMI_WINDOWS)
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h>
#include <shellapi.h>
// C RunTime Header Files
#define WM_USER_SHELLICON WM_USER + 1
// Global Variables:
struct NotificationState
{
HINSTANCE hInst; // current instance
NOTIFYICONDATA niData; // notify icon data
bool initialized = false;
SDL_Window * window;
};
NotificationState state;
void NotificationHandler::notify(std::string msg)
{
NOTIFYICONDATA niData;
SDL_SysWMinfo info;
SDL_VERSION(&info.version);
if(!SDL_GetWindowWMInfo(state.window, &info))
return;
if(info.info.win.window == GetForegroundWindow())
return;
ZeroMemory(&niData, sizeof(NOTIFYICONDATA));
niData.cbSize = sizeof(NOTIFYICONDATA);
niData.hWnd = info.info.win.window;
niData.uID = 1;
niData.uFlags = NIF_INFO | NIF_MESSAGE;
niData.uCallbackMessage = WM_USER_SHELLICON;
niData.dwInfoFlags = NIIF_INFO;
msg.copy(niData.szInfo, msg.length());
Shell_NotifyIcon(NIM_MODIFY, &niData);
}
void NotificationHandler::init(SDL_Window * window)
{
state.window = window;
if(state.initialized)
return;
SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
NOTIFYICONDATA niData;
SDL_SysWMinfo info;
SDL_VERSION(&info.version);
if(!SDL_GetWindowWMInfo(state.window, &info))
return;
ZeroMemory(&niData, sizeof(NOTIFYICONDATA));
state.hInst = (HINSTANCE)GetModuleHandle("VCMI_client.exe");
niData.cbSize = sizeof(NOTIFYICONDATA);
niData.hWnd = info.info.win.window;
niData.uID = 1;
niData.uFlags = NIF_ICON | NIF_MESSAGE;
niData.uCallbackMessage = WM_USER_SHELLICON;
niData.hIcon = (HICON)LoadImage(
state.hInst,
"IDI_ICON1",
IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON),
LR_DEFAULTSIZE);
Shell_NotifyIcon(NIM_ADD, &niData);
state.initialized = true;
}
void NotificationHandler::destroy()
{
NOTIFYICONDATA niData;
SDL_SysWMinfo info;
SDL_VERSION(&info.version);
if(!SDL_GetWindowWMInfo(state.window, &info))
return;
ZeroMemory(&niData, sizeof(NOTIFYICONDATA));
niData.cbSize = sizeof(NOTIFYICONDATA);
niData.hWnd = info.info.win.window;
niData.uID = 1;
Shell_NotifyIcon(NIM_DELETE, &niData);
}
bool NotificationHandler::handleSdlEvent(const SDL_Event & ev)
{
if(ev.syswm.msg->msg.win.msg == WM_USER_SHELLICON)
{
auto winMsg = LOWORD(ev.syswm.msg->msg.win.lParam);
if(winMsg == WM_LBUTTONUP || winMsg == NIN_BALLOONUSERCLICK)
{
SDL_MinimizeWindow(state.window);
SDL_RestoreWindow(state.window);
SDL_RaiseWindow(state.window);
return true;
}
}
return false;
}
#else
void NotificationHandler::notify(std::string msg)
{
}
void NotificationHandler::init(SDL_Window * window)
{
}
void NotificationHandler::destroy()
{
}
bool NotificationHandler::handleSdlEvent(const SDL_Event & ev)
{
return false;
}
#endif
|