+
+ view->find_results = g_array_new (FALSE,
+ FALSE,
+ sizeof (EvFindResult));
+ view->results_on_this_page = 0;
+ view->next_page_with_result = 0;
+}
+
+static void
+update_find_results (EvView *view)
+{
+ const EvFindResult *results;
+ int i;
+ int on_this_page;
+ int next_page_with_result;
+ int earliest_page_with_result;
+ int current_page;
+ gboolean counts_changed;
+
+ results = (EvFindResult*) view->find_results->data;
+ current_page = ev_document_get_page (view->document);
+ next_page_with_result = 0;
+ on_this_page = 0;
+ earliest_page_with_result = 0;
+
+ i = 0;
+ while (i < view->find_results->len) {
+ if (results[i].page_num == current_page) {
+ ++on_this_page;
+ } else {
+ int delta = results[i].page_num - current_page;
+
+ if (delta > 0 && /* result on later page */
+ (next_page_with_result == 0 ||
+ results[i].page_num < next_page_with_result))
+ next_page_with_result = results[i].page_num;
+
+ if (delta < 0 && /* result on a previous page */
+ (earliest_page_with_result == 0 ||
+ results[i].page_num < earliest_page_with_result))
+ earliest_page_with_result = results[i].page_num;
+ }
+ ++i;
+ }
+
+ /* If earliest page is just the current page, there is no earliest page */
+ if (earliest_page_with_result == current_page)
+ earliest_page_with_result = 0;
+
+ /* If no next page, then wrap and the wrapped page is the next page */
+ if (next_page_with_result == 0)
+ next_page_with_result = earliest_page_with_result;
+
+ counts_changed = FALSE;
+ if (on_this_page != view->results_on_this_page ||
+ next_page_with_result != view->next_page_with_result) {
+ view->results_on_this_page = on_this_page;
+ view->next_page_with_result = next_page_with_result;
+ counts_changed = TRUE;
+ }
+
+ /* If there are no results at all, then the
+ * results of ev_view_get_find_status_message() will change
+ * to reflect the percent_complete so we have to emit the signal
+ */
+ if (counts_changed ||
+ view->find_results->len == 0) {
+ g_signal_emit_by_name (view,
+ "find-status-changed");
+ }
+}
+
+static void
+found_results_callback (EvDocument *document,
+ const EvFindResult *results,
+ int n_results,
+ double percent_complete,
+ void *data)
+{
+ EvView *view = EV_VIEW (data);
+
+ g_array_set_size (view->find_results, 0);
+
+ if (n_results > 0)
+ g_array_append_vals (view->find_results,
+ results, n_results);
+
+#if 0
+ {
+ int i;
+
+ g_printerr ("%d results %d%%: ", n_results,
+ (int) (percent_complete * 100));
+ i = 0;
+ while (i < n_results) {
+ g_printerr ("%d ", results[i].page_num);
+ ++i;
+ }
+ g_printerr ("\n");
+ }
+#endif
+
+ view->find_percent_complete = percent_complete;
+ update_find_results (view);
+
+ gtk_widget_queue_draw (GTK_WIDGET (view));