]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtktoolbutton.c
Merge branch 'master' into toolpalette
[~andy/gtk] / gtk / gtktoolbutton.c
index a38da3ab3149dedf6e15f4b303f4d5a012461e57..38055e06cf6bf27ec1945b5bfcba3ff6c18ecdc8 100644 (file)
@@ -20,7 +20,7 @@
  * Boston, MA 02111-1307, USA.
  */
 
-#include <config.h>
+#include "config.h"
 #include "gtktoolbutton.h"
 #include "gtkbutton.h"
 #include "gtkhbox.h"
@@ -32,6 +32,7 @@
 #include "gtkvbox.h"
 #include "gtkintl.h"
 #include "gtktoolbar.h"
+#include "gtkactivatable.h"
 #include "gtkprivate.h"
 #include "gtkalias.h"
 
@@ -77,11 +78,14 @@ static void gtk_tool_button_style_set      (GtkWidget          *widget,
                                            GtkStyle           *prev_style);
 
 static void gtk_tool_button_construct_contents (GtkToolItem *tool_item);
-      
-static GObjectClass *parent_class = NULL;
-static guint         toolbutton_signals[LAST_SIGNAL] = { 0 };
 
-#define GTK_TOOL_BUTTON_GET_PRIVATE(obj)(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_TOOL_BUTTON, GtkToolButtonPrivate))
+static void gtk_tool_button_activatable_interface_init (GtkActivatableIface  *iface);
+static void gtk_tool_button_update                     (GtkActivatable       *activatable,
+                                                       GtkAction            *action,
+                                                       const gchar          *property_name);
+static void gtk_tool_button_sync_action_properties     (GtkActivatable       *activatable,
+                                                       GtkAction            *action);
+
 
 struct _GtkToolButtonPrivate
 {
@@ -92,24 +96,44 @@ struct _GtkToolButtonPrivate
   gchar *label_text;
   GtkWidget *label_widget;
   GtkWidget *icon_widget;
-  
+
+  GtkSizeGroup *text_size_group;
+
   guint use_underline : 1;
   guint contents_invalid : 1;
 };
 
+static GObjectClass        *parent_class = NULL;
+static GtkActivatableIface *parent_activatable_iface;
+static guint                toolbutton_signals[LAST_SIGNAL] = { 0 };
+
+#define GTK_TOOL_BUTTON_GET_PRIVATE(obj)(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_TOOL_BUTTON, GtkToolButtonPrivate))
+
 GType
 gtk_tool_button_get_type (void)
 {
-  static GtkType type = 0;
-
+  static GType type = 0;
+  
   if (!type)
-    type = g_type_register_static_simple (GTK_TYPE_TOOL_ITEM,
-                                         I_("GtkToolButton"),
-                                         sizeof (GtkToolButtonClass),
-                                         (GClassInitFunc) gtk_tool_button_class_init,
-                                         sizeof (GtkToolButton),
-                                         (GInstanceInitFunc) gtk_tool_button_init,
-                                         0);
+    {
+      const GInterfaceInfo activatable_info =
+      {
+        (GInterfaceInitFunc) gtk_tool_button_activatable_interface_init,
+        (GInterfaceFinalizeFunc) NULL,
+        NULL
+      };
+
+      type = g_type_register_static_simple (GTK_TYPE_TOOL_ITEM,
+                                           I_("GtkToolButton"),
+                                           sizeof (GtkToolButtonClass),
+                                           (GClassInitFunc) gtk_tool_button_class_init,
+                                           sizeof (GtkToolButton),
+                                           (GInstanceInitFunc) gtk_tool_button_init,
+                                           0);
+
+      g_type_add_interface_static (type, GTK_TYPE_ACTIVATABLE,
+                                   &activatable_info);
+    }
   return type;
 }
 
@@ -296,6 +320,8 @@ gtk_tool_button_construct_contents (GtkToolItem *tool_item)
   GtkIconSize icon_size;
   GtkWidget *box = NULL;
   guint icon_spacing;
+  GtkOrientation text_orientation = GTK_ORIENTATION_HORIZONTAL;
+  GtkSizeGroup *size_group = NULL;
 
   button->priv->contents_invalid = FALSE;
 
@@ -333,7 +359,8 @@ gtk_tool_button_construct_contents (GtkToolItem *tool_item)
 
   if (style == GTK_TOOLBAR_BOTH_HORIZ &&
       (gtk_tool_item_get_is_important (GTK_TOOL_ITEM (button)) ||
-       gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button)) == GTK_ORIENTATION_VERTICAL))
+       gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button)) == GTK_ORIENTATION_VERTICAL ||
+       gtk_tool_item_get_text_orientation (GTK_TOOL_ITEM (button)) == GTK_ORIENTATION_VERTICAL))
     {
       need_label = TRUE;
     }
@@ -393,6 +420,28 @@ gtk_tool_button_construct_contents (GtkToolItem *tool_item)
          
          gtk_widget_show (label);
        }
+
+      gtk_label_set_ellipsize (GTK_LABEL (label),
+                              gtk_tool_item_get_ellipsize_mode (GTK_TOOL_ITEM (button)));
+      text_orientation = gtk_tool_item_get_text_orientation (GTK_TOOL_ITEM (button));
+      if (text_orientation == GTK_ORIENTATION_HORIZONTAL)
+       {
+          gtk_label_set_angle (GTK_LABEL (label), 0);
+          gtk_misc_set_alignment (GTK_MISC (label),
+                                  gtk_tool_item_get_text_alignment (GTK_TOOL_ITEM (button)),
+                                  0.5);
+        }
+      else
+        {
+          gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_NONE);
+         if (gtk_widget_get_direction (GTK_WIDGET (tool_item)) == GTK_TEXT_DIR_RTL)
+           gtk_label_set_angle (GTK_LABEL (label), -90);
+         else
+           gtk_label_set_angle (GTK_LABEL (label), 90);
+          gtk_misc_set_alignment (GTK_MISC (label),
+                                  0.5,
+                                  1 - gtk_tool_item_get_text_alignment (GTK_TOOL_ITEM (button)));
+        }
     }
 
   icon_size = gtk_tool_item_get_icon_size (GTK_TOOL_ITEM (button));
@@ -420,6 +469,22 @@ gtk_tool_button_construct_contents (GtkToolItem *tool_item)
          icon = gtk_image_new_from_icon_name (button->priv->icon_name, icon_size);
          gtk_widget_show (icon);
        }
+
+      if (icon && text_orientation == GTK_ORIENTATION_HORIZONTAL)
+       gtk_misc_set_alignment (GTK_MISC (icon),
+                               1.0 - gtk_tool_item_get_text_alignment (GTK_TOOL_ITEM (button)),
+                               0.5);
+      else if (icon)
+       gtk_misc_set_alignment (GTK_MISC (icon),
+                               0.5,
+                               gtk_tool_item_get_text_alignment (GTK_TOOL_ITEM (button)));
+
+      if (icon)
+       {
+         size_group = gtk_tool_item_get_text_size_group (GTK_TOOL_ITEM (button));
+         if (size_group != NULL)
+           gtk_size_group_add_widget (size_group, icon);
+       }
     }
 
   switch (style)
@@ -430,19 +495,33 @@ gtk_tool_button_construct_contents (GtkToolItem *tool_item)
       break;
 
     case GTK_TOOLBAR_BOTH:
-      box = gtk_vbox_new (FALSE, icon_spacing);
+      if (text_orientation == GTK_ORIENTATION_HORIZONTAL)
+       box = gtk_vbox_new (FALSE, icon_spacing);
+      else
+       box = gtk_hbox_new (FALSE, icon_spacing);
       if (icon)
        gtk_box_pack_start (GTK_BOX (box), icon, TRUE, TRUE, 0);
-      gtk_box_pack_start (GTK_BOX (box), label, FALSE, TRUE, 0);
+      gtk_box_pack_end (GTK_BOX (box), label, FALSE, TRUE, 0);
       gtk_container_add (GTK_CONTAINER (button->priv->button), box);
       break;
 
     case GTK_TOOLBAR_BOTH_HORIZ:
-      box = gtk_hbox_new (FALSE, icon_spacing);
-      if (icon)
-       gtk_box_pack_start (GTK_BOX (box), icon, label? FALSE : TRUE, TRUE, 0);
-      if (label)
-       gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
+      if (text_orientation == GTK_ORIENTATION_HORIZONTAL)
+       {
+         box = gtk_hbox_new (FALSE, icon_spacing);
+         if (icon)
+           gtk_box_pack_start (GTK_BOX (box), icon, label? FALSE : TRUE, TRUE, 0);
+         if (label)
+           gtk_box_pack_end (GTK_BOX (box), label, TRUE, TRUE, 0);
+       }
+      else
+       {
+         box = gtk_vbox_new (FALSE, icon_spacing);
+         if (icon)
+           gtk_box_pack_end (GTK_BOX (box), icon, label ? FALSE : TRUE, TRUE, 0);
+         if (label)
+           gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
+       }
       gtk_container_add (GTK_CONTAINER (button->priv->button), box);
       break;
 
@@ -579,6 +658,12 @@ clone_image_menu_size (GtkImage *image, GtkSettings *settings)
       gtk_image_get_icon_set (image, &icon_set, NULL);
       return gtk_image_new_from_icon_set (icon_set, GTK_ICON_SIZE_MENU);
     }
+  else if (storage_type == GTK_IMAGE_GICON)
+    {
+      GIcon *icon;
+      gtk_image_get_gicon (image, &icon, NULL);
+      return gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_MENU);
+    }
   else if (storage_type == GTK_IMAGE_PIXBUF)
     {
       gint width, height;
@@ -614,6 +699,9 @@ gtk_tool_button_create_menu_proxy (GtkToolItem *item)
   gboolean use_mnemonic = TRUE;
   const char *label;
 
+  if (_gtk_tool_item_create_menu_proxy (item))
+    return TRUE;
   if (GTK_IS_LABEL (button->priv->label_widget))
     {
       label = gtk_label_get_label (GTK_LABEL (button->priv->label_widget));
@@ -666,6 +754,13 @@ static void
 button_clicked (GtkWidget     *widget,
                GtkToolButton *button)
 {
+  GtkAction *action;
+
+  action = gtk_activatable_get_related_action (GTK_ACTIVATABLE (button));
+  
+  if (action)
+    gtk_action_activate (action);
+
   g_signal_emit_by_name (button, "clicked");
 }
 
@@ -698,6 +793,104 @@ gtk_tool_button_style_set (GtkWidget *widget,
   gtk_tool_button_update_icon_spacing (GTK_TOOL_BUTTON (widget));
 }
 
+static void 
+gtk_tool_button_activatable_interface_init (GtkActivatableIface  *iface)
+{
+  parent_activatable_iface = g_type_interface_peek_parent (iface);
+  iface->update = gtk_tool_button_update;
+  iface->sync_action_properties = gtk_tool_button_sync_action_properties;
+}
+
+static void
+gtk_tool_button_update (GtkActivatable *activatable,
+                       GtkAction      *action,
+                       const gchar    *property_name)
+{
+  GtkToolButton *button;
+  GtkWidget *image;
+
+  parent_activatable_iface->update (activatable, action, property_name);
+
+  if (!gtk_activatable_get_use_action_appearance (activatable))
+    return;
+
+  button = GTK_TOOL_BUTTON (activatable);
+  
+  if (strcmp (property_name, "short-label") == 0)
+    gtk_tool_button_set_label (button, gtk_action_get_short_label (action));
+  else if (strcmp (property_name, "stock-id") == 0)
+    gtk_tool_button_set_stock_id (button, gtk_action_get_stock_id (action));
+  else if (strcmp (property_name, "gicon") == 0)
+    {
+      const gchar *stock_id = gtk_action_get_stock_id (action);
+      GIcon *icon = gtk_action_get_gicon (action);
+      GtkIconSize icon_size = GTK_ICON_SIZE_BUTTON;
+
+      if ((stock_id && gtk_icon_factory_lookup_default (stock_id)) || !icon)
+       image = NULL;
+      else 
+       {   
+         image = gtk_tool_button_get_icon_widget (button);
+         icon_size = gtk_tool_item_get_icon_size (GTK_TOOL_ITEM (button));
+
+         if (!image)
+           image = gtk_image_new ();
+       }
+
+      gtk_tool_button_set_icon_widget (button, image);
+      gtk_image_set_from_gicon (GTK_IMAGE (image), icon, icon_size);
+
+    }
+  else if (strcmp (property_name, "icon-name") == 0)
+    gtk_tool_button_set_icon_name (button, gtk_action_get_icon_name (action));
+}
+
+static void
+gtk_tool_button_sync_action_properties (GtkActivatable *activatable,
+                                       GtkAction      *action)
+{
+  GtkToolButton *button;
+  GIcon         *icon;
+  const gchar   *stock_id;
+
+  parent_activatable_iface->sync_action_properties (activatable, action);
+
+  if (!action)
+    return;
+
+  if (!gtk_activatable_get_use_action_appearance (activatable))
+    return;
+
+  button = GTK_TOOL_BUTTON (activatable);
+  stock_id = gtk_action_get_stock_id (action);
+
+  gtk_tool_button_set_label (button, gtk_action_get_short_label (action));
+  gtk_tool_button_set_use_underline (button, TRUE);
+  gtk_tool_button_set_stock_id (button, stock_id);
+  gtk_tool_button_set_icon_name (button, gtk_action_get_icon_name (action));
+
+  if (stock_id && gtk_icon_factory_lookup_default (stock_id))
+      gtk_tool_button_set_icon_widget (button, NULL);
+  else if ((icon = gtk_action_get_gicon (action)) != NULL)
+    {
+      GtkIconSize icon_size = gtk_tool_item_get_icon_size (GTK_TOOL_ITEM (button));
+      GtkWidget  *image = gtk_tool_button_get_icon_widget (button);
+      
+      if (!image)
+       {
+         image = gtk_image_new ();
+         gtk_widget_show (image);
+         gtk_tool_button_set_icon_widget (button, image);
+       }
+
+      gtk_image_set_from_gicon (GTK_IMAGE (image), icon, icon_size);
+    }
+  else if (gtk_action_get_icon_name (action))
+    gtk_tool_button_set_icon_name (button, gtk_action_get_icon_name (action));
+  else
+    gtk_tool_button_set_label (button, gtk_action_get_short_label (action));
+}
+
 /**
  * gtk_tool_button_new_from_stock:
  * @stock_id: the name of the stock item 
@@ -770,6 +963,8 @@ gtk_tool_button_set_label (GtkToolButton *button,
                           const gchar   *label)
 {
   gchar *old_label;
+  gchar *elided_label;
+  AtkObject *accessible;
   
   g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
 
@@ -778,6 +973,14 @@ gtk_tool_button_set_label (GtkToolButton *button,
   button->priv->label_text = g_strdup (label);
   button->priv->contents_invalid = TRUE;     
 
+  if (label)
+    {
+      elided_label = _gtk_toolbar_elide_underscores (label);
+      accessible = gtk_widget_get_accessible (GTK_WIDGET (button->priv->button));
+      atk_object_set_name (accessible, elided_label);
+      g_free (elided_label);
+    }
+
   g_free (old_label);
  
   g_object_notify (G_OBJECT (button), "label");