]> git.saurik.com Git - wxWidgets.git/blame - src/msw/taskbar.cpp
signed/unsigned comparison warning fixed
[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
b39dbf34 32#if defined(__WIN95__)
2bda0e17 33
3d05544e 34#include <windows.h>
c25a510b
JS
35
36#include "wx/msw/winundef.h"
37
2bda0e17 38#include <string.h>
6af507f7 39#include "wx/taskbar.h"
3096bd2f 40#include "wx/msw/private.h"
2bda0e17 41
b39dbf34
JS
42#ifdef __GNUWIN32_OLD__
43 #include "wx/msw/gnuwin32/extra.h"
65fd5cb0 44#endif
2bda0e17 45
ce3ed50d 46#ifdef __SALFORDC__
c42404a5 47 #include <shellapi.h>
ce3ed50d
JS
48#endif
49
d162a7ee
VZ
50#include "wx/listimpl.cpp"
51WX_DEFINE_LIST(wxTaskBarIconList);
52
4d0d77af
VZ
53LRESULT APIENTRY _EXPORT
54wxTaskBarIconWindowProc( HWND hWnd, unsigned msg, UINT wParam, LONG lParam );
2bda0e17 55
ba14d986 56wxChar *wxTaskBarWindowClass = (wxChar*) wxT("wxTaskBarWindowClass");
2bda0e17 57
d162a7ee 58wxTaskBarIconList wxTaskBarIcon::sm_taskBarIcons;
4d0d77af
VZ
59
60// initialized on demand
61UINT gs_msgTaskbar = 0;
62UINT gs_msgRestartTaskbar = 0;
2bda0e17 63
6af507f7 64#if WXWIN_COMPATIBILITY_2_4
56194595 65BEGIN_EVENT_TABLE(wxTaskBarIcon, wxEvtHandler)
69ecd30f
RD
66 EVT_TASKBAR_MOVE (wxTaskBarIcon::_OnMouseMove)
67 EVT_TASKBAR_LEFT_DOWN (wxTaskBarIcon::_OnLButtonDown)
68 EVT_TASKBAR_LEFT_UP (wxTaskBarIcon::_OnLButtonUp)
69 EVT_TASKBAR_RIGHT_DOWN (wxTaskBarIcon::_OnRButtonDown)
70 EVT_TASKBAR_RIGHT_UP (wxTaskBarIcon::_OnRButtonUp)
71 EVT_TASKBAR_LEFT_DCLICK (wxTaskBarIcon::_OnLButtonDClick)
72 EVT_TASKBAR_RIGHT_DCLICK (wxTaskBarIcon::_OnRButtonDClick)
56194595 73END_EVENT_TABLE()
6af507f7 74#endif
56194595
RD
75
76
77IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
56194595 78
4d0d77af
VZ
79// ============================================================================
80// implementation
81// ============================================================================
82
83// ----------------------------------------------------------------------------
84// NotifyIconData: wrapper around NOTIFYICONDATA
85// ----------------------------------------------------------------------------
56194595 86
4d0d77af
VZ
87struct NotifyIconData : public NOTIFYICONDATA
88{
89 NotifyIconData(WXHWND hwnd)
90 {
91 memset(this, 0, sizeof(NOTIFYICONDATA));
92 cbSize = sizeof(NOTIFYICONDATA);
93 hWnd = (HWND) hwnd;
94 uCallbackMessage = gs_msgTaskbar;
95 uFlags = NIF_MESSAGE;
96
97 // we use the same id for all taskbar icons as we don't need it to
98 // distinguish between them
99 uID = 99;
100 }
101};
102
103// ----------------------------------------------------------------------------
104// wxTaskBarIcon
105// ----------------------------------------------------------------------------
106
107wxTaskBarIcon::wxTaskBarIcon()
2bda0e17
KB
108{
109 m_hWnd = 0;
04cd30de 110 m_iconAdded = false;
2bda0e17
KB
111
112 AddObject(this);
113
114 if (RegisterWindowClass())
115 m_hWnd = CreateTaskBarWindow();
116}
117
4d0d77af 118wxTaskBarIcon::~wxTaskBarIcon()
2bda0e17
KB
119{
120 RemoveObject(this);
121
122 if (m_iconAdded)
123 {
124 RemoveIcon();
125 }
126
127 if (m_hWnd)
128 {
129 ::DestroyWindow((HWND) m_hWnd);
130 m_hWnd = 0;
131 }
132}
133
134// Operations
135bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
136{
6af507f7 137 if (!IsOk())
04cd30de 138 return false;
2bda0e17 139
4d0d77af
VZ
140 m_icon = icon;
141 m_strTooltip = tooltip;
142
143 NotifyIconData notifyData(m_hWnd);
2bda0e17 144
e96360ef 145 if (icon.Ok())
2bda0e17 146 {
e96360ef 147 notifyData.uFlags |= NIF_ICON;
4d0d77af 148 notifyData.hIcon = GetHiconOf(icon);
2bda0e17
KB
149 }
150
4d0d77af 151 if ( !tooltip.empty() )
2bda0e17 152 {
4d0d77af
VZ
153 notifyData.uFlags |= NIF_TIP;
154 lstrcpyn(notifyData.szTip, tooltip.c_str(), WXSIZEOF(notifyData.szTip));
2bda0e17
KB
155 }
156
4d0d77af
VZ
157 bool ok = Shell_NotifyIcon(m_iconAdded ? NIM_MODIFY
158 : NIM_ADD, &notifyData) != 0;
2bda0e17 159
4d0d77af
VZ
160 if ( !m_iconAdded && ok )
161 m_iconAdded = true;
162
163 return ok;
2bda0e17
KB
164}
165
4d0d77af 166bool wxTaskBarIcon::RemoveIcon()
2bda0e17
KB
167{
168 if (!m_iconAdded)
04cd30de 169 return false;
2bda0e17 170
04cd30de 171 m_iconAdded = false;
2bda0e17 172
4d0d77af
VZ
173 NotifyIconData notifyData(m_hWnd);
174
175 return Shell_NotifyIcon(NIM_DELETE, &notifyData) != 0;
2bda0e17
KB
176}
177
4d0d77af 178bool wxTaskBarIcon::PopupMenu(wxMenu *menu)
69ecd30f 179{
04cd30de 180 static bool s_inPopup = false;
c7527e3f
JS
181
182 if (s_inPopup)
04cd30de 183 return false;
c7527e3f 184
04cd30de 185 s_inPopup = true;
c7527e3f 186
04cd30de 187 bool rval = false;
69ecd30f
RD
188 wxWindow* win;
189 int x, y;
190 wxGetMousePosition(&x, &y);
191
192 // is wxFrame the best window type to use???
2b5f62a0 193 win = new wxFrame(NULL, -1, wxEmptyString, wxPoint(x,y), wxSize(-1,-1), 0);
69ecd30f
RD
194 win->PushEventHandler(this);
195
d66d9d5b
JS
196 // Remove from record of top-level windows, or will confuse wxWindows
197 // if we try to exit right now.
198 wxTopLevelWindows.DeleteObject(win);
199
50bcd1ae
JS
200 menu->UpdateUI();
201
f6bcfd97
BP
202 // Work around a WIN32 bug
203 ::SetForegroundWindow ((HWND) win->GetHWND ());
204
69ecd30f
RD
205 rval = win->PopupMenu(menu, 0, 0);
206
f6bcfd97
BP
207 // Work around a WIN32 bug
208 ::PostMessage ((HWND) win->GetHWND(),WM_NULL,0,0L);
209
04cd30de 210 win->PopEventHandler(false);
50c319be 211 win->Destroy();
c7527e3f
JS
212 delete win;
213
04cd30de 214 s_inPopup = false;
d66d9d5b 215
69ecd30f
RD
216 return rval;
217}
218
6af507f7 219#if WXWIN_COMPATIBILITY_2_4
2bda0e17 220// Overridables
6af507f7
VS
221void wxTaskBarIcon::OnMouseMove(wxEvent&) {}
222void wxTaskBarIcon::OnLButtonDown(wxEvent&) {}
223void wxTaskBarIcon::OnLButtonUp(wxEvent&) {}
224void wxTaskBarIcon::OnRButtonDown(wxEvent&) {}
225void wxTaskBarIcon::OnRButtonUp(wxEvent&) {}
226void wxTaskBarIcon::OnLButtonDClick(wxEvent&) {}
227void wxTaskBarIcon::OnRButtonDClick(wxEvent&) {}
2bda0e17 228
69ecd30f
RD
229void wxTaskBarIcon::_OnMouseMove(wxEvent& e) { OnMouseMove(e); }
230void wxTaskBarIcon::_OnLButtonDown(wxEvent& e) { OnLButtonDown(e); }
231void wxTaskBarIcon::_OnLButtonUp(wxEvent& e) { OnLButtonUp(e); }
232void wxTaskBarIcon::_OnRButtonDown(wxEvent& e) { OnRButtonDown(e); }
233void wxTaskBarIcon::_OnRButtonUp(wxEvent& e) { OnRButtonUp(e); }
234void wxTaskBarIcon::_OnLButtonDClick(wxEvent& e) { OnLButtonDClick(e); }
235void wxTaskBarIcon::_OnRButtonDClick(wxEvent& e) { OnRButtonDClick(e); }
6af507f7 236#endif
69ecd30f 237
2bda0e17
KB
238wxTaskBarIcon* wxTaskBarIcon::FindObjectForHWND(WXHWND hWnd)
239{
d162a7ee 240 wxTaskBarIconList::Node *node = sm_taskBarIcons.GetFirst();
2bda0e17
KB
241 while (node)
242 {
d162a7ee 243 wxTaskBarIcon *obj = node->GetData();
2bda0e17
KB
244 if (obj->GetHWND() == hWnd)
245 return obj;
04cd30de 246 node = node->GetNext();
2bda0e17
KB
247 }
248 return NULL;
249}
250
251void wxTaskBarIcon::AddObject(wxTaskBarIcon* obj)
252{
253 sm_taskBarIcons.Append(obj);
254}
255
256void wxTaskBarIcon::RemoveObject(wxTaskBarIcon* obj)
257{
258 sm_taskBarIcons.DeleteObject(obj);
259}
260
261bool wxTaskBarIcon::RegisterWindowClass()
262{
4d0d77af 263 static bool s_registered = false;
2bda0e17 264
4d0d77af
VZ
265 if ( s_registered )
266 return true;
2bda0e17 267
4d0d77af
VZ
268 // Taskbar restart msg will be sent to us if the icon needs to be redrawn
269 gs_msgRestartTaskbar = RegisterWindowMessage(wxT("TaskbarCreated"));
2bda0e17 270
4d0d77af
VZ
271 // Also register the taskbar message here
272 gs_msgTaskbar = ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
2bda0e17 273
4d0d77af
VZ
274 // set up and register window class
275 WNDCLASS wc;
2bda0e17
KB
276 wc.style = CS_HREDRAW | CS_VREDRAW;
277 wc.lpfnWndProc = (WNDPROC) wxTaskBarIconWindowProc;
278 wc.cbClsExtra = 0;
279 wc.cbWndExtra = 0;
4d0d77af 280 wc.hInstance = wxGetInstance();
2bda0e17
KB
281 wc.hIcon = 0;
282 wc.hCursor = 0;
283 wc.hbrBackground = 0;
284 wc.lpszMenuName = NULL;
4d0d77af
VZ
285 wc.lpszClassName = wxTaskBarWindowClass;
286
287 if ( !::RegisterClass(&wc) )
288 {
289 wxLogLastError(_T("RegisterClass(taskbar icon)"));
290
291 return false;
292 }
2bda0e17 293
4d0d77af 294 s_registered = true;
2bda0e17 295
4d0d77af 296 return true;
2bda0e17
KB
297}
298
299WXHWND wxTaskBarIcon::CreateTaskBarWindow()
300{
301 HINSTANCE hInstance = GetModuleHandle(NULL);
302
303 HWND hWnd = CreateWindowEx (0, wxTaskBarWindowClass,
223d09f6 304 wxT("wxTaskBarWindow"),
2bda0e17
KB
305 WS_OVERLAPPED,
306 0,
307 0,
308 10,
309 10,
310 NULL,
311 (HMENU) 0,
312 hInstance,
313 NULL);
314
315 return (WXHWND) hWnd;
316}
317
4d0d77af
VZ
318// ----------------------------------------------------------------------------
319// wxTaskBarIcon window proc
320// ----------------------------------------------------------------------------
321
322long wxTaskBarIcon::WindowProc(WXHWND hWnd,
323 unsigned int msg,
324 unsigned int wParam,
325 long lParam)
2bda0e17 326{
56194595
RD
327 wxEventType eventType = 0;
328
4d0d77af
VZ
329 if (msg == gs_msgRestartTaskbar) // does the icon need to be redrawn?
330 {
331 m_iconAdded = false;
332 SetIcon(m_icon, m_strTooltip);
333 }
334
335 if (msg != gs_msgTaskbar)
2bda0e17
KB
336 return DefWindowProc((HWND) hWnd, msg, wParam, lParam);
337
338 switch (lParam)
339 {
c42404a5 340 case WM_LBUTTONDOWN:
56194595
RD
341 eventType = wxEVT_TASKBAR_LEFT_DOWN;
342 break;
2bda0e17 343
c42404a5 344 case WM_LBUTTONUP:
56194595
RD
345 eventType = wxEVT_TASKBAR_LEFT_UP;
346 break;
2bda0e17 347
c42404a5 348 case WM_RBUTTONDOWN:
56194595
RD
349 eventType = wxEVT_TASKBAR_RIGHT_DOWN;
350 break;
2bda0e17 351
c42404a5 352 case WM_RBUTTONUP:
56194595
RD
353 eventType = wxEVT_TASKBAR_RIGHT_UP;
354 break;
2bda0e17 355
c42404a5 356 case WM_LBUTTONDBLCLK:
56194595
RD
357 eventType = wxEVT_TASKBAR_LEFT_DCLICK;
358 break;
2bda0e17 359
c42404a5 360 case WM_RBUTTONDBLCLK:
56194595
RD
361 eventType = wxEVT_TASKBAR_RIGHT_DCLICK;
362 break;
2bda0e17 363
c42404a5 364 case WM_MOUSEMOVE:
56194595
RD
365 eventType = wxEVT_TASKBAR_MOVE;
366 break;
2bda0e17 367
c42404a5 368 default:
56194595 369 break;
4d0d77af 370 }
56194595 371
4d0d77af
VZ
372 if (eventType)
373 {
fa1c12bd 374 wxTaskBarIconEvent event(eventType, this);
56194595
RD
375
376 ProcessEvent(event);
377 }
4d0d77af 378
2bda0e17
KB
379 return 0;
380}
381
4d0d77af
VZ
382LRESULT APIENTRY _EXPORT
383wxTaskBarIconWindowProc(HWND hWnd, unsigned msg, UINT wParam, LONG lParam)
2bda0e17 384{
d162a7ee 385 wxTaskBarIcon *obj = wxTaskBarIcon::FindObjectForHWND((WXHWND) hWnd);
2bda0e17
KB
386 if (obj)
387 return obj->WindowProc((WXHWND) hWnd, msg, wParam, lParam);
388 else
389 return DefWindowProc(hWnd, msg, wParam, lParam);
390}
391
392#endif
393 // __WIN95__