ev-view.c 180 KB
Newer Older
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
Owen Taylor's avatar
Owen Taylor committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* this file is part of evince, a gnome document viewer
 *
 *  Copyright (C) 2004 Red Hat, Inc
 *
 * Evince is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Evince is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

21 22
#include "config.h"

23
#include <stdlib.h>
24
#include <math.h>
25
#include <string.h>
26

27
#include <glib/gi18n-lib.h>
28
#include <gtk/gtk.h>
29
#include <gdk/gdkkeysyms.h>
Owen Taylor's avatar
Owen Taylor committed
30

31
#include "ev-mapping.h"
32
#include "ev-document-forms.h"
33 34
#include "ev-document-images.h"
#include "ev-document-links.h"
35
#include "ev-document-misc.h"
36
#include "ev-document-transition.h"
37
#include "ev-pixbuf-cache.h"
38
#include "ev-transition-animation.h"
39
#include "ev-view-marshal.h"
40 41
#include "ev-document-annotations.h"
#include "ev-annotation-window.h"
42
#include "ev-view.h"
43
#include "ev-view-accessible.h"
44
#include "ev-view-private.h"
45
#include "ev-view-type-builtins.h"
46

Owen Taylor's avatar
Owen Taylor committed
47 48 49 50
#define EV_VIEW_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), EV_TYPE_VIEW, EvViewClass))
#define EV_IS_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), EV_TYPE_VIEW))
#define EV_VIEW_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), EV_TYPE_VIEW, EvViewClass))

51 52
enum {
	PROP_0,
53 54
	PROP_CONTINUOUS,
	PROP_DUAL_PAGE,
55
	PROP_FULLSCREEN,
56 57
	PROP_PRESENTATION,
	PROP_SIZING_MODE,
58
	PROP_ZOOM,
59
	PROP_ROTATION,
60
	PROP_HAS_SELECTION,
61 62
};

63
enum {
64
	SIGNAL_BINDING_ACTIVATED,
65
	SIGNAL_HANDLE_LINK,
66
	SIGNAL_EXTERNAL_LINK,
67
	SIGNAL_POPUP_MENU,
68 69 70
	N_SIGNALS,
};

71 72 73 74 75 76
enum {
	TARGET_DND_URI,
	TARGET_DND_TEXT,
	TARGET_DND_IMAGE
};

77 78
static guint signals[N_SIGNALS];

79 80 81 82 83
typedef enum {
	EV_VIEW_FIND_NEXT,
	EV_VIEW_FIND_PREV
} EvViewFindDirection;

84 85 86
#define ZOOM_IN_FACTOR  1.2
#define ZOOM_OUT_FACTOR (1.0/ZOOM_IN_FACTOR)

87 88
#define SCROLL_TIME 150

89
/*** Scrolling ***/
90
static void       ev_view_set_scroll_adjustments             (GtkLayout          *layout,
91 92 93 94 95 96 97 98
							      GtkAdjustment      *hadjustment,
							      GtkAdjustment      *vadjustment);
static void       view_update_range_and_current_page         (EvView             *view);
static void       set_scroll_adjustment                      (EvView             *view,
							      GtkOrientation      orientation,
							      GtkAdjustment      *adjustment);
static void       add_scroll_binding_keypad                  (GtkBindingSet      *binding_set,
							      guint               keyval,
99
							      GdkModifierType modifiers,
100
							      GtkScrollType       scroll,
101 102 103 104 105 106 107 108 109
							      gboolean            horizontal);
static void       ensure_rectangle_is_visible                (EvView             *view,
							      GdkRectangle       *rect);

/*** Geometry computations ***/
static void       compute_border                             (EvView             *view,
							      int                 width,
							      int                 height,
							      GtkBorder          *border);
110 111 112
static void       get_page_y_offset                          (EvView             *view,
							      int                 page,
							      int                *y_offset);
113 114 115 116 117 118 119 120 121 122 123 124
static gboolean   get_page_extents                           (EvView             *view,
							      gint                page,
							      GdkRectangle       *page_area,
							      GtkBorder          *border);
static void       view_rect_to_doc_rect                      (EvView             *view,
							      GdkRectangle       *view_rect,
							      GdkRectangle       *page_area,
							      EvRectangle        *doc_rect);
static void       doc_rect_to_view_rect                      (EvView             *view,
							      int                 page,
							      EvRectangle        *doc_rect,
							      GdkRectangle       *view_rect);
125
static gboolean   get_dual_even_left                         (EvView             *view);
126 127 128 129 130 131
static void       find_page_at_location                      (EvView             *view,
							      gdouble             x,
							      gdouble             y,
							      gint               *page,
							      gint               *x_offset,
							      gint               *y_offset);
132
static void       doc_point_to_view_point 		     (EvView             *view,
133 134 135
				                              int                 page,
							      EvPoint            *doc_point,
					     	              GdkPoint           *view_point);
136
/*** Hyperrefs ***/
137 138 139
static EvLink *   ev_view_get_link_at_location 		     (EvView             *view,
				  	         	      gdouble             x,
		            				      gdouble             y);
140
static char*      tip_from_link                              (EvView             *view,
141
							      EvLink             *link);
142 143 144 145 146
/*** Forms ***/
static EvFormField *ev_view_get_form_field_at_location       (EvView             *view,
							       gdouble            x,
							       gdouble            y);

147 148 149 150 151 152 153 154
/*** Annotations ***/
static EvAnnotation *ev_view_get_annotation_at_location      (EvView             *view,
							      gdouble             x,
							      gdouble             y);
static void          show_annotation_windows                 (EvView             *view,
							      gint                page);
static void          hide_annotation_windows                 (EvView             *view,
							      gint                page);
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
/*** GtkWidget implementation ***/
static void       ev_view_size_request_continuous_dual_page  (EvView             *view,
							      GtkRequisition     *requisition);
static void       ev_view_size_request_continuous            (EvView             *view,
							      GtkRequisition     *requisition);
static void       ev_view_size_request_dual_page             (EvView             *view,
							      GtkRequisition     *requisition);
static void       ev_view_size_request_single_page           (EvView             *view,
							      GtkRequisition     *requisition);
static void       ev_view_size_request                       (GtkWidget          *widget,
							      GtkRequisition     *requisition);
static void       ev_view_size_allocate                      (GtkWidget          *widget,
							      GtkAllocation      *allocation);
static void       ev_view_realize                            (GtkWidget          *widget);
static gboolean   ev_view_scroll_event                       (GtkWidget          *widget,
							      GdkEventScroll     *event);
static gboolean   ev_view_expose_event                       (GtkWidget          *widget,
							      GdkEventExpose     *event);
173
static gboolean   ev_view_popup_menu                         (GtkWidget 	 *widget);
174 175 176 177 178 179
static gboolean   ev_view_button_press_event                 (GtkWidget          *widget,
							      GdkEventButton     *event);
static gboolean   ev_view_motion_notify_event                (GtkWidget          *widget,
							      GdkEventMotion     *event);
static gboolean   ev_view_button_release_event               (GtkWidget          *widget,
							      GdkEventButton     *event);
180 181
static gboolean   ev_view_enter_notify_event                 (GtkWidget          *widget,
							      GdkEventCrossing   *event);
182 183
static gboolean   ev_view_leave_notify_event                 (GtkWidget          *widget,
							      GdkEventCrossing   *event);
184 185
static void       ev_view_style_set                          (GtkWidget          *widget,
							      GtkStyle           *old_style);
186
static void       ev_view_remove_all                         (EvView             *view);
187

188 189
static AtkObject *ev_view_get_accessible                     (GtkWidget *widget);

190 191 192 193 194 195 196 197 198 199
/*** Drawing ***/
static guint32    ev_gdk_color_to_rgb                        (const GdkColor     *color);
static void       draw_rubberband                            (GtkWidget          *widget,
							      GdkWindow          *window,
							      const GdkRectangle *rect,
							      guchar              alpha);
static void       highlight_find_results                     (EvView             *view,
							      int                 page);
static void       draw_one_page                              (EvView             *view,
							      gint                page,
200
							      cairo_t            *cr,
201 202
							      GdkRectangle       *page_area,
							      GtkBorder          *border,
203 204
							      GdkRectangle       *expose_area,
							      gboolean		 *page_ready);
205 206 207
static void	  draw_loading_text 			     (EvView             *view,
							      GdkRectangle       *page_area,
							      GdkRectangle       *expose_area);
Carlos Garcia Campos's avatar
Carlos Garcia Campos committed
208 209 210
static void       ev_view_reload_page                        (EvView             *view,
							      gint                page,
							      GdkRegion          *region);
211 212

/*** Callbacks ***/
213 214 215
static void       ev_view_change_page                        (EvView             *view,
							      gint                new_page,
							      gboolean            start_transition);
216
static void       job_finished_cb                            (EvPixbufCache      *pixbuf_cache,
217
							      GdkRegion          *region,
218
							      EvView             *view);
219 220 221
static void       ev_view_page_changed_cb                    (EvDocumentModel    *model,
							      gint                old_page,
							      gint                new_page,
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
							      EvView             *view);
static void       on_adjustment_value_changed                (GtkAdjustment      *adjustment,
							      EvView             *view);

/*** GObject ***/
static void       ev_view_finalize                           (GObject            *object);
static void       ev_view_destroy                            (GtkObject          *object);
static void       ev_view_set_property                       (GObject            *object,
							      guint               prop_id,
							      const GValue       *value,
							      GParamSpec         *pspec);
static void       ev_view_get_property                       (GObject            *object,
							      guint               prop_id,
							      GValue             *value,
							      GParamSpec         *pspec);
static void       ev_view_class_init                         (EvViewClass        *class);
static void       ev_view_init                               (EvView             *view);

/*** Zoom and sizing ***/
241 242 243
static double   zoom_for_size_fit_width	 		     (gdouble doc_width,
							      gdouble doc_height,
	    						      int     target_width,
244
							      int     target_height);
245 246 247
static double   zoom_for_size_fit_height		     (gdouble doc_width,
			  				      gdouble doc_height,
							      int     target_width,
248
							      int     target_height);
249 250 251
static double	zoom_for_size_best_fit 			     (gdouble doc_width,
							      gdouble doc_height,
							      int     target_width,
252 253 254 255
							      int     target_height);
static void     ev_view_zoom_for_size                        (EvView *view,
							      int     width,
							      int     height);
256 257 258 259 260
static void	ev_view_zoom_for_size_presentation	     (EvView *view,
							      int     width,
					    		      int     height);
static void	ev_view_zoom_for_size_continuous_and_dual_page (EvView *view,
							        int     width,
261
						     	        int     height);
262 263
static void	ev_view_zoom_for_size_continuous	       (EvView *view,
					    		        int     width,
264
								int     height);
265 266
static void 	ev_view_zoom_for_size_dual_page 	       (EvView *view,
						    		int     width,
267
								int     height);
268 269
static void	ev_view_zoom_for_size_single_page 	       (EvView *view,
				    			        int     width,
270
					    			int     height);
271 272 273 274
/*** Cursors ***/
static GdkCursor* ev_view_create_invisible_cursor            (void);
static void       ev_view_set_cursor                         (EvView             *view,
							      EvViewCursor        new_cursor);
275 276 277
static void       ev_view_handle_cursor_over_xy              (EvView *view,
							      gint x,
							      gint y);
278

279
/*** Find ***/
Carlos Garcia Campos's avatar
Removed  
Carlos Garcia Campos committed
280 281 282 283 284
static gint         ev_view_find_get_n_results               (EvView             *view,
							      gint                page);
static EvRectangle *ev_view_find_get_result                  (EvView             *view,
							      gint                page,
							      gint                result);
285
static void       jump_to_find_result                        (EvView             *view);
286
static void       jump_to_find_page                          (EvView             *view, 
287 288
							      EvViewFindDirection direction,
							      gint                shift);
289 290 291

/*** Selection ***/
static void       compute_selections                         (EvView             *view,
292
							      EvSelectionStyle    style,
293 294
							      GdkPoint           *start,
							      GdkPoint           *stop);
295
static void       clear_selection                            (EvView             *view);
296
static void       clear_link_selected                        (EvView             *view);
297
static void       selection_free                             (EvViewSelection    *selection);
298 299 300 301 302 303 304 305 306
static char*      get_selected_text                          (EvView             *ev_view);
static void       ev_view_primary_get_cb                     (GtkClipboard       *clipboard,
							      GtkSelectionData   *selection_data,
							      guint               info,
							      gpointer            data);
static void       ev_view_primary_clear_cb                   (GtkClipboard       *clipboard,
							      gpointer            data);
static void       ev_view_update_primary_selection           (EvView             *ev_view);

307 308 309 310
/*** Presentation ***/
static void       ev_view_presentation_transition_start      (EvView             *ev_view);
static void       ev_view_presentation_transition_stop       (EvView             *ev_view);

311

312
G_DEFINE_TYPE (EvView, ev_view, GTK_TYPE_LAYOUT)
Owen Taylor's avatar
Owen Taylor committed
313

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
/* HeightToPage cache */
#define EV_HEIGHT_TO_PAGE_CACHE_KEY "ev-height-to-page-cache"

static void
build_height_to_page (EvHeightToPageCache *cache,
		      EvDocument          *document,
		      gint                 rotation)
{
	gboolean swap, uniform, dual_even_left;
	int i;
	double uniform_height, page_height, next_page_height;
	double saved_height;
	gdouble u_width, u_height;
	gint n_pages;

	swap = (rotation == 90 || rotation == 270);

	uniform = ev_document_is_page_size_uniform (document);
	n_pages = ev_document_get_n_pages (document);
	dual_even_left = (n_pages > 2);

	g_free (cache->height_to_page);
	g_free (cache->dual_height_to_page);

	cache->rotation = rotation;
	cache->height_to_page = g_new0 (gdouble, n_pages + 1);
	cache->dual_height_to_page = g_new0 (gdouble, n_pages + 2);

	if (uniform)
		ev_document_get_page_size (document, 0, &u_width, &u_height);

	saved_height = 0;
	for (i = 0; i <= n_pages; i++) {
		if (uniform) {
			uniform_height = swap ? u_width : u_height;
			cache->height_to_page[i] = i * uniform_height;
		} else {
			if (i < n_pages) {
				gdouble w, h;

				ev_document_get_page_size (document, i, &w, &h);
				page_height = swap ? w : h;
			} else {
				page_height = 0;
			}
			cache->height_to_page[i] = saved_height;
			saved_height += page_height;
		}
	}

	if (dual_even_left && !uniform) {
		gdouble w, h;

		ev_document_get_page_size (document, 0, &w, &h);
		saved_height = swap ? w : h;
	} else {
		saved_height = 0;
	}

	for (i = dual_even_left; i < n_pages + 2; i += 2) {
    		if (uniform) {
			uniform_height = swap ? u_width : u_height;
			cache->dual_height_to_page[i] = ((i + dual_even_left) / 2) * uniform_height;
			if (i + 1 < n_pages + 2)
				cache->dual_height_to_page[i + 1] = ((i + dual_even_left) / 2) * uniform_height;
		} else {
			if (i + 1 < n_pages) {
				gdouble w, h;

				ev_document_get_page_size (document, i + 1, &w, &h);
				next_page_height = swap ? w : h;
			} else {
				next_page_height = 0;
			}

			if (i < n_pages) {
				gdouble w, h;

				ev_document_get_page_size (document, i, &w, &h);
				page_height = swap ? w : h;
			} else {
				page_height = 0;
			}

			if (i + 1 < n_pages + 2) {
				cache->dual_height_to_page[i] = saved_height;
				cache->dual_height_to_page[i + 1] = saved_height;
				saved_height += MAX(page_height, next_page_height);
			} else {
				cache->dual_height_to_page[i] = saved_height;
			}
		}
	}
}

static void
ev_height_to_page_cache_get_height (EvHeightToPageCache *cache,
				    EvDocument          *document,
				    gint                 page,
				    gint                 rotation,
				    gdouble             *height,
				    gdouble             *dual_height)
{
	if (cache->rotation != rotation)
		build_height_to_page (cache, document, rotation);

	*height = cache->height_to_page[page];
	*dual_height = cache->dual_height_to_page[page];
}

static void
ev_height_to_page_cache_free (EvHeightToPageCache *cache)
{
	if (cache->height_to_page) {
		g_free (cache->height_to_page);
		cache->height_to_page = NULL;
	}

	if (cache->dual_height_to_page) {
		g_free (cache->dual_height_to_page);
		cache->dual_height_to_page = NULL;
	}
	g_free (cache);
}

static EvHeightToPageCache *
ev_view_get_height_to_page_cache (EvView *view)
{
	EvHeightToPageCache *cache;

	if (!view->document)
		return NULL;

	cache = g_object_get_data (G_OBJECT (view->document), EV_HEIGHT_TO_PAGE_CACHE_KEY);
	if (!cache) {
		cache = g_new0 (EvHeightToPageCache, 1);
		build_height_to_page (cache, view->document, view->rotation);
		g_object_set_data_full (G_OBJECT (view->document),
					EV_HEIGHT_TO_PAGE_CACHE_KEY,
					cache,
					(GDestroyNotify)ev_height_to_page_cache_free);
	}

	return cache;
}

static void
ev_view_get_height_to_page (EvView *view,
			    gint    page,
			    gint   *height,
			    gint   *dual_height)
{
	gdouble h, dh;

	if (!view->height_to_page_cache)
		return;

	ev_height_to_page_cache_get_height (view->height_to_page_cache,
					    view->document,
					    page,
					    view->rotation,
					    &h, &dh);
	if (height)
		*height = (gint)(h * view->scale + 0.5);

	if (dual_height)
		*dual_height = (gint)(dh * view->scale + 0.5);
}

483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
static gint
ev_view_get_scrollbar_size (EvView        *view,
			    GtkOrientation orientation)
{
	GtkWidget *widget = GTK_WIDGET (view);
	GtkWidget *sb;
	GtkWidget *swindow = gtk_widget_get_parent (GTK_WIDGET (view));
	GtkRequisition req;
	gint spacing;

	if (!GTK_IS_SCROLLED_WINDOW (swindow))
		return 0;

	if (orientation == GTK_ORIENTATION_VERTICAL) {
		if (widget->allocation.height >= widget->requisition.height)
			sb = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (swindow));
		else
			return 0;
	} else {
		if (widget->allocation.width >= widget->requisition.width)
			sb = gtk_scrolled_window_get_hscrollbar (GTK_SCROLLED_WINDOW (swindow));
		else
			return 0;
	}

	gtk_widget_style_get (swindow, "scrollbar_spacing", &spacing, NULL);
	gtk_widget_size_request (sb, &req);

	return (orientation == GTK_ORIENTATION_VERTICAL ? req.width : req.height) + spacing;
}

Owen Taylor's avatar
Owen Taylor committed
514
static void
515 516 517 518
scroll_to_point (EvView        *view,
		 gdouble        x,
		 gdouble        y,
		 GtkOrientation orientation)
Owen Taylor's avatar
Owen Taylor committed
519
{
520
	if (orientation == GTK_ORIENTATION_VERTICAL) {
521 522
		if (view->continuous) {
    			gtk_adjustment_clamp_page (view->vadjustment,
523 524
						   y - view->spacing / 2,
						   y + view->vadjustment->page_size);
525 526
		} else {
			gtk_adjustment_set_value (view->vadjustment,
527
						  CLAMP (y,
528 529 530
						  view->vadjustment->lower,
						  view->vadjustment->upper -
						  view->vadjustment->page_size));
531 532 533
		}
	} else {
		if (view->dual_page) {
534 535
			gtk_adjustment_clamp_page (view->hadjustment, x,
						   x + view->hadjustment->page_size);
536 537
		} else {
			gtk_adjustment_set_value (view->hadjustment,
538
						  CLAMP (x,
539
						  view->hadjustment->lower,
540
						  view->hadjustment->upper -
541 542 543
						  view->hadjustment->page_size));
		}
	}
Owen Taylor's avatar
Owen Taylor committed
544 545
}

546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
static void
ev_view_scroll_to_page_position (EvView *view, GtkOrientation orientation)
{
	gdouble x, y;

	if (!view->document)
		return;

	if ((orientation == GTK_ORIENTATION_VERTICAL && view->pending_point.y == 0) ||
	    (orientation == GTK_ORIENTATION_HORIZONTAL && view->pending_point.x == 0)) {
		GdkRectangle page_area;
		GtkBorder    border;

		get_page_extents (view, view->current_page, &page_area, &border);
		x = page_area.x;
		y = page_area.y;
	} else {
		GdkPoint view_point;

		doc_point_to_view_point (view, view->current_page,
					 &view->pending_point, &view_point);
		x = view_point.x;
		y = view_point.y;
	}

	scroll_to_point (view, x, y, orientation);
}

Owen Taylor's avatar
Owen Taylor committed
574 575 576 577 578 579 580 581 582
static void
view_set_adjustment_values (EvView         *view,
			    GtkOrientation  orientation)
{
	GtkWidget *widget = GTK_WIDGET (view);
	GtkAdjustment *adjustment;
	int requisition;
	int allocation;

583 584 585
	double factor;
	gint new_value;

Owen Taylor's avatar
Owen Taylor committed
586 587 588 589 590 591 592 593 594 595 596 597
	if (orientation == GTK_ORIENTATION_HORIZONTAL)  {
		requisition = widget->requisition.width;
		allocation = widget->allocation.width;
		adjustment = view->hadjustment;
	} else {
		requisition = widget->requisition.height;
		allocation = widget->allocation.height;
		adjustment = view->vadjustment;
	}

	if (!adjustment)
		return;
598

599 600
	factor = 1.0;
	switch (view->pending_scroll) {
601
    	        case SCROLL_TO_KEEP_POSITION:
602
    	        case SCROLL_TO_FIND_LOCATION:
603
			factor = (adjustment->value) / adjustment->upper;
604
			break;
605
    	        case SCROLL_TO_PAGE_POSITION:
606
			break;
607
    	        case SCROLL_TO_CENTER:
608
			factor = (adjustment->value + adjustment->page_size * 0.5) / adjustment->upper;
609 610
			break;
	}
611

Owen Taylor's avatar
Owen Taylor committed
612 613 614 615 616 617
	adjustment->page_size = allocation;
	adjustment->step_increment = allocation * 0.1;
	adjustment->page_increment = allocation * 0.9;
	adjustment->lower = 0;
	adjustment->upper = MAX (allocation, requisition);

618
	/*
619 620
	 * We add 0.5 to the values before to average out our rounding errors.
	 */
621
	switch (view->pending_scroll) {
622
    	        case SCROLL_TO_KEEP_POSITION:
623
    	        case SCROLL_TO_FIND_LOCATION:
624
			new_value = CLAMP (adjustment->upper * factor + 0.5, 0, adjustment->upper - adjustment->page_size);
625
			gtk_adjustment_set_value (adjustment, (int)new_value);
626
			break;
627
    	        case SCROLL_TO_PAGE_POSITION:
628
			ev_view_scroll_to_page_position (view, orientation);
629
			break;
630
    	        case SCROLL_TO_CENTER:
631
			new_value = CLAMP (adjustment->upper * factor - adjustment->page_size * 0.5 + 0.5,
632
					   0, adjustment->upper - adjustment->page_size);
633
			gtk_adjustment_set_value (adjustment, (int)new_value);
634
			break;
Owen Taylor's avatar
Owen Taylor committed
635 636 637 638 639
	}

	gtk_adjustment_changed (adjustment);
}

640 641 642
static void
view_update_range_and_current_page (EvView *view)
{
643 644
	gint start = view->start_page;
	gint end = view->end_page;
645
	
646 647 648 649 650 651
	/* Presentation trumps all other modes */
	if (view->presentation) {
		view->start_page = view->current_page;
		view->end_page = view->current_page;
	} else if (view->continuous) {
		GdkRectangle current_area, unused, page_area;
652
		GtkBorder border;
653
		gboolean found = FALSE;
654
		gint area_max = -1, area;
655
		gint best_current_page = -1;
656
		int i, j = 0;
657

658 659
		if (!(view->vadjustment && view->hadjustment))
			return;
660

661
		current_area.x = view->hadjustment->value;
662
		current_area.width = view->hadjustment->page_size;
663 664
		current_area.y = view->vadjustment->value;
		current_area.height = view->vadjustment->page_size;
665

666
		for (i = 0; i < ev_document_get_n_pages (view->document); i++) {
667 668

			get_page_extents (view, i, &page_area, &border);
669

670
			if (gdk_rectangle_intersect (&current_area, &page_area, &unused)) {
671 672 673 674
				area = unused.width * unused.height;

				if (!found) {
					area_max = area;
675 676
					view->start_page = i;
					found = TRUE;
677 678 679 680 681
					best_current_page = i;
				}
				if (area > area_max) {
					best_current_page = (area == area_max) ? MIN (i, best_current_page) : i;
					area_max = area;
682
				}
683

684
				view->end_page = i;
685
				j = 0;
686
			} else if (found && view->current_page <= view->end_page) {
687 688 689 690 691 692 693
				if (view->dual_page && j < 1) {
					/* In dual mode  we stop searching
					 * after two consecutive non-visible pages.
					 */
					j++;
					continue;
				}
694 695 696
				break;
			}
		}
697

698 699 700 701 702 703 704 705
		if (view->pending_scroll == SCROLL_TO_KEEP_POSITION) {
			best_current_page = MAX (best_current_page, view->start_page);

			if (view->current_page != best_current_page) {
				view->current_page = best_current_page;
				ev_document_model_set_page (view->model, best_current_page);
			}
		}
706
	} else if (view->dual_page) {
707
		if (view->current_page % 2 == get_dual_even_left (view)) {
708
			view->start_page = view->current_page;
709
			if (view->current_page + 1 < ev_document_get_n_pages (view->document))
710
				view->end_page = view->start_page + 1;
711
			else
712 713 714 715 716 717
				view->end_page = view->start_page;
		} else {
			if (view->current_page < 1)
				view->start_page = view->current_page;
			else
				view->start_page = view->current_page - 1;
718 719
			view->end_page = view->current_page;
		}
720 721 722
	} else {
		view->start_page = view->current_page;
		view->end_page = view->current_page;
723 724
	}

725 726 727 728 729 730 731 732 733 734 735
	if (start != view->start_page || end != view->end_page) {
		gint i;

		for (i = start; i < view->start_page; i++) {
			hide_annotation_windows (view, i);
		}

		for (i = end; i > view->end_page; i--) {
			hide_annotation_windows (view, i);
		}
	}
736 737 738 739 740 741 742

	ev_pixbuf_cache_set_page_range (view->pixbuf_cache,
					view->start_page,
					view->end_page,
					view->rotation,
					view->scale,
					view->selection_info.selections);
743 744 745

	if (ev_pixbuf_cache_get_surface (view->pixbuf_cache, view->current_page))
	    gtk_widget_queue_draw (GTK_WIDGET (view));
746 747
}

748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
static void
set_scroll_adjustment (EvView *view,
		       GtkOrientation  orientation,
		       GtkAdjustment  *adjustment)
{
	GtkAdjustment **to_set;

	if (orientation == GTK_ORIENTATION_HORIZONTAL)
		to_set = &view->hadjustment;
	else
		to_set = &view->vadjustment;

	if (*to_set != adjustment) {
		if (*to_set) {
			g_signal_handlers_disconnect_by_func (*to_set,
							      (gpointer) on_adjustment_value_changed,
							      view);
			g_object_unref (*to_set);
		}

		*to_set = adjustment;
		view_set_adjustment_values (view, orientation);

		if (*to_set) {
			g_object_ref (*to_set);
			g_signal_connect (*to_set, "value_changed",
					  G_CALLBACK (on_adjustment_value_changed), view);
		}
	}
}
778

Owen Taylor's avatar
Owen Taylor committed
779
static void
780
ev_view_set_scroll_adjustments (GtkLayout      *layout,
781 782
				GtkAdjustment  *hadjustment,
				GtkAdjustment  *vadjustment)
Owen Taylor's avatar
Owen Taylor committed
783
{
784 785
	EvView *view = EV_VIEW (layout);
	
786 787
	set_scroll_adjustment (view, GTK_ORIENTATION_HORIZONTAL, hadjustment);
	set_scroll_adjustment (view, GTK_ORIENTATION_VERTICAL, vadjustment);
788
	
789
	on_adjustment_value_changed (NULL, view);
790
}
Marco Pesenti Gritti's avatar
Marco Pesenti Gritti committed
791

792 793 794
static void
add_scroll_binding_keypad (GtkBindingSet  *binding_set,
    			   guint           keyval,
795
    			   GdkModifierType modifiers,
796
    			   GtkScrollType   scroll,
797 798 799
			   gboolean        horizontal)
{
  guint keypad_keyval = keyval - GDK_Left + GDK_KP_Left;
Owen Taylor's avatar
Owen Taylor committed
800

801
  gtk_binding_entry_add_signal (binding_set, keyval, modifiers,
802
                                "binding_activated", 2,
803
                                GTK_TYPE_SCROLL_TYPE, scroll,
804
				G_TYPE_BOOLEAN, horizontal);
805
  gtk_binding_entry_add_signal (binding_set, keypad_keyval, modifiers,
806
                                "binding_activated", 2,
807
                                GTK_TYPE_SCROLL_TYPE, scroll,
808 809
				G_TYPE_BOOLEAN, horizontal);
}
Marco Pesenti Gritti's avatar
Marco Pesenti Gritti committed
810

811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
static gdouble
compute_scroll_increment (EvView        *view,
			  GtkScrollType  scroll)
{
	GtkWidget *widget = GTK_WIDGET (view);
	GtkAdjustment *adjustment = view->vadjustment;
	GdkRegion *text_region, *region;
	gint page;
	GdkRectangle rect;
	EvRectangle doc_rect;
	GdkRectangle page_area;
	GtkBorder border;
	GdkRectangle *recs;
	gint n_recs;
	gdouble fraction = 1.0;

	if (scroll != GTK_SCROLL_PAGE_BACKWARD && scroll != GTK_SCROLL_PAGE_FORWARD)
		return adjustment->page_size;

	page = scroll == GTK_SCROLL_PAGE_BACKWARD ? view->start_page : view->end_page;

	text_region = ev_pixbuf_cache_get_text_mapping (view->pixbuf_cache, page);
	if (!text_region || gdk_region_empty (text_region))
		return adjustment->page_size;

	get_page_extents (view, page, &page_area, &border);
	rect.x = page_area.x + view->scroll_x;
	rect.y = view->scroll_y + (scroll == GTK_SCROLL_PAGE_BACKWARD ? 5 : widget->allocation.height - 5);
	rect.width = page_area.width;
	rect.height = 1;
	view_rect_to_doc_rect (view, &rect, &page_area, &doc_rect);

	/* Convert the doc rectangle into a GdkRectangle */
	rect.x = doc_rect.x1;
	rect.y = doc_rect.y1;
	rect.width = doc_rect.x2 - doc_rect.x1;
	rect.height = MAX (1, doc_rect.y2 - doc_rect.y1);
	region = gdk_region_rectangle (&rect);

	gdk_region_intersect (region, text_region);
	gdk_region_get_rectangles (region, &recs, &n_recs);
	gdk_region_destroy (region);
	if (n_recs > 0) {
		EvRenderContext *rc;
		EvPage  *ev_page;

		ev_page = ev_document_get_page (view->document, page);
		rc = ev_render_context_new (ev_page, view->rotation, view->scale);
		g_object_unref (ev_page);
		/* Get the selection region to know the height of the line */
		doc_rect.x1 = doc_rect.x2 = recs[0].x + 0.5;
		doc_rect.y1 = doc_rect.y2 = recs[0].y + 0.5;

		ev_document_doc_mutex_lock ();
		region = ev_selection_get_selection_region (EV_SELECTION (view->document),
							    rc, EV_SELECTION_STYLE_LINE,
							    &doc_rect);
		ev_document_doc_mutex_unlock ();

		g_object_unref (rc);
		g_free (recs);
		gdk_region_get_rectangles (region, &recs, &n_recs);
		gdk_region_destroy (region);
		if (n_recs > 0) {
			fraction = 1 - (recs[0].height / adjustment->page_size);
		}
		g_free (recs);
	}

	return adjustment->page_size * fraction;

}

884 885
void
ev_view_scroll (EvView        *view,
886 887
	        GtkScrollType  scroll,
		gboolean       horizontal)
888 889 890 891 892 893
{
	GtkAdjustment *adjustment;
	double value, increment;
	gboolean first_page = FALSE;
	gboolean last_page = FALSE;

894 895
	view->jump_to_find_result = FALSE;

896
	if (view->presentation || view->sizing_mode == EV_SIZING_BEST_FIT) {
897
		switch (scroll) {
898 899
			case GTK_SCROLL_PAGE_BACKWARD:
			case GTK_SCROLL_STEP_BACKWARD:
900 901
				ev_view_previous_page (view);
				break;
902 903
			case GTK_SCROLL_PAGE_FORWARD:
			case GTK_SCROLL_STEP_FORWARD:
904 905 906 907 908 909 910 911
				ev_view_next_page (view);
				break;
			default:
				break;
		}
		return;
	}

912
	/* Assign values for increment and vertical adjustment */
913
	adjustment = horizontal ? view->hadjustment : view->vadjustment;
914 915 916 917 918
	value = adjustment->value;

	/* Assign boolean for first and last page */
	if (view->current_page == 0)
		first_page = TRUE;
919
	if (view->current_page == ev_document_get_n_pages (view->document) - 1)
920 921 922
		last_page = TRUE;

	switch (scroll) {
923
		case GTK_SCROLL_PAGE_BACKWARD:
924 925 926 927 928 929
			/* Do not jump backwards if at the first page */
			if (value == (adjustment->lower) && first_page) {
				/* Do nothing */
				/* At the top of a page, assign the upper bound limit of previous page */
			} else if (value == (adjustment->lower)) {
				value = adjustment->upper - adjustment->page_size;
930
				ev_view_previous_page (view);
931 932
				/* Jump to the top */
			} else {
933
				increment = compute_scroll_increment (view, GTK_SCROLL_PAGE_BACKWARD);
934 935 936
				value = MAX (value - increment, adjustment->lower);
			}
			break;
937
		case GTK_SCROLL_PAGE_FORWARD:
938 939 940 941 942 943
			/* Do not jump forward if at the last page */
			if (value == (adjustment->upper - adjustment->page_size) && last_page) {
				/* Do nothing */
			/* At the bottom of a page, assign the lower bound limit of next page */
			} else if (value == (adjustment->upper - adjustment->page_size)) {
				value = 0;
944
				ev_view_next_page (view);
945 946
			/* Jump to the bottom */
			} else {
947
				increment = compute_scroll_increment (view, GTK_SCROLL_PAGE_FORWARD);
948 949 950
				value = MIN (value + increment, adjustment->upper - adjustment->page_size);
			}
			break;
951
	        case GTK_SCROLL_STEP_BACKWARD:
952 953
			value -= adjustment->step_increment;
			break;
954
	        case GTK_SCROLL_STEP_FORWARD:
955 956
			value += adjustment->step_increment;
			break;
957
        	case GTK_SCROLL_STEP_DOWN:
958
			value -= adjustment->step_increment / 10;
959
			break;
960
        	case GTK_SCROLL_STEP_UP:
961
			value += adjustment->step_increment / 10;
962
			break;
963
        	default:
964 965
			break;
	}
966

967
	value = CLAMP (value, adjustment->lower,
968
		       adjustment->upper - adjustment->page_size);	
969

970
	gtk_adjustment_set_value (adjustment, value);
971 972 973 974 975 976 977 978 979 980 981
}

#define MARGIN 5

static void
ensure_rectangle_is_visible (EvView *view, GdkRectangle *rect)
{
	GtkWidget *widget = GTK_WIDGET (view);
	GtkAdjustment *adjustment;
	int value;

982
	view->pending_scroll = SCROLL_TO_FIND_LOCATION;
983

984 985 986 987 988 989 990 991 992 993
	adjustment = view->vadjustment;

	if (rect->y < adjustment->value) {
		value = MAX (adjustment->lower, rect->y - MARGIN);
		gtk_adjustment_set_value (view->vadjustment, value);
	} else if (rect->y + rect->height >
		   adjustment->value + widget->allocation.height) {
		value = MIN (adjustment->upper, rect->y + rect->height -
			     widget->allocation.height + MARGIN);
		gtk_adjustment_set_value (view->vadjustment, value);
994
	}
995

996 997 998 999 1000 1001 1002 1003 1004 1005 1006
	adjustment = view->hadjustment;

	if (rect->x < adjustment->value) {
		value = MAX (adjustment->lower, rect->x - MARGIN);
		gtk_adjustment_set_value (view->hadjustment, value);
	} else if (rect->x + rect->height >
		   adjustment->value + widget->allocation.width) {
		value = MIN (adjustment->upper, rect->x + rect->width -
			     widget->allocation.width + MARGIN);
		gtk_adjustment_set_value (view->hadjustment, value);
	}
1007 1008

	gtk_widget_queue_resize (GTK_WIDGET (view));
Owen Taylor's avatar
Owen Taylor committed
1009 1010
}

1011 1012
/*** Geometry computations ***/

1013
static void
1014
compute_border (EvView *view, int width, int height, GtkBorder *border)
1015
{
1016 1017 1018 1019 1020 1021 1022 1023 1024
	if (view->presentation) {
		border->left = 0;
		border->right = 0;
		border->top = 0;
		border->bottom = 0;
	} else {
		ev_document_misc_get_page_border_size (width, height, border);
	}
}
1025

1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
void
_get_page_size_for_scale_and_rotation (EvDocument *document,
				       gint        page,
				       gdouble     scale,
				       gint        rotation,
				       gint       *page_width,
				       gint       *page_height)
{
	gdouble w, h;
	gint    width, height;

	ev_document_get_page_size (document, page, &w, &h);

	width = (gint)(w * scale + 0.5);
	height = (gint)(h * scale + 0.5);

	if (page_width)
		*page_width = (rotation == 0 || rotation == 180) ? width : height;
	if (page_height)
		*page_height = (rotation == 0 || rotation == 180) ? height : width;
}

static void
ev_view_get_page_size (EvView *view,
		       gint    page,
		       gint   *page_width,
		       gint   *page_height)
{
	_get_page_size_for_scale_and_rotation (view->document,
					       page,
					       view->scale,
					       view->rotation,
					       page_width,
					       page_height);
}

1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
static void
ev_view_get_max_page_size (EvView *view,
			   gint   *max_width,
			   gint   *max_height)
{
	double w, h;
	gint   width, height;

	ev_document_get_max_page_size (view->document, &w, &h);

	width = (gint)(w * view->scale + 0.5);
	height = (gint)(h * view->scale + 0.5);

	if (max_width)
		*max_width = (view->rotation == 0 || view->rotation == 180) ? width : height;
	if (max_height)
		*max_height = (view->rotation == 0 || view->rotation == 180) ? height : width;
}

1081
static void
1082
get_page_y_offset (EvView *view, int page, int *y_offset)
1083 1084 1085
{
	int max_width, offset;
	GtkBorder border;
1086

1087
	g_return_if_fail (y_offset != NULL);
1088

1089
	ev_view_get_max_page_size (view, &max_width, NULL);
1090
	compute_border (view, max_width, max_width, &border);
1091

1092
	if (view->dual_page) {
1093
		ev_view_get_height_to_page (view, page, NULL, &offset);
1094 1095
		offset += ((page + get_dual_even_left (view)) / 2 + 1) * view->spacing +
			((page + get_dual_even_left (view)) / 2 ) * (border.top + border.bottom);
1096
	} else {
1097
		ev_view_get_height_to_page (view, page, &offset, NULL);
1098 1099
		offset += (page + 1) * view->spacing + page * (border.top + border.bottom);
	}
1100

1101 1102 1103 1104
	*y_offset = offset;
	return;
}

1105 1106 1107 1108 1109 1110 1111 1112
static gboolean
get_page_extents (EvView       *view,
		  gint          page,
		  GdkRectangle *page_area,
		  GtkBorder    *border)
{
	GtkWidget *widget;
	int width, height;
1113

1114 1115 1116
	widget = GTK_WIDGET (view);

	/* Get the size of the page */
1117
	ev_view_get_page_size (view, page, &width, &height);
1118 1119 1120 1121 1122 1123 1124 1125
	compute_border (view, width, height, border);
	page_area->width = width + border->left + border->right;
	page_area->height = height + border->top + border->bottom;

	if (view->presentation) {
		page_area->x = (MAX (0, widget->allocation.width - width))/2;
		page_area->y = (MAX (0, widget->allocation.height - height))/2;
	} else if (view->continuous) {
1126
		gint max_width;
1127 1128
		gint x, y;

1129
		ev_view_get_max_page_size (view, &max_width, NULL);
1130
		max_width = max_width + border->left + border->right;
1131 1132
		/* Get the location of the bounding box */
		if (view->dual_page) {
1133
			x = view->spacing + ((page % 2 == get_dual_even_left (view)) ? 0 : 1) * (max_width + view->spacing);
1134
			x = x + MAX (0, widget->allocation.width - (max_width * 2 + view->spacing * 3)) / 2;
1135
			if (page % 2 == get_dual_even_left (view))
1136
				x = x + (max_width - width - border->left - border->right);
1137 1138
		} else {
			x = view->spacing;
1139
			x = x + MAX (0, widget->allocation.width - (width + view->spacing * 2)) / 2;
1140
		}
1141

1142
		get_page_y_offset (view, page, &y);
1143

1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
		page_area->x = x;
		page_area->y = y;
	} else {
		gint x, y;
		if (view->dual_page) {
			gint width_2, height_2;
			gint max_width = width;
			gint max_height = height;
			GtkBorder overall_border;
			gint other_page;

1155
			other_page = (page % 2 == get_dual_even_left (view)) ? page + 1: page - 1;
1156 1157

			/* First, we get the bounding box of the two pages */
1158
			if (other_page < ev_document_get_n_pages (view->document)
1159
			    && (0 <= other_page)) {
1160 1161
				ev_view_get_page_size (view, other_page,
						       &width_2, &height_2);
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
				if (width_2 > width)
					max_width = width_2;
				if (height_2 > height)
					max_height = height_2;
			}
			compute_border (view, max_width, max_height, &overall_border);

			/* Find the offsets */
			x = view->spacing;
			y = view->spacing;

			/* Adjust for being the left or right page */
1174
			if (page % 2 == get_dual_even_left (view))
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
				x = x + max_width - width;
			else
				x = x + (max_width + overall_border.left + overall_border.right) + view->spacing;

			y = y + (max_height - height)/2;

			/* Adjust for extra allocation */
			x = x + MAX (0, widget->allocation.width -
				     ((max_width + overall_border.left + overall_border.right) * 2 + view->spacing * 3))/2;
			y = y + MAX (0, widget->allocation.height - (height + view->spacing * 2))/2;
		} else {
			x = view->spacing;
			y = view->spacing;
1188

1189
			/* Adjust for extra allocation */
1190 1191
			x = x + MAX (0, widget->allocation.width - (width + border->left + border->right + view->spacing * 2))/2;
			y = y + MAX (0, widget->allocation.height - (height + border->top + border->bottom +  view->spacing * 2))/2;
1192 1193 1194 1195 1196 1197 1198
		}

		page_area->x = x;
		page_area->y = y;
	}

	return TRUE;
1199 1200
}

1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
static void
get_doc_page_size (EvView  *view,
		   gint     page,
		   gdouble *width,
		   gdouble *height)
{
	double w, h;

	ev_document_get_page_size (view->document, page, &w, &h);
	if (view->rotation == 0 || view->rotation == 180) {
		if (width) *width = w;
		if (height) *height = h;
	} else {
		if (width) *width = h;
		if (height) *height = w;
	}
}

1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
static void
view_point_to_doc_point (EvView *view,
			 GdkPoint *view_point,
			 GdkRectangle *page_area,
			 double  *doc_point_x,
			 double  *doc_point_y)
{
	*doc_point_x = (double) (view_point->x - page_area->x) / view->scale;
	*doc_point_y = (double) (view_point->y - page_area->y) / view->scale;
}

1230
static void
Marco Pesenti Gritti's avatar
Marco Pesenti Gritti committed
1231 1232 1233 1234
view_rect_to_doc_rect (EvView *view,
		       GdkRectangle *view_rect,
		       GdkRectangle *page_area,
		       EvRectangle  *doc_rect)
1235
{
1236 1237 1238 1239
	doc_rect->x1 = (double) (view_rect->x - page_area->x) / view->scale;
	doc_rect->y1 = (double) (view_rect->y - page_area->y) / view->scale;
	doc_rect->x2 = doc_rect->x1 + (double) view_rect->width / view->scale;
	doc_rect->y2 = doc_rect->y1 + (double) view_rect->height / view->scale;
Marco Pesenti Gritti's avatar
Marco Pesenti Gritti committed
1240 1241
}

1242
static void
1243 1244 1245 1246 1247 1248 1249
doc_point_to_view_point (EvView       *view,
                         int           page,
		         EvPoint      *doc_point,
		         GdkPoint     *view_point)
{
	GdkRectangle page_area;
	GtkBorder border;
1250
	double x, y, view_x, view_y;
1251
	gdouble width, height;
1252

1253
	get_doc_page_size (view, page, &width, &height);
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272

	if (view->rotation == 0) {
		x = doc_point->x;
		y = doc_point->y;
	} else if (view->rotation == 90) {
		x = width - doc_point->y;
		y = doc_point->x;
	} else if (view->rotation == 180) {
		x = width - doc_point->x;
		y = height - doc_point->y;
	} else if (view->rotation == 270) {
		x = doc_point->y;
		y = height - doc_point->x;
	} else {
		g_assert_not_reached ();
	}

	get_page_extents (view, page, &page_area, &border);

1273 1274
	view_x = CLAMP (x * view->scale, 0, page_area.width);
	view_y = CLAMP (y * view->scale, 0, page_area.height);
1275 1276
	view_point->x = view_x + page_area.x;
	view_point->y = view_y + page_area.y;
1277 1278
}

1279
static void
1280 1281 1282 1283
doc_rect_to_view_rect (EvView       *view,
                       int           page,
		       EvRectangle  *doc_rect,
		       GdkRectangle *view_rect)
1284
{
1285 1286
	GdkRectangle page_area;
	GtkBorder border;
1287
	double x, y, w, h;
1288
	gdouble width, height;
1289

1290
	get_doc_page_size (view, page, &width, &height);
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315

	if (view->rotation == 0) {
		x = doc_rect->x1;
		y = doc_rect->y1;
		w = doc_rect->x2 - doc_rect->x1;
		h = doc_rect->y2 - doc_rect->y1;
	} else if (view->rotation == 90) {
		x = width - doc_rect->y2;
		y = doc_rect->x1;
		w = doc_rect->y2 - doc_rect->y1;
		h = doc_rect->x2 - doc_rect->x1;
	} else if (view->rotation == 180) {
		x = width - doc_rect->x2;
		y = height - doc_rect->y2;
		w = doc_rect->x2 - doc_rect->x1;
		h = doc_rect->y2 - doc_rect->y1;
	} else if (view->rotation == 270) {
		x = doc_rect->y1;
		y = height - doc_rect->x2;
		w = doc_rect->y2 - doc_rect->y1;
		h = doc_rect->x2 - doc_rect->x1;
	} else {
		g_assert_not_reached ();
	}

1316
	get_page_extents (view, page, &page_area, &border)