]> Pileus Git - ~andy/gtk/blob - gtk/a11y/gtktextcellaccessible.c
GtkRendererCellAccessible: add a private struct
[~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, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "config.h"
19
20 #include <gtk/gtk.h>
21 #include "../gtkpango.h"
22 #include "gtktextcellaccessible.h"
23 #include "gtkcontainercellaccessible.h"
24 #include "gtkcellaccessibleparent.h"
25
26 static const gchar* gtk_text_cell_accessible_get_name    (AtkObject      *atk_obj);
27
28
29 /* atktext.h */
30
31 static gchar*    gtk_text_cell_accessible_get_text                (AtkText        *text,
32                                                         gint            start_pos,
33                                                         gint            end_pos);
34 static gunichar gtk_text_cell_accessible_get_character_at_offset  (AtkText        *text,
35                                                          gint           offset);
36 static gchar*   gtk_text_cell_accessible_get_text_before_offset   (AtkText        *text,
37                                                          gint           offset,
38                                                          AtkTextBoundary boundary_type,
39                                                          gint           *start_offset,
40                                                          gint           *end_offset);
41 static gchar*   gtk_text_cell_accessible_get_text_at_offset       (AtkText        *text,
42                                                          gint           offset,
43                                                          AtkTextBoundary boundary_type,
44                                                          gint           *start_offset,
45                                                          gint           *end_offset);
46 static gchar*   gtk_text_cell_accessible_get_text_after_offset    (AtkText        *text,
47                                                          gint           offset,
48                                                          AtkTextBoundary boundary_type,
49                                                          gint           *start_offset,
50                                                          gint           *end_offset);
51 static gint      gtk_text_cell_accessible_get_character_count     (AtkText        *text);
52 static gint      gtk_text_cell_accessible_get_caret_offset        (AtkText        *text);
53 static gboolean  gtk_text_cell_accessible_set_caret_offset        (AtkText        *text,
54                                                          gint           offset);
55 static void      gtk_text_cell_accessible_get_character_extents   (AtkText        *text,
56                                                          gint           offset,
57                                                          gint           *x,
58                                                          gint           *y,
59                                                          gint           *width,
60                                                          gint           *height,
61                                                          AtkCoordType   coords);
62 static gint      gtk_text_cell_accessible_get_offset_at_point     (AtkText        *text,
63                                                          gint           x,
64                                                          gint           y,
65                                                          AtkCoordType   coords);
66 static AtkAttributeSet* gtk_text_cell_accessible_get_run_attributes 
67                                                         (AtkText        *text,
68                                                          gint           offset,
69                                                          gint           *start_offset,      
70                                                          gint           *end_offset); 
71 static AtkAttributeSet* gtk_text_cell_accessible_get_default_attributes 
72                                                         (AtkText        *text);
73
74 static GtkWidget*       get_widget                      (GtkTextCellAccessible *cell);
75 static PangoLayout*     create_pango_layout             (GtkTextCellAccessible *cell);
76 static void             add_attr                        (PangoAttrList  *attr_list,
77                                                          PangoAttribute *attr);
78
79 /* Misc */
80
81 static void gtk_text_cell_accessible_update_cache       (GtkCellAccessible *cell);
82
83 static void atk_text_interface_init (AtkTextIface *iface);
84
85 G_DEFINE_TYPE_WITH_CODE (GtkTextCellAccessible, _gtk_text_cell_accessible, GTK_TYPE_RENDERER_CELL_ACCESSIBLE,
86                          G_IMPLEMENT_INTERFACE (ATK_TYPE_TEXT, atk_text_interface_init))
87
88 static AtkStateSet *
89 gtk_text_cell_accessible_ref_state_set (AtkObject *accessible)
90 {
91   AtkStateSet *state_set;
92
93   state_set = ATK_OBJECT_CLASS (_gtk_text_cell_accessible_parent_class)->ref_state_set (accessible);
94
95   atk_state_set_add_state (state_set, ATK_STATE_SINGLE_LINE);
96
97   return state_set;
98 }
99
100 static void
101 gtk_text_cell_accessible_finalize (GObject *object)
102 {
103   GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (object);
104
105   g_free (text_cell->cell_text);
106
107   G_OBJECT_CLASS (_gtk_text_cell_accessible_parent_class)->finalize (object);
108 }
109
110 static const gchar *
111 gtk_text_cell_accessible_get_name (AtkObject *atk_obj)
112 {
113   GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (atk_obj);
114
115   if (atk_obj->name)
116     return atk_obj->name;
117
118   return text_cell->cell_text;
119 }
120
121 static void
122 gtk_text_cell_accessible_update_cache (GtkCellAccessible *cell)
123 {
124   GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (cell);
125   AtkObject *obj = ATK_OBJECT (cell);
126   gboolean rv = FALSE;
127   gint temp_length;
128   gchar *text;
129   GtkCellRenderer *renderer;
130
131   g_object_get (cell, "renderer", &renderer, NULL);
132   g_object_get (renderer,
133                 "text", &text,
134                 NULL);
135   g_object_unref (renderer);
136
137   if (text_cell->cell_text)
138     {
139       if (text == NULL || g_strcmp0 (text_cell->cell_text, text) != 0)
140         {
141           g_free (text_cell->cell_text);
142           temp_length = text_cell->cell_length;
143           text_cell->cell_text = NULL;
144           text_cell->cell_length = 0;
145           g_signal_emit_by_name (cell, "text-changed::delete", 0, temp_length);
146           if (obj->name == NULL)
147             g_object_notify (G_OBJECT (obj), "accessible-name");
148           if (text)
149             rv = TRUE;
150         }
151     }
152   else
153     rv = TRUE;
154
155   if (rv)
156     {
157       if (text == NULL)
158         {
159           text_cell->cell_text = g_strdup ("");
160           text_cell->cell_length = 0;
161         }
162       else
163         {
164           text_cell->cell_text = g_strdup (text);
165           text_cell->cell_length = g_utf8_strlen (text, -1);
166         }
167     }
168
169   g_free (text);
170
171   if (rv)
172     {
173       g_signal_emit_by_name (cell, "text-changed::insert",
174                              0, text_cell->cell_length);
175
176       if (obj->name == NULL)
177         g_object_notify (G_OBJECT (obj), "accessible-name");
178     }
179 }
180
181 static void
182 _gtk_text_cell_accessible_class_init (GtkTextCellAccessibleClass *klass)
183 {
184   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
185   AtkObjectClass *atk_object_class = ATK_OBJECT_CLASS (klass);
186   GtkCellAccessibleClass *cell_class = GTK_CELL_ACCESSIBLE_CLASS (klass);
187
188   cell_class->update_cache = gtk_text_cell_accessible_update_cache;
189
190   atk_object_class->get_name = gtk_text_cell_accessible_get_name;
191   atk_object_class->ref_state_set = gtk_text_cell_accessible_ref_state_set;
192
193   gobject_class->finalize = gtk_text_cell_accessible_finalize;
194 }
195
196 static void
197 _gtk_text_cell_accessible_init (GtkTextCellAccessible *text_cell)
198 {
199   text_cell->cell_text = NULL;
200   text_cell->caret_pos = 0;
201   text_cell->cell_length = 0;
202 }
203
204 static gchar *
205 gtk_text_cell_accessible_get_text (AtkText *atk_text,
206                                    gint     start_pos,
207                                    gint     end_pos)
208 {
209   gchar *text;
210
211   text = GTK_TEXT_CELL_ACCESSIBLE (atk_text)->cell_text;
212   if (text)
213     return g_utf8_substring (text, start_pos, end_pos > -1 ? end_pos : g_utf8_strlen (text, -1));
214   else
215     return g_strdup ("");
216 }
217
218 static gchar *
219 gtk_text_cell_accessible_get_text_before_offset (AtkText         *atk_text,
220                                                  gint             offset,
221                                                  AtkTextBoundary  boundary_type,
222                                                  gint            *start_offset,
223                                                  gint            *end_offset)
224 {
225   PangoLayout *layout;
226   gchar *text;
227
228   layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (atk_text));
229   text = _gtk_pango_get_text_before (layout, boundary_type, offset, start_offset, end_offset);
230   g_object_unref (layout);
231
232   return text;
233 }
234
235 static gchar *
236 gtk_text_cell_accessible_get_text_at_offset (AtkText         *atk_text,
237                                              gint             offset,
238                                              AtkTextBoundary  boundary_type,
239                                              gint            *start_offset,
240                                              gint            *end_offset)
241 {
242   PangoLayout *layout;
243   gchar *text;
244
245   layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (atk_text));
246   text = _gtk_pango_get_text_at (layout, boundary_type, offset, start_offset, end_offset);
247   g_object_unref (layout);
248
249   return text;
250 }
251
252 static gchar *
253 gtk_text_cell_accessible_get_text_after_offset (AtkText         *atk_text,
254                                                 gint             offset,
255                                                 AtkTextBoundary  boundary_type,
256                                                 gint            *start_offset,
257                                                 gint            *end_offset)
258 {
259   PangoLayout *layout;
260   gchar *text;
261
262   layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (atk_text));
263   text = _gtk_pango_get_text_after (layout, boundary_type, offset, start_offset, end_offset);
264   g_object_unref (layout);
265
266   return text;
267 }
268
269 static gint
270 gtk_text_cell_accessible_get_character_count (AtkText *text)
271 {
272   if (GTK_TEXT_CELL_ACCESSIBLE (text)->cell_text != NULL)
273     return GTK_TEXT_CELL_ACCESSIBLE (text)->cell_length;
274   else
275     return 0;
276 }
277
278 static gint
279 gtk_text_cell_accessible_get_caret_offset (AtkText *text)
280 {
281   return GTK_TEXT_CELL_ACCESSIBLE (text)->caret_pos;
282 }
283
284 static gboolean
285 gtk_text_cell_accessible_set_caret_offset (AtkText *text,
286                                            gint     offset)
287 {
288   GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (text);
289
290   if (text_cell->cell_text == NULL)
291     return FALSE;
292   else
293     {
294       /* Only set the caret within the bounds and if it is to a new position. */
295       if (offset >= 0 &&
296           offset <= text_cell->cell_length &&
297           offset != text_cell->caret_pos)
298         {
299           text_cell->caret_pos = offset;
300
301           /* emit the signal */
302           g_signal_emit_by_name (text, "text-caret-moved", offset);
303           return TRUE;
304         }
305       else
306         return FALSE;
307     }
308 }
309
310 static AtkAttributeSet *
311 gtk_text_cell_accessible_get_run_attributes (AtkText *text,
312                                              gint     offset,
313                                              gint    *start_offset,
314                                              gint    *end_offset)
315 {
316   AtkAttributeSet *attrib_set = NULL;
317   PangoLayout *layout;
318
319   layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
320   attrib_set = _gtk_pango_get_run_attributes  (NULL, layout, offset, start_offset, end_offset);
321   g_object_unref (G_OBJECT (layout));
322
323   return attrib_set;
324 }
325
326 static AtkAttributeSet *
327 add_attribute (AtkAttributeSet  *attributes,
328                AtkTextAttribute  attr,
329                const gchar      *value)
330 {
331   AtkAttribute *at;
332
333   at = g_new (AtkAttribute, 1);
334   at->name = g_strdup (atk_text_attribute_get_name (attr));
335   at->value = g_strdup (value);
336
337   return g_slist_prepend (attributes, at);
338 }
339
340 static AtkAttributeSet *
341 gtk_text_cell_accessible_get_default_attributes (AtkText *text)
342 {
343   AtkAttributeSet *attrib_set = NULL;
344   PangoLayout *layout;
345   GtkWidget *widget;
346
347   layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
348   widget = get_widget (GTK_TEXT_CELL_ACCESSIBLE (text));
349
350   attrib_set = add_attribute (attrib_set, ATK_TEXT_ATTR_DIRECTION,
351                    atk_text_attribute_get_value (ATK_TEXT_ATTR_DIRECTION,
352                                                  gtk_widget_get_direction (widget)));
353   attrib_set = _gtk_pango_get_default_attributes (NULL, layout);
354
355   attrib_set = _gtk_style_context_get_attributes (attrib_set,
356                                                   gtk_widget_get_style_context (widget),
357                                                   gtk_widget_get_state_flags (widget));
358
359   g_object_unref (G_OBJECT (layout));
360
361   return attrib_set;
362 }
363
364 GtkWidget *
365 get_widget (GtkTextCellAccessible *text)
366 {
367   AtkObject *parent;
368
369   parent = atk_object_get_parent (ATK_OBJECT (text));
370   if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
371     parent = atk_object_get_parent (parent);
372
373   return gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
374 }
375
376 /* This function is used by gtk_text_cell_accessible_get_offset_at_point()
377  * and gtk_text_cell_accessible_get_character_extents(). There is no
378  * cached PangoLayout so we must create a temporary one using this function.
379  */
380 static PangoLayout *
381 create_pango_layout (GtkTextCellAccessible *text)
382 {
383   GdkRGBA *foreground_rgba;
384   PangoAttrList *attr_list, *attributes;
385   PangoLayout *layout;
386   PangoUnderline uline, underline;
387   PangoFontMask mask;
388   PangoFontDescription *font_desc;
389   gboolean foreground_set, strikethrough_set, strikethrough;
390   gboolean scale_set, underline_set, rise_set;
391   gchar *renderer_text;
392   gdouble scale;
393   gint rise;
394   GtkRendererCellAccessible *gail_renderer;
395   GtkCellRendererText *gtk_renderer;
396
397   gail_renderer = GTK_RENDERER_CELL_ACCESSIBLE (text);
398   g_object_get (gail_renderer, "renderer", &gtk_renderer, NULL);
399
400   g_object_get (gtk_renderer,
401                 "text", &renderer_text,
402                 "attributes", &attributes,
403                 "foreground-set", &foreground_set,
404                 "foreground-rgba", &foreground_rgba,
405                 "strikethrough-set", &strikethrough_set,
406                 "strikethrough", &strikethrough,
407                 "font-desc", &font_desc,
408                 "scale-set", &scale_set,
409                 "scale", &scale,
410                 "underline-set", &underline_set,
411                 "underline", &underline,
412                 "rise-set", &rise_set,
413                 "rise", &rise,
414                 NULL);
415   g_object_unref (gtk_renderer);
416
417   layout = gtk_widget_create_pango_layout (get_widget (text), renderer_text);
418
419   if (attributes)
420     attr_list = pango_attr_list_copy (attributes);
421   else
422     attr_list = pango_attr_list_new ();
423
424   if (foreground_set)
425     {
426       add_attr (attr_list, pango_attr_foreground_new (foreground_rgba->red * 65535,
427                                                       foreground_rgba->green * 65535,
428                                                       foreground_rgba->blue * 65535));
429     }
430
431   if (strikethrough_set)
432     add_attr (attr_list,
433               pango_attr_strikethrough_new (strikethrough));
434
435   mask = pango_font_description_get_set_fields (font_desc);
436
437   if (mask & PANGO_FONT_MASK_FAMILY)
438     add_attr (attr_list,
439       pango_attr_family_new (pango_font_description_get_family (font_desc)));
440
441   if (mask & PANGO_FONT_MASK_STYLE)
442     add_attr (attr_list, pango_attr_style_new (pango_font_description_get_style (font_desc)));
443
444   if (mask & PANGO_FONT_MASK_VARIANT)
445     add_attr (attr_list, pango_attr_variant_new (pango_font_description_get_variant (font_desc)));
446
447   if (mask & PANGO_FONT_MASK_WEIGHT)
448     add_attr (attr_list, pango_attr_weight_new (pango_font_description_get_weight (font_desc)));
449
450   if (mask & PANGO_FONT_MASK_STRETCH)
451     add_attr (attr_list, pango_attr_stretch_new (pango_font_description_get_stretch (font_desc)));
452
453   if (mask & PANGO_FONT_MASK_SIZE)
454     add_attr (attr_list, pango_attr_size_new (pango_font_description_get_size (font_desc)));
455
456   if (scale_set && scale != 1.0)
457     add_attr (attr_list, pango_attr_scale_new (scale));
458
459   if (underline_set)
460     uline = underline;
461   else
462     uline = PANGO_UNDERLINE_NONE;
463
464   if (uline != PANGO_UNDERLINE_NONE)
465     add_attr (attr_list,
466               pango_attr_underline_new (underline));
467
468   if (rise_set)
469     add_attr (attr_list, pango_attr_rise_new (rise));
470
471   pango_layout_set_attributes (layout, attr_list);
472   pango_layout_set_width (layout, -1);
473   pango_attr_list_unref (attr_list);
474
475   pango_font_description_free (font_desc);
476   pango_attr_list_unref (attributes);
477   g_free (renderer_text);
478   gdk_rgba_free (foreground_rgba);
479
480   return layout;
481 }
482
483 static void
484 add_attr (PangoAttrList *attr_list,
485          PangoAttribute *attr)
486 {
487   attr->start_index = 0;
488   attr->end_index = G_MAXINT;
489   pango_attr_list_insert (attr_list, attr);
490 }
491
492
493 static void
494 get_origins (GtkWidget *widget,
495              gint      *x_window,
496              gint      *y_window,
497              gint      *x_toplevel,
498              gint      *y_toplevel)
499 {
500   GdkWindow *window;
501
502   if (GTK_IS_TREE_VIEW (widget))
503     window = gtk_tree_view_get_bin_window (GTK_TREE_VIEW (widget));
504   else
505     window = gtk_widget_get_window (widget);
506
507   gdk_window_get_origin (window, x_window, y_window);
508   window = gdk_window_get_toplevel (gtk_widget_get_window (widget));
509   gdk_window_get_origin (window, x_toplevel, y_toplevel);
510 }
511
512 static void
513 gtk_text_cell_accessible_get_character_extents (AtkText      *text,
514                                                 gint          offset,
515                                                 gint         *x,
516                                                 gint         *y,
517                                                 gint         *width,
518                                                 gint         *height,
519                                                 AtkCoordType  coords)
520 {
521   GtkRendererCellAccessible *gail_renderer;
522   GtkRequisition min_size;
523   GtkCellRendererText *gtk_renderer;
524   GdkRectangle rendered_rect;
525   GtkWidget *widget;
526   AtkObject *parent;
527   PangoRectangle char_rect;
528   PangoLayout *layout;
529   gchar *renderer_text;
530   gfloat xalign, yalign;
531   gint x_offset, y_offset, index;
532   gint xpad, ypad;
533   gint x_window, y_window, x_toplevel, y_toplevel;
534
535   if (!GTK_TEXT_CELL_ACCESSIBLE (text)->cell_text)
536     {
537       *x = *y = *height = *width = 0;
538       return;
539     }
540   if (offset < 0 || offset >= GTK_TEXT_CELL_ACCESSIBLE (text)->cell_length)
541     {
542       *x = *y = *height = *width = 0;
543       return;
544     }
545   gail_renderer = GTK_RENDERER_CELL_ACCESSIBLE (text);
546   g_object_get (gail_renderer, "renderer", &gtk_renderer, NULL);
547   g_object_get (gtk_renderer, "text", &renderer_text, NULL);
548   g_object_unref (gtk_renderer);
549   if (renderer_text == NULL)
550     {
551       g_object_unref (gtk_renderer);
552       return;
553     }
554
555   parent = atk_object_get_parent (ATK_OBJECT (text));
556   if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
557     parent = atk_object_get_parent (parent);
558   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
559   g_return_if_fail (GTK_IS_CELL_ACCESSIBLE_PARENT (parent));
560   _gtk_cell_accessible_parent_get_cell_area (GTK_CELL_ACCESSIBLE_PARENT (parent),
561                                              GTK_CELL_ACCESSIBLE (text),
562                                              &rendered_rect);
563
564   gtk_cell_renderer_get_preferred_size (GTK_CELL_RENDERER (gtk_renderer),
565                                         widget,
566                                         &min_size, NULL);
567
568   gtk_cell_renderer_get_alignment (GTK_CELL_RENDERER (gtk_renderer), &xalign, &yalign);
569   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
570     xalign = 1.0 - xalign;
571   x_offset = MAX (0, xalign * (rendered_rect.width - min_size.width));
572   y_offset = MAX (0, yalign * (rendered_rect.height - min_size.height));
573
574   layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
575
576   index = g_utf8_offset_to_pointer (renderer_text, offset) - renderer_text;
577   pango_layout_index_to_pos (layout, index, &char_rect);
578
579   gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (gtk_renderer), &xpad, &ypad);
580
581   get_origins (widget, &x_window, &y_window, &x_toplevel, &y_toplevel);
582
583   *x = (char_rect.x / PANGO_SCALE) + x_offset + rendered_rect.x + xpad + x_window;
584   *y = (char_rect.y / PANGO_SCALE) + y_offset + rendered_rect.y + ypad + y_window;
585   *height = char_rect.height / PANGO_SCALE;
586   *width = char_rect.width / PANGO_SCALE;
587
588   if (coords == ATK_XY_WINDOW)
589     {
590       *x -= x_toplevel;
591       *y -= y_toplevel;
592     }
593   else if (coords != ATK_XY_SCREEN)
594     {
595       *x = 0;
596       *y = 0;
597       *height = 0;
598       *width = 0;
599     }
600
601   g_free (renderer_text);
602   g_object_unref (layout);
603   g_object_unref (gtk_renderer);
604 }
605
606 static gint
607 gtk_text_cell_accessible_get_offset_at_point (AtkText      *text,
608                                               gint          x,
609                                               gint          y,
610                                               AtkCoordType  coords)
611 {
612   AtkObject *parent;
613   GtkRendererCellAccessible *gail_renderer;
614   GtkCellRendererText *gtk_renderer;
615   GtkRequisition min_size;
616   GtkWidget *widget;
617   GdkRectangle rendered_rect;
618   PangoLayout *layout;
619   gchar *renderer_text;
620   gfloat xalign, yalign;
621   gint x_offset, y_offset, index;
622   gint xpad, ypad;
623   gint x_window, y_window, x_toplevel, y_toplevel;
624   gint x_temp, y_temp;
625   gboolean ret;
626
627   if (!GTK_TEXT_CELL_ACCESSIBLE (text)->cell_text)
628     return -1;
629
630   gail_renderer = GTK_RENDERER_CELL_ACCESSIBLE (text);
631   g_object_get (gail_renderer, "renderer", &gtk_renderer, NULL);
632   parent = atk_object_get_parent (ATK_OBJECT (text));
633
634   g_object_get (gtk_renderer, "text", &renderer_text, NULL);
635   if (text == NULL)
636     {
637       g_object_unref (gtk_renderer);
638       g_free (renderer_text);
639       return -1;
640     }
641
642   if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
643     parent = atk_object_get_parent (parent);
644
645   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
646
647   g_return_val_if_fail (GTK_IS_CELL_ACCESSIBLE_PARENT (parent), -1);
648   _gtk_cell_accessible_parent_get_cell_area (GTK_CELL_ACCESSIBLE_PARENT (parent),
649                                              GTK_CELL_ACCESSIBLE (text),
650                                              &rendered_rect);
651
652   gtk_cell_renderer_get_preferred_size (GTK_CELL_RENDERER (gtk_renderer),
653                                         widget,
654                                         &min_size, NULL);
655   gtk_cell_renderer_get_alignment (GTK_CELL_RENDERER (gtk_renderer), &xalign, &yalign);
656   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
657     xalign = 1.0 - xalign;
658   x_offset = MAX (0, xalign * (rendered_rect.width - min_size.width));
659   y_offset = MAX (0, yalign * (rendered_rect.height - min_size.height));
660
661   layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
662
663   gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (gtk_renderer), &xpad, &ypad);
664
665   get_origins (widget, &x_window, &y_window, &x_toplevel, &y_toplevel);
666
667   x_temp =  x - (x_offset + rendered_rect.x + xpad) - x_window;
668   y_temp =  y - (y_offset + rendered_rect.y + ypad) - y_window;
669   if (coords == ATK_XY_WINDOW)
670     {
671       x_temp += x_toplevel;
672       y_temp += y_toplevel;
673     }
674   else if (coords != ATK_XY_SCREEN)
675     index = -1;
676
677   ret = pango_layout_xy_to_index (layout,
678                                   x_temp * PANGO_SCALE,
679                                   y_temp * PANGO_SCALE,
680                                   &index, NULL);
681   if (!ret)
682     {
683       if (x_temp < 0 || y_temp < 0)
684         index = 0;
685       else
686         index = -1;
687     }
688
689   g_object_unref (layout);
690   g_object_unref (gtk_renderer);
691
692   if (index == -1)
693     {
694       if (coords == ATK_XY_WINDOW || coords == ATK_XY_SCREEN)
695         {
696           glong length;
697
698           length = g_utf8_strlen (renderer_text, -1);
699           g_free (renderer_text);
700
701           return length;
702         }
703
704       g_free (renderer_text);
705
706       return index;
707     }
708   else
709     {
710       glong offset;
711
712       offset = g_utf8_pointer_to_offset (renderer_text,
713                                          renderer_text + index);
714       g_free (renderer_text);
715
716       return offset;
717     }
718 }
719
720 static gunichar
721 gtk_text_cell_accessible_get_character_at_offset (AtkText *text,
722                                                   gint     offset)
723 {
724   gchar *index;
725   gchar *string;
726
727   string = GTK_TEXT_CELL_ACCESSIBLE(text)->cell_text;
728
729   if (!string)
730     return '\0';
731
732   if (offset >= g_utf8_strlen (string, -1))
733     return '\0';
734
735   index = g_utf8_offset_to_pointer (string, offset);
736
737   return g_utf8_get_char (index);
738 }
739
740 static void
741 atk_text_interface_init (AtkTextIface *iface)
742 {
743   iface->get_text = gtk_text_cell_accessible_get_text;
744   iface->get_character_at_offset = gtk_text_cell_accessible_get_character_at_offset;
745   iface->get_text_before_offset = gtk_text_cell_accessible_get_text_before_offset;
746   iface->get_text_at_offset = gtk_text_cell_accessible_get_text_at_offset;
747   iface->get_text_after_offset = gtk_text_cell_accessible_get_text_after_offset;
748   iface->get_character_count = gtk_text_cell_accessible_get_character_count;
749   iface->get_caret_offset = gtk_text_cell_accessible_get_caret_offset;
750   iface->set_caret_offset = gtk_text_cell_accessible_set_caret_offset;
751   iface->get_run_attributes = gtk_text_cell_accessible_get_run_attributes;
752   iface->get_default_attributes = gtk_text_cell_accessible_get_default_attributes;
753   iface->get_character_extents = gtk_text_cell_accessible_get_character_extents;
754   iface->get_offset_at_point = gtk_text_cell_accessible_get_offset_at_point;
755 }