2 * ggv-utils.c: misc utility functions
4 * Copyright 2002 - 2005 The Free Software Foundation
6 * Author: Jaka Mocnik <jaka@gnu.org>
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.
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.
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.
29 #include <gtk/gtkstock.h>
30 #include <gtk/gtkiconfactory.h>
32 #include <libgnomevfs/gnome-vfs-utils.h>
41 GtkGSPaperSize ggv_paper_sizes[] = {
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,},
63 const gfloat ggv_unit_factors[] = {
70 const gchar *ggv_orientation_labels[] = {
78 const gint ggv_max_orientation_labels =
79 (sizeof(ggv_orientation_labels) / sizeof(gchar *)) - 2;
81 const gchar *ggv_unit_labels[] = {
89 const gint ggv_max_unit_labels =
90 (sizeof(ggv_unit_labels) / sizeof(gchar *)) - 2;
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
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",
104 const gint ggv_max_zoom_levels = (sizeof(ggv_zoom_levels) / sizeof(gfloat)) - 1;
106 const gchar *ggv_auto_fit_modes[] = {
107 N_("None"), N_("Fit to page width"), N_("Fit to page size")
110 const gint ggv_max_auto_fit_modes =
111 (sizeof(ggv_auto_fit_modes) / sizeof(gchar *)) - 1;
114 ggv_zoom_index_from_float(gfloat zoom_level)
118 for(i = 0; i <= ggv_max_zoom_levels; i++) {
121 /* if we're close to a zoom level */
122 this = ggv_zoom_levels[i];
123 epsilon = this * 0.01;
125 if(zoom_level < this + epsilon)
129 return ggv_max_zoom_levels;
133 ggv_zoom_level_from_index(gint index)
135 if(index > ggv_max_zoom_levels)
136 index = ggv_max_zoom_levels;
138 return ggv_zoom_levels[index];
142 ggv_split_string(const gchar * string, const gchar * delimiter)
144 const gchar *ptr = string;
145 int pos = 0, escape = 0;
149 g_return_val_if_fail(string != NULL, NULL);
150 g_return_val_if_fail(delimiter != NULL, NULL);
158 g_warning("string too long, aborting");
173 for(d = delimiter; *d; d++) {
176 list = g_slist_prepend(list, g_strdup(buffer));
188 list = g_slist_prepend(list, g_strdup(buffer));
194 ggv_get_index_of_string(gchar * string, gchar ** strings)
198 while(strings[idx] != NULL) {
199 if(strcmp(strings[idx], string) == 0)
207 /* Quote filename for system call */
209 ggv_quote_filename(const gchar * str)
211 return g_shell_quote(str);
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. */
218 ggv_filename_to_uri(const gchar * fname)
220 gchar *full_path, *ret_val;
224 /* relative file name - we will have to absolutize it */
225 cwd = g_get_current_dir();
226 full_path = g_strconcat(cwd, "/", fname, NULL);
230 ret_val = gnome_vfs_get_uri_from_local_path(full_path ? full_path : fname);
236 /* If file exists and is a regular file then return its length, else -1 */
238 ggv_file_length(const gchar * filename)
240 struct stat stat_rec;
242 if(filename && (stat(filename, &stat_rec) == 0)
243 && S_ISREG(stat_rec.st_mode))
244 return stat_rec.st_size;
249 /* Test if file exists, is a regular file and its length is > 0 */
251 ggv_file_readable(const char *filename)
253 return (ggv_file_length(filename) > 0);
256 /* Set a tooltip for a widget */
258 ggv_set_tooltip(GtkWidget * w, const gchar * tip)
260 GtkTooltips *t = gtk_tooltips_new();
262 gtk_tooltips_set_tip(t, w, tip, NULL);
266 ggv_compute_zoom(gint zoom_spec)
268 return pow(1.2, zoom_spec); /* The Knuth magstep formula rules */
272 ggv_compute_spec(gfloat zoom)
274 zoom = MAX(0.02, zoom);
275 zoom = MIN(10.0, zoom);
277 zoom = log(zoom) / log(1.2);
278 return (gint) rint(zoom);
282 ggv_raise_and_focus_widget(GtkWidget * widget)
284 g_assert(GTK_WIDGET_REALIZED(widget));
285 gdk_window_raise(widget->window);
286 gtk_widget_grab_focus(widget);
290 ggv_get_window_size(GtkWidget * widget, gint * width, gint * height)
292 *width = widget->allocation.width;
293 *height = widget->allocation.height;
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"}
306 ggv_register_stock_icons(GtkIconFactory * factory)
309 GtkIconSource *source;
311 source = gtk_icon_source_new();
313 for(i = 0; i < G_N_ELEMENTS(items); ++i) {
314 GtkIconSet *icon_set;
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);
323 gtk_icon_source_set_filename(source, items[i].name);
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);
330 gtk_icon_source_free(source);
334 ggv_init_stock_icons(void)
336 GtkIconFactory *factory;
337 static gboolean initialized = FALSE;
343 factory = gtk_icon_factory_new();
344 gtk_icon_factory_add_default(factory);
345 ggv_register_stock_icons(factory);
347 g_object_unref(factory);