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