]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkbutton.c
GtkBubbleWindow: rework how drawing is done
[~andy/gtk] / gtk / gtkbutton.c
index 1ecd6d1468867ce322b8d7510269c31cd28ae584..3ac36a71b8d37ddeccc5ee2b95a641466345d1eb 100644 (file)
@@ -57,7 +57,7 @@
 #include "gtkintl.h"
 #include "a11y/gtkbuttonaccessible.h"
 #include "gtkapplicationprivate.h"
-#include "gtkactionable.h"
+#include "gtkactionhelper.h"
 
 static const GtkBorder default_default_border = { 1, 1, 1, 1 };
 static const GtkBorder default_default_outside_border = { 0, 0, 0, 0 };
@@ -91,6 +91,7 @@ enum {
   PROP_IMAGE_POSITION,
   PROP_ACTION_NAME,
   PROP_ACTION_TARGET,
+  PROP_ALWAYS_SHOW_IMAGE,
 
   /* activatable properties */
   PROP_ACTIVATABLE_RELATED_ACTION,
@@ -150,9 +151,6 @@ static void gtk_button_state_changed   (GtkWidget             *widget,
                                        GtkStateType           previous_state);
 static void gtk_button_grab_notify     (GtkWidget             *widget,
                                        gboolean               was_grabbed);
-static void gtk_button_hierarchy_changed (GtkWidget           *widget,
-                                          GtkWidget           *previous_toplevel);
-
 
 static void gtk_button_actionable_iface_init     (GtkActionableInterface *iface);
 static void gtk_button_activatable_interface_init(GtkActivatableIface  *iface);
@@ -216,7 +214,6 @@ gtk_button_class_init (GtkButtonClass *klass)
   widget_class->leave_notify_event = gtk_button_leave_notify;
   widget_class->state_changed = gtk_button_state_changed;
   widget_class->grab_notify = gtk_button_grab_notify;
-  widget_class->hierarchy_changed = gtk_button_hierarchy_changed;
 
   container_class->child_type = gtk_button_child_type;
   container_class->add = gtk_button_add;
@@ -339,6 +336,25 @@ gtk_button_class_init (GtkButtonClass *klass)
                                                       GTK_POS_LEFT,
                                                       GTK_PARAM_READWRITE));
 
+  /**
+   * GtkButton:always-show-image:
+   *
+   * If %TRUE, the button will ignore the #GtkSettings:gtk-button-images
+   * setting and always show the image, if available.
+   *
+   * Use this property if the button would be useless or hard to use
+   * without the image.
+   *
+   * Since: 3.6
+   */
+  g_object_class_install_property (gobject_class,
+                                   PROP_ALWAYS_SHOW_IMAGE,
+                                   g_param_spec_boolean ("always-show-image",
+                                                         P_("Always show image"),
+                                                         P_("Whether the image will always be shown"),
+                                                         FALSE,
+                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
+
   g_object_class_override_property (gobject_class, PROP_ACTION_NAME, "action-name");
   g_object_class_override_property (gobject_class, PROP_ACTION_TARGET, "action-target");
 
@@ -596,12 +612,6 @@ gtk_button_destroy (GtkWidget *widget)
       priv->label_text = NULL;
     }
 
-  if (priv->action_name)
-    {
-      g_free (priv->action_name);
-      priv->action_name = NULL;
-    }
-
   GTK_WIDGET_CLASS (gtk_button_parent_class)->destroy (widget);
 }
 
@@ -684,7 +694,7 @@ gtk_button_dispose (GObject *object)
   GtkButton *button = GTK_BUTTON (object);
   GtkButtonPrivate *priv = button->priv;
 
-  g_clear_object (&priv->action_observer);
+  g_clear_object (&priv->action_helper);
 
   if (priv->action)
     {
@@ -694,53 +704,22 @@ gtk_button_dispose (GObject *object)
   G_OBJECT_CLASS (gtk_button_parent_class)->dispose (object);
 }
 
-static void
-gtk_button_update_action_observer (GtkButton *button)
-{
-  GtkWidget *window;
-
-  g_signal_handlers_disconnect_by_func (button, gtk_real_button_clicked, NULL);
-
-  /* we are the only owner so this will clear all the signals */
-  g_clear_object (&button->priv->action_observer);
-
-  window = gtk_widget_get_toplevel (GTK_WIDGET (button));
-
-  if (GTK_IS_APPLICATION_WINDOW (window) && button->priv->action_name)
-    {
-      GSimpleActionObserver *observer;
-
-      observer = gtk_application_window_create_observer (GTK_APPLICATION_WINDOW (window),
-                                                         button->priv->action_name,
-                                                         button->priv->action_target);
-
-      _gtk_button_set_depressed (button, g_simple_action_observer_get_active (observer));
-
-      if (g_object_class_find_property (G_OBJECT_GET_CLASS (button), "active"))
-        g_object_bind_property (observer, "active", button, "active", G_BINDING_SYNC_CREATE);
-      g_object_bind_property (observer, "enabled", button, "sensitive", G_BINDING_SYNC_CREATE);
-
-      button->priv->action_observer = observer;
-
-      g_signal_connect_after (button, "clicked", G_CALLBACK (gtk_real_button_clicked), NULL);
-    }
-}
-
 static void
 gtk_button_set_action_name (GtkActionable *actionable,
                             const gchar   *action_name)
 {
   GtkButton *button = GTK_BUTTON (actionable);
 
-  g_return_if_fail (GTK_IS_BUTTON (button));
   g_return_if_fail (button->priv->action == NULL);
 
-  g_free (button->priv->action_name);
-  button->priv->action_name = g_strdup (action_name);
+  if (!button->priv->action_helper)
+    button->priv->action_helper = gtk_action_helper_new (actionable);
 
-  gtk_button_update_action_observer (button);
+  g_signal_handlers_disconnect_by_func (button, gtk_real_button_clicked, NULL);
+  if (action_name)
+    g_signal_connect_after (button, "clicked", G_CALLBACK (gtk_real_button_clicked), NULL);
 
-  g_object_notify (G_OBJECT (button), "action-name");
+  gtk_action_helper_set_action_name (button->priv->action_helper, action_name);
 }
 
 static void
@@ -749,24 +728,10 @@ gtk_button_set_action_target_value (GtkActionable *actionable,
 {
   GtkButton *button = GTK_BUTTON (actionable);
 
-  g_return_if_fail (GTK_IS_BUTTON (button));
-
-  if (action_target != button->priv->action_target &&
-      (!action_target || !button->priv->action_target ||
-       !g_variant_equal (action_target, button->priv->action_target)))
-    {
-      if (button->priv->action_target)
-        g_variant_unref (button->priv->action_target);
-
-      button->priv->action_target = NULL;
-
-      if (action_target)
-        button->priv->action_target = g_variant_ref_sink (action_target);
+  if (!button->priv->action_helper)
+    button->priv->action_helper = gtk_action_helper_new (actionable);
 
-      gtk_button_update_action_observer (button);
-
-      g_object_notify (G_OBJECT (button), "action-target");
-    }
+  gtk_action_helper_set_action_target_value (button->priv->action_helper, action_target);
 }
 
 static void
@@ -786,6 +751,9 @@ gtk_button_set_property (GObject         *object,
     case PROP_IMAGE:
       gtk_button_set_image (button, (GtkWidget *) g_value_get_object (value));
       break;
+    case PROP_ALWAYS_SHOW_IMAGE:
+      gtk_button_set_always_show_image (button, g_value_get_boolean (value));
+      break;
     case PROP_RELIEF:
       gtk_button_set_relief (button, g_value_get_enum (value));
       break;
@@ -842,6 +810,9 @@ gtk_button_get_property (GObject         *object,
     case PROP_IMAGE:
       g_value_set_object (value, (GObject *)priv->image);
       break;
+    case PROP_ALWAYS_SHOW_IMAGE:
+      g_value_set_boolean (value, gtk_button_get_always_show_image (button));
+      break;
     case PROP_RELIEF:
       g_value_set_enum (value, priv->relief);
       break;
@@ -870,10 +841,10 @@ gtk_button_get_property (GObject         *object,
       g_value_set_boolean (value, priv->use_action_appearance);
       break;
     case PROP_ACTION_NAME:
-      g_value_set_string (value, priv->action_name);
+      g_value_set_string (value, gtk_action_helper_get_action_name (priv->action_helper));
       break;
     case PROP_ACTION_TARGET:
-      g_value_set_variant (value, priv->action_target);
+      g_value_set_variant (value, gtk_action_helper_get_action_target_value (priv->action_helper));
       break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -886,7 +857,7 @@ gtk_button_get_action_name (GtkActionable *actionable)
 {
   GtkButton *button = GTK_BUTTON (actionable);
 
-  return button->priv->action_name;
+  return gtk_action_helper_get_action_name (button->priv->action_helper);
 }
 
 static GVariant *
@@ -894,7 +865,7 @@ gtk_button_get_action_target_value (GtkActionable *actionable)
 {
   GtkButton *button = GTK_BUTTON (actionable);
 
-  return button->priv->action_target;
+  return gtk_action_helper_get_action_target_value (button->priv->action_helper);
 }
 
 static void
@@ -1032,6 +1003,9 @@ gtk_button_sync_action_properties (GtkActivatable *activatable,
       activatable_update_gicon (GTK_BUTTON (activatable), action);
       activatable_update_icon_name (GTK_BUTTON (activatable), action);
     }
+
+  gtk_button_set_always_show_image (button,
+                                    gtk_action_get_always_show_image (action));
 }
 
 static void
@@ -1040,7 +1014,7 @@ gtk_button_set_related_action (GtkButton *button,
 {
   GtkButtonPrivate *priv = button->priv;
 
-  g_return_if_fail (button->priv->action_name == NULL);
+  g_return_if_fail (gtk_action_helper_get_action_name (button->priv->action_helper) == NULL);
 
   if (priv->action == action)
     return;
@@ -1093,7 +1067,7 @@ show_image (GtkButton *button)
   GtkButtonPrivate *priv = button->priv;
   gboolean show;
 
-  if (priv->label_text)
+  if (priv->label_text && !priv->always_show_image)
     {
       GtkSettings *settings;
 
@@ -1106,6 +1080,7 @@ show_image (GtkButton *button)
   return show;
 }
 
+
 static void
 gtk_button_construct_child (GtkButton *button)
 {
@@ -1445,7 +1420,7 @@ gtk_button_realize (GtkWidget *widget)
 
   priv->event_window = gdk_window_new (window,
                                        &attributes, attributes_mask);
-  gdk_window_set_user_data (priv->event_window, button);
+  gtk_widget_register_window (widget, priv->event_window);
 }
 
 static void
@@ -1459,7 +1434,7 @@ gtk_button_unrealize (GtkWidget *widget)
 
   if (priv->event_window)
     {
-      gdk_window_set_user_data (priv->event_window, NULL);
+      gtk_widget_unregister_window (widget, priv->event_window);
       gdk_window_destroy (priv->event_window);
       priv->event_window = NULL;
     }
@@ -1486,7 +1461,10 @@ gtk_button_unmap (GtkWidget *widget)
   GtkButtonPrivate *priv = button->priv;
 
   if (priv->event_window)
-    gdk_window_hide (priv->event_window);
+    {
+      gdk_window_hide (priv->event_window);
+      priv->in_button = FALSE;
+    }
 
   GTK_WIDGET_CLASS (gtk_button_parent_class)->unmap (widget);
 }
@@ -1677,15 +1655,12 @@ gtk_button_size_allocate (GtkWidget     *widget,
     }
 }
 
-void
-_gtk_button_paint (GtkButton          *button,
-                  cairo_t            *cr,
-                   int                 width,
-                   int                 height,
-                  GtkStateFlags       state)
+static gboolean
+gtk_button_draw (GtkWidget *widget,
+                cairo_t   *cr)
 {
+  GtkButton *button = GTK_BUTTON (widget);
   GtkButtonPrivate *priv = button->priv;
-  GtkWidget *widget;
   gint x, y;
   GtkBorder default_border;
   GtkBorder default_outside_border;
@@ -1694,13 +1669,12 @@ _gtk_button_paint (GtkButton          *button,
   gint focus_pad;
   GtkAllocation allocation;
   GtkStyleContext *context;
+  GtkStateFlags state;
   gboolean draw_focus;
+  gint width, height;
 
-  widget = GTK_WIDGET (button);
   context = gtk_widget_get_style_context (widget);
-
-  gtk_style_context_save (context);
-  gtk_style_context_set_state (context, state);
+  state = gtk_style_context_get_state (context);
 
   gtk_button_get_props (button, &default_border, &default_outside_border, NULL, NULL, &interior_focus);
   gtk_style_context_get_style (context,
@@ -1712,6 +1686,8 @@ _gtk_button_paint (GtkButton          *button,
 
   x = 0;
   y = 0;
+  width = allocation.width;
+  height = allocation.height;
 
   if (gtk_widget_has_default (widget) &&
       priv->relief == GTK_RELIEF_NORMAL)
@@ -1787,20 +1763,6 @@ _gtk_button_paint (GtkButton          *button,
       gtk_render_focus (context, cr, x, y, width, height);
     }
 
-  gtk_style_context_restore (context);
-}
-
-static gboolean
-gtk_button_draw (GtkWidget *widget,
-                cairo_t   *cr)
-{
-  GtkButton *button = GTK_BUTTON (widget);
-
-  _gtk_button_paint (button, cr, 
-                     gtk_widget_get_allocated_width (widget),
-                     gtk_widget_get_allocated_height (widget),
-                     gtk_widget_get_state_flags (widget));
-
   GTK_WIDGET_CLASS (gtk_button_parent_class)->draw (widget, cr);
 
   return FALSE;
@@ -2013,8 +1975,8 @@ gtk_real_button_clicked (GtkButton *button)
 {
   GtkButtonPrivate *priv = button->priv;
 
-  if (priv->action_observer)
-    g_simple_action_observer_activate (priv->action_observer);
+  if (priv->action_helper)
+    gtk_action_helper_activate (priv->action_helper);
 
   if (priv->action)
     gtk_action_activate (priv->action);
@@ -2610,28 +2572,6 @@ gtk_button_grab_notify (GtkWidget *widget,
     }
 }
 
-static void
-gtk_button_hierarchy_changed (GtkWidget *widget,
-                              GtkWidget *previous_toplevel)
-{
-  GtkButton *button = GTK_BUTTON (widget);
-  GtkWidgetClass *parent_class;
-
-  parent_class = GTK_WIDGET_CLASS (gtk_button_parent_class);
-  if (parent_class->hierarchy_changed)
-    parent_class->hierarchy_changed (widget, previous_toplevel);
-
-  if (button->priv->action_name)
-    {
-      GtkWidget *toplevel;
-
-      toplevel = gtk_widget_get_toplevel (widget);
-
-      if (toplevel != previous_toplevel)
-        gtk_button_update_action_observer (button);
-    }
-}
-
 /**
  * gtk_button_set_image:
  * @button: a #GtkButton
@@ -2741,6 +2681,64 @@ gtk_button_get_image_position (GtkButton *button)
   return button->priv->image_position;
 }
 
+/**
+ * gtk_button_set_always_show_image:
+ * @button: a #GtkButton
+ * @always_show: %TRUE if the menuitem should always show the image
+ *
+ * If %TRUE, the button will ignore the #GtkSettings:gtk-button-images
+ * setting and always show the image, if available.
+ *
+ * Use this property if the button  would be useless or hard to use
+ * without the image.
+ *
+ * Since: 3.6
+ */
+void
+gtk_button_set_always_show_image (GtkButton *button,
+                                  gboolean    always_show)
+{
+  GtkButtonPrivate *priv;
+
+  g_return_if_fail (GTK_IS_BUTTON (button));
+
+  priv = button->priv;
+
+  if (priv->always_show_image != always_show)
+    {
+      priv->always_show_image = always_show;
+
+      if (priv->image)
+        {
+          if (show_image (button))
+            gtk_widget_show (priv->image);
+          else
+            gtk_widget_hide (priv->image);
+        }
+
+      g_object_notify (G_OBJECT (button), "always-show-image");
+    }
+}
+
+/**
+ * gtk_button_get_always_show_image:
+ * @button: a #GtkButton
+ *
+ * Returns whether the button will ignore the #GtkSettings:gtk-button-images
+ * setting and always show the image, if available.
+ *
+ * Returns: %TRUE if the button will always show the image
+ *
+ * Since: 3.6
+ */
+gboolean
+gtk_button_get_always_show_image (GtkButton *button)
+{
+  g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
+
+  return button->priv->always_show_image;
+}
+
 /**
  * gtk_button_get_event_window:
  * @button: a #GtkButton