]> git.saurik.com Git - wxWidgets.git/blob - src/msw/taskbar.cpp
Fixed my fix
[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 menu->UpdateUI();
174
175 rval = win->PopupMenu(menu, 0, 0);
176
177 win->PopEventHandler(FALSE);
178 win->Destroy();
179 //delete win;
180
181 return rval;
182 }
183
184
185 // Overridables
186 void wxTaskBarIcon::OnMouseMove(wxEvent&)
187 {
188 }
189
190 void wxTaskBarIcon::OnLButtonDown(wxEvent&)
191 {
192 }
193
194 void wxTaskBarIcon::OnLButtonUp(wxEvent&)
195 {
196 }
197
198 void wxTaskBarIcon::OnRButtonDown(wxEvent&)
199 {
200 }
201
202 void wxTaskBarIcon::OnRButtonUp(wxEvent&)
203 {
204 }
205
206 void wxTaskBarIcon::OnLButtonDClick(wxEvent&)
207 {
208 }
209
210 void wxTaskBarIcon::OnRButtonDClick(wxEvent&)
211 {
212 }
213
214 void wxTaskBarIcon::_OnMouseMove(wxEvent& e) { OnMouseMove(e); }
215 void wxTaskBarIcon::_OnLButtonDown(wxEvent& e) { OnLButtonDown(e); }
216 void wxTaskBarIcon::_OnLButtonUp(wxEvent& e) { OnLButtonUp(e); }
217 void wxTaskBarIcon::_OnRButtonDown(wxEvent& e) { OnRButtonDown(e); }
218 void wxTaskBarIcon::_OnRButtonUp(wxEvent& e) { OnRButtonUp(e); }
219 void wxTaskBarIcon::_OnLButtonDClick(wxEvent& e) { OnLButtonDClick(e); }
220 void wxTaskBarIcon::_OnRButtonDClick(wxEvent& e) { OnRButtonDClick(e); }
221
222
223 wxTaskBarIcon* wxTaskBarIcon::FindObjectForHWND(WXHWND hWnd)
224 {
225 wxNode*node = sm_taskBarIcons.First();
226 while (node)
227 {
228 wxTaskBarIcon* obj = (wxTaskBarIcon*) node->Data();
229 if (obj->GetHWND() == hWnd)
230 return obj;
231 node = node->Next();
232 }
233 return NULL;
234 }
235
236 void wxTaskBarIcon::AddObject(wxTaskBarIcon* obj)
237 {
238 sm_taskBarIcons.Append(obj);
239 }
240
241 void wxTaskBarIcon::RemoveObject(wxTaskBarIcon* obj)
242 {
243 sm_taskBarIcons.DeleteObject(obj);
244 }
245
246 bool wxTaskBarIcon::RegisterWindowClass()
247 {
248 if (sm_registeredClass)
249 return TRUE;
250
251 // Also register the taskbar message here
252 sm_taskbarMsg = ::RegisterWindowMessage(_T("wxTaskBarIconMessage"));
253
254 WNDCLASS wc;
255 bool rc;
256
257 HINSTANCE hInstance = GetModuleHandle(NULL);
258
259 /*
260 * set up and register window class
261 */
262 wc.style = CS_HREDRAW | CS_VREDRAW;
263 wc.lpfnWndProc = (WNDPROC) wxTaskBarIconWindowProc;
264 wc.cbClsExtra = 0;
265 wc.cbWndExtra = 0;
266 wc.hInstance = hInstance;
267 wc.hIcon = 0;
268 wc.hCursor = 0;
269 wc.hbrBackground = 0;
270 wc.lpszMenuName = NULL;
271 wc.lpszClassName = wxTaskBarWindowClass ;
272 rc = (::RegisterClass( &wc ) != 0);
273
274 sm_registeredClass = (rc != 0);
275
276 return( (rc != 0) );
277 }
278
279 WXHWND wxTaskBarIcon::CreateTaskBarWindow()
280 {
281 HINSTANCE hInstance = GetModuleHandle(NULL);
282
283 HWND hWnd = CreateWindowEx (0, wxTaskBarWindowClass,
284 _T("wxTaskBarWindow"),
285 WS_OVERLAPPED,
286 0,
287 0,
288 10,
289 10,
290 NULL,
291 (HMENU) 0,
292 hInstance,
293 NULL);
294
295 return (WXHWND) hWnd;
296 }
297
298 long wxTaskBarIcon::WindowProc( WXHWND hWnd, unsigned int msg, unsigned int wParam, long lParam )
299 {
300 wxEventType eventType = 0;
301
302 if (msg != sm_taskbarMsg)
303 return DefWindowProc((HWND) hWnd, msg, wParam, lParam);
304
305 switch (lParam)
306 {
307 case WM_LBUTTONDOWN:
308 eventType = wxEVT_TASKBAR_LEFT_DOWN;
309 break;
310
311 case WM_LBUTTONUP:
312 eventType = wxEVT_TASKBAR_LEFT_UP;
313 break;
314
315 case WM_RBUTTONDOWN:
316 eventType = wxEVT_TASKBAR_RIGHT_DOWN;
317 break;
318
319 case WM_RBUTTONUP:
320 eventType = wxEVT_TASKBAR_RIGHT_UP;
321 break;
322
323 case WM_LBUTTONDBLCLK:
324 eventType = wxEVT_TASKBAR_LEFT_DCLICK;
325 break;
326
327 case WM_RBUTTONDBLCLK:
328 eventType = wxEVT_TASKBAR_RIGHT_DCLICK;
329 break;
330
331 case WM_MOUSEMOVE:
332 eventType = wxEVT_TASKBAR_MOVE;
333 break;
334
335 default:
336 break;
337 }
338
339 if (eventType) {
340 wxEvent event;
341 event.SetEventType(eventType);
342 event.SetEventObject(this);
343
344 ProcessEvent(event);
345 }
346 return 0;
347 }
348
349 LRESULT APIENTRY _EXPORT wxTaskBarIconWindowProc( HWND hWnd, unsigned msg,
350 UINT wParam, LONG lParam )
351 {
352 wxTaskBarIcon* obj = wxTaskBarIcon::FindObjectForHWND((WXHWND) hWnd);
353 if (obj)
354 return obj->WindowProc((WXHWND) hWnd, msg, wParam, lParam);
355 else
356 return DefWindowProc(hWnd, msg, wParam, lParam);
357 }
358
359 #endif
360 // __WIN95__