]> Pileus Git - ~andy/gtk/blob - gtk/gtkaccessible.c
accessible: Remove stray semicolon
[~andy/gtk] / gtk / gtkaccessible.c
1 /* GTK - The GIMP Toolkit
2  * Copyright 2001 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 #include <string.h>
22
23 #include "gtkwidget.h"
24 #include "gtkintl.h"
25 #include "gtkaccessible.h"
26
27 /**
28  * SECTION:gtkaccessible
29  * @Short_description: Accessibility support for widgets
30  * @Title: GtkAccessible
31  *
32  * The #GtkAccessible class is the base class for accessible
33  * implementations for #GtkWidget subclasses. It is a thin
34  * wrapper around #AtkObject, which adds facilities for associating
35  * a widget with its accessible object.
36  *
37  * An accessible implementation for a third-party widget should
38  * derive from #GtkAccessible and implement the suitable interfaces
39  * from ATK, such as #AtkText or #AtkSelection. To establish
40  * the connection between the widget class and its corresponding
41  * acccessible implementation, override the get_accessible vfunc
42  * in #GtkWidgetClass.
43  */
44
45 struct _GtkAccessiblePrivate
46 {
47   GtkWidget *widget;
48 };
49
50 enum {
51   PROP_0,
52   PROP_WIDGET
53 };
54
55 static void gtk_accessible_real_connect_widget_destroyed (GtkAccessible *accessible);
56
57 G_DEFINE_TYPE (GtkAccessible, gtk_accessible, ATK_TYPE_OBJECT)
58
59 static void
60 gtk_accessible_set_property (GObject      *object,
61                              guint         prop_id,
62                              const GValue *value,
63                              GParamSpec   *pspec)
64 {
65   GtkAccessible *accessible = GTK_ACCESSIBLE (object);
66
67   switch (prop_id)
68     {
69     case PROP_WIDGET:
70       gtk_accessible_set_widget (accessible, g_value_get_object (value));
71       break;
72     default:
73       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
74       break;
75     }
76 }
77
78 static void
79 gtk_accessible_get_property (GObject    *object,
80                              guint       prop_id,
81                              GValue     *value,
82                              GParamSpec *pspec)
83 {
84   GtkAccessible *accessible = GTK_ACCESSIBLE (object);
85   GtkAccessiblePrivate *priv = accessible->priv;
86
87   switch (prop_id)
88     {
89     case PROP_WIDGET:
90       g_value_set_object (value, priv->widget);
91       break;
92     default:
93       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
94       break;
95     }
96 }
97
98 static void
99 gtk_accessible_init (GtkAccessible *accessible)
100 {
101   accessible->priv = G_TYPE_INSTANCE_GET_PRIVATE (accessible,
102                                                   GTK_TYPE_ACCESSIBLE,
103                                                   GtkAccessiblePrivate);
104 }
105
106 static AtkStateSet *
107 gtk_accessible_ref_state_set (AtkObject *object)
108 {
109   GtkAccessible *accessible = GTK_ACCESSIBLE (object);
110   AtkStateSet *state_set;
111
112   state_set = ATK_OBJECT_CLASS (gtk_accessible_parent_class)->ref_state_set (object);
113
114   if (accessible->priv->widget == NULL)
115     atk_state_set_add_state (state_set, ATK_STATE_DEFUNCT);
116
117   return state_set;
118 }
119
120 static void
121 gtk_accessible_real_widget_set (GtkAccessible *accessible)
122 {
123   atk_object_notify_state_change (ATK_OBJECT (accessible), ATK_STATE_DEFUNCT, FALSE);
124 }
125
126 static void
127 gtk_accessible_real_widget_unset (GtkAccessible *accessible)
128 {
129   atk_object_notify_state_change (ATK_OBJECT (accessible), ATK_STATE_DEFUNCT, TRUE);
130 }
131
132 static void
133 gtk_accessible_finalize (GObject *object)
134 {
135   GtkAccessible *accessible = GTK_ACCESSIBLE (object);
136   
137   gtk_accessible_set_widget (accessible, NULL);
138
139   G_OBJECT_CLASS (gtk_accessible_parent_class)->finalize (object);
140 }
141
142 static void
143 gtk_accessible_class_init (GtkAccessibleClass *klass)
144 {
145   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
146   AtkObjectClass *atkobject_class = ATK_OBJECT_CLASS (klass);
147
148   klass->connect_widget_destroyed = gtk_accessible_real_connect_widget_destroyed;
149   klass->widget_set = gtk_accessible_real_widget_set;
150   klass->widget_unset = gtk_accessible_real_widget_unset;
151
152   atkobject_class->ref_state_set = gtk_accessible_ref_state_set;
153   gobject_class->get_property = gtk_accessible_get_property;
154   gobject_class->set_property = gtk_accessible_set_property;
155   gobject_class->finalize = gtk_accessible_finalize;
156
157   g_object_class_install_property (gobject_class,
158                                    PROP_WIDGET,
159                                    g_param_spec_object ("widget",
160                                                         P_("Widget"),
161                                                         P_("The widget referenced by this accessible."),
162                                                         GTK_TYPE_WIDGET,
163                                                         G_PARAM_READWRITE));
164
165   g_type_class_add_private (klass, sizeof (GtkAccessiblePrivate));
166 }
167
168 /**
169  * gtk_accessible_set_widget:
170  * @accessible: a #GtkAccessible
171  * @widget: (allow-none): a #GtkWidget or %NULL to unset
172  *
173  * Sets the #GtkWidget corresponding to the #GtkAccessible.
174  *
175  * <note><para>@accessible will not hold a reference to @widget.
176  * It is the caller's responsibility to ensure that when @widget
177  * is destroyed, the widget is unset by calling this function
178  * again with @widget set to %NULL.</para></note>
179  * Since: 2.22
180  */
181 void
182 gtk_accessible_set_widget (GtkAccessible *accessible,
183                            GtkWidget     *widget)
184 {
185   GtkAccessiblePrivate *priv;
186   GtkAccessibleClass *klass;
187
188   g_return_if_fail (GTK_IS_ACCESSIBLE (accessible));
189
190   priv = accessible->priv;
191   klass = GTK_ACCESSIBLE_GET_CLASS (accessible);
192
193   if (priv->widget == widget)
194     return;
195
196   if (priv->widget)
197     klass->widget_unset (accessible);
198
199   priv->widget = widget;
200
201   if (widget)
202     klass->widget_set (accessible);
203
204   g_object_notify (G_OBJECT (accessible), "widget");
205 }
206
207 /**
208  * gtk_accessible_get_widget:
209  * @accessible: a #GtkAccessible
210  *
211  * Gets the #GtkWidget corresponding to the #GtkAccessible.
212  * The returned widget does not have a reference added, so
213  * you do not need to unref it.
214  *
215  * Returns: (transfer none): pointer to the #GtkWidget
216  *     corresponding to the #GtkAccessible, or %NULL.
217  *
218  * Since: 2.22
219  */
220 GtkWidget*
221 gtk_accessible_get_widget (GtkAccessible *accessible)
222 {
223   g_return_val_if_fail (GTK_IS_ACCESSIBLE (accessible), NULL);
224
225   return accessible->priv->widget;
226 }
227
228 /**
229  * gtk_accessible_connect_widget_destroyed:
230  * @accessible: a #GtkAccessible
231  *
232  * This function specifies the callback function to be called
233  * when the widget corresponding to a GtkAccessible is destroyed.
234  *
235  * Deprecated: 3.4: Use gtk_accessible_set_widget() and its vfuncs.
236  */
237 void
238 gtk_accessible_connect_widget_destroyed (GtkAccessible *accessible)
239 {
240   GtkAccessibleClass *class;
241
242   g_return_if_fail (GTK_IS_ACCESSIBLE (accessible));
243
244   class = GTK_ACCESSIBLE_GET_CLASS (accessible);
245
246   if (class->connect_widget_destroyed)
247     class->connect_widget_destroyed (accessible);
248 }
249
250 static void
251 gtk_accessible_widget_destroyed (GtkWidget     *widget,
252                                  GtkAccessible *accessible)
253 {
254   gtk_accessible_set_widget (accessible, NULL);
255 }
256
257 static void
258 gtk_accessible_real_connect_widget_destroyed (GtkAccessible *accessible)
259 {
260   GtkAccessiblePrivate *priv = accessible->priv;
261
262   if (priv->widget)
263     g_signal_connect (priv->widget, "destroy",
264                       G_CALLBACK (gtk_accessible_widget_destroyed), accessible);
265 }