]> git.saurik.com Git - wxWidgets.git/blame - src/msw/taskbar.cpp
Avoid core dumps when SetImageList is used.
[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)
c42404a5 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
b39dbf34
JS
40#ifdef __GNUWIN32_OLD__
41 #include "wx/msw/gnuwin32/extra.h"
65fd5cb0 42#endif
2bda0e17 43
4676948b
JS
44#ifdef __WXWINCE__
45 #include <winreg.h>
c42404a5 46 #include <shellapi.h>
ce3ed50d
JS
47#endif
48
4d0d77af
VZ
49// initialized on demand
50UINT gs_msgTaskbar = 0;
51UINT gs_msgRestartTaskbar = 0;
2bda0e17 52
6af507f7 53#if WXWIN_COMPATIBILITY_2_4
56194595 54BEGIN_EVENT_TABLE(wxTaskBarIcon, wxEvtHandler)
69ecd30f
RD
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)
56194595 62END_EVENT_TABLE()
6af507f7 63#endif
56194595
RD
64
65
66IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
56194595 67
4d0d77af
VZ
68// ============================================================================
69// implementation
70// ============================================================================
71
1e6d9c20
VS
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
80class wxTaskBarIconWindow : public wxFrame
81{
82public:
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 {
dda36afd 94 return m_icon->WindowProc(msg, wParam, lParam);
1e6d9c20
VS
95 }
96 else
97 {
98 return wxFrame::MSWWindowProc(msg, wParam, lParam);
99 }
100 }
101
102private:
103 wxTaskBarIcon *m_icon;
104};
105
106
4d0d77af
VZ
107// ----------------------------------------------------------------------------
108// NotifyIconData: wrapper around NOTIFYICONDATA
109// ----------------------------------------------------------------------------
56194595 110
4d0d77af
VZ
111struct 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
131wxTaskBarIcon::wxTaskBarIcon()
2bda0e17 132{
1e6d9c20 133 m_win = NULL;
04cd30de 134 m_iconAdded = false;
1e6d9c20 135 RegisterWindowMessages();
2bda0e17
KB
136}
137
4d0d77af 138wxTaskBarIcon::~wxTaskBarIcon()
2bda0e17 139{
2bda0e17 140 if (m_iconAdded)
2bda0e17 141 RemoveIcon();
2bda0e17 142
1e6d9c20
VS
143 if (m_win)
144 m_win->Destroy();
2bda0e17
KB
145}
146
147// Operations
148bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
149{
1e6d9c20
VS
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 }
2bda0e17 157
4d0d77af
VZ
158 m_icon = icon;
159 m_strTooltip = tooltip;
160
1e6d9c20 161 NotifyIconData notifyData((HWND)m_win->GetHWND());
2bda0e17 162
e96360ef 163 if (icon.Ok())
2bda0e17 164 {
e96360ef 165 notifyData.uFlags |= NIF_ICON;
4d0d77af 166 notifyData.hIcon = GetHiconOf(icon);
2bda0e17
KB
167 }
168
4d0d77af 169 if ( !tooltip.empty() )
2bda0e17 170 {
4d0d77af 171 notifyData.uFlags |= NIF_TIP;
4676948b
JS
172// lstrcpyn(notifyData.szTip, tooltip.c_str(), WXSIZEOF(notifyData.szTip));
173 wxStrncpy(notifyData.szTip, tooltip.c_str(), WXSIZEOF(notifyData.szTip));
2bda0e17
KB
174 }
175
4d0d77af
VZ
176 bool ok = Shell_NotifyIcon(m_iconAdded ? NIM_MODIFY
177 : NIM_ADD, &notifyData) != 0;
2bda0e17 178
4d0d77af
VZ
179 if ( !m_iconAdded && ok )
180 m_iconAdded = true;
181
182 return ok;
2bda0e17
KB
183}
184
4d0d77af 185bool wxTaskBarIcon::RemoveIcon()
2bda0e17
KB
186{
187 if (!m_iconAdded)
04cd30de 188 return false;
2bda0e17 189
04cd30de 190 m_iconAdded = false;
2bda0e17 191
1e6d9c20 192 NotifyIconData notifyData((HWND)m_win->GetHWND());
4d0d77af
VZ
193
194 return Shell_NotifyIcon(NIM_DELETE, &notifyData) != 0;
2bda0e17
KB
195}
196
4d0d77af 197bool wxTaskBarIcon::PopupMenu(wxMenu *menu)
69ecd30f 198{
1e6d9c20
VS
199 wxASSERT_MSG( m_win != NULL, _T("taskbar icon not initialized") );
200
04cd30de 201 static bool s_inPopup = false;
c7527e3f
JS
202
203 if (s_inPopup)
04cd30de 204 return false;
c7527e3f 205
04cd30de 206 s_inPopup = true;
c7527e3f 207
69ecd30f
RD
208 int x, y;
209 wxGetMousePosition(&x, &y);
210
1e6d9c20
VS
211 m_win->Move(x, y);
212
213 m_win->PushEventHandler(this);
d66d9d5b 214
50bcd1ae
JS
215 menu->UpdateUI();
216
f6bcfd97 217 // Work around a WIN32 bug
1e6d9c20 218 ::SetForegroundWindow((HWND)m_win->GetHWND());
f6bcfd97 219
1e6d9c20 220 bool rval = m_win->PopupMenu(menu, 0, 0);
69ecd30f 221
f6bcfd97 222 // Work around a WIN32 bug
1e6d9c20 223 ::PostMessage((HWND)m_win->GetHWND(), WM_NULL, 0, 0L);
f6bcfd97 224
1e6d9c20 225 m_win->PopEventHandler(false);
c7527e3f 226
04cd30de 227 s_inPopup = false;
d66d9d5b 228
69ecd30f
RD
229 return rval;
230}
231
6af507f7 232#if WXWIN_COMPATIBILITY_2_4
2bda0e17 233// Overridables
6af507f7
VS
234void wxTaskBarIcon::OnMouseMove(wxEvent&) {}
235void wxTaskBarIcon::OnLButtonDown(wxEvent&) {}
236void wxTaskBarIcon::OnLButtonUp(wxEvent&) {}
237void wxTaskBarIcon::OnRButtonDown(wxEvent&) {}
238void wxTaskBarIcon::OnRButtonUp(wxEvent&) {}
239void wxTaskBarIcon::OnLButtonDClick(wxEvent&) {}
240void wxTaskBarIcon::OnRButtonDClick(wxEvent&) {}
2bda0e17 241
69ecd30f
RD
242void wxTaskBarIcon::_OnMouseMove(wxEvent& e) { OnMouseMove(e); }
243void wxTaskBarIcon::_OnLButtonDown(wxEvent& e) { OnLButtonDown(e); }
244void wxTaskBarIcon::_OnLButtonUp(wxEvent& e) { OnLButtonUp(e); }
245void wxTaskBarIcon::_OnRButtonDown(wxEvent& e) { OnRButtonDown(e); }
246void wxTaskBarIcon::_OnRButtonUp(wxEvent& e) { OnRButtonUp(e); }
247void wxTaskBarIcon::_OnLButtonDClick(wxEvent& e) { OnLButtonDClick(e); }
248void wxTaskBarIcon::_OnRButtonDClick(wxEvent& e) { OnRButtonDClick(e); }
6af507f7 249#endif
69ecd30f 250
1e6d9c20 251void wxTaskBarIcon::RegisterWindowMessages()
2bda0e17 252{
4d0d77af 253 static bool s_registered = false;
2bda0e17 254
1e6d9c20
VS
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"));
2bda0e17 259
1e6d9c20
VS
260 // Also register the taskbar message here
261 gs_msgTaskbar = ::RegisterWindowMessage(wxT("wxTaskBarIconMessage"));
2bda0e17 262
1e6d9c20
VS
263 s_registered = true;
264 }
2bda0e17
KB
265}
266
4d0d77af
VZ
267// ----------------------------------------------------------------------------
268// wxTaskBarIcon window proc
269// ----------------------------------------------------------------------------
270
dda36afd
VS
271long wxTaskBarIcon::WindowProc(unsigned int msg,
272 unsigned int WXUNUSED(wParam),
4d0d77af 273 long lParam)
2bda0e17 274{
56194595
RD
275 wxEventType eventType = 0;
276
4d0d77af
VZ
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
1e6d9c20
VS
283 // this function should only be called for gs_msg(Restart)Taskbar messages
284 wxASSERT(msg == gs_msgTaskbar);
2bda0e17
KB
285
286 switch (lParam)
287 {
c42404a5 288 case WM_LBUTTONDOWN:
56194595
RD
289 eventType = wxEVT_TASKBAR_LEFT_DOWN;
290 break;
2bda0e17 291
c42404a5 292 case WM_LBUTTONUP:
56194595
RD
293 eventType = wxEVT_TASKBAR_LEFT_UP;
294 break;
2bda0e17 295
c42404a5 296 case WM_RBUTTONDOWN:
56194595
RD
297 eventType = wxEVT_TASKBAR_RIGHT_DOWN;
298 break;
2bda0e17 299
c42404a5 300 case WM_RBUTTONUP:
56194595
RD
301 eventType = wxEVT_TASKBAR_RIGHT_UP;
302 break;
2bda0e17 303
c42404a5 304 case WM_LBUTTONDBLCLK:
56194595
RD
305 eventType = wxEVT_TASKBAR_LEFT_DCLICK;
306 break;
2bda0e17 307
c42404a5 308 case WM_RBUTTONDBLCLK:
56194595
RD
309 eventType = wxEVT_TASKBAR_RIGHT_DCLICK;
310 break;
2bda0e17 311
c42404a5 312 case WM_MOUSEMOVE:
56194595
RD
313 eventType = wxEVT_TASKBAR_MOVE;
314 break;
2bda0e17 315
c42404a5 316 default:
56194595 317 break;
4d0d77af 318 }
56194595 319
4d0d77af
VZ
320 if (eventType)
321 {
fa1c12bd 322 wxTaskBarIconEvent event(eventType, this);
56194595
RD
323
324 ProcessEvent(event);
325 }
4d0d77af 326
2bda0e17
KB
327 return 0;
328}
329
1e6d9c20 330#endif // __WIN95__