]> git.saurik.com Git - wxWidgets.git/blame - src/msw/taskbar.cpp
fixed somebody's poorly done StreamSize-->GetSize transition
[wxWidgets.git] / src / msw / taskbar.cpp
CommitLineData
2bda0e17
KB
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"
ad5c34f3
JS
26#include "wx/window.h"
27#include "wx/frame.h"
28#include "wx/utils.h"
2bda0e17
KB
29#endif
30
57c208c5 31#if defined(__WIN95__) && !defined(__TWIN32__)
2bda0e17 32
3d05544e 33#include <windows.h>
2bda0e17 34#include <string.h>
ad5c34f3 35#include <wx/msw/taskbar.h>
2bda0e17
KB
36#include <wx/msw/private.h>
37
57c208c5 38#ifndef __TWIN32__
2bda0e17 39#ifdef __GNUWIN32__
65fd5cb0 40#ifndef wxUSE_NORLANDER_HEADERS
2bda0e17
KB
41#include <wx/msw/gnuwin32/extra.h>
42#endif
57c208c5 43#endif
65fd5cb0 44#endif
2bda0e17 45
ce3ed50d
JS
46#ifdef __SALFORDC__
47#include <shellapi.h>
48#endif
49
2bda0e17
KB
50LRESULT APIENTRY _EXPORT wxTaskBarIconWindowProc( HWND hWnd, unsigned msg,
51 UINT wParam, LONG lParam );
52
837e5743 53wxChar *wxTaskBarWindowClass = _T("wxTaskBarWindowClass");
2bda0e17
KB
54
55wxList wxTaskBarIcon::sm_taskBarIcons;
56bool wxTaskBarIcon::sm_registeredClass = FALSE;
57UINT wxTaskBarIcon::sm_taskbarMsg = 0;
58
56194595
RD
59
60#if !USE_SHARED_LIBRARY
61BEGIN_EVENT_TABLE(wxTaskBarIcon, wxEvtHandler)
69ecd30f
RD
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)
56194595
RD
69END_EVENT_TABLE()
70
71
72IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
73#endif
74
75
2bda0e17
KB
76wxTaskBarIcon::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
87wxTaskBarIcon::~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
104bool 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
837e5743 122 if (((const wxChar*) tooltip != NULL) && (tooltip != _T("")))
2bda0e17
KB
123 {
124 notifyData.uFlags |= NIF_TIP ;
837e5743 125 lstrcpyn(notifyData.szTip, WXSTRINGCAST tooltip, sizeof(notifyData.szTip));
2bda0e17
KB
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
139bool 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
69ecd30f
RD
158bool 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);
50c319be 172 win->Destroy();
69ecd30f
RD
173 return rval;
174}
175
176
2bda0e17 177// Overridables
56194595 178void wxTaskBarIcon::OnMouseMove(wxEvent&)
2bda0e17
KB
179{
180}
181
56194595 182void wxTaskBarIcon::OnLButtonDown(wxEvent&)
2bda0e17
KB
183{
184}
185
56194595 186void wxTaskBarIcon::OnLButtonUp(wxEvent&)
2bda0e17
KB
187{
188}
189
56194595 190void wxTaskBarIcon::OnRButtonDown(wxEvent&)
2bda0e17
KB
191{
192}
193
56194595 194void wxTaskBarIcon::OnRButtonUp(wxEvent&)
2bda0e17
KB
195{
196}
197
56194595 198void wxTaskBarIcon::OnLButtonDClick(wxEvent&)
2bda0e17
KB
199{
200}
201
56194595 202void wxTaskBarIcon::OnRButtonDClick(wxEvent&)
2bda0e17
KB
203{
204}
205
69ecd30f
RD
206void wxTaskBarIcon::_OnMouseMove(wxEvent& e) { OnMouseMove(e); }
207void wxTaskBarIcon::_OnLButtonDown(wxEvent& e) { OnLButtonDown(e); }
208void wxTaskBarIcon::_OnLButtonUp(wxEvent& e) { OnLButtonUp(e); }
209void wxTaskBarIcon::_OnRButtonDown(wxEvent& e) { OnRButtonDown(e); }
210void wxTaskBarIcon::_OnRButtonUp(wxEvent& e) { OnRButtonUp(e); }
211void wxTaskBarIcon::_OnLButtonDClick(wxEvent& e) { OnLButtonDClick(e); }
212void wxTaskBarIcon::_OnRButtonDClick(wxEvent& e) { OnRButtonDClick(e); }
213
214
2bda0e17
KB
215wxTaskBarIcon* 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
228void wxTaskBarIcon::AddObject(wxTaskBarIcon* obj)
229{
230 sm_taskBarIcons.Append(obj);
231}
232
233void wxTaskBarIcon::RemoveObject(wxTaskBarIcon* obj)
234{
235 sm_taskBarIcons.DeleteObject(obj);
236}
237
238bool wxTaskBarIcon::RegisterWindowClass()
239{
240 if (sm_registeredClass)
241 return TRUE;
242
243 // Also register the taskbar message here
837e5743 244 sm_taskbarMsg = ::RegisterWindowMessage(_T("wxTaskBarIconMessage"));
2bda0e17
KB
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
271WXHWND wxTaskBarIcon::CreateTaskBarWindow()
272{
273 HINSTANCE hInstance = GetModuleHandle(NULL);
274
275 HWND hWnd = CreateWindowEx (0, wxTaskBarWindowClass,
837e5743 276 _T("wxTaskBarWindow"),
2bda0e17
KB
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
290long wxTaskBarIcon::WindowProc( WXHWND hWnd, unsigned int msg, unsigned int wParam, long lParam )
291{
56194595
RD
292 wxEventType eventType = 0;
293
2bda0e17
KB
294 if (msg != sm_taskbarMsg)
295 return DefWindowProc((HWND) hWnd, msg, wParam, lParam);
296
297 switch (lParam)
298 {
299 case WM_LBUTTONDOWN:
56194595
RD
300 eventType = wxEVT_TASKBAR_LEFT_DOWN;
301 break;
2bda0e17
KB
302
303 case WM_LBUTTONUP:
56194595
RD
304 eventType = wxEVT_TASKBAR_LEFT_UP;
305 break;
2bda0e17
KB
306
307 case WM_RBUTTONDOWN:
56194595
RD
308 eventType = wxEVT_TASKBAR_RIGHT_DOWN;
309 break;
2bda0e17
KB
310
311 case WM_RBUTTONUP:
56194595
RD
312 eventType = wxEVT_TASKBAR_RIGHT_UP;
313 break;
2bda0e17
KB
314
315 case WM_LBUTTONDBLCLK:
56194595
RD
316 eventType = wxEVT_TASKBAR_LEFT_DCLICK;
317 break;
2bda0e17
KB
318
319 case WM_RBUTTONDBLCLK:
56194595
RD
320 eventType = wxEVT_TASKBAR_RIGHT_DCLICK;
321 break;
2bda0e17
KB
322
323 case WM_MOUSEMOVE:
56194595
RD
324 eventType = wxEVT_TASKBAR_MOVE;
325 break;
2bda0e17
KB
326
327 default:
56194595 328 break;
2bda0e17 329 }
56194595
RD
330
331 if (eventType) {
332 wxEvent event;
333 event.SetEventType(eventType);
334 event.SetEventObject(this);
335
336 ProcessEvent(event);
337 }
2bda0e17
KB
338 return 0;
339}
340
341LRESULT 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__