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