Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ps/ggvutils.c
blob: c79ec041183e78f6caa230e701069d523345ccd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 * ggv-utils.c: misc utility functions
 *
 * Copyright 2002 - 2005 The Free Software Foundation
 *
 * Author: Jaka Mocnik  <jaka@gnu.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "config.h"
#include <math.h>
#include <ctype.h>
#include <sys/stat.h>

#include <gnome.h>
#include <gtk/gtkstock.h>
#include <gtk/gtkiconfactory.h>

#include <libgnomevfs/gnome-vfs-utils.h>

#include "ggvutils.h"

typedef struct {
  char *stock_id;
  char *name;
} GgvStockIcon;

GtkGSPaperSize ggv_paper_sizes[] = {
  {N_("BBox"), 0, 0},
  {N_("Letter"), 612, 792,},
  {N_("Tabloid"), 792, 1224,},
  {N_("Ledger"), 1224, 792,},
  {N_("Legal"), 612, 1008,},
  {N_("Statement"), 396, 612,},
  {N_("Executive"), 540, 720,},
  {N_("A0"), 2380, 3368,},
  {N_("A1"), 1684, 2380,},
  {N_("A2"), 1190, 1684,},
  {N_("A3"), 842, 1190,},
  {N_("A4"), 595, 842,},
  {N_("A5"), 420, 595,},
  {N_("B4"), 729, 1032,},
  {N_("B5"), 516, 729,},
  {N_("Folio"), 612, 936,},
  {N_("Quarto"), 610, 780,},
  {N_("10x14"), 720, 1008,},
  {NULL, 0, 0}
};

const gfloat ggv_unit_factors[] = {
  1.0,
  25.4,
  2.54,
  72.0
};

const gchar *ggv_orientation_labels[] = {
  N_("Portrait"),
  N_("Landscape"),
  N_("Upside Down"),
  N_("Seascape"),
  NULL,
};

const gint ggv_max_orientation_labels =
  (sizeof(ggv_orientation_labels) / sizeof(gchar *)) - 2;

const gchar *ggv_unit_labels[] = {
  N_("inch"),
  N_("mm"),
  N_("cm"),
  N_("point"),
  NULL
};

const gint ggv_max_unit_labels =
  (sizeof(ggv_unit_labels) / sizeof(gchar *)) - 2;

gfloat ggv_zoom_levels[] = {
  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,
  3.0 / 2.0, 2.0, 3.0, 4.0, 5.0, 6.0
};

const gchar *ggv_zoom_level_names[] = {
  "1:6", "1:5", "1:4", "1:3",
  "1:2", "3:4", "1:1", "3:2",
  "2:1", "3:1", "4:1", "5:1",
  "6:1",
};

const gint ggv_max_zoom_levels = (sizeof(ggv_zoom_levels) / sizeof(gfloat)) - 1;

const gchar *ggv_auto_fit_modes[] = {
  N_("None"), N_("Fit to page width"), N_("Fit to page size")
};

const gint ggv_max_auto_fit_modes =
  (sizeof(ggv_auto_fit_modes) / sizeof(gchar *)) - 1;

gint
ggv_zoom_index_from_float(gfloat zoom_level)
{
  int i;

  for(i = 0; i <= ggv_max_zoom_levels; i++) {
    float this, epsilon;

    /* if we're close to a zoom level */
    this = ggv_zoom_levels[i];
    epsilon = this * 0.01;

    if(zoom_level < this + epsilon)
      return i;
  }

  return ggv_max_zoom_levels;
}

gfloat
ggv_zoom_level_from_index(gint index)
{
  if(index > ggv_max_zoom_levels)
    index = ggv_max_zoom_levels;

  return ggv_zoom_levels[index];
}

GSList *
ggv_split_string(const gchar * string, const gchar * delimiter)
{
  const gchar *ptr = string;
  int pos = 0, escape = 0;
  char buffer[BUFSIZ];
  GSList *list = NULL;

  g_return_val_if_fail(string != NULL, NULL);
  g_return_val_if_fail(delimiter != NULL, NULL);

  while(*ptr) {
    char c = *ptr++;
    const gchar *d;
    int found = 0;

    if(pos >= BUFSIZ) {
      g_warning("string too long, aborting");
      return list;
    }

    if(escape) {
      buffer[pos++] = c;
      escape = 0;
      continue;
    }

    if(c == '\\') {
      escape = 1;
      continue;
    }

    for(d = delimiter; *d; d++) {
      if(c == *d) {
        buffer[pos++] = 0;
        list = g_slist_prepend(list, g_strdup(buffer));
        pos = 0;
        found = 1;
        break;
      }
    }

    if(!found)
      buffer[pos++] = c;
  }

  buffer[pos++] = 0;
  list = g_slist_prepend(list, g_strdup(buffer));

  return list;
}

gint
ggv_get_index_of_string(gchar * string, gchar ** strings)
{
  guint idx = 0;

  while(strings[idx] != NULL) {
    if(strcmp(strings[idx], string) == 0)
      return idx;
    idx++;
  }

  return -1;
}

/* Quote filename for system call */
gchar *
ggv_quote_filename(const gchar * str)
{
  return g_shell_quote(str);
}

/* escapes filename to form a proper URI: works conservatively - anything
   except [a-zA-Z0-9_] will be escaped with a %XX escape sequence where
   XX is the hex value of the char. */
gchar *
ggv_filename_to_uri(const gchar * fname)
{
  gchar *full_path, *ret_val;

  if(*fname != '/') {
    gchar *cwd;
    /* relative file name - we will have to absolutize it */
    cwd = g_get_current_dir();
    full_path = g_strconcat(cwd, "/", fname, NULL);
  }
  else
    full_path = NULL;
  ret_val = gnome_vfs_get_uri_from_local_path(full_path ? full_path : fname);
  if(full_path)
    g_free(full_path);
  return ret_val;
}

/* If file exists and is a regular file then return its length, else -1 */
gint
ggv_file_length(const gchar * filename)
{
  struct stat stat_rec;

  if(filename && (stat(filename, &stat_rec) == 0)
     && S_ISREG(stat_rec.st_mode))
    return stat_rec.st_size;
  else
    return -1;
}

/* Test if file exists, is a regular file and its length is > 0 */
gboolean
ggv_file_readable(const char *filename)
{
  return (ggv_file_length(filename) > 0);
}

/* Set a tooltip for a widget */
void
ggv_set_tooltip(GtkWidget * w, const gchar * tip)
{
  GtkTooltips *t = gtk_tooltips_new();

  gtk_tooltips_set_tip(t, w, tip, NULL);
}

gfloat
ggv_compute_zoom(gint zoom_spec)
{
  return pow(1.2, zoom_spec);   /* The Knuth magstep formula rules */
}

gint
ggv_compute_spec(gfloat zoom)
{
  zoom = MAX(0.02, zoom);
  zoom = MIN(10.0, zoom);

  zoom = log(zoom) / log(1.2);
  return (gint) rint(zoom);
}

void
ggv_raise_and_focus_widget(GtkWidget * widget)
{
  g_assert(GTK_WIDGET_REALIZED(widget));
  gdk_window_raise(widget->window);
  gtk_widget_grab_focus(widget);
}

void
ggv_get_window_size(GtkWidget * widget, gint * width, gint * height)
{
  *width = widget->allocation.width;
  *height = widget->allocation.height;
}

static GgvStockIcon items[] = {
  {GGV_CLEAR_ALL, GNOMEICONDIR "/ggv/clearall.xpm"},
  {GGV_TOGGLE_ALL, GNOMEICONDIR "/ggv/toggleall.xpm"},
  {GGV_TOGGLE_EVEN, GNOMEICONDIR "/ggv/toggleeven.xpm"},
  {GGV_TOGGLE_ODD, GNOMEICONDIR "/ggv/toggleodd.xpm"},
  {GGV_FIT_WIDTH, GNOMEICONDIR "/ggv/fitwidth.png"},
  {GGV_ZOOM, GNOMEICONDIR "/ggv/zoom.xpm"}
};

static void
ggv_register_stock_icons(GtkIconFactory * factory)
{
  gint i;
  GtkIconSource *source;

  source = gtk_icon_source_new();

  for(i = 0; i < G_N_ELEMENTS(items); ++i) {
    GtkIconSet *icon_set;

    if(!g_file_test(items[i].name, G_FILE_TEST_EXISTS)) {
      g_warning(_("Unable to load ggv stock icon '%s'\n"), items[i].name);
      icon_set = gtk_icon_factory_lookup_default(GTK_STOCK_MISSING_IMAGE);
      gtk_icon_factory_add(factory, items[i].stock_id, icon_set);
      continue;
    }

    gtk_icon_source_set_filename(source, items[i].name);

    icon_set = gtk_icon_set_new();
    gtk_icon_set_add_source(icon_set, source);
    gtk_icon_factory_add(factory, items[i].stock_id, icon_set);
    gtk_icon_set_unref(icon_set);
  }
  gtk_icon_source_free(source);
}

void
ggv_init_stock_icons(void)
{
  GtkIconFactory *factory;
  static gboolean initialized = FALSE;

  if(initialized)
    return;
  initialized = TRUE;

  factory = gtk_icon_factory_new();
  gtk_icon_factory_add_default(factory);
  ggv_register_stock_icons(factory);

  g_object_unref(factory);
}