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