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 // initialized on demand
36 static UINT gs_msgTaskbar
= 0;
37 static UINT gs_msgRestartTaskbar
= 0;
40 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
42 // ============================================================================
44 // ============================================================================
46 // wrapper around Shell_NotifyIcon(): this function is not present in Win95
47 // shell32.dll so load it dynamically to allow programs using wxTaskBarIcon to
48 // start under this OS
49 static BOOL
wxShellNotifyIcon(DWORD dwMessage
, NOTIFYICONDATA
*pData
)
51 #if wxUSE_DYNLIB_CLASS
52 typedef BOOL (WINAPI
*Shell_NotifyIcon_t
)(DWORD
, NOTIFYICONDATA
*);
54 static Shell_NotifyIcon_t s_pfnShell_NotifyIcon
= NULL
;
55 static bool s_initialized
= false;
61 wxDynamicLibrary
dllShell("shell32.dll");
62 if ( dllShell
.IsLoaded() )
64 wxDL_INIT_FUNC_AW(s_pfn
, Shell_NotifyIcon
, dllShell
);
67 // NB: it's ok to destroy dllShell here, we link to shell32.dll
68 // implicitly so it won't be unloaded
71 return s_pfnShell_NotifyIcon
? (*s_pfnShell_NotifyIcon
)(dwMessage
, pData
)
73 #else // !wxUSE_DYNLIB_CLASS
74 return Shell_NotifyIcon(dwMessage
, pData
);
75 #endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS
78 // ----------------------------------------------------------------------------
79 // wxTaskBarIconWindow: helper window
80 // ----------------------------------------------------------------------------
82 // NB: this class serves two purposes:
83 // 1. win32 needs a HWND associated with taskbar icon, this provides it
84 // 2. we need wxTopLevelWindow so that the app doesn't exit when
85 // last frame is closed but there still is a taskbar icon
86 class wxTaskBarIconWindow
: public wxFrame
89 wxTaskBarIconWindow(wxTaskBarIcon
*icon
)
90 : wxFrame(NULL
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
, 0),
95 WXLRESULT
MSWWindowProc(WXUINT msg
,
96 WXWPARAM wParam
, WXLPARAM lParam
)
98 if (msg
== gs_msgRestartTaskbar
|| msg
== gs_msgTaskbar
)
100 return m_icon
->WindowProc(msg
, wParam
, lParam
);
104 return wxFrame::MSWWindowProc(msg
, wParam
, lParam
);
109 wxTaskBarIcon
*m_icon
;
113 // ----------------------------------------------------------------------------
114 // NotifyIconData: wrapper around NOTIFYICONDATA
115 // ----------------------------------------------------------------------------
117 struct NotifyIconData
: public NOTIFYICONDATA
119 NotifyIconData(WXHWND hwnd
)
121 memset(this, 0, sizeof(NOTIFYICONDATA
));
122 cbSize
= sizeof(NOTIFYICONDATA
);
124 uCallbackMessage
= gs_msgTaskbar
;
125 uFlags
= NIF_MESSAGE
;
127 // we use the same id for all taskbar icons as we don't need it to
128 // distinguish between them
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 wxTaskBarIcon::wxTaskBarIcon()
141 RegisterWindowMessages();
144 wxTaskBarIcon::~wxTaskBarIcon()
154 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
156 // NB: we have to create the window lazily because of backward compatibility,
157 // old applications may create a wxTaskBarIcon instance before wxApp
158 // is initialized (as samples/taskbar used to do)
161 m_win
= new wxTaskBarIconWindow(this);
165 m_strTooltip
= tooltip
;
167 NotifyIconData
notifyData(GetHwndOf(m_win
));
171 notifyData
.uFlags
|= NIF_ICON
;
172 notifyData
.hIcon
= GetHiconOf(icon
);
175 // set NIF_TIP even for an empty tooltip: otherwise it would be impossible
176 // to remove an existing tooltip using this function
177 notifyData
.uFlags
|= NIF_TIP
;
178 if ( !tooltip
.empty() )
180 wxStrncpy(notifyData
.szTip
, tooltip
.wx_str(), WXSIZEOF(notifyData
.szTip
));
183 bool ok
= wxShellNotifyIcon(m_iconAdded
? NIM_MODIFY
184 : NIM_ADD
, ¬ifyData
) != 0;
186 if ( !m_iconAdded
&& ok
)
193 wxTaskBarIcon::ShowBalloon(const wxString
& title
,
194 const wxString
& text
,
198 wxCHECK_MSG( m_iconAdded
, false,
199 _T("can't be used before the icon is created") );
201 const HWND hwnd
= GetHwndOf(m_win
);
203 // we need to enable version 5.0 behaviour to receive notifications about
204 // the balloon disappearance
205 NotifyIconData
notifyData(hwnd
);
206 notifyData
.uFlags
= 0;
207 notifyData
.uVersion
= 3 /* NOTIFYICON_VERSION for Windows XP */;
209 wxShellNotifyIcon(NIM_SETVERSION
, ¬ifyData
);
212 // do show the balloon now
213 notifyData
= NotifyIconData(hwnd
);
214 notifyData
.uFlags
|= NIF_INFO
;
215 notifyData
.uTimeout
= msec
;
216 wxStrncpy(notifyData
.szInfo
, text
.wx_str(), WXSIZEOF(notifyData
.szInfo
));
217 wxStrncpy(notifyData
.szInfoTitle
, title
.wx_str(),
218 WXSIZEOF(notifyData
.szInfoTitle
));
220 if ( flags
& wxICON_INFORMATION
)
221 notifyData
.dwInfoFlags
|= NIIF_INFO
;
222 else if ( flags
& wxICON_WARNING
)
223 notifyData
.dwInfoFlags
|= NIIF_WARNING
;
224 else if ( flags
& wxICON_ERROR
)
225 notifyData
.dwInfoFlags
|= NIIF_ERROR
;
227 return wxShellNotifyIcon(NIM_MODIFY
, ¬ifyData
) != 0;
230 bool wxTaskBarIcon::RemoveIcon()
237 NotifyIconData
notifyData(GetHwndOf(m_win
));
239 return wxShellNotifyIcon(NIM_DELETE
, ¬ifyData
) != 0;
243 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
)
245 wxASSERT_MSG( m_win
!= NULL
, _T("taskbar icon not initialized") );
247 static bool s_inPopup
= false;
255 wxGetMousePosition(&x
, &y
);
259 m_win
->PushEventHandler(this);
263 // the SetForegroundWindow() and PostMessage() calls are needed to work
264 // around Win32 bug with the popup menus shown for the notifications as
265 // documented at http://support.microsoft.com/kb/q135788/
266 ::SetForegroundWindow(GetHwndOf(m_win
));
268 bool rval
= m_win
->PopupMenu(menu
, 0, 0);
270 ::PostMessage(GetHwndOf(m_win
), WM_NULL
, 0, 0L);
272 m_win
->PopEventHandler(false);
278 #endif // wxUSE_MENUS
280 void wxTaskBarIcon::RegisterWindowMessages()
282 static bool s_registered
= false;
286 // Taskbar restart msg will be sent to us if the icon needs to be redrawn
287 gs_msgRestartTaskbar
= RegisterWindowMessage(wxT("TaskbarCreated"));
289 // Also register the taskbar message here
290 gs_msgTaskbar
= ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
296 // ----------------------------------------------------------------------------
297 // wxTaskBarIcon window proc
298 // ----------------------------------------------------------------------------
300 long wxTaskBarIcon::WindowProc(unsigned int msg
,
301 unsigned int WXUNUSED(wParam
),
304 if ( msg
== gs_msgRestartTaskbar
) // does the icon need to be redrawn?
307 SetIcon(m_icon
, m_strTooltip
);
311 // this function should only be called for gs_msg(Restart)Taskbar messages
312 wxASSERT( msg
== gs_msgTaskbar
);
314 wxEventType eventType
= 0;
318 eventType
= wxEVT_TASKBAR_LEFT_DOWN
;
322 eventType
= wxEVT_TASKBAR_LEFT_UP
;
326 eventType
= wxEVT_TASKBAR_RIGHT_DOWN
;
330 eventType
= wxEVT_TASKBAR_RIGHT_UP
;
333 case WM_LBUTTONDBLCLK
:
334 eventType
= wxEVT_TASKBAR_LEFT_DCLICK
;
337 case WM_RBUTTONDBLCLK
:
338 eventType
= wxEVT_TASKBAR_RIGHT_DCLICK
;
342 eventType
= wxEVT_TASKBAR_MOVE
;
345 case NIN_BALLOONTIMEOUT
:
346 eventType
= wxEVT_TASKBAR_BALLOON_TIMEOUT
;
349 case NIN_BALLOONUSERCLICK
:
350 eventType
= wxEVT_TASKBAR_BALLOON_CLICK
;
356 wxTaskBarIconEvent
event(eventType
, this);
364 #endif // wxUSE_TASKBARICON