]> git.saurik.com Git - wxWidgets.git/blob - src/msw/taskbar.cpp
fixed recent modeless dialog breakage caused by removing wxDIALOG_MODAL (wxModelessWi...
[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, wxEvtHandler)
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, -1, 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&) {}
235 void wxTaskBarIcon::OnLButtonDown(wxEvent&) {}
236 void wxTaskBarIcon::OnLButtonUp(wxEvent&) {}
237 void wxTaskBarIcon::OnRButtonDown(wxEvent&) {}
238 void wxTaskBarIcon::OnRButtonUp(wxEvent&) {}
239 void wxTaskBarIcon::OnLButtonDClick(wxEvent&) {}
240 void wxTaskBarIcon::OnRButtonDClick(wxEvent&) {}
241
242 void wxTaskBarIcon::_OnMouseMove(wxEvent& e) { OnMouseMove(e); }
243 void wxTaskBarIcon::_OnLButtonDown(wxEvent& e) { OnLButtonDown(e); }
244 void wxTaskBarIcon::_OnLButtonUp(wxEvent& e) { OnLButtonUp(e); }
245 void wxTaskBarIcon::_OnRButtonDown(wxEvent& e) { OnRButtonDown(e); }
246 void wxTaskBarIcon::_OnRButtonUp(wxEvent& e) { OnRButtonUp(e); }
247 void wxTaskBarIcon::_OnLButtonDClick(wxEvent& e) { OnLButtonDClick(e); }
248 void wxTaskBarIcon::_OnRButtonDClick(wxEvent& e) { OnRButtonDClick(e); }
249 #endif
250
251 void wxTaskBarIcon::RegisterWindowMessages()
252 {
253 static bool s_registered = false;
254
255 if ( !s_registered )
256 {
257 // Taskbar restart msg will be sent to us if the icon needs to be redrawn
258 gs_msgRestartTaskbar = RegisterWindowMessage(wxT("TaskbarCreated"));
259
260 // Also register the taskbar message here
261 gs_msgTaskbar = ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
262
263 s_registered = true;
264 }
265 }
266
267 // ----------------------------------------------------------------------------
268 // wxTaskBarIcon window proc
269 // ----------------------------------------------------------------------------
270
271 long wxTaskBarIcon::WindowProc(unsigned int msg,
272 unsigned int WXUNUSED(wParam),
273 long lParam)
274 {
275 wxEventType eventType = 0;
276
277 if (msg == gs_msgRestartTaskbar) // does the icon need to be redrawn?
278 {
279 m_iconAdded = false;
280 SetIcon(m_icon, m_strTooltip);
281 }
282
283 // this function should only be called for gs_msg(Restart)Taskbar messages
284 wxASSERT(msg == gs_msgTaskbar);
285
286 switch (lParam)
287 {
288 case WM_LBUTTONDOWN:
289 eventType = wxEVT_TASKBAR_LEFT_DOWN;
290 break;
291
292 case WM_LBUTTONUP:
293 eventType = wxEVT_TASKBAR_LEFT_UP;
294 break;
295
296 case WM_RBUTTONDOWN:
297 eventType = wxEVT_TASKBAR_RIGHT_DOWN;
298 break;
299
300 case WM_RBUTTONUP:
301 eventType = wxEVT_TASKBAR_RIGHT_UP;
302 break;
303
304 case WM_LBUTTONDBLCLK:
305 eventType = wxEVT_TASKBAR_LEFT_DCLICK;
306 break;
307
308 case WM_RBUTTONDBLCLK:
309 eventType = wxEVT_TASKBAR_RIGHT_DCLICK;
310 break;
311
312 case WM_MOUSEMOVE:
313 eventType = wxEVT_TASKBAR_MOVE;
314 break;
315
316 default:
317 break;
318 }
319
320 if (eventType)
321 {
322 wxTaskBarIconEvent event(eventType, this);
323
324 ProcessEvent(event);
325 }
326
327 return 0;
328 }
329
330 #endif // __WIN95__