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 // Remove from record of top-level windows, or will confuse wxWindows
170 // if we try to exit right now.
171 wxTopLevelWindows
.DeleteObject(win
);
173 rval
= win
->PopupMenu(menu
, 0, 0);
175 win
->PopEventHandler(FALSE
);
184 void wxTaskBarIcon::OnMouseMove(wxEvent
&)
188 void wxTaskBarIcon::OnLButtonDown(wxEvent
&)
192 void wxTaskBarIcon::OnLButtonUp(wxEvent
&)
196 void wxTaskBarIcon::OnRButtonDown(wxEvent
&)
200 void wxTaskBarIcon::OnRButtonUp(wxEvent
&)
204 void wxTaskBarIcon::OnLButtonDClick(wxEvent
&)
208 void wxTaskBarIcon::OnRButtonDClick(wxEvent
&)
212 void wxTaskBarIcon::_OnMouseMove(wxEvent
& e
) { OnMouseMove(e
); }
213 void wxTaskBarIcon::_OnLButtonDown(wxEvent
& e
) { OnLButtonDown(e
); }
214 void wxTaskBarIcon::_OnLButtonUp(wxEvent
& e
) { OnLButtonUp(e
); }
215 void wxTaskBarIcon::_OnRButtonDown(wxEvent
& e
) { OnRButtonDown(e
); }
216 void wxTaskBarIcon::_OnRButtonUp(wxEvent
& e
) { OnRButtonUp(e
); }
217 void wxTaskBarIcon::_OnLButtonDClick(wxEvent
& e
) { OnLButtonDClick(e
); }
218 void wxTaskBarIcon::_OnRButtonDClick(wxEvent
& e
) { OnRButtonDClick(e
); }
221 wxTaskBarIcon
* wxTaskBarIcon::FindObjectForHWND(WXHWND hWnd
)
223 wxNode
*node
= sm_taskBarIcons
.First();
226 wxTaskBarIcon
* obj
= (wxTaskBarIcon
*) node
->Data();
227 if (obj
->GetHWND() == hWnd
)
234 void wxTaskBarIcon::AddObject(wxTaskBarIcon
* obj
)
236 sm_taskBarIcons
.Append(obj
);
239 void wxTaskBarIcon::RemoveObject(wxTaskBarIcon
* obj
)
241 sm_taskBarIcons
.DeleteObject(obj
);
244 bool wxTaskBarIcon::RegisterWindowClass()
246 if (sm_registeredClass
)
249 // Also register the taskbar message here
250 sm_taskbarMsg
= ::RegisterWindowMessage(_T("wxTaskBarIconMessage"));
255 HINSTANCE hInstance
= GetModuleHandle(NULL
);
258 * set up and register window class
260 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
261 wc
.lpfnWndProc
= (WNDPROC
) wxTaskBarIconWindowProc
;
264 wc
.hInstance
= hInstance
;
267 wc
.hbrBackground
= 0;
268 wc
.lpszMenuName
= NULL
;
269 wc
.lpszClassName
= wxTaskBarWindowClass
;
270 rc
= (::RegisterClass( &wc
) != 0);
272 sm_registeredClass
= (rc
!= 0);
277 WXHWND
wxTaskBarIcon::CreateTaskBarWindow()
279 HINSTANCE hInstance
= GetModuleHandle(NULL
);
281 HWND hWnd
= CreateWindowEx (0, wxTaskBarWindowClass
,
282 _T("wxTaskBarWindow"),
293 return (WXHWND
) hWnd
;
296 long wxTaskBarIcon::WindowProc( WXHWND hWnd
, unsigned int msg
, unsigned int wParam
, long lParam
)
298 wxEventType eventType
= 0;
300 if (msg
!= sm_taskbarMsg
)
301 return DefWindowProc((HWND
) hWnd
, msg
, wParam
, lParam
);
306 eventType
= wxEVT_TASKBAR_LEFT_DOWN
;
310 eventType
= wxEVT_TASKBAR_LEFT_UP
;
314 eventType
= wxEVT_TASKBAR_RIGHT_DOWN
;
318 eventType
= wxEVT_TASKBAR_RIGHT_UP
;
321 case WM_LBUTTONDBLCLK
:
322 eventType
= wxEVT_TASKBAR_LEFT_DCLICK
;
325 case WM_RBUTTONDBLCLK
:
326 eventType
= wxEVT_TASKBAR_RIGHT_DCLICK
;
330 eventType
= wxEVT_TASKBAR_MOVE
;
339 event
.SetEventType(eventType
);
340 event
.SetEventObject(this);
347 LRESULT APIENTRY _EXPORT
wxTaskBarIconWindowProc( HWND hWnd
, unsigned msg
,
348 UINT wParam
, LONG lParam
)
350 wxTaskBarIcon
* obj
= wxTaskBarIcon::FindObjectForHWND((WXHWND
) hWnd
);
352 return obj
->WindowProc((WXHWND
) hWnd
, msg
, wParam
, lParam
);
354 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);