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