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