]> Pileus Git - ~andy/gtk/blob - modules/other/gail/gailtogglebutton.c
Integrate gail into gtk+. Bug #169488.
[~andy/gtk] / modules / other / gail / gailtogglebutton.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 <string.h>
21 #include <gtk/gtk.h>
22 #include "gailtogglebutton.h"
23
24 static void      gail_toggle_button_class_init        (GailToggleButtonClass *klass);
25
26 static void      gail_toggle_button_toggled_gtk       (GtkWidget             *widget);
27
28 static void      gail_toggle_button_real_notify_gtk   (GObject               *obj,
29                                                        GParamSpec            *pspec);
30
31 static void      gail_toggle_button_real_initialize   (AtkObject             *obj,
32                                                        gpointer              data);
33
34 static AtkStateSet* gail_toggle_button_ref_state_set  (AtkObject             *accessible);
35
36 static GailButtonClass *parent_class = NULL;
37
38 GType
39 gail_toggle_button_get_type (void)
40 {
41   static GType type = 0;
42
43   if (!type)
44     {
45       static const GTypeInfo tinfo =
46       {
47         sizeof (GailToggleButtonClass),
48         (GBaseInitFunc) NULL, /* base init */
49         (GBaseFinalizeFunc) NULL, /* base finalize */
50         (GClassInitFunc) gail_toggle_button_class_init, /* class init */
51         (GClassFinalizeFunc) NULL, /* class finalize */
52         NULL, /* class data */
53         sizeof (GailToggleButton), /* instance size */
54         0, /* nb preallocs */
55         (GInstanceInitFunc) NULL, /* instance init */
56         NULL /* value table */
57       };
58
59       type = g_type_register_static (GAIL_TYPE_BUTTON,
60                                      "GailToggleButton", &tinfo, 0);
61     }
62
63   return type;
64 }
65
66 static void
67 gail_toggle_button_class_init (GailToggleButtonClass *klass)
68 {
69   GailWidgetClass *widget_class;
70   AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
71
72   widget_class = (GailWidgetClass*)klass;
73   widget_class->notify_gtk = gail_toggle_button_real_notify_gtk;
74
75   parent_class = g_type_class_peek_parent (klass);
76
77   class->ref_state_set = gail_toggle_button_ref_state_set;
78   class->initialize = gail_toggle_button_real_initialize;
79 }
80
81 AtkObject* 
82 gail_toggle_button_new (GtkWidget *widget)
83 {
84   GObject *object;
85   AtkObject *accessible;
86
87   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (widget), NULL);
88
89   object = g_object_new (GAIL_TYPE_TOGGLE_BUTTON, NULL);
90
91   accessible = ATK_OBJECT (object);
92   atk_object_initialize (accessible, widget);
93
94   return accessible;
95 }
96
97 static void
98 gail_toggle_button_real_initialize (AtkObject *obj,
99                                     gpointer  data)
100 {
101   ATK_OBJECT_CLASS (parent_class)->initialize (obj, data);
102
103   g_signal_connect (data,
104                     "toggled",
105                     G_CALLBACK (gail_toggle_button_toggled_gtk),
106                     NULL);
107
108   if (GTK_IS_CHECK_BUTTON (data))
109     obj->role = ATK_ROLE_CHECK_BOX;
110   else
111     obj->role = ATK_ROLE_TOGGLE_BUTTON;
112 }
113
114 static void
115 gail_toggle_button_toggled_gtk (GtkWidget       *widget)
116 {
117   AtkObject *accessible;
118   GtkToggleButton *toggle_button;
119
120   toggle_button = GTK_TOGGLE_BUTTON (widget);
121
122   accessible = gtk_widget_get_accessible (widget);
123   atk_object_notify_state_change (accessible, ATK_STATE_CHECKED, 
124                                   toggle_button->active);
125
126
127 static AtkStateSet*
128 gail_toggle_button_ref_state_set (AtkObject *accessible)
129 {
130   AtkStateSet *state_set;
131   GtkToggleButton *toggle_button;
132   GtkWidget *widget;
133
134   state_set = ATK_OBJECT_CLASS (parent_class)->ref_state_set (accessible);
135   widget = GTK_ACCESSIBLE (accessible)->widget;
136  
137   if (widget == NULL)
138     return state_set;
139
140   toggle_button = GTK_TOGGLE_BUTTON (widget);
141
142   if (gtk_toggle_button_get_active (toggle_button))
143     atk_state_set_add_state (state_set, ATK_STATE_CHECKED);
144
145   if (gtk_toggle_button_get_inconsistent (toggle_button))
146     atk_state_set_remove_state (state_set, ATK_STATE_ENABLED);
147  
148   return state_set;
149 }
150
151 static void
152 gail_toggle_button_real_notify_gtk (GObject           *obj,
153                                     GParamSpec        *pspec)
154 {
155   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (obj);
156   AtkObject *atk_obj;
157
158   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (toggle_button));
159
160   if (strcmp (pspec->name, "inconsistent") == 0)
161     atk_object_notify_state_change (atk_obj, ATK_STATE_ENABLED,
162                        !gtk_toggle_button_get_inconsistent (toggle_button));
163   else
164     GAIL_WIDGET_CLASS (parent_class)->notify_gtk (obj, pspec);
165 }