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