]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkswitch.c
filechooserbutton: Give proper names to the SELECT_FOLDER tests with cancelled dialog
[~andy/gtk] / gtk / gtkswitch.c
index b3ed707d78513a7a7c1e7c63478807f1884aaf97..dfcfedc4bd351664ebbe959f2ceb984646212547 100644 (file)
@@ -47,6 +47,7 @@
 #include "gtkapplicationprivate.h"
 #include "gtkactionable.h"
 #include "a11y/gtkswitchaccessible.h"
+#include "gtkactionhelper.h"
 
 #include <math.h>
 
@@ -57,10 +58,7 @@ struct _GtkSwitchPrivate
 {
   GdkWindow *event_window;
   GtkAction *action;
-
-  gchar                 *action_name;
-  GVariant              *action_target;
-  GSimpleActionObserver *action_observer;
+  GtkActionHelper *action_helper;
 
   gint handle_x;
   gint offset;
@@ -95,9 +93,6 @@ static guint signals[LAST_SIGNAL] = { 0 };
 
 static GParamSpec *switch_props[LAST_PROP] = { NULL, };
 
-static void gtk_switch_hierarchy_changed (GtkWidget *widget,
-                                          GtkWidget *previous_toplevel);
 static void gtk_switch_actionable_iface_init (GtkActionableInterface *iface);
 static void gtk_switch_activatable_interface_init (GtkActivatableIface *iface);
 
@@ -463,7 +458,7 @@ gtk_switch_realize (GtkWidget *widget)
   priv->event_window = gdk_window_new (parent_window,
                                        &attributes,
                                        attributes_mask);
-  gdk_window_set_user_data (priv->event_window, widget);
+  gtk_widget_register_window (widget, priv->event_window);
 }
 
 static void
@@ -473,7 +468,7 @@ gtk_switch_unrealize (GtkWidget *widget)
 
   if (priv->event_window != NULL)
     {
-      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;
     }
@@ -529,15 +524,12 @@ gtk_switch_draw (GtkWidget *widget,
   GtkStyleContext *context;
   GdkRectangle handle;
   PangoLayout *layout;
-  PangoFontDescription *desc;
-  const PangoFontDescription *style_desc;
   PangoRectangle rect;
   gint label_x, label_y;
   GtkBorder padding;
   GtkStateFlags state;
   gint focus_width, focus_pad;
   gint x, y, width, height;
-  gint font_size, style_font_size;
 
   gtk_widget_style_get (widget,
                         "focus-line-width", &focus_width,
@@ -590,20 +582,6 @@ gtk_switch_draw (GtkWidget *widget,
    */
   layout = gtk_widget_create_pango_layout (widget, C_("switch", "ON"));
 
-  /* FIXME: this should be really done in the theme, but overriding font size
-   * from it doesn't currently work. So we have to hardcode this here and
-   * below for the "OFF" label.
-   */
-  desc = pango_font_description_new ();
-
-  style_desc = gtk_style_context_get_font (context, state);
-  style_font_size = pango_font_description_get_size (style_desc);
-  font_size = MAX (style_font_size - 1 * PANGO_SCALE, ceil (style_font_size * PANGO_SCALE_SMALL));
-
-  pango_font_description_set_size (desc, font_size);
-
-  pango_layout_set_font_description (layout, desc);
-
   pango_layout_get_extents (layout, NULL, &rect);
   pango_extents_to_pixels (&rect, NULL);
 
@@ -618,7 +596,6 @@ gtk_switch_draw (GtkWidget *widget,
    * glyphs then use WHITE CIRCLE (U+25CB) as the text for the state
    */
   layout = gtk_widget_create_pango_layout (widget, C_("switch", "OFF"));
-  pango_layout_set_font_description (layout, desc);
 
   pango_layout_get_extents (layout, NULL, &rect);
   pango_extents_to_pixels (&rect, NULL);
@@ -641,8 +618,6 @@ gtk_switch_draw (GtkWidget *widget,
 
   gtk_switch_paint_handle (widget, cr, &handle);
 
-  pango_font_description_free (desc);
-
   return FALSE;
 }
 
@@ -674,45 +649,16 @@ gtk_switch_set_use_action_appearance (GtkSwitch *sw,
     }
 }
 
-static void
-gtk_switch_update_action_observer (GtkSwitch *sw)
-{
-  GtkWidget *window;
-
-  /* we are the only owner so this will clear all the signals */
-  g_clear_object (&sw->priv->action_observer);
-
-  window = gtk_widget_get_toplevel (GTK_WIDGET (sw));
-
-  if (GTK_IS_APPLICATION_WINDOW (window) && sw->priv->action_name)
-    {
-      GSimpleActionObserver *observer;
-
-      observer = gtk_application_window_create_observer (GTK_APPLICATION_WINDOW (window),
-                                                         sw->priv->action_name,
-                                                         sw->priv->action_target);
-
-      g_object_bind_property (observer, "active", sw, "active", G_BINDING_SYNC_CREATE);
-      g_object_bind_property (observer, "enabled", sw, "sensitive", G_BINDING_SYNC_CREATE);
-
-      sw->priv->action_observer = observer;
-    }
-}
-
 static void
 gtk_switch_set_action_name (GtkActionable *actionable,
                             const gchar   *action_name)
 {
   GtkSwitch *sw = GTK_SWITCH (actionable);
 
-  g_return_if_fail (GTK_IS_SWITCH (sw));
-
-  g_free (sw->priv->action_name);
-  sw->priv->action_name = g_strdup (action_name);
+  if (!sw->priv->action_helper)
+    sw->priv->action_helper = gtk_action_helper_new (actionable);
 
-  gtk_switch_update_action_observer (sw);
-
-  g_object_notify (G_OBJECT (sw), "action-name");
+  gtk_action_helper_set_action_name (sw->priv->action_helper, action_name);
 }
 
 static void
@@ -721,24 +667,10 @@ gtk_switch_set_action_target_value (GtkActionable *actionable,
 {
   GtkSwitch *sw = GTK_SWITCH (actionable);
 
-  g_return_if_fail (GTK_IS_SWITCH (sw));
-
-  if (action_target != sw->priv->action_target &&
-      (!action_target || !sw->priv->action_target ||
-       !g_variant_equal (action_target, sw->priv->action_target)))
-    {
-      if (sw->priv->action_target)
-        g_variant_unref (sw->priv->action_target);
-
-      sw->priv->action_target = NULL;
-
-      if (action_target)
-        sw->priv->action_target = g_variant_ref_sink (action_target);
+  if (!sw->priv->action_helper)
+    sw->priv->action_helper = gtk_action_helper_new (actionable);
 
-      gtk_switch_update_action_observer (sw);
-
-      g_object_notify (G_OBJECT (sw), "action-target");
-    }
+  gtk_action_helper_set_action_target_value (sw->priv->action_helper, action_target);
 }
 
 static const gchar *
@@ -746,7 +678,7 @@ gtk_switch_get_action_name (GtkActionable *actionable)
 {
   GtkSwitch *sw = GTK_SWITCH (actionable);
 
-  return sw->priv->action_name;
+  return gtk_action_helper_get_action_name (sw->priv->action_helper);
 }
 
 static GVariant *
@@ -754,7 +686,7 @@ gtk_switch_get_action_target_value (GtkActionable *actionable)
 {
   GtkSwitch *sw = GTK_SWITCH (actionable);
 
-  return sw->priv->action_target;
+  return gtk_action_helper_get_action_target_value (sw->priv->action_helper);
 }
 
 static void
@@ -766,28 +698,6 @@ gtk_switch_actionable_iface_init (GtkActionableInterface *iface)
   iface->set_action_target_value = gtk_switch_set_action_target_value;
 }
 
-static void
-gtk_switch_hierarchy_changed (GtkWidget *widget,
-                              GtkWidget *previous_toplevel)
-{
-  GtkSwitch *sw = GTK_SWITCH (widget);
-  GtkWidgetClass *parent_class;
-
-  parent_class = GTK_WIDGET_CLASS (gtk_switch_parent_class);
-  if (parent_class->hierarchy_changed)
-    parent_class->hierarchy_changed (widget, previous_toplevel);
-
-  if (sw->priv->action_name)
-    {
-      GtkWidget *toplevel;
-
-      toplevel = gtk_widget_get_toplevel (widget);
-
-      if (toplevel != previous_toplevel)
-        gtk_switch_update_action_observer (sw);
-    }
-}
-
 static void
 gtk_switch_set_property (GObject      *gobject,
                          guint         prop_id,
@@ -846,11 +756,11 @@ gtk_switch_get_property (GObject    *gobject,
       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:
@@ -863,13 +773,7 @@ gtk_switch_dispose (GObject *object)
 {
   GtkSwitchPrivate *priv = GTK_SWITCH (object)->priv;
 
-  g_clear_object (&priv->action_observer);
-
-  if (priv->action_name)
-    {
-      g_free (priv->action_name);
-      priv->action_name = NULL;
-    }
+  g_clear_object (&priv->action_helper);
 
   if (priv->action)
     {
@@ -931,7 +835,6 @@ gtk_switch_class_init (GtkSwitchClass *klass)
   widget_class->motion_notify_event = gtk_switch_motion;
   widget_class->enter_notify_event = gtk_switch_enter;
   widget_class->leave_notify_event = gtk_switch_leave;
-  widget_class->hierarchy_changed = gtk_switch_hierarchy_changed;
 
   klass->activate = gtk_switch_activate;
 
@@ -1026,8 +929,8 @@ gtk_switch_set_active (GtkSwitch *sw,
 
       g_object_notify_by_pspec (G_OBJECT (sw), switch_props[PROP_ACTIVE]);
 
-      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);