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