gtkentryaccessible.c 30.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* GAIL - The GNOME Accessibility Implementation Library
 * Copyright 2001, 2002, 2003 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
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

20
#include "config.h"
21

22
#include <gtk/gtk.h>
23
#include "gtkpango.h"
24
#include "gtkentryaccessible.h"
25
#include "gtkcomboboxaccessible.h"
26 27 28

/* Callbacks */

29 30 31 32 33 34 35 36 37 38 39 40
static void     insert_text_cb             (GtkEditable        *editable,
                                            gchar              *new_text,
                                            gint                new_text_length,
                                            gint               *position);
static void     delete_text_cb             (GtkEditable        *editable,
                                            gint                start,
                                            gint                end);
static void     changed_cb                 (GtkEditable        *editable);

static gboolean check_for_selection_change (GtkEntryAccessible *entry,
                                            GtkEntry           *gtk_entry);

41

42 43 44 45
static void atk_editable_text_interface_init (AtkEditableTextIface *iface);
static void atk_text_interface_init          (AtkTextIface         *iface);
static void atk_action_interface_init        (AtkActionIface       *iface);

46

47
G_DEFINE_TYPE_WITH_CODE (GtkEntryAccessible, _gtk_entry_accessible, GTK_TYPE_WIDGET_ACCESSIBLE,
48 49 50
                         G_IMPLEMENT_INTERFACE (ATK_TYPE_EDITABLE_TEXT, atk_editable_text_interface_init)
                         G_IMPLEMENT_INTERFACE (ATK_TYPE_TEXT, atk_text_interface_init)
                         G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init))
51

52

53
static AtkStateSet *
54 55 56 57 58
gtk_entry_accessible_ref_state_set (AtkObject *accessible)
{
  AtkStateSet *state_set;
  gboolean value;
  GtkWidget *widget;
59

60 61 62 63
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
  if (widget == NULL)
    return NULL;

64
  state_set = ATK_OBJECT_CLASS (_gtk_entry_accessible_parent_class)->ref_state_set (accessible);
65 66 67 68 69

  g_object_get (G_OBJECT (widget), "editable", &value, NULL);
  if (value)
    atk_state_set_add_state (state_set, ATK_STATE_EDITABLE);
  atk_state_set_add_state (state_set, ATK_STATE_SINGLE_LINE);
70

71
  return state_set;
72 73
}

74 75
static AtkAttributeSet *
gtk_entry_accessible_get_attributes (AtkObject *accessible)
76
{
77 78 79 80 81
  GtkWidget *widget;
  AtkAttributeSet *attributes;
  AtkAttribute *placeholder_text;
  const gchar *text;

82
  attributes = ATK_OBJECT_CLASS (_gtk_entry_accessible_parent_class)->get_attributes (accessible);
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
  if (widget == NULL)
    return attributes;

  text = gtk_entry_get_placeholder_text (GTK_ENTRY (widget));
  if (text == NULL)
    return attributes;

  placeholder_text = g_malloc (sizeof (AtkAttribute));
  placeholder_text->name = g_strdup ("placeholder-text");
  placeholder_text->value = g_strdup (text);

  attributes = g_slist_append (attributes, placeholder_text);

  return attributes;
99 100 101
}

static void
102 103
gtk_entry_accessible_initialize (AtkObject *obj,
                                 gpointer   data)
104 105
{
  GtkEntry *entry;
106
  GtkEntryAccessible *gtk_entry_accessible;
107
  gint start_pos, end_pos;
108

109
  ATK_OBJECT_CLASS (_gtk_entry_accessible_parent_class)->initialize (obj, data);
110

111
  gtk_entry_accessible = GTK_ENTRY_ACCESSIBLE (obj);
112 113

  entry = GTK_ENTRY (data);
114 115
  gtk_editable_get_selection_bounds (GTK_EDITABLE (entry),
                                     &start_pos, &end_pos);
116 117
  gtk_entry_accessible->cursor_position = end_pos;
  gtk_entry_accessible->selection_bound = start_pos;
118 119

  /* Set up signal callbacks */
120 121 122
  g_signal_connect (entry, "insert-text", G_CALLBACK (insert_text_cb), NULL);
  g_signal_connect (entry, "delete-text", G_CALLBACK (delete_text_cb), NULL);
  g_signal_connect (entry, "changed", G_CALLBACK (changed_cb), NULL);
123

124
  if (gtk_entry_get_visibility (entry))
125 126 127 128 129 130
    obj->role = ATK_ROLE_TEXT;
  else
    obj->role = ATK_ROLE_PASSWORD_TEXT;
}

static void
131 132
gtk_entry_accessible_notify_gtk (GObject    *obj,
                                 GParamSpec *pspec)
133 134 135 136
{
  GtkWidget *widget;
  AtkObject* atk_obj;
  GtkEntry* gtk_entry;
137
  GtkEntryAccessible* entry;
138 139 140 141

  widget = GTK_WIDGET (obj);
  atk_obj = gtk_widget_get_accessible (widget);
  gtk_entry = GTK_ENTRY (widget);
142
  entry = GTK_ENTRY_ACCESSIBLE (atk_obj);
143

Matthias Clasen's avatar
Matthias Clasen committed
144
  if (g_strcmp0 (pspec->name, "cursor-position") == 0)
145 146 147 148 149 150
    {
      if (check_for_selection_change (entry, gtk_entry))
        g_signal_emit_by_name (atk_obj, "text_selection_changed");
      /*
       * The entry cursor position has moved so generate the signal.
       */
151
      g_signal_emit_by_name (atk_obj, "text_caret_moved",
152 153
                             entry->cursor_position);
    }
Matthias Clasen's avatar
Matthias Clasen committed
154
  else if (g_strcmp0 (pspec->name, "selection-bound") == 0)
155 156 157 158
    {
      if (check_for_selection_change (entry, gtk_entry))
        g_signal_emit_by_name (atk_obj, "text_selection_changed");
    }
Matthias Clasen's avatar
Matthias Clasen committed
159
  else if (g_strcmp0 (pspec->name, "editable") == 0)
160 161 162 163
    {
      gboolean value;

      g_object_get (obj, "editable", &value, NULL);
164
      atk_object_notify_state_change (atk_obj, ATK_STATE_EDITABLE, value);
165
    }
Matthias Clasen's avatar
Matthias Clasen committed
166
  else if (g_strcmp0 (pspec->name, "visibility") == 0)
167 168 169 170 171 172 173 174 175
    {
      gboolean visibility;
      AtkRole new_role;

      visibility = gtk_entry_get_visibility (gtk_entry);
      new_role = visibility ? ATK_ROLE_TEXT : ATK_ROLE_PASSWORD_TEXT;
      atk_object_set_role (atk_obj, new_role);
    }
  else
176
    GTK_WIDGET_ACCESSIBLE_CLASS (_gtk_entry_accessible_parent_class)->notify_gtk (obj, pspec);
177 178 179
}

static gint
180
gtk_entry_accessible_get_index_in_parent (AtkObject *accessible)
181 182 183 184 185 186
{
  /*
   * If the parent widget is a combo box then the index is 1
   * otherwise do the normal thing.
   */
  if (accessible->accessible_parent)
187
    if (GTK_IS_COMBO_BOX_ACCESSIBLE (accessible->accessible_parent))
188 189
      return 1;

190
  return ATK_OBJECT_CLASS (_gtk_entry_accessible_parent_class)->get_index_in_parent (accessible);
191 192
}

193
static void
194
_gtk_entry_accessible_class_init (GtkEntryAccessibleClass *klass)
195
{
196
  AtkObjectClass  *class = ATK_OBJECT_CLASS (klass);
197
  GtkWidgetAccessibleClass *widget_class = (GtkWidgetAccessibleClass*)klass;
198

199 200 201 202
  class->ref_state_set = gtk_entry_accessible_ref_state_set;
  class->get_index_in_parent = gtk_entry_accessible_get_index_in_parent;
  class->initialize = gtk_entry_accessible_initialize;
  class->get_attributes = gtk_entry_accessible_get_attributes;
203

204
  widget_class->notify_gtk = gtk_entry_accessible_notify_gtk;
205 206
}

207
static void
208
_gtk_entry_accessible_init (GtkEntryAccessible *entry)
209
{
210 211
  entry->length_insert = 0;
  entry->length_delete = 0;
212 213
  entry->cursor_position = 0;
  entry->selection_bound = 0;
214 215
}

216 217 218 219
static gchar *
gtk_entry_accessible_get_text (AtkText *atk_text,
                               gint     start_pos,
                               gint     end_pos)
220 221
{
  GtkWidget *widget;
222
  const gchar *text;
223

224
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (atk_text));
225 226 227
  if (widget == NULL)
    return NULL;

228 229 230 231 232 233 234 235 236 237
  /* FIXME: is this acceptable ? */
  if (!gtk_entry_get_visibility (GTK_ENTRY (widget)))
    return g_strdup ("");

  text = gtk_entry_get_text (GTK_ENTRY (widget));

  if (text)
    return g_utf8_substring (text, start_pos, end_pos > -1 ? end_pos : g_utf8_strlen (text, -1));

  return NULL;
238 239
}

240 241 242 243 244 245
static gchar *
gtk_entry_accessible_get_text_before_offset (AtkText         *text,
                                             gint             offset,
                                             AtkTextBoundary  boundary_type,
                                             gint            *start_offset,
                                             gint            *end_offset)
246 247 248
{
  GtkWidget *widget;

249
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
250 251 252
  if (widget == NULL)
    return NULL;

253 254 255
  /* FIXME: is this acceptable ? */
  if (!gtk_entry_get_visibility (GTK_ENTRY (widget)))
    return g_strdup ("");
256

257 258 259
  return _gtk_pango_get_text_before (gtk_entry_get_layout (GTK_ENTRY (widget)),
                                     boundary_type, offset,
                                     start_offset, end_offset);
260 261
}

262 263 264 265 266 267
static gchar *
gtk_entry_accessible_get_text_at_offset (AtkText         *text,
                                         gint             offset,
                                         AtkTextBoundary  boundary_type,
                                         gint            *start_offset,
                                         gint            *end_offset)
268 269 270
{
  GtkWidget *widget;

271
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
272 273 274
  if (widget == NULL)
    return NULL;

275 276 277
  /* FIXME: is this acceptable ? */
  if (!gtk_entry_get_visibility (GTK_ENTRY (widget)))
    return g_strdup ("");
278

279 280 281
  return _gtk_pango_get_text_at (gtk_entry_get_layout (GTK_ENTRY (widget)),
                                 boundary_type, offset,
                                 start_offset, end_offset);
282 283
}

284 285 286 287 288 289
static gchar *
gtk_entry_accessible_get_text_after_offset (AtkText         *text,
                                            gint             offset,
                                            AtkTextBoundary  boundary_type,
                                            gint            *start_offset,
                                            gint            *end_offset)
290 291 292
{
  GtkWidget *widget;

293
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
294 295 296
  if (widget == NULL)
    return NULL;

297 298 299
  /* FIXME: is this acceptable ? */
  if (!gtk_entry_get_visibility (GTK_ENTRY (widget)))
    return g_strdup ("");
300

301 302 303
  return _gtk_pango_get_text_after (gtk_entry_get_layout (GTK_ENTRY (widget)),
                                    boundary_type, offset,
                                    start_offset, end_offset);
304 305 306
}

static gint
307
gtk_entry_accessible_get_character_count (AtkText *atk_text)
308 309
{
  GtkWidget *widget;
310
  const gchar *text;
311

312
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (atk_text));
313 314 315
  if (widget == NULL)
    return 0;

316 317 318 319 320 321
  text = gtk_entry_get_text (GTK_ENTRY (widget));

  if (text)
    return g_utf8_strlen (text, -1);

  return 0;
322 323 324
}

static gint
325
gtk_entry_accessible_get_caret_offset (AtkText *text)
326 327 328
{
  GtkWidget *widget;

329
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
330 331 332
  if (widget == NULL)
    return 0;

333
  return gtk_editable_get_position (GTK_EDITABLE (widget));
334 335 336
}

static gboolean
337 338
gtk_entry_accessible_set_caret_offset (AtkText *text,
                                       gint     offset)
339 340 341
{
  GtkWidget *widget;

342
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
343 344 345
  if (widget == NULL)
    return FALSE;

346
  gtk_editable_set_position (GTK_EDITABLE (widget), offset);
347 348 349 350

  return TRUE;
}

351
static AtkAttributeSet *
352 353 354
add_text_attribute (AtkAttributeSet  *attributes,
                    AtkTextAttribute  attr,
                    gint              i)
355 356 357 358 359
{
  AtkAttribute *at;

  at = g_new (AtkAttribute, 1);
  at->name = g_strdup (atk_text_attribute_get_name (attr));
360
  at->value = g_strdup (atk_text_attribute_get_value (attr, i));
361 362 363 364 365

  return g_slist_prepend (attributes, at);
}

static AtkAttributeSet *
366
gtk_entry_accessible_get_run_attributes (AtkText *text,
367 368 369
                                         gint     offset,
                                         gint    *start_offset,
                                         gint    *end_offset)
370 371
{
  GtkWidget *widget;
372
  AtkAttributeSet *attributes;
373

374
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
375 376 377
  if (widget == NULL)
    return NULL;

378
  attributes = NULL;
379 380
  attributes = add_text_attribute (attributes, ATK_TEXT_ATTR_DIRECTION,
                                   gtk_widget_get_direction (widget));
381 382 383 384 385
  attributes = _gtk_pango_get_run_attributes (attributes,
                                              gtk_entry_get_layout (GTK_ENTRY (widget)),
                                              offset,
                                              start_offset,
                                              end_offset);
386

387
  return attributes;
388 389
}

390
static AtkAttributeSet *
391
gtk_entry_accessible_get_default_attributes (AtkText *text)
392 393
{
  GtkWidget *widget;
394
  AtkAttributeSet *attributes;
395

396
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
397 398 399
  if (widget == NULL)
    return NULL;

400
  attributes = NULL;
401 402
  attributes = add_text_attribute (attributes, ATK_TEXT_ATTR_DIRECTION,
                                   gtk_widget_get_direction (widget));
403 404 405 406 407
  attributes = _gtk_pango_get_default_attributes (attributes,
                                                  gtk_entry_get_layout (GTK_ENTRY (widget)));
  attributes = _gtk_style_context_get_attributes (attributes,
                                                  gtk_widget_get_style_context (widget),
                                                  gtk_widget_get_state_flags (widget));
408

409
  return attributes;
410
}
411

412
static void
413 414 415 416 417 418 419
gtk_entry_accessible_get_character_extents (AtkText      *text,
                                            gint          offset,
                                            gint         *x,
                                            gint         *y,
                                            gint         *width,
                                            gint         *height,
                                            AtkCoordType  coords)
420 421 422 423
{
  GtkWidget *widget;
  GtkEntry *entry;
  PangoRectangle char_rect;
424
  const gchar *entry_text;
425 426 427
  gint index, x_layout, y_layout;
  GdkWindow *window;
  gint x_window, y_window;
428

429
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
430 431 432 433 434 435
  if (widget == NULL)
    return;

  entry = GTK_ENTRY (widget);

  gtk_entry_get_layout_offsets (entry, &x_layout, &y_layout);
436 437
  entry_text = gtk_entry_get_text (entry);
  index = g_utf8_offset_to_pointer (entry_text, offset) - entry_text;
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
  pango_layout_index_to_pos (gtk_entry_get_layout (entry), index, &char_rect);
  pango_extents_to_pixels (&char_rect, NULL);

  window = gtk_widget_get_window (widget);
  gdk_window_get_origin (window, &x_window, &y_window);

  *x = x_window + x_layout + char_rect.x;
  *y = x_window + y_layout + char_rect.y;
  *width = char_rect.width;
  *height = char_rect.height;

  if (coords == ATK_XY_WINDOW)
    {
      window = gdk_window_get_toplevel (window);
      gdk_window_get_origin (window, &x_window, &y_window);

      *x -= x_window;
      *y -= y_window;
    }
}
458

459 460 461 462 463 464
static gint
gtk_entry_accessible_get_offset_at_point (AtkText      *atk_text,
                                          gint          x,
                                          gint          y,
                                          AtkCoordType  coords)
{
465 466
  GtkWidget *widget;
  GtkEntry *entry;
467
  const gchar *text;
468
  gint index, x_layout, y_layout;
469 470 471
  gint x_window, y_window;
  gint x_local, y_local;
  GdkWindow *window;
472

473
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (atk_text));
474 475 476 477
  if (widget == NULL)
    return -1;

  entry = GTK_ENTRY (widget);
478

479
  gtk_entry_get_layout_offsets (entry, &x_layout, &y_layout);
480 481 482 483 484 485 486 487

  window = gtk_widget_get_window (widget);
  gdk_window_get_origin (window, &x_window, &y_window);

  x_local = x - x_layout - x_window;
  y_local = y - y_layout - y_window;

  if (coords == ATK_XY_WINDOW)
488
    {
489 490
      window = gdk_window_get_toplevel (window);
      gdk_window_get_origin (window, &x_window, &y_window);
491

492 493
      x_local += x_window;
      y_local += y_window;
494
    }
495 496 497 498
  if (!pango_layout_xy_to_index (gtk_entry_get_layout (entry),
                                 x_local * PANGO_SCALE,
                                 y_local * PANGO_SCALE,
                                 &index, NULL))
499
    {
500 501 502 503 504
      if (x_local < 0 || y_local < 0)
        index = 0;
      else
        index = -1;
    }
505

506 507 508 509
  if (index != -1)
    {
      text = gtk_entry_get_text (entry);
      return g_utf8_pointer_to_offset (text, text + index);
510
    }
511 512

  return -1;
513 514 515
}

static gint
516
gtk_entry_accessible_get_n_selections (AtkText *text)
517 518
{
  GtkWidget *widget;
519
  gint start, end;
520

521
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
522
  if (widget == NULL)
523
    return 0;
524

525
  if (gtk_editable_get_selection_bounds (GTK_EDITABLE (widget), &start, &end))
526
    return 1;
527 528

  return 0;
529 530
}

531
static gchar *
532
gtk_entry_accessible_get_selection (AtkText *text,
533 534 535
                                    gint     selection_num,
                                    gint    *start_pos,
                                    gint    *end_pos)
536 537 538
{
  GtkWidget *widget;

539
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
540 541 542 543 544 545
  if (widget == NULL)
    return NULL;

  if (selection_num != 0)
     return NULL;

546 547
  if (gtk_editable_get_selection_bounds (GTK_EDITABLE (widget), start_pos, end_pos))
    return gtk_editable_get_chars (GTK_EDITABLE (widget), *start_pos, *end_pos);
548

549
  return NULL;
550 551 552
}

static gboolean
553
gtk_entry_accessible_add_selection (AtkText *text,
554 555
                                    gint     start_pos,
                                    gint     end_pos)
556 557 558
{
  GtkEntry *entry;
  GtkWidget *widget;
559
  gint start, end;
560

561
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
562 563 564 565 566
  if (widget == NULL)
    return FALSE;

  entry = GTK_ENTRY (widget);

567
  if (!gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
568
    {
569 570
      gtk_editable_select_region (GTK_EDITABLE (entry), start_pos, end_pos);
      return TRUE;
571 572
    }
  else
573
    return FALSE;
574 575 576
}

static gboolean
577
gtk_entry_accessible_remove_selection (AtkText *text,
578
                                       gint     selection_num)
579 580
{
  GtkWidget *widget;
Matthias Clasen's avatar
Matthias Clasen committed
581
  gint start, end;
582

583
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
584 585 586 587 588 589
  if (widget == NULL)
    return FALSE;

  if (selection_num != 0)
     return FALSE;

590
  if (gtk_editable_get_selection_bounds (GTK_EDITABLE (widget), &start, &end))
591
    {
Matthias Clasen's avatar
Matthias Clasen committed
592
      gtk_editable_select_region (GTK_EDITABLE (widget), end, end);
593 594 595 596 597 598 599
      return TRUE;
    }
  else
    return FALSE;
}

static gboolean
600
gtk_entry_accessible_set_selection (AtkText *text,
601 602 603
                                    gint     selection_num,
                                    gint     start_pos,
                                    gint     end_pos)
604 605
{
  GtkWidget *widget;
606
  gint start, end;
607

608
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
609 610 611 612 613 614
  if (widget == NULL)
    return FALSE;

  if (selection_num != 0)
     return FALSE;

615
  if (gtk_editable_get_selection_bounds (GTK_EDITABLE (widget), &start, &end))
616
    {
617
      gtk_editable_select_region (GTK_EDITABLE (widget), start_pos, end_pos);
618 619 620 621 622 623
      return TRUE;
    }
  else
    return FALSE;
}

624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
static gunichar
gtk_entry_accessible_get_character_at_offset (AtkText *atk_text,
                                              gint     offset)
{
  GtkWidget *widget;
  const gchar *text;
  gchar *index;

  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (atk_text));
  if (widget == NULL)
    return '\0';

  if (!gtk_entry_get_visibility (GTK_ENTRY (widget)))
    return '\0';

  text = gtk_entry_get_text (GTK_ENTRY (widget));
  if (offset >= g_utf8_strlen (text, -1))
    return '\0';

  index = g_utf8_offset_to_pointer (text, offset);

  return g_utf8_get_char (index);
}

648
static void
649
atk_text_interface_init (AtkTextIface *iface)
650
{
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
  iface->get_text = gtk_entry_accessible_get_text;
  iface->get_character_at_offset = gtk_entry_accessible_get_character_at_offset;
  iface->get_text_before_offset = gtk_entry_accessible_get_text_before_offset;
  iface->get_text_at_offset = gtk_entry_accessible_get_text_at_offset;
  iface->get_text_after_offset = gtk_entry_accessible_get_text_after_offset;
  iface->get_caret_offset = gtk_entry_accessible_get_caret_offset;
  iface->set_caret_offset = gtk_entry_accessible_set_caret_offset;
  iface->get_character_count = gtk_entry_accessible_get_character_count;
  iface->get_n_selections = gtk_entry_accessible_get_n_selections;
  iface->get_selection = gtk_entry_accessible_get_selection;
  iface->add_selection = gtk_entry_accessible_add_selection;
  iface->remove_selection = gtk_entry_accessible_remove_selection;
  iface->set_selection = gtk_entry_accessible_set_selection;
  iface->get_run_attributes = gtk_entry_accessible_get_run_attributes;
  iface->get_default_attributes = gtk_entry_accessible_get_default_attributes;
  iface->get_character_extents = gtk_entry_accessible_get_character_extents;
  iface->get_offset_at_point = gtk_entry_accessible_get_offset_at_point;
668 669 670
}

static void
671
gtk_entry_accessible_set_text_contents (AtkEditableText *text,
672
                                        const gchar     *string)
673 674 675
{
  GtkWidget *widget;

676
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
677 678 679
  if (widget == NULL)
    return;

680
  if (!gtk_editable_get_editable (GTK_EDITABLE (widget)))
681 682
    return;

683
  gtk_entry_set_text (GTK_ENTRY (widget), string);
684 685 686
}

static void
687
gtk_entry_accessible_insert_text (AtkEditableText *text,
688 689 690
                                  const gchar     *string,
                                  gint             length,
                                  gint            *position)
691 692 693 694
{
  GtkWidget *widget;
  GtkEditable *editable;

695
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
696 697 698
  if (widget == NULL)
    return;

699
  editable = GTK_EDITABLE (widget);
700 701 702 703
  if (!gtk_editable_get_editable (editable))
    return;

  gtk_editable_insert_text (editable, string, length, position);
704
  gtk_editable_set_position (editable, *position);
705 706 707
}

static void
708 709 710
gtk_entry_accessible_copy_text (AtkEditableText *text,
                                gint             start_pos,
                                gint             end_pos)
711 712 713 714
{
  GtkWidget *widget;
  GtkEditable *editable;
  gchar *str;
715
  GtkClipboard *clipboard;
716

717
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
718 719 720
  if (widget == NULL)
    return;

721 722 723 724
  if (!gtk_widget_has_screen (widget))
    return;

  editable = GTK_EDITABLE (widget);
725
  str = gtk_editable_get_chars (editable, start_pos, end_pos);
726
  clipboard = gtk_widget_get_clipboard (widget, GDK_SELECTION_CLIPBOARD);
727
  gtk_clipboard_set_text (clipboard, str, -1);
728
  g_free (str);
729 730 731
}

static void
732
gtk_entry_accessible_cut_text (AtkEditableText *text,
733 734
                               gint             start_pos,
                               gint             end_pos)
735 736 737 738
{
  GtkWidget *widget;
  GtkEditable *editable;
  gchar *str;
739
  GtkClipboard *clipboard;
740

741
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
742 743 744
  if (widget == NULL)
    return;

745 746 747 748
  if (!gtk_widget_has_screen (widget))
    return;

  editable = GTK_EDITABLE (widget);
749 750
  if (!gtk_editable_get_editable (editable))
    return;
751

752
  str = gtk_editable_get_chars (editable, start_pos, end_pos);
753
  clipboard = gtk_widget_get_clipboard (widget, GDK_SELECTION_CLIPBOARD);
754
  gtk_clipboard_set_text (clipboard, str, -1);
755 756 757 758
  gtk_editable_delete_text (editable, start_pos, end_pos);
}

static void
759
gtk_entry_accessible_delete_text (AtkEditableText *text,
760 761
                                  gint             start_pos,
                                  gint             end_pos)
762 763 764 765
{
  GtkWidget *widget;
  GtkEditable *editable;

766
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
767 768 769
  if (widget == NULL)
    return;

770
  editable = GTK_EDITABLE (widget);
771 772 773 774 775 776
  if (!gtk_editable_get_editable (editable))
    return;

  gtk_editable_delete_text (editable, start_pos, end_pos);
}

777 778 779 780 781 782
typedef struct
{
  GtkEntry* entry;
  gint position;
} PasteData;

783
static void
784 785 786
paste_received_cb (GtkClipboard *clipboard,
                   const gchar  *text,
                   gpointer      data)
787
{
788
  PasteData *paste = data;
789 790 791 792 793 794 795 796 797

  if (text)
    gtk_editable_insert_text (GTK_EDITABLE (paste->entry), text, -1,
                              &paste->position);

  g_object_unref (paste->entry);
  g_free (paste);
}

798
static void
799
gtk_entry_accessible_paste_text (AtkEditableText *text,
800
                                 gint             position)
801 802 803
{
  GtkWidget *widget;
  GtkEditable *editable;
804
  PasteData *paste;
805
  GtkClipboard *clipboard;
806

807
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
808
  if (widget == NULL)
809 810 811
    return;

  if (!gtk_widget_has_screen (widget))
812 813 814 815 816
    return;

  editable = GTK_EDITABLE (widget);
  if (!gtk_editable_get_editable (editable))
    return;
817

818
  paste = g_new0 (PasteData, 1);
819 820 821 822 823
  paste->entry = GTK_ENTRY (widget);
  paste->position = position;

  g_object_ref (paste->entry);
  clipboard = gtk_widget_get_clipboard (widget, GDK_SELECTION_CLIPBOARD);
824
  gtk_clipboard_request_text (clipboard, paste_received_cb, paste);
825 826 827
}

static void
828
atk_editable_text_interface_init (AtkEditableTextIface *iface)
829
{
830 831 832 833 834 835 836
  iface->set_text_contents = gtk_entry_accessible_set_text_contents;
  iface->insert_text = gtk_entry_accessible_insert_text;
  iface->copy_text = gtk_entry_accessible_copy_text;
  iface->cut_text = gtk_entry_accessible_cut_text;
  iface->delete_text = gtk_entry_accessible_delete_text;
  iface->paste_text = gtk_entry_accessible_paste_text;
  iface->set_run_attributes = NULL;
837 838
}

839 840 841 842 843
/* We connect to GtkEditable::insert-text, since it carries
 * the information we need. But we delay emitting our own
 * text_changed::insert signal until the entry has update
 * all its internal state and emits GtkEntry::changed.
 */
844
static void
845 846 847 848
insert_text_cb (GtkEditable *editable,
                gchar       *new_text,
                gint         new_text_length,
                gint        *position)
849
{
850
  GtkEntryAccessible *accessible;
Li Yuan's avatar
Li Yuan committed
851

852
  if (new_text_length == 0)
Li Yuan's avatar
Li Yuan committed
853 854
    return;

855
  accessible = GTK_ENTRY_ACCESSIBLE (gtk_widget_get_accessible (GTK_WIDGET (editable)));
856
  if (accessible->length_insert == 0)
857
    {
858 859
      accessible->position_insert = *position;
      accessible->length_insert = g_utf8_strlen (new_text, new_text_length);
860 861 862
    }
}

863 864 865 866
/* We connect to GtkEditable::delete-text, since it carries
 * the information we need. But we delay emitting our own
 * text_changed::delete signal until the entry has update
 * all its internal state and emits GtkEntry::changed.
867
 */
868
static void
869 870 871
delete_text_cb (GtkEditable *editable,
                gint         start,
                gint         end)
872
{
873
  GtkEntryAccessible *accessible;
874

875
  if (end < 0)
876
    {
877 878
      const gchar *text;

879
      text = gtk_entry_get_text (GTK_ENTRY (editable));
880
      end = g_utf8_strlen (text, -1);
881 882
    }

883 884 885
  if (end == start)
    return;

886
  accessible = GTK_ENTRY_ACCESSIBLE (gtk_widget_get_accessible (GTK_WIDGET (editable)));
887
  if (accessible->length_delete == 0)
888
    {
889 890
      accessible->position_delete = start;
      accessible->length_delete = end - start;
891 892 893
    }
}

894 895 896 897
/* Note the assumption here: A single ::changed emission
 * will only collect a single deletion/insertion, and there
 * won't be multiple insertions or deletions in a single
 * change.
898
 */
899
static void
900
changed_cb (GtkEditable *editable)
901
{
902
  GtkEntryAccessible *accessible;
903

904
  accessible = GTK_ENTRY_ACCESSIBLE (gtk_widget_get_accessible (GTK_WIDGET (editable)));
905

906
  if (accessible->length_delete > 0)
907
    {
908 909 910 911 912 913 914 915 916 917 918 919 920
      g_signal_emit_by_name (accessible,
                             "text_changed::delete",
                             accessible->position_delete,
                             accessible->length_delete);
      accessible->length_delete = 0;
    }
  if (accessible->length_insert > 0)
    {
      g_signal_emit_by_name (accessible,
                             "text_changed::insert",
                             accessible->position_insert,
                             accessible->length_insert);
      accessible->length_insert = 0;
921 922 923
    }
}

924
static gboolean
925 926
check_for_selection_change (GtkEntryAccessible *accessible,
                            GtkEntry           *entry)
927
{
928 929
  gboolean ret_val = FALSE;
  gint start, end;
930

931
  if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
932
    {
933 934
      if (end != accessible->cursor_position ||
          start != accessible->selection_bound)
935 936 937
        /*
         * This check is here as this function can be called
         * for notification of selection_bound and current_pos.
938
         * The values of current_pos and selection_bound may be the same
939 940 941 942 943
         * for both notifications and we only want to generate one
         * text_selection_changed signal.
         */
        ret_val = TRUE;
    }
944
  else
945 946
    {
      /* We had a selection */
947
      ret_val = (accessible->cursor_position != accessible->selection_bound);
948
    }
949 950 951

  accessible->cursor_position = end;
  accessible->selection_bound = start;
952 953 954 955 956

  return ret_val;
}

static gboolean
957
gtk_entry_accessible_do_action (AtkAction *action,
958
                                gint       i)
959 960 961
{
  GtkWidget *widget;

962
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (action));
963 964 965
  if (widget == NULL)
    return FALSE;

966
  if (!gtk_widget_get_sensitive (widget) || !gtk_widget_get_visible (widget))
967 968
    return FALSE;

969 970 971
  if (i != 0)
    return FALSE;

972
  gtk_widget_activate (widget);
973 974

  return TRUE;
975 976 977
}

static gint
978
gtk_entry_accessible_get_n_actions (AtkAction *action)
979 980 981 982
{
  return 1;
}

983
static const gchar *
984
gtk_entry_accessible_get_keybinding (AtkAction *action,
985
                                     gint       i)
986
{
987 988 989 990 991 992 993 994 995 996
  GtkWidget *widget;
  GtkWidget *label;
  AtkRelationSet *set;
  AtkRelation *relation;
  GPtrArray *target;
  gpointer target_object;
  guint key_val;

  if (i != 0)
    return NULL;
997

998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (action));
  if (widget == NULL)
    return NULL;

  set = atk_object_ref_relation_set (ATK_OBJECT (action));
  if (!set)
    return NULL;

  label = NULL;
  relation = atk_relation_set_get_relation_by_type (set, ATK_RELATION_LABELLED_BY);
  if (relation)
1009
    {
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
      target = atk_relation_get_target (relation);

      target_object = g_ptr_array_index (target, 0);
      label = gtk_accessible_get_widget (GTK_ACCESSIBLE (target_object));
    }

  g_object_unref (set);

  if (GTK_IS_LABEL (label))
    {
      key_val = gtk_label_get_mnemonic_keyval (GTK_LABEL (label));
      if (key_val != GDK_KEY_VoidSymbol)
        return gtk_accelerator_name (key_val, GDK_MOD1_MASK);
1023
    }
1024 1025

  return NULL;
1026 1027
}

1028
static const gchar*
1029
gtk_entry_accessible_action_get_name (AtkAction *action,
1030
                                      gint       i)
1031
{
1032 1033
  if (i != 0)
    return NULL;
1034

1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
  return "activate";
}

static void
atk_action_interface_init (AtkActionIface *iface)
{
  iface->do_action = gtk_entry_accessible_do_action;
  iface->get_n_actions = gtk_entry_accessible_get_n_actions;
  iface->get_keybinding = gtk_entry_accessible_get_keybinding;
  iface->get_name = gtk_entry_accessible_action_get_name;
1045
}