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 Gtk::Main main_instance (&argc, const_cast<char ***>(&argv));
183 struct SPSlideShow ss;
185 int option,
186 num_parsed_options = 0;
188 // the list of arguments is in the net line
189 while ((option = getopt(argc, (char* const* )argv, "t:")) != -1)
190 {
191 switch(option) {
192 case 't': // for timer
193 // fprintf(stderr, "set timer arg %s\n", optarg );
194 ss.timer = atoi(optarg);
195 num_parsed_options += 2; // 2 because of flag + option
196 break;
197 case '?':
198 default:
199 usage();
200 }
201 }
203 GtkWidget *w;
204 int i;
206 bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
207 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
208 textdomain (GETTEXT_PACKAGE);
210 LIBXML_TEST_VERSION
212 Inkscape::GC::init();
213 Inkscape::Preferences::loadSkeleton();
215 gtk_init (&argc, (char ***) &argv);
217 #ifdef lalaWITH_MODULES
218 g_warning ("Have to autoinit modules (lauris)");
219 sp_modulesys_init();
220 #endif /* WITH_MODULES */
222 /* We must set LC_NUMERIC to default, or otherwise */
223 /* we'll end with localised SVG files :-( */
225 setlocale (LC_NUMERIC, "C");
227 ss.size = 32;
228 ss.length = 0;
229 ss.current = 0;
230 ss.slides = g_new (char *, ss.size);
231 ss.current = 0;
232 ss.doc = NULL;
233 ss.view = NULL;
234 ss.fullscreen = false;
236 inkscape = (Inkscape::Application *)g_object_new (SP_TYPE_INKSCAPE, NULL);
237 Inkscape::Preferences::load();
239 // starting at where the commandline options stopped parsing because
240 // we want all the files to be in the list
241 for (i = num_parsed_options + 1 ; i < argc; i++) {
242 struct stat st;
243 if (stat (argv[i], &st)
244 || !S_ISREG (st.st_mode)
245 || (st.st_size < 64)) {
246 fprintf(stderr, "could not open file %s\n", argv[i]);
247 } else {
249 #ifdef WITH_INKJAR
250 if (is_jar(argv[i])) {
251 Inkjar::JarFileReader jar_file_reader(argv[i]);
252 for (;;) {
253 GByteArray *gba = jar_file_reader.get_next_file();
254 if (gba == NULL) {
255 char *c_ptr;
256 gchar *last_filename = jar_file_reader.get_last_filename();
257 if (last_filename == NULL)
258 break;
259 if ((c_ptr = std::strrchr(last_filename, '/')) != NULL) {
260 if (*(++c_ptr) == '\0') {
261 g_free(last_filename);
262 continue;
263 }
264 }
265 } else if (gba->len > 0) {
266 //::write(1, gba->data, gba->len);
267 /* Append to list */
268 if (ss.length >= ss.size) {
269 /* Expand */
270 ss.size <<= 1;
271 ss.slides = g_renew (char *, ss.slides, ss.size);
272 }
274 ss.doc = sp_document_new_from_mem ((const gchar *)gba->data,
275 gba->len,
276 TRUE);
277 gchar *last_filename = jar_file_reader.get_last_filename();
278 if (ss.doc) {
279 ss.slides[ss.length++] = strdup (last_filename);
280 sp_document_set_uri (ss.doc, strdup(last_filename));
281 }
282 g_byte_array_free(gba, TRUE);
283 g_free(last_filename);
284 } else
285 break;
286 }
287 } else {
288 #endif /* WITH_INKJAR */
289 /* Append to list */
290 if (ss.length >= ss.size) {
291 /* Expand */
292 ss.size <<= 1;
293 ss.slides = g_renew (char *, ss.slides, ss.size);
295 }
297 ss.slides[ss.length++] = strdup (argv[i]);
298 ss.doc = sp_document_new (ss.slides[ss.current], TRUE, false);
300 if (!ss.doc && ++ss.current >= ss.length) {
301 /* No loadable documents */
302 return 1;
303 }
304 #ifdef WITH_INKJAR
305 }
306 #endif
307 }
308 }
310 if(!ss.doc)
311 return 1; /* none of the slides loadable */
313 w = gtk_window_new (GTK_WINDOW_TOPLEVEL);
314 gtk_window_set_title (GTK_WINDOW (w), SP_DOCUMENT_NAME (ss.doc));
315 gtk_window_set_default_size (GTK_WINDOW (w),
316 MIN ((int)sp_document_width (ss.doc), (int)gdk_screen_width () - 64),
317 MIN ((int)sp_document_height (ss.doc), (int)gdk_screen_height () - 64));
318 gtk_window_set_policy (GTK_WINDOW (w), TRUE, TRUE, FALSE);
319 ss.window = w;
321 g_signal_connect (G_OBJECT (w), "delete_event", (GCallback) sp_svgview_main_delete, &ss);
322 g_signal_connect (G_OBJECT (w), "key_press_event", (GCallback) sp_svgview_main_key_press, &ss);
324 ss.view = sp_svg_view_widget_new (ss.doc);
325 sp_svg_view_widget_set_resize (SP_SVG_VIEW_WIDGET (ss.view), FALSE, sp_document_width (ss.doc), sp_document_height (ss.doc));
326 sp_document_ensure_up_to_date (ss.doc);
327 sp_document_unref (ss.doc);
328 gtk_widget_show (ss.view);
329 gtk_container_add (GTK_CONTAINER (w), ss.view);
331 gtk_widget_show (w);
333 gtk_main ();
335 return 0;
336 }
338 static int
339 sp_svgview_ctrlwin_delete (GtkWidget *widget, GdkEvent *event, void *data)
340 {
341 ctrlwin = NULL;
342 return FALSE;
343 }
345 static GtkWidget *
346 sp_svgview_control_show (struct SPSlideShow *ss)
347 {
348 if (!ctrlwin) {
349 GtkWidget *t, *b;
350 ctrlwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
351 gtk_window_set_transient_for (GTK_WINDOW(ctrlwin), GTK_WINDOW(ss->window));
352 g_signal_connect (G_OBJECT (ctrlwin), "key_press_event", (GCallback) sp_svgview_main_key_press, ss);
353 g_signal_connect (G_OBJECT (ctrlwin), "delete_event", (GCallback) sp_svgview_ctrlwin_delete, NULL);
354 t = gtk_table_new (1, 4, TRUE);
355 gtk_container_add ((GtkContainer *) ctrlwin, t);
356 b = gtk_button_new_from_stock (GTK_STOCK_GOTO_FIRST);
357 gtk_table_attach ((GtkTable *) t, b, 0, 1, 0, 1,
358 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL),
359 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL),
360 0, 0);
361 g_signal_connect ((GObject *) b, "clicked", (GCallback) sp_svgview_goto_first_cb, ss);
362 b = gtk_button_new_from_stock (GTK_STOCK_GO_BACK);
363 gtk_table_attach ((GtkTable *) t, b, 1, 2, 0, 1,
364 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL),
365 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL),
366 0, 0);
367 g_signal_connect (G_OBJECT(b), "clicked", (GCallback) sp_svgview_show_prev_cb, ss);
368 b = gtk_button_new_from_stock (GTK_STOCK_GO_FORWARD);
369 gtk_table_attach ((GtkTable *) t, b, 2, 3, 0, 1,
370 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL),
371 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL),
372 0, 0);
373 g_signal_connect (G_OBJECT(b), "clicked", (GCallback) sp_svgview_show_next_cb, ss);
374 b = gtk_button_new_from_stock (GTK_STOCK_GOTO_LAST);
375 gtk_table_attach ((GtkTable *) t, b, 3, 4, 0, 1,
376 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL),
377 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL),
378 0, 0);
379 g_signal_connect (G_OBJECT(b), "clicked", (GCallback) sp_svgview_goto_last_cb, ss);
380 gtk_widget_show_all (ctrlwin);
381 } else {
382 gtk_window_present ((GtkWindow *) ctrlwin);
383 }
385 return NULL;
386 }
388 static int
389 sp_svgview_show_next_cb (GtkWidget *widget, void *data)
390 {
391 sp_svgview_show_next(static_cast<struct SPSlideShow *>(data));
392 return FALSE;
393 }
395 static int
396 sp_svgview_show_prev_cb (GtkWidget *widget, void *data)
397 {
398 sp_svgview_show_prev(static_cast<struct SPSlideShow *>(data));
399 return FALSE;
400 }
402 static int
403 sp_svgview_goto_first_cb (GtkWidget *widget, void *data)
404 {
405 sp_svgview_goto_first(static_cast<struct SPSlideShow *>(data));
406 return FALSE;
407 }
409 static int
410 sp_svgview_goto_last_cb (GtkWidget *widget, void *data)
411 {
412 sp_svgview_goto_last(static_cast<struct SPSlideShow *>(data));
413 return FALSE;
414 }
416 static void
417 sp_svgview_waiting_cursor(struct SPSlideShow *ss)
418 {
419 GdkCursor *waiting = gdk_cursor_new(GDK_WATCH);
420 gdk_window_set_cursor(GTK_WIDGET(ss->window)->window, waiting);
421 gdk_cursor_unref(waiting);
422 if (ctrlwin) {
423 GdkCursor *waiting = gdk_cursor_new(GDK_WATCH);
424 gdk_window_set_cursor(GTK_WIDGET(ctrlwin)->window, waiting);
425 gdk_cursor_unref(waiting);
426 }
427 while(gtk_events_pending())
428 gtk_main_iteration();
429 }
431 static void
432 sp_svgview_normal_cursor(struct SPSlideShow *ss)
433 {
434 gdk_window_set_cursor(GTK_WIDGET(ss->window)->window, NULL);
435 if (ctrlwin) {
436 gdk_window_set_cursor(GTK_WIDGET(ctrlwin)->window, NULL);
437 }
438 }
440 static void
441 sp_svgview_set_document(struct SPSlideShow *ss, SPDocument *doc, int current)
442 {
443 if (doc && doc != ss->doc) {
444 reinterpret_cast<SPSVGView*>(SP_VIEW_WIDGET_VIEW (ss->view))->setDocument (doc);
445 sp_document_ensure_up_to_date (doc);
446 if (ss->doc) {
447 delete ss->doc;
448 }
449 ss->doc = doc;
450 ss->current = current;
451 }
452 }
454 static void
455 sp_svgview_show_next (struct SPSlideShow *ss)
456 {
457 sp_svgview_waiting_cursor(ss);
459 SPDocument *doc = NULL;
460 int current = ss->current;
461 while (!doc && (current < ss->length - 1)) {
462 doc = sp_document_new (ss->slides[++current], TRUE, false);
463 }
465 sp_svgview_set_document(ss, doc, current);
467 sp_svgview_normal_cursor(ss);
468 }
470 static void
471 sp_svgview_show_prev (struct SPSlideShow *ss)
472 {
473 sp_svgview_waiting_cursor(ss);
475 SPDocument *doc = NULL;
476 int current = ss->current;
477 while (!doc && (current > 0)) {
478 doc = sp_document_new (ss->slides[--current], TRUE, false);
479 }
481 sp_svgview_set_document(ss, doc, current);
483 sp_svgview_normal_cursor(ss);
484 }
486 static void
487 sp_svgview_goto_first (struct SPSlideShow *ss)
488 {
489 sp_svgview_waiting_cursor(ss);
491 SPDocument *doc = NULL;
492 int current = 0;
493 while ( !doc && (current < ss->length - 1)) {
494 if (current == ss->current)
495 break;
496 doc = sp_document_new (ss->slides[current++], TRUE, false);
497 }
499 sp_svgview_set_document(ss, doc, current - 1);
501 sp_svgview_normal_cursor(ss);
502 }
504 static void
505 sp_svgview_goto_last (struct SPSlideShow *ss)
506 {
507 sp_svgview_waiting_cursor(ss);
509 SPDocument *doc = NULL;
510 int current = ss->length - 1;
511 while (!doc && (current >= 0)) {
512 if (current == ss->current)
513 break;
514 doc = sp_document_new (ss->slides[current--], TRUE, false);
515 }
517 sp_svgview_set_document(ss, doc, current + 1);
519 sp_svgview_normal_cursor(ss);
520 }
522 #ifdef WITH_INKJAR
523 static bool
524 is_jar(char const *filename)
525 {
526 /* fixme: Check MIME type or something. /usr/share/misc/file/magic suggests that checking for
527 initial string "PK\003\004" in content should suffice. */
528 size_t const filename_len = strlen(filename);
529 if (filename_len < 5) {
530 return false;
531 }
532 char const *extension = filename + filename_len - 4;
533 return ((memcmp(extension, ".jar", 4) == 0) ||
534 (memcmp(extension, ".sxw", 4) == 0) );
535 }
536 #endif /* WITH_INKJAR */
538 static void usage()
539 {
540 fprintf(stderr,
541 "Usage: inkview [OPTIONS...] [FILES ...]\n"
542 "\twhere FILES are SVG (.svg or .svgz)"
543 #ifdef WITH_INKJAR
544 " or archives of SVGs (.sxw, .jar)"
545 #endif
546 "\n\n"
547 "Available options:\n"
548 "\t-t\t\tTimer for automatically changing slides in seconds.\n"
549 "\n");
550 exit(1);
551 }
553 #ifdef XXX
554 /* TODO !!! make this temporary stub unnecessary */
555 Inkscape::Application *inkscape_get_instance() { return NULL; }
556 void inkscape_ref (void) {}
557 void inkscape_unref (void) {}
558 void inkscape_add_document (SPDocument *document) {}
559 void inkscape_remove_document (SPDocument *document) {}
560 Inkscape::XML::Node *inkscape_get_repr (Inkscape::Application *inkscape, const gchar *key) {return NULL;}
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 :