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.
21 #include <gtk/gtkalignment.h>
22 #include <glib/gi18n.h>
23 #include <gtk/gtkbindings.h>
24 #include <gtk/gtkselection.h>
25 #include <gtk/gtkclipboard.h>
26 #include <gdk/gdkkeysyms.h>
27 #include <libgnomevfs/gnome-vfs-utils.h>
29 #include "ev-marshal.h"
31 #include "ev-document-find.h"
34 #define EV_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EV_TYPE_VIEW, EvViewClass))
35 #define EV_IS_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), EV_TYPE_VIEW))
36 #define EV_VIEW_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), EV_TYPE_VIEW, EvViewClass))
49 TARGET_TEXT_BUFFER_CONTENTS
52 static const GtkTargetEntry targets[] = {
53 { "STRING", 0, TARGET_STRING },
54 { "TEXT", 0, TARGET_TEXT },
55 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT },
56 { "UTF8_STRING", 0, TARGET_UTF8_STRING },
60 EV_VIEW_CURSOR_NORMAL,
66 GtkWidget parent_instance;
70 GdkWindow *bin_window;
78 gboolean pressed_button;
79 gboolean has_selection;
80 GdkPoint selection_start;
81 GdkRectangle selection;
84 GtkAdjustment *hadjustment;
85 GtkAdjustment *vadjustment;
94 GtkWidgetClass parent_class;
96 void (*set_scroll_adjustments) (EvView *view,
97 GtkAdjustment *hadjustment,
98 GtkAdjustment *vadjustment);
99 void (*scroll_view) (EvView *view,
100 GtkScrollType scroll,
101 gboolean horizontal);
103 /* Should this be notify::page? */
104 void (*page_changed) (EvView *view);
107 static guint page_changed_signal = 0;
109 static void ev_view_set_scroll_adjustments (EvView *view,
110 GtkAdjustment *hadjustment,
111 GtkAdjustment *vadjustment);
113 G_DEFINE_TYPE (EvView, ev_view, GTK_TYPE_WIDGET)
115 /*** Helper functions ***/
118 view_update_adjustments (EvView *view)
120 int old_x = view->scroll_x;
121 int old_y = view->scroll_y;
123 if (view->hadjustment)
124 view->scroll_x = view->hadjustment->value;
128 if (view->vadjustment)
129 view->scroll_y = view->vadjustment->value;
133 if (GTK_WIDGET_REALIZED (view) &&
134 (view->scroll_x != old_x || view->scroll_y != old_y)) {
135 gdk_window_move (view->bin_window, - view->scroll_x, - view->scroll_y);
136 gdk_window_process_updates (view->bin_window, TRUE);
141 view_set_adjustment_values (EvView *view,
142 GtkOrientation orientation)
144 GtkWidget *widget = GTK_WIDGET (view);
145 GtkAdjustment *adjustment;
146 gboolean value_changed = FALSE;
150 if (orientation == GTK_ORIENTATION_HORIZONTAL) {
151 requisition = widget->requisition.width;
152 allocation = widget->allocation.width;
153 adjustment = view->hadjustment;
155 requisition = widget->requisition.height;
156 allocation = widget->allocation.height;
157 adjustment = view->vadjustment;
163 adjustment->page_size = allocation;
164 adjustment->step_increment = allocation * 0.1;
165 adjustment->page_increment = allocation * 0.9;
166 adjustment->lower = 0;
167 adjustment->upper = MAX (allocation, requisition);
169 if (adjustment->value > adjustment->upper - adjustment->page_size) {
170 adjustment->value = adjustment->upper - adjustment->page_size;
171 value_changed = TRUE;
174 gtk_adjustment_changed (adjustment);
176 gtk_adjustment_value_changed (adjustment);
179 /*** Virtual function implementations ***/
182 ev_view_finalize (GObject *object)
184 EvView *view = EV_VIEW (object);
189 g_object_unref (view->document);
191 ev_view_set_scroll_adjustments (view, NULL, NULL);
193 G_OBJECT_CLASS (ev_view_parent_class)->finalize (object);
197 ev_view_destroy (GtkObject *object)
199 EvView *view = EV_VIEW (object);
201 ev_view_set_scroll_adjustments (view, NULL, NULL);
203 GTK_OBJECT_CLASS (ev_view_parent_class)->destroy (object);
207 ev_view_size_request (GtkWidget *widget,
208 GtkRequisition *requisition)
210 EvView *view = EV_VIEW (widget);
212 if (GTK_WIDGET_REALIZED (widget)) {
213 if (view->document) {
214 ev_document_get_page_size (view->document,
217 &requisition->height);
219 requisition->width = 10;
220 requisition->height = 10;
223 requisition->width += 2;
224 requisition->height += 2;
230 ev_view_size_allocate (GtkWidget *widget,
231 GtkAllocation *allocation)
233 EvView *view = EV_VIEW (widget);
235 GTK_WIDGET_CLASS (ev_view_parent_class)->size_allocate (widget, allocation);
237 view_set_adjustment_values (view, GTK_ORIENTATION_HORIZONTAL);
238 view_set_adjustment_values (view, GTK_ORIENTATION_VERTICAL);
240 if (GTK_WIDGET_REALIZED (widget)) {
241 gdk_window_resize (view->bin_window,
242 MAX (widget->allocation.width, widget->requisition.width),
243 MAX (widget->allocation.height, widget->requisition.height));
248 ev_view_realize (GtkWidget *widget)
250 EvView *view = EV_VIEW (widget);
251 GdkWindowAttr attributes;
253 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
255 attributes.window_type = GDK_WINDOW_CHILD;
256 attributes.wclass = GDK_INPUT_OUTPUT;
257 attributes.visual = gtk_widget_get_visual (widget);
258 attributes.colormap = gtk_widget_get_colormap (widget);
260 attributes.x = widget->allocation.x;
261 attributes.y = widget->allocation.y;
262 attributes.width = widget->allocation.width;
263 attributes.height = widget->allocation.height;
264 attributes.event_mask = 0;
266 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
268 GDK_WA_X | GDK_WA_Y |
271 gdk_window_set_user_data (widget->window, widget);
272 widget->style = gtk_style_attach (widget->style, widget->window);
276 attributes.width = MAX (widget->allocation.width, widget->requisition.width);
277 attributes.height = MAX (widget->allocation.height, widget->requisition.height);
278 attributes.event_mask = GDK_EXPOSURE_MASK |
279 GDK_BUTTON_PRESS_MASK |
280 GDK_BUTTON_RELEASE_MASK |
283 GDK_POINTER_MOTION_MASK;
285 view->bin_window = gdk_window_new (widget->window,
287 GDK_WA_X | GDK_WA_Y |
290 gdk_window_set_user_data (view->bin_window, widget);
291 gdk_window_show (view->bin_window);
293 widget->style = gtk_style_attach (widget->style, view->bin_window);
294 gdk_window_set_background (view->bin_window, &widget->style->mid[widget->state]);
296 if (view->document) {
297 ev_document_set_target (view->document, view->bin_window);
299 /* We can't get page size without a target, so we have to
300 * queue a size request at realization. Could be fixed
301 * with EvDocument changes to allow setting a GdkScreen
302 * without setting a target.
304 gtk_widget_queue_resize (widget);
309 ev_view_unrealize (GtkWidget *widget)
311 EvView *view = EV_VIEW (widget);
314 ev_document_set_target (view->document, NULL);
316 gdk_window_set_user_data (view->bin_window, NULL);
317 gdk_window_destroy (view->bin_window);
318 view->bin_window = NULL;
320 GTK_WIDGET_CLASS (ev_view_parent_class)->unrealize (widget);
324 ev_gdk_color_to_rgb (const GdkColor *color)
327 result = (0xff0000 | (color->red & 0xff00));
329 result |= ((color->green & 0xff00) | (color->blue >> 8));
334 draw_rubberband (GtkWidget *widget, GdkWindow *window,
335 const GdkRectangle *rect, guchar alpha)
339 GdkColor *fill_color_gdk;
342 fill_color_gdk = gdk_color_copy (>K_WIDGET (widget)->style->base[GTK_STATE_SELECTED]);
343 fill_color = ev_gdk_color_to_rgb (fill_color_gdk) << 8 | alpha;
345 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
346 rect->width, rect->height);
347 gdk_pixbuf_fill (pixbuf, fill_color);
349 gdk_draw_pixbuf (window, NULL, pixbuf,
352 rect->width, rect->height,
356 g_object_unref (pixbuf);
358 gc = gdk_gc_new (window);
359 gdk_gc_set_rgb_fg_color (gc, fill_color_gdk);
360 gdk_draw_rectangle (window, gc, FALSE,
366 gdk_color_free (fill_color_gdk);
370 highlight_find_results (EvView *view)
372 EvDocumentFind *find = EV_DOCUMENT_FIND (view->document);
375 results = ev_document_find_get_n_results (find);
377 for (i = 0; i < results; i++) {
378 GdkRectangle rectangle;
381 alpha = (i == view->find_result) ? 0x90 : 0x20;
382 ev_document_find_get_result (find, i, &rectangle);
383 draw_rubberband (GTK_WIDGET (view), view->bin_window,
389 expose_bin_window (GtkWidget *widget,
390 GdkEventExpose *event)
392 EvView *view = EV_VIEW (widget);
393 int x_offset, y_offset;
395 if (view->document == NULL)
398 x_offset = MAX (0, (widget->allocation.width -
399 widget->requisition.width) / 2);
400 y_offset = MAX (0, (widget->allocation.height -
401 widget->requisition.height) / 2);
402 gdk_draw_rectangle (view->bin_window,
403 widget->style->black_gc,
407 widget->requisition.width - 1,
408 widget->requisition.height - 1);
410 ev_document_set_page_offset (view->document,
414 LOG ("Render area %d %d %d %d", event->area.x, event->area.y,
415 event->area.width, event->area.height);
417 ev_document_render (view->document,
418 event->area.x, event->area.y,
419 event->area.width, event->area.height);
421 if (EV_IS_DOCUMENT_FIND (view->document)) {
422 highlight_find_results (view);
425 if (view->has_selection) {
426 draw_rubberband (widget, view->bin_window,
427 &view->selection, 0x40);
432 ev_view_expose_event (GtkWidget *widget,
433 GdkEventExpose *event)
435 EvView *view = EV_VIEW (widget);
437 if (event->window == view->bin_window)
438 expose_bin_window (widget, event);
440 return GTK_WIDGET_CLASS (ev_view_parent_class)->expose_event (widget, event);
446 ev_view_select_all (EvView *ev_view)
448 GtkWidget *widget = GTK_WIDGET (ev_view);
450 g_return_if_fail (EV_IS_VIEW (ev_view));
452 ev_view->has_selection = TRUE;
453 ev_view->selection.x = ev_view->selection.y = 0;
454 ev_view->selection.width = widget->requisition.width;
455 ev_view->selection.height = widget->requisition.height;
457 gtk_widget_queue_draw (widget);
461 ev_view_copy (EvView *ev_view)
463 GtkClipboard *clipboard;
466 text = ev_document_get_text (ev_view->document, &ev_view->selection);
467 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (ev_view),
468 GDK_SELECTION_CLIPBOARD);
469 gtk_clipboard_set_text (clipboard, text, -1);
474 ev_view_primary_get_cb (GtkClipboard *clipboard,
475 GtkSelectionData *selection_data,
479 EvView *ev_view = EV_VIEW (data);
482 text = ev_document_get_text (ev_view->document, &ev_view->selection);
483 gtk_selection_data_set_text (selection_data, text, -1);
487 ev_view_primary_clear_cb (GtkClipboard *clipboard,
490 EvView *ev_view = EV_VIEW (data);
492 ev_view->has_selection = FALSE;
496 ev_view_update_primary_selection (EvView *ev_view)
498 GtkClipboard *clipboard;
500 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (ev_view),
501 GDK_SELECTION_PRIMARY);
503 if (ev_view->has_selection) {
504 if (!gtk_clipboard_set_with_owner (clipboard,
506 G_N_ELEMENTS (targets),
507 ev_view_primary_get_cb,
508 ev_view_primary_clear_cb,
510 ev_view_primary_clear_cb (clipboard, ev_view);
512 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (ev_view))
513 gtk_clipboard_clear (clipboard);
518 ev_view_button_press_event (GtkWidget *widget,
519 GdkEventButton *event)
521 EvView *view = EV_VIEW (widget);
523 if (!GTK_WIDGET_HAS_FOCUS (widget)) {
524 gtk_widget_grab_focus (widget);
527 view->pressed_button = event->button;
529 switch (event->button) {
531 if (view->has_selection) {
532 view->has_selection = FALSE;
533 gtk_widget_queue_draw (widget);
536 view->selection_start.x = event->x;
537 view->selection_start.y = event->y;
545 status_message_from_link (EvLink *link)
551 type = ev_link_get_link_type (link);
554 case EV_LINK_TYPE_TITLE:
555 msg = g_strdup (ev_link_get_title (link));
557 case EV_LINK_TYPE_PAGE:
558 page = ev_link_get_page (link);
559 msg = g_strdup_printf (_("Go to page %d"), page);
561 case EV_LINK_TYPE_EXTERNAL_URI:
562 msg = g_strdup (ev_link_get_uri (link));
572 ev_view_set_status (EvView *view, const char *message)
574 g_return_if_fail (EV_IS_VIEW (view));
576 if (message != view->status) {
577 g_free (view->status);
578 view->status = g_strdup (message);
579 g_object_notify (G_OBJECT (view), "status");
584 ev_view_set_find_status (EvView *view, const char *message)
586 g_return_if_fail (EV_IS_VIEW (view));
588 g_free (view->find_status);
589 view->find_status = g_strdup (message);
590 g_object_notify (G_OBJECT (view), "find-status");
595 ev_view_set_cursor (EvView *view, EvViewCursor new_cursor)
597 GdkCursor *cursor = NULL;
601 if (view->cursor == new_cursor) {
605 widget = gtk_widget_get_toplevel (GTK_WIDGET (view));
606 display = gtk_widget_get_display (widget);
607 view->cursor = new_cursor;
609 switch (new_cursor) {
610 case EV_VIEW_CURSOR_NORMAL:
611 gdk_window_set_cursor (widget->window, NULL);
613 case EV_VIEW_CURSOR_LINK:
614 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
616 case EV_VIEW_CURSOR_WAIT:
617 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
622 gdk_window_set_cursor (widget->window, cursor);
623 gdk_cursor_unref (cursor);
629 ev_view_motion_notify_event (GtkWidget *widget,
630 GdkEventMotion *event)
632 EvView *view = EV_VIEW (widget);
634 if (view->pressed_button > 0) {
635 view->has_selection = TRUE;
636 view->selection.x = MIN (view->selection_start.x, event->x);
637 view->selection.y = MIN (view->selection_start.y, event->y);
638 view->selection.width = ABS (view->selection_start.x - event->x) + 1;
639 view->selection.height = ABS (view->selection_start.y - event->y) + 1;
641 gtk_widget_queue_draw (widget);
642 } else if (view->document) {
645 link = ev_document_get_link (view->document, event->x, event->y);
649 msg = status_message_from_link (link);
650 ev_view_set_status (view, msg);
651 ev_view_set_cursor (view, EV_VIEW_CURSOR_LINK);
654 g_object_unref (link);
656 ev_view_set_status (view, NULL);
657 if (view->cursor == EV_VIEW_CURSOR_LINK) {
658 ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL);
667 ev_view_button_release_event (GtkWidget *widget,
668 GdkEventButton *event)
670 EvView *view = EV_VIEW (widget);
672 view->pressed_button = -1;
674 if (view->has_selection) {
675 ev_view_update_primary_selection (view);
676 } else if (view->document) {
679 link = ev_document_get_link (view->document,
683 ev_view_go_to_link (view, link);
684 g_object_unref (link);
692 on_adjustment_value_changed (GtkAdjustment *adjustment,
695 view_update_adjustments (view);
699 set_scroll_adjustment (EvView *view,
700 GtkOrientation orientation,
701 GtkAdjustment *adjustment)
703 GtkAdjustment **to_set;
705 if (orientation == GTK_ORIENTATION_HORIZONTAL)
706 to_set = &view->hadjustment;
708 to_set = &view->vadjustment;
710 if (*to_set != adjustment) {
712 g_signal_handlers_disconnect_by_func (*to_set,
713 (gpointer) on_adjustment_value_changed,
715 g_object_unref (*to_set);
718 *to_set = adjustment;
719 view_set_adjustment_values (view, orientation);
722 g_object_ref (*to_set);
723 g_signal_connect (*to_set, "value_changed",
724 G_CALLBACK (on_adjustment_value_changed), view);
730 ev_view_set_scroll_adjustments (EvView *view,
731 GtkAdjustment *hadjustment,
732 GtkAdjustment *vadjustment)
734 set_scroll_adjustment (view, GTK_ORIENTATION_HORIZONTAL, hadjustment);
735 set_scroll_adjustment (view, GTK_ORIENTATION_VERTICAL, vadjustment);
737 view_update_adjustments (view);
741 add_scroll_binding (GtkBindingSet *binding_set,
743 GtkScrollType scroll,
746 guint keypad_keyval = keyval - GDK_Left + GDK_KP_Left;
748 gtk_binding_entry_add_signal (binding_set, keyval, 0,
750 GTK_TYPE_SCROLL_TYPE, scroll,
751 G_TYPE_BOOLEAN, horizontal);
752 gtk_binding_entry_add_signal (binding_set, keypad_keyval, 0,
754 GTK_TYPE_SCROLL_TYPE, scroll,
755 G_TYPE_BOOLEAN, horizontal);
759 ev_view_scroll_view (EvView *view,
760 GtkScrollType scroll,
763 if (scroll == GTK_SCROLL_PAGE_BACKWARD) {
764 ev_view_set_page (view, ev_view_get_page (view) - 1);
765 } else if (scroll == GTK_SCROLL_PAGE_FORWARD) {
766 ev_view_set_page (view, ev_view_get_page (view) + 1);
768 GtkAdjustment *adjustment;
772 adjustment = view->hadjustment;
774 adjustment = view->vadjustment;
777 value = adjustment->value;
780 case GTK_SCROLL_STEP_BACKWARD:
781 value -= adjustment->step_increment;
783 case GTK_SCROLL_STEP_FORWARD:
784 value += adjustment->step_increment;
790 value = CLAMP (value, adjustment->lower,
791 adjustment->upper - adjustment->page_size);
793 gtk_adjustment_set_value (adjustment, value);
798 ev_view_set_property (GObject *object,
807 case PROP_FIND_STATUS:
813 ev_view_get_property (GObject *object,
818 EvView *view = EV_VIEW (object);
823 g_value_set_string (value, view->status);
825 case PROP_FIND_STATUS:
826 g_value_set_string (value, view->status);
832 ev_view_class_init (EvViewClass *class)
834 GObjectClass *object_class = G_OBJECT_CLASS (class);
835 GtkObjectClass *gtk_object_class = GTK_OBJECT_CLASS (class);
836 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
837 GtkBindingSet *binding_set;
839 object_class->finalize = ev_view_finalize;
840 object_class->set_property = ev_view_set_property;
841 object_class->get_property = ev_view_get_property;
843 widget_class->expose_event = ev_view_expose_event;
844 widget_class->button_press_event = ev_view_button_press_event;
845 widget_class->motion_notify_event = ev_view_motion_notify_event;
846 widget_class->button_release_event = ev_view_button_release_event;
847 widget_class->size_request = ev_view_size_request;
848 widget_class->size_allocate = ev_view_size_allocate;
849 widget_class->realize = ev_view_realize;
850 widget_class->unrealize = ev_view_unrealize;
851 gtk_object_class->destroy = ev_view_destroy;
853 class->set_scroll_adjustments = ev_view_set_scroll_adjustments;
854 class->scroll_view = ev_view_scroll_view;
856 widget_class->set_scroll_adjustments_signal = g_signal_new ("set-scroll-adjustments",
857 G_OBJECT_CLASS_TYPE (object_class),
858 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
859 G_STRUCT_OFFSET (EvViewClass, set_scroll_adjustments),
861 ev_marshal_VOID__OBJECT_OBJECT,
864 GTK_TYPE_ADJUSTMENT);
865 page_changed_signal = g_signal_new ("page-changed",
866 G_OBJECT_CLASS_TYPE (object_class),
867 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
868 G_STRUCT_OFFSET (EvViewClass, page_changed),
870 ev_marshal_VOID__NONE,
873 g_signal_new ("scroll_view",
874 G_TYPE_FROM_CLASS (object_class),
875 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
876 G_STRUCT_OFFSET (EvViewClass, scroll_view),
878 ev_marshal_VOID__ENUM_BOOLEAN,
880 GTK_TYPE_SCROLL_TYPE,
883 g_object_class_install_property (object_class,
885 g_param_spec_string ("status",
887 "The status message",
891 g_object_class_install_property (object_class,
893 g_param_spec_string ("find-status",
894 "Find Status Message",
895 "The find status message",
899 binding_set = gtk_binding_set_by_class (class);
901 add_scroll_binding (binding_set, GDK_Left, GTK_SCROLL_STEP_BACKWARD, TRUE);
902 add_scroll_binding (binding_set, GDK_Right, GTK_SCROLL_STEP_FORWARD, TRUE);
903 add_scroll_binding (binding_set, GDK_Up, GTK_SCROLL_STEP_BACKWARD, FALSE);
904 add_scroll_binding (binding_set, GDK_Down, GTK_SCROLL_STEP_FORWARD, FALSE);
906 add_scroll_binding (binding_set, GDK_Page_Up, GTK_SCROLL_PAGE_BACKWARD, FALSE);
907 add_scroll_binding (binding_set, GDK_Page_Down, GTK_SCROLL_PAGE_FORWARD, FALSE);
911 ev_view_init (EvView *view)
913 GTK_WIDGET_SET_FLAGS (view, GTK_CAN_FOCUS);
916 view->pressed_button = -1;
917 view->cursor = EV_VIEW_CURSOR_NORMAL;
921 update_find_status_message (EvView *view)
925 if (ev_document_get_page (view->document) == view->find_page) {
928 results = ev_document_find_get_n_results
929 (EV_DOCUMENT_FIND (view->document));
931 message = g_strdup_printf (_("%d found on this page"),
936 percent = ev_document_find_get_progress
937 (EV_DOCUMENT_FIND (view->document));
939 if (percent >= (1.0 - 1e-10)) {
940 message = g_strdup (_("Not found"));
942 message = g_strdup_printf (_("%3d%% remaining to search"),
943 (int) ((1.0 - percent) * 100));
948 ev_view_set_find_status (view, message);
953 set_document_page (EvView *view, int new_page)
958 pages = ev_document_get_n_pages (view->document);
959 page = CLAMP (new_page, 1, pages);
961 if (view->document) {
962 int old_page = ev_document_get_page (view->document);
963 int old_width, old_height;
965 ev_document_get_page_size (view->document,
967 &old_width, &old_height);
969 if (old_page != page) {
970 ev_view_set_cursor (view, EV_VIEW_CURSOR_WAIT);
971 ev_document_set_page (view->document, page);
974 if (old_page != ev_document_get_page (view->document)) {
977 g_signal_emit (view, page_changed_signal, 0);
979 view->has_selection = FALSE;
980 ev_document_get_page_size (view->document,
983 if (width != old_width || height != old_height)
984 gtk_widget_queue_resize (GTK_WIDGET (view));
986 gtk_adjustment_set_value (view->vadjustment,
987 view->vadjustment->lower);
990 view->find_page = page;
991 view->find_result = 0;
993 if (EV_IS_DOCUMENT_FIND (view->document)) {
994 update_find_status_message (view);
1002 ensure_rectangle_is_visible (EvView *view, GdkRectangle *rect)
1004 GtkWidget *widget = GTK_WIDGET (view);
1005 GtkAdjustment *adjustment;
1008 adjustment = view->vadjustment;
1010 if (rect->y < adjustment->value) {
1011 value = MAX (adjustment->lower, rect->y - MARGIN);
1012 gtk_adjustment_set_value (view->vadjustment, value);
1013 } else if (rect->y + rect->height >
1014 adjustment->value + widget->allocation.height) {
1015 value = MIN (adjustment->upper, rect->y + rect->height -
1016 widget->allocation.height + MARGIN);
1017 gtk_adjustment_set_value (view->vadjustment, value);
1020 adjustment = view->hadjustment;
1022 if (rect->x < adjustment->value) {
1023 value = MAX (adjustment->lower, rect->x - MARGIN);
1024 gtk_adjustment_set_value (view->hadjustment, value);
1025 } else if (rect->x + rect->height >
1026 adjustment->value + widget->allocation.width) {
1027 value = MIN (adjustment->upper, rect->x + rect->width -
1028 widget->allocation.width + MARGIN);
1029 gtk_adjustment_set_value (view->hadjustment, value);
1034 jump_to_find_result (EvView *view)
1036 EvDocumentFind *find = EV_DOCUMENT_FIND (view->document);
1040 n_results = ev_document_find_get_n_results (find);
1042 if (n_results > view->find_result) {
1043 ev_document_find_get_result
1044 (find, view->find_result, &rect);
1045 ensure_rectangle_is_visible (view, &rect);
1050 jump_to_find_page (EvView *view)
1054 n_pages = ev_document_get_n_pages (view->document);
1056 for (i = 0; i <= n_pages; i++) {
1060 page = i + view->find_page;
1061 if (page > n_pages) {
1062 page = page - n_pages;
1065 has_results = ev_document_find_page_has_results
1066 (EV_DOCUMENT_FIND (view->document), page);
1067 if (has_results == -1) {
1068 view->find_page = page;
1070 } else if (has_results == 1) {
1071 set_document_page (view, page);
1072 jump_to_find_result (view);
1079 find_changed_cb (EvDocument *document, int page, EvView *view)
1081 jump_to_find_page (view);
1082 jump_to_find_result (view);
1083 update_find_status_message (view);
1085 if (ev_document_get_page (document) == page) {
1086 gtk_widget_queue_draw (GTK_WIDGET (view));
1091 document_changed_callback (EvDocument *document,
1094 gtk_widget_queue_draw (GTK_WIDGET (view));
1095 ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL);
1098 /*** Public API ***/
1103 return g_object_new (EV_TYPE_VIEW, NULL);
1107 ev_view_set_document (EvView *view,
1108 EvDocument *document)
1110 g_return_if_fail (EV_IS_VIEW (view));
1112 if (document != view->document) {
1113 if (view->document) {
1114 g_signal_handlers_disconnect_by_func
1115 (view->document, find_changed_cb, view);
1116 g_object_unref (view->document);
1119 view->document = document;
1120 view->find_page = 1;
1121 view->find_result = 0;
1123 if (view->document) {
1124 g_object_ref (view->document);
1125 if (EV_IS_DOCUMENT_FIND (view->document)) {
1126 g_signal_connect (view->document,
1128 G_CALLBACK (find_changed_cb),
1131 g_signal_connect (view->document,
1133 G_CALLBACK (document_changed_callback),
1137 if (GTK_WIDGET_REALIZED (view))
1138 ev_document_set_target (view->document, view->bin_window);
1140 gtk_widget_queue_resize (GTK_WIDGET (view));
1142 g_signal_emit (view, page_changed_signal, 0);
1147 go_to_link (EvView *view, EvLink *link)
1153 type = ev_link_get_link_type (link);
1156 case EV_LINK_TYPE_TITLE:
1158 case EV_LINK_TYPE_PAGE:
1159 page = ev_link_get_page (link);
1160 set_document_page (view, page);
1162 case EV_LINK_TYPE_EXTERNAL_URI:
1163 uri = ev_link_get_uri (link);
1164 gnome_vfs_url_show (uri);
1170 ev_view_go_to_link (EvView *view, EvLink *link)
1172 go_to_link (view, link);
1176 ev_view_set_page (EvView *view,
1179 g_return_if_fail (EV_IS_VIEW (view));
1181 set_document_page (view, page);
1185 ev_view_get_page (EvView *view)
1188 return ev_document_get_page (view->document);
1193 #define ZOOM_IN_FACTOR 1.2
1194 #define ZOOM_OUT_FACTOR (1.0/ZOOM_IN_FACTOR)
1196 #define MIN_SCALE 0.05409
1197 #define MAX_SCALE 18.4884
1200 ev_view_zoom (EvView *view,
1207 scale = view->scale * factor;
1211 view->scale = CLAMP (scale, MIN_SCALE, MAX_SCALE);
1213 ev_document_set_scale (view->document, view->scale);
1215 gtk_widget_queue_resize (GTK_WIDGET (view));
1219 ev_view_zoom_in (EvView *view)
1221 ev_view_zoom (view, ZOOM_IN_FACTOR, TRUE);
1225 ev_view_zoom_out (EvView *view)
1227 ev_view_zoom (view, ZOOM_OUT_FACTOR, TRUE);
1231 ev_view_normal_size (EvView *view)
1233 ev_view_zoom (view, 1.0, FALSE);
1236 /* Unfortunately this is not idempotent (!) (numerical stability
1237 * issues because width and height are rounded)
1239 * One more reason to make this a toggle and not a command */
1241 ev_view_best_fit (EvView *view, int allocation_width, int allocation_height)
1244 int available_width, available_height;
1248 /* This is the bad part. You could make it stable by doing
1249 * ev_document_set_scale 1.0. But at least with pdf this means
1250 * redrawing the whole page */
1251 ev_document_get_page_size (view->document, -1, &width, &height);
1253 LOG ("Best fit %d %d", allocation_width, allocation_height);
1256 if (width != 0 && height != 0) {
1257 double scale_w, scale_h;
1259 available_width = MAX (1, allocation_width - 2); /* 1 px border left and right */
1260 available_height = MAX (1, allocation_height - 2); /* 1 px border above and below */
1262 scale_w = (double)available_width * view->scale / (width + 0.5);
1263 scale_h = (double)available_height * view->scale / (height + 0.5);
1265 scale = (scale_w < scale_h) ? scale_w : scale_h;
1268 ev_view_zoom (view, scale, FALSE);
1272 ev_view_fit_width (EvView *view, int allocation_width, int allocation_height,
1275 int available_width, available_height;
1280 ev_document_get_page_size (view->document, -1, &width, &height);
1284 available_width = MAX (1, allocation_width - 2); /* 1px border */
1285 available_height = MAX (1, allocation_height - 2); /* 1px border */
1287 scale = (double)available_width * view->scale / (width + 0.5);
1289 if ((height + 0.5) * scale / view->scale > available_height)
1290 scale = ((double)(available_width - vsb_width) * view->scale /
1294 ev_view_zoom (view, scale, FALSE);
1298 ev_view_get_status (EvView *view)
1300 g_return_val_if_fail (EV_IS_VIEW (view), NULL);
1302 return view->status;
1306 ev_view_get_find_status (EvView *view)
1308 g_return_val_if_fail (EV_IS_VIEW (view), NULL);
1310 return view->find_status;
1314 ev_view_find_next (EvView *view)
1316 int n_results, n_pages;
1317 EvDocumentFind *find = EV_DOCUMENT_FIND (view->document);
1319 n_results = ev_document_find_get_n_results (find);
1320 n_pages = ev_document_get_n_pages (view->document);
1322 view->find_result++;
1324 if (view->find_result >= n_results) {
1325 view->find_result = 0;
1328 if (view->find_page > n_pages) {
1329 view->find_page = 1;
1332 jump_to_find_page (view);
1334 jump_to_find_result (view);
1335 gtk_widget_queue_draw (GTK_WIDGET (view));
1340 ev_view_find_previous (EvView *view)
1342 int n_results, n_pages;
1343 EvDocumentFind *find = EV_DOCUMENT_FIND (view->document);
1345 n_results = ev_document_find_get_n_results (find);
1346 n_pages = ev_document_get_n_pages (view->document);
1348 view->find_result--;
1350 if (view->find_result < 0) {
1351 view->find_result = 0;
1354 if (view->find_page < 1) {
1355 view->find_page = n_pages;
1358 jump_to_find_page (view);
1360 jump_to_find_result (view);
1361 gtk_widget_queue_draw (GTK_WIDGET (view));