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