]> git.saurik.com Git - wxWidgets.git/blob - src/msw/taskbar.cpp
workarounded false memory leak report
[wxWidgets.git] / src / msw / taskbar.cpp
1 /////////////////////////////////////////////////////////////////////////
2 // File: taskbar.cpp
3 // Purpose: Implements wxTaskBarIcon class for manipulating icons on
4 // the Windows task bar.
5 // Author: Julian Smart
6 // Modified by:
7 // Created: 24/3/98
8 // RCS-ID: $Id$
9 // Copyright: (c)
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////
12
13 #ifdef __GNUG__
14 #pragma implementation "taskbar.h"
15 #endif
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/defs.h"
26 #include "wx/window.h"
27 #include "wx/frame.h"
28 #include "wx/utils.h"
29 #include "wx/menu.h"
30 #endif
31
32 #if defined(__WIN95__) && !defined(__TWIN32__)
33
34 #include <windows.h>
35
36 #include "wx/msw/winundef.h"
37
38 #include <string.h>
39 #include "wx/msw/taskbar.h"
40 #include "wx/msw/private.h"
41
42 #ifndef __TWIN32__
43 #ifdef __GNUWIN32_OLD__
44 #include "wx/msw/gnuwin32/extra.h"
45 #endif
46 #endif
47
48 #ifdef __SALFORDC__
49 #include <shellapi.h>
50 #endif
51
52 LRESULT APIENTRY _EXPORT wxTaskBarIconWindowProc( HWND hWnd, unsigned msg,
53 UINT wParam, LONG lParam );
54
55 wxChar *wxTaskBarWindowClass = wxT("wxTaskBarWindowClass");
56
57 wxList wxTaskBarIcon::sm_taskBarIcons;
58 bool wxTaskBarIcon::sm_registeredClass = FALSE;
59 UINT wxTaskBarIcon::sm_taskbarMsg = 0;
60
61
62 BEGIN_EVENT_TABLE(wxTaskBarIcon, wxEvtHandler)
63 EVT_TASKBAR_MOVE (wxTaskBarIcon::_OnMouseMove)
64 EVT_TASKBAR_LEFT_DOWN (wxTaskBarIcon::_OnLButtonDown)
65 EVT_TASKBAR_LEFT_UP (wxTaskBarIcon::_OnLButtonUp)
66 EVT_TASKBAR_RIGHT_DOWN (wxTaskBarIcon::_OnRButtonDown)
67 EVT_TASKBAR_RIGHT_UP (wxTaskBarIcon::_OnRButtonUp)
68 EVT_TASKBAR_LEFT_DCLICK (wxTaskBarIcon::_OnLButtonDClick)
69 EVT_TASKBAR_RIGHT_DCLICK (wxTaskBarIcon::_OnRButtonDClick)
70 END_EVENT_TABLE()
71
72
73 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
74
75
76 wxTaskBarIcon::wxTaskBarIcon(void)
77 {
78 m_hWnd = 0;
79 m_iconAdded = FALSE;
80
81 AddObject(this);
82
83 if (RegisterWindowClass())
84 m_hWnd = CreateTaskBarWindow();
85 }
86
87 wxTaskBarIcon::~wxTaskBarIcon(void)
88 {
89 RemoveObject(this);
90
91 if (m_iconAdded)
92 {
93 RemoveIcon();
94 }
95
96 if (m_hWnd)
97 {
98 ::DestroyWindow((HWND) m_hWnd);
99 m_hWnd = 0;
100 }
101 }
102
103 // Operations
104 bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
105 {
106 if (!IsOK())
107 return FALSE;
108
109 NOTIFYICONDATA notifyData;
110
111 memset(&notifyData, 0, sizeof(notifyData));
112 notifyData.cbSize = sizeof(notifyData);
113 notifyData.hWnd = (HWND) m_hWnd;
114 notifyData.uCallbackMessage = sm_taskbarMsg;
115 notifyData.uFlags = NIF_MESSAGE ;
116 if (icon.Ok())
117 {
118 notifyData.uFlags |= NIF_ICON;
119 notifyData.hIcon = (HICON) icon.GetHICON();
120 }
121
122 if (((const wxChar*) tooltip != NULL) && (tooltip != wxT("")))
123 {
124 notifyData.uFlags |= NIF_TIP ;
125 lstrcpyn(notifyData.szTip, WXSTRINGCAST tooltip, sizeof(notifyData.szTip));
126 }
127
128 notifyData.uID = 99;
129
130 if (m_iconAdded)
131 return (Shell_NotifyIcon(NIM_MODIFY, & notifyData) != 0);
132 else
133 {
134 m_iconAdded = (Shell_NotifyIcon(NIM_ADD, & notifyData) != 0);
135 return m_iconAdded;
136 }
137 }
138
139 bool wxTaskBarIcon::RemoveIcon(void)
140 {
141 if (!m_iconAdded)
142 return FALSE;
143
144 NOTIFYICONDATA notifyData;
145
146 memset(&notifyData, 0, sizeof(notifyData));
147 notifyData.cbSize = sizeof(notifyData);
148 notifyData.hWnd = (HWND) m_hWnd;
149 notifyData.uCallbackMessage = sm_taskbarMsg;
150 notifyData.uFlags = NIF_MESSAGE;
151 notifyData.hIcon = 0 ; // hIcon;
152 notifyData.uID = 99;
153 m_iconAdded = FALSE;
154
155 return (Shell_NotifyIcon(NIM_DELETE, & notifyData) != 0);
156 }
157
158 bool wxTaskBarIcon::PopupMenu(wxMenu *menu) //, int x, int y);
159 {
160 // OK, so I know this isn't thread-friendly, but
161 // what to do? We need this check.
162
163 static bool s_inPopup = FALSE;
164
165 if (s_inPopup)
166 return FALSE;
167
168 s_inPopup = TRUE;
169
170 bool rval = FALSE;
171 wxWindow* win;
172 int x, y;
173 wxGetMousePosition(&x, &y);
174
175 // is wxFrame the best window type to use???
176 win = new wxFrame(NULL, -1, "", wxPoint(x,y), wxSize(-1,-1), 0);
177 win->PushEventHandler(this);
178
179 // Remove from record of top-level windows, or will confuse wxWindows
180 // if we try to exit right now.
181 wxTopLevelWindows.DeleteObject(win);
182
183 menu->UpdateUI();
184
185 // Work around a WIN32 bug
186 ::SetForegroundWindow ((HWND) win->GetHWND ());
187
188 rval = win->PopupMenu(menu, 0, 0);
189
190 // Work around a WIN32 bug
191 ::PostMessage ((HWND) win->GetHWND(),WM_NULL,0,0L);
192
193 win->PopEventHandler(FALSE);
194 win->Destroy();
195 delete win;
196
197 s_inPopup = FALSE;
198
199 return rval;
200 }
201
202
203 // Overridables
204 void wxTaskBarIcon::OnMouseMove(wxEvent&)
205 {
206 }
207
208 void wxTaskBarIcon::OnLButtonDown(wxEvent&)
209 {
210 }
211
212 void wxTaskBarIcon::OnLButtonUp(wxEvent&)
213 {
214 }
215
216 void wxTaskBarIcon::OnRButtonDown(wxEvent&)
217 {
218 }
219
220 void wxTaskBarIcon::OnRButtonUp(wxEvent&)
221 {
222 }
223
224 void wxTaskBarIcon::OnLButtonDClick(wxEvent&)
225 {
226 }
227
228 void wxTaskBarIcon::OnRButtonDClick(wxEvent&)
229 {
230 }
231
232 void wxTaskBarIcon::_OnMouseMove(wxEvent& e) { OnMouseMove(e); }
233 void wxTaskBarIcon::_OnLButtonDown(wxEvent& e) { OnLButtonDown(e); }
234 void wxTaskBarIcon::_OnLButtonUp(wxEvent& e) { OnLButtonUp(e); }
235 void wxTaskBarIcon::_OnRButtonDown(wxEvent& e) { OnRButtonDown(e); }
236 void wxTaskBarIcon::_OnRButtonUp(wxEvent& e) { OnRButtonUp(e); }
237 void wxTaskBarIcon::_OnLButtonDClick(wxEvent& e) { OnLButtonDClick(e); }
238 void wxTaskBarIcon::_OnRButtonDClick(wxEvent& e) { OnRButtonDClick(e); }
239
240
241 wxTaskBarIcon* wxTaskBarIcon::FindObjectForHWND(WXHWND hWnd)
242 {
243 wxNode*node = sm_taskBarIcons.First();
244 while (node)
245 {
246 wxTaskBarIcon* obj = (wxTaskBarIcon*) node->Data();
247 if (obj->GetHWND() == hWnd)
248 return obj;
249 node = node->Next();
250 }
251 return NULL;
252 }
253
254 void wxTaskBarIcon::AddObject(wxTaskBarIcon* obj)
255 {
256 sm_taskBarIcons.Append(obj);
257 }
258
259 void wxTaskBarIcon::RemoveObject(wxTaskBarIcon* obj)
260 {
261 sm_taskBarIcons.DeleteObject(obj);
262 }
263
264 bool wxTaskBarIcon::RegisterWindowClass()
265 {
266 if (sm_registeredClass)
267 return TRUE;
268
269 // Also register the taskbar message here
270 sm_taskbarMsg = ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
271
272 WNDCLASS wc;
273 bool rc;
274
275 HINSTANCE hInstance = GetModuleHandle(NULL);
276
277 /*
278 * set up and register window class
279 */
280 wc.style = CS_HREDRAW | CS_VREDRAW;
281 wc.lpfnWndProc = (WNDPROC) wxTaskBarIconWindowProc;
282 wc.cbClsExtra = 0;
283 wc.cbWndExtra = 0;
284 wc.hInstance = hInstance;
285 wc.hIcon = 0;
286 wc.hCursor = 0;
287 wc.hbrBackground = 0;
288 wc.lpszMenuName = NULL;
289 wc.lpszClassName = wxTaskBarWindowClass ;
290 rc = (::RegisterClass( &wc ) != 0);
291
292 sm_registeredClass = (rc != 0);
293
294 return( (rc != 0) );
295 }
296
297 WXHWND wxTaskBarIcon::CreateTaskBarWindow()
298 {
299 HINSTANCE hInstance = GetModuleHandle(NULL);
300
301 HWND hWnd = CreateWindowEx (0, wxTaskBarWindowClass,
302 wxT("wxTaskBarWindow"),
303 WS_OVERLAPPED,
304 0,
305 0,
306 10,
307 10,
308 NULL,
309 (HMENU) 0,
310 hInstance,
311 NULL);
312
313 return (WXHWND) hWnd;
314 }
315
316 long wxTaskBarIcon::WindowProc( WXHWND hWnd, unsigned int msg, unsigned int wParam, long lParam )
317 {
318 wxEventType eventType = 0;
319
320 if (msg != sm_taskbarMsg)
321 return DefWindowProc((HWND) hWnd, msg, wParam, lParam);
322
323 switch (lParam)
324 {
325 case WM_LBUTTONDOWN:
326 eventType = wxEVT_TASKBAR_LEFT_DOWN;
327 break;
328
329 case WM_LBUTTONUP:
330 eventType = wxEVT_TASKBAR_LEFT_UP;
331 break;
332
333 case WM_RBUTTONDOWN:
334 eventType = wxEVT_TASKBAR_RIGHT_DOWN;
335 break;
336
337 case WM_RBUTTONUP:
338 eventType = wxEVT_TASKBAR_RIGHT_UP;
339 break;
340
341 case WM_LBUTTONDBLCLK:
342 eventType = wxEVT_TASKBAR_LEFT_DCLICK;
343 break;
344
345 case WM_RBUTTONDBLCLK:
346 eventType = wxEVT_TASKBAR_RIGHT_DCLICK;
347 break;
348
349 case WM_MOUSEMOVE:
350 eventType = wxEVT_TASKBAR_MOVE;
351 break;
352
353 default:
354 break;
355 }
356
357 if (eventType) {
358 wxEvent event;
359 event.SetEventType(eventType);
360 event.SetEventObject(this);
361
362 ProcessEvent(event);
363 }
364 return 0;
365 }
366
367 LRESULT APIENTRY _EXPORT wxTaskBarIconWindowProc( HWND hWnd, unsigned msg,
368 UINT wParam, LONG lParam )
369 {
370 wxTaskBarIcon* obj = wxTaskBarIcon::FindObjectForHWND((WXHWND) hWnd);
371 if (obj)
372 return obj->WindowProc((WXHWND) hWnd, msg, wParam, lParam);
373 else
374 return DefWindowProc(hWnd, msg, wParam, lParam);
375 }
376
377 #endif
378 // __WIN95__