]> Pileus Git - ~andy/gtk/blob - gtk/gtkadjustment.c
Revert name change
[~andy/gtk] / gtk / gtkadjustment.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include "config.h"
28 #include "gtkadjustment.h"
29 #include "gtkmarshalers.h"
30 #include "gtkprivate.h"
31 #include "gtkintl.h"
32 #include "gtkalias.h"
33
34 enum
35 {
36   PROP_0,
37   PROP_VALUE,
38   PROP_LOWER,
39   PROP_UPPER,
40   PROP_STEP_INCREMENT,
41   PROP_PAGE_INCREMENT,
42   PROP_PAGE_SIZE
43 };
44
45 enum
46 {
47   CHANGED,
48   VALUE_CHANGED,
49   LAST_SIGNAL
50 };
51
52
53 static void gtk_adjustment_get_property                (GObject      *object,
54                                                         guint         prop_id,
55                                                         GValue       *value,
56                                                         GParamSpec   *pspec);
57 static void gtk_adjustment_set_property                (GObject      *object,
58                                                         guint         prop_id,
59                                                         const GValue *value,
60                                                         GParamSpec   *pspec);
61 static void gtk_adjustment_dispatch_properties_changed (GObject      *object,
62                                                         guint         n_pspecs,
63                                                         GParamSpec  **pspecs);
64
65 static guint adjustment_signals[LAST_SIGNAL] = { 0 };
66
67 G_DEFINE_TYPE (GtkAdjustment, gtk_adjustment, GTK_TYPE_OBJECT)
68
69 static void
70 gtk_adjustment_class_init (GtkAdjustmentClass *class)
71 {
72   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
73
74   gobject_class->set_property                = gtk_adjustment_set_property;
75   gobject_class->get_property                = gtk_adjustment_get_property;
76   gobject_class->dispatch_properties_changed = gtk_adjustment_dispatch_properties_changed;
77
78   class->changed = NULL;
79   class->value_changed = NULL;
80
81   /**
82    * GtkAdjustment:value:
83    * 
84    * The value of the adjustment.
85    * 
86    * Since: 2.4
87    */
88   g_object_class_install_property (gobject_class,
89                                    PROP_VALUE,
90                                    g_param_spec_double ("value",
91                                                         P_("Value"),
92                                                         P_("The value of the adjustment"),
93                                                         -G_MAXDOUBLE, 
94                                                         G_MAXDOUBLE, 
95                                                         0.0, 
96                                                         GTK_PARAM_READWRITE));
97   
98   /**
99    * GtkAdjustment:lower:
100    * 
101    * The minimum value of the adjustment.
102    * 
103    * Since: 2.4
104    */
105   g_object_class_install_property (gobject_class,
106                                    PROP_LOWER,
107                                    g_param_spec_double ("lower",
108                                                         P_("Minimum Value"),
109                                                         P_("The minimum value of the adjustment"),
110                                                         -G_MAXDOUBLE, 
111                                                         G_MAXDOUBLE, 
112                                                         0.0,
113                                                         GTK_PARAM_READWRITE));
114   
115   /**
116    * GtkAdjustment:upper:
117    * 
118    * The maximum value of the adjustment. 
119    * Note that values will be restricted by 
120    * <literal>upper - page-size</literal> if the page-size 
121    * property is nonzero.
122    *
123    * Since: 2.4
124    */
125   g_object_class_install_property (gobject_class,
126                                    PROP_UPPER,
127                                    g_param_spec_double ("upper",
128                                                         P_("Maximum Value"),
129                                                         P_("The maximum value of the adjustment"),
130                                                         -G_MAXDOUBLE, 
131                                                         G_MAXDOUBLE, 
132                                                         0.0, 
133                                                         GTK_PARAM_READWRITE));
134   
135   /**
136    * GtkAdjustment:step-increment:
137    * 
138    * The step increment of the adjustment.
139    * 
140    * Since: 2.4
141    */
142   g_object_class_install_property (gobject_class,
143                                    PROP_STEP_INCREMENT,
144                                    g_param_spec_double ("step-increment",
145                                                         P_("Step Increment"),
146                                                         P_("The step increment of the adjustment"),
147                                                         -G_MAXDOUBLE, 
148                                                         G_MAXDOUBLE, 
149                                                         0.0, 
150                                                         GTK_PARAM_READWRITE));
151   
152   /**
153    * GtkAdjustment:page-increment:
154    * 
155    * The page increment of the adjustment.
156    * 
157    * Since: 2.4
158    */
159   g_object_class_install_property (gobject_class,
160                                    PROP_PAGE_INCREMENT,
161                                    g_param_spec_double ("page-increment",
162                                                         P_("Page Increment"),
163                                                         P_("The page increment of the adjustment"),
164                                                         -G_MAXDOUBLE, 
165                                                         G_MAXDOUBLE, 
166                                                         0.0, 
167                                                         GTK_PARAM_READWRITE));
168   
169   /**
170    * GtkAdjustment:page-size:
171    * 
172    * The page size of the adjustment. 
173    * Note that the page-size is irrelevant and should be set to zero
174    * if the adjustment is used for a simple scalar value, e.g. in a 
175    * #GtkSpinButton.
176    * 
177    * Since: 2.4
178    */
179   g_object_class_install_property (gobject_class,
180                                    PROP_PAGE_SIZE,
181                                    g_param_spec_double ("page-size",
182                                                         P_("Page Size"),
183                                                         P_("The page size of the adjustment"),
184                                                         -G_MAXDOUBLE, 
185                                                         G_MAXDOUBLE, 
186                                                         0.0, 
187                                                         GTK_PARAM_READWRITE));
188
189   adjustment_signals[CHANGED] =
190     g_signal_new (I_("changed"),
191                   G_OBJECT_CLASS_TYPE (class),
192                   G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
193                   G_STRUCT_OFFSET (GtkAdjustmentClass, changed),
194                   NULL, NULL,
195                   _gtk_marshal_VOID__VOID,
196                   G_TYPE_NONE, 0);
197
198   adjustment_signals[VALUE_CHANGED] =
199     g_signal_new (I_("value_changed"),
200                   G_OBJECT_CLASS_TYPE (class),
201                   G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
202                   G_STRUCT_OFFSET (GtkAdjustmentClass, value_changed),
203                   NULL, NULL,
204                   _gtk_marshal_VOID__VOID,
205                   G_TYPE_NONE, 0);
206 }
207
208 static void
209 gtk_adjustment_init (GtkAdjustment *adjustment)
210 {
211   adjustment->value = 0.0;
212   adjustment->lower = 0.0;
213   adjustment->upper = 0.0;
214   adjustment->step_increment = 0.0;
215   adjustment->page_increment = 0.0;
216   adjustment->page_size = 0.0;
217 }
218
219 static void
220 gtk_adjustment_get_property (GObject    *object,
221                              guint       prop_id,
222                              GValue     *value,
223                              GParamSpec *pspec)
224 {
225   GtkAdjustment *adjustment = GTK_ADJUSTMENT (object);
226
227   switch (prop_id)
228     {
229     case PROP_VALUE:
230       g_value_set_double (value, adjustment->value);
231       break;
232     case PROP_LOWER:
233       g_value_set_double (value, adjustment->lower);
234       break;
235     case PROP_UPPER:
236       g_value_set_double (value, adjustment->upper);
237       break;
238     case PROP_STEP_INCREMENT:
239       g_value_set_double (value, adjustment->step_increment);
240       break;
241     case PROP_PAGE_INCREMENT:
242       g_value_set_double (value, adjustment->page_increment);
243       break;
244     case PROP_PAGE_SIZE:
245       g_value_set_double (value, adjustment->page_size);
246       break;
247     default:
248       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
249       break;
250     }
251 }
252
253 static void
254 gtk_adjustment_set_property (GObject      *object,
255                              guint         prop_id,
256                              const GValue *value,
257                              GParamSpec   *pspec)
258 {
259   GtkAdjustment *adjustment = GTK_ADJUSTMENT (object);
260   gdouble double_value = g_value_get_double (value);
261
262   switch (prop_id)
263     {
264     case PROP_VALUE:
265       gtk_adjustment_set_value (adjustment, double_value);
266       break;
267     case PROP_LOWER:
268       adjustment->lower = double_value;
269       break;
270     case PROP_UPPER:
271       adjustment->upper = double_value;
272       break;
273     case PROP_STEP_INCREMENT:
274       adjustment->step_increment = double_value;
275       break;
276     case PROP_PAGE_INCREMENT:
277       adjustment->page_increment = double_value;
278       break;
279     case PROP_PAGE_SIZE:
280       adjustment->page_size = double_value;
281       break;
282     default:
283       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
284       break;
285     }
286 }
287
288 static void
289 gtk_adjustment_dispatch_properties_changed (GObject     *object,
290                                             guint        n_pspecs,
291                                             GParamSpec **pspecs)
292 {
293   gboolean changed = FALSE;
294   gint i;
295
296   G_OBJECT_CLASS (gtk_adjustment_parent_class)->dispatch_properties_changed (object, n_pspecs, pspecs);
297
298   for (i = 0; i < n_pspecs; i++)
299     switch (pspecs[i]->param_id)
300       {
301       case PROP_LOWER:
302       case PROP_UPPER:
303       case PROP_STEP_INCREMENT:
304       case PROP_PAGE_INCREMENT:
305       case PROP_PAGE_SIZE:
306         changed = TRUE;
307         break;
308       default:
309         break;
310       }
311
312   if (changed)
313     gtk_adjustment_changed (GTK_ADJUSTMENT (object));
314 }
315
316 GtkObject *
317 gtk_adjustment_new (gdouble value,
318                     gdouble lower,
319                     gdouble upper,
320                     gdouble step_increment,
321                     gdouble page_increment,
322                     gdouble page_size)
323 {
324   return g_object_new (GTK_TYPE_ADJUSTMENT, 
325                        "lower", lower,
326                        "upper", upper,
327                        "step-increment", step_increment,
328                        "page-increment", page_increment,
329                        "page-size", page_size,
330                        "value", value,
331                        NULL);
332 }
333
334 /**
335  * gtk_adjustment_get_value:
336  * @adjustment: a #GtkAdjustment
337  *
338  * Gets the current value of the adjustment. See
339  * gtk_adjustment_set_value ().
340  *
341  * Return value: The current value of the adjustment.
342  **/
343 gdouble
344 gtk_adjustment_get_value (GtkAdjustment *adjustment)
345 {
346   g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), 0.0);
347
348   return adjustment->value;
349 }
350
351 void
352 gtk_adjustment_set_value (GtkAdjustment *adjustment,
353                           gdouble        value)
354 {
355   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
356
357   value = CLAMP (value, adjustment->lower, adjustment->upper);
358
359   if (value != adjustment->value)
360     {
361       adjustment->value = value;
362
363       gtk_adjustment_value_changed (adjustment);
364     }
365 }
366
367 void
368 gtk_adjustment_changed (GtkAdjustment *adjustment)
369 {
370   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
371
372   g_signal_emit (adjustment, adjustment_signals[CHANGED], 0);
373 }
374
375 void
376 gtk_adjustment_value_changed (GtkAdjustment *adjustment)
377 {
378   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
379
380   g_signal_emit (adjustment, adjustment_signals[VALUE_CHANGED], 0);
381   g_object_notify (G_OBJECT (adjustment), "value");
382 }
383
384 void
385 gtk_adjustment_clamp_page (GtkAdjustment *adjustment,
386                            gdouble        lower,
387                            gdouble        upper)
388 {
389   gboolean need_emission;
390
391   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
392
393   lower = CLAMP (lower, adjustment->lower, adjustment->upper);
394   upper = CLAMP (upper, adjustment->lower, adjustment->upper);
395
396   need_emission = FALSE;
397
398   if (adjustment->value + adjustment->page_size < upper)
399     {
400       adjustment->value = upper - adjustment->page_size;
401       need_emission = TRUE;
402     }
403   if (adjustment->value > lower)
404     {
405       adjustment->value = lower;
406       need_emission = TRUE;
407     }
408
409   if (need_emission)
410     gtk_adjustment_value_changed (adjustment);
411 }
412
413 #define __GTK_ADJUSTMENT_C__
414 #include "gtkaliasdef.c"