]> Pileus Git - ~andy/gtk/blob - gtk/gtkadjustment.c
: Mark param spec strings as static.
[~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 "gtkintl.h"
30 #include "gtkmarshalers.h"
31 #include "gtkalias.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 #define STATIC_STRINGS G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
106
107   /**
108    * GtkAdjustment:value:
109    * 
110    * The value of the adjustment.
111    * 
112    * Since: 2.4
113    */
114   g_object_class_install_property (gobject_class,
115                                    PROP_VALUE,
116                                    g_param_spec_double ("value",
117                                                         P_("Value"),
118                                                         P_("The value of the adjustment"),
119                                                         -G_MAXDOUBLE, 
120                                                         G_MAXDOUBLE, 
121                                                         0.0, 
122                                                         G_PARAM_READWRITE | STATIC_STRINGS));
123   
124   /**
125    * GtkAdjustment:lower:
126    * 
127    * The minimum value of the adjustment.
128    * 
129    * Since: 2.4
130    */
131   g_object_class_install_property (gobject_class,
132                                    PROP_LOWER,
133                                    g_param_spec_double ("lower",
134                                                         P_("Minimum Value"),
135                                                         P_("The minimum value of the adjustment"),
136                                                         -G_MAXDOUBLE, 
137                                                         G_MAXDOUBLE, 
138                                                         0.0,
139                                                         G_PARAM_READWRITE | STATIC_STRINGS));
140   
141   /**
142    * GtkAdjustment:upper:
143    * 
144    * The maximum value of the adjustment. 
145    * Note that values will be restricted by 
146    * <literal>upper - page-size</literal> if the page-size 
147    * property is nonzero.
148    *
149    * Since: 2.4
150    */
151   g_object_class_install_property (gobject_class,
152                                    PROP_UPPER,
153                                    g_param_spec_double ("upper",
154                                                         P_("Maximum Value"),
155                                                         P_("The maximum value of the adjustment"),
156                                                         -G_MAXDOUBLE, 
157                                                         G_MAXDOUBLE, 
158                                                         0.0, 
159                                                         G_PARAM_READWRITE | STATIC_STRINGS));
160   
161   /**
162    * GtkAdjustment:step-increment:
163    * 
164    * The step increment of the adjustment.
165    * 
166    * Since: 2.4
167    */
168   g_object_class_install_property (gobject_class,
169                                    PROP_STEP_INCREMENT,
170                                    g_param_spec_double ("step-increment",
171                                                         P_("Step Increment"),
172                                                         P_("The step increment of the adjustment"),
173                                                         -G_MAXDOUBLE, 
174                                                         G_MAXDOUBLE, 
175                                                         0.0, 
176                                                         G_PARAM_READWRITE | STATIC_STRINGS));
177   
178   /**
179    * GtkAdjustment:page-increment:
180    * 
181    * The page increment of the adjustment.
182    * 
183    * Since: 2.4
184    */
185   g_object_class_install_property (gobject_class,
186                                    PROP_PAGE_INCREMENT,
187                                    g_param_spec_double ("page-increment",
188                                                         P_("Page Increment"),
189                                                         P_("The page increment of the adjustment"),
190                                                         -G_MAXDOUBLE, 
191                                                         G_MAXDOUBLE, 
192                                                         0.0, 
193                                                         G_PARAM_READWRITE | STATIC_STRINGS));
194   
195   /**
196    * GtkAdjustment:page-size:
197    * 
198    * The page size of the adjustment. 
199    * Note that the page-size is irrelevant and should be set to zero
200    * if the adjustment is used for a simple scalar value, e.g. in a 
201    * #GtkSpinButton.
202    * 
203    * Since: 2.4
204    */
205   g_object_class_install_property (gobject_class,
206                                    PROP_PAGE_SIZE,
207                                    g_param_spec_double ("page-size",
208                                                         P_("Page Size"),
209                                                         P_("The page size of the adjustment"),
210                                                         -G_MAXDOUBLE, 
211                                                         G_MAXDOUBLE, 
212                                                         0.0, 
213                                                         G_PARAM_READWRITE | STATIC_STRINGS));
214   
215
216   adjustment_signals[CHANGED] =
217     g_signal_new ("changed",
218                   G_OBJECT_CLASS_TYPE (class),
219                   G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
220                   G_STRUCT_OFFSET (GtkAdjustmentClass, changed),
221                   NULL, NULL,
222                   _gtk_marshal_VOID__VOID,
223                   G_TYPE_NONE, 0);
224   adjustment_signals[VALUE_CHANGED] =
225     g_signal_new ("value_changed",
226                   G_OBJECT_CLASS_TYPE (class),
227                   G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
228                   G_STRUCT_OFFSET (GtkAdjustmentClass, value_changed),
229                   NULL, NULL,
230                   _gtk_marshal_VOID__VOID,
231                   G_TYPE_NONE, 0);
232 }
233
234 static void
235 gtk_adjustment_init (GtkAdjustment *adjustment)
236 {
237   adjustment->value = 0.0;
238   adjustment->lower = 0.0;
239   adjustment->upper = 0.0;
240   adjustment->step_increment = 0.0;
241   adjustment->page_increment = 0.0;
242   adjustment->page_size = 0.0;
243 }
244
245 static void
246 gtk_adjustment_get_property (GObject *object, guint prop_id, GValue *value, 
247                              GParamSpec *pspec)
248 {
249   GtkAdjustment *adjustment = GTK_ADJUSTMENT (object);
250
251   switch (prop_id)
252     {
253     case PROP_VALUE:
254       g_value_set_double (value, adjustment->value);
255       break;
256     case PROP_LOWER:
257       g_value_set_double (value, adjustment->lower);
258       break;
259     case PROP_UPPER:
260       g_value_set_double (value, adjustment->upper);
261       break;
262     case PROP_STEP_INCREMENT:
263       g_value_set_double (value, adjustment->step_increment);
264       break;
265     case PROP_PAGE_INCREMENT:
266       g_value_set_double (value, adjustment->page_increment);
267       break;
268     case PROP_PAGE_SIZE:
269       g_value_set_double (value, adjustment->page_size);
270       break;
271     default:
272       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
273       break;
274     }
275 }
276
277 static void
278 gtk_adjustment_set_property (GObject * object, guint prop_id, 
279                              const GValue * value, GParamSpec * pspec)
280 {
281   GtkAdjustment *adjustment = GTK_ADJUSTMENT (object);
282   gdouble double_value = g_value_get_double (value);
283
284   switch (prop_id)
285     {
286     case PROP_VALUE:
287       gtk_adjustment_set_value (GTK_ADJUSTMENT (object), double_value);
288       break;
289     case PROP_LOWER:
290       if (adjustment->lower != double_value)
291         {
292           adjustment->lower = double_value;
293           g_object_notify (G_OBJECT (adjustment), "lower");
294         }
295       break;
296     case PROP_UPPER:
297       if (adjustment->upper != double_value)
298         {
299           adjustment->upper = double_value;
300           g_object_notify (G_OBJECT (adjustment), "upper");
301         }
302       break;
303     case PROP_STEP_INCREMENT:
304       if (adjustment->step_increment != double_value)
305         {
306           adjustment->step_increment = double_value;
307           g_object_notify (G_OBJECT (adjustment), "step-increment");
308         }
309       break;
310     case PROP_PAGE_INCREMENT:
311       if (adjustment->page_increment != double_value)
312         {
313           adjustment->page_increment = double_value;
314           g_object_notify (G_OBJECT (adjustment), "page-increment");
315         }
316       break;
317     case PROP_PAGE_SIZE:
318       if (adjustment->page_size != double_value)
319         {
320           adjustment->page_size = double_value;
321           g_object_notify (G_OBJECT (adjustment), "page-size");
322         }
323       break;
324     default:
325       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
326       break;
327     }
328 }
329
330 GtkObject*
331 gtk_adjustment_new (gdouble value,
332                     gdouble lower,
333                     gdouble upper,
334                     gdouble step_increment,
335                     gdouble page_increment,
336                     gdouble page_size)
337 {
338   return g_object_new (GTK_TYPE_ADJUSTMENT, 
339                        "lower", lower,
340                        "upper", upper,
341                        "step-increment", step_increment,
342                        "page-increment", page_increment,
343                        "page_size", page_size,
344                        "value", value,
345                        NULL);
346 }
347
348 /**
349  * gtk_adjustment_get_value:
350  * @adjustment: a #GtkAdjustment
351  *
352  * Gets the current value of the adjustment. See
353  * gtk_adjustment_set_value ().
354  *
355  * Return value: The current value of the adjustment.
356  **/
357 gdouble
358 gtk_adjustment_get_value (GtkAdjustment *adjustment)
359 {
360   g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), 0.);
361
362   return adjustment->value;
363 }
364
365 void
366 gtk_adjustment_set_value (GtkAdjustment        *adjustment,
367                           gdouble               value)
368 {
369   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
370
371   value = CLAMP (value, adjustment->lower, adjustment->upper);
372
373   if (value != adjustment->value)
374     {
375       adjustment->value = value;
376
377       gtk_adjustment_value_changed (adjustment);
378     }
379 }
380
381 void
382 gtk_adjustment_changed (GtkAdjustment        *adjustment)
383 {
384   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
385
386   g_signal_emit (adjustment, adjustment_signals[CHANGED], 0);
387 }
388
389 void
390 gtk_adjustment_value_changed (GtkAdjustment        *adjustment)
391 {
392   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
393
394   g_signal_emit (adjustment, adjustment_signals[VALUE_CHANGED], 0);
395   g_object_notify (G_OBJECT (adjustment), "value");
396 }
397
398 void
399 gtk_adjustment_clamp_page (GtkAdjustment *adjustment,
400                            gdouble        lower,
401                            gdouble        upper)
402 {
403   gboolean need_emission;
404
405   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
406
407   lower = CLAMP (lower, adjustment->lower, adjustment->upper);
408   upper = CLAMP (upper, adjustment->lower, adjustment->upper);
409
410   need_emission = FALSE;
411
412   if (adjustment->value + adjustment->page_size < upper)
413     {
414       adjustment->value = upper - adjustment->page_size;
415       need_emission = TRUE;
416     }
417   if (adjustment->value > lower)
418     {
419       adjustment->value = lower;
420       need_emission = TRUE;
421     }
422
423   if (need_emission)
424     gtk_adjustment_value_changed (adjustment);
425 }
426
427 #define __GTK_ADJUSTMENT_C__
428 #include "gtkaliasdef.c"