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
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////
13 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
23 #include "wx/window.h"
29 #include "wx/msw/wrapshl.h"
32 #include "wx/taskbar.h"
33 #include "wx/dynlib.h"
35 #ifndef NIN_BALLOONTIMEOUT
36 #define NIN_BALLOONTIMEOUT 0x0404
37 #define NIN_BALLOONUSERCLICK 0x0405
40 #ifndef NIM_SETVERSION
41 #define NIM_SETVERSION 0x00000004
45 #define NIF_INFO 0x00000010
48 #ifndef NOTIFYICONDATA_V1_SIZE
50 #define NOTIFYICONDATA_V1_SIZE 0x0098
52 #define NOTIFYICONDATA_V1_SIZE 0x0058
56 #ifndef NOTIFYICONDATA_V2_SIZE
58 #define NOTIFYICONDATA_V2_SIZE 0x03A8;
60 #define NOTIFYICONDATA_V2_SIZE 0x01E8;
64 // initialized on demand
65 static UINT gs_msgTaskbar
= 0;
66 static UINT gs_msgRestartTaskbar
= 0;
69 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
71 // ============================================================================
73 // ============================================================================
75 // wrapper around Shell_NotifyIcon(): this function is not present in Win95
76 // shell32.dll so load it dynamically to allow programs using wxTaskBarIcon to
77 // start under this OS
78 static BOOL
wxShellNotifyIcon(DWORD dwMessage
, NOTIFYICONDATA
*pData
)
80 #if wxUSE_DYNLIB_CLASS
81 typedef BOOL (WINAPI
*Shell_NotifyIcon_t
)(DWORD
, NOTIFYICONDATA
*);
83 static Shell_NotifyIcon_t s_pfnShell_NotifyIcon
= NULL
;
84 static bool s_initialized
= false;
90 wxDynamicLibrary
dllShell("shell32.dll");
91 if ( dllShell
.IsLoaded() )
93 wxDL_INIT_FUNC_AW(s_pfn
, Shell_NotifyIcon
, dllShell
);
96 // NB: it's ok to destroy dllShell here, we link to shell32.dll
97 // implicitly so it won't be unloaded
100 return s_pfnShell_NotifyIcon
? (*s_pfnShell_NotifyIcon
)(dwMessage
, pData
)
102 #else // !wxUSE_DYNLIB_CLASS
103 return Shell_NotifyIcon(dwMessage
, pData
);
104 #endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS
107 // ----------------------------------------------------------------------------
108 // wxTaskBarIconWindow: helper window
109 // ----------------------------------------------------------------------------
111 // NB: this class serves two purposes:
112 // 1. win32 needs a HWND associated with taskbar icon, this provides it
113 // 2. we need wxTopLevelWindow so that the app doesn't exit when
114 // last frame is closed but there still is a taskbar icon
115 class wxTaskBarIconWindow
: public wxFrame
118 wxTaskBarIconWindow(wxTaskBarIcon
*icon
)
119 : wxFrame(NULL
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
, 0),
124 WXLRESULT
MSWWindowProc(WXUINT msg
,
125 WXWPARAM wParam
, WXLPARAM lParam
)
127 if (msg
== gs_msgRestartTaskbar
|| msg
== gs_msgTaskbar
)
129 return m_icon
->WindowProc(msg
, wParam
, lParam
);
133 return wxFrame::MSWWindowProc(msg
, wParam
, lParam
);
138 wxTaskBarIcon
*m_icon
;
142 // ----------------------------------------------------------------------------
143 // NotifyIconData: wrapper around NOTIFYICONDATA
144 // ----------------------------------------------------------------------------
146 struct NotifyIconData
: public NOTIFYICONDATA
148 NotifyIconData(WXHWND hwnd
)
150 memset(this, 0, sizeof(NOTIFYICONDATA
));
152 // Do _not_ use sizeof(NOTIFYICONDATA) here, it may be too big if we're
153 // compiled with newer headers but running on an older system and while
154 // we could do complicated tests for the exact system version it's
155 // easier to just use an old size which should be supported everywhere
156 // from Windows 2000 up and which is all we need as we don't use any
157 // newer features so far. But if we're running under a really ancient
158 // system (Win9x), fall back to even smaller size -- then the balloon
159 // related features won't be available but the rest will still work.
160 cbSize
= wxTheApp
->GetShell32Version() >= 500
161 ? NOTIFYICONDATA_V2_SIZE
162 : NOTIFYICONDATA_V1_SIZE
;
165 uCallbackMessage
= gs_msgTaskbar
;
166 uFlags
= NIF_MESSAGE
;
168 // we use the same id for all taskbar icons as we don't need it to
169 // distinguish between them
174 // ----------------------------------------------------------------------------
176 // ----------------------------------------------------------------------------
178 wxTaskBarIcon::wxTaskBarIcon()
182 RegisterWindowMessages();
185 wxTaskBarIcon::~wxTaskBarIcon()
192 // we must use delete and not Destroy() here because the latter will
193 // only schedule the window to be deleted during the next idle event
194 // processing but we may not get any idle events if there are no other
195 // windows left in the program
201 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
203 // NB: we have to create the window lazily because of backward compatibility,
204 // old applications may create a wxTaskBarIcon instance before wxApp
205 // is initialized (as samples/taskbar used to do)
208 m_win
= new wxTaskBarIconWindow(this);
212 m_strTooltip
= tooltip
;
214 NotifyIconData
notifyData(GetHwndOf(m_win
));
218 notifyData
.uFlags
|= NIF_ICON
;
219 notifyData
.hIcon
= GetHiconOf(icon
);
222 // set NIF_TIP even for an empty tooltip: otherwise it would be impossible
223 // to remove an existing tooltip using this function
224 notifyData
.uFlags
|= NIF_TIP
;
225 if ( !tooltip
.empty() )
227 wxStrlcpy(notifyData
.szTip
, tooltip
.wx_str(), WXSIZEOF(notifyData
.szTip
));
230 bool ok
= wxShellNotifyIcon(m_iconAdded
? NIM_MODIFY
231 : NIM_ADD
, ¬ifyData
) != 0;
235 wxLogLastError(wxT("wxShellNotifyIcon(NIM_MODIFY/ADD)"));
238 if ( !m_iconAdded
&& ok
)
244 #if wxUSE_TASKBARICON_BALLOONS
247 wxTaskBarIcon::ShowBalloon(const wxString
& title
,
248 const wxString
& text
,
252 wxCHECK_MSG( m_iconAdded
, false,
253 wxT("can't be used before the icon is created") );
255 const HWND hwnd
= GetHwndOf(m_win
);
257 // we need to enable version 5.0 behaviour to receive notifications about
258 // the balloon disappearance
259 NotifyIconData
notifyData(hwnd
);
260 notifyData
.uFlags
= 0;
261 notifyData
.uVersion
= 3 /* NOTIFYICON_VERSION for Windows 2000/XP */;
263 if ( !wxShellNotifyIcon(NIM_SETVERSION
, ¬ifyData
) )
265 wxLogLastError(wxT("wxShellNotifyIcon(NIM_SETVERSION)"));
268 // do show the balloon now
269 notifyData
= NotifyIconData(hwnd
);
270 notifyData
.uFlags
|= NIF_INFO
;
271 notifyData
.uTimeout
= msec
;
272 wxStrlcpy(notifyData
.szInfo
, text
.wx_str(), WXSIZEOF(notifyData
.szInfo
));
273 wxStrlcpy(notifyData
.szInfoTitle
, title
.wx_str(),
274 WXSIZEOF(notifyData
.szInfoTitle
));
276 if ( flags
& wxICON_INFORMATION
)
277 notifyData
.dwInfoFlags
|= NIIF_INFO
;
278 else if ( flags
& wxICON_WARNING
)
279 notifyData
.dwInfoFlags
|= NIIF_WARNING
;
280 else if ( flags
& wxICON_ERROR
)
281 notifyData
.dwInfoFlags
|= NIIF_ERROR
;
283 bool ok
= wxShellNotifyIcon(NIM_MODIFY
, ¬ifyData
) != 0;
286 wxLogLastError(wxT("wxShellNotifyIcon(NIM_MODIFY)"));
292 #endif // wxUSE_TASKBARICON_BALLOONS
294 bool wxTaskBarIcon::RemoveIcon()
301 NotifyIconData
notifyData(GetHwndOf(m_win
));
303 bool ok
= wxShellNotifyIcon(NIM_DELETE
, ¬ifyData
) != 0;
306 wxLogLastError(wxT("wxShellNotifyIcon(NIM_DELETE)"));
313 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
)
315 wxASSERT_MSG( m_win
!= NULL
, wxT("taskbar icon not initialized") );
317 static bool s_inPopup
= false;
325 wxGetMousePosition(&x
, &y
);
329 m_win
->PushEventHandler(this);
333 // the SetForegroundWindow() and PostMessage() calls are needed to work
334 // around Win32 bug with the popup menus shown for the notifications as
335 // documented at http://support.microsoft.com/kb/q135788/
336 ::SetForegroundWindow(GetHwndOf(m_win
));
338 bool rval
= m_win
->PopupMenu(menu
, 0, 0);
340 ::PostMessage(GetHwndOf(m_win
), WM_NULL
, 0, 0L);
342 m_win
->PopEventHandler(false);
348 #endif // wxUSE_MENUS
350 void wxTaskBarIcon::RegisterWindowMessages()
352 static bool s_registered
= false;
356 // Taskbar restart msg will be sent to us if the icon needs to be redrawn
357 gs_msgRestartTaskbar
= RegisterWindowMessage(wxT("TaskbarCreated"));
359 // Also register the taskbar message here
360 gs_msgTaskbar
= ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
366 // ----------------------------------------------------------------------------
367 // wxTaskBarIcon window proc
368 // ----------------------------------------------------------------------------
370 long wxTaskBarIcon::WindowProc(unsigned int msg
,
371 unsigned int WXUNUSED(wParam
),
374 if ( msg
== gs_msgRestartTaskbar
) // does the icon need to be redrawn?
377 SetIcon(m_icon
, m_strTooltip
);
381 // this function should only be called for gs_msg(Restart)Taskbar messages
382 wxASSERT( msg
== gs_msgTaskbar
);
384 wxEventType eventType
= 0;
388 eventType
= wxEVT_TASKBAR_LEFT_DOWN
;
392 eventType
= wxEVT_TASKBAR_LEFT_UP
;
396 eventType
= wxEVT_TASKBAR_RIGHT_DOWN
;
400 eventType
= wxEVT_TASKBAR_RIGHT_UP
;
403 case WM_LBUTTONDBLCLK
:
404 eventType
= wxEVT_TASKBAR_LEFT_DCLICK
;
407 case WM_RBUTTONDBLCLK
:
408 eventType
= wxEVT_TASKBAR_RIGHT_DCLICK
;
412 eventType
= wxEVT_TASKBAR_MOVE
;
415 case NIN_BALLOONTIMEOUT
:
416 eventType
= wxEVT_TASKBAR_BALLOON_TIMEOUT
;
419 case NIN_BALLOONUSERCLICK
:
420 eventType
= wxEVT_TASKBAR_BALLOON_CLICK
;
426 wxTaskBarIconEvent
event(eventType
, this);
434 #endif // wxUSE_TASKBARICON