]> git.saurik.com Git - wxWidgets.git/blame - src/msw/taskbar.cpp
don't assert when right/down cursor arrows are used in an empty tree control with...
[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
56194595
RD
42
43IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
56194595 44
4d0d77af
VZ
45// ============================================================================
46// implementation
47// ============================================================================
48
1e6d9c20
VS
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
57class wxTaskBarIconWindow : public wxFrame
58{
59public:
60 wxTaskBarIconWindow(wxTaskBarIcon *icon)
bfbb0b4c 61 : wxFrame(NULL, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0),
1e6d9c20
VS
62 m_icon(icon)
63 {
64 }
bfbb0b4c 65
1e6d9c20
VS
66 WXLRESULT MSWWindowProc(WXUINT msg,
67 WXWPARAM wParam, WXLPARAM lParam)
68 {
69 if (msg == gs_msgRestartTaskbar || msg == gs_msgTaskbar)
70 {
dda36afd 71 return m_icon->WindowProc(msg, wParam, lParam);
1e6d9c20
VS
72 }
73 else
74 {
75 return wxFrame::MSWWindowProc(msg, wParam, lParam);
76 }
77 }
78
79private:
80 wxTaskBarIcon *m_icon;
81};
82
bfbb0b4c 83
4d0d77af
VZ
84// ----------------------------------------------------------------------------
85// NotifyIconData: wrapper around NOTIFYICONDATA
86// ----------------------------------------------------------------------------
56194595 87
4d0d77af
VZ
88struct 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
108wxTaskBarIcon::wxTaskBarIcon()
2bda0e17 109{
1e6d9c20 110 m_win = NULL;
04cd30de 111 m_iconAdded = false;
1e6d9c20 112 RegisterWindowMessages();
2bda0e17
KB
113}
114
4d0d77af 115wxTaskBarIcon::~wxTaskBarIcon()
2bda0e17 116{
2bda0e17 117 if (m_iconAdded)
2bda0e17 118 RemoveIcon();
2bda0e17 119
1e6d9c20
VS
120 if (m_win)
121 m_win->Destroy();
2bda0e17
KB
122}
123
124// Operations
125bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
126{
3103e8a9
JS
127 // NB: we have to create the window lazily because of backward compatibility,
128 // old applications may create a wxTaskBarIcon instance before wxApp
1e6d9c20
VS
129 // is initialized (as samples/taskbar used to do)
130 if (!m_win)
131 {
132 m_win = new wxTaskBarIconWindow(this);
133 }
2bda0e17 134
4d0d77af
VZ
135 m_icon = icon;
136 m_strTooltip = tooltip;
137
63b3dc58 138 NotifyIconData notifyData(GetHwndOf(m_win));
2bda0e17 139
e96360ef 140 if (icon.Ok())
2bda0e17 141 {
e96360ef 142 notifyData.uFlags |= NIF_ICON;
4d0d77af 143 notifyData.hIcon = GetHiconOf(icon);
2bda0e17
KB
144 }
145
b2057360
VZ
146 // set NIF_TIP even for an empty tooltip: otherwise it would be impossible
147 // to remove an existing tooltip using this function
148 notifyData.uFlags |= NIF_TIP;
4d0d77af 149 if ( !tooltip.empty() )
2bda0e17 150 {
4676948b 151 wxStrncpy(notifyData.szTip, tooltip.c_str(), WXSIZEOF(notifyData.szTip));
2bda0e17
KB
152 }
153
4d0d77af
VZ
154 bool ok = Shell_NotifyIcon(m_iconAdded ? NIM_MODIFY
155 : NIM_ADD, &notifyData) != 0;
2bda0e17 156
4d0d77af
VZ
157 if ( !m_iconAdded && ok )
158 m_iconAdded = true;
159
160 return ok;
2bda0e17
KB
161}
162
4d0d77af 163bool wxTaskBarIcon::RemoveIcon()
2bda0e17
KB
164{
165 if (!m_iconAdded)
04cd30de 166 return false;
2bda0e17 167
04cd30de 168 m_iconAdded = false;
2bda0e17 169
63b3dc58 170 NotifyIconData notifyData(GetHwndOf(m_win));
4d0d77af
VZ
171
172 return Shell_NotifyIcon(NIM_DELETE, &notifyData) != 0;
2bda0e17
KB
173}
174
53a118d6 175#if wxUSE_MENUS
4d0d77af 176bool wxTaskBarIcon::PopupMenu(wxMenu *menu)
69ecd30f 177{
1e6d9c20
VS
178 wxASSERT_MSG( m_win != NULL, _T("taskbar icon not initialized") );
179
04cd30de 180 static bool s_inPopup = false;
c7527e3f
JS
181
182 if (s_inPopup)
04cd30de 183 return false;
c7527e3f 184
04cd30de 185 s_inPopup = true;
c7527e3f 186
69ecd30f
RD
187 int x, y;
188 wxGetMousePosition(&x, &y);
189
1e6d9c20 190 m_win->Move(x, y);
bfbb0b4c 191
1e6d9c20 192 m_win->PushEventHandler(this);
d66d9d5b 193
50bcd1ae
JS
194 menu->UpdateUI();
195
63b3dc58
VZ
196 // the SetForegroundWindow() and PostMessage() calls are needed to work
197 // around Win32 bug with the popup menus shown for the notifications as
198 // documented at http://support.microsoft.com/kb/q135788/
199 ::SetForegroundWindow(GetHwndOf(m_win));
f6bcfd97 200
1e6d9c20 201 bool rval = m_win->PopupMenu(menu, 0, 0);
69ecd30f 202
63b3dc58 203 ::PostMessage(GetHwndOf(m_win), WM_NULL, 0, 0L);
f6bcfd97 204
1e6d9c20 205 m_win->PopEventHandler(false);
c7527e3f 206
04cd30de 207 s_inPopup = false;
d66d9d5b 208
69ecd30f
RD
209 return rval;
210}
53a118d6 211#endif // wxUSE_MENUS
69ecd30f 212
1e6d9c20 213void wxTaskBarIcon::RegisterWindowMessages()
2bda0e17 214{
4d0d77af 215 static bool s_registered = false;
2bda0e17 216
1e6d9c20 217 if ( !s_registered )
bfbb0b4c 218 {
1e6d9c20
VS
219 // Taskbar restart msg will be sent to us if the icon needs to be redrawn
220 gs_msgRestartTaskbar = RegisterWindowMessage(wxT("TaskbarCreated"));
2bda0e17 221
1e6d9c20
VS
222 // Also register the taskbar message here
223 gs_msgTaskbar = ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
2bda0e17 224
1e6d9c20
VS
225 s_registered = true;
226 }
2bda0e17
KB
227}
228
4d0d77af
VZ
229// ----------------------------------------------------------------------------
230// wxTaskBarIcon window proc
231// ----------------------------------------------------------------------------
232
dda36afd
VS
233long wxTaskBarIcon::WindowProc(unsigned int msg,
234 unsigned int WXUNUSED(wParam),
4d0d77af 235 long lParam)
2bda0e17 236{
56194595
RD
237 wxEventType eventType = 0;
238
4d0d77af
VZ
239 if (msg == gs_msgRestartTaskbar) // does the icon need to be redrawn?
240 {
241 m_iconAdded = false;
242 SetIcon(m_icon, m_strTooltip);
243 }
244
1e6d9c20
VS
245 // this function should only be called for gs_msg(Restart)Taskbar messages
246 wxASSERT(msg == gs_msgTaskbar);
2bda0e17
KB
247
248 switch (lParam)
249 {
c42404a5 250 case WM_LBUTTONDOWN:
56194595
RD
251 eventType = wxEVT_TASKBAR_LEFT_DOWN;
252 break;
2bda0e17 253
c42404a5 254 case WM_LBUTTONUP:
56194595
RD
255 eventType = wxEVT_TASKBAR_LEFT_UP;
256 break;
2bda0e17 257
c42404a5 258 case WM_RBUTTONDOWN:
56194595
RD
259 eventType = wxEVT_TASKBAR_RIGHT_DOWN;
260 break;
2bda0e17 261
c42404a5 262 case WM_RBUTTONUP:
56194595
RD
263 eventType = wxEVT_TASKBAR_RIGHT_UP;
264 break;
2bda0e17 265
c42404a5 266 case WM_LBUTTONDBLCLK:
56194595
RD
267 eventType = wxEVT_TASKBAR_LEFT_DCLICK;
268 break;
2bda0e17 269
c42404a5 270 case WM_RBUTTONDBLCLK:
56194595
RD
271 eventType = wxEVT_TASKBAR_RIGHT_DCLICK;
272 break;
2bda0e17 273
c42404a5 274 case WM_MOUSEMOVE:
56194595
RD
275 eventType = wxEVT_TASKBAR_MOVE;
276 break;
2bda0e17 277
c42404a5 278 default:
56194595 279 break;
4d0d77af 280 }
56194595 281
4d0d77af
VZ
282 if (eventType)
283 {
fa1c12bd 284 wxTaskBarIconEvent event(eventType, this);
56194595
RD
285
286 ProcessEvent(event);
287 }
4d0d77af 288
2bda0e17
KB
289 return 0;
290}