+static void
+compute_border (EvView *view, int width, int height, GtkBorder *border)
+{
+ 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);
+ }
+}
+
+static gboolean
+get_page_extents (EvView *view,
+ gint page,
+ GdkRectangle *page_area,
+ GtkBorder *border)
+{
+ GtkWidget *widget;
+ int width, height;
+
+ widget = GTK_WIDGET (view);
+
+ /* Quick sanity check */
+ if (view->presentation) {
+ if (view->current_page != page)
+ return FALSE;
+ } else if (view->continuous) {
+ if (page < view->start_page ||
+ page > view->end_page)
+ return FALSE;
+ } else if (view->dual_page) {
+ if (ABS (page - view->current_page) > 1)
+ return FALSE;
+ } else {
+ if (view->current_page != page)
+ return FALSE;
+ }
+
+ /* Get the size of the page */
+ ev_page_cache_get_size (view->page_cache, page,
+ view->scale,
+ &width, &height);
+ 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) {
+ gint max_width, max_height;
+ gint x, y;
+
+ get_bounding_box_size (view, &max_width, &max_height);
+ /* Get the location of the bounding box */
+ if (view->dual_page) {
+ x = view->spacing + (page % 2) * (max_width + view->spacing);
+ y = view->spacing + (page / 2) * (max_height + view->spacing);
+ x = x + MAX (0, widget->allocation.width - (max_width * 2 + view->spacing * 3))/2;
+ } else {
+ x = view->spacing;
+ y = view->spacing + page * (max_height + view->spacing);
+ x = x + MAX (0, widget->allocation.width - (max_width + view->spacing * 2))/2;
+ }
+ 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;
+
+ other_page = page ^ 1;
+
+ /* First, we get the bounding box of the two pages */
+ if (other_page < ev_page_cache_get_n_pages (view->page_cache)) {
+ ev_page_cache_get_size (view->page_cache,
+ page + 1,
+ view->scale,
+ &width_2, &height_2);
+ 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 */
+ if (page % 2 == 0)
+ 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;
+
+ /* Adjust for extra allocation */
+ x = x + MAX (0, widget->allocation.width - (width + view->spacing * 2))/2;
+ y = y + MAX (0, widget->allocation.height - (height + view->spacing * 2))/2;
+ }
+
+ page_area->x = x;
+ page_area->y = y;
+ }
+
+ return TRUE;
+}
+
+static void
+view_rect_to_doc_rect (EvView *view,
+ GdkRectangle *view_rect,
+ GdkRectangle *page_area,
+ EvRectangle *doc_rect)
+{
+ doc_rect->x1 = floor ((view_rect->x - page_area->x) / view->scale);
+ doc_rect->y1 = floor ((view_rect->y - page_area->y) / view->scale);
+ doc_rect->x2 = doc_rect->x1 + ceil (view_rect->width / view->scale);
+ doc_rect->y2 = doc_rect->y1 + ceil (view_rect->height / view->scale);
+}
+
+static void
+compute_selections (EvView *view, GdkRectangle *view_rect)
+{
+ int n_pages, i;
+
+ clear_selection (view);
+
+ n_pages = ev_page_cache_get_n_pages (view->page_cache);
+ for (i = 0; i < n_pages; i++) {
+ GdkRectangle page_area;
+ GtkBorder border;
+
+ if (get_page_extents (view, i, &page_area, &border)) {
+ GdkRectangle overlap;
+
+ if (gdk_rectangle_intersect (&page_area, view_rect, &overlap)) {
+ EvViewSelection *selection;
+
+ selection = g_new0 (EvViewSelection, 1);
+ selection->page = i;
+ view_rect_to_doc_rect (view, &overlap, &page_area,
+ &(selection->rect));
+
+ view->selections = g_list_append
+ (view->selections, selection);
+ }
+ }
+ }
+}
+
+static void
+doc_rect_to_view_rect (EvView *view,
+ int page,
+ EvRectangle *doc_rect,
+ GdkRectangle *view_rect)
+{
+ GdkRectangle page_area;
+ GtkBorder border;
+ int width, height;
+
+ get_page_extents (view, page, &page_area, &border);
+
+ width = doc_rect->x2 - doc_rect->x1;
+ height = doc_rect->y2 - doc_rect->y1;
+ view_rect->x = floor (doc_rect->x1 * view->scale) + page_area.x;
+ view_rect->y = floor (doc_rect->y1 * view->scale) + page_area.y;
+ view_rect->width = ceil (width * view->scale);
+ view_rect->height = ceil (height * view->scale);
+}
+
+static void
+get_bounding_box_size (EvView *view, int *max_width, int *max_height)
+{
+ GtkBorder border;
+ int width, height;
+
+ if (max_width) {
+ ev_page_cache_get_max_width_size (view->page_cache,
+ view->scale,
+ &width, &height);
+ compute_border (view, width, height, &border);
+ *max_width = width + border.left + border.right;
+ }
+
+
+ if (max_height) {
+ ev_page_cache_get_max_height_size (view->page_cache,
+ view->scale,
+ &width, &height);
+ compute_border (view, width, height, &border);
+ *max_height = height + border.top + border.bottom;
+ }
+}
+
+
+static void
+ev_view_size_request_continuous_and_dual_page (EvView *view,
+ GtkRequisition *requisition)
+{
+ int max_width, max_height;
+ int n_rows;
+
+ get_bounding_box_size (view, &max_width, &max_height);
+
+ n_rows = (1 + ev_page_cache_get_n_pages (view->page_cache)) / 2;
+
+ requisition->width = (max_width * 2) + (view->spacing * 3);
+ requisition->height = max_height * n_rows + (view->spacing * (n_rows + 1));
+
+ if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
+ requisition->width = 1;
+ } else if (view->sizing_mode == EV_SIZING_BEST_FIT) {
+ requisition->width = 1;
+ /* FIXME: This could actually be set on one page docs or docs
+ * with a strange aspect ratio. */
+ /* requisition->height = 1;*/
+ }
+}
+
+static void
+ev_view_size_request_continuous (EvView *view,
+ GtkRequisition *requisition)
+{
+ int max_width, max_height;
+ int n_pages;
+
+ get_bounding_box_size (view, &max_width, &max_height);
+
+ n_pages = ev_page_cache_get_n_pages (view->page_cache);
+
+ requisition->width = max_width + (view->spacing * 2);
+ requisition->height = max_height * n_pages + (view->spacing * (n_pages + 1));
+
+ if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
+ requisition->width = 1;
+ } else if (view->sizing_mode == EV_SIZING_BEST_FIT) {
+ requisition->width = 1;
+ /* FIXME: This could actually be set on one page docs or docs
+ * with a strange aspect ratio. */
+ /* requisition->height = 1;*/
+ }
+}
+
+static void
+ev_view_size_request_dual_page (EvView *view,
+ GtkRequisition *requisition)
+{
+ GtkBorder border;
+ gint width, height;
+
+ /* Find the largest of the two. */
+ ev_page_cache_get_size (view->page_cache,
+ view->current_page,
+ view->scale,
+ &width, &height);
+ if (view->current_page + 1 < ev_page_cache_get_n_pages (view->page_cache)) {
+ gint width_2, height_2;
+ ev_page_cache_get_size (view->page_cache,
+ view->current_page + 1,
+ view->scale,
+ &width_2, &height_2);
+ if (width_2 > width) {
+ width = width_2;
+ height = height_2;
+ }
+ }
+ compute_border (view, width, height, &border);
+
+ requisition->width = ((width + border.left + border.right) * 2) +
+ (view->spacing * 3);
+ requisition->height = (height + border.top + border.bottom) +
+ (view->spacing * 2);
+
+ if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
+ requisition->width = 1;
+ } else if (view->sizing_mode == EV_SIZING_BEST_FIT) {
+ requisition->width = 1;
+ requisition->height = 1;
+ }
+}
+
+static void
+ev_view_size_request_single_page (EvView *view,
+ GtkRequisition *requisition)
+{
+ GtkBorder border;
+ gint width, height;
+
+ ev_page_cache_get_size (view->page_cache,
+ view->current_page,
+ view->scale,
+ &width, &height);
+ compute_border (view, width, height, &border);
+
+ requisition->width = width + border.left + border.right + (2 * view->spacing);
+ requisition->height = height + border.top + border.bottom + (2 * view->spacing);
+
+ if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
+ requisition->width = 1;
+ requisition->height = height + border.top + border.bottom + (2 * view->spacing);
+ } else if (view->sizing_mode == EV_SIZING_BEST_FIT) {
+ requisition->width = 1;
+ requisition->height = 1;
+ }
+}
+