1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /* this file is part of evince, a gnome document viewer
4 * Copyright (C) 2004 Red Hat, Inc
6 * Evince is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Evince is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include <glib/gi18n-lib.h>
25 #include <libgail-util/gail-util.h>
27 #include "ev-selection.h"
28 #include "ev-page-cache.h"
29 #include "ev-view-accessible.h"
30 #include "ev-view-private.h"
32 #define EV_TYPE_VIEW_ACCESSIBLE (ev_view_accessible_get_type ())
33 #define EV_VIEW_ACCESSIBLE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EV_TYPE_VIEW_ACCESSIBLE, EvViewAccessible))
34 #define EV_IS_VIEW_ACCESSIBLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EV_TYPE_VIEW_ACCESSIBLE))
36 static GType ev_view_accessible_get_type (void);
44 static const gchar *const ev_view_accessible_action_names[] =
51 static const gchar *const ev_view_accessible_action_descriptions[] =
54 N_("Scroll View Down"),
60 gchar *action_descriptions[LAST_ACTION];
61 guint action_idle_handler;
62 GtkScrollType idle_scroll;
63 GtkTextBuffer *buffer;
65 } EvViewAccessiblePriv;
67 typedef GtkAccessibleClass EvViewAccessibleClass;
68 typedef GtkAccessible EvViewAccessible;
70 #define EV_VIEW_ACCESSIBLE_GET_PRIVATE(inst) (G_TYPE_INSTANCE_GET_PRIVATE ((inst), EV_TYPE_VIEW_ACCESSIBLE, EvViewAccessiblePriv))
73 ev_view_accessible_finalize (GObject *object)
75 EvViewAccessiblePriv *priv = EV_VIEW_ACCESSIBLE_GET_PRIVATE (object);
78 if (priv->action_idle_handler)
79 g_source_remove (priv->action_idle_handler);
80 for (i = 0; i < LAST_ACTION; i++)
81 g_free (priv->action_descriptions [i]);
83 g_object_unref (priv->buffer);
87 static void ev_view_accessible_class_init (EvViewAccessibleClass *klass)
89 GObjectClass *object_class = G_OBJECT_CLASS (klass);
91 object_class->finalize = ev_view_accessible_finalize;
93 g_type_class_add_private (klass, sizeof (EvViewAccessiblePriv));
96 static GtkTextBuffer *
97 ev_view_accessible_get_text_buffer (EvViewAccessible *accessible, EvView *view)
99 EvPageCache *page_cache;
100 const gchar *retval = NULL;
101 EvViewAccessiblePriv* priv = EV_VIEW_ACCESSIBLE_GET_PRIVATE (accessible);
103 page_cache = view->page_cache;
108 if (view->current_page == priv->current_page && priv->buffer) {
112 priv->current_page = view->current_page;
115 priv->buffer = gtk_text_buffer_new (NULL);
118 retval = ev_page_cache_get_text (page_cache, view->current_page);
119 gtk_text_buffer_set_text (priv->buffer, retval, -1);
125 ev_view_accessible_get_text (AtkText *text,
130 GtkTextIter start, end;
131 GtkTextBuffer *buffer;
134 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
136 /* State is defunct */
139 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
143 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_pos);
144 gtk_text_buffer_get_iter_at_offset (buffer, &end, end_pos);
145 retval = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
151 ev_view_accessible_get_character_at_offset (AtkText *text,
155 GtkTextIter start, end;
156 GtkTextBuffer *buffer;
160 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
162 /* State is defunct */
165 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
169 if (offset >= gtk_text_buffer_get_char_count (buffer))
172 gtk_text_buffer_get_iter_at_offset (buffer, &start, offset);
174 gtk_text_iter_forward_char (&end);
175 string = gtk_text_buffer_get_slice (buffer, &start, &end, FALSE);
176 unichar = g_utf8_get_char (string);
183 ev_view_accessible_get_text_before_offset (AtkText *text,
185 AtkTextBoundary boundary_type,
190 gpointer layout = NULL;
191 GailTextUtil *gail_text = NULL;
192 gchar *retval = NULL;
193 GtkTextBuffer *buffer;
195 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
197 /* State is defunct */
200 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
204 gail_text = gail_text_util_new ();
205 gail_text_util_buffer_setup (gail_text, buffer);
206 retval = gail_text_util_get_text (gail_text, layout,
207 GAIL_BEFORE_OFFSET, boundary_type,
208 offset, start_offset, end_offset);
209 g_object_unref (gail_text);
215 ev_view_accessible_get_text_at_offset (AtkText *text,
217 AtkTextBoundary boundary_type,
222 gpointer layout = NULL;
223 GailTextUtil *gail_text = NULL;
224 gchar *retval = NULL;
225 GtkTextBuffer *buffer;
227 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
229 /* State is defunct */
232 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
236 gail_text = gail_text_util_new ();
237 gail_text_util_buffer_setup (gail_text, buffer);
238 retval = gail_text_util_get_text (gail_text, layout,
239 GAIL_AT_OFFSET, boundary_type,
240 offset, start_offset, end_offset);
241 g_object_unref (gail_text);
247 ev_view_accessible_get_text_after_offset (AtkText *text,
249 AtkTextBoundary boundary_type,
254 gpointer layout = NULL;
255 GailTextUtil *gail_text = NULL;
256 gchar *retval = NULL;
257 GtkTextBuffer *buffer;
259 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
261 /* State is defunct */
264 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
268 gail_text = gail_text_util_new ();
269 gail_text_util_buffer_setup (gail_text, buffer);
270 retval = gail_text_util_get_text (gail_text, layout,
271 GAIL_AFTER_OFFSET, boundary_type,
272 offset, start_offset, end_offset);
273 g_object_unref (gail_text);
279 ev_view_accessible_get_character_count (AtkText *text)
282 GtkTextBuffer *buffer;
285 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
287 /* State is defunct */
290 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
294 retval = gtk_text_buffer_get_char_count (buffer);
300 ev_view_accessible_get_caret_offset (AtkText *text)
303 GtkTextBuffer *buffer;
304 GtkTextMark *cursor_mark;
305 GtkTextIter cursor_itr;
308 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
310 /* State is defunct */
313 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
317 cursor_mark = gtk_text_buffer_get_insert (buffer);
318 gtk_text_buffer_get_iter_at_mark (buffer, &cursor_itr, cursor_mark);
319 retval = gtk_text_iter_get_offset (&cursor_itr);
325 ev_view_accessible_set_caret_offset (AtkText *text, gint offset)
328 GtkTextBuffer *buffer;
331 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
333 /* State is defunct */
336 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
340 gtk_text_buffer_get_iter_at_offset (buffer, &pos_itr, offset);
341 gtk_text_buffer_place_cursor (buffer, &pos_itr);
346 static AtkAttributeSet*
347 ev_view_accessible_get_run_attributes (AtkText *text,
353 GtkTextBuffer *buffer;
354 AtkAttributeSet *retval;
356 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
358 /* State is defunct */
361 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
365 retval = gail_misc_buffer_get_run_attributes (buffer, offset,
366 start_offset, end_offset);
371 static AtkAttributeSet*
372 ev_view_accessible_get_default_attributes (AtkText *text)
376 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
378 /* State is defunct */
384 ev_view_accessible_get_character_extents (AtkText *text,
392 GtkWidget *widget, *toplevel;
393 EvRectangle *areas = NULL;
394 EvRectangle *rect = NULL;
396 EvPageCache *page_cache;
397 gint x_widget, y_widget, x_window, y_window;
400 GdkRectangle page_area;
402 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
404 /* State is defunct */
407 page_cache = EV_VIEW (widget)->page_cache;
411 ev_view_get_page_extents (EV_VIEW (widget), EV_VIEW (widget)->current_page,
412 &page_area, &border);
414 scale = EV_VIEW (widget)->scale;
415 ev_page_cache_get_text_layout (page_cache, EV_VIEW (widget)->current_page, &areas, &n_areas);
419 if (offset > n_areas)
422 rect = areas + offset;
423 *x = (int)(rect->x1 * scale);
424 *y = (int)(rect->y1 * scale);
426 *width = (int)(fabs (rect->x2 - rect->x1) * scale);
427 *height = (int)(fabs (rect->y2 - rect->y1) * scale);
429 toplevel = gtk_widget_get_toplevel (widget);
430 gtk_widget_translate_coordinates (widget, toplevel, 0, 0, &x_widget, &y_widget);
434 if (coords == ATK_XY_SCREEN) {
435 gdk_window_get_origin (gtk_widget_get_window (toplevel), &x_window, &y_window);
440 *x -= EV_VIEW (widget)->scroll_x;
441 *y -= EV_VIEW (widget)->scroll_y;
448 ev_view_accessible_get_offset_at_point (AtkText *text,
453 GtkWidget *widget, *toplevel;
454 EvRectangle *areas = NULL;
455 EvRectangle *rect = NULL;
458 EvPageCache *page_cache;
459 gint x_window, y_window, x_widget, y_widget;
460 gint offset=-1, rx, ry;
463 GdkRectangle page_area;
465 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
467 /* State is defunct */
470 page_cache = EV_VIEW (widget)->page_cache;
474 ev_view_get_page_extents (EV_VIEW (widget), EV_VIEW (widget)->current_page,
475 &page_area, &border);
477 scale = EV_VIEW (widget)->scale;
478 ev_page_cache_get_text_layout (page_cache, EV_VIEW (widget)->current_page, &areas, &n_areas);
488 rx += EV_VIEW (widget)->scroll_x;
489 ry += EV_VIEW (widget)->scroll_y;
491 toplevel = gtk_widget_get_toplevel (widget);
492 gtk_widget_translate_coordinates (widget, toplevel, 0, 0, &x_widget, &y_widget);
496 if (coords == ATK_XY_SCREEN) {
497 gdk_window_get_origin (gtk_widget_get_window (toplevel), &x_window, &y_window);
505 for (i = 0; i < n_areas; i++) {
507 if (rx >= rect->x1 && rx <= rect->x2 &&
508 ry >= rect->y1 && ry <= rect->y2)
516 ev_view_accessible_get_n_selections (AtkText *text)
519 GtkTextBuffer *buffer;
520 GtkTextIter start, end;
521 gint select_start, select_end;
523 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
525 /* State is defunct */
528 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
532 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
533 select_start = gtk_text_iter_get_offset (&start);
534 select_end = gtk_text_iter_get_offset (&end);
536 if (select_start != select_end)
543 ev_view_accessible_get_selection (AtkText *text,
549 GtkTextBuffer *buffer;
550 GtkTextIter start, end;
551 gchar *retval = NULL;
553 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
555 /* State is defunct */
558 if (selection_num != 0)
561 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
565 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
566 *start_pos = gtk_text_iter_get_offset (&start);
567 *end_pos = gtk_text_iter_get_offset (&end);
569 if (*start_pos != *end_pos)
570 retval = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
576 ev_view_accessible_add_selection (AtkText *text,
581 GtkTextBuffer *buffer;
583 GtkTextIter start, end;
584 gint select_start, select_end;
585 gboolean retval = FALSE;
587 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
589 /* State is defunct */
592 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
596 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
597 select_start = gtk_text_iter_get_offset (&start);
598 select_end = gtk_text_iter_get_offset (&end);
600 /* If there is already a selection, then don't allow
601 * another to be added
603 if (select_start == select_end) {
604 gtk_text_buffer_get_iter_at_offset (buffer, &pos_itr, start_pos);
605 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &pos_itr);
606 gtk_text_buffer_get_iter_at_offset (buffer, &pos_itr, end_pos);
607 gtk_text_buffer_move_mark_by_name (buffer, "insert", &pos_itr);
616 ev_view_accessible_remove_selection (AtkText *text,
620 GtkTextBuffer *buffer;
621 GtkTextMark *cursor_mark;
622 GtkTextIter cursor_itr;
623 GtkTextIter start, end;
624 gint select_start, select_end;
625 gboolean retval = FALSE;
627 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
629 /* State is defunct */
632 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
636 gtk_text_buffer_get_selection_bounds(buffer, &start, &end);
637 select_start = gtk_text_iter_get_offset(&start);
638 select_end = gtk_text_iter_get_offset(&end);
640 if (select_start != select_end) {
641 /* Setting the start & end of the selected region
642 * to the caret position turns off the selection.
644 cursor_mark = gtk_text_buffer_get_insert (buffer);
645 gtk_text_buffer_get_iter_at_mark (buffer, &cursor_itr, cursor_mark);
646 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &cursor_itr);
655 ev_view_accessible_set_selection (AtkText *text,
661 GtkTextBuffer *buffer;
663 GtkTextIter start, end;
664 gint select_start, select_end;
665 gboolean retval = FALSE;
667 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
669 /* State is defunct */
672 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
676 gtk_text_buffer_get_selection_bounds(buffer, &start, &end);
677 select_start = gtk_text_iter_get_offset(&start);
678 select_end = gtk_text_iter_get_offset(&end);
680 if (select_start != select_end) {
681 gtk_text_buffer_get_iter_at_offset (buffer, &pos_itr, start_pos);
682 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &pos_itr);
683 gtk_text_buffer_get_iter_at_offset (buffer, &pos_itr, end_pos);
684 gtk_text_buffer_move_mark_by_name (buffer, "insert", &pos_itr);
692 static void ev_view_accessible_text_iface_init (AtkTextIface * iface)
694 g_return_if_fail (iface != NULL);
696 iface->get_text = ev_view_accessible_get_text;
697 iface->get_character_at_offset = ev_view_accessible_get_character_at_offset;
698 iface->get_text_before_offset = ev_view_accessible_get_text_before_offset;
699 iface->get_text_at_offset = ev_view_accessible_get_text_at_offset;
700 iface->get_text_after_offset = ev_view_accessible_get_text_after_offset;
701 iface->get_caret_offset = ev_view_accessible_get_caret_offset;
702 iface->set_caret_offset = ev_view_accessible_set_caret_offset;
703 iface->get_character_count = ev_view_accessible_get_character_count;
704 iface->get_n_selections = ev_view_accessible_get_n_selections;
705 iface->get_selection = ev_view_accessible_get_selection;
706 iface->add_selection = ev_view_accessible_add_selection;
707 iface->remove_selection = ev_view_accessible_remove_selection;
708 iface->set_selection = ev_view_accessible_set_selection;
709 iface->get_run_attributes = ev_view_accessible_get_run_attributes;
710 iface->get_default_attributes = ev_view_accessible_get_default_attributes;
711 iface->get_character_extents = ev_view_accessible_get_character_extents;
712 iface->get_offset_at_point = ev_view_accessible_get_offset_at_point;
717 ev_view_accessible_idle_do_action (gpointer data)
719 EvViewAccessiblePriv* priv = EV_VIEW_ACCESSIBLE_GET_PRIVATE (data);
721 ev_view_scroll (EV_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (data))),
724 priv->action_idle_handler = 0;
729 ev_view_accessible_action_do_action (AtkAction *action,
732 EvViewAccessiblePriv* priv = EV_VIEW_ACCESSIBLE_GET_PRIVATE (action);
734 if (gtk_accessible_get_widget (GTK_ACCESSIBLE (action)) == NULL)
737 if (priv->action_idle_handler)
741 case ACTION_SCROLL_UP:
742 priv->idle_scroll = GTK_SCROLL_PAGE_BACKWARD;
744 case ACTION_SCROLL_DOWN:
745 priv->idle_scroll = GTK_SCROLL_PAGE_FORWARD;
750 priv->action_idle_handler = g_idle_add (ev_view_accessible_idle_do_action,
756 ev_view_accessible_action_get_n_actions (AtkAction *action)
762 ev_view_accessible_action_get_description (AtkAction *action,
765 EvViewAccessiblePriv* priv = EV_VIEW_ACCESSIBLE_GET_PRIVATE (action);
767 if (i < 0 || i >= LAST_ACTION)
770 if (priv->action_descriptions[i])
771 return priv->action_descriptions[i];
773 return ev_view_accessible_action_descriptions[i];
777 ev_view_accessible_action_get_name (AtkAction *action,
780 if (i < 0 || i >= LAST_ACTION)
783 return ev_view_accessible_action_names[i];
787 ev_view_accessible_action_set_description (AtkAction *action,
789 const gchar *description)
791 EvViewAccessiblePriv* priv = EV_VIEW_ACCESSIBLE_GET_PRIVATE (action);
792 gchar *old_description;
794 if (i < 0 || i >= LAST_ACTION)
797 old_description = priv->action_descriptions[i];
798 priv->action_descriptions[i] = g_strdup (description);
799 g_free (old_description);
804 static void ev_view_accessible_action_iface_init (AtkActionIface * iface)
806 iface->do_action = ev_view_accessible_action_do_action;
807 iface->get_n_actions = ev_view_accessible_action_get_n_actions;
808 iface->get_description = ev_view_accessible_action_get_description;
809 iface->get_name = ev_view_accessible_action_get_name;
810 iface->set_description = ev_view_accessible_action_set_description;
813 GType ev_view_accessible_get_type (void)
815 static GType type = 0;
817 if (G_UNLIKELY (type == 0)) {
820 (GBaseInitFunc) NULL, /* base init */
821 (GBaseFinalizeFunc) NULL, /* base finalize */
822 (GClassInitFunc) ev_view_accessible_class_init, /* class init */
823 (GClassFinalizeFunc) NULL, /* class finalize */
824 NULL, /* class data */
825 0, /* instance size */
826 0, /* nb preallocs */
827 (GInstanceInitFunc) NULL, /* instance init */
828 NULL /* value table */
831 const GInterfaceInfo atk_text_info = {
833 ev_view_accessible_text_iface_init,
834 (GInterfaceFinalizeFunc) NULL,
838 const GInterfaceInfo atk_action_info = {
840 ev_view_accessible_action_iface_init,
841 (GInterfaceFinalizeFunc) NULL,
845 * Figure out the size of the class and instance
846 * we are deriving from
848 AtkObjectFactory *factory;
851 GType derived_atk_type;
853 derived_type = g_type_parent (EV_TYPE_VIEW);
854 factory = atk_registry_get_factory (atk_get_default_registry (),
856 derived_atk_type = atk_object_factory_get_accessible_type (factory);
858 g_type_query (derived_atk_type, &query);
859 tinfo.class_size = query.class_size;
860 tinfo.instance_size = query.instance_size;
862 type = g_type_register_static (derived_atk_type, "EvViewAccessible",
864 g_type_add_interface_static (type, ATK_TYPE_TEXT,
866 g_type_add_interface_static (type, ATK_TYPE_ACTION,
873 static AtkObject *ev_view_accessible_new(GObject * obj)
875 AtkObject *accessible;
877 g_return_val_if_fail(EV_IS_VIEW (obj), NULL);
879 accessible = g_object_new (ev_view_accessible_get_type (), NULL);
880 atk_object_initialize (accessible, obj);
882 atk_object_set_name (ATK_OBJECT (accessible), _("Document View"));
883 atk_object_set_role (ATK_OBJECT (accessible), ATK_ROLE_UNKNOWN);
888 typedef AtkObjectFactory EvViewAccessibleFactory;
889 typedef AtkObjectFactoryClass EvViewAccessibleFactoryClass;
891 static void ev_view_accessible_factory_init (EvViewAccessibleFactory *factory)
895 static GType ev_view_accessible_factory_get_accessible_type(void)
897 return ev_view_accessible_get_type();
900 static AtkObject *ev_view_accessible_factory_create_accessible (GObject * obj)
902 return ev_view_accessible_new(obj);
905 static void ev_view_accessible_factory_class_init (AtkObjectFactoryClass * klass)
907 klass->create_accessible = ev_view_accessible_factory_create_accessible;
908 klass->get_accessible_type = ev_view_accessible_factory_get_accessible_type;
911 G_DEFINE_TYPE (EvViewAccessibleFactory, ev_view_accessible_factory, ATK_TYPE_OBJECT_FACTORY)