1 /* GAIL - The GNOME Accessibility Enabling 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.
25 #include "gaillabel.h"
26 #include "gailwindow.h"
27 #include <libgail-util/gailmisc.h>
29 static void gail_label_class_init (GailLabelClass *klass);
30 static void gail_label_init (GailLabel *label);
31 static void gail_label_real_initialize (AtkObject *obj,
33 static void gail_label_real_notify_gtk (GObject *obj,
35 static void gail_label_map_gtk (GtkWidget *widget,
37 static void gail_label_init_text_util (GailLabel *gail_label,
39 static void gail_label_finalize (GObject *object);
41 static void atk_text_interface_init (AtkTextIface *iface);
45 static const gchar* gail_label_get_name (AtkObject *accessible);
46 static AtkStateSet* gail_label_ref_state_set (AtkObject *accessible);
47 static AtkRelationSet* gail_label_ref_relation_set (AtkObject *accessible);
51 static gchar* gail_label_get_text (AtkText *text,
54 static gunichar gail_label_get_character_at_offset(AtkText *text,
56 static gchar* gail_label_get_text_before_offset(AtkText *text,
58 AtkTextBoundary boundary_type,
61 static gchar* gail_label_get_text_at_offset (AtkText *text,
63 AtkTextBoundary boundary_type,
66 static gchar* gail_label_get_text_after_offset (AtkText *text,
68 AtkTextBoundary boundary_type,
71 static gint gail_label_get_character_count (AtkText *text);
72 static gint gail_label_get_caret_offset (AtkText *text);
73 static gboolean gail_label_set_caret_offset (AtkText *text,
75 static gint gail_label_get_n_selections (AtkText *text);
76 static gchar* gail_label_get_selection (AtkText *text,
80 static gboolean gail_label_add_selection (AtkText *text,
83 static gboolean gail_label_remove_selection (AtkText *text,
85 static gboolean gail_label_set_selection (AtkText *text,
89 static void gail_label_get_character_extents (AtkText *text,
96 static gint gail_label_get_offset_at_point (AtkText *text,
100 static AtkAttributeSet* gail_label_get_run_attributes
105 static AtkAttributeSet* gail_label_get_default_attributes
108 G_DEFINE_TYPE_WITH_CODE (GailLabel, gail_label, GAIL_TYPE_WIDGET,
109 G_IMPLEMENT_INTERFACE (ATK_TYPE_TEXT, atk_text_interface_init))
112 gail_label_class_init (GailLabelClass *klass)
114 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
115 AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
116 GailWidgetClass *widget_class;
118 gobject_class->finalize = gail_label_finalize;
120 widget_class = (GailWidgetClass*)klass;
121 widget_class->notify_gtk = gail_label_real_notify_gtk;
123 class->get_name = gail_label_get_name;
124 class->ref_state_set = gail_label_ref_state_set;
125 class->ref_relation_set = gail_label_ref_relation_set;
126 class->initialize = gail_label_real_initialize;
130 gail_label_init (GailLabel *label)
135 gail_label_real_initialize (AtkObject *obj,
139 GailLabel *gail_label;
141 ATK_OBJECT_CLASS (gail_label_parent_class)->initialize (obj, data);
143 gail_label = GAIL_LABEL (obj);
145 gail_label->window_create_handler = 0;
146 gail_label->has_top_level = FALSE;
147 gail_label->cursor_position = 0;
148 gail_label->selection_bound = 0;
149 gail_label->textutil = NULL;
150 gail_label->label_length = 0;
152 widget = GTK_WIDGET (data);
154 if (gtk_widget_get_mapped (widget))
155 gail_label_init_text_util (gail_label, widget);
157 g_signal_connect (widget,
159 G_CALLBACK (gail_label_map_gtk),
163 * Check whether ancestor of GtkLabel is a GtkButton and if so
164 * set accessible parent for GailLabel
166 while (widget != NULL)
168 widget = gtk_widget_get_parent (widget);
169 if (GTK_IS_BUTTON (widget))
171 atk_object_set_parent (obj, gtk_widget_get_accessible (widget));
176 if (GTK_IS_ACCEL_LABEL (widget))
177 obj->role = ATK_ROLE_ACCEL_LABEL;
179 obj->role = ATK_ROLE_LABEL;
183 gail_label_map_gtk (GtkWidget *widget,
186 GailLabel *gail_label;
188 gail_label = GAIL_LABEL (data);
189 gail_label_init_text_util (gail_label, widget);
193 gail_label_init_text_util (GailLabel *gail_label,
197 const gchar *label_text;
199 if (gail_label->textutil == NULL)
200 gail_label->textutil = gail_text_util_new ();
202 label = GTK_LABEL (widget);
203 label_text = gtk_label_get_text (label);
204 gail_text_util_text_setup (gail_label->textutil, label_text);
206 if (label_text == NULL)
207 gail_label->label_length = 0;
209 gail_label->label_length = g_utf8_strlen (label_text, -1);
213 notify_name_change (AtkObject *atk_obj)
216 GailLabel *gail_label;
220 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (atk_obj));
227 gail_obj = G_OBJECT (atk_obj);
228 label = GTK_LABEL (widget);
229 gail_label = GAIL_LABEL (atk_obj);
231 if (gail_label->textutil == NULL)
235 * Check whether the label has actually changed before emitting
238 if (gail_label->textutil->buffer)
240 GtkTextIter start, end;
241 const char *new_label;
245 gtk_text_buffer_get_start_iter (gail_label->textutil->buffer, &start);
246 gtk_text_buffer_get_end_iter (gail_label->textutil->buffer, &end);
247 old_label = gtk_text_buffer_get_text (gail_label->textutil->buffer, &start, &end, FALSE);
248 new_label = gtk_label_get_text (label);
249 same = strcmp (new_label, old_label);
255 /* Create a delete text and an insert text signal */
257 g_signal_emit_by_name (gail_obj, "text_changed::delete", 0,
258 gail_label->label_length);
260 gail_label_init_text_util (gail_label, widget);
262 g_signal_emit_by_name (gail_obj, "text_changed::insert", 0,
263 gail_label->label_length);
265 if (atk_obj->name == NULL)
267 * The label has changed so notify a change in accessible-name
269 g_object_notify (gail_obj, "accessible-name");
271 g_signal_emit_by_name (gail_obj, "visible_data_changed");
275 window_created (GObject *obj,
278 g_return_if_fail (GAIL_LABEL (data));
280 notify_name_change (ATK_OBJECT (data));
284 gail_label_real_notify_gtk (GObject *obj,
287 GtkWidget *widget = GTK_WIDGET (obj);
288 AtkObject* atk_obj = gtk_widget_get_accessible (widget);
290 GailLabel *gail_label;
292 AtkObject *top_level;
295 gail_label = GAIL_LABEL (atk_obj);
297 if (strcmp (pspec->name, "label") == 0)
300 * We may get a label change for a label which is not attached to an
301 * application. We wait until the toplevel window is created before
302 * emitting the notification.
304 * This happens when [Ctrl+]Alt+Tab is pressed in metacity
306 if (!gail_label->has_top_level)
312 top_level = temp_obj;
313 temp_obj = atk_object_get_parent (top_level);
315 if (atk_object_get_role (top_level) != ATK_ROLE_APPLICATION)
317 if (gail_label->window_create_handler == 0 &&
318 GAIL_IS_WINDOW (top_level))
319 gail_label->window_create_handler = g_signal_connect_after (top_level, "create", G_CALLBACK (window_created), atk_obj);
322 gail_label->has_top_level = TRUE;
324 if (gail_label->has_top_level)
325 notify_name_change (atk_obj);
327 else if (strcmp (pspec->name, "cursor-position") == 0)
329 gint start, end, tmp;
330 gboolean text_caret_moved = FALSE;
331 gboolean selection_changed = FALSE;
333 gail_obj = G_OBJECT (atk_obj);
334 label = GTK_LABEL (widget);
336 if (gail_label->selection_bound != -1 && gail_label->selection_bound < gail_label->cursor_position)
338 tmp = gail_label->selection_bound;
339 gail_label->selection_bound = gail_label->cursor_position;
340 gail_label->cursor_position = tmp;
343 if (gtk_label_get_selection_bounds (label, &start, &end))
345 if (start != gail_label->cursor_position ||
346 end != gail_label->selection_bound)
348 if (end != gail_label->selection_bound)
350 gail_label->selection_bound = start;
351 gail_label->cursor_position = end;
355 gail_label->selection_bound = end;
356 gail_label->cursor_position = start;
358 text_caret_moved = TRUE;
360 selection_changed = TRUE;
365 if (gail_label->cursor_position != gail_label->selection_bound)
366 selection_changed = TRUE;
367 if (gtk_label_get_selectable (label))
369 if (gail_label->cursor_position != -1 && start != gail_label->cursor_position)
370 text_caret_moved = TRUE;
371 if (gail_label->selection_bound != -1 && end != gail_label->selection_bound)
373 text_caret_moved = TRUE;
374 gail_label->cursor_position = end;
375 gail_label->selection_bound = start;
379 gail_label->cursor_position = start;
380 gail_label->selection_bound = end;
385 /* GtkLabel has become non selectable */
387 gail_label->cursor_position = 0;
388 gail_label->selection_bound = 0;
389 text_caret_moved = TRUE;
393 if (text_caret_moved)
394 g_signal_emit_by_name (gail_obj, "text_caret_moved",
395 gail_label->cursor_position);
396 if (selection_changed)
397 g_signal_emit_by_name (gail_obj, "text_selection_changed");
401 GAIL_WIDGET_CLASS (gail_label_parent_class)->notify_gtk (obj, pspec);
405 gail_label_finalize (GObject *object)
407 GailLabel *label = GAIL_LABEL (object);
410 g_object_unref (label->textutil);
411 G_OBJECT_CLASS (gail_label_parent_class)->finalize (object);
418 gail_label_ref_state_set (AtkObject *accessible)
420 AtkStateSet *state_set;
423 state_set = ATK_OBJECT_CLASS (gail_label_parent_class)->ref_state_set (accessible);
424 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
429 atk_state_set_add_state (state_set, ATK_STATE_MULTI_LINE);
435 gail_label_ref_relation_set (AtkObject *obj)
438 AtkRelationSet *relation_set;
440 g_return_val_if_fail (GAIL_IS_LABEL (obj), NULL);
442 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
449 relation_set = ATK_OBJECT_CLASS (gail_label_parent_class)->ref_relation_set (obj);
451 if (!atk_relation_set_contains (relation_set, ATK_RELATION_LABEL_FOR))
454 * Get the mnemonic widget
456 * The relation set is not updated if the mnemonic widget is changed
458 GtkWidget *mnemonic_widget = gtk_label_get_mnemonic_widget (GTK_LABEL (widget));
462 AtkObject *accessible_array[1];
463 AtkRelation* relation;
465 if (!gtk_widget_get_can_focus (mnemonic_widget))
468 * Handle the case where a GtkFileChooserButton is specified as the
469 * mnemonic widget. use the combobox which is a child of the
470 * GtkFileChooserButton as the mnemonic widget. See bug #359843.
472 if (GTK_IS_BOX (mnemonic_widget))
476 list = gtk_container_get_children (GTK_CONTAINER (mnemonic_widget));
477 if (g_list_length (list) == 2)
479 tmpl = g_list_last (list);
480 if (GTK_IS_COMBO_BOX(tmpl->data))
482 mnemonic_widget = GTK_WIDGET(tmpl->data);
488 accessible_array[0] = gtk_widget_get_accessible (mnemonic_widget);
489 relation = atk_relation_new (accessible_array, 1,
490 ATK_RELATION_LABEL_FOR);
491 atk_relation_set_add (relation_set, relation);
493 * Unref the relation so that it is not leaked.
495 g_object_unref (relation);
502 gail_label_get_name (AtkObject *accessible)
506 g_return_val_if_fail (GAIL_IS_LABEL (accessible), NULL);
508 name = ATK_OBJECT_CLASS (gail_label_parent_class)->get_name (accessible);
514 * Get the text on the label
518 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
525 g_return_val_if_fail (GTK_IS_LABEL (widget), NULL);
527 return gtk_label_get_text (GTK_LABEL (widget));
534 atk_text_interface_init (AtkTextIface *iface)
536 iface->get_text = gail_label_get_text;
537 iface->get_character_at_offset = gail_label_get_character_at_offset;
538 iface->get_text_before_offset = gail_label_get_text_before_offset;
539 iface->get_text_at_offset = gail_label_get_text_at_offset;
540 iface->get_text_after_offset = gail_label_get_text_after_offset;
541 iface->get_character_count = gail_label_get_character_count;
542 iface->get_caret_offset = gail_label_get_caret_offset;
543 iface->set_caret_offset = gail_label_set_caret_offset;
544 iface->get_n_selections = gail_label_get_n_selections;
545 iface->get_selection = gail_label_get_selection;
546 iface->add_selection = gail_label_add_selection;
547 iface->remove_selection = gail_label_remove_selection;
548 iface->set_selection = gail_label_set_selection;
549 iface->get_character_extents = gail_label_get_character_extents;
550 iface->get_offset_at_point = gail_label_get_offset_at_point;
551 iface->get_run_attributes = gail_label_get_run_attributes;
552 iface->get_default_attributes = gail_label_get_default_attributes;
556 gail_label_get_text (AtkText *text,
563 const gchar *label_text;
565 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
567 /* State is defunct */
570 label = GTK_LABEL (widget);
572 label_text = gtk_label_get_text (label);
574 if (label_text == NULL)
578 if (GAIL_LABEL (text)->textutil == NULL)
579 gail_label_init_text_util (GAIL_LABEL (text), widget);
580 return gail_text_util_get_substring (GAIL_LABEL(text)->textutil,
586 gail_label_get_text_before_offset (AtkText *text,
588 AtkTextBoundary boundary_type,
595 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
598 /* State is defunct */
602 label = GTK_LABEL (widget);
604 return gail_text_util_get_text (GAIL_LABEL (text)->textutil,
605 gtk_label_get_layout (label), GAIL_BEFORE_OFFSET,
606 boundary_type, offset, start_offset, end_offset);
610 gail_label_get_text_at_offset (AtkText *text,
612 AtkTextBoundary boundary_type,
619 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
622 /* State is defunct */
626 label = GTK_LABEL (widget);
628 return gail_text_util_get_text (GAIL_LABEL (text)->textutil,
629 gtk_label_get_layout (label), GAIL_AT_OFFSET,
630 boundary_type, offset, start_offset, end_offset);
634 gail_label_get_text_after_offset (AtkText *text,
636 AtkTextBoundary boundary_type,
643 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
647 /* State is defunct */
652 label = GTK_LABEL (widget);
654 return gail_text_util_get_text (GAIL_LABEL (text)->textutil,
655 gtk_label_get_layout (label), GAIL_AFTER_OFFSET,
656 boundary_type, offset, start_offset, end_offset);
660 gail_label_get_character_count (AtkText *text)
665 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
667 /* State is defunct */
670 label = GTK_LABEL (widget);
671 return g_utf8_strlen (gtk_label_get_text (label), -1);
675 gail_label_get_caret_offset (AtkText *text)
677 return GAIL_LABEL (text)->cursor_position;
681 gail_label_set_caret_offset (AtkText *text,
687 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
689 /* State is defunct */
692 label = GTK_LABEL (widget);
694 if (gtk_label_get_selectable (label) &&
696 offset <= g_utf8_strlen (gtk_label_get_text (label), -1))
698 gtk_label_select_region (label, offset, offset);
706 gail_label_get_n_selections (AtkText *text)
712 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
714 /* State is defunct */
717 label = GTK_LABEL (widget);
719 if (!gtk_label_get_selectable (label))
722 if (gtk_label_get_selection_bounds (label, &start, &end))
729 gail_label_get_selection (AtkText *text,
737 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
739 /* State is defunct */
742 label = GTK_LABEL (widget);
744 /* Only let the user get the selection if one is set, and if the
745 * selection_num is 0.
747 if (!gtk_label_get_selectable( label) || selection_num != 0)
750 if (gtk_label_get_selection_bounds (label, start_pos, end_pos))
752 const gchar* label_text = gtk_label_get_text (label);
754 if (label_text == NULL)
757 return gail_text_util_get_substring (GAIL_LABEL (text)->textutil,
758 *start_pos, *end_pos);
765 gail_label_add_selection (AtkText *text,
773 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
775 /* State is defunct */
778 label = GTK_LABEL (widget);
780 if (!gtk_label_get_selectable (label))
783 if (! gtk_label_get_selection_bounds (label, &start, &end))
785 gtk_label_select_region (label, start_pos, end_pos);
793 gail_label_remove_selection (AtkText *text,
800 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
802 /* State is defunct */
805 if (selection_num != 0)
808 label = GTK_LABEL (widget);
810 if (!gtk_label_get_selectable (label))
813 if (gtk_label_get_selection_bounds (label, &start, &end))
815 gtk_label_select_region (label, 0, 0);
823 gail_label_set_selection (AtkText *text,
832 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
834 /* State is defunct */
837 if (selection_num != 0)
840 label = GTK_LABEL (widget);
842 if (!gtk_label_get_selectable (label))
845 if (gtk_label_get_selection_bounds (label, &start, &end))
847 gtk_label_select_region (label, start_pos, end_pos);
855 gail_label_get_character_extents (AtkText *text,
865 PangoRectangle char_rect;
866 const gchar *label_text;
867 gint index, x_layout, y_layout;
869 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
872 /* State is defunct */
875 label = GTK_LABEL (widget);
877 gtk_label_get_layout_offsets (label, &x_layout, &y_layout);
878 label_text = gtk_label_get_text (label);
879 index = g_utf8_offset_to_pointer (label_text, offset) - label_text;
880 pango_layout_index_to_pos (gtk_label_get_layout (label), index, &char_rect);
882 gail_misc_get_extents_from_pango_rectangle (widget, &char_rect,
883 x_layout, y_layout, x, y, width, height, coords);
887 gail_label_get_offset_at_point (AtkText *text,
894 const gchar *label_text;
895 gint index, x_layout, y_layout;
897 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
899 /* State is defunct */
901 label = GTK_LABEL (widget);
903 gtk_label_get_layout_offsets (label, &x_layout, &y_layout);
905 index = gail_misc_get_index_at_point_in_layout (widget,
906 gtk_label_get_layout (label),
907 x_layout, y_layout, x, y, coords);
908 label_text = gtk_label_get_text (label);
911 if (coords == ATK_XY_WINDOW || coords == ATK_XY_SCREEN)
912 return g_utf8_strlen (label_text, -1);
917 return g_utf8_pointer_to_offset (label_text, label_text + index);
920 static AtkAttributeSet*
921 gail_label_get_run_attributes (AtkText *text,
928 AtkAttributeSet *at_set = NULL;
929 GtkJustification justify;
930 GtkTextDirection dir;
932 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
934 /* State is defunct */
937 label = GTK_LABEL (widget);
939 /* Get values set for entire label, if any */
940 justify = gtk_label_get_justify (label);
941 if (justify != GTK_JUSTIFY_CENTER)
943 at_set = gail_misc_add_attribute (at_set,
944 ATK_TEXT_ATTR_JUSTIFICATION,
945 g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_JUSTIFICATION, justify)));
947 dir = gtk_widget_get_direction (widget);
948 if (dir == GTK_TEXT_DIR_RTL)
950 at_set = gail_misc_add_attribute (at_set,
951 ATK_TEXT_ATTR_DIRECTION,
952 g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_DIRECTION, dir)));
955 at_set = gail_misc_layout_get_run_attributes (at_set,
956 gtk_label_get_layout (label),
957 gtk_label_get_text (label),
964 static AtkAttributeSet*
965 gail_label_get_default_attributes (AtkText *text)
969 AtkAttributeSet *at_set = NULL;
971 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
973 /* State is defunct */
976 label = GTK_LABEL (widget);
978 at_set = gail_misc_get_default_attributes (at_set,
979 gtk_label_get_layout (label),
985 gail_label_get_character_at_offset (AtkText *text,
993 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
995 /* State is defunct */
998 label = GTK_LABEL (widget);
999 string = gtk_label_get_text (label);
1000 if (offset >= g_utf8_strlen (string, -1))
1002 index = g_utf8_offset_to_pointer (string, offset);
1004 return g_utf8_get_char (index);