1 /*
2 * tpdfview - src/tpdfview.c
3 * Copyright (C) 2011 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 /*
29 * A tiny PDF viewer.
30 */
32 #if HAVE_CONFIG_H
33 # include "config.h"
34 #endif /* HAVE_CONFIG_H */
36 #include "tpdfv.h"
37 #include "tpdfv_features.h"
39 #include "gtk-tpdfv.h"
41 #if HAVE_LIBGEN_H
42 # include <libgen.h>
43 #else /* HAVE_LIBGEN_H */
44 # define basename(path) (path)
45 #endif /* ! HAVE_LIBGEN_H */
47 #include <assert.h>
49 #include <stdio.h>
50 #include <stdlib.h>
52 #include <string.h>
54 #include <unistd.h>
56 #include <gtk/gtk.h>
57 #include <gdk/gdkkeysyms.h>
59 static void
60 exit_usage(char *name, int status)
61 {
62 printf(
63 "Usage: %s [<options>] <filename>\n"
65 "\nA tiny PDF viewer.\n"
67 "\nOptions:\n"
68 " -h display this help and exit\n"
69 " -V display the version number and copyright\n"
71 "\ntpdfview "TPDFV_VERSION_STRING TPDFV_VERSION_EXTRA", "PACKAGE_URL"\n"
72 "Copyright (C) 2011 "PACKAGE_MAINTAINER"\n",
73 basename(name));
74 exit(status);
75 } /* exit_usage */
77 static void
78 exit_version(void)
79 {
80 printf("tpdfview version "TPDFV_VERSION_STRING TPDFV_VERSION_EXTRA", "
81 "built "BUILD_DATE"\n"
82 "Copyright (C) 2011 "PACKAGE_MAINTAINER"\n"
84 "\nThis is free software under the terms of the BSD license, see "
85 "the source for\ncopying conditions. There is NO WARRANTY; not "
86 "even for MERCHANTABILITY or\nFITNESS FOR A PARTICULAR "
87 "PURPOSE.\n");
88 exit(0);
89 } /* exit_version */
91 static void
92 on_destroy(GtkWidget __attribute__((unused)) *widget,
93 gpointer __attribute__((unused)) data)
94 {
95 gtk_main_quit();
96 } /* on_destroy */
98 static gboolean
99 key_press(GtkWidget __attribute__((unused)) *widget,
100 GdkEventKey *event, gpointer data)
101 {
102 GtkWidget *tpdfv;
104 tpdfv = (GtkWidget *)data;
105 assert(tpdfv);
107 switch (event->keyval) {
108 case GDK_q:
109 gtk_main_quit();
110 break;
112 case GDK_r:
113 gtk_tpdfv_reload(tpdfv);
114 break;
116 /* navigation */
117 case GDK_Page_Up:
118 gtk_tpdfv_page_up(tpdfv);
119 break;
120 case GDK_Page_Down:
121 /* fall through */
122 case GDK_space:
123 gtk_tpdfv_page_down(tpdfv);
124 break;
125 case GDK_Home:
126 gtk_tpdfv_first_page(tpdfv);
127 break;
128 case GDK_End:
129 gtk_tpdfv_last_page(tpdfv);
130 break;
132 /* zoom */
133 case GDK_plus:
134 gtk_tpdfv_zoom_in(tpdfv);
135 break;
136 case GDK_minus:
137 gtk_tpdfv_zoom_out(tpdfv);
138 break;
139 case GDK_1:
140 gtk_tpdfv_zoom_1(tpdfv);
141 break;
142 case GDK_w:
143 gtk_tpdfv_zoom_width(tpdfv);
144 break;
145 case GDK_h:
146 gtk_tpdfv_zoom_height(tpdfv);
147 break;
148 case GDK_z:
149 gtk_tpdfv_zoom_fit(tpdfv);
150 break;
152 /* scrolling */
153 case GDK_Up:
154 gtk_tpdfv_scroll_up(tpdfv);
155 break;
156 case GDK_Down:
157 gtk_tpdfv_scroll_down(tpdfv);
158 break;
159 case GDK_Left:
160 gtk_tpdfv_scroll_left(tpdfv);
161 break;
162 case GDK_Right:
163 gtk_tpdfv_scroll_right(tpdfv);
164 break;
165 }
166 return FALSE;
167 } /* key_press */
169 int
170 main(int argc, char **argv)
171 {
172 GtkWidget *win = NULL;
173 GtkWidget *tpdfv = NULL;
175 char win_title[1024];
177 GdkColor bg_color;
179 char *filename;
181 gtk_init(&argc, &argv);
183 while (42) {
184 int opt = getopt(argc, argv, "hV");
186 if (-1 == opt)
187 break;
189 switch (opt) {
190 case 'h':
191 exit_usage(argv[0], 0);
192 break;
193 case 'V':
194 exit_version();
195 break;
196 default:
197 exit_usage(argv[0], 1);
198 }
199 }
201 if (argc - optind != 1) {
202 fprintf(stderr, "%s: missing filename\n", argv[0]);
203 exit_usage(argv[0], 1);
204 }
206 filename = argv[optind];
208 tpdfv = gtk_tpdfv_new(filename);
209 if (! tpdfv)
210 return 1;
212 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
213 if (! win)
214 return 1;
216 snprintf(win_title, sizeof(win_title), "tpdfview: %s",
217 basename(filename));
218 gtk_window_set_title(GTK_WINDOW(win), win_title);
220 gtk_container_add(GTK_CONTAINER(win), tpdfv);
222 g_signal_connect(G_OBJECT(win), "destroy",
223 G_CALLBACK(on_destroy), NULL);
224 g_signal_connect(G_OBJECT(win), "key-press-event",
225 G_CALLBACK(key_press), tpdfv);
227 /* TODO: use resource file */
228 gdk_color_parse("#000000", &bg_color);
229 gtk_widget_modify_bg(tpdfv, GTK_STATE_NORMAL, &bg_color);
231 gtk_widget_show_all(win);
233 gtk_main();
234 return 0;
235 } /* main */
237 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */