]> Pileus Git - ~andy/gtk/blob - gtk/gtkrange.c
comboboxtext: Add gtk_combo_box_text_remove_all()
[~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, *parent;
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    * This is slightly tricky. We can't just use
1605    * gtk_widget_translate_coordinates (widget, window, 0, 0, &x, &y)
1606    * since that translates wrt to the _current_ allocation
1607    * and will lead to alternating between overlap and nonoverlap
1608    * for successive allocations.
1609    * Therefore, we find the window-widget to whose window allocation
1610    * is relative, and translate from there upwards.
1611    */
1612   parent = widget;
1613   while (gtk_widget_get_window (parent) == gtk_widget_get_window (widget) &&
1614          parent != window)
1615     {
1616       parent = gtk_widget_get_parent (parent);
1617     }
1618
1619   if (parent == window)
1620     translated_rect = *allocation;
1621   else
1622     {
1623       gtk_widget_translate_coordinates (gtk_widget_get_parent (widget),
1624                                         window,
1625                                         allocation->x, allocation->y,
1626                                         &x, &y);
1627       translated_rect.x = x;
1628       translated_rect.y = y;
1629       translated_rect.width = allocation->width;
1630       translated_rect.height = allocation->height;
1631     }
1632
1633   /* If the stepper button intersects the window resize grip.. */
1634   if (gdk_rectangle_intersect (&grip_rect, &translated_rect, NULL))
1635     {
1636       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1637         {
1638           allocation->width -= grip_rect.width;
1639           if (gtk_widget_get_direction (window) == GTK_TEXT_DIR_RTL)
1640             allocation->x += grip_rect.width;
1641         }
1642       else
1643         {
1644           allocation->height -= grip_rect.height;
1645         }
1646
1647       return TRUE;
1648     }
1649
1650   return FALSE;
1651 }
1652
1653 static void
1654 gtk_range_size_allocate (GtkWidget     *widget,
1655                          GtkAllocation *allocation)
1656 {
1657   GtkRange *range = GTK_RANGE (widget);
1658   GtkRangePrivate *priv = range->priv;
1659
1660   modify_allocation_for_window_grip (widget, allocation);
1661   gtk_widget_set_allocation (widget, allocation);
1662
1663   priv->recalc_marks = TRUE;
1664
1665   priv->need_recalc = TRUE;
1666   gtk_range_calc_layout (range, priv->adjustment->value);
1667
1668   if (gtk_widget_get_realized (widget))
1669     gdk_window_move_resize (priv->event_window,
1670                             allocation->x, allocation->y,
1671                             allocation->width, allocation->height);
1672 }
1673
1674 static void
1675 resize_grip_visible_changed (GObject    *object,
1676                              GParamSpec *pspec,
1677                              gpointer    user_data)
1678 {
1679   gtk_widget_queue_resize (GTK_WIDGET (user_data));
1680 }
1681
1682 static void
1683 gtk_range_hierarchy_changed (GtkWidget *widget,
1684                              GtkWidget *previous_toplevel)
1685 {
1686   GtkWidget *window;
1687
1688   if (previous_toplevel)
1689     g_signal_handlers_disconnect_by_func (previous_toplevel,
1690                                           G_CALLBACK (resize_grip_visible_changed),
1691                                           widget);
1692   window = gtk_widget_get_toplevel (widget);
1693   if (GTK_IS_WINDOW (window))
1694     g_signal_connect (window, "notify::resize-grip-visible",
1695                       G_CALLBACK (resize_grip_visible_changed), widget);
1696 }
1697
1698 static void
1699 gtk_range_realize (GtkWidget *widget)
1700 {
1701   GtkAllocation allocation;
1702   GtkRange *range = GTK_RANGE (widget);
1703   GtkRangePrivate *priv = range->priv;
1704   GdkWindow *window;
1705   GdkWindowAttr attributes;
1706   gint attributes_mask;
1707
1708   gtk_range_calc_layout (range, priv->adjustment->value);
1709
1710   gtk_widget_set_realized (widget, TRUE);
1711
1712   window = gtk_widget_get_parent_window (widget);
1713   gtk_widget_set_window (widget, window);
1714   g_object_ref (window);
1715
1716   gtk_widget_get_allocation (widget, &allocation);
1717   if (modify_allocation_for_window_grip (widget, &allocation))
1718     gtk_widget_set_allocation (widget, &allocation);
1719
1720   attributes.window_type = GDK_WINDOW_CHILD;
1721   attributes.x = allocation.x;
1722   attributes.y = allocation.y;
1723   attributes.width = allocation.width;
1724   attributes.height = allocation.height;
1725   attributes.wclass = GDK_INPUT_ONLY;
1726   attributes.event_mask = gtk_widget_get_events (widget);
1727   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1728                             GDK_BUTTON_RELEASE_MASK |
1729                             GDK_ENTER_NOTIFY_MASK |
1730                             GDK_LEAVE_NOTIFY_MASK |
1731                             GDK_POINTER_MOTION_MASK |
1732                             GDK_POINTER_MOTION_HINT_MASK);
1733
1734   attributes_mask = GDK_WA_X | GDK_WA_Y;
1735
1736   priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
1737                                         &attributes, attributes_mask);
1738   gdk_window_set_user_data (priv->event_window, range);
1739
1740   gtk_widget_style_attach (widget);
1741 }
1742
1743 static void
1744 gtk_range_unrealize (GtkWidget *widget)
1745 {
1746   GtkRange *range = GTK_RANGE (widget);
1747   GtkRangePrivate *priv = range->priv;
1748
1749   gtk_range_remove_step_timer (range);
1750   gtk_range_remove_update_timer (range);
1751
1752   gdk_window_set_user_data (priv->event_window, NULL);
1753   gdk_window_destroy (priv->event_window);
1754   priv->event_window = NULL;
1755
1756   GTK_WIDGET_CLASS (gtk_range_parent_class)->unrealize (widget);
1757 }
1758
1759 static void
1760 gtk_range_map (GtkWidget *widget)
1761 {
1762   GtkRange *range = GTK_RANGE (widget);
1763   GtkRangePrivate *priv = range->priv;
1764
1765   gdk_window_show (priv->event_window);
1766
1767   GTK_WIDGET_CLASS (gtk_range_parent_class)->map (widget);
1768 }
1769
1770 static void
1771 gtk_range_unmap (GtkWidget *widget)
1772 {
1773   GtkRange *range = GTK_RANGE (widget);
1774   GtkRangePrivate *priv = range->priv;
1775
1776   stop_scrolling (range);
1777
1778   gdk_window_hide (priv->event_window);
1779
1780   GTK_WIDGET_CLASS (gtk_range_parent_class)->unmap (widget);
1781 }
1782
1783 static const gchar *
1784 gtk_range_get_slider_detail (GtkRange *range)
1785 {
1786   GtkRangePrivate *priv = range->priv;
1787   const gchar *slider_detail;
1788
1789   if (priv->slider_detail_quark)
1790     return g_quark_to_string (priv->slider_detail_quark);
1791
1792   slider_detail = GTK_RANGE_GET_CLASS (range)->slider_detail;
1793
1794   if (slider_detail && slider_detail[0] == 'X')
1795     {
1796       gchar *detail = g_strdup (slider_detail);
1797
1798       detail[0] = priv->orientation == GTK_ORIENTATION_HORIZONTAL ? 'h' : 'v';
1799
1800       priv->slider_detail_quark = g_quark_from_string (detail);
1801
1802       g_free (detail);
1803
1804       return g_quark_to_string (priv->slider_detail_quark);
1805     }
1806
1807   return slider_detail;
1808 }
1809
1810 static const gchar *
1811 gtk_range_get_stepper_detail (GtkRange *range,
1812                               Stepper   stepper)
1813 {
1814   GtkRangePrivate *priv = range->priv;
1815   const gchar *stepper_detail;
1816   gchar *detail;
1817   const gchar *position = NULL;
1818
1819   if (priv->stepper_detail_quark[stepper])
1820     return g_quark_to_string (priv->stepper_detail_quark[stepper]);
1821
1822   stepper_detail = GTK_RANGE_GET_CLASS (range)->stepper_detail;
1823
1824   switch (stepper)
1825     {
1826     case STEPPER_A:
1827       position = "_start";
1828       break;
1829     case STEPPER_B:
1830       if (priv->has_stepper_a)
1831         position = "_start_inner";
1832       else
1833         position = "_start";
1834       break;
1835     case STEPPER_C:
1836       if (priv->has_stepper_d)
1837         position = "_end_inner";
1838       else
1839         position = "_end";
1840       break;
1841     case STEPPER_D:
1842       position = "_end";
1843       break;
1844     default:
1845       g_assert_not_reached ();
1846     }
1847
1848   detail = g_strconcat (stepper_detail, position, NULL);
1849
1850   if (detail[0] == 'X')
1851     detail[0] = priv->orientation == GTK_ORIENTATION_HORIZONTAL ? 'h' : 'v';
1852
1853   priv->stepper_detail_quark[stepper] = g_quark_from_string (detail);
1854
1855   g_free (detail);
1856
1857   return g_quark_to_string (priv->stepper_detail_quark[stepper]);
1858 }
1859
1860 static void
1861 draw_stepper (GtkRange     *range,
1862               Stepper       stepper,
1863               cairo_t      *cr,
1864               GtkArrowType  arrow_type,
1865               gboolean      clicked,
1866               gboolean      prelighted)
1867 {
1868   GtkRangePrivate *priv = range->priv;
1869   GtkAllocation allocation;
1870   GtkStateType state_type;
1871   GtkShadowType shadow_type;
1872   GtkStyle *style;
1873   GtkWidget *widget = GTK_WIDGET (range);
1874   GdkWindow *window;
1875   gfloat arrow_scaling;
1876   GdkRectangle *rect;
1877   gint arrow_x;
1878   gint arrow_y;
1879   gint arrow_width;
1880   gint arrow_height;
1881
1882   switch (stepper)
1883     {
1884     case STEPPER_A:
1885       rect = &priv->stepper_a;
1886       break;
1887     case STEPPER_B:
1888       rect = &priv->stepper_b;
1889       break;
1890     case STEPPER_C:
1891       rect = &priv->stepper_c;
1892       break;
1893     case STEPPER_D:
1894       rect = &priv->stepper_d;
1895       break;
1896     default:
1897       g_assert_not_reached ();
1898     };
1899
1900   gboolean arrow_sensitive = TRUE;
1901
1902   gtk_widget_get_allocation (widget, &allocation);
1903
1904   if ((!priv->inverted && (arrow_type == GTK_ARROW_DOWN ||
1905                             arrow_type == GTK_ARROW_RIGHT)) ||
1906       (priv->inverted  && (arrow_type == GTK_ARROW_UP ||
1907                             arrow_type == GTK_ARROW_LEFT)))
1908     {
1909       arrow_sensitive = priv->upper_sensitive;
1910     }
1911   else
1912     {
1913       arrow_sensitive = priv->lower_sensitive;
1914     }
1915
1916   if (!gtk_widget_is_sensitive (GTK_WIDGET (range)) || !arrow_sensitive)
1917     state_type = GTK_STATE_INSENSITIVE;
1918   else if (clicked)
1919     state_type = GTK_STATE_ACTIVE;
1920   else if (prelighted)
1921     state_type = GTK_STATE_PRELIGHT;
1922   else 
1923     state_type = GTK_STATE_NORMAL;
1924
1925   if (clicked && arrow_sensitive)
1926     shadow_type = GTK_SHADOW_IN;
1927   else
1928     shadow_type = GTK_SHADOW_OUT;
1929
1930   style = gtk_widget_get_style (widget);
1931   window = gtk_widget_get_window (widget);
1932
1933   gtk_paint_box (style, cr,
1934                  state_type, shadow_type,
1935                  widget,
1936                  gtk_range_get_stepper_detail (range, stepper),
1937                  rect->x,
1938                  rect->y,
1939                  rect->width,
1940                  rect->height);
1941
1942   gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
1943
1944   arrow_width = rect->width * arrow_scaling;
1945   arrow_height = rect->height * arrow_scaling;
1946   arrow_x = rect->x + (rect->width - arrow_width) / 2;
1947   arrow_y = rect->y + (rect->height - arrow_height) / 2;
1948
1949   if (clicked && arrow_sensitive)
1950     {
1951       gint arrow_displacement_x;
1952       gint arrow_displacement_y;
1953
1954       gtk_range_get_props (GTK_RANGE (widget),
1955                            NULL, NULL, NULL, NULL, NULL, NULL,
1956                            &arrow_displacement_x, &arrow_displacement_y);
1957       
1958       arrow_x += arrow_displacement_x;
1959       arrow_y += arrow_displacement_y;
1960     }
1961
1962   gtk_paint_arrow (style, cr,
1963                    state_type, shadow_type,
1964                    widget,
1965                    gtk_range_get_stepper_detail (range, stepper),
1966                    arrow_type,
1967                    TRUE,
1968                    arrow_x, arrow_y, arrow_width, arrow_height);
1969 }
1970
1971 static gboolean
1972 gtk_range_draw (GtkWidget      *widget,
1973                 cairo_t        *cr)
1974 {
1975   GtkRange *range = GTK_RANGE (widget);
1976   GtkRangePrivate *priv = range->priv;
1977   gboolean sensitive;
1978   GtkStateType state;
1979   GtkShadowType shadow_type;
1980   GtkStyle *style;
1981   GdkWindow *window;
1982   gint focus_line_width = 0;
1983   gint focus_padding = 0;
1984   gboolean touchscreen;
1985
1986   g_object_get (gtk_widget_get_settings (widget),
1987                 "gtk-touchscreen-mode", &touchscreen,
1988                 NULL);
1989
1990   style = gtk_widget_get_style (widget);
1991   if (gtk_widget_get_can_focus (GTK_WIDGET (range)))
1992     gtk_widget_style_get (GTK_WIDGET (range),
1993                           "focus-line-width", &focus_line_width,
1994                           "focus-padding", &focus_padding,
1995                           NULL);
1996
1997   window = gtk_widget_get_window (widget);
1998
1999   /* we're now exposing, so there's no need to force early repaints */
2000   if (priv->repaint_id)
2001     g_source_remove (priv->repaint_id);
2002   priv->repaint_id = 0;
2003
2004   gtk_range_calc_marks (range);
2005   gtk_range_calc_layout (range, priv->adjustment->value);
2006
2007   sensitive = gtk_widget_is_sensitive (widget);
2008
2009   /* Just to be confusing, we draw the trough for the whole
2010    * range rectangle, not the trough rectangle (the trough
2011    * rectangle is just for hit detection)
2012    */
2013   cairo_save (cr);
2014   gdk_cairo_rectangle (cr, &priv->range_rect);
2015   cairo_clip (cr);
2016
2017     {
2018       gint     x      = (priv->range_rect.x +
2019                          focus_line_width + focus_padding);
2020       gint     y      = (priv->range_rect.y +
2021                          focus_line_width + focus_padding);
2022       gint     width  = (priv->range_rect.width -
2023                          2 * (focus_line_width + focus_padding));
2024       gint     height = (priv->range_rect.height -
2025                          2 * (focus_line_width + focus_padding));
2026       gboolean trough_under_steppers;
2027       gint     stepper_size;
2028       gint     stepper_spacing;
2029
2030       gtk_widget_style_get (GTK_WIDGET (range),
2031                             "trough-under-steppers", &trough_under_steppers,
2032                             "stepper-size",          &stepper_size,
2033                             "stepper-spacing",       &stepper_spacing,
2034                             NULL);
2035
2036       if (stepper_spacing > 0)
2037         trough_under_steppers = FALSE;
2038
2039       if (!trough_under_steppers)
2040         {
2041           gint offset  = 0;
2042           gint shorter = 0;
2043
2044           if (priv->has_stepper_a)
2045             offset += stepper_size;
2046
2047           if (priv->has_stepper_b)
2048             offset += stepper_size;
2049
2050           shorter += offset;
2051
2052           if (priv->has_stepper_c)
2053             shorter += stepper_size;
2054
2055           if (priv->has_stepper_d)
2056             shorter += stepper_size;
2057
2058           if (priv->has_stepper_a || priv->has_stepper_b)
2059             {
2060               offset  += stepper_spacing;
2061               shorter += stepper_spacing;
2062             }
2063
2064           if (priv->has_stepper_c || priv->has_stepper_d)
2065             {
2066               shorter += stepper_spacing;
2067             }
2068
2069           if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
2070             {
2071               x     += offset;
2072               width -= shorter;
2073             }
2074           else
2075             {
2076               y      += offset;
2077               height -= shorter;
2078             }
2079         }
2080
2081         {
2082           gint trough_change_pos_x = width;
2083           gint trough_change_pos_y = height;
2084
2085           if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
2086             trough_change_pos_x = (priv->slider.x +
2087                                    priv->slider.width / 2 -
2088                                    x);
2089           else
2090             trough_change_pos_y = (priv->slider.y +
2091                                    priv->slider.height / 2 -
2092                                    y);
2093
2094           gtk_paint_box (style, cr,
2095                          sensitive ? GTK_STATE_ACTIVE : GTK_STATE_INSENSITIVE,
2096                          GTK_SHADOW_IN,
2097                          GTK_WIDGET (range),
2098                          should_invert (range) ? "trough-upper" : "trough-lower",
2099                          x, y,
2100                          trough_change_pos_x, trough_change_pos_y);
2101
2102           if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
2103             trough_change_pos_y = 0;
2104           else
2105             trough_change_pos_x = 0;
2106
2107           gtk_paint_box (style, cr,
2108                          sensitive ? GTK_STATE_ACTIVE : GTK_STATE_INSENSITIVE,
2109                          GTK_SHADOW_IN,
2110                          GTK_WIDGET (range),
2111                          should_invert (range) ? "trough-lower" : "trough-upper",
2112                          x + trough_change_pos_x, y + trough_change_pos_y,
2113                          width - trough_change_pos_x,
2114                          height - trough_change_pos_y);
2115         }
2116
2117       if (priv->show_fill_level &&
2118           priv->adjustment->upper - priv->adjustment->page_size -
2119           priv->adjustment->lower != 0)
2120         {
2121           gdouble  fill_level  = priv->fill_level;
2122           gint     fill_x      = x;
2123           gint     fill_y      = y;
2124           gint     fill_width  = width;
2125           gint     fill_height = height;
2126           gchar   *fill_detail;
2127
2128           fill_level = CLAMP (fill_level, priv->adjustment->lower,
2129                               priv->adjustment->upper -
2130                               priv->adjustment->page_size);
2131
2132           if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
2133             {
2134               fill_x     = priv->trough.x;
2135               fill_width = (priv->slider.width +
2136                             (fill_level - priv->adjustment->lower) /
2137                             (priv->adjustment->upper -
2138                              priv->adjustment->lower -
2139                              priv->adjustment->page_size) *
2140                             (priv->trough.width -
2141                              priv->slider.width));
2142
2143               if (should_invert (range))
2144                 fill_x += priv->trough.width - fill_width;
2145             }
2146           else
2147             {
2148               fill_y      = priv->trough.y;
2149               fill_height = (priv->slider.height +
2150                              (fill_level - priv->adjustment->lower) /
2151                              (priv->adjustment->upper -
2152                               priv->adjustment->lower -
2153                               priv->adjustment->page_size) *
2154                              (priv->trough.height -
2155                               priv->slider.height));
2156
2157               if (should_invert (range))
2158                 fill_y += priv->trough.height - fill_height;
2159             }
2160
2161           if (fill_level < priv->adjustment->upper - priv->adjustment->page_size)
2162             fill_detail = "trough-fill-level-full";
2163           else
2164             fill_detail = "trough-fill-level";
2165
2166           gtk_paint_box (style, cr,
2167                          sensitive ? GTK_STATE_ACTIVE : GTK_STATE_INSENSITIVE,
2168                          GTK_SHADOW_OUT,
2169                          GTK_WIDGET (range), fill_detail,
2170                          fill_x, fill_y,
2171                          fill_width, fill_height);
2172         }
2173
2174       if (sensitive && gtk_widget_has_focus (widget))
2175         gtk_paint_focus (style, cr,
2176                          gtk_widget_get_state (widget),
2177                          widget, "trough",
2178                          priv->range_rect.x,
2179                          priv->range_rect.y,
2180                          priv->range_rect.width,
2181                          priv->range_rect.height);
2182     }
2183
2184   cairo_restore (cr);
2185
2186   shadow_type = GTK_SHADOW_OUT;
2187
2188   if (!sensitive)
2189     state = GTK_STATE_INSENSITIVE;
2190   else if (!touchscreen && priv->mouse_location == MOUSE_SLIDER)
2191     state = GTK_STATE_PRELIGHT;
2192   else
2193     state = GTK_STATE_NORMAL;
2194
2195   if (priv->grab_location == MOUSE_SLIDER)
2196     {
2197       state = GTK_STATE_ACTIVE;
2198       shadow_type = GTK_SHADOW_IN;
2199     }
2200
2201   cairo_save (cr);
2202   gdk_cairo_rectangle (cr, &priv->slider);
2203   cairo_clip (cr);
2204
2205     {
2206       gtk_paint_slider (style,
2207                         cr,
2208                         state,
2209                         shadow_type,
2210                         widget,
2211                         gtk_range_get_slider_detail (range),
2212                         priv->slider.x,
2213                         priv->slider.y,
2214                         priv->slider.width,
2215                         priv->slider.height,
2216                         priv->orientation);
2217     }
2218
2219   cairo_restore (cr);
2220
2221   if (priv->has_stepper_a)
2222     draw_stepper (range, STEPPER_A, cr,
2223                   priv->orientation == GTK_ORIENTATION_VERTICAL ? GTK_ARROW_UP : GTK_ARROW_LEFT,
2224                   priv->grab_location == MOUSE_STEPPER_A,
2225                   !touchscreen && priv->mouse_location == MOUSE_STEPPER_A);
2226
2227   if (priv->has_stepper_b)
2228     draw_stepper (range, STEPPER_B, cr,
2229                   priv->orientation == GTK_ORIENTATION_VERTICAL ? GTK_ARROW_DOWN : GTK_ARROW_RIGHT,
2230                   priv->grab_location == MOUSE_STEPPER_B,
2231                   !touchscreen && priv->mouse_location == MOUSE_STEPPER_B);
2232
2233   if (priv->has_stepper_c)
2234     draw_stepper (range, STEPPER_C, cr,
2235                   priv->orientation == GTK_ORIENTATION_VERTICAL ? GTK_ARROW_UP : GTK_ARROW_LEFT,
2236                   priv->grab_location == MOUSE_STEPPER_C,
2237                   !touchscreen && priv->mouse_location == MOUSE_STEPPER_C);
2238
2239   if (priv->has_stepper_d)
2240     draw_stepper (range, STEPPER_D, cr,
2241                   priv->orientation == GTK_ORIENTATION_VERTICAL ? GTK_ARROW_DOWN : GTK_ARROW_RIGHT,
2242                   priv->grab_location == MOUSE_STEPPER_D,
2243                   !touchscreen && priv->mouse_location == MOUSE_STEPPER_D);
2244   
2245   return FALSE;
2246 }
2247
2248 static void
2249 range_grab_add (GtkRange      *range,
2250                 GdkDevice     *device,
2251                 MouseLocation  location,
2252                 gint           button)
2253 {
2254   GtkRangePrivate *priv = range->priv;
2255
2256   if (device == priv->grab_device)
2257     return;
2258
2259   if (priv->grab_device != NULL)
2260     {
2261       g_warning ("GtkRange already had a grab device, releasing device grab");
2262       gtk_device_grab_remove (GTK_WIDGET (range), priv->grab_device);
2263     }
2264
2265   /* we don't actually gdk_grab, since a button is down */
2266   gtk_device_grab_add (GTK_WIDGET (range), device, TRUE);
2267
2268   priv->grab_location = location;
2269   priv->grab_button = button;
2270   priv->grab_device = device;
2271
2272   if (gtk_range_update_mouse_location (range))
2273     gtk_widget_queue_draw (GTK_WIDGET (range));
2274 }
2275
2276 static void
2277 range_grab_remove (GtkRange *range)
2278 {
2279   GtkRangePrivate *priv = range->priv;
2280   MouseLocation location;
2281
2282   if (priv->grab_device)
2283     {
2284       gtk_device_grab_remove (GTK_WIDGET (range),
2285                               priv->grab_device);
2286       priv->grab_device = NULL;
2287     }
2288
2289   location = priv->grab_location;
2290   priv->grab_location = MOUSE_OUTSIDE;
2291   priv->grab_button = 0;
2292
2293   if (gtk_range_update_mouse_location (range) ||
2294       location != MOUSE_OUTSIDE)
2295     gtk_widget_queue_draw (GTK_WIDGET (range));
2296 }
2297
2298 static GtkScrollType
2299 range_get_scroll_for_grab (GtkRange      *range)
2300 {
2301   GtkRangePrivate *priv = range->priv;
2302   gboolean invert;
2303
2304   invert = should_invert (range);
2305   switch (priv->grab_location)
2306     {
2307       /* Backward stepper */
2308     case MOUSE_STEPPER_A:
2309     case MOUSE_STEPPER_C:
2310       switch (priv->grab_button)
2311         {
2312         case 1:
2313           return invert ? GTK_SCROLL_STEP_FORWARD : GTK_SCROLL_STEP_BACKWARD;
2314           break;
2315         case 2:
2316           return invert ? GTK_SCROLL_PAGE_FORWARD : GTK_SCROLL_PAGE_BACKWARD;
2317           break;
2318         case 3:
2319           return invert ? GTK_SCROLL_END : GTK_SCROLL_START;
2320           break;
2321         }
2322       break;
2323
2324       /* Forward stepper */
2325     case MOUSE_STEPPER_B:
2326     case MOUSE_STEPPER_D:
2327       switch (priv->grab_button)
2328         {
2329         case 1:
2330           return invert ? GTK_SCROLL_STEP_BACKWARD : GTK_SCROLL_STEP_FORWARD;
2331           break;
2332         case 2:
2333           return invert ? GTK_SCROLL_PAGE_BACKWARD : GTK_SCROLL_PAGE_FORWARD;
2334           break;
2335         case 3:
2336           return invert ? GTK_SCROLL_START : GTK_SCROLL_END;
2337           break;
2338        }
2339       break;
2340
2341       /* In the trough */
2342     case MOUSE_TROUGH:
2343       {
2344         if (priv->trough_click_forward)
2345           return GTK_SCROLL_PAGE_FORWARD;
2346         else
2347           return GTK_SCROLL_PAGE_BACKWARD;
2348       }
2349       break;
2350
2351     case MOUSE_OUTSIDE:
2352     case MOUSE_SLIDER:
2353     case MOUSE_WIDGET:
2354       break;
2355     }
2356
2357   return GTK_SCROLL_NONE;
2358 }
2359
2360 static gdouble
2361 coord_to_value (GtkRange *range,
2362                 gint      coord)
2363 {
2364   GtkRangePrivate *priv = range->priv;
2365   gdouble frac;
2366   gdouble value;
2367   gint    trough_length;
2368   gint    trough_start;
2369   gint    slider_length;
2370   gint    trough_border;
2371   gint    trough_under_steppers;
2372
2373   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
2374     {
2375       trough_length = priv->trough.height;
2376       trough_start  = priv->trough.y;
2377       slider_length = priv->slider.height;
2378     }
2379   else
2380     {
2381       trough_length = priv->trough.width;
2382       trough_start  = priv->trough.x;
2383       slider_length = priv->slider.width;
2384     }
2385
2386   gtk_range_get_props (range, NULL, NULL, NULL, &trough_border, NULL,
2387                        &trough_under_steppers, NULL, NULL);
2388
2389   if (! trough_under_steppers)
2390     {
2391       trough_start += trough_border;
2392       trough_length -= 2 * trough_border;
2393     }
2394
2395   if (trough_length == slider_length)
2396     frac = 1.0;
2397   else
2398     frac = (MAX (0, coord - trough_start) /
2399             (gdouble) (trough_length - slider_length));
2400
2401   if (should_invert (range))
2402     frac = 1.0 - frac;
2403
2404   value = priv->adjustment->lower + frac * (priv->adjustment->upper -
2405                                             priv->adjustment->lower -
2406                                             priv->adjustment->page_size);
2407
2408   return value;
2409 }
2410
2411 static gboolean
2412 gtk_range_key_press (GtkWidget   *widget,
2413                      GdkEventKey *event)
2414 {
2415   GdkDevice *device;
2416   GtkRange *range = GTK_RANGE (widget);
2417   GtkRangePrivate *priv = range->priv;
2418
2419   device = gdk_event_get_device ((GdkEvent *) event);
2420   device = gdk_device_get_associated_device (device);
2421
2422   if (device == priv->grab_device &&
2423       event->keyval == GDK_KEY_Escape &&
2424       priv->grab_location != MOUSE_OUTSIDE)
2425     {
2426       stop_scrolling (range);
2427
2428       update_slider_position (range,
2429                               priv->slide_initial_coordinate,
2430                               priv->slide_initial_coordinate);
2431
2432       return TRUE;
2433     }
2434
2435   return GTK_WIDGET_CLASS (gtk_range_parent_class)->key_press_event (widget, event);
2436 }
2437
2438 static gint
2439 gtk_range_button_press (GtkWidget      *widget,
2440                         GdkEventButton *event)
2441 {
2442   GtkRange *range = GTK_RANGE (widget);
2443   GtkRangePrivate *priv = range->priv;
2444   GdkDevice *device;
2445
2446   if (!gtk_widget_has_focus (widget))
2447     gtk_widget_grab_focus (widget);
2448
2449   /* ignore presses when we're already doing something else. */
2450   if (priv->grab_location != MOUSE_OUTSIDE)
2451     return FALSE;
2452
2453   device = gdk_event_get_device ((GdkEvent *) event);
2454   priv->mouse_x = event->x;
2455   priv->mouse_y = event->y;
2456
2457   if (gtk_range_update_mouse_location (range))
2458     gtk_widget_queue_draw (widget);
2459
2460   if (priv->mouse_location == MOUSE_TROUGH  &&
2461       event->button == 1)
2462     {
2463       /* button 1 steps by page increment, as with button 2 on a stepper
2464        */
2465       GtkScrollType scroll;
2466       gdouble click_value;
2467       
2468       click_value = coord_to_value (range,
2469                                     priv->orientation == GTK_ORIENTATION_VERTICAL ?
2470                                     event->y : event->x);
2471
2472       priv->trough_click_forward = click_value > priv->adjustment->value;
2473       range_grab_add (range, device, MOUSE_TROUGH, event->button);
2474       
2475       scroll = range_get_scroll_for_grab (range);
2476       
2477       gtk_range_add_step_timer (range, scroll);
2478
2479       return TRUE;
2480     }
2481   else if ((priv->mouse_location == MOUSE_STEPPER_A ||
2482             priv->mouse_location == MOUSE_STEPPER_B ||
2483             priv->mouse_location == MOUSE_STEPPER_C ||
2484             priv->mouse_location == MOUSE_STEPPER_D) &&
2485            (event->button == 1 || event->button == 2 || event->button == 3))
2486     {
2487       GtkAllocation allocation;
2488       GdkRectangle *stepper_area;
2489       GtkScrollType scroll;
2490
2491       range_grab_add (range, device, priv->mouse_location, event->button);
2492
2493       gtk_widget_get_allocation (widget, &allocation);
2494       stepper_area = get_area (range, priv->mouse_location);
2495
2496       gtk_widget_queue_draw_area (widget,
2497                                   allocation.x + stepper_area->x,
2498                                   allocation.y + stepper_area->y,
2499                                   stepper_area->width,
2500                                   stepper_area->height);
2501
2502       scroll = range_get_scroll_for_grab (range);
2503       if (scroll != GTK_SCROLL_NONE)
2504         gtk_range_add_step_timer (range, scroll);
2505       
2506       return TRUE;
2507     }
2508   else if ((priv->mouse_location == MOUSE_TROUGH &&
2509             event->button == 2) ||
2510            priv->mouse_location == MOUSE_SLIDER)
2511     {
2512       gboolean need_value_update = FALSE;
2513
2514       /* Any button can be used to drag the slider, but you can start
2515        * dragging the slider with a trough click using button 2;
2516        * On button 2 press, we warp the slider to mouse position,
2517        * then begin the slider drag.
2518        */
2519       if (event->button == 2)
2520         {
2521           gdouble slider_low_value, slider_high_value, new_value;
2522           
2523           slider_high_value =
2524             coord_to_value (range,
2525                             priv->orientation == GTK_ORIENTATION_VERTICAL ?
2526                             event->y : event->x);
2527           slider_low_value =
2528             coord_to_value (range,
2529                             priv->orientation == GTK_ORIENTATION_VERTICAL ?
2530                             event->y - priv->slider.height :
2531                             event->x - priv->slider.width);
2532
2533           /* compute new value for warped slider */
2534           new_value = slider_low_value + (slider_high_value - slider_low_value) / 2;
2535
2536           /* recalc slider, so we can set slide_initial_slider_position
2537            * properly
2538            */
2539           priv->need_recalc = TRUE;
2540           gtk_range_calc_layout (range, new_value);
2541
2542           /* defer adjustment updates to update_slider_position() in order
2543            * to keep pixel quantisation
2544            */
2545           need_value_update = TRUE;
2546         }
2547
2548       if (priv->orientation == GTK_ORIENTATION_VERTICAL)
2549         {
2550           priv->slide_initial_slider_position = priv->slider.y;
2551           priv->slide_initial_coordinate = event->y;
2552         }
2553       else
2554         {
2555           priv->slide_initial_slider_position = priv->slider.x;
2556           priv->slide_initial_coordinate = event->x;
2557         }
2558
2559       range_grab_add (range, device, MOUSE_SLIDER, event->button);
2560
2561       gtk_widget_queue_draw (widget);
2562
2563       if (need_value_update)
2564         update_slider_position (range, event->x, event->y);
2565
2566       return TRUE;
2567     }
2568   
2569   return FALSE;
2570 }
2571
2572 /* During a slide, move the slider as required given new mouse position */
2573 static void
2574 update_slider_position (GtkRange *range,
2575                         gint      mouse_x,
2576                         gint      mouse_y)
2577 {
2578   GtkRangePrivate *priv = range->priv;
2579   gint delta;
2580   gint c;
2581   gdouble new_value;
2582   gboolean handled;
2583   gdouble next_value;
2584   gdouble mark_value;
2585   gdouble mark_delta;
2586   gint i;
2587
2588   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
2589     delta = mouse_y - priv->slide_initial_coordinate;
2590   else
2591     delta = mouse_x - priv->slide_initial_coordinate;
2592
2593   c = priv->slide_initial_slider_position + delta;
2594
2595   new_value = coord_to_value (range, c);
2596   next_value = coord_to_value (range, c + 1);
2597   mark_delta = fabs (next_value - new_value); 
2598
2599   for (i = 0; i < priv->n_marks; i++)
2600     {
2601       mark_value = priv->marks[i];
2602
2603       if (fabs (priv->adjustment->value - mark_value) < 3 * mark_delta)
2604         {
2605           if (fabs (new_value - mark_value) < (priv->slider_end - priv->slider_start) * 0.5 * mark_delta)
2606             {
2607               new_value = mark_value;
2608               break;
2609             }
2610         }
2611     }  
2612
2613   g_signal_emit (range, signals[CHANGE_VALUE], 0, GTK_SCROLL_JUMP, new_value,
2614                  &handled);
2615 }
2616
2617 static void 
2618 stop_scrolling (GtkRange *range)
2619 {
2620   range_grab_remove (range);
2621   gtk_range_remove_step_timer (range);
2622   /* Flush any pending discontinuous/delayed updates */
2623   gtk_range_update_value (range);
2624 }
2625
2626 static gboolean
2627 gtk_range_grab_broken (GtkWidget          *widget,
2628                        GdkEventGrabBroken *event)
2629 {
2630   GtkRange *range = GTK_RANGE (widget);
2631   GtkRangePrivate *priv = range->priv;
2632   GdkDevice *device;
2633
2634   device = gdk_event_get_device ((GdkEvent *) event);
2635
2636   if (device == priv->grab_device &&
2637       priv->grab_location != MOUSE_OUTSIDE)
2638     {
2639       if (priv->grab_location == MOUSE_SLIDER)
2640         update_slider_position (range, priv->mouse_x, priv->mouse_y);
2641
2642       stop_scrolling (range);
2643       
2644       return TRUE;
2645     }
2646   
2647   return FALSE;
2648 }
2649
2650 static gint
2651 gtk_range_button_release (GtkWidget      *widget,
2652                           GdkEventButton *event)
2653 {
2654   GtkRange *range = GTK_RANGE (widget);
2655   GtkRangePrivate *priv = range->priv;
2656   GdkDevice *device;
2657
2658   if (event->window == priv->event_window)
2659     {
2660       priv->mouse_x = event->x;
2661       priv->mouse_y = event->y;
2662     }
2663   else
2664     {
2665       gdk_window_get_device_position (priv->event_window,
2666                                       event->device,
2667                                       &priv->mouse_x,
2668                                       &priv->mouse_y,
2669                                       NULL);
2670     }
2671
2672   device = gdk_event_get_device ((GdkEvent *) event);
2673
2674   if (priv->grab_device == device &&
2675       priv->grab_button == event->button)
2676     {
2677       if (priv->grab_location == MOUSE_SLIDER)
2678         update_slider_position (range, priv->mouse_x, priv->mouse_y);
2679
2680       stop_scrolling (range);
2681       
2682       return TRUE;
2683     }
2684
2685   return FALSE;
2686 }
2687
2688 /**
2689  * _gtk_range_get_wheel_delta:
2690  * @range: a #GtkRange
2691  * @direction: A #GdkScrollDirection
2692  * 
2693  * Returns a good step value for the mouse wheel.
2694  * 
2695  * Return value: A good step value for the mouse wheel. 
2696  * 
2697  * Since: 2.4
2698  **/
2699 gdouble
2700 _gtk_range_get_wheel_delta (GtkRange           *range,
2701                             GdkScrollDirection  direction)
2702 {
2703   GtkRangePrivate *priv = range->priv;
2704   GtkAdjustment *adj = priv->adjustment;
2705   gdouble delta;
2706
2707   if (GTK_IS_SCROLLBAR (range))
2708     delta = pow (adj->page_size, 2.0 / 3.0);
2709   else
2710     delta = adj->step_increment * 2;
2711   
2712   if (direction == GDK_SCROLL_UP ||
2713       direction == GDK_SCROLL_LEFT)
2714     delta = - delta;
2715   
2716   if (priv->inverted)
2717     delta = - delta;
2718
2719   return delta;
2720 }
2721       
2722 static gboolean
2723 gtk_range_scroll_event (GtkWidget      *widget,
2724                         GdkEventScroll *event)
2725 {
2726   GtkRange *range = GTK_RANGE (widget);
2727   GtkRangePrivate *priv = range->priv;
2728
2729   if (gtk_widget_get_realized (widget))
2730     {
2731       GtkAdjustment *adj = priv->adjustment;
2732       gdouble delta;
2733       gboolean handled;
2734
2735       delta = _gtk_range_get_wheel_delta (range, event->direction);
2736
2737       g_signal_emit (range, signals[CHANGE_VALUE], 0,
2738                      GTK_SCROLL_JUMP, adj->value + delta,
2739                      &handled);
2740       
2741       /* Policy DELAYED makes sense with scroll events,
2742        * but DISCONTINUOUS doesn't, so we update immediately
2743        * for DISCONTINUOUS
2744        */
2745       if (priv->update_policy == GTK_UPDATE_DISCONTINUOUS)
2746         gtk_range_update_value (range);
2747     }
2748
2749   return TRUE;
2750 }
2751
2752 static gboolean
2753 gtk_range_motion_notify (GtkWidget      *widget,
2754                          GdkEventMotion *event)
2755 {
2756   GtkRange *range = GTK_RANGE (widget);
2757   GtkRangePrivate *priv = range->priv;
2758
2759   gdk_event_request_motions (event);
2760
2761   priv->mouse_x = event->x;
2762   priv->mouse_y = event->y;
2763
2764   if (gtk_range_update_mouse_location (range))
2765     gtk_widget_queue_draw (widget);
2766
2767   if (priv->grab_location == MOUSE_SLIDER)
2768     update_slider_position (range, event->x, event->y);
2769
2770   /* We handled the event if the mouse was in the range_rect */
2771   return priv->mouse_location != MOUSE_OUTSIDE;
2772 }
2773
2774 static gboolean
2775 gtk_range_enter_notify (GtkWidget        *widget,
2776                         GdkEventCrossing *event)
2777 {
2778   GtkRange *range = GTK_RANGE (widget);
2779   GtkRangePrivate *priv = range->priv;
2780
2781   priv->mouse_x = event->x;
2782   priv->mouse_y = event->y;
2783
2784   if (gtk_range_update_mouse_location (range))
2785     gtk_widget_queue_draw (widget);
2786   
2787   return TRUE;
2788 }
2789
2790 static gboolean
2791 gtk_range_leave_notify (GtkWidget        *widget,
2792                         GdkEventCrossing *event)
2793 {
2794   GtkRange *range = GTK_RANGE (widget);
2795   GtkRangePrivate *priv = range->priv;
2796
2797   priv->mouse_x = -1;
2798   priv->mouse_y = -1;
2799
2800   if (gtk_range_update_mouse_location (range))
2801     gtk_widget_queue_draw (widget);
2802   
2803   return TRUE;
2804 }
2805
2806 static void
2807 gtk_range_grab_notify (GtkWidget *widget,
2808                        gboolean   was_grabbed)
2809 {
2810   GtkRangePrivate *priv = GTK_RANGE (widget)->priv;
2811
2812   if (priv->grab_device &&
2813       gtk_widget_device_is_shadowed (widget, priv->grab_device))
2814     stop_scrolling (GTK_RANGE (widget));
2815 }
2816
2817 static void
2818 gtk_range_state_changed (GtkWidget    *widget,
2819                          GtkStateType  previous_state)
2820 {
2821   if (!gtk_widget_is_sensitive (widget))
2822     stop_scrolling (GTK_RANGE (widget));
2823 }
2824
2825 #define check_rectangle(rectangle1, rectangle2)              \
2826   {                                                          \
2827     if (rectangle1.x != rectangle2.x) return TRUE;           \
2828     if (rectangle1.y != rectangle2.y) return TRUE;           \
2829     if (rectangle1.width  != rectangle2.width)  return TRUE; \
2830     if (rectangle1.height != rectangle2.height) return TRUE; \
2831   }
2832
2833 static gboolean
2834 layout_changed (GtkRangePrivate *priv1,
2835                 GtkRangePrivate *priv2)
2836 {
2837   check_rectangle (priv1->slider, priv2->slider);
2838   check_rectangle (priv1->trough, priv2->trough);
2839   check_rectangle (priv1->stepper_a, priv2->stepper_a);
2840   check_rectangle (priv1->stepper_d, priv2->stepper_d);
2841   check_rectangle (priv1->stepper_b, priv2->stepper_b);
2842   check_rectangle (priv1->stepper_c, priv2->stepper_c);
2843
2844   if (priv1->upper_sensitive != priv2->upper_sensitive) return TRUE;
2845   if (priv1->lower_sensitive != priv2->lower_sensitive) return TRUE;
2846
2847   return FALSE;
2848 }
2849
2850 static void
2851 gtk_range_adjustment_changed (GtkAdjustment *adjustment,
2852                               gpointer       data)
2853 {
2854   GtkRange *range = GTK_RANGE (data);
2855   GtkRangePrivate *priv = range->priv;
2856   GtkRangePrivate priv_aux = *priv;
2857
2858   priv->recalc_marks = TRUE;
2859   priv->need_recalc = TRUE;
2860   gtk_range_calc_layout (range, priv->adjustment->value);
2861
2862   /* now check whether the layout changed  */
2863   if (layout_changed (priv, &priv_aux))
2864     gtk_widget_queue_draw (GTK_WIDGET (range));
2865
2866   /* Note that we don't round off to priv->round_digits here.
2867    * that's because it's really broken to change a value
2868    * in response to a change signal on that value; round_digits
2869    * is therefore defined to be a filter on what the GtkRange
2870    * can input into the adjustment, not a filter that the GtkRange
2871    * will enforce on the adjustment.
2872    */
2873 }
2874
2875 static gboolean
2876 force_repaint (gpointer data)
2877 {
2878   GtkRange *range = GTK_RANGE (data);
2879   GtkRangePrivate *priv = range->priv;
2880   GtkWidget *widget = GTK_WIDGET (range);
2881
2882   priv->repaint_id = 0;
2883   if (gtk_widget_is_drawable (widget))
2884     gdk_window_process_updates (gtk_widget_get_window (widget), FALSE);
2885
2886   return FALSE;
2887 }
2888
2889 static void
2890 gtk_range_adjustment_value_changed (GtkAdjustment *adjustment,
2891                                     gpointer       data)
2892 {
2893   GtkRange *range = GTK_RANGE (data);
2894   GtkRangePrivate *priv = range->priv;
2895   GtkRangePrivate priv_aux = *priv;
2896
2897   priv->need_recalc = TRUE;
2898   gtk_range_calc_layout (range, priv->adjustment->value);
2899   
2900   /* now check whether the layout changed  */
2901   if (layout_changed (priv, &priv_aux) ||
2902       (GTK_IS_SCALE (range) && gtk_scale_get_draw_value (GTK_SCALE (range))))
2903     {
2904       gtk_widget_queue_draw (GTK_WIDGET (range));
2905       /* setup a timer to ensure the range isn't lagging too much behind the scroll position */
2906       if (!priv->repaint_id)
2907         priv->repaint_id = gdk_threads_add_timeout_full (GDK_PRIORITY_EVENTS, 181, force_repaint, range, NULL);
2908     }
2909   
2910   /* Note that we don't round off to priv->round_digits here.
2911    * that's because it's really broken to change a value
2912    * in response to a change signal on that value; round_digits
2913    * is therefore defined to be a filter on what the GtkRange
2914    * can input into the adjustment, not a filter that the GtkRange
2915    * will enforce on the adjustment.
2916    */
2917
2918   g_signal_emit (range, signals[VALUE_CHANGED], 0);
2919 }
2920
2921 static void
2922 gtk_range_style_set (GtkWidget *widget,
2923                      GtkStyle  *previous_style)
2924 {
2925   GtkRange *range = GTK_RANGE (widget);
2926   GtkRangePrivate *priv = range->priv;
2927
2928   priv->need_recalc = TRUE;
2929
2930   GTK_WIDGET_CLASS (gtk_range_parent_class)->style_set (widget, previous_style);
2931 }
2932
2933 static void
2934 apply_marks (GtkRange *range, 
2935              gdouble   oldval,
2936              gdouble  *newval)
2937 {
2938   GtkRangePrivate *priv = range->priv;
2939   gint i;
2940   gdouble mark;
2941
2942   for (i = 0; i < priv->n_marks; i++)
2943     {
2944       mark = priv->marks[i];
2945       if ((oldval < mark && mark < *newval) ||
2946           (oldval > mark && mark > *newval))
2947         {
2948           *newval = mark;
2949           return;
2950         }
2951     }
2952 }
2953
2954 static void
2955 step_back (GtkRange *range)
2956 {
2957   GtkRangePrivate *priv = range->priv;
2958   gdouble newval;
2959   gboolean handled;
2960
2961   newval = priv->adjustment->value - priv->adjustment->step_increment;
2962   apply_marks (range, priv->adjustment->value, &newval);
2963   g_signal_emit (range, signals[CHANGE_VALUE], 0,
2964                  GTK_SCROLL_STEP_BACKWARD, newval, &handled);
2965 }
2966
2967 static void
2968 step_forward (GtkRange *range)
2969 {
2970   GtkRangePrivate *priv = range->priv;
2971   gdouble newval;
2972   gboolean handled;
2973
2974   newval = priv->adjustment->value + priv->adjustment->step_increment;
2975   apply_marks (range, priv->adjustment->value, &newval);
2976   g_signal_emit (range, signals[CHANGE_VALUE], 0,
2977                  GTK_SCROLL_STEP_FORWARD, newval, &handled);
2978 }
2979
2980
2981 static void
2982 page_back (GtkRange *range)
2983 {
2984   GtkRangePrivate *priv = range->priv;
2985   gdouble newval;
2986   gboolean handled;
2987
2988   newval = priv->adjustment->value - priv->adjustment->page_increment;
2989   apply_marks (range, priv->adjustment->value, &newval);
2990   g_signal_emit (range, signals[CHANGE_VALUE], 0,
2991                  GTK_SCROLL_PAGE_BACKWARD, newval, &handled);
2992 }
2993
2994 static void
2995 page_forward (GtkRange *range)
2996 {
2997   GtkRangePrivate *priv = range->priv;
2998   gdouble newval;
2999   gboolean handled;
3000
3001   newval = priv->adjustment->value + priv->adjustment->page_increment;
3002   apply_marks (range, priv->adjustment->value, &newval);
3003   g_signal_emit (range, signals[CHANGE_VALUE], 0,
3004                  GTK_SCROLL_PAGE_FORWARD, newval, &handled);
3005 }
3006
3007 static void
3008 scroll_begin (GtkRange *range)
3009 {
3010   GtkRangePrivate *priv = range->priv;
3011   gboolean handled;
3012
3013   g_signal_emit (range, signals[CHANGE_VALUE], 0,
3014                  GTK_SCROLL_START, priv->adjustment->lower,
3015                  &handled);
3016 }
3017
3018 static void
3019 scroll_end (GtkRange *range)
3020 {
3021   GtkRangePrivate *priv = range->priv;
3022   gdouble newval;
3023   gboolean handled;
3024
3025   newval = priv->adjustment->upper - priv->adjustment->page_size;
3026   g_signal_emit (range, signals[CHANGE_VALUE], 0, GTK_SCROLL_END, newval,
3027                  &handled);
3028 }
3029
3030 static gboolean
3031 gtk_range_scroll (GtkRange     *range,
3032                   GtkScrollType scroll)
3033 {
3034   GtkRangePrivate *priv = range->priv;
3035   gdouble old_value = priv->adjustment->value;
3036
3037   switch (scroll)
3038     {
3039     case GTK_SCROLL_STEP_LEFT:
3040       if (should_invert (range))
3041         step_forward (range);
3042       else
3043         step_back (range);
3044       break;
3045                     
3046     case GTK_SCROLL_STEP_UP:
3047       if (should_invert (range))
3048         step_forward (range);
3049       else
3050         step_back (range);
3051       break;
3052
3053     case GTK_SCROLL_STEP_RIGHT:
3054       if (should_invert (range))
3055         step_back (range);
3056       else
3057         step_forward (range);
3058       break;
3059                     
3060     case GTK_SCROLL_STEP_DOWN:
3061       if (should_invert (range))
3062         step_back (range);
3063       else
3064         step_forward (range);
3065       break;
3066                   
3067     case GTK_SCROLL_STEP_BACKWARD:
3068       step_back (range);
3069       break;
3070                   
3071     case GTK_SCROLL_STEP_FORWARD:
3072       step_forward (range);
3073       break;
3074
3075     case GTK_SCROLL_PAGE_LEFT:
3076       if (should_invert (range))
3077         page_forward (range);
3078       else
3079         page_back (range);
3080       break;
3081                     
3082     case GTK_SCROLL_PAGE_UP:
3083       if (should_invert (range))
3084         page_forward (range);
3085       else
3086         page_back (range);
3087       break;
3088
3089     case GTK_SCROLL_PAGE_RIGHT:
3090       if (should_invert (range))
3091         page_back (range);
3092       else
3093         page_forward (range);
3094       break;
3095                     
3096     case GTK_SCROLL_PAGE_DOWN:
3097       if (should_invert (range))
3098         page_back (range);
3099       else
3100         page_forward (range);
3101       break;
3102                   
3103     case GTK_SCROLL_PAGE_BACKWARD:
3104       page_back (range);
3105       break;
3106                   
3107     case GTK_SCROLL_PAGE_FORWARD:
3108       page_forward (range);
3109       break;
3110
3111     case GTK_SCROLL_START:
3112       scroll_begin (range);
3113       break;
3114
3115     case GTK_SCROLL_END:
3116       scroll_end (range);
3117       break;
3118
3119     case GTK_SCROLL_JUMP:
3120       /* Used by CList, range doesn't use it. */
3121       break;
3122
3123     case GTK_SCROLL_NONE:
3124       break;
3125     }
3126
3127   return priv->adjustment->value != old_value;
3128 }
3129
3130 static void
3131 gtk_range_move_slider (GtkRange     *range,
3132                        GtkScrollType scroll)
3133 {
3134   GtkRangePrivate *priv = range->priv;
3135   gboolean cursor_only;
3136
3137   g_object_get (gtk_widget_get_settings (GTK_WIDGET (range)),
3138                 "gtk-keynav-cursor-only", &cursor_only,
3139                 NULL);
3140
3141   if (cursor_only)
3142     {
3143       GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (range));
3144
3145       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
3146         {
3147           if (scroll == GTK_SCROLL_STEP_UP ||
3148               scroll == GTK_SCROLL_STEP_DOWN)
3149             {
3150               if (toplevel)
3151                 gtk_widget_child_focus (toplevel,
3152                                         scroll == GTK_SCROLL_STEP_UP ?
3153                                         GTK_DIR_UP : GTK_DIR_DOWN);
3154               return;
3155             }
3156         }
3157       else
3158         {
3159           if (scroll == GTK_SCROLL_STEP_LEFT ||
3160               scroll == GTK_SCROLL_STEP_RIGHT)
3161             {
3162               if (toplevel)
3163                 gtk_widget_child_focus (toplevel,
3164                                         scroll == GTK_SCROLL_STEP_LEFT ?
3165                                         GTK_DIR_LEFT : GTK_DIR_RIGHT);
3166               return;
3167             }
3168         }
3169     }
3170
3171   if (! gtk_range_scroll (range, scroll))
3172     gtk_widget_error_bell (GTK_WIDGET (range));
3173
3174   /* Policy DELAYED makes sense with key events,
3175    * but DISCONTINUOUS doesn't, so we update immediately
3176    * for DISCONTINUOUS
3177    */
3178   if (priv->update_policy == GTK_UPDATE_DISCONTINUOUS)
3179     gtk_range_update_value (range);
3180 }
3181
3182 static void
3183 gtk_range_get_props (GtkRange  *range,
3184                      gint      *slider_width,
3185                      gint      *stepper_size,
3186                      gint      *focus_width,
3187                      gint      *trough_border,
3188                      gint      *stepper_spacing,
3189                      gboolean  *trough_under_steppers,
3190                      gint      *arrow_displacement_x,
3191                      gint      *arrow_displacement_y)
3192 {
3193   GtkWidget *widget =  GTK_WIDGET (range);
3194   gint tmp_slider_width, tmp_stepper_size, tmp_focus_width, tmp_trough_border;
3195   gint tmp_stepper_spacing, tmp_trough_under_steppers;
3196   gint tmp_arrow_displacement_x, tmp_arrow_displacement_y;
3197   
3198   gtk_widget_style_get (widget,
3199                         "slider-width", &tmp_slider_width,
3200                         "trough-border", &tmp_trough_border,
3201                         "stepper-size", &tmp_stepper_size,
3202                         "stepper-spacing", &tmp_stepper_spacing,
3203                         "trough-under-steppers", &tmp_trough_under_steppers,
3204                         "arrow-displacement-x", &tmp_arrow_displacement_x,
3205                         "arrow-displacement-y", &tmp_arrow_displacement_y,
3206                         NULL);
3207
3208   if (tmp_stepper_spacing > 0)
3209     tmp_trough_under_steppers = FALSE;
3210
3211   if (gtk_widget_get_can_focus (GTK_WIDGET (range)))
3212     {
3213       gint focus_line_width;
3214       gint focus_padding;
3215       
3216       gtk_widget_style_get (GTK_WIDGET (range),
3217                             "focus-line-width", &focus_line_width,
3218                             "focus-padding", &focus_padding,
3219                             NULL);
3220
3221       tmp_focus_width = focus_line_width + focus_padding;
3222     }
3223   else
3224     {
3225       tmp_focus_width = 0;
3226     }
3227   
3228   if (slider_width)
3229     *slider_width = tmp_slider_width;
3230
3231   if (focus_width)
3232     *focus_width = tmp_focus_width;
3233
3234   if (trough_border)
3235     *trough_border = tmp_trough_border;
3236
3237   if (stepper_size)
3238     *stepper_size = tmp_stepper_size;
3239
3240   if (stepper_spacing)
3241     *stepper_spacing = tmp_stepper_spacing;
3242
3243   if (trough_under_steppers)
3244     *trough_under_steppers = tmp_trough_under_steppers;
3245
3246   if (arrow_displacement_x)
3247     *arrow_displacement_x = tmp_arrow_displacement_x;
3248
3249   if (arrow_displacement_y)
3250     *arrow_displacement_y = tmp_arrow_displacement_y;
3251 }
3252
3253 #define POINT_IN_RECT(xcoord, ycoord, rect) \
3254  ((xcoord) >= (rect).x &&                   \
3255   (xcoord) <  ((rect).x + (rect).width) &&  \
3256   (ycoord) >= (rect).y &&                   \
3257   (ycoord) <  ((rect).y + (rect).height))
3258
3259 /* Update mouse location, return TRUE if it changes */
3260 static gboolean
3261 gtk_range_update_mouse_location (GtkRange *range)
3262 {
3263   GtkRangePrivate *priv = range->priv;
3264   GtkAllocation allocation;
3265   gint x, y;
3266   MouseLocation old;
3267   GtkWidget *widget = GTK_WIDGET (range);
3268
3269   old = priv->mouse_location;
3270
3271   x = priv->mouse_x;
3272   y = priv->mouse_y;
3273
3274   gtk_widget_get_allocation (widget, &allocation);
3275
3276   if (priv->grab_location != MOUSE_OUTSIDE)
3277     priv->mouse_location = priv->grab_location;
3278   else if (POINT_IN_RECT (x, y, priv->stepper_a))
3279     priv->mouse_location = MOUSE_STEPPER_A;
3280   else if (POINT_IN_RECT (x, y, priv->stepper_b))
3281     priv->mouse_location = MOUSE_STEPPER_B;
3282   else if (POINT_IN_RECT (x, y, priv->stepper_c))
3283     priv->mouse_location = MOUSE_STEPPER_C;
3284   else if (POINT_IN_RECT (x, y, priv->stepper_d))
3285     priv->mouse_location = MOUSE_STEPPER_D;
3286   else if (POINT_IN_RECT (x, y, priv->slider))
3287     priv->mouse_location = MOUSE_SLIDER;
3288   else if (POINT_IN_RECT (x, y, priv->trough))
3289     priv->mouse_location = MOUSE_TROUGH;
3290   else if (POINT_IN_RECT (x, y, allocation))
3291     priv->mouse_location = MOUSE_WIDGET;
3292   else
3293     priv->mouse_location = MOUSE_OUTSIDE;
3294
3295   return old != priv->mouse_location;
3296 }
3297
3298 /* Clamp rect, border inside widget->allocation, such that we prefer
3299  * to take space from border not rect in all directions, and prefer to
3300  * give space to border over rect in one direction.
3301  */
3302 static void
3303 clamp_dimensions (GtkWidget    *widget,
3304                   GdkRectangle *rect,
3305                   GtkBorder    *border,
3306                   gboolean      border_expands_horizontally)
3307 {
3308   GtkAllocation allocation;
3309   gint extra, shortage;
3310   
3311   g_return_if_fail (rect->x == 0);
3312   g_return_if_fail (rect->y == 0);  
3313   g_return_if_fail (rect->width >= 0);
3314   g_return_if_fail (rect->height >= 0);
3315
3316   gtk_widget_get_allocation (widget, &allocation);
3317
3318   /* Width */
3319
3320   extra = allocation.width - border->left - border->right - rect->width;
3321   if (extra > 0)
3322     {
3323       if (border_expands_horizontally)
3324         {
3325           border->left += extra / 2;
3326           border->right += extra / 2 + extra % 2;
3327         }
3328       else
3329         {
3330           rect->width += extra;
3331         }
3332     }
3333   
3334   /* See if we can fit rect, if not kill the border */
3335   shortage = rect->width - allocation.width;
3336   if (shortage > 0)
3337     {
3338       rect->width = allocation.width;
3339       /* lose the border */
3340       border->left = 0;
3341       border->right = 0;
3342     }
3343   else
3344     {
3345       /* See if we can fit rect with borders */
3346       shortage = rect->width + border->left + border->right - allocation.width;
3347       if (shortage > 0)
3348         {
3349           /* Shrink borders */
3350           border->left -= shortage / 2;
3351           border->right -= shortage / 2 + shortage % 2;
3352         }
3353     }
3354
3355   /* Height */
3356
3357   extra = allocation.height - border->top - border->bottom - rect->height;
3358   if (extra > 0)
3359     {
3360       if (border_expands_horizontally)
3361         {
3362           /* don't expand border vertically */
3363           rect->height += extra;
3364         }
3365       else
3366         {
3367           border->top += extra / 2;
3368           border->bottom += extra / 2 + extra % 2;
3369         }
3370     }
3371   
3372   /* See if we can fit rect, if not kill the border */
3373   shortage = rect->height - allocation.height;
3374   if (shortage > 0)
3375     {
3376       rect->height = allocation.height;
3377       /* lose the border */
3378       border->top = 0;
3379       border->bottom = 0;
3380     }
3381   else
3382     {
3383       /* See if we can fit rect with borders */
3384       shortage = rect->height + border->top + border->bottom - allocation.height;
3385       if (shortage > 0)
3386         {
3387           /* Shrink borders */
3388           border->top -= shortage / 2;
3389           border->bottom -= shortage / 2 + shortage % 2;
3390         }
3391     }
3392 }
3393
3394 static void
3395 gtk_range_calc_request (GtkRange      *range,
3396                         gint           slider_width,
3397                         gint           stepper_size,
3398                         gint           focus_width,
3399                         gint           trough_border,
3400                         gint           stepper_spacing,
3401                         GdkRectangle  *range_rect,
3402                         GtkBorder     *border,
3403                         gint          *n_steppers_p,
3404                         gboolean      *has_steppers_ab,
3405                         gboolean      *has_steppers_cd,
3406                         gint          *slider_length_p)
3407 {
3408   GtkRangePrivate *priv = range->priv;
3409   gint slider_length;
3410   gint n_steppers;
3411   gint n_steppers_ab;
3412   gint n_steppers_cd;
3413
3414   border->left = 0;
3415   border->right = 0;
3416   border->top = 0;
3417   border->bottom = 0;
3418
3419   if (GTK_RANGE_GET_CLASS (range)->get_range_border)
3420     GTK_RANGE_GET_CLASS (range)->get_range_border (range, border);
3421
3422   n_steppers_ab = 0;
3423   n_steppers_cd = 0;
3424
3425   if (priv->has_stepper_a)
3426     n_steppers_ab += 1;
3427   if (priv->has_stepper_b)
3428     n_steppers_ab += 1;
3429   if (priv->has_stepper_c)
3430     n_steppers_cd += 1;
3431   if (priv->has_stepper_d)
3432     n_steppers_cd += 1;
3433
3434   n_steppers = n_steppers_ab + n_steppers_cd;
3435
3436   slider_length = priv->min_slider_size;
3437
3438   range_rect->x = 0;
3439   range_rect->y = 0;
3440   
3441   /* We never expand to fill available space in the small dimension
3442    * (i.e. vertical scrollbars are always a fixed width)
3443    */
3444   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
3445     {
3446       range_rect->width = (focus_width + trough_border) * 2 + slider_width;
3447       range_rect->height = stepper_size * n_steppers + (focus_width + trough_border) * 2 + slider_length;
3448
3449       if (n_steppers_ab > 0)
3450         range_rect->height += stepper_spacing;
3451
3452       if (n_steppers_cd > 0)
3453         range_rect->height += stepper_spacing;
3454     }
3455   else
3456     {
3457       range_rect->width = stepper_size * n_steppers + (focus_width + trough_border) * 2 + slider_length;
3458       range_rect->height = (focus_width + trough_border) * 2 + slider_width;
3459
3460       if (n_steppers_ab > 0)
3461         range_rect->width += stepper_spacing;
3462
3463       if (n_steppers_cd > 0)
3464         range_rect->width += stepper_spacing;
3465     }
3466
3467   if (n_steppers_p)
3468     *n_steppers_p = n_steppers;
3469
3470   if (has_steppers_ab)
3471     *has_steppers_ab = (n_steppers_ab > 0);
3472
3473   if (has_steppers_cd)
3474     *has_steppers_cd = (n_steppers_cd > 0);
3475
3476   if (slider_length_p)
3477     *slider_length_p = slider_length;
3478 }
3479
3480 static void
3481 gtk_range_calc_layout (GtkRange *range,
3482                        gdouble   adjustment_value)
3483 {
3484   GtkRangePrivate *priv = range->priv;
3485   gint slider_width, stepper_size, focus_width, trough_border, stepper_spacing;
3486   gint slider_length;
3487   GtkBorder border;
3488   gint n_steppers;
3489   gboolean has_steppers_ab;
3490   gboolean has_steppers_cd;
3491   gboolean trough_under_steppers;
3492   GdkRectangle range_rect;
3493   GtkWidget *widget;
3494
3495   if (!priv->need_recalc)
3496     return;
3497
3498   /* If we have a too-small allocation, we prefer the steppers over
3499    * the trough/slider, probably the steppers are a more useful
3500    * feature in small spaces.
3501    *
3502    * Also, we prefer to draw the range itself rather than the border
3503    * areas if there's a conflict, since the borders will be decoration
3504    * not controls. Though this depends on subclasses cooperating by
3505    * not drawing on priv->range_rect.
3506    */
3507
3508   widget = GTK_WIDGET (range);
3509
3510   gtk_range_get_props (range,
3511                        &slider_width, &stepper_size,
3512                        &focus_width, &trough_border,
3513                        &stepper_spacing, &trough_under_steppers,
3514                        NULL, NULL);
3515
3516   gtk_range_calc_request (range, 
3517                           slider_width, stepper_size,
3518                           focus_width, trough_border, stepper_spacing,
3519                           &range_rect, &border, &n_steppers,
3520                           &has_steppers_ab, &has_steppers_cd, &slider_length);
3521   
3522   /* We never expand to fill available space in the small dimension
3523    * (i.e. vertical scrollbars are always a fixed width)
3524    */
3525   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
3526     {
3527       clamp_dimensions (widget, &range_rect, &border, TRUE);
3528     }
3529   else
3530     {
3531       clamp_dimensions (widget, &range_rect, &border, FALSE);
3532     }
3533   
3534   range_rect.x = border.left;
3535   range_rect.y = border.top;
3536
3537   priv->range_rect = range_rect;
3538
3539   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
3540     {
3541       gint stepper_width, stepper_height;
3542
3543       /* Steppers are the width of the range, and stepper_size in
3544        * height, or if we don't have enough height, divided equally
3545        * among available space.
3546        */
3547       stepper_width = range_rect.width - focus_width * 2;
3548
3549       if (trough_under_steppers)
3550         stepper_width -= trough_border * 2;
3551
3552       if (stepper_width < 1)
3553         stepper_width = range_rect.width; /* screw the trough border */
3554
3555       if (n_steppers == 0)
3556         stepper_height = 0; /* avoid divide by n_steppers */
3557       else
3558         stepper_height = MIN (stepper_size, (range_rect.height / n_steppers));
3559
3560       /* Stepper A */
3561       
3562       priv->stepper_a.x = range_rect.x + focus_width + trough_border * trough_under_steppers;
3563       priv->stepper_a.y = range_rect.y + focus_width + trough_border * trough_under_steppers;
3564
3565       if (priv->has_stepper_a)
3566         {
3567           priv->stepper_a.width = stepper_width;
3568           priv->stepper_a.height = stepper_height;
3569         }
3570       else
3571         {
3572           priv->stepper_a.width = 0;
3573           priv->stepper_a.height = 0;
3574         }
3575
3576       /* Stepper B */
3577       
3578       priv->stepper_b.x = priv->stepper_a.x;
3579       priv->stepper_b.y = priv->stepper_a.y + priv->stepper_a.height;
3580
3581       if (priv->has_stepper_b)
3582         {
3583           priv->stepper_b.width = stepper_width;
3584           priv->stepper_b.height = stepper_height;
3585         }
3586       else
3587         {
3588           priv->stepper_b.width = 0;
3589           priv->stepper_b.height = 0;
3590         }
3591
3592       /* Stepper D */
3593
3594       if (priv->has_stepper_d)
3595         {
3596           priv->stepper_d.width = stepper_width;
3597           priv->stepper_d.height = stepper_height;
3598         }
3599       else
3600         {
3601           priv->stepper_d.width = 0;
3602           priv->stepper_d.height = 0;
3603         }
3604       
3605       priv->stepper_d.x = priv->stepper_a.x;
3606       priv->stepper_d.y = range_rect.y + range_rect.height - priv->stepper_d.height - focus_width - trough_border * trough_under_steppers;
3607
3608       /* Stepper C */
3609
3610       if (priv->has_stepper_c)
3611         {
3612           priv->stepper_c.width = stepper_width;
3613           priv->stepper_c.height = stepper_height;
3614         }
3615       else
3616         {
3617           priv->stepper_c.width = 0;
3618           priv->stepper_c.height = 0;
3619         }
3620       
3621       priv->stepper_c.x = priv->stepper_a.x;
3622       priv->stepper_c.y = priv->stepper_d.y - priv->stepper_c.height;
3623
3624       /* Now the trough is the remaining space between steppers B and C,
3625        * if any, minus spacing
3626        */
3627       priv->trough.x = range_rect.x;
3628       priv->trough.y = priv->stepper_b.y + priv->stepper_b.height + stepper_spacing * has_steppers_ab;
3629       priv->trough.width = range_rect.width;
3630       priv->trough.height = priv->stepper_c.y - priv->trough.y - stepper_spacing * has_steppers_cd;
3631
3632       /* Slider fits into the trough, with stepper_spacing on either side,
3633        * and the size/position based on the adjustment or fixed, depending.
3634        */
3635       priv->slider.x = priv->trough.x + focus_width + trough_border;
3636       priv->slider.width = priv->trough.width - (focus_width + trough_border) * 2;
3637
3638       /* Compute slider position/length */
3639       {
3640         gint y, bottom, top, height;
3641         
3642         top = priv->trough.y;
3643         bottom = priv->trough.y + priv->trough.height;
3644
3645         if (! trough_under_steppers)
3646           {
3647             top += trough_border;
3648             bottom -= trough_border;
3649           }
3650
3651         /* slider height is the fraction (page_size /
3652          * total_adjustment_range) times the trough height in pixels
3653          */
3654
3655         if (priv->adjustment->upper - priv->adjustment->lower != 0)
3656           height = ((bottom - top) * (priv->adjustment->page_size /
3657                                        (priv->adjustment->upper - priv->adjustment->lower)));
3658         else
3659           height = priv->min_slider_size;
3660
3661         if (height < priv->min_slider_size ||
3662             priv->slider_size_fixed)
3663           height = priv->min_slider_size;
3664
3665         height = MIN (height, priv->trough.height);
3666         
3667         y = top;
3668
3669         if (priv->adjustment->upper - priv->adjustment->lower - priv->adjustment->page_size != 0)
3670           y += (bottom - top - height) * ((adjustment_value - priv->adjustment->lower) /
3671                                           (priv->adjustment->upper - priv->adjustment->lower - priv->adjustment->page_size));
3672
3673         y = CLAMP (y, top, bottom);
3674         
3675         if (should_invert (range))
3676           y = bottom - (y - top + height);
3677         
3678         priv->slider.y = y;
3679         priv->slider.height = height;
3680
3681         /* These are publically exported */
3682         priv->slider_start = priv->slider.y;
3683         priv->slider_end = priv->slider.y + priv->slider.height;
3684       }
3685     }
3686   else
3687     {
3688       gint stepper_width, stepper_height;
3689
3690       /* Steppers are the height of the range, and stepper_size in
3691        * width, or if we don't have enough width, divided equally
3692        * among available space.
3693        */
3694       stepper_height = range_rect.height + focus_width * 2;
3695
3696       if (trough_under_steppers)
3697         stepper_height -= trough_border * 2;
3698
3699       if (stepper_height < 1)
3700         stepper_height = range_rect.height; /* screw the trough border */
3701
3702       if (n_steppers == 0)
3703         stepper_width = 0; /* avoid divide by n_steppers */
3704       else
3705         stepper_width = MIN (stepper_size, (range_rect.width / n_steppers));
3706
3707       /* Stepper A */
3708       
3709       priv->stepper_a.x = range_rect.x + focus_width + trough_border * trough_under_steppers;
3710       priv->stepper_a.y = range_rect.y + focus_width + trough_border * trough_under_steppers;
3711
3712       if (priv->has_stepper_a)
3713         {
3714           priv->stepper_a.width = stepper_width;
3715           priv->stepper_a.height = stepper_height;
3716         }
3717       else
3718         {
3719           priv->stepper_a.width = 0;
3720           priv->stepper_a.height = 0;
3721         }
3722
3723       /* Stepper B */
3724       
3725       priv->stepper_b.x = priv->stepper_a.x + priv->stepper_a.width;
3726       priv->stepper_b.y = priv->stepper_a.y;
3727
3728       if (priv->has_stepper_b)
3729         {
3730           priv->stepper_b.width = stepper_width;
3731           priv->stepper_b.height = stepper_height;
3732         }
3733       else
3734         {
3735           priv->stepper_b.width = 0;
3736           priv->stepper_b.height = 0;
3737         }
3738
3739       /* Stepper D */
3740
3741       if (priv->has_stepper_d)
3742         {
3743           priv->stepper_d.width = stepper_width;
3744           priv->stepper_d.height = stepper_height;
3745         }
3746       else
3747         {
3748           priv->stepper_d.width = 0;
3749           priv->stepper_d.height = 0;
3750         }
3751
3752       priv->stepper_d.x = range_rect.x + range_rect.width - priv->stepper_d.width - focus_width - trough_border * trough_under_steppers;
3753       priv->stepper_d.y = priv->stepper_a.y;
3754
3755
3756       /* Stepper C */
3757
3758       if (priv->has_stepper_c)
3759         {
3760           priv->stepper_c.width = stepper_width;
3761           priv->stepper_c.height = stepper_height;
3762         }
3763       else
3764         {
3765           priv->stepper_c.width = 0;
3766           priv->stepper_c.height = 0;
3767         }
3768       
3769       priv->stepper_c.x = priv->stepper_d.x - priv->stepper_c.width;
3770       priv->stepper_c.y = priv->stepper_a.y;
3771
3772       /* Now the trough is the remaining space between steppers B and C,
3773        * if any
3774        */
3775       priv->trough.x = priv->stepper_b.x + priv->stepper_b.width + stepper_spacing * has_steppers_ab;
3776       priv->trough.y = range_rect.y;
3777
3778       priv->trough.width = priv->stepper_c.x - priv->trough.x - stepper_spacing * has_steppers_cd;
3779       priv->trough.height = range_rect.height;
3780
3781       /* Slider fits into the trough, with stepper_spacing on either side,
3782        * and the size/position based on the adjustment or fixed, depending.
3783        */
3784       priv->slider.y = priv->trough.y + focus_width + trough_border;
3785       priv->slider.height = priv->trough.height - (focus_width + trough_border) * 2;
3786
3787       /* Compute slider position/length */
3788       {
3789         gint x, left, right, width;
3790         
3791         left = priv->trough.x;
3792         right = priv->trough.x + priv->trough.width;
3793
3794         if (! trough_under_steppers)
3795           {
3796             left += trough_border;
3797             right -= trough_border;
3798           }
3799
3800         /* slider width is the fraction (page_size /
3801          * total_adjustment_range) times the trough width in pixels
3802          */
3803
3804         if (priv->adjustment->upper - priv->adjustment->lower != 0)
3805           width = ((right - left) * (priv->adjustment->page_size /
3806                                    (priv->adjustment->upper - priv->adjustment->lower)));
3807         else
3808           width = priv->min_slider_size;
3809
3810         if (width < priv->min_slider_size ||
3811             priv->slider_size_fixed)
3812           width = priv->min_slider_size;
3813
3814         width = MIN (width, priv->trough.width);
3815         
3816         x = left;
3817
3818         if (priv->adjustment->upper - priv->adjustment->lower - priv->adjustment->page_size != 0)
3819           x += (right - left - width) * ((adjustment_value - priv->adjustment->lower) /
3820                                          (priv->adjustment->upper - priv->adjustment->lower - priv->adjustment->page_size));
3821         
3822         x = CLAMP (x, left, right);
3823         
3824         if (should_invert (range))
3825           x = right - (x - left + width);
3826         
3827         priv->slider.x = x;
3828         priv->slider.width = width;
3829
3830         /* These are publically exported */
3831         priv->slider_start = priv->slider.x;
3832         priv->slider_end = priv->slider.x + priv->slider.width;
3833       }
3834     }
3835   
3836   gtk_range_update_mouse_location (range);
3837
3838   switch (priv->upper_sensitivity)
3839     {
3840     case GTK_SENSITIVITY_AUTO:
3841       priv->upper_sensitive =
3842         (priv->adjustment->value <
3843          (priv->adjustment->upper - priv->adjustment->page_size));
3844       break;
3845
3846     case GTK_SENSITIVITY_ON:
3847       priv->upper_sensitive = TRUE;
3848       break;
3849
3850     case GTK_SENSITIVITY_OFF:
3851       priv->upper_sensitive = FALSE;
3852       break;
3853     }
3854
3855   switch (priv->lower_sensitivity)
3856     {
3857     case GTK_SENSITIVITY_AUTO:
3858       priv->lower_sensitive =
3859         (priv->adjustment->value > priv->adjustment->lower);
3860       break;
3861
3862     case GTK_SENSITIVITY_ON:
3863       priv->lower_sensitive = TRUE;
3864       break;
3865
3866     case GTK_SENSITIVITY_OFF:
3867       priv->lower_sensitive = FALSE;
3868       break;
3869     }
3870 }
3871
3872 static GdkRectangle*
3873 get_area (GtkRange     *range,
3874           MouseLocation location)
3875 {
3876   GtkRangePrivate *priv = range->priv;
3877
3878   switch (location)
3879     {
3880     case MOUSE_STEPPER_A:
3881       return &priv->stepper_a;
3882     case MOUSE_STEPPER_B:
3883       return &priv->stepper_b;
3884     case MOUSE_STEPPER_C:
3885       return &priv->stepper_c;
3886     case MOUSE_STEPPER_D:
3887       return &priv->stepper_d;
3888     case MOUSE_TROUGH:
3889       return &priv->trough;
3890     case MOUSE_SLIDER:
3891       return &priv->slider;
3892     case MOUSE_WIDGET:
3893     case MOUSE_OUTSIDE:
3894       break;
3895     }
3896
3897   g_warning (G_STRLOC": bug");
3898   return NULL;
3899 }
3900
3901 static void
3902 gtk_range_calc_marks (GtkRange *range)
3903 {
3904   GtkRangePrivate *priv = range->priv;
3905   gint i;
3906
3907   if (!priv->recalc_marks)
3908     return;
3909
3910   priv->recalc_marks = FALSE;
3911
3912   for (i = 0; i < priv->n_marks; i++)
3913     {
3914       priv->need_recalc = TRUE;
3915       gtk_range_calc_layout (range, priv->marks[i]);
3916       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
3917         priv->mark_pos[i] = priv->slider.x + priv->slider.width / 2;
3918       else
3919         priv->mark_pos[i] = priv->slider.y + priv->slider.height / 2;
3920     }
3921
3922   priv->need_recalc = TRUE;
3923 }
3924
3925 static gboolean
3926 gtk_range_real_change_value (GtkRange     *range,
3927                              GtkScrollType scroll,
3928                              gdouble       value)
3929 {
3930   GtkRangePrivate *priv = range->priv;
3931
3932   /* potentially adjust the bounds _before_ we clamp */
3933   g_signal_emit (range, signals[ADJUST_BOUNDS], 0, value);
3934
3935   if (priv->restrict_to_fill_level)
3936     value = MIN (value, MAX (priv->adjustment->lower,
3937                              priv->fill_level));
3938
3939   value = CLAMP (value, priv->adjustment->lower,
3940                  (priv->adjustment->upper - priv->adjustment->page_size));
3941
3942   if (priv->round_digits >= 0)
3943     {
3944       gdouble power;
3945       gint i;
3946
3947       i = priv->round_digits;
3948       power = 1;
3949       while (i--)
3950         power *= 10;
3951       
3952       value = floor ((value * power) + 0.5) / power;
3953     }
3954
3955   if (priv->adjustment->value != value)
3956     {
3957       priv->need_recalc = TRUE;
3958
3959       gtk_widget_queue_draw (GTK_WIDGET (range));
3960
3961       switch (priv->update_policy)
3962         {
3963         case GTK_UPDATE_CONTINUOUS:
3964           gtk_adjustment_set_value (priv->adjustment, value);
3965           break;
3966
3967           /* Delayed means we update after a period of inactivity */
3968         case GTK_UPDATE_DELAYED:
3969           gtk_range_reset_update_timer (range);
3970           /* FALL THRU */
3971
3972           /* Discontinuous means we update on button release */
3973         case GTK_UPDATE_DISCONTINUOUS:
3974           /* don't emit value_changed signal */
3975           priv->adjustment->value = value;
3976           priv->update_pending = TRUE;
3977           break;
3978         }
3979     }
3980   return FALSE;
3981 }
3982
3983 static void
3984 gtk_range_update_value (GtkRange *range)
3985 {
3986   GtkRangePrivate *priv = range->priv;
3987
3988   gtk_range_remove_update_timer (range);
3989
3990   if (priv->update_pending)
3991     {
3992       gtk_adjustment_value_changed (priv->adjustment);
3993
3994       priv->update_pending = FALSE;
3995     }
3996 }
3997
3998 struct _GtkRangeStepTimer
3999 {
4000   guint timeout_id;
4001   GtkScrollType step;
4002 };
4003
4004 static gboolean
4005 second_timeout (gpointer data)
4006 {
4007   GtkRange *range = GTK_RANGE (data);
4008   GtkRangePrivate *priv = range->priv;
4009
4010   gtk_range_scroll (range, priv->timer->step);
4011
4012   return TRUE;
4013 }
4014
4015 static gboolean
4016 initial_timeout (gpointer data)
4017 {
4018   GtkRange *range = GTK_RANGE (data);
4019   GtkRangePrivate *priv = range->priv;
4020   GtkSettings *settings;
4021   guint        timeout;
4022
4023   settings = gtk_widget_get_settings (GTK_WIDGET (data));
4024   g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
4025
4026   priv->timer->timeout_id = gdk_threads_add_timeout (timeout * SCROLL_DELAY_FACTOR,
4027                                             second_timeout,
4028                                             range);
4029   /* remove self */
4030   return FALSE;
4031 }
4032
4033 static void
4034 gtk_range_add_step_timer (GtkRange      *range,
4035                           GtkScrollType  step)
4036 {
4037   GtkRangePrivate *priv = range->priv;
4038   GtkSettings *settings;
4039   guint        timeout;
4040
4041   g_return_if_fail (priv->timer == NULL);
4042   g_return_if_fail (step != GTK_SCROLL_NONE);
4043
4044   settings = gtk_widget_get_settings (GTK_WIDGET (range));
4045   g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
4046
4047   priv->timer = g_new (GtkRangeStepTimer, 1);
4048
4049   priv->timer->timeout_id = gdk_threads_add_timeout (timeout,
4050                                             initial_timeout,
4051                                             range);
4052   priv->timer->step = step;
4053
4054   gtk_range_scroll (range, priv->timer->step);
4055 }
4056
4057 static void
4058 gtk_range_remove_step_timer (GtkRange *range)
4059 {
4060   GtkRangePrivate *priv = range->priv;
4061
4062   if (priv->timer)
4063     {
4064       if (priv->timer->timeout_id != 0)
4065         g_source_remove (priv->timer->timeout_id);
4066
4067       g_free (priv->timer);
4068
4069       priv->timer = NULL;
4070     }
4071 }
4072
4073 static gboolean
4074 update_timeout (gpointer data)
4075 {
4076   GtkRange *range = GTK_RANGE (data);
4077   GtkRangePrivate *priv = range->priv;
4078
4079   gtk_range_update_value (range);
4080   priv->update_timeout_id = 0;
4081
4082   /* self-remove */
4083   return FALSE;
4084 }
4085
4086 static void
4087 gtk_range_reset_update_timer (GtkRange *range)
4088 {
4089   GtkRangePrivate *priv = range->priv;
4090
4091   gtk_range_remove_update_timer (range);
4092
4093   priv->update_timeout_id = gdk_threads_add_timeout (UPDATE_DELAY,
4094                                             update_timeout,
4095                                             range);
4096 }
4097
4098 static void
4099 gtk_range_remove_update_timer (GtkRange *range)
4100 {
4101   GtkRangePrivate *priv = range->priv;
4102
4103   if (priv->update_timeout_id != 0)
4104     {
4105       g_source_remove (priv->update_timeout_id);
4106       priv->update_timeout_id = 0;
4107     }
4108 }
4109
4110 void
4111 _gtk_range_set_stop_values (GtkRange *range,
4112                             gdouble  *values,
4113                             gint      n_values)
4114 {
4115   GtkRangePrivate *priv = range->priv;
4116   gint i;
4117
4118   g_free (priv->marks);
4119   priv->marks = g_new (gdouble, n_values);
4120
4121   g_free (priv->mark_pos);
4122   priv->mark_pos = g_new (gint, n_values);
4123
4124   priv->n_marks = n_values;
4125
4126   for (i = 0; i < n_values; i++) 
4127     priv->marks[i] = values[i];
4128
4129   priv->recalc_marks = TRUE;
4130 }
4131
4132 gint
4133 _gtk_range_get_stop_positions (GtkRange  *range,
4134                                gint     **values)
4135 {
4136   GtkRangePrivate *priv = range->priv;
4137
4138   gtk_range_calc_marks (range);
4139
4140   if (values)
4141     *values = g_memdup (priv->mark_pos, priv->n_marks * sizeof (gint));
4142
4143   return priv->n_marks;
4144 }
4145
4146 void
4147 _gtk_range_set_round_digits (GtkRange *range,
4148                              gint     round_digits)
4149 {
4150   range->priv->round_digits = round_digits;
4151 }
4152
4153 void
4154 _gtk_range_set_steppers (GtkRange      *range,
4155                          gboolean       has_a,
4156                          gboolean       has_b,
4157                          gboolean       has_c,
4158                          gboolean       has_d)
4159 {
4160   range->priv->has_stepper_a = has_a;
4161   range->priv->has_stepper_b = has_b;
4162   range->priv->has_stepper_c = has_c;
4163   range->priv->has_stepper_d = has_d;
4164 }