Contents of /trunk/src/misc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 165 - (show annotations)
Sun Nov 8 20:32:55 2009 UTC (14 years, 5 months ago) by harbaum
File MIME type: text/plain
File size: 14381 byte(s)
Lots of Fremantle'ization
1 /*
2 * Copyright (C) 2008 Till Harbaum <till@harbaum.org>.
3 *
4 * This file is part of GPXView.
5 *
6 * GPXView is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * GPXView 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. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GPXView. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <math.h>
21 #include <string.h>
22 #include <ctype.h>
23
24 #include <glib.h>
25 #include <glib/gstdio.h>
26
27 #include "gpxview.h"
28
29 char strlastchr(char *str) {
30 return str[strlen(str)]-1;
31 }
32
33 /* make sure the entire path "dir" exists and create it if not */
34 int checkdir(char *dir) {
35 struct stat filestat;
36 char *p = dir, tmp;
37
38 /* don't try to create root dir */
39 if(p[0] == '/') p++;
40
41 do {
42 while(*p && *p != '/') p++;
43 tmp = *p;
44 *p = 0;
45
46 int err = stat(dir, &filestat);
47 if(err) {
48 if(mkdir(dir, S_IRWXU) != 0) {
49 perror("mkdir()");
50 *p++ = tmp;
51 return -1;
52 }
53 } else {
54 if(!filestat.st_mode & S_IFDIR) {
55 printf("File %s exists and is _no_ directory\n", dir);
56 *p++ = tmp;
57 return -1;
58 }
59 }
60
61 *p++ = tmp;
62 } while(tmp && strchr(p, '/'));
63
64 return 0;
65 }
66
67 void pos_lat_str(char *str, int len, float latitude) {
68 char *c = _("N");
69 float integral, fractional;
70
71 if(isnan(latitude))
72 str[0] = 0;
73 else {
74 if(latitude < 0) { latitude = fabs(latitude); c = _("S"); }
75 fractional = modff(latitude, &integral);
76
77 snprintf(str, len, "%s %02d° %06.3f'", c, (int)integral, fractional*60.0);
78 }
79 }
80
81 GtkWidget *pos_lat(float latitude, int size, int strikethrough) {
82 char str[32];
83
84 pos_lat_str(str, sizeof(str), latitude);
85 return gtk_label_attrib(str, size, strikethrough);
86 }
87
88 void pos_lon_str(char *str, int len, float longitude) {
89 char *c = _("E");
90 float integral, fractional;
91
92 if(isnan(longitude))
93 str[0] = 0;
94 else {
95 if(longitude < 0) { longitude = fabs(longitude); c = _("W"); }
96 fractional = modff(longitude, &integral);
97
98 snprintf(str, len, "%s %03d° %06.3f'", c, (int)integral, fractional*60.0);
99 }
100 }
101
102 GtkWidget *pos_lon(float longitude, int size, int strikethrough) {
103 char str[32];
104
105 pos_lon_str(str, sizeof(str), longitude);
106 return gtk_label_attrib(str, size, strikethrough);
107 }
108
109 float pos_parse_lat(char *str) {
110 int integral_int;
111 float fractional;
112 char c;
113
114 if(sscanf(str, "%c %d° %f'", &c, &integral_int, &fractional) == 3) {
115 c = toupper(c);
116
117 if(c != 'S' && c != 'N')
118 return NAN;
119
120 /* prevent -0.0 */
121 if(!integral_int && (fractional == 0.0))
122 return 0.0;
123
124 return ((c == 'S')?-1:+1) * (integral_int + fractional/60.0);
125 }
126
127 return NAN;
128 }
129
130 float pos_parse_lon(char *str) {
131 int integral_int;
132 float fractional;
133 char c;
134
135 if(sscanf(str, "%c %d° %f'", &c, &integral_int, &fractional) == 3) {
136 c = toupper(c);
137
138 /* O is german "Ost" for "East" */
139 if(c != 'E' && c != 'W' && c != 'O')
140 return NAN;
141
142 /* prevent -0.0 */
143 if(!integral_int && (fractional == 0.0))
144 return 0.0;
145
146 return ((c == 'W')?-1:+1) * (integral_int + fractional/60.0);
147 }
148
149 return NAN;
150 }
151
152 const char *pos_get_bearing_str(pos_t from, pos_t to) {
153 static const char *bear_str[]={
154 "N", "NE", "E", "SE", "S", "SW", "W", "NW" };
155 int idx = (gpx_pos_get_bearing(from, to)+22.5)/45.0;
156 /* make sure we stay in icon bounds */
157 while(idx < 0) idx += 8;
158 while(idx > 7) idx -= 8;
159 return _(bear_str[idx]);
160 }
161
162 /* the maemo font size is quite huge, so we adjust some fonts */
163 /* differently on maemo and non-maemo. Basically "BIG" does nothing */
164 /* on maemo and "SMALL" only does something on maemo */
165 #ifdef USE_MAEMO
166 #define MARKUP_SMALL "<span size='small'>%s</span>"
167 GtkWidget *gtk_label_small(char *str) {
168 GtkWidget *label = gtk_label_new("");
169 char *markup = g_markup_printf_escaped(MARKUP_SMALL, str);
170 gtk_label_set_markup(GTK_LABEL(label), markup);
171 g_free(markup);
172 return label;
173 }
174 #else
175 #define MARKUP_BIG "<span size='x-large'>%s</span>"
176 GtkWidget *gtk_label_big(char *str) {
177 GtkWidget *label = gtk_label_new("");
178 char *markup = g_markup_printf_escaped(MARKUP_BIG, str);
179 gtk_label_set_markup(GTK_LABEL(label), markup);
180 g_free(markup);
181 return label;
182 }
183 #endif
184
185 void gtk_label_attrib_set(GtkWidget *label,
186 char *str, int size, int strikethrough) {
187 char format[80];
188
189 snprintf(format, sizeof(format), "<span%s%s%s>%%s</span>",
190 #ifdef USE_MAEMO
191 (size==SIZE_SMALL)?" size='small'":"",
192 #else
193 (size==SIZE_BIG)?" size='x-large'":"",
194 #endif
195 strikethrough?" strikethrough='yes'":"",
196 (strikethrough==STRIKETHROUGH_RED)?" strikethrough_color='red'":"");
197
198 char *markup = g_markup_printf_escaped(format, str);
199 // printf("markup = %s\n", markup);
200 gtk_label_set_markup(GTK_LABEL(label), markup);
201 g_free(markup);
202 }
203
204 GtkWidget *gtk_label_attrib(char *str, int size, int strikethrough) {
205 GtkWidget *label = gtk_label_new("");
206 gtk_label_attrib_set(label, str, size, strikethrough);
207 return label;
208 }
209
210 GtkWidget *gtk_button_attrib(char *str, int size, int strikethrough) {
211 GtkWidget *button = gtk_button_new_with_label("");
212 gtk_label_attrib_set(gtk_bin_get_child(GTK_BIN(button)),
213 str, size, strikethrough);
214 return button;
215 }
216
217 void textbox_disable(GtkWidget *widget) {
218 gtk_editable_set_editable(GTK_EDITABLE(widget), FALSE);
219 gtk_widget_set_sensitive(widget, FALSE);
220 }
221
222 void textbox_enable(GtkWidget *widget) {
223 gtk_widget_set_sensitive(widget, TRUE);
224 gtk_editable_set_editable(GTK_EDITABLE(widget), TRUE);
225 }
226
227 pos_t *get_pos(appdata_t *appdata) {
228 pos_t *pos = &appdata->home;
229
230 if(appdata->active_location) {
231 int i = appdata->active_location-1;
232 location_t *loc = appdata->location;
233 while(i--) loc = loc->next;
234 pos = &loc->pos;
235 }
236
237 if(appdata->use_gps) {
238 pos = gps_get_pos(appdata);
239
240 if(!pos) pos = &appdata->gps; /* use saved position */
241 else appdata->gps = *pos; /* save position */
242 }
243 return pos;
244 }
245
246 void distance_str(char *str, int len, float dist, gboolean imperial) {
247 if(isnan(dist))
248 snprintf(str, len, "---");
249 else if(imperial) {
250 /* 1 mil = 1760 yd = 5280 ft ... */
251 if(dist<0.018) snprintf(str, len, "%.1f ft", dist*5280.0);
252 else if(dist<0.055) snprintf(str, len, "%.1f yd", dist*1760.0);
253 else if(dist<0.55) snprintf(str, len, "%.0f yd", dist*1760.0);
254 else if(dist<10.0) snprintf(str, len, "%.2f mi", dist);
255 else if(dist<100.0) snprintf(str, len, "%.1f mi", dist);
256 else snprintf(str, len, "%.0f mi", dist);
257 } else {
258 if(dist<0.01) snprintf(str, len, "%.2f m", dist*1000.0);
259 else if(dist<0.1) snprintf(str, len, "%.1f m", dist*1000.0);
260 else if(dist<1.0) snprintf(str, len, "%.0f m", dist*1000.0);
261 else if(dist<100.0) snprintf(str, len, "%.1f km", dist);
262 else snprintf(str, len, "%.0f km", dist);
263 }
264 }
265
266 /* return distance in miles or kilometers */
267 float distance_parse(char *str, gboolean imperial) {
268 char unit[4];
269 float val = NAN;
270
271 if(sscanf(str, "%f %3s", &val, unit) == 2) {
272 gboolean fimp = FALSE;
273
274 if(strcasecmp(unit, "ft") == 0) { fimp = TRUE; val /= 5280.0; }
275 else if(strcasecmp(unit, "yd") == 0) { fimp = TRUE; val /= 1760.0; }
276 else if(strcasecmp(unit, "mi") == 0) { fimp = TRUE; }
277 else if(strcasecmp(unit, "m") == 0) { fimp = FALSE; val /= 1000.0; }
278 else if(strcasecmp(unit, "km") == 0) { fimp = FALSE; }
279 else val = NAN;
280
281 /* found imperial and metric requested? convert miles into kilometers */
282 if(fimp & !imperial) val *= 1.609344;
283
284 /* found metric and imperial requested? convert kilometers into miles */
285 if(!fimp & imperial) val /= 1.609344;
286 }
287 return val;
288 }
289
290 static gboolean mark(GtkWidget *widget, gboolean valid) {
291 gtk_widget_set_state(widget, valid?GTK_STATE_NORMAL:TAG_STATE);
292 return valid;
293 }
294
295 static void callback_modified_lat(GtkWidget *widget, gpointer data ) {
296 float i = pos_parse_lat((char*)gtk_entry_get_text(GTK_ENTRY(widget)));
297 mark(widget, !isnan(i));
298 }
299
300 /* a entry that is colored red when being "active" */
301 GtkWidget *lat_entry_new(float lat) {
302 GdkColor color;
303 GtkWidget *widget = gtk_entry_new();
304 gdk_color_parse("#ff0000", &color);
305 gtk_widget_modify_text(widget, TAG_STATE, &color);
306
307 char str[32];
308 pos_lat_str(str, sizeof(str), lat);
309 gtk_entry_set_text(GTK_ENTRY(widget), str);
310
311 g_signal_connect(G_OBJECT(widget), "changed",
312 G_CALLBACK(callback_modified_lat), NULL);
313
314 return widget;
315 }
316
317 static void callback_modified_lon(GtkWidget *widget, gpointer data ) {
318 float i = pos_parse_lon((char*)gtk_entry_get_text(GTK_ENTRY(widget)));
319 mark(widget, !isnan(i));
320 }
321
322 /* a entry that is colored red when filled with invalid coordinate */
323 GtkWidget *lon_entry_new(float lon) {
324 GdkColor color;
325 GtkWidget *widget = gtk_entry_new();
326 gdk_color_parse("#ff0000", &color);
327 gtk_widget_modify_text(widget, TAG_STATE, &color);
328
329 char str[32];
330 pos_lon_str(str, sizeof(str), lon);
331 gtk_entry_set_text(GTK_ENTRY(widget), str);
332
333 g_signal_connect(G_OBJECT(widget), "changed",
334 G_CALLBACK(callback_modified_lon), NULL);
335
336 return widget;
337 }
338
339
340 float lat_get(GtkWidget *widget) {
341 char *p = (char*)gtk_entry_get_text(GTK_ENTRY(widget));
342 return pos_parse_lat(p);
343 }
344
345 float lon_get(GtkWidget *widget) {
346 char *p = (char*)gtk_entry_get_text(GTK_ENTRY(widget));
347 return pos_parse_lon(p);
348 }
349
350 static void callback_modified_dist(GtkWidget *widget, gpointer data ) {
351 /* don't care for metric/imperial since we only want to know if this */
352 /* is parseable at all */
353 float i = distance_parse((char*)gtk_entry_get_text(GTK_ENTRY(widget)), FALSE);
354 mark(widget, !isnan(i));
355 }
356
357 /* a entry that is colored red when filled with invalid distance */
358 GtkWidget *dist_entry_new(float dist, gboolean mil) {
359 GdkColor color;
360 GtkWidget *widget = gtk_entry_new();
361 gdk_color_parse("#ff0000", &color);
362 gtk_widget_modify_text(widget, TAG_STATE, &color);
363
364 char str[32];
365 distance_str(str, sizeof(str), dist, mil);
366 gtk_entry_set_text(GTK_ENTRY(widget), str);
367
368 g_signal_connect(G_OBJECT(widget), "changed",
369 G_CALLBACK(callback_modified_dist), NULL);
370
371 return widget;
372 }
373
374 float dist_get(GtkWidget *widget, gboolean mil) {
375 char *p = (char*)gtk_entry_get_text(GTK_ENTRY(widget));
376 return distance_parse(p, mil);
377 }
378
379 #ifndef USE_MAEMO
380 #ifdef ENABLE_BROWSER_INTERFACE
381 #include <libgnome/gnome-url.h>
382
383 int browser_url(appdata_t *appdata, char *url) {
384 /* taken from gnome-open, part of libgnome */
385 GError *err = NULL;
386 gnome_url_show(url, &err);
387 return 0;
388 }
389 #endif
390 #endif
391
392 /* recursively remove an entire file system */
393 void rmdir_recursive(char *path) {
394 GDir *dir = g_dir_open(path, 0, NULL);
395 if(dir) {
396 const char *name = g_dir_read_name(dir);
397 while(name) {
398 char *fullname = g_strdup_printf("%s/%s", path, name);
399 // printf("deleting %s\n", fullname);
400
401 if(g_file_test(fullname, G_FILE_TEST_IS_DIR))
402 rmdir_recursive(fullname);
403 else if(g_file_test(fullname, G_FILE_TEST_IS_REGULAR))
404 g_remove(fullname);
405
406 g_free(fullname);
407 name = g_dir_read_name(dir);
408 }
409
410 g_dir_close(dir);
411 }
412 g_rmdir(path);
413 }
414
415 #ifdef ENABLE_BROWSER_INTERFACE
416 static void on_link_clicked(GtkButton *button, gpointer data) {
417 appdata_t *appdata = (appdata_t*)data;
418 char *url = g_object_get_data(G_OBJECT(button), "url");
419 if(url) browser_url(appdata, url);
420 }
421 #endif
422
423 /* a button containing a weblink */
424 GtkWidget *link_button_attrib(appdata_t *appdata, char *str, char *url,
425 int size, int strikethrough) {
426
427 #ifdef ENABLE_BROWSER_INTERFACE
428 if(url) {
429 GtkWidget *button = gtk_button_attrib(str, size, strikethrough);
430 g_object_set_data(G_OBJECT(button), "url", url);
431 gtk_signal_connect(GTK_OBJECT(button), "clicked",
432 (GtkSignalFunc)on_link_clicked, appdata);
433
434 return button;
435 }
436 #endif
437 return gtk_label_attrib(str, size, strikethrough);
438 }
439
440 #ifdef ENABLE_BROWSER_INTERFACE
441 static void on_link_id_clicked(GtkButton *button, gpointer data) {
442 appdata_t *appdata = (appdata_t*)data;
443
444 unsigned int id = (unsigned int)g_object_get_data(G_OBJECT(button), "id");
445 char *type = g_object_get_data(G_OBJECT(button), "type");
446
447 char *url = g_strdup_printf("http://www.geocaching.com/%s?id=%u",
448 type, id);
449
450 if(url) {
451 browser_url(appdata, url);
452 g_free(url);
453 }
454 }
455 #endif
456
457 GtkWidget *link_button_by_id(appdata_t *appdata, char *str,
458 const char *type, int id) {
459
460 #ifdef ENABLE_BROWSER_INTERFACE
461 if(id) {
462 GtkWidget *ref = gtk_button_new_with_label(str);
463 #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR == 5)
464 // hildon_gtk_widget_set_theme_size(ref,
465 // (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH));
466 #endif
467 g_object_set_data(G_OBJECT(ref), "id", (gpointer)id);
468 g_object_set_data(G_OBJECT(ref), "type", (gpointer)type);
469 gtk_signal_connect(GTK_OBJECT(ref), "clicked",
470 GTK_SIGNAL_FUNC(on_link_id_clicked), appdata);
471
472 return ref;
473 }
474 #endif
475 return gtk_label_new(str);
476 }
477
478
479 GtkWidget *link_icon_button_by_id(appdata_t *appdata, GtkWidget *icon,
480 const char *type, int id) {
481
482 #ifdef ENABLE_BROWSER_INTERFACE
483 if(id) {
484 GtkWidget *ref = gtk_button_new();
485 gtk_button_set_image(GTK_BUTTON(ref), icon);
486
487 #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR == 5)
488 // hildon_gtk_widget_set_theme_size(ref,
489 // (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH));
490 #endif
491 g_object_set_data(G_OBJECT(ref), "id", (gpointer)id);
492 g_object_set_data(G_OBJECT(ref), "type", (gpointer)type);
493 gtk_signal_connect(GTK_OBJECT(ref), "clicked",
494 GTK_SIGNAL_FUNC(on_link_id_clicked), appdata);
495
496 return ref;
497 }
498 #endif
499 return icon;
500 }
501
502 /* left aligned, word wrapped multiline widget */
503 GtkWidget *simple_text_widget(char *text) {
504 GtkWidget *label = gtk_label_new(text);
505
506 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
507 gtk_label_set_line_wrap_mode(GTK_LABEL(label), PANGO_WRAP_WORD);
508 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
509
510 return label;
511 }