-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTrayIcon.cpp
More file actions
326 lines (291 loc) · 8.27 KB
/
Copy pathTrayIcon.cpp
File metadata and controls
326 lines (291 loc) · 8.27 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// From:
// https://www.codeproject.com/Articles/127791/A-simple-and-easy-to-use-trayicon
/*
* Copyright (c) Istvan Pasztor
* This source has been published on www.codeproject.com under the CPOL license.
*/
#include "stdafx.h"
#include "TrayIcon.h"
#define TRAY_WINDOW_MESSAGE (WM_USER+100)
namespace
{
// A map that never holds allocated memory when it is empty. This map will be created with placement new as a static variable,
// and its destructor will be never called, and it shouldn't leak memory if it contains no items at program exit.
// This dirty trick is useful when you create your trayicon object as static. In this case we can not control the
// order of destruction of this map object and the static trayicon object. However this dirty trick ensures that
// the map is freed exactly when the destructor of the last static trayicon is unregistering itself.
class CIdToTrayIconMap
{
public:
typedef UINT KeyType;
typedef CTrayIcon* ValueType;
// typedef didn't work with VC++6
struct StdMap : public std::map<KeyType,ValueType> {};
typedef StdMap::iterator iterator;
CIdToTrayIconMap() : m_Empty(true) {}
ValueType& operator[](KeyType k)
{
return GetOrCreateStdMap()[k];
}
ValueType* find(KeyType k)
{
if (m_Empty)
return false;
StdMap::iterator it = GetStdMap().find(k);
if (it == GetStdMap().end())
return NULL;
return &it->second;
}
int erase(KeyType k)
{
if (m_Empty)
return 0;
StdMap& m = GetStdMap();
int res = (int)m.erase(k);
if (m.empty())
{
m.~StdMap();
m_Empty = true;
}
return res;
}
bool empty() const
{
return m_Empty;
}
// Call this only when the container is not empty!!!
iterator begin()
{
assert(!m_Empty); // Call this only when the container is not empty!!!
return m_Empty ? iterator() : GetStdMap().begin();
}
// Call this only when the container is not empty!!!
iterator end()
{
assert(!m_Empty); // Call this only when the container is not empty!!!
return m_Empty ? iterator() : GetStdMap().end();
}
private:
StdMap &GetStdMap()
{
assert(!m_Empty);
return (StdMap&)m_MapBuffer;
}
StdMap &GetOrCreateStdMap()
{
if (m_Empty)
{
new ((void*)&m_MapBuffer) StdMap();
m_Empty = false;
}
return (StdMap&)m_MapBuffer;
}
private:
bool m_Empty;
char m_MapBuffer[sizeof(StdMap)];
};
static CIdToTrayIconMap& GetIdToTrayIconMap()
{
// This hack prevents running the destructor of our map, so it isn't problem if someone tries to reach this from a static destructor.
// Because of using MyMap this will not cause a memory leak if the user removes all items from the container before exiting.
static char id_to_tray_icon_buffer[sizeof(CIdToTrayIconMap)];
static bool initialized = false;
if (!initialized)
{
initialized = true;
new ((void*)id_to_tray_icon_buffer) CIdToTrayIconMap();
}
return (CIdToTrayIconMap&)id_to_tray_icon_buffer;
}
static UINT GetNextTrayIconId()
{
static UINT next_id = 1;
return next_id++;
}
}
static const UINT g_WndMsgTaskbarCreated = RegisterWindowMessage(TEXT("TaskbarCreated"));
LRESULT CALLBACK CTrayIcon::MessageProcessorWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == TRAY_WINDOW_MESSAGE)
{
if (CTrayIcon** ppIcon = GetIdToTrayIconMap().find((UINT)wParam))
(*ppIcon)->OnMessage((UINT)lParam);
return 0;
}
else if (uMsg == g_WndMsgTaskbarCreated)
{
CIdToTrayIconMap &id_to_tray = GetIdToTrayIconMap();
if (!id_to_tray.empty())
{
for (std::map<UINT,CTrayIcon*>::const_iterator it=id_to_tray.begin(),eit=id_to_tray.end(); it!=eit; ++it)
{
CTrayIcon *pTrayIcon = it->second;
pTrayIcon->OnTaskbarCreated();
}
}
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
HWND CTrayIcon::GetMessageProcessorHWND()
{
static HWND hWnd = NULL;
if (!hWnd)
{
static const char TRAY_ICON_MESSAGE_PROCESSOR_WND_CLASSNAME[] = "TRAY_ICON_MESSAGE_PROCESSOR_WND_CLASS";
HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
WNDCLASSEXA wc;
wc.cbSize = sizeof(wc);
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hIconSm = NULL;
wc.hInstance = hInstance;
wc.lpfnWndProc = MessageProcessorWndProc;
wc.lpszClassName = TRAY_ICON_MESSAGE_PROCESSOR_WND_CLASSNAME;
wc.lpszMenuName = NULL;
wc.style = 0;
if (!RegisterClassExA(&wc))
return NULL;
hWnd = CreateWindowExA(
0,
TRAY_ICON_MESSAGE_PROCESSOR_WND_CLASSNAME,
"TRAY_ICON_MESSAGE_PROCESSOR_WND",
WS_POPUP,
0, 0, 0, 0,
NULL,
NULL,
hInstance,
NULL
);
}
return hWnd;
}
CTrayIcon::CTrayIcon(const char* name, bool visible, HICON hIcon, bool destroy_icon_in_destructor)
: m_Id(GetNextTrayIconId())
, m_Name(name)
, m_hIcon(hIcon)
, m_Visible(false)
, m_DestroyIconInDestructor(destroy_icon_in_destructor)
, m_pOnMessageFunc(NULL)
, m_pListener(NULL)
{
GetIdToTrayIconMap()[m_Id] = this;
SetVisible(visible);
}
CTrayIcon::~CTrayIcon()
{
SetVisible(false);
SetIcon(NULL, m_DestroyIconInDestructor);
GetIdToTrayIconMap().erase(m_Id);
}
HICON CTrayIcon::InternalGetIcon() const
{
return m_hIcon ? m_hIcon : ::LoadIcon(NULL, IDI_APPLICATION);
}
bool CTrayIcon::AddIcon()
{
NOTIFYICONDATAA data;
FillNotifyIconData(data);
data.uFlags |= NIF_MESSAGE|NIF_ICON|NIF_TIP;
data.uCallbackMessage = TRAY_WINDOW_MESSAGE;
data.hIcon = InternalGetIcon();
size_t tip_len = max(sizeof(data.szTip)-1, strlen(m_Name.c_str()));
memcpy(data.szTip, m_Name.c_str(), tip_len);
data.szTip[tip_len] = 0;
return FALSE != Shell_NotifyIconA(NIM_ADD, &data);
}
bool CTrayIcon::RemoveIcon()
{
NOTIFYICONDATAA data;
FillNotifyIconData(data);
return FALSE != Shell_NotifyIconA(NIM_DELETE, &data);
}
void CTrayIcon::OnTaskbarCreated()
{
if (m_Visible)
AddIcon();
}
void CTrayIcon::SetName(const char* name)
{
m_Name = name;
if (m_Visible)
{
NOTIFYICONDATAA data;
FillNotifyIconData(data);
data.uFlags |= NIF_TIP;
size_t tip_len = max(sizeof(data.szTip)-1, strlen(name));
memcpy(data.szTip, name, tip_len);
data.szTip[tip_len] = 0;
Shell_NotifyIconA(NIM_MODIFY, &data);
}
}
bool CTrayIcon::SetVisible(bool visible)
{
if (m_Visible == visible)
return true;
m_Visible = visible;
if (m_Visible)
return AddIcon();
return RemoveIcon();
}
void CTrayIcon::SetIcon(HICON hNewIcon, bool destroy_current_icon)
{
if (m_hIcon == hNewIcon)
return;
if (destroy_current_icon && m_hIcon)
DestroyIcon(m_hIcon);
m_hIcon = hNewIcon;
if (m_Visible)
{
NOTIFYICONDATAA data;
FillNotifyIconData(data);
data.uFlags |= NIF_ICON;
data.hIcon = InternalGetIcon();
Shell_NotifyIconA(NIM_MODIFY, &data);
}
}
bool CTrayIcon::ShowBalloonTooltip(const char* title, const char* msg, ETooltipIcon icon)
{
#ifndef NOTIFYICONDATA_V2_SIZE
return false;
#else
if (!m_Visible)
return false;
#define NOTIFYICONDATA_V2_SIZE_CORRECT 488;
NOTIFYICONDATAA data;
FillNotifyIconData(data);
data.cbSize = NOTIFYICONDATA_V2_SIZE_CORRECT; // win2k and later (use hardcoded "CORRECT")
data.uFlags |= NIF_INFO;
data.dwInfoFlags = icon;
data.uTimeout = 10000; // deprecated as of Windows Vista, it has a min(10000) and max(30000) value on previous Windows versions.
size_t title_len = max(sizeof(data.szInfoTitle)-1, strlen(title));
memcpy(data.szInfoTitle, title, title_len);
data.szInfoTitle[title_len] = 0;
size_t msg_len = max(sizeof(data.szInfo)-1, strlen(msg));
memcpy(data.szInfo, msg, msg_len);
data.szInfo[msg_len] = 0;
return FALSE != Shell_NotifyIconA(NIM_MODIFY, &data);
#endif
}
void CTrayIcon::OnMessage(UINT uMsg)
{
if (m_pOnMessageFunc)
m_pOnMessageFunc(this, uMsg);
if (m_pListener)
m_pListener->OnTrayIconMessage(this, uMsg);
}
void CTrayIcon::FillNotifyIconData(NOTIFYICONDATAA& data)
{
memset(&data, 0, sizeof(data));
// the basic functions need only V1
#ifdef NOTIFYICONDATA_V1_SIZE
data.cbSize = NOTIFYICONDATA_V1_SIZE;
#else
data.cbSize = sizeof(data);
#endif
data.hWnd = GetMessageProcessorHWND();
assert(data.hWnd);
data.uID = m_Id;
}