]> Pileus Git - ~andy/gtk/blob - modules/other/gail/gailentry.c
Include <config.h>. Bug #504720.
[~andy/gtk] / modules / other / gail / gailentry.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 <string.h>
23 #include <gtk/gtk.h>
24 #include <gdk/gdkkeysyms.h>
25 #include "gailentry.h"
26 #include "gailcombo.h"
27 #include "gailcombobox.h"
28 #include <libgail-util/gailmisc.h>
29
30 static void       gail_entry_class_init            (GailEntryClass       *klass);
31 static void       gail_entry_init                  (GailEntry            *entry);
32 static void       gail_entry_real_initialize       (AtkObject            *obj,
33                                                     gpointer             data);
34 static void       text_setup                       (GailEntry            *entry,
35                                                     GtkEntry             *gtk_entry);
36 static void       gail_entry_real_notify_gtk       (GObject              *obj,
37                                                     GParamSpec           *pspec);
38 static void       gail_entry_finalize              (GObject              *object);
39
40 static gint       gail_entry_get_index_in_parent   (AtkObject            *accessible);
41
42 /* atkobject.h */
43
44 static AtkStateSet* gail_entry_ref_state_set       (AtkObject            *accessible);
45
46 /* atktext.h */
47
48 static void       atk_text_interface_init          (AtkTextIface         *iface);
49
50 static gchar*     gail_entry_get_text              (AtkText              *text,
51                                                     gint                 start_pos,
52                                                     gint                 end_pos);
53 static gunichar   gail_entry_get_character_at_offset
54                                                    (AtkText              *text,
55                                                     gint                 offset);
56 static gchar*     gail_entry_get_text_before_offset(AtkText              *text,
57                                                     gint                 offset,
58                                                     AtkTextBoundary      boundary_type,
59                                                     gint                 *start_offset,
60                                                     gint                 *end_offset);
61 static gchar*     gail_entry_get_text_at_offset    (AtkText              *text,
62                                                     gint                 offset,
63                                                     AtkTextBoundary      boundary_type,
64                                                     gint                 *start_offset,
65                                                     gint                 *end_offset);
66 static gchar*     gail_entry_get_text_after_offset (AtkText              *text,
67                                                     gint                 offset,
68                                                     AtkTextBoundary      boundary_type,
69                                                     gint                 *start_offset,
70                                                     gint                 *end_offset);
71 static gint       gail_entry_get_caret_offset      (AtkText              *text);
72 static gboolean   gail_entry_set_caret_offset      (AtkText              *text,
73                                                     gint                 offset);
74 static gint       gail_entry_get_n_selections      (AtkText              *text);
75 static gchar*     gail_entry_get_selection         (AtkText              *text,
76                                                     gint                 selection_num,
77                                                     gint                 *start_offset,
78                                                     gint                 *end_offset);
79 static gboolean   gail_entry_add_selection         (AtkText              *text,
80                                                     gint                 start_offset,
81                                                     gint                 end_offset);
82 static gboolean   gail_entry_remove_selection      (AtkText              *text,
83                                                     gint                 selection_num);
84 static gboolean   gail_entry_set_selection         (AtkText              *text,
85                                                     gint                 selection_num,
86                                                     gint                 start_offset,
87                                                     gint                 end_offset);
88 static gint       gail_entry_get_character_count   (AtkText              *text);
89 static AtkAttributeSet *  gail_entry_get_run_attributes 
90                                                    (AtkText              *text,
91                                                     gint                 offset,
92                                                     gint                 *start_offset,
93                                                     gint                 *end_offset);
94 static AtkAttributeSet *  gail_entry_get_default_attributes 
95                                                    (AtkText              *text);
96 static void gail_entry_get_character_extents       (AtkText              *text,
97                                                     gint                 offset,
98                                                     gint                 *x,
99                                                     gint                 *y,
100                                                     gint                 *width,
101                                                     gint                 *height,
102                                                     AtkCoordType         coords);
103 static gint gail_entry_get_offset_at_point         (AtkText              *text,
104                                                     gint                 x,
105                                                     gint                 y,
106                                                     AtkCoordType         coords);
107 /* atkeditabletext.h */
108
109 static void       atk_editable_text_interface_init (AtkEditableTextIface *iface);
110 static void       gail_entry_set_text_contents     (AtkEditableText      *text,
111                                                     const gchar          *string);
112 static void       gail_entry_insert_text           (AtkEditableText      *text,
113                                                     const gchar          *string,
114                                                     gint                 length,
115                                                     gint                 *position);
116 static void       gail_entry_copy_text             (AtkEditableText      *text,
117                                                     gint                 start_pos,
118                                                     gint                 end_pos);
119 static void       gail_entry_cut_text              (AtkEditableText      *text,
120                                                     gint                 start_pos,
121                                                     gint                 end_pos);
122 static void       gail_entry_delete_text           (AtkEditableText      *text,
123                                                     gint                 start_pos,
124                                                     gint                 end_pos);
125 static void       gail_entry_paste_text            (AtkEditableText      *text,
126                                                     gint                 position);
127 static void       gail_entry_paste_received        (GtkClipboard *clipboard,
128                                                     const gchar  *text,
129                                                     gpointer     data);
130
131
132 /* Callbacks */
133
134 static void       gail_entry_notify_insert         (GailEntry            *entry);
135 static void       gail_entry_notify_delete         (GailEntry            *entry);
136 static void       _gail_entry_insert_text_cb       (GtkEntry             *entry,
137                                                     gchar                *arg1,
138                                                     gint                 arg2,
139                                                     gpointer             arg3);
140 static void       _gail_entry_delete_text_cb       (GtkEntry             *entry,
141                                                     gint                 arg1,
142                                                     gint                 arg2);
143 static void       _gail_entry_changed_cb           (GtkEntry             *entry);
144 static gboolean   check_for_selection_change       (GailEntry            *entry,
145                                                     GtkEntry             *gtk_entry);
146
147 static void                  atk_action_interface_init   (AtkActionIface  *iface);
148
149 static gboolean              gail_entry_do_action        (AtkAction       *action,
150                                                           gint            i);
151 static gboolean              idle_do_action              (gpointer        data);
152 static gint                  gail_entry_get_n_actions    (AtkAction       *action);
153 static G_CONST_RETURN gchar* gail_entry_get_description  (AtkAction       *action,
154                                                           gint            i);
155 static G_CONST_RETURN gchar* gail_entry_get_keybinding   (AtkAction       *action,
156                                                           gint            i);
157 static G_CONST_RETURN gchar* gail_entry_action_get_name  (AtkAction       *action,
158                                                           gint            i);
159 static gboolean              gail_entry_set_description  (AtkAction       *action,
160                                                           gint            i,
161                                                           const gchar     *desc);
162
163 typedef struct _GailEntryPaste                  GailEntryPaste;
164
165 struct _GailEntryPaste
166 {
167   GtkEntry* entry;
168   gint position;
169 };
170
171 G_DEFINE_TYPE_WITH_CODE (GailEntry, gail_entry, GAIL_TYPE_WIDGET,
172                          G_IMPLEMENT_INTERFACE (ATK_TYPE_EDITABLE_TEXT, atk_editable_text_interface_init)
173                          G_IMPLEMENT_INTERFACE (ATK_TYPE_TEXT, atk_text_interface_init)
174                          G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init))
175
176 static void
177 gail_entry_class_init (GailEntryClass *klass)
178 {
179   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
180   AtkObjectClass  *class = ATK_OBJECT_CLASS (klass);
181   GailWidgetClass *widget_class;
182
183   widget_class = (GailWidgetClass*)klass;
184
185   gobject_class->finalize = gail_entry_finalize;
186
187   class->ref_state_set = gail_entry_ref_state_set;
188   class->get_index_in_parent = gail_entry_get_index_in_parent;
189   class->initialize = gail_entry_real_initialize;
190
191   widget_class->notify_gtk = gail_entry_real_notify_gtk;
192 }
193
194 static void
195 gail_entry_init (GailEntry *entry)
196 {
197   entry->textutil = NULL;
198   entry->signal_name_insert = NULL;
199   entry->signal_name_delete = NULL;
200   entry->cursor_position = 0;
201   entry->selection_bound = 0;
202   entry->activate_description = NULL;
203   entry->activate_keybinding = NULL;
204 }
205
206 static void
207 gail_entry_real_initialize (AtkObject *obj, 
208                             gpointer  data)
209 {
210   GtkEntry *entry;
211   GailEntry *gail_entry;
212
213   ATK_OBJECT_CLASS (gail_entry_parent_class)->initialize (obj, data);
214
215   gail_entry = GAIL_ENTRY (obj);
216   gail_entry->textutil = gail_text_util_new ();
217   
218   g_assert (GTK_IS_ENTRY (data));
219
220   entry = GTK_ENTRY (data);
221   text_setup (gail_entry, entry);
222   gail_entry->cursor_position = entry->current_pos;
223   gail_entry->selection_bound = entry->selection_bound;
224
225   /* Set up signal callbacks */
226   g_signal_connect (data, "insert-text",
227         G_CALLBACK (_gail_entry_insert_text_cb), NULL);
228   g_signal_connect (data, "delete-text",
229         G_CALLBACK (_gail_entry_delete_text_cb), NULL);
230   g_signal_connect (data, "changed",
231         G_CALLBACK (_gail_entry_changed_cb), NULL);
232
233   if (entry->visible)
234     obj->role = ATK_ROLE_TEXT;
235   else
236     obj->role = ATK_ROLE_PASSWORD_TEXT;
237 }
238
239 static void
240 gail_entry_real_notify_gtk (GObject             *obj,
241                             GParamSpec          *pspec)
242 {
243   GtkWidget *widget;
244   AtkObject* atk_obj;
245   GtkEntry* gtk_entry;
246   GailEntry* entry;
247
248   widget = GTK_WIDGET (obj);
249   atk_obj = gtk_widget_get_accessible (widget);
250   gtk_entry = GTK_ENTRY (widget);
251   entry = GAIL_ENTRY (atk_obj);
252
253   if (strcmp (pspec->name, "cursor-position") == 0)
254     {
255       gail_entry_notify_insert (entry);
256
257       if (check_for_selection_change (entry, gtk_entry))
258         g_signal_emit_by_name (atk_obj, "text_selection_changed");
259       /*
260        * The entry cursor position has moved so generate the signal.
261        */
262       g_signal_emit_by_name (atk_obj, "text_caret_moved", 
263                              entry->cursor_position);
264     }
265   else if (strcmp (pspec->name, "selection-bound") == 0)
266     {
267       gail_entry_notify_insert (entry);
268
269       if (check_for_selection_change (entry, gtk_entry))
270         g_signal_emit_by_name (atk_obj, "text_selection_changed");
271     }
272   else if (strcmp (pspec->name, "editable") == 0)
273     {
274       gboolean value;
275
276       g_object_get (obj, "editable", &value, NULL);
277       atk_object_notify_state_change (atk_obj, ATK_STATE_EDITABLE,
278                                                value);
279     }
280   else if (strcmp (pspec->name, "visibility") == 0)
281     {
282       gboolean visibility;
283       AtkRole new_role;
284
285       text_setup (entry, gtk_entry);
286       visibility = gtk_entry_get_visibility (gtk_entry);
287       new_role = visibility ? ATK_ROLE_TEXT : ATK_ROLE_PASSWORD_TEXT;
288       atk_object_set_role (atk_obj, new_role);
289     }
290   else if (strcmp (pspec->name, "invisible-char") == 0)
291     {
292       text_setup (entry, gtk_entry);
293     }
294   else
295     GAIL_WIDGET_CLASS (gail_entry_parent_class)->notify_gtk (obj, pspec);
296 }
297
298 static void
299 text_setup (GailEntry *entry,
300             GtkEntry  *gtk_entry)
301 {
302   if (gtk_entry_get_visibility (gtk_entry))
303     {
304       gail_text_util_text_setup (entry->textutil, gtk_entry_get_text (gtk_entry));
305     }
306   else
307     {
308       gunichar invisible_char;
309       GString *tmp_string = g_string_new (NULL);
310       gint ch_len; 
311       gchar buf[7];
312       gint i;
313
314       invisible_char = gtk_entry_get_invisible_char (gtk_entry);
315       if (invisible_char == 0)
316         invisible_char = ' ';
317
318       ch_len = g_unichar_to_utf8 (invisible_char, buf);
319       for (i = 0; i < gtk_entry->text_length; i++)
320         {
321           g_string_append_len (tmp_string, buf, ch_len);
322         }
323
324       gail_text_util_text_setup (entry->textutil, tmp_string->str);
325       g_string_free (tmp_string, TRUE);
326
327     } 
328 }
329
330 static void
331 gail_entry_finalize (GObject            *object)
332 {
333   GailEntry *entry = GAIL_ENTRY (object);
334
335   g_object_unref (entry->textutil);
336   g_free (entry->activate_description);
337   g_free (entry->activate_keybinding);
338   if (entry->action_idle_handler)
339     {
340       g_source_remove (entry->action_idle_handler);
341       entry->action_idle_handler = 0;
342     }
343   if (entry->insert_idle_handler)
344     {
345       g_source_remove (entry->insert_idle_handler);
346       entry->insert_idle_handler = 0;
347     }
348   G_OBJECT_CLASS (gail_entry_parent_class)->finalize (object);
349 }
350
351 AtkObject* 
352 gail_entry_new (GtkWidget *widget)
353 {
354   GObject *object;
355   AtkObject *accessible;
356
357   g_return_val_if_fail (GTK_IS_ENTRY (widget), NULL);
358
359   object = g_object_new (GAIL_TYPE_ENTRY, NULL);
360
361   accessible = ATK_OBJECT (object);
362   atk_object_initialize (accessible, widget);
363
364   return accessible;
365 }
366
367 static gint
368 gail_entry_get_index_in_parent (AtkObject *accessible)
369 {
370   /*
371    * If the parent widget is a combo box then the index is 1
372    * otherwise do the normal thing.
373    */
374   if (accessible->accessible_parent)
375     if (GAIL_IS_COMBO (accessible->accessible_parent) ||
376         GAIL_IS_COMBO_BOX (accessible->accessible_parent))
377       return 1;
378
379   return ATK_OBJECT_CLASS (gail_entry_parent_class)->get_index_in_parent (accessible);
380 }
381
382 /* atkobject.h */
383
384 static AtkStateSet*
385 gail_entry_ref_state_set (AtkObject *accessible)
386 {
387   AtkStateSet *state_set;
388   GtkEntry *entry;
389   gboolean value;
390   GtkWidget *widget;
391
392   state_set = ATK_OBJECT_CLASS (gail_entry_parent_class)->ref_state_set (accessible);
393   widget = GTK_ACCESSIBLE (accessible)->widget;
394   
395   if (widget == NULL)
396     return state_set;
397
398   entry = GTK_ENTRY (widget);
399
400   g_object_get (G_OBJECT (entry), "editable", &value, NULL);
401   if (value)
402     atk_state_set_add_state (state_set, ATK_STATE_EDITABLE);
403   atk_state_set_add_state (state_set, ATK_STATE_SINGLE_LINE);
404
405   return state_set;
406 }
407
408 /* atktext.h */
409
410 static void
411 atk_text_interface_init (AtkTextIface *iface)
412 {
413   iface->get_text = gail_entry_get_text;
414   iface->get_character_at_offset = gail_entry_get_character_at_offset;
415   iface->get_text_before_offset = gail_entry_get_text_before_offset;
416   iface->get_text_at_offset = gail_entry_get_text_at_offset;
417   iface->get_text_after_offset = gail_entry_get_text_after_offset;
418   iface->get_caret_offset = gail_entry_get_caret_offset;
419   iface->set_caret_offset = gail_entry_set_caret_offset;
420   iface->get_character_count = gail_entry_get_character_count;
421   iface->get_n_selections = gail_entry_get_n_selections;
422   iface->get_selection = gail_entry_get_selection;
423   iface->add_selection = gail_entry_add_selection;
424   iface->remove_selection = gail_entry_remove_selection;
425   iface->set_selection = gail_entry_set_selection;
426   iface->get_run_attributes = gail_entry_get_run_attributes;
427   iface->get_default_attributes = gail_entry_get_default_attributes;
428   iface->get_character_extents = gail_entry_get_character_extents;
429   iface->get_offset_at_point = gail_entry_get_offset_at_point;
430 }
431
432 static gchar*
433 gail_entry_get_text (AtkText *text,
434                      gint    start_pos,
435                      gint    end_pos)
436 {
437   GtkWidget *widget;
438
439   widget = GTK_ACCESSIBLE (text)->widget;
440   if (widget == NULL)
441     /* State is defunct */
442     return NULL;
443
444   return gail_text_util_get_substring (GAIL_ENTRY (text)->textutil, start_pos, end_pos);
445 }
446
447 static gchar*
448 gail_entry_get_text_before_offset (AtkText          *text,
449                                    gint             offset,
450                                    AtkTextBoundary  boundary_type,
451                                    gint             *start_offset,
452                                    gint             *end_offset)
453 {
454   GtkWidget *widget;
455   GtkEntry *entry;
456
457   widget = GTK_ACCESSIBLE (text)->widget;
458   if (widget == NULL)
459     /* State is defunct */
460     return NULL;
461
462   /* Get Entry */
463   entry = GTK_ENTRY (widget);
464
465   return gail_text_util_get_text (GAIL_ENTRY (text)->textutil,
466                           gtk_entry_get_layout (entry), GAIL_BEFORE_OFFSET, 
467                           boundary_type, offset, start_offset, end_offset);
468 }
469
470 static gchar*
471 gail_entry_get_text_at_offset (AtkText          *text,
472                                gint             offset,
473                                AtkTextBoundary  boundary_type,
474                                gint             *start_offset,
475                                gint             *end_offset)
476 {
477   GtkWidget *widget;
478   GtkEntry *entry;
479
480   widget = GTK_ACCESSIBLE (text)->widget;
481   if (widget == NULL)
482     /* State is defunct */
483     return NULL;
484
485   /* Get Entry */
486   entry = GTK_ENTRY (widget);
487
488   return gail_text_util_get_text (GAIL_ENTRY (text)->textutil,
489                             gtk_entry_get_layout (entry), GAIL_AT_OFFSET, 
490                             boundary_type, offset, start_offset, end_offset);
491 }
492
493 static gchar*
494 gail_entry_get_text_after_offset  (AtkText          *text,
495                                    gint             offset,
496                                    AtkTextBoundary  boundary_type,
497                                    gint             *start_offset,
498                                    gint             *end_offset)
499 {
500   GtkWidget *widget;
501   GtkEntry *entry;
502
503   widget = GTK_ACCESSIBLE (text)->widget;
504   if (widget == NULL)
505     /* State is defunct */
506     return NULL;
507
508   /* Get Entry */
509   entry = GTK_ENTRY (widget);
510
511   return gail_text_util_get_text (GAIL_ENTRY (text)->textutil,
512                            gtk_entry_get_layout (entry), GAIL_AFTER_OFFSET, 
513                            boundary_type, offset, start_offset, end_offset);
514 }
515
516 static gint
517 gail_entry_get_character_count (AtkText *text)
518 {
519   GtkEntry *entry;
520   GtkWidget *widget;
521
522   widget = GTK_ACCESSIBLE (text)->widget;
523   if (widget == NULL)
524     /* State is defunct */
525     return 0;
526
527   entry = GTK_ENTRY (widget);
528   return g_utf8_strlen (gtk_entry_get_text (entry), -1);
529 }
530
531 static gint
532 gail_entry_get_caret_offset (AtkText *text)
533 {
534   GtkEntry *entry;
535   GtkWidget *widget;
536
537   widget = GTK_ACCESSIBLE (text)->widget;
538   if (widget == NULL)
539     /* State is defunct */
540     return 0;
541
542   entry = GTK_ENTRY (widget);
543
544   return gtk_editable_get_position (GTK_EDITABLE (entry));
545 }
546
547 static gboolean
548 gail_entry_set_caret_offset (AtkText *text, gint offset)
549 {
550   GtkEntry *entry;
551   GtkWidget *widget;
552
553   widget = GTK_ACCESSIBLE (text)->widget;
554   if (widget == NULL)
555     /* State is defunct */
556     return FALSE;
557
558   entry = GTK_ENTRY (widget);
559
560   gtk_editable_set_position (GTK_EDITABLE (entry), offset);
561   return TRUE;
562 }
563
564 static AtkAttributeSet*
565 gail_entry_get_run_attributes (AtkText *text,
566                                gint    offset,
567                                gint    *start_offset,
568                                gint    *end_offset)
569 {
570   GtkWidget *widget;
571   GtkEntry *entry;
572   AtkAttributeSet *at_set = NULL;
573   GtkTextDirection dir;
574
575   widget = GTK_ACCESSIBLE (text)->widget;
576   if (widget == NULL)
577     /* State is defunct */
578     return NULL;
579
580   entry = GTK_ENTRY (widget);
581  
582   dir = gtk_widget_get_direction (widget);
583   if (dir == GTK_TEXT_DIR_RTL)
584     {
585       at_set = gail_misc_add_attribute (at_set,
586                                         ATK_TEXT_ATTR_DIRECTION,
587        g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_DIRECTION, dir)));
588     }
589
590   at_set = gail_misc_layout_get_run_attributes (at_set,
591                                                 gtk_entry_get_layout (entry),
592                                                 entry->text,
593                                                 offset,
594                                                 start_offset,
595                                                 end_offset);
596   return at_set;
597 }
598
599 static AtkAttributeSet*
600 gail_entry_get_default_attributes (AtkText *text)
601 {
602   GtkWidget *widget;
603   GtkEntry *entry;
604   AtkAttributeSet *at_set = NULL;
605
606   widget = GTK_ACCESSIBLE (text)->widget;
607   if (widget == NULL)
608     /* State is defunct */
609     return NULL;
610
611   entry = GTK_ENTRY (widget);
612
613   at_set = gail_misc_get_default_attributes (at_set,
614                                              gtk_entry_get_layout (entry),
615                                              widget);
616   return at_set;
617 }
618   
619 static void
620 gail_entry_get_character_extents (AtkText *text,
621                                   gint    offset,
622                                   gint    *x,
623                                   gint    *y,
624                                   gint    *width,
625                                   gint    *height,
626                                   AtkCoordType coords)
627 {
628   GtkWidget *widget;
629   GtkEntry *entry;
630   PangoRectangle char_rect;
631   gint index, cursor_index, x_layout, y_layout;
632
633   widget = GTK_ACCESSIBLE (text)->widget;
634   if (widget == NULL)
635     /* State is defunct */
636     return;
637
638   entry = GTK_ENTRY (widget);
639
640   gtk_entry_get_layout_offsets (entry, &x_layout, &y_layout);
641   index = g_utf8_offset_to_pointer (entry->text, offset) - entry->text;
642   cursor_index = g_utf8_offset_to_pointer (entry->text, entry->current_pos) - 
643                           entry->text;
644   if (index > cursor_index)
645     index += entry->preedit_length;
646   pango_layout_index_to_pos (gtk_entry_get_layout(entry), index, &char_rect);
647  
648   gail_misc_get_extents_from_pango_rectangle (widget, &char_rect, 
649                         x_layout, y_layout, x, y, width, height, coords);
650
651
652 static gint 
653 gail_entry_get_offset_at_point (AtkText *text,
654                                 gint x,
655                                 gint y,
656                                 AtkCoordType coords)
657
658   GtkWidget *widget;
659   GtkEntry *entry;
660   gint index, cursor_index, x_layout, y_layout;
661
662   widget = GTK_ACCESSIBLE (text)->widget;
663   if (widget == NULL)
664     /* State is defunct */
665     return -1;
666
667   entry = GTK_ENTRY (widget);
668   
669   gtk_entry_get_layout_offsets (entry, &x_layout, &y_layout);
670   
671   index = gail_misc_get_index_at_point_in_layout (widget, 
672                gtk_entry_get_layout(entry), x_layout, y_layout, x, y, coords);
673   if (index == -1)
674     {
675       if (coords == ATK_XY_SCREEN || coords == ATK_XY_WINDOW)
676         return g_utf8_strlen (entry->text, -1);
677
678       return index;  
679     }
680   else
681     {
682       cursor_index = g_utf8_offset_to_pointer (entry->text, 
683                                                entry->current_pos) -
684                                              entry->text;
685       if (index >= cursor_index && entry->preedit_length)
686         {
687           if (index >= cursor_index + entry->preedit_length)
688             index -= entry->preedit_length;
689           else
690             index = cursor_index;
691         }
692       return g_utf8_pointer_to_offset (entry->text, entry->text + index);
693     }
694 }
695
696 static gint
697 gail_entry_get_n_selections (AtkText              *text)
698 {
699   GtkEntry *entry;
700   GtkWidget *widget;
701   gint select_start, select_end;
702
703   widget = GTK_ACCESSIBLE (text)->widget;
704   if (widget == NULL)
705     /* State is defunct */
706     return -1;
707
708   entry = GTK_ENTRY (widget);
709
710   gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &select_start, 
711                                      &select_end);
712
713   if (select_start != select_end)
714     return 1;
715   else
716     return 0;
717 }
718
719 static gchar*
720 gail_entry_get_selection (AtkText *text,
721                           gint    selection_num,
722                           gint    *start_pos,
723                           gint    *end_pos)
724 {
725   GtkEntry *entry;
726   GtkWidget *widget;
727
728   widget = GTK_ACCESSIBLE (text)->widget;
729   if (widget == NULL)
730     /* State is defunct */
731     return NULL;
732
733  /* Only let the user get the selection if one is set, and if the
734   * selection_num is 0.
735   */
736   if (selection_num != 0)
737      return NULL;
738
739   entry = GTK_ENTRY (widget);
740   gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), start_pos, end_pos);
741
742   if (*start_pos != *end_pos)
743      return gtk_editable_get_chars (GTK_EDITABLE (entry), *start_pos, *end_pos);
744   else
745      return NULL;
746 }
747
748 static gboolean
749 gail_entry_add_selection (AtkText *text,
750                           gint    start_pos,
751                           gint    end_pos)
752 {
753   GtkEntry *entry;
754   GtkWidget *widget;
755   gint select_start, select_end;
756
757   widget = GTK_ACCESSIBLE (text)->widget;
758   if (widget == NULL)
759     /* State is defunct */
760     return FALSE;
761
762   entry = GTK_ENTRY (widget);
763
764   gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &select_start, 
765                                      &select_end);
766
767  /* If there is already a selection, then don't allow another to be added,
768   * since GtkEntry only supports one selected region.
769   */
770   if (select_start == select_end)
771     {
772        gtk_editable_select_region (GTK_EDITABLE (entry), start_pos, end_pos);
773        return TRUE;
774     }
775   else
776    return FALSE;
777 }
778
779 static gboolean
780 gail_entry_remove_selection (AtkText *text,
781                              gint    selection_num)
782 {
783   GtkEntry *entry;
784   GtkWidget *widget;
785   gint select_start, select_end, caret_pos;
786
787   widget = GTK_ACCESSIBLE (text)->widget;
788   if (widget == NULL)
789     /* State is defunct */
790     return FALSE;
791
792   if (selection_num != 0)
793      return FALSE;
794
795   entry = GTK_ENTRY (widget);
796   gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &select_start, 
797                                      &select_end);
798
799   if (select_start != select_end)
800     {
801      /* Setting the start & end of the selected region to the caret position
802       * turns off the selection.
803       */
804       caret_pos = gtk_editable_get_position (GTK_EDITABLE (entry));
805       gtk_editable_select_region (GTK_EDITABLE (entry), caret_pos, caret_pos);
806       return TRUE;
807     }
808   else
809     return FALSE;
810 }
811
812 static gboolean
813 gail_entry_set_selection (AtkText *text,
814                           gint    selection_num,
815                           gint    start_pos,
816                           gint    end_pos)
817 {
818   GtkEntry *entry;
819   GtkWidget *widget;
820   gint select_start, select_end;
821
822   widget = GTK_ACCESSIBLE (text)->widget;
823   if (widget == NULL)
824     /* State is defunct */
825     return FALSE;
826
827  /* Only let the user move the selection if one is set, and if the
828   * selection_num is 0
829   */
830   if (selection_num != 0)
831      return FALSE;
832
833   entry = GTK_ENTRY (widget);
834
835   gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &select_start, 
836                                      &select_end);
837
838   if (select_start != select_end)
839     {
840       gtk_editable_select_region (GTK_EDITABLE (entry), start_pos, end_pos);
841       return TRUE;
842     }
843   else
844     return FALSE;
845 }
846
847 static void
848 atk_editable_text_interface_init (AtkEditableTextIface *iface)
849 {
850   iface->set_text_contents = gail_entry_set_text_contents;
851   iface->insert_text = gail_entry_insert_text;
852   iface->copy_text = gail_entry_copy_text;
853   iface->cut_text = gail_entry_cut_text;
854   iface->delete_text = gail_entry_delete_text;
855   iface->paste_text = gail_entry_paste_text;
856   iface->set_run_attributes = NULL;
857 }
858
859 static void
860 gail_entry_set_text_contents (AtkEditableText *text,
861                               const gchar     *string)
862 {
863   GtkEntry *entry;
864   GtkWidget *widget;
865   GtkEditable *editable;
866
867   widget = GTK_ACCESSIBLE (text)->widget;
868   if (widget == NULL)
869     /* State is defunct */
870     return;
871
872   entry = GTK_ENTRY (widget);
873   editable = GTK_EDITABLE (entry);
874   if (!gtk_editable_get_editable (editable))
875     return;
876
877   gtk_entry_set_text (entry, string);
878 }
879
880 static void
881 gail_entry_insert_text (AtkEditableText *text,
882                         const gchar     *string,
883                         gint            length,
884                         gint            *position)
885 {
886   GtkEntry *entry;
887   GtkWidget *widget;
888   GtkEditable *editable;
889
890   widget = GTK_ACCESSIBLE (text)->widget;
891   if (widget == NULL)
892     /* State is defunct */
893     return;
894
895   entry = GTK_ENTRY (widget);
896   editable = GTK_EDITABLE (entry);
897   if (!gtk_editable_get_editable (editable))
898     return;
899
900   gtk_editable_insert_text (editable, string, length, position);
901 }
902
903 static void
904 gail_entry_copy_text   (AtkEditableText *text,
905                         gint            start_pos,
906                         gint            end_pos)
907 {
908   GtkEntry *entry;
909   GtkWidget *widget;
910   GtkEditable *editable;
911   gchar *str;
912
913   widget = GTK_ACCESSIBLE (text)->widget;
914   if (widget == NULL)
915     /* State is defunct */
916     return;
917
918   entry = GTK_ENTRY (widget);
919   editable = GTK_EDITABLE (entry);
920   str = gtk_editable_get_chars (editable, start_pos, end_pos);
921   gtk_clipboard_set_text (gtk_clipboard_get (GDK_NONE), str, -1);
922 }
923
924 static void
925 gail_entry_cut_text (AtkEditableText *text,
926                      gint            start_pos,
927                      gint            end_pos)
928 {
929   GtkEntry *entry;
930   GtkWidget *widget;
931   GtkEditable *editable;
932   gchar *str;
933
934   widget = GTK_ACCESSIBLE (text)->widget;
935   if (widget == NULL)
936     /* State is defunct */
937     return;
938
939   entry = GTK_ENTRY (widget);
940   editable = GTK_EDITABLE (entry);
941   if (!gtk_editable_get_editable (editable))
942     return;
943   str = gtk_editable_get_chars (editable, start_pos, end_pos);
944   gtk_clipboard_set_text (gtk_clipboard_get (GDK_NONE), str, -1);
945   gtk_editable_delete_text (editable, start_pos, end_pos);
946 }
947
948 static void
949 gail_entry_delete_text (AtkEditableText *text,
950                         gint            start_pos,
951                         gint            end_pos)
952 {
953   GtkEntry *entry;
954   GtkWidget *widget;
955   GtkEditable *editable;
956
957   widget = GTK_ACCESSIBLE (text)->widget;
958   if (widget == NULL)
959     /* State is defunct */
960     return;
961
962   entry = GTK_ENTRY (widget);
963   editable = GTK_EDITABLE (entry);
964   if (!gtk_editable_get_editable (editable))
965     return;
966
967   gtk_editable_delete_text (editable, start_pos, end_pos);
968 }
969
970 static void
971 gail_entry_paste_text (AtkEditableText *text,
972                        gint            position)
973 {
974   GtkWidget *widget;
975   GtkEditable *editable;
976   GailEntryPaste paste_struct;
977
978   widget = GTK_ACCESSIBLE (text)->widget;
979   if (widget == NULL)
980     /* State is defunct */
981     return;
982
983   editable = GTK_EDITABLE (widget);
984   if (!gtk_editable_get_editable (editable))
985     return;
986   paste_struct.entry = GTK_ENTRY (widget);
987   paste_struct.position = position;
988
989   g_object_ref (paste_struct.entry);
990   gtk_clipboard_request_text (gtk_clipboard_get(GDK_NONE),
991     gail_entry_paste_received, &paste_struct);
992 }
993
994 static void
995 gail_entry_paste_received (GtkClipboard *clipboard,
996                 const gchar  *text,
997                 gpointer     data)
998 {
999   GailEntryPaste* paste_struct = (GailEntryPaste *)data;
1000
1001   if (text)
1002     gtk_editable_insert_text (GTK_EDITABLE (paste_struct->entry), text, -1,
1003        &(paste_struct->position));
1004
1005   g_object_unref (paste_struct->entry);
1006 }
1007
1008 /* Callbacks */
1009
1010 static gboolean
1011 idle_notify_insert (gpointer data)
1012 {
1013   GailEntry *entry;
1014
1015   entry = GAIL_ENTRY (data);
1016   entry->insert_idle_handler = 0;
1017   gail_entry_notify_insert (entry);
1018
1019   return FALSE;
1020 }
1021
1022 static void
1023 gail_entry_notify_insert (GailEntry *entry)
1024 {
1025   if (entry->signal_name_insert)
1026     {
1027       g_signal_emit_by_name (entry, 
1028                              entry->signal_name_insert,
1029                              entry->position_insert,
1030                              entry->length_insert);
1031       entry->signal_name_insert = NULL;
1032     }
1033 }
1034
1035 /* Note arg1 returns the character at the start of the insert.
1036  * arg2 returns the number of characters inserted.
1037  */
1038 static void 
1039 _gail_entry_insert_text_cb (GtkEntry *entry, 
1040                             gchar    *arg1, 
1041                             gint     arg2,
1042                             gpointer arg3)
1043 {
1044   AtkObject *accessible;
1045   GailEntry *gail_entry;
1046   gint *position = (gint *) arg3;
1047
1048   accessible = gtk_widget_get_accessible (GTK_WIDGET (entry));
1049   gail_entry = GAIL_ENTRY (accessible);
1050   if (!gail_entry->signal_name_insert)
1051     {
1052       gail_entry->signal_name_insert = "text_changed::insert";
1053       gail_entry->position_insert = *position;
1054       gail_entry->length_insert = g_utf8_strlen(arg1, arg2);
1055     }
1056   /*
1057    * The signal will be emitted when the cursor position is updated.
1058    * or in an idle handler if it not updated.
1059    */
1060    if (gail_entry->insert_idle_handler == 0)
1061      gail_entry->insert_idle_handler = gdk_threads_add_idle (idle_notify_insert, gail_entry);
1062 }
1063
1064 static gunichar 
1065 gail_entry_get_character_at_offset (AtkText *text,
1066                                     gint     offset)
1067 {
1068   GtkWidget *widget;
1069   GailEntry *entry;
1070   gchar *string;
1071   gchar *index;
1072   gunichar unichar;
1073
1074   widget = GTK_ACCESSIBLE (text)->widget;
1075   if (widget == NULL)
1076     /* State is defunct */
1077     return '\0';
1078
1079   entry = GAIL_ENTRY (text);
1080   string = gail_text_util_get_substring (entry->textutil, 0, -1);
1081   if (offset >= g_utf8_strlen (string, -1))
1082     {
1083       unichar = '\0';
1084     }
1085   else
1086     {
1087       index = g_utf8_offset_to_pointer (string, offset);
1088
1089       unichar = g_utf8_get_char(index);
1090     }
1091
1092   g_free(string);
1093   return unichar;
1094 }
1095
1096 static void
1097 gail_entry_notify_delete (GailEntry *entry)
1098 {
1099   if (entry->signal_name_delete)
1100     {
1101       g_signal_emit_by_name (entry, 
1102                              entry->signal_name_delete,
1103                              entry->position_delete,
1104                              entry->length_delete);
1105       entry->signal_name_delete = NULL;
1106     }
1107 }
1108
1109 /* Note arg1 returns the start of the delete range, arg2 returns the
1110  * end of the delete range if multiple characters are deleted.  
1111  */
1112 static void 
1113 _gail_entry_delete_text_cb (GtkEntry *entry, 
1114                             gint      arg1, 
1115                             gint      arg2)
1116 {
1117   AtkObject *accessible;
1118   GailEntry *gail_entry;
1119
1120   /*
1121    * Zero length text deleted so ignore
1122    */
1123   if (arg2 - arg1 == 0)
1124     return;
1125
1126   accessible = gtk_widget_get_accessible (GTK_WIDGET (entry));
1127   gail_entry = GAIL_ENTRY (accessible);
1128   if (!gail_entry->signal_name_delete)
1129     {
1130       gail_entry->signal_name_delete = "text_changed::delete";
1131       gail_entry->position_delete = arg1;
1132       gail_entry->length_delete = arg2 - arg1;
1133     }
1134   gail_entry_notify_delete (gail_entry);
1135 }
1136
1137 static void
1138 _gail_entry_changed_cb (GtkEntry *entry)
1139 {
1140   AtkObject *accessible;
1141   GailEntry *gail_entry;
1142
1143   accessible = gtk_widget_get_accessible (GTK_WIDGET (entry));
1144
1145   gail_entry = GAIL_ENTRY (accessible);
1146
1147   text_setup (gail_entry, entry);
1148 }
1149
1150 static gboolean 
1151 check_for_selection_change (GailEntry   *entry,
1152                             GtkEntry    *gtk_entry)
1153 {
1154   gboolean ret_val = FALSE;
1155  
1156   if (gtk_entry->current_pos != gtk_entry->selection_bound)
1157     {
1158       if (gtk_entry->current_pos != entry->cursor_position ||
1159           gtk_entry->selection_bound != entry->selection_bound)
1160         /*
1161          * This check is here as this function can be called
1162          * for notification of selection_bound and current_pos.
1163          * The values of current_pos and selection_bound may be the same 
1164          * for both notifications and we only want to generate one
1165          * text_selection_changed signal.
1166          */
1167         ret_val = TRUE;
1168     }
1169   else 
1170     {
1171       /* We had a selection */
1172       ret_val = (entry->cursor_position != entry->selection_bound);
1173     }
1174   entry->cursor_position = gtk_entry->current_pos;
1175   entry->selection_bound = gtk_entry->selection_bound;
1176
1177   return ret_val;
1178 }
1179
1180 static void
1181 atk_action_interface_init (AtkActionIface *iface)
1182 {
1183   iface->do_action = gail_entry_do_action;
1184   iface->get_n_actions = gail_entry_get_n_actions;
1185   iface->get_description = gail_entry_get_description;
1186   iface->get_keybinding = gail_entry_get_keybinding;
1187   iface->get_name = gail_entry_action_get_name;
1188   iface->set_description = gail_entry_set_description;
1189 }
1190
1191 static gboolean
1192 gail_entry_do_action (AtkAction *action,
1193                       gint      i)
1194 {
1195   GailEntry *entry;
1196   GtkWidget *widget;
1197   gboolean return_value = TRUE;
1198
1199   entry = GAIL_ENTRY (action);
1200   widget = GTK_ACCESSIBLE (action)->widget;
1201   if (widget == NULL)
1202     /*
1203      * State is defunct
1204      */
1205     return FALSE;
1206
1207   if (!GTK_WIDGET_SENSITIVE (widget) || !GTK_WIDGET_VISIBLE (widget))
1208     return FALSE;
1209
1210   switch (i)
1211     {
1212     case 0:
1213       if (entry->action_idle_handler)
1214         return_value = FALSE;
1215       else
1216         entry->action_idle_handler = gdk_threads_add_idle (idle_do_action, entry);
1217       break;
1218     default:
1219       return_value = FALSE;
1220       break;
1221     }
1222   return return_value; 
1223 }
1224
1225 static gboolean
1226 idle_do_action (gpointer data)
1227 {
1228   GailEntry *entry;
1229   GtkWidget *widget;
1230
1231   entry = GAIL_ENTRY (data);
1232   entry->action_idle_handler = 0;
1233   widget = GTK_ACCESSIBLE (entry)->widget;
1234   if (widget == NULL /* State is defunct */ ||
1235       !GTK_WIDGET_SENSITIVE (widget) || !GTK_WIDGET_VISIBLE (widget))
1236     return FALSE;
1237
1238   gtk_widget_activate (widget);
1239
1240   return FALSE;
1241 }
1242
1243 static gint
1244 gail_entry_get_n_actions (AtkAction *action)
1245 {
1246   return 1;
1247 }
1248
1249 static G_CONST_RETURN gchar*
1250 gail_entry_get_description (AtkAction *action,
1251                             gint      i)
1252 {
1253   GailEntry *entry;
1254   G_CONST_RETURN gchar *return_value;
1255
1256   entry = GAIL_ENTRY (action);
1257   switch (i)
1258     {
1259     case 0:
1260       return_value = entry->activate_description;
1261       break;
1262     default:
1263       return_value = NULL;
1264       break;
1265     }
1266   return return_value; 
1267 }
1268
1269 static G_CONST_RETURN gchar*
1270 gail_entry_get_keybinding (AtkAction *action,
1271                            gint      i)
1272 {
1273   GailEntry *entry;
1274   gchar *return_value = NULL;
1275
1276   entry = GAIL_ENTRY (action);
1277   switch (i)
1278     {
1279     case 0:
1280       {
1281         /*
1282          * We look for a mnemonic on the label
1283          */
1284         GtkWidget *widget;
1285         GtkWidget *label;
1286         AtkRelationSet *set;
1287         AtkRelation *relation;
1288         GPtrArray *target;
1289         gpointer target_object;
1290         guint key_val; 
1291
1292         entry = GAIL_ENTRY (action);
1293         widget = GTK_ACCESSIBLE (entry)->widget;
1294         if (widget == NULL)
1295           /*
1296            * State is defunct
1297            */
1298           return NULL;
1299
1300         /* Find labelled-by relation */
1301
1302         set = atk_object_ref_relation_set (ATK_OBJECT (action));
1303         if (!set)
1304           return NULL;
1305         label = NULL;
1306         relation = atk_relation_set_get_relation_by_type (set, ATK_RELATION_LABELLED_BY);
1307         if (relation)
1308           {              
1309             target = atk_relation_get_target (relation);
1310           
1311             target_object = g_ptr_array_index (target, 0);
1312             if (GTK_IS_ACCESSIBLE (target_object))
1313               {
1314                 label = GTK_ACCESSIBLE (target_object)->widget;
1315               } 
1316           }
1317
1318         g_object_unref (set);
1319
1320         if (GTK_IS_LABEL (label))
1321           {
1322             key_val = gtk_label_get_mnemonic_keyval (GTK_LABEL (label)); 
1323             if (key_val != GDK_VoidSymbol)
1324               return_value = gtk_accelerator_name (key_val, GDK_MOD1_MASK);
1325           }
1326         g_free (entry->activate_keybinding);
1327         entry->activate_keybinding = return_value;
1328         break;
1329       }
1330     default:
1331       break;
1332     }
1333   return return_value; 
1334 }
1335
1336 static G_CONST_RETURN gchar*
1337 gail_entry_action_get_name (AtkAction *action,
1338                             gint      i)
1339 {
1340   G_CONST_RETURN gchar *return_value;
1341
1342   switch (i)
1343     {
1344     case 0:
1345       return_value = "activate";
1346       break;
1347     default:
1348       return_value = NULL;
1349       break;
1350   }
1351   return return_value; 
1352 }
1353
1354 static gboolean
1355 gail_entry_set_description (AtkAction      *action,
1356                             gint           i,
1357                             const gchar    *desc)
1358 {
1359   GailEntry *entry;
1360   gchar **value;
1361
1362   entry = GAIL_ENTRY (action);
1363   switch (i)
1364     {
1365     case 0:
1366       value = &entry->activate_description;
1367       break;
1368     default:
1369       value = NULL;
1370       break;
1371     }
1372
1373   if (value)
1374     {
1375       g_free (*value);
1376       *value = g_strdup (desc);
1377       return TRUE;
1378     }
1379   else
1380     return FALSE;
1381 }