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