From: Sebastian Harl Date: Thu, 10 Nov 2011 14:34:28 +0000 (+0100) Subject: gtk-tpdfv/tpdfview: Added a possibility to reload the PDF file. X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=5b2c3d943abbc645b3e66784a85966cab7aaeea3;p=tpdfview.git gtk-tpdfv/tpdfview: Added a possibility to reload the PDF file. - 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. --- diff --git a/src/gtk-tpdfv.c b/src/gtk-tpdfv.c index 771dff9..ee68357 100644 --- a/src/gtk-tpdfv.c +++ b/src/gtk-tpdfv.c @@ -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) { diff --git a/src/gtk-tpdfv.h b/src/gtk-tpdfv.h index 7d260bd..f97224c 100644 --- a/src/gtk-tpdfv.h +++ b/src/gtk-tpdfv.h @@ -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: diff --git a/src/tpdfview.c b/src/tpdfview.c index b92d544..0d37559 100644 --- a/src/tpdfview.c +++ b/src/tpdfview.c @@ -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);