Contents of /trunk/src/misc.c

Parent Directory Parent Directory | Revision Log Revision Log


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