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