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"
31 #if defined(__WIN95__) && !defined(__TWIN32__)
35 #include <wx/msw/taskbar.h>
36 #include <wx/msw/private.h>
40 #ifndef wxUSE_NORLANDER_HEADERS
41 #include <wx/msw/gnuwin32/extra.h>
50 LRESULT APIENTRY _EXPORT
wxTaskBarIconWindowProc( HWND hWnd
, unsigned msg
,
51 UINT wParam
, LONG lParam
);
53 wxChar
*wxTaskBarWindowClass
= _T("wxTaskBarWindowClass");
55 wxList
wxTaskBarIcon::sm_taskBarIcons
;
56 bool wxTaskBarIcon::sm_registeredClass
= FALSE
;
57 UINT
wxTaskBarIcon::sm_taskbarMsg
= 0;
60 #if !USE_SHARED_LIBRARY
61 BEGIN_EVENT_TABLE(wxTaskBarIcon
, wxEvtHandler
)
62 EVT_TASKBAR_MOVE (wxTaskBarIcon::_OnMouseMove
)
63 EVT_TASKBAR_LEFT_DOWN (wxTaskBarIcon::_OnLButtonDown
)
64 EVT_TASKBAR_LEFT_UP (wxTaskBarIcon::_OnLButtonUp
)
65 EVT_TASKBAR_RIGHT_DOWN (wxTaskBarIcon::_OnRButtonDown
)
66 EVT_TASKBAR_RIGHT_UP (wxTaskBarIcon::_OnRButtonUp
)
67 EVT_TASKBAR_LEFT_DCLICK (wxTaskBarIcon::_OnLButtonDClick
)
68 EVT_TASKBAR_RIGHT_DCLICK (wxTaskBarIcon::_OnRButtonDClick
)
72 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
76 wxTaskBarIcon::wxTaskBarIcon(void)
83 if (RegisterWindowClass())
84 m_hWnd
= CreateTaskBarWindow();
87 wxTaskBarIcon::~wxTaskBarIcon(void)
98 ::DestroyWindow((HWND
) m_hWnd
);
104 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
109 NOTIFYICONDATA notifyData
;
111 memset(¬ifyData
, 0, sizeof(notifyData
));
112 notifyData
.cbSize
= sizeof(notifyData
);
113 notifyData
.hWnd
= (HWND
) m_hWnd
;
114 notifyData
.uCallbackMessage
= sm_taskbarMsg
;
115 notifyData
.uFlags
= NIF_MESSAGE
;
118 notifyData
.uFlags
|= NIF_ICON
;
119 notifyData
.hIcon
= (HICON
) icon
.GetHICON();
122 if (((const wxChar
*) tooltip
!= NULL
) && (tooltip
!= _T("")))
124 notifyData
.uFlags
|= NIF_TIP
;
125 lstrcpyn(notifyData
.szTip
, WXSTRINGCAST tooltip
, sizeof(notifyData
.szTip
));
131 return (Shell_NotifyIcon(NIM_MODIFY
, & notifyData
) != 0);
134 m_iconAdded
= (Shell_NotifyIcon(NIM_ADD
, & notifyData
) != 0);
139 bool wxTaskBarIcon::RemoveIcon(void)
144 NOTIFYICONDATA notifyData
;
146 memset(¬ifyData
, 0, sizeof(notifyData
));
147 notifyData
.cbSize
= sizeof(notifyData
);
148 notifyData
.hWnd
= (HWND
) m_hWnd
;
149 notifyData
.uCallbackMessage
= sm_taskbarMsg
;
150 notifyData
.uFlags
= NIF_MESSAGE
;
151 notifyData
.hIcon
= 0 ; // hIcon;
155 return (Shell_NotifyIcon(NIM_DELETE
, & notifyData
) != 0);
158 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
) //, int x, int y);
163 wxGetMousePosition(&x
, &y
);
165 // is wxFrame the best window type to use???
166 win
= new wxFrame(NULL
, -1, "", wxPoint(x
,y
), wxSize(-1,-1), 0);
167 win
->PushEventHandler(this);
169 rval
= win
->PopupMenu(menu
, 0, 0);
171 win
->PopEventHandler(FALSE
);
178 void wxTaskBarIcon::OnMouseMove(wxEvent
&)
182 void wxTaskBarIcon::OnLButtonDown(wxEvent
&)
186 void wxTaskBarIcon::OnLButtonUp(wxEvent
&)
190 void wxTaskBarIcon::OnRButtonDown(wxEvent
&)
194 void wxTaskBarIcon::OnRButtonUp(wxEvent
&)
198 void wxTaskBarIcon::OnLButtonDClick(wxEvent
&)
202 void wxTaskBarIcon::OnRButtonDClick(wxEvent
&)
206 void wxTaskBarIcon::_OnMouseMove(wxEvent
& e
) { OnMouseMove(e
); }
207 void wxTaskBarIcon::_OnLButtonDown(wxEvent
& e
) { OnLButtonDown(e
); }
208 void wxTaskBarIcon::_OnLButtonUp(wxEvent
& e
) { OnLButtonUp(e
); }
209 void wxTaskBarIcon::_OnRButtonDown(wxEvent
& e
) { OnRButtonDown(e
); }
210 void wxTaskBarIcon::_OnRButtonUp(wxEvent
& e
) { OnRButtonUp(e
); }
211 void wxTaskBarIcon::_OnLButtonDClick(wxEvent
& e
) { OnLButtonDClick(e
); }
212 void wxTaskBarIcon::_OnRButtonDClick(wxEvent
& e
) { OnRButtonDClick(e
); }
215 wxTaskBarIcon
* wxTaskBarIcon::FindObjectForHWND(WXHWND hWnd
)
217 wxNode
*node
= sm_taskBarIcons
.First();
220 wxTaskBarIcon
* obj
= (wxTaskBarIcon
*) node
->Data();
221 if (obj
->GetHWND() == hWnd
)
228 void wxTaskBarIcon::AddObject(wxTaskBarIcon
* obj
)
230 sm_taskBarIcons
.Append(obj
);
233 void wxTaskBarIcon::RemoveObject(wxTaskBarIcon
* obj
)
235 sm_taskBarIcons
.DeleteObject(obj
);
238 bool wxTaskBarIcon::RegisterWindowClass()
240 if (sm_registeredClass
)
243 // Also register the taskbar message here
244 sm_taskbarMsg
= ::RegisterWindowMessage(_T("wxTaskBarIconMessage"));
249 HINSTANCE hInstance
= GetModuleHandle(NULL
);
252 * set up and register window class
254 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
255 wc
.lpfnWndProc
= (WNDPROC
) wxTaskBarIconWindowProc
;
258 wc
.hInstance
= hInstance
;
261 wc
.hbrBackground
= 0;
262 wc
.lpszMenuName
= NULL
;
263 wc
.lpszClassName
= wxTaskBarWindowClass
;
264 rc
= (::RegisterClass( &wc
) != 0);
266 sm_registeredClass
= (rc
!= 0);
271 WXHWND
wxTaskBarIcon::CreateTaskBarWindow()
273 HINSTANCE hInstance
= GetModuleHandle(NULL
);
275 HWND hWnd
= CreateWindowEx (0, wxTaskBarWindowClass
,
276 _T("wxTaskBarWindow"),
287 return (WXHWND
) hWnd
;
290 long wxTaskBarIcon::WindowProc( WXHWND hWnd
, unsigned int msg
, unsigned int wParam
, long lParam
)
292 wxEventType eventType
= 0;
294 if (msg
!= sm_taskbarMsg
)
295 return DefWindowProc((HWND
) hWnd
, msg
, wParam
, lParam
);
300 eventType
= wxEVT_TASKBAR_LEFT_DOWN
;
304 eventType
= wxEVT_TASKBAR_LEFT_UP
;
308 eventType
= wxEVT_TASKBAR_RIGHT_DOWN
;
312 eventType
= wxEVT_TASKBAR_RIGHT_UP
;
315 case WM_LBUTTONDBLCLK
:
316 eventType
= wxEVT_TASKBAR_LEFT_DCLICK
;
319 case WM_RBUTTONDBLCLK
:
320 eventType
= wxEVT_TASKBAR_RIGHT_DCLICK
;
324 eventType
= wxEVT_TASKBAR_MOVE
;
333 event
.SetEventType(eventType
);
334 event
.SetEventObject(this);
341 LRESULT APIENTRY _EXPORT
wxTaskBarIconWindowProc( HWND hWnd
, unsigned msg
,
342 UINT wParam
, LONG lParam
)
344 wxTaskBarIcon
* obj
= wxTaskBarIcon::FindObjectForHWND((WXHWND
) hWnd
);
346 return obj
->WindowProc((WXHWND
) hWnd
, msg
, wParam
, lParam
);
348 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);