]> www.fi.muni.cz Git - evince.git/blobdiff - ps/ps-document.c
Default page is 0. Calculate size even when we have no target yet.
[evince.git] / ps / ps-document.c
index fd6fc4a62d5785fcf250a806cda5b5e441f465d8..d78ad744a8b5e5b51718f7820c819c3a7fe36551 100644 (file)
@@ -150,6 +150,7 @@ The DONE message indicates that ghostscript has finished processing.
 #include <math.h>
 
 #include "ps-document.h"
+#include "ev-debug.h"
 #include "gsdefaults.h"
 
 #ifdef HAVE_LOCALE_H
@@ -225,7 +226,7 @@ ps_document_init(PSDocument * gs)
 {
   gs->bpixmap = NULL;
 
-  gs->current_page = -2;
+  gs->current_page = 0;
   gs->disable_start = FALSE;
   gs->interpreter_pid = -1;
 
@@ -324,12 +325,12 @@ ps_document_get_property (GObject *object,
 }
 
 static void
-ps_document_class_init(PSDocumentClass * klass)
+ps_document_class_init(PSDocumentClass *klass)
 {
   GObjectClass *object_class;
 
   object_class = (GObjectClass *) klass;
-  parent_class = gtk_type_class(gtk_widget_get_type());
+  parent_class = g_type_class_peek_parent (klass);
   gs_class = klass;
 
   object_class->finalize = ps_document_finalize;
@@ -378,7 +379,7 @@ ps_document_cleanup(PSDocument * gs)
     g_free(gs->gs_filename_unc);
     gs->gs_filename_unc = NULL;
   }
-  gs->current_page = -1;
+  gs->current_page = 0;
   gs->loaded = FALSE;
   gs->llx = 0;
   gs->lly = 0;
@@ -388,23 +389,26 @@ ps_document_cleanup(PSDocument * gs)
 }
 
 static void
-ps_document_finalize(GObject * object)
+ps_document_finalize (GObject * object)
 {
-  PSDocument *gs;
+       PSDocument *gs;
 
-  g_return_if_fail(object != NULL);
-  g_return_if_fail(GTK_IS_GS(object));
+       g_return_if_fail (object != NULL);
+       g_return_if_fail (GTK_IS_GS (object));
 
-  gs = PS_DOCUMENT(object);
+       LOG ("Finalize");
 
-  ps_document_cleanup(gs);
+       gs = PS_DOCUMENT (object);
 
-  if(gs->input_buffer) {
-    g_free(gs->input_buffer);
-    gs->input_buffer = NULL;
-  }
+       ps_document_cleanup (gs);
+       stop_interpreter (gs);
 
-  (*G_OBJECT_CLASS(parent_class)->finalize) (object);
+       if(gs->input_buffer) {
+               g_free(gs->input_buffer);
+               gs->input_buffer = NULL;
+       }
+
+       (*G_OBJECT_CLASS(parent_class)->finalize) (object);
 }
 
 static void
@@ -482,11 +486,16 @@ set_up_page(PSDocument * gs)
   GdkGC *fill;
   GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF };   /* pixel, r, g, b */
   GdkColormap *colormap;
+  gboolean size_changed;
+
+  LOG ("Setup the page");
 
 #ifdef HAVE_LOCALE_H
   char *savelocale;
 #endif
 
+  size_changed = compute_size (gs);
+
   if (gs->pstarget == NULL)
     return;
 
@@ -496,7 +505,7 @@ set_up_page(PSDocument * gs)
 
   orientation = ps_document_get_orientation(gs);
 
-  if(compute_size(gs)) {
+  if(size_changed) {
     gdk_flush();
 
     /* clear new pixmap (set to white) */
@@ -512,6 +521,7 @@ set_up_page(PSDocument * gs)
           gs->bpixmap = NULL;
         }
 
+        LOG ("Create our internal pixmap");
         gs->bpixmap = gdk_pixmap_new(gs->pstarget, gs->width, gs->height, -1);
 
         gdk_draw_rectangle(gs->bpixmap, fill, TRUE,
@@ -547,6 +557,8 @@ set_up_page(PSDocument * gs)
              gs->left_margin,
              gs->bottom_margin, gs->right_margin, gs->top_margin);
 
+  LOG ("GS property %s", buf);
+
 #ifdef HAVE_LOCALE_H
   setlocale(LC_NUMERIC, savelocale);
 #endif
@@ -711,6 +723,8 @@ start_interpreter(PSDocument * gs)
   int std_out[2];               /* pipe from interp stdout */
   int std_err[2];               /* pipe from interp stderr */
 
+  LOG ("Start the interpreter");
+
 #define NUM_ARGS    100
 #define NUM_GS_ARGS (NUM_ARGS - 20)
 #define NUM_ALPHA_ARGS 10
@@ -775,6 +789,12 @@ start_interpreter(PSDocument * gs)
     return -1;
   }
 
+  gv_env = g_strdup_printf("GHOSTVIEW=%ld %ld",
+                           gdk_x11_drawable_get_xid(gs->pstarget),
+                          gdk_x11_drawable_get_xid(gs->bpixmap));
+
+  LOG ("Launching ghostview with env %s", gv_env);
+
   gs->busy = TRUE;
   gs->interpreter_pid = fork();
   switch (gs->interpreter_pid) {
@@ -810,9 +830,6 @@ start_interpreter(PSDocument * gs)
       }
     }
 
-    gv_env = g_strdup_printf("GHOSTVIEW=%ld %ld",
-                             gdk_x11_drawable_get_xid(gs->pstarget),
-                            gdk_x11_drawable_get_xid(gs->bpixmap));
     putenv(gv_env);
 
     /* change to directory where the input file is. This helps
@@ -862,6 +879,7 @@ stop_interpreter(PSDocument * gs)
 {
   if(gs->interpreter_pid > 0) {
     int status = 0;
+    LOG ("Stop the interpreter");
     kill(gs->interpreter_pid, SIGTERM);
     while((wait(&status) == -1) && (errno == EINTR)) ;
     gs->interpreter_pid = -1;
@@ -1285,6 +1303,8 @@ document_load(PSDocument * gs, const gchar * fname)
   g_return_val_if_fail(gs != NULL, FALSE);
   g_return_val_if_fail(GTK_IS_GS(gs), FALSE);
 
+  LOG ("Load the document");
+
   /* clean up previous document */
   ps_document_cleanup(gs);
 
@@ -1298,7 +1318,7 @@ document_load(PSDocument * gs, const gchar * fname)
   /* default values: no dsc information available  */
   gs->structured_doc = FALSE;
   gs->send_filename_to_gs = TRUE;
-  gs->current_page = -2;
+  gs->current_page = 0;
   gs->loaded = FALSE;
   if(*fname == '/') {
     /* an absolute path */
@@ -1391,6 +1411,8 @@ ps_document_next_page(PSDocument * gs)
 {
   XEvent event;
 
+  LOG ("Make ghostscript render next page");
+
   g_return_val_if_fail(gs != NULL, FALSE);
   g_return_val_if_fail(GTK_IS_GS(gs), FALSE);
 
@@ -1418,37 +1440,14 @@ ps_document_next_page(PSDocument * gs)
   return TRUE;
 }
 
-static gint
-ps_document_get_current_page(PSDocument * gs)
-{
-  g_return_val_if_fail(gs != NULL, -1);
-  g_return_val_if_fail(GTK_IS_GS(gs), -1);
-
-  return gs->current_page;
-}
-
-static gint
-ps_document_get_page_count(PSDocument * gs)
-{
-  if(!gs->gs_filename)
-    return 0;
-
-  if(gs->doc) {
-    if(gs->structured_doc)
-      return gs->doc->numpages;
-    else
-      return G_MAXINT;
-  }
-  else
-    return 0;
-}
-
 static gboolean
 ps_document_goto_page(PSDocument * gs, gint page)
 {
   g_return_val_if_fail(gs != NULL, FALSE);
   g_return_val_if_fail(GTK_IS_GS(gs), FALSE);
 
+  LOG ("Go to page %d", page);
+
   if(!gs->gs_filename) {
     return FALSE;
   }
@@ -1458,6 +1457,9 @@ ps_document_goto_page(PSDocument * gs, gint page)
     page = 0;
 
   if(gs->structured_doc && gs->doc) {
+
+    LOG ("It's a structured document, let's send one page to gs");
+
     if(page >= gs->doc->numpages)
       page = gs->doc->numpages - 1;
 
@@ -1498,6 +1500,9 @@ ps_document_goto_page(PSDocument * gs, gint page)
        the last page of the file was displayed. In that
        case, ggv restarts GS again and the first page is displayed.
      */
+
+    LOG ("It's an unstructured document, gs will just read the file");
+
     if(page == gs->current_page && !gs->changed)
       return TRUE;
 
@@ -1530,6 +1535,8 @@ ps_document_set_page_size(PSDocument * gs, gint new_pagesize, gint pageid)
   gint new_ury = 0;
   GtkGSPaperSize *papersizes = gtk_gs_defaults_get_paper_sizes();
 
+  LOG ("Set the page size");
+
   g_return_val_if_fail(gs != NULL, FALSE);
   g_return_val_if_fail(GTK_IS_GS(gs), FALSE);
 
@@ -1628,54 +1635,19 @@ ps_document_set_page_size(PSDocument * gs, gint new_pagesize, gint pageid)
   return FALSE;
 }
 
-static gfloat
-ps_document_zoom_to_fit(PSDocument * gs, gboolean fit_width)
-{
-  gint new_y;
-  gfloat new_zoom;
-  guint avail_w, avail_h;
-
-  g_return_val_if_fail(gs != NULL, 0.0);
-  g_return_val_if_fail(GTK_IS_GS(gs), 0.0);
-
-  avail_w = (gs->avail_w > 0) ? gs->avail_w : gs->width;
-  avail_h = (gs->avail_h > 0) ? gs->avail_h : gs->height;
-
-  new_zoom = ((gfloat) avail_w) / ((gfloat) gs->width) * gs->zoom_factor;
-  if(!fit_width) {
-    new_y = new_zoom * ((gfloat) gs->height) / gs->zoom_factor;
-    if(new_y > avail_h)
-      new_zoom = ((gfloat) avail_h) / ((gfloat) gs->height) * gs->zoom_factor;
-  }
-
-  return new_zoom;
-}
-
 static void
-ps_document_set_zoom(PSDocument * gs, gfloat zoom)
+ps_document_set_zoom (PSDocument * gs, gfloat zoom)
 {
-  g_return_if_fail(gs != NULL);
-  g_return_if_fail(GTK_IS_GS(gs));
+       g_return_if_fail (gs != NULL);
 
-  switch (gs->zoom_mode) {
-  case GTK_GS_ZOOM_FIT_WIDTH:
-    zoom = ps_document_zoom_to_fit(gs, TRUE);
-    break;
-  case GTK_GS_ZOOM_FIT_PAGE:
-    zoom = ps_document_zoom_to_fit(gs, FALSE);
-    break;
-  case GTK_GS_ZOOM_ABSOLUTE:
-  default:
-    break;
-  }
-
-  if(fabs(gs->zoom_factor - zoom) > 0.001) {
-    gs->zoom_factor = zoom;
-    set_up_page(gs);
-    gs->changed = TRUE;
-  }
+       gs->zoom_factor = zoom;
 
-  ps_document_goto_page(gs, gs->current_page);
+       if (gs->pstarget != NULL) {
+               set_up_page(gs);
+               gs->changed = TRUE;
+               gs->scaling = TRUE;
+               ps_document_goto_page (gs, gs->current_page);
+       }
 }
 
 static gboolean
@@ -1691,6 +1663,12 @@ ps_document_load (EvDocument  *document,
                return FALSE;
 
        result = document_load (PS_DOCUMENT (document), filename);
+       if (!result) {
+               g_set_error (error, G_FILE_ERROR,
+                            G_FILE_ERROR_FAILED,
+                            "Failed to load document '%s'\n",
+                            uri);
+       }
 
        g_free (filename);
 
@@ -1709,20 +1687,32 @@ ps_document_save (EvDocument  *document,
 static int
 ps_document_get_n_pages (EvDocument  *document)
 {
-       return ps_document_get_page_count (PS_DOCUMENT (document));
+       PSDocument *ps = PS_DOCUMENT (document);
+
+       g_return_val_if_fail (ps != NULL, -1);
+
+       if (!ps->gs_filename || !ps->doc) {
+               return -1;
+       }
+
+       return ps->structured_doc ? ps->doc->numpages : 1;
 }
 
 static void
 ps_document_set_page (EvDocument  *document,
                       int          page)
 {
-       ps_document_goto_page (PS_DOCUMENT (document), page);
+       ps_document_goto_page (PS_DOCUMENT (document), page - 1);
 }
 
 static int
 ps_document_get_page (EvDocument  *document)
 {
-       return ps_document_get_current_page (PS_DOCUMENT (document));
+       PSDocument *ps = PS_DOCUMENT (document);
+
+       g_return_val_if_fail (ps != NULL, -1);
+
+       return ps->current_page + 1;
 }
 
 static gboolean
@@ -1733,9 +1723,18 @@ ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data)
        if(event->type != GDK_CLIENT_EVENT)
                return FALSE;
 
+       gs->message_window = event->client.data.l[0];
+
        if (event->client.message_type == gs_class->page_atom) {
+               LOG ("GS rendered the document");
                gs->busy = FALSE;
-               ev_document_changed (EV_DOCUMENT (gs));
+
+               if (gs->scaling) {
+                       ev_document_scale_changed (EV_DOCUMENT (gs));
+                       gs->scaling = FALSE;
+               } else {
+                       ev_document_page_changed (EV_DOCUMENT (gs));
+               }
        }
 
        return TRUE;
@@ -1836,6 +1835,10 @@ ps_document_render (EvDocument  *document,
                           draw.x, draw.y,
                           draw.width, draw.height);
 
+       LOG ("Copy the internal pixmap: %d %d %d %d %d %d",
+             draw.x - page.x, draw.y - page.y,
+             draw.x, draw.y, draw.width, draw.height);
+
        g_object_unref (gc);
 }