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 #include <wx/msw/gnuwin32/extra.h>
48 LRESULT APIENTRY _EXPORT
wxTaskBarIconWindowProc( HWND hWnd
, unsigned msg
,
49 UINT wParam
, LONG lParam
);
51 char *wxTaskBarWindowClass
= "wxTaskBarWindowClass";
53 wxList
wxTaskBarIcon::sm_taskBarIcons
;
54 bool wxTaskBarIcon::sm_registeredClass
= FALSE
;
55 UINT
wxTaskBarIcon::sm_taskbarMsg
= 0;
58 #if !USE_SHARED_LIBRARY
59 BEGIN_EVENT_TABLE(wxTaskBarIcon
, wxEvtHandler
)
60 EVT_TASKBAR_MOVE (wxTaskBarIcon::_OnMouseMove
)
61 EVT_TASKBAR_LEFT_DOWN (wxTaskBarIcon::_OnLButtonDown
)
62 EVT_TASKBAR_LEFT_UP (wxTaskBarIcon::_OnLButtonUp
)
63 EVT_TASKBAR_RIGHT_DOWN (wxTaskBarIcon::_OnRButtonDown
)
64 EVT_TASKBAR_RIGHT_UP (wxTaskBarIcon::_OnRButtonUp
)
65 EVT_TASKBAR_LEFT_DCLICK (wxTaskBarIcon::_OnLButtonDClick
)
66 EVT_TASKBAR_RIGHT_DCLICK (wxTaskBarIcon::_OnRButtonDClick
)
70 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
74 wxTaskBarIcon::wxTaskBarIcon(void)
81 if (RegisterWindowClass())
82 m_hWnd
= CreateTaskBarWindow();
85 wxTaskBarIcon::~wxTaskBarIcon(void)
96 ::DestroyWindow((HWND
) m_hWnd
);
102 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
107 NOTIFYICONDATA notifyData
;
109 memset(¬ifyData
, 0, sizeof(notifyData
));
110 notifyData
.cbSize
= sizeof(notifyData
);
111 notifyData
.hWnd
= (HWND
) m_hWnd
;
112 notifyData
.uCallbackMessage
= sm_taskbarMsg
;
113 notifyData
.uFlags
= NIF_MESSAGE
;
116 notifyData
.uFlags
|= NIF_ICON
;
117 notifyData
.hIcon
= (HICON
) icon
.GetHICON();
120 if (((const char*) tooltip
!= NULL
) && (tooltip
!= ""))
122 notifyData
.uFlags
|= NIF_TIP
;
123 lstrcpyn(notifyData
.szTip
, (char*) (const char*) tooltip
, sizeof(notifyData
.szTip
));
129 return (Shell_NotifyIcon(NIM_MODIFY
, & notifyData
) != 0);
132 m_iconAdded
= (Shell_NotifyIcon(NIM_ADD
, & notifyData
) != 0);
137 bool wxTaskBarIcon::RemoveIcon(void)
142 NOTIFYICONDATA notifyData
;
144 memset(¬ifyData
, 0, sizeof(notifyData
));
145 notifyData
.cbSize
= sizeof(notifyData
);
146 notifyData
.hWnd
= (HWND
) m_hWnd
;
147 notifyData
.uCallbackMessage
= sm_taskbarMsg
;
148 notifyData
.uFlags
= NIF_MESSAGE
;
149 notifyData
.hIcon
= 0 ; // hIcon;
153 return (Shell_NotifyIcon(NIM_DELETE
, & notifyData
) != 0);
156 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
) //, int x, int y);
161 wxGetMousePosition(&x
, &y
);
163 // is wxFrame the best window type to use???
164 win
= new wxFrame(NULL
, -1, "", wxPoint(x
,y
), wxSize(-1,-1), 0);
165 win
->PushEventHandler(this);
167 rval
= win
->PopupMenu(menu
, 0, 0);
169 win
->PopEventHandler(FALSE
);
176 void wxTaskBarIcon::OnMouseMove(wxEvent
&)
180 void wxTaskBarIcon::OnLButtonDown(wxEvent
&)
184 void wxTaskBarIcon::OnLButtonUp(wxEvent
&)
188 void wxTaskBarIcon::OnRButtonDown(wxEvent
&)
192 void wxTaskBarIcon::OnRButtonUp(wxEvent
&)
196 void wxTaskBarIcon::OnLButtonDClick(wxEvent
&)
200 void wxTaskBarIcon::OnRButtonDClick(wxEvent
&)
204 void wxTaskBarIcon::_OnMouseMove(wxEvent
& e
) { OnMouseMove(e
); }
205 void wxTaskBarIcon::_OnLButtonDown(wxEvent
& e
) { OnLButtonDown(e
); }
206 void wxTaskBarIcon::_OnLButtonUp(wxEvent
& e
) { OnLButtonUp(e
); }
207 void wxTaskBarIcon::_OnRButtonDown(wxEvent
& e
) { OnRButtonDown(e
); }
208 void wxTaskBarIcon::_OnRButtonUp(wxEvent
& e
) { OnRButtonUp(e
); }
209 void wxTaskBarIcon::_OnLButtonDClick(wxEvent
& e
) { OnLButtonDClick(e
); }
210 void wxTaskBarIcon::_OnRButtonDClick(wxEvent
& e
) { OnRButtonDClick(e
); }
213 wxTaskBarIcon
* wxTaskBarIcon::FindObjectForHWND(WXHWND hWnd
)
215 wxNode
*node
= sm_taskBarIcons
.First();
218 wxTaskBarIcon
* obj
= (wxTaskBarIcon
*) node
->Data();
219 if (obj
->GetHWND() == hWnd
)
226 void wxTaskBarIcon::AddObject(wxTaskBarIcon
* obj
)
228 sm_taskBarIcons
.Append(obj
);
231 void wxTaskBarIcon::RemoveObject(wxTaskBarIcon
* obj
)
233 sm_taskBarIcons
.DeleteObject(obj
);
236 bool wxTaskBarIcon::RegisterWindowClass()
238 if (sm_registeredClass
)
241 // Also register the taskbar message here
242 sm_taskbarMsg
= ::RegisterWindowMessage("wxTaskBarIconMessage");
247 HINSTANCE hInstance
= GetModuleHandle(NULL
);
250 * set up and register window class
252 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
253 wc
.lpfnWndProc
= (WNDPROC
) wxTaskBarIconWindowProc
;
256 wc
.hInstance
= hInstance
;
259 wc
.hbrBackground
= 0;
260 wc
.lpszMenuName
= NULL
;
261 wc
.lpszClassName
= wxTaskBarWindowClass
;
262 rc
= (::RegisterClass( &wc
) != 0);
264 sm_registeredClass
= (rc
!= 0);
269 WXHWND
wxTaskBarIcon::CreateTaskBarWindow()
271 HINSTANCE hInstance
= GetModuleHandle(NULL
);
273 HWND hWnd
= CreateWindowEx (0, wxTaskBarWindowClass
,
285 return (WXHWND
) hWnd
;
288 long wxTaskBarIcon::WindowProc( WXHWND hWnd
, unsigned int msg
, unsigned int wParam
, long lParam
)
290 wxEventType eventType
= 0;
292 if (msg
!= sm_taskbarMsg
)
293 return DefWindowProc((HWND
) hWnd
, msg
, wParam
, lParam
);
298 eventType
= wxEVT_TASKBAR_LEFT_DOWN
;
302 eventType
= wxEVT_TASKBAR_LEFT_UP
;
306 eventType
= wxEVT_TASKBAR_RIGHT_DOWN
;
310 eventType
= wxEVT_TASKBAR_RIGHT_UP
;
313 case WM_LBUTTONDBLCLK
:
314 eventType
= wxEVT_TASKBAR_LEFT_DCLICK
;
317 case WM_RBUTTONDBLCLK
:
318 eventType
= wxEVT_TASKBAR_RIGHT_DCLICK
;
322 eventType
= wxEVT_TASKBAR_MOVE
;
331 event
.SetEventType(eventType
);
332 event
.SetEventObject(this);
339 LRESULT APIENTRY _EXPORT
wxTaskBarIconWindowProc( HWND hWnd
, unsigned msg
,
340 UINT wParam
, LONG lParam
)
342 wxTaskBarIcon
* obj
= wxTaskBarIcon::FindObjectForHWND((WXHWND
) hWnd
);
344 return obj
->WindowProc((WXHWND
) hWnd
, msg
, wParam
, lParam
);
346 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);