]> git.saurik.com Git - wxWidgets.git/blame - src/msw/taskbar.cpp
Initialise font family when initializing from native font, since this
[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>
3096bd2f
VZ
39#include "wx/msw/taskbar.h"
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
2bda0e17 52LRESULT APIENTRY _EXPORT wxTaskBarIconWindowProc( HWND hWnd, unsigned msg,
c42404a5 53 UINT wParam, LONG lParam );
2bda0e17 54
ba14d986 55wxChar *wxTaskBarWindowClass = (wxChar*) wxT("wxTaskBarWindowClass");
2bda0e17
KB
56
57wxList wxTaskBarIcon::sm_taskBarIcons;
58bool wxTaskBarIcon::sm_registeredClass = FALSE;
59UINT wxTaskBarIcon::sm_taskbarMsg = 0;
60
459e74ba
MB
61DEFINE_EVENT_TYPE( wxEVT_TASKBAR_MOVE )
62DEFINE_EVENT_TYPE( wxEVT_TASKBAR_LEFT_DOWN )
63DEFINE_EVENT_TYPE( wxEVT_TASKBAR_LEFT_UP )
64DEFINE_EVENT_TYPE( wxEVT_TASKBAR_RIGHT_DOWN )
65DEFINE_EVENT_TYPE( wxEVT_TASKBAR_RIGHT_UP )
66DEFINE_EVENT_TYPE( wxEVT_TASKBAR_LEFT_DCLICK )
67DEFINE_EVENT_TYPE( wxEVT_TASKBAR_RIGHT_DCLICK )
56194595 68
56194595 69BEGIN_EVENT_TABLE(wxTaskBarIcon, wxEvtHandler)
69ecd30f
RD
70 EVT_TASKBAR_MOVE (wxTaskBarIcon::_OnMouseMove)
71 EVT_TASKBAR_LEFT_DOWN (wxTaskBarIcon::_OnLButtonDown)
72 EVT_TASKBAR_LEFT_UP (wxTaskBarIcon::_OnLButtonUp)
73 EVT_TASKBAR_RIGHT_DOWN (wxTaskBarIcon::_OnRButtonDown)
74 EVT_TASKBAR_RIGHT_UP (wxTaskBarIcon::_OnRButtonUp)
75 EVT_TASKBAR_LEFT_DCLICK (wxTaskBarIcon::_OnLButtonDClick)
76 EVT_TASKBAR_RIGHT_DCLICK (wxTaskBarIcon::_OnRButtonDClick)
56194595
RD
77END_EVENT_TABLE()
78
79
80IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
56194595
RD
81
82
2bda0e17
KB
83wxTaskBarIcon::wxTaskBarIcon(void)
84{
85 m_hWnd = 0;
86 m_iconAdded = FALSE;
87
88 AddObject(this);
89
90 if (RegisterWindowClass())
91 m_hWnd = CreateTaskBarWindow();
92}
93
94wxTaskBarIcon::~wxTaskBarIcon(void)
95{
96 RemoveObject(this);
97
98 if (m_iconAdded)
99 {
100 RemoveIcon();
101 }
102
103 if (m_hWnd)
104 {
105 ::DestroyWindow((HWND) m_hWnd);
106 m_hWnd = 0;
107 }
108}
109
110// Operations
111bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
112{
113 if (!IsOK())
114 return FALSE;
115
116 NOTIFYICONDATA notifyData;
117
118 memset(&notifyData, 0, sizeof(notifyData));
c42404a5
VZ
119 notifyData.cbSize = sizeof(notifyData);
120 notifyData.hWnd = (HWND) m_hWnd;
121 notifyData.uCallbackMessage = sm_taskbarMsg;
122 notifyData.uFlags = NIF_MESSAGE ;
e96360ef 123 if (icon.Ok())
2bda0e17 124 {
e96360ef
JS
125 notifyData.uFlags |= NIF_ICON;
126 notifyData.hIcon = (HICON) icon.GetHICON();
2bda0e17
KB
127 }
128
223d09f6 129 if (((const wxChar*) tooltip != NULL) && (tooltip != wxT("")))
2bda0e17
KB
130 {
131 notifyData.uFlags |= NIF_TIP ;
c42404a5 132 lstrcpyn(notifyData.szTip, WXSTRINGCAST tooltip, sizeof(notifyData.szTip));
2bda0e17
KB
133 }
134
135 notifyData.uID = 99;
136
137 if (m_iconAdded)
138 return (Shell_NotifyIcon(NIM_MODIFY, & notifyData) != 0);
139 else
140 {
141 m_iconAdded = (Shell_NotifyIcon(NIM_ADD, & notifyData) != 0);
142 return m_iconAdded;
143 }
144}
145
146bool wxTaskBarIcon::RemoveIcon(void)
147{
148 if (!m_iconAdded)
149 return FALSE;
150
151 NOTIFYICONDATA notifyData;
152
153 memset(&notifyData, 0, sizeof(notifyData));
c42404a5
VZ
154 notifyData.cbSize = sizeof(notifyData);
155 notifyData.hWnd = (HWND) m_hWnd;
156 notifyData.uCallbackMessage = sm_taskbarMsg;
157 notifyData.uFlags = NIF_MESSAGE;
158 notifyData.hIcon = 0 ; // hIcon;
2bda0e17
KB
159 notifyData.uID = 99;
160 m_iconAdded = FALSE;
161
162 return (Shell_NotifyIcon(NIM_DELETE, & notifyData) != 0);
163}
164
69ecd30f
RD
165bool wxTaskBarIcon::PopupMenu(wxMenu *menu) //, int x, int y);
166{
c7527e3f
JS
167 // OK, so I know this isn't thread-friendly, but
168 // what to do? We need this check.
169
170 static bool s_inPopup = FALSE;
171
172 if (s_inPopup)
173 return FALSE;
174
175 s_inPopup = TRUE;
176
69ecd30f
RD
177 bool rval = FALSE;
178 wxWindow* win;
179 int x, y;
180 wxGetMousePosition(&x, &y);
181
182 // is wxFrame the best window type to use???
183 win = new wxFrame(NULL, -1, "", wxPoint(x,y), wxSize(-1,-1), 0);
184 win->PushEventHandler(this);
185
d66d9d5b
JS
186 // Remove from record of top-level windows, or will confuse wxWindows
187 // if we try to exit right now.
188 wxTopLevelWindows.DeleteObject(win);
189
50bcd1ae
JS
190 menu->UpdateUI();
191
f6bcfd97
BP
192 // Work around a WIN32 bug
193 ::SetForegroundWindow ((HWND) win->GetHWND ());
194
69ecd30f
RD
195 rval = win->PopupMenu(menu, 0, 0);
196
f6bcfd97
BP
197 // Work around a WIN32 bug
198 ::PostMessage ((HWND) win->GetHWND(),WM_NULL,0,0L);
199
69ecd30f 200 win->PopEventHandler(FALSE);
50c319be 201 win->Destroy();
c7527e3f
JS
202 delete win;
203
204 s_inPopup = FALSE;
d66d9d5b 205
69ecd30f
RD
206 return rval;
207}
208
209
2bda0e17 210// Overridables
56194595 211void wxTaskBarIcon::OnMouseMove(wxEvent&)
2bda0e17
KB
212{
213}
214
56194595 215void wxTaskBarIcon::OnLButtonDown(wxEvent&)
2bda0e17
KB
216{
217}
218
56194595 219void wxTaskBarIcon::OnLButtonUp(wxEvent&)
2bda0e17
KB
220{
221}
222
56194595 223void wxTaskBarIcon::OnRButtonDown(wxEvent&)
2bda0e17
KB
224{
225}
226
56194595 227void wxTaskBarIcon::OnRButtonUp(wxEvent&)
2bda0e17
KB
228{
229}
230
56194595 231void wxTaskBarIcon::OnLButtonDClick(wxEvent&)
2bda0e17
KB
232{
233}
234
56194595 235void wxTaskBarIcon::OnRButtonDClick(wxEvent&)
2bda0e17
KB
236{
237}
238
69ecd30f
RD
239void wxTaskBarIcon::_OnMouseMove(wxEvent& e) { OnMouseMove(e); }
240void wxTaskBarIcon::_OnLButtonDown(wxEvent& e) { OnLButtonDown(e); }
241void wxTaskBarIcon::_OnLButtonUp(wxEvent& e) { OnLButtonUp(e); }
242void wxTaskBarIcon::_OnRButtonDown(wxEvent& e) { OnRButtonDown(e); }
243void wxTaskBarIcon::_OnRButtonUp(wxEvent& e) { OnRButtonUp(e); }
244void wxTaskBarIcon::_OnLButtonDClick(wxEvent& e) { OnLButtonDClick(e); }
245void wxTaskBarIcon::_OnRButtonDClick(wxEvent& e) { OnRButtonDClick(e); }
246
247
2bda0e17
KB
248wxTaskBarIcon* wxTaskBarIcon::FindObjectForHWND(WXHWND hWnd)
249{
250 wxNode*node = sm_taskBarIcons.First();
251 while (node)
252 {
253 wxTaskBarIcon* obj = (wxTaskBarIcon*) node->Data();
254 if (obj->GetHWND() == hWnd)
255 return obj;
256 node = node->Next();
257 }
258 return NULL;
259}
260
261void wxTaskBarIcon::AddObject(wxTaskBarIcon* obj)
262{
263 sm_taskBarIcons.Append(obj);
264}
265
266void wxTaskBarIcon::RemoveObject(wxTaskBarIcon* obj)
267{
268 sm_taskBarIcons.DeleteObject(obj);
269}
270
271bool wxTaskBarIcon::RegisterWindowClass()
272{
273 if (sm_registeredClass)
274 return TRUE;
275
276 // Also register the taskbar message here
223d09f6 277 sm_taskbarMsg = ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
2bda0e17 278
c42404a5
VZ
279 WNDCLASS wc;
280 bool rc;
2bda0e17
KB
281
282 HINSTANCE hInstance = GetModuleHandle(NULL);
283
284 /*
285 * set up and register window class
286 */
287 wc.style = CS_HREDRAW | CS_VREDRAW;
288 wc.lpfnWndProc = (WNDPROC) wxTaskBarIconWindowProc;
289 wc.cbClsExtra = 0;
290 wc.cbWndExtra = 0;
291 wc.hInstance = hInstance;
292 wc.hIcon = 0;
293 wc.hCursor = 0;
294 wc.hbrBackground = 0;
295 wc.lpszMenuName = NULL;
296 wc.lpszClassName = wxTaskBarWindowClass ;
297 rc = (::RegisterClass( &wc ) != 0);
298
299 sm_registeredClass = (rc != 0);
300
301 return( (rc != 0) );
302}
303
304WXHWND wxTaskBarIcon::CreateTaskBarWindow()
305{
306 HINSTANCE hInstance = GetModuleHandle(NULL);
307
308 HWND hWnd = CreateWindowEx (0, wxTaskBarWindowClass,
223d09f6 309 wxT("wxTaskBarWindow"),
2bda0e17
KB
310 WS_OVERLAPPED,
311 0,
312 0,
313 10,
314 10,
315 NULL,
316 (HMENU) 0,
317 hInstance,
318 NULL);
319
320 return (WXHWND) hWnd;
321}
322
323long wxTaskBarIcon::WindowProc( WXHWND hWnd, unsigned int msg, unsigned int wParam, long lParam )
324{
56194595
RD
325 wxEventType eventType = 0;
326
2bda0e17
KB
327 if (msg != sm_taskbarMsg)
328 return DefWindowProc((HWND) hWnd, msg, wParam, lParam);
329
330 switch (lParam)
331 {
c42404a5 332 case WM_LBUTTONDOWN:
56194595
RD
333 eventType = wxEVT_TASKBAR_LEFT_DOWN;
334 break;
2bda0e17 335
c42404a5 336 case WM_LBUTTONUP:
56194595
RD
337 eventType = wxEVT_TASKBAR_LEFT_UP;
338 break;
2bda0e17 339
c42404a5 340 case WM_RBUTTONDOWN:
56194595
RD
341 eventType = wxEVT_TASKBAR_RIGHT_DOWN;
342 break;
2bda0e17 343
c42404a5 344 case WM_RBUTTONUP:
56194595
RD
345 eventType = wxEVT_TASKBAR_RIGHT_UP;
346 break;
2bda0e17 347
c42404a5 348 case WM_LBUTTONDBLCLK:
56194595
RD
349 eventType = wxEVT_TASKBAR_LEFT_DCLICK;
350 break;
2bda0e17 351
c42404a5 352 case WM_RBUTTONDBLCLK:
56194595
RD
353 eventType = wxEVT_TASKBAR_RIGHT_DCLICK;
354 break;
2bda0e17 355
c42404a5 356 case WM_MOUSEMOVE:
56194595
RD
357 eventType = wxEVT_TASKBAR_MOVE;
358 break;
2bda0e17 359
c42404a5 360 default:
56194595 361 break;
c42404a5 362 }
56194595
RD
363
364 if (eventType) {
fa1c12bd 365 wxTaskBarIconEvent event(eventType, this);
56194595
RD
366
367 ProcessEvent(event);
368 }
2bda0e17
KB
369 return 0;
370}
371
372LRESULT APIENTRY _EXPORT wxTaskBarIconWindowProc( HWND hWnd, unsigned msg,
c42404a5 373 UINT wParam, LONG lParam )
2bda0e17
KB
374{
375 wxTaskBarIcon* obj = wxTaskBarIcon::FindObjectForHWND((WXHWND) hWnd);
376 if (obj)
377 return obj->WindowProc((WXHWND) hWnd, msg, wParam, lParam);
378 else
379 return DefWindowProc(hWnd, msg, wParam, lParam);
380}
381
382#endif
383 // __WIN95__