1 /* GTK+ - accessibility implementations
2 * Copyright 2001 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, see <http://www.gnu.org/licenses/>.
21 #include "../gtkpango.h"
22 #include "gtktextcellaccessible.h"
23 #include "gtkcontainercellaccessible.h"
24 #include "gtkcellaccessibleparent.h"
26 struct _GtkTextCellAccessiblePrivate
33 static const gchar* gtk_text_cell_accessible_get_name (AtkObject *atk_obj);
38 static gchar* gtk_text_cell_accessible_get_text (AtkText *text,
41 static gunichar gtk_text_cell_accessible_get_character_at_offset (AtkText *text,
43 static gchar* gtk_text_cell_accessible_get_text_before_offset (AtkText *text,
45 AtkTextBoundary boundary_type,
48 static gchar* gtk_text_cell_accessible_get_text_at_offset (AtkText *text,
50 AtkTextBoundary boundary_type,
53 static gchar* gtk_text_cell_accessible_get_text_after_offset (AtkText *text,
55 AtkTextBoundary boundary_type,
58 static gint gtk_text_cell_accessible_get_character_count (AtkText *text);
59 static gint gtk_text_cell_accessible_get_caret_offset (AtkText *text);
60 static gboolean gtk_text_cell_accessible_set_caret_offset (AtkText *text,
62 static void gtk_text_cell_accessible_get_character_extents (AtkText *text,
69 static gint gtk_text_cell_accessible_get_offset_at_point (AtkText *text,
73 static AtkAttributeSet* gtk_text_cell_accessible_get_run_attributes
78 static AtkAttributeSet* gtk_text_cell_accessible_get_default_attributes
81 static GtkWidget* get_widget (GtkTextCellAccessible *cell);
82 static PangoLayout* create_pango_layout (GtkTextCellAccessible *cell);
83 static void add_attr (PangoAttrList *attr_list,
84 PangoAttribute *attr);
88 static void gtk_text_cell_accessible_update_cache (GtkCellAccessible *cell);
90 static void atk_text_interface_init (AtkTextIface *iface);
92 G_DEFINE_TYPE_WITH_CODE (GtkTextCellAccessible, gtk_text_cell_accessible, GTK_TYPE_RENDERER_CELL_ACCESSIBLE,
93 G_IMPLEMENT_INTERFACE (ATK_TYPE_TEXT, atk_text_interface_init))
96 gtk_text_cell_accessible_ref_state_set (AtkObject *accessible)
98 AtkStateSet *state_set;
100 state_set = ATK_OBJECT_CLASS (gtk_text_cell_accessible_parent_class)->ref_state_set (accessible);
102 atk_state_set_add_state (state_set, ATK_STATE_SINGLE_LINE);
108 gtk_text_cell_accessible_finalize (GObject *object)
110 GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (object);
112 g_free (text_cell->priv->cell_text);
114 G_OBJECT_CLASS (gtk_text_cell_accessible_parent_class)->finalize (object);
118 gtk_text_cell_accessible_get_name (AtkObject *atk_obj)
120 GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (atk_obj);
123 return atk_obj->name;
125 return text_cell->priv->cell_text;
129 gtk_text_cell_accessible_update_cache (GtkCellAccessible *cell)
131 GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (cell);
132 AtkObject *obj = ATK_OBJECT (cell);
136 GtkCellRenderer *renderer;
138 g_object_get (cell, "renderer", &renderer, NULL);
139 g_object_get (renderer, "text", &text, NULL);
140 g_object_unref (renderer);
142 if (text_cell->priv->cell_text)
144 if (text == NULL || g_strcmp0 (text_cell->priv->cell_text, text) != 0)
146 g_free (text_cell->priv->cell_text);
147 temp_length = text_cell->priv->cell_length;
148 text_cell->priv->cell_text = NULL;
149 text_cell->priv->cell_length = 0;
150 g_signal_emit_by_name (cell, "text-changed::delete", 0, temp_length);
151 if (obj->name == NULL)
152 g_object_notify (G_OBJECT (obj), "accessible-name");
164 text_cell->priv->cell_text = g_strdup ("");
165 text_cell->priv->cell_length = 0;
169 text_cell->priv->cell_text = g_strdup (text);
170 text_cell->priv->cell_length = g_utf8_strlen (text, -1);
178 g_signal_emit_by_name (cell, "text-changed::insert",
179 0, text_cell->priv->cell_length);
181 if (obj->name == NULL)
182 g_object_notify (G_OBJECT (obj), "accessible-name");
187 gtk_text_cell_accessible_class_init (GtkTextCellAccessibleClass *klass)
189 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
190 AtkObjectClass *atk_object_class = ATK_OBJECT_CLASS (klass);
191 GtkCellAccessibleClass *cell_class = GTK_CELL_ACCESSIBLE_CLASS (klass);
193 cell_class->update_cache = gtk_text_cell_accessible_update_cache;
195 atk_object_class->get_name = gtk_text_cell_accessible_get_name;
196 atk_object_class->ref_state_set = gtk_text_cell_accessible_ref_state_set;
198 gobject_class->finalize = gtk_text_cell_accessible_finalize;
200 g_type_class_add_private (klass, sizeof (GtkTextCellAccessiblePrivate));
204 gtk_text_cell_accessible_init (GtkTextCellAccessible *text_cell)
206 text_cell->priv = G_TYPE_INSTANCE_GET_PRIVATE (text_cell,
207 GTK_TYPE_TEXT_CELL_ACCESSIBLE,
208 GtkTextCellAccessiblePrivate);
212 gtk_text_cell_accessible_get_text (AtkText *atk_text,
218 text = GTK_TEXT_CELL_ACCESSIBLE (atk_text)->priv->cell_text;
220 return g_utf8_substring (text, start_pos, end_pos > -1 ? end_pos : g_utf8_strlen (text, -1));
222 return g_strdup ("");
226 gtk_text_cell_accessible_get_text_before_offset (AtkText *atk_text,
228 AtkTextBoundary boundary_type,
235 layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (atk_text));
236 text = _gtk_pango_get_text_before (layout, boundary_type, offset, start_offset, end_offset);
237 g_object_unref (layout);
243 gtk_text_cell_accessible_get_text_at_offset (AtkText *atk_text,
245 AtkTextBoundary boundary_type,
252 layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (atk_text));
253 text = _gtk_pango_get_text_at (layout, boundary_type, offset, start_offset, end_offset);
254 g_object_unref (layout);
260 gtk_text_cell_accessible_get_text_after_offset (AtkText *atk_text,
262 AtkTextBoundary boundary_type,
269 layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (atk_text));
270 text = _gtk_pango_get_text_after (layout, boundary_type, offset, start_offset, end_offset);
271 g_object_unref (layout);
277 gtk_text_cell_accessible_get_character_count (AtkText *text)
279 if (GTK_TEXT_CELL_ACCESSIBLE (text)->priv->cell_text != NULL)
280 return GTK_TEXT_CELL_ACCESSIBLE (text)->priv->cell_length;
286 gtk_text_cell_accessible_get_caret_offset (AtkText *text)
288 return GTK_TEXT_CELL_ACCESSIBLE (text)->priv->caret_pos;
292 gtk_text_cell_accessible_set_caret_offset (AtkText *text,
295 GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (text);
297 if (text_cell->priv->cell_text == NULL)
301 /* Only set the caret within the bounds and if it is to a new position. */
303 offset <= text_cell->priv->cell_length &&
304 offset != text_cell->priv->caret_pos)
306 text_cell->priv->caret_pos = offset;
308 /* emit the signal */
309 g_signal_emit_by_name (text, "text-caret-moved", offset);
317 static AtkAttributeSet *
318 gtk_text_cell_accessible_get_run_attributes (AtkText *text,
323 AtkAttributeSet *attrib_set = NULL;
326 layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
327 attrib_set = _gtk_pango_get_run_attributes (NULL, layout, offset, start_offset, end_offset);
328 g_object_unref (G_OBJECT (layout));
333 static AtkAttributeSet *
334 add_attribute (AtkAttributeSet *attributes,
335 AtkTextAttribute attr,
340 at = g_new (AtkAttribute, 1);
341 at->name = g_strdup (atk_text_attribute_get_name (attr));
342 at->value = g_strdup (value);
344 return g_slist_prepend (attributes, at);
347 static AtkAttributeSet *
348 gtk_text_cell_accessible_get_default_attributes (AtkText *text)
350 AtkAttributeSet *attrib_set = NULL;
354 layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
355 widget = get_widget (GTK_TEXT_CELL_ACCESSIBLE (text));
357 attrib_set = add_attribute (attrib_set, ATK_TEXT_ATTR_DIRECTION,
358 atk_text_attribute_get_value (ATK_TEXT_ATTR_DIRECTION,
359 gtk_widget_get_direction (widget)));
360 attrib_set = _gtk_pango_get_default_attributes (NULL, layout);
362 attrib_set = _gtk_style_context_get_attributes (attrib_set,
363 gtk_widget_get_style_context (widget),
364 gtk_widget_get_state_flags (widget));
366 g_object_unref (G_OBJECT (layout));
372 get_widget (GtkTextCellAccessible *text)
376 parent = atk_object_get_parent (ATK_OBJECT (text));
377 if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
378 parent = atk_object_get_parent (parent);
380 return gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
383 /* This function is used by gtk_text_cell_accessible_get_offset_at_point()
384 * and gtk_text_cell_accessible_get_character_extents(). There is no
385 * cached PangoLayout so we must create a temporary one using this function.
388 create_pango_layout (GtkTextCellAccessible *text)
390 GdkRGBA *foreground_rgba;
391 PangoAttrList *attr_list, *attributes;
393 PangoUnderline uline, underline;
395 PangoFontDescription *font_desc;
396 gboolean foreground_set, strikethrough_set, strikethrough;
397 gboolean scale_set, underline_set, rise_set;
398 gchar *renderer_text;
401 GtkRendererCellAccessible *gail_renderer;
402 GtkCellRendererText *gtk_renderer;
404 gail_renderer = GTK_RENDERER_CELL_ACCESSIBLE (text);
405 g_object_get (gail_renderer, "renderer", >k_renderer, NULL);
407 g_object_get (gtk_renderer,
408 "text", &renderer_text,
409 "attributes", &attributes,
410 "foreground-set", &foreground_set,
411 "foreground-rgba", &foreground_rgba,
412 "strikethrough-set", &strikethrough_set,
413 "strikethrough", &strikethrough,
414 "font-desc", &font_desc,
415 "scale-set", &scale_set,
417 "underline-set", &underline_set,
418 "underline", &underline,
419 "rise-set", &rise_set,
422 g_object_unref (gtk_renderer);
424 layout = gtk_widget_create_pango_layout (get_widget (text), renderer_text);
427 attr_list = pango_attr_list_copy (attributes);
429 attr_list = pango_attr_list_new ();
433 add_attr (attr_list, pango_attr_foreground_new (foreground_rgba->red * 65535,
434 foreground_rgba->green * 65535,
435 foreground_rgba->blue * 65535));
438 if (strikethrough_set)
440 pango_attr_strikethrough_new (strikethrough));
442 mask = pango_font_description_get_set_fields (font_desc);
444 if (mask & PANGO_FONT_MASK_FAMILY)
446 pango_attr_family_new (pango_font_description_get_family (font_desc)));
448 if (mask & PANGO_FONT_MASK_STYLE)
449 add_attr (attr_list, pango_attr_style_new (pango_font_description_get_style (font_desc)));
451 if (mask & PANGO_FONT_MASK_VARIANT)
452 add_attr (attr_list, pango_attr_variant_new (pango_font_description_get_variant (font_desc)));
454 if (mask & PANGO_FONT_MASK_WEIGHT)
455 add_attr (attr_list, pango_attr_weight_new (pango_font_description_get_weight (font_desc)));
457 if (mask & PANGO_FONT_MASK_STRETCH)
458 add_attr (attr_list, pango_attr_stretch_new (pango_font_description_get_stretch (font_desc)));
460 if (mask & PANGO_FONT_MASK_SIZE)
461 add_attr (attr_list, pango_attr_size_new (pango_font_description_get_size (font_desc)));
463 if (scale_set && scale != 1.0)
464 add_attr (attr_list, pango_attr_scale_new (scale));
469 uline = PANGO_UNDERLINE_NONE;
471 if (uline != PANGO_UNDERLINE_NONE)
473 pango_attr_underline_new (underline));
476 add_attr (attr_list, pango_attr_rise_new (rise));
478 pango_layout_set_attributes (layout, attr_list);
479 pango_layout_set_width (layout, -1);
480 pango_attr_list_unref (attr_list);
482 pango_font_description_free (font_desc);
483 pango_attr_list_unref (attributes);
484 g_free (renderer_text);
485 gdk_rgba_free (foreground_rgba);
491 add_attr (PangoAttrList *attr_list,
492 PangoAttribute *attr)
494 attr->start_index = 0;
495 attr->end_index = G_MAXINT;
496 pango_attr_list_insert (attr_list, attr);
501 get_origins (GtkWidget *widget,
509 if (GTK_IS_TREE_VIEW (widget))
510 window = gtk_tree_view_get_bin_window (GTK_TREE_VIEW (widget));
512 window = gtk_widget_get_window (widget);
514 gdk_window_get_origin (window, x_window, y_window);
515 window = gdk_window_get_toplevel (gtk_widget_get_window (widget));
516 gdk_window_get_origin (window, x_toplevel, y_toplevel);
520 gtk_text_cell_accessible_get_character_extents (AtkText *text,
528 GtkRendererCellAccessible *gail_renderer;
529 GtkRequisition min_size;
530 GtkCellRendererText *gtk_renderer;
531 GdkRectangle rendered_rect;
534 PangoRectangle char_rect;
536 gchar *renderer_text;
537 gfloat xalign, yalign;
538 gint x_offset, y_offset, index;
540 gint x_window, y_window, x_toplevel, y_toplevel;
542 if (!GTK_TEXT_CELL_ACCESSIBLE (text)->priv->cell_text)
544 *x = *y = *height = *width = 0;
547 if (offset < 0 || offset >= GTK_TEXT_CELL_ACCESSIBLE (text)->priv->cell_length)
549 *x = *y = *height = *width = 0;
552 gail_renderer = GTK_RENDERER_CELL_ACCESSIBLE (text);
553 g_object_get (gail_renderer, "renderer", >k_renderer, NULL);
554 g_object_get (gtk_renderer, "text", &renderer_text, NULL);
555 if (renderer_text == NULL)
557 g_object_unref (gtk_renderer);
561 parent = atk_object_get_parent (ATK_OBJECT (text));
562 if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
563 parent = atk_object_get_parent (parent);
564 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
565 g_return_if_fail (GTK_IS_CELL_ACCESSIBLE_PARENT (parent));
566 gtk_cell_accessible_parent_get_cell_area (GTK_CELL_ACCESSIBLE_PARENT (parent),
567 GTK_CELL_ACCESSIBLE (text),
570 gtk_cell_renderer_get_preferred_size (GTK_CELL_RENDERER (gtk_renderer),
574 gtk_cell_renderer_get_alignment (GTK_CELL_RENDERER (gtk_renderer), &xalign, &yalign);
575 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
576 xalign = 1.0 - xalign;
577 x_offset = MAX (0, xalign * (rendered_rect.width - min_size.width));
578 y_offset = MAX (0, yalign * (rendered_rect.height - min_size.height));
580 layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
582 index = g_utf8_offset_to_pointer (renderer_text, offset) - renderer_text;
583 pango_layout_index_to_pos (layout, index, &char_rect);
585 gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (gtk_renderer), &xpad, &ypad);
587 get_origins (widget, &x_window, &y_window, &x_toplevel, &y_toplevel);
589 *x = (char_rect.x / PANGO_SCALE) + x_offset + rendered_rect.x + xpad + x_window;
590 *y = (char_rect.y / PANGO_SCALE) + y_offset + rendered_rect.y + ypad + y_window;
591 *height = char_rect.height / PANGO_SCALE;
592 *width = char_rect.width / PANGO_SCALE;
594 if (coords == ATK_XY_WINDOW)
599 else if (coords != ATK_XY_SCREEN)
607 g_free (renderer_text);
608 g_object_unref (layout);
609 g_object_unref (gtk_renderer);
613 gtk_text_cell_accessible_get_offset_at_point (AtkText *text,
619 GtkRendererCellAccessible *gail_renderer;
620 GtkCellRendererText *gtk_renderer;
621 GtkRequisition min_size;
623 GdkRectangle rendered_rect;
625 gchar *renderer_text;
626 gfloat xalign, yalign;
627 gint x_offset, y_offset, index;
629 gint x_window, y_window, x_toplevel, y_toplevel;
633 if (!GTK_TEXT_CELL_ACCESSIBLE (text)->priv->cell_text)
636 gail_renderer = GTK_RENDERER_CELL_ACCESSIBLE (text);
637 g_object_get (gail_renderer, "renderer", >k_renderer, NULL);
638 parent = atk_object_get_parent (ATK_OBJECT (text));
640 g_object_get (gtk_renderer, "text", &renderer_text, NULL);
643 g_object_unref (gtk_renderer);
644 g_free (renderer_text);
648 if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
649 parent = atk_object_get_parent (parent);
651 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
653 g_return_val_if_fail (GTK_IS_CELL_ACCESSIBLE_PARENT (parent), -1);
654 gtk_cell_accessible_parent_get_cell_area (GTK_CELL_ACCESSIBLE_PARENT (parent),
655 GTK_CELL_ACCESSIBLE (text),
658 gtk_cell_renderer_get_preferred_size (GTK_CELL_RENDERER (gtk_renderer),
661 gtk_cell_renderer_get_alignment (GTK_CELL_RENDERER (gtk_renderer), &xalign, &yalign);
662 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
663 xalign = 1.0 - xalign;
664 x_offset = MAX (0, xalign * (rendered_rect.width - min_size.width));
665 y_offset = MAX (0, yalign * (rendered_rect.height - min_size.height));
667 layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
669 gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (gtk_renderer), &xpad, &ypad);
671 get_origins (widget, &x_window, &y_window, &x_toplevel, &y_toplevel);
673 x_temp = x - (x_offset + rendered_rect.x + xpad) - x_window;
674 y_temp = y - (y_offset + rendered_rect.y + ypad) - y_window;
675 if (coords == ATK_XY_WINDOW)
677 x_temp += x_toplevel;
678 y_temp += y_toplevel;
680 else if (coords != ATK_XY_SCREEN)
683 ret = pango_layout_xy_to_index (layout,
684 x_temp * PANGO_SCALE,
685 y_temp * PANGO_SCALE,
689 if (x_temp < 0 || y_temp < 0)
695 g_object_unref (layout);
696 g_object_unref (gtk_renderer);
700 if (coords == ATK_XY_WINDOW || coords == ATK_XY_SCREEN)
704 length = g_utf8_strlen (renderer_text, -1);
705 g_free (renderer_text);
710 g_free (renderer_text);
718 offset = g_utf8_pointer_to_offset (renderer_text,
719 renderer_text + index);
720 g_free (renderer_text);
727 gtk_text_cell_accessible_get_character_at_offset (AtkText *text,
733 string = GTK_TEXT_CELL_ACCESSIBLE(text)->priv->cell_text;
738 if (offset >= g_utf8_strlen (string, -1))
741 index = g_utf8_offset_to_pointer (string, offset);
743 return g_utf8_get_char (index);
747 atk_text_interface_init (AtkTextIface *iface)
749 iface->get_text = gtk_text_cell_accessible_get_text;
750 iface->get_character_at_offset = gtk_text_cell_accessible_get_character_at_offset;
751 iface->get_text_before_offset = gtk_text_cell_accessible_get_text_before_offset;
752 iface->get_text_at_offset = gtk_text_cell_accessible_get_text_at_offset;
753 iface->get_text_after_offset = gtk_text_cell_accessible_get_text_after_offset;
754 iface->get_character_count = gtk_text_cell_accessible_get_character_count;
755 iface->get_caret_offset = gtk_text_cell_accessible_get_caret_offset;
756 iface->set_caret_offset = gtk_text_cell_accessible_set_caret_offset;
757 iface->get_run_attributes = gtk_text_cell_accessible_get_run_attributes;
758 iface->get_default_attributes = gtk_text_cell_accessible_get_default_attributes;
759 iface->get_character_extents = gtk_text_cell_accessible_get_character_extents;
760 iface->get_offset_at_point = gtk_text_cell_accessible_get_offset_at_point;