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