]> Pileus Git - ~andy/gtk/blob - gtk/gtkadjustment.c
Get rid of incorrect calls to g_value_init(). (#100669, Johan Dahlin,
[~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 "gtkadjustment.h"
28 #include "gtkmarshalers.h"
29
30
31 enum {
32   CHANGED,
33   VALUE_CHANGED,
34   LAST_SIGNAL
35 };
36
37
38 static void gtk_adjustment_class_init (GtkAdjustmentClass *klass);
39 static void gtk_adjustment_init       (GtkAdjustment      *adjustment);
40
41
42 static guint adjustment_signals[LAST_SIGNAL] = { 0 };
43
44
45 GType
46 gtk_adjustment_get_type (void)
47 {
48   static GType adjustment_type = 0;
49
50   if (!adjustment_type)
51     {
52       static const GTypeInfo adjustment_info =
53       {
54         sizeof (GtkAdjustmentClass),
55         NULL,           /* base_init */
56         NULL,           /* base_finalize */
57         (GClassInitFunc) gtk_adjustment_class_init,
58         NULL,           /* class_finalize */
59         NULL,           /* class_data */
60         sizeof (GtkAdjustment),
61         0,              /* n_preallocs */
62         (GInstanceInitFunc) gtk_adjustment_init,
63       };
64
65       adjustment_type =
66         g_type_register_static (GTK_TYPE_OBJECT, "GtkAdjustment",
67                                 &adjustment_info, 0);
68     }
69
70   return adjustment_type;
71 }
72
73 static void
74 gtk_adjustment_class_init (GtkAdjustmentClass *class)
75 {
76   class->changed = NULL;
77   class->value_changed = NULL;
78
79   adjustment_signals[CHANGED] =
80     g_signal_new ("changed",
81                   G_OBJECT_CLASS_TYPE (class),
82                   G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
83                   G_STRUCT_OFFSET (GtkAdjustmentClass, changed),
84                   NULL, NULL,
85                   _gtk_marshal_VOID__VOID,
86                   G_TYPE_NONE, 0);
87   adjustment_signals[VALUE_CHANGED] =
88     g_signal_new ("value_changed",
89                   G_OBJECT_CLASS_TYPE (class),
90                   G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
91                   G_STRUCT_OFFSET (GtkAdjustmentClass, value_changed),
92                   NULL, NULL,
93                   _gtk_marshal_VOID__VOID,
94                   G_TYPE_NONE, 0);
95 }
96
97 static void
98 gtk_adjustment_init (GtkAdjustment *adjustment)
99 {
100   adjustment->value = 0.0;
101   adjustment->lower = 0.0;
102   adjustment->upper = 0.0;
103   adjustment->step_increment = 0.0;
104   adjustment->page_increment = 0.0;
105   adjustment->page_size = 0.0;
106 }
107
108 GtkObject*
109 gtk_adjustment_new (gdouble value,
110                     gdouble lower,
111                     gdouble upper,
112                     gdouble step_increment,
113                     gdouble page_increment,
114                     gdouble page_size)
115 {
116   GtkAdjustment *adjustment;
117
118   adjustment = g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
119
120   adjustment->value = value;
121   adjustment->lower = lower;
122   adjustment->upper = upper;
123   adjustment->step_increment = step_increment;
124   adjustment->page_increment = page_increment;
125   adjustment->page_size = page_size;
126
127   return GTK_OBJECT (adjustment);
128 }
129
130 /**
131  * gtk_adjustment_get_value:
132  * @adjustment: a #GtkAdjustment
133  *
134  * Gets the current value of the adjustment. See
135  * gtk_adjustment_set_value ().
136  *
137  * Return value: The current value of the adjustment.
138  **/
139 gdouble
140 gtk_adjustment_get_value (GtkAdjustment *adjustment)
141 {
142   g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), 0.);
143
144   return adjustment->value;
145 }
146
147 void
148 gtk_adjustment_set_value (GtkAdjustment        *adjustment,
149                           gdouble               value)
150 {
151   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
152
153   value = CLAMP (value, adjustment->lower, adjustment->upper);
154
155   if (value != adjustment->value)
156     {
157       adjustment->value = value;
158
159       gtk_adjustment_value_changed (adjustment);
160     }
161 }
162
163 void
164 gtk_adjustment_changed (GtkAdjustment        *adjustment)
165 {
166   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
167
168   g_signal_emit (adjustment, adjustment_signals[CHANGED], 0);
169 }
170
171 void
172 gtk_adjustment_value_changed (GtkAdjustment        *adjustment)
173 {
174   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
175
176   g_signal_emit (adjustment, adjustment_signals[VALUE_CHANGED], 0);
177 }
178
179 void
180 gtk_adjustment_clamp_page (GtkAdjustment *adjustment,
181                            gdouble        lower,
182                            gdouble        upper)
183 {
184   gboolean need_emission;
185
186   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
187
188   lower = CLAMP (lower, adjustment->lower, adjustment->upper);
189   upper = CLAMP (upper, adjustment->lower, adjustment->upper);
190
191   need_emission = FALSE;
192
193   if (adjustment->value + adjustment->page_size < upper)
194     {
195       adjustment->value = upper - adjustment->page_size;
196       need_emission = TRUE;
197     }
198   if (adjustment->value > lower)
199     {
200       adjustment->value = lower;
201       need_emission = TRUE;
202     }
203
204   if (need_emission)
205     gtk_adjustment_value_changed (adjustment);
206 }