Debian Codegraph @

gtk/a11y/gtktextcellaccessible.c

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