]>
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/defs.h" | |
22 | #include "wx/window.h" | |
23 | #include "wx/frame.h" | |
24 | #include "wx/utils.h" | |
25 | #include "wx/menu.h" | |
26 | #endif | |
27 | ||
28 | #include "wx/msw/private.h" | |
29 | #include "wx/msw/winundef.h" | |
30 | ||
31 | #include <string.h> | |
32 | #include "wx/taskbar.h" | |
33 | ||
34 | #ifdef __WXWINCE__ | |
35 | #include <winreg.h> | |
36 | #include <shellapi.h> | |
37 | #endif | |
38 | ||
39 | // initialized on demand | |
40 | UINT gs_msgTaskbar = 0; | |
41 | UINT gs_msgRestartTaskbar = 0; | |
42 | ||
43 | #if WXWIN_COMPATIBILITY_2_4 | |
44 | BEGIN_EVENT_TABLE(wxTaskBarIcon, wxTaskBarIconBase) | |
45 | EVT_TASKBAR_MOVE (wxTaskBarIcon::_OnMouseMove) | |
46 | EVT_TASKBAR_LEFT_DOWN (wxTaskBarIcon::_OnLButtonDown) | |
47 | EVT_TASKBAR_LEFT_UP (wxTaskBarIcon::_OnLButtonUp) | |
48 | EVT_TASKBAR_RIGHT_DOWN (wxTaskBarIcon::_OnRButtonDown) | |
49 | EVT_TASKBAR_RIGHT_UP (wxTaskBarIcon::_OnRButtonUp) | |
50 | EVT_TASKBAR_LEFT_DCLICK (wxTaskBarIcon::_OnLButtonDClick) | |
51 | EVT_TASKBAR_RIGHT_DCLICK (wxTaskBarIcon::_OnRButtonDClick) | |
52 | END_EVENT_TABLE() | |
53 | #endif | |
54 | ||
55 | ||
56 | IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler) | |
57 | ||
58 | // ============================================================================ | |
59 | // implementation | |
60 | // ============================================================================ | |
61 | ||
62 | // ---------------------------------------------------------------------------- | |
63 | // wxTaskBarIconWindow: helper window | |
64 | // ---------------------------------------------------------------------------- | |
65 | ||
66 | // NB: this class serves two purposes: | |
67 | // 1. win32 needs a HWND associated with taskbar icon, this provides it | |
68 | // 2. we need wxTopLevelWindow so that the app doesn't exit when | |
69 | // last frame is closed but there still is a taskbar icon | |
70 | class wxTaskBarIconWindow : public wxFrame | |
71 | { | |
72 | public: | |
73 | wxTaskBarIconWindow(wxTaskBarIcon *icon) | |
74 | : wxFrame(NULL, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0), | |
75 | m_icon(icon) | |
76 | { | |
77 | } | |
78 | ||
79 | WXLRESULT MSWWindowProc(WXUINT msg, | |
80 | WXWPARAM wParam, WXLPARAM lParam) | |
81 | { | |
82 | if (msg == gs_msgRestartTaskbar || msg == gs_msgTaskbar) | |
83 | { | |
84 | return m_icon->WindowProc(msg, wParam, lParam); | |
85 | } | |
86 | else | |
87 | { | |
88 | return wxFrame::MSWWindowProc(msg, wParam, lParam); | |
89 | } | |
90 | } | |
91 | ||
92 | private: | |
93 | wxTaskBarIcon *m_icon; | |
94 | }; | |
95 | ||
96 | ||
97 | // ---------------------------------------------------------------------------- | |
98 | // NotifyIconData: wrapper around NOTIFYICONDATA | |
99 | // ---------------------------------------------------------------------------- | |
100 | ||
101 | struct NotifyIconData : public NOTIFYICONDATA | |
102 | { | |
103 | NotifyIconData(WXHWND hwnd) | |
104 | { | |
105 | memset(this, 0, sizeof(NOTIFYICONDATA)); | |
106 | cbSize = sizeof(NOTIFYICONDATA); | |
107 | hWnd = (HWND) hwnd; | |
108 | uCallbackMessage = gs_msgTaskbar; | |
109 | uFlags = NIF_MESSAGE; | |
110 | ||
111 | // we use the same id for all taskbar icons as we don't need it to | |
112 | // distinguish between them | |
113 | uID = 99; | |
114 | } | |
115 | }; | |
116 | ||
117 | // ---------------------------------------------------------------------------- | |
118 | // wxTaskBarIcon | |
119 | // ---------------------------------------------------------------------------- | |
120 | ||
121 | wxTaskBarIcon::wxTaskBarIcon() | |
122 | { | |
123 | m_win = NULL; | |
124 | m_iconAdded = false; | |
125 | RegisterWindowMessages(); | |
126 | } | |
127 | ||
128 | wxTaskBarIcon::~wxTaskBarIcon() | |
129 | { | |
130 | if (m_iconAdded) | |
131 | RemoveIcon(); | |
132 | ||
133 | if (m_win) | |
134 | m_win->Destroy(); | |
135 | } | |
136 | ||
137 | // Operations | |
138 | bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip) | |
139 | { | |
140 | // NB: we have to create the window lazily because of backward compatibility, | |
141 | // old applications may create a wxTaskBarIcon instance before wxApp | |
142 | // is initialized (as samples/taskbar used to do) | |
143 | if (!m_win) | |
144 | { | |
145 | m_win = new wxTaskBarIconWindow(this); | |
146 | } | |
147 | ||
148 | m_icon = icon; | |
149 | m_strTooltip = tooltip; | |
150 | ||
151 | NotifyIconData notifyData((HWND)m_win->GetHWND()); | |
152 | ||
153 | if (icon.Ok()) | |
154 | { | |
155 | notifyData.uFlags |= NIF_ICON; | |
156 | notifyData.hIcon = GetHiconOf(icon); | |
157 | } | |
158 | ||
159 | if ( !tooltip.empty() ) | |
160 | { | |
161 | notifyData.uFlags |= NIF_TIP; | |
162 | // lstrcpyn(notifyData.szTip, tooltip.c_str(), WXSIZEOF(notifyData.szTip)); | |
163 | wxStrncpy(notifyData.szTip, tooltip.c_str(), WXSIZEOF(notifyData.szTip)); | |
164 | } | |
165 | ||
166 | bool ok = Shell_NotifyIcon(m_iconAdded ? NIM_MODIFY | |
167 | : NIM_ADD, ¬ifyData) != 0; | |
168 | ||
169 | if ( !m_iconAdded && ok ) | |
170 | m_iconAdded = true; | |
171 | ||
172 | return ok; | |
173 | } | |
174 | ||
175 | bool wxTaskBarIcon::RemoveIcon() | |
176 | { | |
177 | if (!m_iconAdded) | |
178 | return false; | |
179 | ||
180 | m_iconAdded = false; | |
181 | ||
182 | NotifyIconData notifyData((HWND)m_win->GetHWND()); | |
183 | ||
184 | return Shell_NotifyIcon(NIM_DELETE, ¬ifyData) != 0; | |
185 | } | |
186 | ||
187 | bool wxTaskBarIcon::PopupMenu(wxMenu *menu) | |
188 | { | |
189 | wxASSERT_MSG( m_win != NULL, _T("taskbar icon not initialized") ); | |
190 | ||
191 | static bool s_inPopup = false; | |
192 | ||
193 | if (s_inPopup) | |
194 | return false; | |
195 | ||
196 | s_inPopup = true; | |
197 | ||
198 | int x, y; | |
199 | wxGetMousePosition(&x, &y); | |
200 | ||
201 | m_win->Move(x, y); | |
202 | ||
203 | m_win->PushEventHandler(this); | |
204 | ||
205 | menu->UpdateUI(); | |
206 | ||
207 | // Work around a WIN32 bug | |
208 | ::SetForegroundWindow((HWND)m_win->GetHWND()); | |
209 | ||
210 | bool rval = m_win->PopupMenu(menu, 0, 0); | |
211 | ||
212 | // Work around a WIN32 bug | |
213 | ::PostMessage((HWND)m_win->GetHWND(), 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 | } |