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__)
36 #include "wx/msw/winundef.h"
39 #include "wx/taskbar.h"
40 #include "wx/msw/private.h"
42 #ifdef __GNUWIN32_OLD__
43 #include "wx/msw/gnuwin32/extra.h"
50 #include "wx/listimpl.cpp"
51 WX_DEFINE_LIST(wxTaskBarIconList
);
53 LRESULT APIENTRY _EXPORT
54 wxTaskBarIconWindowProc( HWND hWnd
, unsigned msg
, UINT wParam
, LONG lParam
);
56 wxChar
*wxTaskBarWindowClass
= (wxChar
*) wxT("wxTaskBarWindowClass");
58 wxTaskBarIconList
wxTaskBarIcon::sm_taskBarIcons
;
60 // initialized on demand
61 UINT gs_msgTaskbar
= 0;
62 UINT gs_msgRestartTaskbar
= 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
)
79 // ============================================================================
81 // ============================================================================
83 // ----------------------------------------------------------------------------
84 // NotifyIconData: wrapper around NOTIFYICONDATA
85 // ----------------------------------------------------------------------------
87 struct NotifyIconData
: public NOTIFYICONDATA
89 NotifyIconData(WXHWND hwnd
)
91 memset(this, 0, sizeof(NOTIFYICONDATA
));
92 cbSize
= sizeof(NOTIFYICONDATA
);
94 uCallbackMessage
= gs_msgTaskbar
;
97 // we use the same id for all taskbar icons as we don't need it to
98 // distinguish between them
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 wxTaskBarIcon::wxTaskBarIcon()
114 if (RegisterWindowClass())
115 m_hWnd
= CreateTaskBarWindow();
118 wxTaskBarIcon::~wxTaskBarIcon()
129 ::DestroyWindow((HWND
) m_hWnd
);
135 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
141 m_strTooltip
= tooltip
;
143 NotifyIconData
notifyData(m_hWnd
);
147 notifyData
.uFlags
|= NIF_ICON
;
148 notifyData
.hIcon
= GetHiconOf(icon
);
151 if ( !tooltip
.empty() )
153 notifyData
.uFlags
|= NIF_TIP
;
154 lstrcpyn(notifyData
.szTip
, tooltip
.c_str(), WXSIZEOF(notifyData
.szTip
));
157 bool ok
= Shell_NotifyIcon(m_iconAdded
? NIM_MODIFY
158 : NIM_ADD
, ¬ifyData
) != 0;
160 if ( !m_iconAdded
&& ok
)
166 bool wxTaskBarIcon::RemoveIcon()
173 NotifyIconData
notifyData(m_hWnd
);
175 return Shell_NotifyIcon(NIM_DELETE
, ¬ifyData
) != 0;
178 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
)
180 static bool s_inPopup
= false;
190 wxGetMousePosition(&x
, &y
);
192 // is wxFrame the best window type to use???
193 win
= new wxFrame(NULL
, -1, wxEmptyString
, wxPoint(x
,y
), wxSize(-1,-1), 0);
194 win
->PushEventHandler(this);
196 // Remove from record of top-level windows, or will confuse wxWindows
197 // if we try to exit right now.
198 wxTopLevelWindows
.DeleteObject(win
);
202 // Work around a WIN32 bug
203 ::SetForegroundWindow ((HWND
) win
->GetHWND ());
205 rval
= win
->PopupMenu(menu
, 0, 0);
207 // Work around a WIN32 bug
208 ::PostMessage ((HWND
) win
->GetHWND(),WM_NULL
,0,0L);
210 win
->PopEventHandler(false);
219 #if WXWIN_COMPATIBILITY_2_4
221 void wxTaskBarIcon::OnMouseMove(wxEvent
&) {}
222 void wxTaskBarIcon::OnLButtonDown(wxEvent
&) {}
223 void wxTaskBarIcon::OnLButtonUp(wxEvent
&) {}
224 void wxTaskBarIcon::OnRButtonDown(wxEvent
&) {}
225 void wxTaskBarIcon::OnRButtonUp(wxEvent
&) {}
226 void wxTaskBarIcon::OnLButtonDClick(wxEvent
&) {}
227 void wxTaskBarIcon::OnRButtonDClick(wxEvent
&) {}
229 void wxTaskBarIcon::_OnMouseMove(wxEvent
& e
) { OnMouseMove(e
); }
230 void wxTaskBarIcon::_OnLButtonDown(wxEvent
& e
) { OnLButtonDown(e
); }
231 void wxTaskBarIcon::_OnLButtonUp(wxEvent
& e
) { OnLButtonUp(e
); }
232 void wxTaskBarIcon::_OnRButtonDown(wxEvent
& e
) { OnRButtonDown(e
); }
233 void wxTaskBarIcon::_OnRButtonUp(wxEvent
& e
) { OnRButtonUp(e
); }
234 void wxTaskBarIcon::_OnLButtonDClick(wxEvent
& e
) { OnLButtonDClick(e
); }
235 void wxTaskBarIcon::_OnRButtonDClick(wxEvent
& e
) { OnRButtonDClick(e
); }
238 wxTaskBarIcon
* wxTaskBarIcon::FindObjectForHWND(WXHWND hWnd
)
240 wxTaskBarIconList::Node
*node
= sm_taskBarIcons
.GetFirst();
243 wxTaskBarIcon
*obj
= node
->GetData();
244 if (obj
->GetHWND() == hWnd
)
246 node
= node
->GetNext();
251 void wxTaskBarIcon::AddObject(wxTaskBarIcon
* obj
)
253 sm_taskBarIcons
.Append(obj
);
256 void wxTaskBarIcon::RemoveObject(wxTaskBarIcon
* obj
)
258 sm_taskBarIcons
.DeleteObject(obj
);
261 bool wxTaskBarIcon::RegisterWindowClass()
263 static bool s_registered
= false;
268 // Taskbar restart msg will be sent to us if the icon needs to be redrawn
269 gs_msgRestartTaskbar
= RegisterWindowMessage(wxT("TaskbarCreated"));
271 // Also register the taskbar message here
272 gs_msgTaskbar
= ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
274 // set up and register window class
276 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
277 wc
.lpfnWndProc
= (WNDPROC
) wxTaskBarIconWindowProc
;
280 wc
.hInstance
= wxGetInstance();
283 wc
.hbrBackground
= 0;
284 wc
.lpszMenuName
= NULL
;
285 wc
.lpszClassName
= wxTaskBarWindowClass
;
287 if ( !::RegisterClass(&wc
) )
289 wxLogLastError(_T("RegisterClass(taskbar icon)"));
299 WXHWND
wxTaskBarIcon::CreateTaskBarWindow()
301 HINSTANCE hInstance
= GetModuleHandle(NULL
);
303 HWND hWnd
= CreateWindowEx (0, wxTaskBarWindowClass
,
304 wxT("wxTaskBarWindow"),
315 return (WXHWND
) hWnd
;
318 // ----------------------------------------------------------------------------
319 // wxTaskBarIcon window proc
320 // ----------------------------------------------------------------------------
322 long wxTaskBarIcon::WindowProc(WXHWND hWnd
,
327 wxEventType eventType
= 0;
329 if (msg
== gs_msgRestartTaskbar
) // does the icon need to be redrawn?
332 SetIcon(m_icon
, m_strTooltip
);
335 if (msg
!= gs_msgTaskbar
)
336 return DefWindowProc((HWND
) hWnd
, msg
, wParam
, lParam
);
341 eventType
= wxEVT_TASKBAR_LEFT_DOWN
;
345 eventType
= wxEVT_TASKBAR_LEFT_UP
;
349 eventType
= wxEVT_TASKBAR_RIGHT_DOWN
;
353 eventType
= wxEVT_TASKBAR_RIGHT_UP
;
356 case WM_LBUTTONDBLCLK
:
357 eventType
= wxEVT_TASKBAR_LEFT_DCLICK
;
360 case WM_RBUTTONDBLCLK
:
361 eventType
= wxEVT_TASKBAR_RIGHT_DCLICK
;
365 eventType
= wxEVT_TASKBAR_MOVE
;
374 wxTaskBarIconEvent
event(eventType
, this);
382 LRESULT APIENTRY _EXPORT
383 wxTaskBarIconWindowProc(HWND hWnd
, unsigned msg
, UINT wParam
, LONG lParam
)
385 wxTaskBarIcon
*obj
= wxTaskBarIcon::FindObjectForHWND((WXHWND
) hWnd
);
387 return obj
->WindowProc((WXHWND
) hWnd
, msg
, wParam
, lParam
);
389 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);