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 "gtkcellaccessibleparent.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 void gtk_text_cell_accessible_update_cache (GtkCellAccessible *cell);
85 static void atk_text_interface_init (AtkTextIface *iface);
87 G_DEFINE_TYPE_WITH_CODE (GtkTextCellAccessible, _gtk_text_cell_accessible, GTK_TYPE_RENDERER_CELL_ACCESSIBLE,
88 G_IMPLEMENT_INTERFACE (ATK_TYPE_TEXT, atk_text_interface_init))
91 gtk_text_cell_accessible_ref_state_set (AtkObject *accessible)
93 AtkStateSet *state_set;
95 state_set = ATK_OBJECT_CLASS (_gtk_text_cell_accessible_parent_class)->ref_state_set (accessible);
97 atk_state_set_add_state (state_set, ATK_STATE_SINGLE_LINE);
103 gtk_text_cell_accessible_finalize (GObject *object)
105 GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (object);
107 g_free (text_cell->cell_text);
109 G_OBJECT_CLASS (_gtk_text_cell_accessible_parent_class)->finalize (object);
113 gtk_text_cell_accessible_get_name (AtkObject *atk_obj)
115 GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (atk_obj);
118 return atk_obj->name;
120 return text_cell->cell_text;
124 gtk_text_cell_accessible_update_cache (GtkCellAccessible *cell)
126 GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (cell);
127 AtkObject *obj = ATK_OBJECT (cell);
132 g_object_get (G_OBJECT (GTK_RENDERER_CELL_ACCESSIBLE (cell)->renderer),
136 if (text_cell->cell_text)
138 if (text == NULL || g_strcmp0 (text_cell->cell_text, text) != 0)
140 g_free (text_cell->cell_text);
141 temp_length = text_cell->cell_length;
142 text_cell->cell_text = NULL;
143 text_cell->cell_length = 0;
144 g_signal_emit_by_name (cell, "text-changed::delete", 0, temp_length);
145 if (obj->name == NULL)
146 g_object_notify (G_OBJECT (obj), "accessible-name");
158 text_cell->cell_text = g_strdup ("");
159 text_cell->cell_length = 0;
163 text_cell->cell_text = g_strdup (text);
164 text_cell->cell_length = g_utf8_strlen (text, -1);
172 g_signal_emit_by_name (cell, "text-changed::insert",
173 0, text_cell->cell_length);
175 if (obj->name == NULL)
176 g_object_notify (G_OBJECT (obj), "accessible-name");
181 _gtk_text_cell_accessible_class_init (GtkTextCellAccessibleClass *klass)
183 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
184 AtkObjectClass *atk_object_class = ATK_OBJECT_CLASS (klass);
185 GtkCellAccessibleClass *cell_class = GTK_CELL_ACCESSIBLE_CLASS (klass);
187 cell_class->update_cache = gtk_text_cell_accessible_update_cache;
189 atk_object_class->get_name = gtk_text_cell_accessible_get_name;
190 atk_object_class->ref_state_set = gtk_text_cell_accessible_ref_state_set;
192 gobject_class->finalize = gtk_text_cell_accessible_finalize;
196 _gtk_text_cell_accessible_init (GtkTextCellAccessible *text_cell)
198 text_cell->cell_text = NULL;
199 text_cell->caret_pos = 0;
200 text_cell->cell_length = 0;
204 gtk_text_cell_accessible_get_text (AtkText *atk_text,
210 text = GTK_TEXT_CELL_ACCESSIBLE (atk_text)->cell_text;
212 return g_utf8_substring (text, start_pos, end_pos > -1 ? end_pos : g_utf8_strlen (text, -1));
214 return g_strdup ("");
218 gtk_text_cell_accessible_get_text_before_offset (AtkText *atk_text,
220 AtkTextBoundary boundary_type,
227 layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (atk_text));
228 text = _gtk_pango_get_text_before (layout, boundary_type, offset, start_offset, end_offset);
229 g_object_unref (layout);
235 gtk_text_cell_accessible_get_text_at_offset (AtkText *atk_text,
237 AtkTextBoundary boundary_type,
244 layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (atk_text));
245 text = _gtk_pango_get_text_at (layout, boundary_type, offset, start_offset, end_offset);
246 g_object_unref (layout);
252 gtk_text_cell_accessible_get_text_after_offset (AtkText *atk_text,
254 AtkTextBoundary boundary_type,
261 layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (atk_text));
262 text = _gtk_pango_get_text_after (layout, boundary_type, offset, start_offset, end_offset);
263 g_object_unref (layout);
269 gtk_text_cell_accessible_get_character_count (AtkText *text)
271 if (GTK_TEXT_CELL_ACCESSIBLE (text)->cell_text != NULL)
272 return GTK_TEXT_CELL_ACCESSIBLE (text)->cell_length;
278 gtk_text_cell_accessible_get_caret_offset (AtkText *text)
280 return GTK_TEXT_CELL_ACCESSIBLE (text)->caret_pos;
284 gtk_text_cell_accessible_set_caret_offset (AtkText *text,
287 GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (text);
289 if (text_cell->cell_text == NULL)
293 /* Only set the caret within the bounds and if it is to a new position. */
295 offset <= text_cell->cell_length &&
296 offset != text_cell->caret_pos)
298 text_cell->caret_pos = offset;
300 /* emit the signal */
301 g_signal_emit_by_name (text, "text-caret-moved", offset);
309 static AtkAttributeSet *
310 gtk_text_cell_accessible_get_run_attributes (AtkText *text,
315 AtkAttributeSet *attrib_set = NULL;
318 layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
319 attrib_set = _gtk_pango_get_run_attributes (NULL, layout, offset, start_offset, end_offset);
320 g_object_unref (G_OBJECT (layout));
325 static AtkAttributeSet *
326 add_attribute (AtkAttributeSet *attributes,
327 AtkTextAttribute attr,
332 at = g_new (AtkAttribute, 1);
333 at->name = g_strdup (atk_text_attribute_get_name (attr));
334 at->value = g_strdup (value);
336 return g_slist_prepend (attributes, at);
339 static AtkAttributeSet *
340 gtk_text_cell_accessible_get_default_attributes (AtkText *text)
342 AtkAttributeSet *attrib_set = NULL;
346 layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
347 widget = get_widget (GTK_TEXT_CELL_ACCESSIBLE (text));
349 attrib_set = add_attribute (attrib_set, ATK_TEXT_ATTR_DIRECTION,
350 atk_text_attribute_get_value (ATK_TEXT_ATTR_DIRECTION,
351 gtk_widget_get_direction (widget)));
352 attrib_set = _gtk_pango_get_default_attributes (NULL, layout);
354 attrib_set = _gtk_style_context_get_attributes (attrib_set,
355 gtk_widget_get_style_context (widget),
356 gtk_widget_get_state_flags (widget));
358 g_object_unref (G_OBJECT (layout));
364 get_widget (GtkTextCellAccessible *text)
368 parent = atk_object_get_parent (ATK_OBJECT (text));
369 if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
370 parent = atk_object_get_parent (parent);
372 return gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
375 /* This function is used by gtk_text_cell_accessible_get_offset_at_point()
376 * and gtk_text_cell_accessible_get_character_extents(). There is no
377 * cached PangoLayout so we must create a temporary one using this function.
380 create_pango_layout (GtkTextCellAccessible *text)
382 GdkRGBA *foreground_rgba;
383 PangoAttrList *attr_list, *attributes;
385 PangoUnderline uline, underline;
387 PangoFontDescription *font_desc;
388 gboolean foreground_set, strikethrough_set, strikethrough;
389 gboolean scale_set, underline_set, rise_set;
390 gchar *renderer_text;
393 GtkRendererCellAccessible *gail_renderer;
394 GtkCellRendererText *gtk_renderer;
396 gail_renderer = GTK_RENDERER_CELL_ACCESSIBLE (text);
397 gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
399 g_object_get (gtk_renderer,
400 "text", &renderer_text,
401 "attributes", &attributes,
402 "foreground-set", &foreground_set,
403 "foreground-rgba", &foreground_rgba,
404 "strikethrough-set", &strikethrough_set,
405 "strikethrough", &strikethrough,
406 "font-desc", &font_desc,
407 "scale-set", &scale_set,
409 "underline-set", &underline_set,
410 "underline", &underline,
411 "rise-set", &rise_set,
415 layout = gtk_widget_create_pango_layout (get_widget (text), renderer_text);
418 attr_list = pango_attr_list_copy (attributes);
420 attr_list = pango_attr_list_new ();
424 add_attr (attr_list, pango_attr_foreground_new (foreground_rgba->red * 65535,
425 foreground_rgba->green * 65535,
426 foreground_rgba->blue * 65535));
429 if (strikethrough_set)
431 pango_attr_strikethrough_new (strikethrough));
433 mask = pango_font_description_get_set_fields (font_desc);
435 if (mask & PANGO_FONT_MASK_FAMILY)
437 pango_attr_family_new (pango_font_description_get_family (font_desc)));
439 if (mask & PANGO_FONT_MASK_STYLE)
440 add_attr (attr_list, pango_attr_style_new (pango_font_description_get_style (font_desc)));
442 if (mask & PANGO_FONT_MASK_VARIANT)
443 add_attr (attr_list, pango_attr_variant_new (pango_font_description_get_variant (font_desc)));
445 if (mask & PANGO_FONT_MASK_WEIGHT)
446 add_attr (attr_list, pango_attr_weight_new (pango_font_description_get_weight (font_desc)));
448 if (mask & PANGO_FONT_MASK_STRETCH)
449 add_attr (attr_list, pango_attr_stretch_new (pango_font_description_get_stretch (font_desc)));
451 if (mask & PANGO_FONT_MASK_SIZE)
452 add_attr (attr_list, pango_attr_size_new (pango_font_description_get_size (font_desc)));
454 if (scale_set && scale != 1.0)
455 add_attr (attr_list, pango_attr_scale_new (scale));
460 uline = PANGO_UNDERLINE_NONE;
462 if (uline != PANGO_UNDERLINE_NONE)
464 pango_attr_underline_new (underline));
467 add_attr (attr_list, pango_attr_rise_new (rise));
469 pango_layout_set_attributes (layout, attr_list);
470 pango_layout_set_width (layout, -1);
471 pango_attr_list_unref (attr_list);
473 pango_font_description_free (font_desc);
474 pango_attr_list_unref (attributes);
475 g_free (renderer_text);
476 gdk_rgba_free (foreground_rgba);
482 add_attr (PangoAttrList *attr_list,
483 PangoAttribute *attr)
485 attr->start_index = 0;
486 attr->end_index = G_MAXINT;
487 pango_attr_list_insert (attr_list, attr);
492 get_origins (GtkWidget *widget,
500 if (GTK_IS_TREE_VIEW (widget))
501 window = gtk_tree_view_get_bin_window (GTK_TREE_VIEW (widget));
503 window = gtk_widget_get_window (widget);
505 gdk_window_get_origin (window, x_window, y_window);
506 window = gdk_window_get_toplevel (gtk_widget_get_window (widget));
507 gdk_window_get_origin (window, x_toplevel, y_toplevel);
511 gtk_text_cell_accessible_get_character_extents (AtkText *text,
519 GtkRendererCellAccessible *gail_renderer;
520 GtkRequisition min_size;
521 GtkCellRendererText *gtk_renderer;
522 GdkRectangle rendered_rect;
525 PangoRectangle char_rect;
527 gchar *renderer_text;
528 gfloat xalign, yalign;
529 gint x_offset, y_offset, index;
531 gint x_window, y_window, x_toplevel, y_toplevel;
533 if (!GTK_TEXT_CELL_ACCESSIBLE (text)->cell_text)
535 *x = *y = *height = *width = 0;
538 if (offset < 0 || offset >= GTK_TEXT_CELL_ACCESSIBLE (text)->cell_length)
540 *x = *y = *height = *width = 0;
543 gail_renderer = GTK_RENDERER_CELL_ACCESSIBLE (text);
544 gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
546 g_object_get (gtk_renderer, "text", &renderer_text, NULL);
549 g_free (renderer_text);
553 parent = atk_object_get_parent (ATK_OBJECT (text));
554 if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
555 parent = atk_object_get_parent (parent);
556 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
557 g_return_if_fail (GTK_IS_CELL_ACCESSIBLE_PARENT (parent));
558 _gtk_cell_accessible_parent_get_cell_area (GTK_CELL_ACCESSIBLE_PARENT (parent),
559 GTK_CELL_ACCESSIBLE (text),
562 gtk_cell_renderer_get_preferred_size (GTK_CELL_RENDERER (gtk_renderer),
566 gtk_cell_renderer_get_alignment (GTK_CELL_RENDERER (gtk_renderer), &xalign, &yalign);
567 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
568 xalign = 1.0 - xalign;
569 x_offset = MAX (0, xalign * (rendered_rect.width - min_size.width));
570 y_offset = MAX (0, yalign * (rendered_rect.height - min_size.height));
572 layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
574 index = g_utf8_offset_to_pointer (renderer_text, offset) - renderer_text;
575 pango_layout_index_to_pos (layout, index, &char_rect);
577 gtk_cell_renderer_get_padding (gail_renderer->renderer, &xpad, &ypad);
579 get_origins (widget, &x_window, &y_window, &x_toplevel, &y_toplevel);
581 *x = (char_rect.x / PANGO_SCALE) + x_offset + rendered_rect.x + xpad + x_window;
582 *y = (char_rect.y / PANGO_SCALE) + y_offset + rendered_rect.y + ypad + y_window;
583 *height = char_rect.height / PANGO_SCALE;
584 *width = char_rect.width / PANGO_SCALE;
586 if (coords == ATK_XY_WINDOW)
591 else if (coords != ATK_XY_SCREEN)
599 g_free (renderer_text);
600 g_object_unref (layout);
604 gtk_text_cell_accessible_get_offset_at_point (AtkText *text,
610 GtkRendererCellAccessible *gail_renderer;
611 GtkCellRendererText *gtk_renderer;
612 GtkRequisition min_size;
614 GdkRectangle rendered_rect;
616 gchar *renderer_text;
617 gfloat xalign, yalign;
618 gint x_offset, y_offset, index;
620 gint x_window, y_window, x_toplevel, y_toplevel;
624 if (!GTK_TEXT_CELL_ACCESSIBLE (text)->cell_text)
627 gail_renderer = GTK_RENDERER_CELL_ACCESSIBLE (text);
628 gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
629 parent = atk_object_get_parent (ATK_OBJECT (text));
631 g_object_get (gtk_renderer, "text", &renderer_text, NULL);
634 g_free (renderer_text);
638 if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
639 parent = atk_object_get_parent (parent);
641 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
643 g_return_val_if_fail (GTK_IS_CELL_ACCESSIBLE_PARENT (parent), -1);
644 _gtk_cell_accessible_parent_get_cell_area (GTK_CELL_ACCESSIBLE_PARENT (parent),
645 GTK_CELL_ACCESSIBLE (text),
648 gtk_cell_renderer_get_preferred_size (GTK_CELL_RENDERER (gtk_renderer),
651 gtk_cell_renderer_get_alignment (GTK_CELL_RENDERER (gtk_renderer), &xalign, &yalign);
652 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
653 xalign = 1.0 - xalign;
654 x_offset = MAX (0, xalign * (rendered_rect.width - min_size.width));
655 y_offset = MAX (0, yalign * (rendered_rect.height - min_size.height));
657 layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
659 gtk_cell_renderer_get_padding (gail_renderer->renderer, &xpad, &ypad);
661 get_origins (widget, &x_window, &y_window, &x_toplevel, &y_toplevel);
663 x_temp = x - (x_offset + rendered_rect.x + xpad) - x_window;
664 y_temp = y - (y_offset + rendered_rect.y + ypad) - y_window;
665 if (coords == ATK_XY_WINDOW)
667 x_temp += x_toplevel;
668 y_temp += y_toplevel;
670 else if (coords != ATK_XY_SCREEN)
673 ret = pango_layout_xy_to_index (layout,
674 x_temp * PANGO_SCALE,
675 y_temp * PANGO_SCALE,
679 if (x_temp < 0 || y_temp < 0)
685 g_object_unref (layout);
688 if (coords == ATK_XY_WINDOW || coords == ATK_XY_SCREEN)
692 length = g_utf8_strlen (renderer_text, -1);
693 g_free (renderer_text);
698 g_free (renderer_text);
706 offset = g_utf8_pointer_to_offset (renderer_text,
707 renderer_text + index);
708 g_free (renderer_text);
715 gtk_text_cell_accessible_get_character_at_offset (AtkText *text,
721 string = GTK_TEXT_CELL_ACCESSIBLE(text)->cell_text;
726 if (offset >= g_utf8_strlen (string, -1))
729 index = g_utf8_offset_to_pointer (string, offset);
731 return g_utf8_get_char (index);
735 atk_text_interface_init (AtkTextIface *iface)
737 iface->get_text = gtk_text_cell_accessible_get_text;
738 iface->get_character_at_offset = gtk_text_cell_accessible_get_character_at_offset;
739 iface->get_text_before_offset = gtk_text_cell_accessible_get_text_before_offset;
740 iface->get_text_at_offset = gtk_text_cell_accessible_get_text_at_offset;
741 iface->get_text_after_offset = gtk_text_cell_accessible_get_text_after_offset;
742 iface->get_character_count = gtk_text_cell_accessible_get_character_count;
743 iface->get_caret_offset = gtk_text_cell_accessible_get_caret_offset;
744 iface->set_caret_offset = gtk_text_cell_accessible_set_caret_offset;
745 iface->get_run_attributes = gtk_text_cell_accessible_get_run_attributes;
746 iface->get_default_attributes = gtk_text_cell_accessible_get_default_attributes;
747 iface->get_character_extents = gtk_text_cell_accessible_get_character_extents;
748 iface->get_offset_at_point = gtk_text_cell_accessible_get_offset_at_point;