Code

8c556004540d712a2638292f35bce77e4056d684
[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;
139                 /* scrolling */
140                 case GDK_Up:
141                         gtk_tpdfv_scroll_up(tpdfv);
142                         break;
143                 case GDK_Down:
144                         gtk_tpdfv_scroll_down(tpdfv);
145                         break;
146                 case GDK_Left:
147                         gtk_tpdfv_scroll_left(tpdfv);
148                         break;
149                 case GDK_Right:
150                         gtk_tpdfv_scroll_right(tpdfv);
151                         break;
152         }
153         return FALSE;
154 } /* key_press */
156 int
157 main(int argc, char **argv)
159         GtkWidget *win   = NULL;
160         GtkWidget *tpdfv = NULL;
162         GdkColor bg_color;
164         gtk_init(&argc, &argv);
166         while (42) {
167                 int opt = getopt(argc, argv, "hV");
169                 if (-1 == opt)
170                         break;
172                 switch (opt) {
173                         case 'h':
174                                 exit_usage(argv[0], 0);
175                                 break;
176                         case 'V':
177                                 exit_version();
178                                 break;
179                         default:
180                                 exit_usage(argv[0], 1);
181                 }
182         }
184         if (argc - optind != 1) {
185                 fprintf(stderr, "%s: missing filename\n", argv[0]);
186                 exit_usage(argv[0], 1);
187         }
189         tpdfv = gtk_tpdfv_new(argv[optind]);
190         if (! tpdfv)
191                 return 1;
193         win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
194         if (! win)
195                 return 1;
197         gtk_container_add(GTK_CONTAINER(win), tpdfv);
199         g_signal_connect(G_OBJECT(win), "destroy",
200                         G_CALLBACK(on_destroy), NULL);
201         g_signal_connect(G_OBJECT(win), "key-press-event",
202                         G_CALLBACK(key_press), tpdfv);
204         /* TODO: use resource file */
205         gdk_color_parse("#000000", &bg_color);
206         gtk_widget_modify_bg(tpdfv, GTK_STATE_NORMAL, &bg_color);
208         gtk_widget_show_all(win);
210         gtk_main();
211         return 0;
212 } /* main */
214 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */