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/msw/taskbar.h"
40 #include "wx/msw/private.h"
43 #ifdef __GNUWIN32_OLD__
44 #include "wx/msw/gnuwin32/extra.h"
52 LRESULT APIENTRY _EXPORT
wxTaskBarIconWindowProc( HWND hWnd
, unsigned msg
,
53 UINT wParam
, LONG lParam
);
55 wxChar
*wxTaskBarWindowClass
= (wxChar
*) wxT("wxTaskBarWindowClass");
57 wxList
wxTaskBarIcon::sm_taskBarIcons
;
58 bool wxTaskBarIcon::sm_registeredClass
= FALSE
;
59 UINT
wxTaskBarIcon::sm_taskbarMsg
= 0;
61 DEFINE_EVENT_TYPE( wxEVT_TASKBAR_MOVE
)
62 DEFINE_EVENT_TYPE( wxEVT_TASKBAR_LEFT_DOWN
)
63 DEFINE_EVENT_TYPE( wxEVT_TASKBAR_LEFT_UP
)
64 DEFINE_EVENT_TYPE( wxEVT_TASKBAR_RIGHT_DOWN
)
65 DEFINE_EVENT_TYPE( wxEVT_TASKBAR_RIGHT_UP
)
66 DEFINE_EVENT_TYPE( wxEVT_TASKBAR_LEFT_DCLICK
)
67 DEFINE_EVENT_TYPE( wxEVT_TASKBAR_RIGHT_DCLICK
)
69 BEGIN_EVENT_TABLE(wxTaskBarIcon
, wxEvtHandler
)
70 EVT_TASKBAR_MOVE (wxTaskBarIcon::_OnMouseMove
)
71 EVT_TASKBAR_LEFT_DOWN (wxTaskBarIcon::_OnLButtonDown
)
72 EVT_TASKBAR_LEFT_UP (wxTaskBarIcon::_OnLButtonUp
)
73 EVT_TASKBAR_RIGHT_DOWN (wxTaskBarIcon::_OnRButtonDown
)
74 EVT_TASKBAR_RIGHT_UP (wxTaskBarIcon::_OnRButtonUp
)
75 EVT_TASKBAR_LEFT_DCLICK (wxTaskBarIcon::_OnLButtonDClick
)
76 EVT_TASKBAR_RIGHT_DCLICK (wxTaskBarIcon::_OnRButtonDClick
)
80 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
83 wxTaskBarIcon::wxTaskBarIcon(void)
90 if (RegisterWindowClass())
91 m_hWnd
= CreateTaskBarWindow();
94 wxTaskBarIcon::~wxTaskBarIcon(void)
105 ::DestroyWindow((HWND
) m_hWnd
);
111 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
116 NOTIFYICONDATA notifyData
;
118 memset(¬ifyData
, 0, sizeof(notifyData
));
119 notifyData
.cbSize
= sizeof(notifyData
);
120 notifyData
.hWnd
= (HWND
) m_hWnd
;
121 notifyData
.uCallbackMessage
= sm_taskbarMsg
;
122 notifyData
.uFlags
= NIF_MESSAGE
;
125 notifyData
.uFlags
|= NIF_ICON
;
126 notifyData
.hIcon
= (HICON
) icon
.GetHICON();
129 if (((const wxChar
*) tooltip
!= NULL
) && (tooltip
!= wxT("")))
131 notifyData
.uFlags
|= NIF_TIP
;
132 lstrcpyn(notifyData
.szTip
, WXSTRINGCAST tooltip
, sizeof(notifyData
.szTip
));
138 return (Shell_NotifyIcon(NIM_MODIFY
, & notifyData
) != 0);
141 m_iconAdded
= (Shell_NotifyIcon(NIM_ADD
, & notifyData
) != 0);
146 bool wxTaskBarIcon::RemoveIcon(void)
151 NOTIFYICONDATA notifyData
;
153 memset(¬ifyData
, 0, sizeof(notifyData
));
154 notifyData
.cbSize
= sizeof(notifyData
);
155 notifyData
.hWnd
= (HWND
) m_hWnd
;
156 notifyData
.uCallbackMessage
= sm_taskbarMsg
;
157 notifyData
.uFlags
= NIF_MESSAGE
;
158 notifyData
.hIcon
= 0 ; // hIcon;
162 return (Shell_NotifyIcon(NIM_DELETE
, & notifyData
) != 0);
165 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
) //, int x, int y);
167 // OK, so I know this isn't thread-friendly, but
168 // what to do? We need this check.
170 static bool s_inPopup
= FALSE
;
180 wxGetMousePosition(&x
, &y
);
182 // is wxFrame the best window type to use???
183 win
= new wxFrame(NULL
, -1, "", wxPoint(x
,y
), wxSize(-1,-1), 0);
184 win
->PushEventHandler(this);
186 // Remove from record of top-level windows, or will confuse wxWindows
187 // if we try to exit right now.
188 wxTopLevelWindows
.DeleteObject(win
);
192 // Work around a WIN32 bug
193 ::SetForegroundWindow ((HWND
) win
->GetHWND ());
195 rval
= win
->PopupMenu(menu
, 0, 0);
197 // Work around a WIN32 bug
198 ::PostMessage ((HWND
) win
->GetHWND(),WM_NULL
,0,0L);
200 win
->PopEventHandler(FALSE
);
211 void wxTaskBarIcon::OnMouseMove(wxEvent
&)
215 void wxTaskBarIcon::OnLButtonDown(wxEvent
&)
219 void wxTaskBarIcon::OnLButtonUp(wxEvent
&)
223 void wxTaskBarIcon::OnRButtonDown(wxEvent
&)
227 void wxTaskBarIcon::OnRButtonUp(wxEvent
&)
231 void wxTaskBarIcon::OnLButtonDClick(wxEvent
&)
235 void wxTaskBarIcon::OnRButtonDClick(wxEvent
&)
239 void wxTaskBarIcon::_OnMouseMove(wxEvent
& e
) { OnMouseMove(e
); }
240 void wxTaskBarIcon::_OnLButtonDown(wxEvent
& e
) { OnLButtonDown(e
); }
241 void wxTaskBarIcon::_OnLButtonUp(wxEvent
& e
) { OnLButtonUp(e
); }
242 void wxTaskBarIcon::_OnRButtonDown(wxEvent
& e
) { OnRButtonDown(e
); }
243 void wxTaskBarIcon::_OnRButtonUp(wxEvent
& e
) { OnRButtonUp(e
); }
244 void wxTaskBarIcon::_OnLButtonDClick(wxEvent
& e
) { OnLButtonDClick(e
); }
245 void wxTaskBarIcon::_OnRButtonDClick(wxEvent
& e
) { OnRButtonDClick(e
); }
248 wxTaskBarIcon
* wxTaskBarIcon::FindObjectForHWND(WXHWND hWnd
)
250 wxNode
*node
= sm_taskBarIcons
.First();
253 wxTaskBarIcon
* obj
= (wxTaskBarIcon
*) node
->Data();
254 if (obj
->GetHWND() == hWnd
)
261 void wxTaskBarIcon::AddObject(wxTaskBarIcon
* obj
)
263 sm_taskBarIcons
.Append(obj
);
266 void wxTaskBarIcon::RemoveObject(wxTaskBarIcon
* obj
)
268 sm_taskBarIcons
.DeleteObject(obj
);
271 bool wxTaskBarIcon::RegisterWindowClass()
273 if (sm_registeredClass
)
276 // Also register the taskbar message here
277 sm_taskbarMsg
= ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
282 HINSTANCE hInstance
= GetModuleHandle(NULL
);
285 * set up and register window class
287 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
288 wc
.lpfnWndProc
= (WNDPROC
) wxTaskBarIconWindowProc
;
291 wc
.hInstance
= hInstance
;
294 wc
.hbrBackground
= 0;
295 wc
.lpszMenuName
= NULL
;
296 wc
.lpszClassName
= wxTaskBarWindowClass
;
297 rc
= (::RegisterClass( &wc
) != 0);
299 sm_registeredClass
= (rc
!= 0);
304 WXHWND
wxTaskBarIcon::CreateTaskBarWindow()
306 HINSTANCE hInstance
= GetModuleHandle(NULL
);
308 HWND hWnd
= CreateWindowEx (0, wxTaskBarWindowClass
,
309 wxT("wxTaskBarWindow"),
320 return (WXHWND
) hWnd
;
323 long wxTaskBarIcon::WindowProc( WXHWND hWnd
, unsigned int msg
, unsigned int wParam
, long lParam
)
325 wxEventType eventType
= 0;
327 if (msg
!= sm_taskbarMsg
)
328 return DefWindowProc((HWND
) hWnd
, msg
, wParam
, lParam
);
333 eventType
= wxEVT_TASKBAR_LEFT_DOWN
;
337 eventType
= wxEVT_TASKBAR_LEFT_UP
;
341 eventType
= wxEVT_TASKBAR_RIGHT_DOWN
;
345 eventType
= wxEVT_TASKBAR_RIGHT_UP
;
348 case WM_LBUTTONDBLCLK
:
349 eventType
= wxEVT_TASKBAR_LEFT_DCLICK
;
352 case WM_RBUTTONDBLCLK
:
353 eventType
= wxEVT_TASKBAR_RIGHT_DCLICK
;
357 eventType
= wxEVT_TASKBAR_MOVE
;
365 wxTaskBarIconEvent
event(eventType
, this);
372 LRESULT APIENTRY _EXPORT
wxTaskBarIconWindowProc( HWND hWnd
, unsigned msg
,
373 UINT wParam
, LONG lParam
)
375 wxTaskBarIcon
* obj
= wxTaskBarIcon::FindObjectForHWND((WXHWND
) hWnd
);
377 return obj
->WindowProc((WXHWND
) hWnd
, msg
, wParam
, lParam
);
379 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);