]> git.saurik.com Git - wxWidgets.git/blob - src/msw/taskbar.cpp
Include wx/list.h according to precompiled headers of wx/wx.h (with other minor clean...
[wxWidgets.git] / src / msw / taskbar.cpp
1 /////////////////////////////////////////////////////////////////////////
2 // File: src/msw/taskbar.cpp
3 // Purpose: Implements wxTaskBarIcon class for manipulating icons on
4 // the Windows task bar.
5 // Author: Julian Smart
6 // Modified by: Vaclav Slavik
7 // Created: 24/3/98
8 // RCS-ID: $Id$
9 // Copyright: (c)
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////
12
13 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/window.h"
22 #include "wx/frame.h"
23 #include "wx/utils.h"
24 #include "wx/menu.h"
25 #endif
26
27 #include "wx/msw/private.h"
28 #include "wx/msw/winundef.h"
29
30 #include <string.h>
31 #include "wx/taskbar.h"
32
33 #ifdef __WXWINCE__
34 #include <winreg.h>
35 #include <shellapi.h>
36 #endif
37
38 // initialized on demand
39 UINT gs_msgTaskbar = 0;
40 UINT gs_msgRestartTaskbar = 0;
41
42 #if WXWIN_COMPATIBILITY_2_4
43 BEGIN_EVENT_TABLE(wxTaskBarIcon, wxTaskBarIconBase)
44 EVT_TASKBAR_MOVE (wxTaskBarIcon::_OnMouseMove)
45 EVT_TASKBAR_LEFT_DOWN (wxTaskBarIcon::_OnLButtonDown)
46 EVT_TASKBAR_LEFT_UP (wxTaskBarIcon::_OnLButtonUp)
47 EVT_TASKBAR_RIGHT_DOWN (wxTaskBarIcon::_OnRButtonDown)
48 EVT_TASKBAR_RIGHT_UP (wxTaskBarIcon::_OnRButtonUp)
49 EVT_TASKBAR_LEFT_DCLICK (wxTaskBarIcon::_OnLButtonDClick)
50 EVT_TASKBAR_RIGHT_DCLICK (wxTaskBarIcon::_OnRButtonDClick)
51 END_EVENT_TABLE()
52 #endif
53
54
55 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
56
57 // ============================================================================
58 // implementation
59 // ============================================================================
60
61 // ----------------------------------------------------------------------------
62 // wxTaskBarIconWindow: helper window
63 // ----------------------------------------------------------------------------
64
65 // NB: this class serves two purposes:
66 // 1. win32 needs a HWND associated with taskbar icon, this provides it
67 // 2. we need wxTopLevelWindow so that the app doesn't exit when
68 // last frame is closed but there still is a taskbar icon
69 class wxTaskBarIconWindow : public wxFrame
70 {
71 public:
72 wxTaskBarIconWindow(wxTaskBarIcon *icon)
73 : wxFrame(NULL, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0),
74 m_icon(icon)
75 {
76 }
77
78 WXLRESULT MSWWindowProc(WXUINT msg,
79 WXWPARAM wParam, WXLPARAM lParam)
80 {
81 if (msg == gs_msgRestartTaskbar || msg == gs_msgTaskbar)
82 {
83 return m_icon->WindowProc(msg, wParam, lParam);
84 }
85 else
86 {
87 return wxFrame::MSWWindowProc(msg, wParam, lParam);
88 }
89 }
90
91 private:
92 wxTaskBarIcon *m_icon;
93 };
94
95
96 // ----------------------------------------------------------------------------
97 // NotifyIconData: wrapper around NOTIFYICONDATA
98 // ----------------------------------------------------------------------------
99
100 struct NotifyIconData : public NOTIFYICONDATA
101 {
102 NotifyIconData(WXHWND hwnd)
103 {
104 memset(this, 0, sizeof(NOTIFYICONDATA));
105 cbSize = sizeof(NOTIFYICONDATA);
106 hWnd = (HWND) hwnd;
107 uCallbackMessage = gs_msgTaskbar;
108 uFlags = NIF_MESSAGE;
109
110 // we use the same id for all taskbar icons as we don't need it to
111 // distinguish between them
112 uID = 99;
113 }
114 };
115
116 // ----------------------------------------------------------------------------
117 // wxTaskBarIcon
118 // ----------------------------------------------------------------------------
119
120 wxTaskBarIcon::wxTaskBarIcon()
121 {
122 m_win = NULL;
123 m_iconAdded = false;
124 RegisterWindowMessages();
125 }
126
127 wxTaskBarIcon::~wxTaskBarIcon()
128 {
129 if (m_iconAdded)
130 RemoveIcon();
131
132 if (m_win)
133 m_win->Destroy();
134 }
135
136 // Operations
137 bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
138 {
139 // NB: we have to create the window lazily because of backward compatibility,
140 // old applications may create a wxTaskBarIcon instance before wxApp
141 // is initialized (as samples/taskbar used to do)
142 if (!m_win)
143 {
144 m_win = new wxTaskBarIconWindow(this);
145 }
146
147 m_icon = icon;
148 m_strTooltip = tooltip;
149
150 NotifyIconData notifyData((HWND)m_win->GetHWND());
151
152 if (icon.Ok())
153 {
154 notifyData.uFlags |= NIF_ICON;
155 notifyData.hIcon = GetHiconOf(icon);
156 }
157
158 if ( !tooltip.empty() )
159 {
160 notifyData.uFlags |= NIF_TIP;
161 // lstrcpyn(notifyData.szTip, tooltip.c_str(), WXSIZEOF(notifyData.szTip));
162 wxStrncpy(notifyData.szTip, tooltip.c_str(), WXSIZEOF(notifyData.szTip));
163 }
164
165 bool ok = Shell_NotifyIcon(m_iconAdded ? NIM_MODIFY
166 : NIM_ADD, &notifyData) != 0;
167
168 if ( !m_iconAdded && ok )
169 m_iconAdded = true;
170
171 return ok;
172 }
173
174 bool wxTaskBarIcon::RemoveIcon()
175 {
176 if (!m_iconAdded)
177 return false;
178
179 m_iconAdded = false;
180
181 NotifyIconData notifyData((HWND)m_win->GetHWND());
182
183 return Shell_NotifyIcon(NIM_DELETE, &notifyData) != 0;
184 }
185
186 bool wxTaskBarIcon::PopupMenu(wxMenu *menu)
187 {
188 wxASSERT_MSG( m_win != NULL, _T("taskbar icon not initialized") );
189
190 static bool s_inPopup = false;
191
192 if (s_inPopup)
193 return false;
194
195 s_inPopup = true;
196
197 int x, y;
198 wxGetMousePosition(&x, &y);
199
200 m_win->Move(x, y);
201
202 m_win->PushEventHandler(this);
203
204 menu->UpdateUI();
205
206 // Work around a WIN32 bug
207 ::SetForegroundWindow((HWND)m_win->GetHWND());
208
209 bool rval = m_win->PopupMenu(menu, 0, 0);
210
211 // Work around a WIN32 bug
212 ::PostMessage((HWND)m_win->GetHWND(), WM_NULL, 0, 0L);
213
214 m_win->PopEventHandler(false);
215
216 s_inPopup = false;
217
218 return rval;
219 }
220
221 #if WXWIN_COMPATIBILITY_2_4
222 // Overridables
223 void wxTaskBarIcon::OnMouseMove(wxEvent& e) { e.Skip(); }
224 void wxTaskBarIcon::OnLButtonDown(wxEvent& e) { e.Skip(); }
225 void wxTaskBarIcon::OnLButtonUp(wxEvent& e) { e.Skip(); }
226 void wxTaskBarIcon::OnRButtonDown(wxEvent& e) { e.Skip(); }
227 void wxTaskBarIcon::OnRButtonUp(wxEvent& e) { e.Skip(); }
228 void wxTaskBarIcon::OnLButtonDClick(wxEvent& e) { e.Skip(); }
229 void wxTaskBarIcon::OnRButtonDClick(wxEvent& e) { e.Skip(); }
230
231 void wxTaskBarIcon::_OnMouseMove(wxTaskBarIconEvent& e)
232 { OnMouseMove(e); }
233 void wxTaskBarIcon::_OnLButtonDown(wxTaskBarIconEvent& e)
234 { OnLButtonDown(e); }
235 void wxTaskBarIcon::_OnLButtonUp(wxTaskBarIconEvent& e)
236 { OnLButtonUp(e); }
237 void wxTaskBarIcon::_OnRButtonDown(wxTaskBarIconEvent& e)
238 { OnRButtonDown(e); }
239 void wxTaskBarIcon::_OnRButtonUp(wxTaskBarIconEvent& e)
240 { OnRButtonUp(e); }
241 void wxTaskBarIcon::_OnLButtonDClick(wxTaskBarIconEvent& e)
242 { OnLButtonDClick(e); }
243 void wxTaskBarIcon::_OnRButtonDClick(wxTaskBarIconEvent& e)
244 { OnRButtonDClick(e); }
245 #endif
246
247 void wxTaskBarIcon::RegisterWindowMessages()
248 {
249 static bool s_registered = false;
250
251 if ( !s_registered )
252 {
253 // Taskbar restart msg will be sent to us if the icon needs to be redrawn
254 gs_msgRestartTaskbar = RegisterWindowMessage(wxT("TaskbarCreated"));
255
256 // Also register the taskbar message here
257 gs_msgTaskbar = ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
258
259 s_registered = true;
260 }
261 }
262
263 // ----------------------------------------------------------------------------
264 // wxTaskBarIcon window proc
265 // ----------------------------------------------------------------------------
266
267 long wxTaskBarIcon::WindowProc(unsigned int msg,
268 unsigned int WXUNUSED(wParam),
269 long lParam)
270 {
271 wxEventType eventType = 0;
272
273 if (msg == gs_msgRestartTaskbar) // does the icon need to be redrawn?
274 {
275 m_iconAdded = false;
276 SetIcon(m_icon, m_strTooltip);
277 }
278
279 // this function should only be called for gs_msg(Restart)Taskbar messages
280 wxASSERT(msg == gs_msgTaskbar);
281
282 switch (lParam)
283 {
284 case WM_LBUTTONDOWN:
285 eventType = wxEVT_TASKBAR_LEFT_DOWN;
286 break;
287
288 case WM_LBUTTONUP:
289 eventType = wxEVT_TASKBAR_LEFT_UP;
290 break;
291
292 case WM_RBUTTONDOWN:
293 eventType = wxEVT_TASKBAR_RIGHT_DOWN;
294 break;
295
296 case WM_RBUTTONUP:
297 eventType = wxEVT_TASKBAR_RIGHT_UP;
298 break;
299
300 case WM_LBUTTONDBLCLK:
301 eventType = wxEVT_TASKBAR_LEFT_DCLICK;
302 break;
303
304 case WM_RBUTTONDBLCLK:
305 eventType = wxEVT_TASKBAR_RIGHT_DCLICK;
306 break;
307
308 case WM_MOUSEMOVE:
309 eventType = wxEVT_TASKBAR_MOVE;
310 break;
311
312 default:
313 break;
314 }
315
316 if (eventType)
317 {
318 wxTaskBarIconEvent event(eventType, this);
319
320 ProcessEvent(event);
321 }
322
323 return 0;
324 }