gtkadjustment.c 23.1 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Elliot Lee's avatar
Elliot Lee committed
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10 11
 * 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
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
16
 */
17 18

/*
19
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20 21 22 23 24
 * 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/. 
 */

25
#include "config.h"
Elliot Lee's avatar
Elliot Lee committed
26
#include "gtkadjustment.h"
27
#include "gtkmarshalers.h"
28 29
#include "gtkprivate.h"
#include "gtkintl.h"
30

Elliot Lee's avatar
Elliot Lee committed
31

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/**
 * SECTION:gtkadjustment
 * @Short_description: A representation of an adjustable bounded value
 * @Title: GtkAdjustment
 *
 * The #GtkAdjustment object represents a value which has an associated lower
 * and upper bound, together with step and page increments, and a page size.
 * It is used within several GTK+ widgets, including
 * #GtkSpinButton, #GtkViewport, and #GtkRange (which is a base class for
 * #GtkHScrollbar, #GtkVScrollbar, #GtkHScale, and #GtkVScale).
 *
 * The #GtkAdjustment object does not update the value itself. Instead
 * it is left up to the owner of the #GtkAdjustment to control the value.
 *
 * The owner of the #GtkAdjustment typically calls the
 * gtk_adjustment_value_changed() and gtk_adjustment_changed() functions
 * after changing the value and its bounds. This results in the emission of the
 * #GtkAdjustment::value_changed or #GtkAdjustment::changed signal respectively.
 */


53 54 55 56 57 58 59 60 61
struct _GtkAdjustmentPrivate {
  gdouble lower;
  gdouble upper;
  gdouble value;
  gdouble step_increment;
  gdouble page_increment;
  gdouble page_size;
};

62 63 64 65 66 67 68 69 70 71
enum
{
  PROP_0,
  PROP_VALUE,
  PROP_LOWER,
  PROP_UPPER,
  PROP_STEP_INCREMENT,
  PROP_PAGE_INCREMENT,
  PROP_PAGE_SIZE
};
Elliot Lee's avatar
Elliot Lee committed
72

Michael Natterer's avatar
Michael Natterer committed
73 74
enum
{
Elliot Lee's avatar
Elliot Lee committed
75 76 77 78 79 80
  CHANGED,
  VALUE_CHANGED,
  LAST_SIGNAL
};


Michael Natterer's avatar
Michael Natterer committed
81 82 83 84 85 86 87 88 89 90 91
static void gtk_adjustment_get_property                (GObject      *object,
                                                        guint         prop_id,
                                                        GValue       *value,
                                                        GParamSpec   *pspec);
static void gtk_adjustment_set_property                (GObject      *object,
                                                        guint         prop_id,
                                                        const GValue *value,
                                                        GParamSpec   *pspec);
static void gtk_adjustment_dispatch_properties_changed (GObject      *object,
                                                        guint         n_pspecs,
                                                        GParamSpec  **pspecs);
Elliot Lee's avatar
Elliot Lee committed
92

93
static guint adjustment_signals[LAST_SIGNAL] = { 0 };
Elliot Lee's avatar
Elliot Lee committed
94

95 96
static guint64 adjustment_changed_stamp = 0; /* protected by global gdk lock */

97
G_DEFINE_TYPE_WITH_PRIVATE (GtkAdjustment, gtk_adjustment, G_TYPE_INITIALLY_UNOWNED)
Elliot Lee's avatar
Elliot Lee committed
98 99 100 101

static void
gtk_adjustment_class_init (GtkAdjustmentClass *class)
{
102 103
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);

Michael Natterer's avatar
Michael Natterer committed
104 105 106
  gobject_class->set_property                = gtk_adjustment_set_property;
  gobject_class->get_property                = gtk_adjustment_get_property;
  gobject_class->dispatch_properties_changed = gtk_adjustment_dispatch_properties_changed;
107

108 109 110
  class->changed = NULL;
  class->value_changed = NULL;

111 112 113 114 115 116 117
  /**
   * GtkAdjustment:value:
   * 
   * The value of the adjustment.
   * 
   * Since: 2.4
   */
118 119 120 121 122 123 124 125
  g_object_class_install_property (gobject_class,
                                   PROP_VALUE,
                                   g_param_spec_double ("value",
							P_("Value"),
							P_("The value of the adjustment"),
							-G_MAXDOUBLE, 
							G_MAXDOUBLE, 
							0.0, 
126
							GTK_PARAM_READWRITE));
127
  
128 129 130 131 132 133 134
  /**
   * GtkAdjustment:lower:
   * 
   * The minimum value of the adjustment.
   * 
   * Since: 2.4
   */
135 136 137 138 139 140 141 142
  g_object_class_install_property (gobject_class,
                                   PROP_LOWER,
                                   g_param_spec_double ("lower",
							P_("Minimum Value"),
							P_("The minimum value of the adjustment"),
							-G_MAXDOUBLE, 
							G_MAXDOUBLE, 
							0.0,
143
							GTK_PARAM_READWRITE));
144
  
145 146 147 148 149
  /**
   * GtkAdjustment:upper:
   * 
   * The maximum value of the adjustment. 
   * Note that values will be restricted by 
150
   * `upper - page-size` if the page-size 
151 152 153 154
   * property is nonzero.
   *
   * Since: 2.4
   */
155 156 157 158 159 160 161 162
  g_object_class_install_property (gobject_class,
                                   PROP_UPPER,
                                   g_param_spec_double ("upper",
							P_("Maximum Value"),
							P_("The maximum value of the adjustment"),
							-G_MAXDOUBLE, 
							G_MAXDOUBLE, 
							0.0, 
163
							GTK_PARAM_READWRITE));
164
  
165 166 167 168 169 170 171
  /**
   * GtkAdjustment:step-increment:
   * 
   * The step increment of the adjustment.
   * 
   * Since: 2.4
   */
172 173 174 175 176 177 178 179
  g_object_class_install_property (gobject_class,
                                   PROP_STEP_INCREMENT,
                                   g_param_spec_double ("step-increment",
							P_("Step Increment"),
							P_("The step increment of the adjustment"),
							-G_MAXDOUBLE, 
							G_MAXDOUBLE, 
							0.0, 
180
							GTK_PARAM_READWRITE));
181
  
182 183 184 185 186 187 188
  /**
   * GtkAdjustment:page-increment:
   * 
   * The page increment of the adjustment.
   * 
   * Since: 2.4
   */
189 190 191 192 193 194 195 196
  g_object_class_install_property (gobject_class,
                                   PROP_PAGE_INCREMENT,
                                   g_param_spec_double ("page-increment",
							P_("Page Increment"),
							P_("The page increment of the adjustment"),
							-G_MAXDOUBLE, 
							G_MAXDOUBLE, 
							0.0, 
197
							GTK_PARAM_READWRITE));
198
  
199 200 201 202 203 204 205 206 207 208
  /**
   * GtkAdjustment:page-size:
   * 
   * The page size of the adjustment. 
   * Note that the page-size is irrelevant and should be set to zero
   * if the adjustment is used for a simple scalar value, e.g. in a 
   * #GtkSpinButton.
   * 
   * Since: 2.4
   */
209 210 211 212 213 214 215 216
  g_object_class_install_property (gobject_class,
                                   PROP_PAGE_SIZE,
                                   g_param_spec_double ("page-size",
							P_("Page Size"),
							P_("The page size of the adjustment"),
							-G_MAXDOUBLE, 
							G_MAXDOUBLE, 
							0.0, 
217
							GTK_PARAM_READWRITE));
218

219 220 221 222
  /**
   * GtkAdjustment::changed:
   * @adjustment: the object which received the signal.
   *
223 224
   * Emitted when one or more of the #GtkAdjustment properties have been
   * changed, other than the #GtkAdjustment:value property.
225
   */
Elliot Lee's avatar
Elliot Lee committed
226
  adjustment_signals[CHANGED] =
227
    g_signal_new (I_("changed"),
Manish Singh's avatar
Manish Singh committed
228 229 230 231 232 233
		  G_OBJECT_CLASS_TYPE (class),
		  G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
		  G_STRUCT_OFFSET (GtkAdjustmentClass, changed),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
Michael Natterer's avatar
Michael Natterer committed
234

235 236 237 238
  /**
   * GtkAdjustment::value-changed:
   * @adjustment: the object which received the signal.
   *
239
   * Emitted when the #GtkAdjustment:value property has been changed.
240
   */
Elliot Lee's avatar
Elliot Lee committed
241
  adjustment_signals[VALUE_CHANGED] =
242
    g_signal_new (I_("value-changed"),
Manish Singh's avatar
Manish Singh committed
243 244 245 246 247 248
		  G_OBJECT_CLASS_TYPE (class),
		  G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
		  G_STRUCT_OFFSET (GtkAdjustmentClass, value_changed),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
Elliot Lee's avatar
Elliot Lee committed
249 250 251 252 253
}

static void
gtk_adjustment_init (GtkAdjustment *adjustment)
{
254
  adjustment->priv = gtk_adjustment_get_instance_private (adjustment);
Elliot Lee's avatar
Elliot Lee committed
255 256
}

257
static void
Michael Natterer's avatar
Michael Natterer committed
258 259 260 261
gtk_adjustment_get_property (GObject    *object,
                             guint       prop_id,
                             GValue     *value,
                             GParamSpec *pspec)
262 263
{
  GtkAdjustment *adjustment = GTK_ADJUSTMENT (object);
264
  GtkAdjustmentPrivate *priv = adjustment->priv;
265 266 267 268

  switch (prop_id)
    {
    case PROP_VALUE:
269
      g_value_set_double (value, priv->value);
270 271
      break;
    case PROP_LOWER:
272
      g_value_set_double (value, priv->lower);
273 274
      break;
    case PROP_UPPER:
275
      g_value_set_double (value, priv->upper);
276 277
      break;
    case PROP_STEP_INCREMENT:
278
      g_value_set_double (value, priv->step_increment);
279 280
      break;
    case PROP_PAGE_INCREMENT:
281
      g_value_set_double (value, priv->page_increment);
282 283
      break;
    case PROP_PAGE_SIZE:
284
      g_value_set_double (value, priv->page_size);
285 286 287 288 289 290 291 292
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
Michael Natterer's avatar
Michael Natterer committed
293 294 295 296
gtk_adjustment_set_property (GObject      *object,
                             guint         prop_id,
                             const GValue *value,
                             GParamSpec   *pspec)
297 298 299
{
  GtkAdjustment *adjustment = GTK_ADJUSTMENT (object);
  gdouble double_value = g_value_get_double (value);
300
  GtkAdjustmentPrivate *priv = adjustment->priv;
301 302 303 304

  switch (prop_id)
    {
    case PROP_VALUE:
Michael Natterer's avatar
Michael Natterer committed
305
      gtk_adjustment_set_value (adjustment, double_value);
306 307
      break;
    case PROP_LOWER:
308
      priv->lower = double_value;
309 310
      break;
    case PROP_UPPER:
311
      priv->upper = double_value;
312 313
      break;
    case PROP_STEP_INCREMENT:
314
      priv->step_increment = double_value;
315 316
      break;
    case PROP_PAGE_INCREMENT:
317
      priv->page_increment = double_value;
318 319
      break;
    case PROP_PAGE_SIZE:
320
      priv->page_size = double_value;
321 322 323 324 325 326 327
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

Michael Natterer's avatar
Michael Natterer committed
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
static void
gtk_adjustment_dispatch_properties_changed (GObject     *object,
                                            guint        n_pspecs,
                                            GParamSpec **pspecs)
{
  gboolean changed = FALSE;
  gint i;

  G_OBJECT_CLASS (gtk_adjustment_parent_class)->dispatch_properties_changed (object, n_pspecs, pspecs);

  for (i = 0; i < n_pspecs; i++)
    switch (pspecs[i]->param_id)
      {
      case PROP_LOWER:
      case PROP_UPPER:
      case PROP_STEP_INCREMENT:
      case PROP_PAGE_INCREMENT:
      case PROP_PAGE_SIZE:
        changed = TRUE;
        break;
      default:
        break;
      }

  if (changed)
353 354 355 356
    {
      adjustment_changed_stamp++;
      gtk_adjustment_changed (GTK_ADJUSTMENT (object));
    }
Michael Natterer's avatar
Michael Natterer committed
357 358
}

359 360 361 362 363 364 365 366 367 368 369 370 371
/**
 * gtk_adjustment_new:
 * @value: the initial value.
 * @lower: the minimum value.
 * @upper: the maximum value.
 * @step_increment: the step increment.
 * @page_increment: the page increment.
 * @page_size: the page size.
 *
 * Creates a new #GtkAdjustment.
 *
 * Returns: a new #GtkAdjustment.
 */
372
GtkAdjustment *
373 374 375 376 377 378
gtk_adjustment_new (gdouble value,
		    gdouble lower,
		    gdouble upper,
		    gdouble step_increment,
		    gdouble page_increment,
		    gdouble page_size)
Elliot Lee's avatar
Elliot Lee committed
379
{
380
  return g_object_new (GTK_TYPE_ADJUSTMENT,
381 382 383 384
		       "lower", lower,
		       "upper", upper,
		       "step-increment", step_increment,
		       "page-increment", page_increment,
385
		       "page-size", page_size,
386
		       "value", value,
387
		       NULL);
Elliot Lee's avatar
Elliot Lee committed
388
}
389

390 391 392 393 394 395 396 397 398 399 400 401
/**
 * gtk_adjustment_get_value:
 * @adjustment: a #GtkAdjustment
 *
 * Gets the current value of the adjustment. See
 * gtk_adjustment_set_value ().
 *
 * Return value: The current value of the adjustment.
 **/
gdouble
gtk_adjustment_get_value (GtkAdjustment *adjustment)
{
Michael Natterer's avatar
Michael Natterer committed
402
  g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), 0.0);
403

404
  return adjustment->priv->value;
405 406
}

407 408 409 410 411 412
/**
 * gtk_adjustment_set_value:
 * @adjustment: a #GtkAdjustment.
 * @value: the new value.
 *
 * Sets the #GtkAdjustment value. The value is clamped to lie between
413
 * #GtkAdjustment:lower and #GtkAdjustment:upper.
414 415
 *
 * Note that for adjustments which are used in a #GtkScrollbar, the effective
416 417
 * range of allowed values goes from #GtkAdjustment:lower to
 * #GtkAdjustment:upper - #GtkAdjustment:page_size.
418
 */
419
void
Michael Natterer's avatar
Michael Natterer committed
420 421
gtk_adjustment_set_value (GtkAdjustment *adjustment,
			  gdouble        value)
422
{
423 424
  GtkAdjustmentPrivate *priv;
  
425 426
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

427 428
  priv = adjustment->priv;

429 430 431
  /* don't use CLAMP() so we don't end up below lower if upper - page_size
   * is smaller than lower
   */
432 433
  value = MIN (value, priv->upper - priv->page_size);
  value = MAX (value, priv->lower);
434

435
  if (value != priv->value)
436
    {
437
      priv->value = value;
438

439
      gtk_adjustment_value_changed (adjustment);
440 441 442
    }
}

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
/**
 * gtk_adjustment_get_lower:
 * @adjustment: a #GtkAdjustment
 *
 * Retrieves the minimum value of the adjustment.
 *
 * Return value: The current minimum value of the adjustment.
 *
 * Since: 2.14
 **/
gdouble
gtk_adjustment_get_lower (GtkAdjustment *adjustment)
{
  g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), 0.0);

458
  return adjustment->priv->lower;
459 460 461 462 463 464 465 466 467 468
}

/**
 * gtk_adjustment_set_lower:
 * @adjustment: a #GtkAdjustment
 * @lower: the new minimum value
 *
 * Sets the minimum value of the adjustment.
 *
 * When setting multiple adjustment properties via their individual
469 470 471 472
 * setters, multiple #GtkAdjustment::changed signals will be emitted. However, since
 * the emission of the #GtkAdjustment::changed signal is tied to the emission of the
 * #GObject::notify signals of the changed properties, it's possible
 * to compress the #GtkAdjustment::changed signals into one by calling
473 474 475 476 477
 * g_object_freeze_notify() and g_object_thaw_notify() around the
 * calls to the individual setters.
 *
 * Alternatively, using a single g_object_set() for all the properties
 * to change, or using gtk_adjustment_configure() has the same effect
478
 * of compressing #GtkAdjustment::changed emissions.
479 480 481 482 483 484 485 486 487
 *
 * Since: 2.14
 **/
void
gtk_adjustment_set_lower (GtkAdjustment *adjustment,
                          gdouble        lower)
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

488
  if (lower != adjustment->priv->lower)
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
    g_object_set (adjustment, "lower", lower, NULL);
}

/**
 * gtk_adjustment_get_upper:
 * @adjustment: a #GtkAdjustment
 *
 * Retrieves the maximum value of the adjustment.
 *
 * Return value: The current maximum value of the adjustment.
 *
 * Since: 2.14
 **/
gdouble
gtk_adjustment_get_upper (GtkAdjustment *adjustment)
{
  g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), 0.0);

507
  return adjustment->priv->upper;
508 509 510 511 512 513 514 515 516 517
}

/**
 * gtk_adjustment_set_upper:
 * @adjustment: a #GtkAdjustment
 * @upper: the new maximum value
 *
 * Sets the maximum value of the adjustment.
 *
 * Note that values will be restricted by
518
 * `upper - page-size` if the page-size
519 520 521
 * property is nonzero.
 *
 * See gtk_adjustment_set_lower() about how to compress multiple
522
 * emissions of the #GtkAdjustment::changed signal when setting multiple adjustment
523 524 525 526 527 528 529 530 531 532
 * properties.
 *
 * Since: 2.14
 **/
void
gtk_adjustment_set_upper (GtkAdjustment *adjustment,
                          gdouble        upper)
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

533
  if (upper != adjustment->priv->upper)
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
    g_object_set (adjustment, "upper", upper, NULL);
}

/**
 * gtk_adjustment_get_step_increment:
 * @adjustment: a #GtkAdjustment
 *
 * Retrieves the step increment of the adjustment.
 *
 * Return value: The current step increment of the adjustment.
 *
 * Since: 2.14
 **/
gdouble
gtk_adjustment_get_step_increment (GtkAdjustment *adjustment)
{
  g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), 0.0);

552
  return adjustment->priv->step_increment;
553 554 555 556 557 558 559 560 561 562
}

/**
 * gtk_adjustment_set_step_increment:
 * @adjustment: a #GtkAdjustment
 * @step_increment: the new step increment
 *
 * Sets the step increment of the adjustment.
 *
 * See gtk_adjustment_set_lower() about how to compress multiple
563
 * emissions of the #GtkAdjustment::changed signal when setting multiple adjustment
564 565 566 567 568 569 570 571 572 573
 * properties.
 *
 * Since: 2.14
 **/
void
gtk_adjustment_set_step_increment (GtkAdjustment *adjustment,
                                   gdouble        step_increment)
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

574
  if (step_increment != adjustment->priv->step_increment)
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
    g_object_set (adjustment, "step-increment", step_increment, NULL);
}

/**
 * gtk_adjustment_get_page_increment:
 * @adjustment: a #GtkAdjustment
 *
 * Retrieves the page increment of the adjustment.
 *
 * Return value: The current page increment of the adjustment.
 *
 * Since: 2.14
 **/
gdouble
gtk_adjustment_get_page_increment (GtkAdjustment *adjustment)
{
  g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), 0.0);

593
  return adjustment->priv->page_increment;
594 595 596 597 598 599 600 601 602 603
}

/**
 * gtk_adjustment_set_page_increment:
 * @adjustment: a #GtkAdjustment
 * @page_increment: the new page increment
 *
 * Sets the page increment of the adjustment.
 *
 * See gtk_adjustment_set_lower() about how to compress multiple
604
 * emissions of the #GtkAdjustment::changed signal when setting multiple adjustment
605 606 607 608 609 610 611 612 613 614
 * properties.
 *
 * Since: 2.14
 **/
void
gtk_adjustment_set_page_increment (GtkAdjustment *adjustment,
                                   gdouble        page_increment)
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

615
  if (page_increment != adjustment->priv->page_increment)
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
    g_object_set (adjustment, "page-increment", page_increment, NULL);
}

/**
 * gtk_adjustment_get_page_size:
 * @adjustment: a #GtkAdjustment
 *
 * Retrieves the page size of the adjustment.
 *
 * Return value: The current page size of the adjustment.
 *
 * Since: 2.14
 **/
gdouble
gtk_adjustment_get_page_size (GtkAdjustment *adjustment)
{
  g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), 0.0);

634
  return adjustment->priv->page_size;
635 636 637 638 639 640 641 642 643 644
}

/**
 * gtk_adjustment_set_page_size:
 * @adjustment: a #GtkAdjustment
 * @page_size: the new page size
 *
 * Sets the page size of the adjustment.
 *
 * See gtk_adjustment_set_lower() about how to compress multiple
645
 * emissions of the GtkAdjustment::changed signal when setting multiple adjustment
646 647 648 649 650 651 652 653 654 655
 * properties.
 *
 * Since: 2.14
 **/
void
gtk_adjustment_set_page_size (GtkAdjustment *adjustment,
                              gdouble        page_size)
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

656
  if (page_size != adjustment->priv->page_size)
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
    g_object_set (adjustment, "page-size", page_size, NULL);
}

/**
 * gtk_adjustment_configure:
 * @adjustment: a #GtkAdjustment
 * @value: the new value
 * @lower: the new minimum value
 * @upper: the new maximum value
 * @step_increment: the new step increment
 * @page_increment: the new page increment
 * @page_size: the new page size
 *
 * Sets all properties of the adjustment at once.
 *
672
 * Use this function to avoid multiple emissions of the #GtkAdjustment::changed
673
 * signal. See gtk_adjustment_set_lower() for an alternative way
674
 * of compressing multiple emissions of #GtkAdjustment::changed into one.
675 676 677 678 679 680 681 682 683 684 685 686
 *
 * Since: 2.14
 **/
void
gtk_adjustment_configure (GtkAdjustment *adjustment,
                          gdouble        value,
                          gdouble        lower,
                          gdouble        upper,
                          gdouble        step_increment,
                          gdouble        page_increment,
                          gdouble        page_size)
{
687
  GtkAdjustmentPrivate *priv;
688 689 690 691 692
  gboolean value_changed = FALSE;
  guint64 old_stamp = adjustment_changed_stamp;

  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

693 694
  priv = adjustment->priv;

695 696 697 698 699 700 701 702 703 704
  g_object_freeze_notify (G_OBJECT (adjustment));

  g_object_set (adjustment,
                "lower", lower,
                "upper", upper,
                "step-increment", step_increment,
                "page-increment", page_increment,
                "page-size", page_size,
                NULL);

705 706 707 708 709
  /* don't use CLAMP() so we don't end up below lower if upper - page_size
   * is smaller than lower
   */
  value = MIN (value, upper - page_size);
  value = MAX (value, lower);
710

711
  if (value != priv->value)
712 713 714 715
    {
      /* set value manually to make sure "changed" is emitted with the
       * new value in place and is emitted before "value-changed"
       */
716
      priv->value = value;
717 718 719 720 721 722 723 724 725 726 727 728
      value_changed = TRUE;
    }

  g_object_thaw_notify (G_OBJECT (adjustment));

  if (old_stamp == adjustment_changed_stamp)
    gtk_adjustment_changed (adjustment); /* force emission before ::value-changed */

  if (value_changed)
    gtk_adjustment_value_changed (adjustment);
}

729 730 731 732 733 734
/**
 * gtk_adjustment_changed:
 * @adjustment: a #GtkAdjustment
 *
 * Emits a #GtkAdjustment::changed signal from the #GtkAdjustment.
 * This is typically called by the owner of the #GtkAdjustment after it has
735
 * changed any of the #GtkAdjustment properties other than the value.
736
 */
737
void
Michael Natterer's avatar
Michael Natterer committed
738
gtk_adjustment_changed (GtkAdjustment *adjustment)
739 740 741
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

Manish Singh's avatar
Manish Singh committed
742
  g_signal_emit (adjustment, adjustment_signals[CHANGED], 0);
743 744
}

745 746 747 748 749 750
/**
 * gtk_adjustment_value_changed:
 * @adjustment: a #GtkAdjustment
 *
 * Emits a #GtkAdjustment::value_changed signal from the #GtkAdjustment.
 * This is typically called by the owner of the #GtkAdjustment after it has
751
 * changed the #GtkAdjustment:value property.
752
 */
753
void
Michael Natterer's avatar
Michael Natterer committed
754
gtk_adjustment_value_changed (GtkAdjustment *adjustment)
755 756
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
757

Manish Singh's avatar
Manish Singh committed
758
  g_signal_emit (adjustment, adjustment_signals[VALUE_CHANGED], 0);
759
  g_object_notify (G_OBJECT (adjustment), "value");
760
}
761

762 763 764 765 766 767
/**
 * gtk_adjustment_clamp_page:
 * @adjustment: a #GtkAdjustment.
 * @lower: the lower value.
 * @upper: the upper value.
 *
768
 * Updates the #GtkAdjustment:value property to ensure that the range
769
 * between @lower and @upper is in the current page (i.e. between
770
 * #GtkAdjustment:value and #GtkAdjustment:value + #GtkAdjustment:page_size).
771 772 773 774
 * If the range is larger than the page size, then only the start of it will
 * be in the current page.
 * A #GtkAdjustment::changed signal will be emitted if the value is changed.
 */
775 776
void
gtk_adjustment_clamp_page (GtkAdjustment *adjustment,
777 778
			   gdouble        lower,
			   gdouble        upper)
779
{
780
  GtkAdjustmentPrivate *priv;
781
  gboolean need_emission;
782 783 784

  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

785 786 787 788
  priv = adjustment->priv;

  lower = CLAMP (lower, priv->lower, priv->upper);
  upper = CLAMP (upper, priv->lower, priv->upper);
789 790 791

  need_emission = FALSE;

792
  if (priv->value + priv->page_size < upper)
793
    {
794
      priv->value = upper - priv->page_size;
795 796
      need_emission = TRUE;
    }
797
  if (priv->value > lower)
798
    {
799
      priv->value = lower;
800 801 802 803
      need_emission = TRUE;
    }

  if (need_emission)
804
    gtk_adjustment_value_changed (adjustment);
805
}
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849

/**
 * gtk_adjustment_get_minimum_increment:
 * @adjustment: a #GtkAdjustment
 *
 * Gets the smaller of step increment and page increment.
 *
 * Returns: the minimum increment of @adjustment
 *
 * Since: 3.2
 */
gdouble
gtk_adjustment_get_minimum_increment (GtkAdjustment *adjustment)
{
  GtkAdjustmentPrivate *priv;
  gdouble minimum_increment;

  g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), 0);

  priv = adjustment->priv;

    if (priv->step_increment != 0 && priv->page_increment != 0)
    {
      if (ABS (priv->step_increment) < ABS (priv->page_increment))
        minimum_increment = priv->step_increment;
      else
        minimum_increment = priv->page_increment;
    }
  else if (priv->step_increment == 0 && priv->page_increment == 0)
    {
      minimum_increment = 0;
    }
  else if (priv->step_increment == 0)
    {
      minimum_increment = priv->page_increment;
    }
  else
    {
      minimum_increment = priv->step_increment;
    }

  return minimum_increment;
}