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.h"
32 #include "ev-document-forms.h"
33 #include "ev-document-images.h"
34 #include "ev-document-links.h"
35 #include "ev-document-misc.h"
36 #include "ev-pixbuf-cache.h"
37 #include "ev-page-cache.h"
38 #include "ev-view-marshal.h"
39 #include "ev-document-annotations.h"
40 #include "ev-annotation-window.h"
41 #include "ev-loading-window.h"
43 #include "ev-view-accessible.h"
44 #include "ev-view-private.h"
45 #include "ev-view-type-builtins.h"
47 #define EV_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EV_TYPE_VIEW, EvViewClass))
48 #define EV_IS_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), EV_TYPE_VIEW))
49 #define EV_VIEW_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), EV_TYPE_VIEW, EvViewClass))
52 SIGNAL_BINDING_ACTIVATED,
56 SIGNAL_SELECTION_CHANGED,
66 static guint signals[N_SIGNALS];
71 } EvViewFindDirection;
73 #define ZOOM_IN_FACTOR 1.2
74 #define ZOOM_OUT_FACTOR (1.0/ZOOM_IN_FACTOR)
76 #define SCROLL_TIME 150
79 static void ev_view_set_scroll_adjustments (GtkLayout *layout,
80 GtkAdjustment *hadjustment,
81 GtkAdjustment *vadjustment);
82 static void view_update_range_and_current_page (EvView *view);
83 static void set_scroll_adjustment (EvView *view,
84 GtkOrientation orientation,
85 GtkAdjustment *adjustment);
86 static void add_scroll_binding_keypad (GtkBindingSet *binding_set,
88 GdkModifierType modifiers,
91 static void ensure_rectangle_is_visible (EvView *view,
94 /*** Geometry computations ***/
95 static void compute_border (EvView *view,
99 static void get_page_y_offset (EvView *view,
102 static gboolean get_page_extents (EvView *view,
104 GdkRectangle *page_area,
106 static void view_rect_to_doc_rect (EvView *view,
107 GdkRectangle *view_rect,
108 GdkRectangle *page_area,
109 EvRectangle *doc_rect);
110 static void doc_rect_to_view_rect (EvView *view,
112 EvRectangle *doc_rect,
113 GdkRectangle *view_rect);
114 static gboolean get_dual_even_left (EvView *view);
115 static void find_page_at_location (EvView *view,
121 static void doc_point_to_view_point (EvView *view,
124 GdkPoint *view_point);
126 static EvLink * ev_view_get_link_at_location (EvView *view,
129 static char* tip_from_link (EvView *view,
132 static EvFormField *ev_view_get_form_field_at_location (EvView *view,
136 /*** Annotations ***/
137 static EvAnnotation *ev_view_get_annotation_at_location (EvView *view,
140 static void show_annotation_windows (EvView *view,
142 static void hide_annotation_windows (EvView *view,
144 /*** GtkWidget implementation ***/
145 static void ev_view_size_request_continuous_dual_page (EvView *view,
146 GtkRequisition *requisition);
147 static void ev_view_size_request_continuous (EvView *view,
148 GtkRequisition *requisition);
149 static void ev_view_size_request_dual_page (EvView *view,
150 GtkRequisition *requisition);
151 static void ev_view_size_request_single_page (EvView *view,
152 GtkRequisition *requisition);
153 static void ev_view_size_request (GtkWidget *widget,
154 GtkRequisition *requisition);
155 static void ev_view_size_allocate (GtkWidget *widget,
156 GtkAllocation *allocation);
157 static void ev_view_realize (GtkWidget *widget);
158 static gboolean ev_view_scroll_event (GtkWidget *widget,
159 GdkEventScroll *event);
160 static gboolean ev_view_expose_event (GtkWidget *widget,
161 GdkEventExpose *event);
162 static gboolean ev_view_popup_menu (GtkWidget *widget);
163 static gboolean ev_view_button_press_event (GtkWidget *widget,
164 GdkEventButton *event);
165 static gboolean ev_view_motion_notify_event (GtkWidget *widget,
166 GdkEventMotion *event);
167 static gboolean ev_view_button_release_event (GtkWidget *widget,
168 GdkEventButton *event);
169 static gboolean ev_view_enter_notify_event (GtkWidget *widget,
170 GdkEventCrossing *event);
171 static gboolean ev_view_leave_notify_event (GtkWidget *widget,
172 GdkEventCrossing *event);
173 static void ev_view_style_set (GtkWidget *widget,
174 GtkStyle *old_style);
175 static void ev_view_remove_all (EvView *view);
177 static AtkObject *ev_view_get_accessible (GtkWidget *widget);
180 static void highlight_find_results (EvView *view,
182 static void draw_one_page (EvView *view,
185 GdkRectangle *page_area,
187 GdkRectangle *expose_area,
188 gboolean *page_ready);
189 static void show_loading_window (EvView *view);
190 static void hide_loading_window (EvView *view);
191 static void ev_view_reload_page (EvView *view,
194 static void ev_view_loading_window_move (EvView *view);
197 static void ev_view_change_page (EvView *view,
199 static void job_finished_cb (EvPixbufCache *pixbuf_cache,
202 static void ev_view_page_changed_cb (EvDocumentModel *model,
206 static void on_adjustment_value_changed (GtkAdjustment *adjustment,
210 static void ev_view_finalize (GObject *object);
211 static void ev_view_destroy (GtkObject *object);
212 static void ev_view_class_init (EvViewClass *class);
213 static void ev_view_init (EvView *view);
215 /*** Zoom and sizing ***/
216 static double zoom_for_size_fit_width (gdouble doc_width,
220 static double zoom_for_size_fit_height (gdouble doc_width,
224 static double zoom_for_size_best_fit (gdouble doc_width,
228 static void ev_view_zoom_for_size (EvView *view,
231 static void ev_view_zoom_for_size_continuous_and_dual_page (EvView *view,
234 static void ev_view_zoom_for_size_continuous (EvView *view,
237 static void ev_view_zoom_for_size_dual_page (EvView *view,
240 static void ev_view_zoom_for_size_single_page (EvView *view,
244 static void ev_view_set_cursor (EvView *view,
245 EvViewCursor new_cursor);
246 static void ev_view_handle_cursor_over_xy (EvView *view,
251 static gint ev_view_find_get_n_results (EvView *view,
253 static EvRectangle *ev_view_find_get_result (EvView *view,
256 static void jump_to_find_result (EvView *view);
257 static void jump_to_find_page (EvView *view,
258 EvViewFindDirection direction,
262 static void compute_selections (EvView *view,
263 EvSelectionStyle style,
266 static void clear_selection (EvView *view);
267 static void clear_link_selected (EvView *view);
268 static void selection_free (EvViewSelection *selection);
269 static char* get_selected_text (EvView *ev_view);
270 static void ev_view_primary_get_cb (GtkClipboard *clipboard,
271 GtkSelectionData *selection_data,
274 static void ev_view_primary_clear_cb (GtkClipboard *clipboard,
276 static void ev_view_update_primary_selection (EvView *ev_view);
278 G_DEFINE_TYPE (EvView, ev_view, GTK_TYPE_LAYOUT)
280 /* HeightToPage cache */
281 #define EV_HEIGHT_TO_PAGE_CACHE_KEY "ev-height-to-page-cache"
284 build_height_to_page (EvHeightToPageCache *cache,
285 EvDocument *document,
288 gboolean swap, uniform, dual_even_left;
290 double uniform_height, page_height, next_page_height;
292 gdouble u_width, u_height;
295 swap = (rotation == 90 || rotation == 270);
297 uniform = ev_document_is_page_size_uniform (document);
298 n_pages = ev_document_get_n_pages (document);
299 dual_even_left = (n_pages > 2);
301 g_free (cache->height_to_page);
302 g_free (cache->dual_height_to_page);
304 cache->rotation = rotation;
305 cache->height_to_page = g_new0 (gdouble, n_pages + 1);
306 cache->dual_height_to_page = g_new0 (gdouble, n_pages + 2);
309 ev_document_get_page_size (document, 0, &u_width, &u_height);
312 for (i = 0; i <= n_pages; i++) {
314 uniform_height = swap ? u_width : u_height;
315 cache->height_to_page[i] = i * uniform_height;
320 ev_document_get_page_size (document, i, &w, &h);
321 page_height = swap ? w : h;
325 cache->height_to_page[i] = saved_height;
326 saved_height += page_height;
330 if (dual_even_left && !uniform) {
333 ev_document_get_page_size (document, 0, &w, &h);
334 saved_height = swap ? w : h;
339 for (i = dual_even_left; i < n_pages + 2; i += 2) {
341 uniform_height = swap ? u_width : u_height;
342 cache->dual_height_to_page[i] = ((i + dual_even_left) / 2) * uniform_height;
343 if (i + 1 < n_pages + 2)
344 cache->dual_height_to_page[i + 1] = ((i + dual_even_left) / 2) * uniform_height;
346 if (i + 1 < n_pages) {
349 ev_document_get_page_size (document, i + 1, &w, &h);
350 next_page_height = swap ? w : h;
352 next_page_height = 0;
358 ev_document_get_page_size (document, i, &w, &h);
359 page_height = swap ? w : h;
364 if (i + 1 < n_pages + 2) {
365 cache->dual_height_to_page[i] = saved_height;
366 cache->dual_height_to_page[i + 1] = saved_height;
367 saved_height += MAX(page_height, next_page_height);
369 cache->dual_height_to_page[i] = saved_height;
376 ev_height_to_page_cache_get_height (EvHeightToPageCache *cache,
377 EvDocument *document,
381 gdouble *dual_height)
383 if (cache->rotation != rotation)
384 build_height_to_page (cache, document, rotation);
386 *height = cache->height_to_page[page];
387 *dual_height = cache->dual_height_to_page[page];
391 ev_height_to_page_cache_free (EvHeightToPageCache *cache)
393 if (cache->height_to_page) {
394 g_free (cache->height_to_page);
395 cache->height_to_page = NULL;
398 if (cache->dual_height_to_page) {
399 g_free (cache->dual_height_to_page);
400 cache->dual_height_to_page = NULL;
405 static EvHeightToPageCache *
406 ev_view_get_height_to_page_cache (EvView *view)
408 EvHeightToPageCache *cache;
413 cache = g_object_get_data (G_OBJECT (view->document), EV_HEIGHT_TO_PAGE_CACHE_KEY);
415 cache = g_new0 (EvHeightToPageCache, 1);
416 build_height_to_page (cache, view->document, view->rotation);
417 g_object_set_data_full (G_OBJECT (view->document),
418 EV_HEIGHT_TO_PAGE_CACHE_KEY,
420 (GDestroyNotify)ev_height_to_page_cache_free);
427 ev_view_get_height_to_page (EvView *view,
434 if (!view->height_to_page_cache)
437 ev_height_to_page_cache_get_height (view->height_to_page_cache,
443 *height = (gint)(h * view->scale + 0.5);
446 *dual_height = (gint)(dh * view->scale + 0.5);
450 ev_view_get_scrollbar_size (EvView *view,
451 GtkOrientation orientation)
453 GtkWidget *widget = GTK_WIDGET (view);
455 GtkWidget *swindow = gtk_widget_get_parent (GTK_WIDGET (view));
456 GtkAllocation allocation;
460 if (!GTK_IS_SCROLLED_WINDOW (swindow))
463 gtk_widget_get_allocation (widget, &allocation);
465 if (orientation == GTK_ORIENTATION_VERTICAL) {
466 if (allocation.height >= view->requisition.height)
467 sb = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (swindow));
471 if (allocation.width >= view->requisition.width)
472 sb = gtk_scrolled_window_get_hscrollbar (GTK_SCROLLED_WINDOW (swindow));
477 gtk_widget_style_get (swindow, "scrollbar_spacing", &spacing, NULL);
478 gtk_widget_size_request (sb, &req);
480 return (orientation == GTK_ORIENTATION_VERTICAL ? req.width : req.height) + spacing;
484 scroll_to_point (EvView *view,
487 GtkOrientation orientation)
490 gdouble upper, lower;
492 if (orientation == GTK_ORIENTATION_VERTICAL) {
493 page_size = gtk_adjustment_get_page_size (view->vadjustment);
494 upper = gtk_adjustment_get_upper (view->vadjustment);
495 lower = gtk_adjustment_get_lower (view->vadjustment);
497 if (view->continuous) {
498 gtk_adjustment_clamp_page (view->vadjustment,
499 y - view->spacing / 2,
502 gtk_adjustment_set_value (view->vadjustment,
503 CLAMP (y, lower, upper - page_size));
506 page_size = gtk_adjustment_get_page_size (view->hadjustment);
507 upper = gtk_adjustment_get_upper (view->hadjustment);
508 lower = gtk_adjustment_get_lower (view->hadjustment);
510 if (view->dual_page) {
511 gtk_adjustment_clamp_page (view->hadjustment, x,
514 gtk_adjustment_set_value (view->hadjustment,
515 CLAMP (x, lower, upper - page_size));
521 ev_view_scroll_to_page_position (EvView *view, GtkOrientation orientation)
528 if ((orientation == GTK_ORIENTATION_VERTICAL && view->pending_point.y == 0) ||
529 (orientation == GTK_ORIENTATION_HORIZONTAL && view->pending_point.x == 0)) {
530 GdkRectangle page_area;
533 get_page_extents (view, view->current_page, &page_area, &border);
539 doc_point_to_view_point (view, view->current_page,
540 &view->pending_point, &view_point);
545 scroll_to_point (view, x, y, orientation);
549 view_set_adjustment_values (EvView *view,
550 GtkOrientation orientation)
552 GtkWidget *widget = GTK_WIDGET (view);
553 GtkAdjustment *adjustment;
554 GtkAllocation allocation;
563 gtk_widget_get_allocation (widget, &allocation);
565 if (orientation == GTK_ORIENTATION_HORIZONTAL) {
566 req_size = view->requisition.width;
567 alloc_size = allocation.width;
568 adjustment = view->hadjustment;
570 req_size = view->requisition.height;
571 alloc_size = allocation.height;
572 adjustment = view->vadjustment;
579 value = gtk_adjustment_get_value (adjustment);
580 upper = gtk_adjustment_get_upper (adjustment);
581 page_size = gtk_adjustment_get_page_size (adjustment);
583 switch (view->pending_scroll) {
584 case SCROLL_TO_KEEP_POSITION:
585 case SCROLL_TO_FIND_LOCATION:
586 factor = value / upper;
588 case SCROLL_TO_PAGE_POSITION:
590 case SCROLL_TO_CENTER:
591 factor = (value + page_size * 0.5) / upper;
595 upper = MAX (alloc_size, req_size);
596 page_size = alloc_size;
598 gtk_adjustment_set_page_size (adjustment, page_size);
599 gtk_adjustment_set_step_increment (adjustment, alloc_size * 0.1);
600 gtk_adjustment_set_page_increment (adjustment, alloc_size * 0.9);
601 gtk_adjustment_set_lower (adjustment, 0);
602 gtk_adjustment_set_upper (adjustment, upper);
605 * We add 0.5 to the values before to average out our rounding errors.
607 switch (view->pending_scroll) {
608 case SCROLL_TO_KEEP_POSITION:
609 case SCROLL_TO_FIND_LOCATION:
610 new_value = CLAMP (upper * factor + 0.5, 0, upper - page_size);
611 gtk_adjustment_set_value (adjustment, (int)new_value);
613 case SCROLL_TO_PAGE_POSITION:
614 ev_view_scroll_to_page_position (view, orientation);
616 case SCROLL_TO_CENTER:
617 new_value = CLAMP (upper * factor - page_size * 0.5 + 0.5,
618 0, upper - page_size);
619 gtk_adjustment_set_value (adjustment, (int)new_value);
623 gtk_adjustment_changed (adjustment);
627 view_update_range_and_current_page (EvView *view)
629 gint start = view->start_page;
630 gint end = view->end_page;
632 if (ev_document_get_n_pages (view->document) <= 0 ||
633 !ev_document_check_dimensions (view->document))
636 if (view->continuous) {
637 GdkRectangle current_area, unused, page_area;
639 gboolean found = FALSE;
640 gint area_max = -1, area;
641 gint best_current_page = -1;
644 if (!(view->vadjustment && view->hadjustment))
647 current_area.x = gtk_adjustment_get_value (view->hadjustment);
648 current_area.width = gtk_adjustment_get_page_size (view->hadjustment);
649 current_area.y = gtk_adjustment_get_value (view->vadjustment);
650 current_area.height = gtk_adjustment_get_page_size (view->vadjustment);
652 for (i = 0; i < ev_document_get_n_pages (view->document); i++) {
654 get_page_extents (view, i, &page_area, &border);
656 if (gdk_rectangle_intersect (¤t_area, &page_area, &unused)) {
657 area = unused.width * unused.height;
661 view->start_page = i;
663 best_current_page = i;
665 if (area > area_max) {
666 best_current_page = (area == area_max) ? MIN (i, best_current_page) : i;
672 } else if (found && view->current_page <= view->end_page) {
673 if (view->dual_page && j < 1) {
674 /* In dual mode we stop searching
675 * after two consecutive non-visible pages.
684 if (view->pending_scroll == SCROLL_TO_KEEP_POSITION) {
685 best_current_page = MAX (best_current_page, view->start_page);
687 if (view->current_page != best_current_page) {
688 view->current_page = best_current_page;
689 hide_loading_window (view);
690 ev_document_model_set_page (view->model, best_current_page);
693 } else if (view->dual_page) {
694 if (view->current_page % 2 == get_dual_even_left (view)) {
695 view->start_page = view->current_page;
696 if (view->current_page + 1 < ev_document_get_n_pages (view->document))
697 view->end_page = view->start_page + 1;
699 view->end_page = view->start_page;
701 if (view->current_page < 1)
702 view->start_page = view->current_page;
704 view->start_page = view->current_page - 1;
705 view->end_page = view->current_page;
708 view->start_page = view->current_page;
709 view->end_page = view->current_page;
712 if (view->start_page == -1 || view->end_page == -1)
715 if (start != view->start_page || end != view->end_page) {
718 for (i = start; i < view->start_page && start != -1; i++) {
719 hide_annotation_windows (view, i);
722 for (i = end; i > view->end_page && end != -1; i--) {
723 hide_annotation_windows (view, i);
727 ev_page_cache_set_page_range (view->page_cache,
730 ev_pixbuf_cache_set_page_range (view->pixbuf_cache,
733 view->selection_info.selections);
735 if (ev_pixbuf_cache_get_surface (view->pixbuf_cache, view->current_page))
736 gtk_widget_queue_draw (GTK_WIDGET (view));
740 set_scroll_adjustment (EvView *view,
741 GtkOrientation orientation,
742 GtkAdjustment *adjustment)
744 GtkAdjustment **to_set;
746 if (orientation == GTK_ORIENTATION_HORIZONTAL)
747 to_set = &view->hadjustment;
749 to_set = &view->vadjustment;
751 if (*to_set != adjustment) {
753 g_signal_handlers_disconnect_by_func (*to_set,
754 (gpointer) on_adjustment_value_changed,
756 g_object_unref (*to_set);
759 *to_set = adjustment;
760 view_set_adjustment_values (view, orientation);
763 g_object_ref (*to_set);
764 g_signal_connect (*to_set, "value_changed",
765 G_CALLBACK (on_adjustment_value_changed), view);
771 ev_view_set_scroll_adjustments (GtkLayout *layout,
772 GtkAdjustment *hadjustment,
773 GtkAdjustment *vadjustment)
775 EvView *view = EV_VIEW (layout);
777 set_scroll_adjustment (view, GTK_ORIENTATION_HORIZONTAL, hadjustment);
778 set_scroll_adjustment (view, GTK_ORIENTATION_VERTICAL, vadjustment);
780 on_adjustment_value_changed (NULL, view);
784 add_scroll_binding_keypad (GtkBindingSet *binding_set,
786 GdkModifierType modifiers,
787 GtkScrollType scroll,
790 guint keypad_keyval = keyval - GDK_Left + GDK_KP_Left;
792 gtk_binding_entry_add_signal (binding_set, keyval, modifiers,
793 "binding_activated", 2,
794 GTK_TYPE_SCROLL_TYPE, scroll,
795 G_TYPE_BOOLEAN, horizontal);
796 gtk_binding_entry_add_signal (binding_set, keypad_keyval, modifiers,
797 "binding_activated", 2,
798 GTK_TYPE_SCROLL_TYPE, scroll,
799 G_TYPE_BOOLEAN, horizontal);
803 compute_scroll_increment (EvView *view,
804 GtkScrollType scroll)
806 GtkWidget *widget = GTK_WIDGET (view);
807 GtkAdjustment *adjustment = view->vadjustment;
808 GdkRegion *text_region, *region;
809 GtkAllocation allocation;
812 EvRectangle doc_rect;
813 GdkRectangle page_area;
817 gdouble fraction = 1.0;
819 if (scroll != GTK_SCROLL_PAGE_BACKWARD && scroll != GTK_SCROLL_PAGE_FORWARD)
820 return gtk_adjustment_get_page_size (adjustment);
822 page = scroll == GTK_SCROLL_PAGE_BACKWARD ? view->start_page : view->end_page;
824 text_region = ev_page_cache_get_text_mapping (view->page_cache, page);
825 if (!text_region || gdk_region_empty (text_region))
826 return gtk_adjustment_get_page_size (adjustment);
828 gtk_widget_get_allocation (widget, &allocation);
829 get_page_extents (view, page, &page_area, &border);
830 rect.x = page_area.x + view->scroll_x;
831 rect.y = view->scroll_y + (scroll == GTK_SCROLL_PAGE_BACKWARD ? 5 : allocation.height - 5);
832 rect.width = page_area.width;
834 view_rect_to_doc_rect (view, &rect, &page_area, &doc_rect);
836 /* Convert the doc rectangle into a GdkRectangle */
837 rect.x = doc_rect.x1;
838 rect.y = doc_rect.y1;
839 rect.width = doc_rect.x2 - doc_rect.x1;
840 rect.height = MAX (1, doc_rect.y2 - doc_rect.y1);
841 region = gdk_region_rectangle (&rect);
843 gdk_region_intersect (region, text_region);
844 gdk_region_get_rectangles (region, &recs, &n_recs);
845 gdk_region_destroy (region);
850 ev_page = ev_document_get_page (view->document, page);
851 rc = ev_render_context_new (ev_page, view->rotation, view->scale);
852 g_object_unref (ev_page);
853 /* Get the selection region to know the height of the line */
854 doc_rect.x1 = doc_rect.x2 = recs[0].x + 0.5;
855 doc_rect.y1 = doc_rect.y2 = recs[0].y + 0.5;
857 ev_document_doc_mutex_lock ();
858 region = ev_selection_get_selection_region (EV_SELECTION (view->document),
859 rc, EV_SELECTION_STYLE_LINE,
861 ev_document_doc_mutex_unlock ();
865 gdk_region_get_rectangles (region, &recs, &n_recs);
866 gdk_region_destroy (region);
868 fraction = 1 - (recs[0].height / gtk_adjustment_get_page_size (adjustment));
873 return gtk_adjustment_get_page_size (adjustment) * fraction;
878 ev_view_scroll (EvView *view,
879 GtkScrollType scroll,
882 GtkAdjustment *adjustment;
883 double value, increment;
884 gdouble upper, lower;
886 gdouble step_increment;
887 gboolean first_page = FALSE;
888 gboolean last_page = FALSE;
890 view->jump_to_find_result = FALSE;
892 if (view->sizing_mode == EV_SIZING_BEST_FIT) {
894 case GTK_SCROLL_PAGE_BACKWARD:
895 case GTK_SCROLL_STEP_BACKWARD:
896 ev_view_previous_page (view);
898 case GTK_SCROLL_PAGE_FORWARD:
899 case GTK_SCROLL_STEP_FORWARD:
900 ev_view_next_page (view);
908 /* Assign values for increment and vertical adjustment */
909 adjustment = horizontal ? view->hadjustment : view->vadjustment;
910 value = gtk_adjustment_get_value (adjustment);
911 upper = gtk_adjustment_get_upper (adjustment);
912 lower = gtk_adjustment_get_lower (adjustment);
913 page_size = gtk_adjustment_get_page_size (adjustment);
914 step_increment = gtk_adjustment_get_step_increment (adjustment);
916 /* Assign boolean for first and last page */
917 if (view->current_page == 0)
919 if (view->current_page == ev_document_get_n_pages (view->document) - 1)
923 case GTK_SCROLL_PAGE_BACKWARD:
924 /* Do not jump backwards if at the first page */
925 if (value == lower && first_page) {
927 /* At the top of a page, assign the upper bound limit of previous page */
928 } else if (value == lower) {
929 value = upper - page_size;
930 ev_view_previous_page (view);
931 /* Jump to the top */
933 increment = compute_scroll_increment (view, GTK_SCROLL_PAGE_BACKWARD);
934 value = MAX (value - increment, lower);
937 case GTK_SCROLL_PAGE_FORWARD:
938 /* Do not jump forward if at the last page */
939 if (value == (upper - page_size) && last_page) {
941 /* At the bottom of a page, assign the lower bound limit of next page */
942 } else if (value == (upper - page_size)) {
944 ev_view_next_page (view);
945 /* Jump to the bottom */
947 increment = compute_scroll_increment (view, GTK_SCROLL_PAGE_FORWARD);
948 value = MIN (value + increment, upper - page_size);
951 case GTK_SCROLL_STEP_BACKWARD:
952 value -= step_increment;
954 case GTK_SCROLL_STEP_FORWARD:
955 value += step_increment;
957 case GTK_SCROLL_STEP_DOWN:
958 value -= step_increment / 10;
960 case GTK_SCROLL_STEP_UP:
961 value += step_increment / 10;
967 value = CLAMP (value, lower, upper - page_size);
969 gtk_adjustment_set_value (adjustment, value);
975 ensure_rectangle_is_visible (EvView *view, GdkRectangle *rect)
977 GtkWidget *widget = GTK_WIDGET (view);
978 GtkAdjustment *adjustment;
979 GtkAllocation allocation;
983 view->pending_scroll = SCROLL_TO_FIND_LOCATION;
985 gtk_widget_get_allocation (widget, &allocation);
987 adjustment = view->vadjustment;
988 adj_value = gtk_adjustment_get_value (adjustment);
990 if (rect->y < adj_value) {
991 value = MAX (gtk_adjustment_get_lower (adjustment), rect->y - MARGIN);
992 gtk_adjustment_set_value (view->vadjustment, value);
993 } else if (rect->y + rect->height > adj_value + allocation.height) {
994 value = MIN (gtk_adjustment_get_upper (adjustment), rect->y + rect->height -
995 allocation.height + MARGIN);
996 gtk_adjustment_set_value (view->vadjustment, value);
999 adjustment = view->hadjustment;
1000 adj_value = gtk_adjustment_get_value (adjustment);
1002 if (rect->x < adj_value) {
1003 value = MAX (gtk_adjustment_get_lower (adjustment), rect->x - MARGIN);
1004 gtk_adjustment_set_value (view->hadjustment, value);
1005 } else if (rect->x + rect->height > adj_value + allocation.width) {
1006 value = MIN (gtk_adjustment_get_upper (adjustment), rect->x + rect->width -
1007 allocation.width + MARGIN);
1008 gtk_adjustment_set_value (view->hadjustment, value);
1011 gtk_widget_queue_resize (GTK_WIDGET (view));
1014 /*** Geometry computations ***/
1017 compute_border (EvView *view, int width, int height, GtkBorder *border)
1019 ev_document_misc_get_page_border_size (width, height, border);
1023 _get_page_size_for_scale_and_rotation (EvDocument *document,
1033 ev_document_get_page_size (document, page, &w, &h);
1035 width = (gint)(w * scale + 0.5);
1036 height = (gint)(h * scale + 0.5);
1039 *page_width = (rotation == 0 || rotation == 180) ? width : height;
1041 *page_height = (rotation == 0 || rotation == 180) ? height : width;
1045 ev_view_get_page_size (EvView *view,
1050 _get_page_size_for_scale_and_rotation (view->document,
1059 ev_view_get_max_page_size (EvView *view,
1066 ev_document_get_max_page_size (view->document, &w, &h);
1068 width = (gint)(w * view->scale + 0.5);
1069 height = (gint)(h * view->scale + 0.5);
1072 *max_width = (view->rotation == 0 || view->rotation == 180) ? width : height;
1074 *max_height = (view->rotation == 0 || view->rotation == 180) ? height : width;
1078 get_page_y_offset (EvView *view, int page, int *y_offset)
1080 int max_width, offset;
1083 g_return_if_fail (y_offset != NULL);
1085 ev_view_get_max_page_size (view, &max_width, NULL);
1086 compute_border (view, max_width, max_width, &border);
1088 if (view->dual_page) {
1089 ev_view_get_height_to_page (view, page, NULL, &offset);
1090 offset += ((page + get_dual_even_left (view)) / 2 + 1) * view->spacing +
1091 ((page + get_dual_even_left (view)) / 2 ) * (border.top + border.bottom);
1093 ev_view_get_height_to_page (view, page, &offset, NULL);
1094 offset += (page + 1) * view->spacing + page * (border.top + border.bottom);
1102 get_page_extents (EvView *view,
1104 GdkRectangle *page_area,
1109 GtkAllocation allocation;
1111 widget = GTK_WIDGET (view);
1112 gtk_widget_get_allocation (widget, &allocation);
1114 /* Get the size of the page */
1115 ev_view_get_page_size (view, page, &width, &height);
1116 compute_border (view, width, height, border);
1117 page_area->width = width + border->left + border->right;
1118 page_area->height = height + border->top + border->bottom;
1120 if (view->continuous) {
1124 ev_view_get_max_page_size (view, &max_width, NULL);
1125 max_width = max_width + border->left + border->right;
1126 /* Get the location of the bounding box */
1127 if (view->dual_page) {
1128 x = view->spacing + ((page % 2 == get_dual_even_left (view)) ? 0 : 1) * (max_width + view->spacing);
1129 x = x + MAX (0, allocation.width - (max_width * 2 + view->spacing * 3)) / 2;
1130 if (page % 2 == get_dual_even_left (view))
1131 x = x + (max_width - width - border->left - border->right);
1134 x = x + MAX (0, allocation.width - (width + view->spacing * 2)) / 2;
1137 get_page_y_offset (view, page, &y);
1143 if (view->dual_page) {
1144 gint width_2, height_2;
1145 gint max_width = width;
1146 gint max_height = height;
1147 GtkBorder overall_border;
1150 other_page = (page % 2 == get_dual_even_left (view)) ? page + 1: page - 1;
1152 /* First, we get the bounding box of the two pages */
1153 if (other_page < ev_document_get_n_pages (view->document)
1154 && (0 <= other_page)) {
1155 ev_view_get_page_size (view, other_page,
1156 &width_2, &height_2);
1157 if (width_2 > width)
1158 max_width = width_2;
1159 if (height_2 > height)
1160 max_height = height_2;
1162 compute_border (view, max_width, max_height, &overall_border);
1164 /* Find the offsets */
1168 /* Adjust for being the left or right page */
1169 if (page % 2 == get_dual_even_left (view))
1170 x = x + max_width - width;
1172 x = x + (max_width + overall_border.left + overall_border.right) + view->spacing;
1174 y = y + (max_height - height)/2;
1176 /* Adjust for extra allocation */
1177 x = x + MAX (0, allocation.width -
1178 ((max_width + overall_border.left + overall_border.right) * 2 + view->spacing * 3))/2;
1179 y = y + MAX (0, allocation.height - (height + view->spacing * 2))/2;
1184 /* Adjust for extra allocation */
1185 x = x + MAX (0, allocation.width - (width + border->left + border->right + view->spacing * 2))/2;
1186 y = y + MAX (0, allocation.height - (height + border->top + border->bottom + view->spacing * 2))/2;
1197 get_doc_page_size (EvView *view,
1204 ev_document_get_page_size (view->document, page, &w, &h);
1205 if (view->rotation == 0 || view->rotation == 180) {
1206 if (width) *width = w;
1207 if (height) *height = h;
1209 if (width) *width = h;
1210 if (height) *height = w;
1215 view_point_to_doc_point (EvView *view,
1216 GdkPoint *view_point,
1217 GdkRectangle *page_area,
1218 double *doc_point_x,
1219 double *doc_point_y)
1221 *doc_point_x = (double) (view_point->x - page_area->x) / view->scale;
1222 *doc_point_y = (double) (view_point->y - page_area->y) / view->scale;
1226 view_rect_to_doc_rect (EvView *view,
1227 GdkRectangle *view_rect,
1228 GdkRectangle *page_area,
1229 EvRectangle *doc_rect)
1231 doc_rect->x1 = (double) (view_rect->x - page_area->x) / view->scale;
1232 doc_rect->y1 = (double) (view_rect->y - page_area->y) / view->scale;
1233 doc_rect->x2 = doc_rect->x1 + (double) view_rect->width / view->scale;
1234 doc_rect->y2 = doc_rect->y1 + (double) view_rect->height / view->scale;
1238 doc_point_to_view_point (EvView *view,
1241 GdkPoint *view_point)
1243 GdkRectangle page_area;
1245 double x, y, view_x, view_y;
1246 gdouble width, height;
1248 get_doc_page_size (view, page, &width, &height);
1250 if (view->rotation == 0) {
1253 } else if (view->rotation == 90) {
1254 x = width - doc_point->y;
1256 } else if (view->rotation == 180) {
1257 x = width - doc_point->x;
1258 y = height - doc_point->y;
1259 } else if (view->rotation == 270) {
1261 y = height - doc_point->x;
1263 g_assert_not_reached ();
1266 get_page_extents (view, page, &page_area, &border);
1268 view_x = CLAMP (x * view->scale, 0, page_area.width);
1269 view_y = CLAMP (y * view->scale, 0, page_area.height);
1270 view_point->x = view_x + page_area.x;
1271 view_point->y = view_y + page_area.y;
1275 doc_rect_to_view_rect (EvView *view,
1277 EvRectangle *doc_rect,
1278 GdkRectangle *view_rect)
1280 GdkRectangle page_area;
1283 gdouble width, height;
1285 get_doc_page_size (view, page, &width, &height);
1287 if (view->rotation == 0) {
1290 w = doc_rect->x2 - doc_rect->x1;
1291 h = doc_rect->y2 - doc_rect->y1;
1292 } else if (view->rotation == 90) {
1293 x = width - doc_rect->y2;
1295 w = doc_rect->y2 - doc_rect->y1;
1296 h = doc_rect->x2 - doc_rect->x1;
1297 } else if (view->rotation == 180) {
1298 x = width - doc_rect->x2;
1299 y = height - doc_rect->y2;
1300 w = doc_rect->x2 - doc_rect->x1;
1301 h = doc_rect->y2 - doc_rect->y1;
1302 } else if (view->rotation == 270) {
1304 y = height - doc_rect->x2;
1305 w = doc_rect->y2 - doc_rect->y1;
1306 h = doc_rect->x2 - doc_rect->x1;
1308 g_assert_not_reached ();
1311 get_page_extents (view, page, &page_area, &border);
1313 view_rect->x = x * view->scale + page_area.x;
1314 view_rect->y = y * view->scale + page_area.y;
1315 view_rect->width = w * view->scale;
1316 view_rect->height = h * view->scale;
1320 get_dual_even_left (EvView *view)
1322 return (ev_document_get_n_pages (view->document) > 2);
1326 find_page_at_location (EvView *view,
1335 if (view->document == NULL)
1339 g_assert (x_offset);
1340 g_assert (y_offset);
1342 for (i = view->start_page; i >= 0 && i <= view->end_page; i++) {
1343 GdkRectangle page_area;
1346 if (! get_page_extents (view, i, &page_area, &border))
1349 if ((x >= page_area.x + border.left) &&
1350 (x < page_area.x + page_area.width - border.right) &&
1351 (y >= page_area.y + border.top) &&
1352 (y < page_area.y + page_area.height - border.bottom)) {
1354 *x_offset = x - (page_area.x + border.left);
1355 *y_offset = y - (page_area.y + border.top);
1364 location_in_text (EvView *view,
1370 gint x_offset = 0, y_offset = 0;
1372 find_page_at_location (view, x, y, &page, &x_offset, &y_offset);
1377 region = ev_page_cache_get_text_mapping (view->page_cache, page);
1380 return gdk_region_point_in (region, x_offset / view->scale, y_offset / view->scale);
1386 location_in_selected_text (EvView *view,
1391 gint x_offset = 0, y_offset = 0;
1392 EvViewSelection *selection;
1395 for (l = view->selection_info.selections; l != NULL; l = l->next) {
1396 selection = (EvViewSelection *)l->data;
1398 find_page_at_location (view, x, y, &page, &x_offset, &y_offset);
1400 if (page != selection->page)
1403 if (selection->covered_region &&
1404 gdk_region_point_in (selection->covered_region, x_offset, y_offset))
1412 get_doc_point_from_offset (EvView *view,
1419 gdouble width, height;
1422 get_doc_page_size (view, page, &width, &height);
1424 x_offset = x_offset / view->scale;
1425 y_offset = y_offset / view->scale;
1427 if (view->rotation == 0) {
1430 } else if (view->rotation == 90) {
1432 y = width - x_offset;
1433 } else if (view->rotation == 180) {
1434 x = width - x_offset;
1435 y = height - y_offset;
1436 } else if (view->rotation == 270) {
1437 x = height - y_offset;
1440 g_assert_not_reached ();
1450 get_doc_point_from_location (EvView *view,
1457 gint x_offset = 0, y_offset = 0;
1459 x += view->scroll_x;
1460 y += view->scroll_y;
1461 find_page_at_location (view, x, y, page, &x_offset, &y_offset);
1465 return get_doc_point_from_offset (view, *page, x_offset, y_offset, x_new, y_new);
1469 ev_view_get_area_from_mapping (EvView *view,
1471 GList *mapping_list,
1477 mapping = ev_mapping_list_find (mapping_list, data);
1478 doc_rect_to_view_rect (view, page, &mapping->area, area);
1479 area->x -= view->scroll_x;
1480 area->y -= view->scroll_y;
1486 ev_view_get_link_at_location (EvView *view,
1491 gint x_new = 0, y_new = 0;
1492 GList *link_mapping;
1494 if (!EV_IS_DOCUMENT_LINKS (view->document))
1497 if (!get_doc_point_from_location (view, x, y, &page, &x_new, &y_new))
1500 link_mapping = ev_page_cache_get_link_mapping (view->page_cache, page);
1503 return ev_mapping_list_get_data (link_mapping, x_new, y_new);
1509 goto_fitr_dest (EvView *view, EvLinkDest *dest)
1512 gdouble zoom, left, top;
1513 gboolean change_left, change_top;
1514 GtkAllocation allocation;
1516 gtk_widget_get_allocation (GTK_WIDGET (view), &allocation);
1518 left = ev_link_dest_get_left (dest, &change_left);
1519 top = ev_link_dest_get_top (dest, &change_top);
1521 zoom = zoom_for_size_best_fit (ev_link_dest_get_right (dest) - left,
1522 ev_link_dest_get_bottom (dest) - top,
1526 ev_document_model_set_sizing_mode (view->model, EV_SIZING_FREE);
1527 ev_document_model_set_scale (view->model, zoom);
1529 doc_point.x = change_left ? left : 0;
1530 doc_point.y = change_top ? top : 0;
1531 view->pending_point = doc_point;
1533 ev_view_change_page (view, ev_link_dest_get_page (dest));
1537 goto_fitv_dest (EvView *view, EvLinkDest *dest)
1540 gdouble doc_width, doc_height;
1543 gboolean change_left;
1544 GtkAllocation allocation;
1546 gtk_widget_get_allocation (GTK_WIDGET (view), &allocation);
1548 page = ev_link_dest_get_page (dest);
1549 ev_document_get_page_size (view->document, page, &doc_width, &doc_height);
1551 left = ev_link_dest_get_left (dest, &change_left);
1552 doc_point.x = change_left ? left : 0;
1555 zoom = zoom_for_size_fit_height (doc_width - doc_point.x , doc_height,
1559 ev_document_model_set_sizing_mode (view->model, EV_SIZING_FREE);
1560 ev_document_model_set_scale (view->model, zoom);
1562 view->pending_point = doc_point;
1564 ev_view_change_page (view, page);
1568 goto_fith_dest (EvView *view, EvLinkDest *dest)
1571 gdouble doc_width, doc_height;
1574 gboolean change_top;
1575 GtkAllocation allocation;
1577 gtk_widget_get_allocation (GTK_WIDGET (view), &allocation);
1579 page = ev_link_dest_get_page (dest);
1580 ev_document_get_page_size (view->document, page, &doc_width, &doc_height);
1582 top = ev_link_dest_get_top (dest, &change_top);
1585 doc_point.y = change_top ? top : 0;
1587 zoom = zoom_for_size_fit_width (doc_width, top,
1591 ev_document_model_set_sizing_mode (view->model, EV_SIZING_FIT_WIDTH);
1592 ev_document_model_set_scale (view->model, zoom);
1594 view->pending_point = doc_point;
1596 ev_view_change_page (view, page);
1600 goto_fit_dest (EvView *view, EvLinkDest *dest)
1603 gdouble doc_width, doc_height;
1605 GtkAllocation allocation;
1607 gtk_widget_get_allocation (GTK_WIDGET (view), &allocation);
1609 page = ev_link_dest_get_page (dest);
1610 ev_document_get_page_size (view->document, page, &doc_width, &doc_height);
1612 zoom = zoom_for_size_best_fit (doc_width, doc_height,
1616 ev_document_model_set_sizing_mode (view->model, EV_SIZING_BEST_FIT);
1617 ev_document_model_set_scale (view->model, zoom);
1619 ev_view_change_page (view, page);
1623 goto_xyz_dest (EvView *view, EvLinkDest *dest)
1627 gdouble zoom, left, top;
1628 gboolean change_zoom, change_left, change_top;
1630 zoom = ev_link_dest_get_zoom (dest, &change_zoom);
1631 page = ev_link_dest_get_page (dest);
1633 if (change_zoom && zoom > 1) {
1634 ev_document_model_set_sizing_mode (view->model, EV_SIZING_FREE);
1635 ev_document_model_set_scale (view->model, zoom);
1638 left = ev_link_dest_get_left (dest, &change_left);
1639 top = ev_link_dest_get_top (dest, &change_top);
1641 doc_point.x = change_left ? left : 0;
1642 doc_point.y = change_top ? top : 0;
1643 view->pending_point = doc_point;
1645 ev_view_change_page (view, page);
1649 goto_dest (EvView *view, EvLinkDest *dest)
1651 EvLinkDestType type;
1652 int page, n_pages, current_page;
1654 page = ev_link_dest_get_page (dest);
1655 n_pages = ev_document_get_n_pages (view->document);
1657 if (page < 0 || page >= n_pages)
1660 current_page = view->current_page;
1662 type = ev_link_dest_get_dest_type (dest);
1665 case EV_LINK_DEST_TYPE_PAGE:
1666 ev_document_model_set_page (view->model, page);
1668 case EV_LINK_DEST_TYPE_FIT:
1669 goto_fit_dest (view, dest);
1671 case EV_LINK_DEST_TYPE_FITH:
1672 goto_fith_dest (view, dest);
1674 case EV_LINK_DEST_TYPE_FITV:
1675 goto_fitv_dest (view, dest);
1677 case EV_LINK_DEST_TYPE_FITR:
1678 goto_fitr_dest (view, dest);
1680 case EV_LINK_DEST_TYPE_XYZ:
1681 goto_xyz_dest (view, dest);
1683 case EV_LINK_DEST_TYPE_PAGE_LABEL:
1684 ev_document_model_set_page_by_label (view->model, ev_link_dest_get_page_label (dest));
1687 g_assert_not_reached ();
1690 if (current_page != view->current_page)
1691 ev_document_model_set_page (view->model, view->current_page);
1695 ev_view_goto_dest (EvView *view, EvLinkDest *dest)
1697 EvLinkDestType type;
1699 type = ev_link_dest_get_dest_type (dest);
1701 if (type == EV_LINK_DEST_TYPE_NAMED) {
1703 const gchar *named_dest;
1705 named_dest = ev_link_dest_get_named_dest (dest);
1706 dest2 = ev_document_links_find_link_dest (EV_DOCUMENT_LINKS (view->document),
1709 goto_dest (view, dest2);
1710 g_object_unref (dest2);
1716 goto_dest (view, dest);
1720 ev_view_handle_link (EvView *view, EvLink *link)
1722 EvLinkAction *action = NULL;
1723 EvLinkActionType type;
1725 action = ev_link_get_action (link);
1729 type = ev_link_action_get_action_type (action);
1732 case EV_LINK_ACTION_TYPE_GOTO_DEST: {
1735 g_signal_emit (view, signals[SIGNAL_HANDLE_LINK], 0, link);
1737 dest = ev_link_action_get_dest (action);
1738 ev_view_goto_dest (view, dest);
1741 case EV_LINK_ACTION_TYPE_GOTO_REMOTE:
1742 case EV_LINK_ACTION_TYPE_EXTERNAL_URI:
1743 case EV_LINK_ACTION_TYPE_LAUNCH:
1744 case EV_LINK_ACTION_TYPE_NAMED:
1745 g_signal_emit (view, signals[SIGNAL_EXTERNAL_LINK], 0, action);
1751 tip_from_action_named (EvLinkAction *action)
1753 const gchar *name = ev_link_action_get_name (action);
1755 if (g_ascii_strcasecmp (name, "FirstPage") == 0) {
1756 return g_strdup (_("Go to first page"));
1757 } else if (g_ascii_strcasecmp (name, "PrevPage") == 0) {
1758 return g_strdup (_("Go to previous page"));
1759 } else if (g_ascii_strcasecmp (name, "NextPage") == 0) {
1760 return g_strdup (_("Go to next page"));
1761 } else if (g_ascii_strcasecmp (name, "LastPage") == 0) {
1762 return g_strdup (_("Go to last page"));
1763 } else if (g_ascii_strcasecmp (name, "GoToPage") == 0) {
1764 return g_strdup (_("Go to page"));
1765 } else if (g_ascii_strcasecmp (name, "Find") == 0) {
1766 return g_strdup (_("Find"));
1773 tip_from_link (EvView *view, EvLink *link)
1775 EvLinkAction *action;
1776 EvLinkActionType type;
1781 action = ev_link_get_action (link);
1782 title = ev_link_get_title (link);
1785 return title ? g_strdup (title) : NULL;
1787 type = ev_link_action_get_action_type (action);
1790 case EV_LINK_ACTION_TYPE_GOTO_DEST:
1791 page_label = ev_document_links_get_dest_page_label (EV_DOCUMENT_LINKS (view->document),
1792 ev_link_action_get_dest (action));
1794 msg = g_strdup_printf (_("Go to page %s"), page_label);
1795 g_free (page_label);
1798 case EV_LINK_ACTION_TYPE_GOTO_REMOTE:
1800 msg = g_strdup_printf (_("Go to %s on file “%s”"), title,
1801 ev_link_action_get_filename (action));
1803 msg = g_strdup_printf (_("Go to file “%s”"),
1804 ev_link_action_get_filename (action));
1807 case EV_LINK_ACTION_TYPE_EXTERNAL_URI:
1808 msg = g_strdup (ev_link_action_get_uri (action));
1810 case EV_LINK_ACTION_TYPE_LAUNCH:
1811 msg = g_strdup_printf (_("Launch %s"),
1812 ev_link_action_get_filename (action));
1814 case EV_LINK_ACTION_TYPE_NAMED:
1815 msg = tip_from_action_named (action);
1819 msg = g_strdup (title);
1827 ev_view_handle_cursor_over_xy (EvView *view, gint x, gint y)
1831 EvAnnotation *annot = NULL;
1833 if (view->cursor == EV_VIEW_CURSOR_HIDDEN)
1836 if (view->drag_info.in_drag) {
1837 if (view->cursor != EV_VIEW_CURSOR_DRAG)
1838 ev_view_set_cursor (view, EV_VIEW_CURSOR_DRAG);
1842 if (view->scroll_info.autoscrolling) {
1843 if (view->cursor != EV_VIEW_CURSOR_AUTOSCROLL)
1844 ev_view_set_cursor (view, EV_VIEW_CURSOR_AUTOSCROLL);
1848 link = ev_view_get_link_at_location (view, x, y);
1850 ev_view_set_cursor (view, EV_VIEW_CURSOR_LINK);
1851 } else if ((field = ev_view_get_form_field_at_location (view, x, y))) {
1852 if (field->is_read_only) {
1853 if (view->cursor == EV_VIEW_CURSOR_LINK ||
1854 view->cursor == EV_VIEW_CURSOR_IBEAM ||
1855 view->cursor == EV_VIEW_CURSOR_DRAG)
1856 ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL);
1857 } else if (EV_IS_FORM_FIELD_TEXT (field)) {
1858 ev_view_set_cursor (view, EV_VIEW_CURSOR_IBEAM);
1860 ev_view_set_cursor (view, EV_VIEW_CURSOR_LINK);
1862 } else if ((annot = ev_view_get_annotation_at_location (view, x, y))) {
1863 ev_view_set_cursor (view, EV_VIEW_CURSOR_LINK);
1864 } else if (location_in_text (view, x + view->scroll_x, y + view->scroll_y)) {
1865 ev_view_set_cursor (view, EV_VIEW_CURSOR_IBEAM);
1867 if (view->cursor == EV_VIEW_CURSOR_LINK ||
1868 view->cursor == EV_VIEW_CURSOR_IBEAM ||
1869 view->cursor == EV_VIEW_CURSOR_DRAG ||
1870 view->cursor == EV_VIEW_CURSOR_AUTOSCROLL)
1871 ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL);
1875 g_object_set (view, "has-tooltip", TRUE, NULL);
1880 ev_view_get_image_at_location (EvView *view,
1885 gint x_new = 0, y_new = 0;
1886 GList *image_mapping;
1888 if (!EV_IS_DOCUMENT_IMAGES (view->document))
1891 if (!get_doc_point_from_location (view, x, y, &page, &x_new, &y_new))
1894 image_mapping = ev_page_cache_get_image_mapping (view->page_cache, page);
1897 return ev_mapping_list_get_data (image_mapping, x_new, y_new);
1903 static EvFormField *
1904 ev_view_get_form_field_at_location (EvView *view,
1909 gint x_new = 0, y_new = 0;
1910 GList *forms_mapping;
1912 if (!EV_IS_DOCUMENT_FORMS (view->document))
1915 if (!get_doc_point_from_location (view, x, y, &page, &x_new, &y_new))
1918 forms_mapping = ev_page_cache_get_form_field_mapping (view->page_cache, page);
1921 return ev_mapping_list_get_data (forms_mapping, x_new, y_new);
1927 ev_view_form_field_get_region (EvView *view,
1930 GdkRectangle view_area;
1931 GList *forms_mapping;
1933 forms_mapping = ev_page_cache_get_form_field_mapping (view->page_cache,
1934 field->page->index);
1935 ev_view_get_area_from_mapping (view, field->page->index,
1939 return gdk_region_rectangle (&view_area);
1943 ev_view_forms_remove_widgets (EvView *view)
1945 ev_view_remove_all (view);
1951 ev_view_form_field_destroy (GtkWidget *widget,
1954 g_idle_add ((GSourceFunc)ev_view_forms_remove_widgets, view);
1958 ev_view_form_field_button_create_widget (EvView *view,
1961 EvFormFieldButton *field_button = EV_FORM_FIELD_BUTTON (field);
1962 GdkRegion *field_region = NULL;
1964 switch (field_button->type) {
1965 case EV_FORM_FIELD_BUTTON_PUSH:
1967 case EV_FORM_FIELD_BUTTON_CHECK:
1968 case EV_FORM_FIELD_BUTTON_RADIO: {
1970 GList *forms_mapping, *l;
1972 state = ev_document_forms_form_field_button_get_state (EV_DOCUMENT_FORMS (view->document),
1975 /* FIXME: it actually depends on NoToggleToOff flags */
1976 if (field_button->type == EV_FORM_FIELD_BUTTON_RADIO &&
1977 state && field_button->state)
1980 field_region = ev_view_form_field_get_region (view, field);
1982 /* For radio buttons and checkbox buttons that are in a set
1983 * we need to update also the region for the current selected item
1985 forms_mapping = ev_page_cache_get_form_field_mapping (view->page_cache,
1986 field->page->index);
1987 for (l = forms_mapping; l; l = g_list_next (l)) {
1988 EvFormField *button = ((EvMapping *)(l->data))->data;
1989 GdkRegion *button_region;
1991 if (button->id == field->id)
1994 /* FIXME: only buttons in the same group should be updated */
1995 if (!EV_IS_FORM_FIELD_BUTTON (button) ||
1996 EV_FORM_FIELD_BUTTON (button)->type != field_button->type ||
1997 EV_FORM_FIELD_BUTTON (button)->state != TRUE)
2000 button_region = ev_view_form_field_get_region (view, button);
2001 gdk_region_union (field_region, button_region);
2002 gdk_region_destroy (button_region);
2005 ev_document_forms_form_field_button_set_state (EV_DOCUMENT_FORMS (view->document),
2007 field_button->state = !state;
2012 ev_view_reload_page (view, field->page->index, field_region);
2013 gdk_region_destroy (field_region);
2019 ev_view_form_field_text_save (EvView *view,
2024 if (!view->document)
2027 field = g_object_get_data (G_OBJECT (widget), "form-field");
2029 if (field->changed) {
2030 EvFormFieldText *field_text = EV_FORM_FIELD_TEXT (field);
2031 GdkRegion *field_region;
2033 field_region = ev_view_form_field_get_region (view, field);
2035 ev_document_forms_form_field_text_set_text (EV_DOCUMENT_FORMS (view->document),
2036 field, field_text->text);
2037 field->changed = FALSE;
2038 ev_view_reload_page (view, field->page->index, field_region);
2039 gdk_region_destroy (field_region);
2044 ev_view_form_field_text_changed (GtkWidget *widget,
2047 EvFormFieldText *field_text = EV_FORM_FIELD_TEXT (field);
2050 if (GTK_IS_ENTRY (widget)) {
2051 text = g_strdup (gtk_entry_get_text (GTK_ENTRY (widget)));
2052 } else if (GTK_IS_TEXT_BUFFER (widget)) {
2053 GtkTextIter start, end;
2055 gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (widget), &start, &end);
2056 text = gtk_text_buffer_get_text (GTK_TEXT_BUFFER (widget),
2057 &start, &end, FALSE);
2060 if (!field_text->text ||
2061 (field_text->text && g_ascii_strcasecmp (field_text->text, text) != 0)) {
2062 g_free (field_text->text);
2063 field_text->text = text;
2064 field->changed = TRUE;
2069 ev_view_form_field_text_create_widget (EvView *view,
2072 EvFormFieldText *field_text = EV_FORM_FIELD_TEXT (field);
2073 GtkWidget *text = NULL;
2076 txt = ev_document_forms_form_field_text_get_text (EV_DOCUMENT_FORMS (view->document),
2079 switch (field_text->type) {
2080 case EV_FORM_FIELD_TEXT_FILE_SELECT:
2082 case EV_FORM_FIELD_TEXT_NORMAL:
2083 text = gtk_entry_new ();
2084 gtk_entry_set_has_frame (GTK_ENTRY (text), FALSE);
2085 gtk_entry_set_max_length (GTK_ENTRY (text), field_text->max_len);
2086 gtk_entry_set_visibility (GTK_ENTRY (text), !field_text->is_password);
2089 gtk_entry_set_text (GTK_ENTRY (text), txt);
2093 g_signal_connect (text, "changed",
2094 G_CALLBACK (ev_view_form_field_text_changed),
2096 g_signal_connect_after (text, "activate",
2097 G_CALLBACK (ev_view_form_field_destroy),
2100 case EV_FORM_FIELD_TEXT_MULTILINE: {
2101 GtkTextBuffer *buffer;
2103 text = gtk_text_view_new ();
2104 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text));
2107 gtk_text_buffer_set_text (buffer, txt, -1);
2111 g_signal_connect (buffer, "changed",
2112 G_CALLBACK (ev_view_form_field_text_changed),
2118 g_object_weak_ref (G_OBJECT (text),
2119 (GWeakNotify)ev_view_form_field_text_save,
2126 ev_view_form_field_choice_save (EvView *view,
2131 if (!view->document)
2134 field = g_object_get_data (G_OBJECT (widget), "form-field");
2136 if (field->changed) {
2138 EvFormFieldChoice *field_choice = EV_FORM_FIELD_CHOICE (field);
2139 GdkRegion *field_region;
2141 field_region = ev_view_form_field_get_region (view, field);
2143 if (field_choice->is_editable) {
2144 ev_document_forms_form_field_choice_set_text (EV_DOCUMENT_FORMS (view->document),
2145 field, field_choice->text);
2147 ev_document_forms_form_field_choice_unselect_all (EV_DOCUMENT_FORMS (view->document), field);
2148 for (l = field_choice->selected_items; l; l = g_list_next (l)) {
2149 ev_document_forms_form_field_choice_select_item (EV_DOCUMENT_FORMS (view->document),
2151 GPOINTER_TO_INT (l->data));
2154 field->changed = FALSE;
2155 ev_view_reload_page (view, field->page->index, field_region);
2156 gdk_region_destroy (field_region);
2161 ev_view_form_field_choice_changed (GtkWidget *widget,
2164 EvFormFieldChoice *field_choice = EV_FORM_FIELD_CHOICE (field);
2166 if (GTK_IS_COMBO_BOX (widget)) {
2169 item = gtk_combo_box_get_active (GTK_COMBO_BOX (widget));
2170 if (!field_choice->selected_items ||
2171 GPOINTER_TO_INT (field_choice->selected_items->data) != item) {
2172 g_list_free (field_choice->selected_items);
2173 field_choice->selected_items = NULL;
2174 field_choice->selected_items = g_list_prepend (field_choice->selected_items,
2175 GINT_TO_POINTER (item));
2176 field->changed = TRUE;
2179 if (GTK_IS_COMBO_BOX_ENTRY (widget)) {
2182 text = gtk_combo_box_get_active_text (GTK_COMBO_BOX (widget));
2183 if (!field_choice->text ||
2184 (field_choice->text && g_ascii_strcasecmp (field_choice->text, text) != 0)) {
2185 g_free (field_choice->text);
2186 field_choice->text = text;
2187 field->changed = TRUE;
2190 } else if (GTK_IS_TREE_SELECTION (widget)) {
2191 GtkTreeSelection *selection = GTK_TREE_SELECTION (widget);
2192 GtkTreeModel *model;
2195 items = gtk_tree_selection_get_selected_rows (selection, &model);
2196 g_list_free (field_choice->selected_items);
2197 field_choice->selected_items = NULL;
2199 for (l = items; l && l->data; l = g_list_next (l)) {
2201 GtkTreePath *path = (GtkTreePath *)l->data;
2204 gtk_tree_model_get_iter (model, &iter, path);
2205 gtk_tree_model_get (model, &iter, 1, &item, -1);
2207 field_choice->selected_items = g_list_prepend (field_choice->selected_items,
2208 GINT_TO_POINTER (item));
2210 gtk_tree_path_free (path);
2213 g_list_free (items);
2215 field->changed = TRUE;
2220 ev_view_form_field_choice_create_widget (EvView *view,
2223 EvFormFieldChoice *field_choice = EV_FORM_FIELD_CHOICE (field);
2225 GtkTreeModel *model;
2227 gint selected_item = 0;
2229 n_items = ev_document_forms_form_field_choice_get_n_items (EV_DOCUMENT_FORMS (view->document),
2231 model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
2232 for (i = 0; i < n_items; i++) {
2236 item = ev_document_forms_form_field_choice_get_item (EV_DOCUMENT_FORMS (view->document),
2238 if (ev_document_forms_form_field_choice_is_item_selected (
2239 EV_DOCUMENT_FORMS (view->document), field, i)) {
2241 /* FIXME: we need a get_selected_items function in poppler */
2242 field_choice->selected_items = g_list_prepend (field_choice->selected_items,
2243 GINT_TO_POINTER (i));
2247 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
2248 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
2256 if (field_choice->type == EV_FORM_FIELD_CHOICE_LIST) {
2257 GtkCellRenderer *renderer;
2258 GtkWidget *tree_view;
2259 GtkTreeSelection *selection;
2261 tree_view = gtk_tree_view_new_with_model (model);
2262 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (tree_view), FALSE);
2264 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));
2265 if (field_choice->multi_select) {
2266 gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
2269 /* TODO: set selected items */
2271 renderer = gtk_cell_renderer_text_new ();
2272 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view),
2278 choice = gtk_scrolled_window_new (NULL, NULL);
2279 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (choice),
2280 GTK_POLICY_AUTOMATIC,
2281 GTK_POLICY_AUTOMATIC);
2282 gtk_container_add (GTK_CONTAINER (choice), tree_view);
2283 gtk_widget_show (tree_view);
2285 g_signal_connect (selection, "changed",
2286 G_CALLBACK (ev_view_form_field_choice_changed),
2288 g_signal_connect_after (selection, "changed",
2289 G_CALLBACK (ev_view_form_field_destroy),
2291 } else if (field_choice->is_editable) { /* ComboBoxEntry */
2294 choice = gtk_combo_box_entry_new_with_model (model, 0);
2295 text = ev_document_forms_form_field_choice_get_text (EV_DOCUMENT_FORMS (view->document), field);
2297 gtk_entry_set_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (choice))), text);
2301 g_signal_connect (choice, "changed",
2302 G_CALLBACK (ev_view_form_field_choice_changed),
2304 g_signal_connect_after (gtk_bin_get_child (GTK_BIN (choice)),
2306 G_CALLBACK (ev_view_form_field_destroy),
2308 } else { /* ComboBoxText */
2309 GtkCellRenderer *renderer;
2311 choice = gtk_combo_box_new_with_model (model);
2312 renderer = gtk_cell_renderer_text_new ();
2313 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (choice),
2315 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (choice),
2319 gtk_combo_box_set_active (GTK_COMBO_BOX (choice), selected_item);
2320 gtk_combo_box_popup (GTK_COMBO_BOX (choice));
2322 g_signal_connect (choice, "changed",
2323 G_CALLBACK (ev_view_form_field_choice_changed),
2325 g_signal_connect_after (choice, "changed",
2326 G_CALLBACK (ev_view_form_field_destroy),
2330 g_object_unref (model);
2332 g_object_weak_ref (G_OBJECT (choice),
2333 (GWeakNotify)ev_view_form_field_choice_save,
2340 ev_view_handle_form_field (EvView *view,
2345 GtkWidget *field_widget = NULL;
2346 GList *form_field_mapping;
2347 GdkRectangle view_area;
2349 if (field->is_read_only)
2352 if (EV_IS_FORM_FIELD_BUTTON (field)) {
2353 field_widget = ev_view_form_field_button_create_widget (view, field);
2354 } else if (EV_IS_FORM_FIELD_TEXT (field)) {
2355 field_widget = ev_view_form_field_text_create_widget (view, field);
2356 } else if (EV_IS_FORM_FIELD_CHOICE (field)) {
2357 field_widget = ev_view_form_field_choice_create_widget (view, field);
2358 } else if (EV_IS_FORM_FIELD_SIGNATURE (field)) {
2362 /* Form field doesn't require a widget */
2366 g_object_set_data_full (G_OBJECT (field_widget), "form-field",
2367 g_object_ref (field),
2368 (GDestroyNotify)g_object_unref);
2370 form_field_mapping = ev_page_cache_get_form_field_mapping (view->page_cache,
2371 field->page->index);
2372 ev_view_get_area_from_mapping (view, field->page->index,
2376 gtk_layout_put (GTK_LAYOUT (view), field_widget, view_area.x, view_area.y);
2377 gtk_widget_show (field_widget);
2378 gtk_widget_grab_focus (field_widget);
2382 static EvViewWindowChild *
2383 ev_view_get_window_child (EvView *view,
2386 GList *children = view->window_children;
2389 EvViewWindowChild *child;
2391 child = (EvViewWindowChild *)children->data;
2392 children = children->next;
2394 if (child->window == window)
2402 ev_view_window_child_move (EvView *view,
2403 EvViewWindowChild *child,
2409 gtk_window_move (GTK_WINDOW (child->window),
2410 MAX (child->parent_x, x),
2411 MAX (child->parent_y, y));
2415 ev_view_window_child_move_with_parent (EvView *view,
2418 EvViewWindowChild *child;
2419 gint root_x, root_y;
2421 child = ev_view_get_window_child (view, window);
2422 gdk_window_get_origin (gtk_widget_get_window (GTK_WIDGET (view)),
2424 if (root_x != child->parent_x || root_y != child->parent_y) {
2425 gint dest_x, dest_y;
2427 dest_x = child->x + (root_x - child->parent_x);
2428 dest_y = child->y + (root_y - child->parent_y);
2429 child->parent_x = root_x;
2430 child->parent_y = root_y;
2431 ev_view_window_child_move (view, child, dest_x, dest_y);
2434 if (child->visible && !gtk_widget_get_visible (window))
2435 gtk_widget_show (window);
2439 ev_view_window_child_put (EvView *view,
2447 EvViewWindowChild *child;
2448 gint root_x, root_y;
2450 gdk_window_get_origin (gtk_widget_get_window (GTK_WIDGET (view)),
2453 child = g_new0 (EvViewWindowChild, 1);
2454 child->window = window;
2456 child->orig_x = orig_x;
2457 child->orig_y = orig_y;
2458 child->parent_x = root_x;
2459 child->parent_y = root_y;
2460 child->visible = ev_annotation_window_is_open (EV_ANNOTATION_WINDOW (window));
2461 ev_view_window_child_move (view, child, x + root_x, y + root_y);
2464 gtk_widget_show (window);
2466 gtk_widget_hide (window);
2468 view->window_children = g_list_append (view->window_children, child);
2471 static EvViewWindowChild *
2472 ev_view_find_window_child_for_annot (EvView *view,
2474 EvAnnotation *annot)
2476 GList *children = view->window_children;
2479 EvViewWindowChild *child;
2480 EvAnnotation *wannot;
2482 child = (EvViewWindowChild *)children->data;
2483 children = children->next;
2485 if (child->page != page)
2488 wannot = ev_annotation_window_get_annotation (EV_ANNOTATION_WINDOW (child->window));
2489 if (wannot == annot || strcmp (wannot->name, annot->name) == 0)
2497 ev_view_window_children_free (EvView *view)
2501 if (!view->window_children)
2504 for (l = view->window_children; l && l->data; l = g_list_next (l)) {
2505 EvViewWindowChild *child;
2507 child = (EvViewWindowChild *)l->data;
2508 gtk_widget_destroy (GTK_WIDGET (child->window));
2511 g_list_free (view->window_children);
2512 view->window_children = NULL;
2513 view->window_child_focus = NULL;
2517 annotation_window_grab_focus (GtkWidget *widget,
2520 if (view->window_child_focus)
2521 ev_annotation_window_ungrab_focus (EV_ANNOTATION_WINDOW (view->window_child_focus->window));
2522 view->window_child_focus = ev_view_get_window_child (view, widget);
2526 annotation_window_closed (EvAnnotationWindow *window,
2529 EvViewWindowChild *child;
2531 child = ev_view_get_window_child (view, GTK_WIDGET (window));
2532 child->visible = FALSE;
2536 annotation_window_moved (EvAnnotationWindow *window,
2541 EvViewWindowChild *child;
2542 GdkRectangle page_area;
2544 GdkRectangle view_rect;
2545 EvRectangle doc_rect;
2548 child = ev_view_get_window_child (view, GTK_WIDGET (window));
2549 if (child->x == x && child->y == y)
2552 child->moved = TRUE;
2556 /* Window has been moved by the user,
2557 * we have to set a new origin in doc coords
2559 gtk_window_get_size (GTK_WINDOW (window), &width, &height);
2560 view_rect.x = (x - child->parent_x) + view->scroll_x;
2561 view_rect.y = (y - child->parent_y) + view->scroll_y;
2562 view_rect.width = width;
2563 view_rect.height = height;
2565 get_page_extents (view, child->page, &page_area, &border);
2566 view_rect_to_doc_rect (view, &view_rect, &page_area, &doc_rect);
2567 child->orig_x = doc_rect.x1;
2568 child->orig_y = doc_rect.y1;
2572 ev_view_annotation_save (EvView *view,
2573 EvAnnotation *annot)
2575 if (!view->document)
2578 if (!annot->changed)
2581 ev_document_annotations_annotation_set_contents (EV_DOCUMENT_ANNOTATIONS (view->document),
2582 annot, annot->contents);
2583 annot->changed = FALSE;
2587 show_annotation_windows (EvView *view,
2593 parent = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (view)));
2595 annots = ev_page_cache_get_annot_mapping (view->page_cache, page);
2597 for (l = annots; l && l->data; l = g_list_next (l)) {
2598 EvAnnotation *annot;
2599 EvViewWindowChild *child;
2601 EvRectangle *doc_rect;
2602 GdkRectangle view_rect;
2604 annot = ((EvMapping *)(l->data))->data;
2606 if (!EV_IS_ANNOTATION_MARKUP (annot))
2609 if (!ev_annotation_markup_has_popup (EV_ANNOTATION_MARKUP (annot)))
2612 window = g_object_get_data (G_OBJECT (annot), "popup");
2614 ev_view_window_child_move_with_parent (view, window);
2618 /* Look if we already have a popup for this annot */
2619 child = ev_view_find_window_child_for_annot (view, page, annot);
2620 window = child ? child->window : NULL;
2622 ev_annotation_window_set_annotation (EV_ANNOTATION_WINDOW (window), annot);
2623 g_object_set_data (G_OBJECT (annot), "popup", window);
2624 ev_view_window_child_move_with_parent (view, window);
2626 window = ev_annotation_window_new (annot, parent);
2627 g_signal_connect (window, "grab_focus",
2628 G_CALLBACK (annotation_window_grab_focus),
2630 g_signal_connect (window, "closed",
2631 G_CALLBACK (annotation_window_closed),
2633 g_signal_connect (window, "moved",
2634 G_CALLBACK (annotation_window_moved),
2636 g_object_set_data (G_OBJECT (annot), "popup", window);
2638 doc_rect = (EvRectangle *)ev_annotation_window_get_rectangle (EV_ANNOTATION_WINDOW (window));
2639 doc_rect_to_view_rect (view, page, doc_rect, &view_rect);
2640 view_rect.x -= view->scroll_x;
2641 view_rect.y -= view->scroll_y;
2643 ev_view_window_child_put (view, window, page,
2644 view_rect.x, view_rect.y,
2645 doc_rect->x1, doc_rect->y1);
2647 g_object_weak_ref (G_OBJECT (annot),
2648 (GWeakNotify)ev_view_annotation_save,
2655 hide_annotation_windows (EvView *view,
2660 annots = ev_page_cache_get_annot_mapping (view->page_cache, page);
2662 for (l = annots; l && l->data; l = g_list_next (l)) {
2663 EvAnnotation *annot;
2666 annot = ((EvMapping *)(l->data))->data;
2668 if (!EV_IS_ANNOTATION_MARKUP (annot))
2671 window = g_object_get_data (G_OBJECT (annot), "popup");
2673 gtk_widget_hide (window);
2677 static EvAnnotation *
2678 ev_view_get_annotation_at_location (EvView *view,
2683 gint x_new = 0, y_new = 0;
2684 GList *annotations_mapping;
2686 if (!EV_IS_DOCUMENT_ANNOTATIONS (view->document))
2689 if (!get_doc_point_from_location (view, x, y, &page, &x_new, &y_new))
2692 annotations_mapping = ev_page_cache_get_annot_mapping (view->page_cache, page);
2694 if (annotations_mapping)
2695 return ev_mapping_list_get_data (annotations_mapping, x_new, y_new);
2701 ev_view_handle_annotation (EvView *view,
2702 EvAnnotation *annot,
2707 if (EV_IS_ANNOTATION_MARKUP (annot)) {
2710 window = g_object_get_data (G_OBJECT (annot), "popup");
2712 EvViewWindowChild *child;
2714 child = ev_view_get_window_child (view, window);
2715 if (!child->visible) {
2716 child->visible = TRUE;
2717 ev_view_window_child_move (view, child, child->x, child->y);
2718 gtk_widget_show (window);
2723 if (EV_IS_ANNOTATION_ATTACHMENT (annot)) {
2724 EvAttachment *attachment = EV_ANNOTATION_ATTACHMENT (annot)->attachment;
2727 GError *error = NULL;
2729 ev_attachment_open (attachment,
2730 gtk_widget_get_screen (GTK_WIDGET (view)),
2735 g_warning ("%s", error->message);
2736 g_error_free (error);
2742 /*** GtkWidget implementation ***/
2745 ev_view_size_request_continuous_dual_page (EvView *view,
2746 GtkRequisition *requisition)
2750 n_pages = ev_document_get_n_pages (view->document) + 1;
2751 get_page_y_offset (view, n_pages, &requisition->height);
2753 switch (view->sizing_mode) {
2754 case EV_SIZING_FIT_WIDTH:
2755 case EV_SIZING_BEST_FIT:
2756 requisition->width = 1;
2759 case EV_SIZING_FREE: {
2763 ev_view_get_max_page_size (view, &max_width, NULL);
2764 compute_border (view, max_width, max_width, &border);
2765 requisition->width = (max_width + border.left + border.right) * 2 + (view->spacing * 3);
2769 g_assert_not_reached ();
2774 ev_view_size_request_continuous (EvView *view,
2775 GtkRequisition *requisition)
2779 n_pages = ev_document_get_n_pages (view->document);
2780 get_page_y_offset (view, n_pages, &requisition->height);
2782 switch (view->sizing_mode) {
2783 case EV_SIZING_FIT_WIDTH:
2784 case EV_SIZING_BEST_FIT:
2785 requisition->width = 1;
2788 case EV_SIZING_FREE: {
2792 ev_view_get_max_page_size (view, &max_width, NULL);
2793 compute_border (view, max_width, max_width, &border);
2794 requisition->width = max_width + (view->spacing * 2) + border.left + border.right;
2798 g_assert_not_reached ();
2803 ev_view_size_request_dual_page (EvView *view,
2804 GtkRequisition *requisition)
2809 if (view->sizing_mode == EV_SIZING_BEST_FIT) {
2810 requisition->width = 1;
2811 requisition->height = 1;
2816 /* Find the largest of the two. */
2817 ev_view_get_page_size (view,
2820 if (view->current_page + 1 < ev_document_get_n_pages (view->document)) {
2821 gint width_2, height_2;
2822 ev_view_get_page_size (view,
2823 view->current_page + 1,
2824 &width_2, &height_2);
2825 if (width_2 > width) {
2830 compute_border (view, width, height, &border);
2832 requisition->width = view->sizing_mode == EV_SIZING_FIT_WIDTH ? 1 :
2833 ((width + border.left + border.right) * 2) + (view->spacing * 3);
2834 requisition->height = (height + border.top + border.bottom) + (view->spacing * 2);
2838 ev_view_size_request_single_page (EvView *view,
2839 GtkRequisition *requisition)
2844 if (view->sizing_mode == EV_SIZING_BEST_FIT) {
2845 requisition->width = 1;
2846 requisition->height = 1;
2851 ev_view_get_page_size (view, view->current_page, &width, &height);
2852 compute_border (view, width, height, &border);
2854 requisition->width = view->sizing_mode == EV_SIZING_FIT_WIDTH ? 1 :
2855 width + border.left + border.right + (2 * view->spacing);
2856 requisition->height = height + border.top + border.bottom + (2 * view->spacing);
2860 ev_view_size_request (GtkWidget *widget,
2861 GtkRequisition *requisition)
2863 EvView *view = EV_VIEW (widget);
2865 if (view->document == NULL) {
2866 view->requisition.width = 1;
2867 view->requisition.height = 1;
2869 *requisition = view->requisition;
2874 /* Get zoom for size here when not called from
2875 * ev_view_size_allocate()
2877 if (!view->internal_size_request &&
2878 (view->sizing_mode == EV_SIZING_FIT_WIDTH ||
2879 view->sizing_mode == EV_SIZING_BEST_FIT)) {
2880 GtkAllocation allocation;
2882 gtk_widget_get_allocation (widget, &allocation);
2883 ev_view_zoom_for_size (view,
2888 if (view->continuous && view->dual_page)
2889 ev_view_size_request_continuous_dual_page (view, &view->requisition);
2890 else if (view->continuous)
2891 ev_view_size_request_continuous (view, &view->requisition);
2892 else if (view->dual_page)
2893 ev_view_size_request_dual_page (view, &view->requisition);
2895 ev_view_size_request_single_page (view, &view->requisition);
2897 *requisition = view->requisition;
2901 ev_view_size_allocate (GtkWidget *widget,
2902 GtkAllocation *allocation)
2904 EvView *view = EV_VIEW (widget);
2905 GList *children, *l;
2906 gint root_x, root_y;
2908 GTK_WIDGET_CLASS (ev_view_parent_class)->size_allocate (widget, allocation);
2910 if (!view->document)
2913 if (view->sizing_mode == EV_SIZING_FIT_WIDTH ||
2914 view->sizing_mode == EV_SIZING_BEST_FIT) {
2917 ev_view_zoom_for_size (view,
2919 allocation->height);
2920 view->internal_size_request = TRUE;
2921 ev_view_size_request (widget, &req);
2922 view->internal_size_request = FALSE;
2925 view_set_adjustment_values (view, GTK_ORIENTATION_HORIZONTAL);
2926 view_set_adjustment_values (view, GTK_ORIENTATION_VERTICAL);
2929 view_update_range_and_current_page (view);
2931 view->pending_scroll = SCROLL_TO_KEEP_POSITION;
2932 view->pending_resize = FALSE;
2933 view->pending_point.x = 0;
2934 view->pending_point.y = 0;
2936 children = gtk_container_get_children (GTK_CONTAINER (widget));
2937 for (l = children; l && l->data; l = g_list_next (l)) {
2939 GdkRectangle view_area;
2940 GList *form_field_mapping;
2941 GtkAllocation child_allocation;
2942 GtkRequisition child_requisition;
2943 GtkWidget *child = (GtkWidget *)l->data;
2945 field = g_object_get_data (G_OBJECT (child), "form-field");
2949 form_field_mapping = ev_page_cache_get_form_field_mapping (view->page_cache,
2950 field->page->index);
2951 ev_view_get_area_from_mapping (view, field->page->index,
2955 gtk_widget_size_request (child, &child_requisition);
2956 if (child_requisition.width != view_area.width ||
2957 child_requisition.height != view_area.height)
2958 gtk_widget_set_size_request (child, view_area.width, view_area.height);
2960 gtk_container_child_get (GTK_CONTAINER (widget),
2962 "x", &child_allocation.x,
2963 "y", &child_allocation.y,
2965 if (child_allocation.x != view_area.x ||
2966 child_allocation.y != view_area.y) {
2967 gtk_layout_move (GTK_LAYOUT (widget), child, view_area.x, view_area.y);
2970 g_list_free (children);
2972 if (view->window_children)
2973 gdk_window_get_origin (gtk_widget_get_window (GTK_WIDGET (view)),
2976 for (l = view->window_children; l && l->data; l = g_list_next (l)) {
2977 EvViewWindowChild *child;
2978 EvRectangle doc_rect;
2979 GdkRectangle view_rect;
2981 child = (EvViewWindowChild *)l->data;
2983 doc_rect = *ev_annotation_window_get_rectangle (EV_ANNOTATION_WINDOW (child->window));
2985 doc_rect.x1 = child->orig_x;
2986 doc_rect.y1 = child->orig_y;
2988 doc_rect_to_view_rect (view, child->page, &doc_rect, &view_rect);
2989 view_rect.x -= view->scroll_x;
2990 view_rect.y -= view->scroll_y;
2992 if (view_rect.x != child->orig_x || view_rect.y != child->orig_y) {
2993 child->parent_x = root_x;
2994 child->parent_y = root_y;
2995 ev_view_window_child_move (view, child, view_rect.x + root_x, view_rect.y + root_y);
3001 ev_view_realize (GtkWidget *widget)
3003 EvView *view = EV_VIEW (widget);
3004 GdkWindow *bin_window;
3007 if (GTK_WIDGET_CLASS (ev_view_parent_class)->realize)
3008 (* GTK_WIDGET_CLASS (ev_view_parent_class)->realize) (widget);
3010 bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (view));
3011 gdk_window_set_events (bin_window,
3012 (gdk_window_get_events (bin_window) |
3014 GDK_BUTTON_PRESS_MASK |
3015 GDK_BUTTON_RELEASE_MASK |
3017 GDK_KEY_PRESS_MASK |
3018 GDK_POINTER_MOTION_MASK |
3019 GDK_POINTER_MOTION_HINT_MASK |
3020 GDK_ENTER_NOTIFY_MASK |
3021 GDK_LEAVE_NOTIFY_MASK));
3023 style = gtk_widget_get_style (widget);
3024 gdk_window_set_background (bin_window, &style->mid[GTK_STATE_NORMAL]);
3026 on_adjustment_value_changed (NULL, view);
3030 ev_view_scroll_event (GtkWidget *widget, GdkEventScroll *event)
3032 EvView *view = EV_VIEW (widget);
3035 state = event->state & gtk_accelerator_get_default_mod_mask ();
3037 if (state == GDK_CONTROL_MASK) {
3038 ev_document_model_set_sizing_mode (view->model, EV_SIZING_FREE);
3039 if (event->direction == GDK_SCROLL_UP ||
3040 event->direction == GDK_SCROLL_LEFT) {
3041 if (ev_view_can_zoom_in (view)) {
3042 ev_view_zoom_in (view);
3045 if (ev_view_can_zoom_out (view)) {
3046 ev_view_zoom_out (view);
3053 view->jump_to_find_result = FALSE;
3055 /* Shift+Wheel scrolls the in the perpendicular direction */
3056 if (state & GDK_SHIFT_MASK) {
3057 if (event->direction == GDK_SCROLL_UP)
3058 event->direction = GDK_SCROLL_LEFT;
3059 else if (event->direction == GDK_SCROLL_LEFT)
3060 event->direction = GDK_SCROLL_UP;
3061 else if (event->direction == GDK_SCROLL_DOWN)
3062 event->direction = GDK_SCROLL_RIGHT;
3063 else if (event->direction == GDK_SCROLL_RIGHT)
3064 event->direction = GDK_SCROLL_DOWN;
3066 event->state &= ~GDK_SHIFT_MASK;
3067 state &= ~GDK_SHIFT_MASK;
3070 if (state == 0 && view->sizing_mode == EV_SIZING_BEST_FIT && !view->continuous) {
3071 switch (event->direction) {
3072 case GDK_SCROLL_DOWN:
3073 case GDK_SCROLL_RIGHT:
3074 ev_view_next_page (view);
3077 case GDK_SCROLL_LEFT:
3078 ev_view_previous_page (view);
3088 static EvViewSelection *
3089 find_selection_for_page (EvView *view,
3094 for (list = view->selection_info.selections; list != NULL; list = list->next) {
3095 EvViewSelection *selection;
3097 selection = (EvViewSelection *) list->data;
3099 if (selection->page == page)
3107 ev_view_expose_event (GtkWidget *widget,
3108 GdkEventExpose *event)
3110 EvView *view = EV_VIEW (widget);
3111 GdkWindow *bin_window;
3115 if (view->loading) {
3116 show_loading_window (view);
3117 } else if (view->loading_window &&
3118 gtk_widget_get_visible (view->loading_window)) {
3119 ev_view_loading_window_move (view);
3122 if (view->document == NULL)
3125 bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (view));
3126 cr = gdk_cairo_create (bin_window);
3128 for (i = view->start_page; i >= 0 && i <= view->end_page; i++) {
3129 GdkRectangle page_area;
3131 gboolean page_ready = TRUE;
3133 if (!get_page_extents (view, i, &page_area, &border))
3136 page_area.x -= view->scroll_x;
3137 page_area.y -= view->scroll_y;
3139 draw_one_page (view, i, cr, &page_area, &border, &(event->area), &page_ready);
3141 if (page_ready && view->find_pages && view->highlight_find_results)
3142 highlight_find_results (view, i);
3143 if (page_ready && EV_IS_DOCUMENT_ANNOTATIONS (view->document))
3144 show_annotation_windows (view, i);
3149 if (GTK_WIDGET_CLASS (ev_view_parent_class)->expose_event)
3150 (* GTK_WIDGET_CLASS (ev_view_parent_class)->expose_event) (widget, event);
3156 ev_view_do_popup_menu (EvView *view,
3160 GList *items = NULL;
3163 EvAnnotation *annot;
3165 image = ev_view_get_image_at_location (view, x, y);
3167 items = g_list_prepend (items, image);
3169 link = ev_view_get_link_at_location (view, x, y);
3171 items = g_list_prepend (items, link);
3173 annot = ev_view_get_annotation_at_location (view, x, y);
3175 items = g_list_prepend (items, annot);
3177 g_signal_emit (view, signals[SIGNAL_POPUP_MENU], 0, items);
3179 g_list_free (items);
3185 ev_view_popup_menu (GtkWidget *widget)
3189 gtk_widget_get_pointer (widget, &x, &y);
3190 return ev_view_do_popup_menu (EV_VIEW (widget), x, y);
3194 get_link_area (EvView *view,
3200 GList *link_mapping;
3202 gint x_offset = 0, y_offset = 0;
3204 x += view->scroll_x;
3205 y += view->scroll_y;
3207 find_page_at_location (view, x, y, &page, &x_offset, &y_offset);
3209 link_mapping = ev_page_cache_get_link_mapping (view->page_cache, page);
3210 ev_view_get_area_from_mapping (view, page,
3216 get_annot_area (EvView *view,
3219 EvAnnotation *annot,
3222 GList *annot_mapping;
3224 gint x_offset = 0, y_offset = 0;
3226 x += view->scroll_x;
3227 y += view->scroll_y;
3229 find_page_at_location (view, x, y, &page, &x_offset, &y_offset);
3231 annot_mapping = ev_page_cache_get_annot_mapping (view->page_cache, page);
3232 ev_view_get_area_from_mapping (view, page,
3238 ev_view_query_tooltip (GtkWidget *widget,
3241 gboolean keyboard_tip,
3242 GtkTooltip *tooltip)
3244 EvView *view = EV_VIEW (widget);
3246 EvAnnotation *annot;
3249 annot = ev_view_get_annotation_at_location (view, x, y);
3250 if (annot && annot->contents) {
3251 GdkRectangle annot_area;
3253 get_annot_area (view, x, y, annot, &annot_area);
3254 gtk_tooltip_set_text (tooltip, annot->contents);
3255 gtk_tooltip_set_tip_area (tooltip, &annot_area);
3260 link = ev_view_get_link_at_location (view, x, y);
3264 text = tip_from_link (view, link);
3265 if (text && g_utf8_validate (text, -1, NULL)) {
3266 GdkRectangle link_area;
3268 get_link_area (view, x, y, link, &link_area);
3269 gtk_tooltip_set_text (tooltip, text);
3270 gtk_tooltip_set_tip_area (tooltip, &link_area);
3278 start_selection_for_event (EvView *view,
3279 GdkEventButton *event)
3281 clear_selection (view);
3283 view->selection_info.start.x = event->x + view->scroll_x;
3284 view->selection_info.start.y = event->y + view->scroll_y;
3286 switch (event->type) {
3287 case GDK_2BUTTON_PRESS:
3288 view->selection_info.style = EV_SELECTION_STYLE_WORD;
3290 case GDK_3BUTTON_PRESS:
3291 view->selection_info.style = EV_SELECTION_STYLE_LINE;
3294 view->selection_info.style = EV_SELECTION_STYLE_GLYPH;
3298 /* In case of WORD or LINE, compute selections now */
3299 compute_selections (view,
3300 view->selection_info.style,
3301 &(view->selection_info.start),
3302 &(view->selection_info.start));
3306 ev_view_button_press_event (GtkWidget *widget,
3307 GdkEventButton *event)
3309 EvView *view = EV_VIEW (widget);
3311 if (!view->document)
3314 if (!gtk_widget_has_focus (widget)) {
3315 gtk_widget_grab_focus (widget);
3318 if (view->window_child_focus) {
3319 EvAnnotationWindow *window;
3320 EvAnnotation *annot;
3322 window = EV_ANNOTATION_WINDOW (view->window_child_focus->window);
3323 annot = ev_annotation_window_get_annotation (window);
3324 ev_annotation_window_ungrab_focus (window);
3325 ev_view_annotation_save (view, annot);
3326 view->window_child_focus = NULL;
3329 view->pressed_button = event->button;
3330 view->selection_info.in_drag = FALSE;
3332 if (view->scroll_info.autoscrolling)
3335 switch (event->button) {
3338 EvAnnotation *annot;
3341 if (EV_IS_SELECTION (view->document) && view->selection_info.selections) {
3342 if (event->type == GDK_3BUTTON_PRESS) {
3343 start_selection_for_event (view, event);
3344 } else if (location_in_selected_text (view,
3345 event->x + view->scroll_x,
3346 event->y + view->scroll_y)) {
3347 view->selection_info.in_drag = TRUE;
3349 start_selection_for_event (view, event);
3352 gtk_widget_queue_draw (widget);
3353 } else if ((annot = ev_view_get_annotation_at_location (view, event->x, event->y))) {
3354 ev_view_handle_annotation (view, annot, event->x, event->y, event->time);
3355 } else if ((field = ev_view_get_form_field_at_location (view, event->x, event->y))) {
3356 ev_view_remove_all (view);
3357 ev_view_handle_form_field (view, field, event->x, event->y);
3358 } else if (!location_in_text (view, event->x + view->scroll_x, event->y + view->scroll_y) &&
3359 (image = ev_view_get_image_at_location (view, event->x, event->y))) {
3360 if (view->image_dnd_info.image)
3361 g_object_unref (view->image_dnd_info.image);
3362 view->image_dnd_info.image = g_object_ref (image);
3363 view->image_dnd_info.in_drag = TRUE;
3365 view->image_dnd_info.start.x = event->x + view->scroll_x;
3366 view->image_dnd_info.start.y = event->y + view->scroll_y;
3368 ev_view_remove_all (view);
3370 if (EV_IS_SELECTION (view->document))
3371 start_selection_for_event (view, event);
3376 /* use root coordinates as reference point because
3377 * scrolling changes window relative coordinates */
3378 view->drag_info.start.x = event->x_root;
3379 view->drag_info.start.y = event->y_root;
3380 view->drag_info.hadj = gtk_adjustment_get_value (view->hadjustment);
3381 view->drag_info.vadj = gtk_adjustment_get_value (view->vadjustment);
3383 ev_view_set_cursor (view, EV_VIEW_CURSOR_DRAG);
3387 view->scroll_info.start_y = event->y;
3388 return ev_view_do_popup_menu (view, event->x, event->y);
3395 ev_view_remove_all (EvView *view)
3397 GList *children, *child;
3399 children = gtk_container_get_children (GTK_CONTAINER (view));
3400 for (child = children; child && child->data; child = g_list_next (child)) {
3401 gtk_container_remove (GTK_CONTAINER (view),
3402 GTK_WIDGET (child->data));
3404 g_list_free (children);
3407 /*** Drag and Drop ***/
3409 ev_view_drag_data_get (GtkWidget *widget,
3410 GdkDragContext *context,
3411 GtkSelectionData *selection_data,
3415 EvView *view = EV_VIEW (widget);
3418 case TARGET_DND_TEXT:
3419 if (EV_IS_SELECTION (view->document) &&
3420 view->selection_info.selections) {
3423 text = get_selected_text (view);
3424 gtk_selection_data_set_text (selection_data,
3430 case TARGET_DND_IMAGE:
3431 if (view->image_dnd_info.image) {
3434 ev_document_doc_mutex_lock ();
3435 pixbuf = ev_document_images_get_image (EV_DOCUMENT_IMAGES (view->document),
3436 view->image_dnd_info.image);
3437 ev_document_doc_mutex_unlock ();
3439 gtk_selection_data_set_pixbuf (selection_data, pixbuf);
3440 g_object_unref (pixbuf);
3443 case TARGET_DND_URI:
3444 if (view->image_dnd_info.image) {
3446 const gchar *tmp_uri;
3449 ev_document_doc_mutex_lock ();
3450 pixbuf = ev_document_images_get_image (EV_DOCUMENT_IMAGES (view->document),
3451 view->image_dnd_info.image);
3452 ev_document_doc_mutex_unlock ();
3454 tmp_uri = ev_image_save_tmp (view->image_dnd_info.image, pixbuf);
3455 g_object_unref (pixbuf);
3457 uris[0] = (gchar *)tmp_uri;
3459 gtk_selection_data_set_uris (selection_data, uris);
3465 ev_view_drag_motion (GtkWidget *widget,
3466 GdkDragContext *context,
3471 if (gtk_drag_get_source_widget (context) == widget)
3472 gdk_drag_status (context, 0, time);
3474 gdk_drag_status (context, gdk_drag_context_get_suggested_action (context), time);
3480 selection_update_idle_cb (EvView *view)
3482 compute_selections (view,
3483 view->selection_info.style,
3484 &view->selection_info.start,
3486 view->selection_update_id = 0;
3491 selection_scroll_timeout_cb (EvView *view)
3493 gint x, y, shift = 0;
3494 GtkWidget *widget = GTK_WIDGET (view);
3495 GtkAllocation allocation;
3497 gtk_widget_get_allocation (widget, &allocation);
3498 gtk_widget_get_pointer (widget, &x, &y);
3500 if (y > allocation.height) {
3501 shift = (y - allocation.height) / 2;
3507 gtk_adjustment_set_value (view->vadjustment,
3508 CLAMP (gtk_adjustment_get_value (view->vadjustment) + shift,
3509 gtk_adjustment_get_lower (view->vadjustment),
3510 gtk_adjustment_get_upper (view->vadjustment) -
3511 gtk_adjustment_get_page_size (view->vadjustment)));
3513 if (x > allocation.width) {
3514 shift = (x - allocation.width) / 2;
3520 gtk_adjustment_set_value (view->hadjustment,
3521 CLAMP (gtk_adjustment_get_value (view->hadjustment) + shift,
3522 gtk_adjustment_get_lower (view->hadjustment),
3523 gtk_adjustment_get_upper (view->hadjustment) -
3524 gtk_adjustment_get_page_size (view->hadjustment)));
3530 ev_view_drag_update_momentum (EvView *view)
3533 if (!view->drag_info.in_drag)
3536 for (i = DRAG_HISTORY - 1; i > 0; i--) {
3537 view->drag_info.buffer[i].x = view->drag_info.buffer[i-1].x;
3538 view->drag_info.buffer[i].y = view->drag_info.buffer[i-1].y;
3541 /* Momentum is a moving average of 10ms granularity over
3542 * the last 100ms with each 10ms stored in buffer.
3545 view->drag_info.momentum.x = (view->drag_info.buffer[DRAG_HISTORY - 1].x - view->drag_info.buffer[0].x);
3546 view->drag_info.momentum.y = (view->drag_info.buffer[DRAG_HISTORY - 1].y - view->drag_info.buffer[0].y);
3552 ev_view_scroll_drag_release (EvView *view)
3554 gdouble dhadj_value, dvadj_value;
3555 gdouble oldhadjustment, oldvadjustment;
3556 gdouble h_page_size, v_page_size;
3557 gdouble h_upper, v_upper;
3558 GtkAllocation allocation;
3560 view->drag_info.momentum.x /= 1.2;
3561 view->drag_info.momentum.y /= 1.2; /* Alter these constants to change "friction" */
3563 gtk_widget_get_allocation (GTK_WIDGET (view), &allocation);
3565 h_page_size = gtk_adjustment_get_page_size (view->hadjustment);
3566 v_page_size = gtk_adjustment_get_page_size (view->vadjustment);
3568 dhadj_value = h_page_size *
3569 (gdouble)view->drag_info.momentum.x / allocation.width;
3570 dvadj_value = v_page_size *
3571 (gdouble)view->drag_info.momentum.y / allocation.height;
3573 oldhadjustment = gtk_adjustment_get_value (view->hadjustment);
3574 oldvadjustment = gtk_adjustment_get_value (view->vadjustment);
3576 h_upper = gtk_adjustment_get_upper (view->hadjustment);
3577 v_upper = gtk_adjustment_get_upper (view->vadjustment);
3579 /* When we reach the edges, we need either to absorb some momentum and bounce by
3580 * multiplying it on -0.5 or stop scrolling by setting momentum to 0. */
3581 if (((oldhadjustment + dhadj_value) > (h_upper - h_page_size)) ||
3582 ((oldhadjustment + dhadj_value) < 0))
3583 view->drag_info.momentum.x = 0;
3584 if (((oldvadjustment + dvadj_value) > (v_upper - v_page_size)) ||
3585 ((oldvadjustment + dvadj_value) < 0))
3586 view->drag_info.momentum.y = 0;
3588 gtk_adjustment_set_value (view->hadjustment,
3589 MIN (oldhadjustment + dhadj_value,
3590 h_upper - h_page_size));
3591 gtk_adjustment_set_value (view->vadjustment,
3592 MIN (oldvadjustment + dvadj_value,
3593 v_upper - v_page_size));
3595 if (((view->drag_info.momentum.x < 1) && (view->drag_info.momentum.x > -1)) &&
3596 ((view->drag_info.momentum.y < 1) && (view->drag_info.momentum.y > -1)))
3603 ev_view_motion_notify_event (GtkWidget *widget,
3604 GdkEventMotion *event)
3606 EvView *view = EV_VIEW (widget);
3607 GdkWindow *bin_window;
3610 if (!view->document)
3613 bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (view));
3615 if (event->is_hint || event->window != bin_window) {
3616 gtk_widget_get_pointer (widget, &x, &y);
3622 if (view->scroll_info.autoscrolling) {
3623 view->scroll_info.last_y = y;
3627 if (view->selection_info.in_drag) {
3628 if (gtk_drag_check_threshold (widget,
3629 view->selection_info.start.x,
3630 view->selection_info.start.y,
3632 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
3634 gtk_target_list_add_text_targets (target_list, TARGET_DND_TEXT);
3636 gtk_drag_begin (widget, target_list,
3638 1, (GdkEvent *)event);
3640 view->selection_info.in_drag = FALSE;
3642 gtk_target_list_unref (target_list);
3646 } else if (view->image_dnd_info.in_drag) {
3647 if (gtk_drag_check_threshold (widget,
3648 view->selection_info.start.x,
3649 view->selection_info.start.y,
3651 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
3653 gtk_target_list_add_uri_targets (target_list, TARGET_DND_URI);
3654 gtk_target_list_add_image_targets (target_list, TARGET_DND_IMAGE, TRUE);
3656 gtk_drag_begin (widget, target_list,
3658 1, (GdkEvent *)event);
3660 view->image_dnd_info.in_drag = FALSE;
3662 gtk_target_list_unref (target_list);
3668 switch (view->pressed_button) {
3670 /* For the Evince 0.4.x release, we limit selection to un-rotated
3673 if (view->rotation != 0)
3676 /* Schedule timeout to scroll during selection and additionally
3677 * scroll once to allow arbitrary speed. */
3678 if (!view->selection_scroll_id)
3679 view->selection_scroll_id = g_timeout_add (SCROLL_TIME,
3680 (GSourceFunc)selection_scroll_timeout_cb,
3683 selection_scroll_timeout_cb (view);
3685 view->selection_info.in_selection = TRUE;
3686 view->motion.x = x + view->scroll_x;
3687 view->motion.y = y + view->scroll_y;
3689 /* Queue an idle to handle the motion. We do this because
3690 * handling any selection events in the motion could be slower
3691 * than new motion events reach us. We always put it in the
3692 * idle to make sure we catch up and don't visibly lag the
3694 if (!view->selection_update_id)
3695 view->selection_update_id = g_idle_add ((GSourceFunc)selection_update_idle_cb, view);
3699 if (!view->drag_info.in_drag) {
3703 start = gtk_drag_check_threshold (widget,
3704 view->drag_info.start.x,
3705 view->drag_info.start.y,
3708 view->drag_info.in_drag = start;
3709 view->drag_info.drag_timeout_id = g_timeout_add (10,
3710 (GSourceFunc)ev_view_drag_update_momentum, view);
3711 /* Set 100 to choose how long it takes to build up momentum */
3712 /* Clear out previous momentum info: */
3713 for (i = 0; i < DRAG_HISTORY; i++) {
3714 view->drag_info.buffer[i].x = event->x;
3715 view->drag_info.buffer[i].y = event->y;
3717 view->drag_info.momentum.x = 0;
3718 view->drag_info.momentum.y = 0;
3721 if (view->drag_info.in_drag) {
3723 gdouble dhadj_value, dvadj_value;
3724 GtkAllocation allocation;
3726 view->drag_info.buffer[0].x = event->x;
3727 view->drag_info.buffer[0].y = event->y;
3729 dx = event->x_root - view->drag_info.start.x;
3730 dy = event->y_root - view->drag_info.start.y;
3732 gtk_widget_get_allocation (widget, &allocation);
3734 dhadj_value = gtk_adjustment_get_page_size (view->hadjustment) *
3735 (gdouble)dx / allocation.width;
3736 dvadj_value = gtk_adjustment_get_page_size (view->vadjustment) *
3737 (gdouble)dy / allocation.height;
3739 /* clamp scrolling to visible area */
3740 gtk_adjustment_set_value (view->hadjustment,
3741 MIN (view->drag_info.hadj - dhadj_value,
3742 gtk_adjustment_get_upper (view->hadjustment) -
3743 gtk_adjustment_get_page_size (view->hadjustment)));
3744 gtk_adjustment_set_value (view->vadjustment,
3745 MIN (view->drag_info.vadj - dvadj_value,
3746 gtk_adjustment_get_upper (view->vadjustment) -
3747 gtk_adjustment_get_page_size (view->vadjustment)));
3754 ev_view_handle_cursor_over_xy (view, x, y);
3761 ev_view_button_release_event (GtkWidget *widget,
3762 GdkEventButton *event)
3764 EvView *view = EV_VIEW (widget);
3765 EvLink *link = NULL;
3767 view->image_dnd_info.in_drag = FALSE;
3769 if (view->scroll_info.autoscrolling) {
3770 ev_view_autoscroll_stop (view);
3771 view->pressed_button = -1;
3776 if (view->drag_info.in_drag) {
3777 view->drag_info.release_timeout_id =
3779 (GSourceFunc)ev_view_scroll_drag_release, view);
3782 if (view->document && !view->drag_info.in_drag && view->pressed_button != 3) {
3783 link = ev_view_get_link_at_location (view, event->x, event->y);
3786 view->drag_info.in_drag = FALSE;
3788 if (view->pressed_button == 2) {
3789 ev_view_handle_cursor_over_xy (view, event->x, event->y);
3792 view->pressed_button = -1;
3794 if (view->selection_scroll_id) {
3795 g_source_remove (view->selection_scroll_id);
3796 view->selection_scroll_id = 0;
3798 if (view->selection_update_id) {
3799 g_source_remove (view->selection_update_id);
3800 view->selection_update_id = 0;
3803 if (view->selection_info.selections) {
3804 clear_link_selected (view);
3805 ev_view_update_primary_selection (view);
3807 if (view->selection_info.in_drag) {
3808 clear_selection (view);
3809 gtk_widget_queue_draw (widget);
3812 view->selection_info.in_drag = FALSE;
3814 if (event->button == 2) {
3815 EvLinkAction *action;
3816 EvLinkActionType type;
3818 action = ev_link_get_action (link);
3822 type = ev_link_action_get_action_type (action);
3823 if (type == EV_LINK_ACTION_TYPE_GOTO_DEST) {
3824 g_signal_emit (view,
3825 signals[SIGNAL_EXTERNAL_LINK],
3829 ev_view_handle_link (view, link);
3837 ev_view_key_press_event (GtkWidget *widget,
3840 EvView *view = EV_VIEW (widget);
3842 if (!view->document)
3845 if (!gtk_widget_has_focus (widget)) {
3846 /* Forward key events to current focused window child */
3847 if (view->window_child_focus) {
3848 GdkEventKey *new_event;
3851 new_event = (GdkEventKey *) gdk_event_copy ((GdkEvent *)event);
3852 g_object_unref (new_event->window);
3853 new_event->window = gtk_widget_get_window (view->window_child_focus->window);
3854 if (new_event->window)
3855 g_object_ref (new_event->window);
3856 gtk_widget_realize (view->window_child_focus->window);
3857 handled = gtk_widget_event (view->window_child_focus->window, (GdkEvent *)new_event);
3858 gdk_event_free ((GdkEvent *)new_event);
3866 return gtk_bindings_activate_event (GTK_OBJECT (widget), event);
3870 ev_view_focus_in (GtkWidget *widget,
3871 GdkEventFocus *event)
3873 if (EV_VIEW (widget)->pixbuf_cache)
3874 ev_pixbuf_cache_style_changed (EV_VIEW (widget)->pixbuf_cache);
3875 gtk_widget_queue_draw (widget);
3881 ev_view_focus_out (GtkWidget *widget,
3882 GdkEventFocus *event)
3884 if (EV_VIEW (widget)->pixbuf_cache)
3885 ev_pixbuf_cache_style_changed (EV_VIEW (widget)->pixbuf_cache);
3886 gtk_widget_queue_draw (widget);
3892 ev_view_leave_notify_event (GtkWidget *widget, GdkEventCrossing *event)
3894 EvView *view = EV_VIEW (widget);
3896 if (view->cursor != EV_VIEW_CURSOR_NORMAL)
3897 ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL);
3903 ev_view_enter_notify_event (GtkWidget *widget, GdkEventCrossing *event)
3905 EvView *view = EV_VIEW (widget);
3907 ev_view_handle_cursor_over_xy (view, event->x, event->y);
3913 ev_view_style_set (GtkWidget *widget,
3914 GtkStyle *old_style)
3916 if (EV_VIEW (widget)->pixbuf_cache)
3917 ev_pixbuf_cache_style_changed (EV_VIEW (widget)->pixbuf_cache);
3919 GTK_WIDGET_CLASS (ev_view_parent_class)->style_set (widget, old_style);
3925 draw_rubberband (EvView *view,
3927 const GdkRectangle *rect,
3931 GdkColor *fill_color_gdk;
3935 style = gtk_widget_get_style (GTK_WIDGET (view));
3936 fill_color_gdk = gdk_color_copy (&style->base[GTK_STATE_SELECTED]);
3937 r = fill_color_gdk->red / 65535.;
3938 g = fill_color_gdk->green / 65535.;
3939 b = fill_color_gdk->blue / 65535.;
3941 cr = gdk_cairo_create (window);
3943 cairo_set_source_rgba (cr, r, g, b, alpha);
3944 cairo_rectangle (cr,
3945 rect->x - view->scroll_x,
3946 rect->y - view->scroll_y,
3947 rect->width, rect->height);
3948 cairo_fill_preserve (cr);
3950 cairo_set_line_width (cr, 0.5);
3951 cairo_set_source_rgb (cr, r, g, b);
3956 gdk_color_free (fill_color_gdk);
3961 highlight_find_results (EvView *view, int page)
3963 gint i, n_results = 0;
3964 GdkWindow *bin_window;
3966 bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (view));
3968 n_results = ev_view_find_get_n_results (view, page);
3970 for (i = 0; i < n_results; i++) {
3971 EvRectangle *rectangle;
3972 GdkRectangle view_rectangle;
3975 if (i == view->find_result && page == view->current_page) {
3981 rectangle = ev_view_find_get_result (view, page, i);
3982 doc_rect_to_view_rect (view, page, rectangle, &view_rectangle);
3983 draw_rubberband (view, bin_window, &view_rectangle, alpha);
3988 ev_view_loading_window_move (EvView *view)
3990 GtkWidget *widget = GTK_WIDGET (view);
3991 EvLoadingWindow *window = EV_LOADING_WINDOW (view->loading_window);
3992 gint root_x, root_y;
3994 GtkAllocation allocation;
3996 gtk_widget_get_allocation (widget, &allocation);
3997 gdk_window_get_origin (gtk_widget_get_window (widget), &root_x, &root_y);
3998 ev_loading_window_get_size (window, &window_width, NULL);
4000 root_x += allocation.width - window_width - 10;
4003 ev_loading_window_move (window, root_x, root_y);
4007 show_loading_window_cb (EvView *view)
4009 if (!view->loading_window) {
4013 parent = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (view)));
4014 view->loading_window = ev_loading_window_new (parent);
4016 /* Show the window off screen to get a valid size asap */
4017 screen = gtk_widget_get_screen (GTK_WIDGET (view));
4018 gtk_window_move (GTK_WINDOW (view->loading_window),
4019 gdk_screen_get_width (screen) + 1,
4020 gdk_screen_get_height (screen) + 1);
4021 gtk_widget_show (view->loading_window);
4024 ev_view_loading_window_move (view);
4026 gtk_widget_show (view->loading_window);
4028 view->loading_timeout = 0;
4034 show_loading_window (EvView *view)
4036 if (view->loading_window && gtk_widget_get_visible (view->loading_window)) {
4037 ev_view_loading_window_move (view);
4041 if (!view->loading_timeout) {
4042 view->loading_timeout =
4043 g_timeout_add_full (G_PRIORITY_LOW,
4044 0.5, (GSourceFunc)show_loading_window_cb,
4050 hide_loading_window (EvView *view)
4052 if (view->loading_timeout) {
4053 g_source_remove (view->loading_timeout);
4054 view->loading_timeout = 0;
4057 if (view->loading_window && gtk_widget_get_visible (view->loading_window)) {
4058 gtk_widget_hide (view->loading_window);
4063 draw_one_page (EvView *view,
4066 GdkRectangle *page_area,
4068 GdkRectangle *expose_area,
4069 gboolean *page_ready)
4071 GdkWindow *bin_window;
4072 GdkRectangle overlap;
4073 GdkRectangle real_page_area;
4075 gboolean inverted_colors;
4077 g_assert (view->document);
4079 if (! gdk_rectangle_intersect (page_area, expose_area, &overlap))
4082 /* Render the document itself */
4083 real_page_area = *page_area;
4085 real_page_area.x += border->left;
4086 real_page_area.y += border->top;
4087 real_page_area.width -= (border->left + border->right);
4088 real_page_area.height -= (border->top + border->bottom);
4091 bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (view));
4092 current_page = ev_document_model_get_page (view->model);
4093 inverted_colors = ev_document_model_get_inverted_colors (view->model);
4094 ev_document_misc_paint_one_page (bin_window,
4097 page == current_page,
4100 if (gdk_rectangle_intersect (&real_page_area, expose_area, &overlap)) {
4102 gint page_width, page_height;
4103 cairo_surface_t *page_surface = NULL;
4104 gint selection_width, selection_height;
4105 cairo_surface_t *selection_surface = NULL;
4107 page_surface = ev_pixbuf_cache_get_surface (view->pixbuf_cache, page);
4109 if (!page_surface) {
4110 if (page == current_page)
4111 show_loading_window (view);
4113 *page_ready = FALSE;
4118 if (page == current_page)
4119 hide_loading_window (view);
4121 ev_view_get_page_size (view, page, &width, &height);
4123 page_width = cairo_image_surface_get_width (page_surface);
4124 page_height = cairo_image_surface_get_height (page_surface);
4127 cairo_translate (cr, overlap.x, overlap.y);
4129 if (width != page_width || height != page_height) {
4130 cairo_pattern_set_filter (cairo_get_source (cr),
4133 (gdouble)width / page_width,
4134 (gdouble)height / page_height);
4137 cairo_surface_set_device_offset (page_surface,
4138 overlap.x - real_page_area.x,
4139 overlap.y - real_page_area.y);
4140 cairo_set_source_surface (cr, page_surface, 0, 0);
4144 /* Get the selection pixbuf iff we have something to draw */
4145 if (find_selection_for_page (view, page) &&
4146 view->selection_mode == EV_VIEW_SELECTION_TEXT) {
4148 ev_pixbuf_cache_get_selection_surface (view->pixbuf_cache,
4154 if (!selection_surface) {
4158 selection_width = cairo_image_surface_get_width (selection_surface);
4159 selection_height = cairo_image_surface_get_height (selection_surface);
4162 cairo_translate (cr, overlap.x, overlap.y);
4164 if (width != selection_width || height != selection_height) {
4165 cairo_pattern_set_filter (cairo_get_source (cr),
4168 (gdouble)width / selection_width,
4169 (gdouble)height / selection_height);
4172 cairo_surface_set_device_offset (selection_surface,
4173 overlap.x - real_page_area.x,
4174 overlap.y - real_page_area.y);
4175 cairo_set_source_surface (cr, selection_surface, 0, 0);
4181 /*** GObject functions ***/
4184 ev_view_finalize (GObject *object)
4186 EvView *view = EV_VIEW (object);
4188 clear_selection (view);
4189 clear_link_selected (view);
4191 if (view->image_dnd_info.image)
4192 g_object_unref (view->image_dnd_info.image);
4193 view->image_dnd_info.image = NULL;
4195 G_OBJECT_CLASS (ev_view_parent_class)->finalize (object);
4199 ev_view_destroy (GtkObject *object)
4201 EvView *view = EV_VIEW (object);
4204 g_object_unref (view->model);
4208 if (view->pixbuf_cache) {
4209 g_object_unref (view->pixbuf_cache);
4210 view->pixbuf_cache = NULL;
4213 if (view->document) {
4214 g_object_unref (view->document);
4215 view->document = NULL;
4218 if (view->page_cache) {
4219 g_object_unref (view->page_cache);
4220 view->page_cache = NULL;
4223 ev_view_window_children_free (view);
4225 if (view->selection_scroll_id) {
4226 g_source_remove (view->selection_scroll_id);
4227 view->selection_scroll_id = 0;
4230 if (view->selection_update_id) {
4231 g_source_remove (view->selection_update_id);
4232 view->selection_update_id = 0;
4235 if (view->scroll_info.timeout_id) {
4236 g_source_remove (view->scroll_info.timeout_id);
4237 view->scroll_info.timeout_id = 0;
4240 if (view->drag_info.drag_timeout_id) {
4241 g_source_remove (view->drag_info.drag_timeout_id);
4242 view->drag_info.drag_timeout_id = 0;
4245 if (view->drag_info.release_timeout_id) {
4246 g_source_remove (view->drag_info.release_timeout_id);
4247 view->drag_info.release_timeout_id = 0;
4250 if (view->loading_timeout) {
4251 g_source_remove (view->loading_timeout);
4252 view->loading_timeout = 0;
4255 ev_view_set_scroll_adjustments (GTK_LAYOUT (view), NULL, NULL);
4257 GTK_OBJECT_CLASS (ev_view_parent_class)->destroy (object);
4261 ev_view_get_accessible (GtkWidget *widget)
4263 static gboolean first_time = TRUE;
4266 AtkObjectFactory *factory;
4267 AtkRegistry *registry;
4269 GType derived_atk_type;
4272 * Figure out whether accessibility is enabled by looking at the
4273 * type of the accessible object which would be created for
4274 * the parent type of EvView.
4276 derived_type = g_type_parent (EV_TYPE_VIEW);
4278 registry = atk_get_default_registry ();
4279 factory = atk_registry_get_factory (registry,
4281 derived_atk_type = atk_object_factory_get_accessible_type (factory);
4282 if (g_type_is_a (derived_atk_type, GTK_TYPE_ACCESSIBLE))
4283 atk_registry_set_factory_type (registry,
4285 ev_view_accessible_factory_get_type ());
4288 return GTK_WIDGET_CLASS (ev_view_parent_class)->get_accessible (widget);
4292 ev_view_class_init (EvViewClass *class)
4294 GObjectClass *object_class = G_OBJECT_CLASS (class);
4295 GtkObjectClass *gtk_object_class = GTK_OBJECT_CLASS (class);
4296 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
4297 GtkLayoutClass *layout_class = GTK_LAYOUT_CLASS (class);
4298 GtkBindingSet *binding_set;
4300 object_class->finalize = ev_view_finalize;
4302 widget_class->expose_event = ev_view_expose_event;
4303 widget_class->button_press_event = ev_view_button_press_event;
4304 widget_class->motion_notify_event = ev_view_motion_notify_event;
4305 widget_class->button_release_event = ev_view_button_release_event;
4306 widget_class->key_press_event = ev_view_key_press_event;
4307 widget_class->focus_in_event = ev_view_focus_in;
4308 widget_class->focus_out_event = ev_view_focus_out;
4309 widget_class->get_accessible = ev_view_get_accessible;
4310 widget_class->size_request = ev_view_size_request;
4311 widget_class->size_allocate = ev_view_size_allocate;
4312 widget_class->realize = ev_view_realize;
4313 widget_class->scroll_event = ev_view_scroll_event;
4314 widget_class->enter_notify_event = ev_view_enter_notify_event;
4315 widget_class->leave_notify_event = ev_view_leave_notify_event;
4316 widget_class->style_set = ev_view_style_set;
4317 widget_class->drag_data_get = ev_view_drag_data_get;
4318 widget_class->drag_motion = ev_view_drag_motion;
4319 widget_class->popup_menu = ev_view_popup_menu;
4320 widget_class->query_tooltip = ev_view_query_tooltip;
4322 gtk_object_class->destroy = ev_view_destroy;
4324 layout_class->set_scroll_adjustments = ev_view_set_scroll_adjustments;
4326 class->binding_activated = ev_view_scroll;
4328 signals[SIGNAL_BINDING_ACTIVATED] = g_signal_new ("binding_activated",
4329 G_TYPE_FROM_CLASS (object_class),
4330 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
4331 G_STRUCT_OFFSET (EvViewClass, binding_activated),
4333 ev_view_marshal_VOID__ENUM_BOOLEAN,
4335 GTK_TYPE_SCROLL_TYPE,
4337 signals[SIGNAL_HANDLE_LINK] = g_signal_new ("handle-link",
4338 G_TYPE_FROM_CLASS (object_class),
4339 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
4340 G_STRUCT_OFFSET (EvViewClass, handle_link),
4342 g_cclosure_marshal_VOID__OBJECT,
4345 signals[SIGNAL_EXTERNAL_LINK] = g_signal_new ("external-link",
4346 G_TYPE_FROM_CLASS (object_class),
4347 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
4348 G_STRUCT_OFFSET (EvViewClass, external_link),
4350 g_cclosure_marshal_VOID__OBJECT,
4353 signals[SIGNAL_POPUP_MENU] = g_signal_new ("popup",
4354 G_TYPE_FROM_CLASS (object_class),
4355 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
4356 G_STRUCT_OFFSET (EvViewClass, popup_menu),
4358 g_cclosure_marshal_VOID__POINTER,
4361 signals[SIGNAL_SELECTION_CHANGED] = g_signal_new ("selection-changed",
4362 G_TYPE_FROM_CLASS (object_class),
4363 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
4364 G_STRUCT_OFFSET (EvViewClass, selection_changed),
4366 g_cclosure_marshal_VOID__VOID,
4370 binding_set = gtk_binding_set_by_class (class);
4372 add_scroll_binding_keypad (binding_set, GDK_Left, 0, GTK_SCROLL_STEP_BACKWARD, TRUE);
4373 add_scroll_binding_keypad (binding_set, GDK_Right, 0, GTK_SCROLL_STEP_FORWARD, TRUE);
4374 add_scroll_binding_keypad (binding_set, GDK_Left, GDK_MOD1_MASK, GTK_SCROLL_STEP_DOWN, TRUE);
4375 add_scroll_binding_keypad (binding_set, GDK_Right, GDK_MOD1_MASK, GTK_SCROLL_STEP_UP, TRUE);
4376 add_scroll_binding_keypad (binding_set, GDK_Up, 0, GTK_SCROLL_STEP_BACKWARD, FALSE);
4377 add_scroll_binding_keypad (binding_set, GDK_Down, 0, GTK_SCROLL_STEP_FORWARD, FALSE);
4378 add_scroll_binding_keypad (binding_set, GDK_Up, GDK_MOD1_MASK, GTK_SCROLL_STEP_DOWN, FALSE);
4379 add_scroll_binding_keypad (binding_set, GDK_Down, GDK_MOD1_MASK, GTK_SCROLL_STEP_UP, FALSE);
4380 gtk_binding_entry_add_signal (binding_set, GDK_H, 0, "binding_activated", 2, GTK_TYPE_SCROLL_TYPE,
4381 GTK_SCROLL_STEP_BACKWARD, G_TYPE_BOOLEAN, TRUE);
4382 gtk_binding_entry_add_signal (binding_set, GDK_J, 0, "binding_activated", 2, GTK_TYPE_SCROLL_TYPE,
4383 GTK_SCROLL_STEP_FORWARD, G_TYPE_BOOLEAN, FALSE);
4384 gtk_binding_entry_add_signal (binding_set, GDK_K, 0, "binding_activated", 2, GTK_TYPE_SCROLL_TYPE,
4385 GTK_SCROLL_STEP_BACKWARD, G_TYPE_BOOLEAN, FALSE);
4386 gtk_binding_entry_add_signal (binding_set, GDK_L, 0, "binding_activated", 2, GTK_TYPE_SCROLL_TYPE,
4387 GTK_SCROLL_STEP_FORWARD, G_TYPE_BOOLEAN, TRUE);
4392 ev_view_init (EvView *view)
4394 gtk_widget_set_can_focus (GTK_WIDGET (view), TRUE);
4396 view->start_page = -1;
4397 view->end_page = -1;
4400 view->current_page = 0;
4401 view->pressed_button = -1;
4402 view->cursor = EV_VIEW_CURSOR_NORMAL;
4403 view->drag_info.in_drag = FALSE;
4404 view->scroll_info.autoscrolling = FALSE;
4405 view->selection_info.selections = NULL;
4406 view->selection_info.in_selection = FALSE;
4407 view->selection_info.in_drag = FALSE;
4408 view->selection_mode = EV_VIEW_SELECTION_TEXT;
4409 view->continuous = TRUE;
4410 view->dual_page = FALSE;
4411 view->fullscreen = FALSE;
4412 view->sizing_mode = EV_SIZING_FIT_WIDTH;
4413 view->pending_scroll = SCROLL_TO_KEEP_POSITION;
4414 view->jump_to_find_result = TRUE;
4415 view->highlight_find_results = FALSE;
4417 gtk_layout_set_hadjustment (GTK_LAYOUT (view), NULL);
4418 gtk_layout_set_vadjustment (GTK_LAYOUT (view), NULL);
4424 ev_view_change_page (EvView *view,
4429 view->current_page = new_page;
4430 view->pending_scroll = SCROLL_TO_PAGE_POSITION;
4432 hide_loading_window (view);
4434 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
4435 ev_view_handle_cursor_over_xy (view, x, y);
4437 gtk_widget_queue_resize (GTK_WIDGET (view));
4441 job_finished_cb (EvPixbufCache *pixbuf_cache,
4446 GdkWindow *bin_window;
4448 bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (view));
4449 gdk_window_invalidate_region (bin_window, region, TRUE);
4451 gtk_widget_queue_draw (GTK_WIDGET (view));
4456 ev_view_page_changed_cb (EvDocumentModel *model,
4461 if (!view->document)
4464 if (view->current_page != new_page) {
4465 ev_view_change_page (view, new_page);
4467 gtk_widget_queue_draw (GTK_WIDGET (view));
4470 view->find_result = 0;
4474 on_adjustment_value_changed (GtkAdjustment *adjustment,
4480 GList *children, *l;
4482 if (!gtk_widget_get_realized (GTK_WIDGET (view)))
4485 if (view->hadjustment) {
4486 value = (gint) gtk_adjustment_get_value (view->hadjustment);
4487 dx = view->scroll_x - value;
4488 view->scroll_x = value;
4493 if (view->vadjustment) {
4494 value = (gint) gtk_adjustment_get_value (view->vadjustment);
4495 dy = view->scroll_y - value;
4496 view->scroll_y = value;
4501 children = gtk_container_get_children (GTK_CONTAINER (view));
4502 for (l = children; l && l->data; l = g_list_next (l)) {
4503 gint child_x, child_y;
4504 GtkWidget *child = (GtkWidget *)l->data;
4506 gtk_container_child_get (GTK_CONTAINER (view),
4511 gtk_layout_move (GTK_LAYOUT (view), child, child_x + dx, child_y + dy);
4513 g_list_free (children);
4515 for (l = view->window_children; l && l->data; l = g_list_next (l)) {
4516 EvViewWindowChild *child;
4518 child = (EvViewWindowChild *)l->data;
4520 ev_view_window_child_move (view, child, child->x + dx, child->y + dy);
4523 if (view->pending_resize) {
4524 gtk_widget_queue_draw (GTK_WIDGET (view));
4526 GdkWindow *bin_window;
4528 bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (view));
4529 gdk_window_scroll (bin_window, dx, dy);
4532 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
4533 ev_view_handle_cursor_over_xy (view, x, y);
4536 view_update_range_and_current_page (view);
4544 view = g_object_new (EV_TYPE_VIEW, NULL);
4550 setup_caches (EvView *view)
4552 gboolean inverted_colors;
4554 view->height_to_page_cache = ev_view_get_height_to_page_cache (view);
4555 view->pixbuf_cache = ev_pixbuf_cache_new (GTK_WIDGET (view), view->model, view->pixbuf_cache_size);
4556 view->page_cache = ev_page_cache_new (view->document);
4557 inverted_colors = ev_document_model_get_inverted_colors (view->model);
4558 ev_pixbuf_cache_set_inverted_colors (view->pixbuf_cache, inverted_colors);
4559 g_signal_connect (view->pixbuf_cache, "job-finished", G_CALLBACK (job_finished_cb), view);
4563 clear_caches (EvView *view)
4565 if (view->pixbuf_cache) {
4566 g_object_unref (view->pixbuf_cache);
4567 view->pixbuf_cache = NULL;
4570 if (view->page_cache) {
4571 g_object_unref (view->page_cache);
4572 view->page_cache = NULL;
4577 * ev_view_set_page_cache_size:
4581 * Sets the maximum size in bytes that will be used to cache
4582 * rendered pages. Use 0 to disable caching rendered pages.
4584 * Note that this limit doesn't affect the current visible page range,
4585 * which will always be rendered. In order to limit the total memory used
4586 * you have to use ev_document_model_set_max_scale() too.
4590 ev_view_set_page_cache_size (EvView *view,
4593 if (view->pixbuf_cache_size == cache_size)
4596 view->pixbuf_cache_size = cache_size;
4597 if (view->pixbuf_cache)
4598 ev_pixbuf_cache_set_max_size (view->pixbuf_cache, cache_size);
4602 ev_view_set_loading (EvView *view,
4605 if (view->loading && !loading)
4606 hide_loading_window (view);
4607 view->loading = loading;
4608 gtk_widget_queue_draw (GTK_WIDGET (view));
4612 ev_view_autoscroll_cb (EvView *view)
4614 gdouble speed, value;
4616 /* If the user stops autoscrolling, autoscrolling will be
4617 * set to false but the timeout will continue; stop the timeout: */
4618 if (!view->scroll_info.autoscrolling) {
4619 view->scroll_info.timeout_id = 0;
4623 if (view->scroll_info.last_y > view->scroll_info.start_y &&
4624 (view->scroll_info.last_y < view->scroll_info.start_y))
4627 /* Replace 100 with your speed of choice: The lower the faster.
4628 * Replace 3 with another speed of choice: The higher, the faster it accelerated
4629 * based on the distance of the starting point from the mouse
4630 * (All also effected by the timeout interval of this callback) */
4632 if (view->scroll_info.start_y > view->scroll_info.last_y)
4633 speed = -pow ((((gdouble)view->scroll_info.start_y - view->scroll_info.last_y) / 100), 3);
4635 speed = pow ((((gdouble)view->scroll_info.last_y - view->scroll_info.start_y) / 100), 3);
4637 value = gtk_adjustment_get_value (view->vadjustment);
4638 value = CLAMP (value + speed, 0,
4639 gtk_adjustment_get_upper (view->vadjustment) -
4640 gtk_adjustment_get_page_size (view->vadjustment));
4641 gtk_adjustment_set_value (view->vadjustment, value);
4648 ev_view_autoscroll_start (EvView *view)
4652 g_return_if_fail (EV_IS_VIEW (view));
4654 if (view->scroll_info.autoscrolling)
4657 view->scroll_info.autoscrolling = TRUE;
4658 view->scroll_info.timeout_id =
4659 g_timeout_add (20, (GSourceFunc)ev_view_autoscroll_cb,
4662 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
4663 ev_view_handle_cursor_over_xy (view, x, y);
4667 ev_view_autoscroll_stop (EvView *view)
4671 g_return_if_fail (EV_IS_VIEW (view));
4673 if (!view->scroll_info.autoscrolling)
4676 view->scroll_info.autoscrolling = FALSE;
4677 if (view->scroll_info.timeout_id) {
4678 g_source_remove (view->scroll_info.timeout_id);
4679 view->scroll_info.timeout_id = 0;
4682 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
4683 ev_view_handle_cursor_over_xy (view, x, y);
4687 ev_view_document_changed_cb (EvDocumentModel *model,
4691 EvDocument *document = ev_document_model_get_document (model);
4693 if (document != view->document) {
4696 clear_caches (view);
4698 if (view->document) {
4699 g_object_unref (view->document);
4702 view->document = document;
4703 view->find_result = 0;
4705 if (view->document) {
4706 view->loading = FALSE;
4707 g_object_ref (view->document);
4708 setup_caches (view);
4711 current_page = ev_document_model_get_page (model);
4712 if (view->current_page != current_page) {
4713 ev_view_change_page (view, current_page);
4715 view->pending_scroll = SCROLL_TO_KEEP_POSITION;
4716 gtk_widget_queue_resize (GTK_WIDGET (view));
4722 ev_view_rotation_changed_cb (EvDocumentModel *model,
4726 gint rotation = ev_document_model_get_rotation (model);
4728 view->rotation = rotation;
4730 if (view->pixbuf_cache) {
4731 ev_pixbuf_cache_clear (view->pixbuf_cache);
4732 if (!ev_document_is_page_size_uniform (view->document))
4733 view->pending_scroll = SCROLL_TO_PAGE_POSITION;
4734 gtk_widget_queue_resize (GTK_WIDGET (view));
4737 ev_view_remove_all (view);
4740 clear_selection (view);
4744 ev_view_inverted_colors_changed_cb (EvDocumentModel *model,
4748 if (view->pixbuf_cache) {
4749 gboolean inverted_colors;
4751 inverted_colors = ev_document_model_get_inverted_colors (model);
4752 ev_pixbuf_cache_set_inverted_colors (view->pixbuf_cache, inverted_colors);
4753 gtk_widget_queue_draw (GTK_WIDGET (view));
4758 ev_view_sizing_mode_changed_cb (EvDocumentModel *model,
4762 EvSizingMode mode = ev_document_model_get_sizing_mode (model);
4764 view->sizing_mode = mode;
4765 if (mode != EV_SIZING_FREE)
4766 gtk_widget_queue_resize (GTK_WIDGET (view));
4769 #define EPSILON 0.0000001
4771 ev_view_scale_changed_cb (EvDocumentModel *model,
4775 gdouble scale = ev_document_model_get_scale (model);
4777 if (ABS (view->scale - scale) < EPSILON)
4780 view->scale = scale;
4782 view->pending_resize = TRUE;
4783 if (view->sizing_mode == EV_SIZING_FREE)
4784 gtk_widget_queue_resize (GTK_WIDGET (view));
4788 ev_view_continuous_changed_cb (EvDocumentModel *model,
4792 gboolean continuous = ev_document_model_get_continuous (model);
4794 view->continuous = continuous;
4795 view->pending_scroll = SCROLL_TO_PAGE_POSITION;
4796 gtk_widget_queue_resize (GTK_WIDGET (view));
4800 ev_view_dual_page_changed_cb (EvDocumentModel *model,
4804 gboolean dual_page = ev_document_model_get_dual_page (model);
4806 view->dual_page = dual_page;
4807 view->pending_scroll = SCROLL_TO_PAGE_POSITION;
4808 /* FIXME: if we're keeping the pixbuf cache around, we should extend the
4809 * preload_cache_size to be 2 if dual_page is set.
4811 gtk_widget_queue_resize (GTK_WIDGET (view));
4815 ev_view_fullscreen_changed_cb (EvDocumentModel *model,
4819 gboolean fullscreen = ev_document_model_get_fullscreen (model);
4821 view->fullscreen = fullscreen;
4822 gtk_widget_queue_resize (GTK_WIDGET (view));
4826 ev_view_set_model (EvView *view,
4827 EvDocumentModel *model)
4829 g_return_if_fail (EV_IS_VIEW (view));
4830 g_return_if_fail (EV_IS_DOCUMENT_MODEL (model));
4832 if (model == view->model)
4836 g_signal_handlers_disconnect_by_func (view->model,
4837 ev_view_document_changed_cb,
4839 g_signal_handlers_disconnect_by_func (view->model,
4840 ev_view_page_changed_cb,
4842 g_object_unref (view->model);
4844 view->model = g_object_ref (model);
4846 /* Initialize view from model */
4847 view->rotation = ev_document_model_get_rotation (view->model);
4848 view->sizing_mode = ev_document_model_get_sizing_mode (view->model);
4849 view->scale = ev_document_model_get_scale (view->model);
4850 view->continuous = ev_document_model_get_continuous (view->model);
4851 view->dual_page = ev_document_model_get_dual_page (view->model);
4852 view->fullscreen = ev_document_model_get_fullscreen (view->model);
4853 ev_view_document_changed_cb (view->model, NULL, view);
4855 g_signal_connect (view->model, "notify::document",
4856 G_CALLBACK (ev_view_document_changed_cb),
4858 g_signal_connect (view->model, "notify::rotation",
4859 G_CALLBACK (ev_view_rotation_changed_cb),
4861 g_signal_connect (view->model, "notify::inverted-colors",
4862 G_CALLBACK (ev_view_inverted_colors_changed_cb),
4864 g_signal_connect (view->model, "notify::sizing-mode",
4865 G_CALLBACK (ev_view_sizing_mode_changed_cb),
4867 g_signal_connect (view->model, "notify::scale",
4868 G_CALLBACK (ev_view_scale_changed_cb),
4870 g_signal_connect (view->model, "notify::continuous",
4871 G_CALLBACK (ev_view_continuous_changed_cb),
4873 g_signal_connect (view->model, "notify::dual-page",
4874 G_CALLBACK (ev_view_dual_page_changed_cb),
4876 g_signal_connect (view->model, "notify::fullscreen",
4877 G_CALLBACK (ev_view_fullscreen_changed_cb),
4879 g_signal_connect (view->model, "page-changed",
4880 G_CALLBACK (ev_view_page_changed_cb),
4885 ev_view_reload_page (EvView *view,
4889 ev_pixbuf_cache_reload_page (view->pixbuf_cache,
4897 ev_view_reload (EvView *view)
4899 ev_pixbuf_cache_clear (view->pixbuf_cache);
4900 view_update_range_and_current_page (view);
4903 /*** Zoom and sizing mode ***/
4906 ev_view_can_zoom_in (EvView *view)
4908 return view->scale * ZOOM_IN_FACTOR <= ev_document_model_get_max_scale (view->model);
4912 ev_view_can_zoom_out (EvView *view)
4914 return view->scale * ZOOM_OUT_FACTOR >= ev_document_model_get_min_scale (view->model);
4918 ev_view_zoom_in (EvView *view)
4922 g_return_if_fail (view->sizing_mode == EV_SIZING_FREE);
4924 view->pending_scroll = SCROLL_TO_CENTER;
4925 scale = ev_document_model_get_scale (view->model) * ZOOM_IN_FACTOR;
4926 ev_document_model_set_scale (view->model, scale);
4930 ev_view_zoom_out (EvView *view)
4934 g_return_if_fail (view->sizing_mode == EV_SIZING_FREE);
4936 view->pending_scroll = SCROLL_TO_CENTER;
4937 scale = ev_document_model_get_scale (view->model) * ZOOM_OUT_FACTOR;
4938 ev_document_model_set_scale (view->model, scale);
4942 zoom_for_size_fit_width (gdouble doc_width,
4947 return (double)target_width / doc_width;
4951 zoom_for_size_fit_height (gdouble doc_width,
4956 return (double)target_height / doc_height;
4960 zoom_for_size_best_fit (gdouble doc_width,
4968 w_scale = (double)target_width / doc_width;
4969 h_scale = (double)target_height / doc_height;
4971 return MIN (w_scale, h_scale);
4975 ev_view_zoom_for_size_continuous_and_dual_page (EvView *view,
4979 gdouble doc_width, doc_height;
4984 ev_document_get_max_page_size (view->document, &doc_width, &doc_height);
4985 if (view->rotation == 90 || view->rotation == 270) {
4989 doc_width = doc_height;
4993 compute_border (view, doc_width, doc_height, &border);
4996 width -= (2 * (border.left + border.right) + 3 * view->spacing);
4997 height -= (border.top + border.bottom + 2 * view->spacing - 1);
4999 sb_size = ev_view_get_scrollbar_size (view, GTK_ORIENTATION_VERTICAL);
5001 if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
5002 scale = zoom_for_size_fit_width (doc_width, doc_height, width - sb_size, height);
5003 } else if (view->sizing_mode == EV_SIZING_BEST_FIT)
5004 scale = zoom_for_size_best_fit (doc_width, doc_height, width - sb_size, height);
5006 g_assert_not_reached ();
5008 ev_document_model_set_scale (view->model, scale);
5012 ev_view_zoom_for_size_continuous (EvView *view,
5016 gdouble doc_width, doc_height;
5021 ev_document_get_max_page_size (view->document, &doc_width, &doc_height);
5022 if (view->rotation == 90 || view->rotation == 270) {
5026 doc_width = doc_height;
5030 compute_border (view, doc_width, doc_height, &border);
5032 width -= (border.left + border.right + 2 * view->spacing);
5033 height -= (border.top + border.bottom + 2 * view->spacing - 1);
5035 sb_size = ev_view_get_scrollbar_size (view, GTK_ORIENTATION_VERTICAL);
5037 if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
5038 scale = zoom_for_size_fit_width (doc_width, doc_height, width - sb_size, height);
5039 } else if (view->sizing_mode == EV_SIZING_BEST_FIT)
5040 scale = zoom_for_size_best_fit (doc_width, doc_height, width - sb_size, height);
5042 g_assert_not_reached ();
5044 ev_document_model_set_scale (view->model, scale);
5048 ev_view_zoom_for_size_dual_page (EvView *view,
5053 gdouble doc_width, doc_height;
5057 other_page = view->current_page ^ 1;
5059 /* Find the largest of the two. */
5060 get_doc_page_size (view, view->current_page, &doc_width, &doc_height);
5061 if (other_page < ev_document_get_n_pages (view->document)) {
5062 gdouble width_2, height_2;
5064 get_doc_page_size (view, other_page, &width_2, &height_2);
5065 if (width_2 > doc_width)
5066 doc_width = width_2;
5067 if (height_2 > doc_height)
5068 doc_height = height_2;
5070 compute_border (view, width, height, &border);
5072 doc_width = doc_width * 2;
5073 width -= ((border.left + border.right)* 2 + 3 * view->spacing);
5074 height -= (border.top + border.bottom + 2 * view->spacing);
5076 if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
5079 sb_size = ev_view_get_scrollbar_size (view, GTK_ORIENTATION_VERTICAL);
5080 scale = zoom_for_size_fit_width (doc_width, doc_height, width - sb_size, height);
5081 } else if (view->sizing_mode == EV_SIZING_BEST_FIT)
5082 scale = zoom_for_size_best_fit (doc_width, doc_height, width, height);
5084 g_assert_not_reached ();
5086 ev_document_model_set_scale (view->model, scale);
5090 ev_view_zoom_for_size_single_page (EvView *view,
5094 gdouble doc_width, doc_height;
5098 get_doc_page_size (view, view->current_page, &doc_width, &doc_height);
5100 /* Get an approximate border */
5101 compute_border (view, width, height, &border);
5103 width -= (border.left + border.right + 2 * view->spacing);
5104 height -= (border.top + border.bottom + 2 * view->spacing);
5106 if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
5109 sb_size = ev_view_get_scrollbar_size (view, GTK_ORIENTATION_VERTICAL);
5110 scale = zoom_for_size_fit_width (doc_width, doc_height, width - sb_size, height);
5111 } else if (view->sizing_mode == EV_SIZING_BEST_FIT)
5112 scale = zoom_for_size_best_fit (doc_width, doc_height, width, height);
5114 g_assert_not_reached ();
5116 ev_document_model_set_scale (view->model, scale);
5120 ev_view_zoom_for_size (EvView *view,
5124 g_return_if_fail (EV_IS_VIEW (view));
5125 g_return_if_fail (view->sizing_mode == EV_SIZING_FIT_WIDTH ||
5126 view->sizing_mode == EV_SIZING_BEST_FIT);
5127 g_return_if_fail (width >= 0);
5128 g_return_if_fail (height >= 0);
5130 if (view->document == NULL)
5133 if (view->continuous && view->dual_page)
5134 ev_view_zoom_for_size_continuous_and_dual_page (view, width, height);
5135 else if (view->continuous)
5136 ev_view_zoom_for_size_continuous (view, width, height);
5137 else if (view->dual_page)
5138 ev_view_zoom_for_size_dual_page (view, width, height);
5140 ev_view_zoom_for_size_single_page (view, width, height);
5145 ev_view_find_get_n_results (EvView *view, gint page)
5147 return view->find_pages ? g_list_length (view->find_pages[page]) : 0;
5150 static EvRectangle *
5151 ev_view_find_get_result (EvView *view, gint page, gint result)
5153 return view->find_pages ? (EvRectangle *) g_list_nth_data (view->find_pages[page], result) : NULL;
5157 jump_to_find_result (EvView *view)
5160 gint page = view->current_page;
5162 n_results = ev_view_find_get_n_results (view, page);
5164 if (n_results > 0 && view->find_result < n_results) {
5166 GdkRectangle view_rect;
5168 rect = ev_view_find_get_result (view, page, view->find_result);
5169 doc_rect_to_view_rect (view, page, rect, &view_rect);
5170 ensure_rectangle_is_visible (view, &view_rect);
5171 view->jump_to_find_result = FALSE;
5177 * @view: #EvView instance
5178 * @direction: Direction to look
5179 * @shift: Shift from current page
5181 * Jumps to the first page that has occurences of searched word.
5182 * Uses a direction where to look and a shift from current page.
5186 jump_to_find_page (EvView *view, EvViewFindDirection direction, gint shift)
5190 n_pages = ev_document_get_n_pages (view->document);
5192 for (i = 0; i < n_pages; i++) {
5195 if (direction == EV_VIEW_FIND_NEXT)
5196 page = view->current_page + i;
5198 page = view->current_page - i;
5201 if (page >= n_pages) {
5202 page = page - n_pages;
5203 } else if (page < 0)
5204 page = page + n_pages;
5206 if (ev_view_find_get_n_results (view, page) > 0) {
5207 ev_document_model_set_page (view->model, page);
5214 ev_view_find_changed (EvView *view, GList **results, gint page)
5216 view->find_pages = results;
5218 if (view->jump_to_find_result == TRUE) {
5219 jump_to_find_page (view, EV_VIEW_FIND_NEXT, 0);
5220 jump_to_find_result (view);
5223 if (view->current_page == page)
5224 gtk_widget_queue_draw (GTK_WIDGET (view));
5228 ev_view_find_next (EvView *view)
5232 n_results = ev_view_find_get_n_results (view, view->current_page);
5233 view->find_result++;
5235 if (view->find_result >= n_results) {
5236 view->find_result = 0;
5237 jump_to_find_page (view, EV_VIEW_FIND_NEXT, 1);
5238 jump_to_find_result (view);
5240 jump_to_find_result (view);
5241 gtk_widget_queue_draw (GTK_WIDGET (view));
5246 ev_view_find_previous (EvView *view)
5248 view->find_result--;
5250 if (view->find_result < 0) {
5251 jump_to_find_page (view, EV_VIEW_FIND_PREV, -1);
5252 view->find_result = MAX (0, ev_view_find_get_n_results (view, view->current_page) - 1);
5253 jump_to_find_result (view);
5255 jump_to_find_result (view);
5256 gtk_widget_queue_draw (GTK_WIDGET (view));
5261 ev_view_find_search_changed (EvView *view)
5263 /* search string has changed, focus on new search result */
5264 view->jump_to_find_result = TRUE;
5265 view->find_pages = NULL;
5269 ev_view_find_set_highlight_search (EvView *view, gboolean value)
5271 view->highlight_find_results = value;
5272 gtk_widget_queue_draw (GTK_WIDGET (view));
5276 ev_view_find_cancel (EvView *view)
5278 view->find_pages = NULL;
5281 /*** Selections ***/
5283 /* compute_new_selection_rect/text calculates the area currently selected by
5284 * view_rect. each handles a different mode;
5287 compute_new_selection_rect (EvView *view,
5291 GdkRectangle view_rect;
5295 g_assert (view->selection_mode == EV_VIEW_SELECTION_RECTANGLE);
5297 view_rect.x = MIN (start->x, stop->x);
5298 view_rect.y = MIN (start->y, stop->y);
5299 view_rect.width = MAX (start->x, stop->x) - view_rect.x;
5300 view_rect.width = MAX (start->y, stop->y) - view_rect.y;
5302 n_pages = ev_document_get_n_pages (view->document);
5304 for (i = 0; i < n_pages; i++) {
5305 GdkRectangle page_area;
5308 if (get_page_extents (view, i, &page_area, &border)) {
5309 GdkRectangle overlap;
5311 if (gdk_rectangle_intersect (&page_area, &view_rect, &overlap)) {
5312 EvViewSelection *selection;
5314 selection = g_new0 (EvViewSelection, 1);
5315 selection->page = i;
5316 view_rect_to_doc_rect (view, &overlap, &page_area,
5317 &(selection->rect));
5319 list = g_list_append (list, selection);
5328 gdk_rectangle_point_in (GdkRectangle *rectangle,
5331 return rectangle->x <= point->x &&
5332 rectangle->y <= point->y &&
5333 point->x < rectangle->x + rectangle->width &&
5334 point->y < rectangle->y + rectangle->height;
5338 compute_new_selection_text (EvView *view,
5339 EvSelectionStyle style,
5343 int n_pages, i, first, last;
5345 EvViewSelection *selection;
5346 gdouble width, height;
5347 int start_page, end_page;
5349 g_assert (view->selection_mode == EV_VIEW_SELECTION_TEXT);
5351 n_pages = ev_document_get_n_pages (view->document);
5353 /* First figure out the range of pages the selection
5357 if (view->continuous) {
5360 } else if (view->dual_page) {
5361 start_page = view->start_page;
5362 end_page = view->end_page + 1;
5364 start_page = view->current_page;
5365 end_page = view->current_page + 1;
5368 for (i = start_page; i < end_page; i++) {
5369 GdkRectangle page_area;
5372 get_page_extents (view, i, &page_area, &border);
5373 if (gdk_rectangle_point_in (&page_area, start) ||
5374 gdk_rectangle_point_in (&page_area, stop)) {
5375 if (first == n_pages)
5382 /* Now create a list of EvViewSelection's for the affected
5383 * pages. This could be an empty list, a list of just one
5384 * page or a number of pages.*/
5385 for (i = first; i < last + 1; i++) {
5386 GdkRectangle page_area;
5390 get_doc_page_size (view, i, &width, &height);
5392 selection = g_new0 (EvViewSelection, 1);
5393 selection->page = i;
5394 selection->style = style;
5395 selection->rect.x1 = selection->rect.y1 = 0;
5396 selection->rect.x2 = width;
5397 selection->rect.y2 = height;
5399 get_page_extents (view, i, &page_area, &border);
5401 if (gdk_rectangle_point_in (&page_area, start))
5407 view_point_to_doc_point (view, point, &page_area,
5408 &selection->rect.x1,
5409 &selection->rect.y1);
5411 /* If the selection is contained within just one page,
5412 * make sure we don't write 'start' into both points
5413 * in selection->rect. */
5418 view_point_to_doc_point (view, point, &page_area,
5419 &selection->rect.x2,
5420 &selection->rect.y2);
5422 list = g_list_append (list, selection);
5428 /* This function takes the newly calculated list, and figures out which regions
5429 * have changed. It then queues a redraw approporiately.
5432 merge_selection_region (EvView *view,
5436 GList *new_list_ptr, *old_list_ptr;
5438 /* Update the selection */
5439 old_list = ev_pixbuf_cache_get_selection_list (view->pixbuf_cache);
5440 g_list_foreach (view->selection_info.selections, (GFunc)selection_free, NULL);
5441 g_list_free (view->selection_info.selections);
5442 view->selection_info.selections = new_list;
5443 ev_pixbuf_cache_set_selection_list (view->pixbuf_cache, new_list);
5444 g_signal_emit (view, signals[SIGNAL_SELECTION_CHANGED], 0, NULL);
5446 new_list_ptr = new_list;
5447 old_list_ptr = old_list;
5449 while (new_list_ptr || old_list_ptr) {
5450 EvViewSelection *old_sel, *new_sel;
5452 GdkRegion *region = NULL;
5454 new_sel = (new_list_ptr) ? (new_list_ptr->data) : NULL;
5455 old_sel = (old_list_ptr) ? (old_list_ptr->data) : NULL;
5457 /* Assume that the lists are in order, and we run through them
5458 * comparing them, one page at a time. We come out with the
5459 * first page we see. */
5460 if (new_sel && old_sel) {
5461 if (new_sel->page < old_sel->page) {
5462 new_list_ptr = new_list_ptr->next;
5464 } else if (new_sel->page > old_sel->page) {
5465 old_list_ptr = old_list_ptr->next;
5468 new_list_ptr = new_list_ptr->next;
5469 old_list_ptr = old_list_ptr->next;
5471 } else if (new_sel) {
5472 new_list_ptr = new_list_ptr->next;
5473 } else if (old_sel) {
5474 old_list_ptr = old_list_ptr->next;
5477 g_assert (new_sel || old_sel);
5479 /* is the page we're looking at on the screen?*/
5480 cur_page = new_sel ? new_sel->page : old_sel->page;
5481 if (cur_page < view->start_page || cur_page > view->end_page)
5484 /* seed the cache with a new page. We are going to need the new
5487 GdkRegion *tmp_region = NULL;
5489 ev_pixbuf_cache_get_selection_surface (view->pixbuf_cache,
5494 if (tmp_region && !gdk_region_empty (tmp_region)) {
5495 new_sel->covered_region = gdk_region_copy (tmp_region);
5499 /* Now we figure out what needs redrawing */
5500 if (old_sel && new_sel) {
5501 if (old_sel->covered_region && new_sel->covered_region) {
5502 /* We only want to redraw the areas that have
5503 * changed, so we xor the old and new regions
5504 * and redraw if it's different */
5505 region = gdk_region_copy (old_sel->covered_region);
5506 gdk_region_xor (region, new_sel->covered_region);
5507 if (gdk_region_empty (region)) {
5508 gdk_region_destroy (region);
5511 } else if (old_sel->covered_region) {
5512 region = gdk_region_copy (old_sel->covered_region);
5513 } else if (new_sel->covered_region) {
5514 region = gdk_region_copy (new_sel->covered_region);
5516 } else if (old_sel && !new_sel) {
5517 if (old_sel->covered_region && !gdk_region_empty (old_sel->covered_region)) {
5518 region = gdk_region_copy (old_sel->covered_region);
5520 } else if (!old_sel && new_sel) {
5521 if (new_sel->covered_region && !gdk_region_empty (new_sel->covered_region)) {
5522 region = gdk_region_copy (new_sel->covered_region);
5525 g_assert_not_reached ();
5528 /* Redraw the damaged region! */
5530 GdkWindow *bin_window;
5531 GdkRectangle page_area;
5534 bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (view));
5536 /* I don't know why but the region is smaller
5537 * than expected. This hack fixes it, I guess
5538 * 10 pixels more won't hurt
5540 gdk_region_shrink (region, -5, -5);
5542 get_page_extents (view, cur_page, &page_area, &border);
5543 gdk_region_offset (region,
5544 page_area.x + border.left - view->scroll_x,
5545 page_area.y + border.top - view->scroll_y);
5546 gdk_window_invalidate_region (bin_window, region, TRUE);
5547 gdk_region_destroy (region);
5551 /* Free the old list, now that we're done with it. */
5552 g_list_foreach (old_list, (GFunc) selection_free, NULL);
5553 g_list_free (old_list);
5557 compute_selections (EvView *view,
5558 EvSelectionStyle style,
5564 if (view->selection_mode == EV_VIEW_SELECTION_RECTANGLE)
5565 list = compute_new_selection_rect (view, start, stop);
5567 list = compute_new_selection_text (view, style, start, stop);
5568 merge_selection_region (view, list);
5571 /* Free's the selection. It's up to the caller to queue redraws if needed.
5574 selection_free (EvViewSelection *selection)
5576 if (selection->covered_region)
5577 gdk_region_destroy (selection->covered_region);
5582 clear_selection (EvView *view)
5584 if (view->selection_info.selections) {
5585 g_list_foreach (view->selection_info.selections, (GFunc)selection_free, NULL);
5586 g_list_free (view->selection_info.selections);
5587 view->selection_info.selections = NULL;
5589 g_signal_emit (view, signals[SIGNAL_SELECTION_CHANGED], 0, NULL);
5591 view->selection_info.in_selection = FALSE;
5592 if (view->pixbuf_cache)
5593 ev_pixbuf_cache_set_selection_list (view->pixbuf_cache, NULL);
5597 ev_view_select_all (EvView *view)
5599 GList *selections = NULL;
5602 /* Disable selection on rotated pages for the 0.4.0 series */
5603 if (view->rotation != 0)
5606 clear_selection (view);
5608 n_pages = ev_document_get_n_pages (view->document);
5609 for (i = 0; i < n_pages; i++) {
5610 gdouble width, height;
5611 EvViewSelection *selection;
5613 get_doc_page_size (view, i, &width, &height);
5615 selection = g_new0 (EvViewSelection, 1);
5616 selection->page = i;
5617 selection->style = EV_SELECTION_STYLE_GLYPH;
5618 selection->rect.x1 = selection->rect.y1 = 0;
5619 selection->rect.x2 = width;
5620 selection->rect.y2 = height;
5622 selections = g_list_append (selections, selection);
5625 merge_selection_region (view, selections);
5626 gtk_widget_queue_draw (GTK_WIDGET (view));
5630 ev_view_get_has_selection (EvView *view)
5632 return view->selection_info.selections != NULL;
5636 get_selected_text (EvView *view)
5640 gchar *normalized_text;
5642 text = g_string_new (NULL);
5644 ev_document_doc_mutex_lock ();
5646 for (l = view->selection_info.selections; l != NULL; l = l->next) {
5647 EvViewSelection *selection = (EvViewSelection *)l->data;
5651 page = ev_document_get_page (view->document, selection->page);
5652 tmp = ev_selection_get_selected_text (EV_SELECTION (view->document),
5653 page, selection->style,
5654 &(selection->rect));
5655 g_object_unref (page);
5656 g_string_append (text, tmp);
5660 ev_document_doc_mutex_unlock ();
5662 normalized_text = g_utf8_normalize (text->str, text->len, G_NORMALIZE_NFKC);
5663 g_string_free (text, TRUE);
5664 return normalized_text;
5668 ev_view_clipboard_copy (EvView *view,
5671 GtkClipboard *clipboard;
5673 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (view),
5674 GDK_SELECTION_CLIPBOARD);
5675 gtk_clipboard_set_text (clipboard, text, -1);
5679 ev_view_copy (EvView *ev_view)
5683 if (!EV_IS_SELECTION (ev_view->document))
5686 text = get_selected_text (ev_view);
5687 ev_view_clipboard_copy (ev_view, text);
5692 ev_view_primary_get_cb (GtkClipboard *clipboard,
5693 GtkSelectionData *selection_data,
5697 EvView *ev_view = EV_VIEW (data);
5699 if (ev_view->link_selected) {
5700 gtk_selection_data_set_text (selection_data,
5701 ev_link_action_get_uri (ev_view->link_selected),
5703 } else if (EV_IS_SELECTION (ev_view->document) &&
5704 ev_view->selection_info.selections) {
5707 text = get_selected_text (ev_view);
5709 gtk_selection_data_set_text (selection_data, text, -1);
5716 ev_view_primary_clear_cb (GtkClipboard *clipboard,
5719 EvView *view = EV_VIEW (data);
5721 clear_selection (view);
5722 clear_link_selected (view);
5726 ev_view_update_primary_selection (EvView *ev_view)
5728 GtkClipboard *clipboard;
5730 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (ev_view),
5731 GDK_SELECTION_PRIMARY);
5733 if (ev_view->selection_info.selections || ev_view->link_selected) {
5734 GtkTargetList *target_list;
5735 GtkTargetEntry *targets;
5738 target_list = gtk_target_list_new (NULL, 0);
5739 gtk_target_list_add_text_targets (target_list, 0);
5740 targets = gtk_target_table_new_from_list (target_list, &n_targets);
5741 gtk_target_list_unref (target_list);
5743 if (!gtk_clipboard_set_with_owner (clipboard,
5745 ev_view_primary_get_cb,
5746 ev_view_primary_clear_cb,
5747 G_OBJECT (ev_view)))
5748 ev_view_primary_clear_cb (clipboard, ev_view);
5750 gtk_target_table_free (targets, n_targets);
5752 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (ev_view))
5753 gtk_clipboard_clear (clipboard);
5758 clear_link_selected (EvView *view)
5760 if (view->link_selected) {
5761 g_object_unref (view->link_selected);
5762 view->link_selected = NULL;
5767 ev_view_copy_link_address (EvView *view,
5768 EvLinkAction *action)
5770 clear_link_selected (view);
5772 ev_view_clipboard_copy (view, ev_link_action_get_uri (action));
5774 view->link_selected = g_object_ref (action);
5775 ev_view_update_primary_selection (view);
5778 /*** Cursor operations ***/
5780 ev_view_set_cursor (EvView *view, EvViewCursor new_cursor)
5782 GdkCursor *cursor = NULL;
5784 GdkWindow *bin_window;
5786 if (view->cursor == new_cursor) {
5790 view->cursor = new_cursor;
5792 bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (view));
5793 widget = gtk_widget_get_toplevel (GTK_WIDGET (view));
5794 cursor = ev_view_cursor_new (gtk_widget_get_display (widget), new_cursor);
5795 gdk_window_set_cursor (bin_window, cursor);
5798 gdk_cursor_unref (cursor);
5802 ev_view_hide_cursor (EvView *view)
5804 ev_view_set_cursor (view, EV_VIEW_CURSOR_HIDDEN);
5808 ev_view_show_cursor (EvView *view)
5810 ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL);
5814 ev_view_next_page (EvView *view)
5818 g_return_val_if_fail (EV_IS_VIEW (view), FALSE);
5820 if (!view->document)
5823 page = ev_document_model_get_page (view->model);
5824 n_pages = ev_document_get_n_pages (view->document);
5826 if (view->dual_page)
5831 if (page < n_pages) {
5832 ev_document_model_set_page (view->model, page);
5834 } else if (view->dual_page && page == n_pages) {
5835 ev_document_model_set_page (view->model, page - 1);
5843 ev_view_previous_page (EvView *view)
5847 g_return_val_if_fail (EV_IS_VIEW (view), FALSE);
5849 if (!view->document)
5852 page = ev_document_model_get_page (view->model);
5854 if (view->dual_page)
5860 ev_document_model_set_page (view->model, page);
5862 } else if (view->dual_page && page == -1) {
5863 ev_document_model_set_page (view->model, 0);