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