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