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