#include "gtkprivate.h"
#include "gtkintl.h"
#include "a11y/gtkbuttonaccessible.h"
-
+#include "gtkapplicationprivate.h"
+#include "gtkactionable.h"
static const GtkBorder default_default_border = { 1, 1, 1, 1 };
static const GtkBorder default_default_outside_border = { 0, 0, 0, 0 };
PROP_XALIGN,
PROP_YALIGN,
PROP_IMAGE_POSITION,
+ PROP_ACTION_NAME,
+ PROP_ACTION_TARGET,
/* activatable properties */
PROP_ACTIVATABLE_RELATED_ACTION,
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);
static void gtk_button_update (GtkActivatable *activatable,
GtkAction *action,
static guint button_signals[LAST_SIGNAL] = { 0 };
G_DEFINE_TYPE_WITH_CODE (GtkButton, gtk_button, GTK_TYPE_BIN,
+ G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIONABLE, gtk_button_actionable_iface_init)
G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
gtk_button_activatable_interface_init))
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;
GTK_POS_LEFT,
GTK_PARAM_READWRITE));
+ g_object_class_override_property (gobject_class, PROP_ACTION_NAME, "action-name");
+ g_object_class_override_property (gobject_class, PROP_ACTION_TARGET, "action-target");
+
g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
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);
}
GtkButton *button = GTK_BUTTON (object);
GtkButtonPrivate *priv = button->priv;
+ g_clear_object (&priv->action_observer);
+
if (priv->action)
{
gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), NULL);
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_get_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));
+
+ if (g_strcmp0 (action_name, button->priv->action_name) != 0)
+ {
+ g_free (button->priv->action_name);
+ button->priv->action_name = g_strdup (action_name);
+
+ gtk_button_update_action_observer (button);
+
+ g_object_notify (G_OBJECT (button), "action-name");
+ }
+}
+
+static void
+gtk_button_set_action_target_value (GtkActionable *actionable,
+ GVariant *action_target)
+{
+ 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);
+
+ gtk_button_update_action_observer (button);
+
+ g_object_notify (G_OBJECT (button), "action-target");
+ }
+}
+
static void
gtk_button_set_property (GObject *object,
guint prop_id,
case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
gtk_button_set_use_action_appearance (button, g_value_get_boolean (value));
break;
+ case PROP_ACTION_NAME:
+ gtk_button_set_action_name (GTK_ACTIONABLE (button), g_value_get_string (value));
+ break;
+ case PROP_ACTION_TARGET:
+ gtk_button_set_action_target_value (GTK_ACTIONABLE (button), g_value_get_variant (value));
+ break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
g_value_set_boolean (value, priv->use_action_appearance);
break;
+ case PROP_ACTION_NAME:
+ g_value_set_string (value, priv->action_name);
+ break;
+ case PROP_ACTION_TARGET:
+ g_value_set_variant (value, priv->action_target);
+ break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
+static const gchar *
+gtk_button_get_action_name (GtkActionable *actionable)
+{
+ GtkButton *button = GTK_BUTTON (actionable);
+
+ return button->priv->action_name;
+}
+
+static GVariant *
+gtk_button_get_action_target_value (GtkActionable *actionable)
+{
+ GtkButton *button = GTK_BUTTON (actionable);
+
+ return button->priv->action_target;
+}
+
+static void
+gtk_button_actionable_iface_init (GtkActionableInterface *iface)
+{
+ iface->get_action_name = gtk_button_get_action_name;
+ iface->set_action_name = gtk_button_set_action_name;
+ iface->get_action_target_value = gtk_button_get_action_target_value;
+ iface->set_action_target_value = gtk_button_set_action_target_value;
+}
+
static void
gtk_button_activatable_interface_init (GtkActivatableIface *iface)
{
{
GtkButtonPrivate *priv = button->priv;
+ if (priv->action_observer)
+ g_simple_action_observer_activate (priv->action_observer);
+
if (priv->action)
gtk_action_activate (priv->action);
}
}
}
+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
return button->priv->image_position;
}
-
/**
* gtk_button_get_event_window:
* @button: a #GtkButton