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