]> Pileus Git - ~andy/gtk/blob - modules/other/gail/gailspinbutton.c
767099d4c8cc462c5bc21b6165c1d1232dd63a3c
[~andy/gtk] / modules / other / gail / gailspinbutton.c
1 /* GAIL - The GNOME Accessibility Implementation Library
2  * Copyright 2001, 2002, 2003 Sun Microsystems Inc.
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 #include "config.h"
21
22 #include <string.h>
23 #include <gtk/gtk.h>
24 #include "gailspinbutton.h"
25 #include "gailadjustment.h"
26 #include "gail-private-macros.h"
27
28 static void      gail_spin_button_class_init        (GailSpinButtonClass *klass);
29 static void      gail_spin_button_init              (GailSpinButton *button);
30 static void      gail_spin_button_real_initialize   (AtkObject      *obj,
31                                                      gpointer       data);
32 static void      gail_spin_button_finalize          (GObject        *object);
33
34 static void      atk_value_interface_init           (AtkValueIface  *iface);
35
36 static void      gail_spin_button_real_notify_gtk   (GObject        *obj,
37                                                      GParamSpec     *pspec);
38
39 static void      gail_spin_button_get_current_value (AtkValue       *obj,
40                                                      GValue         *value);
41 static void      gail_spin_button_get_maximum_value (AtkValue       *obj,
42                                                      GValue         *value);
43 static void      gail_spin_button_get_minimum_value (AtkValue       *obj,
44                                                      GValue         *value);
45 static void      gail_spin_button_get_minimum_increment (AtkValue       *obj,
46                                                          GValue         *value);
47 static gboolean  gail_spin_button_set_current_value (AtkValue       *obj,
48                                                      const GValue   *value);
49 static void      gail_spin_button_value_changed     (GtkAdjustment  *adjustment,
50                                                      gpointer       data);
51         
52 G_DEFINE_TYPE_WITH_CODE (GailSpinButton, gail_spin_button, GAIL_TYPE_ENTRY,
53                          G_IMPLEMENT_INTERFACE (ATK_TYPE_VALUE, atk_value_interface_init))
54
55 static void
56 gail_spin_button_class_init (GailSpinButtonClass *klass)
57 {
58   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
59   AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
60   GailWidgetClass *widget_class;
61
62   widget_class = (GailWidgetClass*)klass;
63
64   widget_class->notify_gtk = gail_spin_button_real_notify_gtk;
65
66   class->initialize = gail_spin_button_real_initialize;
67
68   gobject_class->finalize = gail_spin_button_finalize;
69 }
70
71 static void
72 gail_spin_button_init (GailSpinButton *button)
73 {
74 }
75
76 static void
77 gail_spin_button_real_initialize (AtkObject *obj,
78                                   gpointer  data)
79 {
80   GtkAdjustment *adjustment;
81   GailSpinButton *spin_button = GAIL_SPIN_BUTTON (obj);
82   GtkSpinButton *gtk_spin_button;
83
84   ATK_OBJECT_CLASS (gail_spin_button_parent_class)->initialize (obj, data);
85
86   gtk_spin_button = GTK_SPIN_BUTTON (data); 
87   /*
88    * If a GtkAdjustment already exists for the spin_button, 
89    * create the GailAdjustment
90    */
91   adjustment = gtk_spin_button_get_adjustment (gtk_spin_button);
92   if (adjustment)
93     {
94       spin_button->adjustment = gail_adjustment_new (adjustment);
95       g_signal_connect (adjustment,
96                         "value-changed",
97                         G_CALLBACK (gail_spin_button_value_changed),
98                         obj);
99     }
100   else
101     spin_button->adjustment = NULL;
102
103   obj->role = ATK_ROLE_SPIN_BUTTON;
104
105 }
106
107 static void
108 atk_value_interface_init (AtkValueIface *iface)
109 {
110   iface->get_current_value = gail_spin_button_get_current_value;
111   iface->get_maximum_value = gail_spin_button_get_maximum_value;
112   iface->get_minimum_value = gail_spin_button_get_minimum_value;
113   iface->get_minimum_increment = gail_spin_button_get_minimum_increment;
114   iface->set_current_value = gail_spin_button_set_current_value;
115 }
116
117 static void
118 gail_spin_button_get_current_value (AtkValue       *obj,
119                                     GValue         *value)
120 {
121   GailSpinButton *spin_button;
122
123   g_return_if_fail (GAIL_IS_SPIN_BUTTON (obj));
124
125   spin_button = GAIL_SPIN_BUTTON (obj);
126   if (spin_button->adjustment == NULL)
127     /*
128      * Adjustment has not been specified
129      */
130     return;
131
132   atk_value_get_current_value (ATK_VALUE (spin_button->adjustment), value);
133 }
134
135 static void      
136 gail_spin_button_get_maximum_value (AtkValue       *obj,
137                                     GValue         *value)
138 {
139   GailSpinButton *spin_button;
140
141   g_return_if_fail (GAIL_IS_SPIN_BUTTON (obj));
142
143   spin_button = GAIL_SPIN_BUTTON (obj);
144   if (spin_button->adjustment == NULL)
145     /*
146      * Adjustment has not been specified
147      */
148     return;
149
150   atk_value_get_maximum_value (ATK_VALUE (spin_button->adjustment), value);
151 }
152
153 static void 
154 gail_spin_button_get_minimum_value (AtkValue       *obj,
155                                     GValue         *value)
156 {
157  GailSpinButton *spin_button;
158
159   g_return_if_fail (GAIL_IS_SPIN_BUTTON (obj));
160
161   spin_button = GAIL_SPIN_BUTTON (obj);
162   if (spin_button->adjustment == NULL)
163     /*
164      * Adjustment has not been specified
165      */
166     return;
167
168   atk_value_get_minimum_value (ATK_VALUE (spin_button->adjustment), value);
169 }
170
171 static void
172 gail_spin_button_get_minimum_increment (AtkValue *obj, GValue *value)
173 {
174  GailSpinButton *spin_button;
175
176   g_return_if_fail (GAIL_IS_SPIN_BUTTON (obj));
177
178   spin_button = GAIL_SPIN_BUTTON (obj);
179   if (spin_button->adjustment == NULL)
180     /*
181      * Adjustment has not been specified
182      */
183     return;
184
185   atk_value_get_minimum_increment (ATK_VALUE (spin_button->adjustment), value);
186 }
187
188 static gboolean  
189 gail_spin_button_set_current_value (AtkValue       *obj,
190                                     const GValue   *value)
191 {
192  GailSpinButton *spin_button;
193
194   g_return_val_if_fail (GAIL_IS_SPIN_BUTTON (obj), FALSE);
195
196   spin_button = GAIL_SPIN_BUTTON (obj);
197   if (spin_button->adjustment == NULL)
198     /*
199      * Adjustment has not been specified
200      */
201     return FALSE;
202
203   return atk_value_set_current_value (ATK_VALUE (spin_button->adjustment), value);
204 }
205
206 static void
207 gail_spin_button_finalize (GObject            *object)
208 {
209   GailSpinButton *spin_button = GAIL_SPIN_BUTTON (object);
210
211   if (spin_button->adjustment)
212     {
213       g_object_unref (spin_button->adjustment);
214       spin_button->adjustment = NULL;
215     }
216   G_OBJECT_CLASS (gail_spin_button_parent_class)->finalize (object);
217 }
218
219
220 static void
221 gail_spin_button_real_notify_gtk (GObject    *obj,
222                                   GParamSpec *pspec)
223 {
224   GtkWidget *widget = GTK_WIDGET (obj);
225   GailSpinButton *spin_button = GAIL_SPIN_BUTTON (gtk_widget_get_accessible (widget));
226
227   if (strcmp (pspec->name, "adjustment") == 0)
228     {
229       /*
230        * Get rid of the GailAdjustment for the GtkAdjustment
231        * which was associated with the spin_button.
232        */
233       GtkAdjustment* adjustment;
234       GtkSpinButton* gtk_spin_button;
235
236       if (spin_button->adjustment)
237         {
238           g_object_unref (spin_button->adjustment);
239           spin_button->adjustment = NULL;
240         }
241       /*
242        * Create the GailAdjustment when notify for "adjustment" property
243        * is received
244        */
245       gtk_spin_button = GTK_SPIN_BUTTON (widget);
246       adjustment = gtk_spin_button_get_adjustment (gtk_spin_button);
247       spin_button->adjustment = gail_adjustment_new (adjustment);
248       g_signal_connect (adjustment,
249                         "value-changed",
250                         G_CALLBACK (gail_spin_button_value_changed),
251                         spin_button);
252     }
253   else
254     GAIL_WIDGET_CLASS (gail_spin_button_parent_class)->notify_gtk (obj, pspec);
255 }
256
257
258 static void
259 gail_spin_button_value_changed (GtkAdjustment    *adjustment,
260                                 gpointer         data)
261 {
262   GailSpinButton *spin_button;
263
264   gail_return_if_fail (adjustment != NULL);
265   gail_return_if_fail (data != NULL);
266
267   spin_button = GAIL_SPIN_BUTTON (data);
268
269   g_object_notify (G_OBJECT (spin_button), "accessible-value");
270 }
271