1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include "gtkadjustment.h"
31 #include "gtkmarshalers.h"
51 static void gtk_adjustment_class_init (GtkAdjustmentClass *klass);
52 static void gtk_adjustment_init (GtkAdjustment *adjustment);
54 static void gtk_adjustment_get_property (GObject *object,
58 static void gtk_adjustment_set_property (GObject *object,
63 static guint adjustment_signals[LAST_SIGNAL] = { 0 };
67 gtk_adjustment_get_type (void)
69 static GType adjustment_type = 0;
73 static const GTypeInfo adjustment_info =
75 sizeof (GtkAdjustmentClass),
77 NULL, /* base_finalize */
78 (GClassInitFunc) gtk_adjustment_class_init,
79 NULL, /* class_finalize */
80 NULL, /* class_data */
81 sizeof (GtkAdjustment),
83 (GInstanceInitFunc) gtk_adjustment_init,
87 g_type_register_static (GTK_TYPE_OBJECT, "GtkAdjustment",
91 return adjustment_type;
95 gtk_adjustment_class_init (GtkAdjustmentClass *class)
97 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
99 gobject_class->set_property = gtk_adjustment_set_property;
100 gobject_class->get_property = gtk_adjustment_get_property;
102 class->changed = NULL;
103 class->value_changed = NULL;
106 * GtkAdjustment:value:
108 * The value of the adjustment.
112 g_object_class_install_property (gobject_class,
114 g_param_spec_double ("value",
116 P_("The value of the adjustment"),
123 * GtkAdjustment:lower:
125 * The minimum value of the adjustment.
129 g_object_class_install_property (gobject_class,
131 g_param_spec_double ("lower",
133 P_("The minimum value of the adjustment"),
140 * GtkAdjustment:upper:
142 * The maximum value of the adjustment.
143 * Note that values will be restricted by
144 * <literal>upper - page-size</literal> if the page-size
145 * property is nonzero.
149 g_object_class_install_property (gobject_class,
151 g_param_spec_double ("upper",
153 P_("The maximum value of the adjustment"),
160 * GtkAdjustment:step-increment:
162 * The step increment of the adjustment.
166 g_object_class_install_property (gobject_class,
168 g_param_spec_double ("step-increment",
169 P_("Step Increment"),
170 P_("The step increment of the adjustment"),
177 * GtkAdjustment:page-increment:
179 * The page increment of the adjustment.
183 g_object_class_install_property (gobject_class,
185 g_param_spec_double ("page-increment",
186 P_("Page Increment"),
187 P_("The page increment of the adjustment"),
194 * GtkAdjustment:page-size:
196 * The page size of the adjustment.
197 * Note that the page-size is irrelevant and should be set to zero
198 * if the adjustment is used for a simple scalar value, e.g. in a
203 g_object_class_install_property (gobject_class,
205 g_param_spec_double ("page-size",
207 P_("The page size of the adjustment"),
214 adjustment_signals[CHANGED] =
215 g_signal_new ("changed",
216 G_OBJECT_CLASS_TYPE (class),
217 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
218 G_STRUCT_OFFSET (GtkAdjustmentClass, changed),
220 _gtk_marshal_VOID__VOID,
222 adjustment_signals[VALUE_CHANGED] =
223 g_signal_new ("value_changed",
224 G_OBJECT_CLASS_TYPE (class),
225 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
226 G_STRUCT_OFFSET (GtkAdjustmentClass, value_changed),
228 _gtk_marshal_VOID__VOID,
233 gtk_adjustment_init (GtkAdjustment *adjustment)
235 adjustment->value = 0.0;
236 adjustment->lower = 0.0;
237 adjustment->upper = 0.0;
238 adjustment->step_increment = 0.0;
239 adjustment->page_increment = 0.0;
240 adjustment->page_size = 0.0;
244 gtk_adjustment_get_property (GObject *object, guint prop_id, GValue *value,
247 GtkAdjustment *adjustment = GTK_ADJUSTMENT (object);
252 g_value_set_double (value, adjustment->value);
255 g_value_set_double (value, adjustment->lower);
258 g_value_set_double (value, adjustment->upper);
260 case PROP_STEP_INCREMENT:
261 g_value_set_double (value, adjustment->step_increment);
263 case PROP_PAGE_INCREMENT:
264 g_value_set_double (value, adjustment->page_increment);
267 g_value_set_double (value, adjustment->page_size);
270 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
276 gtk_adjustment_set_property (GObject * object, guint prop_id,
277 const GValue * value, GParamSpec * pspec)
279 GtkAdjustment *adjustment = GTK_ADJUSTMENT (object);
280 gdouble double_value = g_value_get_double (value);
285 gtk_adjustment_set_value (GTK_ADJUSTMENT (object), double_value);
288 if (adjustment->lower != double_value)
290 adjustment->lower = double_value;
291 g_object_notify (G_OBJECT (adjustment), "lower");
295 if (adjustment->upper != double_value)
297 adjustment->upper = double_value;
298 g_object_notify (G_OBJECT (adjustment), "upper");
301 case PROP_STEP_INCREMENT:
302 if (adjustment->step_increment != double_value)
304 adjustment->step_increment = double_value;
305 g_object_notify (G_OBJECT (adjustment), "step-increment");
308 case PROP_PAGE_INCREMENT:
309 if (adjustment->page_increment != double_value)
311 adjustment->page_increment = double_value;
312 g_object_notify (G_OBJECT (adjustment), "page-increment");
316 if (adjustment->page_size != double_value)
318 adjustment->page_size = double_value;
319 g_object_notify (G_OBJECT (adjustment), "page-size");
323 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
329 gtk_adjustment_new (gdouble value,
332 gdouble step_increment,
333 gdouble page_increment,
336 return g_object_new (GTK_TYPE_ADJUSTMENT,
339 "step-increment", step_increment,
340 "page-increment", page_increment,
341 "page_size", page_size,
347 * gtk_adjustment_get_value:
348 * @adjustment: a #GtkAdjustment
350 * Gets the current value of the adjustment. See
351 * gtk_adjustment_set_value ().
353 * Return value: The current value of the adjustment.
356 gtk_adjustment_get_value (GtkAdjustment *adjustment)
358 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), 0.);
360 return adjustment->value;
364 gtk_adjustment_set_value (GtkAdjustment *adjustment,
367 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
369 value = CLAMP (value, adjustment->lower, adjustment->upper);
371 if (value != adjustment->value)
373 adjustment->value = value;
375 gtk_adjustment_value_changed (adjustment);
380 gtk_adjustment_changed (GtkAdjustment *adjustment)
382 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
384 g_signal_emit (adjustment, adjustment_signals[CHANGED], 0);
388 gtk_adjustment_value_changed (GtkAdjustment *adjustment)
390 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
392 g_signal_emit (adjustment, adjustment_signals[VALUE_CHANGED], 0);
393 g_object_notify (G_OBJECT (adjustment), "value");
397 gtk_adjustment_clamp_page (GtkAdjustment *adjustment,
401 gboolean need_emission;
403 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
405 lower = CLAMP (lower, adjustment->lower, adjustment->upper);
406 upper = CLAMP (upper, adjustment->lower, adjustment->upper);
408 need_emission = FALSE;
410 if (adjustment->value + adjustment->page_size < upper)
412 adjustment->value = upper - adjustment->page_size;
413 need_emission = TRUE;
415 if (adjustment->value > lower)
417 adjustment->value = lower;
418 need_emission = TRUE;
422 gtk_adjustment_value_changed (adjustment);