]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkswitch.c
Deprecate all the public API that is using GdkColor struct
[~andy/gtk] / gtk / gtkswitch.c
index f3ef450967f44dcbf31717958f5b927ffc162a5d..acb306db532a349a03030d9c289de8844e57b7cf 100644 (file)
@@ -22,8 +22,7 @@
  *      Emmanuele Bassi <ebassi@linux.intel.com>
  *      Matthias Clasen <mclasen@redhat.com>
  *
- * Based on similar code from:
- *      Thomas Wood <thos@linux.intel.com>
+ * Based on similar code from Mx.
  */
 
 /**
 
 #include "gtkswitch.h"
 
-#include <gdk/gdkkeysyms.h>
-
-#include "gtkaccessible.h"
 #include "gtkactivatable.h"
 #include "gtkintl.h"
-#include "gtkstyle.h"
 #include "gtkprivate.h"
 #include "gtktoggleaction.h"
 #include "gtkwidget.h"
+#include "gtkmarshalers.h"
+#include "a11y/gtkswitchaccessible.h"
+
+#include <math.h>
 
 #define DEFAULT_SLIDER_WIDTH    (36)
 #define DEFAULT_SLIDER_HEIGHT   (22)
@@ -80,9 +79,15 @@ enum
   LAST_PROP
 };
 
-static GParamSpec *switch_props[LAST_PROP] = { NULL, };
+enum
+{
+  ACTIVATE,
+  LAST_SIGNAL
+};
 
-static GType gtk_switch_accessible_factory_get_type (void);
+static guint signals[LAST_SIGNAL] = { 0 };
+
+static GParamSpec *switch_props[LAST_PROP] = { NULL, };
 
 static void gtk_switch_activatable_interface_init (GtkActivatableIface *iface);
 
@@ -107,7 +112,7 @@ gtk_switch_button_press (GtkWidget      *widget,
       if (event->x <= allocation.width / 2)
         {
           priv->in_press = TRUE;
-          return FALSE;
+          return TRUE;
         }
 
       priv->offset = event->x - allocation.width / 2;
@@ -120,7 +125,7 @@ gtk_switch_button_press (GtkWidget      *widget,
       if (event->x >= allocation.width / 2)
         {
           priv->in_press = TRUE;
-          return FALSE;
+          return TRUE;
         }
 
       priv->offset = event->x;
@@ -132,7 +137,7 @@ gtk_switch_button_press (GtkWidget      *widget,
                 "gtk-dnd-drag-threshold", &priv->drag_threshold,
                 NULL);
 
-  return FALSE;
+  return TRUE;
 }
 
 static gboolean
@@ -152,16 +157,33 @@ gtk_switch_motion (GtkWidget      *widget,
     {
       gint position = event->x - priv->offset;
       GtkAllocation allocation;
-      GtkStyle *style;
-
-      style = gtk_widget_get_style (widget);
+      GtkStyleContext *context;
+      GtkStateFlags state;
+      GtkBorder padding;
+      gint width, focus_width, focus_pad;
+
+      gtk_widget_style_get (widget,
+                            "focus-line-width", &focus_width,
+                            "focus-padding", &focus_pad,
+                            NULL);
+
+      context = gtk_widget_get_style_context (widget);
+      state = gtk_widget_get_state_flags (widget);
+
+      gtk_style_context_save (context);
+      gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
+      gtk_style_context_get_padding (context, state, &padding);
+      gtk_style_context_restore (context);
+      
       gtk_widget_get_allocation (widget, &allocation);
 
+      width = allocation.width - 2 * (focus_width + focus_pad);
+
       /* constrain the handle within the trough width */
-      if (position > (allocation.width / 2 - style->xthickness))
-        priv->handle_x = allocation.width / 2 - style->xthickness;
-      else if (position < style->xthickness)
-        priv->handle_x = style->xthickness;
+      if (position > (width / 2) - padding.right)
+        priv->handle_x = width / 2 - padding.right;
+      else if (position < padding.left)
+        priv->handle_x = 0;
       else
         priv->handle_x = position;
 
@@ -201,6 +223,15 @@ gtk_switch_button_release (GtkWidget      *widget,
       return TRUE;
     }
 
+  /* toggle the switch if the handle was clicked but a drag had not been
+   * initiated */
+  if (!priv->is_dragging && !priv->in_press)
+    {
+      gtk_switch_set_active (GTK_SWITCH (widget), !priv->is_active);
+
+      return TRUE;
+    }
+
   /* dragged toggle */
   if (priv->is_dragging)
     {
@@ -252,22 +283,12 @@ gtk_switch_leave (GtkWidget        *widget,
   return FALSE;
 }
 
-static gboolean
-gtk_switch_key_release (GtkWidget   *widget,
-                        GdkEventKey *event)
+static void
+gtk_switch_activate (GtkSwitch *sw)
 {
-  GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
-
-  if (event->keyval == GDK_KEY_Return ||
-      event->keyval == GDK_KEY_KP_Enter ||
-      event->keyval == GDK_KEY_ISO_Enter ||
-      event->keyval == GDK_KEY_space ||
-      event->keyval == GDK_KEY_KP_Space)
-    {
-      gtk_switch_set_active (GTK_SWITCH (widget), !priv->is_active);
-    }
+  GtkSwitchPrivate *priv = sw->priv;
 
-  return FALSE;
+  gtk_switch_set_active (sw, !priv->is_active);
 }
 
 static void
@@ -275,12 +296,29 @@ gtk_switch_get_preferred_width (GtkWidget *widget,
                                 gint      *minimum,
                                 gint      *natural)
 {
-  GtkStyle *style = gtk_widget_get_style (widget);
+  GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
+  GtkStyleContext *context;
+  GtkStateFlags state;
+  GtkBorder padding;
   gint width, slider_width, focus_width, focus_pad;
   PangoLayout *layout;
   PangoRectangle logical_rect;
 
-  width = style->xthickness * 2;
+  context = gtk_widget_get_style_context (widget);
+  state = gtk_widget_get_state_flags (widget);
+
+  if (priv->is_active)
+    state |= GTK_STATE_FLAG_ACTIVE;
+
+  gtk_style_context_save (context);
+
+  gtk_style_context_set_state (context, state);
+  gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
+  gtk_style_context_get_padding (context, state, &padding);
+
+  width = padding.left + padding.right;
+
+  gtk_style_context_restore (context);
 
   gtk_widget_style_get (widget,
                         "slider-width", &slider_width,
@@ -321,13 +359,30 @@ gtk_switch_get_preferred_height (GtkWidget *widget,
                                  gint      *minimum,
                                  gint      *natural)
 {
-  GtkStyle *style = gtk_widget_get_style (widget);
+  GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
+  GtkStyleContext *context;
+  GtkStateFlags state;
+  GtkBorder padding;
   gint height, focus_width, focus_pad;
   PangoLayout *layout;
   PangoRectangle logical_rect;
   gchar *str;
 
-  height = style->ythickness * 2;
+  context = gtk_widget_get_style_context (widget);
+  state = gtk_widget_get_state_flags (widget);
+
+  if (priv->is_active)
+    state |= GTK_STATE_FLAG_ACTIVE;
+
+  gtk_style_context_save (context);
+
+  gtk_style_context_set_state (context, state);
+  gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
+  gtk_style_context_get_padding (context, state, &padding);
+
+  height = padding.top + padding.bottom;
+
+  gtk_style_context_restore (context);
 
   gtk_widget_style_get (widget,
                         "focus-line-width", &focus_width,
@@ -407,8 +462,6 @@ gtk_switch_realize (GtkWidget *widget)
                                        &attributes,
                                        attributes_mask);
   gdk_window_set_user_data (priv->event_window, widget);
-
-  gtk_widget_style_attach (widget);
 }
 
 static void
@@ -453,17 +506,25 @@ gtk_switch_paint_handle (GtkWidget    *widget,
                          cairo_t      *cr,
                          GdkRectangle *box)
 {
-  GtkStyle *style = gtk_widget_get_style (widget);
-
-  gtk_paint_slider (style, cr,
-                    gtk_widget_get_state (widget),
-                    GTK_SHADOW_OUT,
-                    GTK_WIDGET (widget), "switch-slider",
-                    box->x,
-                    box->y,
-                    box->width,
-                    box->height,
-                    GTK_ORIENTATION_HORIZONTAL);
+  GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
+  GtkStyleContext *context = gtk_widget_get_style_context (widget);
+  GtkStateFlags state;
+
+  state = gtk_widget_get_state_flags (widget);
+
+  if (priv->is_active)
+    state |= GTK_STATE_FLAG_ACTIVE;
+
+  gtk_style_context_save (context);
+  gtk_style_context_set_state (context, state);
+  gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
+
+  gtk_render_slider (context, cr,
+                     box->x, box->y,
+                     box->width, box->height,
+                     GTK_ORIENTATION_HORIZONTAL);
+
+  gtk_style_context_restore (context);
 }
 
 static gboolean
@@ -471,79 +532,96 @@ gtk_switch_draw (GtkWidget *widget,
                  cairo_t   *cr)
 {
   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
-  GtkStyle *style;
+  GtkStyleContext *context;
   GdkRectangle handle;
   PangoLayout *layout;
+  PangoFontDescription *desc;
+  const PangoFontDescription *style_desc;
   PangoRectangle rect;
   gint label_x, label_y;
-  GtkStateType state;
+  GtkStateFlags state;
+  GtkBorder padding;
   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,
                         "focus-padding", &focus_pad,
                         NULL);
 
-  style = gtk_widget_get_style (widget);
+  context = gtk_widget_get_style_context (widget);
+  state = gtk_widget_get_state_flags (widget);
+
+  if (priv->is_active)
+    state |= GTK_STATE_FLAG_ACTIVE;
+
+  gtk_style_context_save (context);
+
+  gtk_style_context_set_state (context, state);
+  gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
+
+  gtk_style_context_get_padding (context, state, &padding);
+
+  gtk_style_context_restore (context);
 
   x = 0;
   y = 0;
   width = gtk_widget_get_allocated_width (widget);
   height = gtk_widget_get_allocated_height (widget);
 
-  if (gtk_widget_has_focus (widget))
-    gtk_paint_focus (style, cr,
-                     gtk_widget_get_state (widget),
-                     widget, "button",
-                     x, y, width, height);
+  if (gtk_widget_has_visible_focus (widget))
+    gtk_render_focus (context, cr, x, y, width, height);
 
   x += focus_width + focus_pad;
   y += focus_width + focus_pad;
   width -= 2 * (focus_width + focus_pad);
   height -= 2 * (focus_width + focus_pad);
 
-  state = priv->is_active ? GTK_STATE_SELECTED : gtk_widget_get_state (widget);
+  gtk_style_context_save (context);
+  gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
+  gtk_style_context_set_state (context, state);
 
-  /* background - XXX should this be a flat box instead? we're missing
-   * the border given by the shadow with that, which would require
-   * fixing all theme engines to add a subtle border for this widget
-   */
-  gtk_paint_box (style, cr,
-                 state,
-                 GTK_SHADOW_IN,
-                 widget, "switch-background",
-                 x, y, width, height);
+  gtk_render_background (context, cr, x, y, width, height);
+  gtk_render_frame (context, cr, x, y, width, height);
 
-  if (!gtk_widget_is_sensitive (widget))
-    state = GTK_STATE_INSENSITIVE;
+  width -= padding.left + padding.right;
+  height -= padding.top + padding.bottom;
 
-  /* XXX the +1/-1 it's pixel wriggling after checking with the default
-   * theme and xmag
-   */
-  handle.y = y + style->ythickness + 1;
-  handle.width = (width - style->xthickness * 2) / 2;
-  handle.height = (height - style->ythickness * 2) - 1;
+  x += padding.left;
+  y += padding.top;
+
+  handle.y = y;
+  handle.width = width / 2;
+  handle.height = height;
 
   /* Translators: if the "on" state label requires more than three
    * glyphs then use MEDIUM VERTICAL BAR (U+2759) as the text for
    * the state
    */
   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);
 
-  label_x = x + style->xthickness
-          + ((width / 2) - rect.width - (style->xthickness * 2)) / 2;
-  label_y = y + style->ythickness
-          + (height - rect.height - (style->ythickness * 2)) / 2;
+  label_x = x +  ((width / 2) - rect.width) / 2;
+  label_y = y + (height - rect.height) / 2;
 
-  gtk_paint_layout (style, cr,
-                    state,
-                    FALSE,
-                    widget, "switch-on-label",
-                    label_x, label_y,
-                    layout);
+  gtk_render_layout (context, cr, label_x, label_y, layout);
 
   g_object_unref (layout);
 
@@ -551,66 +629,32 @@ 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);
 
-  label_x = x + style->xthickness
-          + (width / 2)
-          + ((width / 2) - rect.width - (style->xthickness * 2)) / 2;
-  label_y = y + style->ythickness
-          + (height - rect.height - (style->ythickness * 2)) / 2;
+  label_x = x +  (width / 2) + ((width / 2) - rect.width) / 2;
+  label_y = y + (height - rect.height) / 2;
 
-  gtk_paint_layout (style, cr,
-                    state,
-                    FALSE,
-                    widget, "switch-off-label",
-                    label_x, label_y,
-                    layout);
+  gtk_render_layout (context, cr, label_x, label_y, layout);
 
   g_object_unref (layout);
 
   if (priv->is_dragging)
     handle.x = x + priv->handle_x;
   else if (priv->is_active)
-    handle.x = x + width - handle.width - style->xthickness;
+    handle.x = x + width - handle.width;
   else
-    handle.x = x + style->xthickness;
+    handle.x = x;
 
-  gtk_switch_paint_handle (widget, cr, &handle);
-
-  return FALSE;
-}
-
-static AtkObject *
-gtk_switch_get_accessible (GtkWidget *widget)
-{
-  static gboolean first_time = TRUE;
-
-  if (G_UNLIKELY (first_time))
-    {
-      AtkObjectFactory *factory;
-      AtkRegistry *registry;
-      GType derived_type;
-      GType derived_atk_type;
-
-      /* Figure out whether accessibility is enabled by looking at the
-       * type of the accessible object which would be created for the
-       * parent type of GtkSwitch
-       */
-      derived_type = g_type_parent (GTK_TYPE_SWITCH);
+  gtk_style_context_restore (context);
 
-      registry = atk_get_default_registry ();
-      factory = atk_registry_get_factory (registry, derived_type);
-      derived_atk_type = atk_object_factory_get_accessible_type (factory);
-      if (g_type_is_a (derived_atk_type, GTK_TYPE_ACCESSIBLE))
-        atk_registry_set_factory_type (registry,
-                                       GTK_TYPE_SWITCH,
-                                       gtk_switch_accessible_factory_get_type ());
+  gtk_switch_paint_handle (widget, cr, &handle);
 
-      first_time = FALSE;
-    }
+  pango_font_description_free (desc);
 
-  return GTK_WIDGET_CLASS (gtk_switch_parent_class)->get_accessible (widget);
+  return FALSE;
 }
 
 static void
@@ -760,8 +804,8 @@ 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->key_release_event = gtk_switch_key_release;
-  widget_class->get_accessible = gtk_switch_get_accessible;
+
+  klass->activate = gtk_switch_activate;
 
   /**
    * GtkSwitch:slider-width:
@@ -775,6 +819,27 @@ gtk_switch_class_init (GtkSwitchClass *klass)
                                                              DEFAULT_SLIDER_WIDTH, G_MAXINT,
                                                              DEFAULT_SLIDER_WIDTH,
                                                              GTK_PARAM_READABLE));
+
+  /**
+   * GtkSwitch::activate:
+   * @widget: the object which received the signal.
+   *
+   * The ::activate signal on GtkSwitch is an action signal and
+   * emitting it causes the switch to animate.
+   * Applications should never connect to this signal, but use the
+   * notify::active signal.
+   */
+  signals[ACTIVATE] =
+    g_signal_new (I_("activate"),
+                  G_OBJECT_CLASS_TYPE (gobject_class),
+                  G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
+                  G_STRUCT_OFFSET (GtkSwitchClass, activate),
+                  NULL, NULL,
+                  _gtk_marshal_VOID__VOID,
+                  G_TYPE_NONE, 0);
+  widget_class->activate_signal = signals[ACTIVATE];
+
+  gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_SWITCH_ACCESSIBLE);
 }
 
 static void
@@ -825,7 +890,10 @@ gtk_switch_set_active (GtkSwitch *sw,
   if (priv->is_active != is_active)
     {
       AtkObject *accessible;
+      GtkWidget *widget;
+      GtkStyleContext *context;
 
+      widget = GTK_WIDGET (sw);
       priv->is_active = is_active;
 
       g_object_notify_by_pspec (G_OBJECT (sw), switch_props[PROP_ACTIVE]);
@@ -836,6 +904,14 @@ gtk_switch_set_active (GtkSwitch *sw,
       accessible = gtk_widget_get_accessible (GTK_WIDGET (sw));
       atk_object_notify_state_change (accessible, ATK_STATE_CHECKED, priv->is_active);
 
+      if (gtk_widget_get_realized (widget))
+        {
+          context = gtk_widget_get_style_context (widget);
+          gtk_style_context_notify_state_change (context,
+                                                 gtk_widget_get_window (widget),
+                                                 NULL, GTK_STATE_ACTIVE, is_active);
+        }
+
       gtk_widget_queue_draw (GTK_WIDGET (sw));
     }
 }
@@ -907,92 +983,3 @@ gtk_switch_activatable_interface_init (GtkActivatableIface *iface)
   iface->update = gtk_switch_update;
   iface->sync_action_properties = gtk_switch_sync_action_properties;
 }
-
-/* accessibility: object */
-
-/* dummy typedefs */
-typedef struct _GtkSwitchAccessible             GtkSwitchAccessible;
-typedef struct _GtkSwitchAccessibleClass        GtkSwitchAccessibleClass;
-
-ATK_DEFINE_TYPE (GtkSwitchAccessible, gtk_switch_accessible, GTK_TYPE_WIDGET);
-
-static AtkStateSet *
-gtk_switch_accessible_ref_state_set (AtkObject *accessible)
-{
-  AtkStateSet *state_set;
-  GtkWidget *widget;
-
-  state_set = ATK_OBJECT_CLASS (gtk_switch_accessible_parent_class)->ref_state_set (accessible);
-
-  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
-  if (widget == NULL)
-    return state_set;
-
-  if (gtk_switch_get_active (GTK_SWITCH (widget)))
-    atk_state_set_add_state (state_set, ATK_STATE_CHECKED);
-
-  return state_set;
-}
-
-static void
-gtk_switch_accessible_initialize (AtkObject *accessible,
-                                  gpointer   widget)
-{
-  ATK_OBJECT_CLASS (gtk_switch_accessible_parent_class)->initialize (accessible, widget);
-
-  atk_object_set_role (accessible, ATK_ROLE_TOGGLE_BUTTON);
-  atk_object_set_name (accessible, C_("light switch widget", "Switch"));
-  atk_object_set_description (accessible, _("Switches between on and off states"));
-}
-
-static void
-gtk_switch_accessible_class_init (GtkSwitchAccessibleClass *klass)
-{
-  AtkObjectClass *atk_class = ATK_OBJECT_CLASS (klass);
-
-  atk_class->initialize = gtk_switch_accessible_initialize;
-  atk_class->ref_state_set = gtk_switch_accessible_ref_state_set;
-}
-
-static void
-gtk_switch_accessible_init (GtkSwitchAccessible *self)
-{
-}
-
-/* accessibility: factory */
-
-typedef AtkObjectFactoryClass   GtkSwitchAccessibleFactoryClass;
-typedef AtkObjectFactory        GtkSwitchAccessibleFactory;
-
-G_DEFINE_TYPE (GtkSwitchAccessibleFactory,
-               gtk_switch_accessible_factory,
-               ATK_TYPE_OBJECT_FACTORY);
-
-static GType
-gtk_switch_accessible_factory_get_accessible_type (void)
-{
-  return gtk_switch_accessible_get_type ();
-}
-
-static AtkObject *
-gtk_switch_accessible_factory_create_accessible (GObject *obj)
-{
-  AtkObject *accessible;
-
-  accessible = g_object_new (gtk_switch_accessible_get_type (), NULL);
-  atk_object_initialize (accessible, obj);
-
-  return accessible;
-}
-
-static void
-gtk_switch_accessible_factory_class_init (AtkObjectFactoryClass *klass)
-{
-  klass->create_accessible = gtk_switch_accessible_factory_create_accessible;
-  klass->get_accessible_type = gtk_switch_accessible_factory_get_accessible_type;
-}
-
-static void
-gtk_switch_accessible_factory_init (AtkObjectFactory *factory)
-{
-}