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