]> Pileus Git - ~andy/gtk/blob - gtk/a11y/gtktextcellaccessible.c
Convert GailTextCell to GtkTextCellAccessible
[~andy/gtk] / gtk / a11y / gtktextcellaccessible.c
1 /* GAIL - The GNOME Accessibility Enabling Library
2  * Copyright 2001 Sun Microsystems Inc.
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "config.h"
21 #include <string.h>
22 #include <gtk/gtk.h>
23 #include "../gtkpango.h"
24 #include "gtktextcellaccessible.h"
25 #include "gtkcontainercellaccessible.h"
26 #include "gailcellparent.h"
27
28 static const gchar* gtk_text_cell_accessible_get_name    (AtkObject      *atk_obj);
29
30
31 /* atktext.h */
32
33 static gchar*    gtk_text_cell_accessible_get_text                (AtkText        *text,
34                                                         gint            start_pos,
35                                                         gint            end_pos);
36 static gunichar gtk_text_cell_accessible_get_character_at_offset  (AtkText        *text,
37                                                          gint           offset);
38 static gchar*   gtk_text_cell_accessible_get_text_before_offset   (AtkText        *text,
39                                                          gint           offset,
40                                                          AtkTextBoundary boundary_type,
41                                                          gint           *start_offset,
42                                                          gint           *end_offset);
43 static gchar*   gtk_text_cell_accessible_get_text_at_offset       (AtkText        *text,
44                                                          gint           offset,
45                                                          AtkTextBoundary boundary_type,
46                                                          gint           *start_offset,
47                                                          gint           *end_offset);
48 static gchar*   gtk_text_cell_accessible_get_text_after_offset    (AtkText        *text,
49                                                          gint           offset,
50                                                          AtkTextBoundary boundary_type,
51                                                          gint           *start_offset,
52                                                          gint           *end_offset);
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,
56                                                          gint           offset);
57 static void      gtk_text_cell_accessible_get_character_extents   (AtkText        *text,
58                                                          gint           offset,
59                                                          gint           *x,
60                                                          gint           *y,
61                                                          gint           *width,
62                                                          gint           *height,
63                                                          AtkCoordType   coords);
64 static gint      gtk_text_cell_accessible_get_offset_at_point     (AtkText        *text,
65                                                          gint           x,
66                                                          gint           y,
67                                                          AtkCoordType   coords);
68 static AtkAttributeSet* gtk_text_cell_accessible_get_run_attributes 
69                                                         (AtkText        *text,
70                                                          gint           offset,
71                                                          gint           *start_offset,      
72                                                          gint           *end_offset); 
73 static AtkAttributeSet* gtk_text_cell_accessible_get_default_attributes 
74                                                         (AtkText        *text);
75
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);
80
81 /* Misc */
82
83 static gboolean gtk_text_cell_accessible_update_cache             (GtkRendererCellAccessible *cell,
84                                                          gboolean       emit_change_signal);
85
86 static gchar *property_list[] = {
87   /* Set font_desc first since it resets other values if it is NULL */
88   "font_desc",
89   "attributes",
90   "background-gdk",
91   "editable",
92   "family",
93   "foreground-gdk",
94   "rise",
95   "scale",
96   "size",
97   "size-points",
98   "stretch",
99   "strikethrough",
100   "style",
101   "text",
102   "underline",
103   "variant",
104   "weight",
105
106   /* Also need the sets */
107   "background-set",
108   "editable-set",
109   "family-set",
110   "foreground-set",
111   "rise-set",
112   "scale-set",
113   "size-set",
114   "stretch-set",
115   "strikethrough-set",
116   "style-set",
117   "underline-set",
118   "variant-set",
119   "weight-set",
120   NULL
121 };
122
123 static void atk_text_interface_init (AtkTextIface *iface);
124
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))
127
128
129 static void
130 gtk_text_cell_accessible_finalize (GObject *object)
131 {
132   GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (object);
133
134   g_free (text_cell->cell_text);
135
136   G_OBJECT_CLASS (_gtk_text_cell_accessible_parent_class)->finalize (object);
137 }
138
139 static const gchar *
140 gtk_text_cell_accessible_get_name (AtkObject *atk_obj)
141 {
142   GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (atk_obj);
143
144   if (atk_obj->name)
145     return atk_obj->name;
146
147   return text_cell->cell_text;
148 }
149
150 static gboolean
151 gtk_text_cell_accessible_update_cache (GtkRendererCellAccessible *cell,
152                                        gboolean                   emit_change_signal)
153 {
154   GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (cell);
155   AtkObject *obj = ATK_OBJECT (cell);
156   gboolean rv = FALSE;
157   gint temp_length;
158   gchar *text;
159
160   g_object_get (G_OBJECT (cell->renderer), "text", &text, NULL);
161
162   if (text_cell->cell_text)
163     {
164       if (text == NULL || strcmp (text_cell->cell_text, text) != 0)
165         {
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)
171             {
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");
175             }
176           if (text)
177             rv = TRUE;
178         }
179     }
180   else
181     rv = TRUE;
182
183   if (rv)
184     {
185       if (text == NULL)
186         {
187           text_cell->cell_text = g_strdup ("");
188           text_cell->cell_length = 0;
189         }
190       else
191         {
192           text_cell->cell_text = g_strdup (text);
193           text_cell->cell_length = g_utf8_strlen (text, -1);
194         }
195     }
196
197   g_free (text);
198
199   if (rv)
200     {
201       if (emit_change_signal)
202         {
203           g_signal_emit_by_name (cell, "text_changed::insert",
204                                  0, text_cell->cell_length);
205
206           if (obj->name == NULL)
207             g_object_notify (G_OBJECT (obj), "accessible-name");
208         }
209     }
210   return rv;
211 }
212
213 static void
214 _gtk_text_cell_accessible_class_init (GtkTextCellAccessibleClass *klass)
215 {
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);
219
220   renderer_cell_class->update_cache = gtk_text_cell_accessible_update_cache;
221   renderer_cell_class->property_list = property_list;
222
223   atk_object_class->get_name = gtk_text_cell_accessible_get_name;
224
225   gobject_class->finalize = gtk_text_cell_accessible_finalize;
226 }
227
228 static void
229 _gtk_text_cell_accessible_init (GtkTextCellAccessible *text_cell)
230 {
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);
236 }
237
238 AtkObject *
239 _gtk_text_cell_accessible_new (void)
240 {
241   GObject *object;
242   AtkObject *atk_object;
243   GtkRendererCellAccessible *cell;
244
245   object = g_object_new (GTK_TYPE_TEXT_CELL_ACCESSIBLE, NULL);
246
247   g_return_val_if_fail (object != NULL, NULL);
248
249   atk_object = ATK_OBJECT (object);
250   atk_object->role = ATK_ROLE_TABLE_CELL;
251
252   cell = GTK_RENDERER_CELL_ACCESSIBLE(object);
253
254   cell->renderer = gtk_cell_renderer_text_new ();
255   g_object_ref_sink (cell->renderer);
256
257   return atk_object;
258 }
259
260 static gchar *
261 gtk_text_cell_accessible_get_text (AtkText *atk_text,
262                                    gint     start_pos,
263                                    gint     end_pos)
264 {
265   gchar *text;
266
267   text = GTK_TEXT_CELL_ACCESSIBLE (atk_text)->cell_text;
268   if (text)
269     return g_utf8_substring (text, start_pos, end_pos > -1 ? end_pos : g_utf8_strlen (text, -1));
270   else
271     return g_strdup ("");
272 }
273
274 static gchar *
275 gtk_text_cell_accessible_get_text_before_offset (AtkText         *atk_text,
276                                                  gint             offset,
277                                                  AtkTextBoundary  boundary_type,
278                                                  gint            *start_offset,
279                                                  gint            *end_offset)
280 {
281   PangoLayout *layout;
282   gchar *text;
283
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);
287
288   return text;
289 }
290
291 static gchar *
292 gtk_text_cell_accessible_get_text_at_offset (AtkText         *atk_text,
293                                              gint             offset,
294                                              AtkTextBoundary  boundary_type,
295                                              gint            *start_offset,
296                                              gint            *end_offset)
297 {
298   PangoLayout *layout;
299   gchar *text;
300
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);
304
305   return text;
306 }
307
308 static gchar *
309 gtk_text_cell_accessible_get_text_after_offset (AtkText         *atk_text,
310                                                 gint             offset,
311                                                 AtkTextBoundary  boundary_type,
312                                                 gint            *start_offset,
313                                                 gint            *end_offset)
314 {
315   PangoLayout *layout;
316   gchar *text;
317
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);
321
322   return text;
323 }
324
325 static gint
326 gtk_text_cell_accessible_get_character_count (AtkText *text)
327 {
328   if (GTK_TEXT_CELL_ACCESSIBLE (text)->cell_text != NULL)
329     return GTK_TEXT_CELL_ACCESSIBLE (text)->cell_length;
330   else
331     return 0;
332 }
333
334 static gint
335 gtk_text_cell_accessible_get_caret_offset (AtkText *text)
336 {
337   return GTK_TEXT_CELL_ACCESSIBLE (text)->caret_pos;
338 }
339
340 static gboolean
341 gtk_text_cell_accessible_set_caret_offset (AtkText *text,
342                                            gint     offset)
343 {
344   GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (text);
345
346   if (text_cell->cell_text == NULL)
347     return FALSE;
348   else
349     {
350       /* Only set the caret within the bounds and if it is to a new position. */
351       if (offset >= 0 &&
352           offset <= text_cell->cell_length &&
353           offset != text_cell->caret_pos)
354         {
355           text_cell->caret_pos = offset;
356
357           /* emit the signal */
358           g_signal_emit_by_name (text, "text_caret_moved", offset);
359           return TRUE;
360         }
361       else
362         return FALSE;
363     }
364 }
365
366 static AtkAttributeSet *
367 gtk_text_cell_accessible_get_run_attributes (AtkText *text,
368                                              gint     offset,
369                                              gint    *start_offset,
370                                              gint    *end_offset)
371 {
372   AtkAttributeSet *attrib_set = NULL;
373   PangoLayout *layout;
374
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));
378
379   return attrib_set;
380 }
381
382 static AtkAttributeSet *
383 add_attribute (AtkAttributeSet  *attributes,
384                AtkTextAttribute  attr,
385                const gchar      *value)
386 {
387   AtkAttribute *at;
388
389   at = g_new (AtkAttribute, 1);
390   at->name = g_strdup (atk_text_attribute_get_name (attr));
391   at->value = g_strdup (value);
392
393   return g_slist_prepend (attributes, at);
394 }
395
396 static AtkAttributeSet *
397 gtk_text_cell_accessible_get_default_attributes (AtkText *text)
398 {
399   AtkAttributeSet *attrib_set = NULL;
400   PangoLayout *layout;
401   GtkWidget *widget;
402
403   layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
404   widget = get_widget (GTK_TEXT_CELL_ACCESSIBLE (text));
405
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);
410
411   attrib_set = _gtk_style_context_get_attributes (attrib_set,
412                                                   gtk_widget_get_style_context (widget),
413                                                   gtk_widget_get_state_flags (widget));
414
415   g_object_unref (G_OBJECT (layout));
416
417   return attrib_set;
418 }
419
420 GtkWidget *
421 get_widget (GtkTextCellAccessible *text)
422 {
423   AtkObject *parent;
424
425   parent = atk_object_get_parent (ATK_OBJECT (text));
426   if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
427     parent = atk_object_get_parent (parent);
428
429   return gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
430 }
431
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.
435  */
436 static PangoLayout *
437 create_pango_layout (GtkTextCellAccessible *text)
438 {
439   GdkRGBA *foreground_rgba;
440   PangoAttrList *attr_list, *attributes;
441   PangoLayout *layout;
442   PangoUnderline uline, underline;
443   PangoFontMask mask;
444   PangoFontDescription *font_desc;
445   gboolean foreground_set, strikethrough_set, strikethrough;
446   gboolean scale_set, underline_set, rise_set;
447   gchar *renderer_text;
448   gdouble scale;
449   gint rise;
450   GtkRendererCellAccessible *gail_renderer;
451   GtkCellRendererText *gtk_renderer;
452
453   gail_renderer = GTK_RENDERER_CELL_ACCESSIBLE (text);
454   gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
455
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,
465                 "scale", &scale,
466                 "underline-set", &underline_set,
467                 "underline", &underline,
468                 "rise-set", &rise_set,
469                 "rise", &rise,
470                 NULL);
471
472   layout = gtk_widget_create_pango_layout (get_widget (text), renderer_text);
473
474   if (attributes)
475     attr_list = pango_attr_list_copy (attributes);
476   else
477     attr_list = pango_attr_list_new ();
478
479   if (foreground_set)
480     {
481       add_attr (attr_list, pango_attr_foreground_new (foreground_rgba->red * 65535,
482                                                       foreground_rgba->green * 65535,
483                                                       foreground_rgba->blue * 65535));
484     }
485
486   if (strikethrough_set)
487     add_attr (attr_list,
488               pango_attr_strikethrough_new (strikethrough));
489
490   mask = pango_font_description_get_set_fields (font_desc);
491
492   if (mask & PANGO_FONT_MASK_FAMILY)
493     add_attr (attr_list,
494       pango_attr_family_new (pango_font_description_get_family (font_desc)));
495
496   if (mask & PANGO_FONT_MASK_STYLE)
497     add_attr (attr_list, pango_attr_style_new (pango_font_description_get_style (font_desc)));
498
499   if (mask & PANGO_FONT_MASK_VARIANT)
500     add_attr (attr_list, pango_attr_variant_new (pango_font_description_get_variant (font_desc)));
501
502   if (mask & PANGO_FONT_MASK_WEIGHT)
503     add_attr (attr_list, pango_attr_weight_new (pango_font_description_get_weight (font_desc)));
504
505   if (mask & PANGO_FONT_MASK_STRETCH)
506     add_attr (attr_list, pango_attr_stretch_new (pango_font_description_get_stretch (font_desc)));
507
508   if (mask & PANGO_FONT_MASK_SIZE)
509     add_attr (attr_list, pango_attr_size_new (pango_font_description_get_size (font_desc)));
510
511   if (scale_set && scale != 1.0)
512     add_attr (attr_list, pango_attr_scale_new (scale));
513
514   if (underline_set)
515     uline = underline;
516   else
517     uline = PANGO_UNDERLINE_NONE;
518
519   if (uline != PANGO_UNDERLINE_NONE)
520     add_attr (attr_list,
521               pango_attr_underline_new (underline));
522
523   if (rise_set)
524     add_attr (attr_list, pango_attr_rise_new (rise));
525
526   pango_layout_set_attributes (layout, attr_list);
527   pango_layout_set_width (layout, -1);
528   pango_attr_list_unref (attr_list);
529
530   pango_font_description_free (font_desc);
531   pango_attr_list_unref (attributes);
532   g_free (renderer_text);
533   gdk_rgba_free (foreground_rgba);
534
535   return layout;
536 }
537
538 static void
539 add_attr (PangoAttrList *attr_list,
540          PangoAttribute *attr)
541 {
542   attr->start_index = 0;
543   attr->end_index = G_MAXINT;
544   pango_attr_list_insert (attr_list, attr);
545 }
546
547
548 static void
549 get_origins (GtkWidget *widget,
550              gint      *x_window,
551              gint      *y_window,
552              gint      *x_toplevel,
553              gint      *y_toplevel)
554 {
555   GdkWindow *window;
556
557   if (GTK_IS_TREE_VIEW (widget))
558     window = gtk_tree_view_get_bin_window (GTK_TREE_VIEW (widget));
559   else
560     window = gtk_widget_get_window (widget);
561
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);
565 }
566
567 static void
568 gtk_text_cell_accessible_get_character_extents (AtkText      *text,
569                                                 gint          offset,
570                                                 gint         *x,
571                                                 gint         *y,
572                                                 gint         *width,
573                                                 gint         *height,
574                                                 AtkCoordType  coords)
575 {
576   GtkRendererCellAccessible *gail_renderer;
577   GtkRequisition min_size;
578   GtkCellRendererText *gtk_renderer;
579   GdkRectangle rendered_rect;
580   GtkWidget *widget;
581   AtkObject *parent;
582   PangoRectangle char_rect;
583   PangoLayout *layout;
584   gchar *renderer_text;
585   gfloat xalign, yalign;
586   gint x_offset, y_offset, index;
587   gint xpad, ypad;
588   gint x_window, y_window, x_toplevel, y_toplevel;
589
590   if (!GTK_TEXT_CELL_ACCESSIBLE (text)->cell_text)
591     {
592       *x = *y = *height = *width = 0;
593       return;
594     }
595   if (offset < 0 || offset >= GTK_TEXT_CELL_ACCESSIBLE (text)->cell_length)
596     {
597       *x = *y = *height = *width = 0;
598       return;
599     }
600   gail_renderer = GTK_RENDERER_CELL_ACCESSIBLE (text);
601   gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
602
603   g_object_get (gtk_renderer, "text", &renderer_text, NULL);
604   if (text == NULL)
605     {
606       g_free (renderer_text);
607       return;
608     }
609
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),
616                                   &rendered_rect);
617
618   gtk_cell_renderer_get_preferred_size (GTK_CELL_RENDERER (gtk_renderer),
619                                         widget,
620                                         &min_size, NULL);
621
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));
627
628   layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
629
630   index = g_utf8_offset_to_pointer (renderer_text, offset) - renderer_text;
631   pango_layout_index_to_pos (layout, index, &char_rect);
632
633   gtk_cell_renderer_get_padding (gail_renderer->renderer, &xpad, &ypad);
634
635   get_origins (widget, &x_window, &y_window, &x_toplevel, &y_toplevel);
636
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;
641
642   if (coords == ATK_XY_WINDOW)
643     {
644       *x -= x_toplevel;
645       *y -= y_toplevel;
646     }
647   else if (coords != ATK_XY_SCREEN)
648     {
649       *x = 0;
650       *y = 0;
651       *height = 0;
652       *width = 0;
653     }
654
655   g_free (renderer_text);
656   g_object_unref (layout);
657 }
658
659 static gint
660 gtk_text_cell_accessible_get_offset_at_point (AtkText      *text,
661                                               gint          x,
662                                               gint          y,
663                                               AtkCoordType  coords)
664 {
665   AtkObject *parent;
666   GtkRendererCellAccessible *gail_renderer;
667   GtkCellRendererText *gtk_renderer;
668   GtkRequisition min_size;
669   GtkWidget *widget;
670   GdkRectangle rendered_rect;
671   PangoLayout *layout;
672   gchar *renderer_text;
673   gfloat xalign, yalign;
674   gint x_offset, y_offset, index;
675   gint xpad, ypad;
676   gint x_window, y_window, x_toplevel, y_toplevel;
677   gint x_temp, y_temp;
678   gboolean ret;
679
680   if (!GTK_TEXT_CELL_ACCESSIBLE (text)->cell_text)
681     return -1;
682
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));
686
687   g_object_get (gtk_renderer, "text", &renderer_text, NULL);
688   if (text == NULL)
689     {
690       g_free (renderer_text);
691       return -1;
692     }
693
694   if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
695     parent = atk_object_get_parent (parent);
696
697   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
698
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),
701                                   &rendered_rect);
702
703   gtk_cell_renderer_get_preferred_size (GTK_CELL_RENDERER (gtk_renderer),
704                                         widget,
705                                         &min_size, NULL);
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));
711
712   layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
713
714   gtk_cell_renderer_get_padding (gail_renderer->renderer, &xpad, &ypad);
715
716   get_origins (widget, &x_window, &y_window, &x_toplevel, &y_toplevel);
717
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)
721     {
722       x_temp += x_toplevel;
723       y_temp += y_toplevel;
724     }
725   else if (coords != ATK_XY_SCREEN)
726     index = -1;
727
728   ret = pango_layout_xy_to_index (layout,
729                                   x_temp * PANGO_SCALE,
730                                   y_temp * PANGO_SCALE,
731                                   &index, NULL);
732   if (!ret)
733     {
734       if (x_temp < 0 || y_temp < 0)
735         index = 0;
736       else
737         index = -1;
738     }
739
740   g_object_unref (layout);
741   if (index == -1)
742     {
743       if (coords == ATK_XY_WINDOW || coords == ATK_XY_SCREEN)
744         {
745           glong length;
746
747           length = g_utf8_strlen (renderer_text, -1);
748           g_free (renderer_text);
749
750           return length;
751         }
752
753       g_free (renderer_text);
754
755       return index;
756     }
757   else
758     {
759       glong offset;
760
761       offset = g_utf8_pointer_to_offset (renderer_text,
762                                          renderer_text + index);
763       g_free (renderer_text);
764
765       return offset;
766     }
767 }
768
769 static gunichar
770 gtk_text_cell_accessible_get_character_at_offset (AtkText *text,
771                                                   gint     offset)
772 {
773   gchar *index;
774   gchar *string;
775
776   string = GTK_TEXT_CELL_ACCESSIBLE(text)->cell_text;
777
778   if (!string)
779     return '\0';
780
781   if (offset >= g_utf8_strlen (string, -1))
782     return '\0';
783
784   index = g_utf8_offset_to_pointer (string, offset);
785
786   return g_utf8_get_char (index);
787 }
788
789 static void
790 atk_text_interface_init (AtkTextIface *iface)
791 {
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;
804 }