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"
30 #include <wx/msw/taskbar.h>
33 #include <wx/msw/private.h>
36 #include <wx/msw/gnuwin32/extra.h>
39 LRESULT APIENTRY _EXPORT
wxTaskBarIconWindowProc( HWND hWnd
, unsigned msg
,
40 UINT wParam
, LONG lParam
);
42 char *wxTaskBarWindowClass
= "wxTaskBarWindowClass";
44 wxList
wxTaskBarIcon::sm_taskBarIcons
;
45 bool wxTaskBarIcon::sm_registeredClass
= FALSE
;
46 UINT
wxTaskBarIcon::sm_taskbarMsg
= 0;
49 #if !USE_SHARED_LIBRARY
50 BEGIN_EVENT_TABLE(wxTaskBarIcon
, wxEvtHandler
)
51 EVT_TASKBAR_MOVE (wxTaskBarIcon::_OnMouseMove
)
52 EVT_TASKBAR_LEFT_DOWN (wxTaskBarIcon::_OnLButtonDown
)
53 EVT_TASKBAR_LEFT_UP (wxTaskBarIcon::_OnLButtonUp
)
54 EVT_TASKBAR_RIGHT_DOWN (wxTaskBarIcon::_OnRButtonDown
)
55 EVT_TASKBAR_RIGHT_UP (wxTaskBarIcon::_OnRButtonUp
)
56 EVT_TASKBAR_LEFT_DCLICK (wxTaskBarIcon::_OnLButtonDClick
)
57 EVT_TASKBAR_RIGHT_DCLICK (wxTaskBarIcon::_OnRButtonDClick
)
61 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
65 wxTaskBarIcon::wxTaskBarIcon(void)
72 if (RegisterWindowClass())
73 m_hWnd
= CreateTaskBarWindow();
76 wxTaskBarIcon::~wxTaskBarIcon(void)
87 ::DestroyWindow((HWND
) m_hWnd
);
93 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
98 NOTIFYICONDATA notifyData
;
100 memset(¬ifyData
, 0, sizeof(notifyData
));
101 notifyData
.cbSize
= sizeof(notifyData
);
102 notifyData
.hWnd
= (HWND
) m_hWnd
;
103 notifyData
.uCallbackMessage
= sm_taskbarMsg
;
104 notifyData
.uFlags
= NIF_MESSAGE
;
107 notifyData
.uFlags
|= NIF_ICON
;
108 notifyData
.hIcon
= (HICON
) icon
.GetHICON();
111 if (((const char*) tooltip
!= NULL
) && (tooltip
!= ""))
113 notifyData
.uFlags
|= NIF_TIP
;
114 lstrcpyn(notifyData
.szTip
, (char*) (const char*) tooltip
, sizeof(notifyData
.szTip
));
120 return (Shell_NotifyIcon(NIM_MODIFY
, & notifyData
) != 0);
123 m_iconAdded
= (Shell_NotifyIcon(NIM_ADD
, & notifyData
) != 0);
128 bool wxTaskBarIcon::RemoveIcon(void)
133 NOTIFYICONDATA notifyData
;
135 memset(¬ifyData
, 0, sizeof(notifyData
));
136 notifyData
.cbSize
= sizeof(notifyData
);
137 notifyData
.hWnd
= (HWND
) m_hWnd
;
138 notifyData
.uCallbackMessage
= sm_taskbarMsg
;
139 notifyData
.uFlags
= NIF_MESSAGE
;
140 notifyData
.hIcon
= 0 ; // hIcon;
144 return (Shell_NotifyIcon(NIM_DELETE
, & notifyData
) != 0);
147 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
) //, int x, int y);
152 wxGetMousePosition(&x
, &y
);
154 // is wxFrame the best window type to use???
155 win
= new wxFrame(NULL
, -1, "", wxPoint(x
,y
), wxSize(-1,-1), 0);
156 win
->PushEventHandler(this);
158 rval
= win
->PopupMenu(menu
, 0, 0);
160 win
->PopEventHandler(FALSE
);
167 void wxTaskBarIcon::OnMouseMove(wxEvent
&)
171 void wxTaskBarIcon::OnLButtonDown(wxEvent
&)
175 void wxTaskBarIcon::OnLButtonUp(wxEvent
&)
179 void wxTaskBarIcon::OnRButtonDown(wxEvent
&)
183 void wxTaskBarIcon::OnRButtonUp(wxEvent
&)
187 void wxTaskBarIcon::OnLButtonDClick(wxEvent
&)
191 void wxTaskBarIcon::OnRButtonDClick(wxEvent
&)
195 void wxTaskBarIcon::_OnMouseMove(wxEvent
& e
) { OnMouseMove(e
); }
196 void wxTaskBarIcon::_OnLButtonDown(wxEvent
& e
) { OnLButtonDown(e
); }
197 void wxTaskBarIcon::_OnLButtonUp(wxEvent
& e
) { OnLButtonUp(e
); }
198 void wxTaskBarIcon::_OnRButtonDown(wxEvent
& e
) { OnRButtonDown(e
); }
199 void wxTaskBarIcon::_OnRButtonUp(wxEvent
& e
) { OnRButtonUp(e
); }
200 void wxTaskBarIcon::_OnLButtonDClick(wxEvent
& e
) { OnLButtonDClick(e
); }
201 void wxTaskBarIcon::_OnRButtonDClick(wxEvent
& e
) { OnRButtonDClick(e
); }
204 wxTaskBarIcon
* wxTaskBarIcon::FindObjectForHWND(WXHWND hWnd
)
206 wxNode
*node
= sm_taskBarIcons
.First();
209 wxTaskBarIcon
* obj
= (wxTaskBarIcon
*) node
->Data();
210 if (obj
->GetHWND() == hWnd
)
217 void wxTaskBarIcon::AddObject(wxTaskBarIcon
* obj
)
219 sm_taskBarIcons
.Append(obj
);
222 void wxTaskBarIcon::RemoveObject(wxTaskBarIcon
* obj
)
224 sm_taskBarIcons
.DeleteObject(obj
);
227 bool wxTaskBarIcon::RegisterWindowClass()
229 if (sm_registeredClass
)
232 // Also register the taskbar message here
233 sm_taskbarMsg
= ::RegisterWindowMessage("wxTaskBarIconMessage");
238 HINSTANCE hInstance
= GetModuleHandle(NULL
);
241 * set up and register window class
243 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
244 wc
.lpfnWndProc
= (WNDPROC
) wxTaskBarIconWindowProc
;
247 wc
.hInstance
= hInstance
;
250 wc
.hbrBackground
= 0;
251 wc
.lpszMenuName
= NULL
;
252 wc
.lpszClassName
= wxTaskBarWindowClass
;
253 rc
= (::RegisterClass( &wc
) != 0);
255 sm_registeredClass
= (rc
!= 0);
260 WXHWND
wxTaskBarIcon::CreateTaskBarWindow()
262 HINSTANCE hInstance
= GetModuleHandle(NULL
);
264 HWND hWnd
= CreateWindowEx (0, wxTaskBarWindowClass
,
276 return (WXHWND
) hWnd
;
279 long wxTaskBarIcon::WindowProc( WXHWND hWnd
, unsigned int msg
, unsigned int wParam
, long lParam
)
281 wxEventType eventType
= 0;
283 if (msg
!= sm_taskbarMsg
)
284 return DefWindowProc((HWND
) hWnd
, msg
, wParam
, lParam
);
289 eventType
= wxEVT_TASKBAR_LEFT_DOWN
;
293 eventType
= wxEVT_TASKBAR_LEFT_UP
;
297 eventType
= wxEVT_TASKBAR_RIGHT_DOWN
;
301 eventType
= wxEVT_TASKBAR_RIGHT_UP
;
304 case WM_LBUTTONDBLCLK
:
305 eventType
= wxEVT_TASKBAR_LEFT_DCLICK
;
308 case WM_RBUTTONDBLCLK
:
309 eventType
= wxEVT_TASKBAR_RIGHT_DCLICK
;
313 eventType
= wxEVT_TASKBAR_MOVE
;
322 event
.SetEventType(eventType
);
323 event
.SetEventObject(this);
330 LRESULT APIENTRY _EXPORT
wxTaskBarIconWindowProc( HWND hWnd
, unsigned msg
,
331 UINT wParam
, LONG lParam
)
333 wxTaskBarIcon
* obj
= wxTaskBarIcon::FindObjectForHWND((WXHWND
) hWnd
);
335 return obj
->WindowProc((WXHWND
) hWnd
, msg
, wParam
, lParam
);
337 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);