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