GtkWidget parent_instance;
EvDocument *document;
- EvHistory *history;
GdkWindow *bin_window;
gboolean pressed_button;
gboolean has_selection;
+ GdkPoint selection_start;
GdkRectangle selection;
EvViewCursor cursor;
GtkAdjustment *hadjustment;
GtkAdjustment *vadjustment;
- GArray *find_results;
- int results_on_this_page;
- int next_page_with_result;
- double find_percent_complete;
+ int find_page;
+ int find_result;
double scale;
};
if (view->document)
g_object_unref (view->document);
- if (view->history)
- g_object_unref (view->history);
-
ev_view_set_scroll_adjustments (view, NULL, NULL);
- g_array_free (view->find_results, TRUE);
- view->find_results = NULL;
-
G_OBJECT_CLASS (ev_view_parent_class)->finalize (object);
}
if (GTK_WIDGET_REALIZED (widget)) {
if (view->document) {
ev_document_get_page_size (view->document,
+ -1,
&requisition->width,
&requisition->height);
} else {
}
static void
-draw_rubberband (GtkWidget *widget, GdkWindow *window, const GdkRectangle *rect)
+draw_rubberband (GtkWidget *widget, GdkWindow *window,
+ const GdkRectangle *rect, guchar alpha)
{
GdkGC *gc;
GdkPixbuf *pixbuf;
guint fill_color;
fill_color_gdk = gdk_color_copy (>K_WIDGET (widget)->style->base[GTK_STATE_SELECTED]);
- fill_color = ev_gdk_color_to_rgb (fill_color_gdk) << 8 | 0x40;
+ fill_color = ev_gdk_color_to_rgb (fill_color_gdk) << 8 | alpha;
pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
rect->width, rect->height);
gdk_color_free (fill_color_gdk);
}
+static void
+highlight_find_results (EvView *view)
+{
+ EvDocumentFind *find = EV_DOCUMENT_FIND (view->document);
+ int i, results;
+
+ results = ev_document_find_get_n_results (find);
+
+ for (i = 0; i < results; i++) {
+ GdkRectangle rectangle;
+ guchar alpha;
+
+ alpha = (i == view->find_result) ? 0x90 : 0x20;
+ ev_document_find_get_result (find, i, &rectangle);
+ draw_rubberband (GTK_WIDGET (view), view->bin_window,
+ &rectangle, alpha);
+ }
+}
+
static void
expose_bin_window (GtkWidget *widget,
GdkEventExpose *event)
{
EvView *view = EV_VIEW (widget);
- int i, current_page;
int x_offset, y_offset;
- const EvFindResult *results;
if (view->document == NULL)
return;
event->area.x, event->area.y,
event->area.width, event->area.height);
- results = (EvFindResult*) view->find_results->data;
- current_page = ev_document_get_page (view->document);
- i = 0;
- while (i < view->find_results->len) {
-#if 0
- g_printerr ("highlighting result %d page %d at %d,%d %dx%d\n",
- i, results[i].page_num,
- results[i].highlight_area.x,
- results[i].highlight_area.y,
- results[i].highlight_area.width,
- results[i].highlight_area.height);
-#endif
- if (results[i].page_num == current_page)
- draw_rubberband (widget, view->bin_window,
- &results[i].highlight_area);
- ++i;
- }
+ if (EV_IS_DOCUMENT_FIND (view->document)) {
+ highlight_find_results (view);
+ }
if (view->has_selection) {
- draw_rubberband (widget, view->bin_window, &view->selection);
+ draw_rubberband (widget, view->bin_window,
+ &view->selection, 0x40);
}
}
gtk_widget_queue_draw (widget);
}
- view->selection.x = event->x;
- view->selection.y = event->y;
- view->selection.width = 0;
- view->selection.height = 0;
+ view->selection_start.x = event->x;
+ view->selection_start.y = event->y;
break;
}
g_object_notify (G_OBJECT (view), "find-status");
}
+
static void
ev_view_set_cursor (EvView *view, EvViewCursor new_cursor)
{
if (view->pressed_button > 0) {
view->has_selection = TRUE;
- view->selection.x = MIN (view->selection.x, event->x);
- view->selection.y = MIN (view->selection.y, event->y);
- view->selection.width = ABS (view->selection.x - event->x) + 1;
- view->selection.height = ABS (view->selection.y - event->y) + 1;
+ view->selection.x = MIN (view->selection_start.x, event->x);
+ view->selection.y = MIN (view->selection_start.y, event->y);
+ view->selection.width = ABS (view->selection_start.x - event->x) + 1;
+ view->selection.height = ABS (view->selection_start.y - event->y) + 1;
+
+ gtk_widget_queue_draw (widget);
} else if (view->document) {
EvLink *link;
}
}
- gtk_widget_queue_draw (widget);
-
return TRUE;
}
view->scale = 1.0;
view->pressed_button = -1;
view->cursor = EV_VIEW_CURSOR_NORMAL;
-
- view->find_results = g_array_new (FALSE,
- FALSE,
- sizeof (EvFindResult));
- view->results_on_this_page = 0;
- view->next_page_with_result = 0;
}
-static char *
-ev_view_get_find_status_message (EvView *view)
+static void
+update_find_status_message (EvView *view)
{
- if (view->find_results->len == 0) {
- if (view->find_percent_complete >= (1.0 - 1e-10)) {
- return g_strdup (_("Not found"));
+ char *message;
+
+ if (ev_document_get_page (view->document) == view->find_page) {
+ int results;
+
+ results = ev_document_find_get_n_results
+ (EV_DOCUMENT_FIND (view->document));
+
+ message = g_strdup_printf (_("%d found on this page"),
+ results);
+ } else {
+ double percent;
+
+ percent = ev_document_find_get_progress
+ (EV_DOCUMENT_FIND (view->document));
+
+ if (percent >= (1.0 - 1e-10)) {
+ message = g_strdup (_("Not found"));
} else {
- return g_strdup_printf (_("%3d%% remaining to search"),
- (int) ((1.0 - view->find_percent_complete) * 100));
+ message = g_strdup_printf (_("%3d%% remaining to search"),
+ (int) ((1.0 - percent) * 100));
}
- } else if (view->results_on_this_page == 0) {
- g_assert (view->next_page_with_result != 0);
- return g_strdup_printf (_("Found on page %d"),
- view->next_page_with_result);
- } else {
- return g_strdup_printf (_("%d found on this page"),
- view->results_on_this_page);
+
}
+
+ ev_view_set_find_status (view, message);
+ g_free (message);
}
static void
-update_find_results (EvView *view)
+set_document_page (EvView *view, int page)
{
- 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 (view->document) {
+ int old_page = ev_document_get_page (view->document);
+ int old_width, old_height;
+
+ ev_document_get_page_size (view->document,
+ -1,
+ &old_width, &old_height);
+
+ if (old_page != page) {
+ ev_view_set_cursor (view, EV_VIEW_CURSOR_WAIT);
+ ev_document_set_page (view->document, page);
+ }
+
+ if (old_page != ev_document_get_page (view->document)) {
+ int width, height;
- 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;
+ g_signal_emit (view, page_changed_signal, 0);
+
+ view->has_selection = FALSE;
+ ev_document_get_page_size (view->document,
+ -1,
+ &width, &height);
+ if (width != old_width || height != old_height)
+ gtk_widget_queue_resize (GTK_WIDGET (view));
+
+ gtk_adjustment_set_value (view->vadjustment,
+ view->vadjustment->lower);
}
- ++i;
+
+ view->find_page = page;
+ view->find_result = 0;
+ update_find_status_message (view);
}
+}
- /* 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;
+#define MARGIN 5
+
+static void
+ensure_rectangle_is_visible (EvView *view, GdkRectangle *rect)
+{
+ GtkWidget *widget = GTK_WIDGET (view);
+ GtkAdjustment *adjustment;
+ int value;
+
+ 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);
}
- if (counts_changed ||
- view->find_results->len == 0) {
- char *message;
+ adjustment = view->hadjustment;
- message = ev_view_get_find_status_message (view);
- ev_view_set_find_status (view, message);
- g_free (message);
+ 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);
}
}
static void
-found_results_callback (EvDocument *document,
- const EvFindResult *results,
- int n_results,
- double percent_complete,
- void *data)
+jump_to_find_result (EvView *view)
{
- 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));
+ GdkRectangle rect;
+
+ ev_document_find_get_result (EV_DOCUMENT_FIND (view->document),
+ view->find_result, &rect);
+ ensure_rectangle_is_visible (view, &rect);
+}
+
+static void
+jump_to_find_page (EvView *view)
+{
+ int n_pages, i;
+
+ n_pages = ev_document_get_n_pages (view->document);
+
+ for (i = 0; i <= n_pages; i++) {
+ int has_results;
+ int page;
+
+ page = i + view->find_page;
+ if (page > n_pages) {
+ page = page - n_pages;
+ }
+
+ has_results = ev_document_find_page_has_results
+ (EV_DOCUMENT_FIND (view->document), page);
+ if (has_results == -1) {
+ view->find_page = page;
+ break;
+ } else if (has_results == 1) {
+ set_document_page (view, page);
+ jump_to_find_result (view);
+ break;
+ }
+ }
+}
+
+static void
+find_changed_cb (EvDocument *document, int page, EvView *view)
+{
+ jump_to_find_page (view);
+ jump_to_find_result (view);
+ update_find_status_message (view);
+
+ if (ev_document_get_page (document) == page) {
+ gtk_widget_queue_draw (GTK_WIDGET (view));
+ }
}
static void
if (document != view->document) {
if (view->document) {
g_signal_handlers_disconnect_by_func (view->document,
- found_results_callback,
+ find_changed_cb,
view);
- g_array_set_size (view->find_results, 0);
- view->results_on_this_page = 0;
- view->next_page_with_result = 0;
-
g_object_unref (view->document);
}
view->document = document;
+ view->find_page = 1;
+ view->find_result = 0;
if (view->document) {
g_object_ref (view->document);
if (EV_IS_DOCUMENT_FIND (view->document))
g_signal_connect (view->document,
- "found",
- G_CALLBACK (found_results_callback),
+ "find_changed",
+ G_CALLBACK (find_changed_cb),
view);
g_signal_connect (view->document,
"changed",
}
}
-static void
-set_document_page (EvView *view, int page)
-{
- if (view->document) {
- int old_page = ev_document_get_page (view->document);
- int old_width, old_height;
-
- ev_document_get_page_size (view->document,
- &old_width, &old_height);
-
- if (old_page != page) {
- ev_view_set_cursor (view, EV_VIEW_CURSOR_WAIT);
- ev_document_set_page (view->document, page);
- }
-
- if (old_page != ev_document_get_page (view->document)) {
- int width, height;
-
- g_signal_emit (view, page_changed_signal, 0);
-
- view->has_selection = FALSE;
- ev_document_get_page_size (view->document,
- &width, &height);
- if (width != old_width || height != old_height)
- gtk_widget_queue_resize (GTK_WIDGET (view));
-
- view->find_percent_complete = 0.0;
- update_find_results (view);
- }
- }
-}
-
static void
go_to_link (EvView *view, EvLink *link)
{
void
ev_view_go_to_link (EvView *view, EvLink *link)
{
- EvLinkType type;
-
go_to_link (view, link);
-
- type = ev_link_get_link_type (link);
- if (type == EV_LINK_TYPE_PAGE) {
- ev_history_add_link (view->history, link);
- }
-}
-
-static void
-go_to_index (EvView *view, int index)
-{
- EvLink *link;
-
- link = ev_history_get_link_nth (view->history, index);
- g_return_if_fail (link != NULL);
-
- go_to_link (view, link);
-}
-
-gboolean
-ev_view_can_go_back (EvView *view)
-{
- int index, n;
-
- if (view->history == NULL) {
- return FALSE;
- }
-
- index = ev_history_get_current_index (view->history);
- n = ev_history_get_n_links (view->history);
-
- if (n > 0) {
- return index != MAX (0, index - 1);
- } else {
- return FALSE;
- }
-}
-
-void
-ev_view_go_back (EvView *view)
-{
- int index, n;
-
- g_return_if_fail (EV_IS_HISTORY (view->history));
-
- index = ev_history_get_current_index (view->history);
- n = ev_history_get_n_links (view->history);
-
- if (n > 0) {
- index = MAX (0, index - 1);
- ev_history_set_current_index (view->history, index);
- }
-}
-
-gboolean
-ev_view_can_go_forward (EvView *view)
-{
- int index, n;
-
- if (view->history == NULL) {
- return FALSE;
- }
-
- index = ev_history_get_current_index (view->history);
- n = ev_history_get_n_links (view->history);
-
- if (n > 0) {
- return index != MIN (n - 1, index + 1);
- } else {
- return FALSE;
- }
}
-void
-ev_view_go_forward (EvView *view)
-{
- int index, n;
-
- g_return_if_fail (EV_IS_HISTORY (view->history));
-
- index = ev_history_get_current_index (view->history);
- n = ev_history_get_n_links (view->history);
-
- if (n > 0) {
- index = MIN (n - 1, index + 1);
- ev_history_set_current_index (view->history, index);
- }
-}
-
-
void
ev_view_set_page (EvView *view,
int page)
{
g_return_if_fail (EV_IS_VIEW (view));
- g_return_if_fail (EV_IS_HISTORY (view->history));
set_document_page (view, page);
- ev_history_add_page (view->history, page);
}
int
int width, height;
width = height = 0;
- ev_document_get_page_size (view->document, &width, &height);
+ ev_document_get_page_size (view->document, -1, &width, &height);
scale = 1.0;
if (width != 0 && height != 0) {
int width;
width = 0;
- ev_document_get_page_size (view->document, &width, NULL);
+ ev_document_get_page_size (view->document, -1, &width, NULL);
scale = 1.0;
if (width != 0)
ev_view_zoom (view, scale, FALSE);
}
-static void
-history_index_changed_cb (EvHistory *history,
- GParamSpec *pspec,
- EvView *view)
-{
- int index;
-
- index = ev_history_get_current_index (history);
- go_to_index (view, index);
-}
-
-void
-ev_view_set_history (EvView *view,
- EvHistory *history)
-{
- if (view->history) {
- g_object_unref (view->history);
- }
-
- view->history = g_object_ref (history);
- ev_history_add_page (view->history, ev_view_get_page (view));
-
- g_signal_connect (view->history, "notify::index",
- G_CALLBACK (history_index_changed_cb),
- view);
-}
-
const char *
ev_view_get_status (EvView *view)
{
return view->find_status;
}
+void
+ev_view_find_next (EvView *view)
+{
+ int n_results, n_pages;
+ EvDocumentFind *find = EV_DOCUMENT_FIND (view->document);
+
+ n_results = ev_document_find_get_n_results (find);
+ n_pages = ev_document_get_n_pages (view->document);
+
+ view->find_result++;
+
+ if (view->find_result >= n_results) {
+ view->find_result = 0;
+ view->find_page++;
+
+ if (view->find_page > n_pages) {
+ view->find_page = 1;
+ }
+
+ jump_to_find_page (view);
+ } else {
+ jump_to_find_result (view);
+ gtk_widget_queue_draw (GTK_WIDGET (view));
+ }
+}
+
+void
+ev_view_find_previous (EvView *view)
+{
+ int n_results, n_pages;
+ EvDocumentFind *find = EV_DOCUMENT_FIND (view->document);
+
+ n_results = ev_document_find_get_n_results (find);
+ n_pages = ev_document_get_n_pages (view->document);
+
+ view->find_result--;
+ if (view->find_result < 0) {
+ view->find_result = 0;
+ view->find_page--;
+
+ if (view->find_page < 1) {
+ view->find_page = n_pages;
+ }
+
+ jump_to_find_page (view);
+ } else {
+ jump_to_find_result (view);
+ gtk_widget_queue_draw (GTK_WIDGET (view));
+ }
+}