/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
 * 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/. 
 */

#include "config.h"
#include "gtkadjustment.h"
#include "gtkmarshalers.h"
#include "gtkprivate.h"
#include "gtkintl.h"
#include "gtkalias.h"
#include <math.h>

/* This value is open for discussion */
#define DELTA 1e-10

enum
{
  PROP_0,
  PROP_VALUE,
  PROP_LOWER,
  PROP_UPPER,
  PROP_STEP_INCREMENT,
  PROP_PAGE_INCREMENT,
  PROP_PAGE_SIZE
};

enum
{
  CHANGED,
  VALUE_CHANGED,
  LAST_SIGNAL
};

enum
{
  OLD_VAL,
  NEW_VAL,
  NO_VALS
};

/* Value of each property is stored twice in order to be able to compare values
 * and emit proper signal only when property has changed.
 */
struct _GtkAdjustmentPriv
{
  gdouble value[NO_VALS];
  gdouble lower[NO_VALS];
  gdouble upper[NO_VALS];
  gdouble step_increment[NO_VALS];
  gdouble page_increment[NO_VALS];
  gdouble page_size[NO_VALS];
};

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);

/* Local helper functions */
static void enforce_restrictions                       (GtkAdjustment  *adjustment);
static gint check_and_emit_notify                      (GObject        *object,
							gdouble        *vals,
							const char     *prop);

static guint adjustment_signals[LAST_SIGNAL] = { 0 };

G_DEFINE_TYPE (GtkAdjustment, gtk_adjustment, G_TYPE_INITIALLY_UNOWNED)

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

  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;

  class->changed = NULL;
  class->value_changed = NULL;

  /**
   * GtkAdjustment:value:
   * 
   * The value of the adjustment.
   * 
   * Since: 2.4
   */
  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, 
							GTK_PARAM_READWRITE));
  
  /**
   * GtkAdjustment:lower:
   * 
   * The minimum value of the adjustment.
   * 
   * Since: 2.4
   */
  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,
							GTK_PARAM_READWRITE));
  
  /**
   * GtkAdjustment:upper:
   * 
   * The maximum value of the adjustment. 
   *
   * Since: 2.4
   */
  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, 
							GTK_PARAM_READWRITE));
  
  /**
   * GtkAdjustment:step-increment:
   * 
   * The step increment of the adjustment.
   * 
   * Since: 2.4
   */
  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, 
							GTK_PARAM_READWRITE));
  
  /**
   * GtkAdjustment:page-increment:
   * 
   * The page increment of the adjustment.
   * 
   * Since: 2.4
   */
  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, 
							GTK_PARAM_READWRITE));
  
  /**
   * GtkAdjustment:page-size:
   * 
   * The page size of the adjustment. 
   * 
   * Since: 2.4
   */
  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, 
							GTK_PARAM_READWRITE));

  adjustment_signals[CHANGED] =
    g_signal_new (I_("changed"),
		  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);

  adjustment_signals[VALUE_CHANGED] =
    g_signal_new (I_("value-changed"),
		  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);
}

static void
gtk_adjustment_init (GtkAdjustment *adjustment)
{
  adjustment->priv = G_TYPE_INSTANCE_GET_PRIVATE (adjustment,
						  GTK_TYPE_ADJUSTMENT,
						  GtkAdjustmentPriv);
  adjustment->priv->value[OLD_VAL] = 0.0;
  adjustment->priv->value[NEW_VAL] = 0.0;
  adjustment->priv->lower[OLD_VAL] = 0.0;
  adjustment->priv->lower[NEW_VAL] = 0.0;
  adjustment->priv->upper[OLD_VAL] = 0.0;
  adjustment->priv->upper[NEW_VAL] = 0.0;
  adjustment->priv->step_increment[OLD_VAL] = 0.0;
  adjustment->priv->step_increment[NEW_VAL] = 0.0;
  adjustment->priv->page_increment[OLD_VAL] = 0.0;
  adjustment->priv->page_increment[NEW_VAL] = 0.0;
  adjustment->priv->page_size[OLD_VAL] = 0.0;
  adjustment->priv->page_size[NEW_VAL] = 0.0;
}

static void
gtk_adjustment_get_property (GObject    *object,
                             guint       prop_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
  GtkAdjustmentPriv *priv = GTK_ADJUSTMENT (object)->priv;

  /* If getter is called before we had a chance to process everyting, we return
   * old values, since we know they are OK. */
  switch (prop_id)
    {
    case PROP_VALUE:
      g_value_set_double (value, priv->value[OLD_VAL]);
      break;
    case PROP_LOWER:
      g_value_set_double (value, priv->lower[OLD_VAL]);
      break;
    case PROP_UPPER:
      g_value_set_double (value, priv->upper[OLD_VAL]);
      break;
    case PROP_STEP_INCREMENT:
      g_value_set_double (value, priv->step_increment[OLD_VAL]);
      break;
    case PROP_PAGE_INCREMENT:
      g_value_set_double (value, priv->page_increment[OLD_VAL]);
      break;
    case PROP_PAGE_SIZE:
      g_value_set_double (value, priv->page_size[OLD_VAL]);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_adjustment_set_property (GObject      *object,
                             guint         prop_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
  GtkAdjustmentPriv *priv = GTK_ADJUSTMENT (object)->priv;
  gdouble double_value = g_value_get_double (value);

  switch (prop_id)
    {
    case PROP_VALUE:
      priv->value[1] = double_value;
      break;
    case PROP_LOWER:
      priv->lower[1] = double_value;
      break;
    case PROP_UPPER:
      priv->upper[1] = double_value;
      break;
    case PROP_STEP_INCREMENT:
      priv->step_increment[1] = double_value;
      break;
    case PROP_PAGE_INCREMENT:
      priv->page_increment[1] = double_value;
      break;
    case PROP_PAGE_SIZE:
      priv->page_size[1] = double_value;
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_adjustment_dispatch_properties_changed (GObject     *object,
                                            guint        n_pspecs,
                                            GParamSpec **pspecs)
{
  /* This function operates in three steps:
   * 1. ensure adjustment is in sane state
   * 2. emit notifications for every modified property (properties can be
   *    modified either in _set_property() or during step 1)
   * 3. emit proper signals; if both ::changed and ::value-changed need to be
   *    emitted, ::changed is emitted first, ::value-changed second
   */
  GtkAdjustmentPriv *priv = GTK_ADJUSTMENT (object)->priv;
  gint ch = 0;     /* Should ::changed signal be emitted? */
  gint val_ch = 0; /* Should ::value-changed signal be emitted? */

  /* Step 1 */
  enforce_restrictions (GTK_ADJUSTMENT (object));

  /* Step 2 */
  /* NOTE:
   * This is done manually, because GObject's notify dispatcher will not emit
   * notifications for properties we only modified inside
   * enforce_restrictions() (when for example, upper has been set to be smaller
   * than current value, which caused value to be clamped).
   */
  val_ch = check_and_emit_notify (object, priv->value, "value");
  ch += check_and_emit_notify (object, priv->lower, "lower");
  ch += check_and_emit_notify (object, priv->upper, "upper");
  ch += check_and_emit_notify (object, priv->step_increment, "step-increment");
  ch += check_and_emit_notify (object, priv->page_increment, "page-increment");
  ch += check_and_emit_notify (object, priv->page_size, "page-size");

  /* Give GObject's machinery a chance to do it's work */
  G_OBJECT_CLASS (gtk_adjustment_parent_class)->dispatch_properties_changed (object, n_pspecs, pspecs);

  /* Step 3 */
  if (ch)
    g_signal_emit (object, adjustment_signals[CHANGED], 0);

  if (val_ch)
    g_signal_emit (object, adjustment_signals[VALUE_CHANGED], 0);
}

static void
enforce_restrictions (GtkAdjustment *adjustment)
{
  GtkAdjustmentPriv *priv = adjustment->priv;

  /* NOTE TO MYSELF: Keep those checks properly sorted!! */

  /* Check if lower > upper */
  priv->upper[NEW_VAL] = MAX (priv->upper[NEW_VAL], priv->lower[NEW_VAL]);

  /* Check page_size */
  priv->page_size[NEW_VAL] = CLAMP (priv->page_size[NEW_VAL], 0,
				    priv->upper[NEW_VAL] - priv->lower[NEW_VAL]);

  /* Check value */
  priv->value[NEW_VAL] = CLAMP (priv->value[NEW_VAL], priv->lower[NEW_VAL],
				priv->upper[NEW_VAL] - priv->page_size[NEW_VAL]);
}

static gint
check_and_emit_notify (GObject    *object,
		       gdouble    *vals,
		       const char *prop)
{
  if (fabs (vals[0] - vals[1]) > DELTA)
    {
      vals[OLD_VAL] = vals[NEW_VAL];
      g_object_notify (object, prop);
      return 1;
    }
  else
    return 0;
}

GObject *
gtk_adjustment_new (gdouble value,
		    gdouble lower,
		    gdouble upper,
		    gdouble step_increment,
		    gdouble page_increment,
		    gdouble page_size)
{
  return g_object_new (GTK_TYPE_ADJUSTMENT,
		       "lower", lower,
		       "upper", upper,
		       "step-increment", step_increment,
		       "page-increment", page_increment,
		       "page-size", page_size,
		       "value", value,
		       NULL);
}

/**
 * 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)
{
  g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), 0.0);

  return adjustment->priv->value[OLD_VAL];
}

void
gtk_adjustment_set_value (GtkAdjustment *adjustment,
			  gdouble        value)
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

  g_object_set (adjustment, "value", value, NULL);
}

/**
 * gtk_adjustment_increment_value:
 * @adjustment: #GtkAdjustment you wan to modify
 * @increment: the amount to increment for
 *
 * This is a convenience wrapper that allows you to increment
 * #GtkAdjustment:value property without retrieving #GtkAdjustment:value first.
 *
 * This function can be emulated like this:
 * <programlisting><![CDATA[
 * gdouble value = gtk_adjustment_get_value (adjustment);
 * gtk_adjustment_set_value (adjustment, value + increment);
 * ]]></programlisting>
 * but using this function is slightly more efficient.
 *
 * @increment can be negative, in which case this function will perform exactly
 * the same action as gtk_adjustment_decrement_value();
 *
 * Since: 3.0
 */
void
gtk_adjustment_increment_value (GtkAdjustment *adjustment,
				gdouble        increment)
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

  g_object_set (adjustment, "value",
		adjustment->priv->value[OLD_VAL] + increment, NULL);
}

/**
 * gtk_adjustment_decrement_value:
 * @adjustment: #GtkAdjustment you wan to modify
 * @decrement: the amount to decrement for
 *
 * This is a convenience wrapper that allows you to decrement
 * #GtkAdjustment:value property without retrieving #GtkAdjustment:value first.
 *
 * This function can be emulated like this:
 * <programlisting><![CDATA[
 * gdouble value = gtk_adjustment_get_value (adjustment);
 * gtk_adjustment_set_value (adjustment, value - increment);
 * ]]></programlisting>
 * but using this function is slightly more efficient.
 *
 * @decrement can be negative, in which case this function will perform exactly
 * the same action as gtk_adjustment_increment_value();
 *
 * Since: 3.0
 */
void
gtk_adjustment_decrement_value (GtkAdjustment *adjustment,
				gdouble        decrement)
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

  g_object_set (adjustment, "value",
		adjustment->priv->value[OLD_VAL] - decrement, NULL);
}

/**
 * gtk_adjustment_increment_value_by_steps:
 * @adjustment: #GtkAdjustment you want to modify
 * @no_steps: number of #GtkAdjustment:step-increment<!---->s to use when
 *            incrementing
 *
 * This function is a convenience wrapper that allows you to modify
 * #GtkAdjustment:value in #GtkAdjustment:step-increment intervals.
 *
 * @no_steps can be negative, in which case this function operates exactly the
 * same as gtk_adjustment_decrement_value_by_steps().
 *
 * Since: 3.0
 */
void
gtk_adjustment_increment_value_by_steps (GtkAdjustment *adjustment,
					 gint           no_steps)
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

  g_object_set (adjustment, "value",
		adjustment->priv->value[OLD_VAL] + no_steps *
		adjustment->priv->step_increment[OLD_VAL], NULL);
}

/**
 * gtk_adjustment_decrement_value_by_steps:
 * @adjustment: #GtkAdjustment you want to modify
 * @no_steps: number of #GtkAdjustment:step-increment<!---->s to use when
 *            decrementing
 *
 * This function is a convenience wrapper that allows you to modify
 * #GtkAdjustment:value in #GtkAdjustment:step-increment intervals.
 *
 * @no_steps can be negative, in which case this function operates exactly the
 * same as gtk_adjustment_increment_value_by_steps().
 *
 * Since: 3.0
 */
void
gtk_adjustment_decrement_value_by_steps (GtkAdjustment *adjustment,
					 gint           no_steps)
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

  g_object_set (adjustment, "value",
		adjustment->priv->value[OLD_VAL] - no_steps *
		adjustment->priv->step_increment[OLD_VAL], NULL);
}

/**
 * gtk_adjustment_increment_value_by_pages:
 * @adjustment: #GtkAdjustment you wish to modify
 * @no_pages: number of #GtkAdjustment:page-increment<!---->s to use when
 *            incrementing
 *
 * This is a convenience wrapper that allows you to increment
 * #GtkAdjustment:value in #GtkAdjustment:page-increment steps.
 *
 * @no_pages can be negative, in which case this function operates exactly like
 * gtk_adjustment_decrement_value_by_pages().
 *
 * Since: 3.0
 */
void
gtk_adjustment_increment_value_by_pages (GtkAdjustment *adjustment,
					 gint           no_pages)
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

  g_object_set (adjustment, "value",
		adjustment->priv->value[OLD_VAL] + no_pages *
		adjustment->priv->page_increment[OLD_VAL], NULL);
}

/**
 * gtk_adjustment_decrement_value_by_pages:
 * @adjustment: #GtkAdjustment you wish to modify
 * @no_pages: number of #GtkAdjustment:page-increment<!---->s to use when
 *            decrementing
 *
 * This is a convenience wrapper that allows you to decrement
 * #GtkAdjustment:value in #GtkAdjustment:page-increment steps.
 *
 * @no_pages can be negative, in which case this function operates exactly like
 * gtk_adjustment_increment_value_by_pages().
 *
 * Since: 3.0
 */
void
gtk_adjustment_decrement_value_by_pages (GtkAdjustment *adjustment,
					 gint           no_pages)
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

  g_object_set (adjustment, "value",
		adjustment->priv->value[OLD_VAL] + no_pages *
		adjustment->priv->page_increment[OLD_VAL], NULL);
}

/**
 * gtk_adjustment_set_value_to_min:
 * @adjustment: #GtkAdjustment to modify
 *
 * This is a convenience wrapper that allows you to set #GtkAdjustment:value to
 * the smallest possible value. This function can be emulated using
 * gtk_adjustment_get_lower() and gtk_adjustment_set_value(), but it is slightly
 * more efficient.
 *
 * Since: 3.0
 */
void
gtk_adjustment_set_value_to_min (GtkAdjustment *adjustment)
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

  g_object_set (adjustment, "value", adjustment->priv->lower[OLD_VAL], NULL);
}

/**
 * gtk_adjustment_set_value_to_max:
 * @adjustment: #GtkAdjustment to modify
 *
 * This is a convenience wrapper that allows you to set #GtkAdjustment:value to
 * the largest possible value. This function can be emulated using
 * gtk_adjustment_get_upper(), gtk_adjustment_get_page_size() and
 * gtk_adjustment_set_value(), but is slightly more efficient.
 *
 * Since: 3.0
 */
void
gtk_adjustment_set_value_to_max (GtkAdjustment *adjustment)
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

  g_object_set (adjustment, "value",
		adjustment->priv->upper[OLD_VAL] -
		adjustment->priv->page_size[OLD_VAL], NULL);
}

/**
 * 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);

  return adjustment->priv->lower[OLD_VAL];
}

/**
 * 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
 * setters, multiple "changed" signals will be emitted. However, since
 * the emission of the "changed" signal is tied to the emission of the
 * "GObject::notify" signals of the changed properties, it's possible
 * to compress the "changed" signals into one by calling
 * 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
 * of compressing "changed" emissions.
 *
 * Since: 2.14
 **/
void
gtk_adjustment_set_lower (GtkAdjustment *adjustment,
                          gdouble        lower)
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

  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);

  return adjustment->priv->upper[OLD_VAL];
}

/**
 * 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
 * <literal>upper - page-size</literal> if the page-size
 * property is nonzero.
 *
 * See gtk_adjustment_set_lower() about how to compress multiple
 * emissions of the "changed" signal when setting multiple adjustment
 * properties.
 *
 * Since: 2.14
 **/
void
gtk_adjustment_set_upper (GtkAdjustment *adjustment,
                          gdouble        upper)
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

  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);

  return adjustment->priv->step_increment[OLD_VAL];
}

/**
 * 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
 * emissions of the "changed" signal when setting multiple adjustment
 * properties.
 *
 * Since: 2.14
 **/
void
gtk_adjustment_set_step_increment (GtkAdjustment *adjustment,
                                   gdouble        step_increment)
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

  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);

  return adjustment->priv->page_increment[OLD_VAL];
}

/**
 * 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
 * emissions of the "changed" signal when setting multiple adjustment
 * properties.
 *
 * Since: 2.14
 **/
void
gtk_adjustment_set_page_increment (GtkAdjustment *adjustment,
                                   gdouble        page_increment)
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

  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);

  return adjustment->priv->page_size[OLD_VAL];
}

/**
 * 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
 * emissions of the "changed" signal when setting multiple adjustment
 * properties.
 *
 * Since: 2.14
 **/
void
gtk_adjustment_set_page_size (GtkAdjustment *adjustment,
                              gdouble        page_size)
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

  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.
 *
 * Use this function to avoid multiple emissions of the "changed"
 * signal. See gtk_adjustment_set_lower() for an alternative way
 * of compressing multiple emissions of "changed" into one.
 *
 * Since: 2.14
 **/
void
gtk_adjustment_configure (GtkAdjustment *adjustment,
                          gdouble        value,
                          gdouble        lower,
                          gdouble        upper,
                          gdouble        step_increment,
                          gdouble        page_increment,
                          gdouble        page_size)
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

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

void
gtk_adjustment_clamp_page (GtkAdjustment *adjustment,
			   gdouble        lower,
			   gdouble        upper)
{
  gdouble new_value;

  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

  lower = CLAMP (lower,
		 adjustment->priv->lower[OLD_VAL],
		 adjustment->priv->upper[OLD_VAL]);
  upper = CLAMP (upper,
		 adjustment->priv->lower[OLD_VAL],
		 adjustment->priv->upper[OLD_VAL]);
  new_value = adjustment->priv->value[OLD_VAL];

  if (new_value + adjustment->priv->page_size[OLD_VAL] < upper)
      new_value = upper - adjustment->priv->page_size[OLD_VAL];

  if (new_value > lower)
      new_value = lower;

  if (new_value != adjustment->priv->value[OLD_VAL])
    g_object_set (adjustment, "value", new_value, NULL);
}

#define __GTK_ADJUSTMENT_C__
#include "gtkaliasdef.c"
