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)
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)
499 gdk_window_get_origin (gtk_widget_get_window (toplevel), &x_window, &y_window);
507 for (i = 0; i < n_areas; i++)
510 if (rx >= rect->x1 && rx <= rect->x2 &&
511 ry >= rect->y1 && ry <= rect->y2)
519 ev_view_accessible_get_n_selections (AtkText *text)
522 GtkTextBuffer *buffer;
523 GtkTextIter start, end;
524 gint select_start, select_end;
526 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
528 /* State is defunct */
531 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
535 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
536 select_start = gtk_text_iter_get_offset (&start);
537 select_end = gtk_text_iter_get_offset (&end);
539 if (select_start != select_end)
546 ev_view_accessible_get_selection (AtkText *text,
552 GtkTextBuffer *buffer;
553 GtkTextIter start, end;
554 gchar *retval = NULL;
556 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
558 /* State is defunct */
561 if (selection_num != 0)
564 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
568 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
569 *start_pos = gtk_text_iter_get_offset (&start);
570 *end_pos = gtk_text_iter_get_offset (&end);
572 if (*start_pos != *end_pos)
573 retval = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
579 ev_view_accessible_add_selection (AtkText *text,
584 GtkTextBuffer *buffer;
586 GtkTextIter start, end;
587 gint select_start, select_end;
588 gboolean retval = FALSE;
590 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
592 /* State is defunct */
595 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
599 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
600 select_start = gtk_text_iter_get_offset (&start);
601 select_end = gtk_text_iter_get_offset (&end);
603 /* If there is already a selection, then don't allow
604 * another to be added
606 if (select_start == select_end) {
607 gtk_text_buffer_get_iter_at_offset (buffer, &pos_itr, start_pos);
608 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &pos_itr);
609 gtk_text_buffer_get_iter_at_offset (buffer, &pos_itr, end_pos);
610 gtk_text_buffer_move_mark_by_name (buffer, "insert", &pos_itr);
619 ev_view_accessible_remove_selection (AtkText *text,
623 GtkTextBuffer *buffer;
624 GtkTextMark *cursor_mark;
625 GtkTextIter cursor_itr;
626 GtkTextIter start, end;
627 gint select_start, select_end;
628 gboolean retval = FALSE;
630 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
632 /* State is defunct */
635 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
639 gtk_text_buffer_get_selection_bounds(buffer, &start, &end);
640 select_start = gtk_text_iter_get_offset(&start);
641 select_end = gtk_text_iter_get_offset(&end);
643 if (select_start != select_end) {
644 /* Setting the start & end of the selected region
645 * to the caret position turns off the selection.
647 cursor_mark = gtk_text_buffer_get_insert (buffer);
648 gtk_text_buffer_get_iter_at_mark (buffer, &cursor_itr, cursor_mark);
649 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &cursor_itr);
658 ev_view_accessible_set_selection (AtkText *text,
664 GtkTextBuffer *buffer;
666 GtkTextIter start, end;
667 gint select_start, select_end;
668 gboolean retval = FALSE;
670 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
672 /* State is defunct */
675 buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
679 gtk_text_buffer_get_selection_bounds(buffer, &start, &end);
680 select_start = gtk_text_iter_get_offset(&start);
681 select_end = gtk_text_iter_get_offset(&end);
683 if (select_start != select_end) {
684 gtk_text_buffer_get_iter_at_offset (buffer, &pos_itr, start_pos);
685 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &pos_itr);
686 gtk_text_buffer_get_iter_at_offset (buffer, &pos_itr, end_pos);
687 gtk_text_buffer_move_mark_by_name (buffer, "insert", &pos_itr);
695 static void ev_view_accessible_text_iface_init (AtkTextIface * iface)
697 g_return_if_fail (iface != NULL);
699 iface->get_text = ev_view_accessible_get_text;
700 iface->get_character_at_offset = ev_view_accessible_get_character_at_offset;
701 iface->get_text_before_offset = ev_view_accessible_get_text_before_offset;
702 iface->get_text_at_offset = ev_view_accessible_get_text_at_offset;
703 iface->get_text_after_offset = ev_view_accessible_get_text_after_offset;
704 iface->get_caret_offset = ev_view_accessible_get_caret_offset;
705 iface->set_caret_offset = ev_view_accessible_set_caret_offset;
706 iface->get_character_count = ev_view_accessible_get_character_count;
707 iface->get_n_selections = ev_view_accessible_get_n_selections;
708 iface->get_selection = ev_view_accessible_get_selection;
709 iface->add_selection = ev_view_accessible_add_selection;
710 iface->remove_selection = ev_view_accessible_remove_selection;
711 iface->set_selection = ev_view_accessible_set_selection;
712 iface->get_run_attributes = ev_view_accessible_get_run_attributes;
713 iface->get_default_attributes = ev_view_accessible_get_default_attributes;
714 iface->get_character_extents = ev_view_accessible_get_character_extents;
715 iface->get_offset_at_point = ev_view_accessible_get_offset_at_point;
720 ev_view_accessible_idle_do_action (gpointer data)
722 EvViewAccessiblePriv* priv = EV_VIEW_ACCESSIBLE_GET_PRIVATE (data);
724 ev_view_scroll (EV_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (data))),
727 priv->action_idle_handler = 0;
732 ev_view_accessible_action_do_action (AtkAction *action,
735 EvViewAccessiblePriv* priv = EV_VIEW_ACCESSIBLE_GET_PRIVATE (action);
737 if (gtk_accessible_get_widget (GTK_ACCESSIBLE (action)) == NULL)
740 if (priv->action_idle_handler)
744 case ACTION_SCROLL_UP:
745 priv->idle_scroll = GTK_SCROLL_PAGE_BACKWARD;
747 case ACTION_SCROLL_DOWN:
748 priv->idle_scroll = GTK_SCROLL_PAGE_FORWARD;
753 priv->action_idle_handler = g_idle_add (ev_view_accessible_idle_do_action,
759 ev_view_accessible_action_get_n_actions (AtkAction *action)
765 ev_view_accessible_action_get_description (AtkAction *action,
768 EvViewAccessiblePriv* priv = EV_VIEW_ACCESSIBLE_GET_PRIVATE (action);
770 if (i < 0 || i >= LAST_ACTION)
773 if (priv->action_descriptions[i])
774 return priv->action_descriptions[i];
776 return ev_view_accessible_action_descriptions[i];
780 ev_view_accessible_action_get_name (AtkAction *action,
783 if (i < 0 || i >= LAST_ACTION)
786 return ev_view_accessible_action_names[i];
790 ev_view_accessible_action_set_description (AtkAction *action,
792 const gchar *description)
794 EvViewAccessiblePriv* priv = EV_VIEW_ACCESSIBLE_GET_PRIVATE (action);
795 gchar *old_description;
797 if (i < 0 || i >= LAST_ACTION)
800 old_description = priv->action_descriptions[i];
801 priv->action_descriptions[i] = g_strdup (description);
802 g_free (old_description);
807 static void ev_view_accessible_action_iface_init (AtkActionIface * iface)
809 iface->do_action = ev_view_accessible_action_do_action;
810 iface->get_n_actions = ev_view_accessible_action_get_n_actions;
811 iface->get_description = ev_view_accessible_action_get_description;
812 iface->get_name = ev_view_accessible_action_get_name;
813 iface->set_description = ev_view_accessible_action_set_description;
816 GType ev_view_accessible_get_type (void)
818 static GType type = 0;
820 if (G_UNLIKELY (type == 0)) {
823 (GBaseInitFunc) NULL, /* base init */
824 (GBaseFinalizeFunc) NULL, /* base finalize */
825 (GClassInitFunc) ev_view_accessible_class_init, /* class init */
826 (GClassFinalizeFunc) NULL, /* class finalize */
827 NULL, /* class data */
828 0, /* instance size */
829 0, /* nb preallocs */
830 (GInstanceInitFunc) NULL, /* instance init */
831 NULL /* value table */
834 const GInterfaceInfo atk_text_info = {
836 ev_view_accessible_text_iface_init,
837 (GInterfaceFinalizeFunc) NULL,
841 const GInterfaceInfo atk_action_info = {
843 ev_view_accessible_action_iface_init,
844 (GInterfaceFinalizeFunc) NULL,
848 * Figure out the size of the class and instance
849 * we are deriving from
851 AtkObjectFactory *factory;
854 GType derived_atk_type;
856 derived_type = g_type_parent (EV_TYPE_VIEW);
857 factory = atk_registry_get_factory (atk_get_default_registry (),
859 derived_atk_type = atk_object_factory_get_accessible_type (factory);
861 g_type_query (derived_atk_type, &query);
862 tinfo.class_size = query.class_size;
863 tinfo.instance_size = query.instance_size;
865 type = g_type_register_static (derived_atk_type, "EvViewAccessible",
867 g_type_add_interface_static (type, ATK_TYPE_TEXT,
869 g_type_add_interface_static (type, ATK_TYPE_ACTION,
876 static AtkObject *ev_view_accessible_new(GObject * obj)
878 AtkObject *accessible;
880 g_return_val_if_fail(EV_IS_VIEW (obj), NULL);
882 accessible = g_object_new (ev_view_accessible_get_type (), NULL);
883 atk_object_initialize (accessible, obj);
885 atk_object_set_name (ATK_OBJECT (accessible), _("Document View"));
886 atk_object_set_role (ATK_OBJECT (accessible), ATK_ROLE_UNKNOWN);
891 typedef AtkObjectFactory EvViewAccessibleFactory;
892 typedef AtkObjectFactoryClass EvViewAccessibleFactoryClass;
894 static void ev_view_accessible_factory_init (EvViewAccessibleFactory *factory)
898 static GType ev_view_accessible_factory_get_accessible_type(void)
900 return ev_view_accessible_get_type();
903 static AtkObject *ev_view_accessible_factory_create_accessible (GObject * obj)
905 return ev_view_accessible_new(obj);
908 static void ev_view_accessible_factory_class_init (AtkObjectFactoryClass * klass)
910 klass->create_accessible = ev_view_accessible_factory_create_accessible;
911 klass->get_accessible_type =
912 ev_view_accessible_factory_get_accessible_type;
915 G_DEFINE_TYPE (EvViewAccessibleFactory, ev_view_accessible_factory, ATK_TYPE_OBJECT_FACTORY)