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 current_area.x = view->hadjustment->value;
238 current_area.width = view->hadjustment->page_size;
239 current_area.y = view->vadjustment->value;
240 current_area.height = view->vadjustment->page_size;
242 get_bounding_box_size (view, &(page_area.width), &(page_area.height));
243 page_area.x = view->spacing;
244 page_area.y = view->spacing;
246 for (i = 0; i < ev_page_cache_get_n_pages (view->page_cache); i++) {
247 if (gdk_rectangle_intersect (¤t_area, &page_area, &unused)) {
249 view->start_page = i;
256 if (view->dual_page) {
258 page_area.x += page_area.width + view->spacing;
260 page_area.x = view->spacing;
261 page_area.y += page_area.height + view->spacing;
264 page_area.y += page_area.height + view->spacing;
268 if (view->dual_page) {
269 if (view->current_page % 2 == 0) {
270 view->start_page = view->current_page;
271 if (view->current_page + 1 < ev_page_cache_get_n_pages (view->page_cache))
272 view->end_page = view->start_page + 1;
274 view->start_page = view->current_page - 1;
275 view->end_page = view->current_page;
278 view->start_page = view->current_page;
279 view->end_page = view->current_page;
283 ev_pixbuf_cache_set_page_range (view->pixbuf_cache,
289 /*** Virtual function implementations ***/
292 ev_view_finalize (GObject *object)
294 EvView *view = EV_VIEW (object);
298 g_free (view->status);
299 g_free (view->find_status);
301 G_OBJECT_CLASS (ev_view_parent_class)->finalize (object);
305 ev_view_destroy (GtkObject *object)
307 EvView *view = EV_VIEW (object);
309 if (view->document) {
310 g_object_unref (view->document);
311 view->document = NULL;
313 if (view->pixbuf_cache) {
314 g_object_unref (view->pixbuf_cache);
315 view->pixbuf_cache = NULL;
317 ev_view_set_scroll_adjustments (view, NULL, NULL);
319 GTK_OBJECT_CLASS (ev_view_parent_class)->destroy (object);
323 ev_view_get_offsets (EvView *view, int *x_offset, int *y_offset)
325 EvDocument *document = view->document;
326 GtkWidget *widget = GTK_WIDGET (view);
327 int width, height, target_width, target_height;
329 g_return_if_fail (EV_IS_DOCUMENT (document));
331 ev_page_cache_get_size (view->page_cache,
336 *x_offset = view->spacing;
337 *y_offset = view->spacing;
338 target_width = width + view->border.left +
339 view->border.right + view->spacing * 2;
340 target_height = height + view->border.top +
341 view->border.bottom + view->spacing * 2;
342 *x_offset += MAX (0, (widget->allocation.width - target_width) / 2);
343 *y_offset += MAX (0, (widget->allocation.height - target_height) / 2);
347 view_rect_to_doc_rect (EvView *view, GdkRectangle *view_rect, EvRectangle *doc_rect)
349 int x_offset, y_offset;
351 ev_view_get_offsets (view, &x_offset, &y_offset);
352 doc_rect->x1 = (double) (view_rect->x - x_offset) / view->scale;
353 doc_rect->y1 = (double) (view_rect->y - y_offset) / view->scale;
354 doc_rect->x2 = doc_rect->x1 + (double) view_rect->width / view->scale;
355 doc_rect->y2 = doc_rect->y1 + (double) view_rect->height / view->scale;
359 doc_rect_to_view_rect (EvView *view, EvRectangle *doc_rect, GdkRectangle *view_rect)
361 int x_offset, y_offset;
363 ev_view_get_offsets (view, &x_offset, &y_offset);
364 view_rect->x = floor (doc_rect->x1 * view->scale) + x_offset;
365 view_rect->y = floor (doc_rect->y1 * view->scale) + y_offset;
366 view_rect->width = ceil (doc_rect->x2 * view->scale) + x_offset - view_rect->x;
367 view_rect->height = ceil (doc_rect->y2 * view->scale) + y_offset - view_rect->y;
371 compute_border (EvView *view, int width, int height, GtkBorder *border)
373 if (view->show_border) {
374 ev_document_misc_get_page_border_size (width, height, border);
384 get_bounding_box_size (EvView *view, int *max_width, int *max_height)
390 ev_page_cache_get_max_width_size (view->page_cache,
393 compute_border (view, width, height, &border);
394 *max_width = width + border.left + border.right;
399 ev_page_cache_get_max_height_size (view->page_cache,
402 compute_border (view, width, height, &border);
403 *max_height = height + border.top + border.bottom;
409 ev_view_size_request_continuous_and_dual_page (EvView *view,
410 GtkRequisition *requisition)
412 int max_width, max_height;
415 get_bounding_box_size (view, &max_width, &max_height);
417 n_rows = (1 + ev_page_cache_get_n_pages (view->page_cache)) / 2;
419 requisition->width = (max_width * 2) + (view->spacing * 3);
420 requisition->height = max_height * n_rows + (view->spacing * (n_rows + 1));
422 if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
423 requisition->width = 1;
424 } else if (view->sizing_mode == EV_SIZING_BEST_FIT) {
425 requisition->width = 1;
426 /* FIXME: This could actually be set on one page docs or docs
427 * with a strange aspect ratio. */
428 /* requisition->height = 1;*/
433 ev_view_size_request_continuous (EvView *view,
434 GtkRequisition *requisition)
436 int max_width, max_height;
439 get_bounding_box_size (view, &max_width, &max_height);
441 n_pages = ev_page_cache_get_n_pages (view->page_cache);
443 requisition->width = max_width + (view->spacing * 2);
444 requisition->height = max_height * n_pages + (view->spacing * (n_pages + 1));
446 if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
447 requisition->width = 1;
448 } else if (view->sizing_mode == EV_SIZING_BEST_FIT) {
449 requisition->width = 1;
450 /* FIXME: This could actually be set on one page docs or docs
451 * with a strange aspect ratio. */
452 /* requisition->height = 1;*/
457 ev_view_size_request_dual_page (EvView *view,
458 GtkRequisition *requisition)
463 /* Find the largest of the two. */
464 ev_page_cache_get_size (view->page_cache,
468 if (view->current_page + 1 < ev_page_cache_get_n_pages (view->page_cache)) {
469 gint width_2, height_2;
470 ev_page_cache_get_size (view->page_cache,
471 view->current_page + 1,
473 &width_2, &height_2);
474 if (width_2 > width) {
479 compute_border (view, width, height, &border);
481 requisition->width = ((width + border.left + border.right) * 2) +
483 requisition->height = (height + border.top + border.bottom) +
486 if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
487 requisition->width = 1;
488 } else if (view->sizing_mode == EV_SIZING_BEST_FIT) {
489 requisition->width = 1;
490 requisition->height = 1;
495 ev_view_size_request_single_page (EvView *view,
496 GtkRequisition *requisition)
501 ev_page_cache_get_size (view->page_cache,
505 compute_border (view, width, height, &border);
507 requisition->width = width + border.left + border.right + (2 * view->spacing);
508 requisition->height = height + border.top + border.bottom + (2 * view->spacing);
510 if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
511 requisition->width = 1;
512 requisition->height = height + border.top + border.bottom + (2 * view->spacing);
513 } else if (view->sizing_mode == EV_SIZING_BEST_FIT) {
514 requisition->width = 1;
515 requisition->height = 1;
520 ev_view_size_request (GtkWidget *widget,
521 GtkRequisition *requisition)
523 EvView *view = EV_VIEW (widget);
525 if (!GTK_WIDGET_REALIZED (widget))
528 if (view->document == NULL) {
529 requisition->width = 1;
530 requisition->height = 1;
534 if (view->presentation) {
535 requisition->width = 1;
536 requisition->height = 1;
540 if (view->continuous && view->dual_page)
541 ev_view_size_request_continuous_and_dual_page (view, requisition);
542 else if (view->continuous)
543 ev_view_size_request_continuous (view, requisition);
544 else if (view->dual_page)
545 ev_view_size_request_dual_page (view, requisition);
547 ev_view_size_request_single_page (view, requisition);
551 ev_view_size_allocate (GtkWidget *widget,
552 GtkAllocation *allocation)
554 EvView *view = EV_VIEW (widget);
556 GTK_WIDGET_CLASS (ev_view_parent_class)->size_allocate (widget, allocation);
558 view_set_adjustment_values (view, GTK_ORIENTATION_HORIZONTAL);
559 view_set_adjustment_values (view, GTK_ORIENTATION_VERTICAL);
561 if (GTK_WIDGET_REALIZED (widget)) {
562 gdk_window_resize (view->bin_window,
563 MAX (widget->allocation.width, widget->requisition.width),
564 MAX (widget->allocation.height, widget->requisition.height));
568 view_update_range_and_current_page (view);
572 ev_view_realize (GtkWidget *widget)
574 EvView *view = EV_VIEW (widget);
575 GdkWindowAttr attributes;
577 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
580 attributes.window_type = GDK_WINDOW_CHILD;
581 attributes.wclass = GDK_INPUT_OUTPUT;
582 attributes.visual = gtk_widget_get_visual (widget);
583 attributes.colormap = gtk_widget_get_colormap (widget);
585 attributes.x = widget->allocation.x;
586 attributes.y = widget->allocation.y;
587 attributes.width = widget->allocation.width;
588 attributes.height = widget->allocation.height;
589 attributes.event_mask = 0;
591 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
593 GDK_WA_X | GDK_WA_Y |
596 gdk_window_set_user_data (widget->window, widget);
597 widget->style = gtk_style_attach (widget->style, widget->window);
598 gdk_window_set_background (widget->window, &widget->style->mid[widget->state]);
602 attributes.width = MAX (widget->allocation.width, widget->requisition.width);
603 attributes.height = MAX (widget->allocation.height, widget->requisition.height);
604 attributes.event_mask = GDK_EXPOSURE_MASK |
605 GDK_BUTTON_PRESS_MASK |
606 GDK_BUTTON_RELEASE_MASK |
609 GDK_POINTER_MOTION_MASK |
610 GDK_LEAVE_NOTIFY_MASK;
612 view->bin_window = gdk_window_new (widget->window,
614 GDK_WA_X | GDK_WA_Y |
617 gdk_window_set_user_data (view->bin_window, widget);
618 gdk_window_show (view->bin_window);
620 widget->style = gtk_style_attach (widget->style, view->bin_window);
621 gdk_window_set_background (view->bin_window, &widget->style->mid[widget->state]);
623 if (view->document) {
624 /* We can't get page size without a target, so we have to
625 * queue a size request at realization. Could be fixed
626 * with EvDocument changes to allow setting a GdkScreen
627 * without setting a target.
629 gtk_widget_queue_resize (widget);
634 ev_view_unrealize (GtkWidget *widget)
636 EvView *view = EV_VIEW (widget);
638 gdk_window_set_user_data (view->bin_window, NULL);
639 gdk_window_destroy (view->bin_window);
640 view->bin_window = NULL;
642 GTK_WIDGET_CLASS (ev_view_parent_class)->unrealize (widget);
647 ev_gdk_color_to_rgb (const GdkColor *color)
650 result = (0xff0000 | (color->red & 0xff00));
652 result |= ((color->green & 0xff00) | (color->blue >> 8));
657 draw_rubberband (GtkWidget *widget, GdkWindow *window,
658 const GdkRectangle *rect, guchar alpha)
662 GdkColor *fill_color_gdk;
665 fill_color_gdk = gdk_color_copy (>K_WIDGET (widget)->style->base[GTK_STATE_SELECTED]);
666 fill_color = ev_gdk_color_to_rgb (fill_color_gdk) << 8 | alpha;
668 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
669 rect->width, rect->height);
670 gdk_pixbuf_fill (pixbuf, fill_color);
672 gdk_draw_pixbuf (window, NULL, pixbuf,
675 rect->width, rect->height,
679 g_object_unref (pixbuf);
681 gc = gdk_gc_new (window);
682 gdk_gc_set_rgb_fg_color (gc, fill_color_gdk);
683 gdk_draw_rectangle (window, gc, FALSE,
689 gdk_color_free (fill_color_gdk);
694 highlight_find_results (EvView *view)
696 EvDocumentFind *find;
699 g_return_if_fail (EV_IS_DOCUMENT_FIND (view->document));
701 find = EV_DOCUMENT_FIND (view->document);
703 results = ev_document_find_get_n_results (find, view->current_page);
705 for (i = 0; i < results; i++) {
706 EvRectangle rectangle;
707 GdkRectangle view_rectangle;
710 alpha = (i == view->find_result) ? 0x90 : 0x20;
711 ev_document_find_get_result (find, view->current_page,
713 doc_rect_to_view_rect (view, &rectangle, &view_rectangle);
714 draw_rubberband (GTK_WIDGET (view), view->bin_window,
715 &view_rectangle, alpha);
722 get_page_extents (EvView *view,
724 GdkRectangle *page_area,
730 widget = GTK_WIDGET (view);
732 /* Quick sanity check */
733 if (view->presentation) {
734 if (view->current_page != page)
736 } else if (view->continuous) {
737 if (page < view->start_page ||
738 page > view->end_page)
740 } else if (view->dual_page) {
741 if (ABS (page - view->current_page) > 1)
744 if (view->current_page != page)
748 /* Get the size of the page */
749 ev_page_cache_get_size (view->page_cache, page,
752 compute_border (view, width, height, border);
753 page_area->width = width + border->left + border->right;
754 page_area->height = height + border->top + border->bottom;
756 if (view->presentation) {
757 page_area->x = (MAX (0, widget->allocation.width - width))/2;
758 page_area->y = (MAX (0, widget->allocation.height - height))/2;
759 } else if (view->continuous) {
760 gint max_width, max_height;
763 get_bounding_box_size (view, &max_width, &max_height);
764 /* Get the location of the bounding box */
765 if (view->dual_page) {
766 x = view->spacing + (page % 2) * (max_width + view->spacing);
767 y = view->spacing + (page / 2) * (max_height + view->spacing);
768 x = x + MAX (0, widget->allocation.width - (max_width * 2 + view->spacing * 3))/2;
771 y = view->spacing + page * (max_height + view->spacing);
772 x = x + MAX (0, widget->allocation.width - (max_width + view->spacing * 2))/2;
778 if (view->dual_page) {
779 gint width_2, height_2;
780 gint max_width = width;
781 gint max_height = height;
782 GtkBorder overall_border;
785 other_page = page ^ 1;
787 /* First, we get the bounding box of the two pages */
788 if (other_page < ev_page_cache_get_n_pages (view->page_cache)) {
789 ev_page_cache_get_size (view->page_cache,
792 &width_2, &height_2);
795 if (height_2 > height)
796 max_height = height_2;
798 compute_border (view, max_width, max_height, &overall_border);
800 /* Find the offsets */
804 /* Adjust for being the left or right page */
806 x = x + max_width - width;
808 x = x + (max_width + overall_border.left + overall_border.right) + view->spacing;
810 y = y + (max_height - height)/2;
812 /* Adjust for extra allocation */
813 x = x + MAX (0, widget->allocation.width -
814 ((max_width + overall_border.left + overall_border.right) * 2 + view->spacing * 3))/2;
815 y = y + MAX (0, widget->allocation.height - (height + view->spacing * 2))/2;
820 /* Adjust for extra allocation */
821 x = x + MAX (0, widget->allocation.width - (width + view->spacing * 2))/2;
822 y = y + MAX (0, widget->allocation.height - (height + view->spacing * 2))/2;
833 draw_one_page (EvView *view,
835 GdkRectangle *page_area,
837 GdkRectangle *expose_area)
840 GdkPixbuf *scaled_image;
841 GdkPixbuf *current_pixbuf;
842 GdkRectangle overlap;
843 GdkRectangle real_page_area;
845 g_assert (view->document);
847 if (! gdk_rectangle_intersect (page_area, expose_area, &overlap))
850 ev_page_cache_get_size (view->page_cache,
854 if (view->show_border)
855 ev_document_misc_paint_one_page (view->bin_window,
859 /* Render the document itself */
860 real_page_area = *page_area;
862 real_page_area.x += border->left;
863 real_page_area.y += border->top;
864 real_page_area.width -= (border->left + border->right);
865 real_page_area.height -= (border->top + border->bottom);
867 if (! gdk_rectangle_intersect (&real_page_area, expose_area, &overlap))
870 current_pixbuf = ev_pixbuf_cache_get_pixbuf (view->pixbuf_cache, page);
871 if (current_pixbuf == NULL)
873 else if (width == gdk_pixbuf_get_width (current_pixbuf) &&
874 height == gdk_pixbuf_get_height (current_pixbuf))
875 scaled_image = g_object_ref (current_pixbuf);
877 scaled_image = gdk_pixbuf_scale_simple (current_pixbuf,
882 gdk_draw_pixbuf (view->bin_window,
883 GTK_WIDGET (view)->style->fg_gc[GTK_STATE_NORMAL],
885 overlap.x - real_page_area.x,
886 overlap.y - real_page_area.y,
887 overlap.x, overlap.y,
888 overlap.width, overlap.height,
889 GDK_RGB_DITHER_NORMAL,
891 g_object_unref (scaled_image);
896 ev_view_bin_expose (EvView *view,
897 GdkEventExpose *event)
901 if (view->document == NULL)
904 for (i = view->start_page; i <= view->end_page; i++) {
905 GdkRectangle page_area;
908 if (! get_page_extents (view, i, &page_area, &border))
910 draw_one_page (view, i, &page_area, &border, &(event->area));
914 if (EV_IS_DOCUMENT_FIND (view->document)) {
915 highlight_find_results (view);
918 if (view->has_selection) {
919 GdkRectangle rubberband;
921 doc_rect_to_view_rect (view, &view->selection, &rubberband);
922 if (rubberband.width > 0 && rubberband.height > 0) {
923 draw_rubberband (GTK_WIDGET (view), view->bin_window,
931 ev_view_expose_event (GtkWidget *widget,
932 GdkEventExpose *event)
934 EvView *view = EV_VIEW (widget);
936 if (event->window == view->bin_window)
937 ev_view_bin_expose (view, event);
939 return GTK_WIDGET_CLASS (ev_view_parent_class)->expose_event (widget, event);
945 ev_view_select_all (EvView *ev_view)
947 GtkWidget *widget = GTK_WIDGET (ev_view);
948 GdkRectangle selection;
950 int x_offset, y_offset;
952 g_return_if_fail (EV_IS_VIEW (ev_view));
955 ev_view_get_offsets (ev_view, &x_offset, &y_offset);
956 ev_page_cache_get_size (ev_view->page_cache,
957 ev_view->current_page,
961 ev_view->has_selection = TRUE;
962 selection.x = x_offset + ev_view->border.left;
963 selection.y = y_offset + ev_view->border.top;
964 selection.width = width;
965 selection.height = height;
966 view_rect_to_doc_rect (ev_view, &selection, &ev_view->selection);
968 gtk_widget_queue_draw (widget);
972 ev_view_copy (EvView *ev_view)
974 GtkClipboard *clipboard;
977 if (!ev_document_can_get_text (ev_view->document)) {
981 ev_document_doc_mutex_lock ();
982 text = ev_document_get_text (ev_view->document,
983 ev_view->current_page,
984 &ev_view->selection);
985 ev_document_doc_mutex_unlock ();
987 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (ev_view),
988 GDK_SELECTION_CLIPBOARD);
989 gtk_clipboard_set_text (clipboard, text, -1);
994 ev_view_set_show_border (EvView *view, gboolean show_border)
996 g_return_if_fail (EV_IS_VIEW (view));
998 view->show_border = show_border;
1000 gtk_widget_queue_resize (GTK_WIDGET (view));
1004 ev_view_primary_get_cb (GtkClipboard *clipboard,
1005 GtkSelectionData *selection_data,
1009 EvView *ev_view = EV_VIEW (data);
1012 if (!ev_document_can_get_text (ev_view->document)) {
1016 ev_document_doc_mutex_lock ();
1017 text = ev_document_get_text (ev_view->document,
1018 ev_view->current_page,
1019 &ev_view->selection);
1020 ev_document_doc_mutex_unlock ();
1021 gtk_selection_data_set_text (selection_data, text, -1);
1025 ev_view_primary_clear_cb (GtkClipboard *clipboard,
1028 EvView *ev_view = EV_VIEW (data);
1030 ev_view->has_selection = FALSE;
1034 ev_view_update_primary_selection (EvView *ev_view)
1036 GtkClipboard *clipboard;
1038 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (ev_view),
1039 GDK_SELECTION_PRIMARY);
1041 if (ev_view->has_selection) {
1042 if (!gtk_clipboard_set_with_owner (clipboard,
1044 G_N_ELEMENTS (targets),
1045 ev_view_primary_get_cb,
1046 ev_view_primary_clear_cb,
1047 G_OBJECT (ev_view)))
1048 ev_view_primary_clear_cb (clipboard, ev_view);
1050 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (ev_view))
1051 gtk_clipboard_clear (clipboard);
1056 ev_view_button_press_event (GtkWidget *widget,
1057 GdkEventButton *event)
1059 EvView *view = EV_VIEW (widget);
1061 if (!GTK_WIDGET_HAS_FOCUS (widget)) {
1062 gtk_widget_grab_focus (widget);
1065 view->pressed_button = event->button;
1067 switch (event->button) {
1069 if (view->has_selection) {
1070 view->has_selection = FALSE;
1071 gtk_widget_queue_draw (widget);
1074 view->selection_start.x = event->x;
1075 view->selection_start.y = event->y;
1083 status_message_from_link (EvView *view, EvLink *link)
1089 type = ev_link_get_link_type (link);
1092 case EV_LINK_TYPE_TITLE:
1093 if (ev_link_get_title (link))
1094 msg = g_strdup (ev_link_get_title (link));
1096 case EV_LINK_TYPE_PAGE:
1097 page_label = ev_page_cache_get_page_label (view->page_cache, ev_link_get_page (link));
1098 msg = g_strdup_printf (_("Go to page %s"), page_label);
1099 g_free (page_label);
1101 case EV_LINK_TYPE_EXTERNAL_URI:
1102 msg = g_strdup (ev_link_get_uri (link));
1112 ev_view_set_status (EvView *view, const char *message)
1114 g_return_if_fail (EV_IS_VIEW (view));
1116 if (message != view->status) {
1117 g_free (view->status);
1118 view->status = g_strdup (message);
1119 g_object_notify (G_OBJECT (view), "status");
1124 ev_view_set_find_status (EvView *view, const char *message)
1126 g_return_if_fail (EV_IS_VIEW (view));
1128 g_free (view->find_status);
1129 view->find_status = g_strdup (message);
1130 g_object_notify (G_OBJECT (view), "find-status");
1134 ev_view_create_invisible_cursor(void)
1137 GdkColor black = { 0, 0, 0, 0 };
1138 static char bits[] = { 0x00 };
1140 empty = gdk_bitmap_create_from_data (NULL, bits, 1, 1);
1142 return gdk_cursor_new_from_pixmap (empty, empty, &black, &black, 0, 0);
1146 ev_view_set_cursor (EvView *view, EvViewCursor new_cursor)
1148 GdkCursor *cursor = NULL;
1149 GdkDisplay *display;
1152 if (view->cursor == new_cursor) {
1156 widget = gtk_widget_get_toplevel (GTK_WIDGET (view));
1157 display = gtk_widget_get_display (widget);
1158 view->cursor = new_cursor;
1160 switch (new_cursor) {
1161 case EV_VIEW_CURSOR_NORMAL:
1162 gdk_window_set_cursor (widget->window, NULL);
1164 case EV_VIEW_CURSOR_LINK:
1165 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
1167 case EV_VIEW_CURSOR_WAIT:
1168 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
1170 case EV_VIEW_CURSOR_HIDDEN:
1171 cursor = ev_view_create_invisible_cursor ();
1177 gdk_window_set_cursor (widget->window, cursor);
1178 gdk_cursor_unref (cursor);
1185 find_page_at_location (EvView *view,
1194 ev_page_cache_get_size (view->page_cache,
1199 x -= (view->border.left + view->spacing);
1200 y -= (view->border.top + view->spacing);
1202 if ((x < 0) || (y < 0) ||
1203 (x >= width) || (y >= height)) {
1207 *page = view->current_page;
1208 *x_offset = (gint) x;
1209 *y_offset = (gint) y;
1213 get_link_at_location (EvView *view,
1218 gint x_offset, y_offset;
1219 GList *link_mapping;
1221 find_page_at_location (view, x, y, &page, &x_offset, &y_offset);
1225 link_mapping = ev_pixbuf_cache_get_link_mapping (view->pixbuf_cache, page);
1227 return ev_link_mapping_find (link_mapping, x_offset /view->scale, y_offset /view->scale);
1232 ev_view_motion_notify_event (GtkWidget *widget,
1233 GdkEventMotion *event)
1235 EvView *view = EV_VIEW (widget);
1237 if (view->pressed_button > 0) {
1238 GdkRectangle selection;
1240 view->has_selection = TRUE;
1241 selection.x = MIN (view->selection_start.x, event->x);
1242 selection.y = MIN (view->selection_start.y, event->y);
1243 selection.width = ABS (view->selection_start.x - event->x) + 1;
1244 selection.height = ABS (view->selection_start.y - event->y) + 1;
1245 view_rect_to_doc_rect (view, &selection, &view->selection);
1247 gtk_widget_queue_draw (widget);
1248 } else if (view->document) {
1251 link = get_link_at_location (view, event->x, event->y);
1255 msg = status_message_from_link (view, link);
1256 ev_view_set_status (view, msg);
1257 ev_view_set_cursor (view, EV_VIEW_CURSOR_LINK);
1260 ev_view_set_status (view, NULL);
1261 if (view->cursor == EV_VIEW_CURSOR_LINK) {
1262 ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL);
1270 /* FIXME: standardize this sometime */
1272 go_to_link (EvView *view, EvLink *link)
1278 type = ev_link_get_link_type (link);
1281 case EV_LINK_TYPE_TITLE:
1283 case EV_LINK_TYPE_PAGE:
1284 page = ev_link_get_page (link);
1285 ev_page_cache_set_current_page (view->page_cache, page);
1287 case EV_LINK_TYPE_EXTERNAL_URI:
1288 uri = ev_link_get_uri (link);
1289 gnome_vfs_url_show (uri);
1296 ev_view_button_release_event (GtkWidget *widget,
1297 GdkEventButton *event)
1299 EvView *view = EV_VIEW (widget);
1301 view->pressed_button = -1;
1303 if (view->has_selection) {
1304 ev_view_update_primary_selection (view);
1305 } else if (view->document) {
1308 link = get_link_at_location (view, event->x, event->y);
1310 go_to_link (view, link);
1318 on_adjustment_value_changed (GtkAdjustment *adjustment,
1321 view_update_adjustments (view);
1325 set_scroll_adjustment (EvView *view,
1326 GtkOrientation orientation,
1327 GtkAdjustment *adjustment)
1329 GtkAdjustment **to_set;
1331 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1332 to_set = &view->hadjustment;
1334 to_set = &view->vadjustment;
1336 if (*to_set != adjustment) {
1338 g_signal_handlers_disconnect_by_func (*to_set,
1339 (gpointer) on_adjustment_value_changed,
1341 g_object_unref (*to_set);
1344 *to_set = adjustment;
1345 view_set_adjustment_values (view, orientation);
1348 g_object_ref (*to_set);
1349 g_signal_connect (*to_set, "value_changed",
1350 G_CALLBACK (on_adjustment_value_changed), view);
1356 ev_view_set_scroll_adjustments (EvView *view,
1357 GtkAdjustment *hadjustment,
1358 GtkAdjustment *vadjustment)
1360 set_scroll_adjustment (view, GTK_ORIENTATION_HORIZONTAL, hadjustment);
1361 set_scroll_adjustment (view, GTK_ORIENTATION_VERTICAL, vadjustment);
1363 view_update_adjustments (view);
1367 add_scroll_binding_keypad (GtkBindingSet *binding_set,
1369 GtkScrollType scroll,
1370 gboolean horizontal)
1372 guint keypad_keyval = keyval - GDK_Left + GDK_KP_Left;
1374 gtk_binding_entry_add_signal (binding_set, keyval, 0,
1376 GTK_TYPE_SCROLL_TYPE, scroll,
1377 G_TYPE_BOOLEAN, horizontal);
1378 gtk_binding_entry_add_signal (binding_set, keypad_keyval, 0,
1380 GTK_TYPE_SCROLL_TYPE, scroll,
1381 G_TYPE_BOOLEAN, horizontal);
1385 add_scroll_binding_shifted (GtkBindingSet *binding_set,
1387 GtkScrollType scroll_normal,
1388 GtkScrollType scroll_shifted,
1389 gboolean horizontal)
1391 gtk_binding_entry_add_signal (binding_set, keyval, 0,
1393 GTK_TYPE_SCROLL_TYPE, scroll_normal,
1394 G_TYPE_BOOLEAN, horizontal);
1395 gtk_binding_entry_add_signal (binding_set, keyval, GDK_SHIFT_MASK,
1397 GTK_TYPE_SCROLL_TYPE, scroll_shifted,
1398 G_TYPE_BOOLEAN, horizontal);
1402 ev_view_jump (EvView *view,
1403 GtkScrollType scroll)
1405 GtkAdjustment *adjustment;
1406 double value, increment;
1407 gboolean first_page = FALSE;
1408 gboolean last_page = FALSE;
1410 /* Assign values for increment and vertical adjustment */
1411 adjustment = view->vadjustment;
1412 increment = adjustment->page_size * 0.75;
1413 value = adjustment->value;
1415 /* Assign boolean for first and last page */
1416 if (view->current_page == 0)
1418 if (view->current_page == ev_page_cache_get_n_pages (view->page_cache) - 1)
1422 case EV_SCROLL_PAGE_BACKWARD:
1423 /* Do not jump backwards if at the first page */
1424 if (value == (adjustment->lower) && first_page) {
1426 /* At the top of a page, assign the upper bound limit of previous page */
1427 } else if (value == (adjustment->lower)) {
1428 value = adjustment->upper - adjustment->page_size;
1429 ev_page_cache_set_current_page (view->page_cache, view->current_page - 1);
1430 /* Jump to the top */
1432 value = MAX (value - increment, adjustment->lower);
1435 case EV_SCROLL_PAGE_FORWARD:
1436 /* Do not jump forward if at the last page */
1437 if (value == (adjustment->upper - adjustment->page_size) && last_page) {
1439 /* At the bottom of a page, assign the lower bound limit of next page */
1440 } else if (value == (adjustment->upper - adjustment->page_size)) {
1442 ev_page_cache_set_current_page (view->page_cache, view->current_page + 1);
1443 /* Jump to the bottom */
1445 value = MIN (value + increment, adjustment->upper - adjustment->page_size);
1452 gtk_adjustment_set_value (adjustment, value);
1456 ev_view_scroll_view (EvView *view,
1457 GtkScrollType scroll,
1458 gboolean horizontal)
1460 if (scroll == GTK_SCROLL_PAGE_BACKWARD) {
1461 ev_page_cache_prev_page (view->page_cache);
1462 } else if (scroll == GTK_SCROLL_PAGE_FORWARD) {
1463 ev_page_cache_next_page (view->page_cache);
1464 } else if (scroll == EV_SCROLL_PAGE_BACKWARD || scroll == EV_SCROLL_PAGE_FORWARD) {
1465 ev_view_jump (view, scroll);
1467 GtkAdjustment *adjustment;
1471 adjustment = view->hadjustment;
1473 adjustment = view->vadjustment;
1476 value = adjustment->value;
1479 case GTK_SCROLL_STEP_BACKWARD:
1480 value -= adjustment->step_increment;
1482 case GTK_SCROLL_STEP_FORWARD:
1483 value += adjustment->step_increment;
1489 value = CLAMP (value, adjustment->lower,
1490 adjustment->upper - adjustment->page_size);
1492 gtk_adjustment_set_value (adjustment, value);
1497 ev_view_set_property (GObject *object,
1499 const GValue *value,
1502 EvView *view = EV_VIEW (object);
1506 case PROP_CONTINUOUS:
1507 ev_view_set_continuous (view, g_value_get_boolean (value));
1509 case PROP_DUAL_PAGE:
1510 ev_view_set_dual_page (view, g_value_get_boolean (value));
1512 case PROP_FULL_SCREEN:
1513 ev_view_set_full_screen (view, g_value_get_boolean (value));
1515 case PROP_PRESENTATION:
1516 ev_view_set_presentation (view, g_value_get_boolean (value));
1518 case PROP_SIZING_MODE:
1519 ev_view_set_sizing_mode (view, g_value_get_enum (value));
1522 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1527 ev_view_get_property (GObject *object,
1532 EvView *view = EV_VIEW (object);
1537 g_value_set_string (value, view->status);
1539 case PROP_FIND_STATUS:
1540 g_value_set_string (value, view->status);
1542 case PROP_CONTINUOUS:
1543 g_value_set_boolean (value, view->continuous);
1545 case PROP_DUAL_PAGE:
1546 g_value_set_boolean (value, view->dual_page);
1548 case PROP_FULL_SCREEN:
1549 g_value_set_boolean (value, view->full_screen);
1551 case PROP_PRESENTATION:
1552 g_value_set_boolean (value, view->presentation);
1554 case PROP_SIZING_MODE:
1555 g_value_set_enum (value, view->sizing_mode);
1558 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1563 ev_view_class_init (EvViewClass *class)
1565 GObjectClass *object_class = G_OBJECT_CLASS (class);
1566 GtkObjectClass *gtk_object_class = GTK_OBJECT_CLASS (class);
1567 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
1568 GtkBindingSet *binding_set;
1570 object_class->finalize = ev_view_finalize;
1571 object_class->set_property = ev_view_set_property;
1572 object_class->get_property = ev_view_get_property;
1574 widget_class->expose_event = ev_view_expose_event;
1575 widget_class->button_press_event = ev_view_button_press_event;
1576 widget_class->motion_notify_event = ev_view_motion_notify_event;
1577 widget_class->button_release_event = ev_view_button_release_event;
1578 widget_class->size_request = ev_view_size_request;
1579 widget_class->size_allocate = ev_view_size_allocate;
1580 widget_class->realize = ev_view_realize;
1581 widget_class->unrealize = ev_view_unrealize;
1582 gtk_object_class->destroy = ev_view_destroy;
1584 class->set_scroll_adjustments = ev_view_set_scroll_adjustments;
1585 class->scroll_view = ev_view_scroll_view;
1587 widget_class->set_scroll_adjustments_signal = g_signal_new ("set-scroll-adjustments",
1588 G_OBJECT_CLASS_TYPE (object_class),
1589 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1590 G_STRUCT_OFFSET (EvViewClass, set_scroll_adjustments),
1592 ev_marshal_VOID__OBJECT_OBJECT,
1594 GTK_TYPE_ADJUSTMENT,
1595 GTK_TYPE_ADJUSTMENT);
1597 g_signal_new ("scroll_view",
1598 G_TYPE_FROM_CLASS (object_class),
1599 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1600 G_STRUCT_OFFSET (EvViewClass, scroll_view),
1602 ev_marshal_VOID__ENUM_BOOLEAN,
1604 GTK_TYPE_SCROLL_TYPE,
1607 g_object_class_install_property (object_class,
1609 g_param_spec_string ("status",
1611 "The status message",
1615 g_object_class_install_property (object_class,
1617 g_param_spec_string ("find-status",
1618 "Find Status Message",
1619 "The find status message",
1623 g_object_class_install_property (object_class,
1625 g_param_spec_boolean ("continuous",
1627 "Continuous scrolling mode",
1629 G_PARAM_READWRITE));
1631 g_object_class_install_property (object_class,
1633 g_param_spec_boolean ("dual-page",
1635 "Two pages visible at once",
1637 G_PARAM_READWRITE));
1638 g_object_class_install_property (object_class,
1640 g_param_spec_boolean ("full-screen",
1642 "Draw page in a full-screen fashion",
1644 G_PARAM_READWRITE));
1645 g_object_class_install_property (object_class,
1647 g_param_spec_boolean ("presentation",
1649 "Draw page in presentation mode",
1651 G_PARAM_READWRITE));
1653 g_object_class_install_property (object_class,
1655 g_param_spec_enum ("sizing-mode",
1658 EV_TYPE_SIZING_MODE,
1659 EV_SIZING_FIT_WIDTH,
1660 G_PARAM_READWRITE));
1662 binding_set = gtk_binding_set_by_class (class);
1664 add_scroll_binding_keypad (binding_set, GDK_Left, GTK_SCROLL_STEP_BACKWARD, TRUE);
1665 add_scroll_binding_keypad (binding_set, GDK_Right, GTK_SCROLL_STEP_FORWARD, TRUE);
1666 add_scroll_binding_keypad (binding_set, GDK_Up, GTK_SCROLL_STEP_BACKWARD, FALSE);
1667 add_scroll_binding_keypad (binding_set, GDK_Down, GTK_SCROLL_STEP_FORWARD, FALSE);
1669 add_scroll_binding_keypad (binding_set, GDK_Page_Up, GTK_SCROLL_PAGE_BACKWARD, FALSE);
1670 add_scroll_binding_keypad (binding_set, GDK_Page_Down, GTK_SCROLL_PAGE_FORWARD, FALSE);
1672 add_scroll_binding_shifted (binding_set, GDK_space, EV_SCROLL_PAGE_FORWARD, EV_SCROLL_PAGE_BACKWARD, FALSE);
1673 add_scroll_binding_shifted (binding_set, GDK_BackSpace, EV_SCROLL_PAGE_BACKWARD, EV_SCROLL_PAGE_FORWARD, FALSE);
1677 ev_view_set_spacing (EvView *view,
1680 g_return_if_fail (EV_IS_VIEW (view));
1682 view->spacing = spacing;
1684 if (view->document) {
1685 gtk_widget_queue_resize (GTK_WIDGET (view));
1690 ev_view_init (EvView *view)
1692 GTK_WIDGET_SET_FLAGS (view, GTK_CAN_FOCUS);
1696 view->current_page = 0;
1697 view->pressed_button = -1;
1698 view->cursor = EV_VIEW_CURSOR_NORMAL;
1699 view->show_border = TRUE;
1701 view->continuous = TRUE;
1702 view->dual_page = FALSE;
1703 view->presentation = FALSE;
1704 view->full_screen = FALSE;
1705 view->sizing_mode = EV_SIZING_FIT_WIDTH;
1709 update_find_status_message (EvView *view)
1713 // ev_document_doc_mutex_lock ();
1714 if (view->current_page == view->find_page) {
1717 // ev_document_doc_mutex_lock ();
1718 results = ev_document_find_get_n_results
1719 (EV_DOCUMENT_FIND (view->document),
1720 view->current_page);
1721 // ev_document_doc_mutex_unlock ();
1722 /* TRANS: Sometimes this could be better translated as
1723 "%d hit(s) on this page". Therefore this string
1724 contains plural cases. */
1725 message = g_strdup_printf (ngettext ("%d found on this page",
1726 "%d found on this page",
1732 ev_document_doc_mutex_lock ();
1733 percent = ev_document_find_get_progress
1734 (EV_DOCUMENT_FIND (view->document));
1735 ev_document_doc_mutex_unlock ();
1736 if (percent >= (1.0 - 1e-10)) {
1737 message = g_strdup (_("Not found"));
1739 message = g_strdup_printf (_("%3d%% remaining to search"),
1740 (int) ((1.0 - percent) * 100));
1744 // ev_document_doc_mutex_unlock ();
1746 ev_view_set_find_status (view, message);
1747 // g_free (message);
1753 ensure_rectangle_is_visible (EvView *view, GdkRectangle *rect)
1755 GtkWidget *widget = GTK_WIDGET (view);
1756 GtkAdjustment *adjustment;
1759 adjustment = view->vadjustment;
1761 if (rect->y < adjustment->value) {
1762 value = MAX (adjustment->lower, rect->y - MARGIN);
1763 gtk_adjustment_set_value (view->vadjustment, value);
1764 } else if (rect->y + rect->height >
1765 adjustment->value + widget->allocation.height) {
1766 value = MIN (adjustment->upper, rect->y + rect->height -
1767 widget->allocation.height + MARGIN);
1768 gtk_adjustment_set_value (view->vadjustment, value);
1771 adjustment = view->hadjustment;
1773 if (rect->x < adjustment->value) {
1774 value = MAX (adjustment->lower, rect->x - MARGIN);
1775 gtk_adjustment_set_value (view->hadjustment, value);
1776 } else if (rect->x + rect->height >
1777 adjustment->value + widget->allocation.width) {
1778 value = MIN (adjustment->upper, rect->x + rect->width -
1779 widget->allocation.width + MARGIN);
1780 gtk_adjustment_set_value (view->hadjustment, value);
1785 jump_to_find_result (EvView *view)
1787 EvDocumentFind *find = EV_DOCUMENT_FIND (view->document);
1789 GdkRectangle view_rect;
1792 ev_document_doc_mutex_lock ();
1793 n_results = ev_document_find_get_n_results (find, view->current_page);
1794 ev_document_doc_mutex_unlock ();
1796 if (n_results > view->find_result) {
1797 ev_document_doc_mutex_lock ();
1798 ev_document_find_get_result
1799 (find, view->current_page, view->find_result, &rect);
1800 ev_document_doc_mutex_unlock ();
1802 doc_rect_to_view_rect (view, &rect, &view_rect);
1803 ensure_rectangle_is_visible (view, &view_rect);
1808 jump_to_find_page (EvView *view)
1812 n_pages = ev_page_cache_get_n_pages (view->page_cache);
1814 for (i = 0; i < n_pages; i++) {
1818 page = i + view->find_page;
1819 if (page >= n_pages) {
1820 page = page - n_pages;
1823 // ev_document_doc_mutex_lock ();
1824 has_results = ev_document_find_page_has_results
1825 (EV_DOCUMENT_FIND (view->document), page);
1826 if (has_results == -1) {
1827 view->find_page = page;
1829 } else if (has_results == 1) {
1830 ev_page_cache_set_current_page (view->page_cache, page);
1831 jump_to_find_result (view);
1838 find_changed_cb (EvDocument *document, int page, EvView *view)
1840 jump_to_find_page (view);
1841 jump_to_find_result (view);
1842 update_find_status_message (view);
1844 if (view->current_page == page)
1845 gtk_widget_queue_draw (GTK_WIDGET (view));
1847 /*** Public API ***/
1854 view = g_object_new (EV_TYPE_VIEW, NULL);
1860 job_finished_cb (EvPixbufCache *pixbuf_cache,
1863 gtk_widget_queue_draw (GTK_WIDGET (view));
1868 page_changed_cb (EvPageCache *page_cache,
1872 int old_page = view->current_page;
1873 int old_width, old_height;
1874 int new_width, new_height;
1876 if (old_page == new_page)
1879 ev_page_cache_get_size (page_cache,
1882 &old_width, &old_height);
1884 view->current_page = new_page;
1885 view->has_selection = FALSE;
1887 ev_page_cache_get_size (page_cache,
1890 &new_width, &new_height);
1892 compute_border (view, new_width, new_height, &(view->border));
1894 if (new_width != old_width || new_height != old_height)
1895 gtk_widget_queue_resize (GTK_WIDGET (view));
1897 gtk_widget_queue_draw (GTK_WIDGET (view));
1899 gtk_adjustment_set_value (view->vadjustment,
1900 view->vadjustment->lower);
1902 if (EV_IS_DOCUMENT_FIND (view->document)) {
1903 view->find_page = new_page;
1904 view->find_result = 0;
1905 update_find_status_message (view);
1908 view_update_range_and_current_page (view);
1912 ev_view_set_document (EvView *view,
1913 EvDocument *document)
1915 g_return_if_fail (EV_IS_VIEW (view));
1917 if (document != view->document) {
1918 if (view->document) {
1919 g_signal_handlers_disconnect_by_func (view->document,
1922 g_object_unref (view->document);
1923 view->page_cache = NULL;
1927 view->document = document;
1928 view->find_page = 0;
1929 view->find_result = 0;
1931 if (view->document) {
1932 g_object_ref (view->document);
1933 if (EV_IS_DOCUMENT_FIND (view->document)) {
1934 g_signal_connect (view->document,
1936 G_CALLBACK (find_changed_cb),
1939 view->page_cache = ev_document_get_page_cache (view->document);
1940 g_signal_connect (view->page_cache, "page-changed", G_CALLBACK (page_changed_cb), view);
1941 view->pixbuf_cache = ev_pixbuf_cache_new (view->document);
1942 g_signal_connect (view->pixbuf_cache, "job-finished", G_CALLBACK (job_finished_cb), view);
1945 gtk_widget_queue_resize (GTK_WIDGET (view));
1949 #define EPSILON 0.0000001
1951 ev_view_zoom (EvView *view,
1958 scale = view->scale * factor;
1962 scale = CLAMP (scale, MIN_SCALE, MAX_SCALE);
1964 if (ABS (view->scale - scale) < EPSILON)
1966 view->scale = scale;
1967 gtk_widget_queue_resize (GTK_WIDGET (view));
1972 ev_view_set_continuous (EvView *view,
1973 gboolean continuous)
1975 g_return_if_fail (EV_IS_VIEW (view));
1977 continuous = continuous != FALSE;
1979 if (view->continuous != continuous) {
1980 view->continuous = continuous;
1981 gtk_widget_queue_resize (GTK_WIDGET (view));
1984 g_object_notify (G_OBJECT (view), "continuous");
1988 ev_view_set_dual_page (EvView *view,
1991 g_return_if_fail (EV_IS_VIEW (view));
1993 dual_page = dual_page != FALSE;
1995 if (view->dual_page == dual_page)
1998 view->dual_page = dual_page;
1999 /* FIXME: if we're keeping the pixbuf cache around, we should extend the
2000 * preload_cache_size to be 2 if dual_page is set.
2002 gtk_widget_queue_resize (GTK_WIDGET (view));
2004 g_object_notify (G_OBJECT (view), "dual-page");
2008 ev_view_set_full_screen (EvView *view,
2009 gboolean full_screen)
2011 g_return_if_fail (EV_IS_VIEW (view));
2013 full_screen = full_screen != FALSE;
2015 if (view->full_screen != full_screen) {
2016 view->full_screen = full_screen;
2017 gtk_widget_queue_resize (GTK_WIDGET (view));
2020 g_object_notify (G_OBJECT (view), "full-screen");
2024 ev_view_set_presentation (EvView *view,
2025 gboolean presentation)
2027 g_return_if_fail (EV_IS_VIEW (view));
2029 presentation = presentation != FALSE;
2031 if (view->presentation != presentation) {
2032 view->presentation = presentation;
2033 gtk_widget_queue_resize (GTK_WIDGET (view));
2036 g_object_notify (G_OBJECT (view), "presentation");
2040 ev_view_set_sizing_mode (EvView *view,
2041 EvSizingMode sizing_mode)
2043 g_return_if_fail (EV_IS_VIEW (view));
2045 if (view->sizing_mode != sizing_mode) {
2046 view->sizing_mode = sizing_mode;
2047 gtk_widget_queue_resize (GTK_WIDGET (view));
2050 g_object_notify (G_OBJECT (view), "sizing-mode");
2055 ev_view_can_zoom_in (EvView *view)
2057 return view->scale * ZOOM_IN_FACTOR <= MAX_SCALE;
2061 ev_view_can_zoom_out (EvView *view)
2063 return view->scale * ZOOM_OUT_FACTOR >= MIN_SCALE;
2067 ev_view_zoom_in (EvView *view)
2069 g_return_if_fail (view->sizing_mode == EV_SIZING_FREE);
2071 ev_view_zoom (view, ZOOM_IN_FACTOR, TRUE);
2075 ev_view_zoom_out (EvView *view)
2077 g_return_if_fail (view->sizing_mode == EV_SIZING_FREE);
2079 ev_view_zoom (view, ZOOM_OUT_FACTOR, TRUE);
2085 zoom_for_size_fit_width (int doc_width,
2093 scale = (double)target_width / doc_width;
2095 if (doc_height * scale > target_height)
2096 scale = (double) (target_width - vsb_width) / doc_width;
2102 zoom_for_size_best_fit (int doc_width,
2112 w_scale = (double)target_width / doc_width;
2113 h_scale = (double)target_height / doc_height;
2115 if (doc_height * w_scale > target_height)
2116 w_scale = (double) (target_width - vsb_width) / doc_width;
2117 if (doc_width * h_scale > target_width)
2118 h_scale = (double) (target_height - hsb_width) / doc_height;
2120 return MIN (w_scale, h_scale);
2124 ev_view_zoom_for_size_continuous_and_dual_page (EvView *view,
2130 int doc_width, doc_height;
2134 ev_page_cache_get_max_width_size (view->page_cache,
2137 ev_page_cache_get_max_height_size (view->page_cache,
2140 compute_border (view, doc_width, doc_height, &border);
2142 doc_width = doc_width * 2;
2143 width -= ((border.left + border.right)* 2 + 3 * view->spacing);
2144 height -= (border.top + border.bottom + 2 * view->spacing);
2146 /* FIXME: We really need to calculat the overall height here, not the
2147 * page height. We assume there's always a vertical scrollbar for
2148 * now. We need to fix this. */
2149 if (view->sizing_mode == EV_SIZING_FIT_WIDTH)
2150 scale = zoom_for_size_fit_width (doc_width, doc_height, width - vsb_width, height, 0);
2151 else if (view->sizing_mode == EV_SIZING_BEST_FIT)
2152 scale = zoom_for_size_best_fit (doc_width, doc_height, width - vsb_width, height, 0, hsb_height);
2154 g_assert_not_reached ();
2156 ev_view_zoom (view, scale, FALSE);
2160 ev_view_zoom_for_size_continuous (EvView *view,
2166 int doc_width, doc_height;
2170 ev_page_cache_get_max_width_size (view->page_cache,
2173 ev_page_cache_get_max_height_size (view->page_cache,
2176 compute_border (view, doc_width, doc_height, &border);
2178 width -= (border.left + border.right + 2 * view->spacing);
2179 height -= (border.top + border.bottom + 2 * view->spacing);
2181 if (view->sizing_mode == EV_SIZING_FIT_WIDTH)
2182 scale = zoom_for_size_fit_width (doc_width, doc_height, width, height, vsb_width);
2183 else if (view->sizing_mode == EV_SIZING_BEST_FIT)
2184 scale = zoom_for_size_best_fit (doc_width, doc_height, width, height, vsb_width, hsb_height);
2186 g_assert_not_reached ();
2188 ev_view_zoom (view, scale, FALSE);
2192 ev_view_zoom_for_size_dual_page (EvView *view,
2199 gint doc_width, doc_height;
2203 other_page = view->current_page ^ 1;
2205 /* Find the largest of the two. */
2206 ev_page_cache_get_size (view->page_cache,
2209 &doc_width, &doc_height);
2211 if (other_page < ev_page_cache_get_n_pages (view->page_cache)) {
2212 gint width_2, height_2;
2213 ev_page_cache_get_size (view->page_cache,
2216 &width_2, &height_2);
2217 if (width_2 > doc_width)
2218 doc_width = width_2;
2219 if (height_2 > doc_height)
2220 doc_height = height_2;
2222 compute_border (view, doc_width, doc_height, &border);
2224 doc_width = doc_width * 2;
2225 width -= ((border.left + border.right)* 2 + 3 * view->spacing);
2226 height -= (border.top + border.bottom + 2 * view->spacing);
2228 if (view->sizing_mode == EV_SIZING_FIT_WIDTH)
2229 scale = zoom_for_size_fit_width (doc_width, doc_height, width, height, vsb_width);
2230 else if (view->sizing_mode == EV_SIZING_BEST_FIT)
2231 scale = zoom_for_size_best_fit (doc_width, doc_height, width, height, vsb_width, hsb_height);
2233 g_assert_not_reached ();
2235 ev_view_zoom (view, scale, FALSE);
2239 ev_view_zoom_for_size_single_page (EvView *view,
2245 int doc_width, doc_height;
2249 ev_page_cache_get_size (view->page_cache,
2254 /* Get an approximate border */
2255 compute_border (view, width, height, &border);
2257 width -= (border.left + border.right + 2 * view->spacing);
2258 height -= (border.top + border.bottom + 2 * view->spacing);
2260 if (view->sizing_mode == EV_SIZING_FIT_WIDTH)
2261 scale = zoom_for_size_fit_width (doc_width, doc_height, width, height, vsb_width);
2262 else if (view->sizing_mode == EV_SIZING_BEST_FIT)
2263 scale = zoom_for_size_best_fit (doc_width, doc_height, width, height, vsb_width, hsb_height);
2265 g_assert_not_reached ();
2267 ev_view_zoom (view, scale, FALSE);
2272 ev_view_zoom_normal (EvView *view)
2274 ev_view_zoom (view, 1.0, FALSE);
2278 ev_view_set_zoom_for_size (EvView *view,
2284 g_return_if_fail (view->sizing_mode == EV_SIZING_FIT_WIDTH ||
2285 view->sizing_mode == EV_SIZING_BEST_FIT);
2286 g_return_if_fail (width >= 0);
2287 g_return_if_fail (height >= 0);
2289 if (view->document == NULL)
2292 if (view->continuous && view->dual_page)
2293 ev_view_zoom_for_size_continuous_and_dual_page (view, width, height, vsb_width, hsb_height);
2294 else if (view->continuous)
2295 ev_view_zoom_for_size_continuous (view, width, height, vsb_width, hsb_height);
2296 else if (view->dual_page)
2297 ev_view_zoom_for_size_dual_page (view, width, height, vsb_width, hsb_height);
2299 ev_view_zoom_for_size_single_page (view, width, height, vsb_width, hsb_height);
2303 ev_view_get_status (EvView *view)
2305 g_return_val_if_fail (EV_IS_VIEW (view), NULL);
2307 return view->status;
2311 ev_view_get_find_status (EvView *view)
2313 g_return_val_if_fail (EV_IS_VIEW (view), NULL);
2315 return view->find_status;
2319 ev_view_can_find_next (EvView *view)
2323 if (EV_IS_DOCUMENT_FIND (view->document)) {
2324 EvDocumentFind *find = EV_DOCUMENT_FIND (view->document);
2326 ev_document_doc_mutex_lock ();
2327 n_results = ev_document_find_get_n_results (find, view->current_page);
2328 ev_document_doc_mutex_unlock ();
2331 return n_results > 0;
2335 ev_view_find_next (EvView *view)
2337 EvPageCache *page_cache;
2338 int n_results, n_pages;
2339 EvDocumentFind *find = EV_DOCUMENT_FIND (view->document);
2341 page_cache = ev_document_get_page_cache (view->document);
2342 ev_document_doc_mutex_lock ();
2343 n_results = ev_document_find_get_n_results (find, view->current_page);
2344 ev_document_doc_mutex_unlock ();
2346 n_pages = ev_page_cache_get_n_pages (page_cache);
2348 view->find_result++;
2350 if (view->find_result >= n_results) {
2351 view->find_result = 0;
2354 if (view->find_page >= n_pages) {
2355 view->find_page = 0;
2358 jump_to_find_page (view);
2360 jump_to_find_result (view);
2361 gtk_widget_queue_draw (GTK_WIDGET (view));
2366 ev_view_find_previous (EvView *view)
2368 int n_results, n_pages;
2369 EvDocumentFind *find = EV_DOCUMENT_FIND (view->document);
2370 EvPageCache *page_cache;
2372 page_cache = ev_document_get_page_cache (view->document);
2374 ev_document_doc_mutex_lock ();
2375 n_results = ev_document_find_get_n_results (find, view->current_page);
2376 ev_document_doc_mutex_unlock ();
2378 n_pages = ev_page_cache_get_n_pages (page_cache);
2380 view->find_result--;
2382 if (view->find_result < 0) {
2383 view->find_result = 0;
2386 if (view->find_page < 0) {
2387 view->find_page = n_pages - 1;
2390 jump_to_find_page (view);
2392 jump_to_find_result (view);
2393 gtk_widget_queue_draw (GTK_WIDGET (view));
2397 ev_view_hide_cursor (EvView *view)
2399 ev_view_set_cursor (view, EV_VIEW_CURSOR_HIDDEN);
2403 ev_view_show_cursor (EvView *view)
2405 ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL);
2410 ev_sizing_mode_get_type (void)
2412 static GType etype = 0;
2414 static const GEnumValue values[] = {
2415 { EV_SIZING_FIT_WIDTH, "EV_SIZING_FIT_WIDTH", "fit-width" },
2416 { EV_SIZING_BEST_FIT, "EV_SIZING_BEST_FIT", "best-fit" },
2417 { EV_SIZING_FREE, "EV_SIZING_FREE", "free" },
2420 etype = g_enum_register_static ("EvSizingMode", values);