1 /* gtkstatusicon.c:
2  *
3  * Copyright (C) 2003 Sun Microsystems, Inc.
4  * Copyright (C) 2005 Hans Breuer <hans@breuer.org>
5  * Copyright (C) 2005 Novell, Inc.
6  * Copyright (C) 2006 Imendio AB
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  *
23  * Authors:
24  *	Mark McLoughlin <mark@skynet.ie>
25  *	Hans Breuer <hans@breuer.org>
26  *	Tor Lillqvist <tml@novell.com>
27  *	Mikael Hallendal <micke@imendio.com>
28  */
29 
30 #include "config.h"
31 #include <string.h>
32 
33 #include "gtkstatusicon.h"
34 
35 #include "gtkintl.h"
36 #include "gtkiconfactory.h"
37 #include "gtkmain.h"
38 #include "gtkmarshalers.h"
39 #include "gtktrayicon.h"
40 
41 #include "gtkprivate.h"
42 #include "gtkwidget.h"
43 #include "gtktooltip.h"
44 
45 #ifdef GDK_WINDOWING_X11
46 #include "gdk/x11/gdkx.h"
47 #endif
48 
49 #ifdef GDK_WINDOWING_WIN32
50 #include "gtkicontheme.h"
51 #include "gtklabel.h"
52 
53 #include "win32/gdkwin32.h"
54 #define WM_GTK_TRAY_NOTIFICATION (WM_USER+1)
55 #endif
56 
57 #ifdef GDK_WINDOWING_QUARTZ
58 #include "gtkicontheme.h"
59 #include "gtklabel.h"
60 #endif
61 
62 #include "gdkkeysyms.h"
63 
64 #include "gtkalias.h"
65 
66 #define BLINK_TIMEOUT 500
67 
68 enum
69 {
70   PROP_0,
71   PROP_PIXBUF,
72   PROP_FILE,
73   PROP_STOCK,
74   PROP_ICON_NAME,
75   PROP_GICON,
76   PROP_STORAGE_TYPE,
77   PROP_SIZE,
78   PROP_SCREEN,
79   PROP_VISIBLE,
80   PROP_ORIENTATION,
81   PROP_EMBEDDED,
82   PROP_BLINKING,
83   PROP_HAS_TOOLTIP,
84   PROP_TOOLTIP_TEXT,
85   PROP_TOOLTIP_MARKUP,
86   PROP_TITLE
87 };
88 
89 enum
90 {
91   ACTIVATE_SIGNAL,
92   POPUP_MENU_SIGNAL,
93   SIZE_CHANGED_SIGNAL,
94   BUTTON_PRESS_EVENT_SIGNAL,
95   BUTTON_RELEASE_EVENT_SIGNAL,
96   SCROLL_EVENT_SIGNAL,
97   QUERY_TOOLTIP_SIGNAL,
98   LAST_SIGNAL
99 };
100 
101 static guint status_icon_signals [LAST_SIGNAL] = { 0 };
102 
103 #ifdef GDK_WINDOWING_QUARTZ
104 #include "gtkstatusicon-quartz.c"
105 #endif
106 
107 struct _GtkStatusIconPrivate
108 {
109 #ifdef GDK_WINDOWING_X11
110   GtkWidget    *tray_icon;
111   GtkWidget    *image;
112 #endif
113 
114 #ifdef GDK_WINDOWING_WIN32
115   GtkWidget     *dummy_widget;
116   NOTIFYICONDATAW nid;
117   gint          taskbar_top;
118   gint		last_click_x, last_click_y;
119   GtkOrientation orientation;
120   gchar         *tooltip_text;
121   gchar         *title;
122 #endif
123 
124 #ifdef GDK_WINDOWING_QUARTZ
125   GtkWidget     *dummy_widget;
126   GtkQuartzStatusIcon *status_item;
127   gchar         *tooltip_text;
128   gchar         *title;
129 #endif
130 
131   gint          size;
132 
133   gint          image_width;
134   gint          image_height;
135 
136   GtkImageType  storage_type;
137 
138   union
139     {
140       GdkPixbuf *pixbuf;
141       gchar     *stock_id;
142       gchar     *icon_name;
143       GIcon     *gicon;
144     } image_data;
145 
146   GdkPixbuf    *blank_icon;
147   guint         blinking_timeout;
148 
149   guint         blinking : 1;
150   guint         blink_off : 1;
151   guint         visible : 1;
152 };
153 
154 static GObject* gtk_status_icon_constructor      (GType                  type,
155                                                   guint                  n_construct_properties,
156                                                   GObjectConstructParam *construct_params);
157 static void     gtk_status_icon_finalize         (GObject        *object);
158 static void     gtk_status_icon_set_property     (GObject        *object,
159 						  guint           prop_id,
160 						  const GValue   *value,
161 						  GParamSpec     *pspec);
162 static void     gtk_status_icon_get_property     (GObject        *object,
163 						  guint           prop_id,
164 						  GValue         *value,
165 					          GParamSpec     *pspec);
166 
167 #ifdef GDK_WINDOWING_X11
168 static void     gtk_status_icon_size_allocate    (GtkStatusIcon  *status_icon,
169 						  GtkAllocation  *allocation);
170 static void     gtk_status_icon_screen_changed   (GtkStatusIcon  *status_icon,
171 						  GdkScreen      *old_screen);
172 static void     gtk_status_icon_embedded_changed (GtkStatusIcon *status_icon);
173 static void     gtk_status_icon_orientation_changed (GtkStatusIcon *status_icon);
174 static gboolean gtk_status_icon_scroll           (GtkStatusIcon  *status_icon,
175 						  GdkEventScroll *event);
176 static gboolean gtk_status_icon_query_tooltip    (GtkStatusIcon *status_icon,
177 						  gint           x,
178 						  gint           y,
179 						  gboolean       keyboard_tip,
180 						  GtkTooltip    *tooltip);
181 
182 static gboolean gtk_status_icon_key_press        (GtkStatusIcon  *status_icon,
183 						  GdkEventKey    *event);
184 static void     gtk_status_icon_popup_menu       (GtkStatusIcon  *status_icon);
185 #endif
186 static gboolean gtk_status_icon_button_press     (GtkStatusIcon  *status_icon,
187 						  GdkEventButton *event);
188 static gboolean gtk_status_icon_button_release   (GtkStatusIcon  *status_icon,
189 						  GdkEventButton *event);
190 static void     gtk_status_icon_disable_blinking (GtkStatusIcon  *status_icon);
191 static void     gtk_status_icon_reset_image_data (GtkStatusIcon  *status_icon);
192 static void     gtk_status_icon_update_image    (GtkStatusIcon *status_icon);
193 
G_DEFINE_TYPE(GtkStatusIcon,gtk_status_icon,G_TYPE_OBJECT)194 G_DEFINE_TYPE (GtkStatusIcon, gtk_status_icon, G_TYPE_OBJECT)
195 
196 static void
197 gtk_status_icon_class_init (GtkStatusIconClass *class)
198 {
199   GObjectClass *gobject_class = (GObjectClass *) class;
200 
201   gobject_class->constructor  = gtk_status_icon_constructor;
202   gobject_class->finalize     = gtk_status_icon_finalize;
203   gobject_class->set_property = gtk_status_icon_set_property;
204   gobject_class->get_property = gtk_status_icon_get_property;
205 
206   class->button_press_event   = NULL;
207   class->button_release_event = NULL;
208   class->scroll_event         = NULL;
209   class->query_tooltip        = NULL;
210 
211   g_object_class_install_property (gobject_class,
212 				   PROP_PIXBUF,
213 				   g_param_spec_object ("pixbuf",
214 							P_("Pixbuf"),
215 							P_("A GdkPixbuf to display"),
216 							GDK_TYPE_PIXBUF,
217 							GTK_PARAM_READWRITE));
218 
219   g_object_class_install_property (gobject_class,
220 				   PROP_FILE,
221 				   g_param_spec_string ("file",
222 							P_("Filename"),
223 							P_("Filename to load and display"),
224 							NULL,
225 							GTK_PARAM_WRITABLE));
226 
227   g_object_class_install_property (gobject_class,
228 				   PROP_STOCK,
229 				   g_param_spec_string ("stock",
230 							P_("Stock ID"),
231 							P_("Stock ID for a stock image to display"),
232 							NULL,
233 							GTK_PARAM_READWRITE));
234 
235   g_object_class_install_property (gobject_class,
236                                    PROP_ICON_NAME,
237                                    g_param_spec_string ("icon-name",
238                                                         P_("Icon Name"),
239                                                         P_("The name of the icon from the icon theme"),
240                                                         NULL,
241                                                         GTK_PARAM_READWRITE));
242 
243   /**
244    * GtkStatusIcon:gicon:
245    *
246    * The #GIcon displayed in the #GtkStatusIcon. For themed icons,
247    * the image will be updated automatically if the theme changes.
248    *
249    * Since: 2.14
250    */
251   g_object_class_install_property (gobject_class,
252                                    PROP_GICON,
253                                    g_param_spec_object ("gicon",
254                                                         P_("GIcon"),
255                                                         P_("The GIcon being displayed"),
256                                                         G_TYPE_ICON,
257                                                         GTK_PARAM_READWRITE));
258 
259   g_object_class_install_property (gobject_class,
260 				   PROP_STORAGE_TYPE,
261 				   g_param_spec_enum ("storage-type",
262 						      P_("Storage type"),
263 						      P_("The representation being used for image data"),
264 						      GTK_TYPE_IMAGE_TYPE,
265 						      GTK_IMAGE_EMPTY,
266 						      GTK_PARAM_READABLE));
267 
268   g_object_class_install_property (gobject_class,
269 				   PROP_SIZE,
270 				   g_param_spec_int ("size",
271 						     P_("Size"),
272 						     P_("The size of the icon"),
273 						     0,
274 						     G_MAXINT,
275 						     0,
276 						     GTK_PARAM_READABLE));
277 
278   g_object_class_install_property (gobject_class,
279 				   PROP_SCREEN,
280 				   g_param_spec_object ("screen",
281  							P_("Screen"),
282  							P_("The screen where this status icon will be displayed"),
283 							GDK_TYPE_SCREEN,
284  							GTK_PARAM_READWRITE));
285 
286   /**
287    * GtkStatusIcon:blinking:
288    *
289    * Whether or not the status icon is blinking.
290    *
291    * Deprecated: 2.22: This property will be removed in GTK+ 3
292    */
293   g_object_class_install_property (gobject_class,
294 				   PROP_BLINKING,
295 				   g_param_spec_boolean ("blinking",
296 							 P_("Blinking"),
297 							 P_("Whether or not the status icon is blinking"),
298 							 FALSE,
299 							 GTK_PARAM_READWRITE | G_PARAM_DEPRECATED));
300 
301   g_object_class_install_property (gobject_class,
302 				   PROP_VISIBLE,
303 				   g_param_spec_boolean ("visible",
304 							 P_("Visible"),
305 							 P_("Whether or not the status icon is visible"),
306 							 TRUE,
307 							 GTK_PARAM_READWRITE));
308 
309 
310   /**
311    * GtkStatusIcon:embedded:
312    *
313    * %TRUE if the statusicon is embedded in a notification area.
314    *
315    * Since: 2.12
316    */
317   g_object_class_install_property (gobject_class,
318 				   PROP_EMBEDDED,
319 				   g_param_spec_boolean ("embedded",
320 							 P_("Embedded"),
321 							 P_("Whether or not the status icon is embedded"),
322 							 FALSE,
323 							 GTK_PARAM_READABLE));
324 
325   /**
326    * GtkStatusIcon:orientation:
327    *
328    * The orientation of the tray in which the statusicon
329    * is embedded.
330    *
331    * Since: 2.12
332    */
333   g_object_class_install_property (gobject_class,
334 				   PROP_ORIENTATION,
335 				   g_param_spec_enum ("orientation",
336 						      P_("Orientation"),
337 						      P_("The orientation of the tray"),
338 						      GTK_TYPE_ORIENTATION,
339 						      GTK_ORIENTATION_HORIZONTAL,
340 						      GTK_PARAM_READABLE));
341 
342 /**
343  * GtkStatusIcon:has-tooltip:
344  *
345  * Enables or disables the emission of #GtkStatusIcon::query-tooltip on
346  * @status_icon.  A value of %TRUE indicates that @status_icon can have a
347  * tooltip, in this case the status icon will be queried using
348  * #GtkStatusIcon::query-tooltip to determine whether it will provide a
349  * tooltip or not.
350  *
351  * Note that setting this property to %TRUE for the first time will change
352  * the event masks of the windows of this status icon to include leave-notify
353  * and motion-notify events. This will not be undone when the property is set
354  * to %FALSE again.
355  *
356  * Whether this property is respected is platform dependent.
357  * For plain text tooltips, use #GtkStatusIcon:tooltip-text in preference.
358  *
359  * Since: 2.16
360  */
361   g_object_class_install_property (gobject_class,
362 				   PROP_HAS_TOOLTIP,
363 				   g_param_spec_boolean ("has-tooltip",
364  							 P_("Has tooltip"),
365  							 P_("Whether this tray icon has a tooltip"),
366  							 FALSE,
367  							 GTK_PARAM_READWRITE));
368   /**
369    * GtkStatusIcon:tooltip-text:
370    *
371    * Sets the text of tooltip to be the given string.
372    *
373    * Also see gtk_tooltip_set_text().
374    *
375    * This is a convenience property which will take care of getting the
376    * tooltip shown if the given string is not %NULL.
377    * #GtkStatusIcon:has-tooltip will automatically be set to %TRUE and
378    * the default handler for the #GtkStatusIcon::query-tooltip signal
379    * will take care of displaying the tooltip.
380    *
381    * Note that some platforms have limitations on the length of tooltips
382    * that they allow on status icons, e.g. Windows only shows the first
383    * 64 characters.
384    *
385    * Since: 2.16
386    */
387   g_object_class_install_property (gobject_class,
388                                    PROP_TOOLTIP_TEXT,
389                                    g_param_spec_string ("tooltip-text",
390                                                         P_("Tooltip Text"),
391                                                         P_("The contents of the tooltip for this widget"),
392                                                         NULL,
393                                                         GTK_PARAM_READWRITE));
394   /**
395    * GtkStatusIcon:tooltip-markup:
396    *
397    * Sets the text of tooltip to be the given string, which is marked up
398    * with the <link linkend="PangoMarkupFormat">Pango text markup
399    * language</link>. Also see gtk_tooltip_set_markup().
400    *
401    * This is a convenience property which will take care of getting the
402    * tooltip shown if the given string is not %NULL.
403    * #GtkStatusIcon:has-tooltip will automatically be set to %TRUE and
404    * the default handler for the #GtkStatusIcon::query-tooltip signal
405    * will take care of displaying the tooltip.
406    *
407    * On some platforms, embedded markup will be ignored.
408    *
409    * Since: 2.16
410    */
411   g_object_class_install_property (gobject_class,
412 				   PROP_TOOLTIP_MARKUP,
413 				   g_param_spec_string ("tooltip-markup",
414  							P_("Tooltip markup"),
415 							P_("The contents of the tooltip for this tray icon"),
416 							NULL,
417 							GTK_PARAM_READWRITE));
418 
419 
420   /**
421    * GtkStatusIcon:title:
422    *
423    * The title of this tray icon. This should be a short, human-readable,
424    * localized string describing the tray icon. It may be used by tools
425    * like screen readers to render the tray icon.
426    *
427    * Since: 2.18
428    */
429   g_object_class_install_property (gobject_class,
430                                    PROP_TITLE,
431                                    g_param_spec_string ("title",
432                                                         P_("Title"),
433                                                         P_("The title of this tray icon"),
434                                                         NULL,
435                                                         GTK_PARAM_READWRITE));
436 
437   /**
438    * GtkStatusIcon::activate:
439    * @status_icon: the object which received the signal
440    *
441    * Gets emitted when the user activates the status icon.
442    * If and how status icons can activated is platform-dependent.
443    *
444    * Unlike most G_SIGNAL_ACTION signals, this signal is meant to
445    * be used by applications and should be wrapped by language bindings.
446    *
447    * Since: 2.10
448    */
449   status_icon_signals [ACTIVATE_SIGNAL] =
450     g_signal_new (I_("activate"),
451 		  G_TYPE_FROM_CLASS (gobject_class),
452 		  G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
453 		  G_STRUCT_OFFSET (GtkStatusIconClass, activate),
454 		  NULL,
455 		  NULL,
456 		  g_cclosure_marshal_VOID__VOID,
457 		  G_TYPE_NONE,
458 		  0);
459 
460   /**
461    * GtkStatusIcon::popup-menu:
462    * @status_icon: the object which received the signal
463    * @button: the button that was pressed, or 0 if the
464    *   signal is not emitted in response to a button press event
465    * @activate_time: the timestamp of the event that
466    *   triggered the signal emission
467    *
468    * Gets emitted when the user brings up the context menu
469    * of the status icon. Whether status icons can have context
470    * menus and how these are activated is platform-dependent.
471    *
472    * The @button and @activate_time parameters should be
473    * passed as the last to arguments to gtk_menu_popup().
474    *
475    * Unlike most G_SIGNAL_ACTION signals, this signal is meant to
476    * be used by applications and should be wrapped by language bindings.
477    *
478    * Since: 2.10
479    */
480   status_icon_signals [POPUP_MENU_SIGNAL] =
481     g_signal_new (I_("popup-menu"),
482 		  G_TYPE_FROM_CLASS (gobject_class),
483 		  G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
484 		  G_STRUCT_OFFSET (GtkStatusIconClass, popup_menu),
485 		  NULL,
486 		  NULL,
487 		  _gtk_marshal_VOID__UINT_UINT,
488 		  G_TYPE_NONE,
489 		  2,
490 		  G_TYPE_UINT,
491 		  G_TYPE_UINT);
492 
493   /**
494    * GtkStatusIcon::size-changed:
495    * @status_icon: the object which received the signal
496    * @size: the new size
497    *
498    * Gets emitted when the size available for the image
499    * changes, e.g. because the notification area got resized.
500    *
501    * Return value: %TRUE if the icon was updated for the new
502    * size. Otherwise, GTK+ will scale the icon as necessary.
503    *
504    * Since: 2.10
505    */
506   status_icon_signals [SIZE_CHANGED_SIGNAL] =
507     g_signal_new (I_("size-changed"),
508 		  G_TYPE_FROM_CLASS (gobject_class),
509 		  G_SIGNAL_RUN_LAST,
510 		  G_STRUCT_OFFSET (GtkStatusIconClass, size_changed),
511 		  g_signal_accumulator_true_handled,
512 		  NULL,
513 		  _gtk_marshal_BOOLEAN__INT,
514 		  G_TYPE_BOOLEAN,
515 		  1,
516 		  G_TYPE_INT);
517 
518   /**
519    * GtkStatusIcon::button-press-event:
520    * @status_icon: the object which received the signal
521    * @event: the #GdkEventButton which triggered this signal
522    *
523    * The ::button-press-event signal will be emitted when a button
524    * (typically from a mouse) is pressed.
525    *
526    * Whether this event is emitted is platform-dependent.  Use the ::activate
527    * and ::popup-menu signals in preference.
528    *
529    * Return value: %TRUE to stop other handlers from being invoked
530    * for the event. %FALSE to propagate the event further.
531    *
532    * Since: 2.14
533    */
534   status_icon_signals [BUTTON_PRESS_EVENT_SIGNAL] =
535     g_signal_new (I_("button_press_event"),
536 		  G_TYPE_FROM_CLASS (gobject_class),
537 		  G_SIGNAL_RUN_LAST,
538 		  G_STRUCT_OFFSET (GtkStatusIconClass, button_press_event),
539 		  g_signal_accumulator_true_handled, NULL,
540 		  _gtk_marshal_BOOLEAN__BOXED,
541 		  G_TYPE_BOOLEAN, 1,
542 		  GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
543 
544   /**
545    * GtkStatusIcon::button-release-event:
546    * @status_icon: the object which received the signal
547    * @event: the #GdkEventButton which triggered this signal
548    *
549    * The ::button-release-event signal will be emitted when a button
550    * (typically from a mouse) is released.
551    *
552    * Whether this event is emitted is platform-dependent.  Use the ::activate
553    * and ::popup-menu signals in preference.
554    *
555    * Return value: %TRUE to stop other handlers from being invoked
556    * for the event. %FALSE to propagate the event further.
557    *
558    * Since: 2.14
559    */
560   status_icon_signals [BUTTON_RELEASE_EVENT_SIGNAL] =
561     g_signal_new (I_("button_release_event"),
562 		  G_TYPE_FROM_CLASS (gobject_class),
563 		  G_SIGNAL_RUN_LAST,
564 		  G_STRUCT_OFFSET (GtkStatusIconClass, button_release_event),
565 		  g_signal_accumulator_true_handled, NULL,
566 		  _gtk_marshal_BOOLEAN__BOXED,
567 		  G_TYPE_BOOLEAN, 1,
568 		  GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
569 
570   /**
571    * GtkStatusIcon::scroll-event:
572    * @status_icon: the object which received the signal.
573    * @event: the #GdkEventScroll which triggered this signal
574    *
575    * The ::scroll-event signal is emitted when a button in the 4 to 7
576    * range is pressed. Wheel mice are usually configured to generate
577    * button press events for buttons 4 and 5 when the wheel is turned.
578    *
579    * Whether this event is emitted is platform-dependent.
580    *
581    * Returns: %TRUE to stop other handlers from being invoked for the event.
582    *   %FALSE to propagate the event further.
583    */
584   status_icon_signals[SCROLL_EVENT_SIGNAL] =
585     g_signal_new (I_("scroll_event"),
586 		  G_TYPE_FROM_CLASS (gobject_class),
587 		  G_SIGNAL_RUN_LAST,
588 		  G_STRUCT_OFFSET (GtkStatusIconClass, scroll_event),
589 		  g_signal_accumulator_true_handled, NULL,
590 		  _gtk_marshal_BOOLEAN__BOXED,
591 		  G_TYPE_BOOLEAN, 1,
592 		  GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
593 
594   /**
595    * GtkStatusIcon::query-tooltip:
596    * @status_icon: the object which received the signal
597    * @x: the x coordinate of the cursor position where the request has been
598    *     emitted, relative to @status_icon
599    * @y: the y coordinate of the cursor position where the request has been
600    *     emitted, relative to @status_icon
601    * @keyboard_mode: %TRUE if the tooltip was trigged using the keyboard
602    * @tooltip: a #GtkTooltip
603    *
604    * Emitted when the #GtkSettings:gtk-tooltip-timeout has expired with the
605    * cursor hovering above @status_icon; or emitted when @status_icon got
606    * focus in keyboard mode.
607    *
608    * Using the given coordinates, the signal handler should determine
609    * whether a tooltip should be shown for @status_icon. If this is
610    * the case %TRUE should be returned, %FALSE otherwise. Note that if
611    * @keyboard_mode is %TRUE, the values of @x and @y are undefined and
612    * should not be used.
613    *
614    * The signal handler is free to manipulate @tooltip with the therefore
615    * destined function calls.
616    *
617    * Whether this signal is emitted is platform-dependent.
618    * For plain text tooltips, use #GtkStatusIcon:tooltip-text in preference.
619    *
620    * Returns: %TRUE if @tooltip should be shown right now, %FALSE otherwise.
621    *
622    * Since: 2.16
623    */
624   status_icon_signals [QUERY_TOOLTIP_SIGNAL] =
625     g_signal_new (I_("query_tooltip"),
626 		  G_TYPE_FROM_CLASS (gobject_class),
627 		  G_SIGNAL_RUN_LAST,
628 		  G_STRUCT_OFFSET (GtkStatusIconClass, query_tooltip),
629 		  g_signal_accumulator_true_handled, NULL,
630 		  _gtk_marshal_BOOLEAN__INT_INT_BOOLEAN_OBJECT,
631 		  G_TYPE_BOOLEAN, 4,
632 		  G_TYPE_INT,
633 		  G_TYPE_INT,
634 		  G_TYPE_BOOLEAN,
635 		  GTK_TYPE_TOOLTIP);
636 
637   g_type_class_add_private (class, sizeof (GtkStatusIconPrivate));
638 }
639 
640 #ifdef GDK_WINDOWING_WIN32
641 
642 static void
build_button_event(GtkStatusIconPrivate * priv,GdkEventButton * e,guint button)643 build_button_event (GtkStatusIconPrivate *priv,
644 		    GdkEventButton       *e,
645 		    guint                 button)
646 {
647   POINT pos;
648   GdkRectangle monitor0;
649 
650   /* We know that gdk/win32 puts the primary monitor at index 0 */
651   gdk_screen_get_monitor_geometry (gdk_screen_get_default (), 0, &monitor0);
652   e->window = g_object_ref (gdk_get_default_root_window ());
653   e->send_event = TRUE;
654   e->time = GetTickCount ();
655   GetCursorPos (&pos);
656   priv->last_click_x = e->x = pos.x + monitor0.x;
657   priv->last_click_y = e->y = pos.y + monitor0.y;
658   e->axes = NULL;
659   e->state = 0;
660   e->button = button;
661   e->device = gdk_display_get_default ()->core_pointer;
662   e->x_root = e->x;
663   e->y_root = e->y;
664 }
665 
666 typedef struct
667 {
668   GtkStatusIcon *status_icon;
669   GdkEventButton *event;
670 } ButtonCallbackData;
671 
672 static gboolean
button_callback(gpointer data)673 button_callback (gpointer data)
674 {
675   ButtonCallbackData *bc = (ButtonCallbackData *) data;
676 
677   if (bc->event->type == GDK_BUTTON_PRESS)
678     gtk_status_icon_button_press (bc->status_icon, bc->event);
679   else
680     gtk_status_icon_button_release (bc->status_icon, bc->event);
681 
682   gdk_event_free ((GdkEvent *) bc->event);
683   g_free (data);
684 
685   return FALSE;
686 }
687 
688 static UINT taskbar_created_msg = 0;
689 static GSList *status_icons = NULL;
690 static UINT status_icon_id = 0;
691 
692 static GtkStatusIcon *
find_status_icon(UINT id)693 find_status_icon (UINT id)
694 {
695   GSList *rover;
696 
697   for (rover = status_icons; rover != NULL; rover = rover->next)
698     {
699       GtkStatusIcon *status_icon = GTK_STATUS_ICON (rover->data);
700       GtkStatusIconPrivate *priv = status_icon->priv;
701 
702       if (priv->nid.uID == id)
703         return status_icon;
704     }
705 
706   return NULL;
707 }
708 
709 static LRESULT CALLBACK
wndproc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)710 wndproc (HWND   hwnd,
711 	 UINT   message,
712 	 WPARAM wparam,
713 	 LPARAM lparam)
714 {
715   if (message == taskbar_created_msg)
716     {
717       GSList *rover;
718 
719       for (rover = status_icons; rover != NULL; rover = rover->next)
720 	{
721 	  GtkStatusIcon *status_icon = GTK_STATUS_ICON (rover->data);
722 	  GtkStatusIconPrivate *priv = status_icon->priv;
723 
724 	  /* taskbar_created_msg is also fired when DPI changes. Try to delete existing icons if possible. */
725 	  if (!Shell_NotifyIconW (NIM_DELETE, &priv->nid))
726 	  {
727 		g_warning (G_STRLOC ": Shell_NotifyIcon(NIM_DELETE) on existing icon failed");
728 	  }
729 
730 	  priv->nid.hWnd = hwnd;
731 	  priv->nid.uID = status_icon_id++;
732 	  priv->nid.uCallbackMessage = WM_GTK_TRAY_NOTIFICATION;
733 	  priv->nid.uFlags = NIF_MESSAGE;
734 
735 	  if (!Shell_NotifyIconW (NIM_ADD, &priv->nid))
736 	    {
737 	      g_warning (G_STRLOC ": Shell_NotifyIcon(NIM_ADD) failed");
738 	      priv->nid.hWnd = NULL;
739 	      continue;
740 	    }
741 
742 	  gtk_status_icon_update_image (status_icon);
743 	}
744       return 0;
745     }
746 
747   if (message == WM_GTK_TRAY_NOTIFICATION)
748     {
749       ButtonCallbackData *bc;
750       guint button;
751 
752       switch (lparam)
753 	{
754 	case WM_LBUTTONDOWN:
755 	  button = 1;
756 	  goto buttondown0;
757 
758 	case WM_MBUTTONDOWN:
759 	  button = 2;
760 	  goto buttondown0;
761 
762 	case WM_RBUTTONDOWN:
763 	  button = 3;
764 	  goto buttondown0;
765 
766 	case WM_XBUTTONDOWN:
767 	  if (HIWORD (wparam) == XBUTTON1)
768 	    button = 4;
769 	  else
770 	    button = 5;
771 
772 	buttondown0:
773 	  bc = g_new (ButtonCallbackData, 1);
774 	  bc->event = (GdkEventButton *) gdk_event_new (GDK_BUTTON_PRESS);
775 	  bc->status_icon = find_status_icon (wparam);
776 	  build_button_event (bc->status_icon->priv, bc->event, button);
777 	  g_idle_add (button_callback, bc);
778 	  break;
779 
780 	case WM_LBUTTONUP:
781 	  button = 1;
782 	  goto buttonup0;
783 
784 	case WM_MBUTTONUP:
785 	  button = 2;
786 	  goto buttonup0;
787 
788 	case WM_RBUTTONUP:
789 	  button = 3;
790 	  goto buttonup0;
791 
792 	case WM_XBUTTONUP:
793 	  if (HIWORD (wparam) == XBUTTON1)
794 	    button = 4;
795 	  else
796 	    button = 5;
797 
798 	buttonup0:
799 	  bc = g_new (ButtonCallbackData, 1);
800 	  bc->event = (GdkEventButton *) gdk_event_new (GDK_BUTTON_RELEASE);
801 	  bc->status_icon = find_status_icon (wparam);
802 	  build_button_event (bc->status_icon->priv, bc->event, button);
803 	  g_idle_add (button_callback, bc);
804 	  break;
805 
806 	default :
807 	  break;
808 	}
809 	return 0;
810     }
811   else
812     {
813       return DefWindowProc (hwnd, message, wparam, lparam);
814     }
815 }
816 
817 static HWND
create_tray_observer(void)818 create_tray_observer (void)
819 {
820   WNDCLASS    wclass;
821   static HWND hwnd = NULL;
822   ATOM        klass;
823   HINSTANCE   hmodule = GetModuleHandle (NULL);
824 
825   if (hwnd)
826     return hwnd;
827 
828   taskbar_created_msg = RegisterWindowMessage("TaskbarCreated");
829 
830   memset (&wclass, 0, sizeof(WNDCLASS));
831   wclass.lpszClassName = "gtkstatusicon-observer";
832   wclass.lpfnWndProc   = wndproc;
833   wclass.hInstance     = hmodule;
834 
835   klass = RegisterClass (&wclass);
836   if (!klass)
837     return NULL;
838 
839   hwnd = CreateWindow (MAKEINTRESOURCE (klass),
840                        NULL, WS_POPUP,
841                        0, 0, 1, 1, NULL, NULL,
842                        hmodule, NULL);
843   if (!hwnd)
844     {
845       UnregisterClass (MAKEINTRESOURCE(klass), hmodule);
846       return NULL;
847     }
848 
849   return hwnd;
850 }
851 
852 #endif
853 
854 static void
gtk_status_icon_init(GtkStatusIcon * status_icon)855 gtk_status_icon_init (GtkStatusIcon *status_icon)
856 {
857   GtkStatusIconPrivate *priv;
858 
859   priv = G_TYPE_INSTANCE_GET_PRIVATE (status_icon, GTK_TYPE_STATUS_ICON,
860 				      GtkStatusIconPrivate);
861   status_icon->priv = priv;
862 
863   priv->storage_type = GTK_IMAGE_EMPTY;
864   priv->visible      = TRUE;
865 
866 #ifdef GDK_WINDOWING_X11
867   priv->size         = 0;
868   priv->image_width  = 0;
869   priv->image_height = 0;
870 
871   priv->tray_icon = GTK_WIDGET (_gtk_tray_icon_new (NULL));
872 
873   gtk_widget_add_events (GTK_WIDGET (priv->tray_icon),
874 			 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
875 			 GDK_SCROLL_MASK);
876 
877   g_signal_connect_swapped (priv->tray_icon, "key-press-event",
878 			    G_CALLBACK (gtk_status_icon_key_press), status_icon);
879   g_signal_connect_swapped (priv->tray_icon, "popup-menu",
880 			    G_CALLBACK (gtk_status_icon_popup_menu), status_icon);
881   g_signal_connect_swapped (priv->tray_icon, "notify::embedded",
882 			    G_CALLBACK (gtk_status_icon_embedded_changed), status_icon);
883   g_signal_connect_swapped (priv->tray_icon, "notify::orientation",
884 			    G_CALLBACK (gtk_status_icon_orientation_changed), status_icon);
885   g_signal_connect_swapped (priv->tray_icon, "button-press-event",
886 			    G_CALLBACK (gtk_status_icon_button_press), status_icon);
887   g_signal_connect_swapped (priv->tray_icon, "button-release-event",
888 			    G_CALLBACK (gtk_status_icon_button_release), status_icon);
889   g_signal_connect_swapped (priv->tray_icon, "scroll-event",
890 			    G_CALLBACK (gtk_status_icon_scroll), status_icon);
891   g_signal_connect_swapped (priv->tray_icon, "query-tooltip",
892 			    G_CALLBACK (gtk_status_icon_query_tooltip), status_icon);
893   g_signal_connect_swapped (priv->tray_icon, "screen-changed",
894 		    	    G_CALLBACK (gtk_status_icon_screen_changed), status_icon);
895   priv->image = gtk_image_new ();
896   gtk_widget_set_can_focus (priv->image, TRUE);
897   gtk_container_add (GTK_CONTAINER (priv->tray_icon), priv->image);
898   gtk_widget_show (priv->image);
899 
900   g_signal_connect_swapped (priv->image, "size-allocate",
901 			    G_CALLBACK (gtk_status_icon_size_allocate), status_icon);
902 
903 #endif
904 
905 #ifdef GDK_WINDOWING_WIN32
906 
907   /* Get position and orientation of Windows taskbar. */
908   {
909     APPBARDATA abd;
910 
911     abd.cbSize = sizeof (abd);
912     SHAppBarMessage (ABM_GETTASKBARPOS, &abd);
913     if (abd.rc.bottom - abd.rc.top > abd.rc.right - abd.rc.left)
914       priv->orientation = GTK_ORIENTATION_VERTICAL;
915     else
916       priv->orientation = GTK_ORIENTATION_HORIZONTAL;
917 
918     priv->taskbar_top = abd.rc.top;
919   }
920 
921   priv->last_click_x = priv->last_click_y = 0;
922 
923   /* Are the system tray icons always 16 pixels square? */
924   priv->size         = 16;
925   priv->image_width  = 16;
926   priv->image_height = 16;
927 
928   priv->dummy_widget = gtk_label_new ("");
929 
930   memset (&priv->nid, 0, sizeof (priv->nid));
931 
932   priv->nid.hWnd = create_tray_observer ();
933   priv->nid.uID = status_icon_id++;
934   priv->nid.uCallbackMessage = WM_GTK_TRAY_NOTIFICATION;
935   priv->nid.uFlags = NIF_MESSAGE;
936 
937   /* To help win7 identify the icon create it with an application "unique" tip */
938   if (g_get_prgname ())
939   {
940     WCHAR *wcs = g_utf8_to_utf16 (g_get_prgname (), -1, NULL, NULL, NULL);
941 
942     priv->nid.uFlags |= NIF_TIP;
943     wcsncpy (priv->nid.szTip, wcs, G_N_ELEMENTS (priv->nid.szTip) - 1);
944     priv->nid.szTip[G_N_ELEMENTS (priv->nid.szTip) - 1] = 0;
945     g_free (wcs);
946   }
947 
948   if (!Shell_NotifyIconW (NIM_ADD, &priv->nid))
949     {
950       g_warning (G_STRLOC ": Shell_NotifyIcon(NIM_ADD) failed");
951       priv->nid.hWnd = NULL;
952     }
953 
954   status_icons = g_slist_append (status_icons, status_icon);
955 
956 #endif
957 
958 #ifdef GDK_WINDOWING_QUARTZ
959   priv->dummy_widget = gtk_label_new ("");
960 
961   QUARTZ_POOL_ALLOC;
962 
963   priv->status_item = [[GtkQuartzStatusIcon alloc] initWithStatusIcon:status_icon];
964 
965   priv->image_width = priv->image_height = [priv->status_item getHeight];
966   priv->size = priv->image_height;
967 
968   QUARTZ_POOL_RELEASE;
969 
970 #endif
971 }
972 
973 static GObject*
gtk_status_icon_constructor(GType type,guint n_construct_properties,GObjectConstructParam * construct_params)974 gtk_status_icon_constructor (GType                  type,
975                              guint                  n_construct_properties,
976                              GObjectConstructParam *construct_params)
977 {
978   GObject *object;
979 #ifdef GDK_WINDOWING_X11
980   GtkStatusIcon *status_icon;
981   GtkStatusIconPrivate *priv;
982 #endif
983 
984   object = G_OBJECT_CLASS (gtk_status_icon_parent_class)->constructor (type,
985                                                                        n_construct_properties,
986                                                                        construct_params);
987 
988 #ifdef GDK_WINDOWING_X11
989   status_icon = GTK_STATUS_ICON (object);
990   priv = status_icon->priv;
991 
992   if (priv->visible)
993     gtk_widget_show (priv->tray_icon);
994 #endif
995 
996   return object;
997 }
998 
999 static void
gtk_status_icon_finalize(GObject * object)1000 gtk_status_icon_finalize (GObject *object)
1001 {
1002   GtkStatusIcon *status_icon = GTK_STATUS_ICON (object);
1003   GtkStatusIconPrivate *priv = status_icon->priv;
1004 
1005   gtk_status_icon_disable_blinking (status_icon);
1006 
1007   gtk_status_icon_reset_image_data (status_icon);
1008 
1009   if (priv->blank_icon)
1010     g_object_unref (priv->blank_icon);
1011   priv->blank_icon = NULL;
1012 
1013 #ifdef GDK_WINDOWING_X11
1014   g_signal_handlers_disconnect_by_func (priv->tray_icon,
1015 			                gtk_status_icon_key_press, status_icon);
1016   g_signal_handlers_disconnect_by_func (priv->tray_icon,
1017 			                gtk_status_icon_popup_menu, status_icon);
1018   g_signal_handlers_disconnect_by_func (priv->tray_icon,
1019 			                gtk_status_icon_embedded_changed, status_icon);
1020   g_signal_handlers_disconnect_by_func (priv->tray_icon,
1021 			                gtk_status_icon_orientation_changed, status_icon);
1022   g_signal_handlers_disconnect_by_func (priv->tray_icon,
1023 			                gtk_status_icon_button_press, status_icon);
1024   g_signal_handlers_disconnect_by_func (priv->tray_icon,
1025 			                gtk_status_icon_button_release, status_icon);
1026   g_signal_handlers_disconnect_by_func (priv->tray_icon,
1027 			                gtk_status_icon_scroll, status_icon);
1028   g_signal_handlers_disconnect_by_func (priv->tray_icon,
1029 			                gtk_status_icon_query_tooltip, status_icon);
1030   g_signal_handlers_disconnect_by_func (priv->tray_icon,
1031 		    	                gtk_status_icon_screen_changed, status_icon);
1032   gtk_widget_destroy (priv->image);
1033   gtk_widget_destroy (priv->tray_icon);
1034 #endif
1035 
1036 #ifdef GDK_WINDOWING_WIN32
1037   if (priv->nid.hWnd != NULL && priv->visible)
1038     Shell_NotifyIconW (NIM_DELETE, &priv->nid);
1039   if (priv->nid.hIcon)
1040     DestroyIcon (priv->nid.hIcon);
1041   g_free (priv->tooltip_text);
1042 
1043   gtk_widget_destroy (priv->dummy_widget);
1044 
1045   status_icons = g_slist_remove (status_icons, status_icon);
1046 #endif
1047 
1048 #ifdef GDK_WINDOWING_QUARTZ
1049   QUARTZ_POOL_ALLOC;
1050   [priv->status_item release];
1051   QUARTZ_POOL_RELEASE;
1052   g_free (priv->tooltip_text);
1053 #endif
1054 
1055   G_OBJECT_CLASS (gtk_status_icon_parent_class)->finalize (object);
1056 }
1057 
1058 static void
gtk_status_icon_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)1059 gtk_status_icon_set_property (GObject      *object,
1060 			      guint         prop_id,
1061 			      const GValue *value,
1062 			      GParamSpec   *pspec)
1063 {
1064   GtkStatusIcon *status_icon = GTK_STATUS_ICON (object);
1065 
1066   switch (prop_id)
1067     {
1068     case PROP_PIXBUF:
1069       gtk_status_icon_set_from_pixbuf (status_icon, g_value_get_object (value));
1070       break;
1071     case PROP_FILE:
1072       gtk_status_icon_set_from_file (status_icon, g_value_get_string (value));
1073       break;
1074     case PROP_STOCK:
1075       gtk_status_icon_set_from_stock (status_icon, g_value_get_string (value));
1076       break;
1077     case PROP_ICON_NAME:
1078       gtk_status_icon_set_from_icon_name (status_icon, g_value_get_string (value));
1079       break;
1080     case PROP_GICON:
1081       gtk_status_icon_set_from_gicon (status_icon, g_value_get_object (value));
1082       break;
1083     case PROP_SCREEN:
1084       gtk_status_icon_set_screen (status_icon, g_value_get_object (value));
1085       break;
1086     case PROP_BLINKING:
1087       gtk_status_icon_set_blinking (status_icon, g_value_get_boolean (value));
1088       break;
1089     case PROP_VISIBLE:
1090       gtk_status_icon_set_visible (status_icon, g_value_get_boolean (value));
1091       break;
1092     case PROP_HAS_TOOLTIP:
1093       gtk_status_icon_set_has_tooltip (status_icon, g_value_get_boolean (value));
1094       break;
1095     case PROP_TOOLTIP_TEXT:
1096       gtk_status_icon_set_tooltip_text (status_icon, g_value_get_string (value));
1097       break;
1098     case PROP_TOOLTIP_MARKUP:
1099       gtk_status_icon_set_tooltip_markup (status_icon, g_value_get_string (value));
1100       break;
1101     case PROP_TITLE:
1102       gtk_status_icon_set_title (status_icon, g_value_get_string (value));
1103       break;
1104     default:
1105       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1106       break;
1107     }
1108 }
1109 
1110 static void
gtk_status_icon_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)1111 gtk_status_icon_get_property (GObject    *object,
1112 			      guint       prop_id,
1113 			      GValue     *value,
1114 			      GParamSpec *pspec)
1115 {
1116   GtkStatusIcon *status_icon = GTK_STATUS_ICON (object);
1117   GtkStatusIconPrivate *priv = status_icon->priv;
1118 
1119   /* The "getter" functions whine if you try to get the wrong
1120    * storage type. This function is instead robust against that,
1121    * so that GUI builders don't have to jump through hoops
1122    * to avoid g_warning
1123    */
1124 
1125   switch (prop_id)
1126     {
1127     case PROP_PIXBUF:
1128       if (priv->storage_type != GTK_IMAGE_PIXBUF)
1129 	g_value_set_object (value, NULL);
1130       else
1131 	g_value_set_object (value, gtk_status_icon_get_pixbuf (status_icon));
1132       break;
1133     case PROP_STOCK:
1134       if (priv->storage_type != GTK_IMAGE_STOCK)
1135 	g_value_set_string (value, NULL);
1136       else
1137 	g_value_set_string (value, gtk_status_icon_get_stock (status_icon));
1138       break;
1139     case PROP_ICON_NAME:
1140       if (priv->storage_type != GTK_IMAGE_ICON_NAME)
1141 	g_value_set_string (value, NULL);
1142       else
1143 	g_value_set_string (value, gtk_status_icon_get_icon_name (status_icon));
1144       break;
1145     case PROP_GICON:
1146       if (priv->storage_type != GTK_IMAGE_GICON)
1147         g_value_set_object (value, NULL);
1148       else
1149         g_value_set_object (value, gtk_status_icon_get_gicon (status_icon));
1150       break;
1151     case PROP_STORAGE_TYPE:
1152       g_value_set_enum (value, gtk_status_icon_get_storage_type (status_icon));
1153       break;
1154     case PROP_SIZE:
1155       g_value_set_int (value, gtk_status_icon_get_size (status_icon));
1156       break;
1157     case PROP_SCREEN:
1158       g_value_set_object (value, gtk_status_icon_get_screen (status_icon));
1159       break;
1160     case PROP_BLINKING:
1161       g_value_set_boolean (value, gtk_status_icon_get_blinking (status_icon));
1162       break;
1163     case PROP_VISIBLE:
1164       g_value_set_boolean (value, gtk_status_icon_get_visible (status_icon));
1165       break;
1166     case PROP_EMBEDDED:
1167       g_value_set_boolean (value, gtk_status_icon_is_embedded (status_icon));
1168       break;
1169     case PROP_ORIENTATION:
1170 #ifdef GDK_WINDOWING_X11
1171       g_value_set_enum (value, _gtk_tray_icon_get_orientation (GTK_TRAY_ICON (status_icon->priv->tray_icon)));
1172 #endif
1173 #ifdef GDK_WINDOWING_WIN32
1174       g_value_set_enum (value, status_icon->priv->orientation);
1175 #endif
1176       break;
1177     case PROP_HAS_TOOLTIP:
1178       g_value_set_boolean (value, gtk_status_icon_get_has_tooltip (status_icon));
1179       break;
1180     case PROP_TOOLTIP_TEXT:
1181       g_value_set_string (value, gtk_status_icon_get_tooltip_text (status_icon));
1182       break;
1183     case PROP_TOOLTIP_MARKUP:
1184       g_value_set_string (value, gtk_status_icon_get_tooltip_markup (status_icon));
1185       break;
1186     case PROP_TITLE:
1187       g_value_set_string (value, gtk_status_icon_get_title (status_icon));
1188       break;
1189     default:
1190       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1191       break;
1192     }
1193 }
1194 
1195 /**
1196  * gtk_status_icon_new:
1197  *
1198  * Creates an empty status icon object.
1199  *
1200  * Return value: a new #GtkStatusIcon
1201  *
1202  * Since: 2.10
1203  **/
1204 GtkStatusIcon *
gtk_status_icon_new(void)1205 gtk_status_icon_new (void)
1206 {
1207   return g_object_new (GTK_TYPE_STATUS_ICON, NULL);
1208 }
1209 
1210 /**
1211  * gtk_status_icon_new_from_pixbuf:
1212  * @pixbuf: a #GdkPixbuf
1213  *
1214  * Creates a status icon displaying @pixbuf.
1215  *
1216  * The image will be scaled down to fit in the available
1217  * space in the notification area, if necessary.
1218  *
1219  * Return value: a new #GtkStatusIcon
1220  *
1221  * Since: 2.10
1222  **/
1223 GtkStatusIcon *
gtk_status_icon_new_from_pixbuf(GdkPixbuf * pixbuf)1224 gtk_status_icon_new_from_pixbuf (GdkPixbuf *pixbuf)
1225 {
1226   return g_object_new (GTK_TYPE_STATUS_ICON,
1227 		       "pixbuf", pixbuf,
1228 		       NULL);
1229 }
1230 
1231 /**
1232  * gtk_status_icon_new_from_file:
1233  * @filename: a filename
1234  *
1235  * Creates a status icon displaying the file @filename.
1236  *
1237  * The image will be scaled down to fit in the available
1238  * space in the notification area, if necessary.
1239  *
1240  * Return value: a new #GtkStatusIcon
1241  *
1242  * Since: 2.10
1243  **/
1244 GtkStatusIcon *
gtk_status_icon_new_from_file(const gchar * filename)1245 gtk_status_icon_new_from_file (const gchar *filename)
1246 {
1247   return g_object_new (GTK_TYPE_STATUS_ICON,
1248 		       "file", filename,
1249 		       NULL);
1250 }
1251 
1252 /**
1253  * gtk_status_icon_new_from_stock:
1254  * @stock_id: a stock icon id
1255  *
1256  * Creates a status icon displaying a stock icon. Sample stock icon
1257  * names are #GTK_STOCK_OPEN, #GTK_STOCK_QUIT. You can register your
1258  * own stock icon names, see gtk_icon_factory_add_default() and
1259  * gtk_icon_factory_add().
1260  *
1261  * Return value: a new #GtkStatusIcon
1262  *
1263  * Since: 2.10
1264  **/
1265 GtkStatusIcon *
gtk_status_icon_new_from_stock(const gchar * stock_id)1266 gtk_status_icon_new_from_stock (const gchar *stock_id)
1267 {
1268   return g_object_new (GTK_TYPE_STATUS_ICON,
1269 		       "stock", stock_id,
1270 		       NULL);
1271 }
1272 
1273 /**
1274  * gtk_status_icon_new_from_icon_name:
1275  * @icon_name: an icon name
1276  *
1277  * Creates a status icon displaying an icon from the current icon theme.
1278  * If the current icon theme is changed, the icon will be updated
1279  * appropriately.
1280  *
1281  * Return value: a new #GtkStatusIcon
1282  *
1283  * Since: 2.10
1284  **/
1285 GtkStatusIcon *
gtk_status_icon_new_from_icon_name(const gchar * icon_name)1286 gtk_status_icon_new_from_icon_name (const gchar *icon_name)
1287 {
1288   return g_object_new (GTK_TYPE_STATUS_ICON,
1289 		       "icon-name", icon_name,
1290 		       NULL);
1291 }
1292 
1293 /**
1294  * gtk_status_icon_new_from_gicon:
1295  * @icon: a #GIcon
1296  *
1297  * Creates a status icon displaying a #GIcon. If the icon is a
1298  * themed icon, it will be updated when the theme changes.
1299  *
1300  * Return value: a new #GtkStatusIcon
1301  *
1302  * Since: 2.14
1303  **/
1304 GtkStatusIcon *
gtk_status_icon_new_from_gicon(GIcon * icon)1305 gtk_status_icon_new_from_gicon (GIcon *icon)
1306 {
1307   return g_object_new (GTK_TYPE_STATUS_ICON,
1308 		       "gicon", icon,
1309 		       NULL);
1310 }
1311 
1312 static void
emit_activate_signal(GtkStatusIcon * status_icon)1313 emit_activate_signal (GtkStatusIcon *status_icon)
1314 {
1315   g_signal_emit (status_icon,
1316 		 status_icon_signals [ACTIVATE_SIGNAL], 0);
1317 }
1318 
1319 static void
emit_popup_menu_signal(GtkStatusIcon * status_icon,guint button,guint32 activate_time)1320 emit_popup_menu_signal (GtkStatusIcon *status_icon,
1321 			guint          button,
1322 			guint32        activate_time)
1323 {
1324   g_signal_emit (status_icon,
1325 		 status_icon_signals [POPUP_MENU_SIGNAL], 0,
1326 		 button,
1327 		 activate_time);
1328 }
1329 
1330 #ifdef GDK_WINDOWING_X11
1331 
1332 static gboolean
emit_size_changed_signal(GtkStatusIcon * status_icon,gint size)1333 emit_size_changed_signal (GtkStatusIcon *status_icon,
1334 			  gint           size)
1335 {
1336   gboolean handled = FALSE;
1337 
1338   g_signal_emit (status_icon,
1339 		 status_icon_signals [SIZE_CHANGED_SIGNAL], 0,
1340 		 size,
1341 		 &handled);
1342 
1343   return handled;
1344 }
1345 
1346 #endif
1347 
1348 static GdkPixbuf *
gtk_status_icon_blank_icon(GtkStatusIcon * status_icon)1349 gtk_status_icon_blank_icon (GtkStatusIcon *status_icon)
1350 {
1351   GtkStatusIconPrivate *priv = status_icon->priv;
1352 
1353   if (priv->blank_icon)
1354     {
1355       gint width, height;
1356 
1357       width  = gdk_pixbuf_get_width (priv->blank_icon);
1358       height = gdk_pixbuf_get_height (priv->blank_icon);
1359 
1360 
1361       if (width == priv->image_width && height == priv->image_height)
1362 	return priv->blank_icon;
1363       else
1364 	{
1365 	  g_object_unref (priv->blank_icon);
1366 	  priv->blank_icon = NULL;
1367 	}
1368     }
1369 
1370   priv->blank_icon = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
1371 				     priv->image_width,
1372 				     priv->image_height);
1373   if (priv->blank_icon)
1374     gdk_pixbuf_fill (priv->blank_icon, 0);
1375 
1376   return priv->blank_icon;
1377 }
1378 
1379 #ifdef GDK_WINDOWING_X11
1380 
1381 static GtkIconSize
find_icon_size(GtkWidget * widget,gint pixel_size)1382 find_icon_size (GtkWidget *widget,
1383 		gint       pixel_size)
1384 {
1385   GdkScreen *screen;
1386   GtkSettings *settings;
1387   GtkIconSize s, size;
1388   gint w, h, d, dist;
1389 
1390   screen = gtk_widget_get_screen (widget);
1391 
1392   if (!screen)
1393     return GTK_ICON_SIZE_MENU;
1394 
1395   settings = gtk_settings_get_for_screen (screen);
1396 
1397   dist = G_MAXINT;
1398   size = GTK_ICON_SIZE_MENU;
1399 
1400   for (s = GTK_ICON_SIZE_MENU; s <= GTK_ICON_SIZE_DIALOG; s++)
1401     {
1402       if (gtk_icon_size_lookup_for_settings (settings, s, &w, &h) &&
1403 	  w <= pixel_size && h <= pixel_size)
1404 	{
1405 	  d = MAX (pixel_size - w, pixel_size - h);
1406 	  if (d < dist)
1407 	    {
1408 	      dist = d;
1409 	      size = s;
1410 	    }
1411 	}
1412     }
1413 
1414   return size;
1415 }
1416 
1417 #endif
1418 
1419 static void
gtk_status_icon_update_image(GtkStatusIcon * status_icon)1420 gtk_status_icon_update_image (GtkStatusIcon *status_icon)
1421 {
1422   GtkStatusIconPrivate *priv = status_icon->priv;
1423 #ifdef GDK_WINDOWING_WIN32
1424   HICON prev_hicon;
1425 #endif
1426 
1427   if (priv->blink_off)
1428     {
1429 #ifdef GDK_WINDOWING_X11
1430       gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image),
1431 				 gtk_status_icon_blank_icon (status_icon));
1432 #endif
1433 #ifdef GDK_WINDOWING_WIN32
1434       prev_hicon = priv->nid.hIcon;
1435       priv->nid.hIcon = gdk_win32_pixbuf_to_hicon_libgtk_only (gtk_status_icon_blank_icon (status_icon));
1436       priv->nid.uFlags |= NIF_ICON;
1437       if (priv->nid.hWnd != NULL && priv->visible)
1438 	if (!Shell_NotifyIconW (NIM_MODIFY, &priv->nid))
1439 	  g_warning (G_STRLOC ": Shell_NotifyIcon(NIM_MODIFY) failed");
1440       if (prev_hicon)
1441 	DestroyIcon (prev_hicon);
1442 #endif
1443 #ifdef GDK_WINDOWING_QUARTZ
1444       QUARTZ_POOL_ALLOC;
1445       [priv->status_item setImage:gtk_status_icon_blank_icon (status_icon)];
1446       QUARTZ_POOL_RELEASE;
1447 #endif
1448       return;
1449     }
1450 
1451   switch (priv->storage_type)
1452     {
1453     case GTK_IMAGE_PIXBUF:
1454       {
1455 	GdkPixbuf *pixbuf;
1456 
1457 	pixbuf = priv->image_data.pixbuf;
1458 
1459 	if (pixbuf)
1460 	  {
1461 	    GdkPixbuf *scaled;
1462 	    gint size;
1463 	    gint width;
1464 	    gint height;
1465 
1466 	    size = priv->size;
1467 
1468 	    width  = gdk_pixbuf_get_width  (pixbuf);
1469 	    height = gdk_pixbuf_get_height (pixbuf);
1470 
1471 	    if (width > size || height > size)
1472 	      scaled = gdk_pixbuf_scale_simple (pixbuf,
1473 						MIN (size, width),
1474 						MIN (size, height),
1475 						GDK_INTERP_BILINEAR);
1476 	    else
1477 	      scaled = g_object_ref (pixbuf);
1478 
1479 #ifdef GDK_WINDOWING_X11
1480 	    gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), scaled);
1481 #endif
1482 #ifdef GDK_WINDOWING_WIN32
1483 	    prev_hicon = priv->nid.hIcon;
1484 	    priv->nid.hIcon = gdk_win32_pixbuf_to_hicon_libgtk_only (scaled);
1485 	    priv->nid.uFlags |= NIF_ICON;
1486 	    if (priv->nid.hWnd != NULL && priv->visible)
1487 	      if (!Shell_NotifyIconW (NIM_MODIFY, &priv->nid))
1488 		  g_warning (G_STRLOC ": Shell_NotifyIcon(NIM_MODIFY) failed");
1489 	    if (prev_hicon)
1490 	      DestroyIcon (prev_hicon);
1491 #endif
1492 #ifdef GDK_WINDOWING_QUARTZ
1493       QUARTZ_POOL_ALLOC;
1494       [priv->status_item setImage:scaled];
1495       QUARTZ_POOL_RELEASE;
1496 #endif
1497 
1498 	    g_object_unref (scaled);
1499 	  }
1500 	else
1501 	  {
1502 #ifdef GDK_WINDOWING_X11
1503 	    gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), NULL);
1504 #endif
1505 #ifdef GDK_WINDOWING_WIN32
1506 	    priv->nid.uFlags &= ~NIF_ICON;
1507 	    if (priv->nid.hWnd != NULL && priv->visible)
1508 	      if (!Shell_NotifyIconW (NIM_MODIFY, &priv->nid))
1509 		g_warning (G_STRLOC ": Shell_NotifyIcon(NIM_MODIFY) failed");
1510 #endif
1511 #ifdef GDK_WINDOWING_QUARTZ
1512       [priv->status_item setImage:NULL];
1513 #endif
1514 	  }
1515       }
1516       break;
1517 
1518     case GTK_IMAGE_STOCK:
1519       {
1520 #ifdef GDK_WINDOWING_X11
1521 	GtkIconSize size = find_icon_size (priv->image, priv->size);
1522 	gtk_image_set_from_stock (GTK_IMAGE (priv->image),
1523 				  priv->image_data.stock_id,
1524 				  size);
1525 #endif
1526 #ifdef GDK_WINDOWING_WIN32
1527 	{
1528 	  GdkPixbuf *pixbuf =
1529 	    gtk_widget_render_icon (priv->dummy_widget,
1530 				    priv->image_data.stock_id,
1531 				    GTK_ICON_SIZE_SMALL_TOOLBAR,
1532 				    NULL);
1533 
1534 	  prev_hicon = priv->nid.hIcon;
1535 	  priv->nid.hIcon = gdk_win32_pixbuf_to_hicon_libgtk_only (pixbuf);
1536 	  priv->nid.uFlags |= NIF_ICON;
1537 	  if (priv->nid.hWnd != NULL && priv->visible)
1538 	    if (!Shell_NotifyIconW (NIM_MODIFY, &priv->nid))
1539 	      g_warning (G_STRLOC ": Shell_NotifyIcon(NIM_MODIFY) failed");
1540 	  if (prev_hicon)
1541 	    DestroyIcon (prev_hicon);
1542 	  g_object_unref (pixbuf);
1543 	}
1544 #endif
1545 #ifdef GDK_WINDOWING_QUARTZ
1546 	{
1547 	  GdkPixbuf *pixbuf;
1548 
1549 	  pixbuf = gtk_widget_render_icon (priv->dummy_widget,
1550 					   priv->image_data.stock_id,
1551 					   GTK_ICON_SIZE_SMALL_TOOLBAR,
1552 					   NULL);
1553 	  QUARTZ_POOL_ALLOC;
1554 	  [priv->status_item setImage:pixbuf];
1555 	  QUARTZ_POOL_RELEASE;
1556 	  g_object_unref (pixbuf);
1557 	}
1558 #endif
1559       }
1560       break;
1561 
1562     case GTK_IMAGE_ICON_NAME:
1563       {
1564 #ifdef GDK_WINDOWING_X11
1565 	GtkIconSize size = find_icon_size (priv->image, priv->size);
1566 	gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1567 				      priv->image_data.icon_name,
1568 				      size);
1569 #endif
1570 #ifdef GDK_WINDOWING_WIN32
1571 	{
1572 	  GdkPixbuf *pixbuf =
1573 	    gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
1574 				      priv->image_data.icon_name,
1575 				      priv->size,
1576 				      0, NULL);
1577 
1578 	  prev_hicon = priv->nid.hIcon;
1579 	  priv->nid.hIcon = gdk_win32_pixbuf_to_hicon_libgtk_only (pixbuf);
1580 	  priv->nid.uFlags |= NIF_ICON;
1581 	  if (priv->nid.hWnd != NULL && priv->visible)
1582 	    if (!Shell_NotifyIconW (NIM_MODIFY, &priv->nid))
1583 	      g_warning (G_STRLOC ": Shell_NotifyIcon(NIM_MODIFY) failed");
1584 	  if (prev_hicon)
1585 	    DestroyIcon (prev_hicon);
1586 	  g_object_unref (pixbuf);
1587 	}
1588 #endif
1589 #ifdef GDK_WINDOWING_QUARTZ
1590 	{
1591 	  GdkPixbuf *pixbuf;
1592 
1593 	  pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
1594 					     priv->image_data.icon_name,
1595 					     priv->size,
1596 					     0, NULL);
1597 
1598 	  QUARTZ_POOL_ALLOC;
1599 	  [priv->status_item setImage:pixbuf];
1600 	  QUARTZ_POOL_RELEASE;
1601 	  g_object_unref (pixbuf);
1602 	}
1603 #endif
1604 
1605       }
1606       break;
1607 
1608     case GTK_IMAGE_GICON:
1609       {
1610 #ifdef GDK_WINDOWING_X11
1611         GtkIconSize size = find_icon_size (priv->image, priv->size);
1612         gtk_image_set_from_gicon (GTK_IMAGE (priv->image),
1613                                   priv->image_data.gicon,
1614                                   size);
1615 #endif
1616 #ifdef GDK_WINDOWING_WIN32
1617       {
1618         GtkIconInfo *info =
1619         gtk_icon_theme_lookup_by_gicon (gtk_icon_theme_get_default (),
1620                                         priv->image_data.gicon,
1621                                         priv->size,
1622                                         0);
1623         GdkPixbuf *pixbuf = gtk_icon_info_load_icon (info, NULL);
1624 
1625         prev_hicon = priv->nid.hIcon;
1626         priv->nid.hIcon = gdk_win32_pixbuf_to_hicon_libgtk_only (pixbuf);
1627         priv->nid.uFlags |= NIF_ICON;
1628         if (priv->nid.hWnd != NULL && priv->visible)
1629           if (!Shell_NotifyIconW (NIM_MODIFY, &priv->nid))
1630             g_warning (G_STRLOC ": Shell_NotifyIcon(NIM_MODIFY) failed");
1631           if (prev_hicon)
1632             DestroyIcon (prev_hicon);
1633           g_object_unref (pixbuf);
1634       }
1635 #endif
1636 #ifdef GDK_WINDOWING_QUARTZ
1637       {
1638         GtkIconInfo *info =
1639         gtk_icon_theme_lookup_by_gicon (gtk_icon_theme_get_default (),
1640                                         priv->image_data.gicon,
1641                                         priv->size,
1642                                         0);
1643         GdkPixbuf *pixbuf = gtk_icon_info_load_icon (info, NULL);
1644 
1645         QUARTZ_POOL_ALLOC;
1646         [priv->status_item setImage:pixbuf];
1647         QUARTZ_POOL_RELEASE;
1648         g_object_unref (pixbuf);
1649       }
1650 #endif
1651 
1652       }
1653       break;
1654 
1655     case GTK_IMAGE_EMPTY:
1656 #ifdef GDK_WINDOWING_X11
1657       gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), NULL);
1658 #endif
1659 #ifdef GDK_WINDOWING_WIN32
1660       priv->nid.uFlags &= ~NIF_ICON;
1661       if (priv->nid.hWnd != NULL && priv->visible)
1662 	if (!Shell_NotifyIconW (NIM_MODIFY, &priv->nid))
1663 	  g_warning (G_STRLOC ": Shell_NotifyIcon(NIM_MODIFY) failed");
1664 #endif
1665 #ifdef GDK_WINDOWING_QUARTZ
1666         {
1667           QUARTZ_POOL_ALLOC;
1668           [priv->status_item setImage:NULL];
1669           QUARTZ_POOL_RELEASE;
1670         }
1671 #endif
1672       break;
1673     default:
1674       g_assert_not_reached ();
1675       break;
1676     }
1677 }
1678 
1679 #ifdef GDK_WINDOWING_X11
1680 
1681 static void
gtk_status_icon_size_allocate(GtkStatusIcon * status_icon,GtkAllocation * allocation)1682 gtk_status_icon_size_allocate (GtkStatusIcon *status_icon,
1683 			       GtkAllocation *allocation)
1684 {
1685   GtkStatusIconPrivate *priv = status_icon->priv;
1686   GtkOrientation orientation;
1687   gint size;
1688 
1689   orientation = _gtk_tray_icon_get_orientation (GTK_TRAY_ICON (priv->tray_icon));
1690 
1691   if (orientation == GTK_ORIENTATION_HORIZONTAL)
1692     size = allocation->height;
1693   else
1694     size = allocation->width;
1695 
1696   priv->image_width = allocation->width - GTK_MISC (priv->image)->xpad * 2;
1697   priv->image_height = allocation->height - GTK_MISC (priv->image)->ypad * 2;
1698 
1699   if (priv->size - 1 > size || priv->size + 1 < size)
1700     {
1701       priv->size = size;
1702 
1703       g_object_notify (G_OBJECT (status_icon), "size");
1704 
1705       if (!emit_size_changed_signal (status_icon, size))
1706 	gtk_status_icon_update_image (status_icon);
1707     }
1708 }
1709 
1710 static void
gtk_status_icon_screen_changed(GtkStatusIcon * status_icon,GdkScreen * old_screen)1711 gtk_status_icon_screen_changed (GtkStatusIcon *status_icon,
1712 				GdkScreen *old_screen)
1713 {
1714   GtkStatusIconPrivate *priv = status_icon->priv;
1715 
1716   if (gtk_widget_get_screen (priv->tray_icon) != old_screen)
1717     {
1718       g_object_notify (G_OBJECT (status_icon), "screen");
1719     }
1720 }
1721 
1722 #endif
1723 
1724 #ifdef GDK_WINDOWING_X11
1725 
1726 static void
gtk_status_icon_embedded_changed(GtkStatusIcon * status_icon)1727 gtk_status_icon_embedded_changed (GtkStatusIcon *status_icon)
1728 {
1729   g_object_notify (G_OBJECT (status_icon), "embedded");
1730 }
1731 
1732 static void
gtk_status_icon_orientation_changed(GtkStatusIcon * status_icon)1733 gtk_status_icon_orientation_changed (GtkStatusIcon *status_icon)
1734 {
1735   g_object_notify (G_OBJECT (status_icon), "orientation");
1736 }
1737 
1738 static gboolean
gtk_status_icon_key_press(GtkStatusIcon * status_icon,GdkEventKey * event)1739 gtk_status_icon_key_press (GtkStatusIcon  *status_icon,
1740 			   GdkEventKey    *event)
1741 {
1742   guint state, keyval;
1743 
1744   state = event->state & gtk_accelerator_get_default_mod_mask ();
1745   keyval = event->keyval;
1746   if (state == 0 &&
1747       (keyval == GDK_Return ||
1748        keyval == GDK_KP_Enter ||
1749        keyval == GDK_ISO_Enter ||
1750        keyval == GDK_space ||
1751        keyval == GDK_KP_Space))
1752     {
1753       emit_activate_signal (status_icon);
1754       return TRUE;
1755     }
1756 
1757   return FALSE;
1758 }
1759 
1760 static void
gtk_status_icon_popup_menu(GtkStatusIcon * status_icon)1761 gtk_status_icon_popup_menu (GtkStatusIcon  *status_icon)
1762 {
1763   emit_popup_menu_signal (status_icon, 0, gtk_get_current_event_time ());
1764 }
1765 
1766 #endif
1767 
1768 static gboolean
gtk_status_icon_button_press(GtkStatusIcon * status_icon,GdkEventButton * event)1769 gtk_status_icon_button_press (GtkStatusIcon  *status_icon,
1770 			      GdkEventButton *event)
1771 {
1772   gboolean handled = FALSE;
1773 
1774   g_signal_emit (status_icon,
1775 		 status_icon_signals [BUTTON_PRESS_EVENT_SIGNAL], 0,
1776 		 event, &handled);
1777   if (handled)
1778     return TRUE;
1779 
1780   if (_gtk_button_event_triggers_context_menu (event))
1781     {
1782       emit_popup_menu_signal (status_icon, event->button, event->time);
1783       return TRUE;
1784     }
1785   else if (event->button == 1 && event->type == GDK_BUTTON_PRESS)
1786     {
1787       emit_activate_signal (status_icon);
1788       return TRUE;
1789     }
1790 
1791   return FALSE;
1792 }
1793 
1794 static gboolean
gtk_status_icon_button_release(GtkStatusIcon * status_icon,GdkEventButton * event)1795 gtk_status_icon_button_release (GtkStatusIcon  *status_icon,
1796 				GdkEventButton *event)
1797 {
1798   gboolean handled = FALSE;
1799   g_signal_emit (status_icon,
1800 		 status_icon_signals [BUTTON_RELEASE_EVENT_SIGNAL], 0,
1801 		 event, &handled);
1802   return handled;
1803 }
1804 
1805 #ifdef GDK_WINDOWING_X11
1806 static gboolean
gtk_status_icon_scroll(GtkStatusIcon * status_icon,GdkEventScroll * event)1807 gtk_status_icon_scroll (GtkStatusIcon  *status_icon,
1808 			GdkEventScroll *event)
1809 {
1810   gboolean handled = FALSE;
1811   g_signal_emit (status_icon,
1812 		 status_icon_signals [SCROLL_EVENT_SIGNAL], 0,
1813 		 event, &handled);
1814   return handled;
1815 }
1816 
1817 static gboolean
gtk_status_icon_query_tooltip(GtkStatusIcon * status_icon,gint x,gint y,gboolean keyboard_tip,GtkTooltip * tooltip)1818 gtk_status_icon_query_tooltip (GtkStatusIcon *status_icon,
1819 			       gint           x,
1820 			       gint           y,
1821 			       gboolean       keyboard_tip,
1822 			       GtkTooltip    *tooltip)
1823 {
1824   gboolean handled = FALSE;
1825   g_signal_emit (status_icon,
1826 		 status_icon_signals [QUERY_TOOLTIP_SIGNAL], 0,
1827 		 x, y, keyboard_tip, tooltip, &handled);
1828   return handled;
1829 }
1830 #endif /* GDK_WINDOWING_X11 */
1831 
1832 static void
gtk_status_icon_reset_image_data(GtkStatusIcon * status_icon)1833 gtk_status_icon_reset_image_data (GtkStatusIcon *status_icon)
1834 {
1835   GtkStatusIconPrivate *priv = status_icon->priv;
1836 
1837   switch (priv->storage_type)
1838   {
1839     case GTK_IMAGE_PIXBUF:
1840       if (priv->image_data.pixbuf)
1841 	g_object_unref (priv->image_data.pixbuf);
1842       priv->image_data.pixbuf = NULL;
1843       g_object_notify (G_OBJECT (status_icon), "pixbuf");
1844       break;
1845 
1846     case GTK_IMAGE_STOCK:
1847       g_free (priv->image_data.stock_id);
1848       priv->image_data.stock_id = NULL;
1849 
1850       g_object_notify (G_OBJECT (status_icon), "stock");
1851       break;
1852 
1853     case GTK_IMAGE_ICON_NAME:
1854       g_free (priv->image_data.icon_name);
1855       priv->image_data.icon_name = NULL;
1856 
1857       g_object_notify (G_OBJECT (status_icon), "icon-name");
1858       break;
1859 
1860     case GTK_IMAGE_GICON:
1861       if (priv->image_data.gicon)
1862         g_object_unref (priv->image_data.gicon);
1863       priv->image_data.gicon = NULL;
1864 
1865       g_object_notify (G_OBJECT (status_icon), "gicon");
1866       break;
1867 
1868     case GTK_IMAGE_EMPTY:
1869       break;
1870     default:
1871       g_assert_not_reached ();
1872       break;
1873   }
1874 
1875   priv->storage_type = GTK_IMAGE_EMPTY;
1876   g_object_notify (G_OBJECT (status_icon), "storage-type");
1877 }
1878 
1879 static void
gtk_status_icon_set_image(GtkStatusIcon * status_icon,GtkImageType storage_type,gpointer data)1880 gtk_status_icon_set_image (GtkStatusIcon *status_icon,
1881     			   GtkImageType   storage_type,
1882 			   gpointer       data)
1883 {
1884   GtkStatusIconPrivate *priv = status_icon->priv;
1885 
1886   g_object_freeze_notify (G_OBJECT (status_icon));
1887 
1888   gtk_status_icon_reset_image_data (status_icon);
1889 
1890   priv->storage_type = storage_type;
1891   g_object_notify (G_OBJECT (status_icon), "storage-type");
1892 
1893   switch (storage_type)
1894     {
1895     case GTK_IMAGE_PIXBUF:
1896       priv->image_data.pixbuf = (GdkPixbuf *)data;
1897       g_object_notify (G_OBJECT (status_icon), "pixbuf");
1898       break;
1899     case GTK_IMAGE_STOCK:
1900       priv->image_data.stock_id = g_strdup ((const gchar *)data);
1901       g_object_notify (G_OBJECT (status_icon), "stock");
1902       break;
1903     case GTK_IMAGE_ICON_NAME:
1904       priv->image_data.icon_name = g_strdup ((const gchar *)data);
1905       g_object_notify (G_OBJECT (status_icon), "icon-name");
1906       break;
1907     case GTK_IMAGE_GICON:
1908       priv->image_data.gicon = (GIcon *)data;
1909       g_object_notify (G_OBJECT (status_icon), "gicon");
1910       break;
1911     default:
1912       g_warning ("Image type %u not handled by GtkStatusIcon", storage_type);
1913     }
1914 
1915   g_object_thaw_notify (G_OBJECT (status_icon));
1916 
1917   gtk_status_icon_update_image (status_icon);
1918 }
1919 
1920 /**
1921  * gtk_status_icon_set_from_pixbuf:
1922  * @status_icon: a #GtkStatusIcon
1923  * @pixbuf: (allow-none): a #GdkPixbuf or %NULL
1924  *
1925  * Makes @status_icon display @pixbuf.
1926  * See gtk_status_icon_new_from_pixbuf() for details.
1927  *
1928  * Since: 2.10
1929  **/
1930 void
gtk_status_icon_set_from_pixbuf(GtkStatusIcon * status_icon,GdkPixbuf * pixbuf)1931 gtk_status_icon_set_from_pixbuf (GtkStatusIcon *status_icon,
1932 				 GdkPixbuf     *pixbuf)
1933 {
1934   g_return_if_fail (GTK_IS_STATUS_ICON (status_icon));
1935   g_return_if_fail (pixbuf == NULL || GDK_IS_PIXBUF (pixbuf));
1936 
1937   if (pixbuf)
1938     g_object_ref (pixbuf);
1939 
1940   gtk_status_icon_set_image (status_icon, GTK_IMAGE_PIXBUF,
1941       			     (gpointer) pixbuf);
1942 }
1943 
1944 /**
1945  * gtk_status_icon_set_from_file:
1946  * @status_icon: a #GtkStatusIcon
1947  * @filename: a filename
1948  *
1949  * Makes @status_icon display the file @filename.
1950  * See gtk_status_icon_new_from_file() for details.
1951  *
1952  * Since: 2.10
1953  **/
1954 void
gtk_status_icon_set_from_file(GtkStatusIcon * status_icon,const gchar * filename)1955 gtk_status_icon_set_from_file (GtkStatusIcon *status_icon,
1956  			       const gchar   *filename)
1957 {
1958   GdkPixbuf *pixbuf;
1959 
1960   g_return_if_fail (GTK_IS_STATUS_ICON (status_icon));
1961   g_return_if_fail (filename != NULL);
1962 
1963   pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
1964 
1965   gtk_status_icon_set_from_pixbuf (status_icon, pixbuf);
1966 
1967   if (pixbuf)
1968     g_object_unref (pixbuf);
1969 }
1970 
1971 /**
1972  * gtk_status_icon_set_from_stock:
1973  * @status_icon: a #GtkStatusIcon
1974  * @stock_id: a stock icon id
1975  *
1976  * Makes @status_icon display the stock icon with the id @stock_id.
1977  * See gtk_status_icon_new_from_stock() for details.
1978  *
1979  * Since: 2.10
1980  **/
1981 void
gtk_status_icon_set_from_stock(GtkStatusIcon * status_icon,const gchar * stock_id)1982 gtk_status_icon_set_from_stock (GtkStatusIcon *status_icon,
1983 				const gchar   *stock_id)
1984 {
1985   g_return_if_fail (GTK_IS_STATUS_ICON (status_icon));
1986   g_return_if_fail (stock_id != NULL);
1987 
1988   gtk_status_icon_set_image (status_icon, GTK_IMAGE_STOCK,
1989       			     (gpointer) stock_id);
1990 }
1991 
1992 /**
1993  * gtk_status_icon_set_from_icon_name:
1994  * @status_icon: a #GtkStatusIcon
1995  * @icon_name: an icon name
1996  *
1997  * Makes @status_icon display the icon named @icon_name from the
1998  * current icon theme.
1999  * See gtk_status_icon_new_from_icon_name() for details.
2000  *
2001  * Since: 2.10
2002  **/
2003 void
gtk_status_icon_set_from_icon_name(GtkStatusIcon * status_icon,const gchar * icon_name)2004 gtk_status_icon_set_from_icon_name (GtkStatusIcon *status_icon,
2005 				    const gchar   *icon_name)
2006 {
2007   g_return_if_fail (GTK_IS_STATUS_ICON (status_icon));
2008   g_return_if_fail (icon_name != NULL);
2009 
2010   gtk_status_icon_set_image (status_icon, GTK_IMAGE_ICON_NAME,
2011       			     (gpointer) icon_name);
2012 }
2013 
2014 /**
2015  * gtk_status_icon_set_from_gicon:
2016  * @status_icon: a #GtkStatusIcon
2017  * @icon: a GIcon
2018  *
2019  * Makes @status_icon display the #GIcon.
2020  * See gtk_status_icon_new_from_gicon() for details.
2021  *
2022  * Since: 2.14
2023  **/
2024 void
gtk_status_icon_set_from_gicon(GtkStatusIcon * status_icon,GIcon * icon)2025 gtk_status_icon_set_from_gicon (GtkStatusIcon *status_icon,
2026                                 GIcon         *icon)
2027 {
2028   g_return_if_fail (GTK_IS_STATUS_ICON (status_icon));
2029   g_return_if_fail (icon != NULL);
2030 
2031   if (icon)
2032     g_object_ref (icon);
2033 
2034   gtk_status_icon_set_image (status_icon, GTK_IMAGE_GICON,
2035                              (gpointer) icon);
2036 }
2037 
2038 /**
2039  * gtk_status_icon_get_storage_type:
2040  * @status_icon: a #GtkStatusIcon
2041  *
2042  * Gets the type of representation being used by the #GtkStatusIcon
2043  * to store image data. If the #GtkStatusIcon has no image data,
2044  * the return value will be %GTK_IMAGE_EMPTY.
2045  *
2046  * Return value: the image representation being used
2047  *
2048  * Since: 2.10
2049  **/
2050 GtkImageType
gtk_status_icon_get_storage_type(GtkStatusIcon * status_icon)2051 gtk_status_icon_get_storage_type (GtkStatusIcon *status_icon)
2052 {
2053   g_return_val_if_fail (GTK_IS_STATUS_ICON (status_icon), GTK_IMAGE_EMPTY);
2054 
2055   return status_icon->priv->storage_type;
2056 }
2057 /**
2058  * gtk_status_icon_get_pixbuf:
2059  * @status_icon: a #GtkStatusIcon
2060  *
2061  * Gets the #GdkPixbuf being displayed by the #GtkStatusIcon.
2062  * The storage type of the status icon must be %GTK_IMAGE_EMPTY or
2063  * %GTK_IMAGE_PIXBUF (see gtk_status_icon_get_storage_type()).
2064  * The caller of this function does not own a reference to the
2065  * returned pixbuf.
2066  *
2067  * Return value: (transfer none): the displayed pixbuf,
2068  *     or %NULL if the image is empty.
2069  *
2070  * Since: 2.10
2071  **/
2072 GdkPixbuf *
gtk_status_icon_get_pixbuf(GtkStatusIcon * status_icon)2073 gtk_status_icon_get_pixbuf (GtkStatusIcon *status_icon)
2074 {
2075   GtkStatusIconPrivate *priv;
2076 
2077   g_return_val_if_fail (GTK_IS_STATUS_ICON (status_icon), NULL);
2078 
2079   priv = status_icon->priv;
2080 
2081   g_return_val_if_fail (priv->storage_type == GTK_IMAGE_PIXBUF ||
2082 			priv->storage_type == GTK_IMAGE_EMPTY, NULL);
2083 
2084   if (priv->storage_type == GTK_IMAGE_EMPTY)
2085     priv->image_data.pixbuf = NULL;
2086 
2087   return priv->image_data.pixbuf;
2088 }
2089 
2090 /**
2091  * gtk_status_icon_get_stock:
2092  * @status_icon: a #GtkStatusIcon
2093  *
2094  * Gets the id of the stock icon being displayed by the #GtkStatusIcon.
2095  * The storage type of the status icon must be %GTK_IMAGE_EMPTY or
2096  * %GTK_IMAGE_STOCK (see gtk_status_icon_get_storage_type()).
2097  * The returned string is owned by the #GtkStatusIcon and should not
2098  * be freed or modified.
2099  *
2100  * Return value: stock id of the displayed stock icon,
2101  *   or %NULL if the image is empty.
2102  *
2103  * Since: 2.10
2104  **/
2105 const gchar *
gtk_status_icon_get_stock(GtkStatusIcon * status_icon)2106 gtk_status_icon_get_stock (GtkStatusIcon *status_icon)
2107 {
2108   GtkStatusIconPrivate *priv;
2109 
2110   g_return_val_if_fail (GTK_IS_STATUS_ICON (status_icon), NULL);
2111 
2112   priv = status_icon->priv;
2113 
2114   g_return_val_if_fail (priv->storage_type == GTK_IMAGE_STOCK ||
2115 			priv->storage_type == GTK_IMAGE_EMPTY, NULL);
2116 
2117   if (priv->storage_type == GTK_IMAGE_EMPTY)
2118     priv->image_data.stock_id = NULL;
2119 
2120   return priv->image_data.stock_id;
2121 }
2122 
2123 /**
2124  * gtk_status_icon_get_icon_name:
2125  * @status_icon: a #GtkStatusIcon
2126  *
2127  * Gets the name of the icon being displayed by the #GtkStatusIcon.
2128  * The storage type of the status icon must be %GTK_IMAGE_EMPTY or
2129  * %GTK_IMAGE_ICON_NAME (see gtk_status_icon_get_storage_type()).
2130  * The returned string is owned by the #GtkStatusIcon and should not
2131  * be freed or modified.
2132  *
2133  * Return value: name of the displayed icon, or %NULL if the image is empty.
2134  *
2135  * Since: 2.10
2136  **/
2137 const gchar *
gtk_status_icon_get_icon_name(GtkStatusIcon * status_icon)2138 gtk_status_icon_get_icon_name (GtkStatusIcon *status_icon)
2139 {
2140   GtkStatusIconPrivate *priv;
2141 
2142   g_return_val_if_fail (GTK_IS_STATUS_ICON (status_icon), NULL);
2143 
2144   priv = status_icon->priv;
2145 
2146   g_return_val_if_fail (priv->storage_type == GTK_IMAGE_ICON_NAME ||
2147 			priv->storage_type == GTK_IMAGE_EMPTY, NULL);
2148 
2149   if (priv->storage_type == GTK_IMAGE_EMPTY)
2150     priv->image_data.icon_name = NULL;
2151 
2152   return priv->image_data.icon_name;
2153 }
2154 
2155 /**
2156  * gtk_status_icon_get_gicon:
2157  * @status_icon: a #GtkStatusIcon
2158  *
2159  * Retrieves the #GIcon being displayed by the #GtkStatusIcon.
2160  * The storage type of the status icon must be %GTK_IMAGE_EMPTY or
2161  * %GTK_IMAGE_GICON (see gtk_status_icon_get_storage_type()).
2162  * The caller of this function does not own a reference to the
2163  * returned #GIcon.
2164  *
2165  * If this function fails, @icon is left unchanged;
2166  *
2167  * Returns: (transfer none): the displayed icon, or %NULL if the image is empty
2168  *
2169  * Since: 2.14
2170  **/
2171 GIcon *
gtk_status_icon_get_gicon(GtkStatusIcon * status_icon)2172 gtk_status_icon_get_gicon (GtkStatusIcon *status_icon)
2173 {
2174   GtkStatusIconPrivate *priv;
2175 
2176   g_return_val_if_fail (GTK_IS_STATUS_ICON (status_icon), NULL);
2177 
2178   priv = status_icon->priv;
2179 
2180   g_return_val_if_fail (priv->storage_type == GTK_IMAGE_GICON ||
2181                         priv->storage_type == GTK_IMAGE_EMPTY, NULL);
2182 
2183   if (priv->storage_type == GTK_IMAGE_EMPTY)
2184     priv->image_data.gicon = NULL;
2185 
2186   return priv->image_data.gicon;
2187 }
2188 
2189 /**
2190  * gtk_status_icon_get_size:
2191  * @status_icon: a #GtkStatusIcon
2192  *
2193  * Gets the size in pixels that is available for the image.
2194  * Stock icons and named icons adapt their size automatically
2195  * if the size of the notification area changes. For other
2196  * storage types, the size-changed signal can be used to
2197  * react to size changes.
2198  *
2199  * Note that the returned size is only meaningful while the
2200  * status icon is embedded (see gtk_status_icon_is_embedded()).
2201  *
2202  * Return value: the size that is available for the image
2203  *
2204  * Since: 2.10
2205  **/
2206 gint
gtk_status_icon_get_size(GtkStatusIcon * status_icon)2207 gtk_status_icon_get_size (GtkStatusIcon *status_icon)
2208 {
2209   g_return_val_if_fail (GTK_IS_STATUS_ICON (status_icon), 0);
2210 
2211   return status_icon->priv->size;
2212 }
2213 
2214 /**
2215  * gtk_status_icon_set_screen:
2216  * @status_icon: a #GtkStatusIcon
2217  * @screen: a #GdkScreen
2218  *
2219  * Sets the #GdkScreen where @status_icon is displayed; if
2220  * the icon is already mapped, it will be unmapped, and
2221  * then remapped on the new screen.
2222  *
2223  * Since: 2.12
2224  */
2225 void
gtk_status_icon_set_screen(GtkStatusIcon * status_icon,GdkScreen * screen)2226 gtk_status_icon_set_screen (GtkStatusIcon *status_icon,
2227                             GdkScreen     *screen)
2228 {
2229   g_return_if_fail (GDK_IS_SCREEN (screen));
2230 
2231 #ifdef GDK_WINDOWING_X11
2232   gtk_window_set_screen (GTK_WINDOW (status_icon->priv->tray_icon), screen);
2233 #endif
2234 }
2235 
2236 /**
2237  * gtk_status_icon_get_screen:
2238  * @status_icon: a #GtkStatusIcon
2239  *
2240  * Returns the #GdkScreen associated with @status_icon.
2241  *
2242  * Return value: (transfer none): a #GdkScreen.
2243  *
2244  * Since: 2.12
2245  */
2246 GdkScreen *
gtk_status_icon_get_screen(GtkStatusIcon * status_icon)2247 gtk_status_icon_get_screen (GtkStatusIcon *status_icon)
2248 {
2249   g_return_val_if_fail (GTK_IS_STATUS_ICON (status_icon), NULL);
2250 
2251 #ifdef GDK_WINDOWING_X11
2252   return gtk_window_get_screen (GTK_WINDOW (status_icon->priv->tray_icon));
2253 #else
2254   return gdk_screen_get_default ();
2255 #endif
2256 }
2257 
2258 /**
2259  * gtk_status_icon_set_tooltip:
2260  * @status_icon: a #GtkStatusIcon
2261  * @tooltip_text: (allow-none): the tooltip text, or %NULL
2262  *
2263  * Sets the tooltip of the status icon.
2264  *
2265  * Since: 2.10
2266  *
2267  * Deprecated: 2.16: Use gtk_status_icon_set_tooltip_text() instead.
2268  */
2269 void
gtk_status_icon_set_tooltip(GtkStatusIcon * status_icon,const gchar * tooltip_text)2270 gtk_status_icon_set_tooltip (GtkStatusIcon *status_icon,
2271 			     const gchar   *tooltip_text)
2272 {
2273   gtk_status_icon_set_tooltip_text (status_icon, tooltip_text);
2274 }
2275 
2276 static gboolean
gtk_status_icon_blinker(GtkStatusIcon * status_icon)2277 gtk_status_icon_blinker (GtkStatusIcon *status_icon)
2278 {
2279   GtkStatusIconPrivate *priv = status_icon->priv;
2280 
2281   priv->blink_off = !priv->blink_off;
2282 
2283   gtk_status_icon_update_image (status_icon);
2284 
2285   return TRUE;
2286 }
2287 
2288 static void
gtk_status_icon_enable_blinking(GtkStatusIcon * status_icon)2289 gtk_status_icon_enable_blinking (GtkStatusIcon *status_icon)
2290 {
2291   GtkStatusIconPrivate *priv = status_icon->priv;
2292 
2293   if (!priv->blinking_timeout)
2294     {
2295       gtk_status_icon_blinker (status_icon);
2296 
2297       priv->blinking_timeout =
2298 	gdk_threads_add_timeout (BLINK_TIMEOUT,
2299 		       (GSourceFunc) gtk_status_icon_blinker,
2300 		       status_icon);
2301     }
2302 }
2303 
2304 static void
gtk_status_icon_disable_blinking(GtkStatusIcon * status_icon)2305 gtk_status_icon_disable_blinking (GtkStatusIcon *status_icon)
2306 {
2307   GtkStatusIconPrivate *priv = status_icon->priv;
2308 
2309   if (priv->blinking_timeout)
2310     {
2311       g_source_remove (priv->blinking_timeout);
2312       priv->blinking_timeout = 0;
2313       priv->blink_off = FALSE;
2314 
2315       gtk_status_icon_update_image (status_icon);
2316     }
2317 }
2318 
2319 /**
2320  * gtk_status_icon_set_visible:
2321  * @status_icon: a #GtkStatusIcon
2322  * @visible: %TRUE to show the status icon, %FALSE to hide it
2323  *
2324  * Shows or hides a status icon.
2325  *
2326  * Since: 2.10
2327  **/
2328 void
gtk_status_icon_set_visible(GtkStatusIcon * status_icon,gboolean visible)2329 gtk_status_icon_set_visible (GtkStatusIcon *status_icon,
2330 			     gboolean       visible)
2331 {
2332   GtkStatusIconPrivate *priv;
2333 
2334   g_return_if_fail (GTK_IS_STATUS_ICON (status_icon));
2335 
2336   priv = status_icon->priv;
2337 
2338   visible = visible != FALSE;
2339 
2340   if (priv->visible != visible)
2341     {
2342       priv->visible = visible;
2343 
2344 #ifdef GDK_WINDOWING_X11
2345       if (visible)
2346 	gtk_widget_show (priv->tray_icon);
2347       else if (gtk_widget_get_realized (priv->tray_icon))
2348         {
2349 	  gtk_widget_hide (priv->tray_icon);
2350 	  gtk_widget_unrealize (priv->tray_icon);
2351         }
2352 #endif
2353 #ifdef GDK_WINDOWING_WIN32
2354       if (priv->nid.hWnd != NULL)
2355 	{
2356 	  if (visible)
2357 	    Shell_NotifyIconW (NIM_ADD, &priv->nid);
2358 	  else
2359 	    Shell_NotifyIconW (NIM_DELETE, &priv->nid);
2360 	}
2361 #endif
2362 #ifdef GDK_WINDOWING_QUARTZ
2363       QUARTZ_POOL_ALLOC;
2364       [priv->status_item setVisible:visible];
2365       QUARTZ_POOL_RELEASE;
2366 #endif
2367       g_object_notify (G_OBJECT (status_icon), "visible");
2368     }
2369 }
2370 
2371 /**
2372  * gtk_status_icon_get_visible:
2373  * @status_icon: a #GtkStatusIcon
2374  *
2375  * Returns whether the status icon is visible or not.
2376  * Note that being visible does not guarantee that
2377  * the user can actually see the icon, see also
2378  * gtk_status_icon_is_embedded().
2379  *
2380  * Return value: %TRUE if the status icon is visible
2381  *
2382  * Since: 2.10
2383  **/
2384 gboolean
gtk_status_icon_get_visible(GtkStatusIcon * status_icon)2385 gtk_status_icon_get_visible (GtkStatusIcon *status_icon)
2386 {
2387   g_return_val_if_fail (GTK_IS_STATUS_ICON (status_icon), FALSE);
2388 
2389   return status_icon->priv->visible;
2390 }
2391 
2392 /**
2393  * gtk_status_icon_set_blinking:
2394  * @status_icon: a #GtkStatusIcon
2395  * @blinking: %TRUE to turn blinking on, %FALSE to turn it off
2396  *
2397  * Makes the status icon start or stop blinking.
2398  * Note that blinking user interface elements may be problematic
2399  * for some users, and thus may be turned off, in which case
2400  * this setting has no effect.
2401  *
2402  * Since: 2.10
2403  *
2404  * Deprecated: 2.22: This function will be removed in GTK+ 3
2405  **/
2406 void
gtk_status_icon_set_blinking(GtkStatusIcon * status_icon,gboolean blinking)2407 gtk_status_icon_set_blinking (GtkStatusIcon *status_icon,
2408 			      gboolean       blinking)
2409 {
2410   GtkStatusIconPrivate *priv;
2411 
2412   g_return_if_fail (GTK_IS_STATUS_ICON (status_icon));
2413 
2414   priv = status_icon->priv;
2415 
2416   blinking = blinking != FALSE;
2417 
2418   if (priv->blinking != blinking)
2419     {
2420       priv->blinking = blinking;
2421 
2422       if (blinking)
2423 	gtk_status_icon_enable_blinking (status_icon);
2424       else
2425 	gtk_status_icon_disable_blinking (status_icon);
2426 
2427       g_object_notify (G_OBJECT (status_icon), "blinking");
2428     }
2429 }
2430 
2431 /**
2432  * gtk_status_icon_get_blinking:
2433  * @status_icon: a #GtkStatusIcon
2434  *
2435  * Returns whether the icon is blinking, see
2436  * gtk_status_icon_set_blinking().
2437  *
2438  * Return value: %TRUE if the icon is blinking
2439  *
2440  * Since: 2.10
2441  *
2442  * Deprecated: 2.22: This function will be removed in GTK+ 3
2443  **/
2444 gboolean
gtk_status_icon_get_blinking(GtkStatusIcon * status_icon)2445 gtk_status_icon_get_blinking (GtkStatusIcon *status_icon)
2446 {
2447   g_return_val_if_fail (GTK_IS_STATUS_ICON (status_icon), FALSE);
2448 
2449   return status_icon->priv->blinking;
2450 }
2451 
2452 /**
2453  * gtk_status_icon_is_embedded:
2454  * @status_icon: a #GtkStatusIcon
2455  *
2456  * Returns whether the status icon is embedded in a notification
2457  * area.
2458  *
2459  * Return value: %TRUE if the status icon is embedded in
2460  *   a notification area.
2461  *
2462  * Since: 2.10
2463  **/
2464 gboolean
gtk_status_icon_is_embedded(GtkStatusIcon * status_icon)2465 gtk_status_icon_is_embedded (GtkStatusIcon *status_icon)
2466 {
2467 #ifdef GDK_WINDOWING_X11
2468   GtkPlug *plug;
2469 #endif
2470 
2471   g_return_val_if_fail (GTK_IS_STATUS_ICON (status_icon), FALSE);
2472 
2473 #ifdef GDK_WINDOWING_X11
2474   plug = GTK_PLUG (status_icon->priv->tray_icon);
2475 
2476   if (plug->socket_window)
2477     return TRUE;
2478   else
2479     return FALSE;
2480 #endif
2481 #ifdef GDK_WINDOWING_WIN32
2482   return TRUE;
2483 #endif
2484 #ifdef GDK_WINDOWING_QUARTZ
2485   return TRUE;
2486 #endif
2487 }
2488 
2489 /**
2490  * gtk_status_icon_position_menu:
2491  * @menu: the #GtkMenu
2492  * @x: return location for the x position
2493  * @y: return location for the y position
2494  * @push_in: whether the first menu item should be offset (pushed in) to be
2495  *           aligned with the menu popup position (only useful for GtkOptionMenu).
2496  * @user_data: the status icon to position the menu on
2497  *
2498  * Menu positioning function to use with gtk_menu_popup()
2499  * to position @menu aligned to the status icon @user_data.
2500  *
2501  * Since: 2.10
2502  **/
2503 void
gtk_status_icon_position_menu(GtkMenu * menu,gint * x,gint * y,gboolean * push_in,gpointer user_data)2504 gtk_status_icon_position_menu (GtkMenu  *menu,
2505 			       gint     *x,
2506 			       gint     *y,
2507 			       gboolean *push_in,
2508 			       gpointer  user_data)
2509 {
2510 #ifdef GDK_WINDOWING_X11
2511   GtkStatusIcon *status_icon;
2512   GtkStatusIconPrivate *priv;
2513   GtkTrayIcon *tray_icon;
2514   GtkWidget *widget;
2515   GdkScreen *screen;
2516   GtkTextDirection direction;
2517   GtkRequisition menu_req;
2518   GdkRectangle monitor;
2519   gint monitor_num, height, width, xoffset, yoffset;
2520 
2521   g_return_if_fail (GTK_IS_MENU (menu));
2522   g_return_if_fail (GTK_IS_STATUS_ICON (user_data));
2523 
2524   status_icon = GTK_STATUS_ICON (user_data);
2525   priv = status_icon->priv;
2526   tray_icon = GTK_TRAY_ICON (priv->tray_icon);
2527   widget = priv->tray_icon;
2528 
2529   direction = gtk_widget_get_direction (widget);
2530 
2531   screen = gtk_widget_get_screen (widget);
2532   gtk_menu_set_screen (menu, screen);
2533 
2534   monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window);
2535   if (monitor_num < 0)
2536     monitor_num = 0;
2537   gtk_menu_set_monitor (menu, monitor_num);
2538 
2539   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
2540 
2541   gdk_window_get_origin (widget->window, x, y);
2542 
2543   gtk_widget_size_request (GTK_WIDGET (menu), &menu_req);
2544 
2545   if (_gtk_tray_icon_get_orientation (tray_icon) == GTK_ORIENTATION_VERTICAL)
2546     {
2547       width = 0;
2548       height = widget->allocation.height;
2549       xoffset = widget->allocation.width;
2550       yoffset = 0;
2551     }
2552   else
2553     {
2554       width = widget->allocation.width;
2555       height = 0;
2556       xoffset = 0;
2557       yoffset = widget->allocation.height;
2558     }
2559 
2560   if (direction == GTK_TEXT_DIR_RTL)
2561     {
2562       if ((*x - (menu_req.width - width)) >= monitor.x)
2563         *x -= menu_req.width - width;
2564       else if ((*x + xoffset + menu_req.width) < (monitor.x + monitor.width))
2565         *x += xoffset;
2566       else if ((monitor.x + monitor.width - (*x + xoffset)) < *x)
2567         *x -= menu_req.width - width;
2568       else
2569         *x += xoffset;
2570     }
2571   else
2572     {
2573       if ((*x + xoffset + menu_req.width) < (monitor.x + monitor.width))
2574         *x += xoffset;
2575       else if ((*x - (menu_req.width - width)) >= monitor.x)
2576         *x -= menu_req.width - width;
2577       else if ((monitor.x + monitor.width - (*x + xoffset)) > *x)
2578         *x += xoffset;
2579       else
2580         *x -= menu_req.width - width;
2581     }
2582 
2583   if ((*y + yoffset + menu_req.height) < (monitor.y + monitor.height))
2584     *y += yoffset;
2585   else if ((*y - (menu_req.height - height)) >= monitor.y)
2586     *y -= menu_req.height - height;
2587   else if (monitor.y + monitor.height - (*y + yoffset) > *y)
2588     *y += yoffset;
2589   else
2590     *y -= menu_req.height - height;
2591 
2592   *push_in = FALSE;
2593 #endif /* GDK_WINDOWING_X11 */
2594 
2595 #ifdef GDK_WINDOWING_WIN32
2596   GtkStatusIcon *status_icon;
2597   GtkStatusIconPrivate *priv;
2598   GtkRequisition menu_req;
2599 
2600   g_return_if_fail (GTK_IS_MENU (menu));
2601   g_return_if_fail (GTK_IS_STATUS_ICON (user_data));
2602 
2603   status_icon = GTK_STATUS_ICON (user_data);
2604   priv = status_icon->priv;
2605 
2606   gtk_widget_size_request (GTK_WIDGET (menu), &menu_req);
2607 
2608   *x = priv->last_click_x;
2609   *y = priv->taskbar_top - menu_req.height;
2610 
2611   *push_in = TRUE;
2612 #endif
2613 }
2614 
2615 /**
2616  * gtk_status_icon_get_geometry:
2617  * @status_icon: a #GtkStatusIcon
2618  * @screen: (out) (transfer none) (allow-none): return location for the screen, or %NULL if the
2619  *          information is not needed
2620  * @area: (out) (allow-none): return location for the area occupied by the status
2621  *        icon, or %NULL
2622  * @orientation: (out) (allow-none): return location for the orientation of the panel
2623  *    in which the status icon is embedded, or %NULL. A panel
2624  *    at the top or bottom of the screen is horizontal, a panel
2625  *    at the left or right is vertical.
2626  *
2627  * Obtains information about the location of the status icon
2628  * on screen. This information can be used to e.g. position
2629  * popups like notification bubbles.
2630  *
2631  * See gtk_status_icon_position_menu() for a more convenient
2632  * alternative for positioning menus.
2633  *
2634  * Note that some platforms do not allow GTK+ to provide
2635  * this information, and even on platforms that do allow it,
2636  * the information is not reliable unless the status icon
2637  * is embedded in a notification area, see
2638  * gtk_status_icon_is_embedded().
2639  *
2640  * Return value: %TRUE if the location information has
2641  *               been filled in
2642  *
2643  * Since: 2.10
2644  */
2645 gboolean
gtk_status_icon_get_geometry(GtkStatusIcon * status_icon,GdkScreen ** screen,GdkRectangle * area,GtkOrientation * orientation)2646 gtk_status_icon_get_geometry (GtkStatusIcon    *status_icon,
2647 			      GdkScreen       **screen,
2648 			      GdkRectangle     *area,
2649 			      GtkOrientation   *orientation)
2650 {
2651 #ifdef GDK_WINDOWING_X11
2652   GtkWidget *widget;
2653   GtkStatusIconPrivate *priv;
2654   gint x, y;
2655 
2656   g_return_val_if_fail (GTK_IS_STATUS_ICON (status_icon), FALSE);
2657 
2658   priv = status_icon->priv;
2659   widget = priv->tray_icon;
2660 
2661   if (screen)
2662     *screen = gtk_widget_get_screen (widget);
2663 
2664   if (area)
2665     {
2666       gdk_window_get_origin (widget->window, &x, &y);
2667       area->x = x;
2668       area->y = y;
2669       area->width = widget->allocation.width;
2670       area->height = widget->allocation.height;
2671     }
2672 
2673   if (orientation)
2674     *orientation = _gtk_tray_icon_get_orientation (GTK_TRAY_ICON (widget));
2675 
2676   return TRUE;
2677 #else
2678   return FALSE;
2679 #endif /* GDK_WINDOWING_X11 */
2680 }
2681 
2682 /**
2683  * gtk_status_icon_set_has_tooltip:
2684  * @status_icon: a #GtkStatusIcon
2685  * @has_tooltip: whether or not @status_icon has a tooltip
2686  *
2687  * Sets the has-tooltip property on @status_icon to @has_tooltip.
2688  * See #GtkStatusIcon:has-tooltip for more information.
2689  *
2690  * Since: 2.16
2691  */
2692 void
gtk_status_icon_set_has_tooltip(GtkStatusIcon * status_icon,gboolean has_tooltip)2693 gtk_status_icon_set_has_tooltip (GtkStatusIcon *status_icon,
2694 				 gboolean       has_tooltip)
2695 {
2696   GtkStatusIconPrivate *priv;
2697 
2698   g_return_if_fail (GTK_IS_STATUS_ICON (status_icon));
2699 
2700   priv = status_icon->priv;
2701 
2702 #ifdef GDK_WINDOWING_X11
2703   gtk_widget_set_has_tooltip (priv->tray_icon, has_tooltip);
2704 #endif
2705 #ifdef GDK_WINDOWING_WIN32
2706   if (!has_tooltip && priv->tooltip_text)
2707     gtk_status_icon_set_tooltip_text (status_icon, NULL);
2708 #endif
2709 #ifdef GDK_WINDOWING_QUARTZ
2710   if (!has_tooltip && priv->tooltip_text)
2711     gtk_status_icon_set_tooltip_text (status_icon, NULL);
2712 #endif
2713 }
2714 
2715 /**
2716  * gtk_status_icon_get_has_tooltip:
2717  * @status_icon: a #GtkStatusIcon
2718  *
2719  * Returns the current value of the has-tooltip property.
2720  * See #GtkStatusIcon:has-tooltip for more information.
2721  *
2722  * Return value: current value of has-tooltip on @status_icon.
2723  *
2724  * Since: 2.16
2725  */
2726 gboolean
gtk_status_icon_get_has_tooltip(GtkStatusIcon * status_icon)2727 gtk_status_icon_get_has_tooltip (GtkStatusIcon *status_icon)
2728 {
2729   GtkStatusIconPrivate *priv;
2730   gboolean has_tooltip = FALSE;
2731 
2732   g_return_val_if_fail (GTK_IS_STATUS_ICON (status_icon), FALSE);
2733 
2734   priv = status_icon->priv;
2735 
2736 #ifdef GDK_WINDOWING_X11
2737   has_tooltip = gtk_widget_get_has_tooltip (priv->tray_icon);
2738 #endif
2739 #ifdef GDK_WINDOWING_WIN32
2740   has_tooltip = (priv->tooltip_text != NULL);
2741 #endif
2742 #ifdef GDK_WINDOWING_QUARTZ
2743   has_tooltip = (priv->tooltip_text != NULL);
2744 #endif
2745 
2746   return has_tooltip;
2747 }
2748 
2749 /**
2750  * gtk_status_icon_set_tooltip_text:
2751  * @status_icon: a #GtkStatusIcon
2752  * @text: the contents of the tooltip for @status_icon
2753  *
2754  * Sets @text as the contents of the tooltip.
2755  *
2756  * This function will take care of setting #GtkStatusIcon:has-tooltip to
2757  * %TRUE and of the default handler for the #GtkStatusIcon::query-tooltip
2758  * signal.
2759  *
2760  * See also the #GtkStatusIcon:tooltip-text property and
2761  * gtk_tooltip_set_text().
2762  *
2763  * Since: 2.16
2764  */
2765 void
gtk_status_icon_set_tooltip_text(GtkStatusIcon * status_icon,const gchar * text)2766 gtk_status_icon_set_tooltip_text (GtkStatusIcon *status_icon,
2767 				  const gchar   *text)
2768 {
2769   GtkStatusIconPrivate *priv;
2770 
2771   g_return_if_fail (GTK_IS_STATUS_ICON (status_icon));
2772 
2773   priv = status_icon->priv;
2774 
2775 #ifdef GDK_WINDOWING_X11
2776 
2777   gtk_widget_set_tooltip_text (priv->tray_icon, text);
2778 
2779 #endif
2780 #ifdef GDK_WINDOWING_WIN32
2781   if (text == NULL)
2782     priv->nid.uFlags &= ~NIF_TIP;
2783   else
2784     {
2785       WCHAR *wcs = g_utf8_to_utf16 (text, -1, NULL, NULL, NULL);
2786 
2787       priv->nid.uFlags |= NIF_TIP;
2788       wcsncpy (priv->nid.szTip, wcs, G_N_ELEMENTS (priv->nid.szTip) - 1);
2789       priv->nid.szTip[G_N_ELEMENTS (priv->nid.szTip) - 1] = 0;
2790       g_free (wcs);
2791     }
2792   if (priv->nid.hWnd != NULL && priv->visible)
2793     if (!Shell_NotifyIconW (NIM_MODIFY, &priv->nid))
2794       g_warning (G_STRLOC ": Shell_NotifyIconW(NIM_MODIFY) failed");
2795 
2796   g_free (priv->tooltip_text);
2797   priv->tooltip_text = g_strdup (text);
2798 #endif
2799 #ifdef GDK_WINDOWING_QUARTZ
2800   QUARTZ_POOL_ALLOC;
2801   [priv->status_item setToolTip:text];
2802   QUARTZ_POOL_RELEASE;
2803 
2804   g_free (priv->tooltip_text);
2805   priv->tooltip_text = g_strdup (text);
2806 #endif
2807 }
2808 
2809 /**
2810  * gtk_status_icon_get_tooltip_text:
2811  * @status_icon: a #GtkStatusIcon
2812  *
2813  * Gets the contents of the tooltip for @status_icon.
2814  *
2815  * Return value: the tooltip text, or %NULL. You should free the
2816  *   returned string with g_free() when done.
2817  *
2818  * Since: 2.16
2819  */
2820 gchar *
gtk_status_icon_get_tooltip_text(GtkStatusIcon * status_icon)2821 gtk_status_icon_get_tooltip_text (GtkStatusIcon *status_icon)
2822 {
2823   GtkStatusIconPrivate *priv;
2824   gchar *tooltip_text = NULL;
2825 
2826   g_return_val_if_fail (GTK_IS_STATUS_ICON (status_icon), NULL);
2827 
2828   priv = status_icon->priv;
2829 
2830 #ifdef GDK_WINDOWING_X11
2831   tooltip_text = gtk_widget_get_tooltip_text (priv->tray_icon);
2832 #endif
2833 #ifdef GDK_WINDOWING_WIN32
2834   if (priv->tooltip_text)
2835     tooltip_text = g_strdup (priv->tooltip_text);
2836 #endif
2837 #ifdef GDK_WINDOWING_QUARTZ
2838   if (priv->tooltip_text)
2839     tooltip_text = g_strdup (priv->tooltip_text);
2840 #endif
2841 
2842   return tooltip_text;
2843 }
2844 
2845 /**
2846  * gtk_status_icon_set_tooltip_markup:
2847  * @status_icon: a #GtkStatusIcon
2848  * @markup: (allow-none): the contents of the tooltip for @status_icon, or %NULL
2849  *
2850  * Sets @markup as the contents of the tooltip, which is marked up with
2851  *  the <link linkend="PangoMarkupFormat">Pango text markup language</link>.
2852  *
2853  * This function will take care of setting #GtkStatusIcon:has-tooltip to %TRUE
2854  * and of the default handler for the #GtkStatusIcon::query-tooltip signal.
2855  *
2856  * See also the #GtkStatusIcon:tooltip-markup property and
2857  * gtk_tooltip_set_markup().
2858  *
2859  * Since: 2.16
2860  */
2861 void
gtk_status_icon_set_tooltip_markup(GtkStatusIcon * status_icon,const gchar * markup)2862 gtk_status_icon_set_tooltip_markup (GtkStatusIcon *status_icon,
2863 				    const gchar   *markup)
2864 {
2865   GtkStatusIconPrivate *priv;
2866 #ifndef GDK_WINDOWING_X11
2867   gchar *text = NULL;
2868 #endif
2869 
2870   g_return_if_fail (GTK_IS_STATUS_ICON (status_icon));
2871 
2872   priv = status_icon->priv;
2873 
2874 #ifdef GDK_WINDOWING_X11
2875   gtk_widget_set_tooltip_markup (priv->tray_icon, markup);
2876 #endif
2877 #ifdef GDK_WINDOWING_WIN32
2878   if (markup)
2879     pango_parse_markup (markup, -1, 0, NULL, &text, NULL, NULL);
2880   gtk_status_icon_set_tooltip_text (status_icon, text);
2881   g_free (text);
2882 #endif
2883 #ifdef GDK_WINDOWING_QUARTZ
2884   if (markup)
2885     pango_parse_markup (markup, -1, 0, NULL, &text, NULL, NULL);
2886   gtk_status_icon_set_tooltip_text (status_icon, text);
2887   g_free (text);
2888 #endif
2889 }
2890 
2891 /**
2892  * gtk_status_icon_get_tooltip_markup:
2893  * @status_icon: a #GtkStatusIcon
2894  *
2895  * Gets the contents of the tooltip for @status_icon.
2896  *
2897  * Return value: the tooltip text, or %NULL. You should free the
2898  *   returned string with g_free() when done.
2899  *
2900  * Since: 2.16
2901  */
2902 gchar *
gtk_status_icon_get_tooltip_markup(GtkStatusIcon * status_icon)2903 gtk_status_icon_get_tooltip_markup (GtkStatusIcon *status_icon)
2904 {
2905   GtkStatusIconPrivate *priv;
2906   gchar *markup = NULL;
2907 
2908   g_return_val_if_fail (GTK_IS_STATUS_ICON (status_icon), NULL);
2909 
2910   priv = status_icon->priv;
2911 
2912 #ifdef GDK_WINDOWING_X11
2913   markup = gtk_widget_get_tooltip_markup (priv->tray_icon);
2914 #endif
2915 #ifdef GDK_WINDOWING_WIN32
2916   if (priv->tooltip_text)
2917     markup = g_markup_escape_text (priv->tooltip_text, -1);
2918 #endif
2919 #ifdef GDK_WINDOWING_QUARTZ
2920   if (priv->tooltip_text)
2921     markup = g_markup_escape_text (priv->tooltip_text, -1);
2922 #endif
2923 
2924   return markup;
2925 }
2926 
2927 /**
2928  * gtk_status_icon_get_x11_window_id:
2929  * @status_icon: a #GtkStatusIcon
2930  *
2931  * This function is only useful on the X11/freedesktop.org platform.
2932  * It returns a window ID for the widget in the underlying
2933  * status icon implementation.  This is useful for the Galago
2934  * notification service, which can send a window ID in the protocol
2935  * in order for the server to position notification windows
2936  * pointing to a status icon reliably.
2937  *
2938  * This function is not intended for other use cases which are
2939  * more likely to be met by one of the non-X11 specific methods, such
2940  * as gtk_status_icon_position_menu().
2941  *
2942  * Return value: An 32 bit unsigned integer identifier for the
2943  * underlying X11 Window
2944  *
2945  * Since: 2.14
2946  */
2947 guint32
gtk_status_icon_get_x11_window_id(GtkStatusIcon * status_icon)2948 gtk_status_icon_get_x11_window_id (GtkStatusIcon *status_icon)
2949 {
2950 #ifdef GDK_WINDOWING_X11
2951   gtk_widget_realize (GTK_WIDGET (status_icon->priv->tray_icon));
2952   return GDK_WINDOW_XID (GTK_WIDGET (status_icon->priv->tray_icon)->window);
2953 #else
2954   return 0;
2955 #endif
2956 }
2957 
2958 /**
2959  * gtk_status_icon_set_title:
2960  * @status_icon: a #GtkStatusIcon
2961  * @title: the title
2962  *
2963  * Sets the title of this tray icon.
2964  * This should be a short, human-readable, localized string
2965  * describing the tray icon. It may be used by tools like screen
2966  * readers to render the tray icon.
2967  *
2968  * Since: 2.18
2969  */
2970 void
gtk_status_icon_set_title(GtkStatusIcon * status_icon,const gchar * title)2971 gtk_status_icon_set_title (GtkStatusIcon *status_icon,
2972                            const gchar   *title)
2973 {
2974   GtkStatusIconPrivate *priv;
2975 
2976   g_return_if_fail (GTK_IS_STATUS_ICON (status_icon));
2977 
2978   priv = status_icon->priv;
2979 
2980 #ifdef GDK_WINDOWING_X11
2981   gtk_window_set_title (GTK_WINDOW (priv->tray_icon), title);
2982 #endif
2983 #ifdef GDK_WINDOWING_QUARTZ
2984   g_free (priv->title);
2985   priv->title = g_strdup (title);
2986 #endif
2987 #ifdef GDK_WINDOWING_WIN32
2988   g_free (priv->title);
2989   priv->title = g_strdup (title);
2990 #endif
2991 
2992   g_object_notify (G_OBJECT (status_icon), "title");
2993 }
2994 
2995 /**
2996  * gtk_status_icon_get_title:
2997  * @status_icon: a #GtkStatusIcon
2998  *
2999  * Gets the title of this tray icon. See gtk_status_icon_set_title().
3000  *
3001  * Returns: the title of the status icon
3002  *
3003  * Since: 2.18
3004  */
3005 const gchar *
gtk_status_icon_get_title(GtkStatusIcon * status_icon)3006 gtk_status_icon_get_title (GtkStatusIcon *status_icon)
3007 {
3008   GtkStatusIconPrivate *priv;
3009 
3010   g_return_val_if_fail (GTK_IS_STATUS_ICON (status_icon), NULL);
3011 
3012   priv = status_icon->priv;
3013 
3014 #ifdef GDK_WINDOWING_X11
3015   return gtk_window_get_title (GTK_WINDOW (priv->tray_icon));
3016 #endif
3017 #ifdef GDK_WINDOWING_QUARTZ
3018   return priv->title;
3019 #endif
3020 #ifdef GDK_WINDOWING_WIN32
3021   return priv->title;
3022 #endif
3023 }
3024 
3025 
3026 /**
3027  * gtk_status_icon_set_name:
3028  * @status_icon: a #GtkStatusIcon
3029  * @name: the name
3030  *
3031  * Sets the name of this tray icon.
3032  * This should be a string identifying this icon. It is may be
3033  * used for sorting the icons in the tray and will not be shown to
3034  * the user.
3035  *
3036  * Since: 2.20
3037  */
3038 void
gtk_status_icon_set_name(GtkStatusIcon * status_icon,const gchar * name)3039 gtk_status_icon_set_name (GtkStatusIcon *status_icon,
3040                           const gchar   *name)
3041 {
3042   GtkStatusIconPrivate *priv;
3043 
3044   g_return_if_fail (GTK_IS_STATUS_ICON (status_icon));
3045 
3046   priv = status_icon->priv;
3047 
3048 #ifdef GDK_WINDOWING_X11
3049   if (gtk_widget_get_realized (priv->tray_icon))
3050     {
3051       /* gtk_window_set_wmclass() only operates on non-realized windows,
3052        * so temporarily unrealize the tray here
3053        */
3054       gtk_widget_hide (priv->tray_icon);
3055       gtk_widget_unrealize (priv->tray_icon);
3056       gtk_window_set_wmclass (GTK_WINDOW (priv->tray_icon), name, name);
3057       gtk_widget_show (priv->tray_icon);
3058     }
3059   else
3060     gtk_window_set_wmclass (GTK_WINDOW (priv->tray_icon), name, name);
3061 #endif
3062 }
3063 
3064 
3065 #define __GTK_STATUS_ICON_C__
3066 #include "gtkaliasdef.c"
3067