]> git.saurik.com Git - wxWidgets.git/blob - src/msw/taskbar.cpp
Fix for probably rare but potential refcount leak.
[wxWidgets.git] / src / msw / taskbar.cpp
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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"
26 #include "wx/window.h"
27 #include "wx/frame.h"
28 #include "wx/utils.h"
29 #include "wx/menu.h"
30 #endif
31
32 #if defined(__WIN95__)
33
34 #include "wx/msw/private.h"
35 #include "wx/msw/winundef.h"
36
37 #include <string.h>
38 #include "wx/taskbar.h"
39
40 #ifdef __GNUWIN32_OLD__
41 #include "wx/msw/gnuwin32/extra.h"
42 #endif
43
44 #ifdef __WXWINCE__
45 #include <winreg.h>
46 #include <shellapi.h>
47 #endif
48
49 // initialized on demand
50 UINT gs_msgTaskbar = 0;
51 UINT gs_msgRestartTaskbar = 0;
52
53 #if WXWIN_COMPATIBILITY_2_4
54 BEGIN_EVENT_TABLE(wxTaskBarIcon, wxTaskBarIconBase)
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)
62 END_EVENT_TABLE()
63 #endif
64
65
66 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
67
68 // ============================================================================
69 // implementation
70 // ============================================================================
71
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)
84 : wxFrame(NULL, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0),
85 m_icon(icon)
86 {
87 }
88
89 WXLRESULT MSWWindowProc(WXUINT msg,
90 WXWPARAM wParam, WXLPARAM lParam)
91 {
92 if (msg == gs_msgRestartTaskbar || msg == gs_msgTaskbar)
93 {
94 return m_icon->WindowProc(msg, wParam, lParam);
95 }
96 else
97 {
98 return wxFrame::MSWWindowProc(msg, wParam, lParam);
99 }
100 }
101
102 private:
103 wxTaskBarIcon *m_icon;
104 };
105
106
107 // ----------------------------------------------------------------------------
108 // NotifyIconData: wrapper around NOTIFYICONDATA
109 // ----------------------------------------------------------------------------
110
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()
132 {
133 m_win = NULL;
134 m_iconAdded = false;
135 RegisterWindowMessages();
136 }
137
138 wxTaskBarIcon::~wxTaskBarIcon()
139 {
140 if (m_iconAdded)
141 RemoveIcon();
142
143 if (m_win)
144 m_win->Destroy();
145 }
146
147 // Operations
148 bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
149 {
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 }
157
158 m_icon = icon;
159 m_strTooltip = tooltip;
160
161 NotifyIconData notifyData((HWND)m_win->GetHWND());
162
163 if (icon.Ok())
164 {
165 notifyData.uFlags |= NIF_ICON;
166 notifyData.hIcon = GetHiconOf(icon);
167 }
168
169 if ( !tooltip.empty() )
170 {
171 notifyData.uFlags |= NIF_TIP;
172 // lstrcpyn(notifyData.szTip, tooltip.c_str(), WXSIZEOF(notifyData.szTip));
173 wxStrncpy(notifyData.szTip, tooltip.c_str(), WXSIZEOF(notifyData.szTip));
174 }
175
176 bool ok = Shell_NotifyIcon(m_iconAdded ? NIM_MODIFY
177 : NIM_ADD, &notifyData) != 0;
178
179 if ( !m_iconAdded && ok )
180 m_iconAdded = true;
181
182 return ok;
183 }
184
185 bool wxTaskBarIcon::RemoveIcon()
186 {
187 if (!m_iconAdded)
188 return false;
189
190 m_iconAdded = false;
191
192 NotifyIconData notifyData((HWND)m_win->GetHWND());
193
194 return Shell_NotifyIcon(NIM_DELETE, &notifyData) != 0;
195 }
196
197 bool wxTaskBarIcon::PopupMenu(wxMenu *menu)
198 {
199 wxASSERT_MSG( m_win != NULL, _T("taskbar icon not initialized") );
200
201 static bool s_inPopup = false;
202
203 if (s_inPopup)
204 return false;
205
206 s_inPopup = true;
207
208 int x, y;
209 wxGetMousePosition(&x, &y);
210
211 m_win->Move(x, y);
212
213 m_win->PushEventHandler(this);
214
215 menu->UpdateUI();
216
217 // Work around a WIN32 bug
218 ::SetForegroundWindow((HWND)m_win->GetHWND());
219
220 bool rval = m_win->PopupMenu(menu, 0, 0);
221
222 // Work around a WIN32 bug
223 ::PostMessage((HWND)m_win->GetHWND(), WM_NULL, 0, 0L);
224
225 m_win->PopEventHandler(false);
226
227 s_inPopup = false;
228
229 return rval;
230 }
231
232 #if WXWIN_COMPATIBILITY_2_4
233 // Overridables
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(); }
241
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); }
256 #endif
257
258 void wxTaskBarIcon::RegisterWindowMessages()
259 {
260 static bool s_registered = false;
261
262 if ( !s_registered )
263 {
264 // Taskbar restart msg will be sent to us if the icon needs to be redrawn
265 gs_msgRestartTaskbar = RegisterWindowMessage(wxT("TaskbarCreated"));
266
267 // Also register the taskbar message here
268 gs_msgTaskbar = ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
269
270 s_registered = true;
271 }
272 }
273
274 // ----------------------------------------------------------------------------
275 // wxTaskBarIcon window proc
276 // ----------------------------------------------------------------------------
277
278 long wxTaskBarIcon::WindowProc(unsigned int msg,
279 unsigned int WXUNUSED(wParam),
280 long lParam)
281 {
282 wxEventType eventType = 0;
283
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
290 // this function should only be called for gs_msg(Restart)Taskbar messages
291 wxASSERT(msg == gs_msgTaskbar);
292
293 switch (lParam)
294 {
295 case WM_LBUTTONDOWN:
296 eventType = wxEVT_TASKBAR_LEFT_DOWN;
297 break;
298
299 case WM_LBUTTONUP:
300 eventType = wxEVT_TASKBAR_LEFT_UP;
301 break;
302
303 case WM_RBUTTONDOWN:
304 eventType = wxEVT_TASKBAR_RIGHT_DOWN;
305 break;
306
307 case WM_RBUTTONUP:
308 eventType = wxEVT_TASKBAR_RIGHT_UP;
309 break;
310
311 case WM_LBUTTONDBLCLK:
312 eventType = wxEVT_TASKBAR_LEFT_DCLICK;
313 break;
314
315 case WM_RBUTTONDBLCLK:
316 eventType = wxEVT_TASKBAR_RIGHT_DCLICK;
317 break;
318
319 case WM_MOUSEMOVE:
320 eventType = wxEVT_TASKBAR_MOVE;
321 break;
322
323 default:
324 break;
325 }
326
327 if (eventType)
328 {
329 wxTaskBarIconEvent event(eventType, this);
330
331 ProcessEvent(event);
332 }
333
334 return 0;
335 }
336
337 #endif // __WIN95__