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>
42 LRESULT APIENTRY _EXPORT
wxTaskBarIconWindowProc( HWND hWnd
, unsigned msg
,
43 UINT wParam
, LONG lParam
);
45 char *wxTaskBarWindowClass
= "wxTaskBarWindowClass";
47 wxList
wxTaskBarIcon::sm_taskBarIcons
;
48 bool wxTaskBarIcon::sm_registeredClass
= FALSE
;
49 UINT
wxTaskBarIcon::sm_taskbarMsg
= 0;
52 #if !USE_SHARED_LIBRARY
53 BEGIN_EVENT_TABLE(wxTaskBarIcon
, wxEvtHandler
)
54 EVT_TASKBAR_MOVE (wxTaskBarIcon::_OnMouseMove
)
55 EVT_TASKBAR_LEFT_DOWN (wxTaskBarIcon::_OnLButtonDown
)
56 EVT_TASKBAR_LEFT_UP (wxTaskBarIcon::_OnLButtonUp
)
57 EVT_TASKBAR_RIGHT_DOWN (wxTaskBarIcon::_OnRButtonDown
)
58 EVT_TASKBAR_RIGHT_UP (wxTaskBarIcon::_OnRButtonUp
)
59 EVT_TASKBAR_LEFT_DCLICK (wxTaskBarIcon::_OnLButtonDClick
)
60 EVT_TASKBAR_RIGHT_DCLICK (wxTaskBarIcon::_OnRButtonDClick
)
64 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
68 wxTaskBarIcon::wxTaskBarIcon(void)
75 if (RegisterWindowClass())
76 m_hWnd
= CreateTaskBarWindow();
79 wxTaskBarIcon::~wxTaskBarIcon(void)
90 ::DestroyWindow((HWND
) m_hWnd
);
96 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
101 NOTIFYICONDATA notifyData
;
103 memset(¬ifyData
, 0, sizeof(notifyData
));
104 notifyData
.cbSize
= sizeof(notifyData
);
105 notifyData
.hWnd
= (HWND
) m_hWnd
;
106 notifyData
.uCallbackMessage
= sm_taskbarMsg
;
107 notifyData
.uFlags
= NIF_MESSAGE
;
110 notifyData
.uFlags
|= NIF_ICON
;
111 notifyData
.hIcon
= (HICON
) icon
.GetHICON();
114 if (((const char*) tooltip
!= NULL
) && (tooltip
!= ""))
116 notifyData
.uFlags
|= NIF_TIP
;
117 lstrcpyn(notifyData
.szTip
, (char*) (const char*) tooltip
, sizeof(notifyData
.szTip
));
123 return (Shell_NotifyIcon(NIM_MODIFY
, & notifyData
) != 0);
126 m_iconAdded
= (Shell_NotifyIcon(NIM_ADD
, & notifyData
) != 0);
131 bool wxTaskBarIcon::RemoveIcon(void)
136 NOTIFYICONDATA notifyData
;
138 memset(¬ifyData
, 0, sizeof(notifyData
));
139 notifyData
.cbSize
= sizeof(notifyData
);
140 notifyData
.hWnd
= (HWND
) m_hWnd
;
141 notifyData
.uCallbackMessage
= sm_taskbarMsg
;
142 notifyData
.uFlags
= NIF_MESSAGE
;
143 notifyData
.hIcon
= 0 ; // hIcon;
147 return (Shell_NotifyIcon(NIM_DELETE
, & notifyData
) != 0);
150 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
) //, int x, int y);
155 wxGetMousePosition(&x
, &y
);
157 // is wxFrame the best window type to use???
158 win
= new wxFrame(NULL
, -1, "", wxPoint(x
,y
), wxSize(-1,-1), 0);
159 win
->PushEventHandler(this);
161 rval
= win
->PopupMenu(menu
, 0, 0);
163 win
->PopEventHandler(FALSE
);
170 void wxTaskBarIcon::OnMouseMove(wxEvent
&)
174 void wxTaskBarIcon::OnLButtonDown(wxEvent
&)
178 void wxTaskBarIcon::OnLButtonUp(wxEvent
&)
182 void wxTaskBarIcon::OnRButtonDown(wxEvent
&)
186 void wxTaskBarIcon::OnRButtonUp(wxEvent
&)
190 void wxTaskBarIcon::OnLButtonDClick(wxEvent
&)
194 void wxTaskBarIcon::OnRButtonDClick(wxEvent
&)
198 void wxTaskBarIcon::_OnMouseMove(wxEvent
& e
) { OnMouseMove(e
); }
199 void wxTaskBarIcon::_OnLButtonDown(wxEvent
& e
) { OnLButtonDown(e
); }
200 void wxTaskBarIcon::_OnLButtonUp(wxEvent
& e
) { OnLButtonUp(e
); }
201 void wxTaskBarIcon::_OnRButtonDown(wxEvent
& e
) { OnRButtonDown(e
); }
202 void wxTaskBarIcon::_OnRButtonUp(wxEvent
& e
) { OnRButtonUp(e
); }
203 void wxTaskBarIcon::_OnLButtonDClick(wxEvent
& e
) { OnLButtonDClick(e
); }
204 void wxTaskBarIcon::_OnRButtonDClick(wxEvent
& e
) { OnRButtonDClick(e
); }
207 wxTaskBarIcon
* wxTaskBarIcon::FindObjectForHWND(WXHWND hWnd
)
209 wxNode
*node
= sm_taskBarIcons
.First();
212 wxTaskBarIcon
* obj
= (wxTaskBarIcon
*) node
->Data();
213 if (obj
->GetHWND() == hWnd
)
220 void wxTaskBarIcon::AddObject(wxTaskBarIcon
* obj
)
222 sm_taskBarIcons
.Append(obj
);
225 void wxTaskBarIcon::RemoveObject(wxTaskBarIcon
* obj
)
227 sm_taskBarIcons
.DeleteObject(obj
);
230 bool wxTaskBarIcon::RegisterWindowClass()
232 if (sm_registeredClass
)
235 // Also register the taskbar message here
236 sm_taskbarMsg
= ::RegisterWindowMessage("wxTaskBarIconMessage");
241 HINSTANCE hInstance
= GetModuleHandle(NULL
);
244 * set up and register window class
246 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
247 wc
.lpfnWndProc
= (WNDPROC
) wxTaskBarIconWindowProc
;
250 wc
.hInstance
= hInstance
;
253 wc
.hbrBackground
= 0;
254 wc
.lpszMenuName
= NULL
;
255 wc
.lpszClassName
= wxTaskBarWindowClass
;
256 rc
= (::RegisterClass( &wc
) != 0);
258 sm_registeredClass
= (rc
!= 0);
263 WXHWND
wxTaskBarIcon::CreateTaskBarWindow()
265 HINSTANCE hInstance
= GetModuleHandle(NULL
);
267 HWND hWnd
= CreateWindowEx (0, wxTaskBarWindowClass
,
279 return (WXHWND
) hWnd
;
282 long wxTaskBarIcon::WindowProc( WXHWND hWnd
, unsigned int msg
, unsigned int wParam
, long lParam
)
284 wxEventType eventType
= 0;
286 if (msg
!= sm_taskbarMsg
)
287 return DefWindowProc((HWND
) hWnd
, msg
, wParam
, lParam
);
292 eventType
= wxEVT_TASKBAR_LEFT_DOWN
;
296 eventType
= wxEVT_TASKBAR_LEFT_UP
;
300 eventType
= wxEVT_TASKBAR_RIGHT_DOWN
;
304 eventType
= wxEVT_TASKBAR_RIGHT_UP
;
307 case WM_LBUTTONDBLCLK
:
308 eventType
= wxEVT_TASKBAR_LEFT_DCLICK
;
311 case WM_RBUTTONDBLCLK
:
312 eventType
= wxEVT_TASKBAR_RIGHT_DCLICK
;
316 eventType
= wxEVT_TASKBAR_MOVE
;
325 event
.SetEventType(eventType
);
326 event
.SetEventObject(this);
333 LRESULT APIENTRY _EXPORT
wxTaskBarIconWindowProc( HWND hWnd
, unsigned msg
,
334 UINT wParam
, LONG lParam
)
336 wxTaskBarIcon
* obj
= wxTaskBarIcon::FindObjectForHWND((WXHWND
) hWnd
);
338 return obj
->WindowProc((WXHWND
) hWnd
, msg
, wParam
, lParam
);
340 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);