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.
27 #include <glib/gi18n-lib.h>
29 #include <gdk/gdkkeysyms.h>
31 #include "ev-mapping-list.h"
32 #include "ev-document-forms.h"
33 #include "ev-document-images.h"
34 #include "ev-document-links.h"
35 #include "ev-document-layers.h"
36 #include "ev-document-misc.h"
37 #include "ev-pixbuf-cache.h"
38 #include "ev-page-cache.h"
39 #include "ev-view-marshal.h"
40 #include "ev-document-annotations.h"
41 #include "ev-annotation-window.h"
42 #include "ev-loading-window.h"
44 #include "ev-view-accessible.h"
45 #include "ev-view-private.h"
46 #include "ev-view-type-builtins.h"
48 #define EV_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EV_TYPE_VIEW, EvViewClass))
49 #define EV_IS_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), EV_TYPE_VIEW))
50 #define EV_VIEW_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), EV_TYPE_VIEW, EvViewClass))
53 SIGNAL_BINDING_ACTIVATED,
57 SIGNAL_SELECTION_CHANGED,
60 SIGNAL_LAYERS_CHANGED,
78 static guint signals[N_SIGNALS];
83 } EvViewFindDirection;
97 #define ZOOM_IN_FACTOR 1.2
98 #define ZOOM_OUT_FACTOR (1.0/ZOOM_IN_FACTOR)
100 #define SCROLL_TIME 150
103 static void view_update_range_and_current_page (EvView *view);
104 static void add_scroll_binding_keypad (GtkBindingSet *binding_set,
106 GdkModifierType modifiers,
107 GtkScrollType scroll,
108 gboolean horizontal);
109 static void ensure_rectangle_is_visible (EvView *view,
112 /*** Geometry computations ***/
113 static void compute_border (EvView *view,
117 static void get_page_y_offset (EvView *view,
120 static void view_rect_to_doc_rect (EvView *view,
121 GdkRectangle *view_rect,
122 GdkRectangle *page_area,
123 EvRectangle *doc_rect);
124 static void doc_rect_to_view_rect (EvView *view,
126 EvRectangle *doc_rect,
127 GdkRectangle *view_rect);
128 static gboolean get_dual_even_left (EvView *view);
129 static void find_page_at_location (EvView *view,
135 static void doc_point_to_view_point (EvView *view,
138 GdkPoint *view_point);
140 static EvLink * ev_view_get_link_at_location (EvView *view,
143 static char* tip_from_link (EvView *view,
146 static EvFormField *ev_view_get_form_field_at_location (EvView *view,
150 /*** Annotations ***/
151 static EvAnnotation *ev_view_get_annotation_at_location (EvView *view,
154 static void show_annotation_windows (EvView *view,
156 static void hide_annotation_windows (EvView *view,
158 /*** GtkWidget implementation ***/
159 static void ev_view_size_request_continuous_dual_page (EvView *view,
160 GtkRequisition *requisition);
161 static void ev_view_size_request_continuous (EvView *view,
162 GtkRequisition *requisition);
163 static void ev_view_size_request_dual_page (EvView *view,
164 GtkRequisition *requisition);
165 static void ev_view_size_request_single_page (EvView *view,
166 GtkRequisition *requisition);
167 static void ev_view_size_request (GtkWidget *widget,
168 GtkRequisition *requisition);
169 static void ev_view_size_allocate (GtkWidget *widget,
170 GtkAllocation *allocation);
171 static gboolean ev_view_scroll_event (GtkWidget *widget,
172 GdkEventScroll *event);
173 static gboolean ev_view_draw (GtkWidget *widget,
175 static gboolean ev_view_popup_menu (GtkWidget *widget);
176 static gboolean ev_view_button_press_event (GtkWidget *widget,
177 GdkEventButton *event);
178 static gboolean ev_view_motion_notify_event (GtkWidget *widget,
179 GdkEventMotion *event);
180 static gboolean ev_view_button_release_event (GtkWidget *widget,
181 GdkEventButton *event);
182 static gboolean ev_view_enter_notify_event (GtkWidget *widget,
183 GdkEventCrossing *event);
184 static gboolean ev_view_leave_notify_event (GtkWidget *widget,
185 GdkEventCrossing *event);
186 static void ev_view_style_updated (GtkWidget *widget);
187 static void ev_view_remove_all (EvView *view);
189 static AtkObject *ev_view_get_accessible (GtkWidget *widget);
192 static void highlight_find_results (EvView *view,
195 static void highlight_forward_search_results (EvView *view,
198 static void focus_annotation (EvView *view,
202 static void draw_one_page (EvView *view,
205 GdkRectangle *page_area,
207 GdkRectangle *expose_area,
208 gboolean *page_ready);
209 static void show_loading_window (EvView *view);
210 static void hide_loading_window (EvView *view);
211 static void ev_view_reload_page (EvView *view,
213 cairo_region_t *region);
214 static void ev_view_loading_window_move (EvView *view);
217 static void ev_view_change_page (EvView *view,
219 static void job_finished_cb (EvPixbufCache *pixbuf_cache,
220 cairo_region_t *region,
222 static void ev_view_page_changed_cb (EvDocumentModel *model,
226 static void on_adjustment_value_changed (GtkAdjustment *adjustment,
230 static void ev_view_finalize (GObject *object);
231 static void ev_view_dispose (GObject *object);
232 static void ev_view_class_init (EvViewClass *class);
233 static void ev_view_init (EvView *view);
235 /*** Zoom and sizing ***/
236 static double zoom_for_size_fit_width (gdouble doc_width,
240 static double zoom_for_size_fit_height (gdouble doc_width,
244 static double zoom_for_size_best_fit (gdouble doc_width,
248 static void ev_view_zoom_for_size (EvView *view,
251 static void ev_view_zoom_for_size_continuous_and_dual_page (EvView *view,
254 static void ev_view_zoom_for_size_continuous (EvView *view,
257 static void ev_view_zoom_for_size_dual_page (EvView *view,
260 static void ev_view_zoom_for_size_single_page (EvView *view,
264 static void ev_view_set_cursor (EvView *view,
265 EvViewCursor new_cursor);
266 static void ev_view_handle_cursor_over_xy (EvView *view,
271 static gint ev_view_find_get_n_results (EvView *view,
273 static EvRectangle *ev_view_find_get_result (EvView *view,
276 static void jump_to_find_result (EvView *view);
277 static void jump_to_find_page (EvView *view,
278 EvViewFindDirection direction,
281 static void compute_selections (EvView *view,
282 EvSelectionStyle style,
285 static void clear_selection (EvView *view);
286 static void clear_link_selected (EvView *view);
287 static void selection_free (EvViewSelection *selection);
288 static char* get_selected_text (EvView *ev_view);
289 static void ev_view_primary_get_cb (GtkClipboard *clipboard,
290 GtkSelectionData *selection_data,
293 static void ev_view_primary_clear_cb (GtkClipboard *clipboard,
295 static void ev_view_update_primary_selection (EvView *ev_view);
297 G_DEFINE_TYPE_WITH_CODE (EvView, ev_view, GTK_TYPE_CONTAINER,
298 G_IMPLEMENT_INTERFACE (GTK_TYPE_SCROLLABLE, NULL))
300 /* HeightToPage cache */
301 #define EV_HEIGHT_TO_PAGE_CACHE_KEY "ev-height-to-page-cache"
304 build_height_to_page (EvHeightToPageCache *cache,
305 EvDocument *document,
308 gboolean swap, uniform, dual_even_left;
310 double uniform_height, page_height, next_page_height;
312 gdouble u_width, u_height;
315 swap = (rotation == 90 || rotation == 270);
317 uniform = ev_document_is_page_size_uniform (document);
318 n_pages = ev_document_get_n_pages (document);
319 dual_even_left = (n_pages > 2);
321 g_free (cache->height_to_page);
322 g_free (cache->dual_height_to_page);
324 cache->rotation = rotation;
325 cache->height_to_page = g_new0 (gdouble, n_pages + 1);
326 cache->dual_height_to_page = g_new0 (gdouble, n_pages + 2);
329 ev_document_get_page_size (document, 0, &u_width, &u_height);
332 for (i = 0; i <= n_pages; i++) {
334 uniform_height = swap ? u_width : u_height;
335 cache->height_to_page[i] = i * uniform_height;
340 ev_document_get_page_size (document, i, &w, &h);
341 page_height = swap ? w : h;
345 cache->height_to_page[i] = saved_height;
346 saved_height += page_height;
350 if (dual_even_left && !uniform) {
353 ev_document_get_page_size (document, 0, &w, &h);
354 saved_height = swap ? w : h;
359 for (i = dual_even_left; i < n_pages + 2; i += 2) {
361 uniform_height = swap ? u_width : u_height;
362 cache->dual_height_to_page[i] = ((i + dual_even_left) / 2) * uniform_height;
363 if (i + 1 < n_pages + 2)
364 cache->dual_height_to_page[i + 1] = ((i + dual_even_left) / 2) * uniform_height;
366 if (i + 1 < n_pages) {
369 ev_document_get_page_size (document, i + 1, &w, &h);
370 next_page_height = swap ? w : h;
372 next_page_height = 0;
378 ev_document_get_page_size (document, i, &w, &h);
379 page_height = swap ? w : h;
384 if (i + 1 < n_pages + 2) {
385 cache->dual_height_to_page[i] = saved_height;
386 cache->dual_height_to_page[i + 1] = saved_height;
387 saved_height += MAX(page_height, next_page_height);
389 cache->dual_height_to_page[i] = saved_height;
396 ev_height_to_page_cache_get_height (EvHeightToPageCache *cache,
397 EvDocument *document,
401 gdouble *dual_height)
403 if (cache->rotation != rotation)
404 build_height_to_page (cache, document, rotation);
406 *height = cache->height_to_page[page];
407 *dual_height = cache->dual_height_to_page[page];
411 ev_height_to_page_cache_free (EvHeightToPageCache *cache)
413 if (cache->height_to_page) {
414 g_free (cache->height_to_page);
415 cache->height_to_page = NULL;
418 if (cache->dual_height_to_page) {
419 g_free (cache->dual_height_to_page);
420 cache->dual_height_to_page = NULL;
425 static EvHeightToPageCache *
426 ev_view_get_height_to_page_cache (EvView *view)
428 EvHeightToPageCache *cache;
433 cache = g_object_get_data (G_OBJECT (view->document), EV_HEIGHT_TO_PAGE_CACHE_KEY);
435 cache = g_new0 (EvHeightToPageCache, 1);
436 build_height_to_page (cache, view->document, view->rotation);
437 g_object_set_data_full (G_OBJECT (view->document),
438 EV_HEIGHT_TO_PAGE_CACHE_KEY,
440 (GDestroyNotify)ev_height_to_page_cache_free);
447 ev_view_get_height_to_page (EvView *view,
454 if (!view->height_to_page_cache)
457 ev_height_to_page_cache_get_height (view->height_to_page_cache,
463 *height = (gint)(h * view->scale + 0.5);
466 *dual_height = (gint)(dh * view->scale + 0.5);
470 ev_view_get_scrollbar_size (EvView *view,
471 GtkOrientation orientation)
473 GtkWidget *widget = GTK_WIDGET (view);
475 GtkWidget *swindow = gtk_widget_get_parent (GTK_WIDGET (view));
476 GtkAllocation allocation;
480 if (!GTK_IS_SCROLLED_WINDOW (swindow))
483 gtk_widget_get_allocation (widget, &allocation);
485 if (orientation == GTK_ORIENTATION_VERTICAL) {
486 if (allocation.height >= view->requisition.height)
487 sb = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (swindow));
491 if (allocation.width >= view->requisition.width)
492 sb = gtk_scrolled_window_get_hscrollbar (GTK_SCROLLED_WINDOW (swindow));
497 gtk_widget_style_get (swindow, "scrollbar_spacing", &spacing, NULL);
498 gtk_widget_get_preferred_size (sb, &req, NULL);
500 return (orientation == GTK_ORIENTATION_VERTICAL ? req.width : req.height) + spacing;
504 scroll_to_point (EvView *view,
507 GtkOrientation orientation)
510 gdouble upper, lower;
512 if (orientation == GTK_ORIENTATION_VERTICAL) {
513 page_size = gtk_adjustment_get_page_size (view->vadjustment);
514 upper = gtk_adjustment_get_upper (view->vadjustment);
515 lower = gtk_adjustment_get_lower (view->vadjustment);
517 if (view->continuous) {
518 gtk_adjustment_clamp_page (view->vadjustment,
519 y - view->spacing / 2,
522 gtk_adjustment_set_value (view->vadjustment,
523 CLAMP (y, lower, upper - page_size));
526 page_size = gtk_adjustment_get_page_size (view->hadjustment);
527 upper = gtk_adjustment_get_upper (view->hadjustment);
528 lower = gtk_adjustment_get_lower (view->hadjustment);
530 if (view->dual_page) {
531 gtk_adjustment_clamp_page (view->hadjustment, x,
534 gtk_adjustment_set_value (view->hadjustment,
535 CLAMP (x, lower, upper - page_size));
541 ev_view_scroll_to_page_position (EvView *view, GtkOrientation orientation)
548 if ((orientation == GTK_ORIENTATION_VERTICAL && view->pending_point.y == 0) ||
549 (orientation == GTK_ORIENTATION_HORIZONTAL && view->pending_point.x == 0)) {
550 GdkRectangle page_area;
553 ev_view_get_page_extents (view, view->current_page, &page_area, &border);
559 doc_point_to_view_point (view, view->current_page,
560 &view->pending_point, &view_point);
565 scroll_to_point (view, x, y, orientation);
569 ev_view_set_adjustment_values (EvView *view,
570 GtkOrientation orientation)
572 GtkWidget *widget = GTK_WIDGET (view);
573 GtkAdjustment *adjustment;
574 GtkAllocation allocation;
583 gtk_widget_get_allocation (widget, &allocation);
585 if (orientation == GTK_ORIENTATION_HORIZONTAL) {
586 req_size = view->requisition.width;
587 alloc_size = allocation.width;
588 adjustment = view->hadjustment;
590 req_size = view->requisition.height;
591 alloc_size = allocation.height;
592 adjustment = view->vadjustment;
599 value = gtk_adjustment_get_value (adjustment);
600 upper = gtk_adjustment_get_upper (adjustment);
601 page_size = gtk_adjustment_get_page_size (adjustment);
603 switch (view->pending_scroll) {
604 case SCROLL_TO_KEEP_POSITION:
605 case SCROLL_TO_FIND_LOCATION:
606 factor = value / upper;
608 case SCROLL_TO_PAGE_POSITION:
610 case SCROLL_TO_CENTER:
611 factor = (value + page_size * 0.5) / upper;
615 upper = MAX (alloc_size, req_size);
616 page_size = alloc_size;
618 gtk_adjustment_set_page_size (adjustment, page_size);
619 gtk_adjustment_set_step_increment (adjustment, alloc_size * 0.1);
620 gtk_adjustment_set_page_increment (adjustment, alloc_size * 0.9);
621 gtk_adjustment_set_lower (adjustment, 0);
622 gtk_adjustment_set_upper (adjustment, upper);
625 * We add 0.5 to the values before to average out our rounding errors.
627 switch (view->pending_scroll) {
628 case SCROLL_TO_KEEP_POSITION:
629 case SCROLL_TO_FIND_LOCATION:
630 new_value = CLAMP (upper * factor + 0.5, 0, upper - page_size);
631 gtk_adjustment_set_value (adjustment, (int)new_value);
633 case SCROLL_TO_PAGE_POSITION:
634 ev_view_scroll_to_page_position (view, orientation);
636 case SCROLL_TO_CENTER:
637 new_value = CLAMP (upper * factor - page_size * 0.5 + 0.5,
638 0, upper - page_size);
639 gtk_adjustment_set_value (adjustment, (int)new_value);
643 gtk_adjustment_changed (adjustment);
647 view_update_range_and_current_page (EvView *view)
649 gint start = view->start_page;
650 gint end = view->end_page;
652 if (ev_document_get_n_pages (view->document) <= 0 ||
653 !ev_document_check_dimensions (view->document))
656 if (view->continuous) {
657 GdkRectangle current_area, unused, page_area;
659 gboolean found = FALSE;
660 gint area_max = -1, area;
661 gint best_current_page = -1;
664 if (!(view->vadjustment && view->hadjustment))
667 current_area.x = gtk_adjustment_get_value (view->hadjustment);
668 current_area.width = gtk_adjustment_get_page_size (view->hadjustment);
669 current_area.y = gtk_adjustment_get_value (view->vadjustment);
670 current_area.height = gtk_adjustment_get_page_size (view->vadjustment);
672 for (i = 0; i < ev_document_get_n_pages (view->document); i++) {
674 ev_view_get_page_extents (view, i, &page_area, &border);
676 if (gdk_rectangle_intersect (¤t_area, &page_area, &unused)) {
677 area = unused.width * unused.height;
681 view->start_page = i;
683 best_current_page = i;
685 if (area > area_max) {
686 best_current_page = (area == area_max) ? MIN (i, best_current_page) : i;
692 } else if (found && view->current_page <= view->end_page) {
693 if (view->dual_page && j < 1) {
694 /* In dual mode we stop searching
695 * after two consecutive non-visible pages.
704 if (view->pending_scroll == SCROLL_TO_KEEP_POSITION) {
705 best_current_page = MAX (best_current_page, view->start_page);
707 if (view->current_page != best_current_page) {
708 view->current_page = best_current_page;
709 hide_loading_window (view);
710 ev_document_model_set_page (view->model, best_current_page);
713 } else if (view->dual_page) {
714 if (view->current_page % 2 == get_dual_even_left (view)) {
715 view->start_page = view->current_page;
716 if (view->current_page + 1 < ev_document_get_n_pages (view->document))
717 view->end_page = view->start_page + 1;
719 view->end_page = view->start_page;
721 if (view->current_page < 1)
722 view->start_page = view->current_page;
724 view->start_page = view->current_page - 1;
725 view->end_page = view->current_page;
728 view->start_page = view->current_page;
729 view->end_page = view->current_page;
732 if (view->start_page == -1 || view->end_page == -1)
735 if (start != view->start_page || end != view->end_page) {
738 for (i = start; i < view->start_page && start != -1; i++) {
739 hide_annotation_windows (view, i);
742 for (i = end; i > view->end_page && end != -1; i--) {
743 hide_annotation_windows (view, i);
747 ev_page_cache_set_page_range (view->page_cache,
750 ev_pixbuf_cache_set_page_range (view->pixbuf_cache,
753 view->selection_info.selections);
755 if (ev_pixbuf_cache_get_surface (view->pixbuf_cache, view->current_page))
756 gtk_widget_queue_draw (GTK_WIDGET (view));
760 ev_view_set_scroll_adjustment (EvView *view,
761 GtkOrientation orientation,
762 GtkAdjustment *adjustment)
764 GtkAdjustment **to_set;
765 const gchar *prop_name;
767 if (orientation == GTK_ORIENTATION_HORIZONTAL) {
768 to_set = &view->hadjustment;
769 prop_name = "hadjustment";
771 to_set = &view->vadjustment;
772 prop_name = "vadjustment";
775 if (adjustment && adjustment == *to_set)
779 g_signal_handlers_disconnect_by_func (*to_set,
780 (gpointer) on_adjustment_value_changed,
782 g_object_unref (*to_set);
786 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
787 g_signal_connect (adjustment, "value_changed",
788 G_CALLBACK (on_adjustment_value_changed),
790 *to_set = g_object_ref_sink (adjustment);
791 ev_view_set_adjustment_values (view, orientation);
793 g_object_notify (G_OBJECT (view), prop_name);
797 add_scroll_binding_keypad (GtkBindingSet *binding_set,
799 GdkModifierType modifiers,
800 GtkScrollType scroll,
803 guint keypad_keyval = keyval - GDK_KEY_Left + GDK_KEY_KP_Left;
805 gtk_binding_entry_add_signal (binding_set, keyval, modifiers,
806 "binding_activated", 2,
807 GTK_TYPE_SCROLL_TYPE, scroll,
808 G_TYPE_BOOLEAN, horizontal);
809 gtk_binding_entry_add_signal (binding_set, keypad_keyval, modifiers,
810 "binding_activated", 2,
811 GTK_TYPE_SCROLL_TYPE, scroll,
812 G_TYPE_BOOLEAN, horizontal);
816 compute_scroll_increment (EvView *view,
817 GtkScrollType scroll)
819 GtkWidget *widget = GTK_WIDGET (view);
820 GtkAdjustment *adjustment = view->vadjustment;
821 cairo_region_t *text_region, *region;
822 GtkAllocation allocation;
825 EvRectangle doc_rect;
826 GdkRectangle page_area;
828 gdouble fraction = 1.0;
830 if (scroll != GTK_SCROLL_PAGE_BACKWARD && scroll != GTK_SCROLL_PAGE_FORWARD)
831 return gtk_adjustment_get_page_size (adjustment);
833 page = scroll == GTK_SCROLL_PAGE_BACKWARD ? view->start_page : view->end_page;
835 text_region = ev_page_cache_get_text_mapping (view->page_cache, page);
836 if (!text_region || cairo_region_is_empty (text_region))
837 return gtk_adjustment_get_page_size (adjustment);
839 gtk_widget_get_allocation (widget, &allocation);
840 ev_view_get_page_extents (view, page, &page_area, &border);
841 rect.x = page_area.x + view->scroll_x;
842 rect.y = view->scroll_y + (scroll == GTK_SCROLL_PAGE_BACKWARD ? 5 : allocation.height - 5);
843 rect.width = page_area.width;
845 view_rect_to_doc_rect (view, &rect, &page_area, &doc_rect);
847 /* Convert the doc rectangle into a GdkRectangle */
848 rect.x = doc_rect.x1;
849 rect.y = doc_rect.y1;
850 rect.width = doc_rect.x2 - doc_rect.x1;
851 rect.height = MAX (1, doc_rect.y2 - doc_rect.y1);
852 region = cairo_region_create_rectangle (&rect);
854 cairo_region_intersect (region, text_region);
855 if (cairo_region_num_rectangles (region)) {
858 cairo_region_t *sel_region;
860 cairo_region_get_rectangle (region, 0, &rect);
861 ev_page = ev_document_get_page (view->document, page);
862 rc = ev_render_context_new (ev_page, view->rotation, view->scale);
863 g_object_unref (ev_page);
864 /* Get the selection region to know the height of the line */
865 doc_rect.x1 = doc_rect.x2 = rect.x + 0.5;
866 doc_rect.y1 = doc_rect.y2 = rect.y + 0.5;
868 ev_document_doc_mutex_lock ();
869 sel_region = ev_selection_get_selection_region (EV_SELECTION (view->document),
870 rc, EV_SELECTION_STYLE_LINE,
872 ev_document_doc_mutex_unlock ();
876 if (cairo_region_num_rectangles (sel_region) > 0) {
877 cairo_region_get_rectangle (sel_region, 0, &rect);
878 fraction = 1 - (rect.height / gtk_adjustment_get_page_size (adjustment));
880 cairo_region_destroy (sel_region);
882 cairo_region_destroy (region);
884 return gtk_adjustment_get_page_size (adjustment) * fraction;
889 ev_view_scroll (EvView *view,
890 GtkScrollType scroll,
893 GtkAdjustment *adjustment;
894 double value, increment;
895 gdouble upper, lower;
897 gdouble step_increment;
898 gboolean first_page = FALSE;
899 gboolean last_page = FALSE;
901 view->jump_to_find_result = FALSE;
903 if (view->sizing_mode == EV_SIZING_BEST_FIT) {
905 case GTK_SCROLL_PAGE_BACKWARD:
906 case GTK_SCROLL_STEP_BACKWARD:
907 ev_view_previous_page (view);
909 case GTK_SCROLL_PAGE_FORWARD:
910 case GTK_SCROLL_STEP_FORWARD:
911 ev_view_next_page (view);
919 /* Assign values for increment and vertical adjustment */
920 adjustment = horizontal ? view->hadjustment : view->vadjustment;
921 value = gtk_adjustment_get_value (adjustment);
922 upper = gtk_adjustment_get_upper (adjustment);
923 lower = gtk_adjustment_get_lower (adjustment);
924 page_size = gtk_adjustment_get_page_size (adjustment);
925 step_increment = gtk_adjustment_get_step_increment (adjustment);
927 /* Assign boolean for first and last page */
928 if (view->current_page == 0)
930 if (view->current_page == ev_document_get_n_pages (view->document) - 1)
934 case GTK_SCROLL_PAGE_BACKWARD:
935 /* Do not jump backwards if at the first page */
936 if (value == lower && first_page) {
938 /* At the top of a page, assign the upper bound limit of previous page */
939 } else if (value == lower) {
940 value = upper - page_size;
941 ev_view_previous_page (view);
942 /* Jump to the top */
944 increment = compute_scroll_increment (view, GTK_SCROLL_PAGE_BACKWARD);
945 value = MAX (value - increment, lower);
948 case GTK_SCROLL_PAGE_FORWARD:
949 /* Do not jump forward if at the last page */
950 if (value == (upper - page_size) && last_page) {
952 /* At the bottom of a page, assign the lower bound limit of next page */
953 } else if (value == (upper - page_size)) {
955 ev_view_next_page (view);
956 /* Jump to the bottom */
958 increment = compute_scroll_increment (view, GTK_SCROLL_PAGE_FORWARD);
959 value = MIN (value + increment, upper - page_size);
962 case GTK_SCROLL_STEP_BACKWARD:
963 value -= step_increment;
965 case GTK_SCROLL_STEP_FORWARD:
966 value += step_increment;
968 case GTK_SCROLL_STEP_DOWN:
969 value -= step_increment / 10;
971 case GTK_SCROLL_STEP_UP:
972 value += step_increment / 10;
978 value = CLAMP (value, lower, upper - page_size);
980 gtk_adjustment_set_value (adjustment, value);
986 ensure_rectangle_is_visible (EvView *view, GdkRectangle *rect)
988 GtkWidget *widget = GTK_WIDGET (view);
989 GtkAdjustment *adjustment;
990 GtkAllocation allocation;
994 view->pending_scroll = SCROLL_TO_FIND_LOCATION;
996 gtk_widget_get_allocation (widget, &allocation);
998 adjustment = view->vadjustment;
999 adj_value = gtk_adjustment_get_value (adjustment);
1001 if (rect->y < adj_value) {
1002 value = MAX (gtk_adjustment_get_lower (adjustment), rect->y - MARGIN);
1003 gtk_adjustment_set_value (view->vadjustment, value);
1004 } else if (rect->y + rect->height > adj_value + allocation.height) {
1005 value = MIN (gtk_adjustment_get_upper (adjustment), rect->y + rect->height -
1006 allocation.height + MARGIN);
1007 gtk_adjustment_set_value (view->vadjustment, value);
1010 adjustment = view->hadjustment;
1011 adj_value = gtk_adjustment_get_value (adjustment);
1013 if (rect->x < adj_value) {
1014 value = MAX (gtk_adjustment_get_lower (adjustment), rect->x - MARGIN);
1015 gtk_adjustment_set_value (view->hadjustment, value);
1016 } else if (rect->x + rect->height > adj_value + allocation.width) {
1017 value = MIN (gtk_adjustment_get_upper (adjustment), rect->x + rect->width -
1018 allocation.width + MARGIN);
1019 gtk_adjustment_set_value (view->hadjustment, value);
1022 gtk_widget_queue_resize (GTK_WIDGET (view));
1025 /*** Geometry computations ***/
1028 compute_border (EvView *view, int width, int height, GtkBorder *border)
1030 ev_document_misc_get_page_border_size (width, height, border);
1034 _get_page_size_for_scale_and_rotation (EvDocument *document,
1044 ev_document_get_page_size (document, page, &w, &h);
1046 width = (gint)(w * scale + 0.5);
1047 height = (gint)(h * scale + 0.5);
1050 *page_width = (rotation == 0 || rotation == 180) ? width : height;
1052 *page_height = (rotation == 0 || rotation == 180) ? height : width;
1056 ev_view_get_page_size (EvView *view,
1061 _get_page_size_for_scale_and_rotation (view->document,
1070 ev_view_get_max_page_size (EvView *view,
1077 ev_document_get_max_page_size (view->document, &w, &h);
1079 width = (gint)(w * view->scale + 0.5);
1080 height = (gint)(h * view->scale + 0.5);
1083 *max_width = (view->rotation == 0 || view->rotation == 180) ? width : height;
1085 *max_height = (view->rotation == 0 || view->rotation == 180) ? height : width;
1089 get_page_y_offset (EvView *view, int page, int *y_offset)
1091 int max_width, offset;
1094 g_return_if_fail (y_offset != NULL);
1096 ev_view_get_max_page_size (view, &max_width, NULL);
1097 compute_border (view, max_width, max_width, &border);
1099 if (view->dual_page) {
1100 ev_view_get_height_to_page (view, page, NULL, &offset);
1101 offset += ((page + get_dual_even_left (view)) / 2 + 1) * view->spacing +
1102 ((page + get_dual_even_left (view)) / 2 ) * (border.top + border.bottom);
1104 ev_view_get_height_to_page (view, page, &offset, NULL);
1105 offset += (page + 1) * view->spacing + page * (border.top + border.bottom);
1113 ev_view_get_page_extents (EvView *view,
1115 GdkRectangle *page_area,
1120 GtkAllocation allocation;
1122 widget = GTK_WIDGET (view);
1123 gtk_widget_get_allocation (widget, &allocation);
1125 /* Get the size of the page */
1126 ev_view_get_page_size (view, page, &width, &height);
1127 compute_border (view, width, height, border);
1128 page_area->width = width + border->left + border->right;
1129 page_area->height = height + border->top + border->bottom;
1131 if (view->continuous) {
1135 ev_view_get_max_page_size (view, &max_width, NULL);
1136 max_width = max_width + border->left + border->right;
1137 /* Get the location of the bounding box */
1138 if (view->dual_page) {
1139 x = view->spacing + ((page % 2 == get_dual_even_left (view)) ? 0 : 1) * (max_width + view->spacing);
1140 x = x + MAX (0, allocation.width - (max_width * 2 + view->spacing * 3)) / 2;
1141 if (page % 2 == get_dual_even_left (view))
1142 x = x + (max_width - width - border->left - border->right);
1145 x = x + MAX (0, allocation.width - (width + view->spacing * 2)) / 2;
1148 get_page_y_offset (view, page, &y);
1154 if (view->dual_page) {
1155 gint width_2, height_2;
1156 gint max_width = width;
1157 gint max_height = height;
1158 GtkBorder overall_border;
1161 other_page = (page % 2 == get_dual_even_left (view)) ? page + 1: page - 1;
1163 /* First, we get the bounding box of the two pages */
1164 if (other_page < ev_document_get_n_pages (view->document)
1165 && (0 <= other_page)) {
1166 ev_view_get_page_size (view, other_page,
1167 &width_2, &height_2);
1168 if (width_2 > width)
1169 max_width = width_2;
1170 if (height_2 > height)
1171 max_height = height_2;
1173 compute_border (view, max_width, max_height, &overall_border);
1175 /* Find the offsets */
1179 /* Adjust for being the left or right page */
1180 if (page % 2 == get_dual_even_left (view))
1181 x = x + max_width - width;
1183 x = x + (max_width + overall_border.left + overall_border.right) + view->spacing;
1185 y = y + (max_height - height)/2;
1187 /* Adjust for extra allocation */
1188 x = x + MAX (0, allocation.width -
1189 ((max_width + overall_border.left + overall_border.right) * 2 + view->spacing * 3))/2;
1190 y = y + MAX (0, allocation.height - (height + view->spacing * 2))/2;
1195 /* Adjust for extra allocation */
1196 x = x + MAX (0, allocation.width - (width + border->left + border->right + view->spacing * 2))/2;
1197 y = y + MAX (0, allocation.height - (height + border->top + border->bottom + view->spacing * 2))/2;
1208 get_doc_page_size (EvView *view,
1215 ev_document_get_page_size (view->document, page, &w, &h);
1216 if (view->rotation == 0 || view->rotation == 180) {
1217 if (width) *width = w;
1218 if (height) *height = h;
1220 if (width) *width = h;
1221 if (height) *height = w;
1226 view_point_to_doc_point (EvView *view,
1227 GdkPoint *view_point,
1228 GdkRectangle *page_area,
1229 double *doc_point_x,
1230 double *doc_point_y)
1232 *doc_point_x = (double) (view_point->x - page_area->x) / view->scale;
1233 *doc_point_y = (double) (view_point->y - page_area->y) / view->scale;
1237 view_rect_to_doc_rect (EvView *view,
1238 GdkRectangle *view_rect,
1239 GdkRectangle *page_area,
1240 EvRectangle *doc_rect)
1242 doc_rect->x1 = (double) (view_rect->x - page_area->x) / view->scale;
1243 doc_rect->y1 = (double) (view_rect->y - page_area->y) / view->scale;
1244 doc_rect->x2 = doc_rect->x1 + (double) view_rect->width / view->scale;
1245 doc_rect->y2 = doc_rect->y1 + (double) view_rect->height / view->scale;
1249 doc_point_to_view_point (EvView *view,
1252 GdkPoint *view_point)
1254 GdkRectangle page_area;
1256 double x, y, view_x, view_y;
1257 gdouble width, height;
1259 get_doc_page_size (view, page, &width, &height);
1261 if (view->rotation == 0) {
1264 } else if (view->rotation == 90) {
1265 x = width - doc_point->y;
1267 } else if (view->rotation == 180) {
1268 x = width - doc_point->x;
1269 y = height - doc_point->y;
1270 } else if (view->rotation == 270) {
1272 y = height - doc_point->x;
1274 g_assert_not_reached ();
1277 ev_view_get_page_extents (view, page, &page_area, &border);
1279 view_x = CLAMP (x * view->scale, 0, page_area.width);
1280 view_y = CLAMP (y * view->scale, 0, page_area.height);
1281 view_point->x = view_x + page_area.x;
1282 view_point->y = view_y + page_area.y;
1286 doc_rect_to_view_rect (EvView *view,
1288 EvRectangle *doc_rect,
1289 GdkRectangle *view_rect)
1291 GdkRectangle page_area;
1294 gdouble width, height;
1296 get_doc_page_size (view, page, &width, &height);
1298 if (view->rotation == 0) {
1301 w = doc_rect->x2 - doc_rect->x1;
1302 h = doc_rect->y2 - doc_rect->y1;
1303 } else if (view->rotation == 90) {
1304 x = width - doc_rect->y2;
1306 w = doc_rect->y2 - doc_rect->y1;
1307 h = doc_rect->x2 - doc_rect->x1;
1308 } else if (view->rotation == 180) {
1309 x = width - doc_rect->x2;
1310 y = height - doc_rect->y2;
1311 w = doc_rect->x2 - doc_rect->x1;
1312 h = doc_rect->y2 - doc_rect->y1;
1313 } else if (view->rotation == 270) {
1315 y = height - doc_rect->x2;
1316 w = doc_rect->y2 - doc_rect->y1;
1317 h = doc_rect->x2 - doc_rect->x1;
1319 g_assert_not_reached ();
1322 ev_view_get_page_extents (view, page, &page_area, &border);
1324 view_rect->x = x * view->scale + page_area.x;
1325 view_rect->y = y * view->scale + page_area.y;
1326 view_rect->width = w * view->scale;
1327 view_rect->height = h * view->scale;
1331 get_dual_even_left (EvView *view)
1333 return (ev_document_get_n_pages (view->document) > 2);
1337 find_page_at_location (EvView *view,
1346 if (view->document == NULL)
1350 g_assert (x_offset);
1351 g_assert (y_offset);
1353 for (i = view->start_page; i >= 0 && i <= view->end_page; i++) {
1354 GdkRectangle page_area;
1357 if (! ev_view_get_page_extents (view, i, &page_area, &border))
1360 if ((x >= page_area.x + border.left) &&
1361 (x < page_area.x + page_area.width - border.right) &&
1362 (y >= page_area.y + border.top) &&
1363 (y < page_area.y + page_area.height - border.bottom)) {
1365 *x_offset = x - (page_area.x + border.left);
1366 *y_offset = y - (page_area.y + border.top);
1375 location_in_text (EvView *view,
1379 cairo_region_t *region;
1381 gint x_offset = 0, y_offset = 0;
1383 find_page_at_location (view, x, y, &page, &x_offset, &y_offset);
1388 region = ev_page_cache_get_text_mapping (view->page_cache, page);
1391 return cairo_region_contains_point (region, x_offset / view->scale, y_offset / view->scale);
1397 location_in_selected_text (EvView *view,
1402 gint x_offset = 0, y_offset = 0;
1403 EvViewSelection *selection;
1406 for (l = view->selection_info.selections; l != NULL; l = l->next) {
1407 selection = (EvViewSelection *)l->data;
1409 find_page_at_location (view, x, y, &page, &x_offset, &y_offset);
1411 if (page != selection->page)
1414 if (selection->covered_region &&
1415 cairo_region_contains_point (selection->covered_region, x_offset, y_offset))
1423 get_doc_point_from_offset (EvView *view,
1430 gdouble width, height;
1433 get_doc_page_size (view, page, &width, &height);
1435 x_offset = x_offset / view->scale;
1436 y_offset = y_offset / view->scale;
1438 if (view->rotation == 0) {
1441 } else if (view->rotation == 90) {
1443 y = width - x_offset;
1444 } else if (view->rotation == 180) {
1445 x = width - x_offset;
1446 y = height - y_offset;
1447 } else if (view->rotation == 270) {
1448 x = height - y_offset;
1451 g_assert_not_reached ();
1461 get_doc_point_from_location (EvView *view,
1468 gint x_offset = 0, y_offset = 0;
1470 x += view->scroll_x;
1471 y += view->scroll_y;
1472 find_page_at_location (view, x, y, page, &x_offset, &y_offset);
1476 return get_doc_point_from_offset (view, *page, x_offset, y_offset, x_new, y_new);
1480 ev_view_get_area_from_mapping (EvView *view,
1482 EvMappingList *mapping_list,
1488 mapping = ev_mapping_list_find (mapping_list, data);
1489 doc_rect_to_view_rect (view, page, &mapping->area, area);
1490 area->x -= view->scroll_x;
1491 area->y -= view->scroll_y;
1495 ev_view_put (EvView *view,
1496 GtkWidget *child_widget,
1500 EvRectangle *doc_rect)
1504 child = g_slice_new (EvViewChild);
1506 child->widget = child_widget;
1510 child->doc_rect = *doc_rect;
1512 gtk_widget_set_parent (child_widget, GTK_WIDGET (view));
1513 view->children = g_list_append (view->children, child);
1517 ev_view_put_to_doc_rect (EvView *view,
1518 GtkWidget *child_widget,
1520 EvRectangle *doc_rect)
1524 doc_rect_to_view_rect (view, page, doc_rect, &area);
1525 area.x -= view->scroll_x;
1526 area.y -= view->scroll_y;
1527 ev_view_put (view, child_widget, area.x, area.y, page, doc_rect);
1532 ev_view_get_link_at_location (EvView *view,
1537 gint x_new = 0, y_new = 0;
1538 EvMappingList *link_mapping;
1540 if (!EV_IS_DOCUMENT_LINKS (view->document))
1543 if (!get_doc_point_from_location (view, x, y, &page, &x_new, &y_new))
1546 link_mapping = ev_page_cache_get_link_mapping (view->page_cache, page);
1549 return ev_mapping_list_get_data (link_mapping, x_new, y_new);
1555 goto_fitr_dest (EvView *view, EvLinkDest *dest)
1558 gdouble zoom, left, top;
1559 gboolean change_left, change_top;
1560 GtkAllocation allocation;
1562 gtk_widget_get_allocation (GTK_WIDGET (view), &allocation);
1564 left = ev_link_dest_get_left (dest, &change_left);
1565 top = ev_link_dest_get_top (dest, &change_top);
1567 zoom = zoom_for_size_best_fit (ev_link_dest_get_right (dest) - left,
1568 ev_link_dest_get_bottom (dest) - top,
1572 ev_document_model_set_sizing_mode (view->model, EV_SIZING_FREE);
1573 ev_document_model_set_scale (view->model, zoom);
1575 doc_point.x = change_left ? left : 0;
1576 doc_point.y = change_top ? top : 0;
1577 view->pending_point = doc_point;
1579 ev_view_change_page (view, ev_link_dest_get_page (dest));
1583 goto_fitv_dest (EvView *view, EvLinkDest *dest)
1586 gdouble doc_width, doc_height;
1589 gboolean change_left;
1590 GtkAllocation allocation;
1592 gtk_widget_get_allocation (GTK_WIDGET (view), &allocation);
1594 page = ev_link_dest_get_page (dest);
1595 ev_document_get_page_size (view->document, page, &doc_width, &doc_height);
1597 left = ev_link_dest_get_left (dest, &change_left);
1598 doc_point.x = change_left ? left : 0;
1601 zoom = zoom_for_size_fit_height (doc_width - doc_point.x , doc_height,
1605 ev_document_model_set_sizing_mode (view->model, EV_SIZING_FREE);
1606 ev_document_model_set_scale (view->model, zoom);
1608 view->pending_point = doc_point;
1610 ev_view_change_page (view, page);
1614 goto_fith_dest (EvView *view, EvLinkDest *dest)
1617 gdouble doc_width, doc_height;
1620 gboolean change_top;
1621 GtkAllocation allocation;
1623 gtk_widget_get_allocation (GTK_WIDGET (view), &allocation);
1625 page = ev_link_dest_get_page (dest);
1626 ev_document_get_page_size (view->document, page, &doc_width, &doc_height);
1628 top = ev_link_dest_get_top (dest, &change_top);
1631 doc_point.y = change_top ? top : 0;
1633 zoom = zoom_for_size_fit_width (doc_width, top,
1637 ev_document_model_set_sizing_mode (view->model, EV_SIZING_FIT_WIDTH);
1638 ev_document_model_set_scale (view->model, zoom);
1640 view->pending_point = doc_point;
1642 ev_view_change_page (view, page);
1646 goto_fit_dest (EvView *view, EvLinkDest *dest)
1649 gdouble doc_width, doc_height;
1651 GtkAllocation allocation;
1653 gtk_widget_get_allocation (GTK_WIDGET (view), &allocation);
1655 page = ev_link_dest_get_page (dest);
1656 ev_document_get_page_size (view->document, page, &doc_width, &doc_height);
1658 zoom = zoom_for_size_best_fit (doc_width, doc_height,
1662 ev_document_model_set_sizing_mode (view->model, EV_SIZING_BEST_FIT);
1663 ev_document_model_set_scale (view->model, zoom);
1665 ev_view_change_page (view, page);
1669 goto_xyz_dest (EvView *view, EvLinkDest *dest)
1673 gdouble zoom, left, top;
1674 gboolean change_zoom, change_left, change_top;
1676 zoom = ev_link_dest_get_zoom (dest, &change_zoom);
1677 page = ev_link_dest_get_page (dest);
1679 if (change_zoom && zoom > 1) {
1680 ev_document_model_set_sizing_mode (view->model, EV_SIZING_FREE);
1681 ev_document_model_set_scale (view->model, zoom);
1684 left = ev_link_dest_get_left (dest, &change_left);
1685 top = ev_link_dest_get_top (dest, &change_top);
1687 doc_point.x = change_left ? left : 0;
1688 doc_point.y = change_top ? top : 0;
1689 view->pending_point = doc_point;
1691 ev_view_change_page (view, page);
1695 goto_dest (EvView *view, EvLinkDest *dest)
1697 EvLinkDestType type;
1698 int page, n_pages, current_page;
1700 page = ev_link_dest_get_page (dest);
1701 n_pages = ev_document_get_n_pages (view->document);
1703 if (page < 0 || page >= n_pages)
1706 current_page = view->current_page;
1708 type = ev_link_dest_get_dest_type (dest);
1711 case EV_LINK_DEST_TYPE_PAGE:
1712 ev_document_model_set_page (view->model, page);
1714 case EV_LINK_DEST_TYPE_FIT:
1715 goto_fit_dest (view, dest);
1717 case EV_LINK_DEST_TYPE_FITH:
1718 goto_fith_dest (view, dest);
1720 case EV_LINK_DEST_TYPE_FITV:
1721 goto_fitv_dest (view, dest);
1723 case EV_LINK_DEST_TYPE_FITR:
1724 goto_fitr_dest (view, dest);
1726 case EV_LINK_DEST_TYPE_XYZ:
1727 goto_xyz_dest (view, dest);
1729 case EV_LINK_DEST_TYPE_PAGE_LABEL:
1730 ev_document_model_set_page_by_label (view->model, ev_link_dest_get_page_label (dest));
1733 g_assert_not_reached ();
1736 if (current_page != view->current_page)
1737 ev_document_model_set_page (view->model, view->current_page);
1741 ev_view_goto_dest (EvView *view, EvLinkDest *dest)
1743 EvLinkDestType type;
1745 type = ev_link_dest_get_dest_type (dest);
1747 if (type == EV_LINK_DEST_TYPE_NAMED) {
1749 const gchar *named_dest;
1751 named_dest = ev_link_dest_get_named_dest (dest);
1752 dest2 = ev_document_links_find_link_dest (EV_DOCUMENT_LINKS (view->document),
1755 goto_dest (view, dest2);
1756 g_object_unref (dest2);
1762 goto_dest (view, dest);
1766 ev_view_handle_link (EvView *view, EvLink *link)
1768 EvLinkAction *action = NULL;
1769 EvLinkActionType type;
1771 action = ev_link_get_action (link);
1775 type = ev_link_action_get_action_type (action);
1778 case EV_LINK_ACTION_TYPE_GOTO_DEST: {
1781 g_signal_emit (view, signals[SIGNAL_HANDLE_LINK], 0, link);
1783 dest = ev_link_action_get_dest (action);
1784 ev_view_goto_dest (view, dest);
1787 case EV_LINK_ACTION_TYPE_LAYERS_STATE: {
1788 GList *show, *hide, *toggle;
1790 EvDocumentLayers *document_layers;
1792 document_layers = EV_DOCUMENT_LAYERS (view->document);
1794 show = ev_link_action_get_show_list (action);
1795 for (l = show; l; l = g_list_next (l)) {
1796 ev_document_layers_show_layer (document_layers, EV_LAYER (l->data));
1799 hide = ev_link_action_get_hide_list (action);
1800 for (l = hide; l; l = g_list_next (l)) {
1801 ev_document_layers_hide_layer (document_layers, EV_LAYER (l->data));
1804 toggle = ev_link_action_get_toggle_list (action);
1805 for (l = toggle; l; l = g_list_next (l)) {
1806 EvLayer *layer = EV_LAYER (l->data);
1808 if (ev_document_layers_layer_is_visible (document_layers, layer)) {
1809 ev_document_layers_hide_layer (document_layers, layer);
1811 ev_document_layers_show_layer (document_layers, layer);
1815 g_signal_emit (view, signals[SIGNAL_LAYERS_CHANGED], 0);
1816 ev_view_reload_page (view, view->current_page, NULL);
1819 case EV_LINK_ACTION_TYPE_GOTO_REMOTE:
1820 case EV_LINK_ACTION_TYPE_EXTERNAL_URI:
1821 case EV_LINK_ACTION_TYPE_LAUNCH:
1822 case EV_LINK_ACTION_TYPE_NAMED:
1823 g_signal_emit (view, signals[SIGNAL_EXTERNAL_LINK], 0, action);
1829 tip_from_action_named (EvLinkAction *action)
1831 const gchar *name = ev_link_action_get_name (action);
1833 if (g_ascii_strcasecmp (name, "FirstPage") == 0) {
1834 return g_strdup (_("Go to first page"));
1835 } else if (g_ascii_strcasecmp (name, "PrevPage") == 0) {
1836 return g_strdup (_("Go to previous page"));
1837 } else if (g_ascii_strcasecmp (name, "NextPage") == 0) {
1838 return g_strdup (_("Go to next page"));
1839 } else if (g_ascii_strcasecmp (name, "LastPage") == 0) {
1840 return g_strdup (_("Go to last page"));
1841 } else if (g_ascii_strcasecmp (name, "GoToPage") == 0) {
1842 return g_strdup (_("Go to page"));
1843 } else if (g_ascii_strcasecmp (name, "Find") == 0) {
1844 return g_strdup (_("Find"));
1851 tip_from_link (EvView *view, EvLink *link)
1853 EvLinkAction *action;
1854 EvLinkActionType type;
1859 action = ev_link_get_action (link);
1860 title = ev_link_get_title (link);
1863 return title ? g_strdup (title) : NULL;
1865 type = ev_link_action_get_action_type (action);
1868 case EV_LINK_ACTION_TYPE_GOTO_DEST:
1869 page_label = ev_document_links_get_dest_page_label (EV_DOCUMENT_LINKS (view->document),
1870 ev_link_action_get_dest (action));
1872 msg = g_strdup_printf (_("Go to page %s"), page_label);
1873 g_free (page_label);
1876 case EV_LINK_ACTION_TYPE_GOTO_REMOTE:
1878 msg = g_strdup_printf (_("Go to %s on file “%s”"), title,
1879 ev_link_action_get_filename (action));
1881 msg = g_strdup_printf (_("Go to file “%s”"),
1882 ev_link_action_get_filename (action));
1885 case EV_LINK_ACTION_TYPE_EXTERNAL_URI:
1886 msg = g_strdup (ev_link_action_get_uri (action));
1888 case EV_LINK_ACTION_TYPE_LAUNCH:
1889 msg = g_strdup_printf (_("Launch %s"),
1890 ev_link_action_get_filename (action));
1892 case EV_LINK_ACTION_TYPE_NAMED:
1893 msg = tip_from_action_named (action);
1897 msg = g_strdup (title);
1905 ev_view_handle_cursor_over_xy (EvView *view, gint x, gint y)
1909 EvAnnotation *annot = NULL;
1911 if (view->cursor == EV_VIEW_CURSOR_HIDDEN)
1914 if (view->adding_annot) {
1915 if (view->cursor != EV_VIEW_CURSOR_ADD)
1916 ev_view_set_cursor (view, EV_VIEW_CURSOR_ADD);
1920 if (view->drag_info.in_drag) {
1921 if (view->cursor != EV_VIEW_CURSOR_DRAG)
1922 ev_view_set_cursor (view, EV_VIEW_CURSOR_DRAG);
1926 if (view->scroll_info.autoscrolling) {
1927 if (view->cursor != EV_VIEW_CURSOR_AUTOSCROLL)
1928 ev_view_set_cursor (view, EV_VIEW_CURSOR_AUTOSCROLL);
1932 link = ev_view_get_link_at_location (view, x, y);
1934 ev_view_set_cursor (view, EV_VIEW_CURSOR_LINK);
1935 } else if ((field = ev_view_get_form_field_at_location (view, x, y))) {
1936 if (field->is_read_only) {
1937 if (view->cursor == EV_VIEW_CURSOR_LINK ||
1938 view->cursor == EV_VIEW_CURSOR_IBEAM ||
1939 view->cursor == EV_VIEW_CURSOR_DRAG)
1940 ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL);
1941 } else if (EV_IS_FORM_FIELD_TEXT (field)) {
1942 ev_view_set_cursor (view, EV_VIEW_CURSOR_IBEAM);
1944 ev_view_set_cursor (view, EV_VIEW_CURSOR_LINK);
1946 } else if ((annot = ev_view_get_annotation_at_location (view, x, y))) {
1947 ev_view_set_cursor (view, EV_VIEW_CURSOR_LINK);
1948 } else if (location_in_text (view, x + view->scroll_x, y + view->scroll_y)) {
1949 ev_view_set_cursor (view, EV_VIEW_CURSOR_IBEAM);
1951 if (view->cursor == EV_VIEW_CURSOR_LINK ||
1952 view->cursor == EV_VIEW_CURSOR_IBEAM ||
1953 view->cursor == EV_VIEW_CURSOR_DRAG ||
1954 view->cursor == EV_VIEW_CURSOR_AUTOSCROLL ||
1955 view->cursor == EV_VIEW_CURSOR_ADD)
1956 ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL);
1960 g_object_set (view, "has-tooltip", TRUE, NULL);
1965 ev_view_get_image_at_location (EvView *view,
1970 gint x_new = 0, y_new = 0;
1971 EvMappingList *image_mapping;
1973 if (!EV_IS_DOCUMENT_IMAGES (view->document))
1976 if (!get_doc_point_from_location (view, x, y, &page, &x_new, &y_new))
1979 image_mapping = ev_page_cache_get_image_mapping (view->page_cache, page);
1982 return ev_mapping_list_get_data (image_mapping, x_new, y_new);
1988 static EvFormField *
1989 ev_view_get_form_field_at_location (EvView *view,
1994 gint x_new = 0, y_new = 0;
1995 EvMappingList *forms_mapping;
1997 if (!EV_IS_DOCUMENT_FORMS (view->document))
2000 if (!get_doc_point_from_location (view, x, y, &page, &x_new, &y_new))
2003 forms_mapping = ev_page_cache_get_form_field_mapping (view->page_cache, page);
2006 return ev_mapping_list_get_data (forms_mapping, x_new, y_new);
2011 static cairo_region_t *
2012 ev_view_form_field_get_region (EvView *view,
2015 GdkRectangle view_area;
2016 EvMappingList *forms_mapping;
2018 forms_mapping = ev_page_cache_get_form_field_mapping (view->page_cache,
2019 field->page->index);
2020 ev_view_get_area_from_mapping (view, field->page->index,
2024 return cairo_region_create_rectangle (&view_area);
2028 ev_view_forms_remove_widgets (EvView *view)
2030 ev_view_remove_all (view);
2036 ev_view_form_field_destroy (GtkWidget *widget,
2039 g_idle_add ((GSourceFunc)ev_view_forms_remove_widgets, view);
2043 ev_view_form_field_button_create_widget (EvView *view,
2046 EvFormFieldButton *field_button = EV_FORM_FIELD_BUTTON (field);
2047 cairo_region_t *field_region = NULL;
2049 switch (field_button->type) {
2050 case EV_FORM_FIELD_BUTTON_PUSH:
2052 case EV_FORM_FIELD_BUTTON_CHECK:
2053 case EV_FORM_FIELD_BUTTON_RADIO: {
2055 EvMappingList *forms_mapping;
2058 state = ev_document_forms_form_field_button_get_state (EV_DOCUMENT_FORMS (view->document),
2061 /* FIXME: it actually depends on NoToggleToOff flags */
2062 if (field_button->type == EV_FORM_FIELD_BUTTON_RADIO &&
2063 state && field_button->state)
2066 field_region = ev_view_form_field_get_region (view, field);
2068 /* For radio buttons and checkbox buttons that are in a set
2069 * we need to update also the region for the current selected item
2071 forms_mapping = ev_page_cache_get_form_field_mapping (view->page_cache,
2072 field->page->index);
2073 for (l = ev_mapping_list_get_list (forms_mapping); l; l = g_list_next (l)) {
2074 EvFormField *button = ((EvMapping *)(l->data))->data;
2075 cairo_region_t *button_region;
2077 if (button->id == field->id)
2080 /* FIXME: only buttons in the same group should be updated */
2081 if (!EV_IS_FORM_FIELD_BUTTON (button) ||
2082 EV_FORM_FIELD_BUTTON (button)->type != field_button->type ||
2083 EV_FORM_FIELD_BUTTON (button)->state != TRUE)
2086 button_region = ev_view_form_field_get_region (view, button);
2087 cairo_region_union (field_region, button_region);
2088 cairo_region_destroy (button_region);
2091 ev_document_forms_form_field_button_set_state (EV_DOCUMENT_FORMS (view->document),
2093 field_button->state = !state;
2098 ev_view_reload_page (view, field->page->index, field_region);
2099 cairo_region_destroy (field_region);
2105 ev_view_form_field_text_save (EvView *view,
2110 if (!view->document)
2113 field = g_object_get_data (G_OBJECT (widget), "form-field");
2115 if (field->changed) {
2116 EvFormFieldText *field_text = EV_FORM_FIELD_TEXT (field);
2117 cairo_region_t *field_region;
2119 field_region = ev_view_form_field_get_region (view, field);
2121 ev_document_forms_form_field_text_set_text (EV_DOCUMENT_FORMS (view->document),
2122 field, field_text->text);
2123 field->changed = FALSE;
2124 ev_view_reload_page (view, field->page->index, field_region);
2125 cairo_region_destroy (field_region);
2130 ev_view_form_field_text_changed (GtkWidget *widget,
2133 EvFormFieldText *field_text = EV_FORM_FIELD_TEXT (field);
2136 if (GTK_IS_ENTRY (widget)) {
2137 text = g_strdup (gtk_entry_get_text (GTK_ENTRY (widget)));
2138 } else if (GTK_IS_TEXT_BUFFER (widget)) {
2139 GtkTextIter start, end;
2141 gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (widget), &start, &end);
2142 text = gtk_text_buffer_get_text (GTK_TEXT_BUFFER (widget),
2143 &start, &end, FALSE);
2146 if (!field_text->text ||
2147 (field_text->text && g_ascii_strcasecmp (field_text->text, text) != 0)) {
2148 g_free (field_text->text);
2149 field_text->text = text;
2150 field->changed = TRUE;
2155 ev_view_form_field_text_focus_out (GtkWidget *widget,
2156 GdkEventFocus *event,
2159 ev_view_form_field_text_save (view, widget);
2165 ev_view_form_field_text_create_widget (EvView *view,
2168 EvFormFieldText *field_text = EV_FORM_FIELD_TEXT (field);
2169 GtkWidget *text = NULL;
2172 txt = ev_document_forms_form_field_text_get_text (EV_DOCUMENT_FORMS (view->document),
2175 switch (field_text->type) {
2176 case EV_FORM_FIELD_TEXT_FILE_SELECT:
2178 case EV_FORM_FIELD_TEXT_NORMAL:
2179 text = gtk_entry_new ();
2180 gtk_entry_set_has_frame (GTK_ENTRY (text), FALSE);
2181 gtk_entry_set_max_length (GTK_ENTRY (text), field_text->max_len);
2182 gtk_entry_set_visibility (GTK_ENTRY (text), !field_text->is_password);
2185 gtk_entry_set_text (GTK_ENTRY (text), txt);
2189 g_signal_connect (text, "focus-out-event",
2190 G_CALLBACK (ev_view_form_field_text_focus_out),
2192 g_signal_connect (text, "changed",
2193 G_CALLBACK (ev_view_form_field_text_changed),
2195 g_signal_connect_after (text, "activate",
2196 G_CALLBACK (ev_view_form_field_destroy),
2199 case EV_FORM_FIELD_TEXT_MULTILINE: {
2200 GtkTextBuffer *buffer;
2202 text = gtk_text_view_new ();
2203 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text));
2206 gtk_text_buffer_set_text (buffer, txt, -1);
2210 g_signal_connect( buffer, "focus-out-event",
2211 G_CALLBACK (ev_view_form_field_text_focus_out),
2213 g_signal_connect (buffer, "changed",
2214 G_CALLBACK (ev_view_form_field_text_changed),
2220 g_object_weak_ref (G_OBJECT (text),
2221 (GWeakNotify)ev_view_form_field_text_save,
2228 ev_view_form_field_choice_save (EvView *view,
2233 if (!view->document)
2236 field = g_object_get_data (G_OBJECT (widget), "form-field");
2238 if (field->changed) {
2240 EvFormFieldChoice *field_choice = EV_FORM_FIELD_CHOICE (field);
2241 cairo_region_t *field_region;
2243 field_region = ev_view_form_field_get_region (view, field);
2245 if (field_choice->is_editable) {
2246 ev_document_forms_form_field_choice_set_text (EV_DOCUMENT_FORMS (view->document),
2247 field, field_choice->text);
2249 ev_document_forms_form_field_choice_unselect_all (EV_DOCUMENT_FORMS (view->document), field);
2250 for (l = field_choice->selected_items; l; l = g_list_next (l)) {
2251 ev_document_forms_form_field_choice_select_item (EV_DOCUMENT_FORMS (view->document),
2253 GPOINTER_TO_INT (l->data));
2256 field->changed = FALSE;
2257 ev_view_reload_page (view, field->page->index, field_region);
2258 cairo_region_destroy (field_region);
2263 ev_view_form_field_choice_changed (GtkWidget *widget,
2266 EvFormFieldChoice *field_choice = EV_FORM_FIELD_CHOICE (field);
2268 if (GTK_IS_COMBO_BOX (widget)) {
2271 item = gtk_combo_box_get_active (GTK_COMBO_BOX (widget));
2272 if (!field_choice->selected_items ||
2273 GPOINTER_TO_INT (field_choice->selected_items->data) != item) {
2274 g_list_free (field_choice->selected_items);
2275 field_choice->selected_items = NULL;
2276 field_choice->selected_items = g_list_prepend (field_choice->selected_items,
2277 GINT_TO_POINTER (item));
2278 field->changed = TRUE;
2281 if (gtk_combo_box_get_has_entry (GTK_COMBO_BOX (widget))) {
2284 text = gtk_entry_get_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (widget))));
2285 if (!field_choice->text ||
2286 (field_choice->text && g_ascii_strcasecmp (field_choice->text, text) != 0)) {
2287 g_free (field_choice->text);
2288 field_choice->text = g_strdup (text);
2289 field->changed = TRUE;
2292 } else if (GTK_IS_TREE_SELECTION (widget)) {
2293 GtkTreeSelection *selection = GTK_TREE_SELECTION (widget);
2294 GtkTreeModel *model;
2297 items = gtk_tree_selection_get_selected_rows (selection, &model);
2298 g_list_free (field_choice->selected_items);
2299 field_choice->selected_items = NULL;
2301 for (l = items; l && l->data; l = g_list_next (l)) {
2303 GtkTreePath *path = (GtkTreePath *)l->data;
2306 gtk_tree_model_get_iter (model, &iter, path);
2307 gtk_tree_model_get (model, &iter, 1, &item, -1);
2309 field_choice->selected_items = g_list_prepend (field_choice->selected_items,
2310 GINT_TO_POINTER (item));
2312 gtk_tree_path_free (path);
2315 g_list_free (items);
2317 field->changed = TRUE;
2322 ev_view_form_field_choice_create_widget (EvView *view,
2325 EvFormFieldChoice *field_choice = EV_FORM_FIELD_CHOICE (field);
2327 GtkTreeModel *model;
2329 gint selected_item = 0;
2331 n_items = ev_document_forms_form_field_choice_get_n_items (EV_DOCUMENT_FORMS (view->document),
2333 model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
2334 for (i = 0; i < n_items; i++) {
2338 item = ev_document_forms_form_field_choice_get_item (EV_DOCUMENT_FORMS (view->document),
2340 if (ev_document_forms_form_field_choice_is_item_selected (
2341 EV_DOCUMENT_FORMS (view->document), field, i)) {
2343 /* FIXME: we need a get_selected_items function in poppler */
2344 field_choice->selected_items = g_list_prepend (field_choice->selected_items,
2345 GINT_TO_POINTER (i));
2349 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
2350 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
2358 if (field_choice->type == EV_FORM_FIELD_CHOICE_LIST) {
2359 GtkCellRenderer *renderer;
2360 GtkWidget *tree_view;
2361 GtkTreeSelection *selection;
2363 tree_view = gtk_tree_view_new_with_model (model);
2364 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (tree_view), FALSE);
2366 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));
2367 if (field_choice->multi_select) {
2368 gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
2371 /* TODO: set selected items */
2373 renderer = gtk_cell_renderer_text_new ();
2374 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view),
2380 choice = gtk_scrolled_window_new (NULL, NULL);
2381 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (choice),
2382 GTK_POLICY_AUTOMATIC,
2383 GTK_POLICY_AUTOMATIC);
2384 gtk_container_add (GTK_CONTAINER (choice), tree_view);
2385 gtk_widget_show (tree_view);
2387 g_signal_connect (selection, "changed",
2388 G_CALLBACK (ev_view_form_field_choice_changed),
2390 g_signal_connect_after (selection, "changed",
2391 G_CALLBACK (ev_view_form_field_destroy),
2393 } else if (field_choice->is_editable) { /* ComboBoxEntry */
2396 /* FIXME once gtk bug 633050 is fixed */
2397 choice = g_object_new (GTK_TYPE_COMBO_BOX, "has-entry", TRUE, "model", model, NULL);
2399 text = ev_document_forms_form_field_choice_get_text (EV_DOCUMENT_FORMS (view->document), field);
2401 gtk_entry_set_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (choice))), text);
2405 g_signal_connect (choice, "changed",
2406 G_CALLBACK (ev_view_form_field_choice_changed),
2408 g_signal_connect_after (gtk_bin_get_child (GTK_BIN (choice)),
2410 G_CALLBACK (ev_view_form_field_destroy),
2412 } else { /* ComboBoxText */
2413 GtkCellRenderer *renderer;
2415 choice = gtk_combo_box_new_with_model (model);
2416 renderer = gtk_cell_renderer_text_new ();
2417 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (choice),
2419 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (choice),
2423 gtk_combo_box_set_active (GTK_COMBO_BOX (choice), selected_item);
2424 gtk_combo_box_popup (GTK_COMBO_BOX (choice));
2426 g_signal_connect (choice, "changed",
2427 G_CALLBACK (ev_view_form_field_choice_changed),
2429 g_signal_connect_after (choice, "changed",
2430 G_CALLBACK (ev_view_form_field_destroy),
2434 g_object_unref (model);
2436 g_object_weak_ref (G_OBJECT (choice),
2437 (GWeakNotify)ev_view_form_field_choice_save,
2444 ev_view_handle_form_field (EvView *view,
2449 GtkWidget *field_widget = NULL;
2450 EvMappingList *form_field_mapping;
2453 if (field->is_read_only)
2456 if (EV_IS_FORM_FIELD_BUTTON (field)) {
2457 field_widget = ev_view_form_field_button_create_widget (view, field);
2458 } else if (EV_IS_FORM_FIELD_TEXT (field)) {
2459 field_widget = ev_view_form_field_text_create_widget (view, field);
2460 } else if (EV_IS_FORM_FIELD_CHOICE (field)) {
2461 field_widget = ev_view_form_field_choice_create_widget (view, field);
2462 } else if (EV_IS_FORM_FIELD_SIGNATURE (field)) {
2466 /* Form field doesn't require a widget */
2470 g_object_set_data_full (G_OBJECT (field_widget), "form-field",
2471 g_object_ref (field),
2472 (GDestroyNotify)g_object_unref);
2474 form_field_mapping = ev_page_cache_get_form_field_mapping (view->page_cache,
2475 field->page->index);
2476 mapping = ev_mapping_list_find (form_field_mapping, field);
2477 ev_view_put_to_doc_rect (view, field_widget, field->page->index, &mapping->area);
2478 gtk_widget_show (field_widget);
2479 gtk_widget_grab_focus (field_widget);
2483 static EvViewWindowChild *
2484 ev_view_get_window_child (EvView *view,
2487 GList *children = view->window_children;
2490 EvViewWindowChild *child;
2492 child = (EvViewWindowChild *)children->data;
2493 children = children->next;
2495 if (child->window == window)
2503 ev_view_window_child_move (EvView *view,
2504 EvViewWindowChild *child,
2508 GtkAllocation allocation;
2511 gtk_widget_get_allocation (GTK_WIDGET (view), &allocation);
2512 gtk_window_get_size (GTK_WINDOW (child->window), &width, &height);
2516 gtk_window_move (GTK_WINDOW (child->window),
2517 CLAMP (x, child->parent_x,
2518 child->parent_x + allocation.width - width),
2519 CLAMP (y, child->parent_y,
2520 child->parent_y + allocation.height - height));
2524 ev_view_window_child_move_with_parent (EvView *view,
2527 EvViewWindowChild *child;
2528 gint root_x, root_y;
2530 child = ev_view_get_window_child (view, window);
2531 gdk_window_get_origin (gtk_widget_get_window (GTK_WIDGET (view)),
2533 if (root_x != child->parent_x || root_y != child->parent_y) {
2534 gint dest_x, dest_y;
2536 dest_x = child->x + (root_x - child->parent_x);
2537 dest_y = child->y + (root_y - child->parent_y);
2538 child->parent_x = root_x;
2539 child->parent_y = root_y;
2540 ev_view_window_child_move (view, child, dest_x, dest_y);
2543 if (child->visible && !gtk_widget_get_visible (window))
2544 gtk_widget_show (window);
2548 ev_view_window_child_put (EvView *view,
2556 EvViewWindowChild *child;
2557 gint root_x, root_y;
2559 gdk_window_get_origin (gtk_widget_get_window (GTK_WIDGET (view)),
2562 child = g_new0 (EvViewWindowChild, 1);
2563 child->window = window;
2565 child->orig_x = orig_x;
2566 child->orig_y = orig_y;
2567 child->parent_x = root_x;
2568 child->parent_y = root_y;
2569 child->visible = ev_annotation_window_is_open (EV_ANNOTATION_WINDOW (window));
2570 ev_view_window_child_move (view, child, x + root_x, y + root_y);
2573 gtk_widget_show (window);
2575 gtk_widget_hide (window);
2577 view->window_children = g_list_append (view->window_children, child);
2580 static EvViewWindowChild *
2581 ev_view_find_window_child_for_annot (EvView *view,
2583 EvAnnotation *annot)
2585 GList *children = view->window_children;
2588 EvViewWindowChild *child;
2589 EvAnnotation *wannot;
2591 child = (EvViewWindowChild *)children->data;
2592 children = children->next;
2594 if (child->page != page)
2597 wannot = ev_annotation_window_get_annotation (EV_ANNOTATION_WINDOW (child->window));
2598 if (ev_annotation_equal (wannot, annot))
2606 ev_view_window_children_free (EvView *view)
2610 if (!view->window_children)
2613 for (l = view->window_children; l && l->data; l = g_list_next (l)) {
2614 EvViewWindowChild *child;
2616 child = (EvViewWindowChild *)l->data;
2617 gtk_widget_destroy (GTK_WIDGET (child->window));
2620 g_list_free (view->window_children);
2621 view->window_children = NULL;
2622 view->window_child_focus = NULL;
2626 annotation_window_grab_focus (GtkWidget *widget,
2629 if (view->window_child_focus)
2630 ev_annotation_window_ungrab_focus (EV_ANNOTATION_WINDOW (view->window_child_focus->window));
2631 view->window_child_focus = ev_view_get_window_child (view, widget);
2635 annotation_window_closed (EvAnnotationWindow *window,
2638 EvViewWindowChild *child;
2640 child = ev_view_get_window_child (view, GTK_WIDGET (window));
2641 child->visible = FALSE;
2645 annotation_window_moved (EvAnnotationWindow *window,
2650 EvViewWindowChild *child;
2651 GdkRectangle page_area;
2653 GdkRectangle view_rect;
2654 EvRectangle doc_rect;
2657 child = ev_view_get_window_child (view, GTK_WIDGET (window));
2658 if (child->x == x && child->y == y)
2661 child->moved = TRUE;
2665 /* Window has been moved by the user,
2666 * we have to set a new origin in doc coords
2668 gtk_window_get_size (GTK_WINDOW (window), &width, &height);
2669 view_rect.x = (x - child->parent_x) + view->scroll_x;
2670 view_rect.y = (y - child->parent_y) + view->scroll_y;
2671 view_rect.width = width;
2672 view_rect.height = height;
2674 ev_view_get_page_extents (view, child->page, &page_area, &border);
2675 view_rect_to_doc_rect (view, &view_rect, &page_area, &doc_rect);
2676 child->orig_x = doc_rect.x1;
2677 child->orig_y = doc_rect.y1;
2681 ev_view_annotation_save_contents (EvView *view,
2683 EvAnnotation *annot)
2685 if (!view->document)
2688 ev_document_doc_mutex_lock ();
2689 ev_document_annotations_save_annotation (EV_DOCUMENT_ANNOTATIONS (view->document),
2690 annot, EV_ANNOTATIONS_SAVE_CONTENTS);
2691 ev_document_doc_mutex_unlock ();
2695 ev_view_create_annotation_window (EvView *view,
2696 EvAnnotation *annot,
2700 EvRectangle doc_rect;
2701 GdkRectangle view_rect;
2704 window = ev_annotation_window_new (annot, parent);
2705 g_signal_connect (window, "grab_focus",
2706 G_CALLBACK (annotation_window_grab_focus),
2708 g_signal_connect (window, "closed",
2709 G_CALLBACK (annotation_window_closed),
2711 g_signal_connect (window, "moved",
2712 G_CALLBACK (annotation_window_moved),
2714 g_signal_connect_swapped (annot, "notify::contents",
2715 G_CALLBACK (ev_view_annotation_save_contents),
2717 g_object_set_data (G_OBJECT (annot), "popup", window);
2719 page = ev_annotation_get_page_index (annot);
2720 ev_annotation_window_get_rectangle (EV_ANNOTATION_WINDOW (window), &doc_rect);
2721 doc_rect_to_view_rect (view, page, &doc_rect, &view_rect);
2722 view_rect.x -= view->scroll_x;
2723 view_rect.y -= view->scroll_y;
2725 ev_view_window_child_put (view, window, page,
2726 view_rect.x, view_rect.y,
2727 doc_rect.x1, doc_rect.y1);
2733 show_annotation_windows (EvView *view,
2736 EvMappingList *annots;
2740 parent = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (view)));
2742 annots = ev_page_cache_get_annot_mapping (view->page_cache, page);
2744 for (l = ev_mapping_list_get_list (annots); l && l->data; l = g_list_next (l)) {
2745 EvAnnotation *annot;
2746 EvViewWindowChild *child;
2749 annot = ((EvMapping *)(l->data))->data;
2751 if (!EV_IS_ANNOTATION_MARKUP (annot))
2754 if (!ev_annotation_markup_has_popup (EV_ANNOTATION_MARKUP (annot)))
2757 window = g_object_get_data (G_OBJECT (annot), "popup");
2759 ev_view_window_child_move_with_parent (view, window);
2763 /* Look if we already have a popup for this annot */
2764 child = ev_view_find_window_child_for_annot (view, page, annot);
2765 window = child ? child->window : NULL;
2767 ev_annotation_window_set_annotation (EV_ANNOTATION_WINDOW (window), annot);
2768 g_object_set_data (G_OBJECT (annot), "popup", window);
2769 ev_view_window_child_move_with_parent (view, window);
2771 ev_view_create_annotation_window (view, annot, parent);
2777 hide_annotation_windows (EvView *view,
2780 EvMappingList *annots;
2783 annots = ev_page_cache_get_annot_mapping (view->page_cache, page);
2785 for (l = ev_mapping_list_get_list (annots); l && l->data; l = g_list_next (l)) {
2786 EvAnnotation *annot;
2789 annot = ((EvMapping *)(l->data))->data;
2791 if (!EV_IS_ANNOTATION_MARKUP (annot))
2794 window = g_object_get_data (G_OBJECT (annot), "popup");
2796 gtk_widget_hide (window);
2800 static EvAnnotation *
2801 ev_view_get_annotation_at_location (EvView *view,
2806 gint x_new = 0, y_new = 0;
2807 EvMappingList *annotations_mapping;
2809 if (!EV_IS_DOCUMENT_ANNOTATIONS (view->document))
2812 if (!get_doc_point_from_location (view, x, y, &page, &x_new, &y_new))
2815 annotations_mapping = ev_page_cache_get_annot_mapping (view->page_cache, page);
2817 if (annotations_mapping)
2818 return ev_mapping_list_get_data (annotations_mapping, x_new, y_new);
2824 ev_view_annotation_show_popup_window (EvView *view,
2827 EvViewWindowChild *child;
2832 child = ev_view_get_window_child (view, window);
2833 if (!child->visible) {
2834 child->visible = TRUE;
2835 ev_view_window_child_move (view, child, child->x, child->y);
2836 gtk_widget_show (window);
2841 ev_view_handle_annotation (EvView *view,
2842 EvAnnotation *annot,
2847 if (EV_IS_ANNOTATION_MARKUP (annot)) {
2850 window = g_object_get_data (G_OBJECT (annot), "popup");
2851 ev_view_annotation_show_popup_window (view, window);
2854 if (EV_IS_ANNOTATION_ATTACHMENT (annot)) {
2855 EvAttachment *attachment;
2857 attachment = ev_annotation_attachment_get_attachment (EV_ANNOTATION_ATTACHMENT (annot));
2859 GError *error = NULL;
2861 ev_attachment_open (attachment,
2862 gtk_widget_get_screen (GTK_WIDGET (view)),
2867 g_warning ("%s", error->message);
2868 g_error_free (error);
2875 ev_view_create_annotation (EvView *view,
2876 EvAnnotationType annot_type,
2880 EvAnnotation *annot;
2882 GdkRectangle page_area;
2884 EvRectangle doc_rect, popup_rect;
2886 GdkColor color = { 0, 65535, 65535, 0 };
2887 GdkRectangle view_rect;
2888 cairo_region_t *region;
2892 ev_view_get_page_extents (view, view->current_page, &page_area, &border);
2893 view_point_to_doc_point (view, &point, &page_area,
2894 &doc_rect.x1, &doc_rect.y1);
2895 doc_rect.x2 = doc_rect.x1 + 24;
2896 doc_rect.y2 = doc_rect.y1 + 24;
2898 ev_document_doc_mutex_lock ();
2899 page = ev_document_get_page (view->document, view->current_page);
2900 switch (annot_type) {
2901 case EV_ANNOTATION_TYPE_TEXT:
2902 annot = ev_annotation_text_new (page);
2904 case EV_ANNOTATION_TYPE_ATTACHMENT:
2906 g_object_unref (page);
2907 ev_document_doc_mutex_unlock ();
2910 g_assert_not_reached ();
2912 g_object_unref (page);
2914 ev_annotation_set_color (annot, &color);
2916 if (EV_IS_ANNOTATION_MARKUP (annot)) {
2917 popup_rect.x1 = doc_rect.x2;
2918 popup_rect.x2 = popup_rect.x1 + 200;
2919 popup_rect.y1 = doc_rect.y2;
2920 popup_rect.y2 = popup_rect.y1 + 150;
2921 g_object_set (annot,
2922 "rectangle", &popup_rect,
2924 "popup_is_open", FALSE,
2925 "label", g_get_real_name (),
2929 ev_document_annotations_add_annotation (EV_DOCUMENT_ANNOTATIONS (view->document),
2931 ev_document_doc_mutex_unlock ();
2933 /* If the page didn't have annots, mark the cache as dirty */
2934 if (!ev_page_cache_get_annot_mapping (view->page_cache, view->current_page))
2935 ev_page_cache_mark_dirty (view->page_cache, view->current_page);
2937 if (EV_IS_ANNOTATION_MARKUP (annot)) {
2941 parent = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (view)));
2942 window = ev_view_create_annotation_window (view, annot, parent);
2944 /* Show the annot window the first time */
2945 ev_view_annotation_show_popup_window (view, window);
2948 doc_rect_to_view_rect (view, view->current_page, &doc_rect, &view_rect);
2949 view_rect.x -= view->scroll_x;
2950 view_rect.y -= view->scroll_y;
2951 region = cairo_region_create_rectangle (&view_rect);
2952 ev_view_reload_page (view, view->current_page, region);
2953 cairo_region_destroy (region);
2955 g_signal_emit (view, signals[SIGNAL_ANNOT_ADDED], 0, annot);
2959 ev_view_focus_annotation (EvView *view,
2960 EvMapping *annot_mapping)
2962 GdkRectangle view_rect;
2963 EvAnnotation *annot;
2966 if (!EV_IS_DOCUMENT_ANNOTATIONS (view->document))
2969 if (view->focus_annotation == annot_mapping)
2972 view->focus_annotation = annot_mapping;
2973 annot = (EvAnnotation *)annot_mapping->data;
2975 page = ev_annotation_get_page_index (annot);
2976 ev_document_model_set_page (view->model, page);
2978 doc_rect_to_view_rect (view, page,
2979 &annot_mapping->area, &view_rect);
2980 ensure_rectangle_is_visible (view, &view_rect);
2981 gtk_widget_queue_draw (GTK_WIDGET (view));
2985 ev_view_begin_add_annotation (EvView *view,
2986 EvAnnotationType annot_type)
2988 if (annot_type == EV_ANNOTATION_TYPE_UNKNOWN)
2991 if (view->adding_annot)
2994 view->adding_annot = TRUE;
2995 view->adding_annot_type = annot_type;
2996 ev_view_set_cursor (view, EV_VIEW_CURSOR_ADD);
3000 ev_view_cancel_add_annotation (EvView *view)
3004 if (!view->adding_annot)
3007 view->adding_annot = FALSE;
3008 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
3009 ev_view_handle_cursor_over_xy (view, x, y);
3013 ev_view_synctex_backward_search (EvView *view,
3018 gint x_new = 0, y_new = 0;
3021 if (!ev_document_has_synctex (view->document))
3024 if (!get_doc_point_from_location (view, x, y, &page, &x_new, &y_new))
3027 link = ev_document_synctex_backward_search (view->document, page, x_new, y_new);
3029 g_signal_emit (view, signals[SIGNAL_SYNC_SOURCE], 0, link);
3038 /*** GtkWidget implementation ***/
3041 ev_view_size_request_continuous_dual_page (EvView *view,
3042 GtkRequisition *requisition)
3046 n_pages = ev_document_get_n_pages (view->document) + 1;
3047 get_page_y_offset (view, n_pages, &requisition->height);
3049 switch (view->sizing_mode) {
3050 case EV_SIZING_FIT_WIDTH:
3051 case EV_SIZING_BEST_FIT:
3052 requisition->width = 1;
3055 case EV_SIZING_FREE: {
3059 ev_view_get_max_page_size (view, &max_width, NULL);
3060 compute_border (view, max_width, max_width, &border);
3061 requisition->width = (max_width + border.left + border.right) * 2 + (view->spacing * 3);
3065 g_assert_not_reached ();
3070 ev_view_size_request_continuous (EvView *view,
3071 GtkRequisition *requisition)
3075 n_pages = ev_document_get_n_pages (view->document);
3076 get_page_y_offset (view, n_pages, &requisition->height);
3078 switch (view->sizing_mode) {
3079 case EV_SIZING_FIT_WIDTH:
3080 case EV_SIZING_BEST_FIT:
3081 requisition->width = 1;
3084 case EV_SIZING_FREE: {
3088 ev_view_get_max_page_size (view, &max_width, NULL);
3089 compute_border (view, max_width, max_width, &border);
3090 requisition->width = max_width + (view->spacing * 2) + border.left + border.right;
3094 g_assert_not_reached ();
3099 ev_view_size_request_dual_page (EvView *view,
3100 GtkRequisition *requisition)
3105 if (view->sizing_mode == EV_SIZING_BEST_FIT) {
3106 requisition->width = 1;
3107 requisition->height = 1;
3112 /* Find the largest of the two. */
3113 ev_view_get_page_size (view,
3116 if (view->current_page + 1 < ev_document_get_n_pages (view->document)) {
3117 gint width_2, height_2;
3118 ev_view_get_page_size (view,
3119 view->current_page + 1,
3120 &width_2, &height_2);
3121 if (width_2 > width) {
3126 compute_border (view, width, height, &border);
3128 requisition->width = view->sizing_mode == EV_SIZING_FIT_WIDTH ? 1 :
3129 ((width + border.left + border.right) * 2) + (view->spacing * 3);
3130 requisition->height = (height + border.top + border.bottom) + (view->spacing * 2);
3134 ev_view_size_request_single_page (EvView *view,
3135 GtkRequisition *requisition)
3140 if (view->sizing_mode == EV_SIZING_BEST_FIT) {
3141 requisition->width = 1;
3142 requisition->height = 1;
3147 ev_view_get_page_size (view, view->current_page, &width, &height);
3148 compute_border (view, width, height, &border);
3150 requisition->width = view->sizing_mode == EV_SIZING_FIT_WIDTH ? 1 :
3151 width + border.left + border.right + (2 * view->spacing);
3152 requisition->height = height + border.top + border.bottom + (2 * view->spacing);
3156 ev_view_size_request (GtkWidget *widget,
3157 GtkRequisition *requisition)
3159 EvView *view = EV_VIEW (widget);
3161 if (view->document == NULL) {
3162 view->requisition.width = 1;
3163 view->requisition.height = 1;
3165 *requisition = view->requisition;
3170 /* Get zoom for size here when not called from
3171 * ev_view_size_allocate()
3173 if (!view->internal_size_request &&
3174 (view->sizing_mode == EV_SIZING_FIT_WIDTH ||
3175 view->sizing_mode == EV_SIZING_BEST_FIT)) {
3176 GtkAllocation allocation;
3178 gtk_widget_get_allocation (widget, &allocation);
3179 ev_view_zoom_for_size (view,
3184 if (view->continuous && view->dual_page)
3185 ev_view_size_request_continuous_dual_page (view, &view->requisition);
3186 else if (view->continuous)
3187 ev_view_size_request_continuous (view, &view->requisition);
3188 else if (view->dual_page)
3189 ev_view_size_request_dual_page (view, &view->requisition);
3191 ev_view_size_request_single_page (view, &view->requisition);
3193 *requisition = view->requisition;
3197 ev_view_get_preferred_width (GtkWidget *widget,
3201 GtkRequisition requisition;
3203 ev_view_size_request (widget, &requisition);
3205 *minimum = *natural = requisition.width;
3209 ev_view_get_preferred_height (GtkWidget *widget,
3213 GtkRequisition requisition;
3215 ev_view_size_request (widget, &requisition);
3217 *minimum = *natural = requisition.height;
3221 ev_view_size_allocate (GtkWidget *widget,
3222 GtkAllocation *allocation)
3224 EvView *view = EV_VIEW (widget);
3226 gint root_x, root_y;
3228 gtk_widget_set_allocation (widget, allocation);
3230 if (gtk_widget_get_realized (widget))
3231 gdk_window_move_resize (gtk_widget_get_window (widget),
3235 allocation->height);
3237 if (!view->document)
3240 if (view->sizing_mode == EV_SIZING_FIT_WIDTH ||
3241 view->sizing_mode == EV_SIZING_BEST_FIT) {
3244 ev_view_zoom_for_size (view,
3246 allocation->height);
3247 view->internal_size_request = TRUE;
3248 ev_view_size_request (widget, &req);
3249 view->internal_size_request = FALSE;
3252 ev_view_set_adjustment_values (view, GTK_ORIENTATION_HORIZONTAL);
3253 ev_view_set_adjustment_values (view, GTK_ORIENTATION_VERTICAL);
3256 view_update_range_and_current_page (view);
3258 view->pending_scroll = SCROLL_TO_KEEP_POSITION;
3259 view->pending_resize = FALSE;
3260 view->pending_point.x = 0;
3261 view->pending_point.y = 0;
3263 for (l = view->children; l && l->data; l = g_list_next (l)) {
3264 GdkRectangle view_area;
3265 EvViewChild *child = (EvViewChild *)l->data;
3267 if (!gtk_widget_get_visible (child->widget))
3270 doc_rect_to_view_rect (view, child->page, &child->doc_rect, &view_area);
3271 view_area.x -= view->scroll_x;
3272 view_area.y -= view->scroll_y;
3274 gtk_widget_set_size_request (child->widget, view_area.width, view_area.height);
3275 gtk_widget_size_allocate (child->widget, &view_area);
3278 if (view->window_children)
3279 gdk_window_get_origin (gtk_widget_get_window (GTK_WIDGET (view)),
3282 for (l = view->window_children; l && l->data; l = g_list_next (l)) {
3283 EvViewWindowChild *child;
3284 EvRectangle doc_rect;
3285 GdkRectangle view_rect;
3287 child = (EvViewWindowChild *)l->data;
3289 ev_annotation_window_get_rectangle (EV_ANNOTATION_WINDOW (child->window), &doc_rect);
3291 doc_rect.x1 = child->orig_x;
3292 doc_rect.y1 = child->orig_y;
3294 doc_rect_to_view_rect (view, child->page, &doc_rect, &view_rect);
3295 view_rect.x -= view->scroll_x;
3296 view_rect.y -= view->scroll_y;
3298 if (view_rect.x != child->orig_x || view_rect.y != child->orig_y) {
3299 child->parent_x = root_x;
3300 child->parent_y = root_y;
3301 ev_view_window_child_move (view, child, view_rect.x + root_x, view_rect.y + root_y);
3307 ev_view_scroll_event (GtkWidget *widget, GdkEventScroll *event)
3309 EvView *view = EV_VIEW (widget);
3312 state = event->state & gtk_accelerator_get_default_mod_mask ();
3314 if (state == GDK_CONTROL_MASK) {
3315 ev_document_model_set_sizing_mode (view->model, EV_SIZING_FREE);
3316 if (event->direction == GDK_SCROLL_UP ||
3317 event->direction == GDK_SCROLL_LEFT) {
3318 if (ev_view_can_zoom_in (view)) {
3319 ev_view_zoom_in (view);
3322 if (ev_view_can_zoom_out (view)) {
3323 ev_view_zoom_out (view);
3330 view->jump_to_find_result = FALSE;
3332 /* Shift+Wheel scrolls the in the perpendicular direction */
3333 if (state & GDK_SHIFT_MASK) {
3334 if (event->direction == GDK_SCROLL_UP)
3335 event->direction = GDK_SCROLL_LEFT;
3336 else if (event->direction == GDK_SCROLL_LEFT)
3337 event->direction = GDK_SCROLL_UP;
3338 else if (event->direction == GDK_SCROLL_DOWN)
3339 event->direction = GDK_SCROLL_RIGHT;
3340 else if (event->direction == GDK_SCROLL_RIGHT)
3341 event->direction = GDK_SCROLL_DOWN;
3343 event->state &= ~GDK_SHIFT_MASK;
3344 state &= ~GDK_SHIFT_MASK;
3347 if (state == 0 && view->sizing_mode == EV_SIZING_BEST_FIT && !view->continuous) {
3348 switch (event->direction) {
3349 case GDK_SCROLL_DOWN:
3350 case GDK_SCROLL_RIGHT:
3351 ev_view_next_page (view);
3354 case GDK_SCROLL_LEFT:
3355 ev_view_previous_page (view);
3365 static EvViewSelection *
3366 find_selection_for_page (EvView *view,
3371 for (list = view->selection_info.selections; list != NULL; list = list->next) {
3372 EvViewSelection *selection;
3374 selection = (EvViewSelection *) list->data;
3376 if (selection->page == page)
3384 ev_view_realize (GtkWidget *widget)
3386 GtkAllocation allocation;
3388 GdkWindowAttr attributes;
3389 gint attributes_mask;
3391 gtk_widget_set_realized (widget, TRUE);
3393 gtk_widget_get_allocation (widget, &allocation);
3395 attributes.window_type = GDK_WINDOW_CHILD;
3396 attributes.x = allocation.x;
3397 attributes.y = allocation.y;
3398 attributes.width = allocation.width;
3399 attributes.height = allocation.height;
3400 attributes.wclass = GDK_INPUT_OUTPUT;
3401 attributes.visual = gtk_widget_get_visual (widget);
3402 attributes.event_mask = gtk_widget_get_events (widget);
3404 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
3406 window = gdk_window_new (gtk_widget_get_parent_window (widget),
3407 &attributes, attributes_mask);
3408 gtk_widget_set_window (widget, window);
3409 gdk_window_set_user_data (window, widget);
3411 gtk_style_context_set_background (gtk_widget_get_style_context (widget),
3416 ev_view_draw (GtkWidget *widget,
3419 EvView *view = EV_VIEW (widget);
3421 GdkRectangle clip_rect;
3423 if (view->loading) {
3424 show_loading_window (view);
3425 } else if (view->loading_window &&
3426 gtk_widget_get_visible (view->loading_window)) {
3427 ev_view_loading_window_move (view);
3430 if (view->document == NULL)
3433 if (!gdk_cairo_get_clip_rectangle (cr, &clip_rect))
3436 for (i = view->start_page; i >= 0 && i <= view->end_page; i++) {
3437 GdkRectangle page_area;
3439 gboolean page_ready = TRUE;
3441 if (!ev_view_get_page_extents (view, i, &page_area, &border))
3444 page_area.x -= view->scroll_x;
3445 page_area.y -= view->scroll_y;
3447 draw_one_page (view, i, cr, &page_area, &border, &clip_rect, &page_ready);
3449 if (page_ready && view->find_pages && view->highlight_find_results)
3450 highlight_find_results (view, cr, i);
3451 if (page_ready && EV_IS_DOCUMENT_ANNOTATIONS (view->document))
3452 show_annotation_windows (view, i);
3453 if (page_ready && view->focus_annotation)
3454 focus_annotation (view, cr, i, &clip_rect);
3455 if (page_ready && view->synctex_result)
3456 highlight_forward_search_results (view, cr, i);
3459 if (GTK_WIDGET_CLASS (ev_view_parent_class)->draw)
3460 GTK_WIDGET_CLASS (ev_view_parent_class)->draw (widget, cr);
3466 ev_view_do_popup_menu (EvView *view,
3470 GList *items = NULL;
3473 EvAnnotation *annot;
3475 image = ev_view_get_image_at_location (view, x, y);
3477 items = g_list_prepend (items, image);
3479 link = ev_view_get_link_at_location (view, x, y);
3481 items = g_list_prepend (items, link);
3483 annot = ev_view_get_annotation_at_location (view, x, y);
3485 items = g_list_prepend (items, annot);
3487 g_signal_emit (view, signals[SIGNAL_POPUP_MENU], 0, items);
3489 g_list_free (items);
3495 ev_view_popup_menu (GtkWidget *widget)
3499 gtk_widget_get_pointer (widget, &x, &y);
3500 return ev_view_do_popup_menu (EV_VIEW (widget), x, y);
3504 get_link_area (EvView *view,
3510 EvMappingList *link_mapping;
3512 gint x_offset = 0, y_offset = 0;
3514 x += view->scroll_x;
3515 y += view->scroll_y;
3517 find_page_at_location (view, x, y, &page, &x_offset, &y_offset);
3519 link_mapping = ev_page_cache_get_link_mapping (view->page_cache, page);
3520 ev_view_get_area_from_mapping (view, page,
3526 get_annot_area (EvView *view,
3529 EvAnnotation *annot,
3532 EvMappingList *annot_mapping;
3534 gint x_offset = 0, y_offset = 0;
3536 x += view->scroll_x;
3537 y += view->scroll_y;
3539 find_page_at_location (view, x, y, &page, &x_offset, &y_offset);
3541 annot_mapping = ev_page_cache_get_annot_mapping (view->page_cache, page);
3542 ev_view_get_area_from_mapping (view, page,
3548 ev_view_query_tooltip (GtkWidget *widget,
3551 gboolean keyboard_tip,
3552 GtkTooltip *tooltip)
3554 EvView *view = EV_VIEW (widget);
3556 EvAnnotation *annot;
3559 annot = ev_view_get_annotation_at_location (view, x, y);
3561 const gchar *contents;
3563 if ((contents = ev_annotation_get_contents (annot))) {
3564 GdkRectangle annot_area;
3566 get_annot_area (view, x, y, annot, &annot_area);
3567 gtk_tooltip_set_text (tooltip, contents);
3568 gtk_tooltip_set_tip_area (tooltip, &annot_area);
3574 link = ev_view_get_link_at_location (view, x, y);
3578 text = tip_from_link (view, link);
3579 if (text && g_utf8_validate (text, -1, NULL)) {
3580 GdkRectangle link_area;
3582 get_link_area (view, x, y, link, &link_area);
3583 gtk_tooltip_set_text (tooltip, text);
3584 gtk_tooltip_set_tip_area (tooltip, &link_area);
3595 start_selection_for_event (EvView *view,
3596 GdkEventButton *event)
3598 clear_selection (view);
3600 view->selection_info.start.x = event->x + view->scroll_x;
3601 view->selection_info.start.y = event->y + view->scroll_y;
3603 switch (event->type) {
3604 case GDK_2BUTTON_PRESS:
3605 view->selection_info.style = EV_SELECTION_STYLE_WORD;
3607 case GDK_3BUTTON_PRESS:
3608 view->selection_info.style = EV_SELECTION_STYLE_LINE;
3611 view->selection_info.style = EV_SELECTION_STYLE_GLYPH;
3615 /* In case of WORD or LINE, compute selections now */
3616 compute_selections (view,
3617 view->selection_info.style,
3618 &(view->selection_info.start),
3619 &(view->selection_info.start));
3623 ev_view_button_press_event (GtkWidget *widget,
3624 GdkEventButton *event)
3626 EvView *view = EV_VIEW (widget);
3628 if (!view->document)
3631 if (!gtk_widget_has_focus (widget)) {
3632 gtk_widget_grab_focus (widget);
3635 if (view->window_child_focus) {
3636 EvAnnotationWindow *window;
3637 EvAnnotation *annot;
3639 window = EV_ANNOTATION_WINDOW (view->window_child_focus->window);
3640 annot = ev_annotation_window_get_annotation (window);
3641 ev_annotation_window_ungrab_focus (window);
3642 view->window_child_focus = NULL;
3645 view->pressed_button = event->button;
3646 view->selection_info.in_drag = FALSE;
3648 if (view->adding_annot)
3651 if (view->scroll_info.autoscrolling)
3654 switch (event->button) {
3657 EvAnnotation *annot;
3660 if (event->state & GDK_CONTROL_MASK)
3661 return ev_view_synctex_backward_search (view, event->x , event->y);
3663 if (EV_IS_SELECTION (view->document) && view->selection_info.selections) {
3664 if (event->type == GDK_3BUTTON_PRESS) {
3665 start_selection_for_event (view, event);
3666 } else if (location_in_selected_text (view,
3667 event->x + view->scroll_x,
3668 event->y + view->scroll_y)) {
3669 view->selection_info.in_drag = TRUE;
3671 start_selection_for_event (view, event);
3674 gtk_widget_queue_draw (widget);
3675 } else if ((annot = ev_view_get_annotation_at_location (view, event->x, event->y))) {
3676 ev_view_handle_annotation (view, annot, event->x, event->y, event->time);
3677 } else if ((field = ev_view_get_form_field_at_location (view, event->x, event->y))) {
3678 ev_view_remove_all (view);
3679 ev_view_handle_form_field (view, field, event->x, event->y);
3680 } else if (!location_in_text (view, event->x + view->scroll_x, event->y + view->scroll_y) &&
3681 (image = ev_view_get_image_at_location (view, event->x, event->y))) {
3682 if (view->image_dnd_info.image)
3683 g_object_unref (view->image_dnd_info.image);
3684 view->image_dnd_info.image = g_object_ref (image);
3685 view->image_dnd_info.in_drag = TRUE;
3687 view->image_dnd_info.start.x = event->x + view->scroll_x;
3688 view->image_dnd_info.start.y = event->y + view->scroll_y;
3690 ev_view_remove_all (view);
3692 if (view->synctex_result) {
3693 g_free (view->synctex_result);
3694 view->synctex_result = NULL;
3695 gtk_widget_queue_draw (widget);
3698 if (view->focus_annotation)
3699 view->focus_annotation = NULL;
3701 if (EV_IS_SELECTION (view->document))
3702 start_selection_for_event (view, event);
3707 /* use root coordinates as reference point because
3708 * scrolling changes window relative coordinates */
3709 view->drag_info.start.x = event->x_root;
3710 view->drag_info.start.y = event->y_root;
3711 view->drag_info.hadj = gtk_adjustment_get_value (view->hadjustment);
3712 view->drag_info.vadj = gtk_adjustment_get_value (view->vadjustment);
3714 ev_view_set_cursor (view, EV_VIEW_CURSOR_DRAG);
3718 view->scroll_info.start_y = event->y;
3719 return ev_view_do_popup_menu (view, event->x, event->y);
3726 ev_view_remove_all (EvView *view)
3728 gtk_container_foreach (GTK_CONTAINER (view), (GtkCallback) gtk_widget_destroy, NULL);
3731 /*** Drag and Drop ***/
3733 ev_view_drag_data_get (GtkWidget *widget,
3734 GdkDragContext *context,
3735 GtkSelectionData *selection_data,
3739 EvView *view = EV_VIEW (widget);
3742 case TARGET_DND_TEXT:
3743 if (EV_IS_SELECTION (view->document) &&
3744 view->selection_info.selections) {
3747 text = get_selected_text (view);
3748 gtk_selection_data_set_text (selection_data,
3754 case TARGET_DND_IMAGE:
3755 if (view->image_dnd_info.image) {
3758 ev_document_doc_mutex_lock ();
3759 pixbuf = ev_document_images_get_image (EV_DOCUMENT_IMAGES (view->document),
3760 view->image_dnd_info.image);
3761 ev_document_doc_mutex_unlock ();
3763 gtk_selection_data_set_pixbuf (selection_data, pixbuf);
3764 g_object_unref (pixbuf);
3767 case TARGET_DND_URI:
3768 if (view->image_dnd_info.image) {
3770 const gchar *tmp_uri;
3773 ev_document_doc_mutex_lock ();
3774 pixbuf = ev_document_images_get_image (EV_DOCUMENT_IMAGES (view->document),
3775 view->image_dnd_info.image);
3776 ev_document_doc_mutex_unlock ();
3778 tmp_uri = ev_image_save_tmp (view->image_dnd_info.image, pixbuf);
3779 g_object_unref (pixbuf);
3781 uris[0] = (gchar *)tmp_uri;
3783 gtk_selection_data_set_uris (selection_data, uris);
3789 ev_view_drag_motion (GtkWidget *widget,
3790 GdkDragContext *context,
3795 if (gtk_drag_get_source_widget (context) == widget)
3796 gdk_drag_status (context, 0, time);
3798 gdk_drag_status (context, gdk_drag_context_get_suggested_action (context), time);
3804 selection_update_idle_cb (EvView *view)
3806 compute_selections (view,
3807 view->selection_info.style,
3808 &view->selection_info.start,
3810 view->selection_update_id = 0;
3815 selection_scroll_timeout_cb (EvView *view)
3817 gint x, y, shift = 0;
3818 GtkWidget *widget = GTK_WIDGET (view);
3819 GtkAllocation allocation;
3821 gtk_widget_get_allocation (widget, &allocation);
3822 gtk_widget_get_pointer (widget, &x, &y);
3824 if (y > allocation.height) {
3825 shift = (y - allocation.height) / 2;
3831 gtk_adjustment_set_value (view->vadjustment,
3832 CLAMP (gtk_adjustment_get_value (view->vadjustment) + shift,
3833 gtk_adjustment_get_lower (view->vadjustment),
3834 gtk_adjustment_get_upper (view->vadjustment) -
3835 gtk_adjustment_get_page_size (view->vadjustment)));
3837 if (x > allocation.width) {
3838 shift = (x - allocation.width) / 2;
3844 gtk_adjustment_set_value (view->hadjustment,
3845 CLAMP (gtk_adjustment_get_value (view->hadjustment) + shift,
3846 gtk_adjustment_get_lower (view->hadjustment),
3847 gtk_adjustment_get_upper (view->hadjustment) -
3848 gtk_adjustment_get_page_size (view->hadjustment)));
3854 ev_view_drag_update_momentum (EvView *view)
3857 if (!view->drag_info.in_drag)
3860 for (i = DRAG_HISTORY - 1; i > 0; i--) {
3861 view->drag_info.buffer[i].x = view->drag_info.buffer[i-1].x;
3862 view->drag_info.buffer[i].y = view->drag_info.buffer[i-1].y;
3865 /* Momentum is a moving average of 10ms granularity over
3866 * the last 100ms with each 10ms stored in buffer.
3869 view->drag_info.momentum.x = (view->drag_info.buffer[DRAG_HISTORY - 1].x - view->drag_info.buffer[0].x);
3870 view->drag_info.momentum.y = (view->drag_info.buffer[DRAG_HISTORY - 1].y - view->drag_info.buffer[0].y);
3876 ev_view_scroll_drag_release (EvView *view)
3878 gdouble dhadj_value, dvadj_value;
3879 gdouble oldhadjustment, oldvadjustment;
3880 gdouble h_page_size, v_page_size;
3881 gdouble h_upper, v_upper;
3882 GtkAllocation allocation;
3884 view->drag_info.momentum.x /= 1.2;
3885 view->drag_info.momentum.y /= 1.2; /* Alter these constants to change "friction" */
3887 gtk_widget_get_allocation (GTK_WIDGET (view), &allocation);
3889 h_page_size = gtk_adjustment_get_page_size (view->hadjustment);
3890 v_page_size = gtk_adjustment_get_page_size (view->vadjustment);
3892 dhadj_value = h_page_size *
3893 (gdouble)view->drag_info.momentum.x / allocation.width;
3894 dvadj_value = v_page_size *
3895 (gdouble)view->drag_info.momentum.y / allocation.height;
3897 oldhadjustment = gtk_adjustment_get_value (view->hadjustment);
3898 oldvadjustment = gtk_adjustment_get_value (view->vadjustment);
3900 h_upper = gtk_adjustment_get_upper (view->hadjustment);
3901 v_upper = gtk_adjustment_get_upper (view->vadjustment);
3903 /* When we reach the edges, we need either to absorb some momentum and bounce by
3904 * multiplying it on -0.5 or stop scrolling by setting momentum to 0. */
3905 if (((oldhadjustment + dhadj_value) > (h_upper - h_page_size)) ||
3906 ((oldhadjustment + dhadj_value) < 0))
3907 view->drag_info.momentum.x = 0;
3908 if (((oldvadjustment + dvadj_value) > (v_upper - v_page_size)) ||
3909 ((oldvadjustment + dvadj_value) < 0))
3910 view->drag_info.momentum.y = 0;
3912 gtk_adjustment_set_value (view->hadjustment,
3913 MIN (oldhadjustment + dhadj_value,
3914 h_upper - h_page_size));
3915 gtk_adjustment_set_value (view->vadjustment,
3916 MIN (oldvadjustment + dvadj_value,
3917 v_upper - v_page_size));
3919 if (((view->drag_info.momentum.x < 1) && (view->drag_info.momentum.x > -1)) &&
3920 ((view->drag_info.momentum.y < 1) && (view->drag_info.momentum.y > -1)))
3927 ev_view_motion_notify_event (GtkWidget *widget,
3928 GdkEventMotion *event)
3930 EvView *view = EV_VIEW (widget);
3934 if (!view->document)
3937 window = gtk_widget_get_window (widget);
3939 if (event->is_hint || event->window != window) {
3940 gtk_widget_get_pointer (widget, &x, &y);
3946 if (view->scroll_info.autoscrolling) {
3947 view->scroll_info.last_y = y;
3951 if (view->selection_info.in_drag) {
3952 if (gtk_drag_check_threshold (widget,
3953 view->selection_info.start.x,
3954 view->selection_info.start.y,
3956 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
3958 gtk_target_list_add_text_targets (target_list, TARGET_DND_TEXT);
3960 gtk_drag_begin (widget, target_list,
3962 1, (GdkEvent *)event);
3964 view->selection_info.in_drag = FALSE;
3966 gtk_target_list_unref (target_list);
3970 } else if (view->image_dnd_info.in_drag) {
3971 if (gtk_drag_check_threshold (widget,
3972 view->selection_info.start.x,
3973 view->selection_info.start.y,
3975 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
3977 gtk_target_list_add_uri_targets (target_list, TARGET_DND_URI);
3978 gtk_target_list_add_image_targets (target_list, TARGET_DND_IMAGE, TRUE);
3980 gtk_drag_begin (widget, target_list,
3982 1, (GdkEvent *)event);
3984 view->image_dnd_info.in_drag = FALSE;
3986 gtk_target_list_unref (target_list);
3992 switch (view->pressed_button) {
3994 /* For the Evince 0.4.x release, we limit selection to un-rotated
3997 if (view->rotation != 0)
4000 /* Schedule timeout to scroll during selection and additionally
4001 * scroll once to allow arbitrary speed. */
4002 if (!view->selection_scroll_id)
4003 view->selection_scroll_id = g_timeout_add (SCROLL_TIME,
4004 (GSourceFunc)selection_scroll_timeout_cb,
4007 selection_scroll_timeout_cb (view);
4009 view->selection_info.in_selection = TRUE;
4010 view->motion.x = x + view->scroll_x;
4011 view->motion.y = y + view->scroll_y;
4013 /* Queue an idle to handle the motion. We do this because
4014 * handling any selection events in the motion could be slower
4015 * than new motion events reach us. We always put it in the
4016 * idle to make sure we catch up and don't visibly lag the
4018 if (!view->selection_update_id)
4019 view->selection_update_id = g_idle_add ((GSourceFunc)selection_update_idle_cb, view);
4023 if (!view->drag_info.in_drag) {
4027 start = gtk_drag_check_threshold (widget,
4028 view->drag_info.start.x,
4029 view->drag_info.start.y,
4032 view->drag_info.in_drag = start;
4033 view->drag_info.drag_timeout_id = g_timeout_add (10,
4034 (GSourceFunc)ev_view_drag_update_momentum, view);
4035 /* Set 100 to choose how long it takes to build up momentum */
4036 /* Clear out previous momentum info: */
4037 for (i = 0; i < DRAG_HISTORY; i++) {
4038 view->drag_info.buffer[i].x = event->x;
4039 view->drag_info.buffer[i].y = event->y;
4041 view->drag_info.momentum.x = 0;
4042 view->drag_info.momentum.y = 0;
4045 if (view->drag_info.in_drag) {
4047 gdouble dhadj_value, dvadj_value;
4048 GtkAllocation allocation;
4050 view->drag_info.buffer[0].x = event->x;
4051 view->drag_info.buffer[0].y = event->y;
4053 dx = event->x_root - view->drag_info.start.x;
4054 dy = event->y_root - view->drag_info.start.y;
4056 gtk_widget_get_allocation (widget, &allocation);
4058 dhadj_value = gtk_adjustment_get_page_size (view->hadjustment) *
4059 (gdouble)dx / allocation.width;
4060 dvadj_value = gtk_adjustment_get_page_size (view->vadjustment) *
4061 (gdouble)dy / allocation.height;
4063 /* clamp scrolling to visible area */
4064 gtk_adjustment_set_value (view->hadjustment,
4065 MIN (view->drag_info.hadj - dhadj_value,
4066 gtk_adjustment_get_upper (view->hadjustment) -
4067 gtk_adjustment_get_page_size (view->hadjustment)));
4068 gtk_adjustment_set_value (view->vadjustment,
4069 MIN (view->drag_info.vadj - dvadj_value,
4070 gtk_adjustment_get_upper (view->vadjustment) -
4071 gtk_adjustment_get_page_size (view->vadjustment)));
4078 ev_view_handle_cursor_over_xy (view, x, y);
4085 ev_view_button_release_event (GtkWidget *widget,
4086 GdkEventButton *event)
4088 EvView *view = EV_VIEW (widget);
4089 EvLink *link = NULL;
4091 view->image_dnd_info.in_drag = FALSE;
4093 if (view->scroll_info.autoscrolling) {
4094 ev_view_autoscroll_stop (view);
4095 view->pressed_button = -1;
4100 if (view->pressed_button == 1 && event->state & GDK_CONTROL_MASK) {
4101 view->pressed_button = -1;
4105 if (view->drag_info.in_drag) {
4106 view->drag_info.release_timeout_id =
4108 (GSourceFunc)ev_view_scroll_drag_release, view);
4111 if (view->document && !view->drag_info.in_drag && view->pressed_button != 3) {
4112 link = ev_view_get_link_at_location (view, event->x, event->y);
4115 view->drag_info.in_drag = FALSE;
4117 if (view->adding_annot && view->pressed_button == 1) {
4118 view->adding_annot = FALSE;
4119 ev_view_handle_cursor_over_xy (view, event->x, event->y);
4120 view->pressed_button = -1;
4122 ev_view_create_annotation (view,
4123 view->adding_annot_type,
4124 event->x + view->scroll_x,
4125 event->y + view->scroll_y);
4130 if (view->pressed_button == 2) {
4131 ev_view_handle_cursor_over_xy (view, event->x, event->y);
4134 view->pressed_button = -1;
4136 if (view->selection_scroll_id) {
4137 g_source_remove (view->selection_scroll_id);
4138 view->selection_scroll_id = 0;
4140 if (view->selection_update_id) {
4141 g_source_remove (view->selection_update_id);
4142 view->selection_update_id = 0;
4145 if (view->selection_info.selections) {
4146 clear_link_selected (view);
4147 ev_view_update_primary_selection (view);
4149 if (view->selection_info.in_drag) {
4150 clear_selection (view);
4151 gtk_widget_queue_draw (widget);
4154 view->selection_info.in_drag = FALSE;
4156 if (event->button == 2) {
4157 EvLinkAction *action;
4158 EvLinkActionType type;
4160 action = ev_link_get_action (link);
4164 type = ev_link_action_get_action_type (action);
4165 if (type == EV_LINK_ACTION_TYPE_GOTO_DEST) {
4166 g_signal_emit (view,
4167 signals[SIGNAL_EXTERNAL_LINK],
4171 ev_view_handle_link (view, link);
4179 ev_view_key_press_event (GtkWidget *widget,
4182 EvView *view = EV_VIEW (widget);
4184 if (!view->document)
4187 if (!gtk_widget_has_focus (widget)) {
4188 /* Forward key events to current focused window child */
4189 if (view->window_child_focus) {
4190 GdkEventKey *new_event;
4193 new_event = (GdkEventKey *) gdk_event_copy ((GdkEvent *)event);
4194 g_object_unref (new_event->window);
4195 new_event->window = gtk_widget_get_window (view->window_child_focus->window);
4196 if (new_event->window)
4197 g_object_ref (new_event->window);
4198 gtk_widget_realize (view->window_child_focus->window);
4199 handled = gtk_widget_event (view->window_child_focus->window, (GdkEvent *)new_event);
4200 gdk_event_free ((GdkEvent *)new_event);
4208 return gtk_bindings_activate_event (G_OBJECT (widget), event);
4212 ev_view_focus_in (GtkWidget *widget,
4213 GdkEventFocus *event)
4215 if (EV_VIEW (widget)->pixbuf_cache)
4216 ev_pixbuf_cache_style_changed (EV_VIEW (widget)->pixbuf_cache);
4217 gtk_widget_queue_draw (widget);
4223 ev_view_focus_out (GtkWidget *widget,
4224 GdkEventFocus *event)
4226 if (EV_VIEW (widget)->pixbuf_cache)
4227 ev_pixbuf_cache_style_changed (EV_VIEW (widget)->pixbuf_cache);
4228 gtk_widget_queue_draw (widget);
4234 ev_view_leave_notify_event (GtkWidget *widget, GdkEventCrossing *event)
4236 EvView *view = EV_VIEW (widget);
4238 if (view->cursor != EV_VIEW_CURSOR_NORMAL)
4239 ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL);
4245 ev_view_enter_notify_event (GtkWidget *widget, GdkEventCrossing *event)
4247 EvView *view = EV_VIEW (widget);
4249 ev_view_handle_cursor_over_xy (view, event->x, event->y);
4255 ev_view_style_updated (GtkWidget *widget)
4257 if (EV_VIEW (widget)->pixbuf_cache)
4258 ev_pixbuf_cache_style_changed (EV_VIEW (widget)->pixbuf_cache);
4260 GTK_WIDGET_CLASS (ev_view_parent_class)->style_updated (widget);
4266 draw_rubberband (EvView *view,
4268 const GdkRectangle *rect,
4271 GtkStyleContext *context;
4274 context = gtk_widget_get_style_context (GTK_WIDGET (view));
4275 gtk_style_context_get_background_color (context, GTK_STATE_FLAG_SELECTED, &color);
4278 cairo_set_source_rgba (cr, color.red, color.green, color.blue, alpha);
4279 cairo_rectangle (cr,
4280 rect->x - view->scroll_x,
4281 rect->y - view->scroll_y,
4282 rect->width, rect->height);
4283 cairo_fill_preserve (cr);
4285 cairo_set_line_width (cr, 0.5);
4286 cairo_set_source_rgb (cr, color.red, color.green, color.blue);
4294 highlight_find_results (EvView *view,
4298 gint i, n_results = 0;
4299 n_results = ev_view_find_get_n_results (view, page);
4301 for (i = 0; i < n_results; i++) {
4302 EvRectangle *rectangle;
4303 GdkRectangle view_rectangle;
4306 if (i == view->find_result && page == view->current_page) {
4312 rectangle = ev_view_find_get_result (view, page, i);
4313 doc_rect_to_view_rect (view, page, rectangle, &view_rectangle);
4314 draw_rubberband (view, cr, &view_rectangle, alpha);
4319 highlight_forward_search_results (EvView *view,
4324 EvMapping *mapping = view->synctex_result;
4326 if (GPOINTER_TO_INT (mapping->data) != page)
4329 doc_rect_to_view_rect (view, page, &mapping->area, &rect);
4332 cairo_set_source_rgb (cr, 1., 0., 0.);
4333 cairo_rectangle (cr,
4334 rect.x - view->scroll_x,
4335 rect.y - view->scroll_y,
4336 rect.width, rect.height);
4342 focus_annotation (EvView *view,
4347 GtkWidget *widget = GTK_WIDGET (view);
4349 EvMapping *mapping = view->focus_annotation;
4350 EvAnnotation *annot = (EvAnnotation *)mapping->data;
4352 if (ev_annotation_get_page_index (annot) != page)
4355 doc_rect_to_view_rect (view, page, &mapping->area, &rect);
4357 gtk_render_focus (gtk_widget_get_style_context (widget),
4359 rect.x - view->scroll_x,
4360 rect.y - view->scroll_y,
4361 rect.width + 1, rect.height + 1);
4365 ev_view_loading_window_move (EvView *view)
4367 GtkWidget *widget = GTK_WIDGET (view);
4368 EvLoadingWindow *window = EV_LOADING_WINDOW (view->loading_window);
4369 gint root_x, root_y;
4371 GtkAllocation allocation;
4373 gtk_widget_get_allocation (widget, &allocation);
4374 gdk_window_get_origin (gtk_widget_get_window (widget), &root_x, &root_y);
4375 ev_loading_window_get_size (window, &window_width, NULL);
4377 root_x += allocation.width - window_width - 10;
4380 ev_loading_window_move (window, root_x, root_y);
4384 show_loading_window_cb (EvView *view)
4386 if (!view->loading_window) {
4390 parent = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (view)));
4391 view->loading_window = ev_loading_window_new (parent);
4393 /* Show the window off screen to get a valid size asap */
4394 screen = gtk_widget_get_screen (GTK_WIDGET (view));
4395 gtk_window_move (GTK_WINDOW (view->loading_window),
4396 gdk_screen_get_width (screen) + 1,
4397 gdk_screen_get_height (screen) + 1);
4398 gtk_widget_show (view->loading_window);
4401 ev_view_loading_window_move (view);
4403 gtk_widget_show (view->loading_window);
4405 view->loading_timeout = 0;
4411 show_loading_window (EvView *view)
4413 if (view->loading_window && gtk_widget_get_visible (view->loading_window)) {
4414 ev_view_loading_window_move (view);
4418 if (!view->loading_timeout) {
4419 view->loading_timeout =
4420 g_timeout_add_full (G_PRIORITY_LOW,
4421 0.5, (GSourceFunc)show_loading_window_cb,
4427 hide_loading_window (EvView *view)
4429 if (view->loading_timeout) {
4430 g_source_remove (view->loading_timeout);
4431 view->loading_timeout = 0;
4434 if (view->loading_window && gtk_widget_get_visible (view->loading_window)) {
4435 gtk_widget_hide (view->loading_window);
4440 draw_one_page (EvView *view,
4443 GdkRectangle *page_area,
4445 GdkRectangle *expose_area,
4446 gboolean *page_ready)
4448 GdkRectangle overlap;
4449 GdkRectangle real_page_area;
4451 gboolean inverted_colors;
4453 g_assert (view->document);
4455 if (! gdk_rectangle_intersect (page_area, expose_area, &overlap))
4458 /* Render the document itself */
4459 real_page_area = *page_area;
4461 real_page_area.x += border->left;
4462 real_page_area.y += border->top;
4463 real_page_area.width -= (border->left + border->right);
4464 real_page_area.height -= (border->top + border->bottom);
4467 current_page = ev_document_model_get_page (view->model);
4468 inverted_colors = ev_document_model_get_inverted_colors (view->model);
4469 ev_document_misc_paint_one_page (cr,
4472 page == current_page,
4475 if (gdk_rectangle_intersect (&real_page_area, expose_area, &overlap)) {
4477 gint page_width, page_height;
4478 cairo_surface_t *page_surface = NULL;
4479 gint selection_width, selection_height;
4480 cairo_surface_t *selection_surface = NULL;
4482 page_surface = ev_pixbuf_cache_get_surface (view->pixbuf_cache, page);
4484 if (!page_surface) {
4485 if (page == current_page)
4486 show_loading_window (view);
4488 *page_ready = FALSE;
4493 if (page == current_page)
4494 hide_loading_window (view);
4496 ev_view_get_page_size (view, page, &width, &height);
4498 page_width = cairo_image_surface_get_width (page_surface);
4499 page_height = cairo_image_surface_get_height (page_surface);
4502 cairo_translate (cr, overlap.x, overlap.y);
4504 if (width != page_width || height != page_height) {
4505 cairo_pattern_set_filter (cairo_get_source (cr),
4508 (gdouble)width / page_width,
4509 (gdouble)height / page_height);
4512 cairo_surface_set_device_offset (page_surface,
4513 overlap.x - real_page_area.x,
4514 overlap.y - real_page_area.y);
4515 cairo_set_source_surface (cr, page_surface, 0, 0);
4519 /* Get the selection pixbuf iff we have something to draw */
4520 if (find_selection_for_page (view, page) &&
4521 view->selection_mode == EV_VIEW_SELECTION_TEXT) {
4523 ev_pixbuf_cache_get_selection_surface (view->pixbuf_cache,
4529 if (!selection_surface) {
4533 selection_width = cairo_image_surface_get_width (selection_surface);
4534 selection_height = cairo_image_surface_get_height (selection_surface);
4537 cairo_translate (cr, overlap.x, overlap.y);
4539 if (width != selection_width || height != selection_height) {
4540 cairo_pattern_set_filter (cairo_get_source (cr),
4543 (gdouble)width / selection_width,
4544 (gdouble)height / selection_height);
4547 cairo_surface_set_device_offset (selection_surface,
4548 overlap.x - real_page_area.x,
4549 overlap.y - real_page_area.y);
4551 cairo_set_source_surface (cr, selection_surface, 0, 0);
4557 /*** GObject functions ***/
4560 ev_view_finalize (GObject *object)
4562 EvView *view = EV_VIEW (object);
4564 clear_selection (view);
4565 clear_link_selected (view);
4567 if (view->synctex_result) {
4568 g_free (view->synctex_result);
4569 view->synctex_result = NULL;
4572 if (view->image_dnd_info.image)
4573 g_object_unref (view->image_dnd_info.image);
4574 view->image_dnd_info.image = NULL;
4576 G_OBJECT_CLASS (ev_view_parent_class)->finalize (object);
4580 ev_view_dispose (GObject *object)
4582 EvView *view = EV_VIEW (object);
4585 g_object_unref (view->model);
4589 if (view->pixbuf_cache) {
4590 g_object_unref (view->pixbuf_cache);
4591 view->pixbuf_cache = NULL;
4594 if (view->document) {
4595 g_object_unref (view->document);
4596 view->document = NULL;
4599 if (view->page_cache) {
4600 g_object_unref (view->page_cache);
4601 view->page_cache = NULL;
4604 ev_view_window_children_free (view);
4606 if (view->selection_scroll_id) {
4607 g_source_remove (view->selection_scroll_id);
4608 view->selection_scroll_id = 0;
4611 if (view->selection_update_id) {
4612 g_source_remove (view->selection_update_id);
4613 view->selection_update_id = 0;
4616 if (view->scroll_info.timeout_id) {
4617 g_source_remove (view->scroll_info.timeout_id);
4618 view->scroll_info.timeout_id = 0;
4621 if (view->drag_info.drag_timeout_id) {
4622 g_source_remove (view->drag_info.drag_timeout_id);
4623 view->drag_info.drag_timeout_id = 0;
4626 if (view->drag_info.release_timeout_id) {
4627 g_source_remove (view->drag_info.release_timeout_id);
4628 view->drag_info.release_timeout_id = 0;
4631 if (view->loading_timeout) {
4632 g_source_remove (view->loading_timeout);
4633 view->loading_timeout = 0;
4636 gtk_scrollable_set_hadjustment (GTK_SCROLLABLE (view), NULL);
4637 gtk_scrollable_set_vadjustment (GTK_SCROLLABLE (view), NULL);
4639 G_OBJECT_CLASS (ev_view_parent_class)->dispose (object);
4643 ev_view_get_property (GObject *object,
4648 EvView *view = EV_VIEW (object);
4651 case PROP_HADJUSTMENT:
4652 g_value_set_object (value, view->hadjustment);
4654 case PROP_VADJUSTMENT:
4655 g_value_set_object (value, view->vadjustment);
4657 case PROP_HSCROLL_POLICY:
4658 g_value_set_enum (value, view->hscroll_policy);
4660 case PROP_VSCROLL_POLICY:
4661 g_value_set_enum (value, view->vscroll_policy);
4664 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
4670 ev_view_set_property (GObject *object,
4672 const GValue *value,
4675 EvView *view = EV_VIEW (object);
4678 case PROP_HADJUSTMENT:
4679 ev_view_set_scroll_adjustment (view, GTK_ORIENTATION_HORIZONTAL,
4680 (GtkAdjustment *) g_value_get_object (value));
4682 case PROP_VADJUSTMENT:
4683 ev_view_set_scroll_adjustment (view, GTK_ORIENTATION_VERTICAL,
4684 (GtkAdjustment *) g_value_get_object (value));
4686 case PROP_HSCROLL_POLICY:
4687 view->hscroll_policy = g_value_get_enum (value);
4688 gtk_widget_queue_resize (GTK_WIDGET (view));
4690 case PROP_VSCROLL_POLICY:
4691 view->vscroll_policy = g_value_get_enum (value);
4692 gtk_widget_queue_resize (GTK_WIDGET (view));
4695 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
4702 ev_view_init_accessibility (EvView *view)
4704 static gboolean first_time = TRUE;
4707 AtkObjectFactory *factory;
4708 AtkRegistry *registry;
4710 GType derived_atk_type;
4713 * Figure out whether accessibility is enabled by looking at the
4714 * type of the accessible object which would be created for
4715 * the parent type of EvView.
4717 derived_type = g_type_parent (EV_TYPE_VIEW);
4719 registry = atk_get_default_registry ();
4720 factory = atk_registry_get_factory (registry,
4722 derived_atk_type = atk_object_factory_get_accessible_type (factory);
4723 if (g_type_is_a (derived_atk_type, GTK_TYPE_ACCESSIBLE)) {
4724 atk_registry_set_factory_type (registry,
4726 ev_view_accessible_factory_get_type ());
4727 view->a11y_enabled = TRUE;
4734 ev_view_get_accessible (GtkWidget *widget)
4736 ev_view_init_accessibility (EV_VIEW (widget));
4737 return GTK_WIDGET_CLASS (ev_view_parent_class)->get_accessible (widget);
4741 ev_view_is_a11y_enabled (EvView *view)
4743 ev_view_init_accessibility (view);
4744 return view->a11y_enabled;
4749 ev_view_remove (GtkContainer *container,
4752 EvView *view = EV_VIEW (container);
4753 GList *tmp_list = view->children;
4757 child = tmp_list->data;
4759 if (child->widget == widget) {
4760 gtk_widget_unparent (widget);
4762 view->children = g_list_remove_link (view->children, tmp_list);
4763 g_list_free_1 (tmp_list);
4764 g_slice_free (EvViewChild, child);
4769 tmp_list = tmp_list->next;
4774 ev_view_forall (GtkContainer *container,
4775 gboolean include_internals,
4776 GtkCallback callback,
4777 gpointer callback_data)
4779 EvView *view = EV_VIEW (container);
4780 GList *tmp_list = view->children;
4784 child = tmp_list->data;
4785 tmp_list = tmp_list->next;
4787 (* callback) (child->widget, callback_data);
4792 ev_view_class_init (EvViewClass *class)
4794 GObjectClass *object_class = G_OBJECT_CLASS (class);
4795 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
4796 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
4797 GtkBindingSet *binding_set;
4799 object_class->get_property = ev_view_get_property;
4800 object_class->set_property = ev_view_set_property;
4801 object_class->dispose = ev_view_dispose;
4802 object_class->finalize = ev_view_finalize;
4804 widget_class->realize = ev_view_realize;
4805 widget_class->draw = ev_view_draw;
4806 widget_class->button_press_event = ev_view_button_press_event;
4807 widget_class->motion_notify_event = ev_view_motion_notify_event;
4808 widget_class->button_release_event = ev_view_button_release_event;
4809 widget_class->key_press_event = ev_view_key_press_event;
4810 widget_class->focus_in_event = ev_view_focus_in;
4811 widget_class->focus_out_event = ev_view_focus_out;
4812 widget_class->get_accessible = ev_view_get_accessible;
4813 widget_class->get_preferred_width = ev_view_get_preferred_width;
4814 widget_class->get_preferred_height = ev_view_get_preferred_height;
4815 widget_class->size_allocate = ev_view_size_allocate;
4816 widget_class->scroll_event = ev_view_scroll_event;
4817 widget_class->enter_notify_event = ev_view_enter_notify_event;
4818 widget_class->leave_notify_event = ev_view_leave_notify_event;
4819 widget_class->style_updated = ev_view_style_updated;
4820 widget_class->drag_data_get = ev_view_drag_data_get;
4821 widget_class->drag_motion = ev_view_drag_motion;
4822 widget_class->popup_menu = ev_view_popup_menu;
4823 widget_class->query_tooltip = ev_view_query_tooltip;
4825 container_class->remove = ev_view_remove;
4826 container_class->forall = ev_view_forall;
4828 class->binding_activated = ev_view_scroll;
4830 /* Scrollable interface */
4831 g_object_class_override_property (object_class, PROP_HADJUSTMENT, "hadjustment");
4832 g_object_class_override_property (object_class, PROP_VADJUSTMENT, "vadjustment");
4833 g_object_class_override_property (object_class, PROP_HSCROLL_POLICY, "hscroll-policy");
4834 g_object_class_override_property (object_class, PROP_VSCROLL_POLICY, "vscroll-policy");
4836 signals[SIGNAL_BINDING_ACTIVATED] = g_signal_new ("binding_activated",
4837 G_TYPE_FROM_CLASS (object_class),
4838 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
4839 G_STRUCT_OFFSET (EvViewClass, binding_activated),
4841 ev_view_marshal_VOID__ENUM_BOOLEAN,
4843 GTK_TYPE_SCROLL_TYPE,
4845 signals[SIGNAL_HANDLE_LINK] = g_signal_new ("handle-link",
4846 G_TYPE_FROM_CLASS (object_class),
4847 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
4848 G_STRUCT_OFFSET (EvViewClass, handle_link),
4850 g_cclosure_marshal_VOID__OBJECT,
4853 signals[SIGNAL_EXTERNAL_LINK] = g_signal_new ("external-link",
4854 G_TYPE_FROM_CLASS (object_class),
4855 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
4856 G_STRUCT_OFFSET (EvViewClass, external_link),
4858 g_cclosure_marshal_VOID__OBJECT,
4861 signals[SIGNAL_POPUP_MENU] = g_signal_new ("popup",
4862 G_TYPE_FROM_CLASS (object_class),
4863 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
4864 G_STRUCT_OFFSET (EvViewClass, popup_menu),
4866 g_cclosure_marshal_VOID__POINTER,
4869 signals[SIGNAL_SELECTION_CHANGED] = g_signal_new ("selection-changed",
4870 G_TYPE_FROM_CLASS (object_class),
4871 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
4872 G_STRUCT_OFFSET (EvViewClass, selection_changed),
4874 g_cclosure_marshal_VOID__VOID,
4877 signals[SIGNAL_SYNC_SOURCE] = g_signal_new ("sync-source",
4878 G_TYPE_FROM_CLASS (object_class),
4879 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
4880 G_STRUCT_OFFSET (EvViewClass, sync_source),
4882 g_cclosure_marshal_VOID__POINTER,
4885 signals[SIGNAL_ANNOT_ADDED] = g_signal_new ("annot-added",
4886 G_TYPE_FROM_CLASS (object_class),
4887 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
4888 G_STRUCT_OFFSET (EvViewClass, annot_added),
4890 g_cclosure_marshal_VOID__OBJECT,
4892 EV_TYPE_ANNOTATION);
4893 signals[SIGNAL_LAYERS_CHANGED] = g_signal_new ("layers-changed",
4894 G_TYPE_FROM_CLASS (object_class),
4895 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
4896 G_STRUCT_OFFSET (EvViewClass, layers_changed),
4898 g_cclosure_marshal_VOID__VOID,
4902 binding_set = gtk_binding_set_by_class (class);
4904 add_scroll_binding_keypad (binding_set, GDK_KEY_Left, 0, GTK_SCROLL_STEP_BACKWARD, TRUE);
4905 add_scroll_binding_keypad (binding_set, GDK_KEY_Right, 0, GTK_SCROLL_STEP_FORWARD, TRUE);
4906 add_scroll_binding_keypad (binding_set, GDK_KEY_Left, GDK_MOD1_MASK, GTK_SCROLL_STEP_DOWN, TRUE);
4907 add_scroll_binding_keypad (binding_set, GDK_KEY_Right, GDK_MOD1_MASK, GTK_SCROLL_STEP_UP, TRUE);
4908 add_scroll_binding_keypad (binding_set, GDK_KEY_Up, 0, GTK_SCROLL_STEP_BACKWARD, FALSE);
4909 add_scroll_binding_keypad (binding_set, GDK_KEY_Down, 0, GTK_SCROLL_STEP_FORWARD, FALSE);
4910 add_scroll_binding_keypad (binding_set, GDK_KEY_Up, GDK_MOD1_MASK, GTK_SCROLL_STEP_DOWN, FALSE);
4911 add_scroll_binding_keypad (binding_set, GDK_KEY_Down, GDK_MOD1_MASK, GTK_SCROLL_STEP_UP, FALSE);
4912 gtk_binding_entry_add_signal (binding_set, GDK_KEY_H, 0, "binding_activated", 2, GTK_TYPE_SCROLL_TYPE,
4913 GTK_SCROLL_STEP_BACKWARD, G_TYPE_BOOLEAN, TRUE);
4914 gtk_binding_entry_add_signal (binding_set, GDK_KEY_J, 0, "binding_activated", 2, GTK_TYPE_SCROLL_TYPE,
4915 GTK_SCROLL_STEP_FORWARD, G_TYPE_BOOLEAN, FALSE);
4916 gtk_binding_entry_add_signal (binding_set, GDK_KEY_K, 0, "binding_activated", 2, GTK_TYPE_SCROLL_TYPE,
4917 GTK_SCROLL_STEP_BACKWARD, G_TYPE_BOOLEAN, FALSE);
4918 gtk_binding_entry_add_signal (binding_set, GDK_KEY_L, 0, "binding_activated", 2, GTK_TYPE_SCROLL_TYPE,
4919 GTK_SCROLL_STEP_FORWARD, G_TYPE_BOOLEAN, TRUE);
4924 ev_view_init (EvView *view)
4926 gtk_widget_set_has_window (GTK_WIDGET (view), TRUE);
4927 gtk_widget_set_can_focus (GTK_WIDGET (view), TRUE);
4928 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (view), FALSE);
4929 gtk_container_set_resize_mode (GTK_CONTAINER (view), GTK_RESIZE_QUEUE);
4931 gtk_widget_set_events (GTK_WIDGET (view),
4933 GDK_BUTTON_PRESS_MASK |
4934 GDK_BUTTON_RELEASE_MASK |
4936 GDK_KEY_PRESS_MASK |
4937 GDK_POINTER_MOTION_MASK |
4938 GDK_POINTER_MOTION_HINT_MASK |
4939 GDK_ENTER_NOTIFY_MASK |
4940 GDK_LEAVE_NOTIFY_MASK);
4942 view->start_page = -1;
4943 view->end_page = -1;
4946 view->current_page = 0;
4947 view->pressed_button = -1;
4948 view->cursor = EV_VIEW_CURSOR_NORMAL;
4949 view->drag_info.in_drag = FALSE;
4950 view->scroll_info.autoscrolling = FALSE;
4951 view->selection_info.selections = NULL;
4952 view->selection_info.in_selection = FALSE;
4953 view->selection_info.in_drag = FALSE;
4954 view->selection_mode = EV_VIEW_SELECTION_TEXT;
4955 view->continuous = TRUE;
4956 view->dual_page = FALSE;
4957 view->fullscreen = FALSE;
4958 view->sizing_mode = EV_SIZING_FIT_WIDTH;
4959 view->pending_scroll = SCROLL_TO_KEEP_POSITION;
4960 view->jump_to_find_result = TRUE;
4961 view->highlight_find_results = FALSE;
4967 ev_view_change_page (EvView *view,
4972 view->current_page = new_page;
4973 view->pending_scroll = SCROLL_TO_PAGE_POSITION;
4975 hide_loading_window (view);
4977 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
4978 ev_view_handle_cursor_over_xy (view, x, y);
4980 gtk_widget_queue_resize (GTK_WIDGET (view));
4984 job_finished_cb (EvPixbufCache *pixbuf_cache,
4985 cairo_region_t *region,
4989 gdk_window_invalidate_region (gtk_widget_get_window (GTK_WIDGET (view)), region, TRUE);
4991 gtk_widget_queue_draw (GTK_WIDGET (view));
4996 ev_view_page_changed_cb (EvDocumentModel *model,
5001 if (!view->document)
5004 if (view->current_page != new_page) {
5005 ev_view_change_page (view, new_page);
5007 gtk_widget_queue_draw (GTK_WIDGET (view));
5010 view->find_result = 0;
5014 on_adjustment_value_changed (GtkAdjustment *adjustment,
5017 GtkWidget *widget = GTK_WIDGET (view);
5023 if (!gtk_widget_get_realized (widget))
5026 if (view->hadjustment) {
5027 value = (gint) gtk_adjustment_get_value (view->hadjustment);
5028 dx = view->scroll_x - value;
5029 view->scroll_x = value;
5034 if (view->vadjustment) {
5035 value = (gint) gtk_adjustment_get_value (view->vadjustment);
5036 dy = view->scroll_y - value;
5037 view->scroll_y = value;
5042 for (l = view->children; l && l->data; l = g_list_next (l)) {
5043 EvViewChild *child = (EvViewChild *)l->data;
5047 if (gtk_widget_get_visible (child->widget) && gtk_widget_get_visible (widget))
5048 gtk_widget_queue_resize (widget);
5051 for (l = view->window_children; l && l->data; l = g_list_next (l)) {
5052 EvViewWindowChild *child;
5054 child = (EvViewWindowChild *)l->data;
5056 ev_view_window_child_move (view, child, child->x + dx, child->y + dy);
5059 if (view->pending_resize) {
5060 gtk_widget_queue_draw (widget);
5062 gdk_window_scroll (gtk_widget_get_window (widget), dx, dy);
5065 gtk_widget_get_pointer (widget, &x, &y);
5066 ev_view_handle_cursor_over_xy (view, x, y);
5069 view_update_range_and_current_page (view);
5077 view = g_object_new (EV_TYPE_VIEW, NULL);
5083 setup_caches (EvView *view)
5085 gboolean inverted_colors;
5087 view->height_to_page_cache = ev_view_get_height_to_page_cache (view);
5088 view->pixbuf_cache = ev_pixbuf_cache_new (GTK_WIDGET (view), view->model, view->pixbuf_cache_size);
5089 view->page_cache = ev_page_cache_new (view->document);
5090 if (ev_view_is_a11y_enabled (view)) {
5091 EvJobPageDataFlags flags = ev_page_cache_get_flags (view->page_cache);
5093 ev_page_cache_set_flags (view->page_cache,
5095 EV_PAGE_DATA_INCLUDE_TEXT_LAYOUT |
5096 EV_PAGE_DATA_INCLUDE_TEXT);
5098 inverted_colors = ev_document_model_get_inverted_colors (view->model);
5099 ev_pixbuf_cache_set_inverted_colors (view->pixbuf_cache, inverted_colors);
5100 g_signal_connect (view->pixbuf_cache, "job-finished", G_CALLBACK (job_finished_cb), view);
5104 clear_caches (EvView *view)
5106 if (view->pixbuf_cache) {
5107 g_object_unref (view->pixbuf_cache);
5108 view->pixbuf_cache = NULL;
5111 if (view->page_cache) {
5112 g_object_unref (view->page_cache);
5113 view->page_cache = NULL;
5118 * ev_view_set_page_cache_size:
5122 * Sets the maximum size in bytes that will be used to cache
5123 * rendered pages. Use 0 to disable caching rendered pages.
5125 * Note that this limit doesn't affect the current visible page range,
5126 * which will always be rendered. In order to limit the total memory used
5127 * you have to use ev_document_model_set_max_scale() too.
5131 ev_view_set_page_cache_size (EvView *view,
5134 if (view->pixbuf_cache_size == cache_size)
5137 view->pixbuf_cache_size = cache_size;
5138 if (view->pixbuf_cache)
5139 ev_pixbuf_cache_set_max_size (view->pixbuf_cache, cache_size);
5143 ev_view_set_loading (EvView *view,
5146 if (view->loading && !loading)
5147 hide_loading_window (view);
5148 view->loading = loading;
5149 gtk_widget_queue_draw (GTK_WIDGET (view));
5153 ev_view_autoscroll_cb (EvView *view)
5155 gdouble speed, value;
5157 /* If the user stops autoscrolling, autoscrolling will be
5158 * set to false but the timeout will continue; stop the timeout: */
5159 if (!view->scroll_info.autoscrolling) {
5160 view->scroll_info.timeout_id = 0;
5164 if (view->scroll_info.last_y > view->scroll_info.start_y &&
5165 (view->scroll_info.last_y < view->scroll_info.start_y))
5168 /* Replace 100 with your speed of choice: The lower the faster.
5169 * Replace 3 with another speed of choice: The higher, the faster it accelerated
5170 * based on the distance of the starting point from the mouse
5171 * (All also effected by the timeout interval of this callback) */
5173 if (view->scroll_info.start_y > view->scroll_info.last_y)
5174 speed = -pow ((((gdouble)view->scroll_info.start_y - view->scroll_info.last_y) / 100), 3);
5176 speed = pow ((((gdouble)view->scroll_info.last_y - view->scroll_info.start_y) / 100), 3);
5178 value = gtk_adjustment_get_value (view->vadjustment);
5179 value = CLAMP (value + speed, 0,
5180 gtk_adjustment_get_upper (view->vadjustment) -
5181 gtk_adjustment_get_page_size (view->vadjustment));
5182 gtk_adjustment_set_value (view->vadjustment, value);
5189 ev_view_autoscroll_start (EvView *view)
5193 g_return_if_fail (EV_IS_VIEW (view));
5195 if (view->scroll_info.autoscrolling)
5198 view->scroll_info.autoscrolling = TRUE;
5199 view->scroll_info.timeout_id =
5200 g_timeout_add (20, (GSourceFunc)ev_view_autoscroll_cb,
5203 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
5204 ev_view_handle_cursor_over_xy (view, x, y);
5208 ev_view_autoscroll_stop (EvView *view)
5212 g_return_if_fail (EV_IS_VIEW (view));
5214 if (!view->scroll_info.autoscrolling)
5217 view->scroll_info.autoscrolling = FALSE;
5218 if (view->scroll_info.timeout_id) {
5219 g_source_remove (view->scroll_info.timeout_id);
5220 view->scroll_info.timeout_id = 0;
5223 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
5224 ev_view_handle_cursor_over_xy (view, x, y);
5228 ev_view_document_changed_cb (EvDocumentModel *model,
5232 EvDocument *document = ev_document_model_get_document (model);
5234 if (document != view->document) {
5237 clear_caches (view);
5238 ev_view_remove_all (view);
5240 if (view->document) {
5241 g_object_unref (view->document);
5244 view->document = document;
5245 view->find_result = 0;
5247 if (view->document) {
5248 view->loading = FALSE;
5249 g_object_ref (view->document);
5250 setup_caches (view);
5253 current_page = ev_document_model_get_page (model);
5254 if (view->current_page != current_page) {
5255 ev_view_change_page (view, current_page);
5257 view->pending_scroll = SCROLL_TO_KEEP_POSITION;
5258 gtk_widget_queue_resize (GTK_WIDGET (view));
5264 ev_view_rotation_changed_cb (EvDocumentModel *model,
5268 gint rotation = ev_document_model_get_rotation (model);
5270 view->rotation = rotation;
5272 if (view->pixbuf_cache) {
5273 ev_pixbuf_cache_clear (view->pixbuf_cache);
5274 if (!ev_document_is_page_size_uniform (view->document))
5275 view->pending_scroll = SCROLL_TO_PAGE_POSITION;
5276 gtk_widget_queue_resize (GTK_WIDGET (view));
5279 ev_view_remove_all (view);
5282 clear_selection (view);
5286 ev_view_inverted_colors_changed_cb (EvDocumentModel *model,
5290 if (view->pixbuf_cache) {
5291 gboolean inverted_colors;
5293 inverted_colors = ev_document_model_get_inverted_colors (model);
5294 ev_pixbuf_cache_set_inverted_colors (view->pixbuf_cache, inverted_colors);
5295 gtk_widget_queue_draw (GTK_WIDGET (view));
5300 ev_view_sizing_mode_changed_cb (EvDocumentModel *model,
5304 EvSizingMode mode = ev_document_model_get_sizing_mode (model);
5306 view->sizing_mode = mode;
5307 if (mode != EV_SIZING_FREE)
5308 gtk_widget_queue_resize (GTK_WIDGET (view));
5311 #define EPSILON 0.0000001
5313 ev_view_scale_changed_cb (EvDocumentModel *model,
5317 gdouble scale = ev_document_model_get_scale (model);
5319 if (ABS (view->scale - scale) < EPSILON)
5322 view->scale = scale;
5324 view->pending_resize = TRUE;
5325 if (view->sizing_mode == EV_SIZING_FREE)
5326 gtk_widget_queue_resize (GTK_WIDGET (view));
5330 ev_view_continuous_changed_cb (EvDocumentModel *model,
5334 gboolean continuous = ev_document_model_get_continuous (model);
5336 view->continuous = continuous;
5337 view->pending_scroll = SCROLL_TO_PAGE_POSITION;
5338 gtk_widget_queue_resize (GTK_WIDGET (view));
5342 ev_view_dual_page_changed_cb (EvDocumentModel *model,
5346 gboolean dual_page = ev_document_model_get_dual_page (model);
5348 view->dual_page = dual_page;
5349 view->pending_scroll = SCROLL_TO_PAGE_POSITION;
5350 /* FIXME: if we're keeping the pixbuf cache around, we should extend the
5351 * preload_cache_size to be 2 if dual_page is set.
5353 gtk_widget_queue_resize (GTK_WIDGET (view));
5357 ev_view_fullscreen_changed_cb (EvDocumentModel *model,
5361 gboolean fullscreen = ev_document_model_get_fullscreen (model);
5363 view->fullscreen = fullscreen;
5364 gtk_widget_queue_resize (GTK_WIDGET (view));
5368 ev_view_set_model (EvView *view,
5369 EvDocumentModel *model)
5371 g_return_if_fail (EV_IS_VIEW (view));
5372 g_return_if_fail (EV_IS_DOCUMENT_MODEL (model));
5374 if (model == view->model)
5378 g_signal_handlers_disconnect_by_func (view->model,
5379 ev_view_document_changed_cb,
5381 g_signal_handlers_disconnect_by_func (view->model,
5382 ev_view_page_changed_cb,
5384 g_object_unref (view->model);
5386 view->model = g_object_ref (model);
5388 /* Initialize view from model */
5389 view->rotation = ev_document_model_get_rotation (view->model);
5390 view->sizing_mode = ev_document_model_get_sizing_mode (view->model);
5391 view->scale = ev_document_model_get_scale (view->model);
5392 view->continuous = ev_document_model_get_continuous (view->model);
5393 view->dual_page = ev_document_model_get_dual_page (view->model);
5394 view->fullscreen = ev_document_model_get_fullscreen (view->model);
5395 ev_view_document_changed_cb (view->model, NULL, view);
5397 g_signal_connect (view->model, "notify::document",
5398 G_CALLBACK (ev_view_document_changed_cb),
5400 g_signal_connect (view->model, "notify::rotation",
5401 G_CALLBACK (ev_view_rotation_changed_cb),
5403 g_signal_connect (view->model, "notify::inverted-colors",
5404 G_CALLBACK (ev_view_inverted_colors_changed_cb),
5406 g_signal_connect (view->model, "notify::sizing-mode",
5407 G_CALLBACK (ev_view_sizing_mode_changed_cb),
5409 g_signal_connect (view->model, "notify::scale",
5410 G_CALLBACK (ev_view_scale_changed_cb),
5412 g_signal_connect (view->model, "notify::continuous",
5413 G_CALLBACK (ev_view_continuous_changed_cb),
5415 g_signal_connect (view->model, "notify::dual-page",
5416 G_CALLBACK (ev_view_dual_page_changed_cb),
5418 g_signal_connect (view->model, "notify::fullscreen",
5419 G_CALLBACK (ev_view_fullscreen_changed_cb),
5421 g_signal_connect (view->model, "page-changed",
5422 G_CALLBACK (ev_view_page_changed_cb),
5427 ev_view_reload_page (EvView *view,
5429 cairo_region_t *region)
5431 ev_pixbuf_cache_reload_page (view->pixbuf_cache,
5439 ev_view_reload (EvView *view)
5441 ev_pixbuf_cache_clear (view->pixbuf_cache);
5442 view_update_range_and_current_page (view);
5445 /*** Zoom and sizing mode ***/
5448 ev_view_can_zoom_in (EvView *view)
5450 return view->scale * ZOOM_IN_FACTOR <= ev_document_model_get_max_scale (view->model);
5454 ev_view_can_zoom_out (EvView *view)
5456 return view->scale * ZOOM_OUT_FACTOR >= ev_document_model_get_min_scale (view->model);
5460 ev_view_zoom_in (EvView *view)
5464 g_return_if_fail (view->sizing_mode == EV_SIZING_FREE);
5466 view->pending_scroll = SCROLL_TO_CENTER;
5467 scale = ev_document_model_get_scale (view->model) * ZOOM_IN_FACTOR;
5468 ev_document_model_set_scale (view->model, scale);
5472 ev_view_zoom_out (EvView *view)
5476 g_return_if_fail (view->sizing_mode == EV_SIZING_FREE);
5478 view->pending_scroll = SCROLL_TO_CENTER;
5479 scale = ev_document_model_get_scale (view->model) * ZOOM_OUT_FACTOR;
5480 ev_document_model_set_scale (view->model, scale);
5484 zoom_for_size_fit_width (gdouble doc_width,
5489 return (double)target_width / doc_width;
5493 zoom_for_size_fit_height (gdouble doc_width,
5498 return (double)target_height / doc_height;
5502 zoom_for_size_best_fit (gdouble doc_width,
5510 w_scale = (double)target_width / doc_width;
5511 h_scale = (double)target_height / doc_height;
5513 return MIN (w_scale, h_scale);
5517 ev_view_zoom_for_size_continuous_and_dual_page (EvView *view,
5521 gdouble doc_width, doc_height;
5526 ev_document_get_max_page_size (view->document, &doc_width, &doc_height);
5527 if (view->rotation == 90 || view->rotation == 270) {
5531 doc_width = doc_height;
5535 compute_border (view, doc_width, doc_height, &border);
5538 width -= (2 * (border.left + border.right) + 3 * view->spacing);
5539 height -= (border.top + border.bottom + 2 * view->spacing - 1);
5541 sb_size = ev_view_get_scrollbar_size (view, GTK_ORIENTATION_VERTICAL);
5543 if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
5544 scale = zoom_for_size_fit_width (doc_width, doc_height, width - sb_size, height);
5545 } else if (view->sizing_mode == EV_SIZING_BEST_FIT)
5546 scale = zoom_for_size_best_fit (doc_width, doc_height, width - sb_size, height);
5548 g_assert_not_reached ();
5550 ev_document_model_set_scale (view->model, scale);
5554 ev_view_zoom_for_size_continuous (EvView *view,
5558 gdouble doc_width, doc_height;
5563 ev_document_get_max_page_size (view->document, &doc_width, &doc_height);
5564 if (view->rotation == 90 || view->rotation == 270) {
5568 doc_width = doc_height;
5572 compute_border (view, doc_width, doc_height, &border);
5574 width -= (border.left + border.right + 2 * view->spacing);
5575 height -= (border.top + border.bottom + 2 * view->spacing - 1);
5577 sb_size = ev_view_get_scrollbar_size (view, GTK_ORIENTATION_VERTICAL);
5579 if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
5580 scale = zoom_for_size_fit_width (doc_width, doc_height, width - sb_size, height);
5581 } else if (view->sizing_mode == EV_SIZING_BEST_FIT)
5582 scale = zoom_for_size_best_fit (doc_width, doc_height, width - sb_size, height);
5584 g_assert_not_reached ();
5586 ev_document_model_set_scale (view->model, scale);
5590 ev_view_zoom_for_size_dual_page (EvView *view,
5595 gdouble doc_width, doc_height;
5599 other_page = view->current_page ^ 1;
5601 /* Find the largest of the two. */
5602 get_doc_page_size (view, view->current_page, &doc_width, &doc_height);
5603 if (other_page < ev_document_get_n_pages (view->document)) {
5604 gdouble width_2, height_2;
5606 get_doc_page_size (view, other_page, &width_2, &height_2);
5607 if (width_2 > doc_width)
5608 doc_width = width_2;
5609 if (height_2 > doc_height)
5610 doc_height = height_2;
5612 compute_border (view, width, height, &border);
5614 doc_width = doc_width * 2;
5615 width -= ((border.left + border.right)* 2 + 3 * view->spacing);
5616 height -= (border.top + border.bottom + 2 * view->spacing);
5618 if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
5621 sb_size = ev_view_get_scrollbar_size (view, GTK_ORIENTATION_VERTICAL);
5622 scale = zoom_for_size_fit_width (doc_width, doc_height, width - sb_size, height);
5623 } else if (view->sizing_mode == EV_SIZING_BEST_FIT)
5624 scale = zoom_for_size_best_fit (doc_width, doc_height, width, height);
5626 g_assert_not_reached ();
5628 ev_document_model_set_scale (view->model, scale);
5632 ev_view_zoom_for_size_single_page (EvView *view,
5636 gdouble doc_width, doc_height;
5640 get_doc_page_size (view, view->current_page, &doc_width, &doc_height);
5642 /* Get an approximate border */
5643 compute_border (view, width, height, &border);
5645 width -= (border.left + border.right + 2 * view->spacing);
5646 height -= (border.top + border.bottom + 2 * view->spacing);
5648 if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
5651 sb_size = ev_view_get_scrollbar_size (view, GTK_ORIENTATION_VERTICAL);
5652 scale = zoom_for_size_fit_width (doc_width, doc_height, width - sb_size, height);
5653 } else if (view->sizing_mode == EV_SIZING_BEST_FIT)
5654 scale = zoom_for_size_best_fit (doc_width, doc_height, width, height);
5656 g_assert_not_reached ();
5658 ev_document_model_set_scale (view->model, scale);
5662 ev_view_zoom_for_size (EvView *view,
5666 g_return_if_fail (EV_IS_VIEW (view));
5667 g_return_if_fail (view->sizing_mode == EV_SIZING_FIT_WIDTH ||
5668 view->sizing_mode == EV_SIZING_BEST_FIT);
5669 g_return_if_fail (width >= 0);
5670 g_return_if_fail (height >= 0);
5672 if (view->document == NULL)
5675 if (view->continuous && view->dual_page)
5676 ev_view_zoom_for_size_continuous_and_dual_page (view, width, height);
5677 else if (view->continuous)
5678 ev_view_zoom_for_size_continuous (view, width, height);
5679 else if (view->dual_page)
5680 ev_view_zoom_for_size_dual_page (view, width, height);
5682 ev_view_zoom_for_size_single_page (view, width, height);
5687 ev_view_find_get_n_results (EvView *view, gint page)
5689 return view->find_pages ? g_list_length (view->find_pages[page]) : 0;
5692 static EvRectangle *
5693 ev_view_find_get_result (EvView *view, gint page, gint result)
5695 return view->find_pages ? (EvRectangle *) g_list_nth_data (view->find_pages[page], result) : NULL;
5699 jump_to_find_result (EvView *view)
5702 gint page = view->current_page;
5704 n_results = ev_view_find_get_n_results (view, page);
5706 if (n_results > 0 && view->find_result < n_results) {
5708 GdkRectangle view_rect;
5710 rect = ev_view_find_get_result (view, page, view->find_result);
5711 doc_rect_to_view_rect (view, page, rect, &view_rect);
5712 ensure_rectangle_is_visible (view, &view_rect);
5713 view->jump_to_find_result = FALSE;
5719 * @view: #EvView instance
5720 * @direction: Direction to look
5721 * @shift: Shift from current page
5723 * Jumps to the first page that has occurences of searched word.
5724 * Uses a direction where to look and a shift from current page.
5728 jump_to_find_page (EvView *view, EvViewFindDirection direction, gint shift)
5732 n_pages = ev_document_get_n_pages (view->document);
5734 for (i = 0; i < n_pages; i++) {
5737 if (direction == EV_VIEW_FIND_NEXT)
5738 page = view->current_page + i;
5740 page = view->current_page - i;
5743 if (page >= n_pages) {
5744 page = page - n_pages;
5745 } else if (page < 0)
5746 page = page + n_pages;
5748 if (ev_view_find_get_n_results (view, page) > 0) {
5749 ev_document_model_set_page (view->model, page);
5756 ev_view_find_changed (EvView *view, GList **results, gint page)
5758 view->find_pages = results;
5760 if (view->jump_to_find_result == TRUE) {
5761 jump_to_find_page (view, EV_VIEW_FIND_NEXT, 0);
5762 jump_to_find_result (view);
5765 if (view->current_page == page)
5766 gtk_widget_queue_draw (GTK_WIDGET (view));
5770 ev_view_find_next (EvView *view)
5774 n_results = ev_view_find_get_n_results (view, view->current_page);
5775 view->find_result++;
5777 if (view->find_result >= n_results) {
5778 view->find_result = 0;
5779 jump_to_find_page (view, EV_VIEW_FIND_NEXT, 1);
5780 jump_to_find_result (view);
5782 jump_to_find_result (view);
5783 gtk_widget_queue_draw (GTK_WIDGET (view));
5788 ev_view_find_previous (EvView *view)
5790 view->find_result--;
5792 if (view->find_result < 0) {
5793 jump_to_find_page (view, EV_VIEW_FIND_PREV, -1);
5794 view->find_result = MAX (0, ev_view_find_get_n_results (view, view->current_page) - 1);
5795 jump_to_find_result (view);
5797 jump_to_find_result (view);
5798 gtk_widget_queue_draw (GTK_WIDGET (view));
5803 ev_view_find_search_changed (EvView *view)
5805 /* search string has changed, focus on new search result */
5806 view->jump_to_find_result = TRUE;
5807 view->find_pages = NULL;
5811 ev_view_find_set_highlight_search (EvView *view, gboolean value)
5813 view->highlight_find_results = value;
5814 gtk_widget_queue_draw (GTK_WIDGET (view));
5818 ev_view_find_cancel (EvView *view)
5820 view->find_pages = NULL;
5825 ev_view_highlight_forward_search (EvView *view,
5830 GdkRectangle view_rect;
5832 if (!ev_document_has_synctex (view->document))
5835 mapping = ev_document_synctex_forward_search (view->document, link);
5839 if (view->synctex_result)
5840 g_free (view->synctex_result);
5841 view->synctex_result = mapping;
5843 page = GPOINTER_TO_INT (mapping->data);
5844 ev_document_model_set_page (view->model, page);
5846 doc_rect_to_view_rect (view, page, &mapping->area, &view_rect);
5847 ensure_rectangle_is_visible (view, &view_rect);
5848 gtk_widget_queue_draw (GTK_WIDGET (view));
5851 /*** Selections ***/
5853 /* compute_new_selection_rect/text calculates the area currently selected by
5854 * view_rect. each handles a different mode;
5857 compute_new_selection_rect (EvView *view,
5861 GdkRectangle view_rect;
5865 g_assert (view->selection_mode == EV_VIEW_SELECTION_RECTANGLE);
5867 view_rect.x = MIN (start->x, stop->x);
5868 view_rect.y = MIN (start->y, stop->y);
5869 view_rect.width = MAX (start->x, stop->x) - view_rect.x;
5870 view_rect.width = MAX (start->y, stop->y) - view_rect.y;
5872 n_pages = ev_document_get_n_pages (view->document);
5874 for (i = 0; i < n_pages; i++) {
5875 GdkRectangle page_area;
5878 if (ev_view_get_page_extents (view, i, &page_area, &border)) {
5879 GdkRectangle overlap;
5881 if (gdk_rectangle_intersect (&page_area, &view_rect, &overlap)) {
5882 EvViewSelection *selection;
5884 selection = g_new0 (EvViewSelection, 1);
5885 selection->page = i;
5886 view_rect_to_doc_rect (view, &overlap, &page_area,
5887 &(selection->rect));
5889 list = g_list_append (list, selection);
5898 gdk_rectangle_point_in (GdkRectangle *rectangle,
5901 return rectangle->x <= point->x &&
5902 rectangle->y <= point->y &&
5903 point->x < rectangle->x + rectangle->width &&
5904 point->y < rectangle->y + rectangle->height;
5908 compute_new_selection_text (EvView *view,
5909 EvSelectionStyle style,
5913 int n_pages, i, first, last;
5915 EvViewSelection *selection;
5916 gdouble width, height;
5917 int start_page, end_page;
5919 g_assert (view->selection_mode == EV_VIEW_SELECTION_TEXT);
5921 n_pages = ev_document_get_n_pages (view->document);
5923 /* First figure out the range of pages the selection
5927 if (view->continuous) {
5930 } else if (view->dual_page) {
5931 start_page = view->start_page;
5932 end_page = view->end_page + 1;
5934 start_page = view->current_page;
5935 end_page = view->current_page + 1;
5938 for (i = start_page; i < end_page; i++) {
5939 GdkRectangle page_area;
5942 ev_view_get_page_extents (view, i, &page_area, &border);
5943 if (gdk_rectangle_point_in (&page_area, start) ||
5944 gdk_rectangle_point_in (&page_area, stop)) {
5945 if (first == n_pages)
5952 /* Now create a list of EvViewSelection's for the affected
5953 * pages. This could be an empty list, a list of just one
5954 * page or a number of pages.*/
5955 for (i = first; i < last + 1; i++) {
5956 GdkRectangle page_area;
5960 get_doc_page_size (view, i, &width, &height);
5962 selection = g_new0 (EvViewSelection, 1);
5963 selection->page = i;
5964 selection->style = style;
5965 selection->rect.x1 = selection->rect.y1 = 0;
5966 selection->rect.x2 = width;
5967 selection->rect.y2 = height;
5969 ev_view_get_page_extents (view, i, &page_area, &border);
5971 if (gdk_rectangle_point_in (&page_area, start))
5977 view_point_to_doc_point (view, point, &page_area,
5978 &selection->rect.x1,
5979 &selection->rect.y1);
5981 /* If the selection is contained within just one page,
5982 * make sure we don't write 'start' into both points
5983 * in selection->rect. */
5988 view_point_to_doc_point (view, point, &page_area,
5989 &selection->rect.x2,
5990 &selection->rect.y2);
5992 list = g_list_append (list, selection);
5998 /* This function takes the newly calculated list, and figures out which regions
5999 * have changed. It then queues a redraw approporiately.
6002 merge_selection_region (EvView *view,
6006 GList *new_list_ptr, *old_list_ptr;
6008 /* Update the selection */
6009 old_list = ev_pixbuf_cache_get_selection_list (view->pixbuf_cache);
6010 g_list_foreach (view->selection_info.selections, (GFunc)selection_free, NULL);
6011 g_list_free (view->selection_info.selections);
6012 view->selection_info.selections = new_list;
6013 ev_pixbuf_cache_set_selection_list (view->pixbuf_cache, new_list);
6014 g_signal_emit (view, signals[SIGNAL_SELECTION_CHANGED], 0, NULL);
6016 new_list_ptr = new_list;
6017 old_list_ptr = old_list;
6019 while (new_list_ptr || old_list_ptr) {
6020 EvViewSelection *old_sel, *new_sel;
6022 cairo_region_t *region = NULL;
6024 new_sel = (new_list_ptr) ? (new_list_ptr->data) : NULL;
6025 old_sel = (old_list_ptr) ? (old_list_ptr->data) : NULL;
6027 /* Assume that the lists are in order, and we run through them
6028 * comparing them, one page at a time. We come out with the
6029 * first page we see. */
6030 if (new_sel && old_sel) {
6031 if (new_sel->page < old_sel->page) {
6032 new_list_ptr = new_list_ptr->next;
6034 } else if (new_sel->page > old_sel->page) {
6035 old_list_ptr = old_list_ptr->next;
6038 new_list_ptr = new_list_ptr->next;
6039 old_list_ptr = old_list_ptr->next;
6041 } else if (new_sel) {
6042 new_list_ptr = new_list_ptr->next;
6043 } else if (old_sel) {
6044 old_list_ptr = old_list_ptr->next;
6047 g_assert (new_sel || old_sel);
6049 /* is the page we're looking at on the screen?*/
6050 cur_page = new_sel ? new_sel->page : old_sel->page;
6051 if (cur_page < view->start_page || cur_page > view->end_page)
6054 /* seed the cache with a new page. We are going to need the new
6057 cairo_region_t *tmp_region = NULL;
6059 ev_pixbuf_cache_get_selection_surface (view->pixbuf_cache,
6064 if (tmp_region && !cairo_region_is_empty (tmp_region)) {
6065 new_sel->covered_region = cairo_region_reference (tmp_region);
6069 /* Now we figure out what needs redrawing */
6070 if (old_sel && new_sel) {
6071 if (old_sel->covered_region && new_sel->covered_region) {
6072 /* We only want to redraw the areas that have
6073 * changed, so we xor the old and new regions
6074 * and redraw if it's different */
6075 region = cairo_region_copy (old_sel->covered_region);
6076 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 9, 12)
6077 cairo_region_xor (region, new_sel->covered_region);
6079 cairo_region_t *tbr;
6080 tbr = cairo_region_copy (new_sel->covered_region);
6082 /* xor old_sel, new_sel*/
6083 cairo_region_subtract (tbr, region);
6084 cairo_region_subtract (region, new_sel->covered_region);
6085 cairo_region_union (region, tbr);
6086 cairo_region_destroy (tbr);
6089 if (cairo_region_is_empty (region)) {
6090 cairo_region_destroy (region);
6093 gint num_rectangles = cairo_region_num_rectangles (region);
6096 /* We need to make the damage region a little bigger
6097 * because the edges of the old selection might change
6099 cairo_region_get_rectangle (region, 0, &r);
6102 cairo_region_union_rectangle (region, &r);
6104 cairo_region_get_rectangle (region, num_rectangles - 1, &r);
6107 cairo_region_union_rectangle (region, &r);
6109 } else if (old_sel->covered_region) {
6110 region = cairo_region_copy (old_sel->covered_region);
6111 } else if (new_sel->covered_region) {
6112 region = cairo_region_copy (new_sel->covered_region);
6114 } else if (old_sel && !new_sel) {
6115 if (old_sel->covered_region && !cairo_region_is_empty (old_sel->covered_region)) {
6116 region = cairo_region_copy (old_sel->covered_region);
6118 } else if (!old_sel && new_sel) {
6119 if (new_sel->covered_region && !cairo_region_is_empty (new_sel->covered_region)) {
6120 region = cairo_region_copy (new_sel->covered_region);
6123 g_assert_not_reached ();
6126 /* Redraw the damaged region! */
6128 GdkRectangle page_area;
6131 ev_view_get_page_extents (view, cur_page, &page_area, &border);
6132 cairo_region_translate (region,
6133 page_area.x + border.left - view->scroll_x,
6134 page_area.y + border.top - view->scroll_y);
6135 gdk_window_invalidate_region (gtk_widget_get_window (GTK_WIDGET (view)), region, TRUE);
6136 cairo_region_destroy (region);
6140 /* Free the old list, now that we're done with it. */
6141 g_list_foreach (old_list, (GFunc) selection_free, NULL);
6142 g_list_free (old_list);
6146 compute_selections (EvView *view,
6147 EvSelectionStyle style,
6153 if (view->selection_mode == EV_VIEW_SELECTION_RECTANGLE)
6154 list = compute_new_selection_rect (view, start, stop);
6156 list = compute_new_selection_text (view, style, start, stop);
6157 merge_selection_region (view, list);
6160 /* Free's the selection. It's up to the caller to queue redraws if needed.
6163 selection_free (EvViewSelection *selection)
6165 if (selection->covered_region)
6166 cairo_region_destroy (selection->covered_region);
6171 clear_selection (EvView *view)
6173 if (view->selection_info.selections) {
6174 g_list_foreach (view->selection_info.selections, (GFunc)selection_free, NULL);
6175 g_list_free (view->selection_info.selections);
6176 view->selection_info.selections = NULL;
6178 g_signal_emit (view, signals[SIGNAL_SELECTION_CHANGED], 0, NULL);
6180 view->selection_info.in_selection = FALSE;
6181 if (view->pixbuf_cache)
6182 ev_pixbuf_cache_set_selection_list (view->pixbuf_cache, NULL);
6186 ev_view_select_all (EvView *view)
6188 GList *selections = NULL;
6191 /* Disable selection on rotated pages for the 0.4.0 series */
6192 if (view->rotation != 0)
6195 clear_selection (view);
6197 n_pages = ev_document_get_n_pages (view->document);
6198 for (i = 0; i < n_pages; i++) {
6199 gdouble width, height;
6200 EvViewSelection *selection;
6202 get_doc_page_size (view, i, &width, &height);
6204 selection = g_new0 (EvViewSelection, 1);
6205 selection->page = i;
6206 selection->style = EV_SELECTION_STYLE_GLYPH;
6207 selection->rect.x1 = selection->rect.y1 = 0;
6208 selection->rect.x2 = width;
6209 selection->rect.y2 = height;
6211 selections = g_list_append (selections, selection);
6214 merge_selection_region (view, selections);
6215 gtk_widget_queue_draw (GTK_WIDGET (view));
6219 ev_view_get_has_selection (EvView *view)
6221 return view->selection_info.selections != NULL;
6225 get_selected_text (EvView *view)
6229 gchar *normalized_text;
6231 text = g_string_new (NULL);
6233 ev_document_doc_mutex_lock ();
6235 for (l = view->selection_info.selections; l != NULL; l = l->next) {
6236 EvViewSelection *selection = (EvViewSelection *)l->data;
6240 page = ev_document_get_page (view->document, selection->page);
6241 tmp = ev_selection_get_selected_text (EV_SELECTION (view->document),
6242 page, selection->style,
6243 &(selection->rect));
6244 g_object_unref (page);
6245 g_string_append (text, tmp);
6249 ev_document_doc_mutex_unlock ();
6251 normalized_text = g_utf8_normalize (text->str, text->len, G_NORMALIZE_NFKC);
6252 g_string_free (text, TRUE);
6253 return normalized_text;
6257 ev_view_clipboard_copy (EvView *view,
6260 GtkClipboard *clipboard;
6262 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (view),
6263 GDK_SELECTION_CLIPBOARD);
6264 gtk_clipboard_set_text (clipboard, text, -1);
6268 ev_view_copy (EvView *ev_view)
6272 if (!EV_IS_SELECTION (ev_view->document))
6275 text = get_selected_text (ev_view);
6276 ev_view_clipboard_copy (ev_view, text);
6281 ev_view_primary_get_cb (GtkClipboard *clipboard,
6282 GtkSelectionData *selection_data,
6286 EvView *ev_view = EV_VIEW (data);
6288 if (ev_view->link_selected) {
6289 gtk_selection_data_set_text (selection_data,
6290 ev_link_action_get_uri (ev_view->link_selected),
6292 } else if (EV_IS_SELECTION (ev_view->document) &&
6293 ev_view->selection_info.selections) {
6296 text = get_selected_text (ev_view);
6298 gtk_selection_data_set_text (selection_data, text, -1);
6305 ev_view_primary_clear_cb (GtkClipboard *clipboard,
6308 EvView *view = EV_VIEW (data);
6310 clear_selection (view);
6311 clear_link_selected (view);
6315 ev_view_update_primary_selection (EvView *ev_view)
6317 GtkClipboard *clipboard;
6319 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (ev_view),
6320 GDK_SELECTION_PRIMARY);
6322 if (ev_view->selection_info.selections || ev_view->link_selected) {
6323 GtkTargetList *target_list;
6324 GtkTargetEntry *targets;
6327 target_list = gtk_target_list_new (NULL, 0);
6328 gtk_target_list_add_text_targets (target_list, 0);
6329 targets = gtk_target_table_new_from_list (target_list, &n_targets);
6330 gtk_target_list_unref (target_list);
6332 if (!gtk_clipboard_set_with_owner (clipboard,
6334 ev_view_primary_get_cb,
6335 ev_view_primary_clear_cb,
6336 G_OBJECT (ev_view)))
6337 ev_view_primary_clear_cb (clipboard, ev_view);
6339 gtk_target_table_free (targets, n_targets);
6341 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (ev_view))
6342 gtk_clipboard_clear (clipboard);
6347 clear_link_selected (EvView *view)
6349 if (view->link_selected) {
6350 g_object_unref (view->link_selected);
6351 view->link_selected = NULL;
6356 ev_view_copy_link_address (EvView *view,
6357 EvLinkAction *action)
6359 clear_link_selected (view);
6361 ev_view_clipboard_copy (view, ev_link_action_get_uri (action));
6363 view->link_selected = g_object_ref (action);
6364 ev_view_update_primary_selection (view);
6367 /*** Cursor operations ***/
6369 ev_view_set_cursor (EvView *view, EvViewCursor new_cursor)
6371 GdkCursor *cursor = NULL;
6375 if (view->cursor == new_cursor) {
6379 view->cursor = new_cursor;
6381 window = gtk_widget_get_window (GTK_WIDGET (view));
6382 widget = gtk_widget_get_toplevel (GTK_WIDGET (view));
6383 cursor = ev_view_cursor_new (gtk_widget_get_display (widget), new_cursor);
6384 gdk_window_set_cursor (window, cursor);
6387 g_object_unref (cursor);
6391 ev_view_hide_cursor (EvView *view)
6393 ev_view_set_cursor (view, EV_VIEW_CURSOR_HIDDEN);
6397 ev_view_show_cursor (EvView *view)
6399 ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL);
6403 ev_view_next_page (EvView *view)
6407 g_return_val_if_fail (EV_IS_VIEW (view), FALSE);
6409 if (!view->document)
6412 page = ev_document_model_get_page (view->model);
6413 n_pages = ev_document_get_n_pages (view->document);
6415 if (view->dual_page)
6420 if (page < n_pages) {
6421 ev_document_model_set_page (view->model, page);
6423 } else if (view->dual_page && page == n_pages) {
6424 ev_document_model_set_page (view->model, page - 1);
6432 ev_view_previous_page (EvView *view)
6436 g_return_val_if_fail (EV_IS_VIEW (view), FALSE);
6438 if (!view->document)
6441 page = ev_document_model_get_page (view->model);
6443 if (view->dual_page)
6449 ev_document_model_set_page (view->model, page);
6451 } else if (view->dual_page && page == -1) {
6452 ev_document_model_set_page (view->model, 0);