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/msw/private.h"
35 #include "wx/dynlib.h"
37 #ifndef NIN_BALLOONTIMEOUT
38 #define NIN_BALLOONTIMEOUT 0x0404
39 #define NIN_BALLOONUSERCLICK 0x0405
42 #ifndef NIM_SETVERSION
43 #define NIM_SETVERSION 0x00000004
47 #define NIF_INFO 0x00000010
50 #ifndef NOTIFYICONDATA_V1_SIZE
52 #define NOTIFYICONDATA_V1_SIZE 0x0098
54 #define NOTIFYICONDATA_V1_SIZE 0x0058
58 #ifndef NOTIFYICONDATA_V2_SIZE
60 #define NOTIFYICONDATA_V2_SIZE 0x03A8
62 #define NOTIFYICONDATA_V2_SIZE 0x01E8
66 // initialized on demand
67 static UINT gs_msgTaskbar
= 0;
68 static UINT gs_msgRestartTaskbar
= 0;
71 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
73 // ============================================================================
75 // ============================================================================
77 // wrapper around Shell_NotifyIcon(): this function is not present in Win95
78 // shell32.dll so load it dynamically to allow programs using wxTaskBarIcon to
79 // start under this OS
80 static BOOL
wxShellNotifyIcon(DWORD dwMessage
, NOTIFYICONDATA
*pData
)
82 #if wxUSE_DYNLIB_CLASS
83 typedef BOOL (WINAPI
*Shell_NotifyIcon_t
)(DWORD
, NOTIFYICONDATA
*);
85 static Shell_NotifyIcon_t s_pfnShell_NotifyIcon
= NULL
;
86 static bool s_initialized
= false;
92 wxDynamicLibrary
dllShell("shell32.dll");
93 if ( dllShell
.IsLoaded() )
95 wxDL_INIT_FUNC_AW(s_pfn
, Shell_NotifyIcon
, dllShell
);
98 // NB: it's ok to destroy dllShell here, we link to shell32.dll
99 // implicitly so it won't be unloaded
102 return s_pfnShell_NotifyIcon
? (*s_pfnShell_NotifyIcon
)(dwMessage
, pData
)
104 #else // !wxUSE_DYNLIB_CLASS
105 return Shell_NotifyIcon(dwMessage
, pData
);
106 #endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS
109 // ----------------------------------------------------------------------------
110 // wxTaskBarIconWindow: helper window
111 // ----------------------------------------------------------------------------
113 // NB: this class serves two purposes:
114 // 1. win32 needs a HWND associated with taskbar icon, this provides it
115 // 2. we need wxTopLevelWindow so that the app doesn't exit when
116 // last frame is closed but there still is a taskbar icon
117 class wxTaskBarIconWindow
: public wxFrame
120 wxTaskBarIconWindow(wxTaskBarIcon
*icon
)
121 : wxFrame(NULL
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
, 0),
126 WXLRESULT
MSWWindowProc(WXUINT msg
,
127 WXWPARAM wParam
, WXLPARAM lParam
)
129 if (msg
== gs_msgRestartTaskbar
|| msg
== gs_msgTaskbar
)
131 return m_icon
->WindowProc(msg
, wParam
, lParam
);
135 return wxFrame::MSWWindowProc(msg
, wParam
, lParam
);
140 wxTaskBarIcon
*m_icon
;
144 // ----------------------------------------------------------------------------
145 // NotifyIconData: wrapper around NOTIFYICONDATA
146 // ----------------------------------------------------------------------------
148 struct NotifyIconData
: public NOTIFYICONDATA
150 NotifyIconData(WXHWND hwnd
)
152 memset(this, 0, sizeof(NOTIFYICONDATA
));
154 // Do _not_ use sizeof(NOTIFYICONDATA) here, it may be too big if we're
155 // compiled with newer headers but running on an older system and while
156 // we could do complicated tests for the exact system version it's
157 // easier to just use an old size which should be supported everywhere
158 // from Windows 2000 up and which is all we need as we don't use any
159 // newer features so far. But if we're running under a really ancient
160 // system (Win9x), fall back to even smaller size -- then the balloon
161 // related features won't be available but the rest will still work.
162 cbSize
= wxTheApp
->GetShell32Version() >= 500
163 ? NOTIFYICONDATA_V2_SIZE
164 : NOTIFYICONDATA_V1_SIZE
;
167 uCallbackMessage
= gs_msgTaskbar
;
168 uFlags
= NIF_MESSAGE
;
170 // we use the same id for all taskbar icons as we don't need it to
171 // distinguish between them
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
180 wxTaskBarIcon::wxTaskBarIcon(wxTaskBarIconType
WXUNUSED(iconType
))
184 RegisterWindowMessages();
187 wxTaskBarIcon::~wxTaskBarIcon()
194 // we must use delete and not Destroy() here because the latter will
195 // only schedule the window to be deleted during the next idle event
196 // processing but we may not get any idle events if there are no other
197 // windows left in the program
203 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
205 // NB: we have to create the window lazily because of backward compatibility,
206 // old applications may create a wxTaskBarIcon instance before wxApp
207 // is initialized (as samples/taskbar used to do)
210 m_win
= new wxTaskBarIconWindow(this);
214 m_strTooltip
= tooltip
;
216 NotifyIconData
notifyData(GetHwndOf(m_win
));
220 notifyData
.uFlags
|= NIF_ICON
;
221 notifyData
.hIcon
= GetHiconOf(icon
);
224 // set NIF_TIP even for an empty tooltip: otherwise it would be impossible
225 // to remove an existing tooltip using this function
226 notifyData
.uFlags
|= NIF_TIP
;
227 if ( !tooltip
.empty() )
229 wxStrlcpy(notifyData
.szTip
, tooltip
.wx_str(), WXSIZEOF(notifyData
.szTip
));
232 bool ok
= wxShellNotifyIcon(m_iconAdded
? NIM_MODIFY
233 : NIM_ADD
, ¬ifyData
) != 0;
237 wxLogLastError(wxT("wxShellNotifyIcon(NIM_MODIFY/ADD)"));
240 if ( !m_iconAdded
&& ok
)
246 #if wxUSE_TASKBARICON_BALLOONS
249 wxTaskBarIcon::ShowBalloon(const wxString
& title
,
250 const wxString
& text
,
254 wxCHECK_MSG( m_iconAdded
, false,
255 wxT("can't be used before the icon is created") );
257 const HWND hwnd
= GetHwndOf(m_win
);
259 // we need to enable version 5.0 behaviour to receive notifications about
260 // the balloon disappearance
261 NotifyIconData
notifyData(hwnd
);
262 notifyData
.uFlags
= 0;
263 notifyData
.uVersion
= 3 /* NOTIFYICON_VERSION for Windows 2000/XP */;
265 if ( !wxShellNotifyIcon(NIM_SETVERSION
, ¬ifyData
) )
267 wxLogLastError(wxT("wxShellNotifyIcon(NIM_SETVERSION)"));
270 // do show the balloon now
271 notifyData
= NotifyIconData(hwnd
);
272 notifyData
.uFlags
|= NIF_INFO
;
273 notifyData
.uTimeout
= msec
;
274 wxStrlcpy(notifyData
.szInfo
, text
.wx_str(), WXSIZEOF(notifyData
.szInfo
));
275 wxStrlcpy(notifyData
.szInfoTitle
, title
.wx_str(),
276 WXSIZEOF(notifyData
.szInfoTitle
));
278 if ( flags
& wxICON_INFORMATION
)
279 notifyData
.dwInfoFlags
|= NIIF_INFO
;
280 else if ( flags
& wxICON_WARNING
)
281 notifyData
.dwInfoFlags
|= NIIF_WARNING
;
282 else if ( flags
& wxICON_ERROR
)
283 notifyData
.dwInfoFlags
|= NIIF_ERROR
;
285 bool ok
= wxShellNotifyIcon(NIM_MODIFY
, ¬ifyData
) != 0;
288 wxLogLastError(wxT("wxShellNotifyIcon(NIM_MODIFY)"));
294 #endif // wxUSE_TASKBARICON_BALLOONS
296 bool wxTaskBarIcon::RemoveIcon()
303 NotifyIconData
notifyData(GetHwndOf(m_win
));
305 bool ok
= wxShellNotifyIcon(NIM_DELETE
, ¬ifyData
) != 0;
308 wxLogLastError(wxT("wxShellNotifyIcon(NIM_DELETE)"));
315 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
)
317 wxASSERT_MSG( m_win
!= NULL
, wxT("taskbar icon not initialized") );
319 static bool s_inPopup
= false;
327 wxGetMousePosition(&x
, &y
);
331 m_win
->PushEventHandler(this);
335 // the SetForegroundWindow() and PostMessage() calls are needed to work
336 // around Win32 bug with the popup menus shown for the notifications as
337 // documented at http://support.microsoft.com/kb/q135788/
338 ::SetForegroundWindow(GetHwndOf(m_win
));
340 bool rval
= m_win
->PopupMenu(menu
, 0, 0);
342 ::PostMessage(GetHwndOf(m_win
), WM_NULL
, 0, 0L);
344 m_win
->PopEventHandler(false);
350 #endif // wxUSE_MENUS
352 void wxTaskBarIcon::RegisterWindowMessages()
354 static bool s_registered
= false;
358 // Taskbar restart msg will be sent to us if the icon needs to be redrawn
359 gs_msgRestartTaskbar
= RegisterWindowMessage(wxT("TaskbarCreated"));
361 // Also register the taskbar message here
362 gs_msgTaskbar
= ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
368 // ----------------------------------------------------------------------------
369 // wxTaskBarIcon window proc
370 // ----------------------------------------------------------------------------
372 long wxTaskBarIcon::WindowProc(unsigned int msg
,
373 unsigned int WXUNUSED(wParam
),
376 if ( msg
== gs_msgRestartTaskbar
) // does the icon need to be redrawn?
379 SetIcon(m_icon
, m_strTooltip
);
383 // this function should only be called for gs_msg(Restart)Taskbar messages
384 wxASSERT( msg
== gs_msgTaskbar
);
386 wxEventType eventType
= 0;
390 eventType
= wxEVT_TASKBAR_LEFT_DOWN
;
394 eventType
= wxEVT_TASKBAR_LEFT_UP
;
398 eventType
= wxEVT_TASKBAR_RIGHT_DOWN
;
402 eventType
= wxEVT_TASKBAR_RIGHT_UP
;
405 case WM_LBUTTONDBLCLK
:
406 eventType
= wxEVT_TASKBAR_LEFT_DCLICK
;
409 case WM_RBUTTONDBLCLK
:
410 eventType
= wxEVT_TASKBAR_RIGHT_DCLICK
;
414 eventType
= wxEVT_TASKBAR_MOVE
;
417 case NIN_BALLOONTIMEOUT
:
418 eventType
= wxEVT_TASKBAR_BALLOON_TIMEOUT
;
421 case NIN_BALLOONUSERCLICK
:
422 eventType
= wxEVT_TASKBAR_BALLOON_CLICK
;
428 wxTaskBarIconEvent
event(eventType
, this);
436 #endif // wxUSE_TASKBARICON