1 /////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTaskBarIcon class for common Unix desktops
4 // Author: Vaclav Slavik
8 // Copyright: (c) Vaclav Slavik, 2003
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "taskbarx11.h"
16 // NB: This implementation does *not* work with every X11 window manager.
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.
21 // Thanks to Ian Campbell, author of XMMS Status Docklet, for publishing
22 // KDE and GNOME 1.2 methods.
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
28 #include "wx/taskbar.h"
30 #include "wx/bitmap.h"
31 #include "wx/statbmp.h"
33 #include "wx/dcclient.h"
38 #pragma message disable nosimpint
41 #include <X11/Xatom.h>
43 #pragma message enable nosimpint
46 // ----------------------------------------------------------------------------
47 // base class that implements toolkit-specific method:
48 // ----------------------------------------------------------------------------
51 #include "wx/gtk/taskbarpriv.h"
53 class WXDLLIMPEXP_ADV wxTaskBarIconAreaBase
: public wxFrame
56 wxTaskBarIconAreaBase()
57 : wxFrame(NULL
, wxID_ANY
, _T("systray icon"),
58 wxDefaultPosition
, wxDefaultSize
,
59 wxDEFAULT_FRAME_STYLE
| wxFRAME_NO_TASKBAR
|
60 wxSIMPLE_BORDER
| wxFRAME_SHAPED
) {}
62 bool IsProtocolSupported() const { return false; }
67 // ----------------------------------------------------------------------------
68 // toolkit dependent methods to set properties on helper window:
69 // ----------------------------------------------------------------------------
71 #if defined(__WXGTK__)
75 #define GetDisplay() GDK_DISPLAY()
76 #define GetXWindow(wxwin) GDK_WINDOW_XWINDOW((wxwin)->m_widget->window)
77 #elif defined(__WXX11__) || defined(__WXMOTIF__)
78 #include "wx/x11/privx.h"
79 #define GetDisplay() ((Display*)wxGlobalDisplay())
80 #define GetXWindow(wxwin) ((Window)(wxwin)->GetHandle())
82 #error "You must define X11 accessors for this port!"
86 // ----------------------------------------------------------------------------
87 // wxTaskBarIconArea is the real window that shows the icon:
88 // ----------------------------------------------------------------------------
90 class WXDLLIMPEXP_ADV wxTaskBarIconArea
: public wxTaskBarIconAreaBase
93 wxTaskBarIconArea(wxTaskBarIcon
*icon
, const wxBitmap
&bmp
);
94 void SetTrayIcon(const wxBitmap
& bmp
);
95 bool IsOk() { return true; }
98 void SetLegacyWMProperties();
100 void OnSizeChange(wxSizeEvent
& event
);
101 void OnPaint(wxPaintEvent
& evt
);
102 void OnMouseEvent(wxMouseEvent
& event
);
103 void OnMenuEvent(wxCommandEvent
& event
);
105 wxTaskBarIcon
*m_icon
;
109 DECLARE_EVENT_TABLE()
112 BEGIN_EVENT_TABLE(wxTaskBarIconArea
, wxTaskBarIconAreaBase
)
113 EVT_SIZE(wxTaskBarIconArea::OnSizeChange
)
114 EVT_MOUSE_EVENTS(wxTaskBarIconArea::OnMouseEvent
)
115 EVT_MENU(-1, wxTaskBarIconArea::OnMenuEvent
)
116 EVT_PAINT(wxTaskBarIconArea::OnPaint
)
119 wxTaskBarIconArea::wxTaskBarIconArea(wxTaskBarIcon
*icon
, const wxBitmap
&bmp
)
120 : wxTaskBarIconAreaBase(), m_icon(icon
), m_pos(0,0)
122 if (!IsProtocolSupported())
124 wxLogTrace(_T("systray"),
125 _T("using legacy KDE1,2 and GNOME 1.2 methods"));
126 SetLegacyWMProperties();
129 // Set initial size to bitmap size (tray manager may and often will
131 SetSize(wxSize(bmp
.GetWidth(), bmp
.GetHeight()));
136 void wxTaskBarIconArea::SetTrayIcon(const wxBitmap
& bmp
)
140 // determine suitable bitmap size:
141 wxSize
winsize(GetSize());
142 wxSize
bmpsize(m_bmp
.GetWidth(), m_bmp
.GetHeight());
143 wxSize
iconsize(wxMin(winsize
.x
, bmpsize
.x
), wxMin(winsize
.y
, bmpsize
.y
));
145 // rescale the bitmap to fit into the tray icon window:
146 if (bmpsize
!= iconsize
)
148 wxImage img
= m_bmp
.ConvertToImage();
149 img
.Rescale(iconsize
.x
, iconsize
.y
);
150 m_bmp
= wxBitmap(img
);
156 // if the bitmap is smaller than the window, offset it:
157 if (winsize
!= iconsize
)
159 m_pos
.x
= (winsize
.x
- iconsize
.x
) / 2;
160 m_pos
.y
= (winsize
.y
- iconsize
.y
) / 2;
161 region
.Offset(m_pos
.x
, m_pos
.y
);
164 // set frame's shape to correct value and redraw:
169 void wxTaskBarIconArea::SetLegacyWMProperties()
172 gtk_widget_realize(m_widget
);
178 Atom _KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR
=
179 XInternAtom(GetDisplay(), "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR", False
);
181 XChangeProperty(GetDisplay(), GetXWindow(this),
182 _KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR
,
184 PropModeReplace
, (unsigned char*)data
, 1);
186 // GNOME 1.2 & KDE 1:
187 Atom KWM_DOCKWINDOW
=
188 XInternAtom(GetDisplay(), "KWM_DOCKWINDOW", False
);
190 XChangeProperty(GetDisplay(), GetXWindow(this),
193 PropModeReplace
, (unsigned char*)data
, 1);
196 void wxTaskBarIconArea::OnSizeChange(wxSizeEvent
& event
)
198 wxLogTrace(_T("systray"), _T("icon size changed to %i x %i"),
199 GetSize().x
, GetSize().y
);
200 // rescale or reposition the icon as needed:
205 void wxTaskBarIconArea::OnPaint(wxPaintEvent
& WXUNUSED(event
))
208 dc
.DrawBitmap(m_bmp
, m_pos
.x
, m_pos
.y
, true);
211 void wxTaskBarIconArea::OnMouseEvent(wxMouseEvent
& event
)
213 wxEventType type
= 0;
214 wxEventType mtype
= event
.GetEventType();
216 if (mtype
== wxEVT_LEFT_DOWN
)
217 type
= wxEVT_TASKBAR_LEFT_DOWN
;
218 else if (mtype
== wxEVT_LEFT_UP
)
219 type
= wxEVT_TASKBAR_LEFT_UP
;
220 else if (mtype
== wxEVT_LEFT_DCLICK
)
221 type
= wxEVT_TASKBAR_LEFT_DCLICK
;
222 else if (mtype
== wxEVT_RIGHT_DOWN
)
223 type
= wxEVT_TASKBAR_RIGHT_DOWN
;
224 else if (mtype
== wxEVT_RIGHT_UP
)
225 type
= wxEVT_TASKBAR_RIGHT_UP
;
226 else if (mtype
== wxEVT_RIGHT_DCLICK
)
227 type
= wxEVT_TASKBAR_RIGHT_DCLICK
;
228 else if (mtype
== wxEVT_MOTION
)
229 type
= wxEVT_TASKBAR_MOVE
;
233 wxTaskBarIconEvent
e(type
, m_icon
);
234 m_icon
->ProcessEvent(e
);
237 void wxTaskBarIconArea::OnMenuEvent(wxCommandEvent
& event
)
239 m_icon
->ProcessEvent(event
);
242 // ----------------------------------------------------------------------------
243 // wxTaskBarIcon class:
244 // ----------------------------------------------------------------------------
246 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
248 wxTaskBarIcon::wxTaskBarIcon() : m_iconWnd(NULL
)
252 wxTaskBarIcon::~wxTaskBarIcon()
258 bool wxTaskBarIcon::IsOk() const
263 bool wxTaskBarIcon::IsIconInstalled() const
265 return m_iconWnd
!= NULL
;
268 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
271 bmp
.CopyFromIcon(icon
);
275 m_iconWnd
= new wxTaskBarIconArea(this, bmp
);
276 if (m_iconWnd
->IsOk())
282 m_iconWnd
->Destroy();
289 m_iconWnd
->SetTrayIcon(bmp
);
293 if (!tooltip
.empty())
294 m_iconWnd
->SetToolTip(tooltip
);
296 m_iconWnd
->SetToolTip(NULL
);
301 bool wxTaskBarIcon::RemoveIcon()
305 m_iconWnd
->Destroy();
310 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
)
314 m_iconWnd
->PopupMenu(menu
);