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