]> www.fi.muni.cz Git - evince.git/blob - ps/ggvutils.c
Import ggv backend. Not hooked up yet.
[evince.git] / ps / ggvutils.c
1 /*
2  * ggv-utils.c: misc utility functions
3  *
4  * Copyright 2002 - 2005 The Free Software Foundation
5  *
6  * Author: Jaka Mocnik  <jaka@gnu.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #include "config.h"
24 #include <math.h>
25 #include <ctype.h>
26 #include <sys/stat.h>
27
28 #include <gnome.h>
29 #include <gtk/gtkstock.h>
30 #include <gtk/gtkiconfactory.h>
31
32 #include <libgnomevfs/gnome-vfs-utils.h>
33
34 #include "ggvutils.h"
35
36 typedef struct {
37   char *stock_id;
38   char *name;
39 } GgvStockIcon;
40
41 GtkGSPaperSize ggv_paper_sizes[] = {
42   {N_("BBox"), 0, 0},
43   {N_("Letter"), 612, 792,},
44   {N_("Tabloid"), 792, 1224,},
45   {N_("Ledger"), 1224, 792,},
46   {N_("Legal"), 612, 1008,},
47   {N_("Statement"), 396, 612,},
48   {N_("Executive"), 540, 720,},
49   {N_("A0"), 2380, 3368,},
50   {N_("A1"), 1684, 2380,},
51   {N_("A2"), 1190, 1684,},
52   {N_("A3"), 842, 1190,},
53   {N_("A4"), 595, 842,},
54   {N_("A5"), 420, 595,},
55   {N_("B4"), 729, 1032,},
56   {N_("B5"), 516, 729,},
57   {N_("Folio"), 612, 936,},
58   {N_("Quarto"), 610, 780,},
59   {N_("10x14"), 720, 1008,},
60   {NULL, 0, 0}
61 };
62
63 const gfloat ggv_unit_factors[] = {
64   1.0,
65   25.4,
66   2.54,
67   72.0
68 };
69
70 const gchar *ggv_orientation_labels[] = {
71   N_("Portrait"),
72   N_("Landscape"),
73   N_("Upside Down"),
74   N_("Seascape"),
75   NULL,
76 };
77
78 const gint ggv_max_orientation_labels =
79   (sizeof(ggv_orientation_labels) / sizeof(gchar *)) - 2;
80
81 const gchar *ggv_unit_labels[] = {
82   N_("inch"),
83   N_("mm"),
84   N_("cm"),
85   N_("point"),
86   NULL
87 };
88
89 const gint ggv_max_unit_labels =
90   (sizeof(ggv_unit_labels) / sizeof(gchar *)) - 2;
91
92 gfloat ggv_zoom_levels[] = {
93   1.0 / 6.0, 1.0 / 5.0, 1.0 / 4.0, 1.0 / 3.0, 1.0 / 2.0, 3.0 / 4.0, 1.0,
94   3.0 / 2.0, 2.0, 3.0, 4.0, 5.0, 6.0
95 };
96
97 const gchar *ggv_zoom_level_names[] = {
98   "1:6", "1:5", "1:4", "1:3",
99   "1:2", "3:4", "1:1", "3:2",
100   "2:1", "3:1", "4:1", "5:1",
101   "6:1",
102 };
103
104 const gint ggv_max_zoom_levels = (sizeof(ggv_zoom_levels) / sizeof(gfloat)) - 1;
105
106 const gchar *ggv_auto_fit_modes[] = {
107   N_("None"), N_("Fit to page width"), N_("Fit to page size")
108 };
109
110 const gint ggv_max_auto_fit_modes =
111   (sizeof(ggv_auto_fit_modes) / sizeof(gchar *)) - 1;
112
113 gint
114 ggv_zoom_index_from_float(gfloat zoom_level)
115 {
116   int i;
117
118   for(i = 0; i <= ggv_max_zoom_levels; i++) {
119     float this, epsilon;
120
121     /* if we're close to a zoom level */
122     this = ggv_zoom_levels[i];
123     epsilon = this * 0.01;
124
125     if(zoom_level < this + epsilon)
126       return i;
127   }
128
129   return ggv_max_zoom_levels;
130 }
131
132 gfloat
133 ggv_zoom_level_from_index(gint index)
134 {
135   if(index > ggv_max_zoom_levels)
136     index = ggv_max_zoom_levels;
137
138   return ggv_zoom_levels[index];
139 }
140
141 GSList *
142 ggv_split_string(const gchar * string, const gchar * delimiter)
143 {
144   const gchar *ptr = string;
145   int pos = 0, escape = 0;
146   char buffer[BUFSIZ];
147   GSList *list = NULL;
148
149   g_return_val_if_fail(string != NULL, NULL);
150   g_return_val_if_fail(delimiter != NULL, NULL);
151
152   while(*ptr) {
153     char c = *ptr++;
154     const gchar *d;
155     int found = 0;
156
157     if(pos >= BUFSIZ) {
158       g_warning("string too long, aborting");
159       return list;
160     }
161
162     if(escape) {
163       buffer[pos++] = c;
164       escape = 0;
165       continue;
166     }
167
168     if(c == '\\') {
169       escape = 1;
170       continue;
171     }
172
173     for(d = delimiter; *d; d++) {
174       if(c == *d) {
175         buffer[pos++] = 0;
176         list = g_slist_prepend(list, g_strdup(buffer));
177         pos = 0;
178         found = 1;
179         break;
180       }
181     }
182
183     if(!found)
184       buffer[pos++] = c;
185   }
186
187   buffer[pos++] = 0;
188   list = g_slist_prepend(list, g_strdup(buffer));
189
190   return list;
191 }
192
193 gint
194 ggv_get_index_of_string(gchar * string, gchar ** strings)
195 {
196   guint idx = 0;
197
198   while(strings[idx] != NULL) {
199     if(strcmp(strings[idx], string) == 0)
200       return idx;
201     idx++;
202   }
203
204   return -1;
205 }
206
207 /* Quote filename for system call */
208 gchar *
209 ggv_quote_filename(const gchar * str)
210 {
211   return g_shell_quote(str);
212 }
213
214 /* escapes filename to form a proper URI: works conservatively - anything
215    except [a-zA-Z0-9_] will be escaped with a %XX escape sequence where
216    XX is the hex value of the char. */
217 gchar *
218 ggv_filename_to_uri(const gchar * fname)
219 {
220   gchar *full_path, *ret_val;
221
222   if(*fname != '/') {
223     gchar *cwd;
224     /* relative file name - we will have to absolutize it */
225     cwd = g_get_current_dir();
226     full_path = g_strconcat(cwd, "/", fname, NULL);
227   }
228   else
229     full_path = NULL;
230   ret_val = gnome_vfs_get_uri_from_local_path(full_path ? full_path : fname);
231   if(full_path)
232     g_free(full_path);
233   return ret_val;
234 }
235
236 /* If file exists and is a regular file then return its length, else -1 */
237 gint
238 ggv_file_length(const gchar * filename)
239 {
240   struct stat stat_rec;
241
242   if(filename && (stat(filename, &stat_rec) == 0)
243      && S_ISREG(stat_rec.st_mode))
244     return stat_rec.st_size;
245   else
246     return -1;
247 }
248
249 /* Test if file exists, is a regular file and its length is > 0 */
250 gboolean
251 ggv_file_readable(const char *filename)
252 {
253   return (ggv_file_length(filename) > 0);
254 }
255
256 /* Set a tooltip for a widget */
257 void
258 ggv_set_tooltip(GtkWidget * w, const gchar * tip)
259 {
260   GtkTooltips *t = gtk_tooltips_new();
261
262   gtk_tooltips_set_tip(t, w, tip, NULL);
263 }
264
265 gfloat
266 ggv_compute_zoom(gint zoom_spec)
267 {
268   return pow(1.2, zoom_spec);   /* The Knuth magstep formula rules */
269 }
270
271 gint
272 ggv_compute_spec(gfloat zoom)
273 {
274   zoom = MAX(0.02, zoom);
275   zoom = MIN(10.0, zoom);
276
277   zoom = log(zoom) / log(1.2);
278   return (gint) rint(zoom);
279 }
280
281 void
282 ggv_raise_and_focus_widget(GtkWidget * widget)
283 {
284   g_assert(GTK_WIDGET_REALIZED(widget));
285   gdk_window_raise(widget->window);
286   gtk_widget_grab_focus(widget);
287 }
288
289 void
290 ggv_get_window_size(GtkWidget * widget, gint * width, gint * height)
291 {
292   *width = widget->allocation.width;
293   *height = widget->allocation.height;
294 }
295
296 static GgvStockIcon items[] = {
297   {GGV_CLEAR_ALL, GNOMEICONDIR "/ggv/clearall.xpm"},
298   {GGV_TOGGLE_ALL, GNOMEICONDIR "/ggv/toggleall.xpm"},
299   {GGV_TOGGLE_EVEN, GNOMEICONDIR "/ggv/toggleeven.xpm"},
300   {GGV_TOGGLE_ODD, GNOMEICONDIR "/ggv/toggleodd.xpm"},
301   {GGV_FIT_WIDTH, GNOMEICONDIR "/ggv/fitwidth.png"},
302   {GGV_ZOOM, GNOMEICONDIR "/ggv/zoom.xpm"}
303 };
304
305 static void
306 ggv_register_stock_icons(GtkIconFactory * factory)
307 {
308   gint i;
309   GtkIconSource *source;
310
311   source = gtk_icon_source_new();
312
313   for(i = 0; i < G_N_ELEMENTS(items); ++i) {
314     GtkIconSet *icon_set;
315
316     if(!g_file_test(items[i].name, G_FILE_TEST_EXISTS)) {
317       g_warning(_("Unable to load ggv stock icon '%s'\n"), items[i].name);
318       icon_set = gtk_icon_factory_lookup_default(GTK_STOCK_MISSING_IMAGE);
319       gtk_icon_factory_add(factory, items[i].stock_id, icon_set);
320       continue;
321     }
322
323     gtk_icon_source_set_filename(source, items[i].name);
324
325     icon_set = gtk_icon_set_new();
326     gtk_icon_set_add_source(icon_set, source);
327     gtk_icon_factory_add(factory, items[i].stock_id, icon_set);
328     gtk_icon_set_unref(icon_set);
329   }
330   gtk_icon_source_free(source);
331 }
332
333 void
334 ggv_init_stock_icons(void)
335 {
336   GtkIconFactory *factory;
337   static gboolean initialized = FALSE;
338
339   if(initialized)
340     return;
341   initialized = TRUE;
342
343   factory = gtk_icon_factory_new();
344   gtk_icon_factory_add_default(factory);
345   ggv_register_stock_icons(factory);
346
347   g_object_unref(factory);
348 }