]> git.saurik.com Git - wxWidgets.git/blame - src/msw/taskbar.cpp
missing commit
[wxWidgets.git] / src / msw / taskbar.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////
a71d815b 2// File: src/msw/taskbar.cpp
c42404a5 3// Purpose: Implements wxTaskBarIcon class for manipulating icons on
2bda0e17
KB
4// the Windows task bar.
5// Author: Julian Smart
1e6d9c20 6// Modified by: Vaclav Slavik
2bda0e17
KB
7// Created: 24/3/98
8// RCS-ID: $Id$
9// Copyright: (c)
65571936 10// Licence: wxWindows licence
2bda0e17
KB
11/////////////////////////////////////////////////////////////////////////
12
2bda0e17
KB
13// For compilers that support precompilation, includes "wx.h".
14#include "wx/wxprec.h"
15
16#ifdef __BORLANDC__
7520f3da 17 #pragma hdrstop
2bda0e17
KB
18#endif
19
4f167b46
VZ
20#if wxUSE_TASKBARICON
21
2bda0e17 22#ifndef WX_PRECOMP
7520f3da
WS
23 #include "wx/window.h"
24 #include "wx/frame.h"
25 #include "wx/utils.h"
26 #include "wx/menu.h"
86ad5903 27 #include "wx/app.h"
2bda0e17
KB
28#endif
29
1032aee2 30#include "wx/msw/wrapshl.h"
c25a510b 31
2bda0e17 32#include <string.h>
6af507f7 33#include "wx/taskbar.h"
142ae7b3 34#include "wx/msw/private.h"
6f9822fa 35#include "wx/dynlib.h"
2bda0e17 36
c6e72bbf
VZ
37#ifndef NIN_BALLOONTIMEOUT
38 #define NIN_BALLOONTIMEOUT 0x0404
39 #define NIN_BALLOONUSERCLICK 0x0405
40#endif
41
4ad16c5f
CE
42#ifndef NIM_SETVERSION
43 #define NIM_SETVERSION 0x00000004
44#endif
45
46#ifndef NIF_INFO
47 #define NIF_INFO 0x00000010
48#endif
49
63d690d7
VZ
50#ifndef NOTIFYICONDATA_V1_SIZE
51 #ifdef UNICODE
52 #define NOTIFYICONDATA_V1_SIZE 0x0098
53 #else
54 #define NOTIFYICONDATA_V1_SIZE 0x0058
55 #endif
56#endif
57
58#ifndef NOTIFYICONDATA_V2_SIZE
59 #ifdef UNICODE
b11b437e 60 #define NOTIFYICONDATA_V2_SIZE 0x03A8
63d690d7 61 #else
b11b437e 62 #define NOTIFYICONDATA_V2_SIZE 0x01E8
63d690d7
VZ
63 #endif
64#endif
4ad16c5f 65
4d0d77af 66// initialized on demand
22c1dcbb
VZ
67static UINT gs_msgTaskbar = 0;
68static UINT gs_msgRestartTaskbar = 0;
2bda0e17 69
56194595
RD
70
71IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
56194595 72
4d0d77af
VZ
73// ============================================================================
74// implementation
75// ============================================================================
76
6f9822fa
VZ
77// wrapper around Shell_NotifyIcon(): this function is not present in Win95
78// shell32.dll so load it dynamically to allow programs using wxTaskBarIcon to
79// start under this OS
80static BOOL wxShellNotifyIcon(DWORD dwMessage, NOTIFYICONDATA *pData)
81{
82#if wxUSE_DYNLIB_CLASS
83 typedef BOOL (WINAPI *Shell_NotifyIcon_t)(DWORD, NOTIFYICONDATA *);
84
85 static Shell_NotifyIcon_t s_pfnShell_NotifyIcon = NULL;
86 static bool s_initialized = false;
87 if ( !s_initialized )
88 {
89 s_initialized = true;
90
91 wxLogNull noLog;
92 wxDynamicLibrary dllShell("shell32.dll");
93 if ( dllShell.IsLoaded() )
94 {
d0edb9da 95 wxDL_INIT_FUNC_AW(s_pfn, Shell_NotifyIcon, dllShell);
6f9822fa
VZ
96 }
97
98 // NB: it's ok to destroy dllShell here, we link to shell32.dll
99 // implicitly so it won't be unloaded
100 }
101
102 return s_pfnShell_NotifyIcon ? (*s_pfnShell_NotifyIcon)(dwMessage, pData)
103 : FALSE;
104#else // !wxUSE_DYNLIB_CLASS
105 return Shell_NotifyIcon(dwMessage, pData);
106#endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS
107}
108
1e6d9c20
VS
109// ----------------------------------------------------------------------------
110// wxTaskBarIconWindow: helper window
111// ----------------------------------------------------------------------------
112
d3f3e7f1
VZ
113// NB: this class serves two purposes:
114// 1. win32 needs a HWND associated with taskbar icon, this provides it
115// 2. we need wxTopLevelWindow so that the app doesn't exit when
116// last frame is closed but there still is a taskbar icon
1e6d9c20
VS
117class wxTaskBarIconWindow : public wxFrame
118{
119public:
120 wxTaskBarIconWindow(wxTaskBarIcon *icon)
bfbb0b4c 121 : wxFrame(NULL, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0),
1e6d9c20
VS
122 m_icon(icon)
123 {
124 }
bfbb0b4c 125
1e6d9c20
VS
126 WXLRESULT MSWWindowProc(WXUINT msg,
127 WXWPARAM wParam, WXLPARAM lParam)
128 {
129 if (msg == gs_msgRestartTaskbar || msg == gs_msgTaskbar)
130 {
dda36afd 131 return m_icon->WindowProc(msg, wParam, lParam);
1e6d9c20
VS
132 }
133 else
134 {
135 return wxFrame::MSWWindowProc(msg, wParam, lParam);
136 }
137 }
138
139private:
140 wxTaskBarIcon *m_icon;
141};
142
bfbb0b4c 143
4d0d77af
VZ
144// ----------------------------------------------------------------------------
145// NotifyIconData: wrapper around NOTIFYICONDATA
146// ----------------------------------------------------------------------------
56194595 147
4d0d77af
VZ
148struct NotifyIconData : public NOTIFYICONDATA
149{
150 NotifyIconData(WXHWND hwnd)
151 {
152 memset(this, 0, sizeof(NOTIFYICONDATA));
63d690d7
VZ
153
154 // Do _not_ use sizeof(NOTIFYICONDATA) here, it may be too big if we're
155 // compiled with newer headers but running on an older system and while
156 // we could do complicated tests for the exact system version it's
157 // easier to just use an old size which should be supported everywhere
158 // from Windows 2000 up and which is all we need as we don't use any
159 // newer features so far. But if we're running under a really ancient
160 // system (Win9x), fall back to even smaller size -- then the balloon
161 // related features won't be available but the rest will still work.
162 cbSize = wxTheApp->GetShell32Version() >= 500
163 ? NOTIFYICONDATA_V2_SIZE
164 : NOTIFYICONDATA_V1_SIZE;
165
4d0d77af
VZ
166 hWnd = (HWND) hwnd;
167 uCallbackMessage = gs_msgTaskbar;
168 uFlags = NIF_MESSAGE;
169
170 // we use the same id for all taskbar icons as we don't need it to
171 // distinguish between them
172 uID = 99;
173 }
174};
175
176// ----------------------------------------------------------------------------
177// wxTaskBarIcon
178// ----------------------------------------------------------------------------
179
70175534 180wxTaskBarIcon::wxTaskBarIcon(wxTaskBarIconType WXUNUSED(iconType))
2bda0e17 181{
1e6d9c20 182 m_win = NULL;
04cd30de 183 m_iconAdded = false;
1e6d9c20 184 RegisterWindowMessages();
2bda0e17
KB
185}
186
4d0d77af 187wxTaskBarIcon::~wxTaskBarIcon()
2bda0e17 188{
bebe622d 189 if ( m_iconAdded )
2bda0e17 190 RemoveIcon();
2bda0e17 191
bebe622d
VZ
192 if ( m_win )
193 {
194 // we must use delete and not Destroy() here because the latter will
195 // only schedule the window to be deleted during the next idle event
196 // processing but we may not get any idle events if there are no other
197 // windows left in the program
198 delete m_win;
199 }
2bda0e17
KB
200}
201
202// Operations
203bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
204{
3103e8a9
JS
205 // NB: we have to create the window lazily because of backward compatibility,
206 // old applications may create a wxTaskBarIcon instance before wxApp
1e6d9c20
VS
207 // is initialized (as samples/taskbar used to do)
208 if (!m_win)
209 {
210 m_win = new wxTaskBarIconWindow(this);
211 }
2bda0e17 212
4d0d77af
VZ
213 m_icon = icon;
214 m_strTooltip = tooltip;
215
63b3dc58 216 NotifyIconData notifyData(GetHwndOf(m_win));
2bda0e17 217
a1b806b9 218 if (icon.IsOk())
2bda0e17 219 {
e96360ef 220 notifyData.uFlags |= NIF_ICON;
4d0d77af 221 notifyData.hIcon = GetHiconOf(icon);
2bda0e17
KB
222 }
223
b2057360
VZ
224 // set NIF_TIP even for an empty tooltip: otherwise it would be impossible
225 // to remove an existing tooltip using this function
226 notifyData.uFlags |= NIF_TIP;
4d0d77af 227 if ( !tooltip.empty() )
2bda0e17 228 {
017dc06b 229 wxStrlcpy(notifyData.szTip, tooltip.t_str(), WXSIZEOF(notifyData.szTip));
2bda0e17
KB
230 }
231
6f9822fa
VZ
232 bool ok = wxShellNotifyIcon(m_iconAdded ? NIM_MODIFY
233 : NIM_ADD, &notifyData) != 0;
2bda0e17 234
aea5b1c1
VZ
235 if ( !ok )
236 {
237 wxLogLastError(wxT("wxShellNotifyIcon(NIM_MODIFY/ADD)"));
238 }
239
4d0d77af
VZ
240 if ( !m_iconAdded && ok )
241 m_iconAdded = true;
242
243 return ok;
2bda0e17
KB
244}
245
23bd008a
VZ
246#if wxUSE_TASKBARICON_BALLOONS
247
cdcfde5d
VZ
248bool
249wxTaskBarIcon::ShowBalloon(const wxString& title,
250 const wxString& text,
251 unsigned msec,
252 int flags)
253{
254 wxCHECK_MSG( m_iconAdded, false,
9a83f860 255 wxT("can't be used before the icon is created") );
cdcfde5d 256
d3f12098
VZ
257 const HWND hwnd = GetHwndOf(m_win);
258
259 // we need to enable version 5.0 behaviour to receive notifications about
260 // the balloon disappearance
261 NotifyIconData notifyData(hwnd);
262 notifyData.uFlags = 0;
63d690d7 263 notifyData.uVersion = 3 /* NOTIFYICON_VERSION for Windows 2000/XP */;
d3f12098 264
aea5b1c1
VZ
265 if ( !wxShellNotifyIcon(NIM_SETVERSION, &notifyData) )
266 {
267 wxLogLastError(wxT("wxShellNotifyIcon(NIM_SETVERSION)"));
268 }
d3f12098
VZ
269
270 // do show the balloon now
271 notifyData = NotifyIconData(hwnd);
cdcfde5d
VZ
272 notifyData.uFlags |= NIF_INFO;
273 notifyData.uTimeout = msec;
017dc06b
VZ
274 wxStrlcpy(notifyData.szInfo, text.t_str(), WXSIZEOF(notifyData.szInfo));
275 wxStrlcpy(notifyData.szInfoTitle, title.t_str(),
cdcfde5d
VZ
276 WXSIZEOF(notifyData.szInfoTitle));
277
278 if ( flags & wxICON_INFORMATION )
279 notifyData.dwInfoFlags |= NIIF_INFO;
280 else if ( flags & wxICON_WARNING )
281 notifyData.dwInfoFlags |= NIIF_WARNING;
282 else if ( flags & wxICON_ERROR )
283 notifyData.dwInfoFlags |= NIIF_ERROR;
284
aea5b1c1
VZ
285 bool ok = wxShellNotifyIcon(NIM_MODIFY, &notifyData) != 0;
286 if ( !ok )
287 {
288 wxLogLastError(wxT("wxShellNotifyIcon(NIM_MODIFY)"));
289 }
290
291 return ok;
cdcfde5d
VZ
292}
293
23bd008a
VZ
294#endif // wxUSE_TASKBARICON_BALLOONS
295
4d0d77af 296bool wxTaskBarIcon::RemoveIcon()
2bda0e17
KB
297{
298 if (!m_iconAdded)
04cd30de 299 return false;
2bda0e17 300
04cd30de 301 m_iconAdded = false;
2bda0e17 302
63b3dc58 303 NotifyIconData notifyData(GetHwndOf(m_win));
4d0d77af 304
aea5b1c1
VZ
305 bool ok = wxShellNotifyIcon(NIM_DELETE, &notifyData) != 0;
306 if ( !ok )
307 {
308 wxLogLastError(wxT("wxShellNotifyIcon(NIM_DELETE)"));
309 }
310
311 return ok;
2bda0e17
KB
312}
313
53a118d6 314#if wxUSE_MENUS
4d0d77af 315bool wxTaskBarIcon::PopupMenu(wxMenu *menu)
69ecd30f 316{
9a83f860 317 wxASSERT_MSG( m_win != NULL, wxT("taskbar icon not initialized") );
1e6d9c20 318
04cd30de 319 static bool s_inPopup = false;
c7527e3f
JS
320
321 if (s_inPopup)
04cd30de 322 return false;
c7527e3f 323
04cd30de 324 s_inPopup = true;
c7527e3f 325
69ecd30f
RD
326 int x, y;
327 wxGetMousePosition(&x, &y);
328
1e6d9c20 329 m_win->Move(x, y);
bfbb0b4c 330
1e6d9c20 331 m_win->PushEventHandler(this);
d66d9d5b 332
50bcd1ae
JS
333 menu->UpdateUI();
334
63b3dc58
VZ
335 // the SetForegroundWindow() and PostMessage() calls are needed to work
336 // around Win32 bug with the popup menus shown for the notifications as
337 // documented at http://support.microsoft.com/kb/q135788/
338 ::SetForegroundWindow(GetHwndOf(m_win));
f6bcfd97 339
1e6d9c20 340 bool rval = m_win->PopupMenu(menu, 0, 0);
69ecd30f 341
63b3dc58 342 ::PostMessage(GetHwndOf(m_win), WM_NULL, 0, 0L);
f6bcfd97 343
1e6d9c20 344 m_win->PopEventHandler(false);
c7527e3f 345
04cd30de 346 s_inPopup = false;
d66d9d5b 347
69ecd30f
RD
348 return rval;
349}
53a118d6 350#endif // wxUSE_MENUS
69ecd30f 351
1e6d9c20 352void wxTaskBarIcon::RegisterWindowMessages()
2bda0e17 353{
4d0d77af 354 static bool s_registered = false;
2bda0e17 355
1e6d9c20 356 if ( !s_registered )
bfbb0b4c 357 {
1e6d9c20
VS
358 // Taskbar restart msg will be sent to us if the icon needs to be redrawn
359 gs_msgRestartTaskbar = RegisterWindowMessage(wxT("TaskbarCreated"));
2bda0e17 360
1e6d9c20
VS
361 // Also register the taskbar message here
362 gs_msgTaskbar = ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
2bda0e17 363
1e6d9c20
VS
364 s_registered = true;
365 }
2bda0e17
KB
366}
367
4d0d77af
VZ
368// ----------------------------------------------------------------------------
369// wxTaskBarIcon window proc
370// ----------------------------------------------------------------------------
371
dda36afd
VS
372long wxTaskBarIcon::WindowProc(unsigned int msg,
373 unsigned int WXUNUSED(wParam),
4d0d77af 374 long lParam)
2bda0e17 375{
d3f12098 376 if ( msg == gs_msgRestartTaskbar ) // does the icon need to be redrawn?
4d0d77af
VZ
377 {
378 m_iconAdded = false;
379 SetIcon(m_icon, m_strTooltip);
ce4f741c 380 return 0;
4d0d77af
VZ
381 }
382
1e6d9c20 383 // this function should only be called for gs_msg(Restart)Taskbar messages
d3f12098 384 wxASSERT( msg == gs_msgTaskbar );
2bda0e17 385
d3f12098
VZ
386 wxEventType eventType = 0;
387 switch ( lParam )
2bda0e17 388 {
c42404a5 389 case WM_LBUTTONDOWN:
56194595
RD
390 eventType = wxEVT_TASKBAR_LEFT_DOWN;
391 break;
2bda0e17 392
c42404a5 393 case WM_LBUTTONUP:
56194595
RD
394 eventType = wxEVT_TASKBAR_LEFT_UP;
395 break;
2bda0e17 396
c42404a5 397 case WM_RBUTTONDOWN:
56194595
RD
398 eventType = wxEVT_TASKBAR_RIGHT_DOWN;
399 break;
2bda0e17 400
c42404a5 401 case WM_RBUTTONUP:
56194595
RD
402 eventType = wxEVT_TASKBAR_RIGHT_UP;
403 break;
2bda0e17 404
c42404a5 405 case WM_LBUTTONDBLCLK:
56194595
RD
406 eventType = wxEVT_TASKBAR_LEFT_DCLICK;
407 break;
2bda0e17 408
c42404a5 409 case WM_RBUTTONDBLCLK:
56194595
RD
410 eventType = wxEVT_TASKBAR_RIGHT_DCLICK;
411 break;
2bda0e17 412
c42404a5 413 case WM_MOUSEMOVE:
56194595
RD
414 eventType = wxEVT_TASKBAR_MOVE;
415 break;
2bda0e17 416
d3f12098
VZ
417 case NIN_BALLOONTIMEOUT:
418 eventType = wxEVT_TASKBAR_BALLOON_TIMEOUT;
419 break;
420
421 case NIN_BALLOONUSERCLICK:
422 eventType = wxEVT_TASKBAR_BALLOON_CLICK;
56194595 423 break;
4d0d77af 424 }
56194595 425
d3f12098 426 if ( eventType )
4d0d77af 427 {
fa1c12bd 428 wxTaskBarIconEvent event(eventType, this);
56194595
RD
429
430 ProcessEvent(event);
431 }
4d0d77af 432
2bda0e17
KB
433 return 0;
434}
4f167b46
VZ
435
436#endif // wxUSE_TASKBARICON
437