summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: b58cebf)
raw | patch | inline | side by side (parent: b58cebf)
author | Sebastian Harl <sh@tokkee.org> | |
Thu, 20 Oct 2011 16:47:52 +0000 (18:47 +0200) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Thu, 20 Oct 2011 16:47:52 +0000 (18:47 +0200) |
This makes much more flexible setups possible, including displaying more than
one file/page at the same time and making use of Gtk's layout features.
one file/page at the same time and making use of Gtk's layout features.
src/Makefile.am | patch | blob | history | |
src/gtk-tpdfv.c | [new file with mode: 0644] | patch | blob |
src/gtk-tpdfv.h | [new file with mode: 0644] | patch | blob |
src/tpdfview.c | patch | blob | history |
diff --git a/src/Makefile.am b/src/Makefile.am
index 3014c17f476eb3383038272521fc5d9131d0aa77..6dda19ac383e4577b55a8ae366d12ac4ac88400b 100644 (file)
--- a/src/Makefile.am
+++ b/src/Makefile.am
BUILT_SOURCES = tpdfv_features.h
-libtpdfv_la_SOURCES = tpdfv.c tpdfv.h tpdfv_features.h
-libtpdfv_la_LDFLAGS = -version-info 0:0:0
-
-bin_PROGRAMS = tpdfview
-
-tpdfview_SOURCES = tpdfview.c tpdfv.h
-tpdfview_CFLAGS = $(AM_CFLAGS) -DBUILD_DATE="\"$$( date --utc '+%F %T' ) (UTC)\"" \
+libtpdfv_la_SOURCES = tpdfv.c tpdfv.h tpdfv_features.h \
+ gtk-tpdfv.c gtk-tpdfv.h
+libtpdfv_la_CFLAGS = $(AM_CFLAGS) \
@CAIRO_CFLAGS@ \
@GTK2_CFLAGS@ \
@POPPLER_GLIB_CFLAGS@
-tpdfview_LDFLAGS = \
+libtpdfv_la_LDFLAGS = -version-info 0:0:0 \
@CAIRO_LIBS@ \
@GTK2_LIBS@ \
@POPPLER_GLIB_LIBS@
+
+bin_PROGRAMS = tpdfview
+
+tpdfview_SOURCES = tpdfview.c tpdfv.h
+tpdfview_CFLAGS = $(AM_CFLAGS) -DBUILD_DATE="\"$$( date --utc '+%F %T' ) (UTC)\"" \
+ @GTK2_CFLAGS@
+tpdfview_LDFLAGS = @GTK2_LIBS@
tpdfview_LDADD = libtpdfv.la
../version: FORCE
diff --git a/src/gtk-tpdfv.c b/src/gtk-tpdfv.c
--- /dev/null
+++ b/src/gtk-tpdfv.c
@@ -0,0 +1,361 @@
+/*
+ * tpdfview - src/gtk-tpdfv.c
+ * Copyright (C) 2011 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * This module provides a Gtk widget to display PDF files.
+ */
+
+#include "gtk-tpdfv.h"
+
+#include <assert.h>
+
+#include <errno.h>
+
+#include <cairo.h>
+#include <gtk/gtk.h>
+
+#include <poppler.h>
+
+#include <stdlib.h>
+#include <string.h>
+
+typedef struct {
+ char *filename;
+
+ PopplerDocument *doc;
+ PopplerPage *current_page;
+
+ int current_page_no;
+ int total_pages;
+
+ double zoom_factor;
+
+ double delta_x;
+ double delta_y;
+} gtk_tpdfv_t;
+
+GType gtk_tpdfv_get_type(void);
+G_DEFINE_TYPE(GtkTPDFV, gtk_tpdfv, GTK_TYPE_DRAWING_AREA);
+
+#define GTK_TPDFV_GET_PRIVATE(obj) \
+ (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_TPDFV, gtk_tpdfv_t))
+
+/*
+ * Private helper functions.
+ */
+
+static gboolean
+tpdfv_init(gtk_tpdfv_t *pdf, const char *filename)
+{
+ GError *err = NULL;
+
+ if (strstr(filename, "://"))
+ pdf->filename = strdup(filename);
+ else {
+ size_t len = strlen("file://") + strlen(filename);
+ pdf->filename = (char *)malloc(len + 1);
+ if (pdf->filename) {
+ *pdf->filename = '\0';
+ strncat(pdf->filename, "file://", len);
+ strncat(pdf->filename, filename, len - strlen("file://"));
+ }
+ }
+
+ /* XXX: error reporting mechanism */
+
+ if (! pdf->filename) {
+ char errbuf[1024];
+ strerror_r(errno, errbuf, sizeof(errbuf));
+ fprintf(stderr, "Failed to allocate string: %s.\n", errbuf);
+ 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->current_page_no = 0;
+ pdf->current_page = poppler_document_get_page(pdf->doc,
+ pdf->current_page_no);
+ if (! pdf->current_page) {
+ fprintf(stderr, "Failed to open first page of the document.\n");
+ return FALSE;
+ }
+
+ pdf->total_pages = poppler_document_get_n_pages(pdf->doc);
+
+ pdf->zoom_factor = 1.0;
+ pdf->delta_x = pdf->delta_y = 0.0;
+ return TRUE;
+} /* tpdfv_init */
+
+static void
+tpdfv_clean(gtk_tpdfv_t *pdf)
+{
+ if (! pdf)
+ return;
+
+ g_object_unref(pdf->doc);
+ pdf->doc = NULL;
+ pdf->current_page = NULL;
+ free(pdf->filename);
+ pdf->filename = NULL;
+ return;
+} /* tpdfv_clean */
+
+static void
+do_redraw(GtkWidget *widget)
+{
+ GdkRegion *region;
+ gtk_tpdfv_t *pdf;
+
+ pdf = GTK_TPDFV_GET_PRIVATE(widget);
+
+ if (poppler_page_get_index(pdf->current_page)
+ != pdf->current_page_no) {
+ pdf->current_page = poppler_document_get_page(pdf->doc,
+ pdf->current_page_no);
+
+ if (! pdf->current_page) {
+ fprintf(stderr, "Failed to open page %i of the document.\n",
+ pdf->current_page_no + 1);
+ return;
+ }
+ }
+
+ region = gdk_drawable_get_clip_region(widget->window);
+ gdk_window_invalidate_region(widget->window, region, TRUE);
+ gdk_window_process_updates(widget->window, TRUE);
+
+ gdk_region_destroy(region);
+} /* do_redraw */
+
+/*
+ * Gtk+ class definition.
+ */
+
+static gboolean
+gtk_tpdfv_expose(GtkWidget *tpdfv, GdkEventExpose *event)
+{
+ gtk_tpdfv_t *pdf;
+ cairo_t *cr;
+
+ pdf = GTK_TPDFV_GET_PRIVATE(tpdfv);
+
+ if (! pdf)
+ return FALSE;
+
+ cr = gdk_cairo_create(tpdfv->window);
+ cairo_rectangle(cr, event->area.x, event->area.y,
+ event->area.width, event->area.height);
+ cairo_clip(cr);
+
+ /* zoom, scrolling */
+ cairo_scale(cr, pdf->zoom_factor, pdf->zoom_factor);
+ cairo_translate(cr, pdf->delta_x, pdf->delta_y);
+
+ poppler_page_render(pdf->current_page, cr);
+
+ cairo_destroy(cr);
+ return FALSE;
+} /* gtk_tpdfv_expose */
+
+static void
+gtk_tpdfv_destroy(GtkObject *obj)
+{
+ GtkTPDFV *tpdfv = GTK_TPDFV(obj);
+ gtk_tpdfv_t *pdf;
+
+ pdf = GTK_TPDFV_GET_PRIVATE(tpdfv);
+ tpdfv_clean(pdf);
+
+ GTK_OBJECT_CLASS(gtk_tpdfv_parent_class)->destroy(obj);
+} /* gtk_tpdfv_destroy */
+
+static void
+gtk_tpdfv_class_init(GtkTPDFVClass *class)
+{
+ GObjectClass *goclass;
+ GtkObjectClass *oclass;
+ GtkWidgetClass *wclass;
+
+ goclass = G_OBJECT_CLASS(class);
+ g_type_class_add_private(goclass, sizeof(gtk_tpdfv_t));
+
+ oclass = GTK_OBJECT_CLASS(class);
+ oclass->destroy = gtk_tpdfv_destroy;
+
+ wclass = GTK_WIDGET_CLASS(class);
+ wclass->expose_event = gtk_tpdfv_expose;
+} /* gtk_tpdfv_class_init */
+
+static void
+gtk_tpdfv_init(GtkTPDFV __attribute__((unused)) *tpdfv)
+{
+ /* nothing to do for now */
+} /* gtk_tpdfv_init */
+
+/*
+ * Public API.
+ */
+
+GtkWidget *
+gtk_tpdfv_new(const char *filename)
+{
+ GtkTPDFV *tpdfv;
+ gtk_tpdfv_t *pdf;
+
+ tpdfv = g_object_new(GTK_TYPE_TPDFV, NULL);
+ pdf = GTK_TPDFV_GET_PRIVATE(tpdfv);
+
+ if (TRUE != tpdfv_init(pdf, filename))
+ return NULL;
+ return GTK_WIDGET(tpdfv);
+} /* gtk_tpdfv_new */
+
+void
+gtk_tpdfv_page_up(GtkWidget *widget)
+{
+ gtk_tpdfv_t *pdf;
+
+ pdf = GTK_TPDFV_GET_PRIVATE(widget);
+
+ if (pdf->current_page_no)
+ --pdf->current_page_no;
+ do_redraw(widget);
+} /* gtk_tpdfv_page_up */
+
+void
+gtk_tpdfv_page_down(GtkWidget *widget)
+{
+ gtk_tpdfv_t *pdf;
+
+ pdf = GTK_TPDFV_GET_PRIVATE(widget);
+
+ if (pdf->current_page_no < pdf->total_pages - 1)
+ ++pdf->current_page_no;
+ do_redraw(widget);
+} /* gtk_tpdfv_page_down */
+
+void
+gtk_tpdfv_first_page(GtkWidget *widget)
+{
+ gtk_tpdfv_t *pdf;
+
+ pdf = GTK_TPDFV_GET_PRIVATE(widget);
+ pdf->current_page_no = 0;
+ do_redraw(widget);
+} /* gtk_tpdfv_first_page */
+
+void
+gtk_tpdfv_last_page(GtkWidget *widget)
+{
+ gtk_tpdfv_t *pdf;
+
+ pdf = GTK_TPDFV_GET_PRIVATE(widget);
+ pdf->current_page_no = pdf->total_pages - 1;
+ do_redraw(widget);
+} /* gtk_tpdfv_last_page */
+
+void
+gtk_tpdfv_zoom_in(GtkWidget *widget)
+{
+ gtk_tpdfv_t *pdf;
+
+ pdf = GTK_TPDFV_GET_PRIVATE(widget);
+ pdf->zoom_factor *= 1.2;
+ do_redraw(widget);
+} /* gtk_tpdfv_zoom_in */
+
+void
+gtk_tpdfv_zoom_out(GtkWidget *widget)
+{
+ gtk_tpdfv_t *pdf;
+
+ pdf = GTK_TPDFV_GET_PRIVATE(widget);
+
+ if (pdf->zoom_factor > DBL_EPSILON * 2.0) {
+ pdf->zoom_factor /= 1.2;
+ do_redraw(widget);
+ }
+} /* gtk_tpdfv_zoom_out */
+
+void
+gtk_tpdfv_zoom_1(GtkWidget *widget)
+{
+ gtk_tpdfv_t *pdf;
+
+ pdf = GTK_TPDFV_GET_PRIVATE(widget);
+ pdf->zoom_factor = 1.0;
+ do_redraw(widget);
+} /* gtk_tpdfv_zoom_1 */
+
+void
+gtk_tpdfv_scroll_up(GtkWidget *widget)
+{
+ gtk_tpdfv_t *pdf;
+
+ pdf = GTK_TPDFV_GET_PRIVATE(widget);
+ pdf->delta_y += 10.0;
+ do_redraw(widget);
+} /* gtk_tpdfv_scroll_up */
+
+void
+gtk_tpdfv_scroll_down(GtkWidget *widget)
+{
+ gtk_tpdfv_t *pdf;
+
+ pdf = GTK_TPDFV_GET_PRIVATE(widget);
+ pdf->delta_y -= 10.0;
+ do_redraw(widget);
+} /* gtk_tpdfv_scroll_down */
+
+void
+gtk_tpdfv_scroll_left(GtkWidget *widget)
+{
+ gtk_tpdfv_t *pdf;
+
+ pdf = GTK_TPDFV_GET_PRIVATE(widget);
+ pdf->delta_x += 10.0;
+ do_redraw(widget);
+} /* gtk_tpdfv_scroll_left */
+
+void
+gtk_tpdfv_scroll_right(GtkWidget *widget)
+{
+ gtk_tpdfv_t *pdf;
+
+ pdf = GTK_TPDFV_GET_PRIVATE(widget);
+ pdf->delta_x -= 10.0;
+ do_redraw(widget);
+} /* gtk_tpdfv_scroll_right */
+
+/* vim: set tw=78 sw=4 ts=4 noexpandtab : */
+
diff --git a/src/gtk-tpdfv.h b/src/gtk-tpdfv.h
--- /dev/null
+++ b/src/gtk-tpdfv.h
@@ -0,0 +1,115 @@
+/*
+ * tpdfview - src/gtk-tpdfv.h
+ * Copyright (C) 2011 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * This module provides a Gtk widget to display PDF files.
+ */
+
+#ifndef GTK_TPDFV_H
+#define GTK_TPDFV_H 1
+
+#include <gtk/gtk.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define GTK_TYPE_TPDFV (gtk_tpdfv_get_type())
+#define GTK_TPDFV(obj) \
+ (G_TYPE_CHECK_INSTANCE_CAST((obj), GTK_TYPE_TPDFV, GtkTPDFV))
+#define GTK_TPDFV_CLASS(obj) \
+ (G_TYPE_CHECK_CLASS_CAST((obj), GTK_TYPE_TPDFV, GtkTPDFVClass))
+#define GTK_IS_TPDFV(obj) \
+ (G_TYPE_CHECK_INSTANCE_TYPE((obj), GTK_TYPE_TPDFV))
+#define GTK_IS_TPDFV_CLASS(obj) \
+ (G_TYPE_CHECK_CLASS_TYPE((obj), GTK_TYPE_TPDFV))
+#define GTK_TPDFV_GET_CLASS(obj) \
+ (G_TYPE_INSTANCE_GET_CLASS((obj), GTK_TYPE_TPDFV, GtkTPDFVClass))
+
+typedef struct {
+ GtkDrawingArea graph;
+} GtkTPDFV;
+
+typedef struct {
+ GtkDrawingAreaClass parent_class;
+} GtkTPDFVClass;
+
+/*
+ * gtk_tpdfv_new:
+ * Creates a new GtkTPDFV widget to display the specified PDF file.
+ */
+GtkWidget *
+gtk_tpdfv_new(const char *filename);
+
+/*
+ * gtk_tpdfv_page_up, gtk_tpdfv_page_down,
+ * gtk_tpdfv_first_page, gtk_tpdfv_last_page:
+ * Navigation.
+ */
+void
+gtk_tpdfv_page_up(GtkWidget *widget);
+void
+gtk_tpdfv_page_down(GtkWidget *widget);
+void
+gtk_tpdfv_first_page(GtkWidget *widget);
+void
+gtk_tpdfv_last_page(GtkWidget *widget);
+
+/*
+ * gtk_tpdfv_zoom_in, gtk_tpdfv_zoom_out,
+ * gtk_tpdfv_zoom_1:
+ * Zooming.
+ */
+void
+gtk_tpdfv_zoom_in(GtkWidget *widget);
+void
+gtk_tpdfv_zoom_out(GtkWidget *widget);
+void
+gtk_tpdfv_zoom_1(GtkWidget *widget);
+
+/*
+ * gtk_tpdfv_scroll_up, gtk_tpdfv_scroll_down,
+ * gtk_tpdfv_scroll_left, gtk_tpdfv_scroll_right:
+ * Scrolling.
+ */
+void
+gtk_tpdfv_scroll_up(GtkWidget *widget);
+void
+gtk_tpdfv_scroll_down(GtkWidget *widget);
+void
+gtk_tpdfv_scroll_left(GtkWidget *widget);
+void
+gtk_tpdfv_scroll_right(GtkWidget *widget);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* ! GTK_TPDFV_H */
+
+/* vim: set tw=78 sw=4 ts=4 noexpandtab : */
+
diff --git a/src/tpdfview.c b/src/tpdfview.c
index 79532672c13154c95ee124869e017bbe64d68fa1..45cdcf3ebb6c4149da9616499672218411a66523 100644 (file)
--- a/src/tpdfview.c
+++ b/src/tpdfview.c
#include "tpdfv.h"
#include "tpdfv_features.h"
+#include "gtk-tpdfv.h"
+
#if HAVE_LIBGEN_H
# include <libgen.h>
#else /* HAVE_LIBGEN_H */
#include <assert.h>
-#include <errno.h>
-
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
-#include <cairo.h>
-
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
-#include <poppler.h>
-
-typedef struct {
- char *filename;
-
- PopplerDocument *doc;
- PopplerPage *current_page;
-
- int current_page_no;
- int total_pages;
-
- double zoom_factor;
-
- double delta_x;
- double delta_y;
-} tpdfv_t;
-
-static tpdfv_t *
-tpdfv_new(const char *filename)
-{
- GError *err = NULL;
- tpdfv_t *pdf;
-
- /* XXX: error reporting mechanism */
-
- pdf = (tpdfv_t *)malloc(sizeof(*pdf));
- if (! pdf) {
- fprintf(stderr, "Failed to allocate PDF object: %s.\n",
- strerror(errno));
- return NULL;
- }
-
- if (strstr(filename, "://"))
- pdf->filename = strdup(filename);
- else {
- size_t len = strlen("file://") + strlen(filename);
- pdf->filename = (char *)malloc(len + 1);
- if (pdf->filename) {
- *pdf->filename = '\0';
- strncat(pdf->filename, "file://", len);
- strncat(pdf->filename, filename, len - strlen("file://"));
- }
- }
-
- if (! pdf->filename) {
- fprintf(stderr, "Failed to allocate string: %s.\n",
- strerror(errno));
- return NULL;
- }
-
- 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 NULL;
- }
-
- pdf->current_page_no = 0;
- pdf->current_page = poppler_document_get_page(pdf->doc,
- pdf->current_page_no);
- if (! pdf->current_page) {
- fprintf(stderr, "Failed to open first page of the document.\n");
- return NULL;
- }
-
- pdf->total_pages = poppler_document_get_n_pages(pdf->doc);
-
- pdf->zoom_factor = 1.0;
- pdf->delta_x = pdf->delta_y = 0.0;
- return pdf;
-} /* tpdfv_new */
static void
exit_usage(char *name, int status)
exit(0);
} /* exit_version */
-static void
-win_redraw(GtkWidget *widget)
-{
- GdkRegion *region;
-
- region = gdk_drawable_get_clip_region(widget->window);
- gdk_window_invalidate_region(widget->window, region, TRUE);
- gdk_window_process_updates(widget->window, TRUE);
-
- gdk_region_destroy(region);
-} /* win_redraw */
-
static void
on_destroy(GtkWidget __attribute__((unused)) *widget,
gpointer __attribute__((unused)) data)
gtk_main_quit();
} /* on_destroy */
-static gboolean
-on_expose(GtkWidget *widget, GdkEventExpose __attribute__((unused)) *event,
- gpointer data)
-{
- cairo_t *cr;
-
- tpdfv_t *pdf = (tpdfv_t *)data;
- assert(data);
-
- cr = gdk_cairo_create(widget->window);
-
- /* zoom */
- cairo_scale(cr, pdf->zoom_factor, pdf->zoom_factor);
- cairo_translate(cr, pdf->delta_x, pdf->delta_y);
-
- poppler_page_render(pdf->current_page, cr);
- cairo_destroy(cr);
- return FALSE;
-} /* on_expose */
-
static gboolean
key_press(GtkWidget __attribute__((unused)) *widget,
GdkEventKey *event, gpointer data)
{
- tpdfv_t *pdf = (tpdfv_t *)data;
-
- int old_page_no;
- _Bool do_redraw = 0;
+ GtkWidget *tpdfv;
- assert(data);
-
- old_page_no = pdf->current_page_no;
+ tpdfv = (GtkWidget *)data;
+ assert(tpdfv);
switch (event->keyval) {
case GDK_q:
/* navigation */
case GDK_Page_Up:
- if (pdf->current_page_no) {
- --pdf->current_page_no;
- }
+ gtk_tpdfv_page_up(tpdfv);
break;
case GDK_Page_Down:
/* fall through */
case GDK_space:
- if (pdf->current_page_no < pdf->total_pages - 1) {
- ++pdf->current_page_no;
- }
+ gtk_tpdfv_page_down(tpdfv);
break;
case GDK_Home:
- pdf->current_page_no = 0;
+ gtk_tpdfv_first_page(tpdfv);
break;
case GDK_End:
- pdf->current_page_no = pdf->total_pages - 1;
+ gtk_tpdfv_last_page(tpdfv);
break;
/* zoom */
case GDK_plus:
- pdf->zoom_factor *= 1.2;
- do_redraw = 1;
+ gtk_tpdfv_zoom_in(tpdfv);
break;
case GDK_minus:
- if (pdf->zoom_factor > DBL_EPSILON * 2.0) {
- pdf->zoom_factor *= 1.0 / 1.2;
- do_redraw = 1;
- }
+ gtk_tpdfv_zoom_out(tpdfv);
break;
case GDK_1:
- pdf->zoom_factor = 1.0;
- do_redraw = 1;
+ gtk_tpdfv_zoom_1(tpdfv);
break;
/* scrolling */
case GDK_Up:
- pdf->delta_y += 10.0;
- do_redraw = 1;
+ gtk_tpdfv_scroll_up(tpdfv);
break;
case GDK_Down:
- pdf->delta_y -= 10.0;
- do_redraw = 1;
+ gtk_tpdfv_scroll_down(tpdfv);
break;
case GDK_Left:
- pdf->delta_x += 10.0;
- do_redraw = 1;
+ gtk_tpdfv_scroll_left(tpdfv);
break;
case GDK_Right:
- pdf->delta_x -= 10.0;
- do_redraw = 1;
+ gtk_tpdfv_scroll_right(tpdfv);
break;
}
-
- if (old_page_no != pdf->current_page_no) {
- pdf->current_page = poppler_document_get_page(pdf->doc,
- pdf->current_page_no);
- if (! pdf->current_page)
- fprintf(stderr, "Failed to open page %i of the document.\n",
- pdf->current_page_no + 1);
- else
- do_redraw = 1;
- }
-
- if (do_redraw)
- win_redraw(widget);
return FALSE;
} /* key_press */
int
main(int argc, char **argv)
{
- GtkWidget *win = NULL;
- tpdfv_t *pdf = NULL;
+ GtkWidget *win = NULL;
+ GtkWidget *tpdfv = NULL;
gtk_init(&argc, &argv);
exit_usage(argv[0], 1);
}
- pdf = tpdfv_new(/* filename = */ argv[optind]);
- if (! pdf)
+ tpdfv = gtk_tpdfv_new(argv[optind]);
+ if (! tpdfv)
return 1;
win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+ if (! win)
+ return 1;
+
+ gtk_container_add(GTK_CONTAINER(win), tpdfv);
+
g_signal_connect(G_OBJECT(win), "destroy",
G_CALLBACK(on_destroy), NULL);
- g_signal_connect(G_OBJECT(win), "expose-event",
- G_CALLBACK(on_expose), pdf);
g_signal_connect(G_OBJECT(win), "key-press-event",
- G_CALLBACK(key_press), pdf);
- gtk_widget_set_app_paintable(win, TRUE);
+ G_CALLBACK(key_press), tpdfv);
+
gtk_widget_show_all(win);
gtk_main();