]> git.saurik.com Git - wxWidgets.git/blob - src/msw/taskbar.cpp
6cd962b79cb8bdd18ccfe9954ce1d8ba7f729150
[wxWidgets.git] / src / msw / taskbar.cpp
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
67 static UINT gs_msgTaskbar = 0;
68 static UINT gs_msgRestartTaskbar = 0;
69
70
71 IMPLEMENT_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
80 static 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 // We need a HWND to create a taskbar icon, so create a special hidden window
114 // just to be able to use its HWND.
115 class wxTaskBarIconWindow : public wxFrame
116 {
117 public:
118 wxTaskBarIconWindow(wxTaskBarIcon *icon)
119 : wxFrame(NULL, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0),
120 m_icon(icon)
121 {
122 }
123
124 // This implicitly created window shouldn't prevent the application from
125 // exiting if all its other windows are closed.
126 virtual bool ShouldPreventAppExit() const { return false; }
127
128 protected:
129 WXLRESULT MSWWindowProc(WXUINT msg,
130 WXWPARAM wParam, WXLPARAM lParam)
131 {
132 if (msg == gs_msgRestartTaskbar || msg == gs_msgTaskbar)
133 {
134 return m_icon->WindowProc(msg, wParam, lParam);
135 }
136 else
137 {
138 return wxFrame::MSWWindowProc(msg, wParam, lParam);
139 }
140 }
141
142 private:
143 wxTaskBarIcon *m_icon;
144 };
145
146
147 // ----------------------------------------------------------------------------
148 // NotifyIconData: wrapper around NOTIFYICONDATA
149 // ----------------------------------------------------------------------------
150
151 struct NotifyIconData : public NOTIFYICONDATA
152 {
153 NotifyIconData(WXHWND hwnd)
154 {
155 memset(this, 0, sizeof(NOTIFYICONDATA));
156
157 // Do _not_ use sizeof(NOTIFYICONDATA) here, it may be too big if we're
158 // compiled with newer headers but running on an older system and while
159 // we could do complicated tests for the exact system version it's
160 // easier to just use an old size which should be supported everywhere
161 // from Windows 2000 up and which is all we need as we don't use any
162 // newer features so far. But if we're running under a really ancient
163 // system (Win9x), fall back to even smaller size -- then the balloon
164 // related features won't be available but the rest will still work.
165 cbSize = wxTheApp->GetShell32Version() >= 500
166 ? NOTIFYICONDATA_V2_SIZE
167 : NOTIFYICONDATA_V1_SIZE;
168
169 hWnd = (HWND) hwnd;
170 uCallbackMessage = gs_msgTaskbar;
171 uFlags = NIF_MESSAGE;
172
173 // we use the same id for all taskbar icons as we don't need it to
174 // distinguish between them
175 uID = 99;
176 }
177 };
178
179 // ----------------------------------------------------------------------------
180 // wxTaskBarIcon
181 // ----------------------------------------------------------------------------
182
183 wxTaskBarIcon::wxTaskBarIcon(wxTaskBarIconType WXUNUSED(iconType))
184 {
185 m_win = NULL;
186 m_iconAdded = false;
187 RegisterWindowMessages();
188 }
189
190 wxTaskBarIcon::~wxTaskBarIcon()
191 {
192 if ( m_iconAdded )
193 RemoveIcon();
194
195 if ( m_win )
196 {
197 // we must use delete and not Destroy() here because the latter will
198 // only schedule the window to be deleted during the next idle event
199 // processing but we may not get any idle events if there are no other
200 // windows left in the program
201 delete m_win;
202 }
203 }
204
205 // Operations
206 bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
207 {
208 // NB: we have to create the window lazily because of backward compatibility,
209 // old applications may create a wxTaskBarIcon instance before wxApp
210 // is initialized (as samples/taskbar used to do)
211 if (!m_win)
212 {
213 m_win = new wxTaskBarIconWindow(this);
214 }
215
216 m_icon = icon;
217 m_strTooltip = tooltip;
218
219 NotifyIconData notifyData(GetHwndOf(m_win));
220
221 if (icon.IsOk())
222 {
223 notifyData.uFlags |= NIF_ICON;
224 notifyData.hIcon = GetHiconOf(icon);
225 }
226
227 // set NIF_TIP even for an empty tooltip: otherwise it would be impossible
228 // to remove an existing tooltip using this function
229 notifyData.uFlags |= NIF_TIP;
230 if ( !tooltip.empty() )
231 {
232 wxStrlcpy(notifyData.szTip, tooltip.t_str(), WXSIZEOF(notifyData.szTip));
233 }
234
235 bool ok = wxShellNotifyIcon(m_iconAdded ? NIM_MODIFY
236 : NIM_ADD, &notifyData) != 0;
237
238 if ( !ok )
239 {
240 wxLogLastError(wxT("wxShellNotifyIcon(NIM_MODIFY/ADD)"));
241 }
242
243 if ( !m_iconAdded && ok )
244 m_iconAdded = true;
245
246 return ok;
247 }
248
249 #if wxUSE_TASKBARICON_BALLOONS
250
251 bool
252 wxTaskBarIcon::ShowBalloon(const wxString& title,
253 const wxString& text,
254 unsigned msec,
255 int flags)
256 {
257 wxCHECK_MSG( m_iconAdded, false,
258 wxT("can't be used before the icon is created") );
259
260 const HWND hwnd = GetHwndOf(m_win);
261
262 // we need to enable version 5.0 behaviour to receive notifications about
263 // the balloon disappearance
264 NotifyIconData notifyData(hwnd);
265 notifyData.uFlags = 0;
266 notifyData.uVersion = 3 /* NOTIFYICON_VERSION for Windows 2000/XP */;
267
268 if ( !wxShellNotifyIcon(NIM_SETVERSION, &notifyData) )
269 {
270 wxLogLastError(wxT("wxShellNotifyIcon(NIM_SETVERSION)"));
271 }
272
273 // do show the balloon now
274 notifyData = NotifyIconData(hwnd);
275 notifyData.uFlags |= NIF_INFO;
276 notifyData.uTimeout = msec;
277 wxStrlcpy(notifyData.szInfo, text.t_str(), WXSIZEOF(notifyData.szInfo));
278 wxStrlcpy(notifyData.szInfoTitle, title.t_str(),
279 WXSIZEOF(notifyData.szInfoTitle));
280
281 if ( flags & wxICON_INFORMATION )
282 notifyData.dwInfoFlags |= NIIF_INFO;
283 else if ( flags & wxICON_WARNING )
284 notifyData.dwInfoFlags |= NIIF_WARNING;
285 else if ( flags & wxICON_ERROR )
286 notifyData.dwInfoFlags |= NIIF_ERROR;
287
288 bool ok = wxShellNotifyIcon(NIM_MODIFY, &notifyData) != 0;
289 if ( !ok )
290 {
291 wxLogLastError(wxT("wxShellNotifyIcon(NIM_MODIFY)"));
292 }
293
294 return ok;
295 }
296
297 #endif // wxUSE_TASKBARICON_BALLOONS
298
299 bool wxTaskBarIcon::RemoveIcon()
300 {
301 if (!m_iconAdded)
302 return false;
303
304 m_iconAdded = false;
305
306 NotifyIconData notifyData(GetHwndOf(m_win));
307
308 bool ok = wxShellNotifyIcon(NIM_DELETE, &notifyData) != 0;
309 if ( !ok )
310 {
311 wxLogLastError(wxT("wxShellNotifyIcon(NIM_DELETE)"));
312 }
313
314 return ok;
315 }
316
317 #if wxUSE_MENUS
318 bool wxTaskBarIcon::PopupMenu(wxMenu *menu)
319 {
320 wxASSERT_MSG( m_win != NULL, wxT("taskbar icon not initialized") );
321
322 static bool s_inPopup = false;
323
324 if (s_inPopup)
325 return false;
326
327 s_inPopup = true;
328
329 int x, y;
330 wxGetMousePosition(&x, &y);
331
332 m_win->Move(x, y);
333
334 m_win->PushEventHandler(this);
335
336 menu->UpdateUI();
337
338 // the SetForegroundWindow() and PostMessage() calls are needed to work
339 // around Win32 bug with the popup menus shown for the notifications as
340 // documented at http://support.microsoft.com/kb/q135788/
341 ::SetForegroundWindow(GetHwndOf(m_win));
342
343 bool rval = m_win->PopupMenu(menu, 0, 0);
344
345 ::PostMessage(GetHwndOf(m_win), WM_NULL, 0, 0L);
346
347 m_win->PopEventHandler(false);
348
349 s_inPopup = false;
350
351 return rval;
352 }
353 #endif // wxUSE_MENUS
354
355 void wxTaskBarIcon::RegisterWindowMessages()
356 {
357 static bool s_registered = false;
358
359 if ( !s_registered )
360 {
361 // Taskbar restart msg will be sent to us if the icon needs to be redrawn
362 gs_msgRestartTaskbar = RegisterWindowMessage(wxT("TaskbarCreated"));
363
364 // Also register the taskbar message here
365 gs_msgTaskbar = ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
366
367 s_registered = true;
368 }
369 }
370
371 // ----------------------------------------------------------------------------
372 // wxTaskBarIcon window proc
373 // ----------------------------------------------------------------------------
374
375 long wxTaskBarIcon::WindowProc(unsigned int msg,
376 unsigned int WXUNUSED(wParam),
377 long lParam)
378 {
379 if ( msg == gs_msgRestartTaskbar ) // does the icon need to be redrawn?
380 {
381 m_iconAdded = false;
382 SetIcon(m_icon, m_strTooltip);
383 return 0;
384 }
385
386 // this function should only be called for gs_msg(Restart)Taskbar messages
387 wxASSERT( msg == gs_msgTaskbar );
388
389 wxEventType eventType = 0;
390 switch ( lParam )
391 {
392 case WM_LBUTTONDOWN:
393 eventType = wxEVT_TASKBAR_LEFT_DOWN;
394 break;
395
396 case WM_LBUTTONUP:
397 eventType = wxEVT_TASKBAR_LEFT_UP;
398 break;
399
400 case WM_RBUTTONDOWN:
401 eventType = wxEVT_TASKBAR_RIGHT_DOWN;
402 break;
403
404 case WM_RBUTTONUP:
405 eventType = wxEVT_TASKBAR_RIGHT_UP;
406 break;
407
408 case WM_LBUTTONDBLCLK:
409 eventType = wxEVT_TASKBAR_LEFT_DCLICK;
410 break;
411
412 case WM_RBUTTONDBLCLK:
413 eventType = wxEVT_TASKBAR_RIGHT_DCLICK;
414 break;
415
416 case WM_MOUSEMOVE:
417 eventType = wxEVT_TASKBAR_MOVE;
418 break;
419
420 case NIN_BALLOONTIMEOUT:
421 eventType = wxEVT_TASKBAR_BALLOON_TIMEOUT;
422 break;
423
424 case NIN_BALLOONUSERCLICK:
425 eventType = wxEVT_TASKBAR_BALLOON_CLICK;
426 break;
427 }
428
429 if ( eventType )
430 {
431 wxTaskBarIconEvent event(eventType, this);
432
433 ProcessEvent(event);
434 }
435
436 return 0;
437 }
438
439 #endif // wxUSE_TASKBARICON
440