]> Pileus Git - ~andy/gtk/blob - gtk/a11y/gtktextcellaccessible.c
a11y: Move update_cache to GtkCellAccesible
[~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
22 #include <gtk/gtk.h>
23 #include "../gtkpango.h"
24 #include "gtktextcellaccessible.h"
25 #include "gtkcontainercellaccessible.h"
26 #include "gtkcellaccessibleparent.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 void gtk_text_cell_accessible_update_cache       (GtkCellAccessible *cell);
84
85 static void atk_text_interface_init (AtkTextIface *iface);
86
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))
89
90 static AtkStateSet *
91 gtk_text_cell_accessible_ref_state_set (AtkObject *accessible)
92 {
93   AtkStateSet *state_set;
94
95   state_set = ATK_OBJECT_CLASS (_gtk_text_cell_accessible_parent_class)->ref_state_set (accessible);
96
97   atk_state_set_add_state (state_set, ATK_STATE_SINGLE_LINE);
98
99   return state_set;
100 }
101
102 static void
103 gtk_text_cell_accessible_finalize (GObject *object)
104 {
105   GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (object);
106
107   g_free (text_cell->cell_text);
108
109   G_OBJECT_CLASS (_gtk_text_cell_accessible_parent_class)->finalize (object);
110 }
111
112 static const gchar *
113 gtk_text_cell_accessible_get_name (AtkObject *atk_obj)
114 {
115   GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (atk_obj);
116
117   if (atk_obj->name)
118     return atk_obj->name;
119
120   return text_cell->cell_text;
121 }
122
123 static void
124 gtk_text_cell_accessible_update_cache (GtkCellAccessible *cell)
125 {
126   GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (cell);
127   AtkObject *obj = ATK_OBJECT (cell);
128   gboolean rv = FALSE;
129   gint temp_length;
130   gchar *text;
131
132   g_object_get (G_OBJECT (GTK_RENDERER_CELL_ACCESSIBLE (cell)->renderer),
133                 "text", &text,
134                 NULL);
135
136   if (text_cell->cell_text)
137     {
138       if (text == NULL || g_strcmp0 (text_cell->cell_text, text) != 0)
139         {
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");
147           if (text)
148             rv = TRUE;
149         }
150     }
151   else
152     rv = TRUE;
153
154   if (rv)
155     {
156       if (text == NULL)
157         {
158           text_cell->cell_text = g_strdup ("");
159           text_cell->cell_length = 0;
160         }
161       else
162         {
163           text_cell->cell_text = g_strdup (text);
164           text_cell->cell_length = g_utf8_strlen (text, -1);
165         }
166     }
167
168   g_free (text);
169
170   if (rv)
171     {
172       g_signal_emit_by_name (cell, "text-changed::insert",
173                              0, text_cell->cell_length);
174
175       if (obj->name == NULL)
176         g_object_notify (G_OBJECT (obj), "accessible-name");
177     }
178 }
179
180 static void
181 _gtk_text_cell_accessible_class_init (GtkTextCellAccessibleClass *klass)
182 {
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);
186
187   cell_class->update_cache = gtk_text_cell_accessible_update_cache;
188
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;
191
192   gobject_class->finalize = gtk_text_cell_accessible_finalize;
193 }
194
195 static void
196 _gtk_text_cell_accessible_init (GtkTextCellAccessible *text_cell)
197 {
198   text_cell->cell_text = NULL;
199   text_cell->caret_pos = 0;
200   text_cell->cell_length = 0;
201 }
202
203 static gchar *
204 gtk_text_cell_accessible_get_text (AtkText *atk_text,
205                                    gint     start_pos,
206                                    gint     end_pos)
207 {
208   gchar *text;
209
210   text = GTK_TEXT_CELL_ACCESSIBLE (atk_text)->cell_text;
211   if (text)
212     return g_utf8_substring (text, start_pos, end_pos > -1 ? end_pos : g_utf8_strlen (text, -1));
213   else
214     return g_strdup ("");
215 }
216
217 static gchar *
218 gtk_text_cell_accessible_get_text_before_offset (AtkText         *atk_text,
219                                                  gint             offset,
220                                                  AtkTextBoundary  boundary_type,
221                                                  gint            *start_offset,
222                                                  gint            *end_offset)
223 {
224   PangoLayout *layout;
225   gchar *text;
226
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);
230
231   return text;
232 }
233
234 static gchar *
235 gtk_text_cell_accessible_get_text_at_offset (AtkText         *atk_text,
236                                              gint             offset,
237                                              AtkTextBoundary  boundary_type,
238                                              gint            *start_offset,
239                                              gint            *end_offset)
240 {
241   PangoLayout *layout;
242   gchar *text;
243
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);
247
248   return text;
249 }
250
251 static gchar *
252 gtk_text_cell_accessible_get_text_after_offset (AtkText         *atk_text,
253                                                 gint             offset,
254                                                 AtkTextBoundary  boundary_type,
255                                                 gint            *start_offset,
256                                                 gint            *end_offset)
257 {
258   PangoLayout *layout;
259   gchar *text;
260
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);
264
265   return text;
266 }
267
268 static gint
269 gtk_text_cell_accessible_get_character_count (AtkText *text)
270 {
271   if (GTK_TEXT_CELL_ACCESSIBLE (text)->cell_text != NULL)
272     return GTK_TEXT_CELL_ACCESSIBLE (text)->cell_length;
273   else
274     return 0;
275 }
276
277 static gint
278 gtk_text_cell_accessible_get_caret_offset (AtkText *text)
279 {
280   return GTK_TEXT_CELL_ACCESSIBLE (text)->caret_pos;
281 }
282
283 static gboolean
284 gtk_text_cell_accessible_set_caret_offset (AtkText *text,
285                                            gint     offset)
286 {
287   GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (text);
288
289   if (text_cell->cell_text == NULL)
290     return FALSE;
291   else
292     {
293       /* Only set the caret within the bounds and if it is to a new position. */
294       if (offset >= 0 &&
295           offset <= text_cell->cell_length &&
296           offset != text_cell->caret_pos)
297         {
298           text_cell->caret_pos = offset;
299
300           /* emit the signal */
301           g_signal_emit_by_name (text, "text-caret-moved", offset);
302           return TRUE;
303         }
304       else
305         return FALSE;
306     }
307 }
308
309 static AtkAttributeSet *
310 gtk_text_cell_accessible_get_run_attributes (AtkText *text,
311                                              gint     offset,
312                                              gint    *start_offset,
313                                              gint    *end_offset)
314 {
315   AtkAttributeSet *attrib_set = NULL;
316   PangoLayout *layout;
317
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));
321
322   return attrib_set;
323 }
324
325 static AtkAttributeSet *
326 add_attribute (AtkAttributeSet  *attributes,
327                AtkTextAttribute  attr,
328                const gchar      *value)
329 {
330   AtkAttribute *at;
331
332   at = g_new (AtkAttribute, 1);
333   at->name = g_strdup (atk_text_attribute_get_name (attr));
334   at->value = g_strdup (value);
335
336   return g_slist_prepend (attributes, at);
337 }
338
339 static AtkAttributeSet *
340 gtk_text_cell_accessible_get_default_attributes (AtkText *text)
341 {
342   AtkAttributeSet *attrib_set = NULL;
343   PangoLayout *layout;
344   GtkWidget *widget;
345
346   layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
347   widget = get_widget (GTK_TEXT_CELL_ACCESSIBLE (text));
348
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);
353
354   attrib_set = _gtk_style_context_get_attributes (attrib_set,
355                                                   gtk_widget_get_style_context (widget),
356                                                   gtk_widget_get_state_flags (widget));
357
358   g_object_unref (G_OBJECT (layout));
359
360   return attrib_set;
361 }
362
363 GtkWidget *
364 get_widget (GtkTextCellAccessible *text)
365 {
366   AtkObject *parent;
367
368   parent = atk_object_get_parent (ATK_OBJECT (text));
369   if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
370     parent = atk_object_get_parent (parent);
371
372   return gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
373 }
374
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.
378  */
379 static PangoLayout *
380 create_pango_layout (GtkTextCellAccessible *text)
381 {
382   GdkRGBA *foreground_rgba;
383   PangoAttrList *attr_list, *attributes;
384   PangoLayout *layout;
385   PangoUnderline uline, underline;
386   PangoFontMask mask;
387   PangoFontDescription *font_desc;
388   gboolean foreground_set, strikethrough_set, strikethrough;
389   gboolean scale_set, underline_set, rise_set;
390   gchar *renderer_text;
391   gdouble scale;
392   gint rise;
393   GtkRendererCellAccessible *gail_renderer;
394   GtkCellRendererText *gtk_renderer;
395
396   gail_renderer = GTK_RENDERER_CELL_ACCESSIBLE (text);
397   gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
398
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,
408                 "scale", &scale,
409                 "underline-set", &underline_set,
410                 "underline", &underline,
411                 "rise-set", &rise_set,
412                 "rise", &rise,
413                 NULL);
414
415   layout = gtk_widget_create_pango_layout (get_widget (text), renderer_text);
416
417   if (attributes)
418     attr_list = pango_attr_list_copy (attributes);
419   else
420     attr_list = pango_attr_list_new ();
421
422   if (foreground_set)
423     {
424       add_attr (attr_list, pango_attr_foreground_new (foreground_rgba->red * 65535,
425                                                       foreground_rgba->green * 65535,
426                                                       foreground_rgba->blue * 65535));
427     }
428
429   if (strikethrough_set)
430     add_attr (attr_list,
431               pango_attr_strikethrough_new (strikethrough));
432
433   mask = pango_font_description_get_set_fields (font_desc);
434
435   if (mask & PANGO_FONT_MASK_FAMILY)
436     add_attr (attr_list,
437       pango_attr_family_new (pango_font_description_get_family (font_desc)));
438
439   if (mask & PANGO_FONT_MASK_STYLE)
440     add_attr (attr_list, pango_attr_style_new (pango_font_description_get_style (font_desc)));
441
442   if (mask & PANGO_FONT_MASK_VARIANT)
443     add_attr (attr_list, pango_attr_variant_new (pango_font_description_get_variant (font_desc)));
444
445   if (mask & PANGO_FONT_MASK_WEIGHT)
446     add_attr (attr_list, pango_attr_weight_new (pango_font_description_get_weight (font_desc)));
447
448   if (mask & PANGO_FONT_MASK_STRETCH)
449     add_attr (attr_list, pango_attr_stretch_new (pango_font_description_get_stretch (font_desc)));
450
451   if (mask & PANGO_FONT_MASK_SIZE)
452     add_attr (attr_list, pango_attr_size_new (pango_font_description_get_size (font_desc)));
453
454   if (scale_set && scale != 1.0)
455     add_attr (attr_list, pango_attr_scale_new (scale));
456
457   if (underline_set)
458     uline = underline;
459   else
460     uline = PANGO_UNDERLINE_NONE;
461
462   if (uline != PANGO_UNDERLINE_NONE)
463     add_attr (attr_list,
464               pango_attr_underline_new (underline));
465
466   if (rise_set)
467     add_attr (attr_list, pango_attr_rise_new (rise));
468
469   pango_layout_set_attributes (layout, attr_list);
470   pango_layout_set_width (layout, -1);
471   pango_attr_list_unref (attr_list);
472
473   pango_font_description_free (font_desc);
474   pango_attr_list_unref (attributes);
475   g_free (renderer_text);
476   gdk_rgba_free (foreground_rgba);
477
478   return layout;
479 }
480
481 static void
482 add_attr (PangoAttrList *attr_list,
483          PangoAttribute *attr)
484 {
485   attr->start_index = 0;
486   attr->end_index = G_MAXINT;
487   pango_attr_list_insert (attr_list, attr);
488 }
489
490
491 static void
492 get_origins (GtkWidget *widget,
493              gint      *x_window,
494              gint      *y_window,
495              gint      *x_toplevel,
496              gint      *y_toplevel)
497 {
498   GdkWindow *window;
499
500   if (GTK_IS_TREE_VIEW (widget))
501     window = gtk_tree_view_get_bin_window (GTK_TREE_VIEW (widget));
502   else
503     window = gtk_widget_get_window (widget);
504
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);
508 }
509
510 static void
511 gtk_text_cell_accessible_get_character_extents (AtkText      *text,
512                                                 gint          offset,
513                                                 gint         *x,
514                                                 gint         *y,
515                                                 gint         *width,
516                                                 gint         *height,
517                                                 AtkCoordType  coords)
518 {
519   GtkRendererCellAccessible *gail_renderer;
520   GtkRequisition min_size;
521   GtkCellRendererText *gtk_renderer;
522   GdkRectangle rendered_rect;
523   GtkWidget *widget;
524   AtkObject *parent;
525   PangoRectangle char_rect;
526   PangoLayout *layout;
527   gchar *renderer_text;
528   gfloat xalign, yalign;
529   gint x_offset, y_offset, index;
530   gint xpad, ypad;
531   gint x_window, y_window, x_toplevel, y_toplevel;
532
533   if (!GTK_TEXT_CELL_ACCESSIBLE (text)->cell_text)
534     {
535       *x = *y = *height = *width = 0;
536       return;
537     }
538   if (offset < 0 || offset >= GTK_TEXT_CELL_ACCESSIBLE (text)->cell_length)
539     {
540       *x = *y = *height = *width = 0;
541       return;
542     }
543   gail_renderer = GTK_RENDERER_CELL_ACCESSIBLE (text);
544   gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
545
546   g_object_get (gtk_renderer, "text", &renderer_text, NULL);
547   if (text == NULL)
548     {
549       g_free (renderer_text);
550       return;
551     }
552
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),
560                                              &rendered_rect);
561
562   gtk_cell_renderer_get_preferred_size (GTK_CELL_RENDERER (gtk_renderer),
563                                         widget,
564                                         &min_size, NULL);
565
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));
571
572   layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
573
574   index = g_utf8_offset_to_pointer (renderer_text, offset) - renderer_text;
575   pango_layout_index_to_pos (layout, index, &char_rect);
576
577   gtk_cell_renderer_get_padding (gail_renderer->renderer, &xpad, &ypad);
578
579   get_origins (widget, &x_window, &y_window, &x_toplevel, &y_toplevel);
580
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;
585
586   if (coords == ATK_XY_WINDOW)
587     {
588       *x -= x_toplevel;
589       *y -= y_toplevel;
590     }
591   else if (coords != ATK_XY_SCREEN)
592     {
593       *x = 0;
594       *y = 0;
595       *height = 0;
596       *width = 0;
597     }
598
599   g_free (renderer_text);
600   g_object_unref (layout);
601 }
602
603 static gint
604 gtk_text_cell_accessible_get_offset_at_point (AtkText      *text,
605                                               gint          x,
606                                               gint          y,
607                                               AtkCoordType  coords)
608 {
609   AtkObject *parent;
610   GtkRendererCellAccessible *gail_renderer;
611   GtkCellRendererText *gtk_renderer;
612   GtkRequisition min_size;
613   GtkWidget *widget;
614   GdkRectangle rendered_rect;
615   PangoLayout *layout;
616   gchar *renderer_text;
617   gfloat xalign, yalign;
618   gint x_offset, y_offset, index;
619   gint xpad, ypad;
620   gint x_window, y_window, x_toplevel, y_toplevel;
621   gint x_temp, y_temp;
622   gboolean ret;
623
624   if (!GTK_TEXT_CELL_ACCESSIBLE (text)->cell_text)
625     return -1;
626
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));
630
631   g_object_get (gtk_renderer, "text", &renderer_text, NULL);
632   if (text == NULL)
633     {
634       g_free (renderer_text);
635       return -1;
636     }
637
638   if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
639     parent = atk_object_get_parent (parent);
640
641   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
642
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),
646                                              &rendered_rect);
647
648   gtk_cell_renderer_get_preferred_size (GTK_CELL_RENDERER (gtk_renderer),
649                                         widget,
650                                         &min_size, NULL);
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));
656
657   layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
658
659   gtk_cell_renderer_get_padding (gail_renderer->renderer, &xpad, &ypad);
660
661   get_origins (widget, &x_window, &y_window, &x_toplevel, &y_toplevel);
662
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)
666     {
667       x_temp += x_toplevel;
668       y_temp += y_toplevel;
669     }
670   else if (coords != ATK_XY_SCREEN)
671     index = -1;
672
673   ret = pango_layout_xy_to_index (layout,
674                                   x_temp * PANGO_SCALE,
675                                   y_temp * PANGO_SCALE,
676                                   &index, NULL);
677   if (!ret)
678     {
679       if (x_temp < 0 || y_temp < 0)
680         index = 0;
681       else
682         index = -1;
683     }
684
685   g_object_unref (layout);
686   if (index == -1)
687     {
688       if (coords == ATK_XY_WINDOW || coords == ATK_XY_SCREEN)
689         {
690           glong length;
691
692           length = g_utf8_strlen (renderer_text, -1);
693           g_free (renderer_text);
694
695           return length;
696         }
697
698       g_free (renderer_text);
699
700       return index;
701     }
702   else
703     {
704       glong offset;
705
706       offset = g_utf8_pointer_to_offset (renderer_text,
707                                          renderer_text + index);
708       g_free (renderer_text);
709
710       return offset;
711     }
712 }
713
714 static gunichar
715 gtk_text_cell_accessible_get_character_at_offset (AtkText *text,
716                                                   gint     offset)
717 {
718   gchar *index;
719   gchar *string;
720
721   string = GTK_TEXT_CELL_ACCESSIBLE(text)->cell_text;
722
723   if (!string)
724     return '\0';
725
726   if (offset >= g_utf8_strlen (string, -1))
727     return '\0';
728
729   index = g_utf8_offset_to_pointer (string, offset);
730
731   return g_utf8_get_char (index);
732 }
733
734 static void
735 atk_text_interface_init (AtkTextIface *iface)
736 {
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;
749 }