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