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/taskbar.h"
40 #include "wx/msw/private.h"
43 #ifdef __GNUWIN32_OLD__
44 #include "wx/msw/gnuwin32/extra.h"
52 #include "wx/listimpl.cpp"
53 WX_DEFINE_LIST(wxTaskBarIconList
);
55 LRESULT APIENTRY _EXPORT
wxTaskBarIconWindowProc( HWND hWnd
, unsigned msg
,
56 UINT wParam
, LONG lParam
);
58 wxChar
*wxTaskBarWindowClass
= (wxChar
*) wxT("wxTaskBarWindowClass");
60 wxTaskBarIconList
wxTaskBarIcon::sm_taskBarIcons
;
61 bool wxTaskBarIcon::sm_registeredClass
= FALSE
;
62 UINT
wxTaskBarIcon::sm_taskbarMsg
= 0;
64 #if WXWIN_COMPATIBILITY_2_4
65 BEGIN_EVENT_TABLE(wxTaskBarIcon
, wxEvtHandler
)
66 EVT_TASKBAR_MOVE (wxTaskBarIcon::_OnMouseMove
)
67 EVT_TASKBAR_LEFT_DOWN (wxTaskBarIcon::_OnLButtonDown
)
68 EVT_TASKBAR_LEFT_UP (wxTaskBarIcon::_OnLButtonUp
)
69 EVT_TASKBAR_RIGHT_DOWN (wxTaskBarIcon::_OnRButtonDown
)
70 EVT_TASKBAR_RIGHT_UP (wxTaskBarIcon::_OnRButtonUp
)
71 EVT_TASKBAR_LEFT_DCLICK (wxTaskBarIcon::_OnLButtonDClick
)
72 EVT_TASKBAR_RIGHT_DCLICK (wxTaskBarIcon::_OnRButtonDClick
)
77 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
80 wxTaskBarIcon::wxTaskBarIcon(void)
87 if (RegisterWindowClass())
88 m_hWnd
= CreateTaskBarWindow();
91 wxTaskBarIcon::~wxTaskBarIcon(void)
102 ::DestroyWindow((HWND
) m_hWnd
);
108 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
113 NOTIFYICONDATA notifyData
;
115 memset(¬ifyData
, 0, sizeof(notifyData
));
116 notifyData
.cbSize
= sizeof(notifyData
);
117 notifyData
.hWnd
= (HWND
) m_hWnd
;
118 notifyData
.uCallbackMessage
= sm_taskbarMsg
;
119 notifyData
.uFlags
= NIF_MESSAGE
;
122 notifyData
.uFlags
|= NIF_ICON
;
123 notifyData
.hIcon
= (HICON
) icon
.GetHICON();
126 if (((const wxChar
*) tooltip
!= NULL
) && (tooltip
!= wxT("")))
128 notifyData
.uFlags
|= NIF_TIP
;
129 lstrcpyn(notifyData
.szTip
, WXSTRINGCAST tooltip
, sizeof(notifyData
.szTip
));
135 return (Shell_NotifyIcon(NIM_MODIFY
, & notifyData
) != 0);
138 m_iconAdded
= (Shell_NotifyIcon(NIM_ADD
, & notifyData
) != 0);
143 bool wxTaskBarIcon::RemoveIcon(void)
148 NOTIFYICONDATA notifyData
;
150 memset(¬ifyData
, 0, sizeof(notifyData
));
151 notifyData
.cbSize
= sizeof(notifyData
);
152 notifyData
.hWnd
= (HWND
) m_hWnd
;
153 notifyData
.uCallbackMessage
= sm_taskbarMsg
;
154 notifyData
.uFlags
= NIF_MESSAGE
;
155 notifyData
.hIcon
= 0 ; // hIcon;
159 return (Shell_NotifyIcon(NIM_DELETE
, & notifyData
) != 0);
162 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
) //, int x, int y);
164 // OK, so I know this isn't thread-friendly, but
165 // what to do? We need this check.
167 static bool s_inPopup
= false;
177 wxGetMousePosition(&x
, &y
);
179 // is wxFrame the best window type to use???
180 win
= new wxFrame(NULL
, -1, wxEmptyString
, wxPoint(x
,y
), wxSize(-1,-1), 0);
181 win
->PushEventHandler(this);
183 // Remove from record of top-level windows, or will confuse wxWindows
184 // if we try to exit right now.
185 wxTopLevelWindows
.DeleteObject(win
);
189 // Work around a WIN32 bug
190 ::SetForegroundWindow ((HWND
) win
->GetHWND ());
192 rval
= win
->PopupMenu(menu
, 0, 0);
194 // Work around a WIN32 bug
195 ::PostMessage ((HWND
) win
->GetHWND(),WM_NULL
,0,0L);
197 win
->PopEventHandler(false);
206 #if WXWIN_COMPATIBILITY_2_4
208 void wxTaskBarIcon::OnMouseMove(wxEvent
&) {}
209 void wxTaskBarIcon::OnLButtonDown(wxEvent
&) {}
210 void wxTaskBarIcon::OnLButtonUp(wxEvent
&) {}
211 void wxTaskBarIcon::OnRButtonDown(wxEvent
&) {}
212 void wxTaskBarIcon::OnRButtonUp(wxEvent
&) {}
213 void wxTaskBarIcon::OnLButtonDClick(wxEvent
&) {}
214 void wxTaskBarIcon::OnRButtonDClick(wxEvent
&) {}
216 void wxTaskBarIcon::_OnMouseMove(wxEvent
& e
) { OnMouseMove(e
); }
217 void wxTaskBarIcon::_OnLButtonDown(wxEvent
& e
) { OnLButtonDown(e
); }
218 void wxTaskBarIcon::_OnLButtonUp(wxEvent
& e
) { OnLButtonUp(e
); }
219 void wxTaskBarIcon::_OnRButtonDown(wxEvent
& e
) { OnRButtonDown(e
); }
220 void wxTaskBarIcon::_OnRButtonUp(wxEvent
& e
) { OnRButtonUp(e
); }
221 void wxTaskBarIcon::_OnLButtonDClick(wxEvent
& e
) { OnLButtonDClick(e
); }
222 void wxTaskBarIcon::_OnRButtonDClick(wxEvent
& e
) { OnRButtonDClick(e
); }
225 wxTaskBarIcon
* wxTaskBarIcon::FindObjectForHWND(WXHWND hWnd
)
227 wxTaskBarIconList::Node
*node
= sm_taskBarIcons
.GetFirst();
230 wxTaskBarIcon
*obj
= node
->GetData();
231 if (obj
->GetHWND() == hWnd
)
233 node
= node
->GetNext();
238 void wxTaskBarIcon::AddObject(wxTaskBarIcon
* obj
)
240 sm_taskBarIcons
.Append(obj
);
243 void wxTaskBarIcon::RemoveObject(wxTaskBarIcon
* obj
)
245 sm_taskBarIcons
.DeleteObject(obj
);
248 bool wxTaskBarIcon::RegisterWindowClass()
250 if (sm_registeredClass
)
253 // Also register the taskbar message here
254 sm_taskbarMsg
= ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
259 HINSTANCE hInstance
= GetModuleHandle(NULL
);
262 * set up and register window class
264 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
265 wc
.lpfnWndProc
= (WNDPROC
) wxTaskBarIconWindowProc
;
268 wc
.hInstance
= hInstance
;
271 wc
.hbrBackground
= 0;
272 wc
.lpszMenuName
= NULL
;
273 wc
.lpszClassName
= wxTaskBarWindowClass
;
274 rc
= (::RegisterClass( &wc
) != 0);
276 sm_registeredClass
= (rc
!= 0);
281 WXHWND
wxTaskBarIcon::CreateTaskBarWindow()
283 HINSTANCE hInstance
= GetModuleHandle(NULL
);
285 HWND hWnd
= CreateWindowEx (0, wxTaskBarWindowClass
,
286 wxT("wxTaskBarWindow"),
297 return (WXHWND
) hWnd
;
300 long wxTaskBarIcon::WindowProc( WXHWND hWnd
, unsigned int msg
, unsigned int wParam
, long lParam
)
302 wxEventType eventType
= 0;
304 if (msg
!= sm_taskbarMsg
)
305 return DefWindowProc((HWND
) hWnd
, msg
, wParam
, lParam
);
310 eventType
= wxEVT_TASKBAR_LEFT_DOWN
;
314 eventType
= wxEVT_TASKBAR_LEFT_UP
;
318 eventType
= wxEVT_TASKBAR_RIGHT_DOWN
;
322 eventType
= wxEVT_TASKBAR_RIGHT_UP
;
325 case WM_LBUTTONDBLCLK
:
326 eventType
= wxEVT_TASKBAR_LEFT_DCLICK
;
329 case WM_RBUTTONDBLCLK
:
330 eventType
= wxEVT_TASKBAR_RIGHT_DCLICK
;
334 eventType
= wxEVT_TASKBAR_MOVE
;
342 wxTaskBarIconEvent
event(eventType
, this);
349 LRESULT APIENTRY _EXPORT
wxTaskBarIconWindowProc( HWND hWnd
, unsigned msg
,
350 UINT wParam
, LONG lParam
)
352 wxTaskBarIcon
*obj
= wxTaskBarIcon::FindObjectForHWND((WXHWND
) hWnd
);
354 return obj
->WindowProc((WXHWND
) hWnd
, msg
, wParam
, lParam
);
356 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);