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