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