]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/taskbar.cpp
Bumped the reference manual date.
[wxWidgets.git] / src / msw / taskbar.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////
2// File: src/msw/taskbar.cpp
3// Purpose: Implements wxTaskBarIcon class for manipulating icons on
4// the Windows task bar.
5// Author: Julian Smart
6// Modified by: Vaclav Slavik
7// Created: 24/3/98
8// RCS-ID: $Id$
9// Copyright: (c)
10// Licence: wxWindows licence
11/////////////////////////////////////////////////////////////////////////
12
13// For compilers that support precompilation, includes "wx.h".
14#include "wx/wxprec.h"
15
16#ifdef __BORLANDC__
17 #pragma hdrstop
18#endif
19
20#if wxUSE_TASKBARICON
21
22#ifndef WX_PRECOMP
23 #include "wx/window.h"
24 #include "wx/frame.h"
25 #include "wx/utils.h"
26 #include "wx/menu.h"
27 #include "wx/app.h"
28#endif
29
30#include "wx/msw/wrapshl.h"
31
32#include <string.h>
33#include "wx/taskbar.h"
34#include "wx/msw/private.h"
35#include "wx/dynlib.h"
36
37#ifndef NIN_BALLOONTIMEOUT
38 #define NIN_BALLOONTIMEOUT 0x0404
39 #define NIN_BALLOONUSERCLICK 0x0405
40#endif
41
42#ifndef NIM_SETVERSION
43 #define NIM_SETVERSION 0x00000004
44#endif
45
46#ifndef NIF_INFO
47 #define NIF_INFO 0x00000010
48#endif
49
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
60 #define NOTIFYICONDATA_V2_SIZE 0x03A8
61 #else
62 #define NOTIFYICONDATA_V2_SIZE 0x01E8
63 #endif
64#endif
65
66// initialized on demand
67static UINT gs_msgTaskbar = 0;
68static UINT gs_msgRestartTaskbar = 0;
69
70
71IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
72
73// ============================================================================
74// implementation
75// ============================================================================
76
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 {
95 wxDL_INIT_FUNC_AW(s_pfn, Shell_NotifyIcon, dllShell);
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
109// ----------------------------------------------------------------------------
110// wxTaskBarIconWindow: helper window
111// ----------------------------------------------------------------------------
112
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
117class wxTaskBarIconWindow : public wxFrame
118{
119public:
120 wxTaskBarIconWindow(wxTaskBarIcon *icon)
121 : wxFrame(NULL, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0),
122 m_icon(icon)
123 {
124 }
125
126 WXLRESULT MSWWindowProc(WXUINT msg,
127 WXWPARAM wParam, WXLPARAM lParam)
128 {
129 if (msg == gs_msgRestartTaskbar || msg == gs_msgTaskbar)
130 {
131 return m_icon->WindowProc(msg, wParam, lParam);
132 }
133 else
134 {
135 return wxFrame::MSWWindowProc(msg, wParam, lParam);
136 }
137 }
138
139private:
140 wxTaskBarIcon *m_icon;
141};
142
143
144// ----------------------------------------------------------------------------
145// NotifyIconData: wrapper around NOTIFYICONDATA
146// ----------------------------------------------------------------------------
147
148struct NotifyIconData : public NOTIFYICONDATA
149{
150 NotifyIconData(WXHWND hwnd)
151 {
152 memset(this, 0, sizeof(NOTIFYICONDATA));
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
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
180wxTaskBarIcon::wxTaskBarIcon(wxTaskBarIconType WXUNUSED(iconType))
181{
182 m_win = NULL;
183 m_iconAdded = false;
184 RegisterWindowMessages();
185}
186
187wxTaskBarIcon::~wxTaskBarIcon()
188{
189 if ( m_iconAdded )
190 RemoveIcon();
191
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 }
200}
201
202// Operations
203bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
204{
205 // NB: we have to create the window lazily because of backward compatibility,
206 // old applications may create a wxTaskBarIcon instance before wxApp
207 // is initialized (as samples/taskbar used to do)
208 if (!m_win)
209 {
210 m_win = new wxTaskBarIconWindow(this);
211 }
212
213 m_icon = icon;
214 m_strTooltip = tooltip;
215
216 NotifyIconData notifyData(GetHwndOf(m_win));
217
218 if (icon.IsOk())
219 {
220 notifyData.uFlags |= NIF_ICON;
221 notifyData.hIcon = GetHiconOf(icon);
222 }
223
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;
227 if ( !tooltip.empty() )
228 {
229 wxStrlcpy(notifyData.szTip, tooltip.t_str(), WXSIZEOF(notifyData.szTip));
230 }
231
232 bool ok = wxShellNotifyIcon(m_iconAdded ? NIM_MODIFY
233 : NIM_ADD, &notifyData) != 0;
234
235 if ( !ok )
236 {
237 wxLogLastError(wxT("wxShellNotifyIcon(NIM_MODIFY/ADD)"));
238 }
239
240 if ( !m_iconAdded && ok )
241 m_iconAdded = true;
242
243 return ok;
244}
245
246#if wxUSE_TASKBARICON_BALLOONS
247
248bool
249wxTaskBarIcon::ShowBalloon(const wxString& title,
250 const wxString& text,
251 unsigned msec,
252 int flags)
253{
254 wxCHECK_MSG( m_iconAdded, false,
255 wxT("can't be used before the icon is created") );
256
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;
263 notifyData.uVersion = 3 /* NOTIFYICON_VERSION for Windows 2000/XP */;
264
265 if ( !wxShellNotifyIcon(NIM_SETVERSION, &notifyData) )
266 {
267 wxLogLastError(wxT("wxShellNotifyIcon(NIM_SETVERSION)"));
268 }
269
270 // do show the balloon now
271 notifyData = NotifyIconData(hwnd);
272 notifyData.uFlags |= NIF_INFO;
273 notifyData.uTimeout = msec;
274 wxStrlcpy(notifyData.szInfo, text.t_str(), WXSIZEOF(notifyData.szInfo));
275 wxStrlcpy(notifyData.szInfoTitle, title.t_str(),
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
285 bool ok = wxShellNotifyIcon(NIM_MODIFY, &notifyData) != 0;
286 if ( !ok )
287 {
288 wxLogLastError(wxT("wxShellNotifyIcon(NIM_MODIFY)"));
289 }
290
291 return ok;
292}
293
294#endif // wxUSE_TASKBARICON_BALLOONS
295
296bool wxTaskBarIcon::RemoveIcon()
297{
298 if (!m_iconAdded)
299 return false;
300
301 m_iconAdded = false;
302
303 NotifyIconData notifyData(GetHwndOf(m_win));
304
305 bool ok = wxShellNotifyIcon(NIM_DELETE, &notifyData) != 0;
306 if ( !ok )
307 {
308 wxLogLastError(wxT("wxShellNotifyIcon(NIM_DELETE)"));
309 }
310
311 return ok;
312}
313
314#if wxUSE_MENUS
315bool wxTaskBarIcon::PopupMenu(wxMenu *menu)
316{
317 wxASSERT_MSG( m_win != NULL, wxT("taskbar icon not initialized") );
318
319 static bool s_inPopup = false;
320
321 if (s_inPopup)
322 return false;
323
324 s_inPopup = true;
325
326 int x, y;
327 wxGetMousePosition(&x, &y);
328
329 m_win->Move(x, y);
330
331 m_win->PushEventHandler(this);
332
333 menu->UpdateUI();
334
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));
339
340 bool rval = m_win->PopupMenu(menu, 0, 0);
341
342 ::PostMessage(GetHwndOf(m_win), WM_NULL, 0, 0L);
343
344 m_win->PopEventHandler(false);
345
346 s_inPopup = false;
347
348 return rval;
349}
350#endif // wxUSE_MENUS
351
352void wxTaskBarIcon::RegisterWindowMessages()
353{
354 static bool s_registered = false;
355
356 if ( !s_registered )
357 {
358 // Taskbar restart msg will be sent to us if the icon needs to be redrawn
359 gs_msgRestartTaskbar = RegisterWindowMessage(wxT("TaskbarCreated"));
360
361 // Also register the taskbar message here
362 gs_msgTaskbar = ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
363
364 s_registered = true;
365 }
366}
367
368// ----------------------------------------------------------------------------
369// wxTaskBarIcon window proc
370// ----------------------------------------------------------------------------
371
372long wxTaskBarIcon::WindowProc(unsigned int msg,
373 unsigned int WXUNUSED(wParam),
374 long lParam)
375{
376 if ( msg == gs_msgRestartTaskbar ) // does the icon need to be redrawn?
377 {
378 m_iconAdded = false;
379 SetIcon(m_icon, m_strTooltip);
380 return 0;
381 }
382
383 // this function should only be called for gs_msg(Restart)Taskbar messages
384 wxASSERT( msg == gs_msgTaskbar );
385
386 wxEventType eventType = 0;
387 switch ( lParam )
388 {
389 case WM_LBUTTONDOWN:
390 eventType = wxEVT_TASKBAR_LEFT_DOWN;
391 break;
392
393 case WM_LBUTTONUP:
394 eventType = wxEVT_TASKBAR_LEFT_UP;
395 break;
396
397 case WM_RBUTTONDOWN:
398 eventType = wxEVT_TASKBAR_RIGHT_DOWN;
399 break;
400
401 case WM_RBUTTONUP:
402 eventType = wxEVT_TASKBAR_RIGHT_UP;
403 break;
404
405 case WM_LBUTTONDBLCLK:
406 eventType = wxEVT_TASKBAR_LEFT_DCLICK;
407 break;
408
409 case WM_RBUTTONDBLCLK:
410 eventType = wxEVT_TASKBAR_RIGHT_DCLICK;
411 break;
412
413 case WM_MOUSEMOVE:
414 eventType = wxEVT_TASKBAR_MOVE;
415 break;
416
417 case NIN_BALLOONTIMEOUT:
418 eventType = wxEVT_TASKBAR_BALLOON_TIMEOUT;
419 break;
420
421 case NIN_BALLOONUSERCLICK:
422 eventType = wxEVT_TASKBAR_BALLOON_CLICK;
423 break;
424 }
425
426 if ( eventType )
427 {
428 wxTaskBarIconEvent event(eventType, this);
429
430 ProcessEvent(event);
431 }
432
433 return 0;
434}
435
436#endif // wxUSE_TASKBARICON
437