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