]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkstatusicon.c
win32: Hack to make statusbar menus show up visible:
[~andy/gtk] / gtk / gtkstatusicon.c
index 7e7ab22a44c03100d0cdb7b429e9f523c3193c54..d8705604724e79460279a176c4263a023e65c775 100644 (file)
 
 #include "config.h"
 
-#include "gtkstatusicon.h"
-
 #include <string.h>
 
+#include "gtkstatusicon.h"
+
 #include "gtkintl.h"
 #include "gtkiconfactory.h"
 #include "gtkmain.h"
 #include "gtkmarshalers.h"
 #include "gtksizerequest.h"
-#include "gtktrayicon.h"
-
 #include "gtkprivate.h"
 #include "gtkwidget.h"
 #include "gtktooltip.h"
+#include "gtkicontheme.h"
+#include "gtklabel.h"
+#include "gtktypebuiltins.h"
 
 #ifdef GDK_WINDOWING_X11
 #include "gdk/x11/gdkx.h"
+#include "gtktrayicon.h"
 #endif
 
 #ifdef GDK_WINDOWING_WIN32
-#include "gtkicontheme.h"
-#include "gtklabel.h"
-
-#include "win32/gdkwin32.h"
+#include "gdk/win32/gdkwin32.h"
 #define WM_GTK_TRAY_NOTIFICATION (WM_USER+1)
 #endif
 
-#ifdef GDK_WINDOWING_QUARTZ
-#include "gtkicontheme.h"
-#include "gtklabel.h"
-#endif 
 
-#include "gdkkeysyms.h"
+/**
+ * SECTION:gtkstatusicon
+ * @Short_description: Display an icon in the system tray
+ * @Title: GtkStatusIcon
+ *
+ * The "system tray" or notification area is normally used for transient icons
+ * that indicate some special state. For example, a system tray icon might
+ * appear to tell the user that they have new mail, or have an incoming instant
+ * message, or something along those lines. The basic idea is that creating an
+ * icon in the notification area is less annoying than popping up a dialog.
+ *
+ * A #GtkStatusIcon object can be used to display an icon in a "system tray".
+ * The icon can have a tooltip, and the user can interact with it by
+ * activating it or popping up a context menu. Critical information should
+ * not solely be displayed in a #GtkStatusIcon, since it may not be
+ * visible (e.g. when the user doesn't have a notification area on his panel).
+ * This can be checked with gtk_status_icon_is_embedded().
+ *
+ * On X11, the implementation follows the freedesktop.org "System Tray"
+ * <ulink url="http://www.freedesktop.org/wiki/Specifications/systemtray-spec">specification</ulink>.
+ * Implementations of the "tray" side of this specification can
+ * be found e.g. in the GNOME 2 and KDE panel applications.
+ *
+ * Note that a GtkStatusIcon is <emphasis>not</emphasis> a widget, but just
+ * a #GObject. Making it a widget would be impractical, since the system tray
+ * on Win32 doesn't allow to embed arbitrary widgets.
+ */
+
 
 #define BLINK_TIMEOUT 500
 
@@ -114,6 +136,7 @@ struct _GtkStatusIconPrivate
 #ifdef GDK_WINDOWING_WIN32
   GtkWidget     *dummy_widget;
   NOTIFYICONDATAW nid;
+  gint          taskbar_top;
   gint         last_click_x, last_click_y;
   GtkOrientation orientation;
   gchar         *tooltip_text;
@@ -166,6 +189,7 @@ static void     gtk_status_icon_screen_changed   (GtkStatusIcon  *status_icon,
 static void     gtk_status_icon_embedded_changed (GtkStatusIcon *status_icon);
 static void     gtk_status_icon_orientation_changed (GtkStatusIcon *status_icon);
 static void     gtk_status_icon_padding_changed  (GtkStatusIcon *status_icon);
+static void     gtk_status_icon_icon_size_changed(GtkStatusIcon *status_icon);
 static void     gtk_status_icon_fg_changed       (GtkStatusIcon *status_icon);
 static void     gtk_status_icon_color_changed    (GtkTrayIcon   *tray,
                                                   GParamSpec    *pspec,
@@ -501,7 +525,8 @@ gtk_status_icon_class_init (GtkStatusIconClass *class)
   /**
    * GtkStatusIcon::button-press-event:
    * @status_icon: the object which received the signal
-   * @event: the #GdkEventButton which triggered this signal
+   * @event: (type Gdk.EventButton): the #GdkEventButton which triggered 
+   *                                 this signal
    *
    * The ::button-press-event signal will be emitted when a button
    * (typically from a mouse) is pressed.
@@ -527,7 +552,8 @@ gtk_status_icon_class_init (GtkStatusIconClass *class)
   /**
    * GtkStatusIcon::button-release-event:
    * @status_icon: the object which received the signal
-   * @event: the #GdkEventButton which triggered this signal
+   * @event: (type Gdk.EventButton): the #GdkEventButton which triggered 
+   *                                 this signal
    *
    * The ::button-release-event signal will be emitted when a button
    * (typically from a mouse) is released.
@@ -553,7 +579,8 @@ gtk_status_icon_class_init (GtkStatusIconClass *class)
   /**
    * GtkStatusIcon::scroll-event:
    * @status_icon: the object which received the signal.
-   * @event: the #GdkEventScroll which triggered this signal
+   * @event: (type Gdk.EventScroll): the #GdkEventScroll which triggered 
+   *                                 this signal
    *
    * The ::scroll-event signal is emitted when a button in the 4 to 7
    * range is pressed. Wheel mice are usually configured to generate
@@ -643,7 +670,7 @@ build_button_event (GtkStatusIconPrivate *priv,
   e->axes = NULL;
   e->state = 0;
   e->button = button;
-  e->device = gdk_display_get_default ()->core_pointer;
+  //FIXME: e->device = gdk_display_get_default ()->core_pointer;
   e->x_root = e->x;
   e->y_root = e->y;
 }
@@ -845,6 +872,8 @@ gtk_status_icon_init (GtkStatusIcon *status_icon)
                            G_CALLBACK (gtk_status_icon_orientation_changed), status_icon);
   g_signal_connect_swapped (priv->tray_icon, "notify::padding",
                            G_CALLBACK (gtk_status_icon_padding_changed), status_icon);
+  g_signal_connect_swapped (priv->tray_icon, "notify::icon-size",
+                           G_CALLBACK (gtk_status_icon_icon_size_changed), status_icon);
   g_signal_connect_swapped (priv->tray_icon, "notify::fg-color",
                             G_CALLBACK (gtk_status_icon_fg_changed), status_icon);
   g_signal_connect (priv->tray_icon, "notify::error-color",
@@ -868,6 +897,12 @@ gtk_status_icon_init (GtkStatusIcon *status_icon)
   gtk_container_add (GTK_CONTAINER (priv->tray_icon), priv->image);
   gtk_widget_show (priv->image);
 
+  /* Force-initialize the symbolic colors */
+  g_object_notify (G_OBJECT (priv->tray_icon), "fg-color");
+  g_object_notify (G_OBJECT (priv->tray_icon), "error-color");
+  g_object_notify (G_OBJECT (priv->tray_icon), "warning-color");
+  g_object_notify (G_OBJECT (priv->tray_icon), "success-color");
+
   g_signal_connect_swapped (priv->image, "size-allocate",
                            G_CALLBACK (gtk_status_icon_size_allocate), status_icon);
 
@@ -885,6 +920,8 @@ gtk_status_icon_init (GtkStatusIcon *status_icon)
       priv->orientation = GTK_ORIENTATION_VERTICAL;
     else
       priv->orientation = GTK_ORIENTATION_HORIZONTAL;
+
+    priv->taskbar_top = abd.rc.top;
   }
 
   priv->last_click_x = priv->last_click_y = 0;
@@ -903,6 +940,17 @@ gtk_status_icon_init (GtkStatusIcon *status_icon)
   priv->nid.uCallbackMessage = WM_GTK_TRAY_NOTIFICATION;
   priv->nid.uFlags = NIF_MESSAGE;
 
+  /* To help win7 identify the icon create it with an application "unique" tip */
+  if (g_get_prgname ())
+  {
+    WCHAR *wcs = g_utf8_to_utf16 (g_get_prgname (), -1, NULL, NULL, NULL);
+
+    priv->nid.uFlags |= NIF_TIP;
+    wcsncpy (priv->nid.szTip, wcs, G_N_ELEMENTS (priv->nid.szTip) - 1);
+    priv->nid.szTip[G_N_ELEMENTS (priv->nid.szTip) - 1] = 0;
+    g_free (wcs);
+  }
+
   if (!Shell_NotifyIconW (NIM_ADD, &priv->nid))
     {
       g_warning (G_STRLOC ": Shell_NotifyIcon(NIM_ADD) failed");
@@ -973,6 +1021,8 @@ gtk_status_icon_finalize (GObject *object)
                                        gtk_status_icon_orientation_changed, status_icon);
   g_signal_handlers_disconnect_by_func (priv->tray_icon,
                                        gtk_status_icon_padding_changed, status_icon);
+  g_signal_handlers_disconnect_by_func (priv->tray_icon,
+                                       gtk_status_icon_icon_size_changed, status_icon);
   g_signal_handlers_disconnect_by_func (priv->tray_icon,
                                         gtk_status_icon_fg_changed, status_icon);
   g_signal_handlers_disconnect_by_func (priv->tray_icon,
@@ -1182,7 +1232,7 @@ gtk_status_icon_new_from_pixbuf (GdkPixbuf *pixbuf)
 
 /**
  * gtk_status_icon_new_from_file:
- * @filename: a filename
+ * @filename: (type filename): a filename
  * 
  * Creates a status icon displaying the file @filename. 
  *
@@ -1423,10 +1473,9 @@ gtk_status_icon_update_image (GtkStatusIcon *status_icon)
 #ifdef GDK_WINDOWING_WIN32
        {
          GdkPixbuf *pixbuf =
-           gtk_widget_render_icon (priv->dummy_widget,
-                                   priv->image_data.stock_id,
-                                   GTK_ICON_SIZE_SMALL_TOOLBAR,
-                                   NULL);
+           gtk_widget_render_icon_pixbuf (priv->dummy_widget,
+                                           priv->image_data.stock_id,
+                                           GTK_ICON_SIZE_SMALL_TOOLBAR);
 
          prev_hicon = priv->nid.hIcon;
          priv->nid.hIcon = gdk_win32_pixbuf_to_hicon_libgtk_only (pixbuf);
@@ -1443,10 +1492,9 @@ gtk_status_icon_update_image (GtkStatusIcon *status_icon)
        {
          GdkPixbuf *pixbuf;
 
-         pixbuf = gtk_widget_render_icon (priv->dummy_widget,
-                                          priv->image_data.stock_id,
-                                          GTK_ICON_SIZE_SMALL_TOOLBAR,
-                                          NULL);
+         pixbuf = gtk_widget_render_icon_pixbuf (priv->dummy_widget,
+                                                  priv->image_data.stock_id,
+                                                  GTK_ICON_SIZE_SMALL_TOOLBAR);
          QUARTZ_POOL_ALLOC;
          [priv->status_item setImage:pixbuf];
          QUARTZ_POOL_RELEASE;
@@ -1634,15 +1682,36 @@ gtk_status_icon_padding_changed (GtkStatusIcon *status_icon)
   padding = _gtk_tray_icon_get_padding (GTK_TRAY_ICON (priv->tray_icon));
 
   if (orientation == GTK_ORIENTATION_HORIZONTAL)
-    gtk_misc_set_padding (GTK_MISC (priv->image), padding, 0);
+    {
+      gtk_widget_set_margin_left (priv->image, padding);
+      gtk_widget_set_margin_right (priv->image, padding);
+    }
   else
-    gtk_misc_set_padding (GTK_MISC (priv->image), 0, padding);
+    {
+      gtk_widget_set_margin_bottom (priv->image, padding);
+      gtk_widget_set_margin_top (priv->image, padding);
+    }
+}
+
+static void
+gtk_status_icon_icon_size_changed (GtkStatusIcon *status_icon)
+{
+  GtkStatusIconPrivate *priv = status_icon->priv;
+  gint icon_size;
+
+  icon_size = _gtk_tray_icon_get_icon_size (GTK_TRAY_ICON (priv->tray_icon));
+
+  if (icon_size != 0)
+    gtk_image_set_pixel_size (GTK_IMAGE (priv->image), icon_size);
+  else
+    gtk_image_set_pixel_size (GTK_IMAGE (priv->image), -1);
 }
 
 static void
 gtk_status_icon_embedded_changed (GtkStatusIcon *status_icon)
 {
   gtk_status_icon_padding_changed (status_icon);
+  gtk_status_icon_icon_size_changed (status_icon);
   g_object_notify (G_OBJECT (status_icon), "embedded");
 }
 
@@ -1657,10 +1726,13 @@ static void
 gtk_status_icon_fg_changed (GtkStatusIcon *status_icon)
 {
   GtkStatusIconPrivate *priv = status_icon->priv;
-  GdkColor color;
+  GdkRGBA *rgba;
 
-  g_object_get (priv->tray_icon, "fg-color", &color, NULL);
-  gtk_widget_modify_fg (priv->image, GTK_STATE_NORMAL, &color);
+  g_object_get (priv->tray_icon, "fg-color", &rgba, NULL);
+
+  gtk_widget_override_color (priv->image, GTK_STATE_FLAG_NORMAL, rgba);
+
+  gdk_rgba_free (rgba);
 }
 
 static void
@@ -1670,7 +1742,6 @@ gtk_status_icon_color_changed (GtkTrayIcon   *tray,
 {
   GtkStatusIconPrivate *priv = status_icon->priv;
   const gchar *name;
-  GdkColor color;
 
   switch (pspec->name[0])
     {
@@ -1690,8 +1761,13 @@ gtk_status_icon_color_changed (GtkTrayIcon   *tray,
 
   if (name)
     {
-      g_object_get (priv->tray_icon, pspec->name, &color, NULL);
-      gtk_widget_modify_symbolic_color (priv->image, name, &color);
+      GdkRGBA rgba;
+
+      g_object_get (priv->tray_icon, pspec->name, &rgba, NULL);
+
+      rgba.alpha = 1;
+
+      gtk_widget_override_symbolic_color (priv->image, name, &rgba);
     }
 }
 
@@ -1737,14 +1813,14 @@ gtk_status_icon_button_press (GtkStatusIcon  *status_icon,
   if (handled)
     return TRUE;
 
-  if (event->button == 1 && event->type == GDK_BUTTON_PRESS)
+  if (gdk_event_triggers_context_menu ((GdkEvent *) event))
     {
-      emit_activate_signal (status_icon);
+      emit_popup_menu_signal (status_icon, event->button, event->time);
       return TRUE;
     }
-  else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
+  else if (event->button == 1 && event->type == GDK_BUTTON_PRESS)
     {
-      emit_popup_menu_signal (status_icon, event->button, event->time);
+      emit_activate_signal (status_icon);
       return TRUE;
     }
 
@@ -1904,7 +1980,7 @@ gtk_status_icon_set_from_pixbuf (GtkStatusIcon *status_icon,
 /**
  * gtk_status_icon_set_from_file:
  * @status_icon: a #GtkStatusIcon
- * @filename: a filename
+ * @filename: (type filename): a filename
  * 
  * Makes @status_icon display the file @filename.
  * See gtk_status_icon_new_from_file() for details.
@@ -2062,7 +2138,7 @@ gtk_status_icon_get_pixbuf (GtkStatusIcon *status_icon)
  *
  * Since: 2.10
  **/
-G_CONST_RETURN gchar *
+const gchar *
 gtk_status_icon_get_stock (GtkStatusIcon *status_icon)
 {
   GtkStatusIconPrivate *priv;
@@ -2094,7 +2170,7 @@ gtk_status_icon_get_stock (GtkStatusIcon *status_icon)
  *
  * Since: 2.10
  **/
-G_CONST_RETURN gchar *
+const gchar *
 gtk_status_icon_get_icon_name (GtkStatusIcon *status_icon)
 {
   GtkStatusIconPrivate *priv;
@@ -2328,11 +2404,12 @@ gtk_status_icon_is_embedded (GtkStatusIcon *status_icon)
 /**
  * gtk_status_icon_position_menu:
  * @menu: the #GtkMenu
- * @x: return location for the x position
- * @y: return location for the y position
- * @push_in: whether the first menu item should be offset (pushed in) to be
- *           aligned with the menu popup position (only useful for GtkOptionMenu).
- * @user_data: the status icon to position the menu on
+ * @x: (out): return location for the x position
+ * @y: (out): return location for the y position
+ * @push_in: (out): whether the first menu item should be offset
+ *           (pushed in) to be aligned with the menu popup position
+ *           (only useful for GtkOptionMenu).
+ * @user_data: (type GtkStatusIcon): the status icon to position the menu on
  *
  * Menu positioning function to use with gtk_menu_popup()
  * to position @menu aligned to the status icon @user_data.
@@ -2439,6 +2516,7 @@ gtk_status_icon_position_menu (GtkMenu  *menu,
 #ifdef GDK_WINDOWING_WIN32
   GtkStatusIcon *status_icon;
   GtkStatusIconPrivate *priv;
+  GtkRequisition menu_req;
   
   g_return_if_fail (GTK_IS_MENU (menu));
   g_return_if_fail (GTK_IS_STATUS_ICON (user_data));
@@ -2446,8 +2524,11 @@ gtk_status_icon_position_menu (GtkMenu  *menu,
   status_icon = GTK_STATUS_ICON (user_data);
   priv = status_icon->priv;
 
+  gtk_widget_size_request (GTK_WIDGET (menu), &menu_req);
+
   *x = priv->last_click_x;
-  *y = priv->last_click_y;
+  *y = priv->taskbar_top - menu_req.height;
+
   *push_in = TRUE;
 #endif
 }
@@ -2455,14 +2536,14 @@ gtk_status_icon_position_menu (GtkMenu  *menu,
 /**
  * gtk_status_icon_get_geometry:
  * @status_icon: a #GtkStatusIcon
- * @screen: (out) (transfer none) (allow-none): return location for the screen, or %NULL if the
- *          information is not needed
- * @area: (out) (allow-none): return location for the area occupied by the status
- *        icon, or %NULL
- * @orientation: (out) (allow-none): return location for the orientation of the panel
- *    in which the status icon is embedded, or %NULL. A panel
- *    at the top or bottom of the screen is horizontal, a panel
- *    at the left or right is vertical.
+ * @screen: (out) (transfer none) (allow-none): return location for
+ *          the screen, or %NULL if the information is not needed
+ * @area: (out) (allow-none): return location for the area occupied by
+ *        the status icon, or %NULL
+ * @orientation: (out) (allow-none): return location for the
+ *    orientation of the panel in which the status icon is embedded,
+ *    or %NULL. A panel at the top or bottom of the screen is
+ *    horizontal, a panel at the left or right is vertical.
  *
  * Obtains information about the location of the status icon
  * on screen. This information can be used to e.g. position 
@@ -2846,7 +2927,7 @@ gtk_status_icon_set_title (GtkStatusIcon *status_icon,
  *
  * Since: 2.18
  */
-G_CONST_RETURN gchar *
+const gchar *
 gtk_status_icon_get_title (GtkStatusIcon *status_icon)
 {
   GtkStatusIconPrivate *priv;
@@ -2890,8 +2971,17 @@ gtk_status_icon_set_name (GtkStatusIcon *status_icon,
   priv = status_icon->priv;
 
 #ifdef GDK_WINDOWING_X11
-  gtk_window_set_wmclass (GTK_WINDOW (priv->tray_icon), name, name);
+  if (gtk_widget_get_realized (priv->tray_icon))
+    {
+      /* gtk_window_set_wmclass() only operates on non-realized windows,
+       * so temporarily unrealize the tray here
+       */
+      gtk_widget_hide (priv->tray_icon);
+      gtk_widget_unrealize (priv->tray_icon);
+      gtk_window_set_wmclass (GTK_WINDOW (priv->tray_icon), name, name);
+      gtk_widget_show (priv->tray_icon);
+    }
+  else
+    gtk_window_set_wmclass (GTK_WINDOW (priv->tray_icon), name, name);
 #endif
-
-  g_object_notify (G_OBJECT (status_icon), "name");
 }