]> www.fi.muni.cz Git - evince.git/blobdiff - shell/ev-view.c
add zooming.
[evince.git] / shell / ev-view.c
index 9fbc81afb8c588c2ae794b5bdd6dce6fe9dc3dfe..78a75d99e8889d2df17bb6fa42d46031cc4a35cc 100644 (file)
@@ -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
@@ -38,6 +39,10 @@ struct _EvView {
 
        GtkAdjustment *hadjustment;
        GtkAdjustment *vadjustment;
+
+        GArray *find_results;
+
+       double scale;
 };
 
 struct _EvViewClass {
@@ -46,8 +51,13 @@ struct _EvViewClass {
        void    (*set_scroll_adjustments) (EvView         *view,
                                           GtkAdjustment  *hadjustment,
                                           GtkAdjustment  *vadjustment);
+       
+       /* Should this be notify::page? */
+       void    (*page_changed)           (EvView         *view);
 };
 
+static guint page_changed_signal = 0;
+
 static void ev_view_set_scroll_adjustments (EvView         *view,
                                            GtkAdjustment  *hadjustment,
                                            GtkAdjustment  *vadjustment);
@@ -130,6 +140,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);
 }
 
@@ -147,10 +160,19 @@ static void
 ev_view_size_request (GtkWidget      *widget,
                      GtkRequisition *requisition)
 {
-       /* EvView *view = EV_VIEW (widget); */
+       EvView *view = EV_VIEW (widget);
+
+       if (GTK_WIDGET_REALIZED (widget)) {
+               if (view->document) {
+                       ev_document_get_page_size (view->document,
+                                                  &requisition->width,
+                                                  &requisition->height);
+               } else {
+                       requisition->width = 10;
+                       requisition->height = 10;
+               }
+       }
   
-       requisition->width = 500;
-       requisition->height = 500;
 }
 
 static void
@@ -223,9 +245,17 @@ ev_view_realize (GtkWidget *widget)
        gdk_window_set_user_data (view->bin_window, widget);
        gdk_window_show (view->bin_window);
 
-       if (view->document)
+       if (view->document) {
                ev_document_set_target (view->document, view->bin_window);
 
+               /* We can't get page size without a target, so we have to
+                * queue a size request at realization. Could be fixed
+                * with EvDocument changes to allow setting a GdkScreen
+                * without setting a target.
+                */
+               gtk_widget_queue_resize (widget);
+       }
+
        update_window_backgrounds (view);
 }
 
@@ -263,11 +293,35 @@ expose_bin_window (GtkWidget      *widget,
                   GdkEventExpose *event)
 {
        EvView *view = EV_VIEW (widget);
+        int i;
+        const EvFindResult *results;
        
        if (view->document)
                ev_document_render (view->document,
                                    event->area.x, event->area.y,
                                    event->area.width, event->area.height);
+
+        results = (EvFindResult*) view->find_results->data;
+        i = 0;
+        while (i < view->find_results->len) {
+#if 0
+                g_printerr ("highlighting result %d at %d,%d %dx%d\n",
+                            i,
+                            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) FIXME
+                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
@@ -282,7 +336,6 @@ ev_view_expose_event (GtkWidget      *widget,
                return GTK_WIDGET_CLASS (ev_view_parent_class)->expose_event (widget, event);
 
        return FALSE;
-  
 }
 
 static gboolean
@@ -393,14 +446,46 @@ ev_view_class_init (EvViewClass *class)
                                                                     G_TYPE_NONE, 2,
                                                                     GTK_TYPE_ADJUSTMENT,
                                                                     GTK_TYPE_ADJUSTMENT);
+       page_changed_signal = g_signal_new ("page-changed",
+                                           G_OBJECT_CLASS_TYPE (object_class),
+                                           G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
+                                           G_STRUCT_OFFSET (EvViewClass, page_changed),
+                                           NULL, NULL,
+                                           ev_marshal_VOID__NONE,
+                                           G_TYPE_NONE, 0);
 }
 
 static void
 ev_view_init (EvView *view)
 {
        static const GdkColor white = { 0, 0xffff, 0xffff, 0xffff };
+
+       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));
+}
+
+
+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);
+  
+  gtk_widget_queue_draw (GTK_WIDGET (view));
 }
 
 /*** Public API ***/       
@@ -418,17 +503,137 @@ ev_view_set_document (EvView     *view,
        g_return_if_fail (EV_IS_VIEW (view));
 
        if (document != view->document) {
-               if (view->document)
+               int old_page = ev_view_get_page (view);
+               
+               if (view->document) {
                        g_object_unref (view->document);
+                        g_signal_handlers_disconnect_by_func (view->document,
+                                                              found_results_callback,
+                                                              view);
+                        g_array_set_size (view->find_results, 0);
+                }
 
                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);
+                }
 
                if (GTK_WIDGET_REALIZED (view))
                        ev_document_set_target (view->document, view->bin_window);
                
                gtk_widget_queue_resize (GTK_WIDGET (view));
+               
+               if (old_page != ev_view_get_page (view))
+                       g_signal_emit (view, page_changed_signal, 0);
+       }
+}
+
+void
+ev_view_set_page (EvView *view,
+                 int     page)
+{
+       if (view->document) {
+               int old_page = ev_document_get_page (view->document);
+               if (old_page != page)
+                       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));
+               }
+       }
+}
+
+int
+ev_view_get_page (EvView *view)
+{
+       if (view->document)
+               return ev_document_get_page (view->document);
+       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 / width;
+               scale_h = (double)GTK_WIDGET (view)->allocation.height / 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 / width;
+
+       ev_view_zoom (view, scale, FALSE);
 }