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