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
= wxT("wxTaskBarWindowClass");
56 wxList
wxTaskBarIcon::sm_taskBarIcons
;
57 bool wxTaskBarIcon::sm_registeredClass
= FALSE
;
58 UINT
wxTaskBarIcon::sm_taskbarMsg
= 0;
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
)
75 wxTaskBarIcon::wxTaskBarIcon(void)
82 if (RegisterWindowClass())
83 m_hWnd
= CreateTaskBarWindow();
86 wxTaskBarIcon::~wxTaskBarIcon(void)
97 ::DestroyWindow((HWND
) m_hWnd
);
103 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
108 NOTIFYICONDATA notifyData
;
110 memset(¬ifyData
, 0, sizeof(notifyData
));
111 notifyData
.cbSize
= sizeof(notifyData
);
112 notifyData
.hWnd
= (HWND
) m_hWnd
;
113 notifyData
.uCallbackMessage
= sm_taskbarMsg
;
114 notifyData
.uFlags
= NIF_MESSAGE
;
117 notifyData
.uFlags
|= NIF_ICON
;
118 notifyData
.hIcon
= (HICON
) icon
.GetHICON();
121 if (((const wxChar
*) tooltip
!= NULL
) && (tooltip
!= wxT("")))
123 notifyData
.uFlags
|= NIF_TIP
;
124 lstrcpyn(notifyData
.szTip
, WXSTRINGCAST tooltip
, sizeof(notifyData
.szTip
));
130 return (Shell_NotifyIcon(NIM_MODIFY
, & notifyData
) != 0);
133 m_iconAdded
= (Shell_NotifyIcon(NIM_ADD
, & notifyData
) != 0);
138 bool wxTaskBarIcon::RemoveIcon(void)
143 NOTIFYICONDATA notifyData
;
145 memset(¬ifyData
, 0, sizeof(notifyData
));
146 notifyData
.cbSize
= sizeof(notifyData
);
147 notifyData
.hWnd
= (HWND
) m_hWnd
;
148 notifyData
.uCallbackMessage
= sm_taskbarMsg
;
149 notifyData
.uFlags
= NIF_MESSAGE
;
150 notifyData
.hIcon
= 0 ; // hIcon;
154 return (Shell_NotifyIcon(NIM_DELETE
, & notifyData
) != 0);
157 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
) //, int x, int y);
159 // OK, so I know this isn't thread-friendly, but
160 // what to do? We need this check.
162 static bool s_inPopup
= FALSE
;
172 wxGetMousePosition(&x
, &y
);
174 // is wxFrame the best window type to use???
175 win
= new wxFrame(NULL
, -1, "", wxPoint(x
,y
), wxSize(-1,-1), 0);
176 win
->PushEventHandler(this);
178 // Remove from record of top-level windows, or will confuse wxWindows
179 // if we try to exit right now.
180 wxTopLevelWindows
.DeleteObject(win
);
184 rval
= win
->PopupMenu(menu
, 0, 0);
186 win
->PopEventHandler(FALSE
);
197 void wxTaskBarIcon::OnMouseMove(wxEvent
&)
201 void wxTaskBarIcon::OnLButtonDown(wxEvent
&)
205 void wxTaskBarIcon::OnLButtonUp(wxEvent
&)
209 void wxTaskBarIcon::OnRButtonDown(wxEvent
&)
213 void wxTaskBarIcon::OnRButtonUp(wxEvent
&)
217 void wxTaskBarIcon::OnLButtonDClick(wxEvent
&)
221 void wxTaskBarIcon::OnRButtonDClick(wxEvent
&)
225 void wxTaskBarIcon::_OnMouseMove(wxEvent
& e
) { OnMouseMove(e
); }
226 void wxTaskBarIcon::_OnLButtonDown(wxEvent
& e
) { OnLButtonDown(e
); }
227 void wxTaskBarIcon::_OnLButtonUp(wxEvent
& e
) { OnLButtonUp(e
); }
228 void wxTaskBarIcon::_OnRButtonDown(wxEvent
& e
) { OnRButtonDown(e
); }
229 void wxTaskBarIcon::_OnRButtonUp(wxEvent
& e
) { OnRButtonUp(e
); }
230 void wxTaskBarIcon::_OnLButtonDClick(wxEvent
& e
) { OnLButtonDClick(e
); }
231 void wxTaskBarIcon::_OnRButtonDClick(wxEvent
& e
) { OnRButtonDClick(e
); }
234 wxTaskBarIcon
* wxTaskBarIcon::FindObjectForHWND(WXHWND hWnd
)
236 wxNode
*node
= sm_taskBarIcons
.First();
239 wxTaskBarIcon
* obj
= (wxTaskBarIcon
*) node
->Data();
240 if (obj
->GetHWND() == hWnd
)
247 void wxTaskBarIcon::AddObject(wxTaskBarIcon
* obj
)
249 sm_taskBarIcons
.Append(obj
);
252 void wxTaskBarIcon::RemoveObject(wxTaskBarIcon
* obj
)
254 sm_taskBarIcons
.DeleteObject(obj
);
257 bool wxTaskBarIcon::RegisterWindowClass()
259 if (sm_registeredClass
)
262 // Also register the taskbar message here
263 sm_taskbarMsg
= ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
268 HINSTANCE hInstance
= GetModuleHandle(NULL
);
271 * set up and register window class
273 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
274 wc
.lpfnWndProc
= (WNDPROC
) wxTaskBarIconWindowProc
;
277 wc
.hInstance
= hInstance
;
280 wc
.hbrBackground
= 0;
281 wc
.lpszMenuName
= NULL
;
282 wc
.lpszClassName
= wxTaskBarWindowClass
;
283 rc
= (::RegisterClass( &wc
) != 0);
285 sm_registeredClass
= (rc
!= 0);
290 WXHWND
wxTaskBarIcon::CreateTaskBarWindow()
292 HINSTANCE hInstance
= GetModuleHandle(NULL
);
294 HWND hWnd
= CreateWindowEx (0, wxTaskBarWindowClass
,
295 wxT("wxTaskBarWindow"),
306 return (WXHWND
) hWnd
;
309 long wxTaskBarIcon::WindowProc( WXHWND hWnd
, unsigned int msg
, unsigned int wParam
, long lParam
)
311 wxEventType eventType
= 0;
313 if (msg
!= sm_taskbarMsg
)
314 return DefWindowProc((HWND
) hWnd
, msg
, wParam
, lParam
);
319 eventType
= wxEVT_TASKBAR_LEFT_DOWN
;
323 eventType
= wxEVT_TASKBAR_LEFT_UP
;
327 eventType
= wxEVT_TASKBAR_RIGHT_DOWN
;
331 eventType
= wxEVT_TASKBAR_RIGHT_UP
;
334 case WM_LBUTTONDBLCLK
:
335 eventType
= wxEVT_TASKBAR_LEFT_DCLICK
;
338 case WM_RBUTTONDBLCLK
:
339 eventType
= wxEVT_TASKBAR_RIGHT_DCLICK
;
343 eventType
= wxEVT_TASKBAR_MOVE
;
352 event
.SetEventType(eventType
);
353 event
.SetEventObject(this);
360 LRESULT APIENTRY _EXPORT
wxTaskBarIconWindowProc( HWND hWnd
, unsigned msg
,
361 UINT wParam
, LONG lParam
)
363 wxTaskBarIcon
* obj
= wxTaskBarIcon::FindObjectForHWND((WXHWND
) hWnd
);
365 return obj
->WindowProc((WXHWND
) hWnd
, msg
, wParam
, lParam
);
367 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);