X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;f=shell%2Fev-view.c;h=3a23cc3ee377c059b5dbb434d6aec701f21e3069;hb=f950c38c30956005c718b86dd9fe19b39340f4cd;hp=7aaa59aba9a611ab083baa466a81e181a439a03b;hpb=d97441740d457e1463083d561afdb719ca99e66b;p=evince.git diff --git a/shell/ev-view.c b/shell/ev-view.c index 7aaa59ab..3a23cc3e 100644 --- a/shell/ev-view.c +++ b/shell/ev-view.c @@ -1,3 +1,4 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */ /* this file is part of evince, a gnome document viewer * * Copyright (C) 2004 Red Hat, Inc @@ -18,9 +19,13 @@ */ #include +#include +#include +#include #include "ev-marshal.h" #include "ev-view.h" +#include "ev-document-find.h" #define EV_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EV_TYPE_VIEW, EvViewClass)) #define EV_IS_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), EV_TYPE_VIEW)) @@ -38,6 +43,13 @@ struct _EvView { GtkAdjustment *hadjustment; GtkAdjustment *vadjustment; + + GArray *find_results; + int results_on_this_page; + int next_page_with_result; + double find_percent_complete; + + double scale; }; struct _EvViewClass { @@ -46,6 +58,9 @@ struct _EvViewClass { void (*set_scroll_adjustments) (EvView *view, GtkAdjustment *hadjustment, GtkAdjustment *vadjustment); + void (*scroll_view) (EvView *view, + GtkScrollType scroll, + gboolean horizontal); /* Should this be notify::page? */ void (*page_changed) (EvView *view); @@ -135,6 +150,9 @@ ev_view_finalize (GObject *object) 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); } @@ -227,7 +245,9 @@ ev_view_realize (GtkWidget *widget) attributes.y = 0; attributes.width = MAX (widget->allocation.width, widget->requisition.width); attributes.height = MAX (widget->allocation.height, widget->requisition.height); - attributes.event_mask = GDK_EXPOSURE_MASK; + attributes.event_mask = GDK_EXPOSURE_MASK | + GDK_SCROLL_MASK | + GDK_KEY_PRESS_MASK; view->bin_window = gdk_window_new (widget->window, &attributes, @@ -285,11 +305,39 @@ expose_bin_window (GtkWidget *widget, GdkEventExpose *event) { EvView *view = EV_VIEW (widget); + int i; + int current_page; + const EvFindResult *results; + + if (view->document == NULL) + return; - if (view->document) - ev_document_render (view->document, - event->area.x, event->area.y, - event->area.width, event->area.height); + ev_document_render (view->document, + 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) + gdk_draw_rectangle (view->bin_window, + widget->style->base_gc[GTK_STATE_SELECTED], + FALSE, + results[i].highlight_area.x, + results[i].highlight_area.y, + results[i].highlight_area.width, + results[i].highlight_area.height); + ++i; + } } static gboolean @@ -304,7 +352,6 @@ ev_view_expose_event (GtkWidget *widget, return GTK_WIDGET_CLASS (ev_view_parent_class)->expose_event (widget, event); return FALSE; - } static gboolean @@ -383,12 +430,69 @@ ev_view_set_scroll_adjustments (EvView *view, view_update_adjustments (view); } +static void +add_scroll_binding (GtkBindingSet *binding_set, + guint keyval, + GtkScrollType scroll, + gboolean horizontal) +{ + guint keypad_keyval = keyval - GDK_Left + GDK_KP_Left; + + gtk_binding_entry_add_signal (binding_set, keyval, 0, + "scroll_view", 2, + GTK_TYPE_SCROLL_TYPE, scroll, + G_TYPE_BOOLEAN, horizontal); + gtk_binding_entry_add_signal (binding_set, keypad_keyval, 0, + "scroll_view", 2, + GTK_TYPE_SCROLL_TYPE, scroll, + G_TYPE_BOOLEAN, horizontal); +} + +static void +ev_view_scroll_view (EvView *view, + GtkScrollType scroll, + gboolean horizontal) +{ + GtkAdjustment *adjustment; + double value; + + if (horizontal) { + adjustment = view->hadjustment; + } else { + adjustment = view->vadjustment; + } + + value = adjustment->value; + + switch (scroll) { + case GTK_SCROLL_STEP_BACKWARD: + value -= adjustment->step_increment; + break; + case GTK_SCROLL_STEP_FORWARD: + value += adjustment->step_increment; + break; + case GTK_SCROLL_PAGE_BACKWARD: + value -= adjustment->page_increment; + break; + case GTK_SCROLL_PAGE_FORWARD: + value += adjustment->page_increment; + break; + default: + break; + } + + value = CLAMP (value, adjustment->lower, adjustment->upper - adjustment->page_size); + + gtk_adjustment_set_value (adjustment, value); +} + static void ev_view_class_init (EvViewClass *class) { GObjectClass *object_class = G_OBJECT_CLASS (class); GtkObjectClass *gtk_object_class = GTK_OBJECT_CLASS (class); GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class); + GtkBindingSet *binding_set; object_class->finalize = ev_view_finalize; @@ -403,8 +507,9 @@ ev_view_class_init (EvViewClass *class) widget_class->style_set = ev_view_style_set; widget_class->state_changed = ev_view_state_changed; gtk_object_class->destroy = ev_view_destroy; - + class->set_scroll_adjustments = ev_view_set_scroll_adjustments; + class->scroll_view = ev_view_scroll_view; widget_class->set_scroll_adjustments_signal = g_signal_new ("set-scroll-adjustments", G_OBJECT_CLASS_TYPE (object_class), @@ -422,14 +527,152 @@ ev_view_class_init (EvViewClass *class) NULL, NULL, ev_marshal_VOID__NONE, G_TYPE_NONE, 0); + + g_signal_new ("find-status-changed", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_LAST, + 0, + NULL, NULL, + ev_marshal_VOID__NONE, + G_TYPE_NONE, 0); + + g_signal_new ("scroll_view", + G_TYPE_FROM_CLASS (object_class), + G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, + G_STRUCT_OFFSET (EvViewClass, scroll_view), + NULL, NULL, + ev_marshal_VOID__ENUM_BOOLEAN, + G_TYPE_NONE, 2, + GTK_TYPE_SCROLL_TYPE, + G_TYPE_BOOLEAN); + + binding_set = gtk_binding_set_by_class (class); + + add_scroll_binding (binding_set, GDK_Left, GTK_SCROLL_STEP_BACKWARD, TRUE); + add_scroll_binding (binding_set, GDK_Right, GTK_SCROLL_STEP_FORWARD, TRUE); + add_scroll_binding (binding_set, GDK_Up, GTK_SCROLL_STEP_BACKWARD, FALSE); + add_scroll_binding (binding_set, GDK_Down, GTK_SCROLL_STEP_FORWARD, FALSE); + + add_scroll_binding (binding_set, GDK_Page_Up, GTK_SCROLL_PAGE_BACKWARD, FALSE); + add_scroll_binding (binding_set, GDK_Page_Down, GTK_SCROLL_PAGE_FORWARD, FALSE); } static void ev_view_init (EvView *view) { static const GdkColor white = { 0, 0xffff, 0xffff, 0xffff }; + + GTK_WIDGET_SET_FLAGS (view, GTK_CAN_FOCUS); + + view->scale = 1.0; gtk_widget_modify_bg (GTK_WIDGET (view), GTK_STATE_NORMAL, &white); + + 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)); } /*** Public API ***/ @@ -440,6 +683,13 @@ ev_view_new (void) return g_object_new (EV_TYPE_VIEW, NULL); } +static void +document_changed_callback (EvDocument *document, + EvView *view) +{ + gtk_widget_queue_draw (GTK_WIDGET (view)); +} + void ev_view_set_document (EvView *view, EvDocument *document) @@ -449,13 +699,30 @@ ev_view_set_document (EvView *view, if (document != view->document) { int old_page = ev_view_get_page (view); - if (view->document) + if (view->document) { + g_signal_handlers_disconnect_by_func (view->document, + found_results_callback, + 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; - if (view->document) + if (view->document) { g_object_ref (view->document); + g_signal_connect (view->document, + "found", + G_CALLBACK (found_results_callback), + view); + g_signal_connect (view->document, + "changed", + G_CALLBACK (document_changed_callback), + view); + } if (GTK_WIDGET_REALIZED (view)) ev_document_set_target (view->document, view->bin_window); @@ -477,7 +744,9 @@ ev_view_set_page (EvView *view, ev_document_set_page (view->document, page); if (old_page != ev_document_get_page (view->document)) { g_signal_emit (view, page_changed_signal, 0); - gtk_widget_queue_draw (GTK_WIDGET (view)); + + view->find_percent_complete = 0.0; + update_find_results (view); } } } @@ -490,3 +759,104 @@ ev_view_get_page (EvView *view) else return 1; } + +#define ZOOM_IN_FACTOR 1.2 +#define ZOOM_OUT_FACTOR (1.0/ZOOM_IN_FACTOR) + +#define MIN_SCALE 0.05409 +#define MAX_SCALE 18.4884 + +static void +ev_view_zoom (EvView *view, + double factor, + gboolean relative) +{ + double scale; + + if (relative) + scale = view->scale * factor; + else + scale = factor; + + view->scale = CLAMP (scale, MIN_SCALE, MAX_SCALE); + + ev_document_set_scale (view->document, view->scale); + + gtk_widget_queue_draw (GTK_WIDGET (view)); +} + +void +ev_view_zoom_in (EvView *view) +{ + ev_view_zoom (view, ZOOM_IN_FACTOR, TRUE); +} + +void +ev_view_zoom_out (EvView *view) +{ + ev_view_zoom (view, ZOOM_OUT_FACTOR, TRUE); +} + +void +ev_view_normal_size (EvView *view) +{ + ev_view_zoom (view, 1.0, FALSE); +} + +void +ev_view_best_fit (EvView *view) +{ + double scale; + int width, height; + + width = height = 0; + ev_document_get_page_size (view->document, &width, &height); + + scale = 1.0; + if (width != 0 && height != 0) { + double scale_w, scale_h; + + scale_w = (double)GTK_WIDGET (view)->allocation.width * view->scale / width; + scale_h = (double)GTK_WIDGET (view)->allocation.height * view->scale / height; + + scale = (scale_w < scale_h) ? scale_w : scale_h; + } + + ev_view_zoom (view, scale, FALSE); +} + +void +ev_view_fit_width (EvView *view) +{ + double scale = 1.0; + int width; + + width = 0; + ev_document_get_page_size (view->document, &width, NULL); + + scale = 1.0; + if (width != 0) + scale = (double)GTK_WIDGET (view)->allocation.width * view->scale / width; + + ev_view_zoom (view, scale, FALSE); +} + +char* +ev_view_get_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")); + } else { + return g_strdup_printf (_("%3d%% remaining to search"), + (int) ((1.0 - view->find_percent_complete) * 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); + } +}