Code

b92d5440799e212cf3314c8be6da87d544b0ade4
[tpdfview.git] / src / tpdfview.c
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)
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                 /* navigation */
113                 case GDK_Page_Up:
114                         gtk_tpdfv_page_up(tpdfv);
115                         break;
116                 case GDK_Page_Down:
117                         /* fall through */
118                 case GDK_space:
119                         gtk_tpdfv_page_down(tpdfv);
120                         break;
121                 case GDK_Home:
122                         gtk_tpdfv_first_page(tpdfv);
123                         break;
124                 case GDK_End:
125                         gtk_tpdfv_last_page(tpdfv);
126                         break;
128                 /* zoom */
129                 case GDK_plus:
130                         gtk_tpdfv_zoom_in(tpdfv);
131                         break;
132                 case GDK_minus:
133                         gtk_tpdfv_zoom_out(tpdfv);
134                         break;
135                 case GDK_1:
136                         gtk_tpdfv_zoom_1(tpdfv);
137                         break;
138                 case GDK_w:
139                         gtk_tpdfv_zoom_width(tpdfv);
140                         break;
141                 case GDK_h:
142                         gtk_tpdfv_zoom_height(tpdfv);
143                         break;
144                 case GDK_z:
145                         gtk_tpdfv_zoom_fit(tpdfv);
146                         break;
148                 /* scrolling */
149                 case GDK_Up:
150                         gtk_tpdfv_scroll_up(tpdfv);
151                         break;
152                 case GDK_Down:
153                         gtk_tpdfv_scroll_down(tpdfv);
154                         break;
155                 case GDK_Left:
156                         gtk_tpdfv_scroll_left(tpdfv);
157                         break;
158                 case GDK_Right:
159                         gtk_tpdfv_scroll_right(tpdfv);
160                         break;
161         }
162         return FALSE;
163 } /* key_press */
165 int
166 main(int argc, char **argv)
168         GtkWidget *win   = NULL;
169         GtkWidget *tpdfv = NULL;
171         char win_title[1024];
173         GdkColor bg_color;
175         char *filename;
177         gtk_init(&argc, &argv);
179         while (42) {
180                 int opt = getopt(argc, argv, "hV");
182                 if (-1 == opt)
183                         break;
185                 switch (opt) {
186                         case 'h':
187                                 exit_usage(argv[0], 0);
188                                 break;
189                         case 'V':
190                                 exit_version();
191                                 break;
192                         default:
193                                 exit_usage(argv[0], 1);
194                 }
195         }
197         if (argc - optind != 1) {
198                 fprintf(stderr, "%s: missing filename\n", argv[0]);
199                 exit_usage(argv[0], 1);
200         }
202         filename = argv[optind];
204         tpdfv = gtk_tpdfv_new(filename);
205         if (! tpdfv)
206                 return 1;
208         win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
209         if (! win)
210                 return 1;
212         snprintf(win_title, sizeof(win_title), "tpdfview: %s",
213                         basename(filename));
214         gtk_window_set_title(GTK_WINDOW(win), win_title);
216         gtk_container_add(GTK_CONTAINER(win), tpdfv);
218         g_signal_connect(G_OBJECT(win), "destroy",
219                         G_CALLBACK(on_destroy), NULL);
220         g_signal_connect(G_OBJECT(win), "key-press-event",
221                         G_CALLBACK(key_press), tpdfv);
223         /* TODO: use resource file */
224         gdk_color_parse("#000000", &bg_color);
225         gtk_widget_modify_bg(tpdfv, GTK_STATE_NORMAL, &bg_color);
227         gtk_widget_show_all(win);
229         gtk_main();
230         return 0;
231 } /* main */
233 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */