gtkrange.c 66 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3
 * Copyright (C) 2001 Red Hat, Inc.
Elliot Lee's avatar
Elliot Lee committed
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
Elliot Lee's avatar
Elliot Lee committed
7 8 9 10 11 12
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16 17 18
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
19
 */
20 21

/*
22
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23 24 25 26 27
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

Elliot Lee's avatar
Elliot Lee committed
28
#include <stdio.h>
29
#include "gtkintl.h"
Elliot Lee's avatar
Elliot Lee committed
30 31 32
#include "gtkmain.h"
#include "gtkrange.h"
#include "gtksignal.h"
Alexander Larsson's avatar
Alexander Larsson committed
33
#include "gtkintl.h"
Elliot Lee's avatar
Elliot Lee committed
34

35 36
#define SCROLL_INITIAL_DELAY 250  /* must hold button this long before ... */
#define SCROLL_LATER_DELAY   100  /* ... it starts repeating at this rate  */
37
#define UPDATE_DELAY         300  /* Delay for queued update */
Elliot Lee's avatar
Elliot Lee committed
38

39
enum {
Alexander Larsson's avatar
Alexander Larsson committed
40
  PROP_0,
41
  PROP_UPDATE_POLICY,
Havoc Pennington's avatar
Havoc Pennington committed
42 43
  PROP_ADJUSTMENT,
  PROP_INVERTED
44
};
Elliot Lee's avatar
Elliot Lee committed
45

46
enum {
47
  VALUE_CHANGED,
48 49 50 51
  MOVE_SLIDER,
  LAST_SIGNAL
};

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
typedef enum {
  MOUSE_OUTSIDE,
  MOUSE_STEPPER_A,
  MOUSE_STEPPER_B,
  MOUSE_STEPPER_C,
  MOUSE_STEPPER_D,
  MOUSE_TROUGH,
  MOUSE_SLIDER,
  MOUSE_WIDGET /* inside widget but not in any of the above GUI elements */
} MouseLocation;

struct _GtkRangeLayout
{
  /* These are in widget->window coordinates */
  GdkRectangle stepper_a;
  GdkRectangle stepper_b;
  GdkRectangle stepper_c;
  GdkRectangle stepper_d;
  /* The trough rectangle is the area the thumb can slide in, not the
   * entire range_rect
   */
  GdkRectangle trough;
  GdkRectangle slider;

  /* Layout-related state */
  
  MouseLocation mouse_location;
  /* last mouse coords we got, or -1 if mouse is outside the range */
  gint mouse_x;
  gint mouse_y;
  /* "grabbed" mouse location, OUTSIDE for no grab */
  MouseLocation grab_location;
  gint grab_button; /* 0 if none */
};

static void gtk_range_class_init     (GtkRangeClass    *klass);
static void gtk_range_init           (GtkRange         *range);
static void gtk_range_set_property   (GObject          *object,
                                      guint             prop_id,
                                      const GValue     *value,
                                      GParamSpec       *pspec);
static void gtk_range_get_property   (GObject          *object,
                                      guint             prop_id,
                                      GValue           *value,
                                      GParamSpec       *pspec);
static void gtk_range_destroy        (GtkObject        *object);
static void gtk_range_finalize       (GObject          *object);
static void gtk_range_size_request   (GtkWidget        *widget,
                                      GtkRequisition   *requisition);
static void gtk_range_size_allocate  (GtkWidget        *widget,
                                      GtkAllocation    *allocation);
static void gtk_range_realize        (GtkWidget        *widget);
static void gtk_range_unrealize      (GtkWidget        *widget);
static gint gtk_range_expose         (GtkWidget        *widget,
                                      GdkEventExpose   *event);
static gint gtk_range_button_press   (GtkWidget        *widget,
                                      GdkEventButton   *event);
static gint gtk_range_button_release (GtkWidget        *widget,
                                      GdkEventButton   *event);
static gint gtk_range_motion_notify  (GtkWidget        *widget,
                                      GdkEventMotion   *event);
static gint gtk_range_enter_notify   (GtkWidget        *widget,
                                      GdkEventCrossing *event);
static gint gtk_range_leave_notify   (GtkWidget        *widget,
                                      GdkEventCrossing *event);
static gint gtk_range_scroll_event   (GtkWidget        *widget,
                                      GdkEventScroll   *event);
static void gtk_range_style_set      (GtkWidget        *widget,
                                      GtkStyle         *previous_style);


/* Range methods */
124

125
static void gtk_range_move_slider              (GtkRange         *range,
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
                                                GtkScrollType     scroll);

/* Internals */
static void          gtk_range_scroll                   (GtkRange      *range,
                                                         GtkScrollType  scroll);
static gboolean      gtk_range_update_mouse_location    (GtkRange      *range);
static void          gtk_range_calc_layout              (GtkRange      *range);
static void          gtk_range_get_props                (GtkRange      *range,
                                                         gint          *slider_width,
                                                         gint          *stepper_size,
                                                         gint          *trough_border,
                                                         gint          *stepper_spacing);
static void          gtk_range_calc_request             (GtkRange      *range,
                                                         gint           slider_width,
                                                         gint           stepper_size,
                                                         gint           trough_border,
                                                         gint           stepper_spacing,
                                                         GdkRectangle  *range_rect,
                                                         GtkBorder     *border,
                                                         gint          *n_steppers_p,
                                                         gint          *slider_length_p);
static void          gtk_range_adjustment_value_changed (GtkAdjustment *adjustment,
                                                         gpointer       data);
static void          gtk_range_adjustment_changed       (GtkAdjustment *adjustment,
                                                         gpointer       data);
static void          gtk_range_add_step_timer           (GtkRange      *range,
                                                         GtkScrollType  step);
static void          gtk_range_remove_step_timer        (GtkRange      *range);
static void          gtk_range_reset_update_timer       (GtkRange      *range);
static void          gtk_range_remove_update_timer      (GtkRange      *range);
static GdkRectangle* get_area                           (GtkRange      *range,
                                                         MouseLocation  location);
static void          gtk_range_internal_set_value       (GtkRange      *range,
                                                         gdouble        value);
static void          gtk_range_update_value             (GtkRange      *range);

Elliot Lee's avatar
Elliot Lee committed
162 163

static GtkWidgetClass *parent_class = NULL;
164
static guint signals[LAST_SIGNAL];
Elliot Lee's avatar
Elliot Lee committed
165 166


167
GtkType
168
gtk_range_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
169
{
170
  static GtkType range_type = 0;
Elliot Lee's avatar
Elliot Lee committed
171 172 173

  if (!range_type)
    {
174
      static const GtkTypeInfo range_info =
Elliot Lee's avatar
Elliot Lee committed
175 176 177 178 179 180
      {
	"GtkRange",
	sizeof (GtkRange),
	sizeof (GtkRangeClass),
	(GtkClassInitFunc) gtk_range_class_init,
	(GtkObjectInitFunc) gtk_range_init,
181 182
	/* reserved_1 */ NULL,
        /* reserved_2 */ NULL,
183
        (GtkClassInitFunc) NULL,
Elliot Lee's avatar
Elliot Lee committed
184 185
      };

186
      range_type = gtk_type_unique (GTK_TYPE_WIDGET, &range_info);
Elliot Lee's avatar
Elliot Lee committed
187 188 189 190 191 192 193 194
    }

  return range_type;
}

static void
gtk_range_class_init (GtkRangeClass *class)
{
Alexander Larsson's avatar
Alexander Larsson committed
195
  GObjectClass   *gobject_class;
Elliot Lee's avatar
Elliot Lee committed
196 197 198
  GtkObjectClass *object_class;
  GtkWidgetClass *widget_class;

Alexander Larsson's avatar
Alexander Larsson committed
199
  gobject_class = G_OBJECT_CLASS (class);
Elliot Lee's avatar
Elliot Lee committed
200 201 202
  object_class = (GtkObjectClass*) class;
  widget_class = (GtkWidgetClass*) class;

203
  parent_class = g_type_class_peek_parent (class);
Elliot Lee's avatar
Elliot Lee committed
204

Alexander Larsson's avatar
Alexander Larsson committed
205 206
  gobject_class->set_property = gtk_range_set_property;
  gobject_class->get_property = gtk_range_get_property;
207
  gobject_class->finalize = gtk_range_finalize;
208
  object_class->destroy = gtk_range_destroy;
Elliot Lee's avatar
Elliot Lee committed
209

210 211 212 213
  widget_class->size_request = gtk_range_size_request;
  widget_class->size_allocate = gtk_range_size_allocate;
  widget_class->realize = gtk_range_realize;
  widget_class->unrealize = gtk_range_unrealize;  
Elliot Lee's avatar
Elliot Lee committed
214 215 216 217
  widget_class->expose_event = gtk_range_expose;
  widget_class->button_press_event = gtk_range_button_press;
  widget_class->button_release_event = gtk_range_button_release;
  widget_class->motion_notify_event = gtk_range_motion_notify;
218
  widget_class->scroll_event = gtk_range_scroll_event;
Elliot Lee's avatar
Elliot Lee committed
219 220
  widget_class->enter_notify_event = gtk_range_enter_notify;
  widget_class->leave_notify_event = gtk_range_leave_notify;
221
  widget_class->style_set = gtk_range_style_set;
222 223

  class->move_slider = gtk_range_move_slider;
224

225 226
  class->slider_detail = "slider";
  class->stepper_detail = "stepper";
227 228 229 230 231 232 233 234 235

  signals[VALUE_CHANGED] =
    g_signal_newc ("value_changed",
		   G_TYPE_FROM_CLASS (object_class),
		   G_SIGNAL_RUN_LAST,
		   G_STRUCT_OFFSET (GtkRangeClass, value_changed),
		   NULL, NULL,
		   gtk_marshal_NONE__NONE,
		   G_TYPE_NONE, 0);
236
  
237 238 239 240 241 242
  signals[MOVE_SLIDER] =
    g_signal_newc ("move_slider",
                   G_TYPE_FROM_CLASS (object_class),
                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                   G_STRUCT_OFFSET (GtkRangeClass, move_slider),
                   NULL, NULL,
243 244 245
                   gtk_marshal_VOID__ENUM,
                   G_TYPE_NONE, 1,
                   GTK_TYPE_SCROLL_TYPE);
246 247

  
Alexander Larsson's avatar
Alexander Larsson committed
248 249 250 251 252 253 254
  g_object_class_install_property (gobject_class,
                                   PROP_UPDATE_POLICY,
                                   g_param_spec_enum ("update_policy",
						      _("Update policy"),
						      _("How the range should be updated on the screen"),
						      GTK_TYPE_UPDATE_TYPE,
						      GTK_UPDATE_CONTINUOUS,
255 256
						      G_PARAM_READWRITE));
  
257 258 259 260 261 262
  g_object_class_install_property (gobject_class,
                                   PROP_ADJUSTMENT,
                                   g_param_spec_object ("adjustment",
							_("Adjustment"),
							_("The GtkAdjustment that contains the current value of this range object"),
                                                        GTK_TYPE_ADJUSTMENT,
263
                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
264

Havoc Pennington's avatar
Havoc Pennington committed
265 266 267 268 269 270 271 272
  g_object_class_install_property (gobject_class,
                                   PROP_INVERTED,
                                   g_param_spec_boolean ("inverted",
							_("Inverted"),
							_("Invert direction slider moves to increase range value"),
                                                         FALSE,
                                                         G_PARAM_READWRITE));
  
273 274 275 276 277 278
  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_int ("slider_width",
							     _("Slider Width"),
							     _("Width of scrollbar or scale thumb"),
							     0,
							     G_MAXINT,
279
							     14,
280 281 282
							     G_PARAM_READABLE));
  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_int ("trough_border",
283 284 285 286 287 288
                                                             _("Trough Border"),
                                                             _("Spacing between thumb/steppers and outer trough bevel"),
                                                             0,
                                                             G_MAXINT,
                                                             1,
                                                             G_PARAM_READABLE));
289 290 291
  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_int ("stepper_size",
							     _("Stepper Size"),
292
							     _("Length of step buttons at ends"),
293 294
							     0,
							     G_MAXINT,
295
							     14,
296 297 298 299 300
							     G_PARAM_READABLE));
  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_int ("stepper_spacing",
							     _("Stepper Spacing"),
							     _("Spacing between step buttons and thumb"),
301
                                                             0,
302
							     G_MAXINT,
303
							     0,
304
							     G_PARAM_READABLE));
Elliot Lee's avatar
Elliot Lee committed
305 306
}

307
static void
Alexander Larsson's avatar
Alexander Larsson committed
308 309 310 311
gtk_range_set_property (GObject      *object,
			guint         prop_id,
			const GValue *value,
			GParamSpec   *pspec)
312 313 314 315 316
{
  GtkRange *range;

  range = GTK_RANGE (object);

Alexander Larsson's avatar
Alexander Larsson committed
317
  switch (prop_id)
318
    {
Alexander Larsson's avatar
Alexander Larsson committed
319 320
    case PROP_UPDATE_POLICY:
      gtk_range_set_update_policy (range, g_value_get_enum (value));
321
      break;
322 323 324
    case PROP_ADJUSTMENT:
      gtk_range_set_adjustment (range, g_value_get_object (value));
      break;
Havoc Pennington's avatar
Havoc Pennington committed
325 326 327
    case PROP_INVERTED:
      gtk_range_set_inverted (range, g_value_get_boolean (value));
      break;
328
    default:
Alexander Larsson's avatar
Alexander Larsson committed
329
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
330 331 332 333 334
      break;
    }
}

static void
Alexander Larsson's avatar
Alexander Larsson committed
335 336 337 338
gtk_range_get_property (GObject      *object,
			guint         prop_id,
			GValue       *value,
			GParamSpec   *pspec)
339 340 341 342 343
{
  GtkRange *range;

  range = GTK_RANGE (object);

Alexander Larsson's avatar
Alexander Larsson committed
344
  switch (prop_id)
345
    {
Alexander Larsson's avatar
Alexander Larsson committed
346
    case PROP_UPDATE_POLICY:
347
      g_value_set_enum (value, range->update_policy);
348
      break;
349 350 351
    case PROP_ADJUSTMENT:
      g_value_set_object (value, G_OBJECT (range->adjustment));
      break;
Havoc Pennington's avatar
Havoc Pennington committed
352 353 354
    case PROP_INVERTED:
      g_value_set_boolean (value, range->inverted);
      break;
355
    default:
Alexander Larsson's avatar
Alexander Larsson committed
356
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
357 358 359 360
      break;
    }
}

Elliot Lee's avatar
Elliot Lee committed
361 362 363 364
static void
gtk_range_init (GtkRange *range)
{
  range->adjustment = NULL;
365 366 367 368 369 370 371 372 373 374 375 376 377 378
  range->update_policy = GTK_UPDATE_CONTINUOUS;
  range->inverted = FALSE;
  range->flippable = FALSE;
  range->min_slider_size = 1;
  range->has_stepper_a = FALSE;
  range->has_stepper_b = FALSE;
  range->has_stepper_c = FALSE;
  range->has_stepper_d = FALSE;
  range->need_recalc = TRUE;
  range->round_digits = -1;
  range->layout = g_new0 (GtkRangeLayout, 1);
  range->layout->grab_location = MOUSE_OUTSIDE;
  range->layout->grab_button = 0;
  range->timer = NULL;  
Elliot Lee's avatar
Elliot Lee committed
379 380
}

381 382 383 384 385 386 387 388 389 390 391
/**
 * gtk_range_get_adjustment:
 * @range: a #GtkRange
 * 
 * Get the #GtkAdjustment which is the "model" object for #GtkRange.
 * See gtk_range_set_adjustment() for details.
 * The return value does not have a reference added, so should not
 * be unreferenced.
 * 
 * Return value: a #GtkAdjustment
 **/
Elliot Lee's avatar
Elliot Lee committed
392 393 394 395 396 397
GtkAdjustment*
gtk_range_get_adjustment (GtkRange *range)
{
  g_return_val_if_fail (range != NULL, NULL);
  g_return_val_if_fail (GTK_IS_RANGE (range), NULL);

398 399 400
  if (!range->adjustment)
    gtk_range_set_adjustment (range, NULL);

Elliot Lee's avatar
Elliot Lee committed
401 402 403
  return range->adjustment;
}

404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
/**
 * gtk_range_set_update_policy:
 * @range: a #GtkRange
 * @policy: update policy
 *
 * Sets the update policy for the range. #GTK_UPDATE_CONTINUOUS means that
 * anytime the range slider is moved, the range value will change and the
 * value_changed signal will be emitted. #GTK_UPDATE_DELAYED means that
 * the value will be updated after a brief timeout where no slider motion
 * occurs, so updates are spaced by a short time rather than
 * continuous. #GTK_UPDATE_DISCONTINUOUS means that the value will only
 * be updated when the user releases the button and ends the slider
 * drag operation.
 * 
 **/
Elliot Lee's avatar
Elliot Lee committed
419 420 421 422 423 424 425
void
gtk_range_set_update_policy (GtkRange      *range,
			     GtkUpdateType  policy)
{
  g_return_if_fail (range != NULL);
  g_return_if_fail (GTK_IS_RANGE (range));

426
  if (range->update_policy != policy)
Alexander Larsson's avatar
Alexander Larsson committed
427
    {
428
      range->update_policy = policy;
Alexander Larsson's avatar
Alexander Larsson committed
429 430
      g_object_notify (G_OBJECT (range), "update_policy");
    }
Elliot Lee's avatar
Elliot Lee committed
431 432
}

433 434 435 436 437 438 439 440 441 442 443 444 445 446
/**
 * gtk_range_set_adjustment:
 * @range: a #GtkRange
 * @adjustment: a #GtkAdjustment
 *
 * Sets the adjustment to be used as the "model" object for this range
 * widget. The adjustment indicates the current range value, the
 * minimum and maximum range values, the step/page increments used
 * for keybindings and scrolling, and the page size. The page size
 * is normally 0 for #GtkScale and nonzero for #GtkScrollbar, and
 * indicates the size of the visible area of the widget being scrolled.
 * The page size affects the size of the scrollbar slider.
 * 
 **/
Elliot Lee's avatar
Elliot Lee committed
447 448 449 450 451 452
void
gtk_range_set_adjustment (GtkRange      *range,
			  GtkAdjustment *adjustment)
{
  g_return_if_fail (range != NULL);
  g_return_if_fail (GTK_IS_RANGE (range));
453 454 455 456 457
  
  if (!adjustment)
    adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
  else
    g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
Elliot Lee's avatar
Elliot Lee committed
458

459
  if (range->adjustment != adjustment)
Elliot Lee's avatar
Elliot Lee committed
460
    {
461 462
      if (range->adjustment)
	{
463 464 465 466 467 468
	  gtk_signal_disconnect_by_func (GTK_OBJECT (range->adjustment),
                                         G_CALLBACK (gtk_range_adjustment_changed),
					 range);
          gtk_signal_disconnect_by_func (GTK_OBJECT (range->adjustment),
                                         G_CALLBACK (gtk_range_adjustment_value_changed),
					 range);
469 470
	  gtk_object_unref (GTK_OBJECT (range->adjustment));
	}
471

472
      range->adjustment = adjustment;
473 474 475 476 477
      gtk_object_ref (GTK_OBJECT (adjustment));
      gtk_object_sink (GTK_OBJECT (adjustment));
      
      gtk_signal_connect (GTK_OBJECT (adjustment), "changed",
			  (GtkSignalFunc) gtk_range_adjustment_changed,
478
			  range);
479 480
      gtk_signal_connect (GTK_OBJECT (adjustment), "value_changed",
			  (GtkSignalFunc) gtk_range_adjustment_value_changed,
481
			  range);
482
      
483
      gtk_range_adjustment_changed (adjustment, range);
484
      g_object_notify (G_OBJECT (range), "adjustment");
Elliot Lee's avatar
Elliot Lee committed
485 486 487
    }
}

488 489 490 491 492 493 494 495 496 497 498
/**
 * gtk_range_set_inverted:
 * @range: a #GtkRange
 * @setting: %TRUE to invert the range
 *
 * Ranges normally move from lower to higher values as the
 * slider moves from top to bottom or left to right. Inverted
 * ranges have higher values at the top or on the right rather than
 * on the bottom or left.
 * 
 **/
499 500 501 502 503 504 505 506 507 508 509
void
gtk_range_set_inverted (GtkRange *range,
                        gboolean  setting)
{
  g_return_if_fail (GTK_IS_RANGE (range));
  
  setting = setting != FALSE;

  if (setting != range->inverted)
    {
      range->inverted = setting;
Havoc Pennington's avatar
Havoc Pennington committed
510
      g_object_notify (G_OBJECT (range), "inverted");
511 512 513 514
      gtk_widget_queue_resize (GTK_WIDGET (range));
    }
}

515 516 517 518 519 520 521 522
/**
 * gtk_range_get_inverted:
 * @range: a #GtkRange
 * 
 * Gets the value set by gtk_range_set_inverted().
 * 
 * Return value: %TRUE if the range is inverted
 **/
523 524 525 526 527 528 529 530
gboolean
gtk_range_get_inverted (GtkRange *range)
{
  g_return_val_if_fail (GTK_IS_RANGE (range), FALSE);

  return range->inverted;
}

531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
/**
 * gtk_range_set_increments:
 * @range: a #GtkRange
 * @step: step size
 * @page: page size
 *
 * Sets the step and page sizes for the range.
 * The step size is used when the user clicks the #GtkScrollbar
 * arrows or moves #GtkScale via arrow keys. The page size
 * is used for example when moving via Page Up or Page Down keys.
 * 
 **/
void
gtk_range_set_increments (GtkRange *range,
                          gdouble   step,
                          gdouble   page)
{
  g_return_if_fail (GTK_IS_RANGE (range));

  range->adjustment->step_increment = step;
  range->adjustment->page_increment = page;

  gtk_adjustment_changed (range->adjustment);
}

/**
 * gtk_range_set_range:
 * @range: a #GtkRange
 * @min: minimum range value
 * @max: maximum range value
 * 
 * Sets the allowable values in the #GtkRange, and clamps the range
 * value to be between min and max.
 **/
void
gtk_range_set_range (GtkRange *range,
                     gdouble   min,
                     gdouble   max)
{
  gdouble value;
  
  g_return_if_fail (GTK_IS_RANGE (range));
  g_return_if_fail (min < max);
  
  range->adjustment->lower = min;
  range->adjustment->upper = max;

  value = CLAMP (range->adjustment->value,
                 range->adjustment->lower,
                 (range->adjustment->upper - range->adjustment->page_size));
  
  gtk_adjustment_changed (range->adjustment);  
}

/**
 * gtk_range_set_value:
 * @range: a #GtkRange
 * @value: new value of the range
 *
 * Sets the current value of the range; if the value is outside the
 * minimum or maximum range values, it will be clamped to fit inside
 * them. The range emits the "value_changed" signal if the value
 * changes.
 * 
 **/
void
gtk_range_set_value (GtkRange *range,
                     gdouble   value)
{
  g_return_if_fail (GTK_IS_RANGE (range));
  
  value = CLAMP (value, range->adjustment->lower,
                 (range->adjustment->upper - range->adjustment->page_size));

  gtk_adjustment_set_value (range->adjustment, value);
}

/**
 * gtk_range_get_value:
 * @range: a #GtkRange
 * 
 * Gets the current value of the range.
 * 
 * Return value: current value of the range.
 **/
gdouble
gtk_range_get_value (GtkRange *range)
{
  g_return_val_if_fail (GTK_IS_RANGE (range), 0.0);

  return range->adjustment->value;
}

624 625 626 627 628 629 630 631 632 633
static gboolean
should_invert (GtkRange *range)
{  
  if (range->orientation == GTK_ORIENTATION_HORIZONTAL)
    return
      (range->inverted && !range->flippable) ||
      (range->inverted && range->flippable && gtk_widget_get_direction (GTK_WIDGET (range)) == GTK_TEXT_DIR_LTR) ||
      (!range->inverted && range->flippable && gtk_widget_get_direction (GTK_WIDGET (range)) == GTK_TEXT_DIR_RTL);
  else
    return range->inverted;
Elliot Lee's avatar
Elliot Lee committed
634 635
}

636 637
static void
gtk_range_finalize (GObject *object)
638
{
639
  GtkRange *range;
640

641
  g_return_if_fail (GTK_IS_RANGE (object));
642

643
  range = GTK_RANGE (object);
Elliot Lee's avatar
Elliot Lee committed
644

645 646 647
  g_free (range->layout);

  (* G_OBJECT_CLASS (parent_class)->finalize) (object);
Elliot Lee's avatar
Elliot Lee committed
648 649
}

650 651
static void
gtk_range_destroy (GtkObject *object)
Elliot Lee's avatar
Elliot Lee committed
652
{
653
  GtkRange *range;
Elliot Lee's avatar
Elliot Lee committed
654

655
  g_return_if_fail (GTK_IS_RANGE (object));
Elliot Lee's avatar
Elliot Lee committed
656

657 658 659 660 661 662 663 664 665 666 667 668 669
  range = GTK_RANGE (object);

  gtk_range_remove_step_timer (range);
  gtk_range_remove_update_timer (range);
  
  if (range->adjustment)
    {
      if (range->adjustment)
	gtk_signal_disconnect_by_data (GTK_OBJECT (range->adjustment),
				       (gpointer) range);
      gtk_object_unref (GTK_OBJECT (range->adjustment));
      range->adjustment = NULL;
    }
Elliot Lee's avatar
Elliot Lee committed
670

671
  (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
Elliot Lee's avatar
Elliot Lee committed
672 673
}

674 675 676
static void
gtk_range_size_request (GtkWidget      *widget,
                        GtkRequisition *requisition)
Elliot Lee's avatar
Elliot Lee committed
677
{
678 679 680 681 682 683 684 685 686 687 688 689 690
  GtkRange *range;
  gint slider_width, stepper_size, trough_border, stepper_spacing;
  GdkRectangle range_rect;
  GtkBorder border;
  
  range = GTK_RANGE (widget);
  
  gtk_range_get_props (range,
                       &slider_width, &stepper_size, &trough_border, &stepper_spacing);

  gtk_range_calc_request (range, 
                          slider_width, stepper_size, trough_border, stepper_spacing,
                          &range_rect, &border, NULL, NULL);
Elliot Lee's avatar
Elliot Lee committed
691

692 693
  requisition->width = range_rect.width + border.left + border.right;
  requisition->height = range_rect.height + border.top + border.bottom;
Elliot Lee's avatar
Elliot Lee committed
694 695
}

696 697 698
static void
gtk_range_size_allocate (GtkWidget     *widget,
                         GtkAllocation *allocation)
Elliot Lee's avatar
Elliot Lee committed
699
{
700 701 702 703 704 705
  GtkRange *range;

  range = GTK_RANGE (widget);

  range->need_recalc = TRUE;
  gtk_range_calc_layout (range);
Elliot Lee's avatar
Elliot Lee committed
706

707
  (* GTK_WIDGET_CLASS (parent_class)->size_allocate) (widget, allocation);
Elliot Lee's avatar
Elliot Lee committed
708 709
}

710 711
static void
gtk_range_realize (GtkWidget *widget)
Elliot Lee's avatar
Elliot Lee committed
712
{
713 714 715
  GtkRange *range;
  GdkWindowAttr attributes;
  gint attributes_mask;  
Elliot Lee's avatar
Elliot Lee committed
716

717
  range = GTK_RANGE (widget);
Elliot Lee's avatar
Elliot Lee committed
718

719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
  gtk_range_calc_layout (range);
  
  GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);

  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.x = widget->allocation.x;
  attributes.y = widget->allocation.y;
  attributes.width = widget->allocation.width;
  attributes.height = widget->allocation.height;
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.colormap = gtk_widget_get_colormap (widget);
  attributes.event_mask = gtk_widget_get_events (widget);
  attributes.event_mask |= (GDK_EXPOSURE_MASK |
			    GDK_BUTTON_PRESS_MASK |
			    GDK_BUTTON_RELEASE_MASK |
			    GDK_ENTER_NOTIFY_MASK |
			    GDK_LEAVE_NOTIFY_MASK |
                            GDK_POINTER_MOTION_MASK |
                            GDK_POINTER_MOTION_HINT_MASK);

  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;

  widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
  gdk_window_set_user_data (widget->window, range);

  widget->style = gtk_style_attach (widget->style, widget->window);
  gtk_style_set_background (widget->style, widget->window, widget->state);
Elliot Lee's avatar
Elliot Lee committed
747 748
}

749 750
static void
gtk_range_unrealize (GtkWidget *widget)
751
{
752 753 754 755
  GtkRange *range;

  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_RANGE (widget));
756

757
  range = GTK_RANGE (widget);
758

759 760 761 762 763
  gtk_range_remove_step_timer (range);
  gtk_range_remove_update_timer (range);
  
  if (GTK_WIDGET_CLASS (parent_class)->unrealize)
    (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
764 765 766
}

static void
767 768 769 770 771 772
draw_stepper (GtkRange     *range,
              GdkRectangle *rect,
              GtkArrowType  arrow_type,
              gboolean      clicked,
              gboolean      prelighted,
              GdkRectangle *area)
773
{
774 775 776
  GtkStateType state_type;
  GtkShadowType shadow_type;
  GdkRectangle intersection;
777

778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
  /* More to get the right clip region than for efficiency */
  if (!gdk_rectangle_intersect (area, rect, &intersection))
    return;
  
  if (!GTK_WIDGET_IS_SENSITIVE (range))
    state_type = GTK_STATE_INSENSITIVE;
  else if (clicked)
    state_type = GTK_STATE_ACTIVE;
  else if (prelighted)
    state_type = GTK_STATE_PRELIGHT;
  else 
    state_type = GTK_STATE_NORMAL;
  
  if (clicked)
    shadow_type = GTK_SHADOW_IN;
793
  else
794 795 796 797 798 799 800 801 802
    shadow_type = GTK_SHADOW_OUT;
  
  gtk_paint_arrow (GTK_WIDGET (range)->style,
                   GTK_WIDGET (range)->window,
                   state_type, shadow_type, 
                   &intersection, GTK_WIDGET (range),
                   GTK_RANGE_GET_CLASS (range)->stepper_detail,
                   arrow_type,
                   TRUE, rect->x, rect->y, rect->width, rect->height);
803 804
}

805 806 807
static gint
gtk_range_expose (GtkWidget      *widget,
		  GdkEventExpose *event)
Elliot Lee's avatar
Elliot Lee committed
808
{
809 810 811 812 813 814 815 816
  GtkRange *range;
  gboolean sensitive;
  GtkStateType state;
  GdkRectangle area;
  
  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_RANGE (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);
Elliot Lee's avatar
Elliot Lee committed
817

818
  range = GTK_RANGE (widget);
Elliot Lee's avatar
Elliot Lee committed
819

820
  gtk_range_calc_layout (range);
821

822 823 824 825 826 827 828 829 830 831 832
  sensitive = GTK_WIDGET_IS_SENSITIVE (widget);
  
  /* Just to be confusing, we draw the trough for the whole
   * range rectangle, not the trough rectangle (the trough
   * rectangle is just for hit detection)
   */
  /* The gdk_rectangle_intersect is more to get the right
   * clip region (limited to range_rect) than for efficiency
   */
  if (gdk_rectangle_intersect (&event->area, &range->range_rect,
                               &area))
Elliot Lee's avatar
Elliot Lee committed
833
    {
834 835 836 837 838 839 840 841 842
      gtk_paint_box (widget->style,
                     widget->window,
                     sensitive ? GTK_STATE_ACTIVE : GTK_STATE_INSENSITIVE,
                     GTK_SHADOW_IN,
                     &area, GTK_WIDGET(range), "trough",
                     range->range_rect.x,
                     range->range_rect.y,
                     range->range_rect.width,
                     range->range_rect.height);
843
      
844 845 846 847 848 849 850 851 852 853
                 
      if (sensitive &&
          GTK_WIDGET_HAS_FOCUS (range))
        gtk_paint_focus (widget->style,
                         widget->window,
                         &area, widget, "trough",
                         range->range_rect.x,
                         range->range_rect.y,
                         range->range_rect.width,
                         range->range_rect.height);
Elliot Lee's avatar
Elliot Lee committed
854 855
    }

856 857 858 859 860 861
  if (!sensitive)
    state = GTK_STATE_INSENSITIVE;
  else if (range->layout->mouse_location == MOUSE_SLIDER)
    state = GTK_STATE_PRELIGHT;
  else
    state = GTK_STATE_NORMAL;
862

863 864 865
  if (gdk_rectangle_intersect (&event->area,
                               &range->layout->slider,
                               &area))
Elliot Lee's avatar
Elliot Lee committed
866
    {
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
      gtk_paint_slider (widget->style,
                        widget->window,
                        state,
                        GTK_SHADOW_OUT,
                        &event->area,
                        widget,
                        GTK_RANGE_GET_CLASS (range)->slider_detail,
                        range->layout->slider.x,
                        range->layout->slider.y,
                        range->layout->slider.width,
                        range->layout->slider.height,
                        range->orientation);
    }
  
  if (range->has_stepper_a)
    draw_stepper (range, &range->layout->stepper_a,
                  range->orientation == GTK_ORIENTATION_VERTICAL ? GTK_ARROW_UP : GTK_ARROW_LEFT,
                  range->layout->grab_location == MOUSE_STEPPER_A,
                  range->layout->mouse_location == MOUSE_STEPPER_A,
                  &event->area);

  if (range->has_stepper_b)
    draw_stepper (range, &range->layout->stepper_b,
                  range->orientation == GTK_ORIENTATION_VERTICAL ? GTK_ARROW_DOWN : GTK_ARROW_RIGHT,
                  range->layout->grab_location == MOUSE_STEPPER_B,
                  range->layout->mouse_location == MOUSE_STEPPER_B,
                  &event->area);

  if (range->has_stepper_c)
    draw_stepper (range, &range->layout->stepper_c,
                  range->orientation == GTK_ORIENTATION_VERTICAL ? GTK_ARROW_UP : GTK_ARROW_LEFT,
                  range->layout->grab_location == MOUSE_STEPPER_C,
                  range->layout->mouse_location == MOUSE_STEPPER_C,
                  &event->area);

  if (range->has_stepper_d)
    draw_stepper (range, &range->layout->stepper_d,
                  range->orientation == GTK_ORIENTATION_VERTICAL ? GTK_ARROW_DOWN : GTK_ARROW_RIGHT,
                  range->layout->grab_location == MOUSE_STEPPER_D,
                  range->layout->mouse_location == MOUSE_STEPPER_D,
                  &event->area);
  
  return FALSE;
}
Elliot Lee's avatar
Elliot Lee committed
911

912 913 914 915 916 917
static void
range_grab_add (GtkRange      *range,
                MouseLocation  location,
                gint           button)
{
  /* we don't actually gtk_grab, since a button is down */
Elliot Lee's avatar
Elliot Lee committed
918

919 920 921 922 923
  range->layout->grab_location = location;
  range->layout->grab_button = button;
  
  if (gtk_range_update_mouse_location (range))
    gtk_widget_queue_draw (GTK_WIDGET (range));
Elliot Lee's avatar
Elliot Lee committed
924 925
}

926 927
static void
range_grab_remove (GtkRange *range)
Elliot Lee's avatar
Elliot Lee committed
928
{
929 930
  range->layout->grab_location = MOUSE_OUTSIDE;
  range->layout->grab_button = 0;
Elliot Lee's avatar
Elliot Lee committed
931

932 933 934
  if (gtk_range_update_mouse_location (range))
    gtk_widget_queue_draw (GTK_WIDGET (range));
}
Elliot Lee's avatar
Elliot Lee committed
935

936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
static GtkScrollType
range_get_scroll_for_grab (GtkRange      *range)
{  
  switch (range->layout->grab_location)
    {
      /* Backward stepper */
    case MOUSE_STEPPER_A:
    case MOUSE_STEPPER_C:
      switch (range->layout->grab_button)
        {
        case 1:
          return GTK_SCROLL_STEP_BACKWARD;
          break;
        case 2:
          return GTK_SCROLL_PAGE_BACKWARD;
          break;
        case 3:
          return GTK_SCROLL_START;
          break;
        }
      break;
Elliot Lee's avatar
Elliot Lee committed
957

958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
      /* Forward stepper */
    case MOUSE_STEPPER_B:
    case MOUSE_STEPPER_D:
      switch (range->layout->grab_button)
        {
        case 1:
          return GTK_SCROLL_STEP_FORWARD;
          break;
        case 2:
          return GTK_SCROLL_PAGE_FORWARD;
          break;
        case 3:
          return GTK_SCROLL_END;
          break;
        }
      break;
974

975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
      /* In the trough */
    case MOUSE_TROUGH:
      {
        if (range->trough_click_forward)
          {
            return range->layout->grab_button == 3
              ? GTK_SCROLL_PAGE_FORWARD : GTK_SCROLL_STEP_FORWARD;
          }
        else
          {
            return range->layout->grab_button == 3
              ? GTK_SCROLL_PAGE_BACKWARD : GTK_SCROLL_STEP_BACKWARD;
          }
      }
      break;
990

991 992 993 994
    case MOUSE_OUTSIDE:
    case MOUSE_SLIDER:
    case MOUSE_WIDGET:
      break;
Elliot Lee's avatar
Elliot Lee committed
995 996
    }

997
  return GTK_SCROLL_NONE;
Elliot Lee's avatar
Elliot Lee committed
998 999
}

1000 1001 1002
static gdouble
coord_to_value (GtkRange *range,
                gint      coord)
Elliot Lee's avatar
Elliot Lee committed
1003
{
1004 1005
  gdouble frac;
  gdouble value;
1006
  
1007 1008 1009 1010 1011 1012
  if (range->orientation == GTK_ORIENTATION_VERTICAL)
    frac = ((coord - range->layout->trough.y) /
            (gdouble) (range->layout->trough.height - range->layout->slider.height));
  else
    frac = ((coord - range->layout->trough.x) /
            (gdouble) (range->layout->trough.width - range->layout->slider.width));
1013

1014 1015
  if (should_invert (range))
    frac = 1.0 - frac;
1016
  
1017 1018
  value = range->adjustment->lower +
    frac * (range->adjustment->upper - range->adjustment->lower - range->adjustment->page_size);
Elliot Lee's avatar
Elliot Lee committed
1019

1020
  return value;
Elliot Lee's avatar
Elliot Lee committed
1021 1022
}

1023 1024 1025
static gint
gtk_range_button_press (GtkWidget      *widget,
			GdkEventButton *event)
Elliot Lee's avatar
Elliot Lee committed
1026
{
1027
  GtkRange *range;
1028

1029 1030
  g_return_val_if_fail (GTK_IS_RANGE (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);
Elliot Lee's avatar
Elliot Lee committed
1031

1032 1033 1034 1035
  range = GTK_RANGE (widget);
  
  if (!GTK_WIDGET_HAS_FOCUS (widget))
    gtk_widget_grab_focus (widget);
Elliot Lee's avatar
Elliot Lee committed
1036

1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
  /* ignore presses when we're already doing something else. */
  if (range->layout->grab_location != MOUSE_OUTSIDE)
    return FALSE;

  range->layout->mouse_x = event->x;
  range->layout->mouse_y = event->y;
  if (gtk_range_update_mouse_location (range))
    gtk_widget_queue_draw (widget);
    
  if (range->layout->mouse_location == MOUSE_TROUGH  &&
      (event->button == 1 || event->button == 3))
Elliot Lee's avatar
Elliot Lee committed
1048
    {
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
      /* button 1 steps by step increment, as with button 1 on a stepper,
       * button 3 steps by page increment, as with button 2 on a stepper
       */
      GtkScrollType scroll;
      gdouble click_value;
      
      click_value = coord_to_value (range,
                                    range->orientation == GTK_ORIENTATION_VERTICAL ?
                                    event->y : event->x);
      
      range->trough_click_forward = click_value > range->adjustment->value;
      range_grab_add (range, MOUSE_TROUGH, event->button);
      
      scroll = range_get_scroll_for_grab (range);
      
      gtk_range_add_step_timer (range, scroll);
Elliot Lee's avatar
Elliot Lee committed
1065

1066
      return TRUE;
Elliot Lee's avatar
Elliot Lee committed
1067
    }
1068 1069 1070 1071 1072
  else if ((range->layout->mouse_location == MOUSE_STEPPER_A ||
            range->layout->mouse_location == MOUSE_STEPPER_B ||
            range->layout->mouse_location == MOUSE_STEPPER_C ||
            range->layout->mouse_location == MOUSE_STEPPER_D) &&
           (event->button == 1 || event->button == 2 || event->button == 3))
Elliot Lee's avatar
Elliot Lee committed
1073
    {
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
      GdkRectangle *stepper_area;
      GtkScrollType scroll;
      
      range_grab_add (range, range->layout->mouse_location, event->button);

      stepper_area = get_area (range, range->layout->mouse_location);
      gtk_widget_queue_draw_area (widget,
                                  stepper_area->x,
                                  stepper_area->y,
                                  stepper_area->width,
                                  stepper_area->height);

      scroll = range_get_scroll_for_grab (range);
      if (scroll != GTK_SCROLL_NONE)
        gtk_range_add_step_timer (range, scroll);
      
      return TRUE;
    }
  else if ((range->layout->mouse_location == MOUSE_TROUGH &&
            event->button == 2) ||
           range->layout->mouse_location == MOUSE_SLIDER)
    {
      /* Any button can be used to drag the slider, but you can start
       * dragging the slider with a trough click using button 2;
       * On button 2 press, we warp the slider to mouse position,
       * then begin the slider drag.
       */
      if (event->button == 2)
        {
1103
          gdouble slider_low_value, slider_high_value;
1104
          
1105 1106 1107 1108 1109 1110 1111 1112 1113
          slider_high_value =
            coord_to_value (range,
                            range->orientation == GTK_ORIENTATION_VERTICAL ?
                            event->y : event->x);
          slider_low_value =
            coord_to_value (range,
                            range->orientation == GTK_ORIENTATION_VERTICAL ?
                            event->y - range->layout->slider.height :
                            event->x - range->layout->slider.width);
1114 1115
          
          /* middle button jumps to point */
1116 1117
          gtk_range_internal_set_value (range,
                                        slider_low_value + (slider_high_value - slider_low_value) / 2);
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129

          /* Calc layout so we can set slide_initial_slider_position
           * properly
           */
          gtk_range_calc_layout (range);
        }
      
      if (range->orientation == GTK_ORIENTATION_VERTICAL)
        {
          range->slide_initial_slider_position = range->layout->slider.y;
          range->slide_initial_coordinate = event->y;
        }
Elliot Lee's avatar
Elliot Lee committed
1130
      else
1131 1132 1133 1134 1135 1136 1137 1138
        {
          range->slide_initial_slider_position = range->layout->slider.x;
          range->slide_initial_coordinate = event->x;
        }

      range_grab_add (range, MOUSE_SLIDER, event->button);
      
      return TRUE;
Elliot Lee's avatar
Elliot Lee committed
1139
    }
1140 1141
  
  return FALSE;
Elliot Lee's avatar
Elliot Lee committed
1142 1143
}

1144 1145 1146 1147 1148
/* During a slide, move the slider as required given new mouse position */
static void
update_slider_position (GtkRange *range,
                        gint      mouse_x,
                        gint      mouse_y)
Elliot Lee's avatar
Elliot Lee committed
1149
{
1150 1151 1152 1153 1154 1155 1156 1157
  gint delta;
  gint c;
  gdouble new_value;
  
  if (range->orientation == GTK_ORIENTATION_VERTICAL)
    delta = mouse_y - range->slide_initial_coordinate;
  else
    delta = mouse_x - range->slide_initial_coordinate;
Elliot Lee's avatar
Elliot Lee committed
1158

1159
  c = range->slide_initial_slider_position + delta;
Elliot Lee's avatar
Elliot Lee committed
1160

1161 1162 1163 1164
  new_value = coord_to_value (range, c);
  
  gtk_range_internal_set_value (range, new_value);
}
Elliot Lee's avatar
Elliot Lee committed
1165

1166 1167 1168 1169 1170
static gint
gtk_range_button_release (GtkWidget      *widget,
			  GdkEventButton *event)
{
  GtkRange *range;
Elliot Lee's avatar
Elliot Lee committed
1171

1172 1173
  g_return_val_if_fail (GTK_IS_RANGE (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);
Elliot Lee's avatar
Elliot Lee committed
1174

1175
  range = GTK_RANGE (widget);
Elliot Lee's avatar
Elliot Lee committed
1176

1177 1178
  range->layout->mouse_x = event->x;
  range->layout->mouse_y = event->y;
1179
  
1180
  if (range->layout->grab_button == event->button)
Elliot Lee's avatar
Elliot Lee committed
1181
    {
1182 1183
      GtkScrollType scroll;
      MouseLocation grab_location;
Elliot Lee's avatar
Elliot Lee committed
1184

1185
      grab_location = range->layout->grab_location;
Elliot Lee's avatar
Elliot Lee committed
1186

1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
      scroll = range_get_scroll_for_grab (range);
      
      range_grab_remove (range);
      gtk_range_remove_step_timer (range);
      
      /* We only do the move if we're still on top of the button at
       * release
       */
      if (grab_location == range->layout->mouse_location &&
          scroll != GTK_SCROLL_NONE)
        {
          gtk_range_scroll (range, scroll);
        }
Elliot Lee's avatar
Elliot Lee committed
1200

1201 1202
      if (grab_location == MOUSE_SLIDER)
        update_slider_position (range, event->x, event->y);
Elliot Lee's avatar
Elliot Lee committed
1203

1204 1205 1206 1207 1208 1209 1210 1211 1212
      /* Flush any pending discontinuous/delayed updates */
      gtk_range_update_value (range);
      
      /* Just be lazy about this, if we scrolled it will all redraw anyway,
       * so no point optimizing the button deactivate case
       */
      gtk_widget_queue_draw (widget);
      
      return TRUE;
1213
    }
1214

Elliot Lee's avatar
Elliot Lee committed
1215 1216 1217 1218
  return FALSE;
}

static gint
1219 1220
gtk_range_scroll_event (GtkWidget      *widget,
			GdkEventScroll *event)
Elliot Lee's avatar
Elliot Lee committed
1221 1222 1223 1224 1225 1226 1227
{
  GtkRange *range;

  g_return_val_if_fail (GTK_IS_RANGE (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

  range = GTK_RANGE (widget);
1228 1229

  if (GTK_WIDGET_REALIZED (range))
Elliot Lee's avatar
Elliot Lee committed
1230
    {
1231 1232
      GtkAdjustment *adj = GTK_RANGE (range)->adjustment;
      gdouble new_value = adj->value + ((event->direction == GDK_SCROLL_UP ||
1233
                                         event->direction == GDK_SCROLL_LEFT) ? 
1234 1235
					-adj->page_increment / 2: 
					adj->page_increment / 2);
Elliot Lee's avatar
Elliot Lee committed
1236

1237
      gtk_range_internal_set_value (range, new_value);
Elliot Lee's avatar
Elliot Lee committed
1238

1239 1240 1241 1242 1243 1244
      /* Policy DELAYED makes sense with scroll events,
       * but DISCONTINUOUS doesn't, so we update immediately
       * for DISCONTINOUS
       */
      if (range->update_policy == GTK_UPDATE_DISCONTINUOUS)
        gtk_range_update_value (range);
Elliot Lee's avatar
Elliot Lee committed
1245 1246
    }

1247
  return TRUE;
Elliot Lee's avatar
Elliot Lee committed
1248 1249 1250
}

static gint
1251 1252
gtk_range_motion_notify (GtkWidget      *widget,
			 GdkEventMotion *event)
Elliot Lee's avatar
Elliot Lee committed
1253 1254
{
  GtkRange *range;
1255
  gint x, y;
Elliot Lee's avatar
Elliot Lee committed
1256 1257 1258 1259 1260 1261

  g_return_val_if_fail (GTK_IS_RANGE (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

  range = GTK_RANGE (widget);

1262 1263 1264 1265
  gdk_window_get_pointer (widget->window, &x, &y, NULL);
  
  range->layout->mouse_x = x;
  range->layout->mouse_y = y;
Elliot Lee's avatar
Elliot Lee committed
1266

1267 1268
  if (gtk_range_update_mouse_location (range))
    gtk_widget_queue_draw (widget);
Elliot Lee's avatar
Elliot Lee committed
1269

1270 1271
  if (range->layout->grab_location == MOUSE_SLIDER)
    update_slider_position (range, event->x, event->y);
Elliot Lee's avatar
Elliot Lee committed
1272

1273 1274
  /* We handled the event if the mouse was in the range_rect */
  return range->layout->mouse_location != MOUSE_OUTSIDE;
Elliot Lee's avatar
Elliot Lee committed
1275 1276
}

1277
static gint
1278 1279
gtk_range_enter_notify (GtkWidget        *widget,
			GdkEventCrossing *event)
1280 1281 1282 1283 1284 1285 1286 1287
{
  GtkRange *range;

  g_return_val_if_fail (GTK_IS_RANGE (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

  range = GTK_RANGE (widget);

1288 1289
  range->layout->mouse_x = event->x;
  range->layout->mouse_y = event->y;
1290

1291 1292 1293
  if (gtk_range_update_mouse_location (range))
    gtk_widget_queue_draw (widget);
  
1294
  return TRUE;
1295 1296
}

Elliot Lee's avatar
Elliot Lee committed
1297
static gint
1298 1299
gtk_range_leave_notify (GtkWidget        *widget,
			GdkEventCrossing *event)
Elliot Lee's avatar
Elliot Lee committed
1300 1301 1302 1303 1304 1305 1306 1307
{
  GtkRange *range;

  g_return_val_if_fail (GTK_IS_RANGE (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

  range = GTK_RANGE (widget);

Havoc Pennington's avatar