Contents of /trunk/src/misc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (hide annotations)
Sat Jun 20 11:08:47 2009 UTC (14 years, 10 months ago) by harbaum
File MIME type: text/plain
File size: 10683 byte(s)
Initial import
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     #include "gpxview.h"
25    
26     char strlastchr(char *str) {
27     return str[strlen(str)]-1;
28     }
29    
30     /* make sure the entire path "dir" exists and create it if not */
31     int checkdir(char *dir) {
32     struct stat filestat;
33     char *p = dir, tmp;
34    
35     /* don't try to create root dir */
36     if(p[0] == '/') p++;
37    
38     do {
39     while(*p && *p != '/') p++;
40     tmp = *p;
41     *p = 0;
42    
43     int err = stat(dir, &filestat);
44     if(err) {
45     if(mkdir(dir, S_IRWXU) != 0) {
46     perror("mkdir()");
47     *p++ = tmp;
48     return -1;
49     }
50     } else {
51     if(!filestat.st_mode & S_IFDIR) {
52     printf("File %s exists and is _no_ directory\n", dir);
53     *p++ = tmp;
54     return -1;
55     }
56     }
57    
58     *p++ = tmp;
59     } while(tmp && strchr(p, '/'));
60    
61     return 0;
62     }
63    
64     void pos_lat_str(char *str, int len, float latitude) {
65     char *c = _("N");
66     float integral, fractional;
67    
68     if(latitude < 0) { latitude = fabs(latitude); c = _("S"); }
69     fractional = modff(latitude, &integral);
70    
71     snprintf(str, len, "%s %02d° %06.3f'", c, (int)integral, fractional*60.0);
72     }
73    
74     GtkWidget *pos_lat(float latitude, int size, int strikethrough) {
75     char str[32];
76    
77     pos_lat_str(str, sizeof(str), latitude);
78     return gtk_label_attrib(str, size, strikethrough);
79     }
80    
81     void pos_lon_str(char *str, int len, float longitude) {
82     char *c = _("E");
83     float integral, fractional;
84    
85     if(longitude < 0) { longitude = fabs(longitude); c = _("W"); }
86     fractional = modff(longitude, &integral);
87    
88     snprintf(str, len, "%s %03d° %06.3f'", c, (int)integral, fractional*60.0);
89     }
90    
91     GtkWidget *pos_lon(float longitude, int size, int strikethrough) {
92     char str[32];
93    
94     pos_lon_str(str, sizeof(str), longitude);
95     return gtk_label_attrib(str, size, strikethrough);
96     }
97    
98     float pos_parse_lat(char *str) {
99     int integral_int;
100     float fractional;
101     char c;
102    
103     if(sscanf(str, "%c %d° %f'", &c, &integral_int, &fractional) == 3) {
104     c = toupper(c);
105    
106     if(c != 'S' && c != 'N')
107     return NAN;
108    
109     /* prevent -0.0 */
110     if(!integral_int && (fractional == 0.0))
111     return 0.0;
112    
113     return ((c == 'S')?-1:+1) * (integral_int + fractional/60.0);
114     }
115    
116     return NAN;
117     }
118    
119     float pos_parse_lon(char *str) {
120     int integral_int;
121     float fractional;
122     char c;
123    
124     if(sscanf(str, "%c %d° %f'", &c, &integral_int, &fractional) == 3) {
125     c = toupper(c);
126    
127     /* O is german "Ost" for "East" */
128     if(c != 'E' && c != 'W' && c != 'O')
129     return NAN;
130    
131     /* prevent -0.0 */
132     if(!integral_int && (fractional == 0.0))
133     return 0.0;
134    
135     return ((c == 'W')?-1:+1) * (integral_int + fractional/60.0);
136     }
137    
138     return NAN;
139     }
140    
141     const char *pos_get_bearing_str(pos_t from, pos_t to) {
142     static const char *bear_str[]={
143     "N", "NE", "E", "SE", "S", "SW", "W", "NW" };
144     int idx = (gpx_pos_get_bearing(from, to)+22.5)/45.0;
145     /* make sure we stay in icon bounds */
146     while(idx < 0) idx += 8;
147     while(idx > 7) idx -= 8;
148     return _(bear_str[idx]);
149     }
150    
151     /* the maemo font size is quite huge, so we adjust some fonts */
152     /* differently on maemo and non-maemo. Basically "BIG" does nothing */
153     /* on maemo and "SMALL" only does something on maemo */
154     #ifdef USE_MAEMO
155     #define MARKUP_SMALL "<span size='small'>%s</span>"
156     GtkWidget *gtk_label_small(char *str) {
157     GtkWidget *label = gtk_label_new("");
158     char *markup = g_markup_printf_escaped(MARKUP_SMALL, str);
159     gtk_label_set_markup(GTK_LABEL(label), markup);
160     g_free(markup);
161     return label;
162     }
163     #else
164     #define MARKUP_BIG "<span size='x-large'>%s</span>"
165     GtkWidget *gtk_label_big(char *str) {
166     GtkWidget *label = gtk_label_new("");
167     char *markup = g_markup_printf_escaped(MARKUP_BIG, str);
168     gtk_label_set_markup(GTK_LABEL(label), markup);
169     g_free(markup);
170     return label;
171     }
172     #endif
173    
174     void gtk_label_attrib_set(GtkWidget *label,
175     char *str, int size, int strikethrough) {
176     char format[80];
177    
178     snprintf(format, sizeof(format), "<span%s%s%s>%%s</span>",
179     #ifdef USE_MAEMO
180     (size==SIZE_SMALL)?" size='small'":"",
181     #else
182     (size==SIZE_BIG)?" size='x-large'":"",
183     #endif
184     strikethrough?" strikethrough='yes'":"",
185     (strikethrough==STRIKETHROUGH_RED)?" strikethrough_color='red'":"");
186    
187     char *markup = g_markup_printf_escaped(format, str);
188     // printf("markup = %s\n", markup);
189     gtk_label_set_markup(GTK_LABEL(label), markup);
190     g_free(markup);
191     }
192    
193     GtkWidget *gtk_label_attrib(char *str, int size, int strikethrough) {
194     GtkWidget *label = gtk_label_new("");
195     gtk_label_attrib_set(label, str, size, strikethrough);
196     return label;
197     }
198    
199     GtkWidget *gtk_button_attrib(char *str, int size, int strikethrough) {
200     GtkWidget *button = gtk_button_new_with_label("");
201     gtk_label_attrib_set(gtk_bin_get_child(GTK_BIN(button)),
202     str, size, strikethrough);
203     return button;
204     }
205    
206     void textbox_disable(GtkWidget *widget) {
207     gtk_editable_set_editable(GTK_EDITABLE(widget), FALSE);
208     gtk_widget_set_sensitive(widget, FALSE);
209     }
210    
211     void textbox_enable(GtkWidget *widget) {
212     gtk_widget_set_sensitive(widget, TRUE);
213     gtk_editable_set_editable(GTK_EDITABLE(widget), TRUE);
214     }
215    
216     pos_t *get_pos(appdata_t *appdata) {
217     pos_t *pos = &appdata->home;
218    
219     if(appdata->active_location) {
220     int i = appdata->active_location-1;
221     location_t *loc = appdata->location;
222     while(i--) loc = loc->next;
223     pos = &loc->pos;
224     }
225    
226     if(appdata->use_gps) {
227     pos = gps_get_pos(appdata);
228    
229     if(!pos) pos = &appdata->gps; /* use saved position */
230     else appdata->gps = *pos; /* save position */
231     }
232     return pos;
233     }
234    
235     void distance_str(char *str, int len, float dist, gboolean imperial) {
236     if(imperial) {
237     /* 1 mil = 1760 yd = 5280 ft ... */
238     if(dist<0.018) snprintf(str, len, "%.1f ft", dist*5280.0);
239     else if(dist<0.055) snprintf(str, len, "%.1f yd", dist*1760.0);
240     else if(dist<0.55) snprintf(str, len, "%.0f yd", dist*1760.0);
241     else if(dist<10.0) snprintf(str, len, "%.2f mi", dist);
242     else if(dist<100.0) snprintf(str, len, "%.1f mi", dist);
243     else snprintf(str, len, "%.0f mi", dist);
244     } else {
245     if(dist<0.01) snprintf(str, len, "%.2f m", dist*1000.0);
246     else if(dist<0.1) snprintf(str, len, "%.1f m", dist*1000.0);
247     else if(dist<1.0) snprintf(str, len, "%.0f m", dist*1000.0);
248     else if(dist<100.0) snprintf(str, len, "%.1f km", dist);
249     else snprintf(str, len, "%.0f km", dist);
250     }
251     }
252    
253     /* return distance in miles or kilometers */
254     float distance_parse(char *str, gboolean imperial) {
255     char unit[4];
256     float val = NAN;
257    
258     if(sscanf(str, "%f %3s", &val, unit) == 2) {
259     gboolean fimp = FALSE;
260    
261     if(strcasecmp(unit, "ft") == 0) { fimp = TRUE; val /= 5280.0; }
262     else if(strcasecmp(unit, "yd") == 0) { fimp = TRUE; val /= 1760.0; }
263     else if(strcasecmp(unit, "mi") == 0) { fimp = TRUE; }
264     else if(strcasecmp(unit, "m") == 0) { fimp = FALSE; val /= 1000.0; }
265     else if(strcasecmp(unit, "km") == 0) { fimp = FALSE; }
266     else val = NAN;
267    
268     /* found imperial and metric requested? convert miles into kilometers */
269     if(fimp & !imperial) val *= 1.609344;
270    
271     /* found metric and imperial requested? convert kilometers into miles */
272     if(!fimp & imperial) val /= 1.609344;
273     }
274     return val;
275     }
276    
277     static gboolean mark(GtkWidget *widget, gboolean valid) {
278     gtk_widget_set_state(widget, valid?GTK_STATE_NORMAL:TAG_STATE);
279     return valid;
280     }
281    
282     static void callback_modified_lat(GtkWidget *widget, gpointer data ) {
283     float i = pos_parse_lat((char*)gtk_entry_get_text(GTK_ENTRY(widget)));
284     mark(widget, !isnan(i));
285     }
286    
287     /* a entry that is colored red when being "active" */
288     GtkWidget *lat_entry_new(float lat) {
289     GdkColor color;
290     GtkWidget *widget = gtk_entry_new();
291     gdk_color_parse("#ff0000", &color);
292     gtk_widget_modify_text(widget, TAG_STATE, &color);
293    
294     char str[32];
295     pos_lat_str(str, sizeof(str), lat);
296     gtk_entry_set_text(GTK_ENTRY(widget), str);
297    
298     g_signal_connect(G_OBJECT(widget), "changed",
299     G_CALLBACK(callback_modified_lat), NULL);
300    
301     return widget;
302     }
303    
304     static void callback_modified_lon(GtkWidget *widget, gpointer data ) {
305     float i = pos_parse_lon((char*)gtk_entry_get_text(GTK_ENTRY(widget)));
306     mark(widget, !isnan(i));
307     }
308    
309     /* a entry that is colored red when filled with invalid coordinate */
310     GtkWidget *lon_entry_new(float lon) {
311     GdkColor color;
312     GtkWidget *widget = gtk_entry_new();
313     gdk_color_parse("#ff0000", &color);
314     gtk_widget_modify_text(widget, TAG_STATE, &color);
315    
316     char str[32];
317     pos_lon_str(str, sizeof(str), lon);
318     gtk_entry_set_text(GTK_ENTRY(widget), str);
319    
320     g_signal_connect(G_OBJECT(widget), "changed",
321     G_CALLBACK(callback_modified_lon), NULL);
322    
323     return widget;
324     }
325    
326    
327     float lat_get(GtkWidget *widget) {
328     char *p = (char*)gtk_entry_get_text(GTK_ENTRY(widget));
329     return pos_parse_lat(p);
330     }
331    
332     float lon_get(GtkWidget *widget) {
333     char *p = (char*)gtk_entry_get_text(GTK_ENTRY(widget));
334     return pos_parse_lon(p);
335     }
336    
337     static void callback_modified_dist(GtkWidget *widget, gpointer data ) {
338     /* don't care for metric/imperial since we only want to know if this */
339     /* is parseable at all */
340     float i = distance_parse((char*)gtk_entry_get_text(GTK_ENTRY(widget)), FALSE);
341     mark(widget, !isnan(i));
342     }
343    
344     /* a entry that is colored red when filled with invalid distance */
345     GtkWidget *dist_entry_new(float dist, gboolean mil) {
346     GdkColor color;
347     GtkWidget *widget = gtk_entry_new();
348     gdk_color_parse("#ff0000", &color);
349     gtk_widget_modify_text(widget, TAG_STATE, &color);
350    
351     char str[32];
352     distance_str(str, sizeof(str), dist, mil);
353     gtk_entry_set_text(GTK_ENTRY(widget), str);
354    
355     g_signal_connect(G_OBJECT(widget), "changed",
356     G_CALLBACK(callback_modified_dist), NULL);
357    
358     return widget;
359     }
360    
361     float dist_get(GtkWidget *widget, gboolean mil) {
362     char *p = (char*)gtk_entry_get_text(GTK_ENTRY(widget));
363     return distance_parse(p, mil);
364     }
365    
366     #ifndef USE_MAEMO
367     #ifdef ENABLE_BROWSER_INTERFACE
368     #include <libgnome/gnome-url.h>
369    
370     int browser_url(appdata_t *appdata, char *url) {
371     /* taken from gnome-open, part of libgnome */
372     GError *err = NULL;
373     gnome_url_show(url, &err);
374     return 0;
375     }
376     #endif
377     #endif