]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////// |
2 | // File: taskbar.cpp | |
c42404a5 | 3 | // Purpose: Implements wxTaskBarIcon class for manipulating icons on |
2bda0e17 KB |
4 | // the Windows task bar. |
5 | // Author: Julian Smart | |
6 | // Modified by: | |
7 | // Created: 24/3/98 | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) | |
c42404a5 | 10 | // Licence: wxWindows licence |
2bda0e17 KB |
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" | |
0ae2df30 | 29 | #include "wx/menu.h" |
2bda0e17 KB |
30 | #endif |
31 | ||
57c208c5 | 32 | #if defined(__WIN95__) && !defined(__TWIN32__) |
2bda0e17 | 33 | |
3d05544e | 34 | #include <windows.h> |
c25a510b JS |
35 | |
36 | #include "wx/msw/winundef.h" | |
37 | ||
2bda0e17 | 38 | #include <string.h> |
3096bd2f VZ |
39 | #include "wx/msw/taskbar.h" |
40 | #include "wx/msw/private.h" | |
2bda0e17 | 41 | |
57c208c5 | 42 | #ifndef __TWIN32__ |
c42404a5 VZ |
43 | #ifdef __GNUWIN32_OLD__ |
44 | #include "wx/msw/gnuwin32/extra.h" | |
45 | #endif | |
65fd5cb0 | 46 | #endif |
2bda0e17 | 47 | |
ce3ed50d | 48 | #ifdef __SALFORDC__ |
c42404a5 | 49 | #include <shellapi.h> |
ce3ed50d JS |
50 | #endif |
51 | ||
2bda0e17 | 52 | LRESULT APIENTRY _EXPORT wxTaskBarIconWindowProc( HWND hWnd, unsigned msg, |
c42404a5 | 53 | UINT wParam, LONG lParam ); |
2bda0e17 | 54 | |
223d09f6 | 55 | wxChar *wxTaskBarWindowClass = wxT("wxTaskBarWindowClass"); |
2bda0e17 KB |
56 | |
57 | wxList wxTaskBarIcon::sm_taskBarIcons; | |
58 | bool wxTaskBarIcon::sm_registeredClass = FALSE; | |
59 | UINT wxTaskBarIcon::sm_taskbarMsg = 0; | |
60 | ||
56194595 | 61 | |
56194595 | 62 | BEGIN_EVENT_TABLE(wxTaskBarIcon, wxEvtHandler) |
69ecd30f RD |
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) | |
56194595 RD |
70 | END_EVENT_TABLE() |
71 | ||
72 | ||
73 | IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler) | |
56194595 RD |
74 | |
75 | ||
2bda0e17 KB |
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(¬ifyData, 0, sizeof(notifyData)); | |
c42404a5 VZ |
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()) | |
2bda0e17 | 117 | { |
c42404a5 VZ |
118 | notifyData.uFlags |= NIF_ICON; |
119 | notifyData.hIcon = (HICON) icon.GetHICON(); | |
2bda0e17 KB |
120 | } |
121 | ||
223d09f6 | 122 | if (((const wxChar*) tooltip != NULL) && (tooltip != wxT(""))) |
2bda0e17 KB |
123 | { |
124 | notifyData.uFlags |= NIF_TIP ; | |
c42404a5 | 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 | ||
139 | bool wxTaskBarIcon::RemoveIcon(void) | |
140 | { | |
141 | if (!m_iconAdded) | |
142 | return FALSE; | |
143 | ||
144 | NOTIFYICONDATA notifyData; | |
145 | ||
146 | memset(¬ifyData, 0, sizeof(notifyData)); | |
c42404a5 VZ |
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; | |
2bda0e17 KB |
152 | notifyData.uID = 99; |
153 | m_iconAdded = FALSE; | |
154 | ||
155 | return (Shell_NotifyIcon(NIM_DELETE, & notifyData) != 0); | |
156 | } | |
157 | ||
69ecd30f RD |
158 | bool wxTaskBarIcon::PopupMenu(wxMenu *menu) //, int x, int y); |
159 | { | |
c7527e3f JS |
160 | // OK, so I know this isn't thread-friendly, but |
161 | // what to do? We need this check. | |
162 | ||
163 | static bool s_inPopup = FALSE; | |
164 | ||
165 | if (s_inPopup) | |
166 | return FALSE; | |
167 | ||
168 | s_inPopup = TRUE; | |
169 | ||
69ecd30f RD |
170 | bool rval = FALSE; |
171 | wxWindow* win; | |
172 | int x, y; | |
173 | wxGetMousePosition(&x, &y); | |
174 | ||
175 | // is wxFrame the best window type to use??? | |
176 | win = new wxFrame(NULL, -1, "", wxPoint(x,y), wxSize(-1,-1), 0); | |
177 | win->PushEventHandler(this); | |
178 | ||
d66d9d5b JS |
179 | // Remove from record of top-level windows, or will confuse wxWindows |
180 | // if we try to exit right now. | |
181 | wxTopLevelWindows.DeleteObject(win); | |
182 | ||
50bcd1ae JS |
183 | menu->UpdateUI(); |
184 | ||
f6bcfd97 BP |
185 | // Work around a WIN32 bug |
186 | ::SetForegroundWindow ((HWND) win->GetHWND ()); | |
187 | ||
69ecd30f RD |
188 | rval = win->PopupMenu(menu, 0, 0); |
189 | ||
f6bcfd97 BP |
190 | // Work around a WIN32 bug |
191 | ::PostMessage ((HWND) win->GetHWND(),WM_NULL,0,0L); | |
192 | ||
69ecd30f | 193 | win->PopEventHandler(FALSE); |
50c319be | 194 | win->Destroy(); |
c7527e3f JS |
195 | delete win; |
196 | ||
197 | s_inPopup = FALSE; | |
d66d9d5b | 198 | |
69ecd30f RD |
199 | return rval; |
200 | } | |
201 | ||
202 | ||
2bda0e17 | 203 | // Overridables |
56194595 | 204 | void wxTaskBarIcon::OnMouseMove(wxEvent&) |
2bda0e17 KB |
205 | { |
206 | } | |
207 | ||
56194595 | 208 | void wxTaskBarIcon::OnLButtonDown(wxEvent&) |
2bda0e17 KB |
209 | { |
210 | } | |
211 | ||
56194595 | 212 | void wxTaskBarIcon::OnLButtonUp(wxEvent&) |
2bda0e17 KB |
213 | { |
214 | } | |
215 | ||
56194595 | 216 | void wxTaskBarIcon::OnRButtonDown(wxEvent&) |
2bda0e17 KB |
217 | { |
218 | } | |
219 | ||
56194595 | 220 | void wxTaskBarIcon::OnRButtonUp(wxEvent&) |
2bda0e17 KB |
221 | { |
222 | } | |
223 | ||
56194595 | 224 | void wxTaskBarIcon::OnLButtonDClick(wxEvent&) |
2bda0e17 KB |
225 | { |
226 | } | |
227 | ||
56194595 | 228 | void wxTaskBarIcon::OnRButtonDClick(wxEvent&) |
2bda0e17 KB |
229 | { |
230 | } | |
231 | ||
69ecd30f RD |
232 | void wxTaskBarIcon::_OnMouseMove(wxEvent& e) { OnMouseMove(e); } |
233 | void wxTaskBarIcon::_OnLButtonDown(wxEvent& e) { OnLButtonDown(e); } | |
234 | void wxTaskBarIcon::_OnLButtonUp(wxEvent& e) { OnLButtonUp(e); } | |
235 | void wxTaskBarIcon::_OnRButtonDown(wxEvent& e) { OnRButtonDown(e); } | |
236 | void wxTaskBarIcon::_OnRButtonUp(wxEvent& e) { OnRButtonUp(e); } | |
237 | void wxTaskBarIcon::_OnLButtonDClick(wxEvent& e) { OnLButtonDClick(e); } | |
238 | void wxTaskBarIcon::_OnRButtonDClick(wxEvent& e) { OnRButtonDClick(e); } | |
239 | ||
240 | ||
2bda0e17 KB |
241 | wxTaskBarIcon* wxTaskBarIcon::FindObjectForHWND(WXHWND hWnd) |
242 | { | |
243 | wxNode*node = sm_taskBarIcons.First(); | |
244 | while (node) | |
245 | { | |
246 | wxTaskBarIcon* obj = (wxTaskBarIcon*) node->Data(); | |
247 | if (obj->GetHWND() == hWnd) | |
248 | return obj; | |
249 | node = node->Next(); | |
250 | } | |
251 | return NULL; | |
252 | } | |
253 | ||
254 | void wxTaskBarIcon::AddObject(wxTaskBarIcon* obj) | |
255 | { | |
256 | sm_taskBarIcons.Append(obj); | |
257 | } | |
258 | ||
259 | void wxTaskBarIcon::RemoveObject(wxTaskBarIcon* obj) | |
260 | { | |
261 | sm_taskBarIcons.DeleteObject(obj); | |
262 | } | |
263 | ||
264 | bool wxTaskBarIcon::RegisterWindowClass() | |
265 | { | |
266 | if (sm_registeredClass) | |
267 | return TRUE; | |
268 | ||
269 | // Also register the taskbar message here | |
223d09f6 | 270 | sm_taskbarMsg = ::RegisterWindowMessage(wxT("wxTaskBarIconMessage")); |
2bda0e17 | 271 | |
c42404a5 VZ |
272 | WNDCLASS wc; |
273 | bool rc; | |
2bda0e17 KB |
274 | |
275 | HINSTANCE hInstance = GetModuleHandle(NULL); | |
276 | ||
277 | /* | |
278 | * set up and register window class | |
279 | */ | |
280 | wc.style = CS_HREDRAW | CS_VREDRAW; | |
281 | wc.lpfnWndProc = (WNDPROC) wxTaskBarIconWindowProc; | |
282 | wc.cbClsExtra = 0; | |
283 | wc.cbWndExtra = 0; | |
284 | wc.hInstance = hInstance; | |
285 | wc.hIcon = 0; | |
286 | wc.hCursor = 0; | |
287 | wc.hbrBackground = 0; | |
288 | wc.lpszMenuName = NULL; | |
289 | wc.lpszClassName = wxTaskBarWindowClass ; | |
290 | rc = (::RegisterClass( &wc ) != 0); | |
291 | ||
292 | sm_registeredClass = (rc != 0); | |
293 | ||
294 | return( (rc != 0) ); | |
295 | } | |
296 | ||
297 | WXHWND wxTaskBarIcon::CreateTaskBarWindow() | |
298 | { | |
299 | HINSTANCE hInstance = GetModuleHandle(NULL); | |
300 | ||
301 | HWND hWnd = CreateWindowEx (0, wxTaskBarWindowClass, | |
223d09f6 | 302 | wxT("wxTaskBarWindow"), |
2bda0e17 KB |
303 | WS_OVERLAPPED, |
304 | 0, | |
305 | 0, | |
306 | 10, | |
307 | 10, | |
308 | NULL, | |
309 | (HMENU) 0, | |
310 | hInstance, | |
311 | NULL); | |
312 | ||
313 | return (WXHWND) hWnd; | |
314 | } | |
315 | ||
316 | long wxTaskBarIcon::WindowProc( WXHWND hWnd, unsigned int msg, unsigned int wParam, long lParam ) | |
317 | { | |
56194595 RD |
318 | wxEventType eventType = 0; |
319 | ||
2bda0e17 KB |
320 | if (msg != sm_taskbarMsg) |
321 | return DefWindowProc((HWND) hWnd, msg, wParam, lParam); | |
322 | ||
323 | switch (lParam) | |
324 | { | |
c42404a5 | 325 | case WM_LBUTTONDOWN: |
56194595 RD |
326 | eventType = wxEVT_TASKBAR_LEFT_DOWN; |
327 | break; | |
2bda0e17 | 328 | |
c42404a5 | 329 | case WM_LBUTTONUP: |
56194595 RD |
330 | eventType = wxEVT_TASKBAR_LEFT_UP; |
331 | break; | |
2bda0e17 | 332 | |
c42404a5 | 333 | case WM_RBUTTONDOWN: |
56194595 RD |
334 | eventType = wxEVT_TASKBAR_RIGHT_DOWN; |
335 | break; | |
2bda0e17 | 336 | |
c42404a5 | 337 | case WM_RBUTTONUP: |
56194595 RD |
338 | eventType = wxEVT_TASKBAR_RIGHT_UP; |
339 | break; | |
2bda0e17 | 340 | |
c42404a5 | 341 | case WM_LBUTTONDBLCLK: |
56194595 RD |
342 | eventType = wxEVT_TASKBAR_LEFT_DCLICK; |
343 | break; | |
2bda0e17 | 344 | |
c42404a5 | 345 | case WM_RBUTTONDBLCLK: |
56194595 RD |
346 | eventType = wxEVT_TASKBAR_RIGHT_DCLICK; |
347 | break; | |
2bda0e17 | 348 | |
c42404a5 | 349 | case WM_MOUSEMOVE: |
56194595 RD |
350 | eventType = wxEVT_TASKBAR_MOVE; |
351 | break; | |
2bda0e17 | 352 | |
c42404a5 | 353 | default: |
56194595 | 354 | break; |
c42404a5 | 355 | } |
56194595 RD |
356 | |
357 | if (eventType) { | |
358 | wxEvent event; | |
359 | event.SetEventType(eventType); | |
360 | event.SetEventObject(this); | |
361 | ||
362 | ProcessEvent(event); | |
363 | } | |
2bda0e17 KB |
364 | return 0; |
365 | } | |
366 | ||
367 | LRESULT APIENTRY _EXPORT wxTaskBarIconWindowProc( HWND hWnd, unsigned msg, | |
c42404a5 | 368 | UINT wParam, LONG lParam ) |
2bda0e17 KB |
369 | { |
370 | wxTaskBarIcon* obj = wxTaskBarIcon::FindObjectForHWND((WXHWND) hWnd); | |
371 | if (obj) | |
372 | return obj->WindowProc((WXHWND) hWnd, msg, wParam, lParam); | |
373 | else | |
374 | return DefWindowProc(hWnd, msg, wParam, lParam); | |
375 | } | |
376 | ||
377 | #endif | |
378 | // __WIN95__ |