+ view->drag_info.in_drag = FALSE;
+ view->selection_info.in_selection = FALSE;
+
+ view->selection_mode = EV_VIEW_SELECTION_TEXT;
+ view->continuous = TRUE;
+ view->dual_page = FALSE;
+ view->presentation = FALSE;
+ view->fullscreen = FALSE;
+ view->sizing_mode = EV_SIZING_FIT_WIDTH;
+ view->pending_scroll = SCROLL_TO_KEEP_POSITION;
+}
+
+/*** Callbacks ***/
+
+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 (view->current_page == page)
+ gtk_widget_queue_draw (GTK_WIDGET (view));
+}
+
+static void
+job_finished_cb (EvPixbufCache *pixbuf_cache,
+ EvView *view)
+{
+ gtk_widget_queue_draw (GTK_WIDGET (view));
+}
+
+static void
+page_changed_cb (EvPageCache *page_cache,
+ int new_page,
+ EvView *view)
+{
+ if (view->current_page != new_page) {
+
+ view->current_page = new_page;
+ view->pending_scroll = SCROLL_TO_CURRENT_PAGE;
+ gtk_widget_queue_resize (GTK_WIDGET (view));
+
+ if (EV_IS_DOCUMENT_FIND (view->document)) {
+ view->find_page = new_page;
+ view->find_result = 0;
+ update_find_status_message (view);
+ }
+ }
+}
+
+static void on_adjustment_value_changed (GtkAdjustment *adjustment,
+ EvView *view)
+{
+ int dx = 0, dy = 0;
+
+ if (! GTK_WIDGET_REALIZED (view))
+ return;
+
+ if (view->hadjustment) {
+ dx = view->scroll_x - (int) view->hadjustment->value;
+ view->scroll_x = (int) view->hadjustment->value;
+ } else {
+ view->scroll_x = 0;
+ }
+
+ if (view->vadjustment) {
+ dy = view->scroll_y - (int) view->vadjustment->value;
+ view->scroll_y = (int) view->vadjustment->value;
+ } else {
+ view->scroll_y = 0;
+ }
+
+
+ if (view->pending_resize)
+ gtk_widget_queue_draw (GTK_WIDGET (view));
+ else
+ gdk_window_scroll (GTK_WIDGET (view)->window, dx, dy);
+
+
+ if (view->document)
+ view_update_range_and_current_page (view);
+}
+
+GtkWidget*
+ev_view_new (void)
+{
+ GtkWidget *view;
+
+ view = g_object_new (EV_TYPE_VIEW, NULL);
+
+ return view;
+}
+
+static void
+setup_caches (EvView *view)
+{
+ view->page_cache = ev_page_cache_get (view->document);
+ g_signal_connect (view->page_cache, "page-changed", G_CALLBACK (page_changed_cb), view);
+ view->pixbuf_cache = ev_pixbuf_cache_new (GTK_WIDGET (view), view->document);
+ g_signal_connect (view->pixbuf_cache, "job-finished", G_CALLBACK (job_finished_cb), view);
+}
+
+static void
+clear_caches (EvView *view)
+{
+ if (view->pixbuf_cache) {
+ g_object_unref (view->pixbuf_cache);
+ view->pixbuf_cache = NULL;
+ }
+
+ if (view->page_cache) {
+ g_object_unref (view->page_cache);
+ view->page_cache = NULL;
+ }
+}
+
+void
+ev_view_set_document (EvView *view,
+ EvDocument *document)
+{
+ g_return_if_fail (EV_IS_VIEW (view));
+
+ if (document != view->document) {
+ clear_caches (view);
+
+ if (view->document) {
+ g_signal_handlers_disconnect_by_func (view->document,
+ find_changed_cb,
+ view);
+ g_object_unref (view->document);
+ view->page_cache = NULL;
+
+ }
+
+ view->document = document;
+ view->find_page = 0;
+ view->find_result = 0;
+
+ if (view->document) {
+ g_object_ref (view->document);
+ if (EV_IS_DOCUMENT_FIND (view->document)) {
+ g_signal_connect (view->document,
+ "find_changed",
+ G_CALLBACK (find_changed_cb),
+ view);
+ }
+
+ setup_caches (view);
+ }
+
+ gtk_widget_queue_resize (GTK_WIDGET (view));
+ }
+}
+
+/*** Zoom and sizing mode ***/
+
+#define EPSILON 0.0000001
+void
+ev_view_set_zoom (EvView *view,
+ double factor,
+ gboolean relative)
+{
+ double scale;
+
+ if (relative)
+ scale = view->scale * factor;
+ else
+ scale = factor;
+
+ scale = CLAMP (scale, MIN_SCALE, MAX_SCALE);
+
+ if (ABS (view->scale - scale) < EPSILON)
+ return;
+
+ view->scale = scale;
+ view->pending_resize = TRUE;
+
+ gtk_widget_queue_resize (GTK_WIDGET (view));
+
+ g_object_notify (G_OBJECT (view), "zoom");
+}
+
+double
+ev_view_get_zoom (EvView *view)
+{
+ return view->scale;
+}
+
+gboolean
+ev_view_get_continuous (EvView *view)
+{
+ g_return_val_if_fail (EV_IS_VIEW (view), FALSE);
+
+ return view->continuous;
+}
+
+void
+ev_view_set_continuous (EvView *view,
+ gboolean continuous)
+{
+ g_return_if_fail (EV_IS_VIEW (view));
+
+ continuous = continuous != FALSE;
+
+ if (view->continuous != continuous) {
+ view->continuous = continuous;
+ view->pending_scroll = SCROLL_TO_CURRENT_PAGE;
+ gtk_widget_queue_resize (GTK_WIDGET (view));
+ }
+
+ g_object_notify (G_OBJECT (view), "continuous");
+}
+
+gboolean
+ev_view_get_dual_page (EvView *view)
+{
+ g_return_val_if_fail (EV_IS_VIEW (view), FALSE);
+
+ return view->dual_page;
+}
+
+void
+ev_view_set_dual_page (EvView *view,
+ gboolean dual_page)
+{
+ g_return_if_fail (EV_IS_VIEW (view));
+
+ dual_page = dual_page != FALSE;
+
+ if (view->dual_page == dual_page)
+ return;
+
+ view->pending_scroll = SCROLL_TO_CURRENT_PAGE;
+ view->dual_page = dual_page;
+ /* FIXME: if we're keeping the pixbuf cache around, we should extend the
+ * preload_cache_size to be 2 if dual_page is set.
+ */
+ gtk_widget_queue_resize (GTK_WIDGET (view));
+
+ g_object_notify (G_OBJECT (view), "dual-page");
+}
+
+void
+ev_view_set_fullscreen (EvView *view,
+ gboolean fullscreen)
+{
+ g_return_if_fail (EV_IS_VIEW (view));
+
+ fullscreen = fullscreen != FALSE;
+
+ if (view->fullscreen == fullscreen)
+ return;
+
+ view->fullscreen = fullscreen;
+ gtk_widget_queue_resize (GTK_WIDGET (view));
+
+ g_object_notify (G_OBJECT (view), "fullscreen");
+}
+
+gboolean
+ev_view_get_fullscreen (EvView *view)
+{
+ g_return_val_if_fail (EV_IS_VIEW (view), FALSE);
+
+ return view->fullscreen;
+}
+
+void
+ev_view_set_presentation (EvView *view,
+ gboolean presentation)
+{
+ g_return_if_fail (EV_IS_VIEW (view));
+
+ presentation = presentation != FALSE;
+
+ if (view->presentation == presentation)
+ return;
+
+ view->presentation = presentation;
+ view->pending_scroll = SCROLL_TO_CURRENT_PAGE;
+ gtk_widget_queue_resize (GTK_WIDGET (view));
+
+ if (GTK_WIDGET_REALIZED (view)) {
+ if (view->presentation)
+ gdk_window_set_background (GTK_WIDGET(view)->window,
+ >K_WIDGET (view)->style->black);
+ else
+ gdk_window_set_background (GTK_WIDGET(view)->window,
+ >K_WIDGET (view)->style->mid [GTK_STATE_NORMAL]);
+ }
+
+
+ g_object_notify (G_OBJECT (view), "presentation");
+}
+
+gboolean
+ev_view_get_presentation (EvView *view)
+{
+ g_return_val_if_fail (EV_IS_VIEW (view), FALSE);
+
+ return view->presentation;
+}
+
+void
+ev_view_set_sizing_mode (EvView *view,
+ EvSizingMode sizing_mode)
+{
+ g_return_if_fail (EV_IS_VIEW (view));
+
+ if (view->sizing_mode == sizing_mode)
+ return;
+
+ view->sizing_mode = sizing_mode;
+ gtk_widget_queue_resize (GTK_WIDGET (view));
+
+ g_object_notify (G_OBJECT (view), "sizing-mode");
+}
+
+EvSizingMode
+ev_view_get_sizing_mode (EvView *view)
+{
+ g_return_val_if_fail (EV_IS_VIEW (view), EV_SIZING_FREE);
+
+ return view->sizing_mode;
+}
+
+gboolean
+ev_view_can_zoom_in (EvView *view)
+{
+ return view->scale * ZOOM_IN_FACTOR <= MAX_SCALE;
+}
+
+gboolean
+ev_view_can_zoom_out (EvView *view)
+{
+ return view->scale * ZOOM_OUT_FACTOR >= MIN_SCALE;
+}
+
+void
+ev_view_zoom_in (EvView *view)
+{
+ g_return_if_fail (view->sizing_mode == EV_SIZING_FREE);
+
+ view->pending_scroll = SCROLL_TO_CENTER;
+ ev_view_set_zoom (view, ZOOM_IN_FACTOR, TRUE);
+}
+
+void
+ev_view_zoom_out (EvView *view)
+{
+ g_return_if_fail (view->sizing_mode == EV_SIZING_FREE);
+
+ view->pending_scroll = SCROLL_TO_CENTER;
+ ev_view_set_zoom (view, ZOOM_OUT_FACTOR, TRUE);
+}
+
+static void
+ev_view_set_rotation (EvView *view, int rotation)
+{
+ view->rotation = rotation;
+
+ ev_pixbuf_cache_clear (view->pixbuf_cache);
+ gtk_widget_queue_resize (GTK_WIDGET (view));
+}
+
+void
+ev_view_rotate_right (EvView *view)
+{
+ int rotation = view->rotation + 90;
+
+ if (rotation >= 360) {
+ rotation -= 360;
+ }
+
+ ev_view_set_rotation (view, rotation);
+}
+
+void
+ev_view_rotate_left (EvView *view)
+{
+ int rotation = view->rotation - 90;
+
+ if (rotation < 0) {
+ rotation += 360;
+ }
+
+ ev_view_set_rotation (view, rotation);
+}
+
+static double
+zoom_for_size_fit_width (int doc_width,
+ int doc_height,
+ int target_width,
+ int target_height,
+ int vsb_width)
+{
+ double scale;
+
+ scale = (double)target_width / doc_width;
+
+ if (doc_height * scale > target_height)
+ scale = (double) (target_width - vsb_width) / doc_width;
+
+ return scale;
+}
+
+static double
+zoom_for_size_best_fit (int doc_width,
+ int doc_height,
+ int target_width,
+ int target_height,
+ int vsb_width,
+ int hsb_width)
+{
+ double w_scale;
+ double h_scale;
+
+ w_scale = (double)target_width / doc_width;
+ h_scale = (double)target_height / doc_height;
+
+ if (doc_height * w_scale > target_height)
+ w_scale = (double) (target_width - vsb_width) / doc_width;
+ if (doc_width * h_scale > target_width)
+ h_scale = (double) (target_height - hsb_width) / doc_height;
+
+ return MIN (w_scale, h_scale);
+}
+
+
+static void
+ev_view_zoom_for_size_presentation (EvView *view,
+ int width,
+ int height)
+{
+ int doc_width, doc_height;
+ gdouble scale;
+
+ ev_page_cache_get_size (view->page_cache,
+ view->current_page,
+ view->rotation,
+ 1.0,
+ &doc_width,
+ &doc_height);
+ scale = zoom_for_size_best_fit (doc_width, doc_height, width, height, 0, 0);
+ ev_view_set_zoom (view, scale, FALSE);
+}
+
+static void
+ev_view_zoom_for_size_continuous_and_dual_page (EvView *view,
+ int width,
+ int height,
+ int vsb_width,
+ int hsb_height)
+{
+ int doc_width, doc_height;
+ GtkBorder border;
+ gdouble scale;
+
+ ev_page_cache_get_max_width (view->page_cache,
+ view->rotation,
+ 1.0,
+ &doc_width);
+ ev_page_cache_get_max_height (view->page_cache,
+ view->rotation,
+ 1.0,
+ &doc_height);
+ compute_border (view, doc_width, doc_height, &border);
+
+ doc_width = doc_width * 2;
+ width -= (2 * (border.left + border.right) + 3 * view->spacing);
+ height -= (border.top + border.bottom + 2 * view->spacing - 1);
+
+ /* FIXME: We really need to calculate the overall height here, not the
+ * page height. We assume there's always a vertical scrollbar for
+ * now. We need to fix this. */
+ if (view->sizing_mode == EV_SIZING_FIT_WIDTH)
+ scale = zoom_for_size_fit_width (doc_width, doc_height, width - vsb_width, height, 0);
+ else if (view->sizing_mode == EV_SIZING_BEST_FIT)
+ scale = zoom_for_size_best_fit (doc_width, doc_height, width - vsb_width, height, 0, hsb_height);
+ else
+ g_assert_not_reached ();
+
+ ev_view_set_zoom (view, scale, FALSE);
+}
+
+static void
+ev_view_zoom_for_size_continuous (EvView *view,
+ int width,
+ int height,
+ int vsb_width,
+ int hsb_height)
+{
+ int doc_width, doc_height;
+ GtkBorder border;
+ gdouble scale;
+
+ ev_page_cache_get_max_width (view->page_cache,
+ view->rotation,
+ 1.0,
+ &doc_width);
+ ev_page_cache_get_max_height (view->page_cache,
+ view->rotation,
+ 1.0,
+ &doc_height);
+ compute_border (view, doc_width, doc_height, &border);
+
+ width -= (border.left + border.right + 2 * view->spacing);
+ height -= (border.top + border.bottom + 2 * view->spacing - 1);
+
+ /* FIXME: We really need to calculate the overall height here, not the
+ * page height. We assume there's always a vertical scrollbar for
+ * now. We need to fix this. */
+ if (view->sizing_mode == EV_SIZING_FIT_WIDTH)
+ scale = zoom_for_size_fit_width (doc_width, doc_height, width - vsb_width, height, 0);
+ else if (view->sizing_mode == EV_SIZING_BEST_FIT)
+ scale = zoom_for_size_best_fit (doc_width, doc_height, width - vsb_width, height, 0, hsb_height);
+ else
+ g_assert_not_reached ();
+
+ ev_view_set_zoom (view, scale, FALSE);
+}
+
+static void
+ev_view_zoom_for_size_dual_page (EvView *view,
+ int width,
+ int height,
+ int vsb_width,
+ int hsb_height)
+{
+ GtkBorder border;
+ gint doc_width, doc_height;
+ gdouble scale;
+ gint other_page;
+
+ other_page = view->current_page ^ 1;
+
+ /* Find the largest of the two. */
+ ev_page_cache_get_size (view->page_cache,
+ view->current_page,
+ view->rotation,
+ 1.0,
+ &doc_width, &doc_height);
+
+ if (other_page < ev_page_cache_get_n_pages (view->page_cache)) {
+ gint width_2, height_2;
+ ev_page_cache_get_size (view->page_cache,
+ other_page,
+ view->rotation,
+ 1.0,
+ &width_2, &height_2);
+ if (width_2 > doc_width)
+ doc_width = width_2;
+ if (height_2 > doc_height)
+ doc_height = height_2;
+ }
+ compute_border (view, doc_width, doc_height, &border);
+
+ doc_width = doc_width * 2;
+ width -= ((border.left + border.right)* 2 + 3 * view->spacing);
+ height -= (border.top + border.bottom + 2 * view->spacing);
+
+ if (view->sizing_mode == EV_SIZING_FIT_WIDTH)
+ scale = zoom_for_size_fit_width (doc_width, doc_height, width, height, vsb_width);
+ else if (view->sizing_mode == EV_SIZING_BEST_FIT)
+ scale = zoom_for_size_best_fit (doc_width, doc_height, width, height, vsb_width, hsb_height);
+ else
+ g_assert_not_reached ();
+
+ ev_view_set_zoom (view, scale, FALSE);
+}
+
+static void
+ev_view_zoom_for_size_single_page (EvView *view,
+ int width,
+ int height,
+ int vsb_width,
+ int hsb_height)
+{
+ int doc_width, doc_height;
+ GtkBorder border;
+ gdouble scale;
+
+ ev_page_cache_get_size (view->page_cache,
+ view->current_page,
+ view->rotation,
+ 1.0,
+ &doc_width,
+ &doc_height);
+ /* Get an approximate border */
+ compute_border (view, width, height, &border);
+
+ width -= (border.left + border.right + 2 * view->spacing);
+ height -= (border.top + border.bottom + 2 * view->spacing);
+
+ if (view->sizing_mode == EV_SIZING_FIT_WIDTH)
+ scale = zoom_for_size_fit_width (doc_width, doc_height, width, height, vsb_width);
+ else if (view->sizing_mode == EV_SIZING_BEST_FIT)
+ scale = zoom_for_size_best_fit (doc_width, doc_height, width, height, vsb_width, hsb_height);
+ else
+ g_assert_not_reached ();
+
+ ev_view_set_zoom (view, scale, FALSE);
+}
+
+void
+ev_view_set_zoom_for_size (EvView *view,
+ int width,
+ int height,
+ int vsb_width,
+ int hsb_height)
+{
+ g_return_if_fail (view->sizing_mode == EV_SIZING_FIT_WIDTH ||
+ view->sizing_mode == EV_SIZING_BEST_FIT);
+ g_return_if_fail (width >= 0);
+ g_return_if_fail (height >= 0);
+
+ if (view->document == NULL)
+ return;
+
+ if (view->presentation)
+ ev_view_zoom_for_size_presentation (view, width, height);
+ else if (view->continuous && view->dual_page)
+ ev_view_zoom_for_size_continuous_and_dual_page (view, width, height, vsb_width, hsb_height);
+ else if (view->continuous)
+ ev_view_zoom_for_size_continuous (view, width, height, vsb_width, hsb_height);
+ else if (view->dual_page)
+ ev_view_zoom_for_size_dual_page (view, width, height, vsb_width, hsb_height);
+ else
+ ev_view_zoom_for_size_single_page (view, width, height, vsb_width, hsb_height);
+}
+
+/*** Status text messages ***/
+
+const char *
+ev_view_get_status (EvView *view)
+{
+ g_return_val_if_fail (EV_IS_VIEW (view), NULL);
+
+ return view->status;
+}
+
+static void
+ev_view_set_status (EvView *view, const char *message)
+{
+ g_return_if_fail (EV_IS_VIEW (view));
+
+ if (message != view->status) {
+ g_free (view->status);
+ view->status = g_strdup (message);
+ g_object_notify (G_OBJECT (view), "status");
+ }