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