]> git.saurik.com Git - wxWidgets.git/blob - src/msw/taskbar.cpp
fixed too small pagesize
[wxWidgets.git] / src / msw / taskbar.cpp
1 /////////////////////////////////////////////////////////////////////////
2 // File: taskbar.cpp
3 // Purpose: Implements wxTaskBarIcon class for manipulating icons on
4 // the Windows task bar.
5 // Author: Julian Smart
6 // Modified by:
7 // Created: 24/3/98
8 // RCS-ID: $Id$
9 // Copyright: (c)
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////
12
13 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
14 #pragma implementation "taskbar.h"
15 #endif
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/defs.h"
26 #include "wx/window.h"
27 #include "wx/frame.h"
28 #include "wx/utils.h"
29 #include "wx/menu.h"
30 #endif
31
32 #if defined(__WIN95__)
33
34 #include "wx/msw/private.h"
35 #include "wx/msw/winundef.h"
36
37 #include <string.h>
38 #include "wx/taskbar.h"
39
40 #ifdef __GNUWIN32_OLD__
41 #include "wx/msw/gnuwin32/extra.h"
42 #endif
43
44 #ifdef __WXWINCE__
45 #include <winreg.h>
46 #include <shellapi.h>
47 #endif
48
49 #include "wx/listimpl.cpp"
50 WX_DEFINE_LIST(wxTaskBarIconList);
51
52 LRESULT APIENTRY _EXPORT
53 wxTaskBarIconWindowProc( HWND hWnd, unsigned msg, UINT wParam, LONG lParam );
54
55 wxChar *wxTaskBarWindowClass = (wxChar*) wxT("wxTaskBarWindowClass");
56
57 wxTaskBarIconList wxTaskBarIcon::sm_taskBarIcons;
58
59 // initialized on demand
60 UINT gs_msgTaskbar = 0;
61 UINT gs_msgRestartTaskbar = 0;
62
63 #if WXWIN_COMPATIBILITY_2_4
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)
72 END_EVENT_TABLE()
73 #endif
74
75
76 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
77
78 // ============================================================================
79 // implementation
80 // ============================================================================
81
82 // ----------------------------------------------------------------------------
83 // NotifyIconData: wrapper around NOTIFYICONDATA
84 // ----------------------------------------------------------------------------
85
86 struct NotifyIconData : public NOTIFYICONDATA
87 {
88 NotifyIconData(WXHWND hwnd)
89 {
90 memset(this, 0, sizeof(NOTIFYICONDATA));
91 cbSize = sizeof(NOTIFYICONDATA);
92 hWnd = (HWND) hwnd;
93 uCallbackMessage = gs_msgTaskbar;
94 uFlags = NIF_MESSAGE;
95
96 // we use the same id for all taskbar icons as we don't need it to
97 // distinguish between them
98 uID = 99;
99 }
100 };
101
102 // ----------------------------------------------------------------------------
103 // wxTaskBarIcon
104 // ----------------------------------------------------------------------------
105
106 wxTaskBarIcon::wxTaskBarIcon()
107 {
108 m_hWnd = 0;
109 m_iconAdded = false;
110
111 AddObject(this);
112
113 if (RegisterWindowClass())
114 m_hWnd = CreateTaskBarWindow();
115 }
116
117 wxTaskBarIcon::~wxTaskBarIcon()
118 {
119 RemoveObject(this);
120
121 if (m_iconAdded)
122 {
123 RemoveIcon();
124 }
125
126 if (m_hWnd)
127 {
128 ::DestroyWindow((HWND) m_hWnd);
129 m_hWnd = 0;
130 }
131 }
132
133 // Operations
134 bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
135 {
136 if (!IsOk())
137 return false;
138
139 m_icon = icon;
140 m_strTooltip = tooltip;
141
142 NotifyIconData notifyData(m_hWnd);
143
144 if (icon.Ok())
145 {
146 notifyData.uFlags |= NIF_ICON;
147 notifyData.hIcon = GetHiconOf(icon);
148 }
149
150 if ( !tooltip.empty() )
151 {
152 notifyData.uFlags |= NIF_TIP;
153 // lstrcpyn(notifyData.szTip, tooltip.c_str(), WXSIZEOF(notifyData.szTip));
154 wxStrncpy(notifyData.szTip, tooltip.c_str(), WXSIZEOF(notifyData.szTip));
155 }
156
157 bool ok = Shell_NotifyIcon(m_iconAdded ? NIM_MODIFY
158 : NIM_ADD, &notifyData) != 0;
159
160 if ( !m_iconAdded && ok )
161 m_iconAdded = true;
162
163 return ok;
164 }
165
166 bool wxTaskBarIcon::RemoveIcon()
167 {
168 if (!m_iconAdded)
169 return false;
170
171 m_iconAdded = false;
172
173 NotifyIconData notifyData(m_hWnd);
174
175 return Shell_NotifyIcon(NIM_DELETE, &notifyData) != 0;
176 }
177
178 bool wxTaskBarIcon::PopupMenu(wxMenu *menu)
179 {
180 static bool s_inPopup = false;
181
182 if (s_inPopup)
183 return false;
184
185 s_inPopup = true;
186
187 bool rval = false;
188 wxWindow* win;
189 int x, y;
190 wxGetMousePosition(&x, &y);
191
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);
195
196 // Remove from record of top-level windows, or will confuse wxWindows
197 // if we try to exit right now.
198 wxTopLevelWindows.DeleteObject(win);
199
200 menu->UpdateUI();
201
202 // Work around a WIN32 bug
203 ::SetForegroundWindow ((HWND) win->GetHWND ());
204
205 rval = win->PopupMenu(menu, 0, 0);
206
207 // Work around a WIN32 bug
208 ::PostMessage ((HWND) win->GetHWND(),WM_NULL,0,0L);
209
210 win->PopEventHandler(false);
211 win->Destroy();
212 delete win;
213
214 s_inPopup = false;
215
216 return rval;
217 }
218
219 #if WXWIN_COMPATIBILITY_2_4
220 // Overridables
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&) {}
228
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); }
236 #endif
237
238 wxTaskBarIcon* wxTaskBarIcon::FindObjectForHWND(WXHWND hWnd)
239 {
240 wxTaskBarIconList::compatibility_iterator node = sm_taskBarIcons.GetFirst();
241 while (node)
242 {
243 wxTaskBarIcon *obj = node->GetData();
244 if (obj->GetHWND() == hWnd)
245 return obj;
246 node = node->GetNext();
247 }
248 return NULL;
249 }
250
251 void wxTaskBarIcon::AddObject(wxTaskBarIcon* obj)
252 {
253 sm_taskBarIcons.Append(obj);
254 }
255
256 void wxTaskBarIcon::RemoveObject(wxTaskBarIcon* obj)
257 {
258 sm_taskBarIcons.DeleteObject(obj);
259 }
260
261 bool wxTaskBarIcon::RegisterWindowClass()
262 {
263 static bool s_registered = false;
264
265 if ( s_registered )
266 return true;
267
268 // Taskbar restart msg will be sent to us if the icon needs to be redrawn
269 gs_msgRestartTaskbar = RegisterWindowMessage(wxT("TaskbarCreated"));
270
271 // Also register the taskbar message here
272 gs_msgTaskbar = ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
273
274 // set up and register window class
275 WNDCLASS wc;
276 wc.style = CS_HREDRAW | CS_VREDRAW;
277 wc.lpfnWndProc = (WNDPROC) wxTaskBarIconWindowProc;
278 wc.cbClsExtra = 0;
279 wc.cbWndExtra = 0;
280 wc.hInstance = wxGetInstance();
281 wc.hIcon = 0;
282 wc.hCursor = 0;
283 wc.hbrBackground = 0;
284 wc.lpszMenuName = NULL;
285 wc.lpszClassName = wxTaskBarWindowClass;
286
287 if ( !::RegisterClass(&wc) )
288 {
289 wxLogLastError(_T("RegisterClass(taskbar icon)"));
290
291 return false;
292 }
293
294 s_registered = true;
295
296 return true;
297 }
298
299 WXHWND wxTaskBarIcon::CreateTaskBarWindow()
300 {
301 HINSTANCE hInstance = GetModuleHandle(NULL);
302
303 HWND hWnd = CreateWindowEx (0, wxTaskBarWindowClass,
304 wxT("wxTaskBarWindow"),
305 WS_OVERLAPPED,
306 0,
307 0,
308 10,
309 10,
310 NULL,
311 (HMENU) 0,
312 hInstance,
313 NULL);
314
315 return (WXHWND) hWnd;
316 }
317
318 // ----------------------------------------------------------------------------
319 // wxTaskBarIcon window proc
320 // ----------------------------------------------------------------------------
321
322 long wxTaskBarIcon::WindowProc(WXHWND hWnd,
323 unsigned int msg,
324 unsigned int wParam,
325 long lParam)
326 {
327 wxEventType eventType = 0;
328
329 if (msg == gs_msgRestartTaskbar) // does the icon need to be redrawn?
330 {
331 m_iconAdded = false;
332 SetIcon(m_icon, m_strTooltip);
333 }
334
335 if (msg != gs_msgTaskbar)
336 return DefWindowProc((HWND) hWnd, msg, wParam, lParam);
337
338 switch (lParam)
339 {
340 case WM_LBUTTONDOWN:
341 eventType = wxEVT_TASKBAR_LEFT_DOWN;
342 break;
343
344 case WM_LBUTTONUP:
345 eventType = wxEVT_TASKBAR_LEFT_UP;
346 break;
347
348 case WM_RBUTTONDOWN:
349 eventType = wxEVT_TASKBAR_RIGHT_DOWN;
350 break;
351
352 case WM_RBUTTONUP:
353 eventType = wxEVT_TASKBAR_RIGHT_UP;
354 break;
355
356 case WM_LBUTTONDBLCLK:
357 eventType = wxEVT_TASKBAR_LEFT_DCLICK;
358 break;
359
360 case WM_RBUTTONDBLCLK:
361 eventType = wxEVT_TASKBAR_RIGHT_DCLICK;
362 break;
363
364 case WM_MOUSEMOVE:
365 eventType = wxEVT_TASKBAR_MOVE;
366 break;
367
368 default:
369 break;
370 }
371
372 if (eventType)
373 {
374 wxTaskBarIconEvent event(eventType, this);
375
376 ProcessEvent(event);
377 }
378
379 return 0;
380 }
381
382 LRESULT APIENTRY _EXPORT
383 wxTaskBarIconWindowProc(HWND hWnd, unsigned msg, UINT wParam, LONG lParam)
384 {
385 wxTaskBarIcon *obj = wxTaskBarIcon::FindObjectForHWND((WXHWND) hWnd);
386 if (obj)
387 return obj->WindowProc((WXHWND) hWnd, msg, wParam, lParam);
388 else
389 return DefWindowProc(hWnd, msg, wParam, lParam);
390 }
391
392 #endif
393 // __WIN95__