Commit 029fb53a authored by Carlos Garnacho's avatar Carlos Garnacho
Browse files

Make GtkSwitch use GtkStyleContext

parent 18b333bf
...@@ -151,16 +151,20 @@ gtk_switch_motion (GtkWidget *widget, ...@@ -151,16 +151,20 @@ gtk_switch_motion (GtkWidget *widget,
{ {
gint position = event->x - priv->offset; gint position = event->x - priv->offset;
GtkAllocation allocation; GtkAllocation allocation;
GtkStyle *style; GtkStyleContext *context;
GtkStateFlags state;
GtkBorder padding;
style = gtk_widget_get_style (widget); context = gtk_widget_get_style_context (widget);
state = gtk_widget_get_state_flags (widget);
gtk_style_context_get_padding (context, state, &padding);
gtk_widget_get_allocation (widget, &allocation); gtk_widget_get_allocation (widget, &allocation);
/* constrain the handle within the trough width */ /* constrain the handle within the trough width */
if (position > (allocation.width / 2 - style->xthickness)) if (position > (allocation.width / 2 - padding.right))
priv->handle_x = allocation.width / 2 - style->xthickness; priv->handle_x = allocation.width / 2 - padding.right;
else if (position < style->xthickness) else if (position < padding.left)
priv->handle_x = style->xthickness; priv->handle_x = padding.left;
else else
priv->handle_x = position; priv->handle_x = position;
...@@ -274,12 +278,18 @@ gtk_switch_get_preferred_width (GtkWidget *widget, ...@@ -274,12 +278,18 @@ gtk_switch_get_preferred_width (GtkWidget *widget,
gint *minimum, gint *minimum,
gint *natural) gint *natural)
{ {
GtkStyle *style = gtk_widget_get_style (widget); GtkStyleContext *context;
GtkStateFlags state;
GtkBorder padding;
gint width, slider_width, focus_width, focus_pad; gint width, slider_width, focus_width, focus_pad;
PangoLayout *layout; PangoLayout *layout;
PangoRectangle logical_rect; PangoRectangle logical_rect;
width = style->xthickness * 2; context = gtk_widget_get_style_context (widget);
state = gtk_widget_get_state_flags (widget);
gtk_style_context_get_padding (context, state, &padding);
width = padding.left + padding.right;
gtk_widget_style_get (widget, gtk_widget_style_get (widget,
"slider-width", &slider_width, "slider-width", &slider_width,
...@@ -320,13 +330,19 @@ gtk_switch_get_preferred_height (GtkWidget *widget, ...@@ -320,13 +330,19 @@ gtk_switch_get_preferred_height (GtkWidget *widget,
gint *minimum, gint *minimum,
gint *natural) gint *natural)
{ {
GtkStyle *style = gtk_widget_get_style (widget); GtkStyleContext *context;
GtkStateFlags state;
GtkBorder padding;
gint height, focus_width, focus_pad; gint height, focus_width, focus_pad;
PangoLayout *layout; PangoLayout *layout;
PangoRectangle logical_rect; PangoRectangle logical_rect;
gchar *str; gchar *str;
height = style->ythickness * 2; context = gtk_widget_get_style_context (widget);
state = gtk_widget_get_state_flags (widget);
gtk_style_context_get_padding (context, state, &padding);
height = padding.top + padding.bottom;
gtk_widget_style_get (widget, gtk_widget_style_get (widget,
"focus-line-width", &focus_width, "focus-line-width", &focus_width,
...@@ -452,17 +468,21 @@ gtk_switch_paint_handle (GtkWidget *widget, ...@@ -452,17 +468,21 @@ gtk_switch_paint_handle (GtkWidget *widget,
cairo_t *cr, cairo_t *cr,
GdkRectangle *box) GdkRectangle *box)
{ {
GtkStyle *style = gtk_widget_get_style (widget); GtkStyleContext *context = gtk_widget_get_style_context (widget);
GtkStateFlags state;
state = gtk_widget_get_state_flags (widget);
gtk_style_context_save (context);
gtk_style_context_set_state (context, state);
gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
gtk_paint_slider (style, cr, gtk_render_slider (context, cr,
gtk_widget_get_state (widget), box->x, box->y,
GTK_SHADOW_OUT, box->width, box->height,
GTK_WIDGET (widget), "switch-slider", GTK_ORIENTATION_HORIZONTAL);
box->x,
box->y, gtk_style_context_restore (context);
box->width,
box->height,
GTK_ORIENTATION_HORIZONTAL);
} }
static gboolean static gboolean
...@@ -470,12 +490,13 @@ gtk_switch_draw (GtkWidget *widget, ...@@ -470,12 +490,13 @@ gtk_switch_draw (GtkWidget *widget,
cairo_t *cr) cairo_t *cr)
{ {
GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv; GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
GtkStyle *style; GtkStyleContext *context;
GdkRectangle handle; GdkRectangle handle;
PangoLayout *layout; PangoLayout *layout;
PangoRectangle rect; PangoRectangle rect;
gint label_x, label_y; gint label_x, label_y;
GtkStateType state; GtkStateFlags state;
GtkBorder padding;
gint focus_width, focus_pad; gint focus_width, focus_pad;
gint x, y, width, height; gint x, y, width, height;
...@@ -484,7 +505,13 @@ gtk_switch_draw (GtkWidget *widget, ...@@ -484,7 +505,13 @@ gtk_switch_draw (GtkWidget *widget,
"focus-padding", &focus_pad, "focus-padding", &focus_pad,
NULL); 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_get_padding (context, state, &padding);
x = 0; x = 0;
y = 0; y = 0;
...@@ -492,37 +519,27 @@ gtk_switch_draw (GtkWidget *widget, ...@@ -492,37 +519,27 @@ gtk_switch_draw (GtkWidget *widget,
height = gtk_widget_get_allocated_height (widget); height = gtk_widget_get_allocated_height (widget);
if (gtk_widget_has_focus (widget)) if (gtk_widget_has_focus (widget))
gtk_paint_focus (style, cr, gtk_render_focus (context, cr, x, y, width, height);
gtk_widget_get_state (widget),
widget, "button", gtk_style_context_save (context);
x, y, width, height); gtk_style_context_set_state (context, state);
x += focus_width + focus_pad; x += focus_width + focus_pad;
y += focus_width + focus_pad; y += focus_width + focus_pad;
width -= 2 * (focus_width + focus_pad); width -= 2 * (focus_width + focus_pad);
height -= 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_add_class (context, GTK_STYLE_CLASS_TROUGH);
/* 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);
if (!gtk_widget_is_sensitive (widget)) gtk_render_background (context, cr, x, y, width, height);
state = GTK_STATE_INSENSITIVE; gtk_render_frame (context, cr, x, y, width, height);
/* XXX the +1/-1 it's pixel wriggling after checking with the default /* XXX the +1/-1 it's pixel wriggling after checking with the default
* theme and xmag * theme and xmag
*/ */
handle.y = y + style->ythickness + 1; handle.y = y + padding.top + 1;
handle.width = (width - style->xthickness * 2) / 2; handle.width = (width - padding.left - padding.right) / 2;
handle.height = (height - style->ythickness * 2) - 1; handle.height = (height - padding.top - padding.bottom) - 1;
/* Translators: if the "on" state label requires more than three /* Translators: if the "on" state label requires more than three
* glyphs then use MEDIUM VERTICAL BAR (U+2759) as the text for * glyphs then use MEDIUM VERTICAL BAR (U+2759) as the text for
...@@ -532,17 +549,12 @@ gtk_switch_draw (GtkWidget *widget, ...@@ -532,17 +549,12 @@ gtk_switch_draw (GtkWidget *widget,
pango_layout_get_extents (layout, NULL, &rect); pango_layout_get_extents (layout, NULL, &rect);
pango_extents_to_pixels (&rect, NULL); pango_extents_to_pixels (&rect, NULL);
label_x = x + style->xthickness label_x = x + padding.left
+ ((width / 2) - rect.width - (style->xthickness * 2)) / 2; + ((width / 2) - rect.width - padding.left - padding.right) / 2;
label_y = y + style->ythickness label_y = y + padding.top
+ (height - rect.height - (style->ythickness * 2)) / 2; + (height - rect.height - padding.top - padding.bottom) / 2;
gtk_paint_layout (style, cr, gtk_render_layout (context, cr, label_x, label_y, layout);
state,
FALSE,
widget, "switch-on-label",
label_x, label_y,
layout);
g_object_unref (layout); g_object_unref (layout);
...@@ -553,27 +565,24 @@ gtk_switch_draw (GtkWidget *widget, ...@@ -553,27 +565,24 @@ gtk_switch_draw (GtkWidget *widget,
pango_layout_get_extents (layout, NULL, &rect); pango_layout_get_extents (layout, NULL, &rect);
pango_extents_to_pixels (&rect, NULL); pango_extents_to_pixels (&rect, NULL);
label_x = x + style->xthickness label_x = x + padding.left
+ (width / 2) + (width / 2)
+ ((width / 2) - rect.width - (style->xthickness * 2)) / 2; + ((width / 2) - rect.width - padding.left - padding.right) / 2;
label_y = y + style->ythickness label_y = y + padding.top
+ (height - rect.height - (style->ythickness * 2)) / 2; + (height - rect.height - padding.top - padding.bottom) / 2;
gtk_paint_layout (style, cr, gtk_render_layout (context, cr, label_x, label_y, layout);
state,
FALSE,
widget, "switch-off-label",
label_x, label_y,
layout);
g_object_unref (layout); g_object_unref (layout);
if (priv->is_dragging) if (priv->is_dragging)
handle.x = x + priv->handle_x; handle.x = x + priv->handle_x;
else if (priv->is_active) else if (priv->is_active)
handle.x = x + width - handle.width - style->xthickness; handle.x = x + width - handle.width - padding.right;
else else
handle.x = x + style->xthickness; handle.x = x + padding.left;
gtk_style_context_restore (context);
gtk_switch_paint_handle (widget, cr, &handle); gtk_switch_paint_handle (widget, cr, &handle);
...@@ -824,7 +833,10 @@ gtk_switch_set_active (GtkSwitch *sw, ...@@ -824,7 +833,10 @@ gtk_switch_set_active (GtkSwitch *sw,
if (priv->is_active != is_active) if (priv->is_active != is_active)
{ {
AtkObject *accessible; AtkObject *accessible;
GtkWidget *widget;
GtkStyleContext *context;
widget = GTK_WIDGET (sw);
priv->is_active = is_active; priv->is_active = is_active;
g_object_notify_by_pspec (G_OBJECT (sw), switch_props[PROP_ACTIVE]); g_object_notify_by_pspec (G_OBJECT (sw), switch_props[PROP_ACTIVE]);
...@@ -835,6 +847,14 @@ gtk_switch_set_active (GtkSwitch *sw, ...@@ -835,6 +847,14 @@ gtk_switch_set_active (GtkSwitch *sw,
accessible = gtk_widget_get_accessible (GTK_WIDGET (sw)); accessible = gtk_widget_get_accessible (GTK_WIDGET (sw));
atk_object_notify_state_change (accessible, ATK_STATE_CHECKED, priv->is_active); 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)); gtk_widget_queue_draw (GTK_WIDGET (sw));
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment