gtkadjustment.c 5.08 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4
/* 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
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
15 16 17
 * 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
18
 */
19 20

/*
21
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22 23 24 25 26
 * 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
27
#include "gtkadjustment.h"
28
#include "gtkmarshalers.h"
Elliot Lee's avatar
Elliot Lee committed
29 30 31 32 33 34 35 36 37 38 39 40 41


enum {
  CHANGED,
  VALUE_CHANGED,
  LAST_SIGNAL
};


static void gtk_adjustment_class_init (GtkAdjustmentClass *klass);
static void gtk_adjustment_init       (GtkAdjustment      *adjustment);


42
static guint adjustment_signals[LAST_SIGNAL] = { 0 };
Elliot Lee's avatar
Elliot Lee committed
43 44


Manish Singh's avatar
Manish Singh committed
45
GType
46
gtk_adjustment_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
47
{
Manish Singh's avatar
Manish Singh committed
48
  static GType adjustment_type = 0;
Elliot Lee's avatar
Elliot Lee committed
49 50 51

  if (!adjustment_type)
    {
Manish Singh's avatar
Manish Singh committed
52
      static const GTypeInfo adjustment_info =
Elliot Lee's avatar
Elliot Lee committed
53 54
      {
	sizeof (GtkAdjustmentClass),
Manish Singh's avatar
Manish Singh committed
55 56 57 58 59 60 61 62
	NULL,		/* base_init */
	NULL,		/* base_finalize */
	(GClassInitFunc) gtk_adjustment_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	sizeof (GtkAdjustment),
	0,		/* n_preallocs */
	(GInstanceInitFunc) gtk_adjustment_init,
Elliot Lee's avatar
Elliot Lee committed
63 64
      };

Manish Singh's avatar
Manish Singh committed
65 66 67
      adjustment_type =
	g_type_register_static (GTK_TYPE_OBJECT, "GtkAdjustment",
				&adjustment_info, 0);
Elliot Lee's avatar
Elliot Lee committed
68 69 70 71 72 73 74 75
    }

  return adjustment_type;
}

static void
gtk_adjustment_class_init (GtkAdjustmentClass *class)
{
76 77 78
  class->changed = NULL;
  class->value_changed = NULL;

Elliot Lee's avatar
Elliot Lee committed
79
  adjustment_signals[CHANGED] =
Manish Singh's avatar
Manish Singh committed
80 81 82 83 84 85 86
    g_signal_new ("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);
Elliot Lee's avatar
Elliot Lee committed
87
  adjustment_signals[VALUE_CHANGED] =
Manish Singh's avatar
Manish Singh committed
88 89 90 91 92 93 94
    g_signal_new ("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);
Elliot Lee's avatar
Elliot Lee committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108
}

static void
gtk_adjustment_init (GtkAdjustment *adjustment)
{
  adjustment->value = 0.0;
  adjustment->lower = 0.0;
  adjustment->upper = 0.0;
  adjustment->step_increment = 0.0;
  adjustment->page_increment = 0.0;
  adjustment->page_size = 0.0;
}

GtkObject*
109 110 111 112 113 114
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
115 116 117
{
  GtkAdjustment *adjustment;

Manish Singh's avatar
Manish Singh committed
118
  adjustment = g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
Elliot Lee's avatar
Elliot Lee committed
119 120 121 122 123 124 125 126 127 128

  adjustment->value = value;
  adjustment->lower = lower;
  adjustment->upper = upper;
  adjustment->step_increment = step_increment;
  adjustment->page_increment = page_increment;
  adjustment->page_size = page_size;

  return GTK_OBJECT (adjustment);
}
129

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
/**
 * 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.);

  return adjustment->value;
}

147 148
void
gtk_adjustment_set_value (GtkAdjustment        *adjustment,
149
			  gdouble               value)
150 151 152
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

153 154 155 156 157 158
  value = CLAMP (value, adjustment->lower, adjustment->upper);

  if (value != adjustment->value)
    {
      adjustment->value = value;

159
      gtk_adjustment_value_changed (adjustment);
160 161 162 163 164 165 166 167
    }
}

void
gtk_adjustment_changed (GtkAdjustment        *adjustment)
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

Manish Singh's avatar
Manish Singh committed
168
  g_signal_emit (adjustment, adjustment_signals[CHANGED], 0);
169 170 171 172 173 174
}

void
gtk_adjustment_value_changed (GtkAdjustment        *adjustment)
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
175

Manish Singh's avatar
Manish Singh committed
176
  g_signal_emit (adjustment, adjustment_signals[VALUE_CHANGED], 0);
177
}
178 179 180

void
gtk_adjustment_clamp_page (GtkAdjustment *adjustment,
181 182
			   gdouble        lower,
			   gdouble        upper)
183
{
184
  gboolean need_emission;
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

  lower = CLAMP (lower, adjustment->lower, adjustment->upper);
  upper = CLAMP (upper, adjustment->lower, adjustment->upper);

  need_emission = FALSE;

  if (adjustment->value + adjustment->page_size < upper)
    {
      adjustment->value = upper - adjustment->page_size;
      need_emission = TRUE;
    }
  if (adjustment->value > lower)
    {
      adjustment->value = lower;
      need_emission = TRUE;
    }

  if (need_emission)
205
    gtk_adjustment_value_changed (adjustment);
206
}