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"
44 #ifndef wxUSE_NORLANDER_HEADERS
45 #include "wx/msw/gnuwin32/extra.h"
54 LRESULT APIENTRY _EXPORT
wxTaskBarIconWindowProc( HWND hWnd
, unsigned msg
,
55 UINT wParam
, LONG lParam
);
57 wxChar
*wxTaskBarWindowClass
= wxT("wxTaskBarWindowClass");
59 wxList
wxTaskBarIcon::sm_taskBarIcons
;
60 bool wxTaskBarIcon::sm_registeredClass
= FALSE
;
61 UINT
wxTaskBarIcon::sm_taskbarMsg
= 0;
64 BEGIN_EVENT_TABLE(wxTaskBarIcon
, wxEvtHandler
)
65 EVT_TASKBAR_MOVE (wxTaskBarIcon::_OnMouseMove
)
66 EVT_TASKBAR_LEFT_DOWN (wxTaskBarIcon::_OnLButtonDown
)
67 EVT_TASKBAR_LEFT_UP (wxTaskBarIcon::_OnLButtonUp
)
68 EVT_TASKBAR_RIGHT_DOWN (wxTaskBarIcon::_OnRButtonDown
)
69 EVT_TASKBAR_RIGHT_UP (wxTaskBarIcon::_OnRButtonUp
)
70 EVT_TASKBAR_LEFT_DCLICK (wxTaskBarIcon::_OnLButtonDClick
)
71 EVT_TASKBAR_RIGHT_DCLICK (wxTaskBarIcon::_OnRButtonDClick
)
75 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
78 wxTaskBarIcon::wxTaskBarIcon(void)
85 if (RegisterWindowClass())
86 m_hWnd
= CreateTaskBarWindow();
89 wxTaskBarIcon::~wxTaskBarIcon(void)
100 ::DestroyWindow((HWND
) m_hWnd
);
106 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
111 NOTIFYICONDATA notifyData
;
113 memset(¬ifyData
, 0, sizeof(notifyData
));
114 notifyData
.cbSize
= sizeof(notifyData
);
115 notifyData
.hWnd
= (HWND
) m_hWnd
;
116 notifyData
.uCallbackMessage
= sm_taskbarMsg
;
117 notifyData
.uFlags
= NIF_MESSAGE
;
120 notifyData
.uFlags
|= NIF_ICON
;
121 notifyData
.hIcon
= (HICON
) icon
.GetHICON();
124 if (((const wxChar
*) tooltip
!= NULL
) && (tooltip
!= wxT("")))
126 notifyData
.uFlags
|= NIF_TIP
;
127 lstrcpyn(notifyData
.szTip
, WXSTRINGCAST tooltip
, sizeof(notifyData
.szTip
));
133 return (Shell_NotifyIcon(NIM_MODIFY
, & notifyData
) != 0);
136 m_iconAdded
= (Shell_NotifyIcon(NIM_ADD
, & notifyData
) != 0);
141 bool wxTaskBarIcon::RemoveIcon(void)
146 NOTIFYICONDATA notifyData
;
148 memset(¬ifyData
, 0, sizeof(notifyData
));
149 notifyData
.cbSize
= sizeof(notifyData
);
150 notifyData
.hWnd
= (HWND
) m_hWnd
;
151 notifyData
.uCallbackMessage
= sm_taskbarMsg
;
152 notifyData
.uFlags
= NIF_MESSAGE
;
153 notifyData
.hIcon
= 0 ; // hIcon;
157 return (Shell_NotifyIcon(NIM_DELETE
, & notifyData
) != 0);
160 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
) //, int x, int y);
162 // OK, so I know this isn't thread-friendly, but
163 // what to do? We need this check.
165 static bool s_inPopup
= FALSE
;
175 wxGetMousePosition(&x
, &y
);
177 // is wxFrame the best window type to use???
178 win
= new wxFrame(NULL
, -1, "", wxPoint(x
,y
), wxSize(-1,-1), 0);
179 win
->PushEventHandler(this);
181 // Remove from record of top-level windows, or will confuse wxWindows
182 // if we try to exit right now.
183 wxTopLevelWindows
.DeleteObject(win
);
187 rval
= win
->PopupMenu(menu
, 0, 0);
189 win
->PopEventHandler(FALSE
);
200 void wxTaskBarIcon::OnMouseMove(wxEvent
&)
204 void wxTaskBarIcon::OnLButtonDown(wxEvent
&)
208 void wxTaskBarIcon::OnLButtonUp(wxEvent
&)
212 void wxTaskBarIcon::OnRButtonDown(wxEvent
&)
216 void wxTaskBarIcon::OnRButtonUp(wxEvent
&)
220 void wxTaskBarIcon::OnLButtonDClick(wxEvent
&)
224 void wxTaskBarIcon::OnRButtonDClick(wxEvent
&)
228 void wxTaskBarIcon::_OnMouseMove(wxEvent
& e
) { OnMouseMove(e
); }
229 void wxTaskBarIcon::_OnLButtonDown(wxEvent
& e
) { OnLButtonDown(e
); }
230 void wxTaskBarIcon::_OnLButtonUp(wxEvent
& e
) { OnLButtonUp(e
); }
231 void wxTaskBarIcon::_OnRButtonDown(wxEvent
& e
) { OnRButtonDown(e
); }
232 void wxTaskBarIcon::_OnRButtonUp(wxEvent
& e
) { OnRButtonUp(e
); }
233 void wxTaskBarIcon::_OnLButtonDClick(wxEvent
& e
) { OnLButtonDClick(e
); }
234 void wxTaskBarIcon::_OnRButtonDClick(wxEvent
& e
) { OnRButtonDClick(e
); }
237 wxTaskBarIcon
* wxTaskBarIcon::FindObjectForHWND(WXHWND hWnd
)
239 wxNode
*node
= sm_taskBarIcons
.First();
242 wxTaskBarIcon
* obj
= (wxTaskBarIcon
*) node
->Data();
243 if (obj
->GetHWND() == hWnd
)
250 void wxTaskBarIcon::AddObject(wxTaskBarIcon
* obj
)
252 sm_taskBarIcons
.Append(obj
);
255 void wxTaskBarIcon::RemoveObject(wxTaskBarIcon
* obj
)
257 sm_taskBarIcons
.DeleteObject(obj
);
260 bool wxTaskBarIcon::RegisterWindowClass()
262 if (sm_registeredClass
)
265 // Also register the taskbar message here
266 sm_taskbarMsg
= ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
271 HINSTANCE hInstance
= GetModuleHandle(NULL
);
274 * set up and register window class
276 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
277 wc
.lpfnWndProc
= (WNDPROC
) wxTaskBarIconWindowProc
;
280 wc
.hInstance
= hInstance
;
283 wc
.hbrBackground
= 0;
284 wc
.lpszMenuName
= NULL
;
285 wc
.lpszClassName
= wxTaskBarWindowClass
;
286 rc
= (::RegisterClass( &wc
) != 0);
288 sm_registeredClass
= (rc
!= 0);
293 WXHWND
wxTaskBarIcon::CreateTaskBarWindow()
295 HINSTANCE hInstance
= GetModuleHandle(NULL
);
297 HWND hWnd
= CreateWindowEx (0, wxTaskBarWindowClass
,
298 wxT("wxTaskBarWindow"),
309 return (WXHWND
) hWnd
;
312 long wxTaskBarIcon::WindowProc( WXHWND hWnd
, unsigned int msg
, unsigned int wParam
, long lParam
)
314 wxEventType eventType
= 0;
316 if (msg
!= sm_taskbarMsg
)
317 return DefWindowProc((HWND
) hWnd
, msg
, wParam
, lParam
);
322 eventType
= wxEVT_TASKBAR_LEFT_DOWN
;
326 eventType
= wxEVT_TASKBAR_LEFT_UP
;
330 eventType
= wxEVT_TASKBAR_RIGHT_DOWN
;
334 eventType
= wxEVT_TASKBAR_RIGHT_UP
;
337 case WM_LBUTTONDBLCLK
:
338 eventType
= wxEVT_TASKBAR_LEFT_DCLICK
;
341 case WM_RBUTTONDBLCLK
:
342 eventType
= wxEVT_TASKBAR_RIGHT_DCLICK
;
346 eventType
= wxEVT_TASKBAR_MOVE
;
355 event
.SetEventType(eventType
);
356 event
.SetEventObject(this);
363 LRESULT APIENTRY _EXPORT
wxTaskBarIconWindowProc( HWND hWnd
, unsigned msg
,
364 UINT wParam
, LONG lParam
)
366 wxTaskBarIcon
* obj
= wxTaskBarIcon::FindObjectForHWND((WXHWND
) hWnd
);
368 return obj
->WindowProc((WXHWND
) hWnd
, msg
, wParam
, lParam
);
370 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);