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 "gailstatusbar.h"
25 #include <libgail-util/gailmisc.h>
27 static void gail_statusbar_class_init (GailStatusbarClass *klass);
28 static void gail_statusbar_init (GailStatusbar *bar);
29 static G_CONST_RETURN gchar* gail_statusbar_get_name (AtkObject *obj);
30 static gint gail_statusbar_get_n_children (AtkObject *obj);
31 static AtkObject* gail_statusbar_ref_child (AtkObject *obj,
33 static void gail_statusbar_real_initialize (AtkObject *obj,
36 static gint gail_statusbar_notify (GObject *obj,
39 static void gail_statusbar_finalize (GObject *object);
40 static void gail_statusbar_init_textutil (GailStatusbar *statusbar,
44 static void atk_text_interface_init (AtkTextIface *iface);
46 static gchar* gail_statusbar_get_text (AtkText *text,
49 static gunichar gail_statusbar_get_character_at_offset
52 static gchar* gail_statusbar_get_text_before_offset
55 AtkTextBoundary boundary_type,
58 static gchar* gail_statusbar_get_text_at_offset(AtkText *text,
60 AtkTextBoundary boundary_type,
63 static gchar* gail_statusbar_get_text_after_offset
66 AtkTextBoundary boundary_type,
69 static gint gail_statusbar_get_character_count (AtkText *text);
70 static void gail_statusbar_get_character_extents
78 static gint gail_statusbar_get_offset_at_point(AtkText *text,
82 static AtkAttributeSet* gail_statusbar_get_run_attributes
87 static AtkAttributeSet* gail_statusbar_get_default_attributes
89 static GtkWidget* get_label_from_statusbar (GtkWidget *statusbar);
91 G_DEFINE_TYPE_WITH_CODE (GailStatusbar, gail_statusbar, GAIL_TYPE_CONTAINER,
92 G_IMPLEMENT_INTERFACE (ATK_TYPE_TEXT, atk_text_interface_init))
95 gail_statusbar_class_init (GailStatusbarClass *klass)
97 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
98 AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
99 GailContainerClass *container_class;
101 container_class = (GailContainerClass*)klass;
103 gobject_class->finalize = gail_statusbar_finalize;
105 class->get_name = gail_statusbar_get_name;
106 class->get_n_children = gail_statusbar_get_n_children;
107 class->ref_child = gail_statusbar_ref_child;
108 class->initialize = gail_statusbar_real_initialize;
110 * As we report the statusbar as having no children we are not interested
111 * in add and remove signals
113 container_class->add_gtk = NULL;
114 container_class->remove_gtk = NULL;
118 gail_statusbar_init (GailStatusbar *bar)
122 static G_CONST_RETURN gchar*
123 gail_statusbar_get_name (AtkObject *obj)
125 G_CONST_RETURN gchar* name;
127 g_return_val_if_fail (GAIL_IS_STATUSBAR (obj), NULL);
129 name = ATK_OBJECT_CLASS (gail_statusbar_parent_class)->get_name (obj);
135 * Get the text on the label
140 widget = GTK_ACCESSIBLE (obj)->widget;
147 g_return_val_if_fail (GTK_IS_STATUSBAR (widget), NULL);
148 label = get_label_from_statusbar (widget);
149 if (GTK_IS_LABEL (label))
150 return gtk_label_get_label (GTK_LABEL (label));
157 gail_statusbar_get_n_children (AtkObject *obj)
163 widget = GTK_ACCESSIBLE (obj)->widget;
167 children = gtk_container_get_children (GTK_CONTAINER (widget));
168 if (children != NULL)
170 count = g_list_length (children);
171 g_list_free (children);
178 gail_statusbar_ref_child (AtkObject *obj,
181 GList *children, *tmp_list;
182 AtkObject *accessible;
185 g_return_val_if_fail ((i >= 0), NULL);
186 widget = GTK_ACCESSIBLE (obj)->widget;
190 children = gtk_container_get_children (GTK_CONTAINER (widget));
191 if (children == NULL)
194 tmp_list = g_list_nth (children, i);
197 g_list_free (children);
200 accessible = gtk_widget_get_accessible (GTK_WIDGET (tmp_list->data));
202 g_list_free (children);
203 g_object_ref (accessible);
208 gail_statusbar_real_initialize (AtkObject *obj,
211 GailStatusbar *statusbar = GAIL_STATUSBAR (obj);
214 ATK_OBJECT_CLASS (gail_statusbar_parent_class)->initialize (obj, data);
217 * We get notified of changes to the label
219 label = get_label_from_statusbar (GTK_WIDGET (data));
220 if (GTK_IS_LABEL (label))
222 gail_statusbar_init_textutil (statusbar, label);
225 obj->role = ATK_ROLE_STATUSBAR;
230 gail_statusbar_notify (GObject *obj,
234 AtkObject *atk_obj = ATK_OBJECT (user_data);
236 GailStatusbar *statusbar;
238 if (strcmp (pspec->name, "label") == 0)
240 const gchar* label_text;
242 label = GTK_LABEL (obj);
244 label_text = gtk_label_get_text (label);
246 statusbar = GAIL_STATUSBAR (atk_obj);
247 gail_text_util_text_setup (statusbar->textutil, label_text);
249 if (atk_obj->name == NULL)
252 * The label has changed so notify a change in accessible-name
254 g_object_notify (G_OBJECT (atk_obj), "accessible-name");
257 * The label is the only property which can be changed
259 g_signal_emit_by_name (atk_obj, "visible_data_changed");
265 gail_statusbar_init_textutil (GailStatusbar *statusbar,
268 const gchar *label_text;
270 statusbar->textutil = gail_text_util_new ();
271 label_text = gtk_label_get_text (GTK_LABEL (label));
272 gail_text_util_text_setup (statusbar->textutil, label_text);
273 g_signal_connect (label,
275 (GCallback) gail_statusbar_notify,
280 gail_statusbar_finalize (GObject *object)
282 GailStatusbar *statusbar = GAIL_STATUSBAR (object);
284 if (statusbar->textutil)
286 g_object_unref (statusbar->textutil);
288 G_OBJECT_CLASS (gail_statusbar_parent_class)->finalize (object);
294 atk_text_interface_init (AtkTextIface *iface)
296 iface->get_text = gail_statusbar_get_text;
297 iface->get_character_at_offset = gail_statusbar_get_character_at_offset;
298 iface->get_text_before_offset = gail_statusbar_get_text_before_offset;
299 iface->get_text_at_offset = gail_statusbar_get_text_at_offset;
300 iface->get_text_after_offset = gail_statusbar_get_text_after_offset;
301 iface->get_character_count = gail_statusbar_get_character_count;
302 iface->get_character_extents = gail_statusbar_get_character_extents;
303 iface->get_offset_at_point = gail_statusbar_get_offset_at_point;
304 iface->get_run_attributes = gail_statusbar_get_run_attributes;
305 iface->get_default_attributes = gail_statusbar_get_default_attributes;
309 gail_statusbar_get_text (AtkText *text,
315 GailStatusbar *statusbar;
316 const gchar *label_text;
318 widget = GTK_ACCESSIBLE (text)->widget;
320 /* State is defunct */
323 label = get_label_from_statusbar (widget);
325 if (!GTK_IS_LABEL (label))
328 statusbar = GAIL_STATUSBAR (text);
329 if (!statusbar->textutil)
330 gail_statusbar_init_textutil (statusbar, label);
332 label_text = gtk_label_get_text (GTK_LABEL (label));
334 if (label_text == NULL)
338 return gail_text_util_get_substring (statusbar->textutil,
344 gail_statusbar_get_text_before_offset (AtkText *text,
346 AtkTextBoundary boundary_type,
352 GailStatusbar *statusbar;
354 widget = GTK_ACCESSIBLE (text)->widget;
357 /* State is defunct */
361 label = get_label_from_statusbar (widget);
363 if (!GTK_IS_LABEL(label))
366 statusbar = GAIL_STATUSBAR (text);
367 if (!statusbar->textutil)
368 gail_statusbar_init_textutil (statusbar, label);
370 return gail_text_util_get_text (statusbar->textutil,
371 gtk_label_get_layout (GTK_LABEL (label)), GAIL_BEFORE_OFFSET,
372 boundary_type, offset, start_offset, end_offset);
376 gail_statusbar_get_text_at_offset (AtkText *text,
378 AtkTextBoundary boundary_type,
384 GailStatusbar *statusbar;
386 widget = GTK_ACCESSIBLE (text)->widget;
389 /* State is defunct */
393 label = get_label_from_statusbar (widget);
395 if (!GTK_IS_LABEL(label))
398 statusbar = GAIL_STATUSBAR (text);
399 if (!statusbar->textutil)
400 gail_statusbar_init_textutil (statusbar, label);
402 return gail_text_util_get_text (statusbar->textutil,
403 gtk_label_get_layout (GTK_LABEL (label)), GAIL_AT_OFFSET,
404 boundary_type, offset, start_offset, end_offset);
408 gail_statusbar_get_text_after_offset (AtkText *text,
410 AtkTextBoundary boundary_type,
416 GailStatusbar *statusbar;
418 widget = GTK_ACCESSIBLE (text)->widget;
422 /* State is defunct */
427 label = get_label_from_statusbar (widget);
429 if (!GTK_IS_LABEL(label))
432 statusbar = GAIL_STATUSBAR (text);
433 if (!statusbar->textutil)
434 gail_statusbar_init_textutil (statusbar, label);
436 return gail_text_util_get_text (statusbar->textutil,
437 gtk_label_get_layout (GTK_LABEL (label)), GAIL_AFTER_OFFSET,
438 boundary_type, offset, start_offset, end_offset);
442 gail_statusbar_get_character_count (AtkText *text)
447 widget = GTK_ACCESSIBLE (text)->widget;
449 /* State is defunct */
452 label = get_label_from_statusbar (widget);
454 if (!GTK_IS_LABEL(label))
457 return g_utf8_strlen (gtk_label_get_text (GTK_LABEL (label)), -1);
461 gail_statusbar_get_character_extents (AtkText *text,
471 PangoRectangle char_rect;
472 gint index, x_layout, y_layout;
473 const gchar *label_text;
475 widget = GTK_ACCESSIBLE (text)->widget;
478 /* State is defunct */
481 label = get_label_from_statusbar (widget);
483 if (!GTK_IS_LABEL(label))
486 gtk_label_get_layout_offsets (GTK_LABEL (label), &x_layout, &y_layout);
487 label_text = gtk_label_get_text (GTK_LABEL (label));
488 index = g_utf8_offset_to_pointer (label_text, offset) - label_text;
489 pango_layout_index_to_pos (gtk_label_get_layout (GTK_LABEL (label)), index, &char_rect);
491 gail_misc_get_extents_from_pango_rectangle (label, &char_rect,
492 x_layout, y_layout, x, y, width, height, coords);
496 gail_statusbar_get_offset_at_point (AtkText *text,
503 gint index, x_layout, y_layout;
504 const gchar *label_text;
506 widget = GTK_ACCESSIBLE (text)->widget;
508 /* State is defunct */
511 label = get_label_from_statusbar (widget);
513 if (!GTK_IS_LABEL(label))
516 gtk_label_get_layout_offsets (GTK_LABEL (label), &x_layout, &y_layout);
518 index = gail_misc_get_index_at_point_in_layout (label,
519 gtk_label_get_layout (GTK_LABEL (label)),
520 x_layout, y_layout, x, y, coords);
521 label_text = gtk_label_get_text (GTK_LABEL (label));
524 if (coords == ATK_XY_WINDOW || coords == ATK_XY_SCREEN)
525 return g_utf8_strlen (label_text, -1);
530 return g_utf8_pointer_to_offset (label_text, label_text + index);
533 static AtkAttributeSet*
534 gail_statusbar_get_run_attributes (AtkText *text,
541 AtkAttributeSet *at_set = NULL;
542 GtkJustification justify;
543 GtkTextDirection dir;
545 widget = GTK_ACCESSIBLE (text)->widget;
547 /* State is defunct */
550 label = get_label_from_statusbar (widget);
552 if (!GTK_IS_LABEL(label))
555 /* Get values set for entire label, if any */
556 justify = gtk_label_get_justify (GTK_LABEL (label));
557 if (justify != GTK_JUSTIFY_CENTER)
559 at_set = gail_misc_add_attribute (at_set,
560 ATK_TEXT_ATTR_JUSTIFICATION,
561 g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_JUSTIFICATION, justify)));
563 dir = gtk_widget_get_direction (label);
564 if (dir == GTK_TEXT_DIR_RTL)
566 at_set = gail_misc_add_attribute (at_set,
567 ATK_TEXT_ATTR_DIRECTION,
568 g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_DIRECTION, dir)));
571 at_set = gail_misc_layout_get_run_attributes (at_set,
572 gtk_label_get_layout (GTK_LABEL (label)),
573 (gchar *) gtk_label_get_text (GTK_LABEL (label)),
580 static AtkAttributeSet*
581 gail_statusbar_get_default_attributes (AtkText *text)
585 AtkAttributeSet *at_set = NULL;
587 widget = GTK_ACCESSIBLE (text)->widget;
589 /* State is defunct */
592 label = get_label_from_statusbar (widget);
594 if (!GTK_IS_LABEL(label))
597 at_set = gail_misc_get_default_attributes (at_set,
598 gtk_label_get_layout (GTK_LABEL (label)),
604 gail_statusbar_get_character_at_offset (AtkText *text,
612 widget = GTK_ACCESSIBLE (text)->widget;
614 /* State is defunct */
617 label = get_label_from_statusbar (widget);
619 if (!GTK_IS_LABEL(label))
621 string = gtk_label_get_text (GTK_LABEL (label));
622 if (offset >= g_utf8_strlen (string, -1))
624 index = g_utf8_offset_to_pointer (string, offset);
626 return g_utf8_get_char (index);
630 get_label_from_statusbar (GtkWidget *statusbar)
632 return GTK_STATUSBAR (statusbar)->label;