Code

gtk-tpdfv/tpdfview: Added a possibility to reload the PDF file.
authorSebastian Harl <sh@tokkee.org>
Thu, 10 Nov 2011 14:34:28 +0000 (15:34 +0100)
committerSebastian Harl <sh@tokkee.org>
Thu, 10 Nov 2011 14:34:28 +0000 (15:34 +0100)
 - In the widget, this is available as 'gtk_tpdfv_reload()' which closes and
   reopens the PDF file; updating internal settings accordingly.

 - In the GUI, this may be triggered by pressing the 'r' key.

src/gtk-tpdfv.c
src/gtk-tpdfv.h
src/tpdfview.c

index 771dff99871ebd2f2490207790760175caa684e9..ee68357d1253de739f77b7002a4785f2b3f4346b 100644 (file)
@@ -79,11 +79,40 @@ G_DEFINE_TYPE(GtkTPDFV, gtk_tpdfv, GTK_TYPE_DRAWING_AREA);
  * Private helper functions.
  */
 
-static gboolean
-tpdfv_init(gtk_tpdfv_t *pdf, const char *filename)
+static void
+tpdfv_open(gtk_tpdfv_t *pdf)
 {
        GError *err = NULL;
 
+       if ((! pdf) || (! pdf->filename))
+               return;
+
+       pdf->doc = poppler_document_new_from_file(pdf->filename,
+                       /* password = */ NULL, &err);
+       if (! pdf->doc) {
+               fprintf(stderr, "Failed to open PDF: %s.\n", err->message);
+               return;
+       }
+
+       pdf->total_pages = poppler_document_get_n_pages(pdf->doc);
+       if (pdf->current_page_no >= pdf->total_pages)
+               pdf->current_page_no = pdf->total_pages - 1;
+} /* tpdfv_open */
+
+static void
+tpdfv_close(gtk_tpdfv_t *pdf)
+{
+       if (! pdf)
+               return;
+
+       g_object_unref(pdf->doc);
+       pdf->doc = NULL;
+       pdf->current_page = NULL;
+} /* tpdfv_close */
+
+static gboolean
+tpdfv_init(gtk_tpdfv_t *pdf, const char *filename)
+{
        if (strstr(filename, "://"))
                pdf->filename = strdup(filename);
        else {
@@ -105,12 +134,8 @@ tpdfv_init(gtk_tpdfv_t *pdf, const char *filename)
                return FALSE;
        }
 
-       pdf->doc = poppler_document_new_from_file(pdf->filename,
-                       /* password = */ NULL, &err);
-       if (! pdf->doc) {
-               fprintf(stderr, "Failed to open PDF: %s.\n", err->message);
-               return FALSE;
-       }
+       pdf->doc = NULL;
+       tpdfv_open(pdf);
 
        pdf->current_page_no = 0;
        pdf->current_page = poppler_document_get_page(pdf->doc,
@@ -120,8 +145,6 @@ tpdfv_init(gtk_tpdfv_t *pdf, const char *filename)
                return FALSE;
        }
 
-       pdf->total_pages = poppler_document_get_n_pages(pdf->doc);
-
        pdf->zoom_mode   = TPDFV_ZOOM_CUSTOM;
        pdf->zoom_factor = 1.0;
        pdf->delta_x = pdf->delta_y = 0.0;
@@ -134,8 +157,7 @@ tpdfv_clean(gtk_tpdfv_t *pdf)
        if (! pdf)
                return;
 
-       g_object_unref(pdf->doc);
-       pdf->doc          = NULL;
+       tpdfv_close(pdf);
        pdf->current_page = NULL;
        free(pdf->filename);
        pdf->filename = NULL;
@@ -150,8 +172,8 @@ do_redraw(GtkWidget *widget)
 
        pdf = GTK_TPDFV_GET_PRIVATE(widget);
 
-       if (poppler_page_get_index(pdf->current_page)
-                       != pdf->current_page_no) {
+       if ((! pdf->current_page) || (poppler_page_get_index(pdf->current_page)
+                       != pdf->current_page_no)) {
                pdf->current_page = poppler_document_get_page(pdf->doc,
                pdf->current_page_no);
 
@@ -287,6 +309,17 @@ gtk_tpdfv_new(const char *filename)
        return GTK_WIDGET(tpdfv);
 } /* gtk_tpdfv_new */
 
+void
+gtk_tpdfv_reload(GtkWidget *widget)
+{
+       gtk_tpdfv_t *pdf;
+
+       pdf = GTK_TPDFV_GET_PRIVATE(widget);
+       tpdfv_close(pdf);
+       tpdfv_open(pdf);
+       do_redraw(widget);
+} /* gtk_tpdfv_reload */
+
 void
 gtk_tpdfv_page_up(GtkWidget *widget)
 {
index 7d260bd28e5491c213567de01a31dd338cc2d8fb..f97224c4b6f87a4b8ada1b7764e4b1d6ae542814 100644 (file)
@@ -65,6 +65,13 @@ typedef struct {
 GtkWidget *
 gtk_tpdfv_new(const char *filename);
 
+/*
+ * gtk_tpdfv_reload:
+ * Reload the previously opened PDF file.
+ */
+void
+gtk_tpdfv_reload(GtkWidget *widget);
+
 /*
  * gtk_tpdfv_page_up, gtk_tpdfv_page_down,
  * gtk_tpdfv_first_page, gtk_tpdfv_last_page:
index b92d5440799e212cf3314c8be6da87d544b0ade4..0d3755958a800a9bb511ccacde5ef3e02678a2e2 100644 (file)
@@ -109,6 +109,10 @@ key_press(GtkWidget __attribute__((unused)) *widget,
                        gtk_main_quit();
                        break;
 
+               case GDK_r:
+                       gtk_tpdfv_reload(tpdfv);
+                       break;
+
                /* navigation */
                case GDK_Page_Up:
                        gtk_tpdfv_page_up(tpdfv);