]> git.saurik.com Git - wxWidgets.git/blob - src/unix/taskbarx11.cpp
don't leave tables non-owned by the grid with dangling view pointer when the grid...
[wxWidgets.git] / src / unix / taskbarx11.cpp
1 /////////////////////////////////////////////////////////////////////////
2 // File: src/unix/taskbarx11.cpp
3 // Purpose: wxTaskBarIcon class for common Unix desktops
4 // Author: Vaclav Slavik
5 // Modified by:
6 // Created: 04/04/2003
7 // RCS-ID: $Id$
8 // Copyright: (c) Vaclav Slavik, 2003
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////
11
12 // NB: This implementation does *not* work with every X11 window manager.
13 // Currently only GNOME 1.2 and KDE 1,2,3 methods are implemented here.
14 // Freedesktop.org's System Tray specification is implemented in
15 // src/gtk/taskbar.cpp and used from here under wxGTK.
16 //
17 // Thanks to Ian Campbell, author of XMMS Status Docklet, for publishing
18 // KDE and GNOME 1.2 methods.
19
20
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
23
24 #include "wx/taskbar.h"
25
26 #ifndef WX_PRECOMP
27 #include "wx/log.h"
28 #include "wx/frame.h"
29 #include "wx/dcclient.h"
30 #include "wx/statbmp.h"
31 #include "wx/sizer.h"
32 #include "wx/bitmap.h"
33 #include "wx/image.h"
34 #endif
35
36 #ifdef __VMS
37 #pragma message disable nosimpint
38 #endif
39 #include <X11/Xlib.h>
40 #include <X11/Xatom.h>
41 #ifdef __VMS
42 #pragma message enable nosimpint
43 #endif
44
45 // ----------------------------------------------------------------------------
46 // base class that implements toolkit-specific method:
47 // ----------------------------------------------------------------------------
48
49 #ifdef __WXGTK20__
50 #include <gtk/gtk.h>
51 #if GTK_CHECK_VERSION(2,1,0)
52 #include "wx/gtk/taskbarpriv.h"
53 #define TASKBAR_ICON_AREA_BASE_INCLUDED
54 #endif
55 #endif
56
57 #ifndef TASKBAR_ICON_AREA_BASE_INCLUDED
58 class WXDLLIMPEXP_ADV wxTaskBarIconAreaBase : public wxFrame
59 {
60 public:
61 wxTaskBarIconAreaBase()
62 : wxFrame(NULL, wxID_ANY, _T("systray icon"),
63 wxDefaultPosition, wxDefaultSize,
64 wxDEFAULT_FRAME_STYLE | wxFRAME_NO_TASKBAR |
65 wxSIMPLE_BORDER | wxFRAME_SHAPED) {}
66
67 bool IsProtocolSupported() const { return false; }
68 };
69 #endif
70
71
72 // ----------------------------------------------------------------------------
73 // toolkit dependent methods to set properties on helper window:
74 // ----------------------------------------------------------------------------
75
76 #if defined(__WXGTK__)
77 #include <gdk/gdk.h>
78 #include <gdk/gdkx.h>
79 #include <gtk/gtk.h>
80 #define GetDisplay() GDK_DISPLAY()
81 #define GetXWindow(wxwin) GDK_WINDOW_XWINDOW((wxwin)->m_widget->window)
82 #elif defined(__WXX11__) || defined(__WXMOTIF__)
83 #include "wx/x11/privx.h"
84 #define GetDisplay() ((Display*)wxGlobalDisplay())
85 #define GetXWindow(wxwin) ((Window)(wxwin)->GetHandle())
86 #else
87 #error "You must define X11 accessors for this port!"
88 #endif
89
90
91 // ----------------------------------------------------------------------------
92 // wxTaskBarIconArea is the real window that shows the icon:
93 // ----------------------------------------------------------------------------
94
95 class WXDLLIMPEXP_ADV wxTaskBarIconArea : public wxTaskBarIconAreaBase
96 {
97 public:
98 wxTaskBarIconArea(wxTaskBarIcon *icon, const wxBitmap &bmp);
99 void SetTrayIcon(const wxBitmap& bmp);
100 bool IsOk() { return true; }
101
102 protected:
103 void SetLegacyWMProperties();
104
105 void OnSizeChange(wxSizeEvent& event);
106 void OnPaint(wxPaintEvent& evt);
107 void OnMouseEvent(wxMouseEvent& event);
108 void OnMenuEvent(wxCommandEvent& event);
109
110 wxTaskBarIcon *m_icon;
111 wxPoint m_pos;
112 wxBitmap m_bmp;
113
114 DECLARE_EVENT_TABLE()
115 };
116
117 BEGIN_EVENT_TABLE(wxTaskBarIconArea, wxTaskBarIconAreaBase)
118 EVT_SIZE(wxTaskBarIconArea::OnSizeChange)
119 EVT_MOUSE_EVENTS(wxTaskBarIconArea::OnMouseEvent)
120 EVT_MENU(wxID_ANY, wxTaskBarIconArea::OnMenuEvent)
121 EVT_PAINT(wxTaskBarIconArea::OnPaint)
122 END_EVENT_TABLE()
123
124 wxTaskBarIconArea::wxTaskBarIconArea(wxTaskBarIcon *icon, const wxBitmap &bmp)
125 : wxTaskBarIconAreaBase(), m_icon(icon), m_pos(0,0)
126 {
127 if (!IsProtocolSupported())
128 {
129 wxLogTrace(_T("systray"),
130 _T("using legacy KDE1,2 and GNOME 1.2 methods"));
131 SetLegacyWMProperties();
132 }
133
134 #if defined(__WXGTK20__) && defined(TASKBAR_ICON_AREA_BASE_INCLUDED)
135 m_invokingWindow = icon;
136 #endif
137
138 // Set initial size to bitmap size (tray manager may and often will
139 // change it):
140 SetSize(wxSize(bmp.GetWidth(), bmp.GetHeight()));
141
142 SetTrayIcon(bmp);
143 }
144
145 void wxTaskBarIconArea::SetTrayIcon(const wxBitmap& bmp)
146 {
147 m_bmp = bmp;
148
149 // determine suitable bitmap size:
150 wxSize winsize(GetSize());
151 wxSize bmpsize(m_bmp.GetWidth(), m_bmp.GetHeight());
152 wxSize iconsize(wxMin(winsize.x, bmpsize.x), wxMin(winsize.y, bmpsize.y));
153
154 // rescale the bitmap to fit into the tray icon window:
155 if (bmpsize != iconsize)
156 {
157 wxImage img = m_bmp.ConvertToImage();
158 img.Rescale(iconsize.x, iconsize.y);
159 m_bmp = wxBitmap(img);
160 }
161
162 wxRegion region;
163 region.Union(m_bmp);
164
165 // if the bitmap is smaller than the window, offset it:
166 if (winsize != iconsize)
167 {
168 m_pos.x = (winsize.x - iconsize.x) / 2;
169 m_pos.y = (winsize.y - iconsize.y) / 2;
170 region.Offset(m_pos.x, m_pos.y);
171 }
172
173 // set frame's shape to correct value and redraw:
174 SetShape(region);
175 Refresh();
176 }
177
178 void wxTaskBarIconArea::SetLegacyWMProperties()
179 {
180 #ifdef __WXGTK__
181 gtk_widget_realize(m_widget);
182 #endif
183
184 long data[1];
185
186 // KDE 2 & KDE 3:
187 Atom _KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR =
188 XInternAtom(GetDisplay(), "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR", False);
189 data[0] = 0;
190 XChangeProperty(GetDisplay(), GetXWindow(this),
191 _KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR,
192 XA_WINDOW, 32,
193 PropModeReplace, (unsigned char*)data, 1);
194
195 // GNOME 1.2 & KDE 1:
196 Atom KWM_DOCKWINDOW =
197 XInternAtom(GetDisplay(), "KWM_DOCKWINDOW", False);
198 data[0] = 1;
199 XChangeProperty(GetDisplay(), GetXWindow(this),
200 KWM_DOCKWINDOW,
201 KWM_DOCKWINDOW, 32,
202 PropModeReplace, (unsigned char*)data, 1);
203 }
204
205 void wxTaskBarIconArea::OnSizeChange(wxSizeEvent& WXUNUSED(event))
206 {
207 wxLogTrace(_T("systray"), _T("icon size changed to %i x %i"),
208 GetSize().x, GetSize().y);
209 // rescale or reposition the icon as needed:
210 wxBitmap bmp(m_bmp);
211 SetTrayIcon(bmp);
212 }
213
214 void wxTaskBarIconArea::OnPaint(wxPaintEvent& WXUNUSED(event))
215 {
216 wxPaintDC dc(this);
217 dc.DrawBitmap(m_bmp, m_pos.x, m_pos.y, true);
218 }
219
220 void wxTaskBarIconArea::OnMouseEvent(wxMouseEvent& event)
221 {
222 wxEventType type = 0;
223 wxEventType mtype = event.GetEventType();
224
225 if (mtype == wxEVT_LEFT_DOWN)
226 type = wxEVT_TASKBAR_LEFT_DOWN;
227 else if (mtype == wxEVT_LEFT_UP)
228 type = wxEVT_TASKBAR_LEFT_UP;
229 else if (mtype == wxEVT_LEFT_DCLICK)
230 type = wxEVT_TASKBAR_LEFT_DCLICK;
231 else if (mtype == wxEVT_RIGHT_DOWN)
232 type = wxEVT_TASKBAR_RIGHT_DOWN;
233 else if (mtype == wxEVT_RIGHT_UP)
234 type = wxEVT_TASKBAR_RIGHT_UP;
235 else if (mtype == wxEVT_RIGHT_DCLICK)
236 type = wxEVT_TASKBAR_RIGHT_DCLICK;
237 else if (mtype == wxEVT_MOTION)
238 type = wxEVT_TASKBAR_MOVE;
239 else
240 return;
241
242 wxTaskBarIconEvent e(type, m_icon);
243 m_icon->ProcessEvent(e);
244 }
245
246 void wxTaskBarIconArea::OnMenuEvent(wxCommandEvent& event)
247 {
248 m_icon->ProcessEvent(event);
249 }
250
251 // ----------------------------------------------------------------------------
252 // wxTaskBarIcon class:
253 // ----------------------------------------------------------------------------
254
255 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
256
257 wxTaskBarIcon::wxTaskBarIcon() : m_iconWnd(NULL)
258 {
259 }
260
261 wxTaskBarIcon::~wxTaskBarIcon()
262 {
263 if (m_iconWnd)
264 RemoveIcon();
265 }
266
267 bool wxTaskBarIcon::IsOk() const
268 {
269 return true;
270 }
271
272 bool wxTaskBarIcon::IsIconInstalled() const
273 {
274 return m_iconWnd != NULL;
275 }
276
277 bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
278 {
279 wxBitmap bmp;
280 bmp.CopyFromIcon(icon);
281
282 if (!m_iconWnd)
283 {
284 m_iconWnd = new wxTaskBarIconArea(this, bmp);
285 if (m_iconWnd->IsOk())
286 {
287 m_iconWnd->Show();
288 }
289 else
290 {
291 m_iconWnd->Destroy();
292 m_iconWnd = NULL;
293 return false;
294 }
295 }
296 else
297 {
298 m_iconWnd->SetTrayIcon(bmp);
299 }
300
301 #if wxUSE_TOOLTIPS
302 if (!tooltip.empty())
303 m_iconWnd->SetToolTip(tooltip);
304 else
305 m_iconWnd->SetToolTip(NULL);
306 #else
307 wxUnusedVar(tooltip);
308 #endif
309 return true;
310 }
311
312 bool wxTaskBarIcon::RemoveIcon()
313 {
314 if (!m_iconWnd)
315 return false;
316 m_iconWnd->Destroy();
317 m_iconWnd = NULL;
318 return true;
319 }
320
321 bool wxTaskBarIcon::PopupMenu(wxMenu *menu)
322 {
323 if (!m_iconWnd)
324 return false;
325 m_iconWnd->PopupMenu(menu);
326 return true;
327 }