+
+ return list;
+}
+
+static GList *
+compute_new_selection_text (EvView *view,
+ GdkRectangle *view_rect)
+{
+ int n_pages, i;
+ GList *list = NULL;
+ EvViewSelection *first_selection = NULL;
+ EvViewSelection *last_selection = NULL;
+ gint width, height;
+
+ g_assert (view->selection_mode == EV_VIEW_SELECTION_TEXT);
+
+ n_pages = ev_page_cache_get_n_pages (view->page_cache);
+
+ /* We get the two edge pages */
+ 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;
+
+ if (first_selection == NULL) {
+ first_selection = g_new0 (EvViewSelection, 1);
+ selection = first_selection;
+ } else if (last_selection == NULL) {
+ last_selection = g_new0 (EvViewSelection, 1);
+ selection = last_selection;
+ } else {
+ selection = last_selection;
+ }
+
+ selection->page = i;
+ view_rect_to_doc_rect (view, &overlap, &page_area,
+ &(selection->rect));
+ }
+ }
+ }
+
+ /* No overlap */
+ if (first_selection == NULL)
+ return NULL;
+
+ /* only one selection. Return a page of it */
+ if (last_selection == NULL)
+ return g_list_append (NULL, first_selection);
+
+ /*clean up the selections;
+ */
+ ev_page_cache_get_size (view->page_cache, first_selection->page,
+ 1.0, &width, &height);
+ first_selection->rect.x2 = width;
+ first_selection->rect.y2 = height;
+ list = g_list_append (NULL, first_selection);
+
+ /* Add all the intervening pages */
+ for (i = first_selection->page + 1; i < last_selection->page; i++) {
+ EvViewSelection *selection;
+
+ selection = g_new0 (EvViewSelection, 1);
+ selection->page = i;
+ ev_page_cache_get_size (view->page_cache, i,
+ 1.0, &width, &height);
+ selection->rect.x1 = selection->rect.y1 = 0;
+ selection->rect.x2 = width;
+ selection->rect.y2 = height;
+ g_list_append (list, selection);
+ }
+
+ /* Clean up the last page */
+ last_selection->rect.x1 = 0;
+ last_selection->rect.y1 = 0;
+ list = g_list_append (list, last_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,
+ GdkRectangle *view_rect)
+{
+ GList *list;
+
+ if (view->selection_mode == EV_VIEW_SELECTION_RECTANGLE)
+ list = compute_new_selection_rect (view, view_rect);
+ else
+ list = compute_new_selection_text (view, view_rect);
+ 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);