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"
33 #include "ev-selection.h"
34 #include "ev-document-find.h"
35 #include "ev-document-misc.h"
37 #include "ev-job-queue.h"
38 #include "ev-page-cache.h"
39 #include "ev-pixbuf-cache.h"
41 #define EV_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EV_TYPE_VIEW, EvViewClass))
42 #define EV_IS_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), EV_TYPE_VIEW))
43 #define EV_VIEW_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), EV_TYPE_VIEW, EvViewClass))
58 SIGNAL_BINDING_ACTIVATED,
68 TARGET_TEXT_BUFFER_CONTENTS
71 static const GtkTargetEntry targets[] = {
72 { "STRING", 0, TARGET_STRING },
73 { "TEXT", 0, TARGET_TEXT },
74 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT },
75 { "UTF8_STRING", 0, TARGET_UTF8_STRING },
78 static guint signals[N_SIGNALS];
81 EV_VIEW_CURSOR_NORMAL,
85 EV_VIEW_CURSOR_HIDDEN,
89 #define ZOOM_IN_FACTOR 1.2
90 #define ZOOM_OUT_FACTOR (1.0/ZOOM_IN_FACTOR)
92 #define MIN_SCALE 0.05409
95 /* Information for middle clicking and moving around the doc */
103 /* Information for handling selection */
105 gboolean in_selection;
111 SCROLL_TO_KEEP_POSITION,
112 SCROLL_TO_CURRENT_PAGE,
117 GtkWidget parent_instance;
119 EvDocument *document;
125 GtkAdjustment *hadjustment;
126 GtkAdjustment *vadjustment;
131 /* Information for middle clicking and dragging around. */
137 guint selection_update_id;
139 EvViewSelectionMode selection_mode;
140 SelectionInfo selection_info;
145 EvPageCache *page_cache;
146 EvPixbufCache *pixbuf_cache;
152 EvJobRender *current_job;
164 gboolean presentation;
165 EvSizingMode sizing_mode;
167 PendingScroll pending_scroll;
168 gboolean pending_resize;
171 struct _EvViewClass {
172 GtkWidgetClass parent_class;
174 void (*set_scroll_adjustments) (EvView *view,
175 GtkAdjustment *hadjustment,
176 GtkAdjustment *vadjustment);
177 void (*binding_activated) (EvView *view,
178 GtkScrollType scroll,
179 gboolean horizontal);
180 void (*zoom_invalid) (EvView *view);
184 static void scroll_to_current_page (EvView *view,
185 GtkOrientation orientation);
186 static void ev_view_set_scroll_adjustments (EvView *view,
187 GtkAdjustment *hadjustment,
188 GtkAdjustment *vadjustment);
189 static void view_update_range_and_current_page (EvView *view);
190 static void set_scroll_adjustment (EvView *view,
191 GtkOrientation orientation,
192 GtkAdjustment *adjustment);
193 static void ev_view_set_scroll_adjustments (EvView *view,
194 GtkAdjustment *hadjustment,
195 GtkAdjustment *vadjustment);
196 static void add_scroll_binding_keypad (GtkBindingSet *binding_set,
198 GtkScrollType scroll,
199 gboolean horizontal);
200 static void ev_view_binding_activated (EvView *view,
201 GtkScrollType scroll,
202 gboolean horizontal);
203 static void ensure_rectangle_is_visible (EvView *view,
206 /*** Geometry computations ***/
207 static void compute_border (EvView *view,
211 static void get_page_y_offset (EvView *view,
215 static gboolean get_page_extents (EvView *view,
217 GdkRectangle *page_area,
219 static void view_rect_to_doc_rect (EvView *view,
220 GdkRectangle *view_rect,
221 GdkRectangle *page_area,
222 EvRectangle *doc_rect);
223 static void doc_rect_to_view_rect (EvView *view,
225 EvRectangle *doc_rect,
226 GdkRectangle *view_rect);
227 static void find_page_at_location (EvView *view,
233 static void ev_view_queue_draw_page (EvView *view,
237 static EvLink* get_link_at_location (EvView *view,
240 static void go_to_link (EvView *view,
242 static char* status_message_from_link (EvView *view,
245 /*** GtkWidget implementation ***/
246 static void ev_view_size_request_continuous_dual_page (EvView *view,
247 GtkRequisition *requisition);
248 static void ev_view_size_request_continuous (EvView *view,
249 GtkRequisition *requisition);
250 static void ev_view_size_request_dual_page (EvView *view,
251 GtkRequisition *requisition);
252 static void ev_view_size_request_single_page (EvView *view,
253 GtkRequisition *requisition);
254 static void ev_view_size_request (GtkWidget *widget,
255 GtkRequisition *requisition);
256 static void ev_view_size_allocate (GtkWidget *widget,
257 GtkAllocation *allocation);
258 static void ev_view_realize (GtkWidget *widget);
259 static void ev_view_unrealize (GtkWidget *widget);
260 static gboolean ev_view_scroll_event (GtkWidget *widget,
261 GdkEventScroll *event);
262 static gboolean ev_view_expose_event (GtkWidget *widget,
263 GdkEventExpose *event);
264 static gboolean ev_view_button_press_event (GtkWidget *widget,
265 GdkEventButton *event);
266 static gboolean ev_view_motion_notify_event (GtkWidget *widget,
267 GdkEventMotion *event);
268 static gboolean ev_view_button_release_event (GtkWidget *widget,
269 GdkEventButton *event);
270 static gboolean ev_view_leave_notify_event (GtkWidget *widget,
271 GdkEventCrossing *event);
272 static void ev_view_style_set (GtkWidget *widget,
273 GtkStyle *old_style);
276 static guint32 ev_gdk_color_to_rgb (const GdkColor *color);
277 static void draw_rubberband (GtkWidget *widget,
279 const GdkRectangle *rect,
281 static void highlight_find_results (EvView *view,
283 static void draw_one_page (EvView *view,
285 GdkRectangle *page_area,
287 GdkRectangle *expose_area);
290 static void find_changed_cb (EvDocument *document,
293 static void job_finished_cb (EvPixbufCache *pixbuf_cache,
295 static void page_changed_cb (EvPageCache *page_cache,
298 static void on_adjustment_value_changed (GtkAdjustment *adjustment,
302 static void ev_view_finalize (GObject *object);
303 static void ev_view_destroy (GtkObject *object);
304 static void ev_view_set_property (GObject *object,
308 static void ev_view_get_property (GObject *object,
312 static void ev_view_class_init (EvViewClass *class);
313 static void ev_view_init (EvView *view);
315 /*** Zoom and sizing ***/
316 static double zoom_for_size_fit_width (int doc_width,
321 static double zoom_for_size_best_fit (int doc_width,
327 static void ev_view_zoom_for_size_presentation (EvView *view,
330 static void ev_view_zoom_for_size_continuous_and_dual_page (EvView *view,
335 static void ev_view_zoom_for_size_continuous (EvView *view,
340 static void ev_view_zoom_for_size_dual_page (EvView *view,
345 static void ev_view_zoom_for_size_single_page (EvView *view,
351 static GdkCursor* ev_view_create_invisible_cursor (void);
352 static void ev_view_set_cursor (EvView *view,
353 EvViewCursor new_cursor);
355 /*** Status messages ***/
356 static void ev_view_set_status (EvView *view,
357 const char *message);
358 static void update_find_status_message (EvView *view);
359 static void ev_view_set_find_status (EvView *view,
360 const char *message);
362 static void jump_to_find_result (EvView *view);
363 static void jump_to_find_page (EvView *view);
366 static void compute_selections (EvView *view,
369 static void clear_selection (EvView *view);
370 static void selection_free (EvViewSelection *selection);
371 static char* get_selected_text (EvView *ev_view);
372 static void ev_view_primary_get_cb (GtkClipboard *clipboard,
373 GtkSelectionData *selection_data,
376 static void ev_view_primary_clear_cb (GtkClipboard *clipboard,
378 static void ev_view_update_primary_selection (EvView *ev_view);
381 G_DEFINE_TYPE (EvView, ev_view, GTK_TYPE_WIDGET)
384 scroll_to_current_page (EvView *view, GtkOrientation orientation)
386 GdkRectangle page_area;
389 if (view->document == NULL) {
393 get_page_extents (view, view->current_page, &page_area, &border);
395 if (orientation == GTK_ORIENTATION_VERTICAL) {
396 if (view->continuous) {
397 gtk_adjustment_clamp_page (view->vadjustment,
398 page_area.y - view->spacing,
399 page_area.y + view->vadjustment->page_size);
401 gtk_adjustment_set_value (view->vadjustment,
402 view->vadjustment->lower);
405 if (view->dual_page) {
406 gtk_adjustment_clamp_page (view->hadjustment,
408 page_area.x + view->hadjustment->page_size);
410 gtk_adjustment_set_value (view->hadjustment,
411 CLAMP (view->hadjustment->value,
412 view->hadjustment->lower,
413 view->hadjustment->upper -
414 view->hadjustment->page_size));
420 view_set_adjustment_values (EvView *view,
421 GtkOrientation orientation)
423 GtkWidget *widget = GTK_WIDGET (view);
424 GtkAdjustment *adjustment;
431 if (orientation == GTK_ORIENTATION_HORIZONTAL) {
432 requisition = widget->requisition.width;
433 allocation = widget->allocation.width;
434 adjustment = view->hadjustment;
436 requisition = widget->requisition.height;
437 allocation = widget->allocation.height;
438 adjustment = view->vadjustment;
445 switch (view->pending_scroll) {
446 case SCROLL_TO_KEEP_POSITION:
447 factor = (adjustment->value) / adjustment->upper;
449 case SCROLL_TO_CURRENT_PAGE:
451 case SCROLL_TO_CENTER:
452 factor = (adjustment->value + adjustment->page_size * 0.5) / adjustment->upper;
456 adjustment->page_size = allocation;
457 adjustment->step_increment = allocation * 0.1;
458 adjustment->page_increment = allocation * 0.9;
459 adjustment->lower = 0;
460 adjustment->upper = MAX (allocation, requisition);
463 * We add 0.5 to the values before to average out our rounding errors.
465 switch (view->pending_scroll) {
466 case SCROLL_TO_KEEP_POSITION:
467 new_value = CLAMP (adjustment->upper * factor + 0.5, 0, adjustment->upper - adjustment->page_size);
468 gtk_adjustment_set_value (adjustment, (int)new_value);
470 case SCROLL_TO_CURRENT_PAGE:
471 scroll_to_current_page (view, orientation);
473 case SCROLL_TO_CENTER:
474 new_value = CLAMP (adjustment->upper * factor - adjustment->page_size * 0.5 + 0.5,
475 0, adjustment->upper - adjustment->page_size);
476 gtk_adjustment_set_value (adjustment, (int)new_value);
480 gtk_adjustment_changed (adjustment);
484 view_update_range_and_current_page (EvView *view)
486 /* Presentation trumps all other modes */
487 if (view->presentation) {
488 view->start_page = view->current_page;
489 view->end_page = view->current_page;
490 } else if (view->continuous) {
491 GdkRectangle current_area, unused, page_area;
494 gboolean found = FALSE;
497 if (!(view->vadjustment && view->hadjustment))
500 current_area.x = view->hadjustment->value;
501 current_area.width = view->hadjustment->upper;
502 current_area.y = view->vadjustment->value;
503 current_area.height = view->vadjustment->page_size;
505 for (i = 0; i < ev_page_cache_get_n_pages (view->page_cache); i++) {
507 get_page_extents (view, i, &page_area, &border);
509 if (gdk_rectangle_intersect (¤t_area, &page_area, &unused)) {
511 view->start_page = i;
521 current_page = ev_page_cache_get_current_page (view->page_cache);
523 if (current_page < view->start_page || current_page > view->end_page) {
524 view->current_page = view->start_page;
525 ev_page_cache_set_current_page (view->page_cache, view->start_page);
528 if (view->dual_page) {
529 if (view->current_page % 2 == 0) {
530 view->start_page = view->current_page;
531 if (view->current_page + 1 < ev_page_cache_get_n_pages (view->page_cache))
532 view->end_page = view->start_page + 1;
534 view->start_page = view->current_page - 1;
535 view->end_page = view->current_page;
538 view->start_page = view->current_page;
539 view->end_page = view->current_page;
543 ev_pixbuf_cache_set_page_range (view->pixbuf_cache,
548 view->selection_info.selections);
552 set_scroll_adjustment (EvView *view,
553 GtkOrientation orientation,
554 GtkAdjustment *adjustment)
556 GtkAdjustment **to_set;
558 if (orientation == GTK_ORIENTATION_HORIZONTAL)
559 to_set = &view->hadjustment;
561 to_set = &view->vadjustment;
563 if (*to_set != adjustment) {
565 g_signal_handlers_disconnect_by_func (*to_set,
566 (gpointer) on_adjustment_value_changed,
568 g_object_unref (*to_set);
571 *to_set = adjustment;
572 view_set_adjustment_values (view, orientation);
575 g_object_ref (*to_set);
576 g_signal_connect (*to_set, "value_changed",
577 G_CALLBACK (on_adjustment_value_changed), view);
583 ev_view_set_scroll_adjustments (EvView *view,
584 GtkAdjustment *hadjustment,
585 GtkAdjustment *vadjustment)
587 set_scroll_adjustment (view, GTK_ORIENTATION_HORIZONTAL, hadjustment);
588 set_scroll_adjustment (view, GTK_ORIENTATION_VERTICAL, vadjustment);
590 on_adjustment_value_changed (NULL, view);
594 add_scroll_binding_keypad (GtkBindingSet *binding_set,
596 GtkScrollType scroll,
599 guint keypad_keyval = keyval - GDK_Left + GDK_KP_Left;
601 gtk_binding_entry_add_signal (binding_set, keyval, 0,
602 "binding_activated", 2,
603 GTK_TYPE_SCROLL_TYPE, scroll,
604 G_TYPE_BOOLEAN, horizontal);
605 gtk_binding_entry_add_signal (binding_set, keypad_keyval, 0,
606 "binding_activated", 2,
607 GTK_TYPE_SCROLL_TYPE, scroll,
608 G_TYPE_BOOLEAN, horizontal);
612 ev_view_scroll (EvView *view,
615 GtkAdjustment *adjustment;
616 double value, increment;
617 gboolean first_page = FALSE;
618 gboolean last_page = FALSE;
620 /* Assign values for increment and vertical adjustment */
621 adjustment = view->vadjustment;
622 increment = adjustment->page_size * 0.75;
623 value = adjustment->value;
625 /* Assign boolean for first and last page */
626 if (view->current_page == 0)
628 if (view->current_page == ev_page_cache_get_n_pages (view->page_cache) - 1)
632 case EV_SCROLL_PAGE_BACKWARD:
633 /* Do not jump backwards if at the first page */
634 if (value == (adjustment->lower) && first_page) {
636 /* At the top of a page, assign the upper bound limit of previous page */
637 } else if (value == (adjustment->lower)) {
638 value = adjustment->upper - adjustment->page_size;
639 ev_page_cache_set_current_page (view->page_cache, view->current_page - 1);
640 /* Jump to the top */
642 value = MAX (value - increment, adjustment->lower);
645 case EV_SCROLL_PAGE_FORWARD:
646 /* Do not jump forward if at the last page */
647 if (value == (adjustment->upper - adjustment->page_size) && last_page) {
649 /* At the bottom of a page, assign the lower bound limit of next page */
650 } else if (value == (adjustment->upper - adjustment->page_size)) {
652 ev_page_cache_set_current_page (view->page_cache, view->current_page + 1);
653 /* Jump to the bottom */
655 value = MIN (value + increment, adjustment->upper - adjustment->page_size);
662 gtk_adjustment_set_value (adjustment, value);
666 ev_view_binding_activated (EvView *view,
667 GtkScrollType scroll,
670 GtkAdjustment *adjustment;
673 if (view->presentation) {
675 case GTK_SCROLL_STEP_BACKWARD:
676 ev_page_cache_prev_page (view->page_cache);
678 case GTK_SCROLL_STEP_FORWARD:
679 ev_page_cache_next_page (view->page_cache);
688 adjustment = view->hadjustment;
690 adjustment = view->vadjustment;
693 value = adjustment->value;
696 case GTK_SCROLL_STEP_BACKWARD:
697 value -= adjustment->step_increment;
699 case GTK_SCROLL_STEP_FORWARD:
700 value += adjustment->step_increment;
706 value = CLAMP (value, adjustment->lower,
707 adjustment->upper - adjustment->page_size);
709 gtk_adjustment_set_value (adjustment, value);
715 ensure_rectangle_is_visible (EvView *view, GdkRectangle *rect)
717 GtkWidget *widget = GTK_WIDGET (view);
718 GtkAdjustment *adjustment;
721 view->pending_scroll = SCROLL_TO_KEEP_POSITION;
723 adjustment = view->vadjustment;
725 if (rect->y < adjustment->value) {
726 value = MAX (adjustment->lower, rect->y - MARGIN);
727 gtk_adjustment_set_value (view->vadjustment, value);
728 } else if (rect->y + rect->height >
729 adjustment->value + widget->allocation.height) {
730 value = MIN (adjustment->upper, rect->y + rect->height -
731 widget->allocation.height + MARGIN);
732 gtk_adjustment_set_value (view->vadjustment, value);
735 adjustment = view->hadjustment;
737 if (rect->x < adjustment->value) {
738 value = MAX (adjustment->lower, rect->x - MARGIN);
739 gtk_adjustment_set_value (view->hadjustment, value);
740 } else if (rect->x + rect->height >
741 adjustment->value + widget->allocation.width) {
742 value = MIN (adjustment->upper, rect->x + rect->width -
743 widget->allocation.width + MARGIN);
744 gtk_adjustment_set_value (view->hadjustment, value);
748 /*** Geometry computations ***/
751 compute_border (EvView *view, int width, int height, GtkBorder *border)
753 if (view->presentation) {
759 ev_document_misc_get_page_border_size (width, height, border);
764 get_page_y_offset (EvView *view, int page, double zoom, int *y_offset)
766 int max_width, offset;
769 g_return_if_fail (y_offset != NULL);
771 ev_page_cache_get_max_width (view->page_cache, view->rotation, zoom, &max_width);
773 compute_border (view, max_width, max_width, &border);
775 if (view->dual_page) {
776 ev_page_cache_get_height_to_page (view->page_cache, page,
777 view->rotation, zoom, NULL, &offset);
778 offset += (page / 2 + 1) * view->spacing + (page / 2) * (border.top + border.bottom);
780 ev_page_cache_get_height_to_page (view->page_cache, page,
781 view->rotation, zoom, &offset, NULL);
782 offset += (page + 1) * view->spacing + page * (border.top + border.bottom);
790 get_page_extents (EvView *view,
792 GdkRectangle *page_area,
798 widget = GTK_WIDGET (view);
800 /* Get the size of the page */
801 ev_page_cache_get_size (view->page_cache, page,
805 compute_border (view, width, height, border);
806 page_area->width = width + border->left + border->right;
807 page_area->height = height + border->top + border->bottom;
809 if (view->presentation) {
810 page_area->x = (MAX (0, widget->allocation.width - width))/2;
811 page_area->y = (MAX (0, widget->allocation.height - height))/2;
812 } else if (view->continuous) {
816 ev_page_cache_get_max_width (view->page_cache, view->scale,
817 view->rotation, &max_width);
818 max_width = max_width + border->left + border->right;
819 /* Get the location of the bounding box */
820 if (view->dual_page) {
821 x = view->spacing + (page % 2) * (max_width + view->spacing);
822 x = x + MAX (0, widget->allocation.width - (max_width * 2 + view->spacing * 3)) / 2;
824 x = x + (max_width - width - border->left - border->right);
827 x = x + MAX (0, widget->allocation.width - (width + view->spacing * 2)) / 2;
830 get_page_y_offset (view, page, view->scale, &y);
836 if (view->dual_page) {
837 gint width_2, height_2;
838 gint max_width = width;
839 gint max_height = height;
840 GtkBorder overall_border;
843 other_page = page ^ 1;
845 /* First, we get the bounding box of the two pages */
846 if (other_page < ev_page_cache_get_n_pages (view->page_cache)) {
847 ev_page_cache_get_size (view->page_cache,
851 &width_2, &height_2);
854 if (height_2 > height)
855 max_height = height_2;
857 compute_border (view, max_width, max_height, &overall_border);
859 /* Find the offsets */
863 /* Adjust for being the left or right page */
865 x = x + max_width - width;
867 x = x + (max_width + overall_border.left + overall_border.right) + view->spacing;
869 y = y + (max_height - height)/2;
871 /* Adjust for extra allocation */
872 x = x + MAX (0, widget->allocation.width -
873 ((max_width + overall_border.left + overall_border.right) * 2 + view->spacing * 3))/2;
874 y = y + MAX (0, widget->allocation.height - (height + view->spacing * 2))/2;
879 /* Adjust for extra allocation */
880 x = x + MAX (0, widget->allocation.width - (width + border->left + border->right + view->spacing * 2))/2;
881 y = y + MAX (0, widget->allocation.height - (height + border->top + border->bottom + view->spacing * 2))/2;
892 view_point_to_doc_point (EvView *view,
893 GdkPoint *view_point,
894 GdkRectangle *page_area,
898 *doc_point_x = (double) (view_point->x - page_area->x) / view->scale;
899 *doc_point_y = (double) (view_point->y - page_area->y) / view->scale;
903 view_rect_to_doc_rect (EvView *view,
904 GdkRectangle *view_rect,
905 GdkRectangle *page_area,
906 EvRectangle *doc_rect)
908 doc_rect->x1 = (double) (view_rect->x - page_area->x) / view->scale;
909 doc_rect->y1 = (double) (view_rect->y - page_area->y) / view->scale;
910 doc_rect->x2 = doc_rect->x1 + (double) view_rect->width / view->scale;
911 doc_rect->y2 = doc_rect->y1 + (double) view_rect->height / view->scale;
915 doc_rect_to_view_rect (EvView *view,
917 EvRectangle *doc_rect,
918 GdkRectangle *view_rect)
920 GdkRectangle page_area;
925 ev_page_cache_get_size (view->page_cache, page,
930 if (view->rotation == 0) {
933 w = doc_rect->x2 - doc_rect->x1;
934 h = doc_rect->y2 - doc_rect->y1;
935 } else if (view->rotation == 90) {
936 x = width - doc_rect->y2;
938 w = doc_rect->y2 - doc_rect->y1;
939 h = doc_rect->x2 - doc_rect->x1;
940 } else if (view->rotation == 180) {
941 x = width - doc_rect->x2;
942 y = height - doc_rect->y2;
943 w = doc_rect->x2 - doc_rect->x1;
944 h = doc_rect->y2 - doc_rect->y1;
945 } else if (view->rotation == 270) {
947 y = height - doc_rect->x2;
948 w = doc_rect->y2 - doc_rect->y1;
949 h = doc_rect->x2 - doc_rect->x1;
951 g_assert_not_reached ();
954 get_page_extents (view, page, &page_area, &border);
956 view_rect->x = x * view->scale + page_area.x;
957 view_rect->y = y * view->scale + page_area.y;
958 view_rect->width = w * view->scale;
959 view_rect->height = h * view->scale;
963 find_page_at_location (EvView *view,
972 if (view->document == NULL)
979 for (i = view->start_page; i <= view->end_page; i++) {
980 GdkRectangle page_area;
983 if (! get_page_extents (view, i, &page_area, &border))
986 if ((x >= page_area.x + border.left) &&
987 (x < page_area.x + page_area.width - border.right) &&
988 (y >= page_area.y + border.top) &&
989 (y < page_area.y + page_area.height - border.bottom)) {
991 *x_offset = x - (page_area.x + border.left);
992 *y_offset = y - (page_area.y + border.top);
1001 ev_view_queue_draw_page (EvView *view,
1005 gtk_widget_queue_draw (GTK_WIDGET (view));
1009 location_in_text (EvView *view,
1015 gint x_offset = 0, y_offset = 0;
1017 find_page_at_location (view, x, y, &page, &x_offset, &y_offset);
1022 region = ev_pixbuf_cache_get_text_mapping (view->pixbuf_cache, page);
1025 return gdk_region_point_in (region, x_offset / view->scale, y_offset / view->scale);
1032 get_link_at_location (EvView *view,
1037 gint x_offset = 0, y_offset = 0;
1038 GList *link_mapping;
1040 find_page_at_location (view, x, y, &page, &x_offset, &y_offset);
1045 link_mapping = ev_pixbuf_cache_get_link_mapping (view->pixbuf_cache, page);
1048 return ev_link_mapping_find (link_mapping, x_offset / view->scale, y_offset / view->scale);
1053 /* FIXME: standardize this sometime */
1055 go_to_link (EvView *view, EvLink *link)
1061 type = ev_link_get_link_type (link);
1064 case EV_LINK_TYPE_TITLE:
1066 case EV_LINK_TYPE_PAGE:
1067 page = ev_link_get_page (link);
1068 ev_page_cache_set_current_page (view->page_cache, page);
1070 case EV_LINK_TYPE_EXTERNAL_URI:
1071 uri = ev_link_get_uri (link);
1072 gnome_vfs_url_show (uri);
1078 status_message_from_link (EvView *view, EvLink *link)
1084 type = ev_link_get_link_type (link);
1087 case EV_LINK_TYPE_TITLE:
1088 if (ev_link_get_title (link))
1089 msg = g_strdup (ev_link_get_title (link));
1091 case EV_LINK_TYPE_PAGE:
1092 page_label = ev_page_cache_get_page_label (view->page_cache, ev_link_get_page (link));
1093 msg = g_strdup_printf (_("Go to page %s"), page_label);
1094 g_free (page_label);
1096 case EV_LINK_TYPE_EXTERNAL_URI:
1097 msg = g_strdup (ev_link_get_uri (link));
1107 /*** GtkWidget implementation ***/
1110 ev_view_size_request_continuous_dual_page (EvView *view,
1111 GtkRequisition *requisition)
1117 ev_page_cache_get_max_width (view->page_cache, view->rotation,
1118 view->scale, &max_width);
1119 compute_border (view, max_width, max_width, &border);
1121 n_pages = ev_page_cache_get_n_pages (view->page_cache) + 1;
1123 requisition->width = (max_width + border.left + border.right) * 2 + (view->spacing * 3);
1124 get_page_y_offset (view, n_pages, view->scale, &requisition->height);
1126 if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
1127 requisition->width = 1;
1128 } else if (view->sizing_mode == EV_SIZING_BEST_FIT) {
1129 requisition->width = 1;
1130 /* FIXME: This could actually be set on one page docs or docs
1131 * with a strange aspect ratio. */
1132 /* requisition->height = 1;*/
1137 ev_view_size_request_continuous (EvView *view,
1138 GtkRequisition *requisition)
1145 ev_page_cache_get_max_width (view->page_cache, view->rotation,
1146 view->scale, &max_width);
1147 n_pages = ev_page_cache_get_n_pages (view->page_cache);
1148 compute_border (view, max_width, max_width, &border);
1150 requisition->width = max_width + (view->spacing * 2) + border.left + border.right;
1151 get_page_y_offset (view, n_pages, view->scale, &requisition->height);
1153 if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
1154 requisition->width = 1;
1155 } else if (view->sizing_mode == EV_SIZING_BEST_FIT) {
1156 requisition->width = 1;
1157 /* FIXME: This could actually be set on one page docs or docs
1158 * with a strange aspect ratio. */
1159 /* requisition->height = 1;*/
1164 ev_view_size_request_dual_page (EvView *view,
1165 GtkRequisition *requisition)
1170 /* Find the largest of the two. */
1171 ev_page_cache_get_size (view->page_cache,
1176 if (view->current_page + 1 < ev_page_cache_get_n_pages (view->page_cache)) {
1177 gint width_2, height_2;
1178 ev_page_cache_get_size (view->page_cache,
1179 view->current_page + 1,
1182 &width_2, &height_2);
1183 if (width_2 > width) {
1188 compute_border (view, width, height, &border);
1190 requisition->width = ((width + border.left + border.right) * 2) +
1191 (view->spacing * 3);
1192 requisition->height = (height + border.top + border.bottom) +
1193 (view->spacing * 2);
1195 if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
1196 requisition->width = 1;
1197 } else if (view->sizing_mode == EV_SIZING_BEST_FIT) {
1198 requisition->width = 1;
1199 requisition->height = 1;
1204 ev_view_size_request_single_page (EvView *view,
1205 GtkRequisition *requisition)
1210 ev_page_cache_get_size (view->page_cache,
1215 compute_border (view, width, height, &border);
1217 requisition->width = width + border.left + border.right + (2 * view->spacing);
1218 requisition->height = height + border.top + border.bottom + (2 * view->spacing);
1220 if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
1221 requisition->width = 1;
1222 requisition->height = height + border.top + border.bottom + (2 * view->spacing);
1223 } else if (view->sizing_mode == EV_SIZING_BEST_FIT) {
1224 requisition->width = 1;
1225 requisition->height = 1;
1230 ev_view_size_request (GtkWidget *widget,
1231 GtkRequisition *requisition)
1233 EvView *view = EV_VIEW (widget);
1235 if (view->document == NULL) {
1236 requisition->width = 1;
1237 requisition->height = 1;
1241 if (view->presentation) {
1242 requisition->width = 1;
1243 requisition->height = 1;
1247 if (view->continuous && view->dual_page)
1248 ev_view_size_request_continuous_dual_page (view, requisition);
1249 else if (view->continuous)
1250 ev_view_size_request_continuous (view, requisition);
1251 else if (view->dual_page)
1252 ev_view_size_request_dual_page (view, requisition);
1254 ev_view_size_request_single_page (view, requisition);
1258 ev_view_size_allocate (GtkWidget *widget,
1259 GtkAllocation *allocation)
1261 EvView *view = EV_VIEW (widget);
1263 if (view->sizing_mode == EV_SIZING_FIT_WIDTH ||
1264 view->sizing_mode == EV_SIZING_BEST_FIT) {
1266 g_signal_emit (view, signals[SIGNAL_ZOOM_INVALID], 0);
1268 ev_view_size_request (widget, &widget->requisition);
1271 view_set_adjustment_values (view, GTK_ORIENTATION_HORIZONTAL);
1272 view_set_adjustment_values (view, GTK_ORIENTATION_VERTICAL);
1274 view->pending_scroll = SCROLL_TO_KEEP_POSITION;
1275 view->pending_resize = FALSE;
1278 view_update_range_and_current_page (view);
1280 GTK_WIDGET_CLASS (ev_view_parent_class)->size_allocate (widget, allocation);
1284 ev_view_realize (GtkWidget *widget)
1286 EvView *view = EV_VIEW (widget);
1287 GdkWindowAttr attributes;
1289 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1292 attributes.window_type = GDK_WINDOW_CHILD;
1293 attributes.wclass = GDK_INPUT_OUTPUT;
1294 attributes.visual = gtk_widget_get_visual (widget);
1295 attributes.colormap = gtk_widget_get_colormap (widget);
1297 attributes.x = widget->allocation.x;
1298 attributes.y = widget->allocation.y;
1299 attributes.width = widget->allocation.width;
1300 attributes.height = widget->allocation.height;
1301 attributes.event_mask = GDK_EXPOSURE_MASK |
1302 GDK_BUTTON_PRESS_MASK |
1303 GDK_BUTTON_RELEASE_MASK |
1305 GDK_KEY_PRESS_MASK |
1306 GDK_POINTER_MOTION_MASK |
1307 GDK_LEAVE_NOTIFY_MASK;
1309 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
1311 GDK_WA_X | GDK_WA_Y |
1314 gdk_window_set_user_data (widget->window, widget);
1315 widget->style = gtk_style_attach (widget->style, widget->window);
1317 if (view->presentation)
1318 gdk_window_set_background (widget->window, &widget->style->black);
1320 gdk_window_set_background (widget->window, &widget->style->mid [GTK_STATE_NORMAL]);
1324 ev_view_unrealize (GtkWidget *widget)
1326 GTK_WIDGET_CLASS (ev_view_parent_class)->unrealize (widget);
1330 ev_view_scroll_event (GtkWidget *widget, GdkEventScroll *event)
1332 EvView *view = EV_VIEW (widget);
1334 if ((event->state & GDK_CONTROL_MASK) != 0) {
1336 ev_view_set_sizing_mode (view, EV_SIZING_FREE);
1338 if (event->direction == GDK_SCROLL_UP ||
1339 event->direction == GDK_SCROLL_LEFT) {
1340 if (ev_view_can_zoom_in (view)) {
1341 ev_view_zoom_in (view);
1344 if (ev_view_can_zoom_out (view)) {
1345 ev_view_zoom_out (view);
1351 if ((event->state & GDK_SHIFT_MASK) != 0) {
1352 if (event->direction == GDK_SCROLL_UP)
1353 event->direction = GDK_SCROLL_LEFT;
1354 if (event->direction == GDK_SCROLL_DOWN)
1355 event->direction = GDK_SCROLL_RIGHT;
1361 static EvViewSelection *
1362 find_selection_for_page (EvView *view,
1367 for (list = view->selection_info.selections; list != NULL; list = list->next) {
1368 EvViewSelection *selection;
1370 selection = (EvViewSelection *) list->data;
1372 if (selection->page == page)
1380 ev_view_expose_event (GtkWidget *widget,
1381 GdkEventExpose *event)
1383 EvView *view = EV_VIEW (widget);
1386 if (view->document == NULL)
1389 for (i = view->start_page; i <= view->end_page; i++) {
1390 GdkRectangle page_area;
1393 if (!get_page_extents (view, i, &page_area, &border))
1396 page_area.x -= view->scroll_x;
1397 page_area.y -= view->scroll_y;
1399 draw_one_page (view, i, &page_area, &border, &(event->area));
1401 if (EV_IS_DOCUMENT_FIND (view->document))
1402 highlight_find_results (view, i);
1409 ev_view_button_press_event (GtkWidget *widget,
1410 GdkEventButton *event)
1412 EvView *view = EV_VIEW (widget);
1414 if (!GTK_WIDGET_HAS_FOCUS (widget)) {
1415 gtk_widget_grab_focus (widget);
1418 view->pressed_button = event->button;
1420 switch (event->button) {
1422 if (view->selection_info.selections) {
1423 clear_selection (view);
1424 gtk_widget_queue_draw (widget);
1427 view->selection_info.start.x = event->x + view->scroll_x;
1428 view->selection_info.start.y = event->y + view->scroll_y;
1431 /* use root coordinates as reference point because
1432 * scrolling changes window relative coordinates */
1433 view->drag_info.start.x = event->x_root;
1434 view->drag_info.start.y = event->y_root;
1435 view->drag_info.hadj = gtk_adjustment_get_value (view->hadjustment);
1436 view->drag_info.vadj = gtk_adjustment_get_value (view->vadjustment);
1438 ev_view_set_cursor (view, EV_VIEW_CURSOR_DRAG);
1448 selection_update_idle_cb (EvView *view)
1451 point.x = view->motion_x;
1452 point.y = view->motion_y;
1453 compute_selections (view, &view->selection_info.start, &point);
1455 view->selection_update_id = 0;
1460 ev_view_motion_notify_event (GtkWidget *widget,
1461 GdkEventMotion *event)
1463 EvView *view = EV_VIEW (widget);
1465 if (!view->document)
1468 if (view->pressed_button == 1) {
1469 view->selection_info.in_selection = TRUE;
1470 view->motion_x = event->x + view->scroll_x;
1471 view->motion_y = event->y + view->scroll_y;
1473 /* Queue an idle to handle the motion. We do this because
1474 * handling any selection events in the motion is probably going
1475 * to be slower than new motion events reach us. This means that */
1477 if (! view->selection_update_id)
1478 view->selection_update_id = g_idle_add ((GSourceFunc)selection_update_idle_cb, view);
1481 } else if (view->pressed_button == 2) {
1482 if (!view->drag_info.in_drag) {
1485 start = gtk_drag_check_threshold (widget,
1486 view->drag_info.start.x,
1487 view->drag_info.start.y,
1490 view->drag_info.in_drag = start;
1493 if (view->drag_info.in_drag) {
1495 gdouble dhadj_value, dvadj_value;
1497 dx = event->x_root - view->drag_info.start.x;
1498 dy = event->y_root - view->drag_info.start.y;
1500 dhadj_value = view->hadjustment->page_size *
1501 (gdouble)dx / widget->allocation.width;
1502 dvadj_value = view->vadjustment->page_size *
1503 (gdouble)dy / widget->allocation.height;
1505 /* clamp scrolling to visible area */
1506 gtk_adjustment_set_value (view->hadjustment,
1507 MIN(view->drag_info.hadj - dhadj_value,
1508 view->hadjustment->upper -
1509 view->hadjustment->page_size));
1510 gtk_adjustment_set_value (view->vadjustment,
1511 MIN(view->drag_info.vadj - dvadj_value,
1512 view->vadjustment->upper -
1513 view->vadjustment->page_size));
1517 } else if (view->pressed_button <= 0) {
1520 link = get_link_at_location (view, event->x + view->scroll_x, event->y + view->scroll_y);
1524 msg = status_message_from_link (view, link);
1525 ev_view_set_status (view, msg);
1526 ev_view_set_cursor (view, EV_VIEW_CURSOR_LINK);
1528 } else if (location_in_text (view, event->x + view->scroll_x, event->y + view->scroll_y)) {
1529 ev_view_set_cursor (view, EV_VIEW_CURSOR_IBEAM);
1531 ev_view_set_status (view, NULL);
1532 if (view->cursor == EV_VIEW_CURSOR_LINK ||
1533 view->cursor == EV_VIEW_CURSOR_IBEAM)
1534 ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL);
1543 ev_view_button_release_event (GtkWidget *widget,
1544 GdkEventButton *event)
1546 EvView *view = EV_VIEW (widget);
1548 if (view->pressed_button == 2) {
1549 ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL);
1552 view->pressed_button = -1;
1553 view->drag_info.in_drag = FALSE;
1555 if (view->selection_info.selections) {
1556 ev_view_update_primary_selection (view);
1557 } else if (view->document) {
1560 link = get_link_at_location (view, event->x + view->scroll_x, event->y + view->scroll_y);
1562 go_to_link (view, link);
1570 ev_view_leave_notify_event (GtkWidget *widget, GdkEventCrossing *event)
1572 EvView *view = EV_VIEW (widget);
1574 ev_view_set_status (view, NULL);
1576 if (view->cursor == EV_VIEW_CURSOR_LINK ||
1577 view->cursor == EV_VIEW_CURSOR_IBEAM)
1578 ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL);
1584 ev_view_style_set (GtkWidget *widget,
1585 GtkStyle *old_style)
1587 if (EV_VIEW (widget)->pixbuf_cache)
1588 ev_pixbuf_cache_style_changed (EV_VIEW (widget)->pixbuf_cache);
1590 GTK_WIDGET_CLASS (ev_view_parent_class)->style_set (widget, old_style);
1597 ev_gdk_color_to_rgb (const GdkColor *color)
1600 result = (0xff0000 | (color->red & 0xff00));
1602 result |= ((color->green & 0xff00) | (color->blue >> 8));
1607 draw_rubberband (GtkWidget *widget, GdkWindow *window,
1608 const GdkRectangle *rect, guchar alpha)
1612 GdkColor *fill_color_gdk;
1615 fill_color_gdk = gdk_color_copy (>K_WIDGET (widget)->style->base[GTK_STATE_SELECTED]);
1616 fill_color = ev_gdk_color_to_rgb (fill_color_gdk) << 8 | alpha;
1618 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
1619 rect->width, rect->height);
1620 gdk_pixbuf_fill (pixbuf, fill_color);
1622 gdk_draw_pixbuf (window, NULL, pixbuf,
1624 rect->x - EV_VIEW (widget)->scroll_x, rect->y - EV_VIEW (widget)->scroll_y,
1625 rect->width, rect->height,
1626 GDK_RGB_DITHER_NONE,
1629 g_object_unref (pixbuf);
1631 gc = gdk_gc_new (window);
1632 gdk_gc_set_rgb_fg_color (gc, fill_color_gdk);
1633 gdk_draw_rectangle (window, gc, FALSE,
1634 rect->x - EV_VIEW (widget)->scroll_x, rect->y - EV_VIEW (widget)->scroll_y,
1637 g_object_unref (gc);
1639 gdk_color_free (fill_color_gdk);
1644 highlight_find_results (EvView *view, int page)
1646 EvDocumentFind *find;
1649 g_return_if_fail (EV_IS_DOCUMENT_FIND (view->document));
1651 find = EV_DOCUMENT_FIND (view->document);
1653 results = ev_document_find_get_n_results (find, page);
1655 for (i = 0; i < results; i++) {
1656 EvRectangle rectangle;
1657 GdkRectangle view_rectangle;
1660 if (i == view->find_result && page == view->find_page) {
1666 ev_document_find_get_result (find, page,
1668 doc_rect_to_view_rect (view, page, &rectangle, &view_rectangle);
1669 draw_rubberband (GTK_WIDGET (view), GTK_WIDGET(view)->window,
1670 &view_rectangle, alpha);
1675 draw_one_page (EvView *view,
1677 GdkRectangle *page_area,
1679 GdkRectangle *expose_area)
1682 GdkPixbuf *current_pixbuf;
1683 GdkRectangle overlap;
1684 GdkRectangle real_page_area;
1685 EvViewSelection *selection;
1687 g_assert (view->document);
1688 if (! gdk_rectangle_intersect (page_area, expose_area, &overlap))
1691 selection = find_selection_for_page (view, page);
1692 ev_page_cache_get_size (view->page_cache,
1693 page, view->rotation,
1696 /* Render the document itself */
1697 real_page_area = *page_area;
1699 real_page_area.x += border->left;
1700 real_page_area.y += border->top;
1701 real_page_area.width -= (border->left + border->right);
1702 real_page_area.height -= (border->top + border->bottom);
1704 ev_document_misc_paint_one_page (GTK_WIDGET(view)->window,
1708 if (gdk_rectangle_intersect (&real_page_area, expose_area, &overlap)) {
1709 GdkPixbuf *selection_pixbuf = NULL;
1710 GdkPixbuf *scaled_image;
1711 GdkPixbuf *scaled_selection;
1713 current_pixbuf = ev_pixbuf_cache_get_pixbuf (view->pixbuf_cache, page);
1715 /* Get the selection pixbuf iff we have something to draw */
1716 if (current_pixbuf && view->selection_mode == EV_VIEW_SELECTION_TEXT && selection)
1717 selection_pixbuf = ev_pixbuf_cache_get_selection_pixbuf (view->pixbuf_cache,
1722 if (current_pixbuf == NULL)
1723 scaled_image = NULL;
1724 else if (width == gdk_pixbuf_get_width (current_pixbuf) &&
1725 height == gdk_pixbuf_get_height (current_pixbuf))
1726 scaled_image = g_object_ref (current_pixbuf);
1728 /* FIXME: We don't want to scale the whole area, just the right
1730 scaled_image = gdk_pixbuf_scale_simple (current_pixbuf,
1732 GDK_INTERP_NEAREST);
1734 if (selection_pixbuf == NULL)
1735 scaled_selection = NULL;
1736 else if (width == gdk_pixbuf_get_width (selection_pixbuf) &&
1737 height == gdk_pixbuf_get_height (selection_pixbuf))
1738 scaled_selection = g_object_ref (selection_pixbuf);
1740 /* FIXME: We don't want to scale the whole area, just the right
1742 scaled_selection = gdk_pixbuf_scale_simple (selection_pixbuf,
1744 GDK_INTERP_NEAREST);
1747 gdk_draw_pixbuf (GTK_WIDGET(view)->window,
1748 GTK_WIDGET (view)->style->fg_gc[GTK_STATE_NORMAL],
1750 overlap.x - real_page_area.x,
1751 overlap.y - real_page_area.y,
1752 overlap.x, overlap.y,
1753 overlap.width, overlap.height,
1754 GDK_RGB_DITHER_NORMAL,
1756 g_object_unref (scaled_image);
1759 if (scaled_selection) {
1760 gdk_draw_pixbuf (GTK_WIDGET(view)->window,
1761 GTK_WIDGET (view)->style->fg_gc[GTK_STATE_NORMAL],
1763 overlap.x - real_page_area.x,
1764 overlap.y - real_page_area.y,
1765 overlap.x, overlap.y,
1766 overlap.width, overlap.height,
1767 GDK_RGB_DITHER_NORMAL,
1769 g_object_unref (scaled_selection);
1774 /*** GObject functions ***/
1777 ev_view_finalize (GObject *object)
1779 EvView *view = EV_VIEW (object);
1783 g_free (view->status);
1784 g_free (view->find_status);
1786 clear_selection (view);
1788 G_OBJECT_CLASS (ev_view_parent_class)->finalize (object);
1792 ev_view_destroy (GtkObject *object)
1794 EvView *view = EV_VIEW (object);
1796 if (view->document) {
1797 g_object_unref (view->document);
1798 view->document = NULL;
1800 if (view->pixbuf_cache) {
1801 g_object_unref (view->pixbuf_cache);
1802 view->pixbuf_cache = NULL;
1804 ev_view_set_scroll_adjustments (view, NULL, NULL);
1806 GTK_OBJECT_CLASS (ev_view_parent_class)->destroy (object);
1810 ev_view_set_property (GObject *object,
1812 const GValue *value,
1815 EvView *view = EV_VIEW (object);
1819 case PROP_CONTINUOUS:
1820 ev_view_set_continuous (view, g_value_get_boolean (value));
1822 case PROP_DUAL_PAGE:
1823 ev_view_set_dual_page (view, g_value_get_boolean (value));
1825 case PROP_FULLSCREEN:
1826 ev_view_set_fullscreen (view, g_value_get_boolean (value));
1828 case PROP_PRESENTATION:
1829 ev_view_set_presentation (view, g_value_get_boolean (value));
1831 case PROP_SIZING_MODE:
1832 ev_view_set_sizing_mode (view, g_value_get_enum (value));
1835 ev_view_set_zoom (view, g_value_get_double (value), FALSE);
1838 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1843 ev_view_get_property (GObject *object,
1848 EvView *view = EV_VIEW (object);
1853 g_value_set_string (value, view->status);
1855 case PROP_FIND_STATUS:
1856 g_value_set_string (value, view->status);
1858 case PROP_CONTINUOUS:
1859 g_value_set_boolean (value, view->continuous);
1861 case PROP_DUAL_PAGE:
1862 g_value_set_boolean (value, view->dual_page);
1864 case PROP_FULLSCREEN:
1865 g_value_set_boolean (value, view->fullscreen);
1867 case PROP_PRESENTATION:
1868 g_value_set_boolean (value, view->presentation);
1870 case PROP_SIZING_MODE:
1871 g_value_set_enum (value, view->sizing_mode);
1874 g_value_set_double (value, view->scale);
1877 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1882 ev_view_class_init (EvViewClass *class)
1884 GObjectClass *object_class = G_OBJECT_CLASS (class);
1885 GtkObjectClass *gtk_object_class = GTK_OBJECT_CLASS (class);
1886 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
1887 GtkBindingSet *binding_set;
1889 object_class->finalize = ev_view_finalize;
1890 object_class->set_property = ev_view_set_property;
1891 object_class->get_property = ev_view_get_property;
1893 widget_class->expose_event = ev_view_expose_event;
1894 widget_class->button_press_event = ev_view_button_press_event;
1895 widget_class->motion_notify_event = ev_view_motion_notify_event;
1896 widget_class->button_release_event = ev_view_button_release_event;
1897 widget_class->size_request = ev_view_size_request;
1898 widget_class->size_allocate = ev_view_size_allocate;
1899 widget_class->realize = ev_view_realize;
1900 widget_class->unrealize = ev_view_unrealize;
1901 widget_class->scroll_event = ev_view_scroll_event;
1902 widget_class->leave_notify_event = ev_view_leave_notify_event;
1903 widget_class->style_set = ev_view_style_set;
1904 gtk_object_class->destroy = ev_view_destroy;
1906 class->set_scroll_adjustments = ev_view_set_scroll_adjustments;
1907 class->binding_activated = ev_view_binding_activated;
1909 widget_class->set_scroll_adjustments_signal =
1910 g_signal_new ("set-scroll-adjustments",
1911 G_OBJECT_CLASS_TYPE (object_class),
1912 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1913 G_STRUCT_OFFSET (EvViewClass, set_scroll_adjustments),
1915 ev_marshal_VOID__OBJECT_OBJECT,
1917 GTK_TYPE_ADJUSTMENT,
1918 GTK_TYPE_ADJUSTMENT);
1920 signals[SIGNAL_BINDING_ACTIVATED] = g_signal_new ("binding_activated",
1921 G_TYPE_FROM_CLASS (object_class),
1922 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1923 G_STRUCT_OFFSET (EvViewClass, binding_activated),
1925 ev_marshal_VOID__ENUM_BOOLEAN,
1927 GTK_TYPE_SCROLL_TYPE,
1930 signals[SIGNAL_ZOOM_INVALID] = g_signal_new ("zoom-invalid",
1931 G_TYPE_FROM_CLASS (object_class),
1932 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1933 G_STRUCT_OFFSET (EvViewClass, zoom_invalid),
1935 ev_marshal_VOID__VOID,
1936 G_TYPE_NONE, 0, G_TYPE_NONE);
1938 g_object_class_install_property (object_class,
1940 g_param_spec_string ("status",
1942 "The status message",
1946 g_object_class_install_property (object_class,
1948 g_param_spec_string ("find-status",
1949 "Find Status Message",
1950 "The find status message",
1954 g_object_class_install_property (object_class,
1956 g_param_spec_boolean ("continuous",
1958 "Continuous scrolling mode",
1960 G_PARAM_READWRITE));
1962 g_object_class_install_property (object_class,
1964 g_param_spec_boolean ("dual-page",
1966 "Two pages visible at once",
1968 G_PARAM_READWRITE));
1969 g_object_class_install_property (object_class,
1971 g_param_spec_boolean ("fullscreen",
1973 "Draw page in a fullscreen fashion",
1975 G_PARAM_READWRITE));
1976 g_object_class_install_property (object_class,
1978 g_param_spec_boolean ("presentation",
1980 "Draw page in presentation mode",
1982 G_PARAM_READWRITE));
1984 g_object_class_install_property (object_class,
1986 g_param_spec_enum ("sizing-mode",
1989 EV_TYPE_SIZING_MODE,
1990 EV_SIZING_FIT_WIDTH,
1991 G_PARAM_READWRITE));
1993 g_object_class_install_property (object_class,
1995 g_param_spec_double ("zoom",
2001 G_PARAM_READWRITE));
2003 binding_set = gtk_binding_set_by_class (class);
2005 add_scroll_binding_keypad (binding_set, GDK_Left, GTK_SCROLL_STEP_BACKWARD, TRUE);
2006 add_scroll_binding_keypad (binding_set, GDK_Right, GTK_SCROLL_STEP_FORWARD, TRUE);
2007 add_scroll_binding_keypad (binding_set, GDK_Up, GTK_SCROLL_STEP_BACKWARD, FALSE);
2008 add_scroll_binding_keypad (binding_set, GDK_Down, GTK_SCROLL_STEP_FORWARD, FALSE);
2012 ev_view_init (EvView *view)
2014 GTK_WIDGET_SET_FLAGS (view, GTK_CAN_FOCUS);
2018 view->current_page = 0;
2019 view->pressed_button = -1;
2020 view->cursor = EV_VIEW_CURSOR_NORMAL;
2021 view->drag_info.in_drag = FALSE;
2022 view->selection_info.in_selection = FALSE;
2024 view->selection_mode = EV_VIEW_SELECTION_TEXT;
2025 view->continuous = TRUE;
2026 view->dual_page = FALSE;
2027 view->presentation = FALSE;
2028 view->fullscreen = FALSE;
2029 view->sizing_mode = EV_SIZING_FIT_WIDTH;
2030 view->pending_scroll = SCROLL_TO_KEEP_POSITION;
2036 find_changed_cb (EvDocument *document, int page, EvView *view)
2038 jump_to_find_page (view);
2039 jump_to_find_result (view);
2040 update_find_status_message (view);
2042 if (view->current_page == page)
2043 gtk_widget_queue_draw (GTK_WIDGET (view));
2047 job_finished_cb (EvPixbufCache *pixbuf_cache,
2050 gtk_widget_queue_draw (GTK_WIDGET (view));
2054 page_changed_cb (EvPageCache *page_cache,
2058 if (view->current_page != new_page) {
2060 view->current_page = new_page;
2061 view->pending_scroll = SCROLL_TO_CURRENT_PAGE;
2062 gtk_widget_queue_resize (GTK_WIDGET (view));
2064 if (EV_IS_DOCUMENT_FIND (view->document)) {
2065 view->find_page = new_page;
2066 view->find_result = 0;
2067 update_find_status_message (view);
2072 static void on_adjustment_value_changed (GtkAdjustment *adjustment,
2077 if (! GTK_WIDGET_REALIZED (view))
2080 if (view->hadjustment) {
2081 dx = view->scroll_x - (int) view->hadjustment->value;
2082 view->scroll_x = (int) view->hadjustment->value;
2087 if (view->vadjustment) {
2088 dy = view->scroll_y - (int) view->vadjustment->value;
2089 view->scroll_y = (int) view->vadjustment->value;
2095 if (view->pending_resize)
2096 gtk_widget_queue_draw (GTK_WIDGET (view));
2098 gdk_window_scroll (GTK_WIDGET (view)->window, dx, dy);
2102 view_update_range_and_current_page (view);
2110 view = g_object_new (EV_TYPE_VIEW, NULL);
2116 setup_caches (EvView *view)
2118 view->page_cache = ev_page_cache_get (view->document);
2119 g_signal_connect (view->page_cache, "page-changed", G_CALLBACK (page_changed_cb), view);
2120 view->pixbuf_cache = ev_pixbuf_cache_new (GTK_WIDGET (view), view->document);
2121 g_signal_connect (view->pixbuf_cache, "job-finished", G_CALLBACK (job_finished_cb), view);
2125 clear_caches (EvView *view)
2127 if (view->pixbuf_cache) {
2128 g_object_unref (view->pixbuf_cache);
2129 view->pixbuf_cache = NULL;
2132 if (view->page_cache) {
2133 g_object_unref (view->page_cache);
2134 view->page_cache = NULL;
2139 ev_view_set_document (EvView *view,
2140 EvDocument *document)
2142 g_return_if_fail (EV_IS_VIEW (view));
2144 if (document != view->document) {
2145 clear_caches (view);
2147 if (view->document) {
2148 g_signal_handlers_disconnect_by_func (view->document,
2151 g_object_unref (view->document);
2152 view->page_cache = NULL;
2156 view->document = document;
2157 view->find_page = 0;
2158 view->find_result = 0;
2160 if (view->document) {
2161 g_object_ref (view->document);
2162 if (EV_IS_DOCUMENT_FIND (view->document)) {
2163 g_signal_connect (view->document,
2165 G_CALLBACK (find_changed_cb),
2169 setup_caches (view);
2172 gtk_widget_queue_resize (GTK_WIDGET (view));
2176 /*** Zoom and sizing mode ***/
2178 #define EPSILON 0.0000001
2180 ev_view_set_zoom (EvView *view,
2187 scale = view->scale * factor;
2191 scale = CLAMP (scale, MIN_SCALE, MAX_SCALE);
2193 if (ABS (view->scale - scale) < EPSILON)
2196 view->scale = scale;
2197 view->pending_resize = TRUE;
2199 gtk_widget_queue_resize (GTK_WIDGET (view));
2201 g_object_notify (G_OBJECT (view), "zoom");
2205 ev_view_get_zoom (EvView *view)
2211 ev_view_get_continuous (EvView *view)
2213 g_return_val_if_fail (EV_IS_VIEW (view), FALSE);
2215 return view->continuous;
2219 ev_view_set_continuous (EvView *view,
2220 gboolean continuous)
2222 g_return_if_fail (EV_IS_VIEW (view));
2224 continuous = continuous != FALSE;
2226 if (view->continuous != continuous) {
2227 view->continuous = continuous;
2228 view->pending_scroll = SCROLL_TO_CURRENT_PAGE;
2229 gtk_widget_queue_resize (GTK_WIDGET (view));
2232 g_object_notify (G_OBJECT (view), "continuous");
2236 ev_view_get_dual_page (EvView *view)
2238 g_return_val_if_fail (EV_IS_VIEW (view), FALSE);
2240 return view->dual_page;
2244 ev_view_set_dual_page (EvView *view,
2247 g_return_if_fail (EV_IS_VIEW (view));
2249 dual_page = dual_page != FALSE;
2251 if (view->dual_page == dual_page)
2254 view->pending_scroll = SCROLL_TO_CURRENT_PAGE;
2255 view->dual_page = dual_page;
2256 /* FIXME: if we're keeping the pixbuf cache around, we should extend the
2257 * preload_cache_size to be 2 if dual_page is set.
2259 gtk_widget_queue_resize (GTK_WIDGET (view));
2261 g_object_notify (G_OBJECT (view), "dual-page");
2265 ev_view_set_fullscreen (EvView *view,
2266 gboolean fullscreen)
2268 g_return_if_fail (EV_IS_VIEW (view));
2270 fullscreen = fullscreen != FALSE;
2272 if (view->fullscreen == fullscreen)
2275 view->fullscreen = fullscreen;
2276 gtk_widget_queue_resize (GTK_WIDGET (view));
2278 g_object_notify (G_OBJECT (view), "fullscreen");
2282 ev_view_get_fullscreen (EvView *view)
2284 g_return_val_if_fail (EV_IS_VIEW (view), FALSE);
2286 return view->fullscreen;
2290 ev_view_set_presentation (EvView *view,
2291 gboolean presentation)
2293 g_return_if_fail (EV_IS_VIEW (view));
2295 presentation = presentation != FALSE;
2297 if (view->presentation == presentation)
2300 view->presentation = presentation;
2301 view->pending_scroll = SCROLL_TO_CURRENT_PAGE;
2302 gtk_widget_queue_resize (GTK_WIDGET (view));
2304 if (GTK_WIDGET_REALIZED (view)) {
2305 if (view->presentation)
2306 gdk_window_set_background (GTK_WIDGET(view)->window,
2307 >K_WIDGET (view)->style->black);
2309 gdk_window_set_background (GTK_WIDGET(view)->window,
2310 >K_WIDGET (view)->style->mid [GTK_STATE_NORMAL]);
2314 g_object_notify (G_OBJECT (view), "presentation");
2318 ev_view_get_presentation (EvView *view)
2320 g_return_val_if_fail (EV_IS_VIEW (view), FALSE);
2322 return view->presentation;
2326 ev_view_set_sizing_mode (EvView *view,
2327 EvSizingMode sizing_mode)
2329 g_return_if_fail (EV_IS_VIEW (view));
2331 if (view->sizing_mode == sizing_mode)
2334 view->sizing_mode = sizing_mode;
2335 gtk_widget_queue_resize (GTK_WIDGET (view));
2337 g_object_notify (G_OBJECT (view), "sizing-mode");
2341 ev_view_get_sizing_mode (EvView *view)
2343 g_return_val_if_fail (EV_IS_VIEW (view), EV_SIZING_FREE);
2345 return view->sizing_mode;
2349 ev_view_can_zoom_in (EvView *view)
2351 return view->scale * ZOOM_IN_FACTOR <= MAX_SCALE;
2355 ev_view_can_zoom_out (EvView *view)
2357 return view->scale * ZOOM_OUT_FACTOR >= MIN_SCALE;
2361 ev_view_zoom_in (EvView *view)
2363 g_return_if_fail (view->sizing_mode == EV_SIZING_FREE);
2365 view->pending_scroll = SCROLL_TO_CENTER;
2366 ev_view_set_zoom (view, ZOOM_IN_FACTOR, TRUE);
2370 ev_view_zoom_out (EvView *view)
2372 g_return_if_fail (view->sizing_mode == EV_SIZING_FREE);
2374 view->pending_scroll = SCROLL_TO_CENTER;
2375 ev_view_set_zoom (view, ZOOM_OUT_FACTOR, TRUE);
2379 ev_view_set_rotation (EvView *view, int rotation)
2381 view->rotation = rotation;
2383 ev_pixbuf_cache_clear (view->pixbuf_cache);
2384 gtk_widget_queue_resize (GTK_WIDGET (view));
2388 ev_view_rotate_right (EvView *view)
2390 int rotation = view->rotation + 90;
2392 if (rotation >= 360) {
2396 ev_view_set_rotation (view, rotation);
2400 ev_view_rotate_left (EvView *view)
2402 int rotation = view->rotation - 90;
2408 ev_view_set_rotation (view, rotation);
2412 zoom_for_size_fit_width (int doc_width,
2420 scale = (double)target_width / doc_width;
2422 if (doc_height * scale > target_height)
2423 scale = (double) (target_width - vsb_width) / doc_width;
2429 zoom_for_size_best_fit (int doc_width,
2439 w_scale = (double)target_width / doc_width;
2440 h_scale = (double)target_height / doc_height;
2442 if (doc_height * w_scale > target_height)
2443 w_scale = (double) (target_width - vsb_width) / doc_width;
2444 if (doc_width * h_scale > target_width)
2445 h_scale = (double) (target_height - hsb_width) / doc_height;
2447 return MIN (w_scale, h_scale);
2452 ev_view_zoom_for_size_presentation (EvView *view,
2456 int doc_width, doc_height;
2459 ev_page_cache_get_size (view->page_cache,
2465 scale = zoom_for_size_best_fit (doc_width, doc_height, width, height, 0, 0);
2466 ev_view_set_zoom (view, scale, FALSE);
2470 ev_view_zoom_for_size_continuous_and_dual_page (EvView *view,
2476 int doc_width, doc_height;
2480 ev_page_cache_get_max_width (view->page_cache,
2484 ev_page_cache_get_max_height (view->page_cache,
2488 compute_border (view, doc_width, doc_height, &border);
2490 doc_width = doc_width * 2;
2491 width -= (2 * (border.left + border.right) + 3 * view->spacing);
2492 height -= (border.top + border.bottom + 2 * view->spacing - 1);
2494 /* FIXME: We really need to calculate the overall height here, not the
2495 * page height. We assume there's always a vertical scrollbar for
2496 * now. We need to fix this. */
2497 if (view->sizing_mode == EV_SIZING_FIT_WIDTH)
2498 scale = zoom_for_size_fit_width (doc_width, doc_height, width - vsb_width, height, 0);
2499 else if (view->sizing_mode == EV_SIZING_BEST_FIT)
2500 scale = zoom_for_size_best_fit (doc_width, doc_height, width - vsb_width, height, 0, hsb_height);
2502 g_assert_not_reached ();
2504 ev_view_set_zoom (view, scale, FALSE);
2508 ev_view_zoom_for_size_continuous (EvView *view,
2514 int doc_width, doc_height;
2518 ev_page_cache_get_max_width (view->page_cache,
2522 ev_page_cache_get_max_height (view->page_cache,
2526 compute_border (view, doc_width, doc_height, &border);
2528 width -= (border.left + border.right + 2 * view->spacing);
2529 height -= (border.top + border.bottom + 2 * view->spacing - 1);
2531 /* FIXME: We really need to calculate the overall height here, not the
2532 * page height. We assume there's always a vertical scrollbar for
2533 * now. We need to fix this. */
2534 if (view->sizing_mode == EV_SIZING_FIT_WIDTH)
2535 scale = zoom_for_size_fit_width (doc_width, doc_height, width - vsb_width, height, 0);
2536 else if (view->sizing_mode == EV_SIZING_BEST_FIT)
2537 scale = zoom_for_size_best_fit (doc_width, doc_height, width - vsb_width, height, 0, hsb_height);
2539 g_assert_not_reached ();
2541 ev_view_set_zoom (view, scale, FALSE);
2545 ev_view_zoom_for_size_dual_page (EvView *view,
2552 gint doc_width, doc_height;
2556 other_page = view->current_page ^ 1;
2558 /* Find the largest of the two. */
2559 ev_page_cache_get_size (view->page_cache,
2563 &doc_width, &doc_height);
2565 if (other_page < ev_page_cache_get_n_pages (view->page_cache)) {
2566 gint width_2, height_2;
2567 ev_page_cache_get_size (view->page_cache,
2571 &width_2, &height_2);
2572 if (width_2 > doc_width)
2573 doc_width = width_2;
2574 if (height_2 > doc_height)
2575 doc_height = height_2;
2577 compute_border (view, doc_width, doc_height, &border);
2579 doc_width = doc_width * 2;
2580 width -= ((border.left + border.right)* 2 + 3 * view->spacing);
2581 height -= (border.top + border.bottom + 2 * view->spacing);
2583 if (view->sizing_mode == EV_SIZING_FIT_WIDTH)
2584 scale = zoom_for_size_fit_width (doc_width, doc_height, width, height, vsb_width);
2585 else if (view->sizing_mode == EV_SIZING_BEST_FIT)
2586 scale = zoom_for_size_best_fit (doc_width, doc_height, width, height, vsb_width, hsb_height);
2588 g_assert_not_reached ();
2590 ev_view_set_zoom (view, scale, FALSE);
2594 ev_view_zoom_for_size_single_page (EvView *view,
2600 int doc_width, doc_height;
2604 ev_page_cache_get_size (view->page_cache,
2610 /* Get an approximate border */
2611 compute_border (view, width, height, &border);
2613 width -= (border.left + border.right + 2 * view->spacing);
2614 height -= (border.top + border.bottom + 2 * view->spacing);
2616 if (view->sizing_mode == EV_SIZING_FIT_WIDTH)
2617 scale = zoom_for_size_fit_width (doc_width, doc_height, width, height, vsb_width);
2618 else if (view->sizing_mode == EV_SIZING_BEST_FIT)
2619 scale = zoom_for_size_best_fit (doc_width, doc_height, width, height, vsb_width, hsb_height);
2621 g_assert_not_reached ();
2623 ev_view_set_zoom (view, scale, FALSE);
2627 ev_view_set_zoom_for_size (EvView *view,
2633 g_return_if_fail (view->sizing_mode == EV_SIZING_FIT_WIDTH ||
2634 view->sizing_mode == EV_SIZING_BEST_FIT);
2635 g_return_if_fail (width >= 0);
2636 g_return_if_fail (height >= 0);
2638 if (view->document == NULL)
2641 if (view->presentation)
2642 ev_view_zoom_for_size_presentation (view, width, height);
2643 else if (view->continuous && view->dual_page)
2644 ev_view_zoom_for_size_continuous_and_dual_page (view, width, height, vsb_width, hsb_height);
2645 else if (view->continuous)
2646 ev_view_zoom_for_size_continuous (view, width, height, vsb_width, hsb_height);
2647 else if (view->dual_page)
2648 ev_view_zoom_for_size_dual_page (view, width, height, vsb_width, hsb_height);
2650 ev_view_zoom_for_size_single_page (view, width, height, vsb_width, hsb_height);
2653 /*** Status text messages ***/
2656 ev_view_get_status (EvView *view)
2658 g_return_val_if_fail (EV_IS_VIEW (view), NULL);
2660 return view->status;
2664 ev_view_set_status (EvView *view, const char *message)
2666 g_return_if_fail (EV_IS_VIEW (view));
2668 if (message != view->status) {
2669 g_free (view->status);
2670 view->status = g_strdup (message);
2671 g_object_notify (G_OBJECT (view), "status");
2676 update_find_status_message (EvView *view)
2680 if (view->current_page == view->find_page) {
2683 results = ev_document_find_get_n_results
2684 (EV_DOCUMENT_FIND (view->document),
2685 view->current_page);
2686 /* TRANS: Sometimes this could be better translated as
2687 "%d hit(s) on this page". Therefore this string
2688 contains plural cases. */
2689 message = g_strdup_printf (ngettext ("%d found on this page",
2690 "%d found on this page",
2696 percent = ev_document_find_get_progress
2697 (EV_DOCUMENT_FIND (view->document));
2698 if (percent >= (1.0 - 1e-10)) {
2699 message = g_strdup (_("Not found"));
2701 message = g_strdup_printf (_("%3d%% remaining to search"),
2702 (int) ((1.0 - percent) * 100));
2706 ev_view_set_find_status (view, message);
2711 ev_view_get_find_status (EvView *view)
2713 g_return_val_if_fail (EV_IS_VIEW (view), NULL);
2715 return view->find_status;
2719 ev_view_set_find_status (EvView *view, const char *message)
2721 g_return_if_fail (EV_IS_VIEW (view));
2723 g_free (view->find_status);
2724 view->find_status = g_strdup (message);
2725 g_object_notify (G_OBJECT (view), "find-status");
2731 jump_to_find_result (EvView *view)
2733 EvDocumentFind *find = EV_DOCUMENT_FIND (view->document);
2735 GdkRectangle view_rect;
2737 int page = view->find_page;
2739 n_results = ev_document_find_get_n_results (find, page);
2741 if (n_results > view->find_result) {
2742 ev_document_find_get_result
2743 (find, page, view->find_result, &rect);
2745 doc_rect_to_view_rect (view, page, &rect, &view_rect);
2746 ensure_rectangle_is_visible (view, &view_rect);
2751 jump_to_find_page (EvView *view)
2755 n_pages = ev_page_cache_get_n_pages (view->page_cache);
2757 for (i = 0; i < n_pages; i++) {
2761 page = i + view->find_page;
2762 if (page >= n_pages) {
2763 page = page - n_pages;
2766 has_results = ev_document_find_page_has_results
2767 (EV_DOCUMENT_FIND (view->document), page);
2768 if (has_results == -1) {
2769 view->find_page = page;
2771 } else if (has_results == 1) {
2772 ev_page_cache_set_current_page (view->page_cache, page);
2773 jump_to_find_result (view);
2780 ev_view_can_find_next (EvView *view)
2784 if (EV_IS_DOCUMENT_FIND (view->document)) {
2785 EvDocumentFind *find = EV_DOCUMENT_FIND (view->document);
2787 n_results = ev_document_find_get_n_results (find, view->current_page);
2790 return n_results > 0;
2794 ev_view_find_next (EvView *view)
2796 int n_results, n_pages;
2797 EvDocumentFind *find = EV_DOCUMENT_FIND (view->document);
2799 n_results = ev_document_find_get_n_results (find, view->current_page);
2801 n_pages = ev_page_cache_get_n_pages (view->page_cache);
2803 view->find_result++;
2805 if (view->find_result >= n_results) {
2806 view->find_result = 0;
2809 if (view->find_page >= n_pages) {
2810 view->find_page = 0;
2813 jump_to_find_page (view);
2815 jump_to_find_result (view);
2816 gtk_widget_queue_draw (GTK_WIDGET (view));
2821 ev_view_find_previous (EvView *view)
2823 int n_results, n_pages;
2824 EvDocumentFind *find = EV_DOCUMENT_FIND (view->document);
2825 EvPageCache *page_cache;
2827 page_cache = ev_page_cache_get (view->document);
2829 n_results = ev_document_find_get_n_results (find, view->current_page);
2831 n_pages = ev_page_cache_get_n_pages (page_cache);
2833 view->find_result--;
2835 if (view->find_result < 0) {
2836 view->find_result = 0;
2839 if (view->find_page < 0) {
2840 view->find_page = n_pages - 1;
2843 jump_to_find_page (view);
2845 jump_to_find_result (view);
2846 gtk_widget_queue_draw (GTK_WIDGET (view));
2850 /*** Selections ***/
2852 /* compute_new_selection_rect/text calculates the area currently selected by
2853 * view_rect. each handles a different mode;
2856 compute_new_selection_rect (EvView *view,
2860 GdkRectangle view_rect;
2864 g_assert (view->selection_mode == EV_VIEW_SELECTION_RECTANGLE);
2866 view_rect.x = MIN (start->x, stop->x);
2867 view_rect.y = MIN (start->y, stop->y);
2868 view_rect.width = MAX (start->x, stop->x) - view_rect.x;
2869 view_rect.width = MAX (start->y, stop->y) - view_rect.y;
2871 n_pages = ev_page_cache_get_n_pages (view->page_cache);
2873 for (i = 0; i < n_pages; i++) {
2874 GdkRectangle page_area;
2877 if (get_page_extents (view, i, &page_area, &border)) {
2878 GdkRectangle overlap;
2880 if (gdk_rectangle_intersect (&page_area, &view_rect, &overlap)) {
2881 EvViewSelection *selection;
2883 selection = g_new0 (EvViewSelection, 1);
2884 selection->page = i;
2885 view_rect_to_doc_rect (view, &overlap, &page_area,
2886 &(selection->rect));
2888 list = g_list_append (list, selection);
2897 gdk_rectangle_point_in (GdkRectangle *rectangle,
2900 return rectangle->x <= point->x &&
2901 rectangle->y <= point->y &&
2902 point->x < rectangle->x + rectangle->width &&
2903 point->y < rectangle->y + rectangle->height;
2907 compute_new_selection_text (EvView *view,
2911 int n_pages, i, first, last;
2913 EvViewSelection *selection;
2916 g_assert (view->selection_mode == EV_VIEW_SELECTION_TEXT);
2918 n_pages = ev_page_cache_get_n_pages (view->page_cache);
2920 /* First figure out the range of pages the selection
2924 for (i = 0; i < n_pages; i++) {
2925 GdkRectangle page_area;
2928 get_page_extents (view, i, &page_area, &border);
2929 if (gdk_rectangle_point_in (&page_area, start) ||
2930 gdk_rectangle_point_in (&page_area, stop)) {
2931 if (first == n_pages)
2940 /* Now create a list of EvViewSelection's for the affected
2941 * pages. This could be an empty list, a list of just one
2942 * page or a number of pages.*/
2943 for (i = first; i < last + 1; i++) {
2944 GdkRectangle page_area;
2948 ev_page_cache_get_size (view->page_cache, i,
2950 1.0, &width, &height);
2952 selection = g_new0 (EvViewSelection, 1);
2953 selection->page = i;
2954 selection->rect.x1 = selection->rect.y1 = 0;
2955 selection->rect.x2 = width;
2956 selection->rect.y2 = height;
2958 get_page_extents (view, i, &page_area, &border);
2960 if (gdk_rectangle_point_in (&page_area, start))
2966 view_point_to_doc_point (view, point, &page_area,
2967 &selection->rect.x1,
2968 &selection->rect.y1);
2970 /* If the selection is contained within just one page,
2971 * make sure we don't write 'start' into both points
2972 * in selection->rect. */
2977 view_point_to_doc_point (view, point, &page_area,
2978 &selection->rect.x2,
2979 &selection->rect.y2);
2981 list = g_list_append (list, selection);
2987 /* This function takes the newly calculated list, and figures out which regions
2988 * have changed. It then queues a redraw approporiately.
2991 merge_selection_region (EvView *view,
2995 GList *new_list_ptr, *old_list_ptr;
2997 /* Update the selection */
2998 old_list = ev_pixbuf_cache_get_selection_list (view->pixbuf_cache);
2999 g_list_foreach (view->selection_info.selections, (GFunc)selection_free, NULL);
3000 view->selection_info.selections = new_list;
3001 ev_pixbuf_cache_set_selection_list (view->pixbuf_cache, new_list);
3003 new_list_ptr = new_list;
3004 old_list_ptr = old_list;
3006 while (new_list_ptr || old_list_ptr) {
3007 EvViewSelection *old_sel, *new_sel;
3009 GdkRegion *region = NULL;
3011 new_sel = (new_list_ptr) ? (new_list_ptr->data) : NULL;
3012 old_sel = (old_list_ptr) ? (old_list_ptr->data) : NULL;
3014 /* Assume that the lists are in order, and we run through them
3015 * comparing them, one page at a time. We come out with the
3016 * first page we see. */
3017 if (new_sel && old_sel) {
3018 if (new_sel->page < old_sel->page) {
3019 new_list_ptr = new_list_ptr->next;
3021 } else if (new_sel->page > old_sel->page) {
3022 old_list_ptr = old_list_ptr->next;
3025 new_list_ptr = new_list_ptr->next;
3026 old_list_ptr = old_list_ptr->next;
3028 } else if (new_sel) {
3029 new_list_ptr = new_list_ptr->next;
3030 } else if (old_sel) {
3031 old_list_ptr = old_list_ptr->next;
3034 g_assert (new_sel || old_sel);
3036 /* is the page we're looking at on the screen?*/
3037 cur_page = new_sel ? new_sel->page : old_sel->page;
3038 if (cur_page < view->start_page || cur_page > view->end_page)
3041 /* seed the cache with a new page. We are going to need the new
3044 GdkRegion *tmp_region = NULL;
3045 ev_pixbuf_cache_get_selection_pixbuf (view->pixbuf_cache,
3050 new_sel->covered_region = gdk_region_copy (tmp_region);
3054 /* Now we figure out what needs redrawing */
3055 if (old_sel && new_sel) {
3056 if (old_sel->covered_region &&
3057 new_sel->covered_region) {
3058 /* We only want to redraw the areas that have
3059 * changed, so we xor the old and new regions
3060 * and redraw if it's different */
3061 region = gdk_region_copy (old_sel->covered_region);
3062 gdk_region_xor (region, new_sel->covered_region);
3063 if (gdk_region_empty (region)) {
3064 gdk_region_destroy (region);
3067 } else if (old_sel->covered_region) {
3068 region = gdk_region_copy (old_sel->covered_region);
3069 } else if (new_sel->covered_region) {
3070 region = gdk_region_copy (new_sel->covered_region);
3072 } else if (old_sel && !new_sel) {
3073 if (old_sel->covered_region && !gdk_region_empty (old_sel->covered_region)) {
3074 region = gdk_region_copy (old_sel->covered_region);
3076 } else if (!old_sel && new_sel) {
3077 if (new_sel->covered_region && !gdk_region_empty (new_sel->covered_region)) {
3078 region = gdk_region_copy (new_sel->covered_region);
3081 g_assert_not_reached ();
3084 /* Redraw the damaged region! */
3086 GdkRectangle page_area;
3089 get_page_extents (view, cur_page, &page_area, &border);
3090 gdk_region_offset (region,
3091 page_area.x + border.left - view->scroll_x,
3092 page_area.y + border.top - view->scroll_y);
3093 gdk_window_invalidate_region (GTK_WIDGET (view)->window, region, TRUE);
3094 gdk_region_destroy (region);
3098 /* Free the old list, now that we're done with it. */
3099 g_list_foreach (old_list, (GFunc) selection_free, NULL);
3103 compute_selections (EvView *view,
3109 if (view->selection_mode == EV_VIEW_SELECTION_RECTANGLE)
3110 list = compute_new_selection_rect (view, start, stop);
3112 list = compute_new_selection_text (view, start, stop);
3113 merge_selection_region (view, list);
3116 /* Free's the selection. It's up to the caller to queue redraws if needed.
3119 selection_free (EvViewSelection *selection)
3121 if (selection->covered_region)
3122 gdk_region_destroy (selection->covered_region);
3127 clear_selection (EvView *view)
3129 g_list_foreach (view->selection_info.selections, (GFunc)selection_free, NULL);
3130 view->selection_info.selections = NULL;
3131 view->selection_info.in_selection = FALSE;
3136 ev_view_select_all (EvView *view)
3140 clear_selection (view);
3142 n_pages = ev_page_cache_get_n_pages (view->page_cache);
3143 for (i = 0; i < n_pages; i++) {
3145 EvViewSelection *selection;
3147 ev_page_cache_get_size (view->page_cache,
3149 i, 1.0, &width, &height);
3151 selection = g_new0 (EvViewSelection, 1);
3152 selection->page = i;
3153 selection->rect.x1 = selection->rect.y1 = 0;
3154 selection->rect.x2 = width;
3155 selection->rect.y2 = height;
3157 view->selection_info.selections = g_list_append (view->selection_info.selections, selection);
3160 ev_pixbuf_cache_set_selection_list (view->pixbuf_cache, view->selection_info.selections);
3161 gtk_widget_queue_draw (GTK_WIDGET (view));
3165 get_selected_text (EvView *ev_view)
3170 text = g_string_new (NULL);
3172 ev_document_doc_mutex_lock ();
3174 for (l = ev_view->selection_info.selections; l != NULL; l = l->next) {
3175 EvViewSelection *selection = (EvViewSelection *)l->data;
3178 tmp = ev_document_get_text (ev_view->document,
3181 g_string_append (text, tmp);
3185 ev_document_doc_mutex_unlock ();
3187 return g_string_free (text, FALSE);
3191 ev_view_copy (EvView *ev_view)
3193 GtkClipboard *clipboard;
3196 if (!ev_document_can_get_text (ev_view->document)) {
3200 text = get_selected_text (ev_view);
3201 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (ev_view),
3202 GDK_SELECTION_CLIPBOARD);
3203 gtk_clipboard_set_text (clipboard, text, -1);
3208 ev_view_primary_get_cb (GtkClipboard *clipboard,
3209 GtkSelectionData *selection_data,
3213 EvView *ev_view = EV_VIEW (data);
3216 if (!ev_document_can_get_text (ev_view->document)) {
3220 text = get_selected_text (ev_view);
3221 gtk_selection_data_set_text (selection_data, text, -1);
3226 ev_view_primary_clear_cb (GtkClipboard *clipboard,
3229 EvView *view = EV_VIEW (data);
3231 clear_selection (view);
3235 ev_view_update_primary_selection (EvView *ev_view)
3237 GtkClipboard *clipboard;
3239 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (ev_view),
3240 GDK_SELECTION_PRIMARY);
3242 if (ev_view->selection_info.selections) {
3243 if (!gtk_clipboard_set_with_owner (clipboard,
3245 G_N_ELEMENTS (targets),
3246 ev_view_primary_get_cb,
3247 ev_view_primary_clear_cb,
3248 G_OBJECT (ev_view)))
3249 ev_view_primary_clear_cb (clipboard, ev_view);
3251 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (ev_view))
3252 gtk_clipboard_clear (clipboard);
3256 /*** Cursor operations ***/
3259 ev_view_create_invisible_cursor(void)
3262 GdkColor black = { 0, 0, 0, 0 };
3263 static char bits[] = { 0x00 };
3265 empty = gdk_bitmap_create_from_data (NULL, bits, 1, 1);
3267 return gdk_cursor_new_from_pixmap (empty, empty, &black, &black, 0, 0);
3271 ev_view_set_cursor (EvView *view, EvViewCursor new_cursor)
3273 GdkCursor *cursor = NULL;
3274 GdkDisplay *display;
3277 if (view->cursor == new_cursor) {
3281 widget = gtk_widget_get_toplevel (GTK_WIDGET (view));
3282 display = gtk_widget_get_display (widget);
3283 view->cursor = new_cursor;
3285 switch (new_cursor) {
3286 case EV_VIEW_CURSOR_NORMAL:
3287 gdk_window_set_cursor (widget->window, NULL);
3289 case EV_VIEW_CURSOR_IBEAM:
3290 cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
3292 case EV_VIEW_CURSOR_LINK:
3293 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
3295 case EV_VIEW_CURSOR_WAIT:
3296 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
3298 case EV_VIEW_CURSOR_HIDDEN:
3299 cursor = ev_view_create_invisible_cursor ();
3301 case EV_VIEW_CURSOR_DRAG:
3302 cursor = gdk_cursor_new_for_display (display, GDK_FLEUR);
3307 gdk_window_set_cursor (widget->window, cursor);
3308 gdk_cursor_unref (cursor);
3314 ev_view_hide_cursor (EvView *view)
3316 ev_view_set_cursor (view, EV_VIEW_CURSOR_HIDDEN);
3320 ev_view_show_cursor (EvView *view)
3322 ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL);
3325 /*** Enum description for usage in signal ***/
3328 ev_sizing_mode_get_type (void)
3330 static GType etype = 0;
3332 static const GEnumValue values[] = {
3333 { EV_SIZING_FIT_WIDTH, "EV_SIZING_FIT_WIDTH", "fit-width" },
3334 { EV_SIZING_BEST_FIT, "EV_SIZING_BEST_FIT", "best-fit" },
3335 { EV_SIZING_FREE, "EV_SIZING_FREE", "free" },
3338 etype = g_enum_register_static ("EvSizingMode", values);