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