]> www.fi.muni.cz Git - evince.git/blobdiff - shell/ev-view.c
Removed EggRecentViewGtk
[evince.git] / shell / ev-view.c
index 45f5de07fa748735d73f27c87e853b0be867ffe7..5cc6f57d549ec305d375d6cffa1c23ca18196793 100644 (file)
@@ -141,7 +141,6 @@ struct _EvView {
        int spacing;
 
        double scale;
-       GtkBorder border;
 
        gboolean continuous;
        gboolean dual_page;
@@ -166,6 +165,8 @@ struct _EvViewClass {
 };
 
 /*** Scrolling ***/
+static void       scroll_to_current_page                    (EvView *view, 
+                                                             GtkOrientation orientation);
 static void       ev_view_set_scroll_adjustments             (EvView             *view,
                                                              GtkAdjustment      *hadjustment,
                                                              GtkAdjustment      *vadjustment);
@@ -191,6 +192,10 @@ static void       compute_border                             (EvView
                                                              int                 width,
                                                              int                 height,
                                                              GtkBorder          *border);
+static void       get_page_y_offset                          (EvView *view,
+                                                             int page,
+                                                             double zoom,
+                                                             int *y_offset);
 static gboolean   get_page_extents                           (EvView             *view,
                                                              gint                page,
                                                              GdkRectangle       *page_area,
@@ -203,9 +208,6 @@ static void       doc_rect_to_view_rect                      (EvView
                                                              int                 page,
                                                              EvRectangle        *doc_rect,
                                                              GdkRectangle       *view_rect);
-static void       get_bounding_box_size                      (EvView             *view,
-                                                             int                *max_width,
-                                                             int                *max_height);
 static void       find_page_at_location                      (EvView             *view,
                                                              gdouble             x,
                                                              gdouble             y,
@@ -357,35 +359,25 @@ G_DEFINE_TYPE (EvView, ev_view, GTK_TYPE_WIDGET)
 static void
 scroll_to_current_page (EvView *view, GtkOrientation orientation)
 {
-       int max_width, max_height, n_rows;
-       
-       get_bounding_box_size (view, &max_width, &max_height);
-       
+       GdkRectangle page_area;
+       GtkBorder border;
+
+       get_page_extents (view, view->current_page, &page_area, &border);
+
        if (orientation == GTK_ORIENTATION_VERTICAL) {  
                if (view->continuous) {
-                       n_rows = view->dual_page ? view->current_page / 2 : view->current_page;
-
                        gtk_adjustment_clamp_page (view->vadjustment,
-                                                  (max_height + view->spacing) * n_rows,
-                                                  (max_height + view->spacing) * n_rows +
-                                                  view->vadjustment->page_size);
+                                                  page_area.y - view->spacing,
+                                                  page_area.y + view->vadjustment->page_size);
                } else {
                        gtk_adjustment_set_value (view->vadjustment,
                                                  view->vadjustment->lower);
                }
        } else {
                if (view->dual_page) {
-                       if (view->current_page % 2 == 0) {
-                               gtk_adjustment_set_value (view->hadjustment,
-                                                         view->hadjustment->lower);
-                       } else {
-                               gtk_adjustment_clamp_page (view->hadjustment,
-                                                          view->hadjustment->lower + 
-                                                          max_width + view->spacing, 
-                                                          view->hadjustment->lower +
-                                                          max_width + view->spacing +
-                                                          view->hadjustment->page_size);
-                       }
+                       gtk_adjustment_clamp_page (view->hadjustment,
+                                                  page_area.x, 
+                                                  page_area.x + view->hadjustment->page_size);
                } else {
                        gtk_adjustment_set_value (view->hadjustment,
                                                  CLAMP (view->hadjustment->value, 
@@ -424,7 +416,7 @@ view_set_adjustment_values (EvView         *view,
        factor = 1.0;
        switch (view->pending_scroll) {
                case SCROLL_TO_KEEP_POSITION: 
-                       factor = adjustment->value / adjustment->upper;
+                       factor = (adjustment->value) / adjustment->upper;
                        break;
                case SCROLL_TO_CURRENT_PAGE: 
                        break;
@@ -439,16 +431,19 @@ view_set_adjustment_values (EvView         *view,
        adjustment->lower = 0;
        adjustment->upper = MAX (allocation, requisition);
 
+       /* 
+        * We add 0.5 to the values before to average out our rounding errors.
+        */
        switch (view->pending_scroll) {
                case SCROLL_TO_KEEP_POSITION: 
-                       new_value = CLAMP (adjustment->upper * factor, 0, adjustment->upper - adjustment->page_size);
+                       new_value = CLAMP (adjustment->upper * factor + 0.5, 0, adjustment->upper - adjustment->page_size);
                        gtk_adjustment_set_value (adjustment, (int)new_value);
                        break;
                case SCROLL_TO_CURRENT_PAGE: 
                        scroll_to_current_page (view, orientation);
                        break;
                case SCROLL_TO_CENTER: 
-                       new_value = CLAMP (adjustment->upper * factor - adjustment->page_size * 0.5,
+                       new_value = CLAMP (adjustment->upper * factor - adjustment->page_size * 0.5 + 0.5,
                                           0, adjustment->upper - adjustment->page_size);
                        gtk_adjustment_set_value (adjustment, (int)new_value);
                        break;
@@ -466,31 +461,23 @@ view_update_range_and_current_page (EvView *view)
                view->end_page = view->current_page;
        } else if (view->continuous) {
                GdkRectangle current_area, unused, page_area;
+               GtkBorder border;
                gint current_page;
                gboolean found = FALSE;
                int i;
                
-               get_bounding_box_size (view, &(page_area.width), &(page_area.height));
-               page_area.x = view->spacing;
-               page_area.y = view->spacing;
-
-               if (view->hadjustment) {
-                       current_area.x = view->hadjustment->value;
-                       current_area.width = view->hadjustment->page_size;
-               } else {
-                       current_area.x = page_area.x;
-                       current_area.width = page_area.width;
-               }
-
-               if (view->vadjustment) {
-                       current_area.y = view->vadjustment->value;
-                       current_area.height = view->vadjustment->page_size;
-               } else {
-                       current_area.y = page_area.y;
-                       current_area.height = page_area.height;
-               }
+               if (!(view->vadjustment && view->hadjustment))
+                       return;
+               
+               current_area.x = view->hadjustment->value;
+               current_area.width = view->hadjustment->upper;
+               current_area.y = view->vadjustment->value;
+               current_area.height = view->vadjustment->page_size;
 
                for (i = 0; i < ev_page_cache_get_n_pages (view->page_cache); i++) {
+
+                       get_page_extents (view, i, &page_area, &border);
+                       
                        if (gdk_rectangle_intersect (&current_area, &page_area, &unused)) {
                                if (! found) {
                                        view->start_page = i;
@@ -501,18 +488,8 @@ view_update_range_and_current_page (EvView *view)
                        } else if (found) {
                                break;
                        }
-                       if (view->dual_page) {
-                               if (i % 2 == 0) {
-                                       page_area.x += page_area.width + view->spacing;
-                               } else {
-                                       page_area.x = view->spacing;
-                                       page_area.y += page_area.height + view->spacing;
-                               }
-                       } else {
-                               page_area.y += page_area.height + view->spacing;
-                       }
                }
-
+               
                current_page = ev_page_cache_get_current_page (view->page_cache);
 
                if (current_page < view->start_page || current_page > view->end_page) {
@@ -737,6 +714,32 @@ compute_border (EvView *view, int width, int height, GtkBorder *border)
        }
 }
 
+static void       get_page_y_offset                          (EvView *view,
+                                                             int page,
+                                                             double zoom,
+                                                             int *y_offset)
+{
+       int max_width, offset;
+       GtkBorder border;
+       
+       g_return_if_fail (y_offset != NULL);
+               
+       ev_page_cache_get_max_width (view->page_cache, zoom, &max_width);
+
+       compute_border (view, max_width, max_width, &border);
+       
+       if (view->dual_page) {
+               ev_page_cache_get_height_to_page (view->page_cache, page, zoom, NULL, &offset);
+               offset += (page / 2 + 1) * view->spacing + (page / 2) * (border.top + border.bottom);
+       } else {
+               ev_page_cache_get_height_to_page (view->page_cache, page, zoom, &offset, NULL);
+               offset += (page + 1) * view->spacing + page * (border.top + border.bottom);
+       }
+       
+       *y_offset = offset;
+       return;
+}
+
 static gboolean
 get_page_extents (EvView       *view,
                  gint          page,
@@ -748,22 +751,6 @@ get_page_extents (EvView       *view,
 
        widget = GTK_WIDGET (view);
 
-       /* Quick sanity check */
-       if (view->presentation) {
-               if (view->current_page != page)
-                       return FALSE;
-       } else if (view->continuous) {
-               if (page < view->start_page ||
-                   page > view->end_page)
-                       return FALSE;
-       } else if (view->dual_page) {
-               if (ABS (page - view->current_page) > 1)
-                       return FALSE;
-       } else {
-               if (view->current_page != page)
-                       return FALSE;
-       }
-
        /* Get the size of the page */
        ev_page_cache_get_size (view->page_cache, page,
                                view->scale,
@@ -776,20 +763,24 @@ get_page_extents (EvView       *view,
                page_area->x = (MAX (0, widget->allocation.width - width))/2;
                page_area->y = (MAX (0, widget->allocation.height - height))/2;
        } else if (view->continuous) {
-               gint max_width, max_height;
+               gint max_width;
                gint x, y;
 
-               get_bounding_box_size (view, &max_width, &max_height);
+               ev_page_cache_get_max_width (view->page_cache, view->scale, &max_width);
+               max_width = max_width + border->left + border->right;
                /* Get the location of the bounding box */
                if (view->dual_page) {
                        x = view->spacing + (page % 2) * (max_width + view->spacing);
-                       y = view->spacing + (page / 2) * (max_height + view->spacing);
-                       x = x + MAX (0, widget->allocation.width - (max_width * 2 + view->spacing * 3))/2;
+                       x = x + MAX (0, widget->allocation.width - (max_width * 2 + view->spacing * 3)) / 2;
+                       if (page % 2 == 0)
+                               x = x + (max_width - width - border->left - border->right);
                } else {
                        x = view->spacing;
-                       y = view->spacing + page * (max_height + view->spacing);
-                       x = x + MAX (0, widget->allocation.width - (max_width + view->spacing * 2))/2;
+                       x = x + MAX (0, widget->allocation.width - (width + view->spacing * 2)) / 2;
                }
+
+               get_page_y_offset (view, page, view->scale, &y);
+
                page_area->x = x;
                page_area->y = y;
        } else {
@@ -806,7 +797,7 @@ get_page_extents (EvView       *view,
                        /* First, we get the bounding box of the two pages */
                        if (other_page < ev_page_cache_get_n_pages (view->page_cache)) {
                                ev_page_cache_get_size (view->page_cache,
-                                                       page + 1,
+                                                       other_page,
                                                        view->scale,
                                                        &width_2, &height_2);
                                if (width_2 > width)
@@ -880,30 +871,6 @@ doc_rect_to_view_rect (EvView       *view,
        view_rect->height = ceil (height * view->scale);
 }
 
-static void
-get_bounding_box_size (EvView *view, int *max_width, int *max_height)
-{
-       GtkBorder border;
-       int width, height;
-
-       if (max_width) {
-               ev_page_cache_get_max_width_size (view->page_cache,
-                                                 view->scale,
-                                                 &width, &height);
-               compute_border (view, width, height, &border);
-               *max_width = width + border.left + border.right;
-       }
-
-
-       if (max_height) {
-               ev_page_cache_get_max_height_size (view->page_cache,
-                                                  view->scale,
-                                                  &width, &height);
-               compute_border (view, width, height, &border);
-               *max_height = height + border.top + border.bottom;
-       }
-}
-
 static void
 find_page_at_location (EvView  *view,
                       gdouble  x,
@@ -1023,17 +990,19 @@ status_message_from_link (EvView *view, EvLink *link)
 
 static void
 ev_view_size_request_continuous_dual_page (EvView         *view,
-                                              GtkRequisition *requisition)
+                                          GtkRequisition *requisition)
 {
-       int max_width, max_height;
-       int n_rows;
+       int max_width;
+       gint n_pages;
+       GtkBorder border;
 
-       get_bounding_box_size (view, &max_width, &max_height);
+       ev_page_cache_get_max_width (view->page_cache, view->scale, &max_width);
+       compute_border (view, max_width, max_width, &border);
 
-       n_rows = (1 + ev_page_cache_get_n_pages (view->page_cache)) / 2;
+       n_pages = ev_page_cache_get_n_pages (view->page_cache) + 1;
 
-       requisition->width = (max_width * 2) + (view->spacing * 3);
-       requisition->height = max_height * n_rows + (view->spacing * (n_rows + 1));
+       requisition->width = (max_width + border.left + border.right) * 2 + (view->spacing * 3);
+       get_page_y_offset (view, n_pages, view->scale, &requisition->height);
 
        if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
                requisition->width = 1;
@@ -1049,15 +1018,17 @@ static void
 ev_view_size_request_continuous (EvView         *view,
                                 GtkRequisition *requisition)
 {
-       int max_width, max_height;
+       int max_width;
        int n_pages;
+       GtkBorder border;
 
-       get_bounding_box_size (view, &max_width, &max_height);
 
+       ev_page_cache_get_max_width (view->page_cache, view->scale, &max_width);
        n_pages = ev_page_cache_get_n_pages (view->page_cache);
+       compute_border (view, max_width, max_width, &border);
 
-       requisition->width = max_width + (view->spacing * 2);
-       requisition->height = max_height * n_pages + (view->spacing * (n_pages + 1));
+       requisition->width = max_width + (view->spacing * 2) + border.left + border.right;
+       get_page_y_offset (view, n_pages, view->scale, &requisition->height);
 
        if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
                requisition->width = 1;
@@ -1351,6 +1322,9 @@ ev_view_motion_notify_event (GtkWidget      *widget,
 {
        EvView *view = EV_VIEW (widget);
 
+       if (!view->document)
+               return FALSE;
+
        if (view->pressed_button == 1) {
                GdkRectangle selection;
 
@@ -1400,7 +1374,7 @@ ev_view_motion_notify_event (GtkWidget      *widget,
 
                        return TRUE;
                }
-       } else if (view->pressed_button <= 0 && view->document) {
+       } else if (view->pressed_button <= 0) {
                EvLink *link;
 
                link = get_link_at_location (view, event->x + view->scroll_x, event->y + view->scroll_y);
@@ -1928,6 +1902,28 @@ ev_view_new (void)
        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 (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->document) {
+               ev_page_cache_clear (view->document);
+       }
+}
+
 void
 ev_view_set_document (EvView     *view,
                      EvDocument *document)
@@ -1935,6 +1931,8 @@ ev_view_set_document (EvView     *view,
        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,
@@ -1956,10 +1954,8 @@ ev_view_set_document (EvView     *view,
                                                  G_CALLBACK (find_changed_cb),
                                                  view);
                        }
-                       view->page_cache = ev_document_get_page_cache (view->document);
-                       g_signal_connect (view->page_cache, "page-changed", G_CALLBACK (page_changed_cb), view);
-                       view->pixbuf_cache = ev_pixbuf_cache_new (view->document);
-                       g_signal_connect (view->pixbuf_cache, "job-finished", G_CALLBACK (job_finished_cb), view);
+
+                       setup_caches (view);
                 }
 
                gtk_widget_queue_resize (GTK_WIDGET (view));
@@ -2149,6 +2145,54 @@ ev_view_zoom_out (EvView *view)
        ev_view_set_zoom (view, ZOOM_OUT_FACTOR, TRUE);
 }
 
+static void
+ev_view_set_orientation (EvView         *view,
+                        EvOrientation   orientation)
+{
+       ev_document_set_orientation (view->document, orientation);
+
+       clear_caches (view);
+       setup_caches (view);
+
+       gtk_widget_queue_resize (GTK_WIDGET (view));
+}
+
+void
+ev_view_rotate_right (EvView *view)
+{
+       EvOrientation orientation, new_orientation;
+
+       orientation = ev_document_get_orientation (view->document);
+       if (orientation == EV_ORIENTATION_PORTRAIT) {
+               new_orientation = EV_ORIENTATION_LANDSCAPE;
+       } else if (orientation == EV_ORIENTATION_LANDSCAPE) {
+               new_orientation = EV_ORIENTATION_UPSIDEDOWN;
+       } else if (orientation == EV_ORIENTATION_UPSIDEDOWN) {
+               new_orientation = EV_ORIENTATION_SEASCAPE;
+       } else {
+               new_orientation = EV_ORIENTATION_PORTRAIT;
+       }
+       ev_view_set_orientation (view, new_orientation);
+}
+
+void
+ev_view_rotate_left (EvView *view)
+{
+       EvOrientation orientation, new_orientation;
+
+       orientation = ev_document_get_orientation (view->document);
+       if (orientation == EV_ORIENTATION_PORTRAIT) {
+               new_orientation = EV_ORIENTATION_SEASCAPE;
+       } else if (orientation == EV_ORIENTATION_SEASCAPE) {
+               new_orientation = EV_ORIENTATION_UPSIDEDOWN;
+       } else if (orientation == EV_ORIENTATION_UPSIDEDOWN) {
+               new_orientation = EV_ORIENTATION_LANDSCAPE;
+       } else {
+               new_orientation = EV_ORIENTATION_PORTRAIT;
+       }
+       ev_view_set_orientation (view, new_orientation);
+}
+
 static double
 zoom_for_size_fit_width (int doc_width,
                         int doc_height,
@@ -2217,12 +2261,12 @@ ev_view_zoom_for_size_continuous_and_dual_page (EvView *view,
        GtkBorder border;
        gdouble scale;
 
-       ev_page_cache_get_max_width_size (view->page_cache,
-                                         1.0,
-                                         &doc_width, NULL);
-       ev_page_cache_get_max_height_size (view->page_cache,
-                                          1.0,
-                                          NULL, &doc_height);
+       ev_page_cache_get_max_width (view->page_cache,
+                                    1.0,
+                                    &doc_width);
+       ev_page_cache_get_max_height (view->page_cache,
+                                     1.0,
+                                     &doc_height);
        compute_border (view, doc_width, doc_height, &border);
 
        doc_width = doc_width * 2;
@@ -2253,12 +2297,12 @@ ev_view_zoom_for_size_continuous (EvView *view,
        GtkBorder border;
        gdouble scale;
 
-       ev_page_cache_get_max_width_size (view->page_cache,
-                                         1.0,
-                                         &doc_width, NULL);
-       ev_page_cache_get_max_height_size (view->page_cache,
-                                          1.0,
-                                          NULL, &doc_height);
+       ev_page_cache_get_max_width (view->page_cache,
+                                    1.0,
+                                    &doc_width);
+       ev_page_cache_get_max_height (view->page_cache,
+                                     1.0,
+                                     &doc_height);
        compute_border (view, doc_width, doc_height, &border);
 
        width -= (border.left + border.right + 2 * view->spacing);
@@ -2529,7 +2573,7 @@ ev_view_find_next (EvView *view)
        int n_results, n_pages;
        EvDocumentFind *find = EV_DOCUMENT_FIND (view->document);
 
-       page_cache = ev_document_get_page_cache (view->document);
+       page_cache = ev_page_cache_get (view->document);
        n_results = ev_document_find_get_n_results (find, view->current_page);
 
        n_pages = ev_page_cache_get_n_pages (page_cache);
@@ -2558,7 +2602,7 @@ ev_view_find_previous (EvView *view)
        EvDocumentFind *find = EV_DOCUMENT_FIND (view->document);
        EvPageCache *page_cache;
 
-       page_cache = ev_document_get_page_cache (view->document);
+       page_cache = ev_page_cache_get (view->document);
 
        n_results = ev_document_find_get_n_results (find, view->current_page);