]> Pileus Git - ~andy/gtk/blob - gtk/gtkrange.c
range: Make drawing use a single cairo context
[~andy/gtk] / gtk / gtkrange.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  * Copyright (C) 2001 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /*
22  * Modified by the GTK+ Team and others 1997-2004.  See the AUTHORS
23  * file for a list of people on the GTK+ Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
26  */
27
28 #include "config.h"
29
30 #include <stdio.h>
31 #include <math.h>
32
33 #include <gdk/gdkkeysyms.h>
34 #include "gtkmain.h"
35 #include "gtkmarshalers.h"
36 #include "gtkorientable.h"
37 #include "gtkrange.h"
38 #include "gtkscale.h"
39 #include "gtkscrollbar.h"
40 #include "gtkprivate.h"
41 #include "gtkintl.h"
42
43 /**
44  * SECTION:gtkrange
45  * @Short_description: Base class for widgets which visualize an adjustment
46  * @Title: GtkRange
47  *
48  * #GtkRange is the common base class for widgets which visualize an
49  * adjustment, e.g #GtkScale or #GtkScrollbar.
50  *
51  * Apart from signals for monitoring the parameters of the adjustment,
52  * #GtkRange provides properties and methods for influencing the sensitivity
53  * of the "steppers". It also provides properties and methods for setting a
54  * "fill level" on range widgets. See gtk_range_set_fill_level().
55  */
56
57
58 #define SCROLL_DELAY_FACTOR 5    /* Scroll repeat multiplier */
59 #define UPDATE_DELAY        300  /* Delay for queued update */
60
61 typedef struct _GtkRangeStepTimer GtkRangeStepTimer;
62
63 typedef enum {
64   MOUSE_OUTSIDE,
65   MOUSE_STEPPER_A,
66   MOUSE_STEPPER_B,
67   MOUSE_STEPPER_C,
68   MOUSE_STEPPER_D,
69   MOUSE_TROUGH,
70   MOUSE_SLIDER,
71   MOUSE_WIDGET /* inside widget but not in any of the above GUI elements */
72 } MouseLocation;
73
74 struct _GtkRangePrivate
75 {
76   MouseLocation      mouse_location;
77   /* last mouse coords we got, or -1 if mouse is outside the range */
78   gint  mouse_x;
79   gint  mouse_y;
80   MouseLocation      grab_location;   /* "grabbed" mouse location, OUTSIDE for no grab */
81   guint              grab_button : 8; /* 0 if none */
82
83   GtkRangeStepTimer *timer;
84
85   GtkAdjustment     *adjustment;
86   GtkOrientation     orientation;
87   GtkSensitivityType lower_sensitivity;
88   GtkSensitivityType upper_sensitivity;
89   GtkUpdateType      update_policy;
90
91   GdkDevice         *grab_device;
92   GdkRectangle       range_rect;     /* Area of entire stepper + trough assembly in widget->window coords */
93   /* These are in widget->window coordinates */
94   GdkRectangle       stepper_a;
95   GdkRectangle       stepper_b;
96   GdkRectangle       stepper_c;
97   GdkRectangle       stepper_d;
98   GdkRectangle       trough;         /* The trough rectangle is the area the thumb can slide in, not the entire range_rect */
99   GdkRectangle       slider;
100   GdkWindow         *event_window;
101
102   GQuark             slider_detail_quark;
103   GQuark             stepper_detail_quark[4];
104
105   gboolean recalc_marks;
106
107   gdouble  fill_level;
108   gdouble *marks;
109
110   gint *mark_pos;
111   gint  min_slider_size;
112   gint  n_marks;
113   gint  round_digits;                /* Round off value to this many digits, -1 for no rounding */
114   gint  slide_initial_slider_position;
115   gint  slide_initial_coordinate;
116   gint  slider_start;                /* Slider range along the long dimension, in widget->window coords */
117   gint  slider_end;
118
119   guint repaint_id;
120   guint update_timeout_id;
121
122   /* Steppers are: < > ---- < >
123    *               a b      c d
124    */
125   guint has_stepper_a          : 1;
126   guint has_stepper_b          : 1;
127   guint has_stepper_c          : 1;
128   guint has_stepper_d          : 1;
129
130   guint flippable              : 1;
131   guint inverted               : 1;
132   guint need_recalc            : 1;
133   guint slider_size_fixed      : 1;
134   guint trough_click_forward   : 1;  /* trough click was on the forward side of slider */
135   guint update_pending         : 1;  /* need to emit value_changed */
136
137   /* Stepper sensitivity */
138   guint lower_sensitive        : 1;
139   guint upper_sensitive        : 1;
140
141   /* Fill level */
142   guint show_fill_level        : 1;
143   guint restrict_to_fill_level : 1;
144 };
145
146
147 enum {
148   PROP_0,
149   PROP_ORIENTATION,
150   PROP_UPDATE_POLICY,
151   PROP_ADJUSTMENT,
152   PROP_INVERTED,
153   PROP_LOWER_STEPPER_SENSITIVITY,
154   PROP_UPPER_STEPPER_SENSITIVITY,
155   PROP_SHOW_FILL_LEVEL,
156   PROP_RESTRICT_TO_FILL_LEVEL,
157   PROP_FILL_LEVEL
158 };
159
160 enum {
161   VALUE_CHANGED,
162   ADJUST_BOUNDS,
163   MOVE_SLIDER,
164   CHANGE_VALUE,
165   LAST_SIGNAL
166 };
167
168 typedef enum {
169   STEPPER_A,
170   STEPPER_B,
171   STEPPER_C,
172   STEPPER_D
173 } Stepper;
174
175 static void gtk_range_set_property   (GObject          *object,
176                                       guint             prop_id,
177                                       const GValue     *value,
178                                       GParamSpec       *pspec);
179 static void gtk_range_get_property   (GObject          *object,
180                                       guint             prop_id,
181                                       GValue           *value,
182                                       GParamSpec       *pspec);
183 static void gtk_range_destroy        (GtkObject        *object);
184 static void gtk_range_size_request   (GtkWidget        *widget,
185                                       GtkRequisition   *requisition);
186 static void gtk_range_size_allocate  (GtkWidget        *widget,
187                                       GtkAllocation    *allocation);
188 static void gtk_range_realize        (GtkWidget        *widget);
189 static void gtk_range_unrealize      (GtkWidget        *widget);
190 static void gtk_range_map            (GtkWidget        *widget);
191 static void gtk_range_unmap          (GtkWidget        *widget);
192 static gboolean gtk_range_expose         (GtkWidget        *widget,
193                                       GdkEventExpose   *event);
194 static gboolean gtk_range_button_press   (GtkWidget        *widget,
195                                       GdkEventButton   *event);
196 static gboolean gtk_range_button_release (GtkWidget        *widget,
197                                       GdkEventButton   *event);
198 static gboolean gtk_range_motion_notify  (GtkWidget        *widget,
199                                       GdkEventMotion   *event);
200 static gboolean gtk_range_enter_notify   (GtkWidget        *widget,
201                                       GdkEventCrossing *event);
202 static gboolean gtk_range_leave_notify   (GtkWidget        *widget,
203                                       GdkEventCrossing *event);
204 static gboolean gtk_range_grab_broken (GtkWidget          *widget,
205                                        GdkEventGrabBroken *event);
206 static void gtk_range_grab_notify    (GtkWidget          *widget,
207                                       gboolean            was_grabbed);
208 static void gtk_range_state_changed  (GtkWidget          *widget,
209                                       GtkStateType        previous_state);
210 static gboolean gtk_range_scroll_event   (GtkWidget        *widget,
211                                       GdkEventScroll   *event);
212 static void gtk_range_style_set      (GtkWidget        *widget,
213                                       GtkStyle         *previous_style);
214 static void update_slider_position   (GtkRange         *range,
215                                       gint              mouse_x,
216                                       gint              mouse_y);
217 static void stop_scrolling           (GtkRange         *range);
218
219 /* Range methods */
220
221 static void gtk_range_move_slider              (GtkRange         *range,
222                                                 GtkScrollType     scroll);
223
224 /* Internals */
225 static gboolean      gtk_range_scroll                   (GtkRange      *range,
226                                                          GtkScrollType  scroll);
227 static gboolean      gtk_range_update_mouse_location    (GtkRange      *range);
228 static void          gtk_range_calc_layout              (GtkRange      *range,
229                                                          gdouble        adjustment_value);
230 static void          gtk_range_calc_marks               (GtkRange      *range);
231 static void          gtk_range_get_props                (GtkRange      *range,
232                                                          gint          *slider_width,
233                                                          gint          *stepper_size,
234                                                          gint          *focus_width,
235                                                          gint          *trough_border,
236                                                          gint          *stepper_spacing,
237                                                          gboolean      *trough_under_steppers,
238                                                          gint          *arrow_displacement_x,
239                                                          gint          *arrow_displacement_y);
240 static void          gtk_range_calc_request             (GtkRange      *range,
241                                                          gint           slider_width,
242                                                          gint           stepper_size,
243                                                          gint           focus_width,
244                                                          gint           trough_border,
245                                                          gint           stepper_spacing,
246                                                          GdkRectangle  *range_rect,
247                                                          GtkBorder     *border,
248                                                          gint          *n_steppers_p,
249                                                          gboolean      *has_steppers_ab,
250                                                          gboolean      *has_steppers_cd,
251                                                          gint          *slider_length_p);
252 static void          gtk_range_adjustment_value_changed (GtkAdjustment *adjustment,
253                                                          gpointer       data);
254 static void          gtk_range_adjustment_changed       (GtkAdjustment *adjustment,
255                                                          gpointer       data);
256 static void          gtk_range_add_step_timer           (GtkRange      *range,
257                                                          GtkScrollType  step);
258 static void          gtk_range_remove_step_timer        (GtkRange      *range);
259 static void          gtk_range_reset_update_timer       (GtkRange      *range);
260 static void          gtk_range_remove_update_timer      (GtkRange      *range);
261 static GdkRectangle* get_area                           (GtkRange      *range,
262                                                          MouseLocation  location);
263 static gboolean      gtk_range_real_change_value        (GtkRange      *range,
264                                                          GtkScrollType  scroll,
265                                                          gdouble        value);
266 static void          gtk_range_update_value             (GtkRange      *range);
267 static gboolean      gtk_range_key_press                (GtkWidget     *range,
268                                                          GdkEventKey   *event);
269
270
271 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GtkRange, gtk_range, GTK_TYPE_WIDGET,
272                                   G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
273                                                          NULL))
274
275 static guint signals[LAST_SIGNAL];
276
277
278 static void
279 gtk_range_class_init (GtkRangeClass *class)
280 {
281   GObjectClass   *gobject_class;
282   GtkObjectClass *object_class;
283   GtkWidgetClass *widget_class;
284
285   gobject_class = G_OBJECT_CLASS (class);
286   object_class = (GtkObjectClass*) class;
287   widget_class = (GtkWidgetClass*) class;
288
289   gobject_class->set_property = gtk_range_set_property;
290   gobject_class->get_property = gtk_range_get_property;
291
292   object_class->destroy = gtk_range_destroy;
293
294   widget_class->size_request = gtk_range_size_request;
295   widget_class->size_allocate = gtk_range_size_allocate;
296   widget_class->realize = gtk_range_realize;
297   widget_class->unrealize = gtk_range_unrealize;  
298   widget_class->map = gtk_range_map;
299   widget_class->unmap = gtk_range_unmap;
300   widget_class->expose_event = gtk_range_expose;
301   widget_class->button_press_event = gtk_range_button_press;
302   widget_class->button_release_event = gtk_range_button_release;
303   widget_class->motion_notify_event = gtk_range_motion_notify;
304   widget_class->scroll_event = gtk_range_scroll_event;
305   widget_class->enter_notify_event = gtk_range_enter_notify;
306   widget_class->leave_notify_event = gtk_range_leave_notify;
307   widget_class->grab_broken_event = gtk_range_grab_broken;
308   widget_class->grab_notify = gtk_range_grab_notify;
309   widget_class->state_changed = gtk_range_state_changed;
310   widget_class->style_set = gtk_range_style_set;
311   widget_class->key_press_event = gtk_range_key_press;
312
313   class->move_slider = gtk_range_move_slider;
314   class->change_value = gtk_range_real_change_value;
315
316   class->slider_detail = "slider";
317   class->stepper_detail = "stepper";
318
319   /**
320    * GtkRange::value-changed:
321    * @range: the #GtkRange that received the signal
322    *
323    * Emitted when the range value changes.
324    */
325   signals[VALUE_CHANGED] =
326     g_signal_new (I_("value-changed"),
327                   G_TYPE_FROM_CLASS (gobject_class),
328                   G_SIGNAL_RUN_LAST,
329                   G_STRUCT_OFFSET (GtkRangeClass, value_changed),
330                   NULL, NULL,
331                   _gtk_marshal_VOID__VOID,
332                   G_TYPE_NONE, 0);
333
334   /**
335    * GtkRange::adjust-bounds:
336    * @range: the #GtkRange that received the signal
337    * @value: the value before we clamp
338    *
339    * Emitted before clamping a value, to give the application a
340    * chance to adjust the bounds.
341    */
342   signals[ADJUST_BOUNDS] =
343     g_signal_new (I_("adjust-bounds"),
344                   G_TYPE_FROM_CLASS (gobject_class),
345                   G_SIGNAL_RUN_LAST,
346                   G_STRUCT_OFFSET (GtkRangeClass, adjust_bounds),
347                   NULL, NULL,
348                   _gtk_marshal_VOID__DOUBLE,
349                   G_TYPE_NONE, 1,
350                   G_TYPE_DOUBLE);
351   
352   /**
353    * GtkRange::move-slider:
354    * @range: the #GtkRange that received the signal
355    * @step: how to move the slider
356    *
357    * Virtual function that moves the slider. Used for keybindings.
358    */
359   signals[MOVE_SLIDER] =
360     g_signal_new (I_("move-slider"),
361                   G_TYPE_FROM_CLASS (gobject_class),
362                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
363                   G_STRUCT_OFFSET (GtkRangeClass, move_slider),
364                   NULL, NULL,
365                   _gtk_marshal_VOID__ENUM,
366                   G_TYPE_NONE, 1,
367                   GTK_TYPE_SCROLL_TYPE);
368
369   /**
370    * GtkRange::change-value:
371    * @range: the #GtkRange that received the signal
372    * @scroll: the type of scroll action that was performed
373    * @value: the new value resulting from the scroll action
374    * @returns: %TRUE to prevent other handlers from being invoked for the
375    * signal, %FALSE to propagate the signal further
376    *
377    * The ::change-value signal is emitted when a scroll action is
378    * performed on a range.  It allows an application to determine the
379    * type of scroll event that occurred and the resultant new value.
380    * The application can handle the event itself and return %TRUE to
381    * prevent further processing.  Or, by returning %FALSE, it can pass
382    * the event to other handlers until the default GTK+ handler is
383    * reached.
384    *
385    * The value parameter is unrounded.  An application that overrides
386    * the ::change-value signal is responsible for clamping the value to
387    * the desired number of decimal digits; the default GTK+ handler 
388    * clamps the value based on @range->round_digits.
389    *
390    * It is not possible to use delayed update policies in an overridden
391    * ::change-value handler.
392    *
393    * Since: 2.6
394    */
395   signals[CHANGE_VALUE] =
396     g_signal_new (I_("change-value"),
397                   G_TYPE_FROM_CLASS (gobject_class),
398                   G_SIGNAL_RUN_LAST,
399                   G_STRUCT_OFFSET (GtkRangeClass, change_value),
400                   _gtk_boolean_handled_accumulator, NULL,
401                   _gtk_marshal_BOOLEAN__ENUM_DOUBLE,
402                   G_TYPE_BOOLEAN, 2,
403                   GTK_TYPE_SCROLL_TYPE,
404                   G_TYPE_DOUBLE);
405
406   g_object_class_override_property (gobject_class,
407                                     PROP_ORIENTATION,
408                                     "orientation");
409
410   g_object_class_install_property (gobject_class,
411                                    PROP_UPDATE_POLICY,
412                                    g_param_spec_enum ("update-policy",
413                                                       P_("Update policy"),
414                                                       P_("How the range should be updated on the screen"),
415                                                       GTK_TYPE_UPDATE_TYPE,
416                                                       GTK_UPDATE_CONTINUOUS,
417                                                       GTK_PARAM_READWRITE));
418   
419   g_object_class_install_property (gobject_class,
420                                    PROP_ADJUSTMENT,
421                                    g_param_spec_object ("adjustment",
422                                                         P_("Adjustment"),
423                                                         P_("The GtkAdjustment that contains the current value of this range object"),
424                                                         GTK_TYPE_ADJUSTMENT,
425                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
426
427   g_object_class_install_property (gobject_class,
428                                    PROP_INVERTED,
429                                    g_param_spec_boolean ("inverted",
430                                                         P_("Inverted"),
431                                                         P_("Invert direction slider moves to increase range value"),
432                                                          FALSE,
433                                                          GTK_PARAM_READWRITE));
434
435   g_object_class_install_property (gobject_class,
436                                    PROP_LOWER_STEPPER_SENSITIVITY,
437                                    g_param_spec_enum ("lower-stepper-sensitivity",
438                                                       P_("Lower stepper sensitivity"),
439                                                       P_("The sensitivity policy for the stepper that points to the adjustment's lower side"),
440                                                       GTK_TYPE_SENSITIVITY_TYPE,
441                                                       GTK_SENSITIVITY_AUTO,
442                                                       GTK_PARAM_READWRITE));
443
444   g_object_class_install_property (gobject_class,
445                                    PROP_UPPER_STEPPER_SENSITIVITY,
446                                    g_param_spec_enum ("upper-stepper-sensitivity",
447                                                       P_("Upper stepper sensitivity"),
448                                                       P_("The sensitivity policy for the stepper that points to the adjustment's upper side"),
449                                                       GTK_TYPE_SENSITIVITY_TYPE,
450                                                       GTK_SENSITIVITY_AUTO,
451                                                       GTK_PARAM_READWRITE));
452
453   /**
454    * GtkRange:show-fill-level:
455    *
456    * The show-fill-level property controls whether fill level indicator
457    * graphics are displayed on the trough. See
458    * gtk_range_set_show_fill_level().
459    *
460    * Since: 2.12
461    **/
462   g_object_class_install_property (gobject_class,
463                                    PROP_SHOW_FILL_LEVEL,
464                                    g_param_spec_boolean ("show-fill-level",
465                                                          P_("Show Fill Level"),
466                                                          P_("Whether to display a fill level indicator graphics on trough."),
467                                                          FALSE,
468                                                          GTK_PARAM_READWRITE));
469
470   /**
471    * GtkRange:restrict-to-fill-level:
472    *
473    * The restrict-to-fill-level property controls whether slider
474    * movement is restricted to an upper boundary set by the
475    * fill level. See gtk_range_set_restrict_to_fill_level().
476    *
477    * Since: 2.12
478    **/
479   g_object_class_install_property (gobject_class,
480                                    PROP_RESTRICT_TO_FILL_LEVEL,
481                                    g_param_spec_boolean ("restrict-to-fill-level",
482                                                          P_("Restrict to Fill Level"),
483                                                          P_("Whether to restrict the upper boundary to the fill level."),
484                                                          TRUE,
485                                                          GTK_PARAM_READWRITE));
486
487   /**
488    * GtkRange:fill-level:
489    *
490    * The fill level (e.g. prebuffering of a network stream).
491    * See gtk_range_set_fill_level().
492    *
493    * Since: 2.12
494    **/
495   g_object_class_install_property (gobject_class,
496                                    PROP_FILL_LEVEL,
497                                    g_param_spec_double ("fill-level",
498                                                         P_("Fill Level"),
499                                                         P_("The fill level."),
500                                                         -G_MAXDOUBLE,
501                                                         G_MAXDOUBLE,
502                                                         G_MAXDOUBLE,
503                                                         GTK_PARAM_READWRITE));
504
505   gtk_widget_class_install_style_property (widget_class,
506                                            g_param_spec_int ("slider-width",
507                                                              P_("Slider Width"),
508                                                              P_("Width of scrollbar or scale thumb"),
509                                                              0,
510                                                              G_MAXINT,
511                                                              14,
512                                                              GTK_PARAM_READABLE));
513   gtk_widget_class_install_style_property (widget_class,
514                                            g_param_spec_int ("trough-border",
515                                                              P_("Trough Border"),
516                                                              P_("Spacing between thumb/steppers and outer trough bevel"),
517                                                              0,
518                                                              G_MAXINT,
519                                                              1,
520                                                              GTK_PARAM_READABLE));
521   gtk_widget_class_install_style_property (widget_class,
522                                            g_param_spec_int ("stepper-size",
523                                                              P_("Stepper Size"),
524                                                              P_("Length of step buttons at ends"),
525                                                              0,
526                                                              G_MAXINT,
527                                                              14,
528                                                              GTK_PARAM_READABLE));
529   /**
530    * GtkRange:stepper-spacing:
531    *
532    * The spacing between the stepper buttons and thumb. Note that
533    * setting this value to anything > 0 will automatically set the
534    * trough-under-steppers style property to %TRUE as well. Also,
535    * stepper-spacing won't have any effect if there are no steppers.
536    */
537   gtk_widget_class_install_style_property (widget_class,
538                                            g_param_spec_int ("stepper-spacing",
539                                                              P_("Stepper Spacing"),
540                                                              P_("Spacing between step buttons and thumb"),
541                                                              0,
542                                                              G_MAXINT,
543                                                              0,
544                                                              GTK_PARAM_READABLE));
545   gtk_widget_class_install_style_property (widget_class,
546                                            g_param_spec_int ("arrow-displacement-x",
547                                                              P_("Arrow X Displacement"),
548                                                              P_("How far in the x direction to move the arrow when the button is depressed"),
549                                                              G_MININT,
550                                                              G_MAXINT,
551                                                              0,
552                                                              GTK_PARAM_READABLE));
553   gtk_widget_class_install_style_property (widget_class,
554                                            g_param_spec_int ("arrow-displacement-y",
555                                                              P_("Arrow Y Displacement"),
556                                                              P_("How far in the y direction to move the arrow when the button is depressed"),
557                                                              G_MININT,
558                                                              G_MAXINT,
559                                                              0,
560                                                              GTK_PARAM_READABLE));
561
562   /**
563    * GtkRange:trough-under-steppers:
564    *
565    * Whether to draw the trough across the full length of the range or
566    * to exclude the steppers and their spacing. Note that setting the
567    * #GtkRange:stepper-spacing style property to any value > 0 will
568    * automatically enable trough-under-steppers too.
569    *
570    * Since: 2.10
571    */
572   gtk_widget_class_install_style_property (widget_class,
573                                            g_param_spec_boolean ("trough-under-steppers",
574                                                                  P_("Trough Under Steppers"),
575                                                                  P_("Whether to draw trough for full length of range or exclude the steppers and spacing"),
576                                                                  TRUE,
577                                                                  GTK_PARAM_READABLE));
578
579   /**
580    * GtkRange:arrow-scaling:
581    *
582    * The arrow size proportion relative to the scroll button size.
583    *
584    * Since: 2.14
585    */
586   gtk_widget_class_install_style_property (widget_class,
587                                            g_param_spec_float ("arrow-scaling",
588                                                                P_("Arrow scaling"),
589                                                                P_("Arrow scaling with regard to scroll button size"),
590                                                                0.0, 1.0, 0.5,
591                                                                GTK_PARAM_READABLE));
592
593   g_type_class_add_private (class, sizeof (GtkRangePrivate));
594 }
595
596 static void
597 gtk_range_set_property (GObject      *object,
598                         guint         prop_id,
599                         const GValue *value,
600                         GParamSpec   *pspec)
601 {
602   GtkRange *range = GTK_RANGE (object);
603   GtkRangePrivate *priv = range->priv;
604
605   switch (prop_id)
606     {
607     case PROP_ORIENTATION:
608       priv->orientation = g_value_get_enum (value);
609
610       priv->slider_detail_quark = 0;
611       priv->stepper_detail_quark[0] = 0;
612       priv->stepper_detail_quark[1] = 0;
613       priv->stepper_detail_quark[2] = 0;
614       priv->stepper_detail_quark[3] = 0;
615
616       gtk_widget_queue_resize (GTK_WIDGET (range));
617       break;
618     case PROP_UPDATE_POLICY:
619       gtk_range_set_update_policy (range, g_value_get_enum (value));
620       break;
621     case PROP_ADJUSTMENT:
622       gtk_range_set_adjustment (range, g_value_get_object (value));
623       break;
624     case PROP_INVERTED:
625       gtk_range_set_inverted (range, g_value_get_boolean (value));
626       break;
627     case PROP_LOWER_STEPPER_SENSITIVITY:
628       gtk_range_set_lower_stepper_sensitivity (range, g_value_get_enum (value));
629       break;
630     case PROP_UPPER_STEPPER_SENSITIVITY:
631       gtk_range_set_upper_stepper_sensitivity (range, g_value_get_enum (value));
632       break;
633     case PROP_SHOW_FILL_LEVEL:
634       gtk_range_set_show_fill_level (range, g_value_get_boolean (value));
635       break;
636     case PROP_RESTRICT_TO_FILL_LEVEL:
637       gtk_range_set_restrict_to_fill_level (range, g_value_get_boolean (value));
638       break;
639     case PROP_FILL_LEVEL:
640       gtk_range_set_fill_level (range, g_value_get_double (value));
641       break;
642     default:
643       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
644       break;
645     }
646 }
647
648 static void
649 gtk_range_get_property (GObject      *object,
650                         guint         prop_id,
651                         GValue       *value,
652                         GParamSpec   *pspec)
653 {
654   GtkRange *range = GTK_RANGE (object);
655   GtkRangePrivate *priv = range->priv;
656
657   switch (prop_id)
658     {
659     case PROP_ORIENTATION:
660       g_value_set_enum (value, priv->orientation);
661       break;
662     case PROP_UPDATE_POLICY:
663       g_value_set_enum (value, priv->update_policy);
664       break;
665     case PROP_ADJUSTMENT:
666       g_value_set_object (value, priv->adjustment);
667       break;
668     case PROP_INVERTED:
669       g_value_set_boolean (value, priv->inverted);
670       break;
671     case PROP_LOWER_STEPPER_SENSITIVITY:
672       g_value_set_enum (value, gtk_range_get_lower_stepper_sensitivity (range));
673       break;
674     case PROP_UPPER_STEPPER_SENSITIVITY:
675       g_value_set_enum (value, gtk_range_get_upper_stepper_sensitivity (range));
676       break;
677     case PROP_SHOW_FILL_LEVEL:
678       g_value_set_boolean (value, gtk_range_get_show_fill_level (range));
679       break;
680     case PROP_RESTRICT_TO_FILL_LEVEL:
681       g_value_set_boolean (value, gtk_range_get_restrict_to_fill_level (range));
682       break;
683     case PROP_FILL_LEVEL:
684       g_value_set_double (value, gtk_range_get_fill_level (range));
685       break;
686     default:
687       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
688       break;
689     }
690 }
691
692 static void
693 gtk_range_init (GtkRange *range)
694 {
695   GtkRangePrivate *priv;
696
697   range->priv = G_TYPE_INSTANCE_GET_PRIVATE (range,
698                                              GTK_TYPE_RANGE,
699                                              GtkRangePrivate);
700   priv = range->priv;
701
702   gtk_widget_set_has_window (GTK_WIDGET (range), FALSE);
703
704   priv->orientation = GTK_ORIENTATION_HORIZONTAL;
705   priv->adjustment = NULL;
706   priv->update_policy = GTK_UPDATE_CONTINUOUS;
707   priv->inverted = FALSE;
708   priv->flippable = FALSE;
709   priv->min_slider_size = 1;
710   priv->has_stepper_a = FALSE;
711   priv->has_stepper_b = FALSE;
712   priv->has_stepper_c = FALSE;
713   priv->has_stepper_d = FALSE;
714   priv->need_recalc = TRUE;
715   priv->round_digits = -1;
716   priv->mouse_location = MOUSE_OUTSIDE;
717   priv->mouse_x = -1;
718   priv->mouse_y = -1;
719   priv->grab_location = MOUSE_OUTSIDE;
720   priv->grab_button = 0;
721   priv->lower_sensitivity = GTK_SENSITIVITY_AUTO;
722   priv->upper_sensitivity = GTK_SENSITIVITY_AUTO;
723   priv->lower_sensitive = TRUE;
724   priv->upper_sensitive = TRUE;
725   priv->show_fill_level = FALSE;
726   priv->restrict_to_fill_level = TRUE;
727   priv->fill_level = G_MAXDOUBLE;
728   priv->timer = NULL;
729 }
730
731 /**
732  * gtk_range_get_adjustment:
733  * @range: a #GtkRange
734  * 
735  * Get the #GtkAdjustment which is the "model" object for #GtkRange.
736  * See gtk_range_set_adjustment() for details.
737  * The return value does not have a reference added, so should not
738  * be unreferenced.
739  * 
740  * Return value: (transfer none): a #GtkAdjustment
741  **/
742 GtkAdjustment*
743 gtk_range_get_adjustment (GtkRange *range)
744 {
745   GtkRangePrivate *priv;
746
747   g_return_val_if_fail (GTK_IS_RANGE (range), NULL);
748
749   priv = range->priv;
750
751   if (!priv->adjustment)
752     gtk_range_set_adjustment (range, NULL);
753
754   return priv->adjustment;
755 }
756
757 /**
758  * gtk_range_set_update_policy:
759  * @range: a #GtkRange
760  * @policy: update policy
761  *
762  * Sets the update policy for the range. #GTK_UPDATE_CONTINUOUS means that
763  * anytime the range slider is moved, the range value will change and the
764  * value_changed signal will be emitted. #GTK_UPDATE_DELAYED means that
765  * the value will be updated after a brief timeout where no slider motion
766  * occurs, so updates are spaced by a short time rather than
767  * continuous. #GTK_UPDATE_DISCONTINUOUS means that the value will only
768  * be updated when the user releases the button and ends the slider
769  * drag operation.
770  **/
771 void
772 gtk_range_set_update_policy (GtkRange      *range,
773                              GtkUpdateType  policy)
774 {
775   GtkRangePrivate *priv;
776
777   g_return_if_fail (GTK_IS_RANGE (range));
778
779   priv = range->priv;
780
781   if (priv->update_policy != policy)
782     {
783       priv->update_policy = policy;
784       g_object_notify (G_OBJECT (range), "update-policy");
785     }
786 }
787
788 /**
789  * gtk_range_get_update_policy:
790  * @range: a #GtkRange
791  *
792  * Gets the update policy of @range. See gtk_range_set_update_policy().
793  *
794  * Return value: the current update policy
795  **/
796 GtkUpdateType
797 gtk_range_get_update_policy (GtkRange *range)
798 {
799   g_return_val_if_fail (GTK_IS_RANGE (range), GTK_UPDATE_CONTINUOUS);
800
801   return range->priv->update_policy;
802 }
803
804 /**
805  * gtk_range_set_adjustment:
806  * @range: a #GtkRange
807  * @adjustment: a #GtkAdjustment
808  *
809  * Sets the adjustment to be used as the "model" object for this range
810  * widget. The adjustment indicates the current range value, the
811  * minimum and maximum range values, the step/page increments used
812  * for keybindings and scrolling, and the page size. The page size
813  * is normally 0 for #GtkScale and nonzero for #GtkScrollbar, and
814  * indicates the size of the visible area of the widget being scrolled.
815  * The page size affects the size of the scrollbar slider.
816  **/
817 void
818 gtk_range_set_adjustment (GtkRange      *range,
819                           GtkAdjustment *adjustment)
820 {
821   GtkRangePrivate *priv;
822
823   g_return_if_fail (GTK_IS_RANGE (range));
824
825   priv = range->priv;
826
827   if (!adjustment)
828     adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
829   else
830     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
831
832   if (priv->adjustment != adjustment)
833     {
834       if (priv->adjustment)
835         {
836           g_signal_handlers_disconnect_by_func (priv->adjustment,
837                                                 gtk_range_adjustment_changed,
838                                                 range);
839           g_signal_handlers_disconnect_by_func (priv->adjustment,
840                                                 gtk_range_adjustment_value_changed,
841                                                 range);
842           g_object_unref (priv->adjustment);
843         }
844
845       priv->adjustment = adjustment;
846       g_object_ref_sink (adjustment);
847       
848       g_signal_connect (adjustment, "changed",
849                         G_CALLBACK (gtk_range_adjustment_changed),
850                         range);
851       g_signal_connect (adjustment, "value-changed",
852                         G_CALLBACK (gtk_range_adjustment_value_changed),
853                         range);
854       
855       gtk_range_adjustment_changed (adjustment, range);
856       g_object_notify (G_OBJECT (range), "adjustment");
857     }
858 }
859
860 /**
861  * gtk_range_set_inverted:
862  * @range: a #GtkRange
863  * @setting: %TRUE to invert the range
864  *
865  * Ranges normally move from lower to higher values as the
866  * slider moves from top to bottom or left to right. Inverted
867  * ranges have higher values at the top or on the right rather than
868  * on the bottom or left.
869  **/
870 void
871 gtk_range_set_inverted (GtkRange *range,
872                         gboolean  setting)
873 {
874   GtkRangePrivate *priv;
875
876   g_return_if_fail (GTK_IS_RANGE (range));
877
878   priv = range->priv;
879
880   setting = setting != FALSE;
881
882   if (setting != priv->inverted)
883     {
884       priv->inverted = setting;
885       g_object_notify (G_OBJECT (range), "inverted");
886       gtk_widget_queue_resize (GTK_WIDGET (range));
887     }
888 }
889
890 /**
891  * gtk_range_get_inverted:
892  * @range: a #GtkRange
893  * 
894  * Gets the value set by gtk_range_set_inverted().
895  * 
896  * Return value: %TRUE if the range is inverted
897  **/
898 gboolean
899 gtk_range_get_inverted (GtkRange *range)
900 {
901   g_return_val_if_fail (GTK_IS_RANGE (range), FALSE);
902
903   return range->priv->inverted;
904 }
905
906 /**
907  * gtk_range_set_flippable:
908  * @range: a #GtkRange
909  * @flippable: %TRUE to make the range flippable
910  *
911  * If a range is flippable, it will switch its direction if it is
912  * horizontal and its direction is %GTK_TEXT_DIR_RTL.
913  *
914  * See gtk_widget_get_direction().
915  *
916  * Since: 2.18
917  **/
918 void
919 gtk_range_set_flippable (GtkRange *range,
920                          gboolean  flippable)
921 {
922   GtkRangePrivate *priv;
923
924   g_return_if_fail (GTK_IS_RANGE (range));
925
926   priv = range->priv;
927
928   flippable = flippable ? TRUE : FALSE;
929
930   if (flippable != priv->flippable)
931     {
932       priv->flippable = flippable;
933
934       gtk_widget_queue_draw (GTK_WIDGET (range));
935     }
936 }
937
938 /**
939  * gtk_range_get_flippable:
940  * @range: a #GtkRange
941  *
942  * Gets the value set by gtk_range_set_flippable().
943  *
944  * Return value: %TRUE if the range is flippable
945  *
946  * Since: 2.18
947  **/
948 gboolean
949 gtk_range_get_flippable (GtkRange *range)
950 {
951   g_return_val_if_fail (GTK_IS_RANGE (range), FALSE);
952
953   return range->priv->flippable;
954 }
955
956 /**
957  * gtk_range_set_slider_size_fixed:
958  * @range: a #GtkRange
959  * @size_fixed: %TRUE to make the slider size constant
960  *
961  * Sets whether the range's slider has a fixed size, or a size that
962  * depends on it's adjustment's page size.
963  *
964  * This function is useful mainly for #GtkRange subclasses.
965  *
966  * Since: 2.20
967  **/
968 void
969 gtk_range_set_slider_size_fixed (GtkRange *range,
970                                  gboolean  size_fixed)
971 {
972   GtkRangePrivate *priv;
973
974   g_return_if_fail (GTK_IS_RANGE (range));
975
976   priv = range->priv;
977
978   if (size_fixed != priv->slider_size_fixed)
979     {
980       priv->slider_size_fixed = size_fixed ? TRUE : FALSE;
981
982       if (priv->adjustment && gtk_widget_get_mapped (GTK_WIDGET (range)))
983         {
984           priv->need_recalc = TRUE;
985           gtk_range_calc_layout (range, gtk_adjustment_get_value (priv->adjustment));
986           gtk_widget_queue_draw (GTK_WIDGET (range));
987         }
988     }
989 }
990
991 /**
992  * gtk_range_get_slider_size_fixed:
993  * @range: a #GtkRange
994  *
995  * This function is useful mainly for #GtkRange subclasses.
996  *
997  * See gtk_range_set_slider_size_fixed().
998  *
999  * Return value: whether the range's slider has a fixed size.
1000  *
1001  * Since: 2.20
1002  **/
1003 gboolean
1004 gtk_range_get_slider_size_fixed (GtkRange *range)
1005 {
1006   g_return_val_if_fail (GTK_IS_RANGE (range), FALSE);
1007
1008   return range->priv->slider_size_fixed;
1009 }
1010
1011 /**
1012  * gtk_range_set_min_slider_size:
1013  * @range: a #GtkRange
1014  * @min_size: The slider's minimum size
1015  *
1016  * Sets the minimum size of the range's slider.
1017  *
1018  * This function is useful mainly for #GtkRange subclasses.
1019  *
1020  * Since: 2.20
1021  **/
1022 void
1023 gtk_range_set_min_slider_size (GtkRange *range,
1024                                gint      min_size)
1025 {
1026   GtkRangePrivate *priv;
1027
1028   g_return_if_fail (GTK_IS_RANGE (range));
1029   g_return_if_fail (min_size > 0);
1030
1031   priv = range->priv;
1032
1033   if (min_size != priv->min_slider_size)
1034     {
1035       priv->min_slider_size = min_size;
1036
1037       priv->need_recalc = TRUE;
1038       gtk_range_calc_layout (range, priv->adjustment->value);
1039       gtk_widget_queue_draw (GTK_WIDGET (range));
1040     }
1041 }
1042
1043 /**
1044  * gtk_range_get_min_slider_size:
1045  * @range: a #GtkRange
1046  *
1047  * This function is useful mainly for #GtkRange subclasses.
1048  *
1049  * See gtk_range_set_min_slider_size().
1050  *
1051  * Return value: The minimum size of the range's slider.
1052  *
1053  * Since: 2.20
1054  **/
1055 gint
1056 gtk_range_get_min_slider_size (GtkRange *range)
1057 {
1058   g_return_val_if_fail (GTK_IS_RANGE (range), FALSE);
1059
1060   return range->priv->min_slider_size;
1061 }
1062
1063 /**
1064  * gtk_range_get_range_rect:
1065  * @range: a #GtkRange
1066  * @range_rect: return location for the range rectangle
1067  *
1068  * This function returns the area that contains the range's trough
1069  * and its steppers, in widget->window coordinates.
1070  *
1071  * This function is useful mainly for #GtkRange subclasses.
1072  *
1073  * Since: 2.20
1074  **/
1075 void
1076 gtk_range_get_range_rect (GtkRange     *range,
1077                           GdkRectangle *range_rect)
1078 {
1079   GtkRangePrivate *priv;
1080
1081   g_return_if_fail (GTK_IS_RANGE (range));
1082   g_return_if_fail (range_rect != NULL);
1083
1084   priv = range->priv;
1085
1086   gtk_range_calc_layout (range, priv->adjustment->value);
1087
1088   *range_rect = priv->range_rect;
1089 }
1090
1091 /**
1092  * gtk_range_get_slider_range:
1093  * @range: a #GtkRange
1094  * @slider_start: (allow-none): return location for the slider's start, or %NULL
1095  * @slider_end: (allow-none): return location for the slider's end, or %NULL
1096  *
1097  * This function returns sliders range along the long dimension,
1098  * in widget->window coordinates.
1099  *
1100  * This function is useful mainly for #GtkRange subclasses.
1101  *
1102  * Since: 2.20
1103  **/
1104 void
1105 gtk_range_get_slider_range (GtkRange *range,
1106                             gint     *slider_start,
1107                             gint     *slider_end)
1108 {
1109   GtkRangePrivate *priv;
1110
1111   g_return_if_fail (GTK_IS_RANGE (range));
1112
1113   priv = range->priv;
1114
1115   gtk_range_calc_layout (range, priv->adjustment->value);
1116
1117   if (slider_start)
1118     *slider_start = priv->slider_start;
1119
1120   if (slider_end)
1121     *slider_end = priv->slider_end;
1122 }
1123
1124 /**
1125  * gtk_range_set_lower_stepper_sensitivity:
1126  * @range:       a #GtkRange
1127  * @sensitivity: the lower stepper's sensitivity policy.
1128  *
1129  * Sets the sensitivity policy for the stepper that points to the
1130  * 'lower' end of the GtkRange's adjustment.
1131  *
1132  * Since: 2.10
1133  **/
1134 void
1135 gtk_range_set_lower_stepper_sensitivity (GtkRange           *range,
1136                                          GtkSensitivityType  sensitivity)
1137 {
1138   GtkRangePrivate *priv;
1139
1140   g_return_if_fail (GTK_IS_RANGE (range));
1141
1142   priv = range->priv;
1143
1144   if (priv->lower_sensitivity != sensitivity)
1145     {
1146       priv->lower_sensitivity = sensitivity;
1147
1148       priv->need_recalc = TRUE;
1149       gtk_range_calc_layout (range, priv->adjustment->value);
1150       gtk_widget_queue_draw (GTK_WIDGET (range));
1151
1152       g_object_notify (G_OBJECT (range), "lower-stepper-sensitivity");
1153     }
1154 }
1155
1156 /**
1157  * gtk_range_get_lower_stepper_sensitivity:
1158  * @range: a #GtkRange
1159  *
1160  * Gets the sensitivity policy for the stepper that points to the
1161  * 'lower' end of the GtkRange's adjustment.
1162  *
1163  * Return value: The lower stepper's sensitivity policy.
1164  *
1165  * Since: 2.10
1166  **/
1167 GtkSensitivityType
1168 gtk_range_get_lower_stepper_sensitivity (GtkRange *range)
1169 {
1170   g_return_val_if_fail (GTK_IS_RANGE (range), GTK_SENSITIVITY_AUTO);
1171
1172   return range->priv->lower_sensitivity;
1173 }
1174
1175 /**
1176  * gtk_range_set_upper_stepper_sensitivity:
1177  * @range:       a #GtkRange
1178  * @sensitivity: the upper stepper's sensitivity policy.
1179  *
1180  * Sets the sensitivity policy for the stepper that points to the
1181  * 'upper' end of the GtkRange's adjustment.
1182  *
1183  * Since: 2.10
1184  **/
1185 void
1186 gtk_range_set_upper_stepper_sensitivity (GtkRange           *range,
1187                                          GtkSensitivityType  sensitivity)
1188 {
1189   GtkRangePrivate *priv;
1190
1191   g_return_if_fail (GTK_IS_RANGE (range));
1192
1193   priv = range->priv;
1194
1195   if (priv->upper_sensitivity != sensitivity)
1196     {
1197       priv->upper_sensitivity = sensitivity;
1198
1199       priv->need_recalc = TRUE;
1200       gtk_range_calc_layout (range, priv->adjustment->value);
1201       gtk_widget_queue_draw (GTK_WIDGET (range));
1202
1203       g_object_notify (G_OBJECT (range), "upper-stepper-sensitivity");
1204     }
1205 }
1206
1207 /**
1208  * gtk_range_get_upper_stepper_sensitivity:
1209  * @range: a #GtkRange
1210  *
1211  * Gets the sensitivity policy for the stepper that points to the
1212  * 'upper' end of the GtkRange's adjustment.
1213  *
1214  * Return value: The upper stepper's sensitivity policy.
1215  *
1216  * Since: 2.10
1217  **/
1218 GtkSensitivityType
1219 gtk_range_get_upper_stepper_sensitivity (GtkRange *range)
1220 {
1221   g_return_val_if_fail (GTK_IS_RANGE (range), GTK_SENSITIVITY_AUTO);
1222
1223   return range->priv->upper_sensitivity;
1224 }
1225
1226 /**
1227  * gtk_range_set_increments:
1228  * @range: a #GtkRange
1229  * @step: step size
1230  * @page: page size
1231  *
1232  * Sets the step and page sizes for the range.
1233  * The step size is used when the user clicks the #GtkScrollbar
1234  * arrows or moves #GtkScale via arrow keys. The page size
1235  * is used for example when moving via Page Up or Page Down keys.
1236  **/
1237 void
1238 gtk_range_set_increments (GtkRange *range,
1239                           gdouble   step,
1240                           gdouble   page)
1241 {
1242   GtkRangePrivate *priv;
1243
1244   g_return_if_fail (GTK_IS_RANGE (range));
1245
1246   priv = range->priv;
1247
1248   priv->adjustment->step_increment = step;
1249   priv->adjustment->page_increment = page;
1250
1251   gtk_adjustment_changed (priv->adjustment);
1252 }
1253
1254 /**
1255  * gtk_range_set_range:
1256  * @range: a #GtkRange
1257  * @min: minimum range value
1258  * @max: maximum range value
1259  * 
1260  * Sets the allowable values in the #GtkRange, and clamps the range
1261  * value to be between @min and @max. (If the range has a non-zero
1262  * page size, it is clamped between @min and @max - page-size.)
1263  **/
1264 void
1265 gtk_range_set_range (GtkRange *range,
1266                      gdouble   min,
1267                      gdouble   max)
1268 {
1269   GtkRangePrivate *priv;
1270   gdouble value;
1271   
1272   g_return_if_fail (GTK_IS_RANGE (range));
1273   g_return_if_fail (min < max);
1274
1275   priv = range->priv;
1276
1277   priv->adjustment->lower = min;
1278   priv->adjustment->upper = max;
1279
1280   value = priv->adjustment->value;
1281
1282   if (priv->restrict_to_fill_level)
1283     value = MIN (value, MAX (priv->adjustment->lower,
1284                              priv->fill_level));
1285
1286   gtk_adjustment_set_value (priv->adjustment, value);
1287   gtk_adjustment_changed (priv->adjustment);
1288 }
1289
1290 /**
1291  * gtk_range_set_value:
1292  * @range: a #GtkRange
1293  * @value: new value of the range
1294  *
1295  * Sets the current value of the range; if the value is outside the
1296  * minimum or maximum range values, it will be clamped to fit inside
1297  * them. The range emits the #GtkRange::value-changed signal if the 
1298  * value changes.
1299  **/
1300 void
1301 gtk_range_set_value (GtkRange *range,
1302                      gdouble   value)
1303 {
1304   GtkRangePrivate *priv;
1305
1306   g_return_if_fail (GTK_IS_RANGE (range));
1307
1308   priv = range->priv;
1309
1310   if (priv->restrict_to_fill_level)
1311     value = MIN (value, MAX (priv->adjustment->lower,
1312                              priv->fill_level));
1313
1314   gtk_adjustment_set_value (priv->adjustment, value);
1315 }
1316
1317 /**
1318  * gtk_range_get_value:
1319  * @range: a #GtkRange
1320  * 
1321  * Gets the current value of the range.
1322  * 
1323  * Return value: current value of the range.
1324  **/
1325 gdouble
1326 gtk_range_get_value (GtkRange *range)
1327 {
1328   g_return_val_if_fail (GTK_IS_RANGE (range), 0.0);
1329
1330   return range->priv->adjustment->value;
1331 }
1332
1333 /**
1334  * gtk_range_set_show_fill_level:
1335  * @range:           A #GtkRange
1336  * @show_fill_level: Whether a fill level indicator graphics is shown.
1337  *
1338  * Sets whether a graphical fill level is show on the trough. See
1339  * gtk_range_set_fill_level() for a general description of the fill
1340  * level concept.
1341  *
1342  * Since: 2.12
1343  **/
1344 void
1345 gtk_range_set_show_fill_level (GtkRange *range,
1346                                gboolean  show_fill_level)
1347 {
1348   GtkRangePrivate *priv;
1349
1350   g_return_if_fail (GTK_IS_RANGE (range));
1351
1352   priv = range->priv;
1353
1354   show_fill_level = show_fill_level ? TRUE : FALSE;
1355
1356   if (show_fill_level != priv->show_fill_level)
1357     {
1358       priv->show_fill_level = show_fill_level;
1359       g_object_notify (G_OBJECT (range), "show-fill-level");
1360       gtk_widget_queue_draw (GTK_WIDGET (range));
1361     }
1362 }
1363
1364 /**
1365  * gtk_range_get_show_fill_level:
1366  * @range: A #GtkRange
1367  *
1368  * Gets whether the range displays the fill level graphically.
1369  *
1370  * Return value: %TRUE if @range shows the fill level.
1371  *
1372  * Since: 2.12
1373  **/
1374 gboolean
1375 gtk_range_get_show_fill_level (GtkRange *range)
1376 {
1377   g_return_val_if_fail (GTK_IS_RANGE (range), FALSE);
1378
1379   return range->priv->show_fill_level;
1380 }
1381
1382 /**
1383  * gtk_range_set_restrict_to_fill_level:
1384  * @range:                  A #GtkRange
1385  * @restrict_to_fill_level: Whether the fill level restricts slider movement.
1386  *
1387  * Sets whether the slider is restricted to the fill level. See
1388  * gtk_range_set_fill_level() for a general description of the fill
1389  * level concept.
1390  *
1391  * Since: 2.12
1392  **/
1393 void
1394 gtk_range_set_restrict_to_fill_level (GtkRange *range,
1395                                       gboolean  restrict_to_fill_level)
1396 {
1397   GtkRangePrivate *priv;
1398
1399   g_return_if_fail (GTK_IS_RANGE (range));
1400
1401   priv = range->priv;
1402
1403   restrict_to_fill_level = restrict_to_fill_level ? TRUE : FALSE;
1404
1405   if (restrict_to_fill_level != priv->restrict_to_fill_level)
1406     {
1407       priv->restrict_to_fill_level = restrict_to_fill_level;
1408       g_object_notify (G_OBJECT (range), "restrict-to-fill-level");
1409
1410       gtk_range_set_value (range, gtk_range_get_value (range));
1411     }
1412 }
1413
1414 /**
1415  * gtk_range_get_restrict_to_fill_level:
1416  * @range: A #GtkRange
1417  *
1418  * Gets whether the range is restricted to the fill level.
1419  *
1420  * Return value: %TRUE if @range is restricted to the fill level.
1421  *
1422  * Since: 2.12
1423  **/
1424 gboolean
1425 gtk_range_get_restrict_to_fill_level (GtkRange *range)
1426 {
1427   g_return_val_if_fail (GTK_IS_RANGE (range), FALSE);
1428
1429   return range->priv->restrict_to_fill_level;
1430 }
1431
1432 /**
1433  * gtk_range_set_fill_level:
1434  * @range: a #GtkRange
1435  * @fill_level: the new position of the fill level indicator
1436  *
1437  * Set the new position of the fill level indicator.
1438  *
1439  * The "fill level" is probably best described by its most prominent
1440  * use case, which is an indicator for the amount of pre-buffering in
1441  * a streaming media player. In that use case, the value of the range
1442  * would indicate the current play position, and the fill level would
1443  * be the position up to which the file/stream has been downloaded.
1444  *
1445  * This amount of prebuffering can be displayed on the range's trough
1446  * and is themeable separately from the trough. To enable fill level
1447  * display, use gtk_range_set_show_fill_level(). The range defaults
1448  * to not showing the fill level.
1449  *
1450  * Additionally, it's possible to restrict the range's slider position
1451  * to values which are smaller than the fill level. This is controller
1452  * by gtk_range_set_restrict_to_fill_level() and is by default
1453  * enabled.
1454  *
1455  * Since: 2.12
1456  **/
1457 void
1458 gtk_range_set_fill_level (GtkRange *range,
1459                           gdouble   fill_level)
1460 {
1461   GtkRangePrivate *priv;
1462
1463   g_return_if_fail (GTK_IS_RANGE (range));
1464
1465   priv = range->priv;
1466
1467   if (fill_level != priv->fill_level)
1468     {
1469       priv->fill_level = fill_level;
1470       g_object_notify (G_OBJECT (range), "fill-level");
1471
1472       if (priv->show_fill_level)
1473         gtk_widget_queue_draw (GTK_WIDGET (range));
1474
1475       if (priv->restrict_to_fill_level)
1476         gtk_range_set_value (range, gtk_range_get_value (range));
1477     }
1478 }
1479
1480 /**
1481  * gtk_range_get_fill_level:
1482  * @range: A #GtkRange
1483  *
1484  * Gets the current position of the fill level indicator.
1485  *
1486  * Return value: The current fill level
1487  *
1488  * Since: 2.12
1489  **/
1490 gdouble
1491 gtk_range_get_fill_level (GtkRange *range)
1492 {
1493   g_return_val_if_fail (GTK_IS_RANGE (range), 0.0);
1494
1495   return range->priv->fill_level;
1496 }
1497
1498 static gboolean
1499 should_invert (GtkRange *range)
1500 {
1501   GtkRangePrivate *priv = range->priv;
1502
1503   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1504     return
1505       (priv->inverted && !priv->flippable) ||
1506       (priv->inverted && priv->flippable && gtk_widget_get_direction (GTK_WIDGET (range)) == GTK_TEXT_DIR_LTR) ||
1507       (!priv->inverted && priv->flippable && gtk_widget_get_direction (GTK_WIDGET (range)) == GTK_TEXT_DIR_RTL);
1508   else
1509     return priv->inverted;
1510 }
1511
1512 static void
1513 gtk_range_destroy (GtkObject *object)
1514 {
1515   GtkRange *range = GTK_RANGE (object);
1516   GtkRangePrivate *priv = range->priv;
1517
1518   gtk_range_remove_step_timer (range);
1519   gtk_range_remove_update_timer (range);
1520
1521   if (priv->repaint_id)
1522     g_source_remove (priv->repaint_id);
1523   priv->repaint_id = 0;
1524
1525   if (priv->adjustment)
1526     {
1527       g_signal_handlers_disconnect_by_func (priv->adjustment,
1528                                             gtk_range_adjustment_changed,
1529                                             range);
1530       g_signal_handlers_disconnect_by_func (priv->adjustment,
1531                                             gtk_range_adjustment_value_changed,
1532                                             range);
1533       g_object_unref (priv->adjustment);
1534       priv->adjustment = NULL;
1535     }
1536
1537   if (priv->n_marks)
1538     {
1539       g_free (priv->marks);
1540       priv->marks = NULL;
1541       g_free (priv->mark_pos);
1542       priv->mark_pos = NULL;
1543       priv->n_marks = 0;
1544     }
1545
1546   GTK_OBJECT_CLASS (gtk_range_parent_class)->destroy (object);
1547 }
1548
1549 static void
1550 gtk_range_size_request (GtkWidget      *widget,
1551                         GtkRequisition *requisition)
1552 {
1553   GtkRange *range;
1554   gint slider_width, stepper_size, focus_width, trough_border, stepper_spacing;
1555   GdkRectangle range_rect;
1556   GtkBorder border;
1557   
1558   range = GTK_RANGE (widget);
1559   
1560   gtk_range_get_props (range,
1561                        &slider_width, &stepper_size,
1562                        &focus_width, &trough_border,
1563                        &stepper_spacing, NULL,
1564                        NULL, NULL);
1565
1566   gtk_range_calc_request (range, 
1567                           slider_width, stepper_size,
1568                           focus_width, trough_border, stepper_spacing,
1569                           &range_rect, &border, NULL, NULL, NULL, NULL);
1570
1571   requisition->width = range_rect.width + border.left + border.right;
1572   requisition->height = range_rect.height + border.top + border.bottom;
1573 }
1574
1575 static void
1576 gtk_range_size_allocate (GtkWidget     *widget,
1577                          GtkAllocation *allocation)
1578 {
1579   GtkRange *range = GTK_RANGE (widget);
1580   GtkRangePrivate *priv = range->priv;
1581
1582   gtk_widget_set_allocation (widget, allocation);
1583
1584   priv->recalc_marks = TRUE;
1585
1586   priv->need_recalc = TRUE;
1587   gtk_range_calc_layout (range, priv->adjustment->value);
1588
1589   if (gtk_widget_get_realized (widget))
1590     gdk_window_move_resize (priv->event_window,
1591                             allocation->x, allocation->y,
1592                             allocation->width, allocation->height);
1593 }
1594
1595 static void
1596 gtk_range_realize (GtkWidget *widget)
1597 {
1598   GtkAllocation allocation;
1599   GtkRange *range = GTK_RANGE (widget);
1600   GtkRangePrivate *priv = range->priv;
1601   GdkWindow *window;
1602   GdkWindowAttr attributes;
1603   gint attributes_mask;  
1604
1605   gtk_range_calc_layout (range, priv->adjustment->value);
1606   
1607   gtk_widget_set_realized (widget, TRUE);
1608
1609   window = gtk_widget_get_parent_window (widget);
1610   gtk_widget_set_window (widget, window);
1611   g_object_ref (window);
1612
1613   gtk_widget_get_allocation (widget, &allocation);
1614
1615   attributes.window_type = GDK_WINDOW_CHILD;
1616   attributes.x = allocation.x;
1617   attributes.y = allocation.y;
1618   attributes.width = allocation.width;
1619   attributes.height = allocation.height;
1620   attributes.wclass = GDK_INPUT_ONLY;
1621   attributes.event_mask = gtk_widget_get_events (widget);
1622   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1623                             GDK_BUTTON_RELEASE_MASK |
1624                             GDK_ENTER_NOTIFY_MASK |
1625                             GDK_LEAVE_NOTIFY_MASK |
1626                             GDK_POINTER_MOTION_MASK |
1627                             GDK_POINTER_MOTION_HINT_MASK);
1628
1629   attributes_mask = GDK_WA_X | GDK_WA_Y;
1630
1631   priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
1632                                         &attributes, attributes_mask);
1633   gdk_window_set_user_data (priv->event_window, range);
1634
1635   gtk_widget_style_attach (widget);
1636 }
1637
1638 static void
1639 gtk_range_unrealize (GtkWidget *widget)
1640 {
1641   GtkRange *range = GTK_RANGE (widget);
1642   GtkRangePrivate *priv = range->priv;
1643
1644   gtk_range_remove_step_timer (range);
1645   gtk_range_remove_update_timer (range);
1646
1647   gdk_window_set_user_data (priv->event_window, NULL);
1648   gdk_window_destroy (priv->event_window);
1649   priv->event_window = NULL;
1650
1651   GTK_WIDGET_CLASS (gtk_range_parent_class)->unrealize (widget);
1652 }
1653
1654 static void
1655 gtk_range_map (GtkWidget *widget)
1656 {
1657   GtkRange *range = GTK_RANGE (widget);
1658   GtkRangePrivate *priv = range->priv;
1659
1660   gdk_window_show (priv->event_window);
1661
1662   GTK_WIDGET_CLASS (gtk_range_parent_class)->map (widget);
1663 }
1664
1665 static void
1666 gtk_range_unmap (GtkWidget *widget)
1667 {
1668   GtkRange *range = GTK_RANGE (widget);
1669   GtkRangePrivate *priv = range->priv;
1670
1671   stop_scrolling (range);
1672
1673   gdk_window_hide (priv->event_window);
1674
1675   GTK_WIDGET_CLASS (gtk_range_parent_class)->unmap (widget);
1676 }
1677
1678 static const gchar *
1679 gtk_range_get_slider_detail (GtkRange *range)
1680 {
1681   GtkRangePrivate *priv = range->priv;
1682   const gchar *slider_detail;
1683
1684   if (priv->slider_detail_quark)
1685     return g_quark_to_string (priv->slider_detail_quark);
1686
1687   slider_detail = GTK_RANGE_GET_CLASS (range)->slider_detail;
1688
1689   if (slider_detail && slider_detail[0] == 'X')
1690     {
1691       gchar *detail = g_strdup (slider_detail);
1692
1693       detail[0] = priv->orientation == GTK_ORIENTATION_HORIZONTAL ? 'h' : 'v';
1694
1695       priv->slider_detail_quark = g_quark_from_string (detail);
1696
1697       g_free (detail);
1698
1699       return g_quark_to_string (priv->slider_detail_quark);
1700     }
1701
1702   return slider_detail;
1703 }
1704
1705 static const gchar *
1706 gtk_range_get_stepper_detail (GtkRange *range,
1707                               Stepper   stepper)
1708 {
1709   GtkRangePrivate *priv = range->priv;
1710   const gchar *stepper_detail;
1711   gchar *detail;
1712   const gchar *position = NULL;
1713
1714   if (priv->stepper_detail_quark[stepper])
1715     return g_quark_to_string (priv->stepper_detail_quark[stepper]);
1716
1717   stepper_detail = GTK_RANGE_GET_CLASS (range)->stepper_detail;
1718
1719   switch (stepper)
1720     {
1721     case STEPPER_A:
1722       position = "_start";
1723       break;
1724     case STEPPER_B:
1725       if (priv->has_stepper_a)
1726         position = "_start_inner";
1727       else
1728         position = "_start";
1729       break;
1730     case STEPPER_C:
1731       if (priv->has_stepper_d)
1732         position = "_end_inner";
1733       else
1734         position = "_end";
1735       break;
1736     case STEPPER_D:
1737       position = "_end";
1738       break;
1739     default:
1740       g_assert_not_reached ();
1741     }
1742
1743   detail = g_strconcat (stepper_detail, position, NULL);
1744
1745   if (detail[0] == 'X')
1746     detail[0] = priv->orientation == GTK_ORIENTATION_HORIZONTAL ? 'h' : 'v';
1747
1748   priv->stepper_detail_quark[stepper] = g_quark_from_string (detail);
1749
1750   g_free (detail);
1751
1752   return g_quark_to_string (priv->stepper_detail_quark[stepper]);
1753 }
1754
1755 static void
1756 draw_stepper (GtkRange     *range,
1757               Stepper       stepper,
1758               cairo_t      *cr,
1759               GtkArrowType  arrow_type,
1760               gboolean      clicked,
1761               gboolean      prelighted)
1762 {
1763   GtkRangePrivate *priv = range->priv;
1764   GtkAllocation allocation;
1765   GtkStateType state_type;
1766   GtkShadowType shadow_type;
1767   GtkStyle *style;
1768   GtkWidget *widget = GTK_WIDGET (range);
1769   GdkWindow *window;
1770   gfloat arrow_scaling;
1771   GdkRectangle *rect;
1772   gint arrow_x;
1773   gint arrow_y;
1774   gint arrow_width;
1775   gint arrow_height;
1776
1777   switch (stepper)
1778     {
1779     case STEPPER_A:
1780       rect = &priv->stepper_a;
1781       break;
1782     case STEPPER_B:
1783       rect = &priv->stepper_b;
1784       break;
1785     case STEPPER_C:
1786       rect = &priv->stepper_c;
1787       break;
1788     case STEPPER_D:
1789       rect = &priv->stepper_d;
1790       break;
1791     default:
1792       g_assert_not_reached ();
1793     };
1794
1795   gboolean arrow_sensitive = TRUE;
1796
1797   gtk_widget_get_allocation (widget, &allocation);
1798
1799   if ((!priv->inverted && (arrow_type == GTK_ARROW_DOWN ||
1800                             arrow_type == GTK_ARROW_RIGHT)) ||
1801       (priv->inverted  && (arrow_type == GTK_ARROW_UP ||
1802                             arrow_type == GTK_ARROW_LEFT)))
1803     {
1804       arrow_sensitive = priv->upper_sensitive;
1805     }
1806   else
1807     {
1808       arrow_sensitive = priv->lower_sensitive;
1809     }
1810
1811   if (!gtk_widget_is_sensitive (GTK_WIDGET (range)) || !arrow_sensitive)
1812     state_type = GTK_STATE_INSENSITIVE;
1813   else if (clicked)
1814     state_type = GTK_STATE_ACTIVE;
1815   else if (prelighted)
1816     state_type = GTK_STATE_PRELIGHT;
1817   else 
1818     state_type = GTK_STATE_NORMAL;
1819
1820   if (clicked && arrow_sensitive)
1821     shadow_type = GTK_SHADOW_IN;
1822   else
1823     shadow_type = GTK_SHADOW_OUT;
1824
1825   style = gtk_widget_get_style (widget);
1826   window = gtk_widget_get_window (widget);
1827
1828   gtk_cairo_paint_box (style, cr,
1829                  state_type, shadow_type,
1830                  widget,
1831                  gtk_range_get_stepper_detail (range, stepper),
1832                  rect->x,
1833                  rect->y,
1834                  rect->width,
1835                  rect->height);
1836
1837   gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
1838
1839   arrow_width = rect->width * arrow_scaling;
1840   arrow_height = rect->height * arrow_scaling;
1841   arrow_x = rect->x + (rect->width - arrow_width) / 2;
1842   arrow_y = rect->y + (rect->height - arrow_height) / 2;
1843
1844   if (clicked && arrow_sensitive)
1845     {
1846       gint arrow_displacement_x;
1847       gint arrow_displacement_y;
1848
1849       gtk_range_get_props (GTK_RANGE (widget),
1850                            NULL, NULL, NULL, NULL, NULL, NULL,
1851                            &arrow_displacement_x, &arrow_displacement_y);
1852       
1853       arrow_x += arrow_displacement_x;
1854       arrow_y += arrow_displacement_y;
1855     }
1856
1857   gtk_cairo_paint_arrow (style, cr,
1858                    state_type, shadow_type,
1859                    widget,
1860                    gtk_range_get_stepper_detail (range, stepper),
1861                    arrow_type,
1862                    TRUE,
1863                    arrow_x, arrow_y, arrow_width, arrow_height);
1864 }
1865
1866 static gboolean
1867 gtk_range_expose (GtkWidget      *widget,
1868                   GdkEventExpose *event)
1869 {
1870   GtkAllocation allocation;
1871   GtkRange *range = GTK_RANGE (widget);
1872   GtkRangePrivate *priv = range->priv;
1873   gboolean sensitive;
1874   GtkStateType state;
1875   GtkShadowType shadow_type;
1876   GtkStyle *style;
1877   GdkWindow *window;
1878   gint focus_line_width = 0;
1879   gint focus_padding = 0;
1880   gboolean touchscreen;
1881   cairo_t *cr;
1882
1883   cr = gdk_cairo_create (event->window);
1884   gdk_cairo_region (cr, event->region);
1885   cairo_clip (cr);
1886   cairo_translate (cr, widget->allocation.x, widget->allocation.y);
1887
1888   g_object_get (gtk_widget_get_settings (widget),
1889                 "gtk-touchscreen-mode", &touchscreen,
1890                 NULL);
1891
1892   style = gtk_widget_get_style (widget);
1893   if (gtk_widget_get_can_focus (GTK_WIDGET (range)))
1894     gtk_widget_style_get (GTK_WIDGET (range),
1895                           "focus-line-width", &focus_line_width,
1896                           "focus-padding", &focus_padding,
1897                           NULL);
1898
1899   window = gtk_widget_get_window (widget);
1900
1901   /* we're now exposing, so there's no need to force early repaints */
1902   if (priv->repaint_id)
1903     g_source_remove (priv->repaint_id);
1904   priv->repaint_id = 0;
1905
1906   gtk_widget_get_allocation (widget, &allocation);
1907
1908   gtk_range_calc_marks (range);
1909   gtk_range_calc_layout (range, priv->adjustment->value);
1910
1911   sensitive = gtk_widget_is_sensitive (widget);
1912
1913   /* Just to be confusing, we draw the trough for the whole
1914    * range rectangle, not the trough rectangle (the trough
1915    * rectangle is just for hit detection)
1916    */
1917   cairo_save (cr);
1918   gdk_cairo_rectangle (cr, &priv->range_rect);
1919   cairo_clip (cr);
1920
1921     {
1922       gint     x      = (priv->range_rect.x +
1923                          focus_line_width + focus_padding);
1924       gint     y      = (priv->range_rect.y +
1925                          focus_line_width + focus_padding);
1926       gint     width  = (priv->range_rect.width -
1927                          2 * (focus_line_width + focus_padding));
1928       gint     height = (priv->range_rect.height -
1929                          2 * (focus_line_width + focus_padding));
1930       gboolean trough_under_steppers;
1931       gint     stepper_size;
1932       gint     stepper_spacing;
1933
1934       gtk_widget_style_get (GTK_WIDGET (range),
1935                             "trough-under-steppers", &trough_under_steppers,
1936                             "stepper-size",          &stepper_size,
1937                             "stepper-spacing",       &stepper_spacing,
1938                             NULL);
1939
1940       if (stepper_spacing > 0)
1941         trough_under_steppers = FALSE;
1942
1943       if (!trough_under_steppers)
1944         {
1945           gint offset  = 0;
1946           gint shorter = 0;
1947
1948           if (priv->has_stepper_a)
1949             offset += stepper_size;
1950
1951           if (priv->has_stepper_b)
1952             offset += stepper_size;
1953
1954           shorter += offset;
1955
1956           if (priv->has_stepper_c)
1957             shorter += stepper_size;
1958
1959           if (priv->has_stepper_d)
1960             shorter += stepper_size;
1961
1962           if (priv->has_stepper_a || priv->has_stepper_b)
1963             {
1964               offset  += stepper_spacing;
1965               shorter += stepper_spacing;
1966             }
1967
1968           if (priv->has_stepper_c || priv->has_stepper_d)
1969             {
1970               shorter += stepper_spacing;
1971             }
1972
1973           if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1974             {
1975               x     += offset;
1976               width -= shorter;
1977             }
1978           else
1979             {
1980               y      += offset;
1981               height -= shorter;
1982             }
1983         }
1984
1985         {
1986           gint trough_change_pos_x = width;
1987           gint trough_change_pos_y = height;
1988
1989           if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1990             trough_change_pos_x = (priv->slider.x +
1991                                    priv->slider.width / 2 -
1992                                    x);
1993           else
1994             trough_change_pos_y = (priv->slider.y +
1995                                    priv->slider.height / 2 -
1996                                    y);
1997
1998           gtk_cairo_paint_box (style, cr,
1999                          sensitive ? GTK_STATE_ACTIVE : GTK_STATE_INSENSITIVE,
2000                          GTK_SHADOW_IN,
2001                          GTK_WIDGET (range),
2002                          should_invert (range) ? "trough-upper" : "trough-lower",
2003                          x, y,
2004                          trough_change_pos_x, trough_change_pos_y);
2005
2006           if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
2007             trough_change_pos_y = 0;
2008           else
2009             trough_change_pos_x = 0;
2010
2011           gtk_cairo_paint_box (style, cr,
2012                          sensitive ? GTK_STATE_ACTIVE : GTK_STATE_INSENSITIVE,
2013                          GTK_SHADOW_IN,
2014                          GTK_WIDGET (range),
2015                          should_invert (range) ? "trough-lower" : "trough-upper",
2016                          x + trough_change_pos_x, y + trough_change_pos_y,
2017                          width - trough_change_pos_x,
2018                          height - trough_change_pos_y);
2019         }
2020
2021       if (priv->show_fill_level &&
2022           priv->adjustment->upper - priv->adjustment->page_size -
2023           priv->adjustment->lower != 0)
2024         {
2025           gdouble  fill_level  = priv->fill_level;
2026           gint     fill_x      = x;
2027           gint     fill_y      = y;
2028           gint     fill_width  = width;
2029           gint     fill_height = height;
2030           gchar   *fill_detail;
2031
2032           fill_level = CLAMP (fill_level, priv->adjustment->lower,
2033                               priv->adjustment->upper -
2034                               priv->adjustment->page_size);
2035
2036           if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
2037             {
2038               fill_x     = priv->trough.x;
2039               fill_width = (priv->slider.width +
2040                             (fill_level - priv->adjustment->lower) /
2041                             (priv->adjustment->upper -
2042                              priv->adjustment->lower -
2043                              priv->adjustment->page_size) *
2044                             (priv->trough.width -
2045                              priv->slider.width));
2046
2047               if (should_invert (range))
2048                 fill_x += priv->trough.width - fill_width;
2049             }
2050           else
2051             {
2052               fill_y      = priv->trough.y;
2053               fill_height = (priv->slider.height +
2054                              (fill_level - priv->adjustment->lower) /
2055                              (priv->adjustment->upper -
2056                               priv->adjustment->lower -
2057                               priv->adjustment->page_size) *
2058                              (priv->trough.height -
2059                               priv->slider.height));
2060
2061               if (should_invert (range))
2062                 fill_y += priv->trough.height - fill_height;
2063             }
2064
2065           if (fill_level < priv->adjustment->upper - priv->adjustment->page_size)
2066             fill_detail = "trough-fill-level-full";
2067           else
2068             fill_detail = "trough-fill-level";
2069
2070           gtk_cairo_paint_box (style, cr,
2071                          sensitive ? GTK_STATE_ACTIVE : GTK_STATE_INSENSITIVE,
2072                          GTK_SHADOW_OUT,
2073                          GTK_WIDGET (range), fill_detail,
2074                          fill_x, fill_y,
2075                          fill_width, fill_height);
2076         }
2077
2078       if (sensitive && gtk_widget_has_focus (widget))
2079         gtk_cairo_paint_focus (style, cr,
2080                          gtk_widget_get_state (widget),
2081                          widget, "trough",
2082                          priv->range_rect.x,
2083                          priv->range_rect.y,
2084                          priv->range_rect.width,
2085                          priv->range_rect.height);
2086     }
2087
2088   cairo_restore (cr);
2089
2090   shadow_type = GTK_SHADOW_OUT;
2091
2092   if (!sensitive)
2093     state = GTK_STATE_INSENSITIVE;
2094   else if (!touchscreen && priv->mouse_location == MOUSE_SLIDER)
2095     state = GTK_STATE_PRELIGHT;
2096   else
2097     state = GTK_STATE_NORMAL;
2098
2099   if (priv->grab_location == MOUSE_SLIDER)
2100     {
2101       state = GTK_STATE_ACTIVE;
2102       shadow_type = GTK_SHADOW_IN;
2103     }
2104
2105   cairo_save (cr);
2106   gdk_cairo_rectangle (cr, &priv->slider);
2107   cairo_clip (cr);
2108
2109     {
2110       gtk_cairo_paint_slider (style,
2111                         cr,
2112                         state,
2113                         shadow_type,
2114                         widget,
2115                         gtk_range_get_slider_detail (range),
2116                         priv->slider.x,
2117                         priv->slider.y,
2118                         priv->slider.width,
2119                         priv->slider.height,
2120                         priv->orientation);
2121     }
2122
2123   cairo_restore (cr);
2124
2125   if (priv->has_stepper_a)
2126     draw_stepper (range, STEPPER_A, cr,
2127                   priv->orientation == GTK_ORIENTATION_VERTICAL ? GTK_ARROW_UP : GTK_ARROW_LEFT,
2128                   priv->grab_location == MOUSE_STEPPER_A,
2129                   !touchscreen && priv->mouse_location == MOUSE_STEPPER_A);
2130
2131   if (priv->has_stepper_b)
2132     draw_stepper (range, STEPPER_B, cr,
2133                   priv->orientation == GTK_ORIENTATION_VERTICAL ? GTK_ARROW_DOWN : GTK_ARROW_RIGHT,
2134                   priv->grab_location == MOUSE_STEPPER_B,
2135                   !touchscreen && priv->mouse_location == MOUSE_STEPPER_B);
2136
2137   if (priv->has_stepper_c)
2138     draw_stepper (range, STEPPER_C, cr,
2139                   priv->orientation == GTK_ORIENTATION_VERTICAL ? GTK_ARROW_UP : GTK_ARROW_LEFT,
2140                   priv->grab_location == MOUSE_STEPPER_C,
2141                   !touchscreen && priv->mouse_location == MOUSE_STEPPER_C);
2142
2143   if (priv->has_stepper_d)
2144     draw_stepper (range, STEPPER_D, cr,
2145                   priv->orientation == GTK_ORIENTATION_VERTICAL ? GTK_ARROW_DOWN : GTK_ARROW_RIGHT,
2146                   priv->grab_location == MOUSE_STEPPER_D,
2147                   !touchscreen && priv->mouse_location == MOUSE_STEPPER_D);
2148   
2149   cairo_destroy (cr);
2150
2151   return FALSE;
2152 }
2153
2154 static void
2155 range_grab_add (GtkRange      *range,
2156                 GdkDevice     *device,
2157                 MouseLocation  location,
2158                 gint           button)
2159 {
2160   GtkRangePrivate *priv = range->priv;
2161
2162   if (device == priv->grab_device)
2163     return;
2164
2165   if (priv->grab_device != NULL)
2166     {
2167       g_warning ("GtkRange already had a grab device, releasing device grab");
2168       gtk_device_grab_remove (GTK_WIDGET (range), priv->grab_device);
2169     }
2170
2171   /* we don't actually gdk_grab, since a button is down */
2172   gtk_device_grab_add (GTK_WIDGET (range), device, TRUE);
2173
2174   priv->grab_location = location;
2175   priv->grab_button = button;
2176   priv->grab_device = device;
2177
2178   if (gtk_range_update_mouse_location (range))
2179     gtk_widget_queue_draw (GTK_WIDGET (range));
2180 }
2181
2182 static void
2183 range_grab_remove (GtkRange *range)
2184 {
2185   GtkRangePrivate *priv = range->priv;
2186   MouseLocation location;
2187
2188   if (priv->grab_device)
2189     {
2190       gtk_device_grab_remove (GTK_WIDGET (range),
2191                               priv->grab_device);
2192       priv->grab_device = NULL;
2193     }
2194
2195   location = priv->grab_location;
2196   priv->grab_location = MOUSE_OUTSIDE;
2197   priv->grab_button = 0;
2198
2199   if (gtk_range_update_mouse_location (range) ||
2200       location != MOUSE_OUTSIDE)
2201     gtk_widget_queue_draw (GTK_WIDGET (range));
2202 }
2203
2204 static GtkScrollType
2205 range_get_scroll_for_grab (GtkRange      *range)
2206 {
2207   GtkRangePrivate *priv = range->priv;
2208   gboolean invert;
2209
2210   invert = should_invert (range);
2211   switch (priv->grab_location)
2212     {
2213       /* Backward stepper */
2214     case MOUSE_STEPPER_A:
2215     case MOUSE_STEPPER_C:
2216       switch (priv->grab_button)
2217         {
2218         case 1:
2219           return invert ? GTK_SCROLL_STEP_FORWARD : GTK_SCROLL_STEP_BACKWARD;
2220           break;
2221         case 2:
2222           return invert ? GTK_SCROLL_PAGE_FORWARD : GTK_SCROLL_PAGE_BACKWARD;
2223           break;
2224         case 3:
2225           return invert ? GTK_SCROLL_END : GTK_SCROLL_START;
2226           break;
2227         }
2228       break;
2229
2230       /* Forward stepper */
2231     case MOUSE_STEPPER_B:
2232     case MOUSE_STEPPER_D:
2233       switch (priv->grab_button)
2234         {
2235         case 1:
2236           return invert ? GTK_SCROLL_STEP_BACKWARD : GTK_SCROLL_STEP_FORWARD;
2237           break;
2238         case 2:
2239           return invert ? GTK_SCROLL_PAGE_BACKWARD : GTK_SCROLL_PAGE_FORWARD;
2240           break;
2241         case 3:
2242           return invert ? GTK_SCROLL_START : GTK_SCROLL_END;
2243           break;
2244        }
2245       break;
2246
2247       /* In the trough */
2248     case MOUSE_TROUGH:
2249       {
2250         if (priv->trough_click_forward)
2251           return GTK_SCROLL_PAGE_FORWARD;
2252         else
2253           return GTK_SCROLL_PAGE_BACKWARD;
2254       }
2255       break;
2256
2257     case MOUSE_OUTSIDE:
2258     case MOUSE_SLIDER:
2259     case MOUSE_WIDGET:
2260       break;
2261     }
2262
2263   return GTK_SCROLL_NONE;
2264 }
2265
2266 static gdouble
2267 coord_to_value (GtkRange *range,
2268                 gint      coord)
2269 {
2270   GtkRangePrivate *priv = range->priv;
2271   gdouble frac;
2272   gdouble value;
2273   gint    trough_length;
2274   gint    trough_start;
2275   gint    slider_length;
2276   gint    trough_border;
2277   gint    trough_under_steppers;
2278
2279   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
2280     {
2281       trough_length = priv->trough.height;
2282       trough_start  = priv->trough.y;
2283       slider_length = priv->slider.height;
2284     }
2285   else
2286     {
2287       trough_length = priv->trough.width;
2288       trough_start  = priv->trough.x;
2289       slider_length = priv->slider.width;
2290     }
2291
2292   gtk_range_get_props (range, NULL, NULL, NULL, &trough_border, NULL,
2293                        &trough_under_steppers, NULL, NULL);
2294
2295   if (! trough_under_steppers)
2296     {
2297       trough_start += trough_border;
2298       trough_length -= 2 * trough_border;
2299     }
2300
2301   if (trough_length == slider_length)
2302     frac = 1.0;
2303   else
2304     frac = (MAX (0, coord - trough_start) /
2305             (gdouble) (trough_length - slider_length));
2306
2307   if (should_invert (range))
2308     frac = 1.0 - frac;
2309
2310   value = priv->adjustment->lower + frac * (priv->adjustment->upper -
2311                                             priv->adjustment->lower -
2312                                             priv->adjustment->page_size);
2313
2314   return value;
2315 }
2316
2317 static gboolean
2318 gtk_range_key_press (GtkWidget   *widget,
2319                      GdkEventKey *event)
2320 {
2321   GdkDevice *device;
2322   GtkRange *range = GTK_RANGE (widget);
2323   GtkRangePrivate *priv = range->priv;
2324
2325   device = gdk_event_get_device ((GdkEvent *) event);
2326   device = gdk_device_get_associated_device (device);
2327
2328   if (device == priv->grab_device &&
2329       event->keyval == GDK_KEY_Escape &&
2330       priv->grab_location != MOUSE_OUTSIDE)
2331     {
2332       stop_scrolling (range);
2333
2334       update_slider_position (range,
2335                               priv->slide_initial_coordinate,
2336                               priv->slide_initial_coordinate);
2337
2338       return TRUE;
2339     }
2340
2341   return GTK_WIDGET_CLASS (gtk_range_parent_class)->key_press_event (widget, event);
2342 }
2343
2344 static gint
2345 gtk_range_button_press (GtkWidget      *widget,
2346                         GdkEventButton *event)
2347 {
2348   GtkRange *range = GTK_RANGE (widget);
2349   GtkRangePrivate *priv = range->priv;
2350   GdkDevice *device;
2351
2352   if (!gtk_widget_has_focus (widget))
2353     gtk_widget_grab_focus (widget);
2354
2355   /* ignore presses when we're already doing something else. */
2356   if (priv->grab_location != MOUSE_OUTSIDE)
2357     return FALSE;
2358
2359   device = gdk_event_get_device ((GdkEvent *) event);
2360   priv->mouse_x = event->x;
2361   priv->mouse_y = event->y;
2362
2363   if (gtk_range_update_mouse_location (range))
2364     gtk_widget_queue_draw (widget);
2365
2366   if (priv->mouse_location == MOUSE_TROUGH  &&
2367       event->button == 1)
2368     {
2369       /* button 1 steps by page increment, as with button 2 on a stepper
2370        */
2371       GtkScrollType scroll;
2372       gdouble click_value;
2373       
2374       click_value = coord_to_value (range,
2375                                     priv->orientation == GTK_ORIENTATION_VERTICAL ?
2376                                     event->y : event->x);
2377
2378       priv->trough_click_forward = click_value > priv->adjustment->value;
2379       range_grab_add (range, device, MOUSE_TROUGH, event->button);
2380       
2381       scroll = range_get_scroll_for_grab (range);
2382       
2383       gtk_range_add_step_timer (range, scroll);
2384
2385       return TRUE;
2386     }
2387   else if ((priv->mouse_location == MOUSE_STEPPER_A ||
2388             priv->mouse_location == MOUSE_STEPPER_B ||
2389             priv->mouse_location == MOUSE_STEPPER_C ||
2390             priv->mouse_location == MOUSE_STEPPER_D) &&
2391            (event->button == 1 || event->button == 2 || event->button == 3))
2392     {
2393       GtkAllocation allocation;
2394       GdkRectangle *stepper_area;
2395       GtkScrollType scroll;
2396
2397       range_grab_add (range, device, priv->mouse_location, event->button);
2398
2399       gtk_widget_get_allocation (widget, &allocation);
2400       stepper_area = get_area (range, priv->mouse_location);
2401
2402       gtk_widget_queue_draw_area (widget,
2403                                   allocation.x + stepper_area->x,
2404                                   allocation.y + stepper_area->y,
2405                                   stepper_area->width,
2406                                   stepper_area->height);
2407
2408       scroll = range_get_scroll_for_grab (range);
2409       if (scroll != GTK_SCROLL_NONE)
2410         gtk_range_add_step_timer (range, scroll);
2411       
2412       return TRUE;
2413     }
2414   else if ((priv->mouse_location == MOUSE_TROUGH &&
2415             event->button == 2) ||
2416            priv->mouse_location == MOUSE_SLIDER)
2417     {
2418       gboolean need_value_update = FALSE;
2419
2420       /* Any button can be used to drag the slider, but you can start
2421        * dragging the slider with a trough click using button 2;
2422        * On button 2 press, we warp the slider to mouse position,
2423        * then begin the slider drag.
2424        */
2425       if (event->button == 2)
2426         {
2427           gdouble slider_low_value, slider_high_value, new_value;
2428           
2429           slider_high_value =
2430             coord_to_value (range,
2431                             priv->orientation == GTK_ORIENTATION_VERTICAL ?
2432                             event->y : event->x);
2433           slider_low_value =
2434             coord_to_value (range,
2435                             priv->orientation == GTK_ORIENTATION_VERTICAL ?
2436                             event->y - priv->slider.height :
2437                             event->x - priv->slider.width);
2438
2439           /* compute new value for warped slider */
2440           new_value = slider_low_value + (slider_high_value - slider_low_value) / 2;
2441
2442           /* recalc slider, so we can set slide_initial_slider_position
2443            * properly
2444            */
2445           priv->need_recalc = TRUE;
2446           gtk_range_calc_layout (range, new_value);
2447
2448           /* defer adjustment updates to update_slider_position() in order
2449            * to keep pixel quantisation
2450            */
2451           need_value_update = TRUE;
2452         }
2453
2454       if (priv->orientation == GTK_ORIENTATION_VERTICAL)
2455         {
2456           priv->slide_initial_slider_position = priv->slider.y;
2457           priv->slide_initial_coordinate = event->y;
2458         }
2459       else
2460         {
2461           priv->slide_initial_slider_position = priv->slider.x;
2462           priv->slide_initial_coordinate = event->x;
2463         }
2464
2465       range_grab_add (range, device, MOUSE_SLIDER, event->button);
2466
2467       gtk_widget_queue_draw (widget);
2468
2469       if (need_value_update)
2470         update_slider_position (range, event->x, event->y);
2471
2472       return TRUE;
2473     }
2474   
2475   return FALSE;
2476 }
2477
2478 /* During a slide, move the slider as required given new mouse position */
2479 static void
2480 update_slider_position (GtkRange *range,
2481                         gint      mouse_x,
2482                         gint      mouse_y)
2483 {
2484   GtkRangePrivate *priv = range->priv;
2485   gint delta;
2486   gint c;
2487   gdouble new_value;
2488   gboolean handled;
2489   gdouble next_value;
2490   gdouble mark_value;
2491   gdouble mark_delta;
2492   gint i;
2493
2494   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
2495     delta = mouse_y - priv->slide_initial_coordinate;
2496   else
2497     delta = mouse_x - priv->slide_initial_coordinate;
2498
2499   c = priv->slide_initial_slider_position + delta;
2500
2501   new_value = coord_to_value (range, c);
2502   next_value = coord_to_value (range, c + 1);
2503   mark_delta = fabs (next_value - new_value); 
2504
2505   for (i = 0; i < priv->n_marks; i++)
2506     {
2507       mark_value = priv->marks[i];
2508
2509       if (fabs (priv->adjustment->value - mark_value) < 3 * mark_delta)
2510         {
2511           if (fabs (new_value - mark_value) < (priv->slider_end - priv->slider_start) * 0.5 * mark_delta)
2512             {
2513               new_value = mark_value;
2514               break;
2515             }
2516         }
2517     }  
2518
2519   g_signal_emit (range, signals[CHANGE_VALUE], 0, GTK_SCROLL_JUMP, new_value,
2520                  &handled);
2521 }
2522
2523 static void 
2524 stop_scrolling (GtkRange *range)
2525 {
2526   range_grab_remove (range);
2527   gtk_range_remove_step_timer (range);
2528   /* Flush any pending discontinuous/delayed updates */
2529   gtk_range_update_value (range);
2530 }
2531
2532 static gboolean
2533 gtk_range_grab_broken (GtkWidget          *widget,
2534                        GdkEventGrabBroken *event)
2535 {
2536   GtkRange *range = GTK_RANGE (widget);
2537   GtkRangePrivate *priv = range->priv;
2538   GdkDevice *device;
2539
2540   device = gdk_event_get_device ((GdkEvent *) event);
2541
2542   if (device == priv->grab_device &&
2543       priv->grab_location != MOUSE_OUTSIDE)
2544     {
2545       if (priv->grab_location == MOUSE_SLIDER)
2546         update_slider_position (range, priv->mouse_x, priv->mouse_y);
2547
2548       stop_scrolling (range);
2549       
2550       return TRUE;
2551     }
2552   
2553   return FALSE;
2554 }
2555
2556 static gint
2557 gtk_range_button_release (GtkWidget      *widget,
2558                           GdkEventButton *event)
2559 {
2560   GtkRange *range = GTK_RANGE (widget);
2561   GtkRangePrivate *priv = range->priv;
2562   GdkDevice *device;
2563
2564   if (event->window == priv->event_window)
2565     {
2566       priv->mouse_x = event->x;
2567       priv->mouse_y = event->y;
2568     }
2569   else
2570     {
2571       gdk_window_get_device_position (priv->event_window,
2572                                       event->device,
2573                                       &priv->mouse_x,
2574                                       &priv->mouse_y,
2575                                       NULL);
2576     }
2577
2578   device = gdk_event_get_device ((GdkEvent *) event);
2579
2580   if (priv->grab_device == device &&
2581       priv->grab_button == event->button)
2582     {
2583       if (priv->grab_location == MOUSE_SLIDER)
2584         update_slider_position (range, priv->mouse_x, priv->mouse_y);
2585
2586       stop_scrolling (range);
2587       
2588       return TRUE;
2589     }
2590
2591   return FALSE;
2592 }
2593
2594 /**
2595  * _gtk_range_get_wheel_delta:
2596  * @range: a #GtkRange
2597  * @direction: A #GdkScrollDirection
2598  * 
2599  * Returns a good step value for the mouse wheel.
2600  * 
2601  * Return value: A good step value for the mouse wheel. 
2602  * 
2603  * Since: 2.4
2604  **/
2605 gdouble
2606 _gtk_range_get_wheel_delta (GtkRange           *range,
2607                             GdkScrollDirection  direction)
2608 {
2609   GtkRangePrivate *priv = range->priv;
2610   GtkAdjustment *adj = priv->adjustment;
2611   gdouble delta;
2612
2613   if (GTK_IS_SCROLLBAR (range))
2614     delta = pow (adj->page_size, 2.0 / 3.0);
2615   else
2616     delta = adj->step_increment * 2;
2617   
2618   if (direction == GDK_SCROLL_UP ||
2619       direction == GDK_SCROLL_LEFT)
2620     delta = - delta;
2621   
2622   if (priv->inverted)
2623     delta = - delta;
2624
2625   return delta;
2626 }
2627       
2628 static gboolean
2629 gtk_range_scroll_event (GtkWidget      *widget,
2630                         GdkEventScroll *event)
2631 {
2632   GtkRange *range = GTK_RANGE (widget);
2633   GtkRangePrivate *priv = range->priv;
2634
2635   if (gtk_widget_get_realized (widget))
2636     {
2637       GtkAdjustment *adj = priv->adjustment;
2638       gdouble delta;
2639       gboolean handled;
2640
2641       delta = _gtk_range_get_wheel_delta (range, event->direction);
2642
2643       g_signal_emit (range, signals[CHANGE_VALUE], 0,
2644                      GTK_SCROLL_JUMP, adj->value + delta,
2645                      &handled);
2646       
2647       /* Policy DELAYED makes sense with scroll events,
2648        * but DISCONTINUOUS doesn't, so we update immediately
2649        * for DISCONTINUOUS
2650        */
2651       if (priv->update_policy == GTK_UPDATE_DISCONTINUOUS)
2652         gtk_range_update_value (range);
2653     }
2654
2655   return TRUE;
2656 }
2657
2658 static gboolean
2659 gtk_range_motion_notify (GtkWidget      *widget,
2660                          GdkEventMotion *event)
2661 {
2662   GtkRange *range = GTK_RANGE (widget);
2663   GtkRangePrivate *priv = range->priv;
2664
2665   gdk_event_request_motions (event);
2666
2667   priv->mouse_x = event->x;
2668   priv->mouse_y = event->y;
2669
2670   if (gtk_range_update_mouse_location (range))
2671     gtk_widget_queue_draw (widget);
2672
2673   if (priv->grab_location == MOUSE_SLIDER)
2674     update_slider_position (range, event->x, event->y);
2675
2676   /* We handled the event if the mouse was in the range_rect */
2677   return priv->mouse_location != MOUSE_OUTSIDE;
2678 }
2679
2680 static gboolean
2681 gtk_range_enter_notify (GtkWidget        *widget,
2682                         GdkEventCrossing *event)
2683 {
2684   GtkRange *range = GTK_RANGE (widget);
2685   GtkRangePrivate *priv = range->priv;
2686
2687   priv->mouse_x = event->x;
2688   priv->mouse_y = event->y;
2689
2690   if (gtk_range_update_mouse_location (range))
2691     gtk_widget_queue_draw (widget);
2692   
2693   return TRUE;
2694 }
2695
2696 static gboolean
2697 gtk_range_leave_notify (GtkWidget        *widget,
2698                         GdkEventCrossing *event)
2699 {
2700   GtkRange *range = GTK_RANGE (widget);
2701   GtkRangePrivate *priv = range->priv;
2702
2703   priv->mouse_x = -1;
2704   priv->mouse_y = -1;
2705
2706   if (gtk_range_update_mouse_location (range))
2707     gtk_widget_queue_draw (widget);
2708   
2709   return TRUE;
2710 }
2711
2712 static void
2713 gtk_range_grab_notify (GtkWidget *widget,
2714                        gboolean   was_grabbed)
2715 {
2716   GtkRangePrivate *priv = GTK_RANGE (widget)->priv;
2717
2718   if (priv->grab_device &&
2719       gtk_widget_device_is_shadowed (widget, priv->grab_device))
2720     stop_scrolling (GTK_RANGE (widget));
2721 }
2722
2723 static void
2724 gtk_range_state_changed (GtkWidget    *widget,
2725                          GtkStateType  previous_state)
2726 {
2727   if (!gtk_widget_is_sensitive (widget))
2728     stop_scrolling (GTK_RANGE (widget));
2729 }
2730
2731 #define check_rectangle(rectangle1, rectangle2)              \
2732   {                                                          \
2733     if (rectangle1.x != rectangle2.x) return TRUE;           \
2734     if (rectangle1.y != rectangle2.y) return TRUE;           \
2735     if (rectangle1.width  != rectangle2.width)  return TRUE; \
2736     if (rectangle1.height != rectangle2.height) return TRUE; \
2737   }
2738
2739 static gboolean
2740 layout_changed (GtkRangePrivate *priv1,
2741                 GtkRangePrivate *priv2)
2742 {
2743   check_rectangle (priv1->slider, priv2->slider);
2744   check_rectangle (priv1->trough, priv2->trough);
2745   check_rectangle (priv1->stepper_a, priv2->stepper_a);
2746   check_rectangle (priv1->stepper_d, priv2->stepper_d);
2747   check_rectangle (priv1->stepper_b, priv2->stepper_b);
2748   check_rectangle (priv1->stepper_c, priv2->stepper_c);
2749
2750   if (priv1->upper_sensitive != priv2->upper_sensitive) return TRUE;
2751   if (priv1->lower_sensitive != priv2->lower_sensitive) return TRUE;
2752
2753   return FALSE;
2754 }
2755
2756 static void
2757 gtk_range_adjustment_changed (GtkAdjustment *adjustment,
2758                               gpointer       data)
2759 {
2760   GtkRange *range = GTK_RANGE (data);
2761   GtkRangePrivate *priv = range->priv;
2762   GtkRangePrivate priv_aux = *priv;
2763
2764   priv->recalc_marks = TRUE;
2765   priv->need_recalc = TRUE;
2766   gtk_range_calc_layout (range, priv->adjustment->value);
2767
2768   /* now check whether the layout changed  */
2769   if (layout_changed (priv, &priv_aux))
2770     gtk_widget_queue_draw (GTK_WIDGET (range));
2771
2772   /* Note that we don't round off to priv->round_digits here.
2773    * that's because it's really broken to change a value
2774    * in response to a change signal on that value; round_digits
2775    * is therefore defined to be a filter on what the GtkRange
2776    * can input into the adjustment, not a filter that the GtkRange
2777    * will enforce on the adjustment.
2778    */
2779 }
2780
2781 static gboolean
2782 force_repaint (gpointer data)
2783 {
2784   GtkRange *range = GTK_RANGE (data);
2785   GtkRangePrivate *priv = range->priv;
2786   GtkWidget *widget = GTK_WIDGET (range);
2787
2788   priv->repaint_id = 0;
2789   if (gtk_widget_is_drawable (widget))
2790     gdk_window_process_updates (gtk_widget_get_window (widget), FALSE);
2791
2792   return FALSE;
2793 }
2794
2795 static void
2796 gtk_range_adjustment_value_changed (GtkAdjustment *adjustment,
2797                                     gpointer       data)
2798 {
2799   GtkRange *range = GTK_RANGE (data);
2800   GtkRangePrivate *priv = range->priv;
2801   GtkRangePrivate priv_aux = *priv;
2802
2803   priv->need_recalc = TRUE;
2804   gtk_range_calc_layout (range, priv->adjustment->value);
2805   
2806   /* now check whether the layout changed  */
2807   if (layout_changed (priv, &priv_aux) ||
2808       (GTK_IS_SCALE (range) && gtk_scale_get_draw_value (GTK_SCALE (range))))
2809     {
2810       gtk_widget_queue_draw (GTK_WIDGET (range));
2811       /* setup a timer to ensure the range isn't lagging too much behind the scroll position */
2812       if (!priv->repaint_id)
2813         priv->repaint_id = gdk_threads_add_timeout_full (GDK_PRIORITY_EVENTS, 181, force_repaint, range, NULL);
2814     }
2815   
2816   /* Note that we don't round off to priv->round_digits here.
2817    * that's because it's really broken to change a value
2818    * in response to a change signal on that value; round_digits
2819    * is therefore defined to be a filter on what the GtkRange
2820    * can input into the adjustment, not a filter that the GtkRange
2821    * will enforce on the adjustment.
2822    */
2823
2824   g_signal_emit (range, signals[VALUE_CHANGED], 0);
2825 }
2826
2827 static void
2828 gtk_range_style_set (GtkWidget *widget,
2829                      GtkStyle  *previous_style)
2830 {
2831   GtkRange *range = GTK_RANGE (widget);
2832   GtkRangePrivate *priv = range->priv;
2833
2834   priv->need_recalc = TRUE;
2835
2836   GTK_WIDGET_CLASS (gtk_range_parent_class)->style_set (widget, previous_style);
2837 }
2838
2839 static void
2840 apply_marks (GtkRange *range, 
2841              gdouble   oldval,
2842              gdouble  *newval)
2843 {
2844   GtkRangePrivate *priv = range->priv;
2845   gint i;
2846   gdouble mark;
2847
2848   for (i = 0; i < priv->n_marks; i++)
2849     {
2850       mark = priv->marks[i];
2851       if ((oldval < mark && mark < *newval) ||
2852           (oldval > mark && mark > *newval))
2853         {
2854           *newval = mark;
2855           return;
2856         }
2857     }
2858 }
2859
2860 static void
2861 step_back (GtkRange *range)
2862 {
2863   GtkRangePrivate *priv = range->priv;
2864   gdouble newval;
2865   gboolean handled;
2866
2867   newval = priv->adjustment->value - priv->adjustment->step_increment;
2868   apply_marks (range, priv->adjustment->value, &newval);
2869   g_signal_emit (range, signals[CHANGE_VALUE], 0,
2870                  GTK_SCROLL_STEP_BACKWARD, newval, &handled);
2871 }
2872
2873 static void
2874 step_forward (GtkRange *range)
2875 {
2876   GtkRangePrivate *priv = range->priv;
2877   gdouble newval;
2878   gboolean handled;
2879
2880   newval = priv->adjustment->value + priv->adjustment->step_increment;
2881   apply_marks (range, priv->adjustment->value, &newval);
2882   g_signal_emit (range, signals[CHANGE_VALUE], 0,
2883                  GTK_SCROLL_STEP_FORWARD, newval, &handled);
2884 }
2885
2886
2887 static void
2888 page_back (GtkRange *range)
2889 {
2890   GtkRangePrivate *priv = range->priv;
2891   gdouble newval;
2892   gboolean handled;
2893
2894   newval = priv->adjustment->value - priv->adjustment->page_increment;
2895   apply_marks (range, priv->adjustment->value, &newval);
2896   g_signal_emit (range, signals[CHANGE_VALUE], 0,
2897                  GTK_SCROLL_PAGE_BACKWARD, newval, &handled);
2898 }
2899
2900 static void
2901 page_forward (GtkRange *range)
2902 {
2903   GtkRangePrivate *priv = range->priv;
2904   gdouble newval;
2905   gboolean handled;
2906
2907   newval = priv->adjustment->value + priv->adjustment->page_increment;
2908   apply_marks (range, priv->adjustment->value, &newval);
2909   g_signal_emit (range, signals[CHANGE_VALUE], 0,
2910                  GTK_SCROLL_PAGE_FORWARD, newval, &handled);
2911 }
2912
2913 static void
2914 scroll_begin (GtkRange *range)
2915 {
2916   GtkRangePrivate *priv = range->priv;
2917   gboolean handled;
2918
2919   g_signal_emit (range, signals[CHANGE_VALUE], 0,
2920                  GTK_SCROLL_START, priv->adjustment->lower,
2921                  &handled);
2922 }
2923
2924 static void
2925 scroll_end (GtkRange *range)
2926 {
2927   GtkRangePrivate *priv = range->priv;
2928   gdouble newval;
2929   gboolean handled;
2930
2931   newval = priv->adjustment->upper - priv->adjustment->page_size;
2932   g_signal_emit (range, signals[CHANGE_VALUE], 0, GTK_SCROLL_END, newval,
2933                  &handled);
2934 }
2935
2936 static gboolean
2937 gtk_range_scroll (GtkRange     *range,
2938                   GtkScrollType scroll)
2939 {
2940   GtkRangePrivate *priv = range->priv;
2941   gdouble old_value = priv->adjustment->value;
2942
2943   switch (scroll)
2944     {
2945     case GTK_SCROLL_STEP_LEFT:
2946       if (should_invert (range))
2947         step_forward (range);
2948       else
2949         step_back (range);
2950       break;
2951                     
2952     case GTK_SCROLL_STEP_UP:
2953       if (should_invert (range))
2954         step_forward (range);
2955       else
2956         step_back (range);
2957       break;
2958
2959     case GTK_SCROLL_STEP_RIGHT:
2960       if (should_invert (range))
2961         step_back (range);
2962       else
2963         step_forward (range);
2964       break;
2965                     
2966     case GTK_SCROLL_STEP_DOWN:
2967       if (should_invert (range))
2968         step_back (range);
2969       else
2970         step_forward (range);
2971       break;
2972                   
2973     case GTK_SCROLL_STEP_BACKWARD:
2974       step_back (range);
2975       break;
2976                   
2977     case GTK_SCROLL_STEP_FORWARD:
2978       step_forward (range);
2979       break;
2980
2981     case GTK_SCROLL_PAGE_LEFT:
2982       if (should_invert (range))
2983         page_forward (range);
2984       else
2985         page_back (range);
2986       break;
2987                     
2988     case GTK_SCROLL_PAGE_UP:
2989       if (should_invert (range))
2990         page_forward (range);
2991       else
2992         page_back (range);
2993       break;
2994
2995     case GTK_SCROLL_PAGE_RIGHT:
2996       if (should_invert (range))
2997         page_back (range);
2998       else
2999         page_forward (range);
3000       break;
3001                     
3002     case GTK_SCROLL_PAGE_DOWN:
3003       if (should_invert (range))
3004         page_back (range);
3005       else
3006         page_forward (range);
3007       break;
3008                   
3009     case GTK_SCROLL_PAGE_BACKWARD:
3010       page_back (range);
3011       break;
3012                   
3013     case GTK_SCROLL_PAGE_FORWARD:
3014       page_forward (range);
3015       break;
3016
3017     case GTK_SCROLL_START:
3018       scroll_begin (range);
3019       break;
3020
3021     case GTK_SCROLL_END:
3022       scroll_end (range);
3023       break;
3024
3025     case GTK_SCROLL_JUMP:
3026       /* Used by CList, range doesn't use it. */
3027       break;
3028
3029     case GTK_SCROLL_NONE:
3030       break;
3031     }
3032
3033   return priv->adjustment->value != old_value;
3034 }
3035
3036 static void
3037 gtk_range_move_slider (GtkRange     *range,
3038                        GtkScrollType scroll)
3039 {
3040   GtkRangePrivate *priv = range->priv;
3041   gboolean cursor_only;
3042
3043   g_object_get (gtk_widget_get_settings (GTK_WIDGET (range)),
3044                 "gtk-keynav-cursor-only", &cursor_only,
3045                 NULL);
3046
3047   if (cursor_only)
3048     {
3049       GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (range));
3050
3051       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
3052         {
3053           if (scroll == GTK_SCROLL_STEP_UP ||
3054               scroll == GTK_SCROLL_STEP_DOWN)
3055             {
3056               if (toplevel)
3057                 gtk_widget_child_focus (toplevel,
3058                                         scroll == GTK_SCROLL_STEP_UP ?
3059                                         GTK_DIR_UP : GTK_DIR_DOWN);
3060               return;
3061             }
3062         }
3063       else
3064         {
3065           if (scroll == GTK_SCROLL_STEP_LEFT ||
3066               scroll == GTK_SCROLL_STEP_RIGHT)
3067             {
3068               if (toplevel)
3069                 gtk_widget_child_focus (toplevel,
3070                                         scroll == GTK_SCROLL_STEP_LEFT ?
3071                                         GTK_DIR_LEFT : GTK_DIR_RIGHT);
3072               return;
3073             }
3074         }
3075     }
3076
3077   if (! gtk_range_scroll (range, scroll))
3078     gtk_widget_error_bell (GTK_WIDGET (range));
3079
3080   /* Policy DELAYED makes sense with key events,
3081    * but DISCONTINUOUS doesn't, so we update immediately
3082    * for DISCONTINUOUS
3083    */
3084   if (priv->update_policy == GTK_UPDATE_DISCONTINUOUS)
3085     gtk_range_update_value (range);
3086 }
3087
3088 static void
3089 gtk_range_get_props (GtkRange  *range,
3090                      gint      *slider_width,
3091                      gint      *stepper_size,
3092                      gint      *focus_width,
3093                      gint      *trough_border,
3094                      gint      *stepper_spacing,
3095                      gboolean  *trough_under_steppers,
3096                      gint      *arrow_displacement_x,
3097                      gint      *arrow_displacement_y)
3098 {
3099   GtkWidget *widget =  GTK_WIDGET (range);
3100   gint tmp_slider_width, tmp_stepper_size, tmp_focus_width, tmp_trough_border;
3101   gint tmp_stepper_spacing, tmp_trough_under_steppers;
3102   gint tmp_arrow_displacement_x, tmp_arrow_displacement_y;
3103   
3104   gtk_widget_style_get (widget,
3105                         "slider-width", &tmp_slider_width,
3106                         "trough-border", &tmp_trough_border,
3107                         "stepper-size", &tmp_stepper_size,
3108                         "stepper-spacing", &tmp_stepper_spacing,
3109                         "trough-under-steppers", &tmp_trough_under_steppers,
3110                         "arrow-displacement-x", &tmp_arrow_displacement_x,
3111                         "arrow-displacement-y", &tmp_arrow_displacement_y,
3112                         NULL);
3113
3114   if (tmp_stepper_spacing > 0)
3115     tmp_trough_under_steppers = FALSE;
3116
3117   if (gtk_widget_get_can_focus (GTK_WIDGET (range)))
3118     {
3119       gint focus_line_width;
3120       gint focus_padding;
3121       
3122       gtk_widget_style_get (GTK_WIDGET (range),
3123                             "focus-line-width", &focus_line_width,
3124                             "focus-padding", &focus_padding,
3125                             NULL);
3126
3127       tmp_focus_width = focus_line_width + focus_padding;
3128     }
3129   else
3130     {
3131       tmp_focus_width = 0;
3132     }
3133   
3134   if (slider_width)
3135     *slider_width = tmp_slider_width;
3136
3137   if (focus_width)
3138     *focus_width = tmp_focus_width;
3139
3140   if (trough_border)
3141     *trough_border = tmp_trough_border;
3142
3143   if (stepper_size)
3144     *stepper_size = tmp_stepper_size;
3145
3146   if (stepper_spacing)
3147     *stepper_spacing = tmp_stepper_spacing;
3148
3149   if (trough_under_steppers)
3150     *trough_under_steppers = tmp_trough_under_steppers;
3151
3152   if (arrow_displacement_x)
3153     *arrow_displacement_x = tmp_arrow_displacement_x;
3154
3155   if (arrow_displacement_y)
3156     *arrow_displacement_y = tmp_arrow_displacement_y;
3157 }
3158
3159 #define POINT_IN_RECT(xcoord, ycoord, rect) \
3160  ((xcoord) >= (rect).x &&                   \
3161   (xcoord) <  ((rect).x + (rect).width) &&  \
3162   (ycoord) >= (rect).y &&                   \
3163   (ycoord) <  ((rect).y + (rect).height))
3164
3165 /* Update mouse location, return TRUE if it changes */
3166 static gboolean
3167 gtk_range_update_mouse_location (GtkRange *range)
3168 {
3169   GtkRangePrivate *priv = range->priv;
3170   GtkAllocation allocation;
3171   gint x, y;
3172   MouseLocation old;
3173   GtkWidget *widget = GTK_WIDGET (range);
3174
3175   old = priv->mouse_location;
3176
3177   x = priv->mouse_x;
3178   y = priv->mouse_y;
3179
3180   gtk_widget_get_allocation (widget, &allocation);
3181
3182   if (priv->grab_location != MOUSE_OUTSIDE)
3183     priv->mouse_location = priv->grab_location;
3184   else if (POINT_IN_RECT (x, y, priv->stepper_a))
3185     priv->mouse_location = MOUSE_STEPPER_A;
3186   else if (POINT_IN_RECT (x, y, priv->stepper_b))
3187     priv->mouse_location = MOUSE_STEPPER_B;
3188   else if (POINT_IN_RECT (x, y, priv->stepper_c))
3189     priv->mouse_location = MOUSE_STEPPER_C;
3190   else if (POINT_IN_RECT (x, y, priv->stepper_d))
3191     priv->mouse_location = MOUSE_STEPPER_D;
3192   else if (POINT_IN_RECT (x, y, priv->slider))
3193     priv->mouse_location = MOUSE_SLIDER;
3194   else if (POINT_IN_RECT (x, y, priv->trough))
3195     priv->mouse_location = MOUSE_TROUGH;
3196   else if (POINT_IN_RECT (x, y, allocation))
3197     priv->mouse_location = MOUSE_WIDGET;
3198   else
3199     priv->mouse_location = MOUSE_OUTSIDE;
3200
3201   return old != priv->mouse_location;
3202 }
3203
3204 /* Clamp rect, border inside widget->allocation, such that we prefer
3205  * to take space from border not rect in all directions, and prefer to
3206  * give space to border over rect in one direction.
3207  */
3208 static void
3209 clamp_dimensions (GtkWidget    *widget,
3210                   GdkRectangle *rect,
3211                   GtkBorder    *border,
3212                   gboolean      border_expands_horizontally)
3213 {
3214   GtkAllocation allocation;
3215   gint extra, shortage;
3216   
3217   g_return_if_fail (rect->x == 0);
3218   g_return_if_fail (rect->y == 0);  
3219   g_return_if_fail (rect->width >= 0);
3220   g_return_if_fail (rect->height >= 0);
3221
3222   gtk_widget_get_allocation (widget, &allocation);
3223
3224   /* Width */
3225
3226   extra = allocation.width - border->left - border->right - rect->width;
3227   if (extra > 0)
3228     {
3229       if (border_expands_horizontally)
3230         {
3231           border->left += extra / 2;
3232           border->right += extra / 2 + extra % 2;
3233         }
3234       else
3235         {
3236           rect->width += extra;
3237         }
3238     }
3239   
3240   /* See if we can fit rect, if not kill the border */
3241   shortage = rect->width - allocation.width;
3242   if (shortage > 0)
3243     {
3244       rect->width = allocation.width;
3245       /* lose the border */
3246       border->left = 0;
3247       border->right = 0;
3248     }
3249   else
3250     {
3251       /* See if we can fit rect with borders */
3252       shortage = rect->width + border->left + border->right - allocation.width;
3253       if (shortage > 0)
3254         {
3255           /* Shrink borders */
3256           border->left -= shortage / 2;
3257           border->right -= shortage / 2 + shortage % 2;
3258         }
3259     }
3260
3261   /* Height */
3262
3263   extra = allocation.height - border->top - border->bottom - rect->height;
3264   if (extra > 0)
3265     {
3266       if (border_expands_horizontally)
3267         {
3268           /* don't expand border vertically */
3269           rect->height += extra;
3270         }
3271       else
3272         {
3273           border->top += extra / 2;
3274           border->bottom += extra / 2 + extra % 2;
3275         }
3276     }
3277   
3278   /* See if we can fit rect, if not kill the border */
3279   shortage = rect->height - allocation.height;
3280   if (shortage > 0)
3281     {
3282       rect->height = allocation.height;
3283       /* lose the border */
3284       border->top = 0;
3285       border->bottom = 0;
3286     }
3287   else
3288     {
3289       /* See if we can fit rect with borders */
3290       shortage = rect->height + border->top + border->bottom - allocation.height;
3291       if (shortage > 0)
3292         {
3293           /* Shrink borders */
3294           border->top -= shortage / 2;
3295           border->bottom -= shortage / 2 + shortage % 2;
3296         }
3297     }
3298 }
3299
3300 static void
3301 gtk_range_calc_request (GtkRange      *range,
3302                         gint           slider_width,
3303                         gint           stepper_size,
3304                         gint           focus_width,
3305                         gint           trough_border,
3306                         gint           stepper_spacing,
3307                         GdkRectangle  *range_rect,
3308                         GtkBorder     *border,
3309                         gint          *n_steppers_p,
3310                         gboolean      *has_steppers_ab,
3311                         gboolean      *has_steppers_cd,
3312                         gint          *slider_length_p)
3313 {
3314   GtkRangePrivate *priv = range->priv;
3315   gint slider_length;
3316   gint n_steppers;
3317   gint n_steppers_ab;
3318   gint n_steppers_cd;
3319
3320   border->left = 0;
3321   border->right = 0;
3322   border->top = 0;
3323   border->bottom = 0;
3324
3325   if (GTK_RANGE_GET_CLASS (range)->get_range_border)
3326     GTK_RANGE_GET_CLASS (range)->get_range_border (range, border);
3327
3328   n_steppers_ab = 0;
3329   n_steppers_cd = 0;
3330
3331   if (priv->has_stepper_a)
3332     n_steppers_ab += 1;
3333   if (priv->has_stepper_b)
3334     n_steppers_ab += 1;
3335   if (priv->has_stepper_c)
3336     n_steppers_cd += 1;
3337   if (priv->has_stepper_d)
3338     n_steppers_cd += 1;
3339
3340   n_steppers = n_steppers_ab + n_steppers_cd;
3341
3342   slider_length = priv->min_slider_size;
3343
3344   range_rect->x = 0;
3345   range_rect->y = 0;
3346   
3347   /* We never expand to fill available space in the small dimension
3348    * (i.e. vertical scrollbars are always a fixed width)
3349    */
3350   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
3351     {
3352       range_rect->width = (focus_width + trough_border) * 2 + slider_width;
3353       range_rect->height = stepper_size * n_steppers + (focus_width + trough_border) * 2 + slider_length;
3354
3355       if (n_steppers_ab > 0)
3356         range_rect->height += stepper_spacing;
3357
3358       if (n_steppers_cd > 0)
3359         range_rect->height += stepper_spacing;
3360     }
3361   else
3362     {
3363       range_rect->width = stepper_size * n_steppers + (focus_width + trough_border) * 2 + slider_length;
3364       range_rect->height = (focus_width + trough_border) * 2 + slider_width;
3365
3366       if (n_steppers_ab > 0)
3367         range_rect->width += stepper_spacing;
3368
3369       if (n_steppers_cd > 0)
3370         range_rect->width += stepper_spacing;
3371     }
3372
3373   if (n_steppers_p)
3374     *n_steppers_p = n_steppers;
3375
3376   if (has_steppers_ab)
3377     *has_steppers_ab = (n_steppers_ab > 0);
3378
3379   if (has_steppers_cd)
3380     *has_steppers_cd = (n_steppers_cd > 0);
3381
3382   if (slider_length_p)
3383     *slider_length_p = slider_length;
3384 }
3385
3386 static void
3387 gtk_range_calc_layout (GtkRange *range,
3388                        gdouble   adjustment_value)
3389 {
3390   GtkRangePrivate *priv = range->priv;
3391   gint slider_width, stepper_size, focus_width, trough_border, stepper_spacing;
3392   gint slider_length;
3393   GtkBorder border;
3394   gint n_steppers;
3395   gboolean has_steppers_ab;
3396   gboolean has_steppers_cd;
3397   gboolean trough_under_steppers;
3398   GdkRectangle range_rect;
3399   GtkWidget *widget;
3400
3401   if (!priv->need_recalc)
3402     return;
3403
3404   /* If we have a too-small allocation, we prefer the steppers over
3405    * the trough/slider, probably the steppers are a more useful
3406    * feature in small spaces.
3407    *
3408    * Also, we prefer to draw the range itself rather than the border
3409    * areas if there's a conflict, since the borders will be decoration
3410    * not controls. Though this depends on subclasses cooperating by
3411    * not drawing on priv->range_rect.
3412    */
3413
3414   widget = GTK_WIDGET (range);
3415
3416   gtk_range_get_props (range,
3417                        &slider_width, &stepper_size,
3418                        &focus_width, &trough_border,
3419                        &stepper_spacing, &trough_under_steppers,
3420                        NULL, NULL);
3421
3422   gtk_range_calc_request (range, 
3423                           slider_width, stepper_size,
3424                           focus_width, trough_border, stepper_spacing,
3425                           &range_rect, &border, &n_steppers,
3426                           &has_steppers_ab, &has_steppers_cd, &slider_length);
3427   
3428   /* We never expand to fill available space in the small dimension
3429    * (i.e. vertical scrollbars are always a fixed width)
3430    */
3431   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
3432     {
3433       clamp_dimensions (widget, &range_rect, &border, TRUE);
3434     }
3435   else
3436     {
3437       clamp_dimensions (widget, &range_rect, &border, FALSE);
3438     }
3439   
3440   range_rect.x = border.left;
3441   range_rect.y = border.top;
3442
3443   priv->range_rect = range_rect;
3444
3445   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
3446     {
3447       gint stepper_width, stepper_height;
3448
3449       /* Steppers are the width of the range, and stepper_size in
3450        * height, or if we don't have enough height, divided equally
3451        * among available space.
3452        */
3453       stepper_width = range_rect.width - focus_width * 2;
3454
3455       if (trough_under_steppers)
3456         stepper_width -= trough_border * 2;
3457
3458       if (stepper_width < 1)
3459         stepper_width = range_rect.width; /* screw the trough border */
3460
3461       if (n_steppers == 0)
3462         stepper_height = 0; /* avoid divide by n_steppers */
3463       else
3464         stepper_height = MIN (stepper_size, (range_rect.height / n_steppers));
3465
3466       /* Stepper A */
3467       
3468       priv->stepper_a.x = range_rect.x + focus_width + trough_border * trough_under_steppers;
3469       priv->stepper_a.y = range_rect.y + focus_width + trough_border * trough_under_steppers;
3470
3471       if (priv->has_stepper_a)
3472         {
3473           priv->stepper_a.width = stepper_width;
3474           priv->stepper_a.height = stepper_height;
3475         }
3476       else
3477         {
3478           priv->stepper_a.width = 0;
3479           priv->stepper_a.height = 0;
3480         }
3481
3482       /* Stepper B */
3483       
3484       priv->stepper_b.x = priv->stepper_a.x;
3485       priv->stepper_b.y = priv->stepper_a.y + priv->stepper_a.height;
3486
3487       if (priv->has_stepper_b)
3488         {
3489           priv->stepper_b.width = stepper_width;
3490           priv->stepper_b.height = stepper_height;
3491         }
3492       else
3493         {
3494           priv->stepper_b.width = 0;
3495           priv->stepper_b.height = 0;
3496         }
3497
3498       /* Stepper D */
3499
3500       if (priv->has_stepper_d)
3501         {
3502           priv->stepper_d.width = stepper_width;
3503           priv->stepper_d.height = stepper_height;
3504         }
3505       else
3506         {
3507           priv->stepper_d.width = 0;
3508           priv->stepper_d.height = 0;
3509         }
3510       
3511       priv->stepper_d.x = priv->stepper_a.x;
3512       priv->stepper_d.y = range_rect.y + range_rect.height - priv->stepper_d.height - focus_width - trough_border * trough_under_steppers;
3513
3514       /* Stepper C */
3515
3516       if (priv->has_stepper_c)
3517         {
3518           priv->stepper_c.width = stepper_width;
3519           priv->stepper_c.height = stepper_height;
3520         }
3521       else
3522         {
3523           priv->stepper_c.width = 0;
3524           priv->stepper_c.height = 0;
3525         }
3526       
3527       priv->stepper_c.x = priv->stepper_a.x;
3528       priv->stepper_c.y = priv->stepper_d.y - priv->stepper_c.height;
3529
3530       /* Now the trough is the remaining space between steppers B and C,
3531        * if any, minus spacing
3532        */
3533       priv->trough.x = range_rect.x;
3534       priv->trough.y = priv->stepper_b.y + priv->stepper_b.height + stepper_spacing * has_steppers_ab;
3535       priv->trough.width = range_rect.width;
3536       priv->trough.height = priv->stepper_c.y - priv->trough.y - stepper_spacing * has_steppers_cd;
3537
3538       /* Slider fits into the trough, with stepper_spacing on either side,
3539        * and the size/position based on the adjustment or fixed, depending.
3540        */
3541       priv->slider.x = priv->trough.x + focus_width + trough_border;
3542       priv->slider.width = priv->trough.width - (focus_width + trough_border) * 2;
3543
3544       /* Compute slider position/length */
3545       {
3546         gint y, bottom, top, height;
3547         
3548         top = priv->trough.y;
3549         bottom = priv->trough.y + priv->trough.height;
3550
3551         if (! trough_under_steppers)
3552           {
3553             top += trough_border;
3554             bottom -= trough_border;
3555           }
3556
3557         /* slider height is the fraction (page_size /
3558          * total_adjustment_range) times the trough height in pixels
3559          */
3560
3561         if (priv->adjustment->upper - priv->adjustment->lower != 0)
3562           height = ((bottom - top) * (priv->adjustment->page_size /
3563                                        (priv->adjustment->upper - priv->adjustment->lower)));
3564         else
3565           height = priv->min_slider_size;
3566
3567         if (height < priv->min_slider_size ||
3568             priv->slider_size_fixed)
3569           height = priv->min_slider_size;
3570
3571         height = MIN (height, priv->trough.height);
3572         
3573         y = top;
3574
3575         if (priv->adjustment->upper - priv->adjustment->lower - priv->adjustment->page_size != 0)
3576           y += (bottom - top - height) * ((adjustment_value - priv->adjustment->lower) /
3577                                           (priv->adjustment->upper - priv->adjustment->lower - priv->adjustment->page_size));
3578
3579         y = CLAMP (y, top, bottom);
3580         
3581         if (should_invert (range))
3582           y = bottom - (y - top + height);
3583         
3584         priv->slider.y = y;
3585         priv->slider.height = height;
3586
3587         /* These are publically exported */
3588         priv->slider_start = priv->slider.y;
3589         priv->slider_end = priv->slider.y + priv->slider.height;
3590       }
3591     }
3592   else
3593     {
3594       gint stepper_width, stepper_height;
3595
3596       /* Steppers are the height of the range, and stepper_size in
3597        * width, or if we don't have enough width, divided equally
3598        * among available space.
3599        */
3600       stepper_height = range_rect.height + focus_width * 2;
3601
3602       if (trough_under_steppers)
3603         stepper_height -= trough_border * 2;
3604
3605       if (stepper_height < 1)
3606         stepper_height = range_rect.height; /* screw the trough border */
3607
3608       if (n_steppers == 0)
3609         stepper_width = 0; /* avoid divide by n_steppers */
3610       else
3611         stepper_width = MIN (stepper_size, (range_rect.width / n_steppers));
3612
3613       /* Stepper A */
3614       
3615       priv->stepper_a.x = range_rect.x + focus_width + trough_border * trough_under_steppers;
3616       priv->stepper_a.y = range_rect.y + focus_width + trough_border * trough_under_steppers;
3617
3618       if (priv->has_stepper_a)
3619         {
3620           priv->stepper_a.width = stepper_width;
3621           priv->stepper_a.height = stepper_height;
3622         }
3623       else
3624         {
3625           priv->stepper_a.width = 0;
3626           priv->stepper_a.height = 0;
3627         }
3628
3629       /* Stepper B */
3630       
3631       priv->stepper_b.x = priv->stepper_a.x + priv->stepper_a.width;
3632       priv->stepper_b.y = priv->stepper_a.y;
3633
3634       if (priv->has_stepper_b)
3635         {
3636           priv->stepper_b.width = stepper_width;
3637           priv->stepper_b.height = stepper_height;
3638         }
3639       else
3640         {
3641           priv->stepper_b.width = 0;
3642           priv->stepper_b.height = 0;
3643         }
3644
3645       /* Stepper D */
3646
3647       if (priv->has_stepper_d)
3648         {
3649           priv->stepper_d.width = stepper_width;
3650           priv->stepper_d.height = stepper_height;
3651         }
3652       else
3653         {
3654           priv->stepper_d.width = 0;
3655           priv->stepper_d.height = 0;
3656         }
3657
3658       priv->stepper_d.x = range_rect.x + range_rect.width - priv->stepper_d.width - focus_width - trough_border * trough_under_steppers;
3659       priv->stepper_d.y = priv->stepper_a.y;
3660
3661
3662       /* Stepper C */
3663
3664       if (priv->has_stepper_c)
3665         {
3666           priv->stepper_c.width = stepper_width;
3667           priv->stepper_c.height = stepper_height;
3668         }
3669       else
3670         {
3671           priv->stepper_c.width = 0;
3672           priv->stepper_c.height = 0;
3673         }
3674       
3675       priv->stepper_c.x = priv->stepper_d.x - priv->stepper_c.width;
3676       priv->stepper_c.y = priv->stepper_a.y;
3677
3678       /* Now the trough is the remaining space between steppers B and C,
3679        * if any
3680        */
3681       priv->trough.x = priv->stepper_b.x + priv->stepper_b.width + stepper_spacing * has_steppers_ab;
3682       priv->trough.y = range_rect.y;
3683
3684       priv->trough.width = priv->stepper_c.x - priv->trough.x - stepper_spacing * has_steppers_cd;
3685       priv->trough.height = range_rect.height;
3686
3687       /* Slider fits into the trough, with stepper_spacing on either side,
3688        * and the size/position based on the adjustment or fixed, depending.
3689        */
3690       priv->slider.y = priv->trough.y + focus_width + trough_border;
3691       priv->slider.height = priv->trough.height - (focus_width + trough_border) * 2;
3692
3693       /* Compute slider position/length */
3694       {
3695         gint x, left, right, width;
3696         
3697         left = priv->trough.x;
3698         right = priv->trough.x + priv->trough.width;
3699
3700         if (! trough_under_steppers)
3701           {
3702             left += trough_border;
3703             right -= trough_border;
3704           }
3705
3706         /* slider width is the fraction (page_size /
3707          * total_adjustment_range) times the trough width in pixels
3708          */
3709
3710         if (priv->adjustment->upper - priv->adjustment->lower != 0)
3711           width = ((right - left) * (priv->adjustment->page_size /
3712                                    (priv->adjustment->upper - priv->adjustment->lower)));
3713         else
3714           width = priv->min_slider_size;
3715
3716         if (width < priv->min_slider_size ||
3717             priv->slider_size_fixed)
3718           width = priv->min_slider_size;
3719
3720         width = MIN (width, priv->trough.width);
3721         
3722         x = left;
3723
3724         if (priv->adjustment->upper - priv->adjustment->lower - priv->adjustment->page_size != 0)
3725           x += (right - left - width) * ((adjustment_value - priv->adjustment->lower) /
3726                                          (priv->adjustment->upper - priv->adjustment->lower - priv->adjustment->page_size));
3727         
3728         x = CLAMP (x, left, right);
3729         
3730         if (should_invert (range))
3731           x = right - (x - left + width);
3732         
3733         priv->slider.x = x;
3734         priv->slider.width = width;
3735
3736         /* These are publically exported */
3737         priv->slider_start = priv->slider.x;
3738         priv->slider_end = priv->slider.x + priv->slider.width;
3739       }
3740     }
3741   
3742   gtk_range_update_mouse_location (range);
3743
3744   switch (priv->upper_sensitivity)
3745     {
3746     case GTK_SENSITIVITY_AUTO:
3747       priv->upper_sensitive =
3748         (priv->adjustment->value <
3749          (priv->adjustment->upper - priv->adjustment->page_size));
3750       break;
3751
3752     case GTK_SENSITIVITY_ON:
3753       priv->upper_sensitive = TRUE;
3754       break;
3755
3756     case GTK_SENSITIVITY_OFF:
3757       priv->upper_sensitive = FALSE;
3758       break;
3759     }
3760
3761   switch (priv->lower_sensitivity)
3762     {
3763     case GTK_SENSITIVITY_AUTO:
3764       priv->lower_sensitive =
3765         (priv->adjustment->value > priv->adjustment->lower);
3766       break;
3767
3768     case GTK_SENSITIVITY_ON:
3769       priv->lower_sensitive = TRUE;
3770       break;
3771
3772     case GTK_SENSITIVITY_OFF:
3773       priv->lower_sensitive = FALSE;
3774       break;
3775     }
3776 }
3777
3778 static GdkRectangle*
3779 get_area (GtkRange     *range,
3780           MouseLocation location)
3781 {
3782   GtkRangePrivate *priv = range->priv;
3783
3784   switch (location)
3785     {
3786     case MOUSE_STEPPER_A:
3787       return &priv->stepper_a;
3788     case MOUSE_STEPPER_B:
3789       return &priv->stepper_b;
3790     case MOUSE_STEPPER_C:
3791       return &priv->stepper_c;
3792     case MOUSE_STEPPER_D:
3793       return &priv->stepper_d;
3794     case MOUSE_TROUGH:
3795       return &priv->trough;
3796     case MOUSE_SLIDER:
3797       return &priv->slider;
3798     case MOUSE_WIDGET:
3799     case MOUSE_OUTSIDE:
3800       break;
3801     }
3802
3803   g_warning (G_STRLOC": bug");
3804   return NULL;
3805 }
3806
3807 static void
3808 gtk_range_calc_marks (GtkRange *range)
3809 {
3810   GtkRangePrivate *priv = range->priv;
3811   gint i;
3812
3813   if (!priv->recalc_marks)
3814     return;
3815
3816   priv->recalc_marks = FALSE;
3817
3818   for (i = 0; i < priv->n_marks; i++)
3819     {
3820       priv->need_recalc = TRUE;
3821       gtk_range_calc_layout (range, priv->marks[i]);
3822       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
3823         priv->mark_pos[i] = priv->slider.x + priv->slider.width / 2;
3824       else
3825         priv->mark_pos[i] = priv->slider.y + priv->slider.height / 2;
3826     }
3827
3828   priv->need_recalc = TRUE;
3829 }
3830
3831 static gboolean
3832 gtk_range_real_change_value (GtkRange     *range,
3833                              GtkScrollType scroll,
3834                              gdouble       value)
3835 {
3836   GtkRangePrivate *priv = range->priv;
3837
3838   /* potentially adjust the bounds _before_ we clamp */
3839   g_signal_emit (range, signals[ADJUST_BOUNDS], 0, value);
3840
3841   if (priv->restrict_to_fill_level)
3842     value = MIN (value, MAX (priv->adjustment->lower,
3843                              priv->fill_level));
3844
3845   value = CLAMP (value, priv->adjustment->lower,
3846                  (priv->adjustment->upper - priv->adjustment->page_size));
3847
3848   if (priv->round_digits >= 0)
3849     {
3850       gdouble power;
3851       gint i;
3852
3853       i = priv->round_digits;
3854       power = 1;
3855       while (i--)
3856         power *= 10;
3857       
3858       value = floor ((value * power) + 0.5) / power;
3859     }
3860
3861   if (priv->adjustment->value != value)
3862     {
3863       priv->need_recalc = TRUE;
3864
3865       gtk_widget_queue_draw (GTK_WIDGET (range));
3866
3867       switch (priv->update_policy)
3868         {
3869         case GTK_UPDATE_CONTINUOUS:
3870           gtk_adjustment_set_value (priv->adjustment, value);
3871           break;
3872
3873           /* Delayed means we update after a period of inactivity */
3874         case GTK_UPDATE_DELAYED:
3875           gtk_range_reset_update_timer (range);
3876           /* FALL THRU */
3877
3878           /* Discontinuous means we update on button release */
3879         case GTK_UPDATE_DISCONTINUOUS:
3880           /* don't emit value_changed signal */
3881           priv->adjustment->value = value;
3882           priv->update_pending = TRUE;
3883           break;
3884         }
3885     }
3886   return FALSE;
3887 }
3888
3889 static void
3890 gtk_range_update_value (GtkRange *range)
3891 {
3892   GtkRangePrivate *priv = range->priv;
3893
3894   gtk_range_remove_update_timer (range);
3895
3896   if (priv->update_pending)
3897     {
3898       gtk_adjustment_value_changed (priv->adjustment);
3899
3900       priv->update_pending = FALSE;
3901     }
3902 }
3903
3904 struct _GtkRangeStepTimer
3905 {
3906   guint timeout_id;
3907   GtkScrollType step;
3908 };
3909
3910 static gboolean
3911 second_timeout (gpointer data)
3912 {
3913   GtkRange *range = GTK_RANGE (data);
3914   GtkRangePrivate *priv = range->priv;
3915
3916   gtk_range_scroll (range, priv->timer->step);
3917
3918   return TRUE;
3919 }
3920
3921 static gboolean
3922 initial_timeout (gpointer data)
3923 {
3924   GtkRange *range = GTK_RANGE (data);
3925   GtkRangePrivate *priv = range->priv;
3926   GtkSettings *settings;
3927   guint        timeout;
3928
3929   settings = gtk_widget_get_settings (GTK_WIDGET (data));
3930   g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
3931
3932   priv->timer->timeout_id = gdk_threads_add_timeout (timeout * SCROLL_DELAY_FACTOR,
3933                                             second_timeout,
3934                                             range);
3935   /* remove self */
3936   return FALSE;
3937 }
3938
3939 static void
3940 gtk_range_add_step_timer (GtkRange      *range,
3941                           GtkScrollType  step)
3942 {
3943   GtkRangePrivate *priv = range->priv;
3944   GtkSettings *settings;
3945   guint        timeout;
3946
3947   g_return_if_fail (priv->timer == NULL);
3948   g_return_if_fail (step != GTK_SCROLL_NONE);
3949
3950   settings = gtk_widget_get_settings (GTK_WIDGET (range));
3951   g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
3952
3953   priv->timer = g_new (GtkRangeStepTimer, 1);
3954
3955   priv->timer->timeout_id = gdk_threads_add_timeout (timeout,
3956                                             initial_timeout,
3957                                             range);
3958   priv->timer->step = step;
3959
3960   gtk_range_scroll (range, priv->timer->step);
3961 }
3962
3963 static void
3964 gtk_range_remove_step_timer (GtkRange *range)
3965 {
3966   GtkRangePrivate *priv = range->priv;
3967
3968   if (priv->timer)
3969     {
3970       if (priv->timer->timeout_id != 0)
3971         g_source_remove (priv->timer->timeout_id);
3972
3973       g_free (priv->timer);
3974
3975       priv->timer = NULL;
3976     }
3977 }
3978
3979 static gboolean
3980 update_timeout (gpointer data)
3981 {
3982   GtkRange *range = GTK_RANGE (data);
3983   GtkRangePrivate *priv = range->priv;
3984
3985   gtk_range_update_value (range);
3986   priv->update_timeout_id = 0;
3987
3988   /* self-remove */
3989   return FALSE;
3990 }
3991
3992 static void
3993 gtk_range_reset_update_timer (GtkRange *range)
3994 {
3995   GtkRangePrivate *priv = range->priv;
3996
3997   gtk_range_remove_update_timer (range);
3998
3999   priv->update_timeout_id = gdk_threads_add_timeout (UPDATE_DELAY,
4000                                             update_timeout,
4001                                             range);
4002 }
4003
4004 static void
4005 gtk_range_remove_update_timer (GtkRange *range)
4006 {
4007   GtkRangePrivate *priv = range->priv;
4008
4009   if (priv->update_timeout_id != 0)
4010     {
4011       g_source_remove (priv->update_timeout_id);
4012       priv->update_timeout_id = 0;
4013     }
4014 }
4015
4016 void
4017 _gtk_range_set_stop_values (GtkRange *range,
4018                             gdouble  *values,
4019                             gint      n_values)
4020 {
4021   GtkRangePrivate *priv = range->priv;
4022   gint i;
4023
4024   g_free (priv->marks);
4025   priv->marks = g_new (gdouble, n_values);
4026
4027   g_free (priv->mark_pos);
4028   priv->mark_pos = g_new (gint, n_values);
4029
4030   priv->n_marks = n_values;
4031
4032   for (i = 0; i < n_values; i++) 
4033     priv->marks[i] = values[i];
4034
4035   priv->recalc_marks = TRUE;
4036 }
4037
4038 gint
4039 _gtk_range_get_stop_positions (GtkRange  *range,
4040                                gint     **values)
4041 {
4042   GtkRangePrivate *priv = range->priv;
4043
4044   gtk_range_calc_marks (range);
4045
4046   if (values)
4047     *values = g_memdup (priv->mark_pos, priv->n_marks * sizeof (gint));
4048
4049   return priv->n_marks;
4050 }
4051
4052 void
4053 _gtk_range_set_round_digits (GtkRange *range,
4054                              gint     round_digits)
4055 {
4056   range->priv->round_digits = round_digits;
4057 }
4058
4059 void
4060 _gtk_range_set_steppers (GtkRange      *range,
4061                          gboolean       has_a,
4062                          gboolean       has_b,
4063                          gboolean       has_c,
4064                          gboolean       has_d)
4065 {
4066   range->priv->has_stepper_a = has_a;
4067   range->priv->has_stepper_b = has_b;
4068   range->priv->has_stepper_c = has_c;
4069   range->priv->has_stepper_d = has_d;
4070 }