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"
30 #include "wx/msw/wrapshl.h"
33 #include "wx/taskbar.h"
34 #include "wx/dynlib.h"
36 #ifndef NIN_BALLOONTIMEOUT
37 #define NIN_BALLOONTIMEOUT 0x0404
38 #define NIN_BALLOONUSERCLICK 0x0405
41 #ifndef NIM_SETVERSION
42 #define NIM_SETVERSION 0x00000004
46 #define NIF_INFO 0x00000010
49 #ifndef NOTIFYICONDATA_V1_SIZE
51 #define NOTIFYICONDATA_V1_SIZE 0x0098
53 #define NOTIFYICONDATA_V1_SIZE 0x0058
57 #ifndef NOTIFYICONDATA_V2_SIZE
59 #define NOTIFYICONDATA_V2_SIZE 0x03A8;
61 #define NOTIFYICONDATA_V2_SIZE 0x01E8;
65 // initialized on demand
66 static UINT gs_msgTaskbar
= 0;
67 static UINT gs_msgRestartTaskbar
= 0;
70 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
72 // ============================================================================
74 // ============================================================================
76 // wrapper around Shell_NotifyIcon(): this function is not present in Win95
77 // shell32.dll so load it dynamically to allow programs using wxTaskBarIcon to
78 // start under this OS
79 static BOOL
wxShellNotifyIcon(DWORD dwMessage
, NOTIFYICONDATA
*pData
)
81 #if wxUSE_DYNLIB_CLASS
82 typedef BOOL (WINAPI
*Shell_NotifyIcon_t
)(DWORD
, NOTIFYICONDATA
*);
84 static Shell_NotifyIcon_t s_pfnShell_NotifyIcon
= NULL
;
85 static bool s_initialized
= false;
91 wxDynamicLibrary
dllShell("shell32.dll");
92 if ( dllShell
.IsLoaded() )
94 wxDL_INIT_FUNC_AW(s_pfn
, Shell_NotifyIcon
, dllShell
);
97 // NB: it's ok to destroy dllShell here, we link to shell32.dll
98 // implicitly so it won't be unloaded
101 return s_pfnShell_NotifyIcon
? (*s_pfnShell_NotifyIcon
)(dwMessage
, pData
)
103 #else // !wxUSE_DYNLIB_CLASS
104 return Shell_NotifyIcon(dwMessage
, pData
);
105 #endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS
108 // ----------------------------------------------------------------------------
109 // wxTaskBarIconWindow: helper window
110 // ----------------------------------------------------------------------------
112 // NB: this class serves two purposes:
113 // 1. win32 needs a HWND associated with taskbar icon, this provides it
114 // 2. we need wxTopLevelWindow so that the app doesn't exit when
115 // last frame is closed but there still is a taskbar icon
116 class wxTaskBarIconWindow
: public wxFrame
119 wxTaskBarIconWindow(wxTaskBarIcon
*icon
)
120 : wxFrame(NULL
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
, 0),
125 WXLRESULT
MSWWindowProc(WXUINT msg
,
126 WXWPARAM wParam
, WXLPARAM lParam
)
128 if (msg
== gs_msgRestartTaskbar
|| msg
== gs_msgTaskbar
)
130 return m_icon
->WindowProc(msg
, wParam
, lParam
);
134 return wxFrame::MSWWindowProc(msg
, wParam
, lParam
);
139 wxTaskBarIcon
*m_icon
;
143 // ----------------------------------------------------------------------------
144 // NotifyIconData: wrapper around NOTIFYICONDATA
145 // ----------------------------------------------------------------------------
147 struct NotifyIconData
: public NOTIFYICONDATA
149 NotifyIconData(WXHWND hwnd
)
151 memset(this, 0, sizeof(NOTIFYICONDATA
));
153 // Do _not_ use sizeof(NOTIFYICONDATA) here, it may be too big if we're
154 // compiled with newer headers but running on an older system and while
155 // we could do complicated tests for the exact system version it's
156 // easier to just use an old size which should be supported everywhere
157 // from Windows 2000 up and which is all we need as we don't use any
158 // newer features so far. But if we're running under a really ancient
159 // system (Win9x), fall back to even smaller size -- then the balloon
160 // related features won't be available but the rest will still work.
161 cbSize
= wxTheApp
->GetShell32Version() >= 500
162 ? NOTIFYICONDATA_V2_SIZE
163 : NOTIFYICONDATA_V1_SIZE
;
166 uCallbackMessage
= gs_msgTaskbar
;
167 uFlags
= NIF_MESSAGE
;
169 // we use the same id for all taskbar icons as we don't need it to
170 // distinguish between them
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
179 wxTaskBarIcon::wxTaskBarIcon()
183 RegisterWindowMessages();
186 wxTaskBarIcon::~wxTaskBarIcon()
193 // we must use delete and not Destroy() here because the latter will
194 // only schedule the window to be deleted during the next idle event
195 // processing but we may not get any idle events if there are no other
196 // windows left in the program
202 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
204 // NB: we have to create the window lazily because of backward compatibility,
205 // old applications may create a wxTaskBarIcon instance before wxApp
206 // is initialized (as samples/taskbar used to do)
209 m_win
= new wxTaskBarIconWindow(this);
213 m_strTooltip
= tooltip
;
215 NotifyIconData
notifyData(GetHwndOf(m_win
));
219 notifyData
.uFlags
|= NIF_ICON
;
220 notifyData
.hIcon
= GetHiconOf(icon
);
223 // set NIF_TIP even for an empty tooltip: otherwise it would be impossible
224 // to remove an existing tooltip using this function
225 notifyData
.uFlags
|= NIF_TIP
;
226 if ( !tooltip
.empty() )
228 wxStrlcpy(notifyData
.szTip
, tooltip
.wx_str(), WXSIZEOF(notifyData
.szTip
));
231 bool ok
= wxShellNotifyIcon(m_iconAdded
? NIM_MODIFY
232 : NIM_ADD
, ¬ifyData
) != 0;
236 wxLogLastError(wxT("wxShellNotifyIcon(NIM_MODIFY/ADD)"));
239 if ( !m_iconAdded
&& ok
)
245 #if wxUSE_TASKBARICON_BALLOONS
248 wxTaskBarIcon::ShowBalloon(const wxString
& title
,
249 const wxString
& text
,
253 wxCHECK_MSG( m_iconAdded
, false,
254 wxT("can't be used before the icon is created") );
256 const HWND hwnd
= GetHwndOf(m_win
);
258 // we need to enable version 5.0 behaviour to receive notifications about
259 // the balloon disappearance
260 NotifyIconData
notifyData(hwnd
);
261 notifyData
.uFlags
= 0;
262 notifyData
.uVersion
= 3 /* NOTIFYICON_VERSION for Windows 2000/XP */;
264 if ( !wxShellNotifyIcon(NIM_SETVERSION
, ¬ifyData
) )
266 wxLogLastError(wxT("wxShellNotifyIcon(NIM_SETVERSION)"));
269 // do show the balloon now
270 notifyData
= NotifyIconData(hwnd
);
271 notifyData
.uFlags
|= NIF_INFO
;
272 notifyData
.uTimeout
= msec
;
273 wxStrlcpy(notifyData
.szInfo
, text
.wx_str(), WXSIZEOF(notifyData
.szInfo
));
274 wxStrlcpy(notifyData
.szInfoTitle
, title
.wx_str(),
275 WXSIZEOF(notifyData
.szInfoTitle
));
277 if ( flags
& wxICON_INFORMATION
)
278 notifyData
.dwInfoFlags
|= NIIF_INFO
;
279 else if ( flags
& wxICON_WARNING
)
280 notifyData
.dwInfoFlags
|= NIIF_WARNING
;
281 else if ( flags
& wxICON_ERROR
)
282 notifyData
.dwInfoFlags
|= NIIF_ERROR
;
284 bool ok
= wxShellNotifyIcon(NIM_MODIFY
, ¬ifyData
) != 0;
287 wxLogLastError(wxT("wxShellNotifyIcon(NIM_MODIFY)"));
293 #endif // wxUSE_TASKBARICON_BALLOONS
295 bool wxTaskBarIcon::RemoveIcon()
302 NotifyIconData
notifyData(GetHwndOf(m_win
));
304 bool ok
= wxShellNotifyIcon(NIM_DELETE
, ¬ifyData
) != 0;
307 wxLogLastError(wxT("wxShellNotifyIcon(NIM_DELETE)"));
314 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
)
316 wxASSERT_MSG( m_win
!= NULL
, wxT("taskbar icon not initialized") );
318 static bool s_inPopup
= false;
326 wxGetMousePosition(&x
, &y
);
330 m_win
->PushEventHandler(this);
334 // the SetForegroundWindow() and PostMessage() calls are needed to work
335 // around Win32 bug with the popup menus shown for the notifications as
336 // documented at http://support.microsoft.com/kb/q135788/
337 ::SetForegroundWindow(GetHwndOf(m_win
));
339 bool rval
= m_win
->PopupMenu(menu
, 0, 0);
341 ::PostMessage(GetHwndOf(m_win
), WM_NULL
, 0, 0L);
343 m_win
->PopEventHandler(false);
349 #endif // wxUSE_MENUS
351 void wxTaskBarIcon::RegisterWindowMessages()
353 static bool s_registered
= false;
357 // Taskbar restart msg will be sent to us if the icon needs to be redrawn
358 gs_msgRestartTaskbar
= RegisterWindowMessage(wxT("TaskbarCreated"));
360 // Also register the taskbar message here
361 gs_msgTaskbar
= ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
367 // ----------------------------------------------------------------------------
368 // wxTaskBarIcon window proc
369 // ----------------------------------------------------------------------------
371 long wxTaskBarIcon::WindowProc(unsigned int msg
,
372 unsigned int WXUNUSED(wParam
),
375 if ( msg
== gs_msgRestartTaskbar
) // does the icon need to be redrawn?
378 SetIcon(m_icon
, m_strTooltip
);
382 // this function should only be called for gs_msg(Restart)Taskbar messages
383 wxASSERT( msg
== gs_msgTaskbar
);
385 wxEventType eventType
= 0;
389 eventType
= wxEVT_TASKBAR_LEFT_DOWN
;
393 eventType
= wxEVT_TASKBAR_LEFT_UP
;
397 eventType
= wxEVT_TASKBAR_RIGHT_DOWN
;
401 eventType
= wxEVT_TASKBAR_RIGHT_UP
;
404 case WM_LBUTTONDBLCLK
:
405 eventType
= wxEVT_TASKBAR_LEFT_DCLICK
;
408 case WM_RBUTTONDBLCLK
:
409 eventType
= wxEVT_TASKBAR_RIGHT_DCLICK
;
413 eventType
= wxEVT_TASKBAR_MOVE
;
416 case NIN_BALLOONTIMEOUT
:
417 eventType
= wxEVT_TASKBAR_BALLOON_TIMEOUT
;
420 case NIN_BALLOONUSERCLICK
:
421 eventType
= wxEVT_TASKBAR_BALLOON_CLICK
;
427 wxTaskBarIconEvent
event(eventType
, this);
435 #endif // wxUSE_TASKBARICON