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