]> Pileus Git - ~andy/gtk/blob - gtk/a11y/gtknotebookpageaccessible.c
8346c0ee66cf21d80465db6d0eedf743312a5420
[~andy/gtk] / gtk / a11y / gtknotebookpageaccessible.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, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "config.h"
19
20 #include <string.h>
21 #include <gtk/gtk.h>
22 #include "gtknotebookpageaccessible.h"
23
24
25 struct _GtkNotebookPageAccessiblePrivate
26 {
27   GtkAccessible *notebook;
28   GtkWidget *child;
29 };
30
31 static void atk_component_interface_init (AtkComponentIface *iface);
32
33 G_DEFINE_TYPE_WITH_CODE (GtkNotebookPageAccessible, _gtk_notebook_page_accessible, ATK_TYPE_OBJECT,
34                          G_IMPLEMENT_INTERFACE (ATK_TYPE_COMPONENT, atk_component_interface_init))
35
36
37 static GtkWidget *
38 find_label_child (GtkContainer *container)
39 {
40   GList *children, *tmp_list;
41   GtkWidget *child;
42
43   children = gtk_container_get_children (container);
44
45   child = NULL;
46   for (tmp_list = children; tmp_list != NULL; tmp_list = tmp_list->next)
47     {
48       if (GTK_IS_LABEL (tmp_list->data))
49         {
50           child = GTK_WIDGET (tmp_list->data);
51           break;
52         }
53       else if (GTK_IS_CONTAINER (tmp_list->data))
54         {
55           child = find_label_child (GTK_CONTAINER (tmp_list->data));
56           if (child)
57             break;
58         }
59     }
60   g_list_free (children);
61
62   return child;
63 }
64
65 static GtkWidget *
66 get_label_from_notebook_page (GtkNotebookPageAccessible *page)
67 {
68   GtkWidget *child;
69   GtkNotebook *notebook;
70
71   notebook = GTK_NOTEBOOK (gtk_accessible_get_widget (page->priv->notebook));
72   if (!notebook)
73     return NULL;
74
75   if (!gtk_notebook_get_show_tabs (notebook))
76     return NULL;
77
78   child = gtk_notebook_get_tab_label (notebook, page->priv->child);
79
80   if (GTK_IS_LABEL (child))
81     return child;
82
83   if (GTK_IS_CONTAINER (child))
84     child = find_label_child (GTK_CONTAINER (child));
85
86   return child;
87 }
88
89 static const gchar *
90 gtk_notebook_page_accessible_get_name (AtkObject *accessible)
91 {
92   GtkWidget *label;
93
94   if (accessible->name != NULL)
95     return accessible->name;
96
97   label = get_label_from_notebook_page (GTK_NOTEBOOK_PAGE_ACCESSIBLE (accessible));
98   if (GTK_IS_LABEL (label))
99     return gtk_label_get_text (GTK_LABEL (label));
100
101   return NULL;
102 }
103
104 static AtkObject *
105 gtk_notebook_page_accessible_get_parent (AtkObject *accessible)
106 {
107   GtkNotebookPageAccessible *page;
108
109   page = GTK_NOTEBOOK_PAGE_ACCESSIBLE (accessible);
110
111   return ATK_OBJECT (page->priv->notebook);
112 }
113
114 static gint
115 gtk_notebook_page_accessible_get_n_children (AtkObject *accessible)
116 {
117   return 1;
118 }
119
120 static AtkObject *
121 gtk_notebook_page_accessible_ref_child (AtkObject *accessible,
122                                         gint       i)
123 {
124   AtkObject *child_obj;
125   GtkNotebookPageAccessible *page = NULL;
126
127   if (i != 0)
128     return NULL;
129
130   page = GTK_NOTEBOOK_PAGE_ACCESSIBLE (accessible);
131   if (!page->priv->child)
132     return NULL;
133
134   child_obj = gtk_widget_get_accessible (page->priv->child);
135   g_object_ref (child_obj);
136
137   return child_obj;
138 }
139
140 static AtkStateSet *
141 gtk_notebook_page_accessible_ref_state_set (AtkObject *accessible)
142 {
143   AtkStateSet *state_set, *label_state_set, *merged_state_set;
144   AtkObject *atk_label;
145   GtkWidget *label;
146
147   state_set = ATK_OBJECT_CLASS (_gtk_notebook_page_accessible_parent_class)->ref_state_set (accessible);
148
149   label = get_label_from_notebook_page (GTK_NOTEBOOK_PAGE_ACCESSIBLE (accessible));
150   if (label)
151     {
152       atk_label = gtk_widget_get_accessible (label);
153       label_state_set = atk_object_ref_state_set (atk_label);
154       merged_state_set = atk_state_set_or_sets (state_set, label_state_set);
155       g_object_unref (label_state_set);
156       g_object_unref (state_set);
157     }
158   else
159     {
160       AtkObject *child;
161
162       child = atk_object_ref_accessible_child (accessible, 0);
163       if (!child)
164         return state_set;
165
166       merged_state_set = state_set;
167       state_set = atk_object_ref_state_set (child);
168       if (atk_state_set_contains_state (state_set, ATK_STATE_VISIBLE))
169         {
170           atk_state_set_add_state (merged_state_set, ATK_STATE_VISIBLE);
171           if (atk_state_set_contains_state (state_set, ATK_STATE_ENABLED))
172               atk_state_set_add_state (merged_state_set, ATK_STATE_ENABLED);
173           if (atk_state_set_contains_state (state_set, ATK_STATE_SHOWING))
174               atk_state_set_add_state (merged_state_set, ATK_STATE_SHOWING);
175
176         }
177       g_object_unref (state_set);
178       g_object_unref (child);
179     }
180   return merged_state_set;
181 }
182
183 static gint
184 gtk_notebook_page_accessible_get_index_in_parent (AtkObject *accessible)
185 {
186   GtkNotebookPageAccessible *page;
187
188   page = GTK_NOTEBOOK_PAGE_ACCESSIBLE (accessible);
189   if (!page->priv->child)
190     return -1;
191
192   return gtk_notebook_page_num (GTK_NOTEBOOK (gtk_accessible_get_widget (page->priv->notebook)),
193                                 page->priv->child);
194 }
195
196 static void
197 _gtk_notebook_page_accessible_class_init (GtkNotebookPageAccessibleClass *klass)
198 {
199   AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
200
201   class->get_name = gtk_notebook_page_accessible_get_name;
202   class->get_parent = gtk_notebook_page_accessible_get_parent;
203   class->get_n_children = gtk_notebook_page_accessible_get_n_children;
204   class->ref_child = gtk_notebook_page_accessible_ref_child;
205   class->ref_state_set = gtk_notebook_page_accessible_ref_state_set;
206   class->get_index_in_parent = gtk_notebook_page_accessible_get_index_in_parent;
207
208   g_type_class_add_private (klass, sizeof (GtkNotebookPageAccessiblePrivate));
209 }
210
211 static void
212 _gtk_notebook_page_accessible_init (GtkNotebookPageAccessible *page)
213 {
214   page->priv = G_TYPE_INSTANCE_GET_PRIVATE (page,
215                                             GTK_TYPE_NOTEBOOK_PAGE_ACCESSIBLE,
216                                             GtkNotebookPageAccessiblePrivate);
217 }
218
219 static void
220 notify_tab_label (GObject    *object,
221                   GParamSpec *pspec,
222                   AtkObject  *atk_obj)
223 {
224   if (atk_obj->name == NULL)
225     g_object_notify (G_OBJECT (atk_obj), "accessible-name");
226   g_signal_emit_by_name (atk_obj, "visible-data-changed");
227 }
228
229 AtkObject *
230 _gtk_notebook_page_accessible_new (GtkNotebookAccessible *notebook,
231                                    GtkWidget             *child)
232 {
233   GObject *object;
234   AtkObject *atk_object;
235   GtkNotebookPageAccessible *page;
236
237   g_return_val_if_fail (GTK_IS_NOTEBOOK_ACCESSIBLE (notebook), NULL);
238   g_return_val_if_fail (GTK_WIDGET (child), NULL);
239
240   object = g_object_new (GTK_TYPE_NOTEBOOK_PAGE_ACCESSIBLE, NULL);
241
242   page = GTK_NOTEBOOK_PAGE_ACCESSIBLE (object);
243   page->priv->notebook = GTK_ACCESSIBLE (notebook);
244   page->priv->child = child;
245
246   atk_object = ATK_OBJECT (page);
247   atk_object->role = ATK_ROLE_PAGE_TAB;
248   atk_object->layer = ATK_LAYER_WIDGET;
249
250   atk_object_set_parent (gtk_widget_get_accessible (child), atk_object);
251
252   g_signal_connect (gtk_accessible_get_widget (page->priv->notebook),
253                     "child-notify::tab-label",
254                     G_CALLBACK (notify_tab_label), page);
255
256   return atk_object;
257 }
258
259 void
260 _gtk_notebook_page_accessible_invalidate (GtkNotebookPageAccessible *page)
261 {
262   AtkObject *obj = ATK_OBJECT (page);
263   GtkWidget *notebook;
264
265   notebook = gtk_accessible_get_widget (page->priv->notebook);
266   if (notebook)
267     g_signal_handlers_disconnect_by_func (notebook, notify_tab_label, page);
268
269   atk_object_notify_state_change (obj, ATK_STATE_DEFUNCT, TRUE);
270   atk_object_set_parent (obj, NULL);
271   page->priv->notebook = NULL;
272   atk_object_set_parent (gtk_widget_get_accessible (page->priv->child), NULL);
273   page->priv->child = NULL;
274 }
275
276 static AtkObject*
277 gtk_notebook_page_accessible_ref_accessible_at_point (AtkComponent *component,
278                                                       gint          x,
279                                                       gint          y,
280                                                       AtkCoordType  coord_type)
281 {
282   /* There is only one child so we return it */
283   AtkObject* child;
284
285   child = atk_object_ref_accessible_child (ATK_OBJECT (component), 0);
286
287   return child;
288 }
289
290 static void
291 gtk_notebook_page_accessible_get_extents (AtkComponent *component,
292                                           gint         *x,
293                                           gint         *y,
294                                           gint         *width,
295                                           gint         *height,
296                                           AtkCoordType  coord_type)
297 {
298   GtkWidget *label;
299   AtkObject *atk_label;
300
301   label = get_label_from_notebook_page (GTK_NOTEBOOK_PAGE_ACCESSIBLE (component));
302   if (!label)
303     {
304       AtkObject *child;
305
306       *width = 0;
307       *height = 0;
308
309       child = atk_object_ref_accessible_child (ATK_OBJECT (component), 0);
310       if (!child)
311         return;
312
313       atk_component_get_position (ATK_COMPONENT (child), x, y, coord_type);
314       g_object_unref (child);
315     }
316   else
317     {
318       atk_label = gtk_widget_get_accessible (label);
319       atk_component_get_extents (ATK_COMPONENT (atk_label),
320                                  x, y, width, height, coord_type);
321     }
322 }
323
324 static void
325 atk_component_interface_init (AtkComponentIface *iface)
326 {
327   /* We use the default implementations for contains, get_position, get_size */
328   iface->ref_accessible_at_point = gtk_notebook_page_accessible_ref_accessible_at_point;
329   iface->get_extents = gtk_notebook_page_accessible_get_extents;
330 }