+
+ return list;
+}
+
+static gboolean
+gdk_rectangle_point_in (GdkRectangle *rectangle,
+ GdkPoint *point)
+{
+ return rectangle->x <= point->x &&
+ rectangle->y <= point->y &&
+ point->x < rectangle->x + rectangle->width &&
+ point->y < rectangle->y + rectangle->height;
+}
+
+static GList *
+compute_new_selection_text (EvView *view,
+ GdkPoint *start,
+ GdkPoint *stop)
+{
+ int n_pages, i, first, last;
+ GList *list = NULL;
+ EvViewSelection *selection;
+ gint width, height;
+
+ g_assert (view->selection_mode == EV_VIEW_SELECTION_TEXT);
+
+ n_pages = ev_page_cache_get_n_pages (view->page_cache);
+
+ /* First figure out the range of pages the selection
+ * affects. */
+ first = n_pages;
+ last = 0;
+ for (i = 0; i < n_pages; i++) {
+ GdkRectangle page_area;
+ GtkBorder border;
+
+ get_page_extents (view, i, &page_area, &border);
+ if (gdk_rectangle_point_in (&page_area, start) ||
+ gdk_rectangle_point_in (&page_area, stop)) {
+ if (first == n_pages)
+ first = i;
+ last = i;
+ }
+
+ }
+
+
+
+ /* Now create a list of EvViewSelection's for the affected
+ * pages. This could be an empty list, a list of just one
+ * page or a number of pages.*/
+ for (i = first; i < last + 1; i++) {
+ GdkRectangle page_area;
+ GtkBorder border;
+ GdkPoint *point;
+
+ ev_page_cache_get_size (view->page_cache, i,
+ view->rotation,
+ 1.0, &width, &height);
+
+ selection = g_new0 (EvViewSelection, 1);
+ selection->page = i;
+ selection->rect.x1 = selection->rect.y1 = 0;
+ selection->rect.x2 = width;
+ selection->rect.y2 = height;
+
+ get_page_extents (view, i, &page_area, &border);
+
+ if (gdk_rectangle_point_in (&page_area, start))
+ point = start;
+ else
+ point = stop;
+
+ if (i == first)
+ view_point_to_doc_point (view, point, &page_area,
+ &selection->rect.x1,
+ &selection->rect.y1);
+
+ /* If the selection is contained within just one page,
+ * make sure we don't write 'start' into both points
+ * in selection->rect. */
+ if (first == last)
+ point = stop;
+
+ if (i == last)
+ view_point_to_doc_point (view, point, &page_area,
+ &selection->rect.x2,
+ &selection->rect.y2);
+
+ list = g_list_append (list, selection);
+ }
+
+ return list;
+}
+
+/* This function takes the newly calculated list, and figures out which regions
+ * have changed. It then queues a redraw approporiately.
+ */
+static void
+merge_selection_region (EvView *view,
+ GList *list)
+{
+
+ /* FIXME: actually write... */
+ clear_selection (view);
+ gtk_widget_queue_draw (GTK_WIDGET (view));
+
+ view->selection_info.selections = list;
+ ev_pixbuf_cache_set_selection_list (view->pixbuf_cache, list);
+}
+
+static void
+compute_selections (EvView *view,
+ GdkPoint *start,
+ GdkPoint *stop)
+{
+ GList *list;
+
+ if (view->selection_mode == EV_VIEW_SELECTION_RECTANGLE)
+ list = compute_new_selection_rect (view, start, stop);
+ else
+ list = compute_new_selection_text (view, start, stop);
+ merge_selection_region (view, list);
+}
+
+/* Free's the selection. It's up to the caller to queue redraws if needed.
+ */
+static void
+selection_free (EvViewSelection *selection)
+{
+ g_free (selection);