1 #define __SPSVGVIEW_C__
3 /*
4 * Inkscape - an ambitious vector drawing program
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * Frank Felfe <innerspace@iname.com>
9 * Davide Puricelli <evo@debian.org>
10 * Mitsuru Oka <oka326@parkcity.ne.jp>
11 * Masatake YAMATO <jet@gyve.org>
12 * F.J.Franklin <F.J.Franklin@sheffield.ac.uk>
13 * Michael Meeks <michael@helixcode.com>
14 * Chema Celorio <chema@celorio.com>
15 * Pawel Palucha
16 * ... and various people who have worked with various projects
17 *
18 * Copyright (C) 1999-2002 authors
19 * Copyright (C) 2001-2002 Ximian, Inc.
20 *
21 * Inkscape authors:
22 * Johan Ceuppens
23 *
24 * Copyright (C) 2004 Inkscape authors
25 *
26 * Released under GNU GPL, read the file 'COPYING' for more information
27 */
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #ifdef HAVE_GETOPT_H
35 #include <getopt.h>
36 #endif
38 #include <string.h>
39 #include <sys/stat.h>
40 #include <locale.h>
42 #include <glib/gmem.h>
43 #include <libnr/nr-macros.h>
45 // #include <stropts.h>
47 #include <libxml/tree.h>
48 #include <gdk/gdkkeysyms.h>
49 #include <gtk/gtkmain.h>
50 #include <gtk/gtkstock.h>
51 #include <gtk/gtkwindow.h>
52 #include <gtk/gtktable.h>
53 #include <gtk/gtkbutton.h>
55 #include <gtkmm/main.h>
57 #include "gc-core.h"
58 #include "preferences.h"
60 #include <glibmm/i18n.h>
61 #include "document.h"
62 #include "svg-view.h"
63 #include "svg-view-widget.h"
65 #ifdef WITH_INKJAR
66 #include "inkjar/jar.h"
67 #endif
69 #include "inkscape-private.h"
71 Inkscape::Application *inkscape;
73 #include <iostream>
75 #ifndef HAVE_BIND_TEXTDOMAIN_CODESET
76 #define bind_textdomain_codeset(p,c)
77 #endif
79 extern char *optarg;
80 extern int optind, opterr;
82 struct SPSlideShow {
83 char **slides;
84 int size;
85 int length;
86 int current;
87 SPDocument *doc;
88 GtkWidget *view;
89 GtkWidget *window;
90 bool fullscreen;
91 int timer;
92 };
94 static GtkWidget *sp_svgview_control_show (struct SPSlideShow *ss);
95 static void sp_svgview_show_next (struct SPSlideShow *ss);
96 static void sp_svgview_show_prev (struct SPSlideShow *ss);
97 static void sp_svgview_goto_first (struct SPSlideShow *ss);
98 static void sp_svgview_goto_last (struct SPSlideShow *ss);
100 static int sp_svgview_show_next_cb (GtkWidget *widget, void *data);
101 static int sp_svgview_show_prev_cb (GtkWidget *widget, void *data);
102 static int sp_svgview_goto_first_cb (GtkWidget *widget, void *data);
103 static int sp_svgview_goto_last_cb (GtkWidget *widget, void *data);
104 #ifdef WITH_INKJAR
105 static bool is_jar(char const *filename);
106 #endif
107 static void usage();
109 static GtkWidget *ctrlwin = NULL;
111 /// Dummy functions to keep linker happy
112 int sp_main_gui (int, char const**) { return 0; }
113 int sp_main_console (int, char const**) { return 0; }
115 static int
116 sp_svgview_main_delete (GtkWidget */*widget*/, GdkEvent */*event*/, struct SPSlideShow */*ss*/)
117 {
118 gtk_main_quit ();
119 return FALSE;
120 }
122 static int
123 sp_svgview_main_key_press (GtkWidget */*widget*/, GdkEventKey *event, struct SPSlideShow *ss)
124 {
125 switch (event->keyval) {
126 case GDK_Up:
127 case GDK_Home:
128 sp_svgview_goto_first(ss);
129 break;
130 case GDK_Down:
131 case GDK_End:
132 sp_svgview_goto_last(ss);
133 break;
134 case GDK_F11:
135 #ifdef HAVE_GTK_WINDOW_FULLSCREEN
136 if (ss->fullscreen) {
137 gtk_window_unfullscreen (GTK_WINDOW(ss->window));
138 ss->fullscreen = false;
139 } else {
140 gtk_window_fullscreen (GTK_WINDOW(ss->window));
141 ss->fullscreen = true;
142 }
143 #else
144 std::cout<<"Your GTK+ does not support fullscreen mode. Upgrade to 2.2."<<std::endl;
145 #endif
146 break;
147 case GDK_Return:
148 sp_svgview_control_show (ss);
149 break;
150 case GDK_KP_Page_Down:
151 case GDK_Page_Down:
152 case GDK_Right:
153 case GDK_space:
154 sp_svgview_show_next (ss);
155 break;
156 case GDK_KP_Page_Up:
157 case GDK_Page_Up:
158 case GDK_Left:
159 case GDK_BackSpace:
160 sp_svgview_show_prev (ss);
161 break;
162 case GDK_Escape:
163 case GDK_q:
164 case GDK_Q:
165 gtk_main_quit();
166 break;
167 default:
168 break;
169 }
170 gtk_window_set_title(GTK_WINDOW(ss->window), SP_DOCUMENT_NAME(ss->doc));
171 return TRUE;
172 }
174 int
175 main (int argc, const char **argv)
176 {
177 if (argc == 1) {
178 usage();
179 }
181 // Prevents errors like "Unable to wrap GdkPixbuf..." (in nr-filter-image.cpp for example)
182 Gtk::Main::init_gtkmm_internals();
184 Gtk::Main main_instance (&argc, const_cast<char ***>(&argv));
186 struct SPSlideShow ss;
188 int option,
189 num_parsed_options = 0;
191 // the list of arguments is in the net line
192 while ((option = getopt(argc, (char* const* )argv, "t:")) != -1)
193 {
194 switch(option) {
195 case 't': // for timer
196 // fprintf(stderr, "set timer arg %s\n", optarg );
197 ss.timer = atoi(optarg);
198 num_parsed_options += 2; // 2 because of flag + option
199 break;
200 case '?':
201 default:
202 usage();
203 }
204 }
206 GtkWidget *w;
207 int i;
209 bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
210 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
211 textdomain (GETTEXT_PACKAGE);
213 LIBXML_TEST_VERSION
215 Inkscape::GC::init();
216 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
217 prefs->load(false, true); // keep quiet about any failures
219 gtk_init (&argc, (char ***) &argv);
221 #ifdef lalaWITH_MODULES
222 g_warning ("Have to autoinit modules (lauris)");
223 sp_modulesys_init();
224 #endif /* WITH_MODULES */
226 /* We must set LC_NUMERIC to default, or otherwise */
227 /* we'll end with localised SVG files :-( */
229 setlocale (LC_NUMERIC, "C");
231 ss.size = 32;
232 ss.length = 0;
233 ss.current = 0;
234 ss.slides = g_new (char *, ss.size);
235 ss.current = 0;
236 ss.doc = NULL;
237 ss.view = NULL;
238 ss.fullscreen = false;
240 inkscape = (Inkscape::Application *)g_object_new (SP_TYPE_INKSCAPE, NULL);
242 // starting at where the commandline options stopped parsing because
243 // we want all the files to be in the list
244 for (i = num_parsed_options + 1 ; i < argc; i++) {
245 struct stat st;
246 if (stat (argv[i], &st)
247 || !S_ISREG (st.st_mode)
248 || (st.st_size < 64)) {
249 fprintf(stderr, "could not open file %s\n", argv[i]);
250 } else {
252 #ifdef WITH_INKJAR
253 if (is_jar(argv[i])) {
254 Inkjar::JarFileReader jar_file_reader(argv[i]);
255 for (;;) {
256 GByteArray *gba = jar_file_reader.get_next_file();
257 if (gba == NULL) {
258 char *c_ptr;
259 gchar *last_filename = jar_file_reader.get_last_filename();
260 if (last_filename == NULL)
261 break;
262 if ((c_ptr = std::strrchr(last_filename, '/')) != NULL) {
263 if (*(++c_ptr) == '\0') {
264 g_free(last_filename);
265 continue;
266 }
267 }
268 } else if (gba->len > 0) {
269 //::write(1, gba->data, gba->len);
270 /* Append to list */
271 if (ss.length >= ss.size) {
272 /* Expand */
273 ss.size <<= 1;
274 ss.slides = g_renew (char *, ss.slides, ss.size);
275 }
277 ss.doc = sp_document_new_from_mem ((const gchar *)gba->data,
278 gba->len,
279 TRUE);
280 gchar *last_filename = jar_file_reader.get_last_filename();
281 if (ss.doc) {
282 ss.slides[ss.length++] = strdup (last_filename);
283 sp_document_set_uri (ss.doc, strdup(last_filename));
284 }
285 g_byte_array_free(gba, TRUE);
286 g_free(last_filename);
287 } else
288 break;
289 }
290 } else {
291 #endif /* WITH_INKJAR */
292 /* Append to list */
293 if (ss.length >= ss.size) {
294 /* Expand */
295 ss.size <<= 1;
296 ss.slides = g_renew (char *, ss.slides, ss.size);
298 }
300 ss.slides[ss.length++] = strdup (argv[i]);
302 if (!ss.doc) {
303 ss.doc = sp_document_new (ss.slides[ss.current], TRUE, false);
304 if (!ss.doc)
305 ++ss.current;
306 }
307 #ifdef WITH_INKJAR
308 }
309 #endif
310 }
311 }
313 if(!ss.doc)
314 return 1; /* none of the slides loadable */
316 w = gtk_window_new (GTK_WINDOW_TOPLEVEL);
317 gtk_window_set_title (GTK_WINDOW (w), SP_DOCUMENT_NAME (ss.doc));
318 gtk_window_set_default_size (GTK_WINDOW (w),
319 MIN ((int)sp_document_width (ss.doc), (int)gdk_screen_width () - 64),
320 MIN ((int)sp_document_height (ss.doc), (int)gdk_screen_height () - 64));
321 gtk_window_set_policy (GTK_WINDOW (w), TRUE, TRUE, FALSE);
322 ss.window = w;
324 g_signal_connect (G_OBJECT (w), "delete_event", (GCallback) sp_svgview_main_delete, &ss);
325 g_signal_connect (G_OBJECT (w), "key_press_event", (GCallback) sp_svgview_main_key_press, &ss);
327 sp_document_ensure_up_to_date (ss.doc);
328 ss.view = sp_svg_view_widget_new (ss.doc);
329 sp_document_unref (ss.doc);
330 sp_svg_view_widget_set_resize (SP_SVG_VIEW_WIDGET (ss.view), FALSE,
331 sp_document_width (ss.doc), sp_document_height (ss.doc));
332 gtk_widget_show (ss.view);
333 gtk_container_add (GTK_CONTAINER (w), ss.view);
335 gtk_widget_show (w);
337 gtk_main ();
339 return 0;
340 }
342 static int
343 sp_svgview_ctrlwin_delete (GtkWidget */*widget*/, GdkEvent */*event*/, void */*data*/)
344 {
345 ctrlwin = NULL;
346 return FALSE;
347 }
349 static GtkWidget *
350 sp_svgview_control_show (struct SPSlideShow *ss)
351 {
352 if (!ctrlwin) {
353 GtkWidget *t, *b;
354 ctrlwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
355 gtk_window_set_transient_for (GTK_WINDOW(ctrlwin), GTK_WINDOW(ss->window));
356 g_signal_connect (G_OBJECT (ctrlwin), "key_press_event", (GCallback) sp_svgview_main_key_press, ss);
357 g_signal_connect (G_OBJECT (ctrlwin), "delete_event", (GCallback) sp_svgview_ctrlwin_delete, NULL);
358 t = gtk_table_new (1, 4, TRUE);
359 gtk_container_add ((GtkContainer *) ctrlwin, t);
360 b = gtk_button_new_from_stock (GTK_STOCK_GOTO_FIRST);
361 gtk_table_attach ((GtkTable *) t, b, 0, 1, 0, 1,
362 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL),
363 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL),
364 0, 0);
365 g_signal_connect ((GObject *) b, "clicked", (GCallback) sp_svgview_goto_first_cb, ss);
366 b = gtk_button_new_from_stock (GTK_STOCK_GO_BACK);
367 gtk_table_attach ((GtkTable *) t, b, 1, 2, 0, 1,
368 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL),
369 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL),
370 0, 0);
371 g_signal_connect (G_OBJECT(b), "clicked", (GCallback) sp_svgview_show_prev_cb, ss);
372 b = gtk_button_new_from_stock (GTK_STOCK_GO_FORWARD);
373 gtk_table_attach ((GtkTable *) t, b, 2, 3, 0, 1,
374 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL),
375 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL),
376 0, 0);
377 g_signal_connect (G_OBJECT(b), "clicked", (GCallback) sp_svgview_show_next_cb, ss);
378 b = gtk_button_new_from_stock (GTK_STOCK_GOTO_LAST);
379 gtk_table_attach ((GtkTable *) t, b, 3, 4, 0, 1,
380 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL),
381 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL),
382 0, 0);
383 g_signal_connect (G_OBJECT(b), "clicked", (GCallback) sp_svgview_goto_last_cb, ss);
384 gtk_widget_show_all (ctrlwin);
385 } else {
386 gtk_window_present ((GtkWindow *) ctrlwin);
387 }
389 return NULL;
390 }
392 static int
393 sp_svgview_show_next_cb (GtkWidget */*widget*/, void *data)
394 {
395 sp_svgview_show_next(static_cast<struct SPSlideShow *>(data));
396 return FALSE;
397 }
399 static int
400 sp_svgview_show_prev_cb (GtkWidget */*widget*/, void *data)
401 {
402 sp_svgview_show_prev(static_cast<struct SPSlideShow *>(data));
403 return FALSE;
404 }
406 static int
407 sp_svgview_goto_first_cb (GtkWidget */*widget*/, void *data)
408 {
409 sp_svgview_goto_first(static_cast<struct SPSlideShow *>(data));
410 return FALSE;
411 }
413 static int
414 sp_svgview_goto_last_cb (GtkWidget */*widget*/, void *data)
415 {
416 sp_svgview_goto_last(static_cast<struct SPSlideShow *>(data));
417 return FALSE;
418 }
420 static void
421 sp_svgview_waiting_cursor(struct SPSlideShow *ss)
422 {
423 GdkCursor *waiting = gdk_cursor_new(GDK_WATCH);
424 gdk_window_set_cursor(GTK_WIDGET(ss->window)->window, waiting);
425 gdk_cursor_unref(waiting);
426 if (ctrlwin) {
427 GdkCursor *waiting = gdk_cursor_new(GDK_WATCH);
428 gdk_window_set_cursor(GTK_WIDGET(ctrlwin)->window, waiting);
429 gdk_cursor_unref(waiting);
430 }
431 while(gtk_events_pending())
432 gtk_main_iteration();
433 }
435 static void
436 sp_svgview_normal_cursor(struct SPSlideShow *ss)
437 {
438 gdk_window_set_cursor(GTK_WIDGET(ss->window)->window, NULL);
439 if (ctrlwin) {
440 gdk_window_set_cursor(GTK_WIDGET(ctrlwin)->window, NULL);
441 }
442 }
444 static void
445 sp_svgview_set_document(struct SPSlideShow *ss, SPDocument *doc, int current)
446 {
447 if (doc && doc != ss->doc) {
448 sp_document_ensure_up_to_date (doc);
449 reinterpret_cast<SPSVGView*>(SP_VIEW_WIDGET_VIEW (ss->view))->setDocument (doc);
450 if (ss->doc) {
451 delete ss->doc;
452 }
453 ss->doc = doc;
454 ss->current = current;
455 }
456 }
458 static void
459 sp_svgview_show_next (struct SPSlideShow *ss)
460 {
461 sp_svgview_waiting_cursor(ss);
463 SPDocument *doc = NULL;
464 int current = ss->current;
465 while (!doc && (current < ss->length - 1)) {
466 doc = sp_document_new (ss->slides[++current], TRUE, false);
467 }
469 sp_svgview_set_document(ss, doc, current);
471 sp_svgview_normal_cursor(ss);
472 }
474 static void
475 sp_svgview_show_prev (struct SPSlideShow *ss)
476 {
477 sp_svgview_waiting_cursor(ss);
479 SPDocument *doc = NULL;
480 int current = ss->current;
481 while (!doc && (current > 0)) {
482 doc = sp_document_new (ss->slides[--current], TRUE, false);
483 }
485 sp_svgview_set_document(ss, doc, current);
487 sp_svgview_normal_cursor(ss);
488 }
490 static void
491 sp_svgview_goto_first (struct SPSlideShow *ss)
492 {
493 sp_svgview_waiting_cursor(ss);
495 SPDocument *doc = NULL;
496 int current = 0;
497 while ( !doc && (current < ss->length - 1)) {
498 if (current == ss->current)
499 break;
500 doc = sp_document_new (ss->slides[current++], TRUE, false);
501 }
503 sp_svgview_set_document(ss, doc, current - 1);
505 sp_svgview_normal_cursor(ss);
506 }
508 static void
509 sp_svgview_goto_last (struct SPSlideShow *ss)
510 {
511 sp_svgview_waiting_cursor(ss);
513 SPDocument *doc = NULL;
514 int current = ss->length - 1;
515 while (!doc && (current >= 0)) {
516 if (current == ss->current)
517 break;
518 doc = sp_document_new (ss->slides[current--], TRUE, false);
519 }
521 sp_svgview_set_document(ss, doc, current + 1);
523 sp_svgview_normal_cursor(ss);
524 }
526 #ifdef WITH_INKJAR
527 static bool
528 is_jar(char const *filename)
529 {
530 /* fixme: Check MIME type or something. /usr/share/misc/file/magic suggests that checking for
531 initial string "PK\003\004" in content should suffice. */
532 size_t const filename_len = strlen(filename);
533 if (filename_len < 5) {
534 return false;
535 }
536 char const *extension = filename + filename_len - 4;
537 return ((memcmp(extension, ".jar", 4) == 0) ||
538 (memcmp(extension, ".sxw", 4) == 0) );
539 }
540 #endif /* WITH_INKJAR */
542 static void usage()
543 {
544 fprintf(stderr,
545 "Usage: inkview [OPTIONS...] [FILES ...]\n"
546 "\twhere FILES are SVG (.svg or .svgz)"
547 #ifdef WITH_INKJAR
548 " or archives of SVGs (.sxw, .jar)"
549 #endif
550 "\n");
551 exit(1);
552 }
554 #ifdef XXX
555 /* TODO !!! make this temporary stub unnecessary */
556 Inkscape::Application *inkscape_get_instance() { return NULL; }
557 void inkscape_ref (void) {}
558 void inkscape_unref (void) {}
559 void inkscape_add_document (SPDocument *document) {}
560 void inkscape_remove_document (SPDocument *document) {}
561 #endif
564 /*
565 Local Variables:
566 mode:c++
567 c-file-style:"stroustrup"
568 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
569 indent-tabs-mode:nil
570 fill-column:99
571 End:
572 */
573 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :