Contents of /trunk/src/misc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 190 - (show annotations)
Tue Nov 17 10:22:41 2009 UTC (14 years, 6 months ago) by harbaum
File MIME type: text/plain
File size: 14893 byte(s)
More fremantleization
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 #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR >= 5)
30 #include <hildon/hildon-entry.h>
31 #endif
32
33 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 if(isnan(latitude))
76 str[0] = 0;
77 else {
78 if(latitude < 0) { latitude = fabs(latitude); c = _("S"); }
79 fractional = modff(latitude, &integral);
80
81 snprintf(str, len, "%s %02d° %06.3f'", c, (int)integral, fractional*60.0);
82 }
83 }
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 if(isnan(longitude))
97 str[0] = 0;
98 else {
99 if(longitude < 0) { longitude = fabs(longitude); c = _("W"); }
100 fractional = modff(longitude, &integral);
101
102 snprintf(str, len, "%s %03d° %06.3f'", c, (int)integral, fractional*60.0);
103 }
104 }
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 "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 }
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 if(isnan(dist))
258 snprintf(str, len, "---");
259 else if(imperial) {
260 /* 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
314 #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5)
315 GtkWidget *widget = gtk_entry_new();
316 #else
317 GtkWidget *widget = hildon_entry_new(HILDON_SIZE_AUTO);
318 #endif
319
320 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
342 #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5)
343 GtkWidget *widget = gtk_entry_new();
344 #else
345 GtkWidget *widget = hildon_entry_new(HILDON_SIZE_AUTO);
346 // gtk_entry_set_width_chars(GTK_ENTRY(widget), 14);
347 #endif
348
349 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 GtkWidget *widget = gtk_entry_new();
384 gdk_color_parse("#ff0000", &color);
385 gtk_widget_modify_text(widget, TAG_STATE, &color);
386
387 char str[32];
388 distance_str(str, sizeof(str), dist, mil);
389 gtk_entry_set_text(GTK_ENTRY(widget), str);
390
391 g_signal_connect(G_OBJECT(widget), "changed",
392 G_CALLBACK(callback_modified_dist), NULL);
393
394 return widget;
395 }
396
397 float dist_get(GtkWidget *widget, gboolean mil) {
398 char *p = (char*)gtk_entry_get_text(GTK_ENTRY(widget));
399 return distance_parse(p, mil);
400 }
401
402 #ifndef USE_MAEMO
403 #ifdef ENABLE_BROWSER_INTERFACE
404 #include <libgnome/gnome-url.h>
405
406 int browser_url(appdata_t *appdata, char *url) {
407 /* taken from gnome-open, part of libgnome */
408 GError *err = NULL;
409 gnome_url_show(url, &err);
410 return 0;
411 }
412 #endif
413 #endif
414
415 /* recursively remove an entire file system */
416 void rmdir_recursive(char *path) {
417 GDir *dir = g_dir_open(path, 0, NULL);
418 if(dir) {
419 const char *name = g_dir_read_name(dir);
420 while(name) {
421 char *fullname = g_strdup_printf("%s/%s", path, name);
422 // printf("deleting %s\n", fullname);
423
424 if(g_file_test(fullname, G_FILE_TEST_IS_DIR))
425 rmdir_recursive(fullname);
426 else if(g_file_test(fullname, G_FILE_TEST_IS_REGULAR))
427 g_remove(fullname);
428
429 g_free(fullname);
430 name = g_dir_read_name(dir);
431 }
432
433 g_dir_close(dir);
434 }
435 g_rmdir(path);
436 }
437
438 #ifdef ENABLE_BROWSER_INTERFACE
439 static void on_link_clicked(GtkButton *button, gpointer data) {
440 appdata_t *appdata = (appdata_t*)data;
441 char *url = g_object_get_data(G_OBJECT(button), "url");
442 if(url) browser_url(appdata, url);
443 }
444 #endif
445
446 /* a button containing a weblink */
447 GtkWidget *link_button_attrib(appdata_t *appdata, char *str, char *url,
448 int size, int strikethrough) {
449
450 #ifdef ENABLE_BROWSER_INTERFACE
451 if(url) {
452 GtkWidget *button = gtk_button_attrib(str, size, strikethrough);
453 g_object_set_data(G_OBJECT(button), "url", url);
454 gtk_signal_connect(GTK_OBJECT(button), "clicked",
455 (GtkSignalFunc)on_link_clicked, appdata);
456
457 return button;
458 }
459 #endif
460 return gtk_label_attrib(str, size, strikethrough);
461 }
462
463 #ifdef ENABLE_BROWSER_INTERFACE
464 static void on_link_id_clicked(GtkButton *button, gpointer data) {
465 appdata_t *appdata = (appdata_t*)data;
466
467 unsigned int id = (unsigned int)g_object_get_data(G_OBJECT(button), "id");
468 char *type = g_object_get_data(G_OBJECT(button), "type");
469
470 char *url = g_strdup_printf("http://www.geocaching.com/%s?id=%u",
471 type, id);
472
473 if(url) {
474 browser_url(appdata, url);
475 g_free(url);
476 }
477 }
478 #endif
479
480 GtkWidget *link_button_by_id(appdata_t *appdata, char *str,
481 const char *type, int id) {
482
483 #ifdef ENABLE_BROWSER_INTERFACE
484 if(id) {
485 GtkWidget *ref = gtk_button_new_with_label(str);
486 #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR == 5)
487 // hildon_gtk_widget_set_theme_size(ref,
488 // (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH));
489 #endif
490 g_object_set_data(G_OBJECT(ref), "id", (gpointer)id);
491 g_object_set_data(G_OBJECT(ref), "type", (gpointer)type);
492 gtk_signal_connect(GTK_OBJECT(ref), "clicked",
493 GTK_SIGNAL_FUNC(on_link_id_clicked), appdata);
494
495 return ref;
496 }
497 #endif
498 return gtk_label_new(str);
499 }
500
501
502 GtkWidget *link_icon_button_by_id(appdata_t *appdata, GtkWidget *icon,
503 const char *type, int id) {
504
505 #ifdef ENABLE_BROWSER_INTERFACE
506 if(id) {
507 GtkWidget *ref = gtk_button_new();
508 gtk_button_set_image(GTK_BUTTON(ref), icon);
509
510 #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR == 5)
511 // hildon_gtk_widget_set_theme_size(ref,
512 // (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH));
513 #endif
514 g_object_set_data(G_OBJECT(ref), "id", (gpointer)id);
515 g_object_set_data(G_OBJECT(ref), "type", (gpointer)type);
516 gtk_signal_connect(GTK_OBJECT(ref), "clicked",
517 GTK_SIGNAL_FUNC(on_link_id_clicked), appdata);
518
519 return ref;
520 }
521 #endif
522 return icon;
523 }
524
525 /* left aligned, word wrapped multiline widget */
526 GtkWidget *simple_text_widget(char *text) {
527 GtkWidget *label = gtk_label_new(text);
528
529 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
530 gtk_label_set_line_wrap_mode(GTK_LABEL(label), PANGO_WRAP_WORD);
531 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
532
533 return label;
534 }