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
49 // initialized on demand
50 static UINT gs_msgTaskbar
= 0;
51 static UINT gs_msgRestartTaskbar
= 0;
54 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
56 // ============================================================================
58 // ============================================================================
60 // wrapper around Shell_NotifyIcon(): this function is not present in Win95
61 // shell32.dll so load it dynamically to allow programs using wxTaskBarIcon to
62 // start under this OS
63 static BOOL
wxShellNotifyIcon(DWORD dwMessage
, NOTIFYICONDATA
*pData
)
65 #if wxUSE_DYNLIB_CLASS
66 typedef BOOL (WINAPI
*Shell_NotifyIcon_t
)(DWORD
, NOTIFYICONDATA
*);
68 static Shell_NotifyIcon_t s_pfnShell_NotifyIcon
= NULL
;
69 static bool s_initialized
= false;
75 wxDynamicLibrary
dllShell("shell32.dll");
76 if ( dllShell
.IsLoaded() )
78 wxDL_INIT_FUNC_AW(s_pfn
, Shell_NotifyIcon
, dllShell
);
81 // NB: it's ok to destroy dllShell here, we link to shell32.dll
82 // implicitly so it won't be unloaded
85 return s_pfnShell_NotifyIcon
? (*s_pfnShell_NotifyIcon
)(dwMessage
, pData
)
87 #else // !wxUSE_DYNLIB_CLASS
88 return Shell_NotifyIcon(dwMessage
, pData
);
89 #endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS
92 // ----------------------------------------------------------------------------
93 // wxTaskBarIconWindow: helper window
94 // ----------------------------------------------------------------------------
96 // NB: this class serves two purposes:
97 // 1. win32 needs a HWND associated with taskbar icon, this provides it
98 // 2. we need wxTopLevelWindow so that the app doesn't exit when
99 // last frame is closed but there still is a taskbar icon
100 class wxTaskBarIconWindow
: public wxFrame
103 wxTaskBarIconWindow(wxTaskBarIcon
*icon
)
104 : wxFrame(NULL
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
, 0),
109 WXLRESULT
MSWWindowProc(WXUINT msg
,
110 WXWPARAM wParam
, WXLPARAM lParam
)
112 if (msg
== gs_msgRestartTaskbar
|| msg
== gs_msgTaskbar
)
114 return m_icon
->WindowProc(msg
, wParam
, lParam
);
118 return wxFrame::MSWWindowProc(msg
, wParam
, lParam
);
123 wxTaskBarIcon
*m_icon
;
127 // ----------------------------------------------------------------------------
128 // NotifyIconData: wrapper around NOTIFYICONDATA
129 // ----------------------------------------------------------------------------
131 struct NotifyIconData
: public NOTIFYICONDATA
133 NotifyIconData(WXHWND hwnd
)
135 memset(this, 0, sizeof(NOTIFYICONDATA
));
136 cbSize
= sizeof(NOTIFYICONDATA
);
138 uCallbackMessage
= gs_msgTaskbar
;
139 uFlags
= NIF_MESSAGE
;
141 // we use the same id for all taskbar icons as we don't need it to
142 // distinguish between them
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
151 wxTaskBarIcon::wxTaskBarIcon()
155 RegisterWindowMessages();
158 wxTaskBarIcon::~wxTaskBarIcon()
165 // we must use delete and not Destroy() here because the latter will
166 // only schedule the window to be deleted during the next idle event
167 // processing but we may not get any idle events if there are no other
168 // windows left in the program
174 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
176 // NB: we have to create the window lazily because of backward compatibility,
177 // old applications may create a wxTaskBarIcon instance before wxApp
178 // is initialized (as samples/taskbar used to do)
181 m_win
= new wxTaskBarIconWindow(this);
185 m_strTooltip
= tooltip
;
187 NotifyIconData
notifyData(GetHwndOf(m_win
));
191 notifyData
.uFlags
|= NIF_ICON
;
192 notifyData
.hIcon
= GetHiconOf(icon
);
195 // set NIF_TIP even for an empty tooltip: otherwise it would be impossible
196 // to remove an existing tooltip using this function
197 notifyData
.uFlags
|= NIF_TIP
;
198 if ( !tooltip
.empty() )
200 wxStrlcpy(notifyData
.szTip
, tooltip
.wx_str(), WXSIZEOF(notifyData
.szTip
));
203 bool ok
= wxShellNotifyIcon(m_iconAdded
? NIM_MODIFY
204 : NIM_ADD
, ¬ifyData
) != 0;
208 wxLogLastError(wxT("wxShellNotifyIcon(NIM_MODIFY/ADD)"));
211 if ( !m_iconAdded
&& ok
)
217 #if wxUSE_TASKBARICON_BALLOONS
220 wxTaskBarIcon::ShowBalloon(const wxString
& title
,
221 const wxString
& text
,
225 wxCHECK_MSG( m_iconAdded
, false,
226 wxT("can't be used before the icon is created") );
228 const HWND hwnd
= GetHwndOf(m_win
);
230 // we need to enable version 5.0 behaviour to receive notifications about
231 // the balloon disappearance
232 NotifyIconData
notifyData(hwnd
);
233 notifyData
.uFlags
= 0;
234 notifyData
.uVersion
= 3 /* NOTIFYICON_VERSION for Windows XP */;
236 if ( !wxShellNotifyIcon(NIM_SETVERSION
, ¬ifyData
) )
238 wxLogLastError(wxT("wxShellNotifyIcon(NIM_SETVERSION)"));
241 // do show the balloon now
242 notifyData
= NotifyIconData(hwnd
);
243 notifyData
.uFlags
|= NIF_INFO
;
244 notifyData
.uTimeout
= msec
;
245 wxStrlcpy(notifyData
.szInfo
, text
.wx_str(), WXSIZEOF(notifyData
.szInfo
));
246 wxStrlcpy(notifyData
.szInfoTitle
, title
.wx_str(),
247 WXSIZEOF(notifyData
.szInfoTitle
));
249 if ( flags
& wxICON_INFORMATION
)
250 notifyData
.dwInfoFlags
|= NIIF_INFO
;
251 else if ( flags
& wxICON_WARNING
)
252 notifyData
.dwInfoFlags
|= NIIF_WARNING
;
253 else if ( flags
& wxICON_ERROR
)
254 notifyData
.dwInfoFlags
|= NIIF_ERROR
;
256 bool ok
= wxShellNotifyIcon(NIM_MODIFY
, ¬ifyData
) != 0;
259 wxLogLastError(wxT("wxShellNotifyIcon(NIM_MODIFY)"));
265 #endif // wxUSE_TASKBARICON_BALLOONS
267 bool wxTaskBarIcon::RemoveIcon()
274 NotifyIconData
notifyData(GetHwndOf(m_win
));
276 bool ok
= wxShellNotifyIcon(NIM_DELETE
, ¬ifyData
) != 0;
279 wxLogLastError(wxT("wxShellNotifyIcon(NIM_DELETE)"));
286 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
)
288 wxASSERT_MSG( m_win
!= NULL
, wxT("taskbar icon not initialized") );
290 static bool s_inPopup
= false;
298 wxGetMousePosition(&x
, &y
);
302 m_win
->PushEventHandler(this);
306 // the SetForegroundWindow() and PostMessage() calls are needed to work
307 // around Win32 bug with the popup menus shown for the notifications as
308 // documented at http://support.microsoft.com/kb/q135788/
309 ::SetForegroundWindow(GetHwndOf(m_win
));
311 bool rval
= m_win
->PopupMenu(menu
, 0, 0);
313 ::PostMessage(GetHwndOf(m_win
), WM_NULL
, 0, 0L);
315 m_win
->PopEventHandler(false);
321 #endif // wxUSE_MENUS
323 void wxTaskBarIcon::RegisterWindowMessages()
325 static bool s_registered
= false;
329 // Taskbar restart msg will be sent to us if the icon needs to be redrawn
330 gs_msgRestartTaskbar
= RegisterWindowMessage(wxT("TaskbarCreated"));
332 // Also register the taskbar message here
333 gs_msgTaskbar
= ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
339 // ----------------------------------------------------------------------------
340 // wxTaskBarIcon window proc
341 // ----------------------------------------------------------------------------
343 long wxTaskBarIcon::WindowProc(unsigned int msg
,
344 unsigned int WXUNUSED(wParam
),
347 if ( msg
== gs_msgRestartTaskbar
) // does the icon need to be redrawn?
350 SetIcon(m_icon
, m_strTooltip
);
354 // this function should only be called for gs_msg(Restart)Taskbar messages
355 wxASSERT( msg
== gs_msgTaskbar
);
357 wxEventType eventType
= 0;
361 eventType
= wxEVT_TASKBAR_LEFT_DOWN
;
365 eventType
= wxEVT_TASKBAR_LEFT_UP
;
369 eventType
= wxEVT_TASKBAR_RIGHT_DOWN
;
373 eventType
= wxEVT_TASKBAR_RIGHT_UP
;
376 case WM_LBUTTONDBLCLK
:
377 eventType
= wxEVT_TASKBAR_LEFT_DCLICK
;
380 case WM_RBUTTONDBLCLK
:
381 eventType
= wxEVT_TASKBAR_RIGHT_DCLICK
;
385 eventType
= wxEVT_TASKBAR_MOVE
;
388 case NIN_BALLOONTIMEOUT
:
389 eventType
= wxEVT_TASKBAR_BALLOON_TIMEOUT
;
392 case NIN_BALLOONUSERCLICK
:
393 eventType
= wxEVT_TASKBAR_BALLOON_CLICK
;
399 wxTaskBarIconEvent
event(eventType
, this);
407 #endif // wxUSE_TASKBARICON