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