]>
Commit | Line | Data |
---|---|---|
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 | #include "wx/menu.h" | |
30 | #endif | |
31 | ||
32 | #if defined(__WIN95__) && !defined(__TWIN32__) | |
33 | ||
34 | #include <windows.h> | |
35 | ||
36 | #include "wx/msw/winundef.h" | |
37 | ||
38 | #include <string.h> | |
39 | #include "wx/msw/taskbar.h" | |
40 | #include "wx/msw/private.h" | |
41 | ||
42 | #ifndef __TWIN32__ | |
43 | #ifdef __GNUWIN32_OLD__ | |
44 | #include "wx/msw/gnuwin32/extra.h" | |
45 | #endif | |
46 | #endif | |
47 | ||
48 | #ifdef __SALFORDC__ | |
49 | #include <shellapi.h> | |
50 | #endif | |
51 | ||
52 | LRESULT APIENTRY _EXPORT wxTaskBarIconWindowProc( HWND hWnd, unsigned msg, | |
53 | UINT wParam, LONG lParam ); | |
54 | ||
55 | wxChar *wxTaskBarWindowClass = wxT("wxTaskBarWindowClass"); | |
56 | ||
57 | wxList wxTaskBarIcon::sm_taskBarIcons; | |
58 | bool wxTaskBarIcon::sm_registeredClass = FALSE; | |
59 | UINT wxTaskBarIcon::sm_taskbarMsg = 0; | |
60 | ||
61 | ||
62 | BEGIN_EVENT_TABLE(wxTaskBarIcon, wxEvtHandler) | |
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) | |
70 | END_EVENT_TABLE() | |
71 | ||
72 | ||
73 | IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler) | |
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(¬ifyData, 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 != wxT(""))) | |
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(¬ifyData, 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 | // 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 | ||
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 | ||
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 | ||
183 | menu->UpdateUI(); | |
184 | ||
185 | rval = win->PopupMenu(menu, 0, 0); | |
186 | ||
187 | win->PopEventHandler(FALSE); | |
188 | win->Destroy(); | |
189 | delete win; | |
190 | ||
191 | s_inPopup = FALSE; | |
192 | ||
193 | return rval; | |
194 | } | |
195 | ||
196 | ||
197 | // Overridables | |
198 | void wxTaskBarIcon::OnMouseMove(wxEvent&) | |
199 | { | |
200 | } | |
201 | ||
202 | void wxTaskBarIcon::OnLButtonDown(wxEvent&) | |
203 | { | |
204 | } | |
205 | ||
206 | void wxTaskBarIcon::OnLButtonUp(wxEvent&) | |
207 | { | |
208 | } | |
209 | ||
210 | void wxTaskBarIcon::OnRButtonDown(wxEvent&) | |
211 | { | |
212 | } | |
213 | ||
214 | void wxTaskBarIcon::OnRButtonUp(wxEvent&) | |
215 | { | |
216 | } | |
217 | ||
218 | void wxTaskBarIcon::OnLButtonDClick(wxEvent&) | |
219 | { | |
220 | } | |
221 | ||
222 | void wxTaskBarIcon::OnRButtonDClick(wxEvent&) | |
223 | { | |
224 | } | |
225 | ||
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 | ||
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 | |
264 | sm_taskbarMsg = ::RegisterWindowMessage(wxT("wxTaskBarIconMessage")); | |
265 | ||
266 | WNDCLASS wc; | |
267 | bool rc; | |
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, | |
296 | wxT("wxTaskBarWindow"), | |
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 | { | |
312 | wxEventType eventType = 0; | |
313 | ||
314 | if (msg != sm_taskbarMsg) | |
315 | return DefWindowProc((HWND) hWnd, msg, wParam, lParam); | |
316 | ||
317 | switch (lParam) | |
318 | { | |
319 | case WM_LBUTTONDOWN: | |
320 | eventType = wxEVT_TASKBAR_LEFT_DOWN; | |
321 | break; | |
322 | ||
323 | case WM_LBUTTONUP: | |
324 | eventType = wxEVT_TASKBAR_LEFT_UP; | |
325 | break; | |
326 | ||
327 | case WM_RBUTTONDOWN: | |
328 | eventType = wxEVT_TASKBAR_RIGHT_DOWN; | |
329 | break; | |
330 | ||
331 | case WM_RBUTTONUP: | |
332 | eventType = wxEVT_TASKBAR_RIGHT_UP; | |
333 | break; | |
334 | ||
335 | case WM_LBUTTONDBLCLK: | |
336 | eventType = wxEVT_TASKBAR_LEFT_DCLICK; | |
337 | break; | |
338 | ||
339 | case WM_RBUTTONDBLCLK: | |
340 | eventType = wxEVT_TASKBAR_RIGHT_DCLICK; | |
341 | break; | |
342 | ||
343 | case WM_MOUSEMOVE: | |
344 | eventType = wxEVT_TASKBAR_MOVE; | |
345 | break; | |
346 | ||
347 | default: | |
348 | break; | |
349 | } | |
350 | ||
351 | if (eventType) { | |
352 | wxEvent event; | |
353 | event.SetEventType(eventType); | |
354 | event.SetEventObject(this); | |
355 | ||
356 | ProcessEvent(event); | |
357 | } | |
358 | return 0; | |
359 | } | |
360 | ||
361 | LRESULT APIENTRY _EXPORT wxTaskBarIconWindowProc( HWND hWnd, unsigned msg, | |
362 | UINT wParam, LONG lParam ) | |
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__ |