]> git.saurik.com Git - wxWidgets.git/blame - src/msw/taskbar.cpp
Acutally use the m_maxChars parameter value instead of ignoring it
[wxWidgets.git] / src / msw / taskbar.cpp
CommitLineData
2bda0e17
KB
1/////////////////////////////////////////////////////////////////////////
2// File: taskbar.cpp
c42404a5 3// Purpose: Implements wxTaskBarIcon class for manipulating icons on
2bda0e17
KB
4// the Windows task bar.
5// Author: Julian Smart
6// Modified by:
7// Created: 24/3/98
8// RCS-ID: $Id$
9// Copyright: (c)
c42404a5 10// Licence: wxWindows licence
2bda0e17
KB
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"
ad5c34f3
JS
26#include "wx/window.h"
27#include "wx/frame.h"
28#include "wx/utils.h"
0ae2df30 29#include "wx/menu.h"
2bda0e17
KB
30#endif
31
57c208c5 32#if defined(__WIN95__) && !defined(__TWIN32__)
2bda0e17 33
3d05544e 34#include <windows.h>
c25a510b
JS
35
36#include "wx/msw/winundef.h"
37
2bda0e17 38#include <string.h>
6af507f7 39#include "wx/taskbar.h"
3096bd2f 40#include "wx/msw/private.h"
2bda0e17 41
57c208c5 42#ifndef __TWIN32__
c42404a5
VZ
43 #ifdef __GNUWIN32_OLD__
44 #include "wx/msw/gnuwin32/extra.h"
45 #endif
65fd5cb0 46#endif
2bda0e17 47
ce3ed50d 48#ifdef __SALFORDC__
c42404a5 49 #include <shellapi.h>
ce3ed50d
JS
50#endif
51
d162a7ee
VZ
52#include "wx/listimpl.cpp"
53WX_DEFINE_LIST(wxTaskBarIconList);
54
4d0d77af
VZ
55LRESULT APIENTRY _EXPORT
56wxTaskBarIconWindowProc( HWND hWnd, unsigned msg, UINT wParam, LONG lParam );
2bda0e17 57
ba14d986 58wxChar *wxTaskBarWindowClass = (wxChar*) wxT("wxTaskBarWindowClass");
2bda0e17 59
d162a7ee 60wxTaskBarIconList wxTaskBarIcon::sm_taskBarIcons;
4d0d77af
VZ
61
62// initialized on demand
63UINT gs_msgTaskbar = 0;
64UINT gs_msgRestartTaskbar = 0;
2bda0e17 65
6af507f7 66#if WXWIN_COMPATIBILITY_2_4
56194595 67BEGIN_EVENT_TABLE(wxTaskBarIcon, wxEvtHandler)
69ecd30f
RD
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)
56194595 75END_EVENT_TABLE()
6af507f7 76#endif
56194595
RD
77
78
79IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
56194595 80
4d0d77af
VZ
81// ============================================================================
82// implementation
83// ============================================================================
84
85// ----------------------------------------------------------------------------
86// NotifyIconData: wrapper around NOTIFYICONDATA
87// ----------------------------------------------------------------------------
56194595 88
4d0d77af
VZ
89struct 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
109wxTaskBarIcon::wxTaskBarIcon()
2bda0e17
KB
110{
111 m_hWnd = 0;
04cd30de 112 m_iconAdded = false;
2bda0e17
KB
113
114 AddObject(this);
115
116 if (RegisterWindowClass())
117 m_hWnd = CreateTaskBarWindow();
118}
119
4d0d77af 120wxTaskBarIcon::~wxTaskBarIcon()
2bda0e17
KB
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
137bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
138{
6af507f7 139 if (!IsOk())
04cd30de 140 return false;
2bda0e17 141
4d0d77af
VZ
142 m_icon = icon;
143 m_strTooltip = tooltip;
144
145 NotifyIconData notifyData(m_hWnd);
2bda0e17 146
e96360ef 147 if (icon.Ok())
2bda0e17 148 {
e96360ef 149 notifyData.uFlags |= NIF_ICON;
4d0d77af 150 notifyData.hIcon = GetHiconOf(icon);
2bda0e17
KB
151 }
152
4d0d77af 153 if ( !tooltip.empty() )
2bda0e17 154 {
4d0d77af
VZ
155 notifyData.uFlags |= NIF_TIP;
156 lstrcpyn(notifyData.szTip, tooltip.c_str(), WXSIZEOF(notifyData.szTip));
2bda0e17
KB
157 }
158
4d0d77af
VZ
159 bool ok = Shell_NotifyIcon(m_iconAdded ? NIM_MODIFY
160 : NIM_ADD, &notifyData) != 0;
2bda0e17 161
4d0d77af
VZ
162 if ( !m_iconAdded && ok )
163 m_iconAdded = true;
164
165 return ok;
2bda0e17
KB
166}
167
4d0d77af 168bool wxTaskBarIcon::RemoveIcon()
2bda0e17
KB
169{
170 if (!m_iconAdded)
04cd30de 171 return false;
2bda0e17 172
04cd30de 173 m_iconAdded = false;
2bda0e17 174
4d0d77af
VZ
175 NotifyIconData notifyData(m_hWnd);
176
177 return Shell_NotifyIcon(NIM_DELETE, &notifyData) != 0;
2bda0e17
KB
178}
179
4d0d77af 180bool wxTaskBarIcon::PopupMenu(wxMenu *menu)
69ecd30f 181{
04cd30de 182 static bool s_inPopup = false;
c7527e3f
JS
183
184 if (s_inPopup)
04cd30de 185 return false;
c7527e3f 186
04cd30de 187 s_inPopup = true;
c7527e3f 188
04cd30de 189 bool rval = false;
69ecd30f
RD
190 wxWindow* win;
191 int x, y;
192 wxGetMousePosition(&x, &y);
193
194 // is wxFrame the best window type to use???
2b5f62a0 195 win = new wxFrame(NULL, -1, wxEmptyString, wxPoint(x,y), wxSize(-1,-1), 0);
69ecd30f
RD
196 win->PushEventHandler(this);
197
d66d9d5b
JS
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
50bcd1ae
JS
202 menu->UpdateUI();
203
f6bcfd97
BP
204 // Work around a WIN32 bug
205 ::SetForegroundWindow ((HWND) win->GetHWND ());
206
69ecd30f
RD
207 rval = win->PopupMenu(menu, 0, 0);
208
f6bcfd97
BP
209 // Work around a WIN32 bug
210 ::PostMessage ((HWND) win->GetHWND(),WM_NULL,0,0L);
211
04cd30de 212 win->PopEventHandler(false);
50c319be 213 win->Destroy();
c7527e3f
JS
214 delete win;
215
04cd30de 216 s_inPopup = false;
d66d9d5b 217
69ecd30f
RD
218 return rval;
219}
220
6af507f7 221#if WXWIN_COMPATIBILITY_2_4
2bda0e17 222// Overridables
6af507f7
VS
223void wxTaskBarIcon::OnMouseMove(wxEvent&) {}
224void wxTaskBarIcon::OnLButtonDown(wxEvent&) {}
225void wxTaskBarIcon::OnLButtonUp(wxEvent&) {}
226void wxTaskBarIcon::OnRButtonDown(wxEvent&) {}
227void wxTaskBarIcon::OnRButtonUp(wxEvent&) {}
228void wxTaskBarIcon::OnLButtonDClick(wxEvent&) {}
229void wxTaskBarIcon::OnRButtonDClick(wxEvent&) {}
2bda0e17 230
69ecd30f
RD
231void wxTaskBarIcon::_OnMouseMove(wxEvent& e) { OnMouseMove(e); }
232void wxTaskBarIcon::_OnLButtonDown(wxEvent& e) { OnLButtonDown(e); }
233void wxTaskBarIcon::_OnLButtonUp(wxEvent& e) { OnLButtonUp(e); }
234void wxTaskBarIcon::_OnRButtonDown(wxEvent& e) { OnRButtonDown(e); }
235void wxTaskBarIcon::_OnRButtonUp(wxEvent& e) { OnRButtonUp(e); }
236void wxTaskBarIcon::_OnLButtonDClick(wxEvent& e) { OnLButtonDClick(e); }
237void wxTaskBarIcon::_OnRButtonDClick(wxEvent& e) { OnRButtonDClick(e); }
6af507f7 238#endif
69ecd30f 239
2bda0e17
KB
240wxTaskBarIcon* wxTaskBarIcon::FindObjectForHWND(WXHWND hWnd)
241{
d162a7ee 242 wxTaskBarIconList::Node *node = sm_taskBarIcons.GetFirst();
2bda0e17
KB
243 while (node)
244 {
d162a7ee 245 wxTaskBarIcon *obj = node->GetData();
2bda0e17
KB
246 if (obj->GetHWND() == hWnd)
247 return obj;
04cd30de 248 node = node->GetNext();
2bda0e17
KB
249 }
250 return NULL;
251}
252
253void wxTaskBarIcon::AddObject(wxTaskBarIcon* obj)
254{
255 sm_taskBarIcons.Append(obj);
256}
257
258void wxTaskBarIcon::RemoveObject(wxTaskBarIcon* obj)
259{
260 sm_taskBarIcons.DeleteObject(obj);
261}
262
263bool wxTaskBarIcon::RegisterWindowClass()
264{
4d0d77af 265 static bool s_registered = false;
2bda0e17 266
4d0d77af
VZ
267 if ( s_registered )
268 return true;
2bda0e17 269
4d0d77af
VZ
270 // Taskbar restart msg will be sent to us if the icon needs to be redrawn
271 gs_msgRestartTaskbar = RegisterWindowMessage(wxT("TaskbarCreated"));
2bda0e17 272
4d0d77af
VZ
273 // Also register the taskbar message here
274 gs_msgTaskbar = ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
2bda0e17 275
4d0d77af
VZ
276 // set up and register window class
277 WNDCLASS wc;
2bda0e17
KB
278 wc.style = CS_HREDRAW | CS_VREDRAW;
279 wc.lpfnWndProc = (WNDPROC) wxTaskBarIconWindowProc;
280 wc.cbClsExtra = 0;
281 wc.cbWndExtra = 0;
4d0d77af 282 wc.hInstance = wxGetInstance();
2bda0e17
KB
283 wc.hIcon = 0;
284 wc.hCursor = 0;
285 wc.hbrBackground = 0;
286 wc.lpszMenuName = NULL;
4d0d77af
VZ
287 wc.lpszClassName = wxTaskBarWindowClass;
288
289 if ( !::RegisterClass(&wc) )
290 {
291 wxLogLastError(_T("RegisterClass(taskbar icon)"));
292
293 return false;
294 }
2bda0e17 295
4d0d77af 296 s_registered = true;
2bda0e17 297
4d0d77af 298 return true;
2bda0e17
KB
299}
300
301WXHWND wxTaskBarIcon::CreateTaskBarWindow()
302{
303 HINSTANCE hInstance = GetModuleHandle(NULL);
304
305 HWND hWnd = CreateWindowEx (0, wxTaskBarWindowClass,
223d09f6 306 wxT("wxTaskBarWindow"),
2bda0e17
KB
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
4d0d77af
VZ
320// ----------------------------------------------------------------------------
321// wxTaskBarIcon window proc
322// ----------------------------------------------------------------------------
323
324long wxTaskBarIcon::WindowProc(WXHWND hWnd,
325 unsigned int msg,
326 unsigned int wParam,
327 long lParam)
2bda0e17 328{
56194595
RD
329 wxEventType eventType = 0;
330
4d0d77af
VZ
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)
2bda0e17
KB
338 return DefWindowProc((HWND) hWnd, msg, wParam, lParam);
339
340 switch (lParam)
341 {
c42404a5 342 case WM_LBUTTONDOWN:
56194595
RD
343 eventType = wxEVT_TASKBAR_LEFT_DOWN;
344 break;
2bda0e17 345
c42404a5 346 case WM_LBUTTONUP:
56194595
RD
347 eventType = wxEVT_TASKBAR_LEFT_UP;
348 break;
2bda0e17 349
c42404a5 350 case WM_RBUTTONDOWN:
56194595
RD
351 eventType = wxEVT_TASKBAR_RIGHT_DOWN;
352 break;
2bda0e17 353
c42404a5 354 case WM_RBUTTONUP:
56194595
RD
355 eventType = wxEVT_TASKBAR_RIGHT_UP;
356 break;
2bda0e17 357
c42404a5 358 case WM_LBUTTONDBLCLK:
56194595
RD
359 eventType = wxEVT_TASKBAR_LEFT_DCLICK;
360 break;
2bda0e17 361
c42404a5 362 case WM_RBUTTONDBLCLK:
56194595
RD
363 eventType = wxEVT_TASKBAR_RIGHT_DCLICK;
364 break;
2bda0e17 365
c42404a5 366 case WM_MOUSEMOVE:
56194595
RD
367 eventType = wxEVT_TASKBAR_MOVE;
368 break;
2bda0e17 369
c42404a5 370 default:
56194595 371 break;
4d0d77af 372 }
56194595 373
4d0d77af
VZ
374 if (eventType)
375 {
fa1c12bd 376 wxTaskBarIconEvent event(eventType, this);
56194595
RD
377
378 ProcessEvent(event);
379 }
4d0d77af 380
2bda0e17
KB
381 return 0;
382}
383
4d0d77af
VZ
384LRESULT APIENTRY _EXPORT
385wxTaskBarIconWindowProc(HWND hWnd, unsigned msg, UINT wParam, LONG lParam)
2bda0e17 386{
d162a7ee 387 wxTaskBarIcon *obj = wxTaskBarIcon::FindObjectForHWND((WXHWND) hWnd);
2bda0e17
KB
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__