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);
120 gtk_text_buffer_set_text (priv->buffer, retval, -1);
126 ev_view_accessible_get_text (AtkText *text,
131 GtkTextIter start, end;
132 GtkTextBuffer *buffer;
135 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
137 /* State is defunct */
140 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
144 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_pos);
145 gtk_text_buffer_get_iter_at_offset (buffer, &end, end_pos);
146 retval = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
152 ev_view_accessible_get_character_at_offset (AtkText *text,
156 GtkTextIter start, end;
157 GtkTextBuffer *buffer;
161 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
163 /* State is defunct */
166 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
170 if (offset >= gtk_text_buffer_get_char_count (buffer))
173 gtk_text_buffer_get_iter_at_offset (buffer, &start, offset);
175 gtk_text_iter_forward_char (&end);
176 string = gtk_text_buffer_get_slice (buffer, &start, &end, FALSE);
177 unichar = g_utf8_get_char (string);
184 ev_view_accessible_get_text_before_offset (AtkText *text,
186 AtkTextBoundary boundary_type,
191 gpointer layout = NULL;
192 GailTextUtil *gail_text = NULL;
193 gchar *retval = NULL;
194 GtkTextBuffer *buffer;
196 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
198 /* State is defunct */
201 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
205 gail_text = gail_text_util_new ();
206 gail_text_util_buffer_setup (gail_text, buffer);
207 retval = gail_text_util_get_text (gail_text, layout,
208 GAIL_BEFORE_OFFSET, boundary_type,
209 offset, start_offset, end_offset);
210 g_object_unref (gail_text);
216 ev_view_accessible_get_text_at_offset (AtkText *text,
218 AtkTextBoundary boundary_type,
223 gpointer layout = NULL;
224 GailTextUtil *gail_text = NULL;
225 gchar *retval = NULL;
226 GtkTextBuffer *buffer;
228 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
230 /* State is defunct */
233 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
237 gail_text = gail_text_util_new ();
238 gail_text_util_buffer_setup (gail_text, buffer);
239 retval = gail_text_util_get_text (gail_text, layout,
240 GAIL_AT_OFFSET, boundary_type,
241 offset, start_offset, end_offset);
242 g_object_unref (gail_text);
248 ev_view_accessible_get_text_after_offset (AtkText *text,
250 AtkTextBoundary boundary_type,
255 gpointer layout = NULL;
256 GailTextUtil *gail_text = NULL;
257 gchar *retval = NULL;
258 GtkTextBuffer *buffer;
260 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
262 /* State is defunct */
265 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
269 gail_text = gail_text_util_new ();
270 gail_text_util_buffer_setup (gail_text, buffer);
271 retval = gail_text_util_get_text (gail_text, layout,
272 GAIL_AFTER_OFFSET, boundary_type,
273 offset, start_offset, end_offset);
274 g_object_unref (gail_text);
280 ev_view_accessible_get_character_count (AtkText *text)
283 GtkTextBuffer *buffer;
286 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
288 /* State is defunct */
291 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
295 retval = gtk_text_buffer_get_char_count (buffer);
301 ev_view_accessible_get_caret_offset (AtkText *text)
304 GtkTextBuffer *buffer;
305 GtkTextMark *cursor_mark;
306 GtkTextIter cursor_itr;
309 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
311 /* State is defunct */
314 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
318 cursor_mark = gtk_text_buffer_get_insert (buffer);
319 gtk_text_buffer_get_iter_at_mark (buffer, &cursor_itr, cursor_mark);
320 retval = gtk_text_iter_get_offset (&cursor_itr);
326 ev_view_accessible_set_caret_offset (AtkText *text, gint offset)
329 GtkTextBuffer *buffer;
332 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
334 /* State is defunct */
337 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
341 gtk_text_buffer_get_iter_at_offset (buffer, &pos_itr, offset);
342 gtk_text_buffer_place_cursor (buffer, &pos_itr);
347 static AtkAttributeSet*
348 ev_view_accessible_get_run_attributes (AtkText *text,
354 GtkTextBuffer *buffer;
355 AtkAttributeSet *retval;
357 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
359 /* State is defunct */
362 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
366 retval = gail_misc_buffer_get_run_attributes (buffer, offset,
367 start_offset, end_offset);
372 static AtkAttributeSet*
373 ev_view_accessible_get_default_attributes (AtkText *text)
377 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
379 /* State is defunct */
385 ev_view_accessible_get_character_extents (AtkText *text,
393 GtkWidget *widget, *toplevel;
394 EvRectangle *areas = NULL;
395 EvRectangle *rect = NULL;
397 EvPageCache *page_cache;
398 gint x_widget, y_widget, x_window, y_window;
401 GdkRectangle page_area;
403 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
405 /* State is defunct */
408 page_cache = EV_VIEW (widget)->page_cache;
412 ev_view_get_page_extents (EV_VIEW (widget), EV_VIEW (widget)->current_page,
413 &page_area, &border);
415 scale = EV_VIEW (widget)->scale;
416 ev_page_cache_get_text_layout (page_cache, EV_VIEW (widget)->current_page, &areas, &n_areas);
420 if (offset >= n_areas)
423 rect = areas + offset;
424 *x = (int)(rect->x1 * scale);
425 *y = (int)(rect->y1 * scale);
427 *width = (int)(fabs (rect->x2 - rect->x1) * scale);
428 *height = (int)(fabs (rect->y2 - rect->y1) * scale);
430 toplevel = gtk_widget_get_toplevel (widget);
431 gtk_widget_translate_coordinates (widget, toplevel, 0, 0, &x_widget, &y_widget);
435 if (coords == ATK_XY_SCREEN) {
436 gdk_window_get_origin (gtk_widget_get_window (toplevel), &x_window, &y_window);
441 *x -= EV_VIEW (widget)->scroll_x;
442 *y -= EV_VIEW (widget)->scroll_y;
449 ev_view_accessible_get_offset_at_point (AtkText *text,
454 GtkWidget *widget, *toplevel;
455 EvRectangle *areas = NULL;
456 EvRectangle *rect = NULL;
459 EvPageCache *page_cache;
460 gint x_window, y_window, x_widget, y_widget;
461 gint offset=-1, rx, ry;
464 GdkRectangle page_area;
466 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
468 /* State is defunct */
471 page_cache = EV_VIEW (widget)->page_cache;
475 ev_view_get_page_extents (EV_VIEW (widget), EV_VIEW (widget)->current_page,
476 &page_area, &border);
478 scale = EV_VIEW (widget)->scale;
479 ev_page_cache_get_text_layout (page_cache, EV_VIEW (widget)->current_page, &areas, &n_areas);
489 rx += EV_VIEW (widget)->scroll_x;
490 ry += EV_VIEW (widget)->scroll_y;
492 toplevel = gtk_widget_get_toplevel (widget);
493 gtk_widget_translate_coordinates (widget, toplevel, 0, 0, &x_widget, &y_widget);
497 if (coords == ATK_XY_SCREEN) {
498 gdk_window_get_origin (gtk_widget_get_window (toplevel), &x_window, &y_window);
506 for (i = 0; i < n_areas; i++) {
508 if (rx >= rect->x1 && rx <= rect->x2 &&
509 ry >= rect->y1 && ry <= rect->y2)
517 ev_view_accessible_get_n_selections (AtkText *text)
520 GtkTextBuffer *buffer;
521 GtkTextIter start, end;
522 gint select_start, select_end;
524 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
526 /* State is defunct */
529 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
533 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
534 select_start = gtk_text_iter_get_offset (&start);
535 select_end = gtk_text_iter_get_offset (&end);
537 if (select_start != select_end)
544 ev_view_accessible_get_selection (AtkText *text,
550 GtkTextBuffer *buffer;
551 GtkTextIter start, end;
552 gchar *retval = NULL;
554 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
556 /* State is defunct */
559 if (selection_num != 0)
562 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
566 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
567 *start_pos = gtk_text_iter_get_offset (&start);
568 *end_pos = gtk_text_iter_get_offset (&end);
570 if (*start_pos != *end_pos)
571 retval = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
577 ev_view_accessible_add_selection (AtkText *text,
582 GtkTextBuffer *buffer;
584 GtkTextIter start, end;
585 gint select_start, select_end;
586 gboolean retval = FALSE;
588 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
590 /* State is defunct */
593 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
597 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
598 select_start = gtk_text_iter_get_offset (&start);
599 select_end = gtk_text_iter_get_offset (&end);
601 /* If there is already a selection, then don't allow
602 * another to be added
604 if (select_start == select_end) {
605 gtk_text_buffer_get_iter_at_offset (buffer, &pos_itr, start_pos);
606 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &pos_itr);
607 gtk_text_buffer_get_iter_at_offset (buffer, &pos_itr, end_pos);
608 gtk_text_buffer_move_mark_by_name (buffer, "insert", &pos_itr);
617 ev_view_accessible_remove_selection (AtkText *text,
621 GtkTextBuffer *buffer;
622 GtkTextMark *cursor_mark;
623 GtkTextIter cursor_itr;
624 GtkTextIter start, end;
625 gint select_start, select_end;
626 gboolean retval = FALSE;
628 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
630 /* State is defunct */
633 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
637 gtk_text_buffer_get_selection_bounds(buffer, &start, &end);
638 select_start = gtk_text_iter_get_offset(&start);
639 select_end = gtk_text_iter_get_offset(&end);
641 if (select_start != select_end) {
642 /* Setting the start & end of the selected region
643 * to the caret position turns off the selection.
645 cursor_mark = gtk_text_buffer_get_insert (buffer);
646 gtk_text_buffer_get_iter_at_mark (buffer, &cursor_itr, cursor_mark);
647 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &cursor_itr);
656 ev_view_accessible_set_selection (AtkText *text,
662 GtkTextBuffer *buffer;
664 GtkTextIter start, end;
665 gint select_start, select_end;
666 gboolean retval = FALSE;
668 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
670 /* State is defunct */
673 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
677 gtk_text_buffer_get_selection_bounds(buffer, &start, &end);
678 select_start = gtk_text_iter_get_offset(&start);
679 select_end = gtk_text_iter_get_offset(&end);
681 if (select_start != select_end) {
682 gtk_text_buffer_get_iter_at_offset (buffer, &pos_itr, start_pos);
683 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &pos_itr);
684 gtk_text_buffer_get_iter_at_offset (buffer, &pos_itr, end_pos);
685 gtk_text_buffer_move_mark_by_name (buffer, "insert", &pos_itr);
693 static void ev_view_accessible_text_iface_init (AtkTextIface * iface)
695 g_return_if_fail (iface != NULL);
697 iface->get_text = ev_view_accessible_get_text;
698 iface->get_character_at_offset = ev_view_accessible_get_character_at_offset;
699 iface->get_text_before_offset = ev_view_accessible_get_text_before_offset;
700 iface->get_text_at_offset = ev_view_accessible_get_text_at_offset;
701 iface->get_text_after_offset = ev_view_accessible_get_text_after_offset;
702 iface->get_caret_offset = ev_view_accessible_get_caret_offset;
703 iface->set_caret_offset = ev_view_accessible_set_caret_offset;
704 iface->get_character_count = ev_view_accessible_get_character_count;
705 iface->get_n_selections = ev_view_accessible_get_n_selections;
706 iface->get_selection = ev_view_accessible_get_selection;
707 iface->add_selection = ev_view_accessible_add_selection;
708 iface->remove_selection = ev_view_accessible_remove_selection;
709 iface->set_selection = ev_view_accessible_set_selection;
710 iface->get_run_attributes = ev_view_accessible_get_run_attributes;
711 iface->get_default_attributes = ev_view_accessible_get_default_attributes;
712 iface->get_character_extents = ev_view_accessible_get_character_extents;
713 iface->get_offset_at_point = ev_view_accessible_get_offset_at_point;
718 ev_view_accessible_idle_do_action (gpointer data)
720 EvViewAccessiblePriv* priv = EV_VIEW_ACCESSIBLE_GET_PRIVATE (data);
722 ev_view_scroll (EV_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (data))),
725 priv->action_idle_handler = 0;
730 ev_view_accessible_action_do_action (AtkAction *action,
733 EvViewAccessiblePriv* priv = EV_VIEW_ACCESSIBLE_GET_PRIVATE (action);
735 if (gtk_accessible_get_widget (GTK_ACCESSIBLE (action)) == NULL)
738 if (priv->action_idle_handler)
742 case ACTION_SCROLL_UP:
743 priv->idle_scroll = GTK_SCROLL_PAGE_BACKWARD;
745 case ACTION_SCROLL_DOWN:
746 priv->idle_scroll = GTK_SCROLL_PAGE_FORWARD;
751 priv->action_idle_handler = g_idle_add (ev_view_accessible_idle_do_action,
757 ev_view_accessible_action_get_n_actions (AtkAction *action)
763 ev_view_accessible_action_get_description (AtkAction *action,
766 EvViewAccessiblePriv* priv = EV_VIEW_ACCESSIBLE_GET_PRIVATE (action);
768 if (i < 0 || i >= LAST_ACTION)
771 if (priv->action_descriptions[i])
772 return priv->action_descriptions[i];
774 return ev_view_accessible_action_descriptions[i];
778 ev_view_accessible_action_get_name (AtkAction *action,
781 if (i < 0 || i >= LAST_ACTION)
784 return ev_view_accessible_action_names[i];
788 ev_view_accessible_action_set_description (AtkAction *action,
790 const gchar *description)
792 EvViewAccessiblePriv* priv = EV_VIEW_ACCESSIBLE_GET_PRIVATE (action);
793 gchar *old_description;
795 if (i < 0 || i >= LAST_ACTION)
798 old_description = priv->action_descriptions[i];
799 priv->action_descriptions[i] = g_strdup (description);
800 g_free (old_description);
805 static void ev_view_accessible_action_iface_init (AtkActionIface * iface)
807 iface->do_action = ev_view_accessible_action_do_action;
808 iface->get_n_actions = ev_view_accessible_action_get_n_actions;
809 iface->get_description = ev_view_accessible_action_get_description;
810 iface->get_name = ev_view_accessible_action_get_name;
811 iface->set_description = ev_view_accessible_action_set_description;
814 GType ev_view_accessible_get_type (void)
816 static GType type = 0;
818 if (G_UNLIKELY (type == 0)) {
821 (GBaseInitFunc) NULL, /* base init */
822 (GBaseFinalizeFunc) NULL, /* base finalize */
823 (GClassInitFunc) ev_view_accessible_class_init, /* class init */
824 (GClassFinalizeFunc) NULL, /* class finalize */
825 NULL, /* class data */
826 0, /* instance size */
827 0, /* nb preallocs */
828 (GInstanceInitFunc) NULL, /* instance init */
829 NULL /* value table */
832 const GInterfaceInfo atk_text_info = {
834 ev_view_accessible_text_iface_init,
835 (GInterfaceFinalizeFunc) NULL,
839 const GInterfaceInfo atk_action_info = {
841 ev_view_accessible_action_iface_init,
842 (GInterfaceFinalizeFunc) NULL,
846 * Figure out the size of the class and instance
847 * we are deriving from
849 AtkObjectFactory *factory;
852 GType derived_atk_type;
854 derived_type = g_type_parent (EV_TYPE_VIEW);
855 factory = atk_registry_get_factory (atk_get_default_registry (),
857 derived_atk_type = atk_object_factory_get_accessible_type (factory);
859 g_type_query (derived_atk_type, &query);
860 tinfo.class_size = query.class_size;
861 tinfo.instance_size = query.instance_size;
863 type = g_type_register_static (derived_atk_type, "EvViewAccessible",
865 g_type_add_interface_static (type, ATK_TYPE_TEXT,
867 g_type_add_interface_static (type, ATK_TYPE_ACTION,
874 static AtkObject *ev_view_accessible_new(GObject * obj)
876 AtkObject *accessible;
878 g_return_val_if_fail(EV_IS_VIEW (obj), NULL);
880 accessible = g_object_new (ev_view_accessible_get_type (), NULL);
881 atk_object_initialize (accessible, obj);
883 atk_object_set_name (ATK_OBJECT (accessible), _("Document View"));
884 atk_object_set_role (ATK_OBJECT (accessible), ATK_ROLE_DOCUMENT_FRAME);
889 typedef AtkObjectFactory EvViewAccessibleFactory;
890 typedef AtkObjectFactoryClass EvViewAccessibleFactoryClass;
892 static void ev_view_accessible_factory_init (EvViewAccessibleFactory *factory)
896 static GType ev_view_accessible_factory_get_accessible_type(void)
898 return ev_view_accessible_get_type();
901 static AtkObject *ev_view_accessible_factory_create_accessible (GObject * obj)
903 return ev_view_accessible_new(obj);
906 static void ev_view_accessible_factory_class_init (AtkObjectFactoryClass * klass)
908 klass->create_accessible = ev_view_accessible_factory_create_accessible;
909 klass->get_accessible_type = ev_view_accessible_factory_get_accessible_type;
912 G_DEFINE_TYPE (EvViewAccessibleFactory, ev_view_accessible_factory, ATK_TYPE_OBJECT_FACTORY)