gtktextcellaccessible.c 25.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* GAIL - The GNOME Accessibility Enabling Library
 * Copyright 2001 Sun Microsystems Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
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 30


/* atktext.h */

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
static gchar*    gtk_text_cell_accessible_get_text                (AtkText        *text,
                                                        gint            start_pos,
                                                        gint            end_pos);
static gunichar gtk_text_cell_accessible_get_character_at_offset  (AtkText        *text,
                                                         gint           offset);
static gchar*   gtk_text_cell_accessible_get_text_before_offset   (AtkText        *text,
                                                         gint           offset,
                                                         AtkTextBoundary boundary_type,
                                                         gint           *start_offset,
                                                         gint           *end_offset);
static gchar*   gtk_text_cell_accessible_get_text_at_offset       (AtkText        *text,
                                                         gint           offset,
                                                         AtkTextBoundary boundary_type,
                                                         gint           *start_offset,
                                                         gint           *end_offset);
static gchar*   gtk_text_cell_accessible_get_text_after_offset    (AtkText        *text,
                                                         gint           offset,
                                                         AtkTextBoundary boundary_type,
                                                         gint           *start_offset,
                                                         gint           *end_offset);
static gint      gtk_text_cell_accessible_get_character_count     (AtkText        *text);
static gint      gtk_text_cell_accessible_get_caret_offset        (AtkText        *text);
static gboolean  gtk_text_cell_accessible_set_caret_offset        (AtkText        *text,
                                                         gint           offset);
static void      gtk_text_cell_accessible_get_character_extents   (AtkText        *text,
                                                         gint           offset,
                                                         gint           *x,
                                                         gint           *y,
                                                         gint           *width,
                                                         gint           *height,
                                                         AtkCoordType   coords);
static gint      gtk_text_cell_accessible_get_offset_at_point     (AtkText        *text,
                                                         gint           x,
                                                         gint           y,
                                                         AtkCoordType   coords);
static AtkAttributeSet* gtk_text_cell_accessible_get_run_attributes 
                                                        (AtkText        *text,
                                                         gint           offset,
                                                         gint           *start_offset,      
                                                         gint           *end_offset); 
static AtkAttributeSet* gtk_text_cell_accessible_get_default_attributes 
72 73
                                                        (AtkText        *text);

74 75
static GtkWidget*       get_widget                      (GtkTextCellAccessible *cell);
static PangoLayout*     create_pango_layout             (GtkTextCellAccessible *cell);
76 77 78 79 80
static void             add_attr                        (PangoAttrList  *attr_list,
                                                         PangoAttribute *attr);

/* Misc */

81
static void gtk_text_cell_accessible_update_cache       (GtkCellAccessible *cell);
82

83
static void atk_text_interface_init (AtkTextIface *iface);
84

85 86
G_DEFINE_TYPE_WITH_CODE (GtkTextCellAccessible, _gtk_text_cell_accessible, GTK_TYPE_RENDERER_CELL_ACCESSIBLE,
                         G_IMPLEMENT_INTERFACE (ATK_TYPE_TEXT, atk_text_interface_init))
87

88 89 90 91 92 93 94 95 96 97 98
static AtkStateSet *
gtk_text_cell_accessible_ref_state_set (AtkObject *accessible)
{
  AtkStateSet *state_set;

  state_set = ATK_OBJECT_CLASS (_gtk_text_cell_accessible_parent_class)->ref_state_set (accessible);

  atk_state_set_add_state (state_set, ATK_STATE_SINGLE_LINE);

  return state_set;
}
99 100

static void
101
gtk_text_cell_accessible_finalize (GObject *object)
102
{
103
  GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (object);
104 105 106

  g_free (text_cell->cell_text);

107
  G_OBJECT_CLASS (_gtk_text_cell_accessible_parent_class)->finalize (object);
108 109
}

110 111
static const gchar *
gtk_text_cell_accessible_get_name (AtkObject *atk_obj)
112
{
113 114
  GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (atk_obj);

115 116 117
  if (atk_obj->name)
    return atk_obj->name;

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 126 127
  AtkObject *obj = ATK_OBJECT (cell);
  gboolean rv = FALSE;
  gint temp_length;
128
  gchar *text;
129

130 131 132
  g_object_get (G_OBJECT (GTK_RENDERER_CELL_ACCESSIBLE (cell)->renderer),
                "text", &text,
                NULL);
133 134 135

  if (text_cell->cell_text)
    {
136
      if (text == NULL || g_strcmp0 (text_cell->cell_text, text) != 0)
137 138 139 140 141
        {
          g_free (text_cell->cell_text);
          temp_length = text_cell->cell_length;
          text_cell->cell_text = NULL;
          text_cell->cell_length = 0;
142 143 144
          g_signal_emit_by_name (cell, "text-changed::delete", 0, temp_length);
          if (obj->name == NULL)
            g_object_notify (G_OBJECT (obj), "accessible-name");
145
          if (text)
146 147 148 149 150 151 152 153
            rv = TRUE;
        }
    }
  else
    rv = TRUE;

  if (rv)
    {
154
      if (text == NULL)
155 156 157 158 159 160
        {
          text_cell->cell_text = g_strdup ("");
          text_cell->cell_length = 0;
        }
      else
        {
161 162
          text_cell->cell_text = g_strdup (text);
          text_cell->cell_length = g_utf8_strlen (text, -1);
163 164 165
        }
    }

166
  g_free (text);
167

168 169
  if (rv)
    {
170 171
      g_signal_emit_by_name (cell, "text-changed::insert",
                             0, text_cell->cell_length);
172

173 174
      if (obj->name == NULL)
        g_object_notify (G_OBJECT (obj), "accessible-name");
175 176 177 178
    }
}

static void
179 180 181 182
_gtk_text_cell_accessible_class_init (GtkTextCellAccessibleClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  AtkObjectClass *atk_object_class = ATK_OBJECT_CLASS (klass);
183
  GtkCellAccessibleClass *cell_class = GTK_CELL_ACCESSIBLE_CLASS (klass);
184

185
  cell_class->update_cache = gtk_text_cell_accessible_update_cache;
186 187

  atk_object_class->get_name = gtk_text_cell_accessible_get_name;
188
  atk_object_class->ref_state_set = gtk_text_cell_accessible_ref_state_set;
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

  gobject_class->finalize = gtk_text_cell_accessible_finalize;
}

static void
_gtk_text_cell_accessible_init (GtkTextCellAccessible *text_cell)
{
  text_cell->cell_text = NULL;
  text_cell->caret_pos = 0;
  text_cell->cell_length = 0;
}

static gchar *
gtk_text_cell_accessible_get_text (AtkText *atk_text,
                                   gint     start_pos,
                                   gint     end_pos)
205
{
206 207
  gchar *text;

208
  text = GTK_TEXT_CELL_ACCESSIBLE (atk_text)->cell_text;
209 210
  if (text)
    return g_utf8_substring (text, start_pos, end_pos > -1 ? end_pos : g_utf8_strlen (text, -1));
211 212 213 214
  else
    return g_strdup ("");
}

215 216 217 218 219 220
static gchar *
gtk_text_cell_accessible_get_text_before_offset (AtkText         *atk_text,
                                                 gint             offset,
                                                 AtkTextBoundary  boundary_type,
                                                 gint            *start_offset,
                                                 gint            *end_offset)
221
{
222 223 224
  PangoLayout *layout;
  gchar *text;

225
  layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (atk_text));
226 227 228 229
  text = _gtk_pango_get_text_before (layout, boundary_type, offset, start_offset, end_offset);
  g_object_unref (layout);

  return text;
230 231
}

232 233 234 235 236 237
static gchar *
gtk_text_cell_accessible_get_text_at_offset (AtkText         *atk_text,
                                             gint             offset,
                                             AtkTextBoundary  boundary_type,
                                             gint            *start_offset,
                                             gint            *end_offset)
238
{
239 240 241
  PangoLayout *layout;
  gchar *text;

242
  layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (atk_text));
243 244 245 246
  text = _gtk_pango_get_text_at (layout, boundary_type, offset, start_offset, end_offset);
  g_object_unref (layout);

  return text;
247 248
}

249 250 251 252 253 254
static gchar *
gtk_text_cell_accessible_get_text_after_offset (AtkText         *atk_text,
                                                gint             offset,
                                                AtkTextBoundary  boundary_type,
                                                gint            *start_offset,
                                                gint            *end_offset)
255
{
256 257 258
  PangoLayout *layout;
  gchar *text;

259
  layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (atk_text));
260 261 262 263
  text = _gtk_pango_get_text_after (layout, boundary_type, offset, start_offset, end_offset);
  g_object_unref (layout);

  return text;
264 265
}

266 267
static gint
gtk_text_cell_accessible_get_character_count (AtkText *text)
268
{
269 270
  if (GTK_TEXT_CELL_ACCESSIBLE (text)->cell_text != NULL)
    return GTK_TEXT_CELL_ACCESSIBLE (text)->cell_length;
271 272 273 274
  else
    return 0;
}

275 276
static gint
gtk_text_cell_accessible_get_caret_offset (AtkText *text)
277
{
278
  return GTK_TEXT_CELL_ACCESSIBLE (text)->caret_pos;
279 280
}

281 282 283
static gboolean
gtk_text_cell_accessible_set_caret_offset (AtkText *text,
                                           gint     offset)
284
{
285
  GtkTextCellAccessible *text_cell = GTK_TEXT_CELL_ACCESSIBLE (text);
286 287 288 289 290 291

  if (text_cell->cell_text == NULL)
    return FALSE;
  else
    {
      /* Only set the caret within the bounds and if it is to a new position. */
292
      if (offset >= 0 &&
293 294 295 296 297 298
          offset <= text_cell->cell_length &&
          offset != text_cell->caret_pos)
        {
          text_cell->caret_pos = offset;

          /* emit the signal */
Matthias Clasen's avatar
Matthias Clasen committed
299
          g_signal_emit_by_name (text, "text-caret-moved", offset);
300 301 302 303 304 305 306
          return TRUE;
        }
      else
        return FALSE;
    }
}

307 308 309 310 311
static AtkAttributeSet *
gtk_text_cell_accessible_get_run_attributes (AtkText *text,
                                             gint     offset,
                                             gint    *start_offset,
                                             gint    *end_offset)
312 313 314 315
{
  AtkAttributeSet *attrib_set = NULL;
  PangoLayout *layout;

316
  layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
317
  attrib_set = _gtk_pango_get_run_attributes  (NULL, layout, offset, start_offset, end_offset);
318
  g_object_unref (G_OBJECT (layout));
319

320 321 322
  return attrib_set;
}

323 324 325 326 327 328 329 330 331 332 333 334 335 336
static AtkAttributeSet *
add_attribute (AtkAttributeSet  *attributes,
               AtkTextAttribute  attr,
               const gchar      *value)
{
  AtkAttribute *at;

  at = g_new (AtkAttribute, 1);
  at->name = g_strdup (atk_text_attribute_get_name (attr));
  at->value = g_strdup (value);

  return g_slist_prepend (attributes, at);
}

337 338
static AtkAttributeSet *
gtk_text_cell_accessible_get_default_attributes (AtkText *text)
339 340 341 342 343
{
  AtkAttributeSet *attrib_set = NULL;
  PangoLayout *layout;
  GtkWidget *widget;

344 345
  layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
  widget = get_widget (GTK_TEXT_CELL_ACCESSIBLE (text));
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

  attrib_set = add_attribute (attrib_set, ATK_TEXT_ATTR_DIRECTION,
                   atk_text_attribute_get_value (ATK_TEXT_ATTR_DIRECTION,
                                                 gtk_widget_get_direction (widget)));
  attrib_set = _gtk_pango_get_default_attributes (NULL, layout);

  attrib_set = _gtk_style_context_get_attributes (attrib_set,
                                                  gtk_widget_get_style_context (widget),
                                                  gtk_widget_get_state_flags (widget));

  g_object_unref (G_OBJECT (layout));

  return attrib_set;
}

GtkWidget *
362
get_widget (GtkTextCellAccessible *text)
363 364
{
  AtkObject *parent;
365 366

  parent = atk_object_get_parent (ATK_OBJECT (text));
367
  if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
368 369
    parent = atk_object_get_parent (parent);

370
  return gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
371 372
}

373 374 375 376 377 378
/* This function is used by gtk_text_cell_accessible_get_offset_at_point()
 * and gtk_text_cell_accessible_get_character_extents(). There is no
 * cached PangoLayout so we must create a temporary one using this function.
 */
static PangoLayout *
create_pango_layout (GtkTextCellAccessible *text)
379
{
380
  GdkRGBA *foreground_rgba;
381
  PangoAttrList *attr_list, *attributes;
382
  PangoLayout *layout;
383
  PangoUnderline uline, underline;
384
  PangoFontMask mask;
385 386 387 388 389 390
  PangoFontDescription *font_desc;
  gboolean foreground_set, strikethrough_set, strikethrough;
  gboolean scale_set, underline_set, rise_set;
  gchar *renderer_text;
  gdouble scale;
  gint rise;
391
  GtkRendererCellAccessible *gail_renderer;
392 393
  GtkCellRendererText *gtk_renderer;

394
  gail_renderer = GTK_RENDERER_CELL_ACCESSIBLE (text);
395
  gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
396 397 398 399 400

  g_object_get (gtk_renderer,
                "text", &renderer_text,
                "attributes", &attributes,
                "foreground-set", &foreground_set,
Javier Jardón's avatar
Javier Jardón committed
401
                "foreground-rgba", &foreground_rgba,
402 403 404 405 406 407 408 409 410 411 412
                "strikethrough-set", &strikethrough_set,
                "strikethrough", &strikethrough,
                "font-desc", &font_desc,
                "scale-set", &scale_set,
                "scale", &scale,
                "underline-set", &underline_set,
                "underline", &underline,
                "rise-set", &rise_set,
                "rise", &rise,
                NULL);

413
  layout = gtk_widget_create_pango_layout (get_widget (text), renderer_text);
414 415 416

  if (attributes)
    attr_list = pango_attr_list_copy (attributes);
417 418 419
  else
    attr_list = pango_attr_list_new ();

420
  if (foreground_set)
421
    {
422 423 424
      add_attr (attr_list, pango_attr_foreground_new (foreground_rgba->red * 65535,
                                                      foreground_rgba->green * 65535,
                                                      foreground_rgba->blue * 65535));
425 426
    }

427
  if (strikethrough_set)
428
    add_attr (attr_list,
429
              pango_attr_strikethrough_new (strikethrough));
430

431
  mask = pango_font_description_get_set_fields (font_desc);
432 433 434

  if (mask & PANGO_FONT_MASK_FAMILY)
    add_attr (attr_list,
435
      pango_attr_family_new (pango_font_description_get_family (font_desc)));
436 437

  if (mask & PANGO_FONT_MASK_STYLE)
438
    add_attr (attr_list, pango_attr_style_new (pango_font_description_get_style (font_desc)));
439 440

  if (mask & PANGO_FONT_MASK_VARIANT)
441
    add_attr (attr_list, pango_attr_variant_new (pango_font_description_get_variant (font_desc)));
442 443

  if (mask & PANGO_FONT_MASK_WEIGHT)
444
    add_attr (attr_list, pango_attr_weight_new (pango_font_description_get_weight (font_desc)));
445 446

  if (mask & PANGO_FONT_MASK_STRETCH)
447
    add_attr (attr_list, pango_attr_stretch_new (pango_font_description_get_stretch (font_desc)));
448 449

  if (mask & PANGO_FONT_MASK_SIZE)
450
    add_attr (attr_list, pango_attr_size_new (pango_font_description_get_size (font_desc)));
451

452 453
  if (scale_set && scale != 1.0)
    add_attr (attr_list, pango_attr_scale_new (scale));
454

455 456
  if (underline_set)
    uline = underline;
457 458 459 460 461
  else
    uline = PANGO_UNDERLINE_NONE;

  if (uline != PANGO_UNDERLINE_NONE)
    add_attr (attr_list,
462
              pango_attr_underline_new (underline));
463

464 465
  if (rise_set)
    add_attr (attr_list, pango_attr_rise_new (rise));
466 467 468 469 470

  pango_layout_set_attributes (layout, attr_list);
  pango_layout_set_width (layout, -1);
  pango_attr_list_unref (attr_list);

471 472 473
  pango_font_description_free (font_desc);
  pango_attr_list_unref (attributes);
  g_free (renderer_text);
474
  gdk_rgba_free (foreground_rgba);
475

476 477 478
  return layout;
}

479 480
static void
add_attr (PangoAttrList *attr_list,
481 482 483 484 485 486 487
         PangoAttribute *attr)
{
  attr->start_index = 0;
  attr->end_index = G_MAXINT;
  pango_attr_list_insert (attr_list, attr);
}

488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507

static void
get_origins (GtkWidget *widget,
             gint      *x_window,
             gint      *y_window,
             gint      *x_toplevel,
             gint      *y_toplevel)
{
  GdkWindow *window;

  if (GTK_IS_TREE_VIEW (widget))
    window = gtk_tree_view_get_bin_window (GTK_TREE_VIEW (widget));
  else
    window = gtk_widget_get_window (widget);

  gdk_window_get_origin (window, x_window, y_window);
  window = gdk_window_get_toplevel (gtk_widget_get_window (widget));
  gdk_window_get_origin (window, x_toplevel, y_toplevel);
}

508 509 510 511 512 513 514 515
static void
gtk_text_cell_accessible_get_character_extents (AtkText      *text,
                                                gint          offset,
                                                gint         *x,
                                                gint         *y,
                                                gint         *width,
                                                gint         *height,
                                                AtkCoordType  coords)
516
{
517
  GtkRendererCellAccessible *gail_renderer;
518
  GtkRequisition min_size;
519 520 521 522 523 524
  GtkCellRendererText *gtk_renderer;
  GdkRectangle rendered_rect;
  GtkWidget *widget;
  AtkObject *parent;
  PangoRectangle char_rect;
  PangoLayout *layout;
525
  gchar *renderer_text;
526
  gfloat xalign, yalign;
527
  gint x_offset, y_offset, index;
528
  gint xpad, ypad;
529
  gint x_window, y_window, x_toplevel, y_toplevel;
530

531
  if (!GTK_TEXT_CELL_ACCESSIBLE (text)->cell_text)
532 533 534 535
    {
      *x = *y = *height = *width = 0;
      return;
    }
536
  if (offset < 0 || offset >= GTK_TEXT_CELL_ACCESSIBLE (text)->cell_length)
537 538 539 540
    {
      *x = *y = *height = *width = 0;
      return;
    }
541
  gail_renderer = GTK_RENDERER_CELL_ACCESSIBLE (text);
542
  gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
543

544
  g_object_get (gtk_renderer, "text", &renderer_text, NULL);
545 546
  if (renderer_text == NULL)
    return;
547 548

  parent = atk_object_get_parent (ATK_OBJECT (text));
549
  if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
550
    parent = atk_object_get_parent (parent);
551
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
552 553 554 555
  g_return_if_fail (GTK_IS_CELL_ACCESSIBLE_PARENT (parent));
  _gtk_cell_accessible_parent_get_cell_area (GTK_CELL_ACCESSIBLE_PARENT (parent),
                                             GTK_CELL_ACCESSIBLE (text),
                                             &rendered_rect);
556

557 558 559
  gtk_cell_renderer_get_preferred_size (GTK_CELL_RENDERER (gtk_renderer),
                                        widget,
                                        &min_size, NULL);
560

561 562 563 564 565
  gtk_cell_renderer_get_alignment (GTK_CELL_RENDERER (gtk_renderer), &xalign, &yalign);
  if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
    xalign = 1.0 - xalign;
  x_offset = MAX (0, xalign * (rendered_rect.width - min_size.width));
  y_offset = MAX (0, yalign * (rendered_rect.height - min_size.height));
566

567
  layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
568

569
  index = g_utf8_offset_to_pointer (renderer_text, offset) - renderer_text;
570
  pango_layout_index_to_pos (layout, index, &char_rect);
571

572
  gtk_cell_renderer_get_padding (gail_renderer->renderer, &xpad, &ypad);
573 574 575 576 577

  get_origins (widget, &x_window, &y_window, &x_toplevel, &y_toplevel);

  *x = (char_rect.x / PANGO_SCALE) + x_offset + rendered_rect.x + xpad + x_window;
  *y = (char_rect.y / PANGO_SCALE) + y_offset + rendered_rect.y + ypad + y_window;
578 579 580
  *height = char_rect.height / PANGO_SCALE;
  *width = char_rect.width / PANGO_SCALE;

581 582 583 584 585 586 587 588 589 590 591 592
  if (coords == ATK_XY_WINDOW)
    {
      *x -= x_toplevel;
      *y -= y_toplevel;
    }
  else if (coords != ATK_XY_SCREEN)
    {
      *x = 0;
      *y = 0;
      *height = 0;
      *width = 0;
    }
593 594

  g_free (renderer_text);
595
  g_object_unref (layout);
596
}
597

598 599 600 601 602
static gint
gtk_text_cell_accessible_get_offset_at_point (AtkText      *text,
                                              gint          x,
                                              gint          y,
                                              AtkCoordType  coords)
603 604
{
  AtkObject *parent;
605
  GtkRendererCellAccessible *gail_renderer;
606
  GtkCellRendererText *gtk_renderer;
607
  GtkRequisition min_size;
608 609 610
  GtkWidget *widget;
  GdkRectangle rendered_rect;
  PangoLayout *layout;
611
  gchar *renderer_text;
612
  gfloat xalign, yalign;
613
  gint x_offset, y_offset, index;
614
  gint xpad, ypad;
615 616 617 618
  gint x_window, y_window, x_toplevel, y_toplevel;
  gint x_temp, y_temp;
  gboolean ret;

619
  if (!GTK_TEXT_CELL_ACCESSIBLE (text)->cell_text)
620 621
    return -1;

622
  gail_renderer = GTK_RENDERER_CELL_ACCESSIBLE (text);
623 624 625
  gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
  parent = atk_object_get_parent (ATK_OBJECT (text));

626 627 628 629 630 631 632
  g_object_get (gtk_renderer, "text", &renderer_text, NULL);
  if (text == NULL)
    {
      g_free (renderer_text);
      return -1;
    }

633
  if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
634 635
    parent = atk_object_get_parent (parent);

636
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
637

638 639 640 641
  g_return_val_if_fail (GTK_IS_CELL_ACCESSIBLE_PARENT (parent), -1);
  _gtk_cell_accessible_parent_get_cell_area (GTK_CELL_ACCESSIBLE_PARENT (parent),
                                             GTK_CELL_ACCESSIBLE (text),
                                             &rendered_rect);
642

643 644 645
  gtk_cell_renderer_get_preferred_size (GTK_CELL_RENDERER (gtk_renderer),
                                        widget,
                                        &min_size, NULL);
646 647 648
  gtk_cell_renderer_get_alignment (GTK_CELL_RENDERER (gtk_renderer), &xalign, &yalign);
  if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
    xalign = 1.0 - xalign;
649 650
  x_offset = MAX (0, xalign * (rendered_rect.width - min_size.width));
  y_offset = MAX (0, yalign * (rendered_rect.height - min_size.height));
651

652
  layout = create_pango_layout (GTK_TEXT_CELL_ACCESSIBLE (text));
653 654

  gtk_cell_renderer_get_padding (gail_renderer->renderer, &xpad, &ypad);
655 656 657 658 659 660 661

  get_origins (widget, &x_window, &y_window, &x_toplevel, &y_toplevel);

  x_temp =  x - (x_offset + rendered_rect.x + xpad) - x_window;
  y_temp =  y - (y_offset + rendered_rect.y + ypad) - y_window;
  if (coords == ATK_XY_WINDOW)
    {
662
      x_temp += x_toplevel;
663 664 665 666 667
      y_temp += y_toplevel;
    }
  else if (coords != ATK_XY_SCREEN)
    index = -1;

668
  ret = pango_layout_xy_to_index (layout,
669 670 671 672 673 674 675 676
                                  x_temp * PANGO_SCALE,
                                  y_temp * PANGO_SCALE,
                                  &index, NULL);
  if (!ret)
    {
      if (x_temp < 0 || y_temp < 0)
        index = 0;
      else
677
        index = -1;
678 679
    }

680 681 682 683
  g_object_unref (layout);
  if (index == -1)
    {
      if (coords == ATK_XY_WINDOW || coords == ATK_XY_SCREEN)
684 685 686 687 688 689 690 691 692 693 694
        {
          glong length;

          length = g_utf8_strlen (renderer_text, -1);
          g_free (renderer_text);

          return length;
        }

      g_free (renderer_text);

695
      return index;
696 697
    }
  else
698 699 700 701 702 703 704 705 706
    {
      glong offset;

      offset = g_utf8_pointer_to_offset (renderer_text,
                                         renderer_text + index);
      g_free (renderer_text);

      return offset;
    }
707 708
}

709 710 711
static gunichar
gtk_text_cell_accessible_get_character_at_offset (AtkText *text,
                                                  gint     offset)
712 713 714 715
{
  gchar *index;
  gchar *string;

716
  string = GTK_TEXT_CELL_ACCESSIBLE(text)->cell_text;
717 718 719 720 721 722 723 724 725 726 727 728

  if (!string)
    return '\0';

  if (offset >= g_utf8_strlen (string, -1))
    return '\0';

  index = g_utf8_offset_to_pointer (string, offset);

  return g_utf8_get_char (index);
}

729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
static void
atk_text_interface_init (AtkTextIface *iface)
{
  iface->get_text = gtk_text_cell_accessible_get_text;
  iface->get_character_at_offset = gtk_text_cell_accessible_get_character_at_offset;
  iface->get_text_before_offset = gtk_text_cell_accessible_get_text_before_offset;
  iface->get_text_at_offset = gtk_text_cell_accessible_get_text_at_offset;
  iface->get_text_after_offset = gtk_text_cell_accessible_get_text_after_offset;
  iface->get_character_count = gtk_text_cell_accessible_get_character_count;
  iface->get_caret_offset = gtk_text_cell_accessible_get_caret_offset;
  iface->set_caret_offset = gtk_text_cell_accessible_set_caret_offset;
  iface->get_run_attributes = gtk_text_cell_accessible_get_run_attributes;
  iface->get_default_attributes = gtk_text_cell_accessible_get_default_attributes;
  iface->get_character_extents = gtk_text_cell_accessible_get_character_extents;
  iface->get_offset_at_point = gtk_text_cell_accessible_get_offset_at_point;
}