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"
35 #include <wx/msw/taskbar.h>
36 #include <wx/msw/private.h>
39 #include <wx/msw/gnuwin32/extra.h>
46 LRESULT APIENTRY _EXPORT
wxTaskBarIconWindowProc( HWND hWnd
, unsigned msg
,
47 UINT wParam
, LONG lParam
);
49 char *wxTaskBarWindowClass
= "wxTaskBarWindowClass";
51 wxList
wxTaskBarIcon::sm_taskBarIcons
;
52 bool wxTaskBarIcon::sm_registeredClass
= FALSE
;
53 UINT
wxTaskBarIcon::sm_taskbarMsg
= 0;
56 #if !USE_SHARED_LIBRARY
57 BEGIN_EVENT_TABLE(wxTaskBarIcon
, wxEvtHandler
)
58 EVT_TASKBAR_MOVE (wxTaskBarIcon::_OnMouseMove
)
59 EVT_TASKBAR_LEFT_DOWN (wxTaskBarIcon::_OnLButtonDown
)
60 EVT_TASKBAR_LEFT_UP (wxTaskBarIcon::_OnLButtonUp
)
61 EVT_TASKBAR_RIGHT_DOWN (wxTaskBarIcon::_OnRButtonDown
)
62 EVT_TASKBAR_RIGHT_UP (wxTaskBarIcon::_OnRButtonUp
)
63 EVT_TASKBAR_LEFT_DCLICK (wxTaskBarIcon::_OnLButtonDClick
)
64 EVT_TASKBAR_RIGHT_DCLICK (wxTaskBarIcon::_OnRButtonDClick
)
68 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
72 wxTaskBarIcon::wxTaskBarIcon(void)
79 if (RegisterWindowClass())
80 m_hWnd
= CreateTaskBarWindow();
83 wxTaskBarIcon::~wxTaskBarIcon(void)
94 ::DestroyWindow((HWND
) m_hWnd
);
100 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
105 NOTIFYICONDATA notifyData
;
107 memset(¬ifyData
, 0, sizeof(notifyData
));
108 notifyData
.cbSize
= sizeof(notifyData
);
109 notifyData
.hWnd
= (HWND
) m_hWnd
;
110 notifyData
.uCallbackMessage
= sm_taskbarMsg
;
111 notifyData
.uFlags
= NIF_MESSAGE
;
114 notifyData
.uFlags
|= NIF_ICON
;
115 notifyData
.hIcon
= (HICON
) icon
.GetHICON();
118 if (((const char*) tooltip
!= NULL
) && (tooltip
!= ""))
120 notifyData
.uFlags
|= NIF_TIP
;
121 lstrcpyn(notifyData
.szTip
, (char*) (const char*) tooltip
, sizeof(notifyData
.szTip
));
127 return (Shell_NotifyIcon(NIM_MODIFY
, & notifyData
) != 0);
130 m_iconAdded
= (Shell_NotifyIcon(NIM_ADD
, & notifyData
) != 0);
135 bool wxTaskBarIcon::RemoveIcon(void)
140 NOTIFYICONDATA notifyData
;
142 memset(¬ifyData
, 0, sizeof(notifyData
));
143 notifyData
.cbSize
= sizeof(notifyData
);
144 notifyData
.hWnd
= (HWND
) m_hWnd
;
145 notifyData
.uCallbackMessage
= sm_taskbarMsg
;
146 notifyData
.uFlags
= NIF_MESSAGE
;
147 notifyData
.hIcon
= 0 ; // hIcon;
151 return (Shell_NotifyIcon(NIM_DELETE
, & notifyData
) != 0);
154 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
) //, int x, int y);
159 wxGetMousePosition(&x
, &y
);
161 // is wxFrame the best window type to use???
162 win
= new wxFrame(NULL
, -1, "", wxPoint(x
,y
), wxSize(-1,-1), 0);
163 win
->PushEventHandler(this);
165 rval
= win
->PopupMenu(menu
, 0, 0);
167 win
->PopEventHandler(FALSE
);
174 void wxTaskBarIcon::OnMouseMove(wxEvent
&)
178 void wxTaskBarIcon::OnLButtonDown(wxEvent
&)
182 void wxTaskBarIcon::OnLButtonUp(wxEvent
&)
186 void wxTaskBarIcon::OnRButtonDown(wxEvent
&)
190 void wxTaskBarIcon::OnRButtonUp(wxEvent
&)
194 void wxTaskBarIcon::OnLButtonDClick(wxEvent
&)
198 void wxTaskBarIcon::OnRButtonDClick(wxEvent
&)
202 void wxTaskBarIcon::_OnMouseMove(wxEvent
& e
) { OnMouseMove(e
); }
203 void wxTaskBarIcon::_OnLButtonDown(wxEvent
& e
) { OnLButtonDown(e
); }
204 void wxTaskBarIcon::_OnLButtonUp(wxEvent
& e
) { OnLButtonUp(e
); }
205 void wxTaskBarIcon::_OnRButtonDown(wxEvent
& e
) { OnRButtonDown(e
); }
206 void wxTaskBarIcon::_OnRButtonUp(wxEvent
& e
) { OnRButtonUp(e
); }
207 void wxTaskBarIcon::_OnLButtonDClick(wxEvent
& e
) { OnLButtonDClick(e
); }
208 void wxTaskBarIcon::_OnRButtonDClick(wxEvent
& e
) { OnRButtonDClick(e
); }
211 wxTaskBarIcon
* wxTaskBarIcon::FindObjectForHWND(WXHWND hWnd
)
213 wxNode
*node
= sm_taskBarIcons
.First();
216 wxTaskBarIcon
* obj
= (wxTaskBarIcon
*) node
->Data();
217 if (obj
->GetHWND() == hWnd
)
224 void wxTaskBarIcon::AddObject(wxTaskBarIcon
* obj
)
226 sm_taskBarIcons
.Append(obj
);
229 void wxTaskBarIcon::RemoveObject(wxTaskBarIcon
* obj
)
231 sm_taskBarIcons
.DeleteObject(obj
);
234 bool wxTaskBarIcon::RegisterWindowClass()
236 if (sm_registeredClass
)
239 // Also register the taskbar message here
240 sm_taskbarMsg
= ::RegisterWindowMessage("wxTaskBarIconMessage");
245 HINSTANCE hInstance
= GetModuleHandle(NULL
);
248 * set up and register window class
250 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
251 wc
.lpfnWndProc
= (WNDPROC
) wxTaskBarIconWindowProc
;
254 wc
.hInstance
= hInstance
;
257 wc
.hbrBackground
= 0;
258 wc
.lpszMenuName
= NULL
;
259 wc
.lpszClassName
= wxTaskBarWindowClass
;
260 rc
= (::RegisterClass( &wc
) != 0);
262 sm_registeredClass
= (rc
!= 0);
267 WXHWND
wxTaskBarIcon::CreateTaskBarWindow()
269 HINSTANCE hInstance
= GetModuleHandle(NULL
);
271 HWND hWnd
= CreateWindowEx (0, wxTaskBarWindowClass
,
283 return (WXHWND
) hWnd
;
286 long wxTaskBarIcon::WindowProc( WXHWND hWnd
, unsigned int msg
, unsigned int wParam
, long lParam
)
288 wxEventType eventType
= 0;
290 if (msg
!= sm_taskbarMsg
)
291 return DefWindowProc((HWND
) hWnd
, msg
, wParam
, lParam
);
296 eventType
= wxEVT_TASKBAR_LEFT_DOWN
;
300 eventType
= wxEVT_TASKBAR_LEFT_UP
;
304 eventType
= wxEVT_TASKBAR_RIGHT_DOWN
;
308 eventType
= wxEVT_TASKBAR_RIGHT_UP
;
311 case WM_LBUTTONDBLCLK
:
312 eventType
= wxEVT_TASKBAR_LEFT_DCLICK
;
315 case WM_RBUTTONDBLCLK
:
316 eventType
= wxEVT_TASKBAR_RIGHT_DCLICK
;
320 eventType
= wxEVT_TASKBAR_MOVE
;
329 event
.SetEventType(eventType
);
330 event
.SetEventObject(this);
337 LRESULT APIENTRY _EXPORT
wxTaskBarIconWindowProc( HWND hWnd
, unsigned msg
,
338 UINT wParam
, LONG lParam
)
340 wxTaskBarIcon
* obj
= wxTaskBarIcon::FindObjectForHWND((WXHWND
) hWnd
);
342 return obj
->WindowProc((WXHWND
) hWnd
, msg
, wParam
, lParam
);
344 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);