X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=gtk%2Fgtkscale.c;h=1af2f538b09dd56a047bb6f1738d476326087c67;hb=02e915273845b21af223e7e5e2425569afcf1b83;hp=d869240eea92fbf1a436ce90ae110e68ad4cddac;hpb=1d3f6b30b0100c96adbca9acb6c6cd49b18d2298;p=~andy%2Fgtk diff --git a/gtk/gtkscale.c b/gtk/gtkscale.c index d869240ee..1af2f538b 100644 --- a/gtk/gtkscale.c +++ b/gtk/gtkscale.c @@ -13,9 +13,7 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. + * License along with this library. If not, see . */ /* @@ -30,31 +28,40 @@ #include #include -#include "gdk/gdkkeysyms.h" -#include "gtkscale.h" +#include "gtkscaleprivate.h" + +#include "gtkadjustment.h" +#include "gtkbindings.h" +#include "gtkbuildable.h" +#include "gtkbuilderprivate.h" #include "gtkiconfactory.h" #include "gtkicontheme.h" +#include "gtkintl.h" #include "gtkmarshalers.h" -#include "gtkbindings.h" #include "gtkorientable.h" #include "gtkprivate.h" -#include "gtkintl.h" -#include "gtkbuildable.h" -#include "gtkbuilderprivate.h" +#include "gtktypebuiltins.h" + +#include "a11y/gtkscaleaccessible.h" /** * SECTION:gtkscale - * @Short_description: Base class for GtkHScale and GtkVScale + * @Short_description: A slider widget for selecting a value from a range * @Title: GtkScale * - * A GtkScale is a slider control used to select a numeric value. + * A GtkScale is a slider control used to select a numeric value. * To use it, you'll probably want to investigate the methods on * its base class, #GtkRange, in addition to the methods for GtkScale itself. * To set the value of a scale, you would normally use gtk_range_set_value(). * To detect changes to the value, you would normally use the * #GtkRange::value-changed signal. * + * Note that using the same upper and lower bounds for the #GtkScale (through + * the #GtkRange methods) will hide the slider itself. This is useful for + * applications that want to show an undeterminate value on the scale, without + * changing the layout of the application (such as movie or music players). + * * GtkScale as GtkBuildable * GtkScale supports a custom <marks> element, which * can contain multiple <mark> elements. The "value" and "position" @@ -72,7 +79,6 @@ * unrelated code portions otherwise */ - typedef struct _GtkScaleMark GtkScaleMark; struct _GtkScalePrivate @@ -91,13 +97,14 @@ struct _GtkScaleMark { gdouble value; gchar *markup; - GtkPositionType position; + GtkPositionType position; /* always GTK_POS_TOP or GTK_POS_BOTTOM */ }; enum { PROP_0, PROP_DIGITS, PROP_DRAW_VALUE, + PROP_HAS_ORIGIN, PROP_VALUE_POS }; @@ -116,10 +123,13 @@ static void gtk_scale_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec); -static void gtk_scale_size_request (GtkWidget *widget, - GtkRequisition *requisition); -static void gtk_scale_style_set (GtkWidget *widget, - GtkStyle *previous); +static void gtk_scale_get_preferred_width (GtkWidget *widget, + gint *minimum, + gint *natural); +static void gtk_scale_get_preferred_height (GtkWidget *widget, + gint *minimum, + gint *natural); +static void gtk_scale_style_updated (GtkWidget *widget); static void gtk_scale_get_range_border (GtkRange *range, GtkBorder *border); static void gtk_scale_get_mark_label_size (GtkScale *scale, @@ -156,23 +166,76 @@ G_DEFINE_TYPE_WITH_CODE (GtkScale, gtk_scale, GTK_TYPE_RANGE, G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE, gtk_scale_buildable_interface_init)) +static gint +compare_marks (gconstpointer a, gconstpointer b, gpointer data) +{ + gboolean inverted = GPOINTER_TO_INT (data); + gint val; + const GtkScaleMark *ma, *mb; -static gboolean -single_string_accumulator (GSignalInvocationHint *ihint, - GValue *return_accu, - const GValue *handler_return, - gpointer dummy) + val = inverted ? -1 : 1; + + ma = a; mb = b; + + return (ma->value > mb->value) ? val : ((ma->value < mb->value) ? -val : 0); +} + +static void +gtk_scale_notify (GObject *object, + GParamSpec *pspec) { - gboolean continue_emission; - const gchar *str; - - str = g_value_get_string (handler_return); - g_value_set_string (return_accu, str); - continue_emission = str == NULL; - - return continue_emission; + if (strcmp (pspec->name, "orientation") == 0) + { + GtkOrientation orientation; + + orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (object)); + gtk_range_set_flippable (GTK_RANGE (object), + orientation == GTK_ORIENTATION_HORIZONTAL); + } + else if (strcmp (pspec->name, "inverted") == 0) + { + GtkScale *scale = GTK_SCALE (object); + GtkScaleMark *mark; + GSList *m; + gint i, n; + gdouble *values; + + scale->priv->marks = g_slist_sort_with_data (scale->priv->marks, + compare_marks, + GINT_TO_POINTER (gtk_range_get_inverted (GTK_RANGE (scale)))); + + n = g_slist_length (scale->priv->marks); + values = g_new (gdouble, n); + for (m = scale->priv->marks, i = 0; m; m = m->next, i++) + { + mark = m->data; + values[i] = mark->value; + } + + _gtk_range_set_stop_values (GTK_RANGE (scale), values, n); + + g_free (values); + } + + if (G_OBJECT_CLASS (gtk_scale_parent_class)->notify) + G_OBJECT_CLASS (gtk_scale_parent_class)->notify (object, pspec); } +static void +gtk_scale_update_style (GtkScale *scale) +{ + gint slider_length; + GtkRange *range; + + range = GTK_RANGE (scale); + + gtk_widget_style_get (GTK_WIDGET (scale), + "slider-length", &slider_length, + NULL); + + gtk_range_set_min_slider_size (range, slider_length); + _gtk_scale_clear_layout (scale); +} #define add_slider_binding(binding_set, keyval, mask, scroll) \ gtk_binding_entry_add_signal (binding_set, keyval, mask, \ @@ -193,12 +256,14 @@ gtk_scale_class_init (GtkScaleClass *class) gobject_class->set_property = gtk_scale_set_property; gobject_class->get_property = gtk_scale_get_property; + gobject_class->notify = gtk_scale_notify; gobject_class->finalize = gtk_scale_finalize; - widget_class->style_set = gtk_scale_style_set; + widget_class->style_updated = gtk_scale_style_updated; widget_class->screen_changed = gtk_scale_screen_changed; widget_class->draw = gtk_scale_draw; - widget_class->size_request = gtk_scale_size_request; + widget_class->get_preferred_width = gtk_scale_get_preferred_width; + widget_class->get_preferred_height = gtk_scale_get_preferred_height; range_class->slider_detail = "Xscale"; range_class->get_range_border = gtk_scale_get_range_border; @@ -233,7 +298,7 @@ gtk_scale_class_init (GtkScaleClass *class) G_TYPE_FROM_CLASS (gobject_class), G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GtkScaleClass, format_value), - single_string_accumulator, NULL, + _gtk_single_string_accumulator, NULL, _gtk_marshal_STRING__DOUBLE, G_TYPE_STRING, 1, G_TYPE_DOUBLE); @@ -255,7 +320,15 @@ gtk_scale_class_init (GtkScaleClass *class) P_("Whether the current value is displayed as a string next to the slider"), TRUE, GTK_PARAM_READWRITE)); - + + g_object_class_install_property (gobject_class, + PROP_HAS_ORIGIN, + g_param_spec_boolean ("has-origin", + P_("Has Origin"), + P_("Whether the scale has an origin"), + TRUE, + GTK_PARAM_READWRITE)); + g_object_class_install_property (gobject_class, PROP_VALUE_POS, g_param_spec_enum ("value-pos", @@ -402,17 +475,8 @@ gtk_scale_class_init (GtkScaleClass *class) GTK_SCROLL_END); g_type_class_add_private (gobject_class, sizeof (GtkScalePrivate)); -} -static void -gtk_scale_orientation_notify (GtkRange *range, - const GParamSpec *pspec) -{ - GtkOrientation orientation; - - orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range)); - gtk_range_set_flippable (range, - orientation == GTK_ORIENTATION_HORIZONTAL); + gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_SCALE_ACCESSIBLE); } static void @@ -420,6 +484,7 @@ gtk_scale_init (GtkScale *scale) { GtkScalePrivate *priv; GtkRange *range = GTK_RANGE (scale); + GtkStyleContext *context; scale->priv = G_TYPE_INSTANCE_GET_PRIVATE (scale, GTK_TYPE_SCALE, @@ -430,15 +495,19 @@ gtk_scale_init (GtkScale *scale) gtk_range_set_slider_size_fixed (range, TRUE); + _gtk_range_set_has_origin (range, TRUE); + priv->draw_value = TRUE; priv->value_pos = GTK_POS_TOP; priv->digits = 1; - _gtk_range_set_round_digits (range, priv->digits); + gtk_range_set_round_digits (range, priv->digits); - gtk_scale_orientation_notify (range, NULL); - g_signal_connect (scale, "notify::orientation", - G_CALLBACK (gtk_scale_orientation_notify), - NULL); + gtk_range_set_flippable (range, + gtk_orientable_get_orientation (GTK_ORIENTABLE (range))== GTK_ORIENTATION_HORIZONTAL); + + context = gtk_widget_get_style_context (GTK_WIDGET (scale)); + gtk_style_context_add_class (context, GTK_STYLE_CLASS_SCALE); + gtk_scale_update_style (scale); } static void @@ -459,6 +528,9 @@ gtk_scale_set_property (GObject *object, case PROP_DRAW_VALUE: gtk_scale_set_draw_value (scale, g_value_get_boolean (value)); break; + case PROP_HAS_ORIGIN: + gtk_scale_set_has_origin (scale, g_value_get_boolean (value)); + break; case PROP_VALUE_POS: gtk_scale_set_value_pos (scale, g_value_get_enum (value)); break; @@ -485,6 +557,9 @@ gtk_scale_get_property (GObject *object, case PROP_DRAW_VALUE: g_value_set_boolean (value, priv->draw_value); break; + case PROP_HAS_ORIGIN: + g_value_set_boolean (value, gtk_scale_get_has_origin (scale)); + break; case PROP_VALUE_POS: g_value_set_enum (value, priv->value_pos); break; @@ -497,8 +572,8 @@ gtk_scale_get_property (GObject *object, /** * gtk_scale_new: * @orientation: the scale's orientation. - * @adjustment: the #GtkAdjustment which sets the range of the scale, or - * %NULL to create a new adjustment. + * @adjustment: (allow-none): the #GtkAdjustment which sets the range + * of the scale, or %NULL to create a new adjustment. * * Creates a new #GtkScale. * @@ -546,7 +621,7 @@ gtk_scale_new_with_range (GtkOrientation orientation, gdouble max, gdouble step) { - GtkObject *adj; + GtkAdjustment *adj; gint digits; g_return_val_if_fail (min < max, NULL); @@ -600,7 +675,7 @@ gtk_scale_set_digits (GtkScale *scale, { priv->digits = digits; if (priv->draw_value) - _gtk_range_set_round_digits (range, digits); + gtk_range_set_round_digits (range, digits); _gtk_scale_clear_layout (scale); gtk_widget_queue_resize (GTK_WIDGET (scale)); @@ -649,9 +724,9 @@ gtk_scale_set_draw_value (GtkScale *scale, { priv->draw_value = draw_value; if (draw_value) - _gtk_range_set_round_digits (GTK_RANGE (scale), priv->digits); + gtk_range_set_round_digits (GTK_RANGE (scale), priv->digits); else - _gtk_range_set_round_digits (GTK_RANGE (scale), -1); + gtk_range_set_round_digits (GTK_RANGE (scale), -1); _gtk_scale_clear_layout (scale); @@ -678,6 +753,54 @@ gtk_scale_get_draw_value (GtkScale *scale) return scale->priv->draw_value; } +/** + * gtk_scale_set_has_origin: + * @scale: a #GtkScale + * @has_origin: %TRUE if the scale has an origin + * + * If @has_origin is set to %TRUE (the default), + * the scale will highlight the part of the scale + * between the origin (bottom or left side) of the scale + * and the current value. + * + * Since: 3.4 + */ +void +gtk_scale_set_has_origin (GtkScale *scale, + gboolean has_origin) +{ + g_return_if_fail (GTK_IS_SCALE (scale)); + + has_origin = has_origin != FALSE; + + if (_gtk_range_get_has_origin (GTK_RANGE (scale)) != has_origin) + { + _gtk_range_set_has_origin (GTK_RANGE (scale), has_origin); + + gtk_widget_queue_draw (GTK_WIDGET (scale)); + + g_object_notify (G_OBJECT (scale), "has-origin"); + } +} + +/** + * gtk_scale_get_has_origin: + * @scale: a #GtkScale + * + * Returns whether the scale has an origin. + * + * Returns: %TRUE if the scale has an origin. + * + * Since: 3.4 + */ +gboolean +gtk_scale_get_has_origin (GtkScale *scale) +{ + g_return_val_if_fail (GTK_IS_SCALE (scale), FALSE); + + return _gtk_range_get_has_origin (GTK_RANGE (scale)); +} + /** * gtk_scale_set_value_pos: * @scale: a #GtkScale @@ -779,17 +902,17 @@ gtk_scale_get_range_border (GtkRange *range, NULL); + gtk_scale_get_mark_label_size (scale, GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2); + if (gtk_orientable_get_orientation (GTK_ORIENTABLE (scale)) == GTK_ORIENTATION_HORIZONTAL) { - gtk_scale_get_mark_label_size (scale, GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2); if (n1 > 0) border->top += h1 + value_spacing + slider_width / 2; if (n2 > 0) - border->bottom += h2 + value_spacing + slider_width / 2; + border->bottom += h2 + value_spacing + slider_width / 2; } else { - gtk_scale_get_mark_label_size (scale, GTK_POS_LEFT, &n1, &w1, &h1, &n2, &w2, &h2); if (n1 > 0) border->left += w1 + value_spacing + slider_width / 2; if (n2 > 0) @@ -884,8 +1007,8 @@ gtk_scale_get_mark_label_size (GtkScale *scale, pango_layout_set_markup (layout, mark->markup, -1); pango_layout_get_pixel_extents (layout, NULL, &logical_rect); - w = logical_rect.width; - h = logical_rect.height; + w = logical_rect.width; + h = logical_rect.height; } else { @@ -911,23 +1034,11 @@ gtk_scale_get_mark_label_size (GtkScale *scale, } static void -gtk_scale_style_set (GtkWidget *widget, - GtkStyle *previous) +gtk_scale_style_updated (GtkWidget *widget) { - gint slider_length; - GtkRange *range; + gtk_scale_update_style (GTK_SCALE (widget)); - range = GTK_RANGE (widget); - - gtk_widget_style_get (widget, - "slider-length", &slider_length, - NULL); - - gtk_range_set_min_slider_size (range, slider_length); - - _gtk_scale_clear_layout (GTK_SCALE (widget)); - - GTK_WIDGET_CLASS (gtk_scale_parent_class)->style_set (widget, previous); + GTK_WIDGET_CLASS (gtk_scale_parent_class)->style_updated (widget); } static void @@ -938,41 +1049,62 @@ gtk_scale_screen_changed (GtkWidget *widget, } static void -gtk_scale_size_request (GtkWidget *widget, - GtkRequisition *requisition) +gtk_scale_get_preferred_width (GtkWidget *widget, + gint *minimum, + gint *natural) { - GtkRange *range = GTK_RANGE (widget); - gint n1, w1, h1, n2, w2, h2; - gint slider_length; - - GTK_WIDGET_CLASS (gtk_scale_parent_class)->size_request (widget, requisition); + GTK_WIDGET_CLASS (gtk_scale_parent_class)->get_preferred_width (widget, minimum, natural); - gtk_widget_style_get (widget, "slider-length", &slider_length, NULL); + if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL) + { + gint n1, w1, h1, n2, w2, h2; + gint slider_length; + gint w; + gtk_widget_style_get (widget, "slider-length", &slider_length, NULL); - if (gtk_orientable_get_orientation (GTK_ORIENTABLE (range)) == GTK_ORIENTATION_HORIZONTAL) - { gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2); w1 = (n1 - 1) * w1 + MAX (w1, slider_length); w2 = (n2 - 1) * w2 + MAX (w2, slider_length); - requisition->width = MAX (requisition->width, MAX (w1, w2)); + w = MAX (w1, w2); + + *minimum = MAX (*minimum, w); + *natural = MAX (*natural, w); } - else +} + +static void +gtk_scale_get_preferred_height (GtkWidget *widget, + gint *minimum, + gint *natural) +{ + GTK_WIDGET_CLASS (gtk_scale_parent_class)->get_preferred_height (widget, minimum, natural); + + + if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_VERTICAL) { - gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_LEFT, &n1, &w1, &h1, &n2, &w2, &h2); + gint n1, w1, h1, n2, w2, h2; + gint slider_length; + gint h; + + gtk_widget_style_get (widget, "slider-length", &slider_length, NULL); + + gtk_scale_get_mark_label_size (GTK_SCALE (widget), GTK_POS_TOP, &n1, &w1, &h1, &n2, &w2, &h2); h1 = (n1 - 1) * h1 + MAX (h1, slider_length); h2 = (n2 - 1) * h1 + MAX (h2, slider_length); - requisition->height = MAX (requisition->height, MAX (h1, h2)); + h = MAX (h1, h2); + + *minimum = MAX (*minimum, h); + *natural = MAX (*natural, h); } } static gint -find_next_pos (GtkWidget *widget, +find_next_pos (GtkWidget *widget, GSList *list, gint *marks, - GtkPositionType pos, - gint match) + GtkPositionType pos) { GtkAllocation allocation; GSList *m; @@ -982,12 +1114,12 @@ find_next_pos (GtkWidget *widget, { GtkScaleMark *mark = m->data; - if (match == (mark->position == pos)) + if (mark->position == pos) return marks[i]; } gtk_widget_get_allocation (widget, &allocation); - if (pos == GTK_POS_TOP || pos == GTK_POS_BOTTOM) + if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL) return allocation.width; else return allocation.height; @@ -1000,20 +1132,18 @@ gtk_scale_draw (GtkWidget *widget, GtkScale *scale = GTK_SCALE (widget); GtkScalePrivate *priv = scale->priv; GtkRange *range = GTK_RANGE (scale); - GtkStateType state_type; - GtkStyle *style; - gint n_marks; + GtkStyleContext *context; gint *marks; gint focus_padding; gint slider_width; gint value_spacing; gint min_sep = 4; - style = gtk_widget_get_style (widget); + context = gtk_widget_get_style_context (widget); gtk_widget_style_get (widget, "focus-padding", &focus_padding, - "slider-width", &slider_width, - "value-spacing", &value_spacing, + "slider-width", &slider_width, + "value-spacing", &value_spacing, NULL); /* We need to chain up _first_ so the various geometry members of @@ -1021,10 +1151,6 @@ gtk_scale_draw (GtkWidget *widget, */ GTK_WIDGET_CLASS (gtk_scale_parent_class)->draw (widget, cr); - state_type = GTK_STATE_NORMAL; - if (!gtk_widget_is_sensitive (widget)) - state_type = GTK_STATE_INSENSITIVE; - if (priv->marks) { GtkOrientation orientation; @@ -1038,7 +1164,8 @@ gtk_scale_draw (GtkWidget *widget, gint min_pos, max_pos; orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range)); - n_marks = _gtk_range_get_stop_positions (range, &marks); + _gtk_range_get_stop_positions (range, &marks); + layout = gtk_widget_create_pango_layout (widget, NULL); gtk_range_get_range_rect (range, &range_rect); @@ -1056,18 +1183,22 @@ gtk_scale_draw (GtkWidget *widget, y1 = range_rect.y; y2 = y1 - slider_width / 2; min_pos = min_pos_before; - max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 1) - min_sep; + max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP) - min_sep; } else { y1 = range_rect.y + range_rect.height; y2 = y1 + slider_width / 2; min_pos = min_pos_after; - max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP, 0) - min_sep; + max_pos = find_next_pos (widget, m, marks + i, GTK_POS_BOTTOM) - min_sep; } - gtk_paint_vline (style, cr, state_type, - widget, "scale-mark", y1, y2, x1); + gtk_style_context_save (context); + gtk_style_context_add_class (context, GTK_STYLE_CLASS_MARK); + + gtk_style_context_add_class (context, GTK_STYLE_CLASS_SEPARATOR); + gtk_render_line (context, cr, x1, y1, x1, y2); + gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SEPARATOR); if (mark->markup) { @@ -1092,31 +1223,35 @@ gtk_scale_draw (GtkWidget *widget, min_pos_after = x3 + logical_rect.width + min_sep; } - gtk_paint_layout (style, cr, state_type, - FALSE, widget, "scale-mark", - x3, y3, layout); + gtk_render_layout (context, cr, x3, y3, layout); } + + gtk_style_context_restore (context); } else { - if (mark->position == GTK_POS_LEFT) + if (mark->position == GTK_POS_TOP) { x1 = range_rect.x; x2 = range_rect.x - slider_width / 2; min_pos = min_pos_before; - max_pos = find_next_pos (widget, m, marks + i, GTK_POS_LEFT, 1) - min_sep; + max_pos = find_next_pos (widget, m, marks + i, GTK_POS_TOP) - min_sep; } else { x1 = range_rect.x + range_rect.width; x2 = range_rect.x + range_rect.width + slider_width / 2; min_pos = min_pos_after; - max_pos = find_next_pos (widget, m, marks + i, GTK_POS_LEFT, 0) - min_sep; + max_pos = find_next_pos (widget, m, marks + i, GTK_POS_BOTTOM) - min_sep; } y1 = marks[i]; - gtk_paint_hline (style, cr, state_type, - widget, "range-mark", x1, x2, y1); + gtk_style_context_save (context); + gtk_style_context_add_class (context, GTK_STYLE_CLASS_MARK); + + gtk_style_context_add_class (context, GTK_STYLE_CLASS_SEPARATOR); + gtk_render_line (context, cr, x1, y1, x2, y1); + gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SEPARATOR); if (mark->markup) { @@ -1130,7 +1265,7 @@ gtk_scale_draw (GtkWidget *widget, y3 = max_pos - logical_rect.height; if (y3 < 0) y3 = 0; - if (mark->position == GTK_POS_LEFT) + if (mark->position == GTK_POS_TOP) { x3 = x2 - value_spacing - logical_rect.width; min_pos_before = y3 + logical_rect.height + min_sep; @@ -1141,12 +1276,12 @@ gtk_scale_draw (GtkWidget *widget, min_pos_after = y3 + logical_rect.height + min_sep; } - gtk_paint_layout (style, cr, state_type, - FALSE, widget, "scale-mark", - x3, y3, layout); + gtk_render_layout (context, cr, x3, y3, layout); } + + gtk_style_context_restore (context); } - } + } g_object_unref (layout); g_free (marks); @@ -1154,28 +1289,19 @@ gtk_scale_draw (GtkWidget *widget, if (priv->draw_value) { - GtkOrientation orientation; GtkAllocation allocation; PangoLayout *layout; gint x, y; - orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (range)); layout = gtk_scale_get_layout (scale); gtk_scale_get_layout_offsets (scale, &x, &y); gtk_widget_get_allocation (widget, &allocation); - gtk_paint_layout (style, - cr, - state_type, - FALSE, - widget, - orientation == GTK_ORIENTATION_HORIZONTAL ? - "hscale" : "vscale", - x - allocation.x, - y - allocation.y, - layout); - + gtk_render_layout (context, cr, + x - allocation.x, + y - allocation.y, + layout); } return FALSE; @@ -1306,8 +1432,7 @@ _gtk_scale_format_value (GtkScale *scale, if (fmt) return fmt; else - /* insert a LRM, to prevent -20 to come out as 20- in RTL locales */ - return g_strdup_printf ("\342\200\216%0.*f", priv->digits, value); + return g_strdup_printf ("%0.*f", priv->digits, value); } static void @@ -1364,8 +1489,8 @@ gtk_scale_get_layout (GtkScale *scale) /** * gtk_scale_get_layout_offsets: * @scale: a #GtkScale - * @x: (allow-none): location to store X offset of layout, or %NULL - * @y: (allow-none): location to store Y offset of layout, or %NULL + * @x: (out) (allow-none): location to store X offset of layout, or %NULL + * @y: (out) (allow-none): location to store Y offset of layout, or %NULL * * Obtains the coordinates where the scale will draw the * #PangoLayout representing the text in the scale. Remember @@ -1412,8 +1537,10 @@ _gtk_scale_clear_layout (GtkScale *scale) } static void -gtk_scale_mark_free (GtkScaleMark *mark) +gtk_scale_mark_free (gpointer data) { + GtkScaleMark *mark = data; + g_free (mark->markup); g_free (mark); } @@ -1430,49 +1557,43 @@ void gtk_scale_clear_marks (GtkScale *scale) { GtkScalePrivate *priv; + GtkStyleContext *context; g_return_if_fail (GTK_IS_SCALE (scale)); priv = scale->priv; - g_slist_foreach (priv->marks, (GFunc)gtk_scale_mark_free, NULL); - g_slist_free (priv->marks); + g_slist_free_full (priv->marks, gtk_scale_mark_free); priv->marks = NULL; + context = gtk_widget_get_style_context (GTK_WIDGET (scale)); + gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW); + gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE); + _gtk_range_set_stop_values (GTK_RANGE (scale), NULL, 0); gtk_widget_queue_resize (GTK_WIDGET (scale)); } -static gint -compare_marks (gpointer a, gpointer b) -{ - GtkScaleMark *ma, *mb; - - ma = a; mb = b; - - return (gint) (ma->value - mb->value); -} - /** * gtk_scale_add_mark: * @scale: a #GtkScale - * @value: the value at which the mark is placed, must be between + * @value: the value at which the mark is placed, must be between * the lower and upper limits of the scales' adjustment * @position: where to draw the mark. For a horizontal scale, #GTK_POS_TOP - * is drawn above the scale, anything else below. For a vertical scale, - * #GTK_POS_LEFT is drawn to the left of the scale, anything else to the - * right. + * and %GTK_POS_LEFT are drawn above the scale, anything else below. + * For a vertical scale, #GTK_POS_LEFT and %GTK_POS_TOP are drawn to + * the left of the scale, anything else to the right. * @markup: (allow-none): Text to be shown at the mark, using Pango markup, or %NULL * * - * Adds a mark at @value. + * Adds a mark at @value. * - * A mark is indicated visually by drawing a tick mark next to the scale, - * and GTK+ makes it easy for the user to position the scale exactly at the + * A mark is indicated visually by drawing a tick mark next to the scale, + * and GTK+ makes it easy for the user to position the scale exactly at the * marks value. * - * If @markup is not %NULL, text is shown next to the tick mark. + * If @markup is not %NULL, text is shown next to the tick mark. * * To remove marks from a scale, use gtk_scale_clear_marks(). * @@ -1489,6 +1610,8 @@ gtk_scale_add_mark (GtkScale *scale, GSList *m; gdouble *values; gint n, i; + GtkStyleContext *context; + int all_pos; g_return_if_fail (GTK_IS_SCALE (scale)); @@ -1497,23 +1620,50 @@ gtk_scale_add_mark (GtkScale *scale, mark = g_new (GtkScaleMark, 1); mark->value = value; mark->markup = g_strdup (markup); - mark->position = position; - - priv->marks = g_slist_insert_sorted (priv->marks, mark, - (GCompareFunc) compare_marks); + if (position == GTK_POS_LEFT || + position == GTK_POS_TOP) + mark->position = GTK_POS_TOP; + else + mark->position = GTK_POS_BOTTOM; + + priv->marks = g_slist_insert_sorted_with_data (priv->marks, mark, + compare_marks, + GINT_TO_POINTER (gtk_range_get_inverted (GTK_RANGE (scale)))); + +#define MARKS_ABOVE 1 +#define MARKS_BELOW 2 + all_pos = 0; n = g_slist_length (priv->marks); values = g_new (gdouble, n); for (m = priv->marks, i = 0; m; m = m->next, i++) { mark = m->data; values[i] = mark->value; + if (mark->position == GTK_POS_TOP) + all_pos |= MARKS_ABOVE; + else + all_pos |= MARKS_BELOW; } - + _gtk_range_set_stop_values (GTK_RANGE (scale), values, n); g_free (values); + /* Set the style classes for the slider, so it could + * point to the right direction when marks are present + */ + context = gtk_widget_get_style_context (GTK_WIDGET (scale)); + + if (all_pos & MARKS_ABOVE) + gtk_style_context_add_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE); + else + gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE); + if (all_pos & MARKS_BELOW) + gtk_style_context_add_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW); + else + gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW); + gtk_widget_queue_resize (GTK_WIDGET (scale)); } @@ -1589,7 +1739,7 @@ marks_start_element (GMarkupParseContext *context, msg_context = values[i]; else if (strcmp (names[i], "value") == 0) { - GValue gvalue = { 0, }; + GValue gvalue = G_VALUE_INIT; if (!gtk_builder_value_from_string_type (parser_data->builder, G_TYPE_DOUBLE, values[i], &gvalue, error)) return; @@ -1599,7 +1749,7 @@ marks_start_element (GMarkupParseContext *context, } else if (strcmp (names[i], "position") == 0) { - GValue gvalue = { 0, }; + GValue gvalue = G_VALUE_INIT; if (!gtk_builder_value_from_string_type (parser_data->builder, GTK_TYPE_POSITION_TYPE, values[i], &gvalue, error)) return; @@ -1638,7 +1788,11 @@ marks_start_element (GMarkupParseContext *context, mark = g_slice_new (MarkData); mark->value = value; - mark->position = position; + if (position == GTK_POS_LEFT || + position == GTK_POS_TOP) + mark->position = GTK_POS_TOP; + else + mark->position = GTK_POS_BOTTOM; mark->markup = g_string_new (""); mark->context = g_strdup (msg_context); mark->translatable = translatable;