]> Pileus Git - ~andy/gtk/blob - gtk/gtkadjustment.c
Document properties as new since 2.4. (#156101, Billy Biggs)
[~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 "gtkalias.h"
29 #include "gtkadjustment.h"
30 #include "gtkintl.h"
31 #include "gtkmarshalers.h"
32
33 enum
34 {
35   PROP_0,
36   PROP_VALUE,
37   PROP_LOWER,
38   PROP_UPPER,
39   PROP_STEP_INCREMENT,
40   PROP_PAGE_INCREMENT,
41   PROP_PAGE_SIZE
42 };
43
44 enum {
45   CHANGED,
46   VALUE_CHANGED,
47   LAST_SIGNAL
48 };
49
50
51 static void gtk_adjustment_class_init (GtkAdjustmentClass *klass);
52 static void gtk_adjustment_init       (GtkAdjustment      *adjustment);
53
54 static void gtk_adjustment_get_property (GObject      *object,
55                                          guint         prop_id,
56                                          GValue       *value,
57                                          GParamSpec   *pspec);
58 static void gtk_adjustment_set_property (GObject      *object,
59                                          guint         prop_id,
60                                          const GValue *value,
61                                          GParamSpec   *pspec);
62
63 static guint adjustment_signals[LAST_SIGNAL] = { 0 };
64
65
66 GType
67 gtk_adjustment_get_type (void)
68 {
69   static GType adjustment_type = 0;
70
71   if (!adjustment_type)
72     {
73       static const GTypeInfo adjustment_info =
74       {
75         sizeof (GtkAdjustmentClass),
76         NULL,           /* base_init */
77         NULL,           /* base_finalize */
78         (GClassInitFunc) gtk_adjustment_class_init,
79         NULL,           /* class_finalize */
80         NULL,           /* class_data */
81         sizeof (GtkAdjustment),
82         0,              /* n_preallocs */
83         (GInstanceInitFunc) gtk_adjustment_init,
84       };
85
86       adjustment_type =
87         g_type_register_static (GTK_TYPE_OBJECT, "GtkAdjustment",
88                                 &adjustment_info, 0);
89     }
90
91   return adjustment_type;
92 }
93
94 static void
95 gtk_adjustment_class_init (GtkAdjustmentClass *class)
96 {
97   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
98
99   gobject_class->set_property = gtk_adjustment_set_property;
100   gobject_class->get_property = gtk_adjustment_get_property;
101
102   class->changed = NULL;
103   class->value_changed = NULL;
104
105   /**
106    * GtkAdjustment:value:
107    * 
108    * The value of the adjustment.
109    * 
110    * Since: 2.4
111    */
112   g_object_class_install_property (gobject_class,
113                                    PROP_VALUE,
114                                    g_param_spec_double ("value",
115                                                         P_("Value"),
116                                                         P_("The value of the adjustment"),
117                                                         -G_MAXDOUBLE, 
118                                                         G_MAXDOUBLE, 
119                                                         0.0, 
120                                                         G_PARAM_READWRITE));
121   
122   /**
123    * GtkAdjustment:lower:
124    * 
125    * The minimum value of the adjustment.
126    * 
127    * Since: 2.4
128    */
129   g_object_class_install_property (gobject_class,
130                                    PROP_LOWER,
131                                    g_param_spec_double ("lower",
132                                                         P_("Minimum Value"),
133                                                         P_("The minimum value of the adjustment"),
134                                                         -G_MAXDOUBLE, 
135                                                         G_MAXDOUBLE, 
136                                                         0.0,
137                                                         G_PARAM_READWRITE));
138   
139   /**
140    * GtkAdjustment:upper:
141    * 
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.
146    *
147    * Since: 2.4
148    */
149   g_object_class_install_property (gobject_class,
150                                    PROP_UPPER,
151                                    g_param_spec_double ("upper",
152                                                         P_("Maximum Value"),
153                                                         P_("The maximum value of the adjustment"),
154                                                         -G_MAXDOUBLE, 
155                                                         G_MAXDOUBLE, 
156                                                         0.0, 
157                                                         G_PARAM_READWRITE));
158   
159   /**
160    * GtkAdjustment:step-increment:
161    * 
162    * The step increment of the adjustment.
163    * 
164    * Since: 2.4
165    */
166   g_object_class_install_property (gobject_class,
167                                    PROP_STEP_INCREMENT,
168                                    g_param_spec_double ("step-increment",
169                                                         P_("Step Increment"),
170                                                         P_("The step increment of the adjustment"),
171                                                         -G_MAXDOUBLE, 
172                                                         G_MAXDOUBLE, 
173                                                         0.0, 
174                                                         G_PARAM_READWRITE));
175   
176   /**
177    * GtkAdjustment:page-increment:
178    * 
179    * The page increment of the adjustment.
180    * 
181    * Since: 2.4
182    */
183   g_object_class_install_property (gobject_class,
184                                    PROP_PAGE_INCREMENT,
185                                    g_param_spec_double ("page-increment",
186                                                         P_("Page Increment"),
187                                                         P_("The page increment of the adjustment"),
188                                                         -G_MAXDOUBLE, 
189                                                         G_MAXDOUBLE, 
190                                                         0.0, 
191                                                         G_PARAM_READWRITE));
192   
193   /**
194    * GtkAdjustment:page-size:
195    * 
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 
199    * #GtkSpinButton.
200    * 
201    * Since: 2.4
202    */
203   g_object_class_install_property (gobject_class,
204                                    PROP_PAGE_SIZE,
205                                    g_param_spec_double ("page-size",
206                                                         P_("Page Size"),
207                                                         P_("The page size of the adjustment"),
208                                                         -G_MAXDOUBLE, 
209                                                         G_MAXDOUBLE, 
210                                                         0.0, 
211                                                         G_PARAM_READWRITE));
212   
213
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),
219                   NULL, NULL,
220                   _gtk_marshal_VOID__VOID,
221                   G_TYPE_NONE, 0);
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),
227                   NULL, NULL,
228                   _gtk_marshal_VOID__VOID,
229                   G_TYPE_NONE, 0);
230 }
231
232 static void
233 gtk_adjustment_init (GtkAdjustment *adjustment)
234 {
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;
241 }
242
243 static void
244 gtk_adjustment_get_property (GObject *object, guint prop_id, GValue *value, 
245                              GParamSpec *pspec)
246 {
247   GtkAdjustment *adjustment = GTK_ADJUSTMENT (object);
248
249   switch (prop_id)
250     {
251     case PROP_VALUE:
252       g_value_set_double (value, adjustment->value);
253       break;
254     case PROP_LOWER:
255       g_value_set_double (value, adjustment->lower);
256       break;
257     case PROP_UPPER:
258       g_value_set_double (value, adjustment->upper);
259       break;
260     case PROP_STEP_INCREMENT:
261       g_value_set_double (value, adjustment->step_increment);
262       break;
263     case PROP_PAGE_INCREMENT:
264       g_value_set_double (value, adjustment->page_increment);
265       break;
266     case PROP_PAGE_SIZE:
267       g_value_set_double (value, adjustment->page_size);
268       break;
269     default:
270       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
271       break;
272     }
273 }
274
275 static void
276 gtk_adjustment_set_property (GObject * object, guint prop_id, 
277                              const GValue * value, GParamSpec * pspec)
278 {
279   GtkAdjustment *adjustment = GTK_ADJUSTMENT (object);
280   gdouble double_value = g_value_get_double (value);
281
282   switch (prop_id)
283     {
284     case PROP_VALUE:
285       gtk_adjustment_set_value (GTK_ADJUSTMENT (object), double_value);
286       break;
287     case PROP_LOWER:
288       if (adjustment->lower != double_value)
289         {
290           adjustment->lower = double_value;
291           g_object_notify (G_OBJECT (adjustment), "lower");
292         }
293       break;
294     case PROP_UPPER:
295       if (adjustment->upper != double_value)
296         {
297           adjustment->upper = double_value;
298           g_object_notify (G_OBJECT (adjustment), "upper");
299         }
300       break;
301     case PROP_STEP_INCREMENT:
302       if (adjustment->step_increment != double_value)
303         {
304           adjustment->step_increment = double_value;
305           g_object_notify (G_OBJECT (adjustment), "step-increment");
306         }
307       break;
308     case PROP_PAGE_INCREMENT:
309       if (adjustment->page_increment != double_value)
310         {
311           adjustment->page_increment = double_value;
312           g_object_notify (G_OBJECT (adjustment), "page-increment");
313         }
314       break;
315     case PROP_PAGE_SIZE:
316       if (adjustment->page_size != double_value)
317         {
318           adjustment->page_size = double_value;
319           g_object_notify (G_OBJECT (adjustment), "page-size");
320         }
321       break;
322     default:
323       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
324       break;
325     }
326 }
327
328 GtkObject*
329 gtk_adjustment_new (gdouble value,
330                     gdouble lower,
331                     gdouble upper,
332                     gdouble step_increment,
333                     gdouble page_increment,
334                     gdouble page_size)
335 {
336   return g_object_new (GTK_TYPE_ADJUSTMENT, 
337                        "lower", lower,
338                        "upper", upper,
339                        "step-increment", step_increment,
340                        "page-increment", page_increment,
341                        "page_size", page_size,
342                        "value", value,
343                        NULL);
344 }
345
346 /**
347  * gtk_adjustment_get_value:
348  * @adjustment: a #GtkAdjustment
349  *
350  * Gets the current value of the adjustment. See
351  * gtk_adjustment_set_value ().
352  *
353  * Return value: The current value of the adjustment.
354  **/
355 gdouble
356 gtk_adjustment_get_value (GtkAdjustment *adjustment)
357 {
358   g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), 0.);
359
360   return adjustment->value;
361 }
362
363 void
364 gtk_adjustment_set_value (GtkAdjustment        *adjustment,
365                           gdouble               value)
366 {
367   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
368
369   value = CLAMP (value, adjustment->lower, adjustment->upper);
370
371   if (value != adjustment->value)
372     {
373       adjustment->value = value;
374
375       gtk_adjustment_value_changed (adjustment);
376     }
377 }
378
379 void
380 gtk_adjustment_changed (GtkAdjustment        *adjustment)
381 {
382   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
383
384   g_signal_emit (adjustment, adjustment_signals[CHANGED], 0);
385 }
386
387 void
388 gtk_adjustment_value_changed (GtkAdjustment        *adjustment)
389 {
390   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
391
392   g_signal_emit (adjustment, adjustment_signals[VALUE_CHANGED], 0);
393   g_object_notify (G_OBJECT (adjustment), "value");
394 }
395
396 void
397 gtk_adjustment_clamp_page (GtkAdjustment *adjustment,
398                            gdouble        lower,
399                            gdouble        upper)
400 {
401   gboolean need_emission;
402
403   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
404
405   lower = CLAMP (lower, adjustment->lower, adjustment->upper);
406   upper = CLAMP (upper, adjustment->lower, adjustment->upper);
407
408   need_emission = FALSE;
409
410   if (adjustment->value + adjustment->page_size < upper)
411     {
412       adjustment->value = upper - adjustment->page_size;
413       need_emission = TRUE;
414     }
415   if (adjustment->value > lower)
416     {
417       adjustment->value = lower;
418       need_emission = TRUE;
419     }
420
421   if (need_emission)
422     gtk_adjustment_value_changed (adjustment);
423 }