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