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