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"
34 // initialized on demand
35 UINT gs_msgTaskbar
= 0;
36 UINT gs_msgRestartTaskbar
= 0;
39 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
41 // ============================================================================
43 // ============================================================================
45 // ----------------------------------------------------------------------------
46 // wxTaskBarIconWindow: helper window
47 // ----------------------------------------------------------------------------
49 // NB: this class serves two purposes:
50 // 1. win32 needs a HWND associated with taskbar icon, this provides it
51 // 2. we need wxTopLevelWindow so that the app doesn't exit when
52 // last frame is closed but there still is a taskbar icon
53 class wxTaskBarIconWindow
: public wxFrame
56 wxTaskBarIconWindow(wxTaskBarIcon
*icon
)
57 : wxFrame(NULL
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
, 0),
62 WXLRESULT
MSWWindowProc(WXUINT msg
,
63 WXWPARAM wParam
, WXLPARAM lParam
)
65 if (msg
== gs_msgRestartTaskbar
|| msg
== gs_msgTaskbar
)
67 return m_icon
->WindowProc(msg
, wParam
, lParam
);
71 return wxFrame::MSWWindowProc(msg
, wParam
, lParam
);
76 wxTaskBarIcon
*m_icon
;
80 // ----------------------------------------------------------------------------
81 // NotifyIconData: wrapper around NOTIFYICONDATA
82 // ----------------------------------------------------------------------------
84 struct NotifyIconData
: public NOTIFYICONDATA
86 NotifyIconData(WXHWND hwnd
)
88 memset(this, 0, sizeof(NOTIFYICONDATA
));
89 cbSize
= sizeof(NOTIFYICONDATA
);
91 uCallbackMessage
= gs_msgTaskbar
;
94 // we use the same id for all taskbar icons as we don't need it to
95 // distinguish between them
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 wxTaskBarIcon::wxTaskBarIcon()
108 RegisterWindowMessages();
111 wxTaskBarIcon::~wxTaskBarIcon()
121 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
123 // NB: we have to create the window lazily because of backward compatibility,
124 // old applications may create a wxTaskBarIcon instance before wxApp
125 // is initialized (as samples/taskbar used to do)
128 m_win
= new wxTaskBarIconWindow(this);
132 m_strTooltip
= tooltip
;
134 NotifyIconData
notifyData(GetHwndOf(m_win
));
138 notifyData
.uFlags
|= NIF_ICON
;
139 notifyData
.hIcon
= GetHiconOf(icon
);
142 // set NIF_TIP even for an empty tooltip: otherwise it would be impossible
143 // to remove an existing tooltip using this function
144 notifyData
.uFlags
|= NIF_TIP
;
145 if ( !tooltip
.empty() )
147 wxStrncpy(notifyData
.szTip
, tooltip
.c_str(), WXSIZEOF(notifyData
.szTip
));
150 bool ok
= Shell_NotifyIcon(m_iconAdded
? NIM_MODIFY
151 : NIM_ADD
, ¬ifyData
) != 0;
153 if ( !m_iconAdded
&& ok
)
159 bool wxTaskBarIcon::RemoveIcon()
166 NotifyIconData
notifyData(GetHwndOf(m_win
));
168 return Shell_NotifyIcon(NIM_DELETE
, ¬ifyData
) != 0;
172 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
)
174 wxASSERT_MSG( m_win
!= NULL
, _T("taskbar icon not initialized") );
176 static bool s_inPopup
= false;
184 wxGetMousePosition(&x
, &y
);
188 m_win
->PushEventHandler(this);
192 // the SetForegroundWindow() and PostMessage() calls are needed to work
193 // around Win32 bug with the popup menus shown for the notifications as
194 // documented at http://support.microsoft.com/kb/q135788/
195 ::SetForegroundWindow(GetHwndOf(m_win
));
197 bool rval
= m_win
->PopupMenu(menu
, 0, 0);
199 ::PostMessage(GetHwndOf(m_win
), WM_NULL
, 0, 0L);
201 m_win
->PopEventHandler(false);
207 #endif // wxUSE_MENUS
209 void wxTaskBarIcon::RegisterWindowMessages()
211 static bool s_registered
= false;
215 // Taskbar restart msg will be sent to us if the icon needs to be redrawn
216 gs_msgRestartTaskbar
= RegisterWindowMessage(wxT("TaskbarCreated"));
218 // Also register the taskbar message here
219 gs_msgTaskbar
= ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
225 // ----------------------------------------------------------------------------
226 // wxTaskBarIcon window proc
227 // ----------------------------------------------------------------------------
229 long wxTaskBarIcon::WindowProc(unsigned int msg
,
230 unsigned int WXUNUSED(wParam
),
233 wxEventType eventType
= 0;
235 if (msg
== gs_msgRestartTaskbar
) // does the icon need to be redrawn?
238 SetIcon(m_icon
, m_strTooltip
);
241 // this function should only be called for gs_msg(Restart)Taskbar messages
242 wxASSERT(msg
== gs_msgTaskbar
);
247 eventType
= wxEVT_TASKBAR_LEFT_DOWN
;
251 eventType
= wxEVT_TASKBAR_LEFT_UP
;
255 eventType
= wxEVT_TASKBAR_RIGHT_DOWN
;
259 eventType
= wxEVT_TASKBAR_RIGHT_UP
;
262 case WM_LBUTTONDBLCLK
:
263 eventType
= wxEVT_TASKBAR_LEFT_DCLICK
;
266 case WM_RBUTTONDBLCLK
:
267 eventType
= wxEVT_TASKBAR_RIGHT_DCLICK
;
271 eventType
= wxEVT_TASKBAR_MOVE
;
280 wxTaskBarIconEvent
event(eventType
, this);
288 #endif // wxUSE_TASKBARICON