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 #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
!= wxT("")))
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);
161 // OK, so I know this isn't thread-friendly, but
162 // what to do? We need this check.
164 static bool s_inPopup
= FALSE
;
174 wxGetMousePosition(&x
, &y
);
176 // is wxFrame the best window type to use???
177 win
= new wxFrame(NULL
, -1, "", wxPoint(x
,y
), wxSize(-1,-1), 0);
178 win
->PushEventHandler(this);
180 // Remove from record of top-level windows, or will confuse wxWindows
181 // if we try to exit right now.
182 wxTopLevelWindows
.DeleteObject(win
);
186 rval
= win
->PopupMenu(menu
, 0, 0);
188 win
->PopEventHandler(FALSE
);
199 void wxTaskBarIcon::OnMouseMove(wxEvent
&)
203 void wxTaskBarIcon::OnLButtonDown(wxEvent
&)
207 void wxTaskBarIcon::OnLButtonUp(wxEvent
&)
211 void wxTaskBarIcon::OnRButtonDown(wxEvent
&)
215 void wxTaskBarIcon::OnRButtonUp(wxEvent
&)
219 void wxTaskBarIcon::OnLButtonDClick(wxEvent
&)
223 void wxTaskBarIcon::OnRButtonDClick(wxEvent
&)
227 void wxTaskBarIcon::_OnMouseMove(wxEvent
& e
) { OnMouseMove(e
); }
228 void wxTaskBarIcon::_OnLButtonDown(wxEvent
& e
) { OnLButtonDown(e
); }
229 void wxTaskBarIcon::_OnLButtonUp(wxEvent
& e
) { OnLButtonUp(e
); }
230 void wxTaskBarIcon::_OnRButtonDown(wxEvent
& e
) { OnRButtonDown(e
); }
231 void wxTaskBarIcon::_OnRButtonUp(wxEvent
& e
) { OnRButtonUp(e
); }
232 void wxTaskBarIcon::_OnLButtonDClick(wxEvent
& e
) { OnLButtonDClick(e
); }
233 void wxTaskBarIcon::_OnRButtonDClick(wxEvent
& e
) { OnRButtonDClick(e
); }
236 wxTaskBarIcon
* wxTaskBarIcon::FindObjectForHWND(WXHWND hWnd
)
238 wxNode
*node
= sm_taskBarIcons
.First();
241 wxTaskBarIcon
* obj
= (wxTaskBarIcon
*) node
->Data();
242 if (obj
->GetHWND() == hWnd
)
249 void wxTaskBarIcon::AddObject(wxTaskBarIcon
* obj
)
251 sm_taskBarIcons
.Append(obj
);
254 void wxTaskBarIcon::RemoveObject(wxTaskBarIcon
* obj
)
256 sm_taskBarIcons
.DeleteObject(obj
);
259 bool wxTaskBarIcon::RegisterWindowClass()
261 if (sm_registeredClass
)
264 // Also register the taskbar message here
265 sm_taskbarMsg
= ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
270 HINSTANCE hInstance
= GetModuleHandle(NULL
);
273 * set up and register window class
275 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
276 wc
.lpfnWndProc
= (WNDPROC
) wxTaskBarIconWindowProc
;
279 wc
.hInstance
= hInstance
;
282 wc
.hbrBackground
= 0;
283 wc
.lpszMenuName
= NULL
;
284 wc
.lpszClassName
= wxTaskBarWindowClass
;
285 rc
= (::RegisterClass( &wc
) != 0);
287 sm_registeredClass
= (rc
!= 0);
292 WXHWND
wxTaskBarIcon::CreateTaskBarWindow()
294 HINSTANCE hInstance
= GetModuleHandle(NULL
);
296 HWND hWnd
= CreateWindowEx (0, wxTaskBarWindowClass
,
297 wxT("wxTaskBarWindow"),
308 return (WXHWND
) hWnd
;
311 long wxTaskBarIcon::WindowProc( WXHWND hWnd
, unsigned int msg
, unsigned int wParam
, long lParam
)
313 wxEventType eventType
= 0;
315 if (msg
!= sm_taskbarMsg
)
316 return DefWindowProc((HWND
) hWnd
, msg
, wParam
, lParam
);
321 eventType
= wxEVT_TASKBAR_LEFT_DOWN
;
325 eventType
= wxEVT_TASKBAR_LEFT_UP
;
329 eventType
= wxEVT_TASKBAR_RIGHT_DOWN
;
333 eventType
= wxEVT_TASKBAR_RIGHT_UP
;
336 case WM_LBUTTONDBLCLK
:
337 eventType
= wxEVT_TASKBAR_LEFT_DCLICK
;
340 case WM_RBUTTONDBLCLK
:
341 eventType
= wxEVT_TASKBAR_RIGHT_DCLICK
;
345 eventType
= wxEVT_TASKBAR_MOVE
;
354 event
.SetEventType(eventType
);
355 event
.SetEventObject(this);
362 LRESULT APIENTRY _EXPORT
wxTaskBarIconWindowProc( HWND hWnd
, unsigned msg
,
363 UINT wParam
, LONG lParam
)
365 wxTaskBarIcon
* obj
= wxTaskBarIcon::FindObjectForHWND((WXHWND
) hWnd
);
367 return obj
->WindowProc((WXHWND
) hWnd
, msg
, wParam
, lParam
);
369 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);