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 "gailtextcell.h"
24 #include "gailcontainercell.h"
25 #include "gailcellparent.h"
26 #include <libgail-util/gailmisc.h>
27 #include "gail-private-macros.h"
29 static void gail_text_cell_class_init (GailTextCellClass *klass);
30 static void gail_text_cell_init (GailTextCell *text_cell);
31 static void gail_text_cell_finalize (GObject *object);
33 static G_CONST_RETURN gchar* gail_text_cell_get_name (AtkObject *atk_obj);
35 static void atk_text_interface_init (AtkTextIface *iface);
39 static gchar* gail_text_cell_get_text (AtkText *text,
42 static gunichar gail_text_cell_get_character_at_offset (AtkText *text,
44 static gchar* gail_text_cell_get_text_before_offset (AtkText *text,
46 AtkTextBoundary boundary_type,
49 static gchar* gail_text_cell_get_text_at_offset (AtkText *text,
51 AtkTextBoundary boundary_type,
54 static gchar* gail_text_cell_get_text_after_offset (AtkText *text,
56 AtkTextBoundary boundary_type,
59 static gint gail_text_cell_get_character_count (AtkText *text);
60 static gint gail_text_cell_get_caret_offset (AtkText *text);
61 static gboolean gail_text_cell_set_caret_offset (AtkText *text,
63 static void gail_text_cell_get_character_extents (AtkText *text,
70 static gint gail_text_cell_get_offset_at_point (AtkText *text,
74 static AtkAttributeSet* gail_text_cell_get_run_attributes
79 static AtkAttributeSet* gail_text_cell_get_default_attributes
82 static PangoLayout* create_pango_layout (GtkCellRendererText *gtk_renderer,
84 static void add_attr (PangoAttrList *attr_list,
85 PangoAttribute *attr);
89 static gboolean gail_text_cell_update_cache (GailRendererCell *cell,
90 gboolean emit_change_signal);
92 gchar *gail_text_cell_property_list[] = {
93 /* Set font_desc first since it resets other values if it is NULL */
113 /* Also need the sets */
130 G_DEFINE_TYPE_WITH_CODE (GailTextCell, gail_text_cell, GAIL_TYPE_RENDERER_CELL,
131 G_IMPLEMENT_INTERFACE (ATK_TYPE_TEXT, atk_text_interface_init))
134 gail_text_cell_class_init (GailTextCellClass *klass)
136 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
137 AtkObjectClass *atk_object_class = ATK_OBJECT_CLASS (klass);
138 GailRendererCellClass *renderer_cell_class = GAIL_RENDERER_CELL_CLASS (klass);
140 renderer_cell_class->update_cache = gail_text_cell_update_cache;
141 renderer_cell_class->property_list = gail_text_cell_property_list;
143 atk_object_class->get_name = gail_text_cell_get_name;
145 gobject_class->finalize = gail_text_cell_finalize;
151 gail_text_cell_init (GailTextCell *text_cell)
153 text_cell->cell_text = NULL;
154 text_cell->caret_pos = 0;
155 text_cell->cell_length = 0;
156 text_cell->textutil = gail_text_util_new ();
157 atk_state_set_add_state (GAIL_CELL (text_cell)->state_set,
158 ATK_STATE_SINGLE_LINE);
162 gail_text_cell_new (void)
165 AtkObject *atk_object;
166 GailRendererCell *cell;
168 object = g_object_new (GAIL_TYPE_TEXT_CELL, NULL);
170 g_return_val_if_fail (object != NULL, NULL);
172 atk_object = ATK_OBJECT (object);
173 atk_object->role = ATK_ROLE_TABLE_CELL;
175 cell = GAIL_RENDERER_CELL(object);
177 cell->renderer = gtk_cell_renderer_text_new ();
178 g_object_ref_sink (cell->renderer);
183 gail_text_cell_finalize (GObject *object)
185 GailTextCell *text_cell = GAIL_TEXT_CELL (object);
187 g_object_unref (text_cell->textutil);
188 g_free (text_cell->cell_text);
190 G_OBJECT_CLASS (gail_text_cell_parent_class)->finalize (object);
193 static G_CONST_RETURN gchar*
194 gail_text_cell_get_name (AtkObject *atk_obj)
197 return atk_obj->name;
200 GailTextCell *text_cell = GAIL_TEXT_CELL (atk_obj);
202 return text_cell->cell_text;
207 gail_text_cell_update_cache (GailRendererCell *cell,
208 gboolean emit_change_signal)
210 GailTextCell *text_cell = GAIL_TEXT_CELL (cell);
211 AtkObject *obj = ATK_OBJECT (cell);
216 g_object_get (G_OBJECT (cell->renderer), "text", &new_cache, NULL);
218 if (text_cell->cell_text)
221 * If the new value is NULL and the old value isn't NULL, then the
224 if (new_cache == NULL ||
225 strcmp (text_cell->cell_text, new_cache))
227 g_free (text_cell->cell_text);
228 temp_length = text_cell->cell_length;
229 text_cell->cell_text = NULL;
230 text_cell->cell_length = 0;
231 if (emit_change_signal)
233 g_signal_emit_by_name (cell, "text_changed::delete", 0, temp_length);
234 if (obj->name == NULL)
235 g_object_notify (G_OBJECT (obj), "accessible-name");
246 if (new_cache == NULL)
248 text_cell->cell_text = g_strdup ("");
249 text_cell->cell_length = 0;
253 text_cell->cell_text = g_strdup (new_cache);
254 text_cell->cell_length = g_utf8_strlen (new_cache, -1);
259 gail_text_util_text_setup (text_cell->textutil, text_cell->cell_text);
263 if (emit_change_signal)
265 g_signal_emit_by_name (cell, "text_changed::insert",
266 0, text_cell->cell_length);
268 if (obj->name == NULL)
269 g_object_notify (G_OBJECT (obj), "accessible-name");
276 atk_text_interface_init (AtkTextIface *iface)
278 iface->get_text = gail_text_cell_get_text;
279 iface->get_character_at_offset = gail_text_cell_get_character_at_offset;
280 iface->get_text_before_offset = gail_text_cell_get_text_before_offset;
281 iface->get_text_at_offset = gail_text_cell_get_text_at_offset;
282 iface->get_text_after_offset = gail_text_cell_get_text_after_offset;
283 iface->get_character_count = gail_text_cell_get_character_count;
284 iface->get_caret_offset = gail_text_cell_get_caret_offset;
285 iface->set_caret_offset = gail_text_cell_set_caret_offset;
286 iface->get_run_attributes = gail_text_cell_get_run_attributes;
287 iface->get_default_attributes = gail_text_cell_get_default_attributes;
288 iface->get_character_extents = gail_text_cell_get_character_extents;
289 iface->get_offset_at_point = gail_text_cell_get_offset_at_point;
293 gail_text_cell_get_text (AtkText *text,
297 if (GAIL_TEXT_CELL (text)->cell_text)
298 return gail_text_util_get_substring (GAIL_TEXT_CELL (text)->textutil,
301 return g_strdup ("");
305 gail_text_cell_get_text_before_offset (AtkText *text,
307 AtkTextBoundary boundary_type,
311 return gail_text_util_get_text (GAIL_TEXT_CELL (text)->textutil,
312 NULL, GAIL_BEFORE_OFFSET, boundary_type, offset, start_offset,
317 gail_text_cell_get_text_at_offset (AtkText *text,
319 AtkTextBoundary boundary_type,
323 return gail_text_util_get_text (GAIL_TEXT_CELL (text)->textutil,
324 NULL, GAIL_AT_OFFSET, boundary_type, offset, start_offset, end_offset);
328 gail_text_cell_get_text_after_offset (AtkText *text,
330 AtkTextBoundary boundary_type,
334 return gail_text_util_get_text (GAIL_TEXT_CELL (text)->textutil,
335 NULL, GAIL_AFTER_OFFSET, boundary_type, offset, start_offset,
340 gail_text_cell_get_character_count (AtkText *text)
342 if (GAIL_TEXT_CELL (text)->cell_text != NULL)
343 return GAIL_TEXT_CELL (text)->cell_length;
349 gail_text_cell_get_caret_offset (AtkText *text)
351 return GAIL_TEXT_CELL (text)->caret_pos;
355 gail_text_cell_set_caret_offset (AtkText *text,
358 GailTextCell *text_cell = GAIL_TEXT_CELL (text);
360 if (text_cell->cell_text == NULL)
365 /* Only set the caret within the bounds and if it is to a new position. */
367 offset <= text_cell->cell_length &&
368 offset != text_cell->caret_pos)
370 text_cell->caret_pos = offset;
372 /* emit the signal */
373 g_signal_emit_by_name (text, "text_caret_moved", offset);
381 static AtkAttributeSet*
382 gail_text_cell_get_run_attributes (AtkText *text,
387 GailRendererCell *gail_renderer;
388 GtkCellRendererText *gtk_renderer;
389 AtkAttributeSet *attrib_set = NULL;
393 gchar *renderer_text;
395 gail_renderer = GAIL_RENDERER_CELL (text);
396 gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
398 parent = atk_object_get_parent (ATK_OBJECT (text));
399 if (GAIL_IS_CONTAINER_CELL (parent))
400 parent = atk_object_get_parent (parent);
401 g_return_val_if_fail (GAIL_IS_CELL_PARENT (parent), NULL);
402 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
403 layout = create_pango_layout (gtk_renderer, widget),
404 g_object_get (gtk_renderer, "text", &renderer_text, NULL);
405 attrib_set = gail_misc_layout_get_run_attributes (attrib_set,
411 g_free (renderer_text);
412 g_object_unref (G_OBJECT (layout));
417 static AtkAttributeSet*
418 gail_text_cell_get_default_attributes (AtkText *text)
420 GailRendererCell *gail_renderer;
421 GtkCellRendererText *gtk_renderer;
422 AtkAttributeSet *attrib_set = NULL;
427 gail_renderer = GAIL_RENDERER_CELL (text);
428 gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
430 parent = atk_object_get_parent (ATK_OBJECT (text));
431 if (GAIL_IS_CONTAINER_CELL (parent))
432 parent = atk_object_get_parent (parent);
433 g_return_val_if_fail (GAIL_IS_CELL_PARENT (parent), NULL);
434 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
435 layout = create_pango_layout (gtk_renderer, widget),
437 attrib_set = gail_misc_get_default_attributes (attrib_set,
440 g_object_unref (G_OBJECT (layout));
445 * This function is used by gail_text_cell_get_offset_at_point()
446 * and gail_text_cell_get_character_extents(). There is no
447 * cached PangoLayout for gailtextcell so we must create a temporary
448 * one using this function.
451 create_pango_layout(GtkCellRendererText *gtk_renderer,
454 GdkColor *foreground_gdk;
455 PangoAttrList *attr_list, *attributes;
457 PangoUnderline uline, underline;
459 PangoFontDescription *font_desc;
460 gboolean foreground_set, strikethrough_set, strikethrough;
461 gboolean scale_set, underline_set, rise_set;
462 gchar *renderer_text;
466 g_object_get (gtk_renderer,
467 "text", &renderer_text,
468 "attributes", &attributes,
469 "foreground-set", &foreground_set,
470 "foreground-gdk", &foreground_gdk,
471 "strikethrough-set", &strikethrough_set,
472 "strikethrough", &strikethrough,
473 "font-desc", &font_desc,
474 "scale-set", &scale_set,
476 "underline-set", &underline_set,
477 "underline", &underline,
478 "rise-set", &rise_set,
482 layout = gtk_widget_create_pango_layout (widget, renderer_text);
485 attr_list = pango_attr_list_copy (attributes);
487 attr_list = pango_attr_list_new ();
491 add_attr (attr_list, pango_attr_foreground_new (foreground_gdk->red,
492 foreground_gdk->green,
493 foreground_gdk->blue));
496 if (strikethrough_set)
498 pango_attr_strikethrough_new (strikethrough));
500 mask = pango_font_description_get_set_fields (font_desc);
502 if (mask & PANGO_FONT_MASK_FAMILY)
504 pango_attr_family_new (pango_font_description_get_family (font_desc)));
506 if (mask & PANGO_FONT_MASK_STYLE)
507 add_attr (attr_list, pango_attr_style_new (pango_font_description_get_style (font_desc)));
509 if (mask & PANGO_FONT_MASK_VARIANT)
510 add_attr (attr_list, pango_attr_variant_new (pango_font_description_get_variant (font_desc)));
512 if (mask & PANGO_FONT_MASK_WEIGHT)
513 add_attr (attr_list, pango_attr_weight_new (pango_font_description_get_weight (font_desc)));
515 if (mask & PANGO_FONT_MASK_STRETCH)
516 add_attr (attr_list, pango_attr_stretch_new (pango_font_description_get_stretch (font_desc)));
518 if (mask & PANGO_FONT_MASK_SIZE)
519 add_attr (attr_list, pango_attr_size_new (pango_font_description_get_size (font_desc)));
521 if (scale_set && scale != 1.0)
522 add_attr (attr_list, pango_attr_scale_new (scale));
527 uline = PANGO_UNDERLINE_NONE;
529 if (uline != PANGO_UNDERLINE_NONE)
531 pango_attr_underline_new (underline));
534 add_attr (attr_list, pango_attr_rise_new (rise));
536 pango_layout_set_attributes (layout, attr_list);
537 pango_layout_set_width (layout, -1);
538 pango_attr_list_unref (attr_list);
540 pango_font_description_free (font_desc);
541 g_free (foreground_gdk);
542 pango_attr_list_unref (attributes);
543 g_free (renderer_text);
549 add_attr (PangoAttrList *attr_list,
550 PangoAttribute *attr)
552 attr->start_index = 0;
553 attr->end_index = G_MAXINT;
554 pango_attr_list_insert (attr_list, attr);
558 gail_text_cell_get_character_extents (AtkText *text,
566 GailRendererCell *gail_renderer;
567 GtkRequisition min_size;
568 GtkCellRendererText *gtk_renderer;
569 GdkRectangle rendered_rect;
572 PangoRectangle char_rect;
574 gchar *renderer_text;
575 gint x_offset, y_offset, index;
578 if (!GAIL_TEXT_CELL (text)->cell_text)
580 *x = *y = *height = *width = 0;
583 if (offset < 0 || offset >= GAIL_TEXT_CELL (text)->cell_length)
585 *x = *y = *height = *width = 0;
588 gail_renderer = GAIL_RENDERER_CELL (text);
589 gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
591 * Thus would be inconsistent with the cache
593 g_object_get (gtk_renderer, "text", &renderer_text, NULL);
596 g_free (renderer_text);
600 parent = atk_object_get_parent (ATK_OBJECT (text));
601 if (GAIL_IS_CONTAINER_CELL (parent))
602 parent = atk_object_get_parent (parent);
603 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
604 g_return_if_fail (GAIL_IS_CELL_PARENT (parent));
605 gail_cell_parent_get_cell_area (GAIL_CELL_PARENT (parent), GAIL_CELL (text),
608 gtk_cell_renderer_get_preferred_size (GTK_CELL_RENDERER (gtk_renderer),
612 gtk_cell_renderer_calc_offset (GTK_CELL_RENDERER (gtk_renderer), &rendered_rect,
613 gtk_widget_get_direction (widget),
614 min_size.width, min_size.height,
615 &x_offset, &y_offset);
617 layout = create_pango_layout (gtk_renderer, widget);
619 index = g_utf8_offset_to_pointer (renderer_text, offset) - renderer_text;
620 pango_layout_index_to_pos (layout, index, &char_rect);
622 gtk_cell_renderer_get_padding (gail_renderer->renderer, &xpad, &ypad);
623 gail_misc_get_extents_from_pango_rectangle (widget,
625 x_offset + rendered_rect.x + xpad,
626 y_offset + rendered_rect.y + ypad,
627 x, y, width, height, coords);
629 g_free (renderer_text);
630 g_object_unref (layout);
636 gail_text_cell_get_offset_at_point (AtkText *text,
642 GailRendererCell *gail_renderer;
643 GtkCellRendererText *gtk_renderer;
644 GtkRequisition min_size;
646 GdkRectangle rendered_rect;
648 gchar *renderer_text;
650 gfloat xalign, yalign;
651 gint x_offset, y_offset, index;
654 if (!GAIL_TEXT_CELL (text)->cell_text)
657 gail_renderer = GAIL_RENDERER_CELL (text);
658 gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
659 parent = atk_object_get_parent (ATK_OBJECT (text));
661 g_object_get (gtk_renderer, "text", &renderer_text, NULL);
664 g_free (renderer_text);
668 if (GAIL_IS_CONTAINER_CELL (parent))
669 parent = atk_object_get_parent (parent);
671 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
673 g_return_val_if_fail (GAIL_IS_CELL_PARENT (parent), -1);
674 gail_cell_parent_get_cell_area (GAIL_CELL_PARENT (parent), GAIL_CELL (text),
677 gtk_cell_renderer_get_preferred_size (GTK_CELL_RENDERER (gtk_renderer),
680 gtk_cell_renderer_get_fixed_size (GTK_CELL_RENDERER (gtk_renderer), &width, &height);
681 gtk_cell_renderer_get_alignment (GTK_CELL_RENDERER (gtk_renderer), &xalign, &yalign);
682 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
683 xalign = 1.0 - xalign;
684 x_offset = MAX (0, xalign * (width - min_size.width));
685 y_offset = MAX (0, yalign * (height - min_size.height));
687 layout = create_pango_layout (gtk_renderer, widget);
689 gtk_cell_renderer_get_padding (gail_renderer->renderer, &xpad, &ypad);
690 index = gail_misc_get_index_at_point_in_layout (widget, layout,
691 x_offset + rendered_rect.x + xpad,
692 y_offset + rendered_rect.y + ypad,
694 g_object_unref (layout);
697 if (coords == ATK_XY_WINDOW || coords == ATK_XY_SCREEN)
701 length = g_utf8_strlen (renderer_text, -1);
702 g_free (renderer_text);
707 g_free (renderer_text);
715 offset = g_utf8_pointer_to_offset (renderer_text,
716 renderer_text + index);
717 g_free (renderer_text);
724 gail_text_cell_get_character_at_offset (AtkText *text,
730 string = GAIL_TEXT_CELL(text)->cell_text;
735 if (offset >= g_utf8_strlen (string, -1))
738 index = g_utf8_offset_to_pointer (string, offset);
740 return g_utf8_get_char (index);