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