Debian Codegraph @

gtk/a11y/gtkentryaccessible.c

raw
1 /* GTK+ - accessibility implementations
2 * Copyright 2001, 2002, 2003 Sun Microsystems Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "config.h"
19
20 #include <glib/gi18n-lib.h>
21 #include <string.h>
22 #include <gtk/gtk.h>
23 #include "gtkpango.h"
24 #include "gtkentryaccessible.h"
25 #include "gtkentryprivate.h"
26 #include "gtkcomboboxaccessible.h"
27 #include "gtkstylecontextprivate.h"
28 #include "gtkwidgetprivate.h"
29
30 #define GTK_TYPE_ENTRY_ICON_ACCESSIBLE (gtk_entry_icon_accessible_get_type ())
31 #define GTK_ENTRY_ICON_ACCESSIBLE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_ENTRY_ICON_ACCESSIBLE, GtkEntryIconAccessible))
32 #define GTK_IS_ENTRY_ICON_ACCESSIBLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_ENTRY_ICON_ACCESSIBLE))
33
34 struct _GtkEntryAccessiblePrivate
35 {
36 gint cursor_position;
37 gint selection_bound;
38 AtkObject *icons[2];
39 };
40
41 typedef struct _GtkEntryIconAccessible GtkEntryIconAccessible;
42 typedef struct _GtkEntryIconAccessibleClass GtkEntryIconAccessibleClass;
43
44 struct _GtkEntryIconAccessible
45 {
46 AtkObject parent;
47
48 GtkEntryAccessible *entry;
49 GtkEntryIconPosition pos;
50 };
51
52 struct _GtkEntryIconAccessibleClass
53 {
54 AtkObjectClass parent_class;
55 };
56
57 static void atk_action_interface_init (AtkActionIface *iface);
58
59 static void icon_atk_action_interface_init (AtkActionIface *iface);
60 static void icon_atk_component_interface_init (AtkComponentIface *iface);
61
62 GType gtk_entry_icon_accessible_get_type (void);
63
64 G_DEFINE_TYPE_WITH_CODE (GtkEntryIconAccessible, gtk_entry_icon_accessible, ATK_TYPE_OBJECT,
65 G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, icon_atk_action_interface_init)
66 G_IMPLEMENT_INTERFACE (ATK_TYPE_COMPONENT, icon_atk_component_interface_init))
67
68 static void
69 gtk_entry_icon_accessible_remove_entry (gpointer data, GObject *obj)
70 {
71 GtkEntryIconAccessible *icon = data;
72
73 if (icon->entry)
74 {
75 icon->entry = NULL;
76 g_object_notify (G_OBJECT (icon), "accessible-parent");
77 atk_object_notify_state_change (ATK_OBJECT (icon), ATK_STATE_DEFUNCT, TRUE);
78 }
79 }
80
81 static AtkObject *
82 gtk_entry_icon_accessible_new (GtkEntryAccessible *entry,
83 GtkEntryIconPosition pos)
84 {
85 GtkEntryIconAccessible *icon;
86 AtkObject *accessible;
87
88 icon = g_object_new (gtk_entry_icon_accessible_get_type (), NULL);
89 icon->entry = entry;
90 g_object_weak_ref (G_OBJECT (entry),
91 gtk_entry_icon_accessible_remove_entry,
92 icon);
93 icon->pos = pos;
94
95 accessible = ATK_OBJECT (icon);
96 atk_object_initialize (accessible, NULL);
97 return accessible;
98 }
99
100 static void
101 gtk_entry_icon_accessible_init (GtkEntryIconAccessible *icon)
102 {
103 }
104
105 static void
106 gtk_entry_icon_accessible_initialize (AtkObject *obj,
107 gpointer data)
108 {
109 GtkEntryIconAccessible *icon = GTK_ENTRY_ICON_ACCESSIBLE (obj);
110 GtkWidget *widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (icon->entry));
111 GtkEntry *gtk_entry = GTK_ENTRY (widget);
112 const gchar *name;
113 gchar *text;
114
115 ATK_OBJECT_CLASS (gtk_entry_icon_accessible_parent_class)->initialize (obj, data);
116 atk_object_set_role (obj, ATK_ROLE_ICON);
117
118 name = gtk_entry_get_icon_name (gtk_entry, icon->pos);
119 if (name)
120 atk_object_set_name (obj, name);
121
122 text = gtk_entry_get_icon_tooltip_text (gtk_entry, icon->pos);
123 if (text)
124 {
125 atk_object_set_description (obj, text);
126 g_free (text);
127 }
128
129 atk_object_set_parent (obj, ATK_OBJECT (icon->entry));
130 }
131
132 static AtkObject *
133 gtk_entry_icon_accessible_get_parent (AtkObject *accessible)
134 {
135 GtkEntryIconAccessible *icon = GTK_ENTRY_ICON_ACCESSIBLE (accessible);
136
137 return ATK_OBJECT (icon->entry);
138 }
139
140 static AtkStateSet *
141 gtk_entry_icon_accessible_ref_state_set (AtkObject *accessible)
142 {
143 GtkEntryIconAccessible *icon = GTK_ENTRY_ICON_ACCESSIBLE (accessible);
144 AtkStateSet *set = atk_state_set_new ();
145 AtkStateSet *entry_set;
146 GtkWidget *widget;
147 GtkEntry *gtk_entry;
148
149 if (!icon->entry)
150 {
151 atk_state_set_add_state (set, ATK_STATE_DEFUNCT);
152 return set;
153 }
154
155 entry_set = atk_object_ref_state_set (ATK_OBJECT (icon->entry));
156 if (!entry_set || atk_state_set_contains_state (entry_set, ATK_STATE_DEFUNCT))
157 {
158 atk_state_set_add_state (set, ATK_STATE_DEFUNCT);
159 g_clear_object (&entry_set);
160 return set;
161 }
162
163 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (icon->entry));
164 gtk_entry = GTK_ENTRY (widget);
165
166 if (atk_state_set_contains_state (entry_set, ATK_STATE_ENABLED))
167 atk_state_set_add_state (set, ATK_STATE_ENABLED);
168 if (atk_state_set_contains_state (entry_set, ATK_STATE_SENSITIVE))
169 atk_state_set_add_state (set, ATK_STATE_SENSITIVE);
170 if (atk_state_set_contains_state (entry_set, ATK_STATE_SHOWING))
171 atk_state_set_add_state (set, ATK_STATE_SHOWING);
172 if (atk_state_set_contains_state (entry_set, ATK_STATE_VISIBLE))
173 atk_state_set_add_state (set, ATK_STATE_VISIBLE);
174
175 if (!gtk_entry_get_icon_sensitive (gtk_entry, icon->pos))
176 atk_state_set_remove_state (set, ATK_STATE_SENSITIVE);
177 if (!gtk_entry_get_icon_activatable (gtk_entry, icon->pos))
178 atk_state_set_remove_state (set, ATK_STATE_ENABLED);
179
180 g_object_unref (entry_set);
181 return set;
182 }
183
184 static void
185 gtk_entry_icon_accessible_invalidate (GtkEntryIconAccessible *icon)
186 {
187 if (!icon->entry)
188 return;
189 g_object_weak_unref (G_OBJECT (icon->entry),
190 gtk_entry_icon_accessible_remove_entry,
191 icon);
192 gtk_entry_icon_accessible_remove_entry (icon, NULL);
193 }
194
195 static void
196 gtk_entry_icon_accessible_finalize (GObject *object)
197 {
198 GtkEntryIconAccessible *icon = GTK_ENTRY_ICON_ACCESSIBLE (object);
199
200 gtk_entry_icon_accessible_invalidate (icon);
201
202 G_OBJECT_CLASS (gtk_entry_icon_accessible_parent_class)->finalize (object);
203 }
204
205 static void
206 gtk_entry_icon_accessible_class_init (GtkEntryIconAccessibleClass *klass)
207 {
208 AtkObjectClass *atk_class = ATK_OBJECT_CLASS (klass);
209 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
210
211 atk_class->initialize = gtk_entry_icon_accessible_initialize;
212 atk_class->get_parent = gtk_entry_icon_accessible_get_parent;
213 atk_class->ref_state_set = gtk_entry_icon_accessible_ref_state_set;
214
215 gobject_class->finalize = gtk_entry_icon_accessible_finalize;
216 }
217
218 static gboolean
219 gtk_entry_icon_accessible_do_action (AtkAction *action,
220 gint i)
221 {
222 GtkEntryIconAccessible *icon = (GtkEntryIconAccessible *)action;
223 GtkWidget *widget;
224 GtkEntry *gtk_entry;
225 GdkEvent event;
226 GdkRectangle icon_area;
227
228 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (icon->entry));
229 if (widget == NULL)
230 return FALSE;
231
232 if (i != 0)
233 return FALSE;
234
235 if (!gtk_widget_is_sensitive (widget) || !gtk_widget_get_visible (widget))
236 return FALSE;
237
238 gtk_entry = GTK_ENTRY (widget);
239
240 if (!gtk_entry_get_icon_sensitive (gtk_entry, icon->pos) ||
241 !gtk_entry_get_icon_activatable (gtk_entry, icon->pos))
242 return FALSE;
243
244 gtk_entry_get_icon_area (gtk_entry, icon->pos, &icon_area);
245 memset (&event, 0, sizeof (event));
246 event.button.type = GDK_BUTTON_PRESS;
247 event.button.window = gtk_widget_get_window (widget);
248 event.button.button = 1;
249 event.button.send_event = TRUE;
250 event.button.time = GDK_CURRENT_TIME;
251 event.button.x = icon_area.x;
252 event.button.y = icon_area.y;
253
254 g_signal_emit_by_name (widget, "icon-press", 0, icon->pos, &event);
255 return TRUE;
256 }
257
258 static gint
259 gtk_entry_icon_accessible_get_n_actions (AtkAction *action)
260 {
261 GtkEntryIconAccessible *icon = GTK_ENTRY_ICON_ACCESSIBLE (action);
262 GtkWidget *widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (icon->entry));
263 GtkEntry *gtk_entry = GTK_ENTRY (widget);
264
265 return (gtk_entry_get_icon_activatable (gtk_entry, icon->pos) ? 1 : 0);
266 }
267
268 static const gchar *
269 gtk_entry_icon_accessible_get_name (AtkAction *action,
270 gint i)
271 {
272 GtkEntryIconAccessible *icon = GTK_ENTRY_ICON_ACCESSIBLE (action);
273 GtkWidget *widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (icon->entry));
274 GtkEntry *gtk_entry = GTK_ENTRY (widget);
275
276 if (i != 0)
277 return NULL;
278 if (!gtk_entry_get_icon_activatable (gtk_entry, icon->pos))
279 return NULL;
280
281 return "activate";
282 }
283
284 static void
285 icon_atk_action_interface_init (AtkActionIface *iface)
286 {
287 iface->do_action = gtk_entry_icon_accessible_do_action;
288 iface->get_n_actions = gtk_entry_icon_accessible_get_n_actions;
289 iface->get_name = gtk_entry_icon_accessible_get_name;
290 }
291
292 static void
293 gtk_entry_icon_accessible_get_extents (AtkComponent *component,
294 gint *x,
295 gint *y,
296 gint *width,
297 gint *height,
298 AtkCoordType coord_type)
299 {
300 GtkEntryIconAccessible *icon = GTK_ENTRY_ICON_ACCESSIBLE (component);
301 GdkRectangle icon_area;
302 GtkEntry *gtk_entry;
303 GtkWidget *widget;
304
305 *x = G_MININT;
306 atk_component_get_extents (ATK_COMPONENT (icon->entry), x, y, width, height,
307 coord_type);
308 if (*x == G_MININT)
309 return;
310
311 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (icon->entry));
312 gtk_entry = GTK_ENTRY (widget);
313 gtk_entry_get_icon_area (gtk_entry, icon->pos, &icon_area);
314 *width = icon_area.width;
315 *height = icon_area.height;
316 *x += icon_area.x;
317 *y += icon_area.y;
318 }
319
320 static void
321 gtk_entry_icon_accessible_get_position (AtkComponent *component,
322 gint *x,
323 gint *y,
324 AtkCoordType coord_type)
325 {
326 GtkEntryIconAccessible *icon = GTK_ENTRY_ICON_ACCESSIBLE (component);
327 GdkRectangle icon_area;
328 GtkEntry *gtk_entry;
329 GtkWidget *widget;
330
331 *x = G_MININT;
332 atk_component_get_extents (ATK_COMPONENT (icon->entry), x, y, NULL, NULL,
333 coord_type);
334 if (*x == G_MININT)
335 return;
336
337 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (icon->entry));
338 gtk_entry = GTK_ENTRY (widget);
339 gtk_entry_get_icon_area (gtk_entry, icon->pos, &icon_area);
340 *x += icon_area.x;
341 *y += icon_area.y;
342 }
343
344 static void
345 gtk_entry_icon_accessible_get_size (AtkComponent *component,
346 gint *width,
347 gint *height)
348 {
349 GtkEntryIconAccessible *icon = GTK_ENTRY_ICON_ACCESSIBLE (component);
350 GdkRectangle icon_area;
351 GtkEntry *gtk_entry;
352 GtkWidget *widget;
353
354 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (icon->entry));
355 gtk_entry = GTK_ENTRY (widget);
356 gtk_entry_get_icon_area (gtk_entry, icon->pos, &icon_area);
357 *width = icon_area.width;
358 *height = icon_area.height;
359 }
360
361 static void
362 icon_atk_component_interface_init (AtkComponentIface *iface)
363 {
364 iface->get_extents = gtk_entry_icon_accessible_get_extents;
365 iface->get_size = gtk_entry_icon_accessible_get_size;
366 iface->get_position = gtk_entry_icon_accessible_get_position;
367 }
368
369 /* Callbacks */
370
371 static void insert_text_cb (GtkEditable *editable,
372 gchar *new_text,
373 gint new_text_length,
374 gint *position);
375 static void delete_text_cb (GtkEditable *editable,
376 gint start,
377 gint end);
378
379 static gboolean check_for_selection_change (GtkEntryAccessible *entry,
380 GtkEntry *gtk_entry);
381
382
383 static void atk_editable_text_interface_init (AtkEditableTextIface *iface);
384 static void atk_text_interface_init (AtkTextIface *iface);
385 static void atk_action_interface_init (AtkActionIface *iface);
386
387
388 G_DEFINE_TYPE_WITH_CODE (GtkEntryAccessible, gtk_entry_accessible, GTK_TYPE_WIDGET_ACCESSIBLE,
389 G_ADD_PRIVATE (GtkEntryAccessible)
390 G_IMPLEMENT_INTERFACE (ATK_TYPE_EDITABLE_TEXT, atk_editable_text_interface_init)
391 G_IMPLEMENT_INTERFACE (ATK_TYPE_TEXT, atk_text_interface_init)
392 G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init))
393
394
395 static AtkStateSet *
396 gtk_entry_accessible_ref_state_set (AtkObject *accessible)
397 {
398 AtkStateSet *state_set;
399 gboolean value;
400 GtkWidget *widget;
401
402 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
403 if (widget == NULL)
404 return NULL;
405
406 state_set = ATK_OBJECT_CLASS (gtk_entry_accessible_parent_class)->ref_state_set (accessible);
407
408 g_object_get (G_OBJECT (widget), "editable", &value, NULL);
409 if (value)
410 atk_state_set_add_state (state_set, ATK_STATE_EDITABLE);
411 atk_state_set_add_state (state_set, ATK_STATE_SINGLE_LINE);
412
413 return state_set;
414 }
415
416 static AtkAttributeSet *
417 gtk_entry_accessible_get_attributes (AtkObject *accessible)
418 {
419 GtkWidget *widget;
420 AtkAttributeSet *attributes;
421 AtkAttribute *placeholder_text;
422 const gchar *text;
423
424 attributes = ATK_OBJECT_CLASS (gtk_entry_accessible_parent_class)->get_attributes (accessible);
425
426 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
427 if (widget == NULL)
428 return attributes;
429
430 text = gtk_entry_get_placeholder_text (GTK_ENTRY (widget));
431 if (text == NULL)
432 return attributes;
433
434 placeholder_text = g_malloc (sizeof (AtkAttribute));
435 placeholder_text->name = g_strdup ("placeholder-text");
436 placeholder_text->value = g_strdup (text);
437
438 attributes = g_slist_append (attributes, placeholder_text);
439
440 return attributes;
441 }
442
443 static void
444 gtk_entry_accessible_initialize (AtkObject *obj,
445 gpointer data)
446 {
447 GtkEntry *entry;
448 GtkEntryAccessible *gtk_entry_accessible;
449 gint start_pos, end_pos;
450
451 ATK_OBJECT_CLASS (gtk_entry_accessible_parent_class)->initialize (obj, data);
452
453 gtk_entry_accessible = GTK_ENTRY_ACCESSIBLE (obj);
454
455 entry = GTK_ENTRY (data);
456 gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_pos, &end_pos);
457 gtk_entry_accessible->priv->cursor_position = end_pos;
458 gtk_entry_accessible->priv->selection_bound = start_pos;
459
460 /* Set up signal callbacks */
461 g_signal_connect_after (entry, "insert-text", G_CALLBACK (insert_text_cb), NULL);
462 g_signal_connect (entry, "delete-text", G_CALLBACK (delete_text_cb), NULL);
463
464 if (gtk_entry_get_visibility (entry))
465 obj->role = ATK_ROLE_TEXT;
466 else
467 obj->role = ATK_ROLE_PASSWORD_TEXT;
468 }
469
470 static void
471 gtk_entry_accessible_notify_gtk (GObject *obj,
472 GParamSpec *pspec)
473 {
474 GtkWidget *widget;
475 AtkObject* atk_obj;
476 GtkEntry* gtk_entry;
477 GtkEntryAccessible* entry;
478 GtkEntryAccessiblePrivate *priv;
479
480 widget = GTK_WIDGET (obj);
481 atk_obj = gtk_widget_get_accessible (widget);
482 gtk_entry = GTK_ENTRY (widget);
483 entry = GTK_ENTRY_ACCESSIBLE (atk_obj);
484 priv = entry->priv;
485
486 if (g_strcmp0 (pspec->name, "cursor-position") == 0)
487 {
488 if (check_for_selection_change (entry, gtk_entry))
489 g_signal_emit_by_name (atk_obj, "text-selection-changed");
490 /*
491 * The entry cursor position has moved so generate the signal.
492 */
493 g_signal_emit_by_name (atk_obj, "text-caret-moved",
494 entry->priv->cursor_position);
495 }
496 else if (g_strcmp0 (pspec->name, "selection-bound") == 0)
497 {
498 if (check_for_selection_change (entry, gtk_entry))
499 g_signal_emit_by_name (atk_obj, "text-selection-changed");
500 }
501 else if (g_strcmp0 (pspec->name, "editable") == 0)
502 {
503 gboolean value;
504
505 g_object_get (obj, "editable", &value, NULL);
506 atk_object_notify_state_change (atk_obj, ATK_STATE_EDITABLE, value);
507 }
508 else if (g_strcmp0 (pspec->name, "visibility") == 0)
509 {
510 gboolean visibility;
511 AtkRole new_role;
512
513 visibility = gtk_entry_get_visibility (gtk_entry);
514 new_role = visibility ? ATK_ROLE_TEXT : ATK_ROLE_PASSWORD_TEXT;
515 atk_object_set_role (atk_obj, new_role);
516 }
517 else if (g_strcmp0 (pspec->name, "primary-icon-storage-type") == 0)
518 {
519 if (gtk_entry_get_icon_storage_type (gtk_entry, GTK_ENTRY_ICON_PRIMARY) != GTK_IMAGE_EMPTY && !priv->icons[GTK_ENTRY_ICON_PRIMARY])
520 {
521 priv->icons[GTK_ENTRY_ICON_PRIMARY] = gtk_entry_icon_accessible_new (entry, GTK_ENTRY_ICON_PRIMARY);
522 g_signal_emit_by_name (entry, "children-changed::add", 0,
523 priv->icons[GTK_ENTRY_ICON_PRIMARY], NULL);
524 }
525 else if (gtk_entry_get_icon_storage_type (gtk_entry, GTK_ENTRY_ICON_PRIMARY) == GTK_IMAGE_EMPTY && priv->icons[GTK_ENTRY_ICON_PRIMARY])
526 {
527 gtk_entry_icon_accessible_invalidate (GTK_ENTRY_ICON_ACCESSIBLE (priv->icons[GTK_ENTRY_ICON_PRIMARY]));
528 g_signal_emit_by_name (entry, "children-changed::remove", 0,
529 priv->icons[GTK_ENTRY_ICON_PRIMARY], NULL);
530 g_clear_object (&priv->icons[GTK_ENTRY_ICON_PRIMARY]);
531 }
532 }
533 else if (g_strcmp0 (pspec->name, "secondary-icon-storage-type") == 0)
534 {
535 gint index = (priv->icons[GTK_ENTRY_ICON_PRIMARY] ? 1 : 0);
536 if (gtk_entry_get_icon_storage_type (gtk_entry, GTK_ENTRY_ICON_SECONDARY) != GTK_IMAGE_EMPTY && !priv->icons[GTK_ENTRY_ICON_SECONDARY])
537 {
538 priv->icons[GTK_ENTRY_ICON_SECONDARY] = gtk_entry_icon_accessible_new (entry, GTK_ENTRY_ICON_SECONDARY);
539 g_signal_emit_by_name (entry, "children-changed::add", index,
540 priv->icons[GTK_ENTRY_ICON_SECONDARY], NULL);
541 }
542 else if (gtk_entry_get_icon_storage_type (gtk_entry, GTK_ENTRY_ICON_SECONDARY) == GTK_IMAGE_EMPTY && priv->icons[GTK_ENTRY_ICON_SECONDARY])
543 {
544 gtk_entry_icon_accessible_invalidate (GTK_ENTRY_ICON_ACCESSIBLE (priv->icons[GTK_ENTRY_ICON_SECONDARY]));
545 g_signal_emit_by_name (entry, "children-changed::remove", index,
546 priv->icons[GTK_ENTRY_ICON_SECONDARY], NULL);
547 g_clear_object (&priv->icons[GTK_ENTRY_ICON_SECONDARY]);
548 }
549 }
550 else if (g_strcmp0 (pspec->name, "primary-icon-name") == 0)
551 {
552 if (priv->icons[GTK_ENTRY_ICON_PRIMARY])
553 {
554 const gchar *name;
555 name = gtk_entry_get_icon_name (gtk_entry,
556 GTK_ENTRY_ICON_PRIMARY);
557 if (name)
558 atk_object_set_name (priv->icons[GTK_ENTRY_ICON_PRIMARY], name);
559 }
560 }
561 else if (g_strcmp0 (pspec->name, "secondary-icon-name") == 0)
562 {
563 if (priv->icons[GTK_ENTRY_ICON_SECONDARY])
564 {
565 const gchar *name;
566 name = gtk_entry_get_icon_name (gtk_entry,
567 GTK_ENTRY_ICON_SECONDARY);
568 if (name)
569 atk_object_set_name (priv->icons[GTK_ENTRY_ICON_SECONDARY], name);
570 }
571 }
572 else if (g_strcmp0 (pspec->name, "primary-icon-tooltip-text") == 0)
573 {
574 if (priv->icons[GTK_ENTRY_ICON_PRIMARY])
575 {
576 gchar *text;
577 text = gtk_entry_get_icon_tooltip_text (gtk_entry,
578 GTK_ENTRY_ICON_PRIMARY);
579 if (text)
580 {
581 atk_object_set_description (priv->icons[GTK_ENTRY_ICON_PRIMARY],
582 text);
583 g_free (text);
584 }
585 else
586 {
587 atk_object_set_description (priv->icons[GTK_ENTRY_ICON_PRIMARY],
588 "");
589 }
590 }
591 }
592 else if (g_strcmp0 (pspec->name, "secondary-icon-tooltip-text") == 0)
593 {
594 if (priv->icons[GTK_ENTRY_ICON_SECONDARY])
595 {
596 gchar *text;
597 text = gtk_entry_get_icon_tooltip_text (gtk_entry,
598 GTK_ENTRY_ICON_SECONDARY);
599 if (text)
600 {
601 atk_object_set_description (priv->icons[GTK_ENTRY_ICON_SECONDARY],
602 text);
603 g_free (text);
604 }
605 else
606 {
607 atk_object_set_description (priv->icons[GTK_ENTRY_ICON_SECONDARY],
608 "");
609 }
610 }
611 }
612 else if (g_strcmp0 (pspec->name, "primary-icon-activatable") == 0)
613 {
614 if (priv->icons[GTK_ENTRY_ICON_PRIMARY])
615 {
616 gboolean on = gtk_entry_get_icon_activatable (gtk_entry, GTK_ENTRY_ICON_PRIMARY);
617 atk_object_notify_state_change (priv->icons[GTK_ENTRY_ICON_PRIMARY],
618 ATK_STATE_ENABLED, on);
619 }
620 }
621 else if (g_strcmp0 (pspec->name, "secondary-icon-activatable") == 0)
622 {
623 if (priv->icons[GTK_ENTRY_ICON_SECONDARY])
624 {
625 gboolean on = gtk_entry_get_icon_activatable (gtk_entry, GTK_ENTRY_ICON_SECONDARY);
626 atk_object_notify_state_change (priv->icons[GTK_ENTRY_ICON_SECONDARY],
627 ATK_STATE_ENABLED, on);
628 }
629 }
630 else if (g_strcmp0 (pspec->name, "primary-icon-sensitive") == 0)
631 {
632 if (priv->icons[GTK_ENTRY_ICON_PRIMARY])
633 {
634 gboolean on = gtk_entry_get_icon_sensitive (gtk_entry, GTK_ENTRY_ICON_PRIMARY);
635 atk_object_notify_state_change (priv->icons[GTK_ENTRY_ICON_PRIMARY],
636 ATK_STATE_SENSITIVE, on);
637 }
638 }
639 else if (g_strcmp0 (pspec->name, "secondary-icon-sensitive") == 0)
640 {
641 if (priv->icons[GTK_ENTRY_ICON_SECONDARY])
642 {
643 gboolean on = gtk_entry_get_icon_sensitive (gtk_entry, GTK_ENTRY_ICON_SECONDARY);
644 atk_object_notify_state_change (priv->icons[GTK_ENTRY_ICON_SECONDARY],
645 ATK_STATE_SENSITIVE, on);
646 }
647 }
648 else
649 GTK_WIDGET_ACCESSIBLE_CLASS (gtk_entry_accessible_parent_class)->notify_gtk (obj, pspec);
650 }
651
652 static gint
653 gtk_entry_accessible_get_index_in_parent (AtkObject *accessible)
654 {
655 /*
656 * If the parent widget is a combo box then the index is 1
657 * otherwise do the normal thing.
658 */
659 if (accessible->accessible_parent)
660 if (GTK_IS_COMBO_BOX_ACCESSIBLE (accessible->accessible_parent))
661 return 1;
662
663 return ATK_OBJECT_CLASS (gtk_entry_accessible_parent_class)->get_index_in_parent (accessible);
664 }
665
666 static gint
667 gtk_entry_accessible_get_n_children (AtkObject* obj)
668 {
669 GtkWidget *widget;
670 GtkEntry *entry;
671 gint count = 0;
672
673 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
674 if (widget == NULL)
675 return 0;
676
677 entry = GTK_ENTRY (widget);
678
679 if (gtk_entry_get_icon_storage_type (entry, GTK_ENTRY_ICON_PRIMARY) != GTK_IMAGE_EMPTY)
680 count++;
681 if (gtk_entry_get_icon_storage_type (entry, GTK_ENTRY_ICON_SECONDARY) != GTK_IMAGE_EMPTY)
682 count++;
683 return count;
684 }
685
686 static AtkObject *
687 gtk_entry_accessible_ref_child (AtkObject *obj,
688 gint i)
689 {
690 GtkEntryAccessible *accessible = GTK_ENTRY_ACCESSIBLE (obj);
691 GtkEntryAccessiblePrivate *priv = accessible->priv;
692 GtkWidget *widget;
693 GtkEntry *entry;
694 GtkEntryIconPosition pos;
695
696 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
697 if (widget == NULL)
698 return NULL;
699
700 entry = GTK_ENTRY (widget);
701
702 switch (i)
703 {
704 case 0:
705 if (gtk_entry_get_icon_storage_type (entry, GTK_ENTRY_ICON_PRIMARY) != GTK_IMAGE_EMPTY)
706 pos = GTK_ENTRY_ICON_PRIMARY;
707 else if (gtk_entry_get_icon_storage_type (entry, GTK_ENTRY_ICON_SECONDARY) != GTK_IMAGE_EMPTY)
708 pos = GTK_ENTRY_ICON_SECONDARY;
709 else
710 return NULL;
711 break;
712 case 1:
713 if (gtk_entry_get_icon_storage_type (entry, GTK_ENTRY_ICON_PRIMARY) == GTK_IMAGE_EMPTY)
714 return NULL;
715 if (gtk_entry_get_icon_storage_type (entry, GTK_ENTRY_ICON_SECONDARY) == GTK_IMAGE_EMPTY)
716 return NULL;
717 pos = GTK_ENTRY_ICON_SECONDARY;
718 break;
719 default:
720 return NULL;
721 }
722
723 if (!priv->icons[pos])
724 priv->icons[pos] = gtk_entry_icon_accessible_new (accessible, pos);
725 return g_object_ref (priv->icons[pos]);
726 }
727
728 static void
729 gtk_entry_accessible_finalize (GObject *object)
730 {
731 GtkEntryAccessible *entry = GTK_ENTRY_ACCESSIBLE (object);
732 GtkEntryAccessiblePrivate *priv = entry->priv;
733
734 g_clear_object (&priv->icons[GTK_ENTRY_ICON_PRIMARY]);
735 g_clear_object (&priv->icons[GTK_ENTRY_ICON_SECONDARY]);
736
737 G_OBJECT_CLASS (gtk_entry_accessible_parent_class)->finalize (object);
738 }
739
740 static void
741 gtk_entry_accessible_class_init (GtkEntryAccessibleClass *klass)
742 {
743 AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
744 GtkWidgetAccessibleClass *widget_class = (GtkWidgetAccessibleClass*)klass;
745 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
746
747 class->ref_state_set = gtk_entry_accessible_ref_state_set;
748 class->get_index_in_parent = gtk_entry_accessible_get_index_in_parent;
749 class->initialize = gtk_entry_accessible_initialize;
750 class->get_attributes = gtk_entry_accessible_get_attributes;
751 class->get_n_children = gtk_entry_accessible_get_n_children;
752 class->ref_child = gtk_entry_accessible_ref_child;
753
754 widget_class->notify_gtk = gtk_entry_accessible_notify_gtk;
755
756 gobject_class->finalize = gtk_entry_accessible_finalize;
757 }
758
759 static void
760 gtk_entry_accessible_init (GtkEntryAccessible *entry)
761 {
762 entry->priv = gtk_entry_accessible_get_instance_private (entry);
763 entry->priv->cursor_position = 0;
764 entry->priv->selection_bound = 0;
765 }
766
767 static gchar *
768 gtk_entry_accessible_get_text (AtkText *atk_text,
769 gint start_pos,
770 gint end_pos)
771 {
772 GtkWidget *widget;
773
774 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (atk_text));
775 if (widget == NULL)
776 return NULL;
777
778 return _gtk_entry_get_display_text (GTK_ENTRY (widget), start_pos, end_pos);
779 }
780
781 static gchar *
782 gtk_entry_accessible_get_text_before_offset (AtkText *text,
783 gint offset,
784 AtkTextBoundary boundary_type,
785 gint *start_offset,
786 gint *end_offset)
787 {
788 GtkWidget *widget;
789
790 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
791 if (widget == NULL)
792 return NULL;
793
794 return _gtk_pango_get_text_before (gtk_entry_get_layout (GTK_ENTRY (widget)),
795 boundary_type, offset,
796 start_offset, end_offset);
797 }
798
799 static gchar *
800 gtk_entry_accessible_get_text_at_offset (AtkText *text,
801 gint offset,
802 AtkTextBoundary boundary_type,
803 gint *start_offset,
804 gint *end_offset)
805 {
806 GtkWidget *widget;
807
808 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
809 if (widget == NULL)
810 return NULL;
811
812 return _gtk_pango_get_text_at (gtk_entry_get_layout (GTK_ENTRY (widget)),
813 boundary_type, offset,
814 start_offset, end_offset);
815 }
816
817 static gchar *
818 gtk_entry_accessible_get_text_after_offset (AtkText *text,
819 gint offset,
820 AtkTextBoundary boundary_type,
821 gint *start_offset,
822 gint *end_offset)
823 {
824 GtkWidget *widget;
825
826 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
827 if (widget == NULL)
828 return NULL;
829
830 return _gtk_pango_get_text_after (gtk_entry_get_layout (GTK_ENTRY (widget)),
831 boundary_type, offset,
832 start_offset, end_offset);
833 }
834
835 static gint
836 gtk_entry_accessible_get_character_count (AtkText *atk_text)
837 {
838 GtkWidget *widget;
839 gchar *text;
840 glong char_count;
841
842 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (atk_text));
843 if (widget == NULL)
844 return 0;
845
846 text = _gtk_entry_get_display_text (GTK_ENTRY (widget), 0, -1);
847
848 char_count = 0;
849 if (text)
850 {
851 char_count = g_utf8_strlen (text, -1);
852 g_free (text);
853 }
854
855 return char_count;
856 }
857
858 static gint
859 gtk_entry_accessible_get_caret_offset (AtkText *text)
860 {
861 GtkWidget *widget;
862
863 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
864 if (widget == NULL)
865 return 0;
866
867 return gtk_editable_get_position (GTK_EDITABLE (widget));
868 }
869
870 static gboolean
871 gtk_entry_accessible_set_caret_offset (AtkText *text,
872 gint offset)
873 {
874 GtkWidget *widget;
875
876 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
877 if (widget == NULL)
878 return FALSE;
879
880 gtk_editable_set_position (GTK_EDITABLE (widget), offset);
881
882 return TRUE;
883 }
884
885 static AtkAttributeSet *
886 add_text_attribute (AtkAttributeSet *attributes,
887 AtkTextAttribute attr,
888 gint i)
889 {
890 AtkAttribute *at;
891
892 at = g_new (AtkAttribute, 1);
893 at->name = g_strdup (atk_text_attribute_get_name (attr));
894 at->value = g_strdup (atk_text_attribute_get_value (attr, i));
895
896 return g_slist_prepend (attributes, at);
897 }
898
899 static AtkAttributeSet *
900 gtk_entry_accessible_get_run_attributes (AtkText *text,
901 gint offset,
902 gint *start_offset,
903 gint *end_offset)
904 {
905 GtkWidget *widget;
906 AtkAttributeSet *attributes;
907
908 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
909 if (widget == NULL)
910 return NULL;
911
912 attributes = NULL;
913 attributes = add_text_attribute (attributes, ATK_TEXT_ATTR_DIRECTION,
914 gtk_widget_get_direction (widget));
915 attributes = _gtk_pango_get_run_attributes (attributes,
916 gtk_entry_get_layout (GTK_ENTRY (widget)),
917 offset,
918 start_offset,
919 end_offset);
920
921 return attributes;
922 }
923
924 static AtkAttributeSet *
925 gtk_entry_accessible_get_default_attributes (AtkText *text)
926 {
927 GtkWidget *widget;
928 AtkAttributeSet *attributes;
929
930 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
931 if (widget == NULL)
932 return NULL;
933
934 attributes = NULL;
935 attributes = add_text_attribute (attributes, ATK_TEXT_ATTR_DIRECTION,
936 gtk_widget_get_direction (widget));
937 attributes = _gtk_pango_get_default_attributes (attributes,
938 gtk_entry_get_layout (GTK_ENTRY (widget)));
939 attributes = _gtk_style_context_get_attributes (attributes,
940 gtk_widget_get_style_context (widget),
941 gtk_widget_get_state_flags (widget));
942
943 return attributes;
944 }
945
946 static void
947 gtk_entry_accessible_get_character_extents (AtkText *text,
948 gint offset,
949 gint *x,
950 gint *y,
951 gint *width,
952 gint *height,
953 AtkCoordType coords)
954 {
955 GtkWidget *widget;
956 GtkEntry *entry;
957 PangoRectangle char_rect;
958 gchar *entry_text;
959 gint index, x_layout, y_layout;
960 GdkWindow *window;
961 gint x_window, y_window;
962 GtkAllocation allocation;
963
964 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
965 if (widget == NULL)
966 return;
967
968 entry = GTK_ENTRY (widget);
969
970 gtk_entry_get_layout_offsets (entry, &x_layout, &y_layout);
971 entry_text = _gtk_entry_get_display_text (entry, 0, -1);
972 index = g_utf8_offset_to_pointer (entry_text, offset) - entry_text;
973 g_free (entry_text);
974
975 pango_layout_index_to_pos (gtk_entry_get_layout (entry), index, &char_rect);
976 pango_extents_to_pixels (&char_rect, NULL);
977
978 _gtk_widget_get_allocation (widget, &allocation);
979
980 window = gtk_widget_get_window (widget);
981 gdk_window_get_origin (window, &x_window, &y_window);
982
983 *x = x_window + allocation.x + x_layout + char_rect.x;
984 *y = y_window + allocation.y + y_layout + char_rect.y;
985 *width = char_rect.width;
986 *height = char_rect.height;
987
988 if (coords == ATK_XY_WINDOW)
989 {
990 window = gdk_window_get_toplevel (window);
991 gdk_window_get_origin (window, &x_window, &y_window);
992
993 *x -= x_window;
994 *y -= y_window;
995 }
996 }
997
998 static gint
999 gtk_entry_accessible_get_offset_at_point (AtkText *atk_text,
1000 gint x,
1001 gint y,
1002 AtkCoordType coords)
1003 {
1004 GtkWidget *widget;
1005 GtkEntry *entry;
1006 gchar *text;
1007 gint index, x_layout, y_layout;
1008 gint x_window, y_window;
1009 gint x_local, y_local;
1010 GdkWindow *window;
1011 glong offset;
1012
1013 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (atk_text));
1014 if (widget == NULL)
1015 return -1;
1016
1017 entry = GTK_ENTRY (widget);
1018
1019 gtk_entry_get_layout_offsets (entry, &x_layout, &y_layout);
1020
1021 window = gtk_widget_get_window (widget);
1022 gdk_window_get_origin (window, &x_window, &y_window);
1023
1024 x_local = x - x_layout - x_window;
1025 y_local = y - y_layout - y_window;
1026
1027 if (coords == ATK_XY_WINDOW)
1028 {
1029 window = gdk_window_get_toplevel (window);
1030 gdk_window_get_origin (window, &x_window, &y_window);
1031
1032 x_local += x_window;
1033 y_local += y_window;
1034 }
1035 if (!pango_layout_xy_to_index (gtk_entry_get_layout (entry),
1036 x_local * PANGO_SCALE,
1037 y_local * PANGO_SCALE,
1038 &index, NULL))
1039 {
1040 if (x_local < 0 || y_local < 0)
1041 index = 0;
1042 else
1043 index = -1;
1044 }
1045
1046 offset = -1;
1047 if (index != -1)
1048 {
1049 text = _gtk_entry_get_display_text (entry, 0, -1);
1050 offset = g_utf8_pointer_to_offset (text, text + index);
1051 g_free (text);
1052 }
1053
1054 return offset;
1055 }
1056
1057 static gint
1058 gtk_entry_accessible_get_n_selections (AtkText *text)
1059 {
1060 GtkWidget *widget;
1061 gint start, end;
1062
1063 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
1064 if (widget == NULL)
1065 return 0;
1066
1067 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (widget), &start, &end))
1068 return 1;
1069
1070 return 0;
1071 }
1072
1073 static gchar *
1074 gtk_entry_accessible_get_selection (AtkText *text,
1075 gint selection_num,
1076 gint *start_pos,
1077 gint *end_pos)
1078 {
1079 GtkWidget *widget;
1080
1081 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
1082 if (widget == NULL)
1083 return NULL;
1084
1085 if (selection_num != 0)
1086 return NULL;
1087
1088 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (widget), start_pos, end_pos))
1089 return gtk_editable_get_chars (GTK_EDITABLE (widget), *start_pos, *end_pos);
1090
1091 return NULL;
1092 }
1093
1094 static gboolean
1095 gtk_entry_accessible_add_selection (AtkText *text,
1096 gint start_pos,
1097 gint end_pos)
1098 {
1099 GtkEntry *entry;
1100 GtkWidget *widget;
1101 gint start, end;
1102
1103 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
1104 if (widget == NULL)
1105 return FALSE;
1106
1107 entry = GTK_ENTRY (widget);
1108
1109 if (!gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
1110 {
1111 gtk_editable_select_region (GTK_EDITABLE (entry), start_pos, end_pos);
1112 return TRUE;
1113 }
1114 else
1115 return FALSE;
1116 }
1117
1118 static gboolean
1119 gtk_entry_accessible_remove_selection (AtkText *text,
1120 gint selection_num)
1121 {
1122 GtkWidget *widget;
1123 gint start, end;
1124
1125 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
1126 if (widget == NULL)
1127 return FALSE;
1128
1129 if (selection_num != 0)
1130 return FALSE;
1131
1132 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (widget), &start, &end))
1133 {
1134 gtk_editable_select_region (GTK_EDITABLE (widget), end, end);
1135 return TRUE;
1136 }
1137 else
1138 return FALSE;
1139 }
1140
1141 static gboolean
1142 gtk_entry_accessible_set_selection (AtkText *text,
1143 gint selection_num,
1144 gint start_pos,
1145 gint end_pos)
1146 {
1147 GtkWidget *widget;
1148 gint start, end;
1149
1150 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
1151 if (widget == NULL)
1152 return FALSE;
1153
1154 if (selection_num != 0)
1155 return FALSE;
1156
1157 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (widget), &start, &end))
1158 {
1159 gtk_editable_select_region (GTK_EDITABLE (widget), start_pos, end_pos);
1160 return TRUE;
1161 }
1162 else
1163 return FALSE;
1164 }
1165
1166 static gunichar
1167 gtk_entry_accessible_get_character_at_offset (AtkText *atk_text,
1168 gint offset)
1169 {
1170 GtkWidget *widget;
1171 gchar *text;
1172 gchar *index;
1173 gunichar result;
1174
1175 result = '\0';
1176
1177 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (atk_text));
1178 if (widget == NULL)
1179 return result;
1180
1181 if (!gtk_entry_get_visibility (GTK_ENTRY (widget)))
1182 return result;
1183
1184 text = _gtk_entry_get_display_text (GTK_ENTRY (widget), 0, -1);
1185 if (offset < g_utf8_strlen (text, -1))
1186 {
1187 index = g_utf8_offset_to_pointer (text, offset);
1188 result = g_utf8_get_char (index);
1189 g_free (text);
1190 }
1191
1192 return result;
1193 }
1194
1195 static void
1196 atk_text_interface_init (AtkTextIface *iface)
1197 {
1198 iface->get_text = gtk_entry_accessible_get_text;
1199 iface->get_character_at_offset = gtk_entry_accessible_get_character_at_offset;
1200 iface->get_text_before_offset = gtk_entry_accessible_get_text_before_offset;
1201 iface->get_text_at_offset = gtk_entry_accessible_get_text_at_offset;
1202 iface->get_text_after_offset = gtk_entry_accessible_get_text_after_offset;
1203 iface->get_caret_offset = gtk_entry_accessible_get_caret_offset;
1204 iface->set_caret_offset = gtk_entry_accessible_set_caret_offset;
1205 iface->get_character_count = gtk_entry_accessible_get_character_count;
1206 iface->get_n_selections = gtk_entry_accessible_get_n_selections;
1207 iface->get_selection = gtk_entry_accessible_get_selection;
1208 iface->add_selection = gtk_entry_accessible_add_selection;
1209 iface->remove_selection = gtk_entry_accessible_remove_selection;
1210 iface->set_selection = gtk_entry_accessible_set_selection;
1211 iface->get_run_attributes = gtk_entry_accessible_get_run_attributes;
1212 iface->get_default_attributes = gtk_entry_accessible_get_default_attributes;
1213 iface->get_character_extents = gtk_entry_accessible_get_character_extents;
1214 iface->get_offset_at_point = gtk_entry_accessible_get_offset_at_point;
1215 }
1216
1217 static void
1218 gtk_entry_accessible_set_text_contents (AtkEditableText *text,
1219 const gchar *string)
1220 {
1221 GtkWidget *widget;
1222
1223 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
1224 if (widget == NULL)
1225 return;
1226
1227 if (!gtk_editable_get_editable (GTK_EDITABLE (widget)))
1228 return;
1229
1230 gtk_entry_set_text (GTK_ENTRY (widget), string);
1231 }
1232
1233 static void
1234 gtk_entry_accessible_insert_text (AtkEditableText *text,
1235 const gchar *string,
1236 gint length,
1237 gint *position)
1238 {
1239 GtkWidget *widget;
1240 GtkEditable *editable;
1241
1242 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
1243 if (widget == NULL)
1244 return;
1245
1246 editable = GTK_EDITABLE (widget);
1247 if (!gtk_editable_get_editable (editable))
1248 return;
1249
1250 gtk_editable_insert_text (editable, string, length, position);
1251 gtk_editable_set_position (editable, *position);
1252 }
1253
1254 static void
1255 gtk_entry_accessible_copy_text (AtkEditableText *text,
1256 gint start_pos,
1257 gint end_pos)
1258 {
1259 GtkWidget *widget;
1260 GtkEditable *editable;
1261 gchar *str;
1262 GtkClipboard *clipboard;
1263
1264 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
1265 if (widget == NULL)
1266 return;
1267
1268 if (!gtk_widget_has_screen (widget))
1269 return;
1270
1271 editable = GTK_EDITABLE (widget);
1272 str = gtk_editable_get_chars (editable, start_pos, end_pos);
1273 clipboard = gtk_widget_get_clipboard (widget, GDK_SELECTION_CLIPBOARD);
1274 gtk_clipboard_set_text (clipboard, str, -1);
1275 g_free (str);
1276 }
1277
1278 static void
1279 gtk_entry_accessible_cut_text (AtkEditableText *text,
1280 gint start_pos,
1281 gint end_pos)
1282 {
1283 GtkWidget *widget;
1284 GtkEditable *editable;
1285 gchar *str;
1286 GtkClipboard *clipboard;
1287
1288 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
1289 if (widget == NULL)
1290 return;
1291
1292 if (!gtk_widget_has_screen (widget))
1293 return;
1294
1295 editable = GTK_EDITABLE (widget);
1296 if (!gtk_editable_get_editable (editable))
1297 return;
1298
1299 str = gtk_editable_get_chars (editable, start_pos, end_pos);
1300 clipboard = gtk_widget_get_clipboard (widget, GDK_SELECTION_CLIPBOARD);
1301 gtk_clipboard_set_text (clipboard, str, -1);
1302 gtk_editable_delete_text (editable, start_pos, end_pos);
1303 }
1304
1305 static void
1306 gtk_entry_accessible_delete_text (AtkEditableText *text,
1307 gint start_pos,
1308 gint end_pos)
1309 {
1310 GtkWidget *widget;
1311 GtkEditable *editable;
1312
1313 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
1314 if (widget == NULL)
1315 return;
1316
1317 editable = GTK_EDITABLE (widget);
1318 if (!gtk_editable_get_editable (editable))
1319 return;
1320
1321 gtk_editable_delete_text (editable, start_pos, end_pos);
1322 }
1323
1324 typedef struct
1325 {
1326 GtkEntry* entry;
1327 gint position;
1328 } PasteData;
1329
1330 static void
1331 paste_received_cb (GtkClipboard *clipboard,
1332 const gchar *text,
1333 gpointer data)
1334 {
1335 PasteData *paste = data;
1336
1337 if (text)
1338 gtk_editable_insert_text (GTK_EDITABLE (paste->entry), text, -1,
1339 &paste->position);
1340
1341 g_object_unref (paste->entry);
1342 g_free (paste);
1343 }
1344
1345 static void
1346 gtk_entry_accessible_paste_text (AtkEditableText *text,
1347 gint position)
1348 {
1349 GtkWidget *widget;
1350 GtkEditable *editable;
1351 PasteData *paste;
1352 GtkClipboard *clipboard;
1353
1354 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
1355 if (widget == NULL)
1356 return;
1357
1358 if (!gtk_widget_has_screen (widget))
1359 return;
1360
1361 editable = GTK_EDITABLE (widget);
1362 if (!gtk_editable_get_editable (editable))
1363 return;
1364
1365 paste = g_new0 (PasteData, 1);
1366 paste->entry = GTK_ENTRY (widget);
1367 paste->position = position;
1368
1369 g_object_ref (paste->entry);
1370 clipboard = gtk_widget_get_clipboard (widget, GDK_SELECTION_CLIPBOARD);
1371 gtk_clipboard_request_text (clipboard, paste_received_cb, paste);
1372 }
1373
1374 static void
1375 atk_editable_text_interface_init (AtkEditableTextIface *iface)
1376 {
1377 iface->set_text_contents = gtk_entry_accessible_set_text_contents;
1378 iface->insert_text = gtk_entry_accessible_insert_text;
1379 iface->copy_text = gtk_entry_accessible_copy_text;
1380 iface->cut_text = gtk_entry_accessible_cut_text;
1381 iface->delete_text = gtk_entry_accessible_delete_text;
1382 iface->paste_text = gtk_entry_accessible_paste_text;
1383 iface->set_run_attributes = NULL;
1384 }
1385
1386 static void
1387 insert_text_cb (GtkEditable *editable,
1388 gchar *new_text,
1389 gint new_text_length,
1390 gint *position)
1391 {
1392 GtkEntryAccessible *accessible;
1393 gint length;
1394
1395 if (new_text_length == 0)
1396 return;
1397
1398 accessible = GTK_ENTRY_ACCESSIBLE (gtk_widget_get_accessible (GTK_WIDGET (editable)));
1399 length = g_utf8_strlen (new_text, new_text_length);
1400
1401 g_signal_emit_by_name (accessible,
1402 "text-changed::insert",
1403 *position - length,
1404 length);
1405 }
1406
1407 /* We connect to GtkEditable::delete-text, since it carries
1408 * the information we need. But we delay emitting our own
1409 * text_changed::delete signal until the entry has update
1410 * all its internal state and emits GtkEntry::changed.
1411 */
1412 static void
1413 delete_text_cb (GtkEditable *editable,
1414 gint start,
1415 gint end)
1416 {
1417 GtkEntryAccessible *accessible;
1418
1419 accessible = GTK_ENTRY_ACCESSIBLE (gtk_widget_get_accessible (GTK_WIDGET (editable)));
1420
1421 if (end < 0)
1422 {
1423 gchar *text;
1424
1425 text = _gtk_entry_get_display_text (GTK_ENTRY (editable), 0, -1);
1426 end = g_utf8_strlen (text, -1);
1427 g_free (text);
1428 }
1429
1430 if (end == start)
1431 return;
1432
1433 g_signal_emit_by_name (accessible,
1434 "text-changed::delete",
1435 start,
1436 end - start);
1437 }
1438
1439 static gboolean
1440 check_for_selection_change (GtkEntryAccessible *accessible,
1441 GtkEntry *entry)
1442 {
1443 gboolean ret_val = FALSE;
1444 gint start, end;
1445
1446 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
1447 {
1448 if (end != accessible->priv->cursor_position ||
1449 start != accessible->priv->selection_bound)
1450 /*
1451 * This check is here as this function can be called
1452 * for notification of selection_bound and current_pos.
1453 * The values of current_pos and selection_bound may be the same
1454 * for both notifications and we only want to generate one
1455 * text_selection_changed signal.
1456 */
1457 ret_val = TRUE;
1458 }
1459 else
1460 {
1461 /* We had a selection */
1462 ret_val = (accessible->priv->cursor_position != accessible->priv->selection_bound);
1463 }
1464
1465 accessible->priv->cursor_position = end;
1466 accessible->priv->selection_bound = start;
1467
1468 return ret_val;
1469 }
1470
1471 static gboolean
1472 gtk_entry_accessible_do_action (AtkAction *action,
1473 gint i)
1474 {
1475 GtkWidget *widget;
1476
1477 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (action));
1478 if (widget == NULL)
1479 return FALSE;
1480
1481 if (!gtk_widget_get_sensitive (widget) || !gtk_widget_get_visible (widget))
1482 return FALSE;
1483
1484 if (i != 0)
1485 return FALSE;
1486
1487 gtk_widget_activate (widget);
1488
1489 return TRUE;
1490 }
1491
1492 static gint
1493 gtk_entry_accessible_get_n_actions (AtkAction *action)
1494 {
1495 return 1;
1496 }
1497
1498 static const gchar *
1499 gtk_entry_accessible_get_keybinding (AtkAction *action,
1500 gint i)
1501 {
1502 GtkWidget *widget;
1503 GtkWidget *label;
1504 AtkRelationSet *set;
1505 AtkRelation *relation;
1506 GPtrArray *target;
1507 gpointer target_object;
1508 guint key_val;
1509
1510 if (i != 0)
1511 return NULL;
1512
1513 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (action));
1514 if (widget == NULL)
1515 return NULL;
1516
1517 set = atk_object_ref_relation_set (ATK_OBJECT (action));
1518 if (!set)
1519 return NULL;
1520
1521 label = NULL;
1522 relation = atk_relation_set_get_relation_by_type (set, ATK_RELATION_LABELLED_BY);
1523 if (relation)
1524 {
1525 target = atk_relation_get_target (relation);
1526
1527 target_object = g_ptr_array_index (target, 0);
1528 label = gtk_accessible_get_widget (GTK_ACCESSIBLE (target_object));
1529 }
1530
1531 g_object_unref (set);
1532
1533 if (GTK_IS_LABEL (label))
1534 {
1535 key_val = gtk_label_get_mnemonic_keyval (GTK_LABEL (label));
1536 if (key_val != GDK_KEY_VoidSymbol)
1537 return gtk_accelerator_name (key_val, GDK_MOD1_MASK);
1538 }
1539
1540 return NULL;
1541 }
1542
1543 static const gchar*
1544 gtk_entry_accessible_action_get_name (AtkAction *action,
1545 gint i)
1546 {
1547 if (i == 0)
1548 return "activate";
1549 return NULL;
1550 }
1551
1552 static const gchar*
1553 gtk_entry_accessible_action_get_localized_name (AtkAction *action,
1554 gint i)
1555 {
1556 if (i == 0)
1557 return C_("Action name", "Activate");
1558 return NULL;
1559 }
1560
1561 static const gchar*
1562 gtk_entry_accessible_action_get_description (AtkAction *action,
1563 gint i)
1564 {
1565 if (i == 0)
1566 return C_("Action description", "Activates the entry");
1567 return NULL;
1568 }
1569
1570 static void
1571 atk_action_interface_init (AtkActionIface *iface)
1572 {
1573 iface->do_action = gtk_entry_accessible_do_action;
1574 iface->get_n_actions = gtk_entry_accessible_get_n_actions;
1575 iface->get_keybinding = gtk_entry_accessible_get_keybinding;
1576 iface->get_name = gtk_entry_accessible_action_get_name;
1577 iface->get_localized_name = gtk_entry_accessible_action_get_localized_name;
1578 iface->get_description = gtk_entry_accessible_action_get_description;
1579 }