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