]> Pileus Git - ~andy/gtk/blob - modules/other/gail/gailentry.c
9da1f4517534bac779827c7c3a633bf6879128b6
[~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 gboolean   gail_entry_idle_notify_insert    (gpointer data);
135 static void       gail_entry_notify_insert         (GailEntry            *entry);
136 static void       gail_entry_notify_delete         (GailEntry            *entry);
137 static void       _gail_entry_insert_text_cb       (GtkEntry             *entry,
138                                                     gchar                *arg1,
139                                                     gint                 arg2,
140                                                     gpointer             arg3);
141 static void       _gail_entry_delete_text_cb       (GtkEntry             *entry,
142                                                     gint                 arg1,
143                                                     gint                 arg2);
144 static void       _gail_entry_changed_cb           (GtkEntry             *entry);
145 static gboolean   check_for_selection_change       (GailEntry            *entry,
146                                                     GtkEntry             *gtk_entry);
147
148 static void                  atk_action_interface_init   (AtkActionIface  *iface);
149
150 static gboolean              gail_entry_do_action        (AtkAction       *action,
151                                                           gint            i);
152 static gboolean              idle_do_action              (gpointer        data);
153 static gint                  gail_entry_get_n_actions    (AtkAction       *action);
154 static G_CONST_RETURN gchar* gail_entry_get_description  (AtkAction       *action,
155                                                           gint            i);
156 static G_CONST_RETURN gchar* gail_entry_get_keybinding   (AtkAction       *action,
157                                                           gint            i);
158 static G_CONST_RETURN gchar* gail_entry_action_get_name  (AtkAction       *action,
159                                                           gint            i);
160 static gboolean              gail_entry_set_description  (AtkAction       *action,
161                                                           gint            i,
162                                                           const gchar     *desc);
163
164 typedef struct _GailEntryPaste                  GailEntryPaste;
165
166 struct _GailEntryPaste
167 {
168   GtkEntry* entry;
169   gint position;
170 };
171
172 G_DEFINE_TYPE_WITH_CODE (GailEntry, gail_entry, GAIL_TYPE_WIDGET,
173                          G_IMPLEMENT_INTERFACE (ATK_TYPE_EDITABLE_TEXT, atk_editable_text_interface_init)
174                          G_IMPLEMENT_INTERFACE (ATK_TYPE_TEXT, atk_text_interface_init)
175                          G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init))
176
177 static void
178 gail_entry_class_init (GailEntryClass *klass)
179 {
180   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
181   AtkObjectClass  *class = ATK_OBJECT_CLASS (klass);
182   GailWidgetClass *widget_class;
183
184   widget_class = (GailWidgetClass*)klass;
185
186   gobject_class->finalize = gail_entry_finalize;
187
188   class->ref_state_set = gail_entry_ref_state_set;
189   class->get_index_in_parent = gail_entry_get_index_in_parent;
190   class->initialize = gail_entry_real_initialize;
191
192   widget_class->notify_gtk = gail_entry_real_notify_gtk;
193 }
194
195 static void
196 gail_entry_init (GailEntry *entry)
197 {
198   entry->textutil = NULL;
199   entry->signal_name_insert = NULL;
200   entry->signal_name_delete = NULL;
201   entry->cursor_position = 0;
202   entry->selection_bound = 0;
203   entry->activate_description = NULL;
204   entry->activate_keybinding = NULL;
205 }
206
207 static void
208 gail_entry_real_initialize (AtkObject *obj, 
209                             gpointer  data)
210 {
211   GtkEntry *entry;
212   GailEntry *gail_entry;
213
214   ATK_OBJECT_CLASS (gail_entry_parent_class)->initialize (obj, data);
215
216   gail_entry = GAIL_ENTRY (obj);
217   gail_entry->textutil = gail_text_util_new ();
218   
219   g_assert (GTK_IS_ENTRY (data));
220
221   entry = GTK_ENTRY (data);
222   text_setup (gail_entry, entry);
223   gail_entry->cursor_position = entry->current_pos;
224   gail_entry->selection_bound = entry->selection_bound;
225
226   /* Set up signal callbacks */
227   g_signal_connect (data, "insert-text",
228         G_CALLBACK (_gail_entry_insert_text_cb), NULL);
229   g_signal_connect (data, "delete-text",
230         G_CALLBACK (_gail_entry_delete_text_cb), NULL);
231   g_signal_connect (data, "changed",
232         G_CALLBACK (_gail_entry_changed_cb), NULL);
233
234   if (gtk_entry_get_visibility (entry))
235     obj->role = ATK_ROLE_TEXT;
236   else
237     obj->role = ATK_ROLE_PASSWORD_TEXT;
238 }
239
240 static void
241 gail_entry_real_notify_gtk (GObject             *obj,
242                             GParamSpec          *pspec)
243 {
244   GtkWidget *widget;
245   AtkObject* atk_obj;
246   GtkEntry* gtk_entry;
247   GailEntry* entry;
248
249   widget = GTK_WIDGET (obj);
250   atk_obj = gtk_widget_get_accessible (widget);
251   gtk_entry = GTK_ENTRY (widget);
252   entry = GAIL_ENTRY (atk_obj);
253
254   if (strcmp (pspec->name, "cursor-position") == 0)
255     {
256       if (entry->insert_idle_handler == 0)
257         entry->insert_idle_handler = gdk_threads_add_idle (gail_entry_idle_notify_insert, entry);
258
259       if (check_for_selection_change (entry, gtk_entry))
260         g_signal_emit_by_name (atk_obj, "text_selection_changed");
261       /*
262        * The entry cursor position has moved so generate the signal.
263        */
264       g_signal_emit_by_name (atk_obj, "text_caret_moved", 
265                              entry->cursor_position);
266     }
267   else if (strcmp (pspec->name, "selection-bound") == 0)
268     {
269       if (entry->insert_idle_handler == 0)
270         entry->insert_idle_handler = gdk_threads_add_idle (gail_entry_idle_notify_insert, entry);
271
272       if (check_for_selection_change (entry, gtk_entry))
273         g_signal_emit_by_name (atk_obj, "text_selection_changed");
274     }
275   else if (strcmp (pspec->name, "editable") == 0)
276     {
277       gboolean value;
278
279       g_object_get (obj, "editable", &value, NULL);
280       atk_object_notify_state_change (atk_obj, ATK_STATE_EDITABLE,
281                                                value);
282     }
283   else if (strcmp (pspec->name, "visibility") == 0)
284     {
285       gboolean visibility;
286       AtkRole new_role;
287
288       text_setup (entry, gtk_entry);
289       visibility = gtk_entry_get_visibility (gtk_entry);
290       new_role = visibility ? ATK_ROLE_TEXT : ATK_ROLE_PASSWORD_TEXT;
291       atk_object_set_role (atk_obj, new_role);
292     }
293   else if (strcmp (pspec->name, "invisible-char") == 0)
294     {
295       text_setup (entry, gtk_entry);
296     }
297   else
298     GAIL_WIDGET_CLASS (gail_entry_parent_class)->notify_gtk (obj, pspec);
299 }
300
301 static void
302 text_setup (GailEntry *entry,
303             GtkEntry  *gtk_entry)
304 {
305   if (gtk_entry_get_visibility (gtk_entry))
306     {
307       gail_text_util_text_setup (entry->textutil, gtk_entry_get_text (gtk_entry));
308     }
309   else
310     {
311       gunichar invisible_char;
312       GString *tmp_string = g_string_new (NULL);
313       gint ch_len; 
314       gchar buf[7];
315       guint length;
316       gint i;
317
318       invisible_char = gtk_entry_get_invisible_char (gtk_entry);
319       if (invisible_char == 0)
320         invisible_char = ' ';
321
322       ch_len = g_unichar_to_utf8 (invisible_char, buf);
323       length = gtk_entry_get_text_length (gtk_entry);
324       for (i = 0; i < length; i++)
325         {
326           g_string_append_len (tmp_string, buf, ch_len);
327         }
328
329       gail_text_util_text_setup (entry->textutil, tmp_string->str);
330       g_string_free (tmp_string, TRUE);
331
332     } 
333 }
334
335 static void
336 gail_entry_finalize (GObject            *object)
337 {
338   GailEntry *entry = GAIL_ENTRY (object);
339
340   g_object_unref (entry->textutil);
341   g_free (entry->activate_description);
342   g_free (entry->activate_keybinding);
343   if (entry->action_idle_handler)
344     {
345       g_source_remove (entry->action_idle_handler);
346       entry->action_idle_handler = 0;
347     }
348   if (entry->insert_idle_handler)
349     {
350       g_source_remove (entry->insert_idle_handler);
351       entry->insert_idle_handler = 0;
352     }
353   G_OBJECT_CLASS (gail_entry_parent_class)->finalize (object);
354 }
355
356 static gint
357 gail_entry_get_index_in_parent (AtkObject *accessible)
358 {
359   /*
360    * If the parent widget is a combo box then the index is 1
361    * otherwise do the normal thing.
362    */
363   if (accessible->accessible_parent)
364     if (GAIL_IS_COMBO (accessible->accessible_parent) ||
365         GAIL_IS_COMBO_BOX (accessible->accessible_parent))
366       return 1;
367
368   return ATK_OBJECT_CLASS (gail_entry_parent_class)->get_index_in_parent (accessible);
369 }
370
371 /* atkobject.h */
372
373 static AtkStateSet*
374 gail_entry_ref_state_set (AtkObject *accessible)
375 {
376   AtkStateSet *state_set;
377   GtkEntry *entry;
378   gboolean value;
379   GtkWidget *widget;
380
381   state_set = ATK_OBJECT_CLASS (gail_entry_parent_class)->ref_state_set (accessible);
382   widget = GTK_ACCESSIBLE (accessible)->widget;
383   
384   if (widget == NULL)
385     return state_set;
386
387   entry = GTK_ENTRY (widget);
388
389   g_object_get (G_OBJECT (entry), "editable", &value, NULL);
390   if (value)
391     atk_state_set_add_state (state_set, ATK_STATE_EDITABLE);
392   atk_state_set_add_state (state_set, ATK_STATE_SINGLE_LINE);
393
394   return state_set;
395 }
396
397 /* atktext.h */
398
399 static void
400 atk_text_interface_init (AtkTextIface *iface)
401 {
402   iface->get_text = gail_entry_get_text;
403   iface->get_character_at_offset = gail_entry_get_character_at_offset;
404   iface->get_text_before_offset = gail_entry_get_text_before_offset;
405   iface->get_text_at_offset = gail_entry_get_text_at_offset;
406   iface->get_text_after_offset = gail_entry_get_text_after_offset;
407   iface->get_caret_offset = gail_entry_get_caret_offset;
408   iface->set_caret_offset = gail_entry_set_caret_offset;
409   iface->get_character_count = gail_entry_get_character_count;
410   iface->get_n_selections = gail_entry_get_n_selections;
411   iface->get_selection = gail_entry_get_selection;
412   iface->add_selection = gail_entry_add_selection;
413   iface->remove_selection = gail_entry_remove_selection;
414   iface->set_selection = gail_entry_set_selection;
415   iface->get_run_attributes = gail_entry_get_run_attributes;
416   iface->get_default_attributes = gail_entry_get_default_attributes;
417   iface->get_character_extents = gail_entry_get_character_extents;
418   iface->get_offset_at_point = gail_entry_get_offset_at_point;
419 }
420
421 static gchar*
422 gail_entry_get_text (AtkText *text,
423                      gint    start_pos,
424                      gint    end_pos)
425 {
426   GtkWidget *widget;
427
428   widget = GTK_ACCESSIBLE (text)->widget;
429   if (widget == NULL)
430     /* State is defunct */
431     return NULL;
432
433   return gail_text_util_get_substring (GAIL_ENTRY (text)->textutil, start_pos, end_pos);
434 }
435
436 static gchar*
437 gail_entry_get_text_before_offset (AtkText          *text,
438                                    gint             offset,
439                                    AtkTextBoundary  boundary_type,
440                                    gint             *start_offset,
441                                    gint             *end_offset)
442 {
443   GtkWidget *widget;
444   GtkEntry *entry;
445
446   widget = GTK_ACCESSIBLE (text)->widget;
447   if (widget == NULL)
448     /* State is defunct */
449     return NULL;
450
451   /* Get Entry */
452   entry = GTK_ENTRY (widget);
453
454   return gail_text_util_get_text (GAIL_ENTRY (text)->textutil,
455                           gtk_entry_get_layout (entry), GAIL_BEFORE_OFFSET, 
456                           boundary_type, offset, start_offset, end_offset);
457 }
458
459 static gchar*
460 gail_entry_get_text_at_offset (AtkText          *text,
461                                gint             offset,
462                                AtkTextBoundary  boundary_type,
463                                gint             *start_offset,
464                                gint             *end_offset)
465 {
466   GtkWidget *widget;
467   GtkEntry *entry;
468
469   widget = GTK_ACCESSIBLE (text)->widget;
470   if (widget == NULL)
471     /* State is defunct */
472     return NULL;
473
474   /* Get Entry */
475   entry = GTK_ENTRY (widget);
476
477   return gail_text_util_get_text (GAIL_ENTRY (text)->textutil,
478                             gtk_entry_get_layout (entry), GAIL_AT_OFFSET, 
479                             boundary_type, offset, start_offset, end_offset);
480 }
481
482 static gchar*
483 gail_entry_get_text_after_offset  (AtkText          *text,
484                                    gint             offset,
485                                    AtkTextBoundary  boundary_type,
486                                    gint             *start_offset,
487                                    gint             *end_offset)
488 {
489   GtkWidget *widget;
490   GtkEntry *entry;
491
492   widget = GTK_ACCESSIBLE (text)->widget;
493   if (widget == NULL)
494     /* State is defunct */
495     return NULL;
496
497   /* Get Entry */
498   entry = GTK_ENTRY (widget);
499
500   return gail_text_util_get_text (GAIL_ENTRY (text)->textutil,
501                            gtk_entry_get_layout (entry), GAIL_AFTER_OFFSET, 
502                            boundary_type, offset, start_offset, end_offset);
503 }
504
505 static gint
506 gail_entry_get_character_count (AtkText *text)
507 {
508   GtkEntry *entry;
509   GtkWidget *widget;
510
511   widget = GTK_ACCESSIBLE (text)->widget;
512   if (widget == NULL)
513     /* State is defunct */
514     return 0;
515
516   entry = GTK_ENTRY (widget);
517   return g_utf8_strlen (gtk_entry_get_text (entry), -1);
518 }
519
520 static gint
521 gail_entry_get_caret_offset (AtkText *text)
522 {
523   GtkEntry *entry;
524   GtkWidget *widget;
525
526   widget = GTK_ACCESSIBLE (text)->widget;
527   if (widget == NULL)
528     /* State is defunct */
529     return 0;
530
531   entry = GTK_ENTRY (widget);
532
533   return gtk_editable_get_position (GTK_EDITABLE (entry));
534 }
535
536 static gboolean
537 gail_entry_set_caret_offset (AtkText *text, gint offset)
538 {
539   GtkEntry *entry;
540   GtkWidget *widget;
541
542   widget = GTK_ACCESSIBLE (text)->widget;
543   if (widget == NULL)
544     /* State is defunct */
545     return FALSE;
546
547   entry = GTK_ENTRY (widget);
548
549   gtk_editable_set_position (GTK_EDITABLE (entry), offset);
550   return TRUE;
551 }
552
553 static AtkAttributeSet*
554 gail_entry_get_run_attributes (AtkText *text,
555                                gint    offset,
556                                gint    *start_offset,
557                                gint    *end_offset)
558 {
559   GtkWidget *widget;
560   GtkEntry *entry;
561   AtkAttributeSet *at_set = NULL;
562   GtkTextDirection dir;
563
564   widget = GTK_ACCESSIBLE (text)->widget;
565   if (widget == NULL)
566     /* State is defunct */
567     return NULL;
568
569   entry = GTK_ENTRY (widget);
570  
571   dir = gtk_widget_get_direction (widget);
572   if (dir == GTK_TEXT_DIR_RTL)
573     {
574       at_set = gail_misc_add_attribute (at_set,
575                                         ATK_TEXT_ATTR_DIRECTION,
576        g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_DIRECTION, dir)));
577     }
578
579   at_set = gail_misc_layout_get_run_attributes (at_set,
580                                                 gtk_entry_get_layout (entry),
581                                                 (gchar*)gtk_entry_get_text (entry),
582                                                 offset,
583                                                 start_offset,
584                                                 end_offset);
585   return at_set;
586 }
587
588 static AtkAttributeSet*
589 gail_entry_get_default_attributes (AtkText *text)
590 {
591   GtkWidget *widget;
592   GtkEntry *entry;
593   AtkAttributeSet *at_set = NULL;
594
595   widget = GTK_ACCESSIBLE (text)->widget;
596   if (widget == NULL)
597     /* State is defunct */
598     return NULL;
599
600   entry = GTK_ENTRY (widget);
601
602   at_set = gail_misc_get_default_attributes (at_set,
603                                              gtk_entry_get_layout (entry),
604                                              widget);
605   return at_set;
606 }
607   
608 static void
609 gail_entry_get_character_extents (AtkText *text,
610                                   gint    offset,
611                                   gint    *x,
612                                   gint    *y,
613                                   gint    *width,
614                                   gint    *height,
615                                   AtkCoordType coords)
616 {
617   GtkWidget *widget;
618   GtkEntry *entry;
619   PangoRectangle char_rect;
620   gint index, cursor_index, x_layout, y_layout;
621   const gchar *entry_text;
622
623   widget = GTK_ACCESSIBLE (text)->widget;
624   if (widget == NULL)
625     /* State is defunct */
626     return;
627
628   entry = GTK_ENTRY (widget);
629
630   gtk_entry_get_layout_offsets (entry, &x_layout, &y_layout);
631   entry_text = gtk_entry_get_text (entry);
632   index = g_utf8_offset_to_pointer (entry_text, offset) - entry_text;
633   cursor_index = g_utf8_offset_to_pointer (entry_text, entry->current_pos) - entry_text;
634   if (index > cursor_index)
635     index += entry->preedit_length;
636   pango_layout_index_to_pos (gtk_entry_get_layout(entry), index, &char_rect);
637  
638   gail_misc_get_extents_from_pango_rectangle (widget, &char_rect, 
639                         x_layout, y_layout, x, y, width, height, coords);
640
641
642 static gint 
643 gail_entry_get_offset_at_point (AtkText *text,
644                                 gint x,
645                                 gint y,
646                                 AtkCoordType coords)
647
648   GtkWidget *widget;
649   GtkEntry *entry;
650   gint index, cursor_index, x_layout, y_layout;
651   const gchar *entry_text;
652   
653   widget = GTK_ACCESSIBLE (text)->widget;
654   if (widget == NULL)
655     /* State is defunct */
656     return -1;
657
658   entry = GTK_ENTRY (widget);
659   
660   gtk_entry_get_layout_offsets (entry, &x_layout, &y_layout);
661   entry_text = gtk_entry_get_text (entry);
662   
663   index = gail_misc_get_index_at_point_in_layout (widget, 
664                gtk_entry_get_layout(entry), x_layout, y_layout, x, y, coords);
665   if (index == -1)
666     {
667       if (coords == ATK_XY_SCREEN || coords == ATK_XY_WINDOW)
668         return g_utf8_strlen (entry_text, -1);
669
670       return index;  
671     }
672   else
673     {
674       cursor_index = g_utf8_offset_to_pointer (entry_text, entry->current_pos) - entry_text;
675       if (index >= cursor_index && entry->preedit_length)
676         {
677           if (index >= cursor_index + entry->preedit_length)
678             index -= entry->preedit_length;
679           else
680             index = cursor_index;
681         }
682       return g_utf8_pointer_to_offset (entry_text, entry_text + index);
683     }
684 }
685
686 static gint
687 gail_entry_get_n_selections (AtkText              *text)
688 {
689   GtkEntry *entry;
690   GtkWidget *widget;
691   gint select_start, select_end;
692
693   widget = GTK_ACCESSIBLE (text)->widget;
694   if (widget == NULL)
695     /* State is defunct */
696     return -1;
697
698   entry = GTK_ENTRY (widget);
699
700   gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &select_start, 
701                                      &select_end);
702
703   if (select_start != select_end)
704     return 1;
705   else
706     return 0;
707 }
708
709 static gchar*
710 gail_entry_get_selection (AtkText *text,
711                           gint    selection_num,
712                           gint    *start_pos,
713                           gint    *end_pos)
714 {
715   GtkEntry *entry;
716   GtkWidget *widget;
717
718   widget = GTK_ACCESSIBLE (text)->widget;
719   if (widget == NULL)
720     /* State is defunct */
721     return NULL;
722
723  /* Only let the user get the selection if one is set, and if the
724   * selection_num is 0.
725   */
726   if (selection_num != 0)
727      return NULL;
728
729   entry = GTK_ENTRY (widget);
730   gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), start_pos, end_pos);
731
732   if (*start_pos != *end_pos)
733      return gtk_editable_get_chars (GTK_EDITABLE (entry), *start_pos, *end_pos);
734   else
735      return NULL;
736 }
737
738 static gboolean
739 gail_entry_add_selection (AtkText *text,
740                           gint    start_pos,
741                           gint    end_pos)
742 {
743   GtkEntry *entry;
744   GtkWidget *widget;
745   gint select_start, select_end;
746
747   widget = GTK_ACCESSIBLE (text)->widget;
748   if (widget == NULL)
749     /* State is defunct */
750     return FALSE;
751
752   entry = GTK_ENTRY (widget);
753
754   gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &select_start, 
755                                      &select_end);
756
757  /* If there is already a selection, then don't allow another to be added,
758   * since GtkEntry only supports one selected region.
759   */
760   if (select_start == select_end)
761     {
762        gtk_editable_select_region (GTK_EDITABLE (entry), start_pos, end_pos);
763        return TRUE;
764     }
765   else
766    return FALSE;
767 }
768
769 static gboolean
770 gail_entry_remove_selection (AtkText *text,
771                              gint    selection_num)
772 {
773   GtkEntry *entry;
774   GtkWidget *widget;
775   gint select_start, select_end, caret_pos;
776
777   widget = GTK_ACCESSIBLE (text)->widget;
778   if (widget == NULL)
779     /* State is defunct */
780     return FALSE;
781
782   if (selection_num != 0)
783      return FALSE;
784
785   entry = GTK_ENTRY (widget);
786   gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &select_start, 
787                                      &select_end);
788
789   if (select_start != select_end)
790     {
791      /* Setting the start & end of the selected region to the caret position
792       * turns off the selection.
793       */
794       caret_pos = gtk_editable_get_position (GTK_EDITABLE (entry));
795       gtk_editable_select_region (GTK_EDITABLE (entry), caret_pos, caret_pos);
796       return TRUE;
797     }
798   else
799     return FALSE;
800 }
801
802 static gboolean
803 gail_entry_set_selection (AtkText *text,
804                           gint    selection_num,
805                           gint    start_pos,
806                           gint    end_pos)
807 {
808   GtkEntry *entry;
809   GtkWidget *widget;
810   gint select_start, select_end;
811
812   widget = GTK_ACCESSIBLE (text)->widget;
813   if (widget == NULL)
814     /* State is defunct */
815     return FALSE;
816
817  /* Only let the user move the selection if one is set, and if the
818   * selection_num is 0
819   */
820   if (selection_num != 0)
821      return FALSE;
822
823   entry = GTK_ENTRY (widget);
824
825   gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &select_start, 
826                                      &select_end);
827
828   if (select_start != select_end)
829     {
830       gtk_editable_select_region (GTK_EDITABLE (entry), start_pos, end_pos);
831       return TRUE;
832     }
833   else
834     return FALSE;
835 }
836
837 static void
838 atk_editable_text_interface_init (AtkEditableTextIface *iface)
839 {
840   iface->set_text_contents = gail_entry_set_text_contents;
841   iface->insert_text = gail_entry_insert_text;
842   iface->copy_text = gail_entry_copy_text;
843   iface->cut_text = gail_entry_cut_text;
844   iface->delete_text = gail_entry_delete_text;
845   iface->paste_text = gail_entry_paste_text;
846   iface->set_run_attributes = NULL;
847 }
848
849 static void
850 gail_entry_set_text_contents (AtkEditableText *text,
851                               const gchar     *string)
852 {
853   GtkEntry *entry;
854   GtkWidget *widget;
855   GtkEditable *editable;
856
857   widget = GTK_ACCESSIBLE (text)->widget;
858   if (widget == NULL)
859     /* State is defunct */
860     return;
861
862   entry = GTK_ENTRY (widget);
863   editable = GTK_EDITABLE (entry);
864   if (!gtk_editable_get_editable (editable))
865     return;
866
867   gtk_entry_set_text (entry, string);
868 }
869
870 static void
871 gail_entry_insert_text (AtkEditableText *text,
872                         const gchar     *string,
873                         gint            length,
874                         gint            *position)
875 {
876   GtkEntry *entry;
877   GtkWidget *widget;
878   GtkEditable *editable;
879
880   widget = GTK_ACCESSIBLE (text)->widget;
881   if (widget == NULL)
882     /* State is defunct */
883     return;
884
885   entry = GTK_ENTRY (widget);
886   editable = GTK_EDITABLE (entry);
887   if (!gtk_editable_get_editable (editable))
888     return;
889
890   gtk_editable_insert_text (editable, string, length, position);
891   gtk_editable_set_position (editable, *position);
892 }
893
894 static void
895 gail_entry_copy_text   (AtkEditableText *text,
896                         gint            start_pos,
897                         gint            end_pos)
898 {
899   GtkEntry *entry;
900   GtkWidget *widget;
901   GtkEditable *editable;
902   gchar *str;
903   GtkClipboard *clipboard;
904
905   widget = GTK_ACCESSIBLE (text)->widget;
906   if (widget == NULL)
907     /* State is defunct */
908     return;
909
910   entry = GTK_ENTRY (widget);
911   editable = GTK_EDITABLE (entry);
912   str = gtk_editable_get_chars (editable, start_pos, end_pos);
913   clipboard = gtk_clipboard_get_for_display (gtk_widget_get_display (widget),
914                                              GDK_SELECTION_CLIPBOARD);
915   gtk_clipboard_set_text (clipboard, str, -1);
916 }
917
918 static void
919 gail_entry_cut_text (AtkEditableText *text,
920                      gint            start_pos,
921                      gint            end_pos)
922 {
923   GtkEntry *entry;
924   GtkWidget *widget;
925   GtkEditable *editable;
926   gchar *str;
927   GtkClipboard *clipboard;
928
929   widget = GTK_ACCESSIBLE (text)->widget;
930   if (widget == NULL)
931     /* State is defunct */
932     return;
933
934   entry = GTK_ENTRY (widget);
935   editable = GTK_EDITABLE (entry);
936   if (!gtk_editable_get_editable (editable))
937     return;
938   str = gtk_editable_get_chars (editable, start_pos, end_pos);
939   clipboard = gtk_clipboard_get_for_display (gtk_widget_get_display (widget),
940                                              GDK_SELECTION_CLIPBOARD);
941   gtk_clipboard_set_text (clipboard, str, -1);
942   gtk_editable_delete_text (editable, start_pos, end_pos);
943 }
944
945 static void
946 gail_entry_delete_text (AtkEditableText *text,
947                         gint            start_pos,
948                         gint            end_pos)
949 {
950   GtkEntry *entry;
951   GtkWidget *widget;
952   GtkEditable *editable;
953
954   widget = GTK_ACCESSIBLE (text)->widget;
955   if (widget == NULL)
956     /* State is defunct */
957     return;
958
959   entry = GTK_ENTRY (widget);
960   editable = GTK_EDITABLE (entry);
961   if (!gtk_editable_get_editable (editable))
962     return;
963
964   gtk_editable_delete_text (editable, start_pos, end_pos);
965 }
966
967 static void
968 gail_entry_paste_text (AtkEditableText *text,
969                        gint            position)
970 {
971   GtkWidget *widget;
972   GtkEditable *editable;
973   GailEntryPaste paste_struct;
974   GtkClipboard *clipboard;
975
976   widget = GTK_ACCESSIBLE (text)->widget;
977   if (widget == NULL)
978     /* State is defunct */
979     return;
980
981   editable = GTK_EDITABLE (widget);
982   if (!gtk_editable_get_editable (editable))
983     return;
984   paste_struct.entry = GTK_ENTRY (widget);
985   paste_struct.position = position;
986
987   g_object_ref (paste_struct.entry);
988   clipboard = gtk_clipboard_get_for_display (gtk_widget_get_display (widget),
989                                              GDK_SELECTION_CLIPBOARD);
990   gtk_clipboard_request_text (clipboard,
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 gail_entry_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 (gail_entry_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_get_sensitive (widget) || !gtk_widget_get_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_get_sensitive (widget) || !gtk_widget_get_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 }