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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 #include <gtk/gtkalignment.h>
23 #include <glib/gi18n.h>
24 #include <gtk/gtkbindings.h>
25 #include <gtk/gtkselection.h>
26 #include <gtk/gtkclipboard.h>
27 #include <gdk/gdkkeysyms.h>
28 #include <libgnomevfs/gnome-vfs-utils.h>
30 #include "ev-marshal.h"
32 #include "ev-document-find.h"
33 #include "ev-document-misc.h"
35 #include "ev-job-queue.h"
36 #include "ev-page-cache.h"
37 #include "ev-pixbuf-cache.h"
39 #define EV_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EV_TYPE_VIEW, EvViewClass))
40 #define EV_IS_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), EV_TYPE_VIEW))
41 #define EV_VIEW_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), EV_TYPE_VIEW, EvViewClass))
59 TARGET_TEXT_BUFFER_CONTENTS
63 EV_SCROLL_PAGE_FORWARD,
64 EV_SCROLL_PAGE_BACKWARD
67 static const GtkTargetEntry targets[] = {
68 { "STRING", 0, TARGET_STRING },
69 { "TEXT", 0, TARGET_TEXT },
70 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT },
71 { "UTF8_STRING", 0, TARGET_UTF8_STRING },
75 EV_VIEW_CURSOR_NORMAL,
81 #define ZOOM_IN_FACTOR 1.2
82 #define ZOOM_OUT_FACTOR (1.0/ZOOM_IN_FACTOR)
84 #define MIN_SCALE 0.05409
88 GtkWidget parent_instance;
92 GdkWindow *bin_window;
100 gboolean pressed_button;
101 gboolean has_selection;
102 GdkPoint selection_start;
103 EvRectangle selection;
106 GtkAdjustment *hadjustment;
107 GtkAdjustment *vadjustment;
109 EvPageCache *page_cache;
110 EvPixbufCache *pixbuf_cache;
116 EvJobRender *current_job;
124 gboolean show_border;
128 gboolean full_screen;
129 gboolean presentation;
130 EvSizingMode sizing_mode;
133 struct _EvViewClass {
134 GtkWidgetClass parent_class;
136 void (*set_scroll_adjustments) (EvView *view,
137 GtkAdjustment *hadjustment,
138 GtkAdjustment *vadjustment);
139 void (*scroll_view) (EvView *view,
140 GtkScrollType scroll,
141 gboolean horizontal);
146 static void ev_view_set_scroll_adjustments (EvView *view,
147 GtkAdjustment *hadjustment,
148 GtkAdjustment *vadjustment);
149 static void get_bounding_box_size (EvView *view,
152 static void view_update_range_and_current_page (EvView *view);
155 G_DEFINE_TYPE (EvView, ev_view, GTK_TYPE_WIDGET)
157 /*** Helper functions ***/
160 view_update_adjustments (EvView *view)
162 int old_x = view->scroll_x;
163 int old_y = view->scroll_y;
165 if (view->hadjustment)
166 view->scroll_x = view->hadjustment->value;
170 if (view->vadjustment)
171 view->scroll_y = view->vadjustment->value;
175 if (GTK_WIDGET_REALIZED (view) &&
176 (view->scroll_x != old_x || view->scroll_y != old_y)) {
177 gdk_window_move (view->bin_window, - view->scroll_x, - view->scroll_y);
178 // gdk_window_process_updates (view->bin_window, TRUE);
182 view_update_range_and_current_page (view);
186 view_set_adjustment_values (EvView *view,
187 GtkOrientation orientation)
189 GtkWidget *widget = GTK_WIDGET (view);
190 GtkAdjustment *adjustment;
191 gboolean value_changed = FALSE;
195 if (orientation == GTK_ORIENTATION_HORIZONTAL) {
196 requisition = widget->requisition.width;
197 allocation = widget->allocation.width;
198 adjustment = view->hadjustment;
200 requisition = widget->requisition.height;
201 allocation = widget->allocation.height;
202 adjustment = view->vadjustment;
208 adjustment->page_size = allocation;
209 adjustment->step_increment = allocation * 0.1;
210 adjustment->page_increment = allocation * 0.9;
211 adjustment->lower = 0;
212 adjustment->upper = MAX (allocation, requisition);
214 if (adjustment->value > adjustment->upper - adjustment->page_size) {
215 adjustment->value = adjustment->upper - adjustment->page_size;
216 value_changed = TRUE;
219 gtk_adjustment_changed (adjustment);
221 gtk_adjustment_value_changed (adjustment);
225 view_update_range_and_current_page (EvView *view)
228 /* Presentation trumps all other modes */
229 if (view->presentation) {
230 view->start_page = view->current_page;
231 view->end_page = view->current_page;
232 } else if (view->continuous) {
233 GdkRectangle current_area, unused, page_area;
234 gboolean found = FALSE;
237 get_bounding_box_size (view, &(page_area.width), &(page_area.height));
238 page_area.x = view->spacing;
239 page_area.y = view->spacing;
241 if (view->hadjustment) {
242 current_area.x = view->hadjustment->value;
243 current_area.width = view->hadjustment->page_size;
245 current_area.x = page_area.x;
246 current_area.width = page_area.width;
249 if (view->vadjustment) {
250 current_area.y = view->vadjustment->value;
251 current_area.height = view->vadjustment->page_size;
253 current_area.y = page_area.y;
254 current_area.height = page_area.height;
257 for (i = 0; i < ev_page_cache_get_n_pages (view->page_cache); i++) {
258 if (gdk_rectangle_intersect (¤t_area, &page_area, &unused)) {
260 view->start_page = i;
267 if (view->dual_page) {
269 page_area.x += page_area.width + view->spacing;
271 page_area.x = view->spacing;
272 page_area.y += page_area.height + view->spacing;
275 page_area.y += page_area.height + view->spacing;
279 if (view->dual_page) {
280 if (view->current_page % 2 == 0) {
281 view->start_page = view->current_page;
282 if (view->current_page + 1 < ev_page_cache_get_n_pages (view->page_cache))
283 view->end_page = view->start_page + 1;
285 view->start_page = view->current_page - 1;
286 view->end_page = view->current_page;
289 view->start_page = view->current_page;
290 view->end_page = view->current_page;
294 ev_pixbuf_cache_set_page_range (view->pixbuf_cache,
300 /*** Virtual function implementations ***/
303 ev_view_finalize (GObject *object)
305 EvView *view = EV_VIEW (object);
309 g_free (view->status);
310 g_free (view->find_status);
312 G_OBJECT_CLASS (ev_view_parent_class)->finalize (object);
316 ev_view_destroy (GtkObject *object)
318 EvView *view = EV_VIEW (object);
320 if (view->document) {
321 g_object_unref (view->document);
322 view->document = NULL;
324 if (view->pixbuf_cache) {
325 g_object_unref (view->pixbuf_cache);
326 view->pixbuf_cache = NULL;
328 ev_view_set_scroll_adjustments (view, NULL, NULL);
330 GTK_OBJECT_CLASS (ev_view_parent_class)->destroy (object);
334 ev_view_get_offsets (EvView *view, int *x_offset, int *y_offset)
336 EvDocument *document = view->document;
337 GtkWidget *widget = GTK_WIDGET (view);
338 int width, height, target_width, target_height;
340 g_return_if_fail (EV_IS_DOCUMENT (document));
342 ev_page_cache_get_size (view->page_cache,
347 *x_offset = view->spacing;
348 *y_offset = view->spacing;
349 target_width = width + view->border.left +
350 view->border.right + view->spacing * 2;
351 target_height = height + view->border.top +
352 view->border.bottom + view->spacing * 2;
353 *x_offset += MAX (0, (widget->allocation.width - target_width) / 2);
354 *y_offset += MAX (0, (widget->allocation.height - target_height) / 2);
358 view_rect_to_doc_rect (EvView *view, GdkRectangle *view_rect, EvRectangle *doc_rect)
360 int x_offset, y_offset;
362 ev_view_get_offsets (view, &x_offset, &y_offset);
363 doc_rect->x1 = (double) (view_rect->x - x_offset) / view->scale;
364 doc_rect->y1 = (double) (view_rect->y - y_offset) / view->scale;
365 doc_rect->x2 = doc_rect->x1 + (double) view_rect->width / view->scale;
366 doc_rect->y2 = doc_rect->y1 + (double) view_rect->height / view->scale;
370 doc_rect_to_view_rect (EvView *view, EvRectangle *doc_rect, GdkRectangle *view_rect)
372 int x_offset, y_offset;
374 ev_view_get_offsets (view, &x_offset, &y_offset);
375 view_rect->x = floor (doc_rect->x1 * view->scale) + x_offset;
376 view_rect->y = floor (doc_rect->y1 * view->scale) + y_offset;
377 view_rect->width = ceil (doc_rect->x2 * view->scale) + x_offset - view_rect->x;
378 view_rect->height = ceil (doc_rect->y2 * view->scale) + y_offset - view_rect->y;
382 compute_border (EvView *view, int width, int height, GtkBorder *border)
384 if (view->show_border) {
385 ev_document_misc_get_page_border_size (width, height, border);
395 get_bounding_box_size (EvView *view, int *max_width, int *max_height)
401 ev_page_cache_get_max_width_size (view->page_cache,
404 compute_border (view, width, height, &border);
405 *max_width = width + border.left + border.right;
410 ev_page_cache_get_max_height_size (view->page_cache,
413 compute_border (view, width, height, &border);
414 *max_height = height + border.top + border.bottom;
420 ev_view_size_request_continuous_and_dual_page (EvView *view,
421 GtkRequisition *requisition)
423 int max_width, max_height;
426 get_bounding_box_size (view, &max_width, &max_height);
428 n_rows = (1 + ev_page_cache_get_n_pages (view->page_cache)) / 2;
430 requisition->width = (max_width * 2) + (view->spacing * 3);
431 requisition->height = max_height * n_rows + (view->spacing * (n_rows + 1));
433 if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
434 requisition->width = 1;
435 } else if (view->sizing_mode == EV_SIZING_BEST_FIT) {
436 requisition->width = 1;
437 /* FIXME: This could actually be set on one page docs or docs
438 * with a strange aspect ratio. */
439 /* requisition->height = 1;*/
444 ev_view_size_request_continuous (EvView *view,
445 GtkRequisition *requisition)
447 int max_width, max_height;
450 get_bounding_box_size (view, &max_width, &max_height);
452 n_pages = ev_page_cache_get_n_pages (view->page_cache);
454 requisition->width = max_width + (view->spacing * 2);
455 requisition->height = max_height * n_pages + (view->spacing * (n_pages + 1));
457 if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
458 requisition->width = 1;
459 } else if (view->sizing_mode == EV_SIZING_BEST_FIT) {
460 requisition->width = 1;
461 /* FIXME: This could actually be set on one page docs or docs
462 * with a strange aspect ratio. */
463 /* requisition->height = 1;*/
468 ev_view_size_request_dual_page (EvView *view,
469 GtkRequisition *requisition)
474 /* Find the largest of the two. */
475 ev_page_cache_get_size (view->page_cache,
479 if (view->current_page + 1 < ev_page_cache_get_n_pages (view->page_cache)) {
480 gint width_2, height_2;
481 ev_page_cache_get_size (view->page_cache,
482 view->current_page + 1,
484 &width_2, &height_2);
485 if (width_2 > width) {
490 compute_border (view, width, height, &border);
492 requisition->width = ((width + border.left + border.right) * 2) +
494 requisition->height = (height + border.top + border.bottom) +
497 if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
498 requisition->width = 1;
499 } else if (view->sizing_mode == EV_SIZING_BEST_FIT) {
500 requisition->width = 1;
501 requisition->height = 1;
506 ev_view_size_request_single_page (EvView *view,
507 GtkRequisition *requisition)
512 ev_page_cache_get_size (view->page_cache,
516 compute_border (view, width, height, &border);
518 requisition->width = width + border.left + border.right + (2 * view->spacing);
519 requisition->height = height + border.top + border.bottom + (2 * view->spacing);
521 if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
522 requisition->width = 1;
523 requisition->height = height + border.top + border.bottom + (2 * view->spacing);
524 } else if (view->sizing_mode == EV_SIZING_BEST_FIT) {
525 requisition->width = 1;
526 requisition->height = 1;
531 ev_view_size_request (GtkWidget *widget,
532 GtkRequisition *requisition)
534 EvView *view = EV_VIEW (widget);
536 if (!GTK_WIDGET_REALIZED (widget))
539 if (view->document == NULL) {
540 requisition->width = 1;
541 requisition->height = 1;
545 if (view->presentation) {
546 requisition->width = 1;
547 requisition->height = 1;
551 if (view->continuous && view->dual_page)
552 ev_view_size_request_continuous_and_dual_page (view, requisition);
553 else if (view->continuous)
554 ev_view_size_request_continuous (view, requisition);
555 else if (view->dual_page)
556 ev_view_size_request_dual_page (view, requisition);
558 ev_view_size_request_single_page (view, requisition);
562 ev_view_size_allocate (GtkWidget *widget,
563 GtkAllocation *allocation)
565 EvView *view = EV_VIEW (widget);
567 GTK_WIDGET_CLASS (ev_view_parent_class)->size_allocate (widget, allocation);
569 view_set_adjustment_values (view, GTK_ORIENTATION_HORIZONTAL);
570 view_set_adjustment_values (view, GTK_ORIENTATION_VERTICAL);
572 if (GTK_WIDGET_REALIZED (widget)) {
573 gdk_window_resize (view->bin_window,
574 MAX (widget->allocation.width, widget->requisition.width),
575 MAX (widget->allocation.height, widget->requisition.height));
579 view_update_range_and_current_page (view);
583 ev_view_realize (GtkWidget *widget)
585 EvView *view = EV_VIEW (widget);
586 GdkWindowAttr attributes;
588 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
591 attributes.window_type = GDK_WINDOW_CHILD;
592 attributes.wclass = GDK_INPUT_OUTPUT;
593 attributes.visual = gtk_widget_get_visual (widget);
594 attributes.colormap = gtk_widget_get_colormap (widget);
596 attributes.x = widget->allocation.x;
597 attributes.y = widget->allocation.y;
598 attributes.width = widget->allocation.width;
599 attributes.height = widget->allocation.height;
600 attributes.event_mask = 0;
602 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
604 GDK_WA_X | GDK_WA_Y |
607 gdk_window_set_user_data (widget->window, widget);
608 widget->style = gtk_style_attach (widget->style, widget->window);
609 gdk_window_set_background (widget->window, &widget->style->mid[widget->state]);
613 attributes.width = MAX (widget->allocation.width, widget->requisition.width);
614 attributes.height = MAX (widget->allocation.height, widget->requisition.height);
615 attributes.event_mask = GDK_EXPOSURE_MASK |
616 GDK_BUTTON_PRESS_MASK |
617 GDK_BUTTON_RELEASE_MASK |
620 GDK_POINTER_MOTION_MASK |
621 GDK_LEAVE_NOTIFY_MASK;
623 view->bin_window = gdk_window_new (widget->window,
625 GDK_WA_X | GDK_WA_Y |
628 gdk_window_set_user_data (view->bin_window, widget);
629 gdk_window_show (view->bin_window);
631 widget->style = gtk_style_attach (widget->style, view->bin_window);
632 gdk_window_set_background (view->bin_window, &widget->style->mid[widget->state]);
634 if (view->document) {
635 /* We can't get page size without a target, so we have to
636 * queue a size request at realization. Could be fixed
637 * with EvDocument changes to allow setting a GdkScreen
638 * without setting a target.
640 gtk_widget_queue_resize (widget);
645 ev_view_unrealize (GtkWidget *widget)
647 EvView *view = EV_VIEW (widget);
649 gdk_window_set_user_data (view->bin_window, NULL);
650 gdk_window_destroy (view->bin_window);
651 view->bin_window = NULL;
653 GTK_WIDGET_CLASS (ev_view_parent_class)->unrealize (widget);
657 ev_view_scroll_event (GtkWidget *widget, GdkEventScroll *event)
659 EvView *view = EV_VIEW (widget);
661 if ((event->state & GDK_CONTROL_MASK) != 0) {
663 ev_view_set_sizing_mode (view, EV_SIZING_FREE);
665 if ((event->direction == GDK_SCROLL_DOWN ||
666 event->direction == GDK_SCROLL_LEFT) &&
667 ev_view_can_zoom_in (view)) {
668 ev_view_zoom_in (view);
669 } else if (ev_view_can_zoom_out (view)) {
670 ev_view_zoom_out (view);
676 if ((event->state & GDK_SHIFT_MASK) != 0) {
677 if (event->direction == GDK_SCROLL_UP)
678 event->direction = GDK_SCROLL_LEFT;
679 if (event->direction == GDK_SCROLL_DOWN)
680 event->direction = GDK_SCROLL_RIGHT;
689 ev_gdk_color_to_rgb (const GdkColor *color)
692 result = (0xff0000 | (color->red & 0xff00));
694 result |= ((color->green & 0xff00) | (color->blue >> 8));
699 draw_rubberband (GtkWidget *widget, GdkWindow *window,
700 const GdkRectangle *rect, guchar alpha)
704 GdkColor *fill_color_gdk;
707 fill_color_gdk = gdk_color_copy (>K_WIDGET (widget)->style->base[GTK_STATE_SELECTED]);
708 fill_color = ev_gdk_color_to_rgb (fill_color_gdk) << 8 | alpha;
710 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
711 rect->width, rect->height);
712 gdk_pixbuf_fill (pixbuf, fill_color);
714 gdk_draw_pixbuf (window, NULL, pixbuf,
717 rect->width, rect->height,
721 g_object_unref (pixbuf);
723 gc = gdk_gc_new (window);
724 gdk_gc_set_rgb_fg_color (gc, fill_color_gdk);
725 gdk_draw_rectangle (window, gc, FALSE,
731 gdk_color_free (fill_color_gdk);
736 highlight_find_results (EvView *view)
738 EvDocumentFind *find;
741 g_return_if_fail (EV_IS_DOCUMENT_FIND (view->document));
743 find = EV_DOCUMENT_FIND (view->document);
745 results = ev_document_find_get_n_results (find, view->current_page);
747 for (i = 0; i < results; i++) {
748 EvRectangle rectangle;
749 GdkRectangle view_rectangle;
752 alpha = (i == view->find_result) ? 0x90 : 0x20;
753 ev_document_find_get_result (find, view->current_page,
755 doc_rect_to_view_rect (view, &rectangle, &view_rectangle);
756 draw_rubberband (GTK_WIDGET (view), view->bin_window,
757 &view_rectangle, alpha);
764 get_page_extents (EvView *view,
766 GdkRectangle *page_area,
772 widget = GTK_WIDGET (view);
774 /* Quick sanity check */
775 if (view->presentation) {
776 if (view->current_page != page)
778 } else if (view->continuous) {
779 if (page < view->start_page ||
780 page > view->end_page)
782 } else if (view->dual_page) {
783 if (ABS (page - view->current_page) > 1)
786 if (view->current_page != page)
790 /* Get the size of the page */
791 ev_page_cache_get_size (view->page_cache, page,
794 compute_border (view, width, height, border);
795 page_area->width = width + border->left + border->right;
796 page_area->height = height + border->top + border->bottom;
798 if (view->presentation) {
799 page_area->x = (MAX (0, widget->allocation.width - width))/2;
800 page_area->y = (MAX (0, widget->allocation.height - height))/2;
801 } else if (view->continuous) {
802 gint max_width, max_height;
805 get_bounding_box_size (view, &max_width, &max_height);
806 /* Get the location of the bounding box */
807 if (view->dual_page) {
808 x = view->spacing + (page % 2) * (max_width + view->spacing);
809 y = view->spacing + (page / 2) * (max_height + view->spacing);
810 x = x + MAX (0, widget->allocation.width - (max_width * 2 + view->spacing * 3))/2;
813 y = view->spacing + page * (max_height + view->spacing);
814 x = x + MAX (0, widget->allocation.width - (max_width + view->spacing * 2))/2;
820 if (view->dual_page) {
821 gint width_2, height_2;
822 gint max_width = width;
823 gint max_height = height;
824 GtkBorder overall_border;
827 other_page = page ^ 1;
829 /* First, we get the bounding box of the two pages */
830 if (other_page < ev_page_cache_get_n_pages (view->page_cache)) {
831 ev_page_cache_get_size (view->page_cache,
834 &width_2, &height_2);
837 if (height_2 > height)
838 max_height = height_2;
840 compute_border (view, max_width, max_height, &overall_border);
842 /* Find the offsets */
846 /* Adjust for being the left or right page */
848 x = x + max_width - width;
850 x = x + (max_width + overall_border.left + overall_border.right) + view->spacing;
852 y = y + (max_height - height)/2;
854 /* Adjust for extra allocation */
855 x = x + MAX (0, widget->allocation.width -
856 ((max_width + overall_border.left + overall_border.right) * 2 + view->spacing * 3))/2;
857 y = y + MAX (0, widget->allocation.height - (height + view->spacing * 2))/2;
862 /* Adjust for extra allocation */
863 x = x + MAX (0, widget->allocation.width - (width + view->spacing * 2))/2;
864 y = y + MAX (0, widget->allocation.height - (height + view->spacing * 2))/2;
875 draw_one_page (EvView *view,
877 GdkRectangle *page_area,
879 GdkRectangle *expose_area)
882 GdkPixbuf *scaled_image;
883 GdkPixbuf *current_pixbuf;
884 GdkRectangle overlap;
885 GdkRectangle real_page_area;
887 g_assert (view->document);
889 if (! gdk_rectangle_intersect (page_area, expose_area, &overlap))
892 ev_page_cache_get_size (view->page_cache,
896 if (view->show_border)
897 ev_document_misc_paint_one_page (view->bin_window,
901 /* Render the document itself */
902 real_page_area = *page_area;
904 real_page_area.x += border->left;
905 real_page_area.y += border->top;
906 real_page_area.width -= (border->left + border->right);
907 real_page_area.height -= (border->top + border->bottom);
909 if (! gdk_rectangle_intersect (&real_page_area, expose_area, &overlap))
912 current_pixbuf = ev_pixbuf_cache_get_pixbuf (view->pixbuf_cache, page);
913 if (current_pixbuf == NULL)
915 else if (width == gdk_pixbuf_get_width (current_pixbuf) &&
916 height == gdk_pixbuf_get_height (current_pixbuf))
917 scaled_image = g_object_ref (current_pixbuf);
919 scaled_image = gdk_pixbuf_scale_simple (current_pixbuf,
924 gdk_draw_pixbuf (view->bin_window,
925 GTK_WIDGET (view)->style->fg_gc[GTK_STATE_NORMAL],
927 overlap.x - real_page_area.x,
928 overlap.y - real_page_area.y,
929 overlap.x, overlap.y,
930 overlap.width, overlap.height,
931 GDK_RGB_DITHER_NORMAL,
933 g_object_unref (scaled_image);
938 ev_view_bin_expose (EvView *view,
939 GdkEventExpose *event)
943 if (view->document == NULL)
946 for (i = view->start_page; i <= view->end_page; i++) {
947 GdkRectangle page_area;
950 if (! get_page_extents (view, i, &page_area, &border))
952 draw_one_page (view, i, &page_area, &border, &(event->area));
956 if (EV_IS_DOCUMENT_FIND (view->document)) {
957 highlight_find_results (view);
960 if (view->has_selection) {
961 GdkRectangle rubberband;
963 doc_rect_to_view_rect (view, &view->selection, &rubberband);
964 if (rubberband.width > 0 && rubberband.height > 0) {
965 draw_rubberband (GTK_WIDGET (view), view->bin_window,
973 ev_view_expose_event (GtkWidget *widget,
974 GdkEventExpose *event)
976 EvView *view = EV_VIEW (widget);
978 if (event->window == view->bin_window)
979 ev_view_bin_expose (view, event);
981 return GTK_WIDGET_CLASS (ev_view_parent_class)->expose_event (widget, event);
987 ev_view_select_all (EvView *ev_view)
989 GtkWidget *widget = GTK_WIDGET (ev_view);
990 GdkRectangle selection;
992 int x_offset, y_offset;
994 g_return_if_fail (EV_IS_VIEW (ev_view));
997 ev_view_get_offsets (ev_view, &x_offset, &y_offset);
998 ev_page_cache_get_size (ev_view->page_cache,
999 ev_view->current_page,
1003 ev_view->has_selection = TRUE;
1004 selection.x = x_offset + ev_view->border.left;
1005 selection.y = y_offset + ev_view->border.top;
1006 selection.width = width;
1007 selection.height = height;
1008 view_rect_to_doc_rect (ev_view, &selection, &ev_view->selection);
1010 gtk_widget_queue_draw (widget);
1014 ev_view_copy (EvView *ev_view)
1016 GtkClipboard *clipboard;
1019 if (!ev_document_can_get_text (ev_view->document)) {
1023 ev_document_doc_mutex_lock ();
1024 text = ev_document_get_text (ev_view->document,
1025 ev_view->current_page,
1026 &ev_view->selection);
1027 ev_document_doc_mutex_unlock ();
1029 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (ev_view),
1030 GDK_SELECTION_CLIPBOARD);
1031 gtk_clipboard_set_text (clipboard, text, -1);
1036 ev_view_primary_get_cb (GtkClipboard *clipboard,
1037 GtkSelectionData *selection_data,
1041 EvView *ev_view = EV_VIEW (data);
1044 if (!ev_document_can_get_text (ev_view->document)) {
1048 ev_document_doc_mutex_lock ();
1049 text = ev_document_get_text (ev_view->document,
1050 ev_view->current_page,
1051 &ev_view->selection);
1052 ev_document_doc_mutex_unlock ();
1053 gtk_selection_data_set_text (selection_data, text, -1);
1057 ev_view_primary_clear_cb (GtkClipboard *clipboard,
1060 EvView *ev_view = EV_VIEW (data);
1062 ev_view->has_selection = FALSE;
1066 ev_view_update_primary_selection (EvView *ev_view)
1068 GtkClipboard *clipboard;
1070 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (ev_view),
1071 GDK_SELECTION_PRIMARY);
1073 if (ev_view->has_selection) {
1074 if (!gtk_clipboard_set_with_owner (clipboard,
1076 G_N_ELEMENTS (targets),
1077 ev_view_primary_get_cb,
1078 ev_view_primary_clear_cb,
1079 G_OBJECT (ev_view)))
1080 ev_view_primary_clear_cb (clipboard, ev_view);
1082 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (ev_view))
1083 gtk_clipboard_clear (clipboard);
1088 ev_view_button_press_event (GtkWidget *widget,
1089 GdkEventButton *event)
1091 EvView *view = EV_VIEW (widget);
1093 if (!GTK_WIDGET_HAS_FOCUS (widget)) {
1094 gtk_widget_grab_focus (widget);
1097 view->pressed_button = event->button;
1099 switch (event->button) {
1101 if (view->has_selection) {
1102 view->has_selection = FALSE;
1103 gtk_widget_queue_draw (widget);
1106 view->selection_start.x = event->x;
1107 view->selection_start.y = event->y;
1115 status_message_from_link (EvView *view, EvLink *link)
1121 type = ev_link_get_link_type (link);
1124 case EV_LINK_TYPE_TITLE:
1125 if (ev_link_get_title (link))
1126 msg = g_strdup (ev_link_get_title (link));
1128 case EV_LINK_TYPE_PAGE:
1129 page_label = ev_page_cache_get_page_label (view->page_cache, ev_link_get_page (link));
1130 msg = g_strdup_printf (_("Go to page %s"), page_label);
1131 g_free (page_label);
1133 case EV_LINK_TYPE_EXTERNAL_URI:
1134 msg = g_strdup (ev_link_get_uri (link));
1144 ev_view_set_status (EvView *view, const char *message)
1146 g_return_if_fail (EV_IS_VIEW (view));
1148 if (message != view->status) {
1149 g_free (view->status);
1150 view->status = g_strdup (message);
1151 g_object_notify (G_OBJECT (view), "status");
1156 ev_view_set_find_status (EvView *view, const char *message)
1158 g_return_if_fail (EV_IS_VIEW (view));
1160 g_free (view->find_status);
1161 view->find_status = g_strdup (message);
1162 g_object_notify (G_OBJECT (view), "find-status");
1166 ev_view_create_invisible_cursor(void)
1169 GdkColor black = { 0, 0, 0, 0 };
1170 static char bits[] = { 0x00 };
1172 empty = gdk_bitmap_create_from_data (NULL, bits, 1, 1);
1174 return gdk_cursor_new_from_pixmap (empty, empty, &black, &black, 0, 0);
1178 ev_view_set_cursor (EvView *view, EvViewCursor new_cursor)
1180 GdkCursor *cursor = NULL;
1181 GdkDisplay *display;
1184 if (view->cursor == new_cursor) {
1188 widget = gtk_widget_get_toplevel (GTK_WIDGET (view));
1189 display = gtk_widget_get_display (widget);
1190 view->cursor = new_cursor;
1192 switch (new_cursor) {
1193 case EV_VIEW_CURSOR_NORMAL:
1194 gdk_window_set_cursor (widget->window, NULL);
1196 case EV_VIEW_CURSOR_LINK:
1197 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
1199 case EV_VIEW_CURSOR_WAIT:
1200 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
1202 case EV_VIEW_CURSOR_HIDDEN:
1203 cursor = ev_view_create_invisible_cursor ();
1209 gdk_window_set_cursor (widget->window, cursor);
1210 gdk_cursor_unref (cursor);
1217 find_page_at_location (EvView *view,
1226 if (view->document == NULL)
1230 g_assert (x_offset);
1231 g_assert (y_offset);
1233 for (i = view->start_page; i <= view->end_page; i++) {
1234 GdkRectangle page_area;
1237 if (! get_page_extents (view, i, &page_area, &border))
1240 if ((x >= page_area.x + border.left) &&
1241 (x < page_area.x + page_area.width - border.right) &&
1242 (y >= page_area.y + border.top) &&
1243 (y < page_area.y + page_area.height - border.bottom)) {
1245 *x_offset = x - (page_area.x + border.left);
1246 *y_offset = y - (page_area.y + border.top);
1255 get_link_at_location (EvView *view,
1260 gint x_offset = 0, y_offset = 0;
1261 GList *link_mapping;
1263 find_page_at_location (view, x, y, &page, &x_offset, &y_offset);
1268 link_mapping = ev_pixbuf_cache_get_link_mapping (view->pixbuf_cache, page);
1271 return ev_link_mapping_find (link_mapping, x_offset / view->scale, y_offset / view->scale);
1278 ev_view_motion_notify_event (GtkWidget *widget,
1279 GdkEventMotion *event)
1281 EvView *view = EV_VIEW (widget);
1283 if (view->pressed_button > 0) {
1284 GdkRectangle selection;
1286 view->has_selection = TRUE;
1287 selection.x = MIN (view->selection_start.x, event->x);
1288 selection.y = MIN (view->selection_start.y, event->y);
1289 selection.width = ABS (view->selection_start.x - event->x) + 1;
1290 selection.height = ABS (view->selection_start.y - event->y) + 1;
1291 view_rect_to_doc_rect (view, &selection, &view->selection);
1293 gtk_widget_queue_draw (widget);
1294 } else if (view->document) {
1297 link = get_link_at_location (view, event->x, event->y);
1301 msg = status_message_from_link (view, link);
1302 ev_view_set_status (view, msg);
1303 ev_view_set_cursor (view, EV_VIEW_CURSOR_LINK);
1306 ev_view_set_status (view, NULL);
1307 if (view->cursor == EV_VIEW_CURSOR_LINK) {
1308 ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL);
1316 /* FIXME: standardize this sometime */
1318 go_to_link (EvView *view, EvLink *link)
1324 type = ev_link_get_link_type (link);
1327 case EV_LINK_TYPE_TITLE:
1329 case EV_LINK_TYPE_PAGE:
1330 page = ev_link_get_page (link);
1331 ev_page_cache_set_current_page (view->page_cache, page);
1333 case EV_LINK_TYPE_EXTERNAL_URI:
1334 uri = ev_link_get_uri (link);
1335 gnome_vfs_url_show (uri);
1342 ev_view_button_release_event (GtkWidget *widget,
1343 GdkEventButton *event)
1345 EvView *view = EV_VIEW (widget);
1347 view->pressed_button = -1;
1349 if (view->has_selection) {
1350 ev_view_update_primary_selection (view);
1351 } else if (view->document) {
1354 link = get_link_at_location (view, event->x, event->y);
1356 go_to_link (view, link);
1364 on_adjustment_value_changed (GtkAdjustment *adjustment,
1367 view_update_adjustments (view);
1371 set_scroll_adjustment (EvView *view,
1372 GtkOrientation orientation,
1373 GtkAdjustment *adjustment)
1375 GtkAdjustment **to_set;
1377 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1378 to_set = &view->hadjustment;
1380 to_set = &view->vadjustment;
1382 if (*to_set != adjustment) {
1384 g_signal_handlers_disconnect_by_func (*to_set,
1385 (gpointer) on_adjustment_value_changed,
1387 g_object_unref (*to_set);
1390 *to_set = adjustment;
1391 view_set_adjustment_values (view, orientation);
1394 g_object_ref (*to_set);
1395 g_signal_connect (*to_set, "value_changed",
1396 G_CALLBACK (on_adjustment_value_changed), view);
1402 ev_view_set_scroll_adjustments (EvView *view,
1403 GtkAdjustment *hadjustment,
1404 GtkAdjustment *vadjustment)
1406 set_scroll_adjustment (view, GTK_ORIENTATION_HORIZONTAL, hadjustment);
1407 set_scroll_adjustment (view, GTK_ORIENTATION_VERTICAL, vadjustment);
1409 view_update_adjustments (view);
1413 add_scroll_binding_keypad (GtkBindingSet *binding_set,
1415 GtkScrollType scroll,
1416 gboolean horizontal)
1418 guint keypad_keyval = keyval - GDK_Left + GDK_KP_Left;
1420 gtk_binding_entry_add_signal (binding_set, keyval, 0,
1422 GTK_TYPE_SCROLL_TYPE, scroll,
1423 G_TYPE_BOOLEAN, horizontal);
1424 gtk_binding_entry_add_signal (binding_set, keypad_keyval, 0,
1426 GTK_TYPE_SCROLL_TYPE, scroll,
1427 G_TYPE_BOOLEAN, horizontal);
1431 add_scroll_binding_shifted (GtkBindingSet *binding_set,
1433 GtkScrollType scroll_normal,
1434 GtkScrollType scroll_shifted,
1435 gboolean horizontal)
1437 gtk_binding_entry_add_signal (binding_set, keyval, 0,
1439 GTK_TYPE_SCROLL_TYPE, scroll_normal,
1440 G_TYPE_BOOLEAN, horizontal);
1441 gtk_binding_entry_add_signal (binding_set, keyval, GDK_SHIFT_MASK,
1443 GTK_TYPE_SCROLL_TYPE, scroll_shifted,
1444 G_TYPE_BOOLEAN, horizontal);
1448 ev_view_jump (EvView *view,
1449 GtkScrollType scroll)
1451 GtkAdjustment *adjustment;
1452 double value, increment;
1453 gboolean first_page = FALSE;
1454 gboolean last_page = FALSE;
1456 /* Assign values for increment and vertical adjustment */
1457 adjustment = view->vadjustment;
1458 increment = adjustment->page_size * 0.75;
1459 value = adjustment->value;
1461 /* Assign boolean for first and last page */
1462 if (view->current_page == 0)
1464 if (view->current_page == ev_page_cache_get_n_pages (view->page_cache) - 1)
1468 case EV_SCROLL_PAGE_BACKWARD:
1469 /* Do not jump backwards if at the first page */
1470 if (value == (adjustment->lower) && first_page) {
1472 /* At the top of a page, assign the upper bound limit of previous page */
1473 } else if (value == (adjustment->lower)) {
1474 value = adjustment->upper - adjustment->page_size;
1475 ev_page_cache_set_current_page (view->page_cache, view->current_page - 1);
1476 /* Jump to the top */
1478 value = MAX (value - increment, adjustment->lower);
1481 case EV_SCROLL_PAGE_FORWARD:
1482 /* Do not jump forward if at the last page */
1483 if (value == (adjustment->upper - adjustment->page_size) && last_page) {
1485 /* At the bottom of a page, assign the lower bound limit of next page */
1486 } else if (value == (adjustment->upper - adjustment->page_size)) {
1488 ev_page_cache_set_current_page (view->page_cache, view->current_page + 1);
1489 /* Jump to the bottom */
1491 value = MIN (value + increment, adjustment->upper - adjustment->page_size);
1498 gtk_adjustment_set_value (adjustment, value);
1502 ev_view_scroll_view (EvView *view,
1503 GtkScrollType scroll,
1504 gboolean horizontal)
1506 if (scroll == GTK_SCROLL_PAGE_BACKWARD) {
1507 ev_page_cache_prev_page (view->page_cache);
1508 } else if (scroll == GTK_SCROLL_PAGE_FORWARD) {
1509 ev_page_cache_next_page (view->page_cache);
1510 } else if (scroll == EV_SCROLL_PAGE_BACKWARD || scroll == EV_SCROLL_PAGE_FORWARD) {
1511 ev_view_jump (view, scroll);
1513 GtkAdjustment *adjustment;
1517 adjustment = view->hadjustment;
1519 adjustment = view->vadjustment;
1522 value = adjustment->value;
1525 case GTK_SCROLL_STEP_BACKWARD:
1526 value -= adjustment->step_increment;
1528 case GTK_SCROLL_STEP_FORWARD:
1529 value += adjustment->step_increment;
1535 value = CLAMP (value, adjustment->lower,
1536 adjustment->upper - adjustment->page_size);
1538 gtk_adjustment_set_value (adjustment, value);
1543 ev_view_set_property (GObject *object,
1545 const GValue *value,
1548 EvView *view = EV_VIEW (object);
1552 case PROP_CONTINUOUS:
1553 ev_view_set_continuous (view, g_value_get_boolean (value));
1555 case PROP_DUAL_PAGE:
1556 ev_view_set_dual_page (view, g_value_get_boolean (value));
1558 case PROP_FULL_SCREEN:
1559 ev_view_set_full_screen (view, g_value_get_boolean (value));
1561 case PROP_PRESENTATION:
1562 ev_view_set_presentation (view, g_value_get_boolean (value));
1564 case PROP_SIZING_MODE:
1565 ev_view_set_sizing_mode (view, g_value_get_enum (value));
1568 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1573 ev_view_get_property (GObject *object,
1578 EvView *view = EV_VIEW (object);
1583 g_value_set_string (value, view->status);
1585 case PROP_FIND_STATUS:
1586 g_value_set_string (value, view->status);
1588 case PROP_CONTINUOUS:
1589 g_value_set_boolean (value, view->continuous);
1591 case PROP_DUAL_PAGE:
1592 g_value_set_boolean (value, view->dual_page);
1594 case PROP_FULL_SCREEN:
1595 g_value_set_boolean (value, view->full_screen);
1597 case PROP_PRESENTATION:
1598 g_value_set_boolean (value, view->presentation);
1600 case PROP_SIZING_MODE:
1601 g_value_set_enum (value, view->sizing_mode);
1604 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1609 ev_view_class_init (EvViewClass *class)
1611 GObjectClass *object_class = G_OBJECT_CLASS (class);
1612 GtkObjectClass *gtk_object_class = GTK_OBJECT_CLASS (class);
1613 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
1614 GtkBindingSet *binding_set;
1616 object_class->finalize = ev_view_finalize;
1617 object_class->set_property = ev_view_set_property;
1618 object_class->get_property = ev_view_get_property;
1620 widget_class->expose_event = ev_view_expose_event;
1621 widget_class->button_press_event = ev_view_button_press_event;
1622 widget_class->motion_notify_event = ev_view_motion_notify_event;
1623 widget_class->button_release_event = ev_view_button_release_event;
1624 widget_class->size_request = ev_view_size_request;
1625 widget_class->size_allocate = ev_view_size_allocate;
1626 widget_class->realize = ev_view_realize;
1627 widget_class->unrealize = ev_view_unrealize;
1628 widget_class->scroll_event = ev_view_scroll_event;
1629 gtk_object_class->destroy = ev_view_destroy;
1631 class->set_scroll_adjustments = ev_view_set_scroll_adjustments;
1632 class->scroll_view = ev_view_scroll_view;
1634 widget_class->set_scroll_adjustments_signal = g_signal_new ("set-scroll-adjustments",
1635 G_OBJECT_CLASS_TYPE (object_class),
1636 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1637 G_STRUCT_OFFSET (EvViewClass, set_scroll_adjustments),
1639 ev_marshal_VOID__OBJECT_OBJECT,
1641 GTK_TYPE_ADJUSTMENT,
1642 GTK_TYPE_ADJUSTMENT);
1644 g_signal_new ("scroll_view",
1645 G_TYPE_FROM_CLASS (object_class),
1646 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1647 G_STRUCT_OFFSET (EvViewClass, scroll_view),
1649 ev_marshal_VOID__ENUM_BOOLEAN,
1651 GTK_TYPE_SCROLL_TYPE,
1654 g_object_class_install_property (object_class,
1656 g_param_spec_string ("status",
1658 "The status message",
1662 g_object_class_install_property (object_class,
1664 g_param_spec_string ("find-status",
1665 "Find Status Message",
1666 "The find status message",
1670 g_object_class_install_property (object_class,
1672 g_param_spec_boolean ("continuous",
1674 "Continuous scrolling mode",
1676 G_PARAM_READWRITE));
1678 g_object_class_install_property (object_class,
1680 g_param_spec_boolean ("dual-page",
1682 "Two pages visible at once",
1684 G_PARAM_READWRITE));
1685 g_object_class_install_property (object_class,
1687 g_param_spec_boolean ("full-screen",
1689 "Draw page in a full-screen fashion",
1691 G_PARAM_READWRITE));
1692 g_object_class_install_property (object_class,
1694 g_param_spec_boolean ("presentation",
1696 "Draw page in presentation mode",
1698 G_PARAM_READWRITE));
1700 g_object_class_install_property (object_class,
1702 g_param_spec_enum ("sizing-mode",
1705 EV_TYPE_SIZING_MODE,
1706 EV_SIZING_FIT_WIDTH,
1707 G_PARAM_READWRITE));
1709 binding_set = gtk_binding_set_by_class (class);
1711 add_scroll_binding_keypad (binding_set, GDK_Left, GTK_SCROLL_STEP_BACKWARD, TRUE);
1712 add_scroll_binding_keypad (binding_set, GDK_Right, GTK_SCROLL_STEP_FORWARD, TRUE);
1713 add_scroll_binding_keypad (binding_set, GDK_Up, GTK_SCROLL_STEP_BACKWARD, FALSE);
1714 add_scroll_binding_keypad (binding_set, GDK_Down, GTK_SCROLL_STEP_FORWARD, FALSE);
1716 add_scroll_binding_keypad (binding_set, GDK_Page_Up, GTK_SCROLL_PAGE_BACKWARD, FALSE);
1717 add_scroll_binding_keypad (binding_set, GDK_Page_Down, GTK_SCROLL_PAGE_FORWARD, FALSE);
1719 add_scroll_binding_shifted (binding_set, GDK_space, EV_SCROLL_PAGE_FORWARD, EV_SCROLL_PAGE_BACKWARD, FALSE);
1720 add_scroll_binding_shifted (binding_set, GDK_BackSpace, EV_SCROLL_PAGE_BACKWARD, EV_SCROLL_PAGE_FORWARD, FALSE);
1724 ev_view_init (EvView *view)
1726 GTK_WIDGET_SET_FLAGS (view, GTK_CAN_FOCUS);
1730 view->current_page = 0;
1731 view->pressed_button = -1;
1732 view->cursor = EV_VIEW_CURSOR_NORMAL;
1733 view->show_border = TRUE;
1735 view->continuous = TRUE;
1736 view->dual_page = FALSE;
1737 view->presentation = FALSE;
1738 view->full_screen = FALSE;
1739 view->sizing_mode = EV_SIZING_FIT_WIDTH;
1743 update_find_status_message (EvView *view)
1747 // ev_document_doc_mutex_lock ();
1748 if (view->current_page == view->find_page) {
1751 // ev_document_doc_mutex_lock ();
1752 results = ev_document_find_get_n_results
1753 (EV_DOCUMENT_FIND (view->document),
1754 view->current_page);
1755 // ev_document_doc_mutex_unlock ();
1756 /* TRANS: Sometimes this could be better translated as
1757 "%d hit(s) on this page". Therefore this string
1758 contains plural cases. */
1759 message = g_strdup_printf (ngettext ("%d found on this page",
1760 "%d found on this page",
1766 ev_document_doc_mutex_lock ();
1767 percent = ev_document_find_get_progress
1768 (EV_DOCUMENT_FIND (view->document));
1769 ev_document_doc_mutex_unlock ();
1770 if (percent >= (1.0 - 1e-10)) {
1771 message = g_strdup (_("Not found"));
1773 message = g_strdup_printf (_("%3d%% remaining to search"),
1774 (int) ((1.0 - percent) * 100));
1778 // ev_document_doc_mutex_unlock ();
1780 ev_view_set_find_status (view, message);
1781 // g_free (message);
1787 ensure_rectangle_is_visible (EvView *view, GdkRectangle *rect)
1789 GtkWidget *widget = GTK_WIDGET (view);
1790 GtkAdjustment *adjustment;
1793 adjustment = view->vadjustment;
1795 if (rect->y < adjustment->value) {
1796 value = MAX (adjustment->lower, rect->y - MARGIN);
1797 gtk_adjustment_set_value (view->vadjustment, value);
1798 } else if (rect->y + rect->height >
1799 adjustment->value + widget->allocation.height) {
1800 value = MIN (adjustment->upper, rect->y + rect->height -
1801 widget->allocation.height + MARGIN);
1802 gtk_adjustment_set_value (view->vadjustment, value);
1805 adjustment = view->hadjustment;
1807 if (rect->x < adjustment->value) {
1808 value = MAX (adjustment->lower, rect->x - MARGIN);
1809 gtk_adjustment_set_value (view->hadjustment, value);
1810 } else if (rect->x + rect->height >
1811 adjustment->value + widget->allocation.width) {
1812 value = MIN (adjustment->upper, rect->x + rect->width -
1813 widget->allocation.width + MARGIN);
1814 gtk_adjustment_set_value (view->hadjustment, value);
1819 jump_to_find_result (EvView *view)
1821 EvDocumentFind *find = EV_DOCUMENT_FIND (view->document);
1823 GdkRectangle view_rect;
1826 ev_document_doc_mutex_lock ();
1827 n_results = ev_document_find_get_n_results (find, view->current_page);
1828 ev_document_doc_mutex_unlock ();
1830 if (n_results > view->find_result) {
1831 ev_document_doc_mutex_lock ();
1832 ev_document_find_get_result
1833 (find, view->current_page, view->find_result, &rect);
1834 ev_document_doc_mutex_unlock ();
1836 doc_rect_to_view_rect (view, &rect, &view_rect);
1837 ensure_rectangle_is_visible (view, &view_rect);
1842 jump_to_find_page (EvView *view)
1846 n_pages = ev_page_cache_get_n_pages (view->page_cache);
1848 for (i = 0; i < n_pages; i++) {
1852 page = i + view->find_page;
1853 if (page >= n_pages) {
1854 page = page - n_pages;
1857 // ev_document_doc_mutex_lock ();
1858 has_results = ev_document_find_page_has_results
1859 (EV_DOCUMENT_FIND (view->document), page);
1860 if (has_results == -1) {
1861 view->find_page = page;
1863 } else if (has_results == 1) {
1864 ev_page_cache_set_current_page (view->page_cache, page);
1865 jump_to_find_result (view);
1872 find_changed_cb (EvDocument *document, int page, EvView *view)
1874 jump_to_find_page (view);
1875 jump_to_find_result (view);
1876 update_find_status_message (view);
1878 if (view->current_page == page)
1879 gtk_widget_queue_draw (GTK_WIDGET (view));
1881 /*** Public API ***/
1888 view = g_object_new (EV_TYPE_VIEW, NULL);
1894 job_finished_cb (EvPixbufCache *pixbuf_cache,
1897 gtk_widget_queue_draw (GTK_WIDGET (view));
1902 page_changed_cb (EvPageCache *page_cache,
1906 int old_page = view->current_page;
1907 int old_width, old_height;
1908 int new_width, new_height;
1909 int max_height, n_rows;
1911 if (old_page == new_page)
1914 ev_page_cache_get_size (page_cache,
1917 &old_width, &old_height);
1919 view->current_page = new_page;
1920 view->has_selection = FALSE;
1922 ev_page_cache_get_size (page_cache,
1925 &new_width, &new_height);
1927 compute_border (view, new_width, new_height, &(view->border));
1929 if (new_width != old_width || new_height != old_height)
1930 gtk_widget_queue_resize (GTK_WIDGET (view));
1932 gtk_widget_queue_draw (GTK_WIDGET (view));
1934 if (view->continuous) {
1936 n_rows = view->dual_page ? new_page / 2 : new_page;
1938 get_bounding_box_size (view, NULL, &max_height);
1940 gtk_adjustment_clamp_page(view->vadjustment,
1941 (max_height + view->spacing) * n_rows,
1942 (max_height + view->spacing) * n_rows +
1943 view->vadjustment->page_size);
1945 gtk_adjustment_set_value (view->vadjustment,
1946 view->vadjustment->lower);
1949 if (EV_IS_DOCUMENT_FIND (view->document)) {
1950 view->find_page = new_page;
1951 view->find_result = 0;
1952 update_find_status_message (view);
1955 view_update_range_and_current_page (view);
1959 ev_view_set_document (EvView *view,
1960 EvDocument *document)
1962 g_return_if_fail (EV_IS_VIEW (view));
1964 if (document != view->document) {
1965 if (view->document) {
1966 g_signal_handlers_disconnect_by_func (view->document,
1969 g_object_unref (view->document);
1970 view->page_cache = NULL;
1974 view->document = document;
1975 view->find_page = 0;
1976 view->find_result = 0;
1978 if (view->document) {
1979 g_object_ref (view->document);
1980 if (EV_IS_DOCUMENT_FIND (view->document)) {
1981 g_signal_connect (view->document,
1983 G_CALLBACK (find_changed_cb),
1986 view->page_cache = ev_document_get_page_cache (view->document);
1987 g_signal_connect (view->page_cache, "page-changed", G_CALLBACK (page_changed_cb), view);
1988 view->pixbuf_cache = ev_pixbuf_cache_new (view->document);
1989 g_signal_connect (view->pixbuf_cache, "job-finished", G_CALLBACK (job_finished_cb), view);
1992 gtk_widget_queue_resize (GTK_WIDGET (view));
1996 #define EPSILON 0.0000001
1998 ev_view_zoom (EvView *view,
2005 scale = view->scale * factor;
2009 scale = CLAMP (scale, MIN_SCALE, MAX_SCALE);
2011 if (ABS (view->scale - scale) < EPSILON)
2013 view->scale = scale;
2014 gtk_widget_queue_resize (GTK_WIDGET (view));
2019 ev_view_set_continuous (EvView *view,
2020 gboolean continuous)
2022 g_return_if_fail (EV_IS_VIEW (view));
2024 continuous = continuous != FALSE;
2026 if (view->continuous != continuous) {
2027 view->continuous = continuous;
2028 gtk_widget_queue_resize (GTK_WIDGET (view));
2031 g_object_notify (G_OBJECT (view), "continuous");
2035 ev_view_set_dual_page (EvView *view,
2038 g_return_if_fail (EV_IS_VIEW (view));
2040 dual_page = dual_page != FALSE;
2042 if (view->dual_page == dual_page)
2045 view->dual_page = dual_page;
2046 /* FIXME: if we're keeping the pixbuf cache around, we should extend the
2047 * preload_cache_size to be 2 if dual_page is set.
2049 gtk_widget_queue_resize (GTK_WIDGET (view));
2051 g_object_notify (G_OBJECT (view), "dual-page");
2055 ev_view_set_full_screen (EvView *view,
2056 gboolean full_screen)
2058 g_return_if_fail (EV_IS_VIEW (view));
2060 full_screen = full_screen != FALSE;
2062 if (view->full_screen != full_screen) {
2063 view->full_screen = full_screen;
2064 gtk_widget_queue_resize (GTK_WIDGET (view));
2067 g_object_notify (G_OBJECT (view), "full-screen");
2071 ev_view_set_presentation (EvView *view,
2072 gboolean presentation)
2074 g_return_if_fail (EV_IS_VIEW (view));
2076 presentation = presentation != FALSE;
2078 if (view->presentation != presentation) {
2079 view->presentation = presentation;
2080 gtk_widget_queue_resize (GTK_WIDGET (view));
2083 g_object_notify (G_OBJECT (view), "presentation");
2087 ev_view_set_sizing_mode (EvView *view,
2088 EvSizingMode sizing_mode)
2090 g_return_if_fail (EV_IS_VIEW (view));
2092 if (view->sizing_mode != sizing_mode) {
2093 view->sizing_mode = sizing_mode;
2094 gtk_widget_queue_resize (GTK_WIDGET (view));
2096 g_object_notify (G_OBJECT (view), "sizing-mode");
2101 ev_view_can_zoom_in (EvView *view)
2103 return view->scale * ZOOM_IN_FACTOR <= MAX_SCALE;
2107 ev_view_can_zoom_out (EvView *view)
2109 return view->scale * ZOOM_OUT_FACTOR >= MIN_SCALE;
2113 ev_view_zoom_in (EvView *view)
2115 g_return_if_fail (view->sizing_mode == EV_SIZING_FREE);
2117 ev_view_zoom (view, ZOOM_IN_FACTOR, TRUE);
2121 ev_view_zoom_out (EvView *view)
2123 g_return_if_fail (view->sizing_mode == EV_SIZING_FREE);
2125 ev_view_zoom (view, ZOOM_OUT_FACTOR, TRUE);
2131 zoom_for_size_fit_width (int doc_width,
2139 scale = (double)target_width / doc_width;
2141 if (doc_height * scale > target_height)
2142 scale = (double) (target_width - vsb_width) / doc_width;
2148 zoom_for_size_best_fit (int doc_width,
2158 w_scale = (double)target_width / doc_width;
2159 h_scale = (double)target_height / doc_height;
2161 if (doc_height * w_scale > target_height)
2162 w_scale = (double) (target_width - vsb_width) / doc_width;
2163 if (doc_width * h_scale > target_width)
2164 h_scale = (double) (target_height - hsb_width) / doc_height;
2166 return MIN (w_scale, h_scale);
2170 ev_view_zoom_for_size_continuous_and_dual_page (EvView *view,
2176 int doc_width, doc_height;
2180 ev_page_cache_get_max_width_size (view->page_cache,
2183 ev_page_cache_get_max_height_size (view->page_cache,
2186 compute_border (view, doc_width, doc_height, &border);
2188 doc_width = doc_width * 2;
2189 width -= (2 * (border.left + border.right) + 3 * view->spacing);
2190 height -= (border.top + border.bottom + 2 * view->spacing - 1);
2192 /* FIXME: We really need to calculate the overall height here, not the
2193 * page height. We assume there's always a vertical scrollbar for
2194 * now. We need to fix this. */
2195 if (view->sizing_mode == EV_SIZING_FIT_WIDTH)
2196 scale = zoom_for_size_fit_width (doc_width, doc_height, width - vsb_width, height, 0);
2197 else if (view->sizing_mode == EV_SIZING_BEST_FIT)
2198 scale = zoom_for_size_best_fit (doc_width, doc_height, width - vsb_width, height, 0, hsb_height);
2200 g_assert_not_reached ();
2202 ev_view_zoom (view, scale, FALSE);
2206 ev_view_zoom_for_size_continuous (EvView *view,
2212 int doc_width, doc_height;
2216 ev_page_cache_get_max_width_size (view->page_cache,
2219 ev_page_cache_get_max_height_size (view->page_cache,
2222 compute_border (view, doc_width, doc_height, &border);
2224 width -= (border.left + border.right + 2 * view->spacing);
2225 height -= (border.top + border.bottom + 2 * view->spacing - 1);
2227 /* FIXME: We really need to calculate the overall height here, not the
2228 * page height. We assume there's always a vertical scrollbar for
2229 * now. We need to fix this. */
2230 if (view->sizing_mode == EV_SIZING_FIT_WIDTH)
2231 scale = zoom_for_size_fit_width (doc_width, doc_height, width - vsb_width, height, 0);
2232 else if (view->sizing_mode == EV_SIZING_BEST_FIT)
2233 scale = zoom_for_size_best_fit (doc_width, doc_height, width - vsb_width, height, 0, hsb_height);
2235 g_assert_not_reached ();
2237 ev_view_zoom (view, scale, FALSE);
2241 ev_view_zoom_for_size_dual_page (EvView *view,
2248 gint doc_width, doc_height;
2252 other_page = view->current_page ^ 1;
2254 /* Find the largest of the two. */
2255 ev_page_cache_get_size (view->page_cache,
2258 &doc_width, &doc_height);
2260 if (other_page < ev_page_cache_get_n_pages (view->page_cache)) {
2261 gint width_2, height_2;
2262 ev_page_cache_get_size (view->page_cache,
2265 &width_2, &height_2);
2266 if (width_2 > doc_width)
2267 doc_width = width_2;
2268 if (height_2 > doc_height)
2269 doc_height = height_2;
2271 compute_border (view, doc_width, doc_height, &border);
2273 doc_width = doc_width * 2;
2274 width -= ((border.left + border.right)* 2 + 3 * view->spacing);
2275 height -= (border.top + border.bottom + 2 * view->spacing);
2277 if (view->sizing_mode == EV_SIZING_FIT_WIDTH)
2278 scale = zoom_for_size_fit_width (doc_width, doc_height, width, height, vsb_width);
2279 else if (view->sizing_mode == EV_SIZING_BEST_FIT)
2280 scale = zoom_for_size_best_fit (doc_width, doc_height, width, height, vsb_width, hsb_height);
2282 g_assert_not_reached ();
2284 ev_view_zoom (view, scale, FALSE);
2288 ev_view_zoom_for_size_single_page (EvView *view,
2294 int doc_width, doc_height;
2298 ev_page_cache_get_size (view->page_cache,
2303 /* Get an approximate border */
2304 compute_border (view, width, height, &border);
2306 width -= (border.left + border.right + 2 * view->spacing);
2307 height -= (border.top + border.bottom + 2 * view->spacing);
2309 if (view->sizing_mode == EV_SIZING_FIT_WIDTH)
2310 scale = zoom_for_size_fit_width (doc_width, doc_height, width, height, vsb_width);
2311 else if (view->sizing_mode == EV_SIZING_BEST_FIT)
2312 scale = zoom_for_size_best_fit (doc_width, doc_height, width, height, vsb_width, hsb_height);
2314 g_assert_not_reached ();
2316 ev_view_zoom (view, scale, FALSE);
2321 ev_view_zoom_normal (EvView *view)
2323 ev_view_zoom (view, 1.0, FALSE);
2327 ev_view_set_zoom_for_size (EvView *view,
2333 g_return_if_fail (view->sizing_mode == EV_SIZING_FIT_WIDTH ||
2334 view->sizing_mode == EV_SIZING_BEST_FIT);
2335 g_return_if_fail (width >= 0);
2336 g_return_if_fail (height >= 0);
2338 if (view->document == NULL)
2341 if (view->continuous && view->dual_page)
2342 ev_view_zoom_for_size_continuous_and_dual_page (view, width, height, vsb_width, hsb_height);
2343 else if (view->continuous)
2344 ev_view_zoom_for_size_continuous (view, width, height, vsb_width, hsb_height);
2345 else if (view->dual_page)
2346 ev_view_zoom_for_size_dual_page (view, width, height, vsb_width, hsb_height);
2348 ev_view_zoom_for_size_single_page (view, width, height, vsb_width, hsb_height);
2352 ev_view_get_status (EvView *view)
2354 g_return_val_if_fail (EV_IS_VIEW (view), NULL);
2356 return view->status;
2360 ev_view_get_find_status (EvView *view)
2362 g_return_val_if_fail (EV_IS_VIEW (view), NULL);
2364 return view->find_status;
2368 ev_view_can_find_next (EvView *view)
2372 if (EV_IS_DOCUMENT_FIND (view->document)) {
2373 EvDocumentFind *find = EV_DOCUMENT_FIND (view->document);
2375 ev_document_doc_mutex_lock ();
2376 n_results = ev_document_find_get_n_results (find, view->current_page);
2377 ev_document_doc_mutex_unlock ();
2380 return n_results > 0;
2384 ev_view_find_next (EvView *view)
2386 EvPageCache *page_cache;
2387 int n_results, n_pages;
2388 EvDocumentFind *find = EV_DOCUMENT_FIND (view->document);
2390 page_cache = ev_document_get_page_cache (view->document);
2391 ev_document_doc_mutex_lock ();
2392 n_results = ev_document_find_get_n_results (find, view->current_page);
2393 ev_document_doc_mutex_unlock ();
2395 n_pages = ev_page_cache_get_n_pages (page_cache);
2397 view->find_result++;
2399 if (view->find_result >= n_results) {
2400 view->find_result = 0;
2403 if (view->find_page >= n_pages) {
2404 view->find_page = 0;
2407 jump_to_find_page (view);
2409 jump_to_find_result (view);
2410 gtk_widget_queue_draw (GTK_WIDGET (view));
2415 ev_view_find_previous (EvView *view)
2417 int n_results, n_pages;
2418 EvDocumentFind *find = EV_DOCUMENT_FIND (view->document);
2419 EvPageCache *page_cache;
2421 page_cache = ev_document_get_page_cache (view->document);
2423 ev_document_doc_mutex_lock ();
2424 n_results = ev_document_find_get_n_results (find, view->current_page);
2425 ev_document_doc_mutex_unlock ();
2427 n_pages = ev_page_cache_get_n_pages (page_cache);
2429 view->find_result--;
2431 if (view->find_result < 0) {
2432 view->find_result = 0;
2435 if (view->find_page < 0) {
2436 view->find_page = n_pages - 1;
2439 jump_to_find_page (view);
2441 jump_to_find_result (view);
2442 gtk_widget_queue_draw (GTK_WIDGET (view));
2446 ev_view_hide_cursor (EvView *view)
2448 ev_view_set_cursor (view, EV_VIEW_CURSOR_HIDDEN);
2452 ev_view_show_cursor (EvView *view)
2454 ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL);
2459 ev_sizing_mode_get_type (void)
2461 static GType etype = 0;
2463 static const GEnumValue values[] = {
2464 { EV_SIZING_FIT_WIDTH, "EV_SIZING_FIT_WIDTH", "fit-width" },
2465 { EV_SIZING_BEST_FIT, "EV_SIZING_BEST_FIT", "best-fit" },
2466 { EV_SIZING_FREE, "EV_SIZING_FREE", "free" },
2469 etype = g_enum_register_static ("EvSizingMode", values);