1 /* GAIL - The GNOME Accessibility Implementation Library
2 * Copyright 2001, 2002, 2003 Sun Microsystems Inc.
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.
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.
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.
24 #include <gdk/gdkkeysyms.h>
25 #include "gailbutton.h"
26 #include <libgail-util/gailmisc.h>
28 #define GAIL_BUTTON_ATTACHED_MENUS "gtk-attached-menus"
30 static void gail_button_class_init (GailButtonClass *klass);
31 static void gail_button_init (GailButton *button);
33 static G_CONST_RETURN gchar* gail_button_get_name (AtkObject *obj);
34 static gint gail_button_get_n_children (AtkObject *obj);
35 static AtkObject* gail_button_ref_child (AtkObject *obj,
37 static AtkStateSet* gail_button_ref_state_set (AtkObject *obj);
38 static void gail_button_notify_label_gtk (GObject *obj,
41 static void gail_button_label_map_gtk (GtkWidget *widget,
44 static void gail_button_real_initialize (AtkObject *obj,
46 static void gail_button_finalize (GObject *object);
47 static void gail_button_init_textutil (GailButton *button,
50 static void gail_button_pressed_enter_handler (GtkWidget *widget);
51 static void gail_button_released_leave_handler (GtkWidget *widget);
52 static gint gail_button_real_add_gtk (GtkContainer *container,
57 static void atk_action_interface_init (AtkActionIface *iface);
58 static gboolean gail_button_do_action (AtkAction *action,
60 static gboolean idle_do_action (gpointer data);
61 static gint gail_button_get_n_actions (AtkAction *action);
62 static G_CONST_RETURN gchar* gail_button_get_description(AtkAction *action,
64 static G_CONST_RETURN gchar* gail_button_get_keybinding (AtkAction *action,
66 static G_CONST_RETURN gchar* gail_button_action_get_name(AtkAction *action,
68 static gboolean gail_button_set_description(AtkAction *action,
71 static void gail_button_notify_label_weak_ref (gpointer data,
73 static void gail_button_notify_weak_ref (gpointer data,
78 static void atk_image_interface_init (AtkImageIface *iface);
79 static G_CONST_RETURN gchar* gail_button_get_image_description
81 static void gail_button_get_image_position
85 AtkCoordType coord_type);
86 static void gail_button_get_image_size (AtkImage *image,
89 static gboolean gail_button_set_image_description
91 const gchar *description);
94 static void atk_text_interface_init (AtkTextIface *iface);
96 static gchar* gail_button_get_text (AtkText *text,
99 static gunichar gail_button_get_character_at_offset(AtkText *text,
101 static gchar* gail_button_get_text_before_offset(AtkText *text,
103 AtkTextBoundary boundary_type,
106 static gchar* gail_button_get_text_at_offset (AtkText *text,
108 AtkTextBoundary boundary_type,
111 static gchar* gail_button_get_text_after_offset(AtkText *text,
113 AtkTextBoundary boundary_type,
116 static gint gail_button_get_character_count (AtkText *text);
117 static void gail_button_get_character_extents (AtkText *text,
123 AtkCoordType coords);
124 static gint gail_button_get_offset_at_point (AtkText *text,
127 AtkCoordType coords);
128 static AtkAttributeSet* gail_button_get_run_attributes
133 static AtkAttributeSet* gail_button_get_default_attributes
135 static GtkImage* get_image_from_button (GtkWidget *button);
136 static GtkWidget* get_label_from_button (GtkWidget *button,
138 gboolean allow_many);
139 static gint get_n_labels_from_button (GtkWidget *button);
140 static void set_role_for_button (AtkObject *accessible,
143 static gint get_n_attached_menus (GtkWidget *widget);
144 static GtkWidget* get_nth_attached_menu (GtkWidget *widget,
147 G_DEFINE_TYPE_WITH_CODE (GailButton, gail_button, GAIL_TYPE_CONTAINER,
148 G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init)
149 G_IMPLEMENT_INTERFACE (ATK_TYPE_IMAGE, atk_image_interface_init)
150 G_IMPLEMENT_INTERFACE (ATK_TYPE_TEXT, atk_text_interface_init))
153 gail_button_class_init (GailButtonClass *klass)
155 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
156 AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
157 GailContainerClass *container_class;
159 container_class = (GailContainerClass*)klass;
161 gobject_class->finalize = gail_button_finalize;
163 class->get_name = gail_button_get_name;
164 class->get_n_children = gail_button_get_n_children;
165 class->ref_child = gail_button_ref_child;
166 class->ref_state_set = gail_button_ref_state_set;
167 class->initialize = gail_button_real_initialize;
169 container_class->add_gtk = gail_button_real_add_gtk;
170 container_class->remove_gtk = NULL;
174 gail_button_init (GailButton *button)
176 button->click_description = NULL;
177 button->press_description = NULL;
178 button->release_description = NULL;
179 button->click_keybinding = NULL;
180 button->action_queue = NULL;
181 button->action_idle_handler = 0;
182 button->textutil = NULL;
185 static G_CONST_RETURN gchar*
186 gail_button_get_name (AtkObject *obj)
188 G_CONST_RETURN gchar* name = NULL;
190 g_return_val_if_fail (GAIL_IS_BUTTON (obj), NULL);
192 name = ATK_OBJECT_CLASS (gail_button_parent_class)->get_name (obj);
196 * Get the text on the label
201 widget = GTK_ACCESSIBLE (obj)->widget;
208 g_return_val_if_fail (GTK_IS_BUTTON (widget), NULL);
210 child = get_label_from_button (widget, 0, FALSE);
211 if (GTK_IS_LABEL (child))
212 name = gtk_label_get_text (GTK_LABEL (child));
217 image = get_image_from_button (widget);
218 if (GTK_IS_IMAGE (image))
222 atk_obj = gtk_widget_get_accessible (GTK_WIDGET (image));
223 name = atk_object_get_name (atk_obj);
231 * A DownArrow in a GtkToggltButton whose parent is not a ColorCombo
232 * has press as default action.
235 gail_button_is_default_press (GtkWidget *widget)
239 gboolean ret = FALSE;
240 const gchar *parent_type_name;
242 child = GTK_BIN (widget)->child;
243 if (GTK_IS_ARROW (child) &&
244 GTK_ARROW (child)->arrow_type == GTK_ARROW_DOWN)
246 parent = gtk_widget_get_parent (widget);
249 parent_type_name = g_type_name (G_OBJECT_TYPE (parent));
250 if (strcmp (parent_type_name, "ColorCombo"))
259 gail_button_real_initialize (AtkObject *obj,
262 GailButton *button = GAIL_BUTTON (obj);
266 ATK_OBJECT_CLASS (gail_button_parent_class)->initialize (obj, data);
268 button->state = GTK_STATE_NORMAL;
270 g_signal_connect (data,
272 G_CALLBACK (gail_button_pressed_enter_handler),
274 g_signal_connect (data,
276 G_CALLBACK (gail_button_pressed_enter_handler),
278 g_signal_connect (data,
280 G_CALLBACK (gail_button_released_leave_handler),
282 g_signal_connect (data,
284 G_CALLBACK (gail_button_released_leave_handler),
288 widget = GTK_WIDGET (data);
289 label = get_label_from_button (widget, 0, FALSE);
290 if (GTK_IS_LABEL (label))
292 if (GTK_WIDGET_MAPPED (label))
293 gail_button_init_textutil (button, label);
295 g_signal_connect (label,
297 G_CALLBACK (gail_button_label_map_gtk),
300 button->default_is_press = gail_button_is_default_press (widget);
302 set_role_for_button (obj, data);
306 gail_button_label_map_gtk (GtkWidget *widget,
311 button = GAIL_BUTTON (data);
312 gail_button_init_textutil (button, widget);
316 gail_button_notify_label_gtk (GObject *obj,
320 AtkObject* atk_obj = ATK_OBJECT (data);
322 GailButton *gail_button;
324 if (strcmp (pspec->name, "label") == 0)
326 const gchar* label_text;
328 label = GTK_LABEL (obj);
330 label_text = gtk_label_get_text (label);
332 gail_button = GAIL_BUTTON (atk_obj);
333 gail_text_util_text_setup (gail_button->textutil, label_text);
335 if (atk_obj->name == NULL)
338 * The label has changed so notify a change in accessible-name
340 g_object_notify (G_OBJECT (atk_obj), "accessible-name");
343 * The label is the only property which can be changed
345 g_signal_emit_by_name (atk_obj, "visible_data_changed");
350 gail_button_notify_weak_ref (gpointer data, GObject* obj)
352 GtkLabel *label = NULL;
354 AtkObject* atk_obj = ATK_OBJECT (obj);
355 if (data && GTK_IS_WIDGET (data))
357 label = GTK_LABEL (data);
360 g_signal_handlers_disconnect_by_func (label,
361 (GCallback) gail_button_notify_label_gtk,
362 GAIL_BUTTON (atk_obj));
363 g_object_weak_unref (G_OBJECT (label),
364 gail_button_notify_label_weak_ref,
365 GAIL_BUTTON (atk_obj));
371 gail_button_notify_label_weak_ref (gpointer data, GObject* obj)
373 GtkLabel *label = NULL;
374 GailButton *button = NULL;
376 label = GTK_LABEL (obj);
377 if (data && GAIL_IS_BUTTON (data))
379 button = GAIL_BUTTON (ATK_OBJECT (data));
381 g_object_weak_unref (G_OBJECT (button), gail_button_notify_weak_ref,
388 gail_button_init_textutil (GailButton *button,
391 const gchar *label_text;
393 if (button->textutil)
394 g_object_unref (button->textutil);
395 button->textutil = gail_text_util_new ();
396 label_text = gtk_label_get_text (GTK_LABEL (label));
397 gail_text_util_text_setup (button->textutil, label_text);
398 g_object_weak_ref (G_OBJECT (button),
399 gail_button_notify_weak_ref, label);
400 g_object_weak_ref (G_OBJECT (label),
401 gail_button_notify_label_weak_ref, button);
402 g_signal_connect (label,
404 (GCallback) gail_button_notify_label_gtk,
409 gail_button_real_add_gtk (GtkContainer *container,
416 if (GTK_IS_LABEL (widget))
418 const gchar* label_text;
420 label = GTK_LABEL (widget);
423 button = GAIL_BUTTON (data);
424 if (!button->textutil)
425 gail_button_init_textutil (button, widget);
428 label_text = gtk_label_get_text (label);
429 gail_text_util_text_setup (button->textutil, label_text);
437 atk_action_interface_init (AtkActionIface *iface)
439 iface->do_action = gail_button_do_action;
440 iface->get_n_actions = gail_button_get_n_actions;
441 iface->get_description = gail_button_get_description;
442 iface->get_keybinding = gail_button_get_keybinding;
443 iface->get_name = gail_button_action_get_name;
444 iface->set_description = gail_button_set_description;
448 gail_button_do_action (AtkAction *action,
453 gboolean return_value = TRUE;
455 widget = GTK_ACCESSIBLE (action)->widget;
462 if (!gtk_widget_is_sensitive (widget) || !gtk_widget_get_visible (widget))
465 button = GAIL_BUTTON (action);
472 if (!button->action_queue)
474 button->action_queue = g_queue_new ();
476 g_queue_push_head (button->action_queue, GINT_TO_POINTER(i));
477 if (!button->action_idle_handler)
478 button->action_idle_handler = gdk_threads_add_idle (idle_do_action, button);
481 return_value = FALSE;
488 idle_do_action (gpointer data)
492 GailButton *gail_button;
495 gail_button = GAIL_BUTTON (data);
496 gail_button->action_idle_handler = 0;
497 widget = GTK_ACCESSIBLE (gail_button)->widget;
498 tmp_event.button.type = GDK_BUTTON_RELEASE;
499 tmp_event.button.window = widget->window;
500 tmp_event.button.button = 1;
501 tmp_event.button.send_event = TRUE;
502 tmp_event.button.time = GDK_CURRENT_TIME;
503 tmp_event.button.axes = NULL;
505 g_object_ref (gail_button);
507 if (widget == NULL /* State is defunct */ ||
508 !gtk_widget_is_sensitive (widget) || !gtk_widget_get_visible (widget))
510 g_object_unref (gail_button);
514 gtk_widget_event (widget, &tmp_event);
516 button = GTK_BUTTON (widget);
517 while (!g_queue_is_empty (gail_button->action_queue))
519 gint action_number = GPOINTER_TO_INT(g_queue_pop_head (gail_button->action_queue));
520 if (gail_button->default_is_press)
522 if (action_number == 0)
524 else if (action_number == 1)
527 switch (action_number)
532 button->in_button = TRUE;
533 g_signal_emit_by_name (button, "enter");
535 * Simulate a button press event. calling gtk_button_pressed() does
536 * not get the job done for a GtkOptionMenu.
538 tmp_event.button.type = GDK_BUTTON_PRESS;
539 tmp_event.button.window = widget->window;
540 tmp_event.button.button = 1;
541 tmp_event.button.send_event = TRUE;
542 tmp_event.button.time = GDK_CURRENT_TIME;
543 tmp_event.button.axes = NULL;
545 gtk_widget_event (widget, &tmp_event);
548 tmp_event.button.type = GDK_BUTTON_RELEASE;
549 gtk_widget_event (widget, &tmp_event);
550 button->in_button = FALSE;
551 g_signal_emit_by_name (button, "leave");
554 button->in_button = TRUE;
555 g_signal_emit_by_name (button, "enter");
557 * Simulate a button press event. calling gtk_button_pressed() does
558 * not get the job done for a GtkOptionMenu.
560 tmp_event.button.type = GDK_BUTTON_PRESS;
561 tmp_event.button.window = widget->window;
562 tmp_event.button.button = 1;
563 tmp_event.button.send_event = TRUE;
564 tmp_event.button.time = GDK_CURRENT_TIME;
565 tmp_event.button.axes = NULL;
567 gtk_widget_event (widget, &tmp_event);
570 button->in_button = FALSE;
571 g_signal_emit_by_name (button, "leave");
574 g_assert_not_reached ();
578 g_object_unref (gail_button);
583 gail_button_get_n_actions (AtkAction *action)
588 static G_CONST_RETURN gchar*
589 gail_button_get_description (AtkAction *action,
593 G_CONST_RETURN gchar *return_value;
595 button = GAIL_BUTTON (action);
597 if (button->default_is_press)
607 return_value = button->click_description;
610 return_value = button->press_description;
613 return_value = button->release_description;
622 static G_CONST_RETURN gchar*
623 gail_button_get_keybinding (AtkAction *action,
627 gchar *return_value = NULL;
629 button = GAIL_BUTTON (action);
630 if (button->default_is_press)
642 * We look for a mnemonic on the label
648 widget = GTK_ACCESSIBLE (button)->widget;
655 g_return_val_if_fail (GTK_IS_BUTTON (widget), NULL);
657 label = get_label_from_button (widget, 0, FALSE);
658 if (GTK_IS_LABEL (label))
660 key_val = gtk_label_get_mnemonic_keyval (GTK_LABEL (label));
661 if (key_val != GDK_VoidSymbol)
662 return_value = gtk_accelerator_name (key_val, GDK_MOD1_MASK);
664 if (return_value == NULL)
666 /* Find labelled-by relation */
668 AtkRelation *relation;
670 gpointer target_object;
672 set = atk_object_ref_relation_set (ATK_OBJECT (action));
675 relation = atk_relation_set_get_relation_by_type (set, ATK_RELATION_LABELLED_BY);
678 target = atk_relation_get_target (relation);
680 target_object = g_ptr_array_index (target, 0);
681 if (GTK_IS_ACCESSIBLE (target_object))
683 label = GTK_ACCESSIBLE (target_object)->widget;
686 g_object_unref (set);
689 if (GTK_IS_LABEL (label))
691 key_val = gtk_label_get_mnemonic_keyval (GTK_LABEL (label));
692 if (key_val != GDK_VoidSymbol)
693 return_value = gtk_accelerator_name (key_val, GDK_MOD1_MASK);
696 g_free (button->click_keybinding);
697 button->click_keybinding = return_value;
706 static G_CONST_RETURN gchar*
707 gail_button_action_get_name (AtkAction *action,
710 G_CONST_RETURN gchar *return_value;
713 button = GAIL_BUTTON (action);
715 if (button->default_is_press)
726 * This action is a "click" to activate a button or "toggle" to change
727 * the state of a toggle button check box or radio button.
729 return_value = "click";
733 * This action simulates a button press by simulating moving the
734 * mouse into the button followed by pressing the left mouse button.
736 return_value = "press";
740 * This action simulates releasing the left mouse button outside the
743 * To simulate releasing the left mouse button inside the button use
746 return_value = "release";
756 gail_button_set_description (AtkAction *action,
763 button = GAIL_BUTTON (action);
765 if (button->default_is_press)
775 value = &button->click_description;
778 value = &button->press_description;
781 value = &button->release_description;
790 *value = g_strdup (desc);
798 gail_button_get_n_children (AtkObject* obj)
803 g_return_val_if_fail (GAIL_IS_BUTTON (obj), 0);
805 widget = GTK_ACCESSIBLE (obj)->widget;
813 * Check whether we have an attached menus for PanelMenuButton
815 n_children = get_n_attached_menus (widget);
819 n_children = get_n_labels_from_button (widget);
827 gail_button_ref_child (AtkObject *obj,
831 GtkWidget *child_widget;
834 g_return_val_if_fail (GAIL_IS_BUTTON (obj), NULL);
836 widget = GTK_ACCESSIBLE (obj)->widget;
843 if (i >= gail_button_get_n_children (obj))
846 if (get_n_attached_menus (widget) > 0)
848 child_widget = get_nth_attached_menu (widget, i);
855 if (get_n_labels_from_button (widget) > 1)
857 child_widget = get_label_from_button (widget, i, TRUE);
863 child = gtk_widget_get_accessible (child_widget);
864 g_object_ref (child);
873 gail_button_ref_state_set (AtkObject *obj)
875 AtkStateSet *state_set;
878 state_set = ATK_OBJECT_CLASS (gail_button_parent_class)->ref_state_set (obj);
879 widget = GTK_ACCESSIBLE (obj)->widget;
884 if (GTK_WIDGET_STATE (widget) == GTK_STATE_ACTIVE)
885 atk_state_set_add_state (state_set, ATK_STATE_ARMED);
887 if (!gtk_widget_get_can_focus (widget))
888 atk_state_set_remove_state (state_set, ATK_STATE_SELECTABLE);
895 * This is the signal handler for the "pressed" or "enter" signal handler
898 * If the state is now GTK_STATE_ACTIVE we notify a property change
901 gail_button_pressed_enter_handler (GtkWidget *widget)
903 AtkObject *accessible;
905 if (GTK_WIDGET_STATE (widget) == GTK_STATE_ACTIVE)
907 accessible = gtk_widget_get_accessible (widget);
908 atk_object_notify_state_change (accessible, ATK_STATE_ARMED, TRUE);
909 GAIL_BUTTON (accessible)->state = GTK_STATE_ACTIVE;
914 * This is the signal handler for the "released" or "leave" signal handler
917 * If the state was GTK_STATE_ACTIVE we notify a property change
920 gail_button_released_leave_handler (GtkWidget *widget)
922 AtkObject *accessible;
924 accessible = gtk_widget_get_accessible (widget);
925 if (GAIL_BUTTON (accessible)->state == GTK_STATE_ACTIVE)
927 atk_object_notify_state_change (accessible, ATK_STATE_ARMED, FALSE);
928 GAIL_BUTTON (accessible)->state = GTK_STATE_NORMAL;
933 atk_image_interface_init (AtkImageIface *iface)
935 iface->get_image_description = gail_button_get_image_description;
936 iface->get_image_position = gail_button_get_image_position;
937 iface->get_image_size = gail_button_get_image_size;
938 iface->set_image_description = gail_button_set_image_description;
942 get_image_from_button (GtkWidget *button)
946 GtkImage *image = NULL;
948 child = gtk_bin_get_child (GTK_BIN (button));
949 if (GTK_IS_IMAGE (child))
950 image = GTK_IMAGE (child);
953 if (GTK_IS_ALIGNMENT (child))
954 child = gtk_bin_get_child (GTK_BIN (child));
955 if (GTK_IS_CONTAINER (child))
957 list = gtk_container_get_children (GTK_CONTAINER (child));
960 if (GTK_IS_IMAGE (list->data))
961 image = GTK_IMAGE (list->data);
969 static G_CONST_RETURN gchar*
970 gail_button_get_image_description (AtkImage *image) {
973 GtkImage *button_image;
976 widget = GTK_ACCESSIBLE (image)->widget;
984 button_image = get_image_from_button (widget);
986 if (button_image != NULL)
988 obj = gtk_widget_get_accessible (GTK_WIDGET (button_image));
989 return atk_image_get_image_description (ATK_IMAGE (obj));
996 gail_button_get_image_position (AtkImage *image,
999 AtkCoordType coord_type)
1002 GtkImage *button_image;
1005 widget = GTK_ACCESSIBLE (image)->widget;
1017 button_image = get_image_from_button (widget);
1019 if (button_image != NULL)
1021 obj = gtk_widget_get_accessible (GTK_WIDGET (button_image));
1022 atk_component_get_position (ATK_COMPONENT (obj), x, y, coord_type);
1032 gail_button_get_image_size (AtkImage *image,
1037 GtkImage *button_image;
1040 widget = GTK_ACCESSIBLE (image)->widget;
1052 button_image = get_image_from_button (widget);
1054 if (button_image != NULL)
1056 obj = gtk_widget_get_accessible (GTK_WIDGET (button_image));
1057 atk_image_get_image_size (ATK_IMAGE (obj), width, height);
1067 gail_button_set_image_description (AtkImage *image,
1068 const gchar *description)
1071 GtkImage *button_image;
1074 widget = GTK_ACCESSIBLE (image)->widget;
1082 button_image = get_image_from_button (widget);
1084 if (button_image != NULL)
1086 obj = gtk_widget_get_accessible (GTK_WIDGET (button_image));
1087 return atk_image_set_image_description (ATK_IMAGE (obj), description);
1096 atk_text_interface_init (AtkTextIface *iface)
1098 iface->get_text = gail_button_get_text;
1099 iface->get_character_at_offset = gail_button_get_character_at_offset;
1100 iface->get_text_before_offset = gail_button_get_text_before_offset;
1101 iface->get_text_at_offset = gail_button_get_text_at_offset;
1102 iface->get_text_after_offset = gail_button_get_text_after_offset;
1103 iface->get_character_count = gail_button_get_character_count;
1104 iface->get_character_extents = gail_button_get_character_extents;
1105 iface->get_offset_at_point = gail_button_get_offset_at_point;
1106 iface->get_run_attributes = gail_button_get_run_attributes;
1107 iface->get_default_attributes = gail_button_get_default_attributes;
1111 gail_button_get_text (AtkText *text,
1118 const gchar *label_text;
1120 widget = GTK_ACCESSIBLE (text)->widget;
1122 /* State is defunct */
1125 label = get_label_from_button (widget, 0, FALSE);
1127 if (!GTK_IS_LABEL (label))
1130 button = GAIL_BUTTON (text);
1131 if (!button->textutil)
1132 gail_button_init_textutil (button, label);
1134 label_text = gtk_label_get_text (GTK_LABEL (label));
1136 if (label_text == NULL)
1140 return gail_text_util_get_substring (button->textutil,
1141 start_pos, end_pos);
1146 gail_button_get_text_before_offset (AtkText *text,
1148 AtkTextBoundary boundary_type,
1156 widget = GTK_ACCESSIBLE (text)->widget;
1159 /* State is defunct */
1163 label = get_label_from_button (widget, 0, FALSE);
1165 if (!GTK_IS_LABEL(label))
1168 button = GAIL_BUTTON (text);
1169 if (!button->textutil)
1170 gail_button_init_textutil (button, label);
1172 return gail_text_util_get_text (button->textutil,
1173 gtk_label_get_layout (GTK_LABEL (label)), GAIL_BEFORE_OFFSET,
1174 boundary_type, offset, start_offset, end_offset);
1178 gail_button_get_text_at_offset (AtkText *text,
1180 AtkTextBoundary boundary_type,
1188 widget = GTK_ACCESSIBLE (text)->widget;
1191 /* State is defunct */
1195 label = get_label_from_button (widget, 0, FALSE);
1197 if (!GTK_IS_LABEL(label))
1200 button = GAIL_BUTTON (text);
1201 if (!button->textutil)
1202 gail_button_init_textutil (button, label);
1204 return gail_text_util_get_text (button->textutil,
1205 gtk_label_get_layout (GTK_LABEL (label)), GAIL_AT_OFFSET,
1206 boundary_type, offset, start_offset, end_offset);
1210 gail_button_get_text_after_offset (AtkText *text,
1212 AtkTextBoundary boundary_type,
1220 widget = GTK_ACCESSIBLE (text)->widget;
1224 /* State is defunct */
1229 label = get_label_from_button (widget, 0, FALSE);
1231 if (!GTK_IS_LABEL(label))
1234 button = GAIL_BUTTON (text);
1235 if (!button->textutil)
1236 gail_button_init_textutil (button, label);
1238 return gail_text_util_get_text (button->textutil,
1239 gtk_label_get_layout (GTK_LABEL (label)), GAIL_AFTER_OFFSET,
1240 boundary_type, offset, start_offset, end_offset);
1244 gail_button_get_character_count (AtkText *text)
1249 widget = GTK_ACCESSIBLE (text)->widget;
1251 /* State is defunct */
1254 label = get_label_from_button (widget, 0, FALSE);
1256 if (!GTK_IS_LABEL(label))
1259 return g_utf8_strlen (gtk_label_get_text (GTK_LABEL (label)), -1);
1263 gail_button_get_character_extents (AtkText *text,
1269 AtkCoordType coords)
1273 PangoRectangle char_rect;
1274 gint index, x_layout, y_layout;
1275 const gchar *label_text;
1277 widget = GTK_ACCESSIBLE (text)->widget;
1280 /* State is defunct */
1283 label = get_label_from_button (widget, 0, FALSE);
1285 if (!GTK_IS_LABEL(label))
1288 gtk_label_get_layout_offsets (GTK_LABEL (label), &x_layout, &y_layout);
1289 label_text = gtk_label_get_text (GTK_LABEL (label));
1290 index = g_utf8_offset_to_pointer (label_text, offset) - label_text;
1291 pango_layout_index_to_pos (gtk_label_get_layout (GTK_LABEL (label)), index, &char_rect);
1293 gail_misc_get_extents_from_pango_rectangle (label, &char_rect,
1294 x_layout, y_layout, x, y, width, height, coords);
1298 gail_button_get_offset_at_point (AtkText *text,
1301 AtkCoordType coords)
1305 gint index, x_layout, y_layout;
1306 const gchar *label_text;
1308 widget = GTK_ACCESSIBLE (text)->widget;
1310 /* State is defunct */
1312 label = get_label_from_button (widget, 0, FALSE);
1314 if (!GTK_IS_LABEL(label))
1317 gtk_label_get_layout_offsets (GTK_LABEL (label), &x_layout, &y_layout);
1319 index = gail_misc_get_index_at_point_in_layout (label,
1320 gtk_label_get_layout (GTK_LABEL (label)),
1321 x_layout, y_layout, x, y, coords);
1322 label_text = gtk_label_get_text (GTK_LABEL (label));
1325 if (coords == ATK_XY_WINDOW || coords == ATK_XY_SCREEN)
1326 return g_utf8_strlen (label_text, -1);
1331 return g_utf8_pointer_to_offset (label_text, label_text + index);
1334 static AtkAttributeSet*
1335 gail_button_get_run_attributes (AtkText *text,
1342 AtkAttributeSet *at_set = NULL;
1343 GtkJustification justify;
1344 GtkTextDirection dir;
1346 widget = GTK_ACCESSIBLE (text)->widget;
1348 /* State is defunct */
1351 label = get_label_from_button (widget, 0, FALSE);
1353 if (!GTK_IS_LABEL(label))
1356 /* Get values set for entire label, if any */
1357 justify = gtk_label_get_justify (GTK_LABEL (label));
1358 if (justify != GTK_JUSTIFY_CENTER)
1360 at_set = gail_misc_add_attribute (at_set,
1361 ATK_TEXT_ATTR_JUSTIFICATION,
1362 g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_JUSTIFICATION, justify)));
1364 dir = gtk_widget_get_direction (label);
1365 if (dir == GTK_TEXT_DIR_RTL)
1367 at_set = gail_misc_add_attribute (at_set,
1368 ATK_TEXT_ATTR_DIRECTION,
1369 g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_DIRECTION, dir)));
1372 at_set = gail_misc_layout_get_run_attributes (at_set,
1373 gtk_label_get_layout (GTK_LABEL (label)),
1374 (gchar *) gtk_label_get_text (GTK_LABEL (label)),
1381 static AtkAttributeSet*
1382 gail_button_get_default_attributes (AtkText *text)
1386 AtkAttributeSet *at_set = NULL;
1388 widget = GTK_ACCESSIBLE (text)->widget;
1390 /* State is defunct */
1393 label = get_label_from_button (widget, 0, FALSE);
1395 if (!GTK_IS_LABEL(label))
1398 at_set = gail_misc_get_default_attributes (at_set,
1399 gtk_label_get_layout (GTK_LABEL (label)),
1405 gail_button_get_character_at_offset (AtkText *text,
1410 const gchar *string;
1413 widget = GTK_ACCESSIBLE (text)->widget;
1415 /* State is defunct */
1418 label = get_label_from_button (widget, 0, FALSE);
1420 if (!GTK_IS_LABEL(label))
1422 string = gtk_label_get_text (GTK_LABEL (label));
1423 if (offset >= g_utf8_strlen (string, -1))
1425 index = g_utf8_offset_to_pointer (string, offset);
1427 return g_utf8_get_char (index);
1431 gail_button_finalize (GObject *object)
1433 GailButton *button = GAIL_BUTTON (object);
1435 g_free (button->click_description);
1436 g_free (button->press_description);
1437 g_free (button->release_description);
1438 g_free (button->click_keybinding);
1439 if (button->action_idle_handler)
1441 g_source_remove (button->action_idle_handler);
1442 button->action_idle_handler = 0;
1444 if (button->action_queue)
1446 g_queue_free (button->action_queue);
1448 if (button->textutil)
1450 g_object_unref (button->textutil);
1452 G_OBJECT_CLASS (gail_button_parent_class)->finalize (object);
1456 find_label_child (GtkContainer *container,
1458 gboolean allow_many)
1460 GList *children, *tmp_list;
1463 children = gtk_container_get_children (container);
1466 for (tmp_list = children; tmp_list != NULL; tmp_list = tmp_list->next)
1468 if (GTK_IS_LABEL (tmp_list->data))
1477 child = GTK_WIDGET (tmp_list->data);
1483 child = GTK_WIDGET (tmp_list->data);
1490 * Label for button which are GtkTreeView column headers are in a
1491 * GtkHBox in a GtkAlignment.
1493 else if (GTK_IS_ALIGNMENT (tmp_list->data))
1497 widget = gtk_bin_get_child (GTK_BIN (tmp_list->data));
1498 if (GTK_IS_LABEL (widget))
1520 else if (GTK_IS_CONTAINER (tmp_list->data))
1522 child = find_label_child (GTK_CONTAINER (tmp_list->data), index, allow_many);
1527 g_list_free (children);
1532 get_label_from_button (GtkWidget *button,
1534 gboolean allow_many)
1538 if (index > 0 && !allow_many)
1539 g_warning ("Inconsistent values passed to get_label_from_button");
1541 child = gtk_bin_get_child (GTK_BIN (button));
1542 if (GTK_IS_ALIGNMENT (child))
1543 child = gtk_bin_get_child (GTK_BIN (child));
1545 if (GTK_IS_CONTAINER (child))
1546 child = find_label_child (GTK_CONTAINER (child), &index, allow_many);
1547 else if (!GTK_IS_LABEL (child))
1554 count_labels (GtkContainer *container,
1557 GList *children, *tmp_list;
1559 children = gtk_container_get_children (container);
1561 for (tmp_list = children; tmp_list != NULL; tmp_list = tmp_list->next)
1563 if (GTK_IS_LABEL (tmp_list->data))
1568 * Label for button which are GtkTreeView column headers are in a
1569 * GtkHBox in a GtkAlignment.
1571 else if (GTK_IS_ALIGNMENT (tmp_list->data))
1575 widget = gtk_bin_get_child (GTK_BIN (tmp_list->data));
1576 if (GTK_IS_LABEL (widget))
1579 else if (GTK_IS_CONTAINER (tmp_list->data))
1581 count_labels (GTK_CONTAINER (tmp_list->data), n_labels);
1584 g_list_free (children);
1588 get_n_labels_from_button (GtkWidget *button)
1595 child = gtk_bin_get_child (GTK_BIN (button));
1596 if (GTK_IS_ALIGNMENT (child))
1597 child = gtk_bin_get_child (GTK_BIN (child));
1599 if (GTK_IS_CONTAINER (child))
1600 count_labels (GTK_CONTAINER (child), &n_labels);
1606 set_role_for_button (AtkObject *accessible,
1612 parent = gtk_widget_get_parent (button);
1613 if (GTK_IS_TREE_VIEW (parent))
1615 role = ATK_ROLE_TABLE_COLUMN_HEADER;
1617 * Even though the accessible parent of the column header will
1618 * be reported as the table because the parent widget of the
1619 * GtkTreeViewColumn's button is the GtkTreeView we set
1620 * the accessible parent for column header to be the table
1621 * to ensure that atk_object_get_index_in_parent() returns
1622 * the correct value; see gail_widget_get_index_in_parent().
1624 atk_object_set_parent (accessible, gtk_widget_get_accessible (parent));
1627 role = ATK_ROLE_PUSH_BUTTON;
1629 accessible->role = role;
1633 get_n_attached_menus (GtkWidget *widget)
1640 list_menus = g_object_get_data (G_OBJECT (widget), GAIL_BUTTON_ATTACHED_MENUS);
1641 if (list_menus == NULL)
1644 return g_list_length (list_menus);
1648 get_nth_attached_menu (GtkWidget *widget,
1651 GtkWidget *attached_menu;
1657 list_menus = g_object_get_data (G_OBJECT (widget), GAIL_BUTTON_ATTACHED_MENUS);
1658 if (list_menus == NULL ||
1659 index >= g_list_length (list_menus))
1662 attached_menu = (GtkWidget *) g_list_nth_data (list_menus, index);
1664 return attached_menu;