]> git.saurik.com Git - wxWidgets.git/blame - src/msw/taskbar.cpp
wxExecute may only be called from the main thread
[wxWidgets.git] / src / msw / taskbar.cpp
CommitLineData
2bda0e17
KB
1/////////////////////////////////////////////////////////////////////////
2// File: taskbar.cpp
c42404a5 3// Purpose: Implements wxTaskBarIcon class for manipulating icons on
2bda0e17
KB
4// the Windows task bar.
5// Author: Julian Smart
6// Modified by:
7// Created: 24/3/98
8// RCS-ID: $Id$
9// Copyright: (c)
c42404a5 10// Licence: wxWindows licence
2bda0e17
KB
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"
ad5c34f3
JS
26#include "wx/window.h"
27#include "wx/frame.h"
28#include "wx/utils.h"
0ae2df30 29#include "wx/menu.h"
2bda0e17
KB
30#endif
31
57c208c5 32#if defined(__WIN95__) && !defined(__TWIN32__)
2bda0e17 33
3d05544e 34#include <windows.h>
c25a510b
JS
35
36#include "wx/msw/winundef.h"
37
2bda0e17 38#include <string.h>
3096bd2f
VZ
39#include "wx/msw/taskbar.h"
40#include "wx/msw/private.h"
2bda0e17 41
57c208c5 42#ifndef __TWIN32__
c42404a5
VZ
43 #ifdef __GNUWIN32_OLD__
44 #include "wx/msw/gnuwin32/extra.h"
45 #endif
65fd5cb0 46#endif
2bda0e17 47
ce3ed50d 48#ifdef __SALFORDC__
c42404a5 49 #include <shellapi.h>
ce3ed50d
JS
50#endif
51
d162a7ee
VZ
52#include "wx/listimpl.cpp"
53WX_DEFINE_LIST(wxTaskBarIconList);
54
2bda0e17 55LRESULT APIENTRY _EXPORT wxTaskBarIconWindowProc( HWND hWnd, unsigned msg,
c42404a5 56 UINT wParam, LONG lParam );
2bda0e17 57
ba14d986 58wxChar *wxTaskBarWindowClass = (wxChar*) wxT("wxTaskBarWindowClass");
2bda0e17 59
d162a7ee
VZ
60wxTaskBarIconList wxTaskBarIcon::sm_taskBarIcons;
61bool wxTaskBarIcon::sm_registeredClass = FALSE;
2bda0e17
KB
62UINT wxTaskBarIcon::sm_taskbarMsg = 0;
63
459e74ba
MB
64DEFINE_EVENT_TYPE( wxEVT_TASKBAR_MOVE )
65DEFINE_EVENT_TYPE( wxEVT_TASKBAR_LEFT_DOWN )
66DEFINE_EVENT_TYPE( wxEVT_TASKBAR_LEFT_UP )
67DEFINE_EVENT_TYPE( wxEVT_TASKBAR_RIGHT_DOWN )
68DEFINE_EVENT_TYPE( wxEVT_TASKBAR_RIGHT_UP )
69DEFINE_EVENT_TYPE( wxEVT_TASKBAR_LEFT_DCLICK )
70DEFINE_EVENT_TYPE( wxEVT_TASKBAR_RIGHT_DCLICK )
56194595 71
56194595 72BEGIN_EVENT_TABLE(wxTaskBarIcon, wxEvtHandler)
69ecd30f
RD
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)
56194595
RD
80END_EVENT_TABLE()
81
82
83IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
56194595
RD
84
85
2bda0e17
KB
86wxTaskBarIcon::wxTaskBarIcon(void)
87{
88 m_hWnd = 0;
04cd30de 89 m_iconAdded = false;
2bda0e17
KB
90
91 AddObject(this);
92
93 if (RegisterWindowClass())
94 m_hWnd = CreateTaskBarWindow();
95}
96
97wxTaskBarIcon::~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
114bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
115{
116 if (!IsOK())
04cd30de 117 return false;
2bda0e17
KB
118
119 NOTIFYICONDATA notifyData;
120
121 memset(&notifyData, 0, sizeof(notifyData));
c42404a5
VZ
122 notifyData.cbSize = sizeof(notifyData);
123 notifyData.hWnd = (HWND) m_hWnd;
124 notifyData.uCallbackMessage = sm_taskbarMsg;
125 notifyData.uFlags = NIF_MESSAGE ;
e96360ef 126 if (icon.Ok())
2bda0e17 127 {
e96360ef
JS
128 notifyData.uFlags |= NIF_ICON;
129 notifyData.hIcon = (HICON) icon.GetHICON();
2bda0e17
KB
130 }
131
223d09f6 132 if (((const wxChar*) tooltip != NULL) && (tooltip != wxT("")))
2bda0e17
KB
133 {
134 notifyData.uFlags |= NIF_TIP ;
c42404a5 135 lstrcpyn(notifyData.szTip, WXSTRINGCAST tooltip, sizeof(notifyData.szTip));
2bda0e17
KB
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
149bool wxTaskBarIcon::RemoveIcon(void)
150{
151 if (!m_iconAdded)
04cd30de 152 return false;
2bda0e17
KB
153
154 NOTIFYICONDATA notifyData;
155
156 memset(&notifyData, 0, sizeof(notifyData));
c42404a5
VZ
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;
2bda0e17 162 notifyData.uID = 99;
04cd30de 163 m_iconAdded = false;
2bda0e17
KB
164
165 return (Shell_NotifyIcon(NIM_DELETE, & notifyData) != 0);
166}
167
69ecd30f
RD
168bool wxTaskBarIcon::PopupMenu(wxMenu *menu) //, int x, int y);
169{
c7527e3f
JS
170 // OK, so I know this isn't thread-friendly, but
171 // what to do? We need this check.
172
04cd30de 173 static bool s_inPopup = false;
c7527e3f
JS
174
175 if (s_inPopup)
04cd30de 176 return false;
c7527e3f 177
04cd30de 178 s_inPopup = true;
c7527e3f 179
04cd30de 180 bool rval = false;
69ecd30f
RD
181 wxWindow* win;
182 int x, y;
183 wxGetMousePosition(&x, &y);
184
185 // is wxFrame the best window type to use???
2b5f62a0 186 win = new wxFrame(NULL, -1, wxEmptyString, wxPoint(x,y), wxSize(-1,-1), 0);
69ecd30f
RD
187 win->PushEventHandler(this);
188
d66d9d5b
JS
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
50bcd1ae
JS
193 menu->UpdateUI();
194
f6bcfd97
BP
195 // Work around a WIN32 bug
196 ::SetForegroundWindow ((HWND) win->GetHWND ());
197
69ecd30f
RD
198 rval = win->PopupMenu(menu, 0, 0);
199
f6bcfd97
BP
200 // Work around a WIN32 bug
201 ::PostMessage ((HWND) win->GetHWND(),WM_NULL,0,0L);
202
04cd30de 203 win->PopEventHandler(false);
50c319be 204 win->Destroy();
c7527e3f
JS
205 delete win;
206
04cd30de 207 s_inPopup = false;
d66d9d5b 208
69ecd30f
RD
209 return rval;
210}
211
212
2bda0e17 213// Overridables
56194595 214void wxTaskBarIcon::OnMouseMove(wxEvent&)
2bda0e17
KB
215{
216}
217
56194595 218void wxTaskBarIcon::OnLButtonDown(wxEvent&)
2bda0e17
KB
219{
220}
221
56194595 222void wxTaskBarIcon::OnLButtonUp(wxEvent&)
2bda0e17
KB
223{
224}
225
56194595 226void wxTaskBarIcon::OnRButtonDown(wxEvent&)
2bda0e17
KB
227{
228}
229
56194595 230void wxTaskBarIcon::OnRButtonUp(wxEvent&)
2bda0e17
KB
231{
232}
233
56194595 234void wxTaskBarIcon::OnLButtonDClick(wxEvent&)
2bda0e17
KB
235{
236}
237
56194595 238void wxTaskBarIcon::OnRButtonDClick(wxEvent&)
2bda0e17
KB
239{
240}
241
69ecd30f
RD
242void wxTaskBarIcon::_OnMouseMove(wxEvent& e) { OnMouseMove(e); }
243void wxTaskBarIcon::_OnLButtonDown(wxEvent& e) { OnLButtonDown(e); }
244void wxTaskBarIcon::_OnLButtonUp(wxEvent& e) { OnLButtonUp(e); }
245void wxTaskBarIcon::_OnRButtonDown(wxEvent& e) { OnRButtonDown(e); }
246void wxTaskBarIcon::_OnRButtonUp(wxEvent& e) { OnRButtonUp(e); }
247void wxTaskBarIcon::_OnLButtonDClick(wxEvent& e) { OnLButtonDClick(e); }
248void wxTaskBarIcon::_OnRButtonDClick(wxEvent& e) { OnRButtonDClick(e); }
249
250
2bda0e17
KB
251wxTaskBarIcon* wxTaskBarIcon::FindObjectForHWND(WXHWND hWnd)
252{
d162a7ee 253 wxTaskBarIconList::Node *node = sm_taskBarIcons.GetFirst();
2bda0e17
KB
254 while (node)
255 {
d162a7ee 256 wxTaskBarIcon *obj = node->GetData();
2bda0e17
KB
257 if (obj->GetHWND() == hWnd)
258 return obj;
04cd30de 259 node = node->GetNext();
2bda0e17
KB
260 }
261 return NULL;
262}
263
264void wxTaskBarIcon::AddObject(wxTaskBarIcon* obj)
265{
266 sm_taskBarIcons.Append(obj);
267}
268
269void wxTaskBarIcon::RemoveObject(wxTaskBarIcon* obj)
270{
271 sm_taskBarIcons.DeleteObject(obj);
272}
273
274bool wxTaskBarIcon::RegisterWindowClass()
275{
276 if (sm_registeredClass)
04cd30de 277 return true;
2bda0e17
KB
278
279 // Also register the taskbar message here
223d09f6 280 sm_taskbarMsg = ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
2bda0e17 281
c42404a5
VZ
282 WNDCLASS wc;
283 bool rc;
2bda0e17
KB
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
307WXHWND wxTaskBarIcon::CreateTaskBarWindow()
308{
309 HINSTANCE hInstance = GetModuleHandle(NULL);
310
311 HWND hWnd = CreateWindowEx (0, wxTaskBarWindowClass,
223d09f6 312 wxT("wxTaskBarWindow"),
2bda0e17
KB
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
326long wxTaskBarIcon::WindowProc( WXHWND hWnd, unsigned int msg, unsigned int wParam, long lParam )
327{
56194595
RD
328 wxEventType eventType = 0;
329
2bda0e17
KB
330 if (msg != sm_taskbarMsg)
331 return DefWindowProc((HWND) hWnd, msg, wParam, lParam);
332
333 switch (lParam)
334 {
c42404a5 335 case WM_LBUTTONDOWN:
56194595
RD
336 eventType = wxEVT_TASKBAR_LEFT_DOWN;
337 break;
2bda0e17 338
c42404a5 339 case WM_LBUTTONUP:
56194595
RD
340 eventType = wxEVT_TASKBAR_LEFT_UP;
341 break;
2bda0e17 342
c42404a5 343 case WM_RBUTTONDOWN:
56194595
RD
344 eventType = wxEVT_TASKBAR_RIGHT_DOWN;
345 break;
2bda0e17 346
c42404a5 347 case WM_RBUTTONUP:
56194595
RD
348 eventType = wxEVT_TASKBAR_RIGHT_UP;
349 break;
2bda0e17 350
c42404a5 351 case WM_LBUTTONDBLCLK:
56194595
RD
352 eventType = wxEVT_TASKBAR_LEFT_DCLICK;
353 break;
2bda0e17 354
c42404a5 355 case WM_RBUTTONDBLCLK:
56194595
RD
356 eventType = wxEVT_TASKBAR_RIGHT_DCLICK;
357 break;
2bda0e17 358
c42404a5 359 case WM_MOUSEMOVE:
56194595
RD
360 eventType = wxEVT_TASKBAR_MOVE;
361 break;
2bda0e17 362
c42404a5 363 default:
56194595 364 break;
c42404a5 365 }
56194595
RD
366
367 if (eventType) {
fa1c12bd 368 wxTaskBarIconEvent event(eventType, this);
56194595
RD
369
370 ProcessEvent(event);
371 }
2bda0e17
KB
372 return 0;
373}
374
375LRESULT APIENTRY _EXPORT wxTaskBarIconWindowProc( HWND hWnd, unsigned msg,
c42404a5 376 UINT wParam, LONG lParam )
2bda0e17 377{
d162a7ee 378 wxTaskBarIcon *obj = wxTaskBarIcon::FindObjectForHWND((WXHWND) hWnd);
2bda0e17
KB
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__