1 /* GAIL - The GNOME Accessibility Enabling Library
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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
23 #include "../gtkpango.h"
24 #include "gtktextcellaccessible.h"
25 #include "gtkcontainercellaccessible.h"
26 #include "gailcellparent.h"
28 static const gchar* gtk_text_cell_accessible_get_name (AtkObject *atk_obj);
33 static gchar* gtk_text_cell_accessible_get_text (AtkText *text,
36 static gunichar gtk_text_cell_accessible_get_character_at_offset (AtkText *text,
38 static gchar* gtk_text_cell_accessible_get_text_before_offset (AtkText *text,
40 AtkTextBoundary boundary_type,
43 static gchar* gtk_text_cell_accessible_get_text_at_offset (AtkText *text,
45 AtkTextBoundary boundary_type,
48 static gchar* gtk_text_cell_accessible_get_text_after_offset (AtkText *text,
50 AtkTextBoundary boundary_type,
53 static gint gtk_text_cell_accessible_get_character_count (AtkText *text);
54 static gint gtk_text_cell_accessible_get_caret_offset (AtkText *text);
55 static gboolean gtk_text_cell_accessible_set_caret_offset (AtkText *text,
57 static void gtk_text_cell_accessible_get_character_extents (AtkText *text,
64 static gint gtk_text_cell_accessible_get_offset_at_point (AtkText *text,
68 static AtkAttributeSet* gtk_text_cell_accessible_get_run_attributes
73 static AtkAttributeSet* gtk_text_cell_accessible_get_default_attributes
76 static GtkWidget* get_widget (GtkTextCellAccessible *cell);
77 static PangoLayout* create_pango_layout (GtkTextCellAccessible *cell);
78 static void add_attr (PangoAttrList *attr_list,
79 PangoAttribute *attr);
83 static gboolean gtk_text_cell_accessible_update_cache (GtkRendererCellAccessible *cell,
84 gboolean emit_change_signal);
86 static gchar *property_list[] = {
87 /* Set font_desc first since it resets other values if it is NULL */
106 /* Also need the sets */
123 static void atk_text_interface_init (AtkTextIface *iface);
125 G_DEFINE_TYPE_WITH_CODE (GtkTextCellAccessible, _gtk_text_cell_accessible, GTK_TYPE_RENDERER_CELL_ACCESSIBLE,
126 G_IMPLEMENT_INTERFACE (ATK_TYPE_TEXT, atk_text_interface_init))
130 gtk_text_cell_accessible_finalize (GObject *object)
132 GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (object);
134 g_free (text_cell->cell_text);
136 G_OBJECT_CLASS (_gtk_text_cell_accessible_parent_class)->finalize (object);
140 gtk_text_cell_accessible_get_name (AtkObject *atk_obj)
142 GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (atk_obj);
145 return atk_obj->name;
147 return text_cell->cell_text;
151 gtk_text_cell_accessible_update_cache (GtkRendererCellAccessible *cell,
152 gboolean emit_change_signal)
154 GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (cell);
155 AtkObject *obj = ATK_OBJECT (cell);
160 g_object_get (G_OBJECT (cell->renderer), "text", &text, NULL);
162 if (text_cell->cell_text)
164 if (text == NULL || strcmp (text_cell->cell_text, text) != 0)
166 g_free (text_cell->cell_text);
167 temp_length = text_cell->cell_length;
168 text_cell->cell_text = NULL;
169 text_cell->cell_length = 0;
170 if (emit_change_signal)
172 g_signal_emit_by_name (cell, "text_changed::delete", 0, temp_length);
173 if (obj->name == NULL)
174 g_object_notify (G_OBJECT (obj), "accessible-name");
187 text_cell->cell_text = g_strdup ("");
188 text_cell->cell_length = 0;
192 text_cell->cell_text = g_strdup (text);
193 text_cell->cell_length = g_utf8_strlen (text, -1);
201 if (emit_change_signal)
203 g_signal_emit_by_name (cell, "text_changed::insert",
204 0, text_cell->cell_length);
206 if (obj->name == NULL)
207 g_object_notify (G_OBJECT (obj), "accessible-name");
214 _gtk_text_cell_accessible_class_init (GtkTextCellAccessibleClass *klass)
216 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
217 AtkObjectClass *atk_object_class = ATK_OBJECT_CLASS (klass);
218 GtkRendererCellAccessibleClass *renderer_cell_class = GTK_RENDERER_CELL_ACCESSIBLE_CLASS (klass);
220 renderer_cell_class->update_cache = gtk_text_cell_accessible_update_cache;
221 renderer_cell_class->property_list = property_list;
223 atk_object_class->get_name = gtk_text_cell_accessible_get_name;
225 gobject_class->finalize = gtk_text_cell_accessible_finalize;
229 _gtk_text_cell_accessible_init (GtkTextCellAccessible *text_cell)
231 text_cell->cell_text = NULL;
232 text_cell->caret_pos = 0;
233 text_cell->cell_length = 0;
234 atk_state_set_add_state (GTK_CELL_ACCESSIBLE (text_cell)->state_set,
235 ATK_STATE_SINGLE_LINE);
239 _gtk_text_cell_accessible_new (void)
242 AtkObject *atk_object;
243 GtkRendererCellAccessible *cell;
245 object = g_object_new (GTK_TYPE_TEXT_CELL_ACCESSIBLE, NULL);
247 g_return_val_if_fail (object != NULL, NULL);
249 atk_object = ATK_OBJECT (object);
250 atk_object->role = ATK_ROLE_TABLE_CELL;
252 cell = GTK_RENDERER_CELL_ACCESSIBLE(object);
254 cell->renderer = gtk_cell_renderer_text_new ();
255 g_object_ref_sink (cell->renderer);
261 gtk_text_cell_accessible_get_text (AtkText *atk_text,
267 text = GTK_TEXT_CELL_ACCESSIBLE (atk_text)->cell_text;
269 return g_utf8_substring (text, start_pos, end_pos > -1 ? end_pos : g_utf8_strlen (text, -1));
271 return g_strdup ("");
275 gtk_text_cell_accessible_get_text_before_offset (AtkText *atk_text,
277 AtkTextBoundary boundary_type,
284 layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (atk_text));
285 text = _gtk_pango_get_text_before (layout, boundary_type, offset, start_offset, end_offset);
286 g_object_unref (layout);
292 gtk_text_cell_accessible_get_text_at_offset (AtkText *atk_text,
294 AtkTextBoundary boundary_type,
301 layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (atk_text));
302 text = _gtk_pango_get_text_at (layout, boundary_type, offset, start_offset, end_offset);
303 g_object_unref (layout);
309 gtk_text_cell_accessible_get_text_after_offset (AtkText *atk_text,
311 AtkTextBoundary boundary_type,
318 layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (atk_text));
319 text = _gtk_pango_get_text_after (layout, boundary_type, offset, start_offset, end_offset);
320 g_object_unref (layout);
326 gtk_text_cell_accessible_get_character_count (AtkText *text)
328 if (GTK_TEXT_CELL_ACCESSIBLE (text)->cell_text != NULL)
329 return GTK_TEXT_CELL_ACCESSIBLE (text)->cell_length;
335 gtk_text_cell_accessible_get_caret_offset (AtkText *text)
337 return GTK_TEXT_CELL_ACCESSIBLE (text)->caret_pos;
341 gtk_text_cell_accessible_set_caret_offset (AtkText *text,
344 GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (text);
346 if (text_cell->cell_text == NULL)
350 /* Only set the caret within the bounds and if it is to a new position. */
352 offset <= text_cell->cell_length &&
353 offset != text_cell->caret_pos)
355 text_cell->caret_pos = offset;
357 /* emit the signal */
358 g_signal_emit_by_name (text, "text_caret_moved", offset);
366 static AtkAttributeSet *
367 gtk_text_cell_accessible_get_run_attributes (AtkText *text,
372 AtkAttributeSet *attrib_set = NULL;
375 layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
376 attrib_set = _gtk_pango_get_run_attributes (NULL, layout, offset, start_offset, end_offset);
377 g_object_unref (G_OBJECT (layout));
382 static AtkAttributeSet *
383 add_attribute (AtkAttributeSet *attributes,
384 AtkTextAttribute attr,
389 at = g_new (AtkAttribute, 1);
390 at->name = g_strdup (atk_text_attribute_get_name (attr));
391 at->value = g_strdup (value);
393 return g_slist_prepend (attributes, at);
396 static AtkAttributeSet *
397 gtk_text_cell_accessible_get_default_attributes (AtkText *text)
399 AtkAttributeSet *attrib_set = NULL;
403 layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
404 widget = get_widget (GTK_TEXT_CELL_ACCESSIBLE (text));
406 attrib_set = add_attribute (attrib_set, ATK_TEXT_ATTR_DIRECTION,
407 atk_text_attribute_get_value (ATK_TEXT_ATTR_DIRECTION,
408 gtk_widget_get_direction (widget)));
409 attrib_set = _gtk_pango_get_default_attributes (NULL, layout);
411 attrib_set = _gtk_style_context_get_attributes (attrib_set,
412 gtk_widget_get_style_context (widget),
413 gtk_widget_get_state_flags (widget));
415 g_object_unref (G_OBJECT (layout));
421 get_widget (GtkTextCellAccessible *text)
425 parent = atk_object_get_parent (ATK_OBJECT (text));
426 if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
427 parent = atk_object_get_parent (parent);
429 return gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
432 /* This function is used by gtk_text_cell_accessible_get_offset_at_point()
433 * and gtk_text_cell_accessible_get_character_extents(). There is no
434 * cached PangoLayout so we must create a temporary one using this function.
437 create_pango_layout (GtkTextCellAccessible *text)
439 GdkRGBA *foreground_rgba;
440 PangoAttrList *attr_list, *attributes;
442 PangoUnderline uline, underline;
444 PangoFontDescription *font_desc;
445 gboolean foreground_set, strikethrough_set, strikethrough;
446 gboolean scale_set, underline_set, rise_set;
447 gchar *renderer_text;
450 GtkRendererCellAccessible *gail_renderer;
451 GtkCellRendererText *gtk_renderer;
453 gail_renderer = GTK_RENDERER_CELL_ACCESSIBLE (text);
454 gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
456 g_object_get (gtk_renderer,
457 "text", &renderer_text,
458 "attributes", &attributes,
459 "foreground-set", &foreground_set,
460 "foreground-rgba", &foreground_rgba,
461 "strikethrough-set", &strikethrough_set,
462 "strikethrough", &strikethrough,
463 "font-desc", &font_desc,
464 "scale-set", &scale_set,
466 "underline-set", &underline_set,
467 "underline", &underline,
468 "rise-set", &rise_set,
472 layout = gtk_widget_create_pango_layout (get_widget (text), renderer_text);
475 attr_list = pango_attr_list_copy (attributes);
477 attr_list = pango_attr_list_new ();
481 add_attr (attr_list, pango_attr_foreground_new (foreground_rgba->red * 65535,
482 foreground_rgba->green * 65535,
483 foreground_rgba->blue * 65535));
486 if (strikethrough_set)
488 pango_attr_strikethrough_new (strikethrough));
490 mask = pango_font_description_get_set_fields (font_desc);
492 if (mask & PANGO_FONT_MASK_FAMILY)
494 pango_attr_family_new (pango_font_description_get_family (font_desc)));
496 if (mask & PANGO_FONT_MASK_STYLE)
497 add_attr (attr_list, pango_attr_style_new (pango_font_description_get_style (font_desc)));
499 if (mask & PANGO_FONT_MASK_VARIANT)
500 add_attr (attr_list, pango_attr_variant_new (pango_font_description_get_variant (font_desc)));
502 if (mask & PANGO_FONT_MASK_WEIGHT)
503 add_attr (attr_list, pango_attr_weight_new (pango_font_description_get_weight (font_desc)));
505 if (mask & PANGO_FONT_MASK_STRETCH)
506 add_attr (attr_list, pango_attr_stretch_new (pango_font_description_get_stretch (font_desc)));
508 if (mask & PANGO_FONT_MASK_SIZE)
509 add_attr (attr_list, pango_attr_size_new (pango_font_description_get_size (font_desc)));
511 if (scale_set && scale != 1.0)
512 add_attr (attr_list, pango_attr_scale_new (scale));
517 uline = PANGO_UNDERLINE_NONE;
519 if (uline != PANGO_UNDERLINE_NONE)
521 pango_attr_underline_new (underline));
524 add_attr (attr_list, pango_attr_rise_new (rise));
526 pango_layout_set_attributes (layout, attr_list);
527 pango_layout_set_width (layout, -1);
528 pango_attr_list_unref (attr_list);
530 pango_font_description_free (font_desc);
531 pango_attr_list_unref (attributes);
532 g_free (renderer_text);
533 gdk_rgba_free (foreground_rgba);
539 add_attr (PangoAttrList *attr_list,
540 PangoAttribute *attr)
542 attr->start_index = 0;
543 attr->end_index = G_MAXINT;
544 pango_attr_list_insert (attr_list, attr);
549 get_origins (GtkWidget *widget,
557 if (GTK_IS_TREE_VIEW (widget))
558 window = gtk_tree_view_get_bin_window (GTK_TREE_VIEW (widget));
560 window = gtk_widget_get_window (widget);
562 gdk_window_get_origin (window, x_window, y_window);
563 window = gdk_window_get_toplevel (gtk_widget_get_window (widget));
564 gdk_window_get_origin (window, x_toplevel, y_toplevel);
568 gtk_text_cell_accessible_get_character_extents (AtkText *text,
576 GtkRendererCellAccessible *gail_renderer;
577 GtkRequisition min_size;
578 GtkCellRendererText *gtk_renderer;
579 GdkRectangle rendered_rect;
582 PangoRectangle char_rect;
584 gchar *renderer_text;
585 gfloat xalign, yalign;
586 gint x_offset, y_offset, index;
588 gint x_window, y_window, x_toplevel, y_toplevel;
590 if (!GTK_TEXT_CELL_ACCESSIBLE (text)->cell_text)
592 *x = *y = *height = *width = 0;
595 if (offset < 0 || offset >= GTK_TEXT_CELL_ACCESSIBLE (text)->cell_length)
597 *x = *y = *height = *width = 0;
600 gail_renderer = GTK_RENDERER_CELL_ACCESSIBLE (text);
601 gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
603 g_object_get (gtk_renderer, "text", &renderer_text, NULL);
606 g_free (renderer_text);
610 parent = atk_object_get_parent (ATK_OBJECT (text));
611 if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
612 parent = atk_object_get_parent (parent);
613 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
614 g_return_if_fail (GAIL_IS_CELL_PARENT (parent));
615 gail_cell_parent_get_cell_area (GAIL_CELL_PARENT (parent), GTK_CELL_ACCESSIBLE (text),
618 gtk_cell_renderer_get_preferred_size (GTK_CELL_RENDERER (gtk_renderer),
622 gtk_cell_renderer_get_alignment (GTK_CELL_RENDERER (gtk_renderer), &xalign, &yalign);
623 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
624 xalign = 1.0 - xalign;
625 x_offset = MAX (0, xalign * (rendered_rect.width - min_size.width));
626 y_offset = MAX (0, yalign * (rendered_rect.height - min_size.height));
628 layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
630 index = g_utf8_offset_to_pointer (renderer_text, offset) - renderer_text;
631 pango_layout_index_to_pos (layout, index, &char_rect);
633 gtk_cell_renderer_get_padding (gail_renderer->renderer, &xpad, &ypad);
635 get_origins (widget, &x_window, &y_window, &x_toplevel, &y_toplevel);
637 *x = (char_rect.x / PANGO_SCALE) + x_offset + rendered_rect.x + xpad + x_window;
638 *y = (char_rect.y / PANGO_SCALE) + y_offset + rendered_rect.y + ypad + y_window;
639 *height = char_rect.height / PANGO_SCALE;
640 *width = char_rect.width / PANGO_SCALE;
642 if (coords == ATK_XY_WINDOW)
647 else if (coords != ATK_XY_SCREEN)
655 g_free (renderer_text);
656 g_object_unref (layout);
660 gtk_text_cell_accessible_get_offset_at_point (AtkText *text,
666 GtkRendererCellAccessible *gail_renderer;
667 GtkCellRendererText *gtk_renderer;
668 GtkRequisition min_size;
670 GdkRectangle rendered_rect;
672 gchar *renderer_text;
673 gfloat xalign, yalign;
674 gint x_offset, y_offset, index;
676 gint x_window, y_window, x_toplevel, y_toplevel;
680 if (!GTK_TEXT_CELL_ACCESSIBLE (text)->cell_text)
683 gail_renderer = GTK_RENDERER_CELL_ACCESSIBLE (text);
684 gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
685 parent = atk_object_get_parent (ATK_OBJECT (text));
687 g_object_get (gtk_renderer, "text", &renderer_text, NULL);
690 g_free (renderer_text);
694 if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
695 parent = atk_object_get_parent (parent);
697 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
699 g_return_val_if_fail (GAIL_IS_CELL_PARENT (parent), -1);
700 gail_cell_parent_get_cell_area (GAIL_CELL_PARENT (parent), GTK_CELL_ACCESSIBLE (text),
703 gtk_cell_renderer_get_preferred_size (GTK_CELL_RENDERER (gtk_renderer),
706 gtk_cell_renderer_get_alignment (GTK_CELL_RENDERER (gtk_renderer), &xalign, &yalign);
707 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
708 xalign = 1.0 - xalign;
709 x_offset = MAX (0, xalign * (rendered_rect.width - min_size.width));
710 y_offset = MAX (0, yalign * (rendered_rect.height - min_size.height));
712 layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
714 gtk_cell_renderer_get_padding (gail_renderer->renderer, &xpad, &ypad);
716 get_origins (widget, &x_window, &y_window, &x_toplevel, &y_toplevel);
718 x_temp = x - (x_offset + rendered_rect.x + xpad) - x_window;
719 y_temp = y - (y_offset + rendered_rect.y + ypad) - y_window;
720 if (coords == ATK_XY_WINDOW)
722 x_temp += x_toplevel;
723 y_temp += y_toplevel;
725 else if (coords != ATK_XY_SCREEN)
728 ret = pango_layout_xy_to_index (layout,
729 x_temp * PANGO_SCALE,
730 y_temp * PANGO_SCALE,
734 if (x_temp < 0 || y_temp < 0)
740 g_object_unref (layout);
743 if (coords == ATK_XY_WINDOW || coords == ATK_XY_SCREEN)
747 length = g_utf8_strlen (renderer_text, -1);
748 g_free (renderer_text);
753 g_free (renderer_text);
761 offset = g_utf8_pointer_to_offset (renderer_text,
762 renderer_text + index);
763 g_free (renderer_text);
770 gtk_text_cell_accessible_get_character_at_offset (AtkText *text,
776 string = GTK_TEXT_CELL_ACCESSIBLE(text)->cell_text;
781 if (offset >= g_utf8_strlen (string, -1))
784 index = g_utf8_offset_to_pointer (string, offset);
786 return g_utf8_get_char (index);
790 atk_text_interface_init (AtkTextIface *iface)
792 iface->get_text = gtk_text_cell_accessible_get_text;
793 iface->get_character_at_offset = gtk_text_cell_accessible_get_character_at_offset;
794 iface->get_text_before_offset = gtk_text_cell_accessible_get_text_before_offset;
795 iface->get_text_at_offset = gtk_text_cell_accessible_get_text_at_offset;
796 iface->get_text_after_offset = gtk_text_cell_accessible_get_text_after_offset;
797 iface->get_character_count = gtk_text_cell_accessible_get_character_count;
798 iface->get_caret_offset = gtk_text_cell_accessible_get_caret_offset;
799 iface->set_caret_offset = gtk_text_cell_accessible_set_caret_offset;
800 iface->get_run_attributes = gtk_text_cell_accessible_get_run_attributes;
801 iface->get_default_attributes = gtk_text_cell_accessible_get_default_attributes;
802 iface->get_character_extents = gtk_text_cell_accessible_get_character_extents;
803 iface->get_offset_at_point = gtk_text_cell_accessible_get_offset_at_point;