]> git.saurik.com Git - wxWidgets.git/blame - src/msw/taskbar.cpp
no need to %import gdi.i
[wxWidgets.git] / src / msw / taskbar.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////
a71d815b 2// File: src/msw/taskbar.cpp
c42404a5 3// Purpose: Implements wxTaskBarIcon class for manipulating icons on
2bda0e17
KB
4// the Windows task bar.
5// Author: Julian Smart
1e6d9c20 6// Modified by: Vaclav Slavik
2bda0e17
KB
7// Created: 24/3/98
8// RCS-ID: $Id$
9// Copyright: (c)
65571936 10// Licence: wxWindows licence
2bda0e17
KB
11/////////////////////////////////////////////////////////////////////////
12
2bda0e17
KB
13// For compilers that support precompilation, includes "wx.h".
14#include "wx/wxprec.h"
15
16#ifdef __BORLANDC__
7520f3da 17 #pragma hdrstop
2bda0e17
KB
18#endif
19
20#ifndef WX_PRECOMP
7520f3da
WS
21 #include "wx/window.h"
22 #include "wx/frame.h"
23 #include "wx/utils.h"
24 #include "wx/menu.h"
2bda0e17
KB
25#endif
26
4676948b 27#include "wx/msw/private.h"
c25a510b
JS
28#include "wx/msw/winundef.h"
29
2bda0e17 30#include <string.h>
6af507f7 31#include "wx/taskbar.h"
2bda0e17 32
4676948b
JS
33#ifdef __WXWINCE__
34 #include <winreg.h>
c42404a5 35 #include <shellapi.h>
ce3ed50d
JS
36#endif
37
4d0d77af
VZ
38// initialized on demand
39UINT gs_msgTaskbar = 0;
40UINT gs_msgRestartTaskbar = 0;
2bda0e17 41
6af507f7 42#if WXWIN_COMPATIBILITY_2_4
41819cbe 43BEGIN_EVENT_TABLE(wxTaskBarIcon, wxTaskBarIconBase)
69ecd30f
RD
44 EVT_TASKBAR_MOVE (wxTaskBarIcon::_OnMouseMove)
45 EVT_TASKBAR_LEFT_DOWN (wxTaskBarIcon::_OnLButtonDown)
46 EVT_TASKBAR_LEFT_UP (wxTaskBarIcon::_OnLButtonUp)
47 EVT_TASKBAR_RIGHT_DOWN (wxTaskBarIcon::_OnRButtonDown)
48 EVT_TASKBAR_RIGHT_UP (wxTaskBarIcon::_OnRButtonUp)
49 EVT_TASKBAR_LEFT_DCLICK (wxTaskBarIcon::_OnLButtonDClick)
50 EVT_TASKBAR_RIGHT_DCLICK (wxTaskBarIcon::_OnRButtonDClick)
56194595 51END_EVENT_TABLE()
6af507f7 52#endif
56194595
RD
53
54
55IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
56194595 56
4d0d77af
VZ
57// ============================================================================
58// implementation
59// ============================================================================
60
1e6d9c20
VS
61// ----------------------------------------------------------------------------
62// wxTaskBarIconWindow: helper window
63// ----------------------------------------------------------------------------
64
65// NB: this class serves two purposes:
66// 1. win32 needs a HWND associated with taskbar icon, this provides it
67// 2. we need wxTopLevelWindow so that the app doesn't exit when
68// last frame is closed but there still is a taskbar icon
69class wxTaskBarIconWindow : public wxFrame
70{
71public:
72 wxTaskBarIconWindow(wxTaskBarIcon *icon)
bfbb0b4c 73 : wxFrame(NULL, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0),
1e6d9c20
VS
74 m_icon(icon)
75 {
76 }
bfbb0b4c 77
1e6d9c20
VS
78 WXLRESULT MSWWindowProc(WXUINT msg,
79 WXWPARAM wParam, WXLPARAM lParam)
80 {
81 if (msg == gs_msgRestartTaskbar || msg == gs_msgTaskbar)
82 {
dda36afd 83 return m_icon->WindowProc(msg, wParam, lParam);
1e6d9c20
VS
84 }
85 else
86 {
87 return wxFrame::MSWWindowProc(msg, wParam, lParam);
88 }
89 }
90
91private:
92 wxTaskBarIcon *m_icon;
93};
94
bfbb0b4c 95
4d0d77af
VZ
96// ----------------------------------------------------------------------------
97// NotifyIconData: wrapper around NOTIFYICONDATA
98// ----------------------------------------------------------------------------
56194595 99
4d0d77af
VZ
100struct NotifyIconData : public NOTIFYICONDATA
101{
102 NotifyIconData(WXHWND hwnd)
103 {
104 memset(this, 0, sizeof(NOTIFYICONDATA));
105 cbSize = sizeof(NOTIFYICONDATA);
106 hWnd = (HWND) hwnd;
107 uCallbackMessage = gs_msgTaskbar;
108 uFlags = NIF_MESSAGE;
109
110 // we use the same id for all taskbar icons as we don't need it to
111 // distinguish between them
112 uID = 99;
113 }
114};
115
116// ----------------------------------------------------------------------------
117// wxTaskBarIcon
118// ----------------------------------------------------------------------------
119
120wxTaskBarIcon::wxTaskBarIcon()
2bda0e17 121{
1e6d9c20 122 m_win = NULL;
04cd30de 123 m_iconAdded = false;
1e6d9c20 124 RegisterWindowMessages();
2bda0e17
KB
125}
126
4d0d77af 127wxTaskBarIcon::~wxTaskBarIcon()
2bda0e17 128{
2bda0e17 129 if (m_iconAdded)
2bda0e17 130 RemoveIcon();
2bda0e17 131
1e6d9c20
VS
132 if (m_win)
133 m_win->Destroy();
2bda0e17
KB
134}
135
136// Operations
137bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
138{
3103e8a9
JS
139 // NB: we have to create the window lazily because of backward compatibility,
140 // old applications may create a wxTaskBarIcon instance before wxApp
1e6d9c20
VS
141 // is initialized (as samples/taskbar used to do)
142 if (!m_win)
143 {
144 m_win = new wxTaskBarIconWindow(this);
145 }
2bda0e17 146
4d0d77af
VZ
147 m_icon = icon;
148 m_strTooltip = tooltip;
149
63b3dc58 150 NotifyIconData notifyData(GetHwndOf(m_win));
2bda0e17 151
e96360ef 152 if (icon.Ok())
2bda0e17 153 {
e96360ef 154 notifyData.uFlags |= NIF_ICON;
4d0d77af 155 notifyData.hIcon = GetHiconOf(icon);
2bda0e17
KB
156 }
157
4d0d77af 158 if ( !tooltip.empty() )
2bda0e17 159 {
4d0d77af 160 notifyData.uFlags |= NIF_TIP;
4676948b
JS
161// lstrcpyn(notifyData.szTip, tooltip.c_str(), WXSIZEOF(notifyData.szTip));
162 wxStrncpy(notifyData.szTip, tooltip.c_str(), WXSIZEOF(notifyData.szTip));
2bda0e17
KB
163 }
164
4d0d77af
VZ
165 bool ok = Shell_NotifyIcon(m_iconAdded ? NIM_MODIFY
166 : NIM_ADD, &notifyData) != 0;
2bda0e17 167
4d0d77af
VZ
168 if ( !m_iconAdded && ok )
169 m_iconAdded = true;
170
171 return ok;
2bda0e17
KB
172}
173
4d0d77af 174bool wxTaskBarIcon::RemoveIcon()
2bda0e17
KB
175{
176 if (!m_iconAdded)
04cd30de 177 return false;
2bda0e17 178
04cd30de 179 m_iconAdded = false;
2bda0e17 180
63b3dc58 181 NotifyIconData notifyData(GetHwndOf(m_win));
4d0d77af
VZ
182
183 return Shell_NotifyIcon(NIM_DELETE, &notifyData) != 0;
2bda0e17
KB
184}
185
4d0d77af 186bool wxTaskBarIcon::PopupMenu(wxMenu *menu)
69ecd30f 187{
1e6d9c20
VS
188 wxASSERT_MSG( m_win != NULL, _T("taskbar icon not initialized") );
189
04cd30de 190 static bool s_inPopup = false;
c7527e3f
JS
191
192 if (s_inPopup)
04cd30de 193 return false;
c7527e3f 194
04cd30de 195 s_inPopup = true;
c7527e3f 196
69ecd30f
RD
197 int x, y;
198 wxGetMousePosition(&x, &y);
199
1e6d9c20 200 m_win->Move(x, y);
bfbb0b4c 201
1e6d9c20 202 m_win->PushEventHandler(this);
d66d9d5b 203
50bcd1ae
JS
204 menu->UpdateUI();
205
63b3dc58
VZ
206 // the SetForegroundWindow() and PostMessage() calls are needed to work
207 // around Win32 bug with the popup menus shown for the notifications as
208 // documented at http://support.microsoft.com/kb/q135788/
209 ::SetForegroundWindow(GetHwndOf(m_win));
f6bcfd97 210
1e6d9c20 211 bool rval = m_win->PopupMenu(menu, 0, 0);
69ecd30f 212
63b3dc58 213 ::PostMessage(GetHwndOf(m_win), WM_NULL, 0, 0L);
f6bcfd97 214
1e6d9c20 215 m_win->PopEventHandler(false);
c7527e3f 216
04cd30de 217 s_inPopup = false;
d66d9d5b 218
69ecd30f
RD
219 return rval;
220}
221
6af507f7 222#if WXWIN_COMPATIBILITY_2_4
2bda0e17 223// Overridables
41819cbe
VS
224void wxTaskBarIcon::OnMouseMove(wxEvent& e) { e.Skip(); }
225void wxTaskBarIcon::OnLButtonDown(wxEvent& e) { e.Skip(); }
226void wxTaskBarIcon::OnLButtonUp(wxEvent& e) { e.Skip(); }
227void wxTaskBarIcon::OnRButtonDown(wxEvent& e) { e.Skip(); }
228void wxTaskBarIcon::OnRButtonUp(wxEvent& e) { e.Skip(); }
229void wxTaskBarIcon::OnLButtonDClick(wxEvent& e) { e.Skip(); }
230void wxTaskBarIcon::OnRButtonDClick(wxEvent& e) { e.Skip(); }
2bda0e17 231
6466d41e
VS
232void wxTaskBarIcon::_OnMouseMove(wxTaskBarIconEvent& e)
233 { OnMouseMove(e); }
234void wxTaskBarIcon::_OnLButtonDown(wxTaskBarIconEvent& e)
235 { OnLButtonDown(e); }
236void wxTaskBarIcon::_OnLButtonUp(wxTaskBarIconEvent& e)
237 { OnLButtonUp(e); }
238void wxTaskBarIcon::_OnRButtonDown(wxTaskBarIconEvent& e)
239 { OnRButtonDown(e); }
240void wxTaskBarIcon::_OnRButtonUp(wxTaskBarIconEvent& e)
241 { OnRButtonUp(e); }
242void wxTaskBarIcon::_OnLButtonDClick(wxTaskBarIconEvent& e)
243 { OnLButtonDClick(e); }
244void wxTaskBarIcon::_OnRButtonDClick(wxTaskBarIconEvent& e)
245 { OnRButtonDClick(e); }
6af507f7 246#endif
69ecd30f 247
1e6d9c20 248void wxTaskBarIcon::RegisterWindowMessages()
2bda0e17 249{
4d0d77af 250 static bool s_registered = false;
2bda0e17 251
1e6d9c20 252 if ( !s_registered )
bfbb0b4c 253 {
1e6d9c20
VS
254 // Taskbar restart msg will be sent to us if the icon needs to be redrawn
255 gs_msgRestartTaskbar = RegisterWindowMessage(wxT("TaskbarCreated"));
2bda0e17 256
1e6d9c20
VS
257 // Also register the taskbar message here
258 gs_msgTaskbar = ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
2bda0e17 259
1e6d9c20
VS
260 s_registered = true;
261 }
2bda0e17
KB
262}
263
4d0d77af
VZ
264// ----------------------------------------------------------------------------
265// wxTaskBarIcon window proc
266// ----------------------------------------------------------------------------
267
dda36afd
VS
268long wxTaskBarIcon::WindowProc(unsigned int msg,
269 unsigned int WXUNUSED(wParam),
4d0d77af 270 long lParam)
2bda0e17 271{
56194595
RD
272 wxEventType eventType = 0;
273
4d0d77af
VZ
274 if (msg == gs_msgRestartTaskbar) // does the icon need to be redrawn?
275 {
276 m_iconAdded = false;
277 SetIcon(m_icon, m_strTooltip);
278 }
279
1e6d9c20
VS
280 // this function should only be called for gs_msg(Restart)Taskbar messages
281 wxASSERT(msg == gs_msgTaskbar);
2bda0e17
KB
282
283 switch (lParam)
284 {
c42404a5 285 case WM_LBUTTONDOWN:
56194595
RD
286 eventType = wxEVT_TASKBAR_LEFT_DOWN;
287 break;
2bda0e17 288
c42404a5 289 case WM_LBUTTONUP:
56194595
RD
290 eventType = wxEVT_TASKBAR_LEFT_UP;
291 break;
2bda0e17 292
c42404a5 293 case WM_RBUTTONDOWN:
56194595
RD
294 eventType = wxEVT_TASKBAR_RIGHT_DOWN;
295 break;
2bda0e17 296
c42404a5 297 case WM_RBUTTONUP:
56194595
RD
298 eventType = wxEVT_TASKBAR_RIGHT_UP;
299 break;
2bda0e17 300
c42404a5 301 case WM_LBUTTONDBLCLK:
56194595
RD
302 eventType = wxEVT_TASKBAR_LEFT_DCLICK;
303 break;
2bda0e17 304
c42404a5 305 case WM_RBUTTONDBLCLK:
56194595
RD
306 eventType = wxEVT_TASKBAR_RIGHT_DCLICK;
307 break;
2bda0e17 308
c42404a5 309 case WM_MOUSEMOVE:
56194595
RD
310 eventType = wxEVT_TASKBAR_MOVE;
311 break;
2bda0e17 312
c42404a5 313 default:
56194595 314 break;
4d0d77af 315 }
56194595 316
4d0d77af
VZ
317 if (eventType)
318 {
fa1c12bd 319 wxTaskBarIconEvent event(eventType, this);
56194595
RD
320
321 ProcessEvent(event);
322 }
4d0d77af 323
2bda0e17
KB
324 return 0;
325}