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;
394 gail_renderer = GAIL_RENDERER_CELL (text);
395 gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
397 parent = atk_object_get_parent (ATK_OBJECT (text));
398 if (GAIL_IS_CONTAINER_CELL (parent))
399 parent = atk_object_get_parent (parent);
400 g_return_val_if_fail (GAIL_IS_CELL_PARENT (parent), NULL);
401 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
402 layout = create_pango_layout (gtk_renderer, widget),
403 attrib_set = gail_misc_layout_get_run_attributes (attrib_set,
409 g_object_unref (G_OBJECT (layout));
414 static AtkAttributeSet*
415 gail_text_cell_get_default_attributes (AtkText *text)
417 GailRendererCell *gail_renderer;
418 GtkCellRendererText *gtk_renderer;
419 AtkAttributeSet *attrib_set = NULL;
424 gail_renderer = GAIL_RENDERER_CELL (text);
425 gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
427 parent = atk_object_get_parent (ATK_OBJECT (text));
428 if (GAIL_IS_CONTAINER_CELL (parent))
429 parent = atk_object_get_parent (parent);
430 g_return_val_if_fail (GAIL_IS_CELL_PARENT (parent), NULL);
431 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
432 layout = create_pango_layout (gtk_renderer, widget),
434 attrib_set = gail_misc_get_default_attributes (attrib_set,
437 g_object_unref (G_OBJECT (layout));
442 * This function is used by gail_text_cell_get_offset_at_point()
443 * and gail_text_cell_get_character_extents(). There is no
444 * cached PangoLayout for gailtextcell so we must create a temporary
445 * one using this function.
448 create_pango_layout(GtkCellRendererText *gtk_renderer,
451 PangoAttrList *attr_list;
453 PangoUnderline uline;
456 layout = gtk_widget_create_pango_layout (widget, gtk_renderer->text);
458 if (gtk_renderer->extra_attrs)
459 attr_list = pango_attr_list_copy (gtk_renderer->extra_attrs);
461 attr_list = pango_attr_list_new ();
463 if (gtk_renderer->foreground_set)
466 color = gtk_renderer->foreground;
467 add_attr (attr_list, pango_attr_foreground_new (color.red,
468 color.green, color.blue));
471 if (gtk_renderer->strikethrough_set)
473 pango_attr_strikethrough_new (gtk_renderer->strikethrough));
475 mask = pango_font_description_get_set_fields (gtk_renderer->font);
477 if (mask & PANGO_FONT_MASK_FAMILY)
479 pango_attr_family_new (pango_font_description_get_family (gtk_renderer->font)));
481 if (mask & PANGO_FONT_MASK_STYLE)
482 add_attr (attr_list, pango_attr_style_new (pango_font_description_get_style (gtk_renderer->font)));
484 if (mask & PANGO_FONT_MASK_VARIANT)
485 add_attr (attr_list, pango_attr_variant_new (pango_font_description_get_variant (gtk_renderer->font)));
487 if (mask & PANGO_FONT_MASK_WEIGHT)
488 add_attr (attr_list, pango_attr_weight_new (pango_font_description_get_weight (gtk_renderer->font)));
490 if (mask & PANGO_FONT_MASK_STRETCH)
491 add_attr (attr_list, pango_attr_stretch_new (pango_font_description_get_stretch (gtk_renderer->font)));
493 if (mask & PANGO_FONT_MASK_SIZE)
494 add_attr (attr_list, pango_attr_size_new (pango_font_description_get_size (gtk_renderer->font)));
496 if (gtk_renderer->scale_set &&
497 gtk_renderer->font_scale != 1.0)
498 add_attr (attr_list, pango_attr_scale_new (gtk_renderer->font_scale));
500 if (gtk_renderer->underline_set)
501 uline = gtk_renderer->underline_style;
503 uline = PANGO_UNDERLINE_NONE;
505 if (uline != PANGO_UNDERLINE_NONE)
507 pango_attr_underline_new (gtk_renderer->underline_style));
509 if (gtk_renderer->rise_set)
510 add_attr (attr_list, pango_attr_rise_new (gtk_renderer->rise));
512 pango_layout_set_attributes (layout, attr_list);
513 pango_layout_set_width (layout, -1);
514 pango_attr_list_unref (attr_list);
520 add_attr (PangoAttrList *attr_list,
521 PangoAttribute *attr)
523 attr->start_index = 0;
524 attr->end_index = G_MAXINT;
525 pango_attr_list_insert (attr_list, attr);
529 gail_text_cell_get_character_extents (AtkText *text,
537 GailRendererCell *gail_renderer;
538 GtkCellRendererText *gtk_renderer;
539 GdkRectangle rendered_rect;
542 PangoRectangle char_rect;
544 gint x_offset, y_offset, index, cell_height, cell_width;
546 if (!GAIL_TEXT_CELL (text)->cell_text)
548 *x = *y = *height = *width = 0;
551 if (offset < 0 || offset >= GAIL_TEXT_CELL (text)->cell_length)
553 *x = *y = *height = *width = 0;
556 gail_renderer = GAIL_RENDERER_CELL (text);
557 gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
559 * Thus would be inconsistent with the cache
561 gail_return_if_fail (gtk_renderer->text);
563 parent = atk_object_get_parent (ATK_OBJECT (text));
564 if (GAIL_IS_CONTAINER_CELL (parent))
565 parent = atk_object_get_parent (parent);
566 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
567 g_return_if_fail (GAIL_IS_CELL_PARENT (parent));
568 gail_cell_parent_get_cell_area (GAIL_CELL_PARENT (parent), GAIL_CELL (text),
571 gtk_cell_renderer_get_size (GTK_CELL_RENDERER (gtk_renderer), widget,
572 &rendered_rect, &x_offset, &y_offset, &cell_width, &cell_height);
573 layout = create_pango_layout (gtk_renderer, widget);
575 index = g_utf8_offset_to_pointer (gtk_renderer->text,
576 offset) - gtk_renderer->text;
577 pango_layout_index_to_pos (layout, index, &char_rect);
579 gail_misc_get_extents_from_pango_rectangle (widget,
581 x_offset + rendered_rect.x + gail_renderer->renderer->xpad,
582 y_offset + rendered_rect.y + gail_renderer->renderer->ypad,
583 x, y, width, height, coords);
584 g_object_unref (layout);
589 gail_text_cell_get_offset_at_point (AtkText *text,
595 GailRendererCell *gail_renderer;
596 GtkCellRendererText *gtk_renderer;
598 GdkRectangle rendered_rect;
600 gint x_offset, y_offset, index;
602 if (!GAIL_TEXT_CELL (text)->cell_text)
605 gail_renderer = GAIL_RENDERER_CELL (text);
606 gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
607 parent = atk_object_get_parent (ATK_OBJECT (text));
609 g_return_val_if_fail (gtk_renderer->text, -1);
610 if (GAIL_IS_CONTAINER_CELL (parent))
611 parent = atk_object_get_parent (parent);
613 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
615 g_return_val_if_fail (GAIL_IS_CELL_PARENT (parent), -1);
616 gail_cell_parent_get_cell_area (GAIL_CELL_PARENT (parent), GAIL_CELL (text),
618 gtk_cell_renderer_get_size (GTK_CELL_RENDERER (gtk_renderer), widget,
619 &rendered_rect, &x_offset, &y_offset, NULL, NULL);
621 layout = create_pango_layout (gtk_renderer, widget);
623 index = gail_misc_get_index_at_point_in_layout (widget, layout,
624 x_offset + rendered_rect.x + gail_renderer->renderer->xpad,
625 y_offset + rendered_rect.y + gail_renderer->renderer->ypad,
627 g_object_unref (layout);
630 if (coords == ATK_XY_WINDOW || coords == ATK_XY_SCREEN)
631 return g_utf8_strlen (gtk_renderer->text, -1);
636 return g_utf8_pointer_to_offset (gtk_renderer->text,
637 gtk_renderer->text + index);
641 gail_text_cell_get_character_at_offset (AtkText *text,
647 string = GAIL_TEXT_CELL(text)->cell_text;
652 if (offset >= g_utf8_strlen (string, -1))
655 index = g_utf8_offset_to_pointer (string, offset);
657 return g_utf8_get_char (index);