X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;ds=inline;f=shell%2Fev-view.c;h=372d4a362262e859d7a1d9117e33db2b6a5e8b7c;hb=d4e3d8364345a6b8ab0ee4fff2bfbf57402aa7e8;hp=095bd24dbed72cab6974a20a433c3fd4d2249530;hpb=0b36c1cd0326cfbb3ed5cddfee5b1cc27e0eb317;p=evince.git diff --git a/shell/ev-view.c b/shell/ev-view.c index 095bd24d..372d4a36 100644 --- a/shell/ev-view.c +++ b/shell/ev-view.c @@ -60,7 +60,8 @@ static const GtkTargetEntry targets[] = { typedef enum { EV_VIEW_CURSOR_NORMAL, EV_VIEW_CURSOR_LINK, - EV_VIEW_CURSOR_WAIT + EV_VIEW_CURSOR_WAIT, + EV_VIEW_CURSOR_HIDDEN } EvViewCursor; #define ZOOM_IN_FACTOR 1.2 @@ -68,8 +69,6 @@ typedef enum { #define MIN_SCALE 0.05409 #define MAX_SCALE 18.4884 -#define ZOOM_EPSILON 1e-10 - struct _EvView { GtkWidget parent_instance; @@ -98,7 +97,8 @@ struct _EvView { int spacing; double scale; - EvSizingMode sizing_mode; + int width; + int height; }; struct _EvViewClass { @@ -214,6 +214,51 @@ ev_view_destroy (GtkObject *object) GTK_OBJECT_CLASS (ev_view_parent_class)->destroy (object); } +static void +ev_view_get_offsets (EvView *view, int *x_offset, int *y_offset) +{ + EvDocument *document = view->document; + GtkWidget *widget = GTK_WIDGET (view); + int width, height, target_width, target_height; + GtkBorder border; + + g_return_if_fail (EV_IS_DOCUMENT (document)); + + ev_document_get_page_size (document, -1, &width, &height); + ev_document_misc_get_page_border_size (width, height, &border); + + *x_offset = view->spacing; + *y_offset = view->spacing; + target_width = width + border.left + border.right + view->spacing * 2; + target_height = height + border.top + border.bottom + view->spacing * 2; + *x_offset += MAX (0, (widget->allocation.width - target_width) / 2); + *y_offset += MAX (0, (widget->allocation.height - target_height) / 2); +} + +static void +view_rect_to_doc_rect (EvView *view, GdkRectangle *view_rect, GdkRectangle *doc_rect) +{ + int x_offset, y_offset; + + ev_view_get_offsets (view, &x_offset, &y_offset); + doc_rect->x = (view_rect->x - x_offset) / view->scale; + doc_rect->y = (view_rect->y - y_offset) / view->scale; + doc_rect->width = view_rect->width / view->scale; + doc_rect->height = view_rect->height / view->scale; +} + +static void +doc_rect_to_view_rect (EvView *view, GdkRectangle *doc_rect, GdkRectangle *view_rect) +{ + int x_offset, y_offset; + + ev_view_get_offsets (view, &x_offset, &y_offset); + view_rect->x = doc_rect->x * view->scale + x_offset; + view_rect->y = doc_rect->y * view->scale + y_offset; + view_rect->width = doc_rect->width * view->scale; + view_rect->height = doc_rect->height * view->scale; +} + static void ev_view_size_request (GtkWidget *widget, GtkRequisition *requisition) @@ -222,10 +267,10 @@ ev_view_size_request (GtkWidget *widget, GtkBorder border; gint width, height; - if (! GTK_WIDGET_REALIZED (widget)) + if (!GTK_WIDGET_REALIZED (widget)) return; - if (! view->document) { + if (!view->document) { requisition->width = 1; requisition->height = 1; return; @@ -235,22 +280,18 @@ ev_view_size_request (GtkWidget *widget, &width, &height); ev_document_misc_get_page_border_size (width, height, &border); - switch (view->sizing_mode) { - case EV_SIZING_BEST_FIT: - requisition->width = MIN_SCALE * ((float) width) / view->scale; - requisition->height = MIN_SCALE * ((float) height) / view->scale; - break; - case EV_SIZING_FIT_WIDTH: - requisition->width = MIN_SCALE * ((float) width) / view->scale; - requisition->height = height + border.top + border.bottom; - requisition->height += view->spacing * 2; - break; - case EV_SIZING_FREE: - requisition->width = width + border.left + border.right; - requisition->height = height + border.top + border.bottom; - requisition->width += view->spacing * 2; - requisition->height += view->spacing * 2; - break; + if (view->width >= 0) { + requisition->width = 0; + } else { + requisition->width = width + border.left + border.right + + view->spacing * 2; + } + + if (view->height >= 0) { + requisition->height = 0; + } else { + requisition->height = height + border.top + border.bottom + + view->spacing * 2; } } @@ -310,7 +351,8 @@ ev_view_realize (GtkWidget *widget) GDK_BUTTON_RELEASE_MASK | GDK_SCROLL_MASK | GDK_KEY_PRESS_MASK | - GDK_POINTER_MOTION_MASK; + GDK_POINTER_MOTION_MASK | + GDK_LEAVE_NOTIFY_MASK; view->bin_window = gdk_window_new (widget->window, &attributes, @@ -399,9 +441,13 @@ draw_rubberband (GtkWidget *widget, GdkWindow *window, static void highlight_find_results (EvView *view) { - EvDocumentFind *find = EV_DOCUMENT_FIND (view->document); + EvDocumentFind *find; int i, results; + g_return_if_fail (EV_IS_DOCUMENT_FIND (view->document)); + + find = EV_DOCUMENT_FIND (view->document); + results = ev_document_find_get_n_results (find); for (i = 0; i < results; i++) { @@ -421,27 +467,19 @@ expose_bin_window (GtkWidget *widget, GdkEventExpose *event) { EvView *view = EV_VIEW (widget); - int x_offset, y_offset; GtkBorder border; gint width, height; GdkRectangle area; - int target_width, target_height; - + int x_offset, y_offset; + if (view->document == NULL) return; + ev_view_get_offsets (view, &x_offset, &y_offset); ev_document_get_page_size (view->document, -1, &width, &height); ev_document_misc_get_page_border_size (width, height, &border); - x_offset = view->spacing; - y_offset = view->spacing; - target_width = width + border.left + border.right + view->spacing * 2; - target_height = height + border.top + border.bottom + view->spacing * 2; - - x_offset += MAX (0, (widget->allocation.width - target_width) / 2); - y_offset += MAX (0, (widget->allocation.height - target_height) / 2); - /* Paint the frame */ area.x = x_offset; area.y = y_offset; @@ -454,8 +492,10 @@ expose_bin_window (GtkWidget *widget, x_offset + border.left, y_offset + border.top); - LOG ("Render area %d %d %d %d", event->area.x, event->area.y, - event->area.width, event->area.height); + LOG ("Render area %d %d %d %d - Offset %d %d", + event->area.x, event->area.y, + event->area.width, event->area.height, + x_offset, y_offset); ev_document_render (view->document, event->area.x, event->area.y, @@ -466,8 +506,13 @@ expose_bin_window (GtkWidget *widget, } if (view->has_selection) { - draw_rubberband (widget, view->bin_window, - &view->selection, 0x40); + GdkRectangle rubberband; + + doc_rect_to_view_rect (view, &view->selection, &rubberband); + if (rubberband.width > 0 && rubberband.height > 0) { + draw_rubberband (widget, view->bin_window, + &rubberband, 0x40); + } } } @@ -489,13 +534,23 @@ void ev_view_select_all (EvView *ev_view) { GtkWidget *widget = GTK_WIDGET (ev_view); + GdkRectangle selection; + int width, height; + int x_offset, y_offset; + GtkBorder border; g_return_if_fail (EV_IS_VIEW (ev_view)); + ev_view_get_offsets (ev_view, &x_offset, &y_offset); + ev_document_get_page_size (ev_view->document, -1, &width, &height); + ev_document_misc_get_page_border_size (width, height, &border); + ev_view->has_selection = TRUE; - ev_view->selection.x = ev_view->selection.y = 0; - ev_view->selection.width = widget->requisition.width; - ev_view->selection.height = widget->requisition.height; + selection.x = x_offset + border.left; + selection.y = y_offset + border.top; + selection.width = width; + selection.height = height; + view_rect_to_doc_rect (ev_view, &selection, &ev_view->selection); gtk_widget_queue_draw (widget); } @@ -504,9 +559,11 @@ void ev_view_copy (EvView *ev_view) { GtkClipboard *clipboard; + GdkRectangle selection; char *text; - text = ev_document_get_text (ev_view->document, &ev_view->selection); + doc_rect_to_view_rect (ev_view, &ev_view->selection, &selection); + text = ev_document_get_text (ev_view->document, &selection); clipboard = gtk_widget_get_clipboard (GTK_WIDGET (ev_view), GDK_SELECTION_CLIPBOARD); gtk_clipboard_set_text (clipboard, text, -1); @@ -520,9 +577,11 @@ ev_view_primary_get_cb (GtkClipboard *clipboard, gpointer data) { EvView *ev_view = EV_VIEW (data); + GdkRectangle selection; char *text; - text = ev_document_get_text (ev_view->document, &ev_view->selection); + doc_rect_to_view_rect (ev_view, &ev_view->selection, &selection); + text = ev_document_get_text (ev_view->document, &selection); gtk_selection_data_set_text (selection_data, text, -1); } @@ -633,6 +692,17 @@ ev_view_set_find_status (EvView *view, const char *message) g_object_notify (G_OBJECT (view), "find-status"); } +static GdkCursor * +ev_view_create_invisible_cursor(void) +{ + GdkBitmap *empty; + GdkColor black = { 0, 0, 0, 0 }; + static unsigned char bits[] = { 0x00 }; + + empty = gdk_bitmap_create_from_data (NULL, bits, 1, 1); + + return gdk_cursor_new_from_pixmap (empty, empty, &black, &black, 0, 0); +} static void ev_view_set_cursor (EvView *view, EvViewCursor new_cursor) @@ -659,6 +729,10 @@ ev_view_set_cursor (EvView *view, EvViewCursor new_cursor) case EV_VIEW_CURSOR_WAIT: cursor = gdk_cursor_new_for_display (display, GDK_WATCH); break; + case EV_VIEW_CURSOR_HIDDEN: + cursor = ev_view_create_invisible_cursor (); + break; + } if (cursor) { @@ -675,11 +749,14 @@ ev_view_motion_notify_event (GtkWidget *widget, EvView *view = EV_VIEW (widget); if (view->pressed_button > 0) { + GdkRectangle selection; + view->has_selection = TRUE; - 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; + selection.x = MIN (view->selection_start.x, event->x); + selection.y = MIN (view->selection_start.y, event->y); + selection.width = ABS (view->selection_start.x - event->x) + 1; + selection.height = ABS (view->selection_start.y - event->y) + 1; + view_rect_to_doc_rect (view, &selection, &view->selection); gtk_widget_queue_draw (widget); } else if (view->document) { @@ -959,7 +1036,6 @@ ev_view_init (EvView *view) view->scale = 1.0; view->pressed_button = -1; view->cursor = EV_VIEW_CURSOR_NORMAL; - view->sizing_mode = EV_SIZING_BEST_FIT; } static void @@ -973,7 +1049,12 @@ update_find_status_message (EvView *view) results = ev_document_find_get_n_results (EV_DOCUMENT_FIND (view->document)); - message = g_strdup_printf (_("%d found on this page"), + /* TRANS: Sometimes this could be better translated as + "%d hit(s) on this page". Therefore this string + contains plural cases. */ + message = g_strdup_printf (ngettext ("%d found on this page", + "%d found on this page", + results), results); } else { double percent; @@ -1012,7 +1093,9 @@ set_document_page (EvView *view, int new_page) &old_width, &old_height); if (old_page != page) { - ev_view_set_cursor (view, EV_VIEW_CURSOR_WAIT); + if (view->cursor != EV_VIEW_CURSOR_HIDDEN) { + ev_view_set_cursor (view, EV_VIEW_CURSOR_WAIT); + } ev_document_set_page (view->document, page); } @@ -1032,10 +1115,9 @@ set_document_page (EvView *view, int new_page) view->vadjustment->lower); } - view->find_page = page; - view->find_result = 0; - if (EV_IS_DOCUMENT_FIND (view->document)) { + view->find_page = page; + view->find_result = 0; update_find_status_message (view); } } @@ -1133,11 +1215,25 @@ find_changed_cb (EvDocument *document, int page, EvView *view) } static void -document_changed_callback (EvDocument *document, +page_changed_callback (EvDocument *document, EvView *view) { + LOG ("Page changed callback"); + gtk_widget_queue_draw (GTK_WIDGET (view)); - ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL); + + if (view->cursor != EV_VIEW_CURSOR_HIDDEN) { + ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL); + } +} + +static void +scale_changed_callback (EvDocument *document, + EvView *view) +{ + LOG ("Scale changed callback"); + + gtk_widget_queue_resize (GTK_WIDGET (view)); } /*** Public API ***/ @@ -1156,8 +1252,9 @@ ev_view_set_document (EvView *view, if (document != view->document) { if (view->document) { - g_signal_handlers_disconnect_by_func - (view->document, find_changed_cb, view); + g_signal_handlers_disconnect_by_func (view->document, + find_changed_cb, + view); g_object_unref (view->document); } @@ -1174,8 +1271,12 @@ ev_view_set_document (EvView *view, view); } g_signal_connect (view->document, - "changed", - G_CALLBACK (document_changed_callback), + "page_changed", + G_CALLBACK (page_changed_callback), + view); + g_signal_connect (view->document, + "scale_changed", + G_CALLBACK (scale_changed_callback), view); } @@ -1188,17 +1289,6 @@ ev_view_set_document (EvView *view, } } -void -ev_view_set_mode (EvView *view, - EvSizingMode sizing_mode) -{ - if (view->sizing_mode == sizing_mode) - return; - - view->sizing_mode = sizing_mode; - gtk_widget_queue_resize (GTK_WIDGET (view)); -} - static void go_to_link (EvView *view, EvLink *link) { @@ -1260,100 +1350,81 @@ ev_view_zoom (EvView *view, scale = CLAMP (scale, MIN_SCALE, MAX_SCALE); - if (ABS (scale - view->scale) < ZOOM_EPSILON) - return; - view->scale = scale; ev_document_set_scale (view->document, view->scale); - - gtk_widget_queue_resize (GTK_WIDGET (view)); } void ev_view_zoom_in (EvView *view) { + view->width = view->height = -1; ev_view_zoom (view, ZOOM_IN_FACTOR, TRUE); } void ev_view_zoom_out (EvView *view) { + view->width = view->height = -1; ev_view_zoom (view, ZOOM_OUT_FACTOR, TRUE); } -void -ev_view_normal_size (EvView *view) -{ - ev_view_zoom (view, 1.0, FALSE); -} - -/* Unfortunately this is not idempotent (!) (numerical stability - * issues because width and height are rounded) */ -void -ev_view_best_fit (EvView *view, int allocation_width, int allocation_height) +static double +size_to_zoom_factor (EvView *view, int width, int height) { - int target_width, target_height; - int width, height; + int doc_width, doc_height; + double scale, scale_w, scale_h; GtkBorder border; - if (view->document == NULL) - return; - - width = height = 0; - /* This is the bad part. You could make it stable by doing - * ev_document_set_scale 1.0. But at least with pdf this means - * redrawing the whole page */ - ev_document_get_page_size (view->document, -1, &width, &height); + doc_width = doc_height = 0; + scale = scale_w = scale_h = 1.0; + ev_document_get_page_size (view->document, -1, &doc_width, &doc_height); /* FIXME: The border size isn't constant. Ugh. Still, if we have extra * space, we just cut it from the border */ - ev_document_misc_get_page_border_size (width, height, &border); + ev_document_misc_get_page_border_size (doc_width, doc_height, &border); - target_width = allocation_width - (view->spacing * 2 + border.left + border.right); - target_height = allocation_height - (view->spacing * 2 + border.top + border.bottom); + if (doc_width == 0 && doc_height == 0) { + return 0; + } - LOG ("Best fit %d %d", allocation_width, allocation_height); + if (width >= 0) { + int target_width; - if (width != 0 && height != 0) { - double scale; - double scale_w, scale_h; + target_width = width - (view->spacing * 2 + border.left + border.right); + scale = scale_w = (double)target_width * view->scale / doc_width; + } - scale_w = (double)target_width * view->scale / width; - scale_h = (double)target_height * view->scale / height; + if (height >= 0) { + int target_height; - scale = (scale_w < scale_h) ? scale_w : scale_h; + target_height = height - (view->spacing * 2 + border.top + border.bottom); + scale = scale_h = (double)target_height * view->scale / doc_height; + } - ev_view_zoom (view, scale, FALSE); + if (width >= 0 && height >= 0) { + scale = (scale_w < scale_h) ? scale_w : scale_h; } + return scale; } void -ev_view_fit_width (EvView *view, int allocation_width, int allocation_height, - int vsb_width) +ev_view_set_size (EvView *view, + int width, + int height) { - int target_width, target_height; - int width, height; - GtkBorder border; + double factor; - if (view->document == NULL) + if (!view->document) { return; + } - width = height = 0; - ev_document_get_page_size (view->document, -1, &width, &height); - ev_document_misc_get_page_border_size (width, height, &border); - - target_width = allocation_width - (view->spacing * 2 + border.left + border.right); - target_height = allocation_height - (view->spacing * 2 + border.top + border.bottom); - - if (width) { - double scale; - scale = (double)target_width * view->scale / width; - - if (height * scale / view->scale > target_height) - scale = ((double)(target_width - vsb_width) * view->scale / width); - - ev_view_zoom (view, scale, FALSE); + if (view->width != width || + view->height != height) { + view->width = width; + view->height = height; + factor = size_to_zoom_factor (view, width, height); + ev_view_zoom (view, factor, FALSE); } } @@ -1424,3 +1495,14 @@ ev_view_find_previous (EvView *view) gtk_widget_queue_draw (GTK_WIDGET (view)); } } +void +ev_view_hide_cursor (EvView *view) +{ + ev_view_set_cursor (view, EV_VIEW_CURSOR_HIDDEN); +} + +void +ev_view_show_cursor (EvView *view) +{ + ev_view_set_cursor (view, EV_VIEW_CURSOR_LINK); +}