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