1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- |
---|
2 | * test-display: |
---|
3 | * |
---|
4 | * Copyright (C) 2002 Dom Lachowicz |
---|
5 | * |
---|
6 | * This program is released into the PUBLIC DOMAIN, and is meant to be a |
---|
7 | * useful example if how to draw a SVG image inside of a GtkWidget. This |
---|
8 | * program is free software; you can redistribute it and/or modify it at your |
---|
9 | * will. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE |
---|
14 | * |
---|
15 | * Simple utility to view a SVG file inside of a GtkWindow |
---|
16 | * |
---|
17 | * To compile, basically: |
---|
18 | * gcc `pkg-config --cflags --libs gtk+-2.0 librsvg-2.0` -lpopt -o svg-display test-display.c |
---|
19 | */ |
---|
20 | |
---|
21 | #include "config.h" |
---|
22 | #include "rsvg.h" |
---|
23 | |
---|
24 | #include <stdio.h> |
---|
25 | #include <stdlib.h> |
---|
26 | #include <popt.h> |
---|
27 | |
---|
28 | #include <gtk/gtk.h> |
---|
29 | |
---|
30 | static void |
---|
31 | quit_cb (GtkWidget *win, gpointer unused) |
---|
32 | { |
---|
33 | /* exit the main loop */ |
---|
34 | gtk_main_quit(); |
---|
35 | } |
---|
36 | |
---|
37 | static void |
---|
38 | view_pixbuf (GdkPixbuf * pixbuf) |
---|
39 | { |
---|
40 | GtkWidget *win, *img; |
---|
41 | |
---|
42 | /* create toplevel window and set its title */ |
---|
43 | win = gtk_window_new (GTK_WINDOW_TOPLEVEL); |
---|
44 | gtk_window_set_title (GTK_WINDOW(win), "SVG Viewer"); |
---|
45 | |
---|
46 | /* exit when 'X' is clicked */ |
---|
47 | g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(quit_cb), NULL); |
---|
48 | |
---|
49 | /* create a new image */ |
---|
50 | img = gtk_image_new_from_pixbuf (pixbuf); |
---|
51 | |
---|
52 | /* pack the window with the image */ |
---|
53 | gtk_container_add(GTK_CONTAINER(win), img); |
---|
54 | gtk_widget_show_all (win); |
---|
55 | } |
---|
56 | |
---|
57 | int |
---|
58 | main (int argc, char **argv) |
---|
59 | { |
---|
60 | poptContext popt_context; |
---|
61 | double x_zoom = 1.0; |
---|
62 | double y_zoom = 1.0; |
---|
63 | double dpi = -1.0; |
---|
64 | int width = -1; |
---|
65 | int height = -1; |
---|
66 | int bVersion = 0; |
---|
67 | |
---|
68 | struct poptOption options_table[] = { |
---|
69 | { "dpi" , 'd', POPT_ARG_DOUBLE, &dpi, 0, "Pixels Per Inch", "<float>"}, |
---|
70 | { "x-zoom", 'x', POPT_ARG_DOUBLE, &x_zoom, 0, "x zoom factor", "<float>" }, |
---|
71 | { "y-zoom", 'y', POPT_ARG_DOUBLE, &y_zoom, 0, "y zoom factor", "<float>" }, |
---|
72 | { "width", 'w', POPT_ARG_INT, &width, 0, "width", "<int>" }, |
---|
73 | { "height", 'h', POPT_ARG_INT, &height, 0, "height", "<int>" }, |
---|
74 | { "version", 'v', POPT_ARG_NONE, &bVersion, 0, "show version information", NULL }, |
---|
75 | POPT_AUTOHELP |
---|
76 | POPT_TABLEEND |
---|
77 | }; |
---|
78 | int c; |
---|
79 | const char * const *args; |
---|
80 | gint n_args = 0; |
---|
81 | GdkPixbuf *pixbuf; |
---|
82 | |
---|
83 | popt_context = poptGetContext ("svg-display", argc, (const char **)argv, options_table, 0); |
---|
84 | poptSetOtherOptionHelp(popt_context, "[OPTIONS...] file.svg"); |
---|
85 | |
---|
86 | c = poptGetNextOpt (popt_context); |
---|
87 | args = poptGetArgs (popt_context); |
---|
88 | |
---|
89 | if (bVersion != 0) |
---|
90 | { |
---|
91 | printf ("svg-display version %s\n", VERSION); |
---|
92 | return 0; |
---|
93 | } |
---|
94 | |
---|
95 | if (args) |
---|
96 | { |
---|
97 | while (args[n_args] != NULL) |
---|
98 | n_args++; |
---|
99 | } |
---|
100 | |
---|
101 | if (n_args != 1) |
---|
102 | { |
---|
103 | poptPrintHelp (popt_context, stderr, 0); |
---|
104 | poptFreeContext (popt_context); |
---|
105 | return 1; |
---|
106 | } |
---|
107 | |
---|
108 | /* initialize gtk+ */ |
---|
109 | gtk_init (&argc, &argv) ; |
---|
110 | |
---|
111 | if (dpi > 0.) |
---|
112 | rsvg_set_default_dpi (dpi); |
---|
113 | |
---|
114 | /* if both are unspecified, assume user wants to zoom the pixbuf in at least 1 dimension */ |
---|
115 | if (width == -1 && height == -1) |
---|
116 | pixbuf = rsvg_pixbuf_from_file_at_zoom (args[0], x_zoom, y_zoom, NULL); |
---|
117 | /* if both are unspecified, assume user wants to resize pixbuf in at least 1 dimension */ |
---|
118 | else if (x_zoom == 1.0 && y_zoom == 1.0) |
---|
119 | pixbuf = rsvg_pixbuf_from_file_at_size (args[0], width, height, NULL); |
---|
120 | else |
---|
121 | /* assume the user wants to zoom the pixbuf, but cap the maximum size */ |
---|
122 | pixbuf = rsvg_pixbuf_from_file_at_zoom_with_max (args[0], x_zoom, y_zoom, |
---|
123 | width, height, NULL); |
---|
124 | |
---|
125 | poptFreeContext (popt_context); |
---|
126 | |
---|
127 | if (!pixbuf) |
---|
128 | { |
---|
129 | fprintf (stderr, "Error displaying pixbuf!\n"); |
---|
130 | return 1; |
---|
131 | } |
---|
132 | |
---|
133 | view_pixbuf (pixbuf); |
---|
134 | |
---|
135 | /* run the gtk+ main loop */ |
---|
136 | gtk_main (); |
---|
137 | |
---|
138 | g_object_unref (G_OBJECT(pixbuf)); |
---|
139 | |
---|
140 | return 0; |
---|
141 | } |
---|