]> Pileus Git - ~andy/gtk/blob - modules/other/gail/gailtextcell.c
Really get rid of _gtk_cell_renderer_calc_offset
[~andy/gtk] / modules / other / gail / gailtextcell.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 "gailtextcell.h"
24 #include "gailcontainercell.h"
25 #include "gailcellparent.h"
26 #include <libgail-util/gailmisc.h>
27 #include "gail-private-macros.h"
28
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);
32
33 static G_CONST_RETURN gchar* gail_text_cell_get_name    (AtkObject      *atk_obj);
34
35 static void      atk_text_interface_init                (AtkTextIface   *iface);
36
37 /* atktext.h */
38
39 static gchar*    gail_text_cell_get_text                (AtkText        *text,
40                                                         gint            start_pos,
41                                                         gint            end_pos);
42 static gunichar gail_text_cell_get_character_at_offset  (AtkText        *text,
43                                                          gint           offset);
44 static gchar*   gail_text_cell_get_text_before_offset   (AtkText        *text,
45                                                          gint           offset,
46                                                          AtkTextBoundary boundary_type,
47                                                          gint           *start_offset,
48                                                          gint           *end_offset);
49 static gchar*   gail_text_cell_get_text_at_offset       (AtkText        *text,
50                                                          gint           offset,
51                                                          AtkTextBoundary boundary_type,
52                                                          gint           *start_offset,
53                                                          gint           *end_offset);
54 static gchar*   gail_text_cell_get_text_after_offset    (AtkText        *text,
55                                                          gint           offset,
56                                                          AtkTextBoundary boundary_type,
57                                                          gint           *start_offset,
58                                                          gint           *end_offset);
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,
62                                                          gint           offset);
63 static void      gail_text_cell_get_character_extents   (AtkText        *text,
64                                                          gint           offset,
65                                                          gint           *x,
66                                                          gint           *y,
67                                                          gint           *width,
68                                                          gint           *height,
69                                                          AtkCoordType   coords);
70 static gint      gail_text_cell_get_offset_at_point     (AtkText        *text,
71                                                          gint           x,
72                                                          gint           y,
73                                                          AtkCoordType   coords);
74 static AtkAttributeSet* gail_text_cell_get_run_attributes 
75                                                         (AtkText        *text,
76                                                          gint           offset,
77                                                          gint           *start_offset,      
78                                                          gint           *end_offset); 
79 static AtkAttributeSet* gail_text_cell_get_default_attributes 
80                                                         (AtkText        *text);
81
82 static PangoLayout*     create_pango_layout             (GtkCellRendererText *gtk_renderer,
83                                                          GtkWidget           *widget);
84 static void             add_attr                        (PangoAttrList  *attr_list,
85                                                          PangoAttribute *attr);
86
87 /* Misc */
88
89 static gboolean gail_text_cell_update_cache             (GailRendererCell *cell,
90                                                          gboolean       emit_change_signal);
91
92 gchar *gail_text_cell_property_list[] = {
93   /* Set font_desc first since it resets other values if it is NULL */
94   "font_desc",
95
96   "attributes",
97   "background_gdk",
98   "editable",
99   "family",
100   "foreground_gdk",
101   "rise",
102   "scale",
103   "size",
104   "size_points",
105   "stretch",
106   "strikethrough",
107   "style",
108   "text",
109   "underline",
110   "variant",
111   "weight",
112
113   /* Also need the sets */
114   "background_set",
115   "editable_set",
116   "family_set",
117   "foreground_set",
118   "rise_set",
119   "scale_set",
120   "size_set",
121   "stretch_set",
122   "strikethrough_set",
123   "style_set",
124   "underline_set",
125   "variant_set",
126   "weight_set",
127   NULL
128 };
129
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))
132
133 static void 
134 gail_text_cell_class_init (GailTextCellClass *klass)
135 {
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);
139
140   renderer_cell_class->update_cache = gail_text_cell_update_cache;
141   renderer_cell_class->property_list = gail_text_cell_property_list;
142
143   atk_object_class->get_name = gail_text_cell_get_name;
144
145   gobject_class->finalize = gail_text_cell_finalize;
146 }
147
148 /* atktext.h */
149
150 static void
151 gail_text_cell_init (GailTextCell *text_cell)
152 {
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);
159 }
160
161 AtkObject* 
162 gail_text_cell_new (void)
163 {
164   GObject *object;
165   AtkObject *atk_object;
166   GailRendererCell *cell;
167
168   object = g_object_new (GAIL_TYPE_TEXT_CELL, NULL);
169
170   g_return_val_if_fail (object != NULL, NULL);
171
172   atk_object = ATK_OBJECT (object);
173   atk_object->role = ATK_ROLE_TABLE_CELL;
174
175   cell = GAIL_RENDERER_CELL(object);
176
177   cell->renderer = gtk_cell_renderer_text_new ();
178   g_object_ref_sink (cell->renderer);
179   return atk_object;
180 }
181
182 static void
183 gail_text_cell_finalize (GObject            *object)
184 {
185   GailTextCell *text_cell = GAIL_TEXT_CELL (object);
186
187   g_object_unref (text_cell->textutil);
188   g_free (text_cell->cell_text);
189
190   G_OBJECT_CLASS (gail_text_cell_parent_class)->finalize (object);
191 }
192
193 static G_CONST_RETURN gchar*
194 gail_text_cell_get_name (AtkObject *atk_obj)
195 {
196   if (atk_obj->name)
197     return atk_obj->name;
198   else
199     {
200       GailTextCell *text_cell = GAIL_TEXT_CELL (atk_obj);
201
202       return text_cell->cell_text;
203     }
204 }
205
206 static gboolean
207 gail_text_cell_update_cache (GailRendererCell *cell,
208                              gboolean         emit_change_signal)
209 {
210   GailTextCell *text_cell = GAIL_TEXT_CELL (cell);
211   AtkObject *obj = ATK_OBJECT (cell);
212   gboolean rv = FALSE;
213   gint temp_length;
214   gchar *new_cache;
215
216   g_object_get (G_OBJECT (cell->renderer), "text", &new_cache, NULL);
217
218   if (text_cell->cell_text)
219     {
220      /*
221       * If the new value is NULL and the old value isn't NULL, then the
222       * value has changed.
223       */
224       if (new_cache == NULL ||
225           strcmp (text_cell->cell_text, new_cache))
226         {
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)
232             {
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");
236             }
237           if (new_cache)
238             rv = TRUE;
239         }
240     }
241   else
242     rv = TRUE;
243
244   if (rv)
245     {
246       if (new_cache == NULL)
247         {
248           text_cell->cell_text = g_strdup ("");
249           text_cell->cell_length = 0;
250         }
251       else
252         {
253           text_cell->cell_text = g_strdup (new_cache);
254           text_cell->cell_length = g_utf8_strlen (new_cache, -1);
255         }
256     }
257
258   g_free (new_cache);
259   gail_text_util_text_setup (text_cell->textutil, text_cell->cell_text);
260   
261   if (rv)
262     {
263       if (emit_change_signal)
264         {
265           g_signal_emit_by_name (cell, "text_changed::insert",
266                                  0, text_cell->cell_length);
267
268           if (obj->name == NULL)
269             g_object_notify (G_OBJECT (obj), "accessible-name");
270         }
271     }
272   return rv;
273 }
274
275 static void
276 atk_text_interface_init (AtkTextIface *iface)
277 {
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;
290 }
291
292 static gchar* 
293 gail_text_cell_get_text (AtkText *text, 
294                          gint    start_pos,
295                          gint    end_pos)
296 {
297   if (GAIL_TEXT_CELL (text)->cell_text)
298     return gail_text_util_get_substring (GAIL_TEXT_CELL (text)->textutil,
299               start_pos, end_pos);
300   else
301     return g_strdup ("");
302 }
303
304 static gchar* 
305 gail_text_cell_get_text_before_offset (AtkText         *text,
306                                        gint            offset,
307                                        AtkTextBoundary boundary_type,
308                                        gint            *start_offset,
309                                        gint            *end_offset)
310 {
311   return gail_text_util_get_text (GAIL_TEXT_CELL (text)->textutil,
312         NULL, GAIL_BEFORE_OFFSET, boundary_type, offset, start_offset,
313         end_offset);
314 }
315
316 static gchar* 
317 gail_text_cell_get_text_at_offset (AtkText         *text,
318                                    gint            offset,
319                                    AtkTextBoundary boundary_type,
320                                    gint            *start_offset,
321                                    gint            *end_offset)
322 {
323   return gail_text_util_get_text (GAIL_TEXT_CELL (text)->textutil,
324         NULL, GAIL_AT_OFFSET, boundary_type, offset, start_offset, end_offset);
325 }
326
327 static gchar* 
328 gail_text_cell_get_text_after_offset (AtkText         *text,
329                                       gint            offset,
330                                       AtkTextBoundary boundary_type,
331                                       gint            *start_offset,
332                                       gint            *end_offset)
333 {
334   return gail_text_util_get_text (GAIL_TEXT_CELL (text)->textutil,
335         NULL, GAIL_AFTER_OFFSET, boundary_type, offset, start_offset,
336         end_offset);
337 }
338
339 static gint 
340 gail_text_cell_get_character_count (AtkText *text)
341 {
342   if (GAIL_TEXT_CELL (text)->cell_text != NULL)
343     return GAIL_TEXT_CELL (text)->cell_length;
344   else
345     return 0;
346 }
347
348 static gint 
349 gail_text_cell_get_caret_offset (AtkText *text)
350 {
351   return GAIL_TEXT_CELL (text)->caret_pos;
352 }
353
354 static gboolean 
355 gail_text_cell_set_caret_offset (AtkText *text,
356                                  gint    offset)
357 {
358   GailTextCell *text_cell = GAIL_TEXT_CELL (text);
359
360   if (text_cell->cell_text == NULL)
361     return FALSE;
362   else
363     {
364
365       /* Only set the caret within the bounds and if it is to a new position. */
366       if (offset >= 0 && 
367           offset <= text_cell->cell_length &&
368           offset != text_cell->caret_pos)
369         {
370           text_cell->caret_pos = offset;
371
372           /* emit the signal */
373           g_signal_emit_by_name (text, "text_caret_moved", offset);
374           return TRUE;
375         }
376       else
377         return FALSE;
378     }
379 }
380
381 static AtkAttributeSet*
382 gail_text_cell_get_run_attributes (AtkText *text,
383                                   gint     offset,
384                                   gint     *start_offset,
385                                   gint     *end_offset) 
386 {
387   GailRendererCell *gail_renderer; 
388   GtkCellRendererText *gtk_renderer;
389   AtkAttributeSet *attrib_set = NULL;
390   PangoLayout *layout;
391   AtkObject *parent;
392   GtkWidget *widget;
393   gchar *renderer_text;
394
395   gail_renderer = GAIL_RENDERER_CELL (text);
396   gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
397
398   parent = atk_object_get_parent (ATK_OBJECT (text));
399   if (GAIL_IS_CONTAINER_CELL (parent))
400     parent = atk_object_get_parent (parent);
401   g_return_val_if_fail (GAIL_IS_CELL_PARENT (parent), NULL);
402   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
403   layout = create_pango_layout (gtk_renderer, widget),
404   g_object_get (gtk_renderer, "text", &renderer_text, NULL);
405   attrib_set = gail_misc_layout_get_run_attributes (attrib_set, 
406                                                     layout,
407                                                     renderer_text,
408                                                     offset,
409                                                     start_offset,
410                                                     end_offset);
411   g_free (renderer_text);
412   g_object_unref (G_OBJECT (layout));
413   
414   return attrib_set;
415 }
416
417 static AtkAttributeSet*
418 gail_text_cell_get_default_attributes (AtkText  *text)
419 {
420   GailRendererCell *gail_renderer; 
421   GtkCellRendererText *gtk_renderer;
422   AtkAttributeSet *attrib_set = NULL;
423   PangoLayout *layout;
424   AtkObject *parent;
425   GtkWidget *widget;
426
427   gail_renderer = GAIL_RENDERER_CELL (text);
428   gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
429
430   parent = atk_object_get_parent (ATK_OBJECT (text));
431   if (GAIL_IS_CONTAINER_CELL (parent))
432     parent = atk_object_get_parent (parent);
433   g_return_val_if_fail (GAIL_IS_CELL_PARENT (parent), NULL);
434   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
435   layout = create_pango_layout (gtk_renderer, widget),
436
437   attrib_set = gail_misc_get_default_attributes (attrib_set, 
438                                                  layout,
439                                                  widget);
440   g_object_unref (G_OBJECT (layout));
441   return attrib_set;
442 }
443
444 /* 
445  * This function is used by gail_text_cell_get_offset_at_point()
446  * and gail_text_cell_get_character_extents(). There is no 
447  * cached PangoLayout for gailtextcell so we must create a temporary
448  * one using this function.
449  */ 
450 static PangoLayout*
451 create_pango_layout(GtkCellRendererText *gtk_renderer,
452                     GtkWidget           *widget)
453 {
454   GdkColor *foreground_gdk;
455   PangoAttrList *attr_list, *attributes;
456   PangoLayout *layout;
457   PangoUnderline uline, underline;
458   PangoFontMask mask;
459   PangoFontDescription *font_desc;
460   gboolean foreground_set, strikethrough_set, strikethrough;
461   gboolean scale_set, underline_set, rise_set;
462   gchar *renderer_text;
463   gdouble scale;
464   gint rise;
465
466   g_object_get (gtk_renderer,
467                 "text", &renderer_text,
468                 "attributes", &attributes,
469                 "foreground-set", &foreground_set,
470                 "foreground-gdk", &foreground_gdk,
471                 "strikethrough-set", &strikethrough_set,
472                 "strikethrough", &strikethrough,
473                 "font-desc", &font_desc,
474                 "scale-set", &scale_set,
475                 "scale", &scale,
476                 "underline-set", &underline_set,
477                 "underline", &underline,
478                 "rise-set", &rise_set,
479                 "rise", &rise,
480                 NULL);
481
482   layout = gtk_widget_create_pango_layout (widget, renderer_text);
483
484   if (attributes)
485     attr_list = pango_attr_list_copy (attributes);
486   else
487     attr_list = pango_attr_list_new ();
488
489   if (foreground_set)
490     {
491       add_attr (attr_list, pango_attr_foreground_new (foreground_gdk->red,
492                                                       foreground_gdk->green,
493                                                       foreground_gdk->blue));
494     }
495
496   if (strikethrough_set)
497     add_attr (attr_list,
498               pango_attr_strikethrough_new (strikethrough));
499
500   mask = pango_font_description_get_set_fields (font_desc);
501
502   if (mask & PANGO_FONT_MASK_FAMILY)
503     add_attr (attr_list,
504       pango_attr_family_new (pango_font_description_get_family (font_desc)));
505
506   if (mask & PANGO_FONT_MASK_STYLE)
507     add_attr (attr_list, pango_attr_style_new (pango_font_description_get_style (font_desc)));
508
509   if (mask & PANGO_FONT_MASK_VARIANT)
510     add_attr (attr_list, pango_attr_variant_new (pango_font_description_get_variant (font_desc)));
511
512   if (mask & PANGO_FONT_MASK_WEIGHT)
513     add_attr (attr_list, pango_attr_weight_new (pango_font_description_get_weight (font_desc)));
514
515   if (mask & PANGO_FONT_MASK_STRETCH)
516     add_attr (attr_list, pango_attr_stretch_new (pango_font_description_get_stretch (font_desc)));
517
518   if (mask & PANGO_FONT_MASK_SIZE)
519     add_attr (attr_list, pango_attr_size_new (pango_font_description_get_size (font_desc)));
520
521   if (scale_set && scale != 1.0)
522     add_attr (attr_list, pango_attr_scale_new (scale));
523
524   if (underline_set)
525     uline = underline;
526   else
527     uline = PANGO_UNDERLINE_NONE;
528
529   if (uline != PANGO_UNDERLINE_NONE)
530     add_attr (attr_list,
531               pango_attr_underline_new (underline));
532
533   if (rise_set)
534     add_attr (attr_list, pango_attr_rise_new (rise));
535
536   pango_layout_set_attributes (layout, attr_list);
537   pango_layout_set_width (layout, -1);
538   pango_attr_list_unref (attr_list);
539
540   pango_font_description_free (font_desc);
541   g_free (foreground_gdk);
542   pango_attr_list_unref (attributes);
543   g_free (renderer_text);
544
545   return layout;
546 }
547
548 static void 
549 add_attr (PangoAttrList  *attr_list,
550          PangoAttribute *attr)
551 {
552   attr->start_index = 0;
553   attr->end_index = G_MAXINT;
554   pango_attr_list_insert (attr_list, attr);
555 }
556
557 static void      
558 gail_text_cell_get_character_extents (AtkText          *text,
559                                       gint             offset,
560                                       gint             *x,
561                                       gint             *y,
562                                       gint             *width,
563                                       gint             *height,
564                                       AtkCoordType     coords)
565 {
566   GailRendererCell *gail_renderer; 
567   GtkRequisition min_size;
568   GtkCellRendererText *gtk_renderer;
569   GdkRectangle rendered_rect;
570   GtkWidget *widget;
571   AtkObject *parent;
572   PangoRectangle char_rect;
573   PangoLayout *layout;
574   gchar *renderer_text;
575   gfloat xalign, yalign;
576   gint x_offset, y_offset, index;
577   gint xpad, ypad;
578
579   if (!GAIL_TEXT_CELL (text)->cell_text)
580     {
581       *x = *y = *height = *width = 0;
582       return;
583     }
584   if (offset < 0 || offset >= GAIL_TEXT_CELL (text)->cell_length)
585     {
586       *x = *y = *height = *width = 0;
587       return;
588     }
589   gail_renderer = GAIL_RENDERER_CELL (text);
590   gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
591   /*
592    * Thus would be inconsistent with the cache
593    */
594   g_object_get (gtk_renderer, "text", &renderer_text, NULL);
595   if (text == NULL)
596     {
597       g_free (renderer_text);
598       return;
599     }
600
601   parent = atk_object_get_parent (ATK_OBJECT (text));
602   if (GAIL_IS_CONTAINER_CELL (parent))
603     parent = atk_object_get_parent (parent);
604   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
605   g_return_if_fail (GAIL_IS_CELL_PARENT (parent));
606   gail_cell_parent_get_cell_area (GAIL_CELL_PARENT (parent), GAIL_CELL (text),
607                                   &rendered_rect);
608
609   gtk_cell_renderer_get_preferred_size (GTK_CELL_RENDERER (gtk_renderer),
610                                         widget,
611                                         &min_size, NULL);
612
613   gtk_cell_renderer_get_alignment (GTK_CELL_RENDERER (gtk_renderer), &xalign, &yalign);
614   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
615     xalign = 1.0 - xalign;
616   x_offset = MAX (0, xalign * (rendered_rect.width - min_size.width));
617   y_offset = MAX (0, yalign * (rendered_rect.height - min_size.height));
618
619   layout = create_pango_layout (gtk_renderer, widget);
620
621   index = g_utf8_offset_to_pointer (renderer_text, offset) - renderer_text;
622   pango_layout_index_to_pos (layout, index, &char_rect);
623
624   gtk_cell_renderer_get_padding (gail_renderer->renderer, &xpad, &ypad);
625   gail_misc_get_extents_from_pango_rectangle (widget,
626       &char_rect,
627       x_offset + rendered_rect.x + xpad,
628       y_offset + rendered_rect.y + ypad,
629       x, y, width, height, coords);
630
631   g_free (renderer_text);
632   g_object_unref (layout);
633
634   return;
635
636
637 static gint      
638 gail_text_cell_get_offset_at_point (AtkText          *text,
639                                     gint             x,
640                                     gint             y,
641                                     AtkCoordType     coords)
642 {
643   AtkObject *parent;
644   GailRendererCell *gail_renderer; 
645   GtkCellRendererText *gtk_renderer;
646   GtkRequisition min_size;
647   GtkWidget *widget;
648   GdkRectangle rendered_rect;
649   PangoLayout *layout;
650   gchar *renderer_text;
651   gfloat xalign, yalign;
652   gint x_offset, y_offset, index;
653   gint xpad, ypad;
654  
655   if (!GAIL_TEXT_CELL (text)->cell_text)
656     return -1;
657
658   gail_renderer = GAIL_RENDERER_CELL (text);
659   gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
660   parent = atk_object_get_parent (ATK_OBJECT (text));
661
662   g_object_get (gtk_renderer, "text", &renderer_text, NULL);
663   if (text == NULL)
664     {
665       g_free (renderer_text);
666       return -1;
667     }
668
669   if (GAIL_IS_CONTAINER_CELL (parent))
670     parent = atk_object_get_parent (parent);
671
672   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
673
674   g_return_val_if_fail (GAIL_IS_CELL_PARENT (parent), -1);
675   gail_cell_parent_get_cell_area (GAIL_CELL_PARENT (parent), GAIL_CELL (text),
676                                   &rendered_rect);
677
678   gtk_cell_renderer_get_preferred_size (GTK_CELL_RENDERER (gtk_renderer),
679                                         widget,
680                                         &min_size, NULL);
681   gtk_cell_renderer_get_alignment (GTK_CELL_RENDERER (gtk_renderer), &xalign, &yalign);
682   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
683     xalign = 1.0 - xalign;
684   x_offset = MAX (0, xalign * (rendered_rect.width - min_size.width));
685   y_offset = MAX (0, yalign * (rendered_rect.height - min_size.height));
686
687   layout = create_pango_layout (gtk_renderer, widget);
688
689   gtk_cell_renderer_get_padding (gail_renderer->renderer, &xpad, &ypad);
690   index = gail_misc_get_index_at_point_in_layout (widget, layout,
691         x_offset + rendered_rect.x + xpad,
692         y_offset + rendered_rect.y + ypad,
693         x, y, coords);
694   g_object_unref (layout);
695   if (index == -1)
696     {
697       if (coords == ATK_XY_WINDOW || coords == ATK_XY_SCREEN)
698         {
699           glong length;
700
701           length = g_utf8_strlen (renderer_text, -1);
702           g_free (renderer_text);
703
704           return length;
705         }
706
707       g_free (renderer_text);
708
709       return index;  
710     }
711   else
712     {
713       glong offset;
714
715       offset = g_utf8_pointer_to_offset (renderer_text,
716                                          renderer_text + index);
717       g_free (renderer_text);
718
719       return offset;
720     }
721 }
722
723 static gunichar 
724 gail_text_cell_get_character_at_offset (AtkText       *text,
725                                         gint          offset)
726 {
727   gchar *index;
728   gchar *string;
729
730   string = GAIL_TEXT_CELL(text)->cell_text;
731
732   if (!string)
733     return '\0';
734
735   if (offset >= g_utf8_strlen (string, -1))
736     return '\0';
737
738   index = g_utf8_offset_to_pointer (string, offset);
739
740   return g_utf8_get_char (index);
741 }
742