1 /////////////////////////////////////////////////////////////////////////
3 // Purpose: Implements wxTaskBarIcon class for manipulating icons on
4 // the Windows task bar.
5 // Author: Julian Smart
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////
14 #pragma implementation "taskbar.h"
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
26 #include "wx/window.h"
32 #if defined(__WIN95__) && !defined(__TWIN32__)
36 #include "wx/msw/winundef.h"
39 #include "wx/taskbar.h"
40 #include "wx/msw/private.h"
43 #ifdef __GNUWIN32_OLD__
44 #include "wx/msw/gnuwin32/extra.h"
52 #include "wx/listimpl.cpp"
53 WX_DEFINE_LIST(wxTaskBarIconList
);
55 LRESULT APIENTRY _EXPORT
56 wxTaskBarIconWindowProc( HWND hWnd
, unsigned msg
, UINT wParam
, LONG lParam
);
58 wxChar
*wxTaskBarWindowClass
= (wxChar
*) wxT("wxTaskBarWindowClass");
60 wxTaskBarIconList
wxTaskBarIcon::sm_taskBarIcons
;
62 // initialized on demand
63 UINT gs_msgTaskbar
= 0;
64 UINT gs_msgRestartTaskbar
= 0;
66 #if WXWIN_COMPATIBILITY_2_4
67 BEGIN_EVENT_TABLE(wxTaskBarIcon
, wxEvtHandler
)
68 EVT_TASKBAR_MOVE (wxTaskBarIcon::_OnMouseMove
)
69 EVT_TASKBAR_LEFT_DOWN (wxTaskBarIcon::_OnLButtonDown
)
70 EVT_TASKBAR_LEFT_UP (wxTaskBarIcon::_OnLButtonUp
)
71 EVT_TASKBAR_RIGHT_DOWN (wxTaskBarIcon::_OnRButtonDown
)
72 EVT_TASKBAR_RIGHT_UP (wxTaskBarIcon::_OnRButtonUp
)
73 EVT_TASKBAR_LEFT_DCLICK (wxTaskBarIcon::_OnLButtonDClick
)
74 EVT_TASKBAR_RIGHT_DCLICK (wxTaskBarIcon::_OnRButtonDClick
)
79 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
81 // ============================================================================
83 // ============================================================================
85 // ----------------------------------------------------------------------------
86 // NotifyIconData: wrapper around NOTIFYICONDATA
87 // ----------------------------------------------------------------------------
89 struct NotifyIconData
: public NOTIFYICONDATA
91 NotifyIconData(WXHWND hwnd
)
93 memset(this, 0, sizeof(NOTIFYICONDATA
));
94 cbSize
= sizeof(NOTIFYICONDATA
);
96 uCallbackMessage
= gs_msgTaskbar
;
99 // we use the same id for all taskbar icons as we don't need it to
100 // distinguish between them
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 wxTaskBarIcon::wxTaskBarIcon()
116 if (RegisterWindowClass())
117 m_hWnd
= CreateTaskBarWindow();
120 wxTaskBarIcon::~wxTaskBarIcon()
131 ::DestroyWindow((HWND
) m_hWnd
);
137 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
143 m_strTooltip
= tooltip
;
145 NotifyIconData
notifyData(m_hWnd
);
149 notifyData
.uFlags
|= NIF_ICON
;
150 notifyData
.hIcon
= GetHiconOf(icon
);
153 if ( !tooltip
.empty() )
155 notifyData
.uFlags
|= NIF_TIP
;
156 lstrcpyn(notifyData
.szTip
, tooltip
.c_str(), WXSIZEOF(notifyData
.szTip
));
159 bool ok
= Shell_NotifyIcon(m_iconAdded
? NIM_MODIFY
160 : NIM_ADD
, ¬ifyData
) != 0;
162 if ( !m_iconAdded
&& ok
)
168 bool wxTaskBarIcon::RemoveIcon()
175 NotifyIconData
notifyData(m_hWnd
);
177 return Shell_NotifyIcon(NIM_DELETE
, ¬ifyData
) != 0;
180 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
)
182 static bool s_inPopup
= false;
192 wxGetMousePosition(&x
, &y
);
194 // is wxFrame the best window type to use???
195 win
= new wxFrame(NULL
, -1, wxEmptyString
, wxPoint(x
,y
), wxSize(-1,-1), 0);
196 win
->PushEventHandler(this);
198 // Remove from record of top-level windows, or will confuse wxWindows
199 // if we try to exit right now.
200 wxTopLevelWindows
.DeleteObject(win
);
204 // Work around a WIN32 bug
205 ::SetForegroundWindow ((HWND
) win
->GetHWND ());
207 rval
= win
->PopupMenu(menu
, 0, 0);
209 // Work around a WIN32 bug
210 ::PostMessage ((HWND
) win
->GetHWND(),WM_NULL
,0,0L);
212 win
->PopEventHandler(false);
221 #if WXWIN_COMPATIBILITY_2_4
223 void wxTaskBarIcon::OnMouseMove(wxEvent
&) {}
224 void wxTaskBarIcon::OnLButtonDown(wxEvent
&) {}
225 void wxTaskBarIcon::OnLButtonUp(wxEvent
&) {}
226 void wxTaskBarIcon::OnRButtonDown(wxEvent
&) {}
227 void wxTaskBarIcon::OnRButtonUp(wxEvent
&) {}
228 void wxTaskBarIcon::OnLButtonDClick(wxEvent
&) {}
229 void wxTaskBarIcon::OnRButtonDClick(wxEvent
&) {}
231 void wxTaskBarIcon::_OnMouseMove(wxEvent
& e
) { OnMouseMove(e
); }
232 void wxTaskBarIcon::_OnLButtonDown(wxEvent
& e
) { OnLButtonDown(e
); }
233 void wxTaskBarIcon::_OnLButtonUp(wxEvent
& e
) { OnLButtonUp(e
); }
234 void wxTaskBarIcon::_OnRButtonDown(wxEvent
& e
) { OnRButtonDown(e
); }
235 void wxTaskBarIcon::_OnRButtonUp(wxEvent
& e
) { OnRButtonUp(e
); }
236 void wxTaskBarIcon::_OnLButtonDClick(wxEvent
& e
) { OnLButtonDClick(e
); }
237 void wxTaskBarIcon::_OnRButtonDClick(wxEvent
& e
) { OnRButtonDClick(e
); }
240 wxTaskBarIcon
* wxTaskBarIcon::FindObjectForHWND(WXHWND hWnd
)
242 wxTaskBarIconList::Node
*node
= sm_taskBarIcons
.GetFirst();
245 wxTaskBarIcon
*obj
= node
->GetData();
246 if (obj
->GetHWND() == hWnd
)
248 node
= node
->GetNext();
253 void wxTaskBarIcon::AddObject(wxTaskBarIcon
* obj
)
255 sm_taskBarIcons
.Append(obj
);
258 void wxTaskBarIcon::RemoveObject(wxTaskBarIcon
* obj
)
260 sm_taskBarIcons
.DeleteObject(obj
);
263 bool wxTaskBarIcon::RegisterWindowClass()
265 static bool s_registered
= false;
270 // Taskbar restart msg will be sent to us if the icon needs to be redrawn
271 gs_msgRestartTaskbar
= RegisterWindowMessage(wxT("TaskbarCreated"));
273 // Also register the taskbar message here
274 gs_msgTaskbar
= ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
276 // set up and register window class
278 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
279 wc
.lpfnWndProc
= (WNDPROC
) wxTaskBarIconWindowProc
;
282 wc
.hInstance
= wxGetInstance();
285 wc
.hbrBackground
= 0;
286 wc
.lpszMenuName
= NULL
;
287 wc
.lpszClassName
= wxTaskBarWindowClass
;
289 if ( !::RegisterClass(&wc
) )
291 wxLogLastError(_T("RegisterClass(taskbar icon)"));
301 WXHWND
wxTaskBarIcon::CreateTaskBarWindow()
303 HINSTANCE hInstance
= GetModuleHandle(NULL
);
305 HWND hWnd
= CreateWindowEx (0, wxTaskBarWindowClass
,
306 wxT("wxTaskBarWindow"),
317 return (WXHWND
) hWnd
;
320 // ----------------------------------------------------------------------------
321 // wxTaskBarIcon window proc
322 // ----------------------------------------------------------------------------
324 long wxTaskBarIcon::WindowProc(WXHWND hWnd
,
329 wxEventType eventType
= 0;
331 if (msg
== gs_msgRestartTaskbar
) // does the icon need to be redrawn?
334 SetIcon(m_icon
, m_strTooltip
);
337 if (msg
!= gs_msgTaskbar
)
338 return DefWindowProc((HWND
) hWnd
, msg
, wParam
, lParam
);
343 eventType
= wxEVT_TASKBAR_LEFT_DOWN
;
347 eventType
= wxEVT_TASKBAR_LEFT_UP
;
351 eventType
= wxEVT_TASKBAR_RIGHT_DOWN
;
355 eventType
= wxEVT_TASKBAR_RIGHT_UP
;
358 case WM_LBUTTONDBLCLK
:
359 eventType
= wxEVT_TASKBAR_LEFT_DCLICK
;
362 case WM_RBUTTONDBLCLK
:
363 eventType
= wxEVT_TASKBAR_RIGHT_DCLICK
;
367 eventType
= wxEVT_TASKBAR_MOVE
;
376 wxTaskBarIconEvent
event(eventType
, this);
384 LRESULT APIENTRY _EXPORT
385 wxTaskBarIconWindowProc(HWND hWnd
, unsigned msg
, UINT wParam
, LONG lParam
)
387 wxTaskBarIcon
*obj
= wxTaskBarIcon::FindObjectForHWND((WXHWND
) hWnd
);
389 return obj
->WindowProc((WXHWND
) hWnd
, msg
, wParam
, lParam
);
391 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);