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