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 // initialized on demand
41 static UINT gs_msgTaskbar
= 0;
42 static UINT gs_msgRestartTaskbar
= 0;
45 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
47 // ============================================================================
49 // ============================================================================
51 // wrapper around Shell_NotifyIcon(): this function is not present in Win95
52 // shell32.dll so load it dynamically to allow programs using wxTaskBarIcon to
53 // start under this OS
54 static BOOL
wxShellNotifyIcon(DWORD dwMessage
, NOTIFYICONDATA
*pData
)
56 #if wxUSE_DYNLIB_CLASS
57 typedef BOOL (WINAPI
*Shell_NotifyIcon_t
)(DWORD
, NOTIFYICONDATA
*);
59 static Shell_NotifyIcon_t s_pfnShell_NotifyIcon
= NULL
;
60 static bool s_initialized
= false;
66 wxDynamicLibrary
dllShell("shell32.dll");
67 if ( dllShell
.IsLoaded() )
69 wxDL_INIT_FUNC_AW(s_pfn
, Shell_NotifyIcon
, dllShell
);
72 // NB: it's ok to destroy dllShell here, we link to shell32.dll
73 // implicitly so it won't be unloaded
76 return s_pfnShell_NotifyIcon
? (*s_pfnShell_NotifyIcon
)(dwMessage
, pData
)
78 #else // !wxUSE_DYNLIB_CLASS
79 return Shell_NotifyIcon(dwMessage
, pData
);
80 #endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS
83 // ----------------------------------------------------------------------------
84 // wxTaskBarIconWindow: helper window
85 // ----------------------------------------------------------------------------
87 // NB: this class serves two purposes:
88 // 1. win32 needs a HWND associated with taskbar icon, this provides it
89 // 2. we need wxTopLevelWindow so that the app doesn't exit when
90 // last frame is closed but there still is a taskbar icon
91 class wxTaskBarIconWindow
: public wxFrame
94 wxTaskBarIconWindow(wxTaskBarIcon
*icon
)
95 : wxFrame(NULL
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
, 0),
100 WXLRESULT
MSWWindowProc(WXUINT msg
,
101 WXWPARAM wParam
, WXLPARAM lParam
)
103 if (msg
== gs_msgRestartTaskbar
|| msg
== gs_msgTaskbar
)
105 return m_icon
->WindowProc(msg
, wParam
, lParam
);
109 return wxFrame::MSWWindowProc(msg
, wParam
, lParam
);
114 wxTaskBarIcon
*m_icon
;
118 // ----------------------------------------------------------------------------
119 // NotifyIconData: wrapper around NOTIFYICONDATA
120 // ----------------------------------------------------------------------------
122 struct NotifyIconData
: public NOTIFYICONDATA
124 NotifyIconData(WXHWND hwnd
)
126 memset(this, 0, sizeof(NOTIFYICONDATA
));
127 cbSize
= sizeof(NOTIFYICONDATA
);
129 uCallbackMessage
= gs_msgTaskbar
;
130 uFlags
= NIF_MESSAGE
;
132 // we use the same id for all taskbar icons as we don't need it to
133 // distinguish between them
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
142 wxTaskBarIcon::wxTaskBarIcon()
146 RegisterWindowMessages();
149 wxTaskBarIcon::~wxTaskBarIcon()
156 // we must use delete and not Destroy() here because the latter will
157 // only schedule the window to be deleted during the next idle event
158 // processing but we may not get any idle events if there are no other
159 // windows left in the program
165 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
167 // NB: we have to create the window lazily because of backward compatibility,
168 // old applications may create a wxTaskBarIcon instance before wxApp
169 // is initialized (as samples/taskbar used to do)
172 m_win
= new wxTaskBarIconWindow(this);
176 m_strTooltip
= tooltip
;
178 NotifyIconData
notifyData(GetHwndOf(m_win
));
182 notifyData
.uFlags
|= NIF_ICON
;
183 notifyData
.hIcon
= GetHiconOf(icon
);
186 // set NIF_TIP even for an empty tooltip: otherwise it would be impossible
187 // to remove an existing tooltip using this function
188 notifyData
.uFlags
|= NIF_TIP
;
189 if ( !tooltip
.empty() )
191 wxStrncpy(notifyData
.szTip
, tooltip
.wx_str(), WXSIZEOF(notifyData
.szTip
));
194 bool ok
= wxShellNotifyIcon(m_iconAdded
? NIM_MODIFY
195 : NIM_ADD
, ¬ifyData
) != 0;
197 if ( !m_iconAdded
&& ok
)
204 wxTaskBarIcon::ShowBalloon(const wxString
& title
,
205 const wxString
& text
,
209 wxCHECK_MSG( m_iconAdded
, false,
210 _T("can't be used before the icon is created") );
212 const HWND hwnd
= GetHwndOf(m_win
);
214 // we need to enable version 5.0 behaviour to receive notifications about
215 // the balloon disappearance
216 NotifyIconData
notifyData(hwnd
);
217 notifyData
.uFlags
= 0;
218 notifyData
.uVersion
= 3 /* NOTIFYICON_VERSION for Windows XP */;
220 wxShellNotifyIcon(NIM_SETVERSION
, ¬ifyData
);
223 // do show the balloon now
224 notifyData
= NotifyIconData(hwnd
);
225 notifyData
.uFlags
|= NIF_INFO
;
226 notifyData
.uTimeout
= msec
;
227 wxStrncpy(notifyData
.szInfo
, text
.wx_str(), WXSIZEOF(notifyData
.szInfo
));
228 wxStrncpy(notifyData
.szInfoTitle
, title
.wx_str(),
229 WXSIZEOF(notifyData
.szInfoTitle
));
231 if ( flags
& wxICON_INFORMATION
)
232 notifyData
.dwInfoFlags
|= NIIF_INFO
;
233 else if ( flags
& wxICON_WARNING
)
234 notifyData
.dwInfoFlags
|= NIIF_WARNING
;
235 else if ( flags
& wxICON_ERROR
)
236 notifyData
.dwInfoFlags
|= NIIF_ERROR
;
238 return wxShellNotifyIcon(NIM_MODIFY
, ¬ifyData
) != 0;
241 bool wxTaskBarIcon::RemoveIcon()
248 NotifyIconData
notifyData(GetHwndOf(m_win
));
250 return wxShellNotifyIcon(NIM_DELETE
, ¬ifyData
) != 0;
254 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
)
256 wxASSERT_MSG( m_win
!= NULL
, _T("taskbar icon not initialized") );
258 static bool s_inPopup
= false;
266 wxGetMousePosition(&x
, &y
);
270 m_win
->PushEventHandler(this);
274 // the SetForegroundWindow() and PostMessage() calls are needed to work
275 // around Win32 bug with the popup menus shown for the notifications as
276 // documented at http://support.microsoft.com/kb/q135788/
277 ::SetForegroundWindow(GetHwndOf(m_win
));
279 bool rval
= m_win
->PopupMenu(menu
, 0, 0);
281 ::PostMessage(GetHwndOf(m_win
), WM_NULL
, 0, 0L);
283 m_win
->PopEventHandler(false);
289 #endif // wxUSE_MENUS
291 void wxTaskBarIcon::RegisterWindowMessages()
293 static bool s_registered
= false;
297 // Taskbar restart msg will be sent to us if the icon needs to be redrawn
298 gs_msgRestartTaskbar
= RegisterWindowMessage(wxT("TaskbarCreated"));
300 // Also register the taskbar message here
301 gs_msgTaskbar
= ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
307 // ----------------------------------------------------------------------------
308 // wxTaskBarIcon window proc
309 // ----------------------------------------------------------------------------
311 long wxTaskBarIcon::WindowProc(unsigned int msg
,
312 unsigned int WXUNUSED(wParam
),
315 if ( msg
== gs_msgRestartTaskbar
) // does the icon need to be redrawn?
318 SetIcon(m_icon
, m_strTooltip
);
322 // this function should only be called for gs_msg(Restart)Taskbar messages
323 wxASSERT( msg
== gs_msgTaskbar
);
325 wxEventType eventType
= 0;
329 eventType
= wxEVT_TASKBAR_LEFT_DOWN
;
333 eventType
= wxEVT_TASKBAR_LEFT_UP
;
337 eventType
= wxEVT_TASKBAR_RIGHT_DOWN
;
341 eventType
= wxEVT_TASKBAR_RIGHT_UP
;
344 case WM_LBUTTONDBLCLK
:
345 eventType
= wxEVT_TASKBAR_LEFT_DCLICK
;
348 case WM_RBUTTONDBLCLK
:
349 eventType
= wxEVT_TASKBAR_RIGHT_DCLICK
;
353 eventType
= wxEVT_TASKBAR_MOVE
;
356 case NIN_BALLOONTIMEOUT
:
357 eventType
= wxEVT_TASKBAR_BALLOON_TIMEOUT
;
360 case NIN_BALLOONUSERCLICK
:
361 eventType
= wxEVT_TASKBAR_BALLOON_CLICK
;
367 wxTaskBarIconEvent
event(eventType
, this);
375 #endif // wxUSE_TASKBARICON