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