]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////// | |
2 | // File: src/msw/taskbar.cpp | |
3 | // Purpose: Implements wxTaskBarIcon class for manipulating icons on | |
4 | // the Windows task bar. | |
5 | // Author: Julian Smart | |
6 | // Modified by: Vaclav Slavik | |
7 | // Created: 24/3/98 | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) | |
10 | // Licence: wxWindows licence | |
11 | ///////////////////////////////////////////////////////////////////////// | |
12 | ||
13 | // For compilers that support precompilation, includes "wx.h". | |
14 | #include "wx/wxprec.h" | |
15 | ||
16 | #ifdef __BORLANDC__ | |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
20 | #ifndef WX_PRECOMP | |
21 | #include "wx/window.h" | |
22 | #include "wx/frame.h" | |
23 | #include "wx/utils.h" | |
24 | #include "wx/menu.h" | |
25 | #endif | |
26 | ||
27 | #include "wx/msw/private.h" | |
28 | #include "wx/msw/winundef.h" | |
29 | ||
30 | #include <string.h> | |
31 | #include "wx/taskbar.h" | |
32 | ||
33 | #ifdef __WXWINCE__ | |
34 | #include <winreg.h> | |
35 | #include <shellapi.h> | |
36 | #endif | |
37 | ||
38 | // initialized on demand | |
39 | UINT gs_msgTaskbar = 0; | |
40 | UINT gs_msgRestartTaskbar = 0; | |
41 | ||
42 | #if WXWIN_COMPATIBILITY_2_4 | |
43 | BEGIN_EVENT_TABLE(wxTaskBarIcon, wxTaskBarIconBase) | |
44 | EVT_TASKBAR_MOVE (wxTaskBarIcon::_OnMouseMove) | |
45 | EVT_TASKBAR_LEFT_DOWN (wxTaskBarIcon::_OnLButtonDown) | |
46 | EVT_TASKBAR_LEFT_UP (wxTaskBarIcon::_OnLButtonUp) | |
47 | EVT_TASKBAR_RIGHT_DOWN (wxTaskBarIcon::_OnRButtonDown) | |
48 | EVT_TASKBAR_RIGHT_UP (wxTaskBarIcon::_OnRButtonUp) | |
49 | EVT_TASKBAR_LEFT_DCLICK (wxTaskBarIcon::_OnLButtonDClick) | |
50 | EVT_TASKBAR_RIGHT_DCLICK (wxTaskBarIcon::_OnRButtonDClick) | |
51 | END_EVENT_TABLE() | |
52 | #endif | |
53 | ||
54 | ||
55 | IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler) | |
56 | ||
57 | // ============================================================================ | |
58 | // implementation | |
59 | // ============================================================================ | |
60 | ||
61 | // ---------------------------------------------------------------------------- | |
62 | // wxTaskBarIconWindow: helper window | |
63 | // ---------------------------------------------------------------------------- | |
64 | ||
65 | // NB: this class serves two purposes: | |
66 | // 1. win32 needs a HWND associated with taskbar icon, this provides it | |
67 | // 2. we need wxTopLevelWindow so that the app doesn't exit when | |
68 | // last frame is closed but there still is a taskbar icon | |
69 | class wxTaskBarIconWindow : public wxFrame | |
70 | { | |
71 | public: | |
72 | wxTaskBarIconWindow(wxTaskBarIcon *icon) | |
73 | : wxFrame(NULL, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0), | |
74 | m_icon(icon) | |
75 | { | |
76 | } | |
77 | ||
78 | WXLRESULT MSWWindowProc(WXUINT msg, | |
79 | WXWPARAM wParam, WXLPARAM lParam) | |
80 | { | |
81 | if (msg == gs_msgRestartTaskbar || msg == gs_msgTaskbar) | |
82 | { | |
83 | return m_icon->WindowProc(msg, wParam, lParam); | |
84 | } | |
85 | else | |
86 | { | |
87 | return wxFrame::MSWWindowProc(msg, wParam, lParam); | |
88 | } | |
89 | } | |
90 | ||
91 | private: | |
92 | wxTaskBarIcon *m_icon; | |
93 | }; | |
94 | ||
95 | ||
96 | // ---------------------------------------------------------------------------- | |
97 | // NotifyIconData: wrapper around NOTIFYICONDATA | |
98 | // ---------------------------------------------------------------------------- | |
99 | ||
100 | struct NotifyIconData : public NOTIFYICONDATA | |
101 | { | |
102 | NotifyIconData(WXHWND hwnd) | |
103 | { | |
104 | memset(this, 0, sizeof(NOTIFYICONDATA)); | |
105 | cbSize = sizeof(NOTIFYICONDATA); | |
106 | hWnd = (HWND) hwnd; | |
107 | uCallbackMessage = gs_msgTaskbar; | |
108 | uFlags = NIF_MESSAGE; | |
109 | ||
110 | // we use the same id for all taskbar icons as we don't need it to | |
111 | // distinguish between them | |
112 | uID = 99; | |
113 | } | |
114 | }; | |
115 | ||
116 | // ---------------------------------------------------------------------------- | |
117 | // wxTaskBarIcon | |
118 | // ---------------------------------------------------------------------------- | |
119 | ||
120 | wxTaskBarIcon::wxTaskBarIcon() | |
121 | { | |
122 | m_win = NULL; | |
123 | m_iconAdded = false; | |
124 | RegisterWindowMessages(); | |
125 | } | |
126 | ||
127 | wxTaskBarIcon::~wxTaskBarIcon() | |
128 | { | |
129 | if (m_iconAdded) | |
130 | RemoveIcon(); | |
131 | ||
132 | if (m_win) | |
133 | m_win->Destroy(); | |
134 | } | |
135 | ||
136 | // Operations | |
137 | bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip) | |
138 | { | |
139 | // NB: we have to create the window lazily because of backward compatibility, | |
140 | // old applications may create a wxTaskBarIcon instance before wxApp | |
141 | // is initialized (as samples/taskbar used to do) | |
142 | if (!m_win) | |
143 | { | |
144 | m_win = new wxTaskBarIconWindow(this); | |
145 | } | |
146 | ||
147 | m_icon = icon; | |
148 | m_strTooltip = tooltip; | |
149 | ||
150 | NotifyIconData notifyData(GetHwndOf(m_win)); | |
151 | ||
152 | if (icon.Ok()) | |
153 | { | |
154 | notifyData.uFlags |= NIF_ICON; | |
155 | notifyData.hIcon = GetHiconOf(icon); | |
156 | } | |
157 | ||
158 | if ( !tooltip.empty() ) | |
159 | { | |
160 | notifyData.uFlags |= NIF_TIP; | |
161 | // lstrcpyn(notifyData.szTip, tooltip.c_str(), WXSIZEOF(notifyData.szTip)); | |
162 | wxStrncpy(notifyData.szTip, tooltip.c_str(), WXSIZEOF(notifyData.szTip)); | |
163 | } | |
164 | ||
165 | bool ok = Shell_NotifyIcon(m_iconAdded ? NIM_MODIFY | |
166 | : NIM_ADD, ¬ifyData) != 0; | |
167 | ||
168 | if ( !m_iconAdded && ok ) | |
169 | m_iconAdded = true; | |
170 | ||
171 | return ok; | |
172 | } | |
173 | ||
174 | bool wxTaskBarIcon::RemoveIcon() | |
175 | { | |
176 | if (!m_iconAdded) | |
177 | return false; | |
178 | ||
179 | m_iconAdded = false; | |
180 | ||
181 | NotifyIconData notifyData(GetHwndOf(m_win)); | |
182 | ||
183 | return Shell_NotifyIcon(NIM_DELETE, ¬ifyData) != 0; | |
184 | } | |
185 | ||
186 | bool wxTaskBarIcon::PopupMenu(wxMenu *menu) | |
187 | { | |
188 | wxASSERT_MSG( m_win != NULL, _T("taskbar icon not initialized") ); | |
189 | ||
190 | static bool s_inPopup = false; | |
191 | ||
192 | if (s_inPopup) | |
193 | return false; | |
194 | ||
195 | s_inPopup = true; | |
196 | ||
197 | int x, y; | |
198 | wxGetMousePosition(&x, &y); | |
199 | ||
200 | m_win->Move(x, y); | |
201 | ||
202 | m_win->PushEventHandler(this); | |
203 | ||
204 | menu->UpdateUI(); | |
205 | ||
206 | // the SetForegroundWindow() and PostMessage() calls are needed to work | |
207 | // around Win32 bug with the popup menus shown for the notifications as | |
208 | // documented at http://support.microsoft.com/kb/q135788/ | |
209 | ::SetForegroundWindow(GetHwndOf(m_win)); | |
210 | ||
211 | bool rval = m_win->PopupMenu(menu, 0, 0); | |
212 | ||
213 | ::PostMessage(GetHwndOf(m_win), WM_NULL, 0, 0L); | |
214 | ||
215 | m_win->PopEventHandler(false); | |
216 | ||
217 | s_inPopup = false; | |
218 | ||
219 | return rval; | |
220 | } | |
221 | ||
222 | #if WXWIN_COMPATIBILITY_2_4 | |
223 | // Overridables | |
224 | void wxTaskBarIcon::OnMouseMove(wxEvent& e) { e.Skip(); } | |
225 | void wxTaskBarIcon::OnLButtonDown(wxEvent& e) { e.Skip(); } | |
226 | void wxTaskBarIcon::OnLButtonUp(wxEvent& e) { e.Skip(); } | |
227 | void wxTaskBarIcon::OnRButtonDown(wxEvent& e) { e.Skip(); } | |
228 | void wxTaskBarIcon::OnRButtonUp(wxEvent& e) { e.Skip(); } | |
229 | void wxTaskBarIcon::OnLButtonDClick(wxEvent& e) { e.Skip(); } | |
230 | void wxTaskBarIcon::OnRButtonDClick(wxEvent& e) { e.Skip(); } | |
231 | ||
232 | void wxTaskBarIcon::_OnMouseMove(wxTaskBarIconEvent& e) | |
233 | { OnMouseMove(e); } | |
234 | void wxTaskBarIcon::_OnLButtonDown(wxTaskBarIconEvent& e) | |
235 | { OnLButtonDown(e); } | |
236 | void wxTaskBarIcon::_OnLButtonUp(wxTaskBarIconEvent& e) | |
237 | { OnLButtonUp(e); } | |
238 | void wxTaskBarIcon::_OnRButtonDown(wxTaskBarIconEvent& e) | |
239 | { OnRButtonDown(e); } | |
240 | void wxTaskBarIcon::_OnRButtonUp(wxTaskBarIconEvent& e) | |
241 | { OnRButtonUp(e); } | |
242 | void wxTaskBarIcon::_OnLButtonDClick(wxTaskBarIconEvent& e) | |
243 | { OnLButtonDClick(e); } | |
244 | void wxTaskBarIcon::_OnRButtonDClick(wxTaskBarIconEvent& e) | |
245 | { OnRButtonDClick(e); } | |
246 | #endif | |
247 | ||
248 | void wxTaskBarIcon::RegisterWindowMessages() | |
249 | { | |
250 | static bool s_registered = false; | |
251 | ||
252 | if ( !s_registered ) | |
253 | { | |
254 | // Taskbar restart msg will be sent to us if the icon needs to be redrawn | |
255 | gs_msgRestartTaskbar = RegisterWindowMessage(wxT("TaskbarCreated")); | |
256 | ||
257 | // Also register the taskbar message here | |
258 | gs_msgTaskbar = ::RegisterWindowMessage(wxT("wxTaskBarIconMessage")); | |
259 | ||
260 | s_registered = true; | |
261 | } | |
262 | } | |
263 | ||
264 | // ---------------------------------------------------------------------------- | |
265 | // wxTaskBarIcon window proc | |
266 | // ---------------------------------------------------------------------------- | |
267 | ||
268 | long wxTaskBarIcon::WindowProc(unsigned int msg, | |
269 | unsigned int WXUNUSED(wParam), | |
270 | long lParam) | |
271 | { | |
272 | wxEventType eventType = 0; | |
273 | ||
274 | if (msg == gs_msgRestartTaskbar) // does the icon need to be redrawn? | |
275 | { | |
276 | m_iconAdded = false; | |
277 | SetIcon(m_icon, m_strTooltip); | |
278 | } | |
279 | ||
280 | // this function should only be called for gs_msg(Restart)Taskbar messages | |
281 | wxASSERT(msg == gs_msgTaskbar); | |
282 | ||
283 | switch (lParam) | |
284 | { | |
285 | case WM_LBUTTONDOWN: | |
286 | eventType = wxEVT_TASKBAR_LEFT_DOWN; | |
287 | break; | |
288 | ||
289 | case WM_LBUTTONUP: | |
290 | eventType = wxEVT_TASKBAR_LEFT_UP; | |
291 | break; | |
292 | ||
293 | case WM_RBUTTONDOWN: | |
294 | eventType = wxEVT_TASKBAR_RIGHT_DOWN; | |
295 | break; | |
296 | ||
297 | case WM_RBUTTONUP: | |
298 | eventType = wxEVT_TASKBAR_RIGHT_UP; | |
299 | break; | |
300 | ||
301 | case WM_LBUTTONDBLCLK: | |
302 | eventType = wxEVT_TASKBAR_LEFT_DCLICK; | |
303 | break; | |
304 | ||
305 | case WM_RBUTTONDBLCLK: | |
306 | eventType = wxEVT_TASKBAR_RIGHT_DCLICK; | |
307 | break; | |
308 | ||
309 | case WM_MOUSEMOVE: | |
310 | eventType = wxEVT_TASKBAR_MOVE; | |
311 | break; | |
312 | ||
313 | default: | |
314 | break; | |
315 | } | |
316 | ||
317 | if (eventType) | |
318 | { | |
319 | wxTaskBarIconEvent event(eventType, this); | |
320 | ||
321 | ProcessEvent(event); | |
322 | } | |
323 | ||
324 | return 0; | |
325 | } |