]> Pileus Git - ~andy/gtk/blob - gtk/a11y/gtkentryaccessible.c
Change FSF Address
[~andy/gtk] / gtk / a11y / gtkentryaccessible.c
1 /* GAIL - The GNOME Accessibility Implementation Library
2  * Copyright 2001, 2002, 2003 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 "gtkentryaccessible.h"
23 #include "gtkcomboboxaccessible.h"
24
25 /* Callbacks */
26
27 static void     insert_text_cb             (GtkEditable        *editable,
28                                             gchar              *new_text,
29                                             gint                new_text_length,
30                                             gint               *position);
31 static void     delete_text_cb             (GtkEditable        *editable,
32                                             gint                start,
33                                             gint                end);
34
35 static gboolean check_for_selection_change (GtkEntryAccessible *entry,
36                                             GtkEntry           *gtk_entry);
37
38
39 static void atk_editable_text_interface_init (AtkEditableTextIface *iface);
40 static void atk_text_interface_init          (AtkTextIface         *iface);
41 static void atk_action_interface_init        (AtkActionIface       *iface);
42
43
44 G_DEFINE_TYPE_WITH_CODE (GtkEntryAccessible, _gtk_entry_accessible, GTK_TYPE_WIDGET_ACCESSIBLE,
45                          G_IMPLEMENT_INTERFACE (ATK_TYPE_EDITABLE_TEXT, atk_editable_text_interface_init)
46                          G_IMPLEMENT_INTERFACE (ATK_TYPE_TEXT, atk_text_interface_init)
47                          G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init))
48
49
50 static AtkStateSet *
51 gtk_entry_accessible_ref_state_set (AtkObject *accessible)
52 {
53   AtkStateSet *state_set;
54   gboolean value;
55   GtkWidget *widget;
56
57   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
58   if (widget == NULL)
59     return NULL;
60
61   state_set = ATK_OBJECT_CLASS (_gtk_entry_accessible_parent_class)->ref_state_set (accessible);
62
63   g_object_get (G_OBJECT (widget), "editable", &value, NULL);
64   if (value)
65     atk_state_set_add_state (state_set, ATK_STATE_EDITABLE);
66   atk_state_set_add_state (state_set, ATK_STATE_SINGLE_LINE);
67
68   return state_set;
69 }
70
71 static AtkAttributeSet *
72 gtk_entry_accessible_get_attributes (AtkObject *accessible)
73 {
74   GtkWidget *widget;
75   AtkAttributeSet *attributes;
76   AtkAttribute *placeholder_text;
77   const gchar *text;
78
79   attributes = ATK_OBJECT_CLASS (_gtk_entry_accessible_parent_class)->get_attributes (accessible);
80
81   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
82   if (widget == NULL)
83     return attributes;
84
85   text = gtk_entry_get_placeholder_text (GTK_ENTRY (widget));
86   if (text == NULL)
87     return attributes;
88
89   placeholder_text = g_malloc (sizeof (AtkAttribute));
90   placeholder_text->name = g_strdup ("placeholder-text");
91   placeholder_text->value = g_strdup (text);
92
93   attributes = g_slist_append (attributes, placeholder_text);
94
95   return attributes;
96 }
97
98 static void
99 gtk_entry_accessible_initialize (AtkObject *obj,
100                                  gpointer   data)
101 {
102   GtkEntry *entry;
103   GtkEntryAccessible *gtk_entry_accessible;
104   gint start_pos, end_pos;
105
106   ATK_OBJECT_CLASS (_gtk_entry_accessible_parent_class)->initialize (obj, data);
107
108   gtk_entry_accessible = GTK_ENTRY_ACCESSIBLE (obj);
109
110   entry = GTK_ENTRY (data);
111   gtk_editable_get_selection_bounds (GTK_EDITABLE (entry),
112                                      &start_pos, &end_pos);
113   gtk_entry_accessible->cursor_position = end_pos;
114   gtk_entry_accessible->selection_bound = start_pos;
115
116   /* Set up signal callbacks */
117   g_signal_connect (entry, "insert-text", G_CALLBACK (insert_text_cb), NULL);
118   g_signal_connect (entry, "delete-text", G_CALLBACK (delete_text_cb), NULL);
119
120   if (gtk_entry_get_visibility (entry))
121     obj->role = ATK_ROLE_TEXT;
122   else
123     obj->role = ATK_ROLE_PASSWORD_TEXT;
124 }
125
126 static void
127 gtk_entry_accessible_notify_gtk (GObject    *obj,
128                                  GParamSpec *pspec)
129 {
130   GtkWidget *widget;
131   AtkObject* atk_obj;
132   GtkEntry* gtk_entry;
133   GtkEntryAccessible* entry;
134
135   widget = GTK_WIDGET (obj);
136   atk_obj = gtk_widget_get_accessible (widget);
137   gtk_entry = GTK_ENTRY (widget);
138   entry = GTK_ENTRY_ACCESSIBLE (atk_obj);
139
140   if (g_strcmp0 (pspec->name, "cursor-position") == 0)
141     {
142       if (check_for_selection_change (entry, gtk_entry))
143         g_signal_emit_by_name (atk_obj, "text-selection-changed");
144       /*
145        * The entry cursor position has moved so generate the signal.
146        */
147       g_signal_emit_by_name (atk_obj, "text-caret-moved",
148                              entry->cursor_position);
149     }
150   else if (g_strcmp0 (pspec->name, "selection-bound") == 0)
151     {
152       if (check_for_selection_change (entry, gtk_entry))
153         g_signal_emit_by_name (atk_obj, "text-selection-changed");
154     }
155   else if (g_strcmp0 (pspec->name, "editable") == 0)
156     {
157       gboolean value;
158
159       g_object_get (obj, "editable", &value, NULL);
160       atk_object_notify_state_change (atk_obj, ATK_STATE_EDITABLE, value);
161     }
162   else if (g_strcmp0 (pspec->name, "visibility") == 0)
163     {
164       gboolean visibility;
165       AtkRole new_role;
166
167       visibility = gtk_entry_get_visibility (gtk_entry);
168       new_role = visibility ? ATK_ROLE_TEXT : ATK_ROLE_PASSWORD_TEXT;
169       atk_object_set_role (atk_obj, new_role);
170     }
171   else
172     GTK_WIDGET_ACCESSIBLE_CLASS (_gtk_entry_accessible_parent_class)->notify_gtk (obj, pspec);
173 }
174
175 static gint
176 gtk_entry_accessible_get_index_in_parent (AtkObject *accessible)
177 {
178   /*
179    * If the parent widget is a combo box then the index is 1
180    * otherwise do the normal thing.
181    */
182   if (accessible->accessible_parent)
183     if (GTK_IS_COMBO_BOX_ACCESSIBLE (accessible->accessible_parent))
184       return 1;
185
186   return ATK_OBJECT_CLASS (_gtk_entry_accessible_parent_class)->get_index_in_parent (accessible);
187 }
188
189 static void
190 _gtk_entry_accessible_class_init (GtkEntryAccessibleClass *klass)
191 {
192   AtkObjectClass  *class = ATK_OBJECT_CLASS (klass);
193   GtkWidgetAccessibleClass *widget_class = (GtkWidgetAccessibleClass*)klass;
194
195   class->ref_state_set = gtk_entry_accessible_ref_state_set;
196   class->get_index_in_parent = gtk_entry_accessible_get_index_in_parent;
197   class->initialize = gtk_entry_accessible_initialize;
198   class->get_attributes = gtk_entry_accessible_get_attributes;
199
200   widget_class->notify_gtk = gtk_entry_accessible_notify_gtk;
201 }
202
203 static void
204 _gtk_entry_accessible_init (GtkEntryAccessible *entry)
205 {
206   entry->cursor_position = 0;
207   entry->selection_bound = 0;
208 }
209
210 static gchar *
211 gtk_entry_accessible_get_text (AtkText *atk_text,
212                                gint     start_pos,
213                                gint     end_pos)
214 {
215   GtkWidget *widget;
216   const gchar *text;
217
218   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (atk_text));
219   if (widget == NULL)
220     return NULL;
221
222   /* FIXME: is this acceptable ? */
223   if (!gtk_entry_get_visibility (GTK_ENTRY (widget)))
224     return g_strdup ("");
225
226   text = gtk_entry_get_text (GTK_ENTRY (widget));
227
228   if (text)
229     return g_utf8_substring (text, start_pos, end_pos > -1 ? end_pos : g_utf8_strlen (text, -1));
230
231   return NULL;
232 }
233
234 static gchar *
235 gtk_entry_accessible_get_text_before_offset (AtkText         *text,
236                                              gint             offset,
237                                              AtkTextBoundary  boundary_type,
238                                              gint            *start_offset,
239                                              gint            *end_offset)
240 {
241   GtkWidget *widget;
242
243   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
244   if (widget == NULL)
245     return NULL;
246
247   /* FIXME: is this acceptable ? */
248   if (!gtk_entry_get_visibility (GTK_ENTRY (widget)))
249     return g_strdup ("");
250
251   return _gtk_pango_get_text_before (gtk_entry_get_layout (GTK_ENTRY (widget)),
252                                      boundary_type, offset,
253                                      start_offset, end_offset);
254 }
255
256 static gchar *
257 gtk_entry_accessible_get_text_at_offset (AtkText         *text,
258                                          gint             offset,
259                                          AtkTextBoundary  boundary_type,
260                                          gint            *start_offset,
261                                          gint            *end_offset)
262 {
263   GtkWidget *widget;
264
265   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
266   if (widget == NULL)
267     return NULL;
268
269   /* FIXME: is this acceptable ? */
270   if (!gtk_entry_get_visibility (GTK_ENTRY (widget)))
271     return g_strdup ("");
272
273   return _gtk_pango_get_text_at (gtk_entry_get_layout (GTK_ENTRY (widget)),
274                                  boundary_type, offset,
275                                  start_offset, end_offset);
276 }
277
278 static gchar *
279 gtk_entry_accessible_get_text_after_offset (AtkText         *text,
280                                             gint             offset,
281                                             AtkTextBoundary  boundary_type,
282                                             gint            *start_offset,
283                                             gint            *end_offset)
284 {
285   GtkWidget *widget;
286
287   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
288   if (widget == NULL)
289     return NULL;
290
291   /* FIXME: is this acceptable ? */
292   if (!gtk_entry_get_visibility (GTK_ENTRY (widget)))
293     return g_strdup ("");
294
295   return _gtk_pango_get_text_after (gtk_entry_get_layout (GTK_ENTRY (widget)),
296                                     boundary_type, offset,
297                                     start_offset, end_offset);
298 }
299
300 static gint
301 gtk_entry_accessible_get_character_count (AtkText *atk_text)
302 {
303   GtkWidget *widget;
304   const gchar *text;
305
306   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (atk_text));
307   if (widget == NULL)
308     return 0;
309
310   text = gtk_entry_get_text (GTK_ENTRY (widget));
311
312   if (text)
313     return g_utf8_strlen (text, -1);
314
315   return 0;
316 }
317
318 static gint
319 gtk_entry_accessible_get_caret_offset (AtkText *text)
320 {
321   GtkWidget *widget;
322
323   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
324   if (widget == NULL)
325     return 0;
326
327   return gtk_editable_get_position (GTK_EDITABLE (widget));
328 }
329
330 static gboolean
331 gtk_entry_accessible_set_caret_offset (AtkText *text,
332                                        gint     offset)
333 {
334   GtkWidget *widget;
335
336   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
337   if (widget == NULL)
338     return FALSE;
339
340   gtk_editable_set_position (GTK_EDITABLE (widget), offset);
341
342   return TRUE;
343 }
344
345 static AtkAttributeSet *
346 add_text_attribute (AtkAttributeSet  *attributes,
347                     AtkTextAttribute  attr,
348                     gint              i)
349 {
350   AtkAttribute *at;
351
352   at = g_new (AtkAttribute, 1);
353   at->name = g_strdup (atk_text_attribute_get_name (attr));
354   at->value = g_strdup (atk_text_attribute_get_value (attr, i));
355
356   return g_slist_prepend (attributes, at);
357 }
358
359 static AtkAttributeSet *
360 gtk_entry_accessible_get_run_attributes (AtkText *text,
361                                          gint     offset,
362                                          gint    *start_offset,
363                                          gint    *end_offset)
364 {
365   GtkWidget *widget;
366   AtkAttributeSet *attributes;
367
368   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
369   if (widget == NULL)
370     return NULL;
371
372   attributes = NULL;
373   attributes = add_text_attribute (attributes, ATK_TEXT_ATTR_DIRECTION,
374                                    gtk_widget_get_direction (widget));
375   attributes = _gtk_pango_get_run_attributes (attributes,
376                                               gtk_entry_get_layout (GTK_ENTRY (widget)),
377                                               offset,
378                                               start_offset,
379                                               end_offset);
380
381   return attributes;
382 }
383
384 static AtkAttributeSet *
385 gtk_entry_accessible_get_default_attributes (AtkText *text)
386 {
387   GtkWidget *widget;
388   AtkAttributeSet *attributes;
389
390   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
391   if (widget == NULL)
392     return NULL;
393
394   attributes = NULL;
395   attributes = add_text_attribute (attributes, ATK_TEXT_ATTR_DIRECTION,
396                                    gtk_widget_get_direction (widget));
397   attributes = _gtk_pango_get_default_attributes (attributes,
398                                                   gtk_entry_get_layout (GTK_ENTRY (widget)));
399   attributes = _gtk_style_context_get_attributes (attributes,
400                                                   gtk_widget_get_style_context (widget),
401                                                   gtk_widget_get_state_flags (widget));
402
403   return attributes;
404 }
405
406 static void
407 gtk_entry_accessible_get_character_extents (AtkText      *text,
408                                             gint          offset,
409                                             gint         *x,
410                                             gint         *y,
411                                             gint         *width,
412                                             gint         *height,
413                                             AtkCoordType  coords)
414 {
415   GtkWidget *widget;
416   GtkEntry *entry;
417   PangoRectangle char_rect;
418   const gchar *entry_text;
419   gint index, x_layout, y_layout;
420   GdkWindow *window;
421   gint x_window, y_window;
422
423   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
424   if (widget == NULL)
425     return;
426
427   entry = GTK_ENTRY (widget);
428
429   gtk_entry_get_layout_offsets (entry, &x_layout, &y_layout);
430   entry_text = gtk_entry_get_text (entry);
431   index = g_utf8_offset_to_pointer (entry_text, offset) - entry_text;
432   pango_layout_index_to_pos (gtk_entry_get_layout (entry), index, &char_rect);
433   pango_extents_to_pixels (&char_rect, NULL);
434
435   window = gtk_widget_get_window (widget);
436   gdk_window_get_origin (window, &x_window, &y_window);
437
438   *x = x_window + x_layout + char_rect.x;
439   *y = y_window + y_layout + char_rect.y;
440   *width = char_rect.width;
441   *height = char_rect.height;
442
443   if (coords == ATK_XY_WINDOW)
444     {
445       window = gdk_window_get_toplevel (window);
446       gdk_window_get_origin (window, &x_window, &y_window);
447
448       *x -= x_window;
449       *y -= y_window;
450     }
451 }
452
453 static gint
454 gtk_entry_accessible_get_offset_at_point (AtkText      *atk_text,
455                                           gint          x,
456                                           gint          y,
457                                           AtkCoordType  coords)
458 {
459   GtkWidget *widget;
460   GtkEntry *entry;
461   const gchar *text;
462   gint index, x_layout, y_layout;
463   gint x_window, y_window;
464   gint x_local, y_local;
465   GdkWindow *window;
466
467   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (atk_text));
468   if (widget == NULL)
469     return -1;
470
471   entry = GTK_ENTRY (widget);
472
473   gtk_entry_get_layout_offsets (entry, &x_layout, &y_layout);
474
475   window = gtk_widget_get_window (widget);
476   gdk_window_get_origin (window, &x_window, &y_window);
477
478   x_local = x - x_layout - x_window;
479   y_local = y - y_layout - y_window;
480
481   if (coords == ATK_XY_WINDOW)
482     {
483       window = gdk_window_get_toplevel (window);
484       gdk_window_get_origin (window, &x_window, &y_window);
485
486       x_local += x_window;
487       y_local += y_window;
488     }
489   if (!pango_layout_xy_to_index (gtk_entry_get_layout (entry),
490                                  x_local * PANGO_SCALE,
491                                  y_local * PANGO_SCALE,
492                                  &index, NULL))
493     {
494       if (x_local < 0 || y_local < 0)
495         index = 0;
496       else
497         index = -1;
498     }
499
500   if (index != -1)
501     {
502       text = gtk_entry_get_text (entry);
503       return g_utf8_pointer_to_offset (text, text + index);
504     }
505
506   return -1;
507 }
508
509 static gint
510 gtk_entry_accessible_get_n_selections (AtkText *text)
511 {
512   GtkWidget *widget;
513   gint start, end;
514
515   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
516   if (widget == NULL)
517     return 0;
518
519   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (widget), &start, &end))
520     return 1;
521
522   return 0;
523 }
524
525 static gchar *
526 gtk_entry_accessible_get_selection (AtkText *text,
527                                     gint     selection_num,
528                                     gint    *start_pos,
529                                     gint    *end_pos)
530 {
531   GtkWidget *widget;
532
533   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
534   if (widget == NULL)
535     return NULL;
536
537   if (selection_num != 0)
538      return NULL;
539
540   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (widget), start_pos, end_pos))
541     return gtk_editable_get_chars (GTK_EDITABLE (widget), *start_pos, *end_pos);
542
543   return NULL;
544 }
545
546 static gboolean
547 gtk_entry_accessible_add_selection (AtkText *text,
548                                     gint     start_pos,
549                                     gint     end_pos)
550 {
551   GtkEntry *entry;
552   GtkWidget *widget;
553   gint start, end;
554
555   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
556   if (widget == NULL)
557     return FALSE;
558
559   entry = GTK_ENTRY (widget);
560
561   if (!gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
562     {
563       gtk_editable_select_region (GTK_EDITABLE (entry), start_pos, end_pos);
564       return TRUE;
565     }
566   else
567     return FALSE;
568 }
569
570 static gboolean
571 gtk_entry_accessible_remove_selection (AtkText *text,
572                                        gint     selection_num)
573 {
574   GtkWidget *widget;
575   gint start, end;
576
577   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
578   if (widget == NULL)
579     return FALSE;
580
581   if (selection_num != 0)
582      return FALSE;
583
584   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (widget), &start, &end))
585     {
586       gtk_editable_select_region (GTK_EDITABLE (widget), end, end);
587       return TRUE;
588     }
589   else
590     return FALSE;
591 }
592
593 static gboolean
594 gtk_entry_accessible_set_selection (AtkText *text,
595                                     gint     selection_num,
596                                     gint     start_pos,
597                                     gint     end_pos)
598 {
599   GtkWidget *widget;
600   gint start, end;
601
602   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
603   if (widget == NULL)
604     return FALSE;
605
606   if (selection_num != 0)
607      return FALSE;
608
609   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (widget), &start, &end))
610     {
611       gtk_editable_select_region (GTK_EDITABLE (widget), start_pos, end_pos);
612       return TRUE;
613     }
614   else
615     return FALSE;
616 }
617
618 static gunichar
619 gtk_entry_accessible_get_character_at_offset (AtkText *atk_text,
620                                               gint     offset)
621 {
622   GtkWidget *widget;
623   const gchar *text;
624   gchar *index;
625
626   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (atk_text));
627   if (widget == NULL)
628     return '\0';
629
630   if (!gtk_entry_get_visibility (GTK_ENTRY (widget)))
631     return '\0';
632
633   text = gtk_entry_get_text (GTK_ENTRY (widget));
634   if (offset >= g_utf8_strlen (text, -1))
635     return '\0';
636
637   index = g_utf8_offset_to_pointer (text, offset);
638
639   return g_utf8_get_char (index);
640 }
641
642 static void
643 atk_text_interface_init (AtkTextIface *iface)
644 {
645   iface->get_text = gtk_entry_accessible_get_text;
646   iface->get_character_at_offset = gtk_entry_accessible_get_character_at_offset;
647   iface->get_text_before_offset = gtk_entry_accessible_get_text_before_offset;
648   iface->get_text_at_offset = gtk_entry_accessible_get_text_at_offset;
649   iface->get_text_after_offset = gtk_entry_accessible_get_text_after_offset;
650   iface->get_caret_offset = gtk_entry_accessible_get_caret_offset;
651   iface->set_caret_offset = gtk_entry_accessible_set_caret_offset;
652   iface->get_character_count = gtk_entry_accessible_get_character_count;
653   iface->get_n_selections = gtk_entry_accessible_get_n_selections;
654   iface->get_selection = gtk_entry_accessible_get_selection;
655   iface->add_selection = gtk_entry_accessible_add_selection;
656   iface->remove_selection = gtk_entry_accessible_remove_selection;
657   iface->set_selection = gtk_entry_accessible_set_selection;
658   iface->get_run_attributes = gtk_entry_accessible_get_run_attributes;
659   iface->get_default_attributes = gtk_entry_accessible_get_default_attributes;
660   iface->get_character_extents = gtk_entry_accessible_get_character_extents;
661   iface->get_offset_at_point = gtk_entry_accessible_get_offset_at_point;
662 }
663
664 static void
665 gtk_entry_accessible_set_text_contents (AtkEditableText *text,
666                                         const gchar     *string)
667 {
668   GtkWidget *widget;
669
670   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
671   if (widget == NULL)
672     return;
673
674   if (!gtk_editable_get_editable (GTK_EDITABLE (widget)))
675     return;
676
677   gtk_entry_set_text (GTK_ENTRY (widget), string);
678 }
679
680 static void
681 gtk_entry_accessible_insert_text (AtkEditableText *text,
682                                   const gchar     *string,
683                                   gint             length,
684                                   gint            *position)
685 {
686   GtkWidget *widget;
687   GtkEditable *editable;
688
689   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
690   if (widget == NULL)
691     return;
692
693   editable = GTK_EDITABLE (widget);
694   if (!gtk_editable_get_editable (editable))
695     return;
696
697   gtk_editable_insert_text (editable, string, length, position);
698   gtk_editable_set_position (editable, *position);
699 }
700
701 static void
702 gtk_entry_accessible_copy_text (AtkEditableText *text,
703                                 gint             start_pos,
704                                 gint             end_pos)
705 {
706   GtkWidget *widget;
707   GtkEditable *editable;
708   gchar *str;
709   GtkClipboard *clipboard;
710
711   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
712   if (widget == NULL)
713     return;
714
715   if (!gtk_widget_has_screen (widget))
716     return;
717
718   editable = GTK_EDITABLE (widget);
719   str = gtk_editable_get_chars (editable, start_pos, end_pos);
720   clipboard = gtk_widget_get_clipboard (widget, GDK_SELECTION_CLIPBOARD);
721   gtk_clipboard_set_text (clipboard, str, -1);
722   g_free (str);
723 }
724
725 static void
726 gtk_entry_accessible_cut_text (AtkEditableText *text,
727                                gint             start_pos,
728                                gint             end_pos)
729 {
730   GtkWidget *widget;
731   GtkEditable *editable;
732   gchar *str;
733   GtkClipboard *clipboard;
734
735   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
736   if (widget == NULL)
737     return;
738
739   if (!gtk_widget_has_screen (widget))
740     return;
741
742   editable = GTK_EDITABLE (widget);
743   if (!gtk_editable_get_editable (editable))
744     return;
745
746   str = gtk_editable_get_chars (editable, start_pos, end_pos);
747   clipboard = gtk_widget_get_clipboard (widget, GDK_SELECTION_CLIPBOARD);
748   gtk_clipboard_set_text (clipboard, str, -1);
749   gtk_editable_delete_text (editable, start_pos, end_pos);
750 }
751
752 static void
753 gtk_entry_accessible_delete_text (AtkEditableText *text,
754                                   gint             start_pos,
755                                   gint             end_pos)
756 {
757   GtkWidget *widget;
758   GtkEditable *editable;
759
760   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
761   if (widget == NULL)
762     return;
763
764   editable = GTK_EDITABLE (widget);
765   if (!gtk_editable_get_editable (editable))
766     return;
767
768   gtk_editable_delete_text (editable, start_pos, end_pos);
769 }
770
771 typedef struct
772 {
773   GtkEntry* entry;
774   gint position;
775 } PasteData;
776
777 static void
778 paste_received_cb (GtkClipboard *clipboard,
779                    const gchar  *text,
780                    gpointer      data)
781 {
782   PasteData *paste = data;
783
784   if (text)
785     gtk_editable_insert_text (GTK_EDITABLE (paste->entry), text, -1,
786                               &paste->position);
787
788   g_object_unref (paste->entry);
789   g_free (paste);
790 }
791
792 static void
793 gtk_entry_accessible_paste_text (AtkEditableText *text,
794                                  gint             position)
795 {
796   GtkWidget *widget;
797   GtkEditable *editable;
798   PasteData *paste;
799   GtkClipboard *clipboard;
800
801   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
802   if (widget == NULL)
803     return;
804
805   if (!gtk_widget_has_screen (widget))
806     return;
807
808   editable = GTK_EDITABLE (widget);
809   if (!gtk_editable_get_editable (editable))
810     return;
811
812   paste = g_new0 (PasteData, 1);
813   paste->entry = GTK_ENTRY (widget);
814   paste->position = position;
815
816   g_object_ref (paste->entry);
817   clipboard = gtk_widget_get_clipboard (widget, GDK_SELECTION_CLIPBOARD);
818   gtk_clipboard_request_text (clipboard, paste_received_cb, paste);
819 }
820
821 static void
822 atk_editable_text_interface_init (AtkEditableTextIface *iface)
823 {
824   iface->set_text_contents = gtk_entry_accessible_set_text_contents;
825   iface->insert_text = gtk_entry_accessible_insert_text;
826   iface->copy_text = gtk_entry_accessible_copy_text;
827   iface->cut_text = gtk_entry_accessible_cut_text;
828   iface->delete_text = gtk_entry_accessible_delete_text;
829   iface->paste_text = gtk_entry_accessible_paste_text;
830   iface->set_run_attributes = NULL;
831 }
832
833 /* We connect to GtkEditable::insert-text, since it carries
834  * the information we need. But we delay emitting our own
835  * text_changed::insert signal until the entry has update
836  * all its internal state and emits GtkEntry::changed.
837  */
838 static void
839 insert_text_cb (GtkEditable *editable,
840                 gchar       *new_text,
841                 gint         new_text_length,
842                 gint        *position)
843 {
844   GtkEntryAccessible *accessible;
845
846   if (new_text_length == 0)
847     return;
848
849   accessible = GTK_ENTRY_ACCESSIBLE (gtk_widget_get_accessible (GTK_WIDGET (editable)));
850
851   g_signal_emit_by_name (accessible,
852                          "text-changed::insert",
853                          *position,
854                           g_utf8_strlen (new_text, new_text_length));
855 }
856
857 /* We connect to GtkEditable::delete-text, since it carries
858  * the information we need. But we delay emitting our own
859  * text_changed::delete signal until the entry has update
860  * all its internal state and emits GtkEntry::changed.
861  */
862 static void
863 delete_text_cb (GtkEditable *editable,
864                 gint         start,
865                 gint         end)
866 {
867   GtkEntryAccessible *accessible;
868
869   accessible = GTK_ENTRY_ACCESSIBLE (gtk_widget_get_accessible (GTK_WIDGET (editable)));
870
871   if (end < 0)
872     {
873       const gchar *text;
874
875       text = gtk_entry_get_text (GTK_ENTRY (editable));
876       end = g_utf8_strlen (text, -1);
877     }
878
879   if (end == start)
880     return;
881
882   g_signal_emit_by_name (accessible,
883                          "text-changed::delete",
884                          start,
885                          end);
886 }
887
888 static gboolean
889 check_for_selection_change (GtkEntryAccessible *accessible,
890                             GtkEntry           *entry)
891 {
892   gboolean ret_val = FALSE;
893   gint start, end;
894
895   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
896     {
897       if (end != accessible->cursor_position ||
898           start != accessible->selection_bound)
899         /*
900          * This check is here as this function can be called
901          * for notification of selection_bound and current_pos.
902          * The values of current_pos and selection_bound may be the same
903          * for both notifications and we only want to generate one
904          * text_selection_changed signal.
905          */
906         ret_val = TRUE;
907     }
908   else
909     {
910       /* We had a selection */
911       ret_val = (accessible->cursor_position != accessible->selection_bound);
912     }
913
914   accessible->cursor_position = end;
915   accessible->selection_bound = start;
916
917   return ret_val;
918 }
919
920 static gboolean
921 gtk_entry_accessible_do_action (AtkAction *action,
922                                 gint       i)
923 {
924   GtkWidget *widget;
925
926   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (action));
927   if (widget == NULL)
928     return FALSE;
929
930   if (!gtk_widget_get_sensitive (widget) || !gtk_widget_get_visible (widget))
931     return FALSE;
932
933   if (i != 0)
934     return FALSE;
935
936   gtk_widget_activate (widget);
937
938   return TRUE;
939 }
940
941 static gint
942 gtk_entry_accessible_get_n_actions (AtkAction *action)
943 {
944   return 1;
945 }
946
947 static const gchar *
948 gtk_entry_accessible_get_keybinding (AtkAction *action,
949                                      gint       i)
950 {
951   GtkWidget *widget;
952   GtkWidget *label;
953   AtkRelationSet *set;
954   AtkRelation *relation;
955   GPtrArray *target;
956   gpointer target_object;
957   guint key_val;
958
959   if (i != 0)
960     return NULL;
961
962   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (action));
963   if (widget == NULL)
964     return NULL;
965
966   set = atk_object_ref_relation_set (ATK_OBJECT (action));
967   if (!set)
968     return NULL;
969
970   label = NULL;
971   relation = atk_relation_set_get_relation_by_type (set, ATK_RELATION_LABELLED_BY);
972   if (relation)
973     {
974       target = atk_relation_get_target (relation);
975
976       target_object = g_ptr_array_index (target, 0);
977       label = gtk_accessible_get_widget (GTK_ACCESSIBLE (target_object));
978     }
979
980   g_object_unref (set);
981
982   if (GTK_IS_LABEL (label))
983     {
984       key_val = gtk_label_get_mnemonic_keyval (GTK_LABEL (label));
985       if (key_val != GDK_KEY_VoidSymbol)
986         return gtk_accelerator_name (key_val, GDK_MOD1_MASK);
987     }
988
989   return NULL;
990 }
991
992 static const gchar*
993 gtk_entry_accessible_action_get_name (AtkAction *action,
994                                       gint       i)
995 {
996   if (i != 0)
997     return NULL;
998
999   return "activate";
1000 }
1001
1002 static void
1003 atk_action_interface_init (AtkActionIface *iface)
1004 {
1005   iface->do_action = gtk_entry_accessible_do_action;
1006   iface->get_n_actions = gtk_entry_accessible_get_n_actions;
1007   iface->get_keybinding = gtk_entry_accessible_get_keybinding;
1008   iface->get_name = gtk_entry_accessible_action_get_name;
1009 }