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/taskbar.h>
37 #include <wx/msw/private.h>
41 #ifndef wxUSE_NORLANDER_HEADERS
42 #include <wx/msw/gnuwin32/extra.h>
51 LRESULT APIENTRY _EXPORT
wxTaskBarIconWindowProc( HWND hWnd
, unsigned msg
,
52 UINT wParam
, LONG lParam
);
54 wxChar
*wxTaskBarWindowClass
= _T("wxTaskBarWindowClass");
56 wxList
wxTaskBarIcon::sm_taskBarIcons
;
57 bool wxTaskBarIcon::sm_registeredClass
= FALSE
;
58 UINT
wxTaskBarIcon::sm_taskbarMsg
= 0;
61 #if !USE_SHARED_LIBRARY
62 BEGIN_EVENT_TABLE(wxTaskBarIcon
, wxEvtHandler
)
63 EVT_TASKBAR_MOVE (wxTaskBarIcon::_OnMouseMove
)
64 EVT_TASKBAR_LEFT_DOWN (wxTaskBarIcon::_OnLButtonDown
)
65 EVT_TASKBAR_LEFT_UP (wxTaskBarIcon::_OnLButtonUp
)
66 EVT_TASKBAR_RIGHT_DOWN (wxTaskBarIcon::_OnRButtonDown
)
67 EVT_TASKBAR_RIGHT_UP (wxTaskBarIcon::_OnRButtonUp
)
68 EVT_TASKBAR_LEFT_DCLICK (wxTaskBarIcon::_OnLButtonDClick
)
69 EVT_TASKBAR_RIGHT_DCLICK (wxTaskBarIcon::_OnRButtonDClick
)
73 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
77 wxTaskBarIcon::wxTaskBarIcon(void)
84 if (RegisterWindowClass())
85 m_hWnd
= CreateTaskBarWindow();
88 wxTaskBarIcon::~wxTaskBarIcon(void)
99 ::DestroyWindow((HWND
) m_hWnd
);
105 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
110 NOTIFYICONDATA notifyData
;
112 memset(¬ifyData
, 0, sizeof(notifyData
));
113 notifyData
.cbSize
= sizeof(notifyData
);
114 notifyData
.hWnd
= (HWND
) m_hWnd
;
115 notifyData
.uCallbackMessage
= sm_taskbarMsg
;
116 notifyData
.uFlags
= NIF_MESSAGE
;
119 notifyData
.uFlags
|= NIF_ICON
;
120 notifyData
.hIcon
= (HICON
) icon
.GetHICON();
123 if (((const wxChar
*) tooltip
!= NULL
) && (tooltip
!= _T("")))
125 notifyData
.uFlags
|= NIF_TIP
;
126 lstrcpyn(notifyData
.szTip
, WXSTRINGCAST tooltip
, sizeof(notifyData
.szTip
));
132 return (Shell_NotifyIcon(NIM_MODIFY
, & notifyData
) != 0);
135 m_iconAdded
= (Shell_NotifyIcon(NIM_ADD
, & notifyData
) != 0);
140 bool wxTaskBarIcon::RemoveIcon(void)
145 NOTIFYICONDATA notifyData
;
147 memset(¬ifyData
, 0, sizeof(notifyData
));
148 notifyData
.cbSize
= sizeof(notifyData
);
149 notifyData
.hWnd
= (HWND
) m_hWnd
;
150 notifyData
.uCallbackMessage
= sm_taskbarMsg
;
151 notifyData
.uFlags
= NIF_MESSAGE
;
152 notifyData
.hIcon
= 0 ; // hIcon;
156 return (Shell_NotifyIcon(NIM_DELETE
, & notifyData
) != 0);
159 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
) //, int x, int y);
164 wxGetMousePosition(&x
, &y
);
166 // is wxFrame the best window type to use???
167 win
= new wxFrame(NULL
, -1, "", wxPoint(x
,y
), wxSize(-1,-1), 0);
168 win
->PushEventHandler(this);
170 // Remove from record of top-level windows, or will confuse wxWindows
171 // if we try to exit right now.
172 wxTopLevelWindows
.DeleteObject(win
);
176 rval
= win
->PopupMenu(menu
, 0, 0);
178 win
->PopEventHandler(FALSE
);
187 void wxTaskBarIcon::OnMouseMove(wxEvent
&)
191 void wxTaskBarIcon::OnLButtonDown(wxEvent
&)
195 void wxTaskBarIcon::OnLButtonUp(wxEvent
&)
199 void wxTaskBarIcon::OnRButtonDown(wxEvent
&)
203 void wxTaskBarIcon::OnRButtonUp(wxEvent
&)
207 void wxTaskBarIcon::OnLButtonDClick(wxEvent
&)
211 void wxTaskBarIcon::OnRButtonDClick(wxEvent
&)
215 void wxTaskBarIcon::_OnMouseMove(wxEvent
& e
) { OnMouseMove(e
); }
216 void wxTaskBarIcon::_OnLButtonDown(wxEvent
& e
) { OnLButtonDown(e
); }
217 void wxTaskBarIcon::_OnLButtonUp(wxEvent
& e
) { OnLButtonUp(e
); }
218 void wxTaskBarIcon::_OnRButtonDown(wxEvent
& e
) { OnRButtonDown(e
); }
219 void wxTaskBarIcon::_OnRButtonUp(wxEvent
& e
) { OnRButtonUp(e
); }
220 void wxTaskBarIcon::_OnLButtonDClick(wxEvent
& e
) { OnLButtonDClick(e
); }
221 void wxTaskBarIcon::_OnRButtonDClick(wxEvent
& e
) { OnRButtonDClick(e
); }
224 wxTaskBarIcon
* wxTaskBarIcon::FindObjectForHWND(WXHWND hWnd
)
226 wxNode
*node
= sm_taskBarIcons
.First();
229 wxTaskBarIcon
* obj
= (wxTaskBarIcon
*) node
->Data();
230 if (obj
->GetHWND() == hWnd
)
237 void wxTaskBarIcon::AddObject(wxTaskBarIcon
* obj
)
239 sm_taskBarIcons
.Append(obj
);
242 void wxTaskBarIcon::RemoveObject(wxTaskBarIcon
* obj
)
244 sm_taskBarIcons
.DeleteObject(obj
);
247 bool wxTaskBarIcon::RegisterWindowClass()
249 if (sm_registeredClass
)
252 // Also register the taskbar message here
253 sm_taskbarMsg
= ::RegisterWindowMessage(_T("wxTaskBarIconMessage"));
258 HINSTANCE hInstance
= GetModuleHandle(NULL
);
261 * set up and register window class
263 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
264 wc
.lpfnWndProc
= (WNDPROC
) wxTaskBarIconWindowProc
;
267 wc
.hInstance
= hInstance
;
270 wc
.hbrBackground
= 0;
271 wc
.lpszMenuName
= NULL
;
272 wc
.lpszClassName
= wxTaskBarWindowClass
;
273 rc
= (::RegisterClass( &wc
) != 0);
275 sm_registeredClass
= (rc
!= 0);
280 WXHWND
wxTaskBarIcon::CreateTaskBarWindow()
282 HINSTANCE hInstance
= GetModuleHandle(NULL
);
284 HWND hWnd
= CreateWindowEx (0, wxTaskBarWindowClass
,
285 _T("wxTaskBarWindow"),
296 return (WXHWND
) hWnd
;
299 long wxTaskBarIcon::WindowProc( WXHWND hWnd
, unsigned int msg
, unsigned int wParam
, long lParam
)
301 wxEventType eventType
= 0;
303 if (msg
!= sm_taskbarMsg
)
304 return DefWindowProc((HWND
) hWnd
, msg
, wParam
, lParam
);
309 eventType
= wxEVT_TASKBAR_LEFT_DOWN
;
313 eventType
= wxEVT_TASKBAR_LEFT_UP
;
317 eventType
= wxEVT_TASKBAR_RIGHT_DOWN
;
321 eventType
= wxEVT_TASKBAR_RIGHT_UP
;
324 case WM_LBUTTONDBLCLK
:
325 eventType
= wxEVT_TASKBAR_LEFT_DCLICK
;
328 case WM_RBUTTONDBLCLK
:
329 eventType
= wxEVT_TASKBAR_RIGHT_DCLICK
;
333 eventType
= wxEVT_TASKBAR_MOVE
;
342 event
.SetEventType(eventType
);
343 event
.SetEventObject(this);
350 LRESULT APIENTRY _EXPORT
wxTaskBarIconWindowProc( HWND hWnd
, unsigned msg
,
351 UINT wParam
, LONG lParam
)
353 wxTaskBarIcon
* obj
= wxTaskBarIcon::FindObjectForHWND((WXHWND
) hWnd
);
355 return obj
->WindowProc((WXHWND
) hWnd
, msg
, wParam
, lParam
);
357 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);