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/winundef.h"
39 #include "wx/msw/taskbar.h"
40 #include "wx/msw/private.h"
43 #ifdef __GNUWIN32_OLD__
44 #include "wx/msw/gnuwin32/extra.h"
52 LRESULT APIENTRY _EXPORT
wxTaskBarIconWindowProc( HWND hWnd
, unsigned msg
,
53 UINT wParam
, LONG lParam
);
55 wxChar
*wxTaskBarWindowClass
= wxT("wxTaskBarWindowClass");
57 wxList
wxTaskBarIcon::sm_taskBarIcons
;
58 bool wxTaskBarIcon::sm_registeredClass
= FALSE
;
59 UINT
wxTaskBarIcon::sm_taskbarMsg
= 0;
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
)
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
!= wxT("")))
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);
160 // OK, so I know this isn't thread-friendly, but
161 // what to do? We need this check.
163 static bool s_inPopup
= FALSE
;
173 wxGetMousePosition(&x
, &y
);
175 // is wxFrame the best window type to use???
176 win
= new wxFrame(NULL
, -1, "", wxPoint(x
,y
), wxSize(-1,-1), 0);
177 win
->PushEventHandler(this);
179 // Remove from record of top-level windows, or will confuse wxWindows
180 // if we try to exit right now.
181 wxTopLevelWindows
.DeleteObject(win
);
185 rval
= win
->PopupMenu(menu
, 0, 0);
187 win
->PopEventHandler(FALSE
);
198 void wxTaskBarIcon::OnMouseMove(wxEvent
&)
202 void wxTaskBarIcon::OnLButtonDown(wxEvent
&)
206 void wxTaskBarIcon::OnLButtonUp(wxEvent
&)
210 void wxTaskBarIcon::OnRButtonDown(wxEvent
&)
214 void wxTaskBarIcon::OnRButtonUp(wxEvent
&)
218 void wxTaskBarIcon::OnLButtonDClick(wxEvent
&)
222 void wxTaskBarIcon::OnRButtonDClick(wxEvent
&)
226 void wxTaskBarIcon::_OnMouseMove(wxEvent
& e
) { OnMouseMove(e
); }
227 void wxTaskBarIcon::_OnLButtonDown(wxEvent
& e
) { OnLButtonDown(e
); }
228 void wxTaskBarIcon::_OnLButtonUp(wxEvent
& e
) { OnLButtonUp(e
); }
229 void wxTaskBarIcon::_OnRButtonDown(wxEvent
& e
) { OnRButtonDown(e
); }
230 void wxTaskBarIcon::_OnRButtonUp(wxEvent
& e
) { OnRButtonUp(e
); }
231 void wxTaskBarIcon::_OnLButtonDClick(wxEvent
& e
) { OnLButtonDClick(e
); }
232 void wxTaskBarIcon::_OnRButtonDClick(wxEvent
& e
) { OnRButtonDClick(e
); }
235 wxTaskBarIcon
* wxTaskBarIcon::FindObjectForHWND(WXHWND hWnd
)
237 wxNode
*node
= sm_taskBarIcons
.First();
240 wxTaskBarIcon
* obj
= (wxTaskBarIcon
*) node
->Data();
241 if (obj
->GetHWND() == hWnd
)
248 void wxTaskBarIcon::AddObject(wxTaskBarIcon
* obj
)
250 sm_taskBarIcons
.Append(obj
);
253 void wxTaskBarIcon::RemoveObject(wxTaskBarIcon
* obj
)
255 sm_taskBarIcons
.DeleteObject(obj
);
258 bool wxTaskBarIcon::RegisterWindowClass()
260 if (sm_registeredClass
)
263 // Also register the taskbar message here
264 sm_taskbarMsg
= ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
269 HINSTANCE hInstance
= GetModuleHandle(NULL
);
272 * set up and register window class
274 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
275 wc
.lpfnWndProc
= (WNDPROC
) wxTaskBarIconWindowProc
;
278 wc
.hInstance
= hInstance
;
281 wc
.hbrBackground
= 0;
282 wc
.lpszMenuName
= NULL
;
283 wc
.lpszClassName
= wxTaskBarWindowClass
;
284 rc
= (::RegisterClass( &wc
) != 0);
286 sm_registeredClass
= (rc
!= 0);
291 WXHWND
wxTaskBarIcon::CreateTaskBarWindow()
293 HINSTANCE hInstance
= GetModuleHandle(NULL
);
295 HWND hWnd
= CreateWindowEx (0, wxTaskBarWindowClass
,
296 wxT("wxTaskBarWindow"),
307 return (WXHWND
) hWnd
;
310 long wxTaskBarIcon::WindowProc( WXHWND hWnd
, unsigned int msg
, unsigned int wParam
, long lParam
)
312 wxEventType eventType
= 0;
314 if (msg
!= sm_taskbarMsg
)
315 return DefWindowProc((HWND
) hWnd
, msg
, wParam
, lParam
);
320 eventType
= wxEVT_TASKBAR_LEFT_DOWN
;
324 eventType
= wxEVT_TASKBAR_LEFT_UP
;
328 eventType
= wxEVT_TASKBAR_RIGHT_DOWN
;
332 eventType
= wxEVT_TASKBAR_RIGHT_UP
;
335 case WM_LBUTTONDBLCLK
:
336 eventType
= wxEVT_TASKBAR_LEFT_DCLICK
;
339 case WM_RBUTTONDBLCLK
:
340 eventType
= wxEVT_TASKBAR_RIGHT_DCLICK
;
344 eventType
= wxEVT_TASKBAR_MOVE
;
353 event
.SetEventType(eventType
);
354 event
.SetEventObject(this);
361 LRESULT APIENTRY _EXPORT
wxTaskBarIconWindowProc( HWND hWnd
, unsigned msg
,
362 UINT wParam
, LONG lParam
)
364 wxTaskBarIcon
* obj
= wxTaskBarIcon::FindObjectForHWND((WXHWND
) hWnd
);
366 return obj
->WindowProc((WXHWND
) hWnd
, msg
, wParam
, lParam
);
368 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);