]>
Commit | Line | Data |
---|---|---|
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" | |
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> |
2bda0e17 | 35 | #include <string.h> |
3096bd2f VZ |
36 | #include "wx/msw/taskbar.h" |
37 | #include "wx/msw/private.h" | |
2bda0e17 | 38 | |
57c208c5 | 39 | #ifndef __TWIN32__ |
2bda0e17 | 40 | #ifdef __GNUWIN32__ |
65fd5cb0 | 41 | #ifndef wxUSE_NORLANDER_HEADERS |
3096bd2f | 42 | #include "wx/msw/gnuwin32/extra.h" |
2bda0e17 | 43 | #endif |
57c208c5 | 44 | #endif |
65fd5cb0 | 45 | #endif |
2bda0e17 | 46 | |
ce3ed50d JS |
47 | #ifdef __SALFORDC__ |
48 | #include <shellapi.h> | |
49 | #endif | |
50 | ||
2bda0e17 KB |
51 | LRESULT APIENTRY _EXPORT wxTaskBarIconWindowProc( HWND hWnd, unsigned msg, |
52 | UINT wParam, LONG lParam ); | |
53 | ||
223d09f6 | 54 | wxChar *wxTaskBarWindowClass = wxT("wxTaskBarWindowClass"); |
2bda0e17 KB |
55 | |
56 | wxList wxTaskBarIcon::sm_taskBarIcons; | |
57 | bool wxTaskBarIcon::sm_registeredClass = FALSE; | |
58 | UINT wxTaskBarIcon::sm_taskbarMsg = 0; | |
59 | ||
56194595 | 60 | |
56194595 | 61 | BEGIN_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 |
69 | END_EVENT_TABLE() |
70 | ||
71 | ||
72 | IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler) | |
56194595 RD |
73 | |
74 | ||
2bda0e17 KB |
75 | wxTaskBarIcon::wxTaskBarIcon(void) |
76 | { | |
77 | m_hWnd = 0; | |
78 | m_iconAdded = FALSE; | |
79 | ||
80 | AddObject(this); | |
81 | ||
82 | if (RegisterWindowClass()) | |
83 | m_hWnd = CreateTaskBarWindow(); | |
84 | } | |
85 | ||
86 | wxTaskBarIcon::~wxTaskBarIcon(void) | |
87 | { | |
88 | RemoveObject(this); | |
89 | ||
90 | if (m_iconAdded) | |
91 | { | |
92 | RemoveIcon(); | |
93 | } | |
94 | ||
95 | if (m_hWnd) | |
96 | { | |
97 | ::DestroyWindow((HWND) m_hWnd); | |
98 | m_hWnd = 0; | |
99 | } | |
100 | } | |
101 | ||
102 | // Operations | |
103 | bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip) | |
104 | { | |
105 | if (!IsOK()) | |
106 | return FALSE; | |
107 | ||
108 | NOTIFYICONDATA notifyData; | |
109 | ||
110 | memset(¬ifyData, 0, sizeof(notifyData)); | |
111 | notifyData.cbSize = sizeof(notifyData); | |
112 | notifyData.hWnd = (HWND) m_hWnd; | |
113 | notifyData.uCallbackMessage = sm_taskbarMsg; | |
114 | notifyData.uFlags = NIF_MESSAGE ; | |
115 | if (icon.Ok()) | |
116 | { | |
117 | notifyData.uFlags |= NIF_ICON; | |
118 | notifyData.hIcon = (HICON) icon.GetHICON(); | |
119 | } | |
120 | ||
223d09f6 | 121 | if (((const wxChar*) tooltip != NULL) && (tooltip != wxT(""))) |
2bda0e17 KB |
122 | { |
123 | notifyData.uFlags |= NIF_TIP ; | |
837e5743 | 124 | lstrcpyn(notifyData.szTip, WXSTRINGCAST tooltip, sizeof(notifyData.szTip)); |
2bda0e17 KB |
125 | } |
126 | ||
127 | notifyData.uID = 99; | |
128 | ||
129 | if (m_iconAdded) | |
130 | return (Shell_NotifyIcon(NIM_MODIFY, & notifyData) != 0); | |
131 | else | |
132 | { | |
133 | m_iconAdded = (Shell_NotifyIcon(NIM_ADD, & notifyData) != 0); | |
134 | return m_iconAdded; | |
135 | } | |
136 | } | |
137 | ||
138 | bool wxTaskBarIcon::RemoveIcon(void) | |
139 | { | |
140 | if (!m_iconAdded) | |
141 | return FALSE; | |
142 | ||
143 | NOTIFYICONDATA notifyData; | |
144 | ||
145 | memset(¬ifyData, 0, sizeof(notifyData)); | |
146 | notifyData.cbSize = sizeof(notifyData); | |
147 | notifyData.hWnd = (HWND) m_hWnd; | |
148 | notifyData.uCallbackMessage = sm_taskbarMsg; | |
149 | notifyData.uFlags = NIF_MESSAGE; | |
150 | notifyData.hIcon = 0 ; // hIcon; | |
151 | notifyData.uID = 99; | |
152 | m_iconAdded = FALSE; | |
153 | ||
154 | return (Shell_NotifyIcon(NIM_DELETE, & notifyData) != 0); | |
155 | } | |
156 | ||
69ecd30f RD |
157 | bool wxTaskBarIcon::PopupMenu(wxMenu *menu) //, int x, int y); |
158 | { | |
c7527e3f JS |
159 | // OK, so I know this isn't thread-friendly, but |
160 | // what to do? We need this check. | |
161 | ||
162 | static bool s_inPopup = FALSE; | |
163 | ||
164 | if (s_inPopup) | |
165 | return FALSE; | |
166 | ||
167 | s_inPopup = TRUE; | |
168 | ||
69ecd30f RD |
169 | bool rval = FALSE; |
170 | wxWindow* win; | |
171 | int x, y; | |
172 | wxGetMousePosition(&x, &y); | |
173 | ||
174 | // is wxFrame the best window type to use??? | |
175 | win = new wxFrame(NULL, -1, "", wxPoint(x,y), wxSize(-1,-1), 0); | |
176 | win->PushEventHandler(this); | |
177 | ||
d66d9d5b JS |
178 | // Remove from record of top-level windows, or will confuse wxWindows |
179 | // if we try to exit right now. | |
180 | wxTopLevelWindows.DeleteObject(win); | |
181 | ||
50bcd1ae JS |
182 | menu->UpdateUI(); |
183 | ||
69ecd30f RD |
184 | rval = win->PopupMenu(menu, 0, 0); |
185 | ||
186 | win->PopEventHandler(FALSE); | |
50c319be | 187 | win->Destroy(); |
c7527e3f JS |
188 | delete win; |
189 | ||
190 | s_inPopup = FALSE; | |
d66d9d5b | 191 | |
69ecd30f RD |
192 | return rval; |
193 | } | |
194 | ||
195 | ||
2bda0e17 | 196 | // Overridables |
56194595 | 197 | void wxTaskBarIcon::OnMouseMove(wxEvent&) |
2bda0e17 KB |
198 | { |
199 | } | |
200 | ||
56194595 | 201 | void wxTaskBarIcon::OnLButtonDown(wxEvent&) |
2bda0e17 KB |
202 | { |
203 | } | |
204 | ||
56194595 | 205 | void wxTaskBarIcon::OnLButtonUp(wxEvent&) |
2bda0e17 KB |
206 | { |
207 | } | |
208 | ||
56194595 | 209 | void wxTaskBarIcon::OnRButtonDown(wxEvent&) |
2bda0e17 KB |
210 | { |
211 | } | |
212 | ||
56194595 | 213 | void wxTaskBarIcon::OnRButtonUp(wxEvent&) |
2bda0e17 KB |
214 | { |
215 | } | |
216 | ||
56194595 | 217 | void wxTaskBarIcon::OnLButtonDClick(wxEvent&) |
2bda0e17 KB |
218 | { |
219 | } | |
220 | ||
56194595 | 221 | void wxTaskBarIcon::OnRButtonDClick(wxEvent&) |
2bda0e17 KB |
222 | { |
223 | } | |
224 | ||
69ecd30f RD |
225 | void wxTaskBarIcon::_OnMouseMove(wxEvent& e) { OnMouseMove(e); } |
226 | void wxTaskBarIcon::_OnLButtonDown(wxEvent& e) { OnLButtonDown(e); } | |
227 | void wxTaskBarIcon::_OnLButtonUp(wxEvent& e) { OnLButtonUp(e); } | |
228 | void wxTaskBarIcon::_OnRButtonDown(wxEvent& e) { OnRButtonDown(e); } | |
229 | void wxTaskBarIcon::_OnRButtonUp(wxEvent& e) { OnRButtonUp(e); } | |
230 | void wxTaskBarIcon::_OnLButtonDClick(wxEvent& e) { OnLButtonDClick(e); } | |
231 | void wxTaskBarIcon::_OnRButtonDClick(wxEvent& e) { OnRButtonDClick(e); } | |
232 | ||
233 | ||
2bda0e17 KB |
234 | wxTaskBarIcon* wxTaskBarIcon::FindObjectForHWND(WXHWND hWnd) |
235 | { | |
236 | wxNode*node = sm_taskBarIcons.First(); | |
237 | while (node) | |
238 | { | |
239 | wxTaskBarIcon* obj = (wxTaskBarIcon*) node->Data(); | |
240 | if (obj->GetHWND() == hWnd) | |
241 | return obj; | |
242 | node = node->Next(); | |
243 | } | |
244 | return NULL; | |
245 | } | |
246 | ||
247 | void wxTaskBarIcon::AddObject(wxTaskBarIcon* obj) | |
248 | { | |
249 | sm_taskBarIcons.Append(obj); | |
250 | } | |
251 | ||
252 | void wxTaskBarIcon::RemoveObject(wxTaskBarIcon* obj) | |
253 | { | |
254 | sm_taskBarIcons.DeleteObject(obj); | |
255 | } | |
256 | ||
257 | bool wxTaskBarIcon::RegisterWindowClass() | |
258 | { | |
259 | if (sm_registeredClass) | |
260 | return TRUE; | |
261 | ||
262 | // Also register the taskbar message here | |
223d09f6 | 263 | sm_taskbarMsg = ::RegisterWindowMessage(wxT("wxTaskBarIconMessage")); |
2bda0e17 KB |
264 | |
265 | WNDCLASS wc; | |
266 | bool rc; | |
267 | ||
268 | HINSTANCE hInstance = GetModuleHandle(NULL); | |
269 | ||
270 | /* | |
271 | * set up and register window class | |
272 | */ | |
273 | wc.style = CS_HREDRAW | CS_VREDRAW; | |
274 | wc.lpfnWndProc = (WNDPROC) wxTaskBarIconWindowProc; | |
275 | wc.cbClsExtra = 0; | |
276 | wc.cbWndExtra = 0; | |
277 | wc.hInstance = hInstance; | |
278 | wc.hIcon = 0; | |
279 | wc.hCursor = 0; | |
280 | wc.hbrBackground = 0; | |
281 | wc.lpszMenuName = NULL; | |
282 | wc.lpszClassName = wxTaskBarWindowClass ; | |
283 | rc = (::RegisterClass( &wc ) != 0); | |
284 | ||
285 | sm_registeredClass = (rc != 0); | |
286 | ||
287 | return( (rc != 0) ); | |
288 | } | |
289 | ||
290 | WXHWND wxTaskBarIcon::CreateTaskBarWindow() | |
291 | { | |
292 | HINSTANCE hInstance = GetModuleHandle(NULL); | |
293 | ||
294 | HWND hWnd = CreateWindowEx (0, wxTaskBarWindowClass, | |
223d09f6 | 295 | wxT("wxTaskBarWindow"), |
2bda0e17 KB |
296 | WS_OVERLAPPED, |
297 | 0, | |
298 | 0, | |
299 | 10, | |
300 | 10, | |
301 | NULL, | |
302 | (HMENU) 0, | |
303 | hInstance, | |
304 | NULL); | |
305 | ||
306 | return (WXHWND) hWnd; | |
307 | } | |
308 | ||
309 | long wxTaskBarIcon::WindowProc( WXHWND hWnd, unsigned int msg, unsigned int wParam, long lParam ) | |
310 | { | |
56194595 RD |
311 | wxEventType eventType = 0; |
312 | ||
2bda0e17 KB |
313 | if (msg != sm_taskbarMsg) |
314 | return DefWindowProc((HWND) hWnd, msg, wParam, lParam); | |
315 | ||
316 | switch (lParam) | |
317 | { | |
318 | case WM_LBUTTONDOWN: | |
56194595 RD |
319 | eventType = wxEVT_TASKBAR_LEFT_DOWN; |
320 | break; | |
2bda0e17 KB |
321 | |
322 | case WM_LBUTTONUP: | |
56194595 RD |
323 | eventType = wxEVT_TASKBAR_LEFT_UP; |
324 | break; | |
2bda0e17 KB |
325 | |
326 | case WM_RBUTTONDOWN: | |
56194595 RD |
327 | eventType = wxEVT_TASKBAR_RIGHT_DOWN; |
328 | break; | |
2bda0e17 KB |
329 | |
330 | case WM_RBUTTONUP: | |
56194595 RD |
331 | eventType = wxEVT_TASKBAR_RIGHT_UP; |
332 | break; | |
2bda0e17 KB |
333 | |
334 | case WM_LBUTTONDBLCLK: | |
56194595 RD |
335 | eventType = wxEVT_TASKBAR_LEFT_DCLICK; |
336 | break; | |
2bda0e17 KB |
337 | |
338 | case WM_RBUTTONDBLCLK: | |
56194595 RD |
339 | eventType = wxEVT_TASKBAR_RIGHT_DCLICK; |
340 | break; | |
2bda0e17 KB |
341 | |
342 | case WM_MOUSEMOVE: | |
56194595 RD |
343 | eventType = wxEVT_TASKBAR_MOVE; |
344 | break; | |
2bda0e17 KB |
345 | |
346 | default: | |
56194595 | 347 | break; |
2bda0e17 | 348 | } |
56194595 RD |
349 | |
350 | if (eventType) { | |
351 | wxEvent event; | |
352 | event.SetEventType(eventType); | |
353 | event.SetEventObject(this); | |
354 | ||
355 | ProcessEvent(event); | |
356 | } | |
2bda0e17 KB |
357 | return 0; |
358 | } | |
359 | ||
360 | LRESULT APIENTRY _EXPORT wxTaskBarIconWindowProc( HWND hWnd, unsigned msg, | |
361 | UINT wParam, LONG lParam ) | |
362 | { | |
363 | wxTaskBarIcon* obj = wxTaskBarIcon::FindObjectForHWND((WXHWND) hWnd); | |
364 | if (obj) | |
365 | return obj->WindowProc((WXHWND) hWnd, msg, wParam, lParam); | |
366 | else | |
367 | return DefWindowProc(hWnd, msg, wParam, lParam); | |
368 | } | |
369 | ||
370 | #endif | |
371 | // __WIN95__ |