]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/taskbar.cpp
Go back to using a fixed value of 3 for lines/columns per action for mouse wheel...
[wxWidgets.git] / src / gtk / taskbar.cpp
1 /////////////////////////////////////////////////////////////////////////
2 // File: src/gtk/taskbar.cpp
3 // Purpose: wxTaskBarIcon
4 // Author: Vaclav Slavik
5 // Modified by: Paul Cornett
6 // Created: 2004/05/29
7 // Copyright: (c) Vaclav Slavik, 2004
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #if wxUSE_TASKBARICON
15
16 #include "wx/taskbar.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/toplevel.h"
20 #include "wx/menu.h"
21 #include "wx/icon.h"
22 #endif
23
24 #include <gtk/gtk.h>
25 #ifdef GDK_WINDOWING_X11
26 #include <gdk/gdkx.h>
27 #endif
28 #ifndef __WXGTK3__
29 #include "eggtrayicon.h"
30 #endif
31
32 #if !GTK_CHECK_VERSION(2,10,0)
33 typedef struct _GtkStatusIcon GtkStatusIcon;
34 #endif
35
36 class wxTaskBarIcon::Private
37 {
38 public:
39 Private(wxTaskBarIcon* taskBarIcon);
40 ~Private();
41 void SetIcon();
42 void size_allocate(int width, int height);
43
44 // owning wxTaskBarIcon
45 wxTaskBarIcon* m_taskBarIcon;
46 // used when GTK+ >= 2.10
47 GtkStatusIcon* m_statusIcon;
48 // for PopupMenu
49 wxWindow* m_win;
50 wxBitmap m_bitmap;
51 wxString m_tipText;
52 #ifndef __WXGTK3__
53 // used when GTK+ < 2.10
54 GtkWidget* m_eggTrayIcon;
55 // for tooltip when GTK+ < 2.10
56 GtkTooltips* m_tooltips;
57 // width and height of available space, only used when GTK+ < 2.10
58 int m_size;
59 #endif
60 };
61 //-----------------------------------------------------------------------------
62
63 extern "C" {
64 #ifndef __WXGTK3__
65 static void
66 icon_size_allocate(GtkWidget*, GtkAllocation* alloc, wxTaskBarIcon::Private* priv)
67 {
68 priv->size_allocate(alloc->width, alloc->height);
69 }
70
71 static void
72 icon_destroy(GtkWidget*, wxTaskBarIcon::Private* priv)
73 {
74 // Icon window destroyed, probably because tray program has died.
75 // Recreate icon so it will appear if tray program is restarted.
76 priv->m_eggTrayIcon = NULL;
77 priv->SetIcon();
78 }
79 #endif
80
81 static void
82 icon_activate(void*, wxTaskBarIcon* taskBarIcon)
83 {
84 // activate occurs from single click with GTK+
85 wxTaskBarIconEvent event(wxEVT_TASKBAR_LEFT_DOWN, taskBarIcon);
86 if (!taskBarIcon->SafelyProcessEvent(event))
87 {
88 // if single click not handled, send double click for compatibility
89 event.SetEventType(wxEVT_TASKBAR_LEFT_DCLICK);
90 taskBarIcon->SafelyProcessEvent(event);
91 }
92 }
93
94 static gboolean
95 icon_popup_menu(GtkWidget*, wxTaskBarIcon* taskBarIcon)
96 {
97 wxTaskBarIconEvent event(wxEVT_TASKBAR_CLICK, taskBarIcon);
98 taskBarIcon->SafelyProcessEvent(event);
99 return true;
100 }
101
102 #ifndef __WXGTK3__
103 static gboolean
104 icon_button_press_event(GtkWidget*, GdkEventButton* event, wxTaskBarIcon* taskBarIcon)
105 {
106 if (event->type == GDK_BUTTON_PRESS)
107 {
108 if (event->button == 1)
109 icon_activate(NULL, taskBarIcon);
110 else if (event->button == 3)
111 icon_popup_menu(NULL, taskBarIcon);
112 }
113 return false;
114 }
115 #endif
116
117 #if GTK_CHECK_VERSION(2,10,0)
118 static void
119 status_icon_popup_menu(GtkStatusIcon*, guint, guint, wxTaskBarIcon* taskBarIcon)
120 {
121 icon_popup_menu(NULL, taskBarIcon);
122 }
123 #endif
124 } // extern "C"
125 //-----------------------------------------------------------------------------
126
127 bool wxTaskBarIconBase::IsAvailable()
128 {
129 #ifdef GDK_WINDOWING_X11
130 char name[32];
131 g_snprintf(name, sizeof(name), "_NET_SYSTEM_TRAY_S%d",
132 gdk_x11_get_default_screen());
133 Atom atom = gdk_x11_get_xatom_by_name(name);
134
135 Window manager = XGetSelectionOwner(gdk_x11_get_default_xdisplay(), atom);
136
137 return manager != None;
138 #else
139 return true;
140 #endif
141 }
142 //-----------------------------------------------------------------------------
143
144 wxTaskBarIcon::Private::Private(wxTaskBarIcon* taskBarIcon)
145 {
146 m_taskBarIcon = taskBarIcon;
147 m_statusIcon = NULL;
148 m_win = NULL;
149 #ifndef __WXGTK3__
150 m_eggTrayIcon = NULL;
151 m_tooltips = NULL;
152 m_size = 0;
153 #endif
154 }
155
156 wxTaskBarIcon::Private::~Private()
157 {
158 if (m_statusIcon)
159 g_object_unref(m_statusIcon);
160 #ifndef __WXGTK3__
161 else if (m_eggTrayIcon)
162 {
163 g_signal_handlers_disconnect_by_func(m_eggTrayIcon, (void*)icon_destroy, this);
164 gtk_widget_destroy(m_eggTrayIcon);
165 }
166 #endif
167 if (m_win)
168 {
169 m_win->PopEventHandler();
170 m_win->Destroy();
171 }
172 #ifndef __WXGTK3__
173 if (m_tooltips)
174 {
175 gtk_object_destroy(GTK_OBJECT(m_tooltips));
176 g_object_unref(m_tooltips);
177 }
178 #endif
179 }
180
181 void wxTaskBarIcon::Private::SetIcon()
182 {
183 #if GTK_CHECK_VERSION(2,10,0)
184 if (GTK_CHECK_VERSION(3,0,0) || gtk_check_version(2,10,0) == NULL)
185 {
186 if (m_statusIcon)
187 gtk_status_icon_set_from_pixbuf(m_statusIcon, m_bitmap.GetPixbuf());
188 else
189 {
190 m_statusIcon = gtk_status_icon_new_from_pixbuf(m_bitmap.GetPixbuf());
191 g_signal_connect(m_statusIcon, "activate",
192 G_CALLBACK(icon_activate), m_taskBarIcon);
193 g_signal_connect(m_statusIcon, "popup_menu",
194 G_CALLBACK(status_icon_popup_menu), m_taskBarIcon);
195 }
196 }
197 else
198 #endif
199 {
200 #ifndef __WXGTK3__
201 m_size = 0;
202 if (m_eggTrayIcon)
203 {
204 GtkWidget* image = gtk_bin_get_child(GTK_BIN(m_eggTrayIcon));
205 gtk_image_set_from_pixbuf(GTK_IMAGE(image), m_bitmap.GetPixbuf());
206 }
207 else
208 {
209 m_eggTrayIcon = GTK_WIDGET(egg_tray_icon_new("wxTaskBarIcon"));
210 gtk_widget_add_events(m_eggTrayIcon, GDK_BUTTON_PRESS_MASK);
211 g_signal_connect(m_eggTrayIcon, "size_allocate",
212 G_CALLBACK(icon_size_allocate), this);
213 g_signal_connect(m_eggTrayIcon, "destroy",
214 G_CALLBACK(icon_destroy), this);
215 g_signal_connect(m_eggTrayIcon, "button_press_event",
216 G_CALLBACK(icon_button_press_event), m_taskBarIcon);
217 g_signal_connect(m_eggTrayIcon, "popup_menu",
218 G_CALLBACK(icon_popup_menu), m_taskBarIcon);
219 GtkWidget* image = gtk_image_new_from_pixbuf(m_bitmap.GetPixbuf());
220 gtk_container_add(GTK_CONTAINER(m_eggTrayIcon), image);
221 gtk_widget_show_all(m_eggTrayIcon);
222 }
223 #endif
224 }
225 #if wxUSE_TOOLTIPS
226 const char *tip_text = NULL;
227 if (!m_tipText.empty())
228 tip_text = m_tipText.utf8_str();
229
230 #if GTK_CHECK_VERSION(2,10,0)
231 if (m_statusIcon)
232 {
233 #if GTK_CHECK_VERSION(2,16,0)
234 if (GTK_CHECK_VERSION(3,0,0) || gtk_check_version(2,16,0) == NULL)
235 gtk_status_icon_set_tooltip_text(m_statusIcon, tip_text);
236 else
237 #endif
238 {
239 #ifndef __WXGTK3__
240 gtk_status_icon_set_tooltip(m_statusIcon, tip_text);
241 #endif
242 }
243 }
244 else
245 #endif // GTK_CHECK_VERSION(2,10,0)
246 {
247 #ifndef __WXGTK3__
248 if (tip_text && m_tooltips == NULL)
249 {
250 m_tooltips = gtk_tooltips_new();
251 g_object_ref(m_tooltips);
252 gtk_object_sink(GTK_OBJECT(m_tooltips));
253 }
254 if (m_tooltips)
255 gtk_tooltips_set_tip(m_tooltips, m_eggTrayIcon, tip_text, "");
256 #endif
257 }
258 #endif // wxUSE_TOOLTIPS
259 }
260
261 #ifndef __WXGTK3__
262 void wxTaskBarIcon::Private::size_allocate(int width, int height)
263 {
264 int size = height;
265 EggTrayIcon* icon = EGG_TRAY_ICON(m_eggTrayIcon);
266 if (egg_tray_icon_get_orientation(icon) == GTK_ORIENTATION_VERTICAL)
267 size = width;
268 if (m_size == size)
269 return;
270 m_size = size;
271 int w = m_bitmap.GetWidth();
272 int h = m_bitmap.GetHeight();
273 if (w > size || h > size)
274 {
275 if (w > size) w = size;
276 if (h > size) h = size;
277 GdkPixbuf* pixbuf =
278 gdk_pixbuf_scale_simple(m_bitmap.GetPixbuf(), w, h, GDK_INTERP_BILINEAR);
279 GtkImage* image = GTK_IMAGE(gtk_bin_get_child(GTK_BIN(m_eggTrayIcon)));
280 gtk_image_set_from_pixbuf(image, pixbuf);
281 g_object_unref(pixbuf);
282 }
283 }
284 #endif
285 //-----------------------------------------------------------------------------
286
287 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
288
289 wxTaskBarIcon::wxTaskBarIcon(wxTaskBarIconType WXUNUSED(iconType))
290 {
291 m_priv = new Private(this);
292 }
293
294 wxTaskBarIcon::~wxTaskBarIcon()
295 {
296 delete m_priv;
297 }
298
299 bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
300 {
301 m_priv->m_bitmap = icon;
302 m_priv->m_tipText = tooltip;
303 m_priv->SetIcon();
304 return true;
305 }
306
307 bool wxTaskBarIcon::RemoveIcon()
308 {
309 delete m_priv;
310 m_priv = new Private(this);
311 return true;
312 }
313
314 bool wxTaskBarIcon::IsIconInstalled() const
315 {
316 #ifdef __WXGTK3__
317 return m_priv->m_statusIcon != NULL;
318 #else
319 return m_priv->m_statusIcon || m_priv->m_eggTrayIcon;
320 #endif
321 }
322
323 bool wxTaskBarIcon::PopupMenu(wxMenu* menu)
324 {
325 #if wxUSE_MENUS
326 if (m_priv->m_win == NULL)
327 {
328 m_priv->m_win = new wxTopLevelWindow(
329 NULL, wxID_ANY, wxString(), wxDefaultPosition, wxDefaultSize, 0);
330 m_priv->m_win->PushEventHandler(this);
331 }
332 wxPoint point(-1, -1);
333 #ifdef __WXUNIVERSAL__
334 point = wxGetMousePosition();
335 #endif
336 m_priv->m_win->PopupMenu(menu, point);
337 #endif // wxUSE_MENUS
338 return true;
339 }
340
341 #endif // wxUSE_TASKBARICON