Diff of /trunk/src/misc.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1 by harbaum, Sat Jun 20 11:08:47 2009 UTC revision 185 by harbaum, Sat Nov 14 16:55:33 2009 UTC
# Line 21  Line 21 
21  #include <string.h>  #include <string.h>
22  #include <ctype.h>  #include <ctype.h>
23    
24    #include <glib.h>
25    #include <glib/gstdio.h>
26    
27  #include "gpxview.h"  #include "gpxview.h"
28    
29  char strlastchr(char *str) {  char strlastchr(char *str) {
# Line 65  void pos_lat_str(char *str, int len, flo Line 68  void pos_lat_str(char *str, int len, flo
68    char *c = _("N");    char *c = _("N");
69    float integral, fractional;    float integral, fractional;
70    
71    if(latitude < 0) { latitude = fabs(latitude); c = _("S"); }    if(isnan(latitude))
72    fractional = modff(latitude, &integral);      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);      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) {  GtkWidget *pos_lat(float latitude, int size, int strikethrough) {
# Line 82  void pos_lon_str(char *str, int len, flo Line 89  void pos_lon_str(char *str, int len, flo
89    char *c = _("E");    char *c = _("E");
90    float integral, fractional;    float integral, fractional;
91    
92    if(longitude < 0) { longitude = fabs(longitude); c = _("W"); }    if(isnan(longitude))
93    fractional = modff(longitude, &integral);      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);      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) {  GtkWidget *pos_lon(float longitude, int size, int strikethrough) {
# Line 140  float pos_parse_lon(char *str) { Line 151  float pos_parse_lon(char *str) {
151    
152  const char *pos_get_bearing_str(pos_t from, pos_t to) {  const char *pos_get_bearing_str(pos_t from, pos_t to) {
153    static const char *bear_str[]={    static const char *bear_str[]={
154      "N", "NE", "E", "SE", "S", "SW", "W", "NW" };      "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 */    float bearing = gpx_pos_get_bearing(from, to);
157    while(idx < 0) idx += 8;    if(!isnan(bearing)) {
158    while(idx > 7) idx -= 8;      int idx = (bearing+22.5)/45.0;
159    return _(bear_str[idx]);      /* make sure we stay in icon bounds */
160        while(idx < 0) idx += 8;
161        while(idx > 7) idx -= 8;
162        return _(bear_str[idx]);
163      }
164    
165      return bear_str[8];  // empty string
166  }  }
167    
168  /* the maemo font size is quite huge, so we adjust some fonts */  /* the maemo font size is quite huge, so we adjust some fonts */
# Line 233  pos_t *get_pos(appdata_t *appdata) { Line 250  pos_t *get_pos(appdata_t *appdata) {
250  }  }
251    
252  void distance_str(char *str, int len, float dist, gboolean imperial) {  void distance_str(char *str, int len, float dist, gboolean imperial) {
253    if(imperial) {    if(isnan(dist))
254        snprintf(str, len, "---");
255      else if(imperial) {
256      /* 1 mil = 1760 yd = 5280 ft ... */      /* 1 mil = 1760 yd = 5280 ft ... */
257      if(dist<0.018)      snprintf(str, len, "%.1f ft", dist*5280.0);      if(dist<0.018)      snprintf(str, len, "%.1f ft", dist*5280.0);
258      else if(dist<0.055) snprintf(str, len, "%.1f yd", dist*1760.0);      else if(dist<0.055) snprintf(str, len, "%.1f yd", dist*1760.0);
# Line 375  int browser_url(appdata_t *appdata, char Line 394  int browser_url(appdata_t *appdata, char
394  }  }
395  #endif  #endif
396  #endif  #endif
397    
398    /* recursively remove an entire file system */
399    void rmdir_recursive(char *path) {
400      GDir *dir = g_dir_open(path, 0, NULL);
401      if(dir) {
402        const char *name = g_dir_read_name(dir);
403        while(name) {
404          char *fullname = g_strdup_printf("%s/%s", path, name);
405          //      printf("deleting %s\n", fullname);
406    
407          if(g_file_test(fullname, G_FILE_TEST_IS_DIR))
408            rmdir_recursive(fullname);
409          else if(g_file_test(fullname, G_FILE_TEST_IS_REGULAR))
410            g_remove(fullname);
411    
412          g_free(fullname);
413          name = g_dir_read_name(dir);
414        }
415    
416        g_dir_close(dir);
417      }
418      g_rmdir(path);
419    }
420    
421    #ifdef ENABLE_BROWSER_INTERFACE
422    static void on_link_clicked(GtkButton *button, gpointer data) {
423      appdata_t *appdata = (appdata_t*)data;
424      char *url = g_object_get_data(G_OBJECT(button), "url");
425      if(url) browser_url(appdata, url);
426    }
427    #endif
428    
429    /* a button containing a weblink */
430    GtkWidget *link_button_attrib(appdata_t *appdata, char *str, char *url,
431                           int size, int strikethrough) {
432    
433    #ifdef ENABLE_BROWSER_INTERFACE
434      if(url) {
435        GtkWidget *button = gtk_button_attrib(str, size, strikethrough);
436        g_object_set_data(G_OBJECT(button), "url", url);
437        gtk_signal_connect(GTK_OBJECT(button), "clicked",
438                           (GtkSignalFunc)on_link_clicked, appdata);
439    
440        return button;
441      }
442    #endif
443      return gtk_label_attrib(str, size, strikethrough);
444    }
445    
446    #ifdef ENABLE_BROWSER_INTERFACE
447    static void on_link_id_clicked(GtkButton *button, gpointer data) {
448      appdata_t *appdata = (appdata_t*)data;
449    
450      unsigned int id = (unsigned int)g_object_get_data(G_OBJECT(button), "id");
451      char *type = g_object_get_data(G_OBJECT(button), "type");
452    
453      char *url = g_strdup_printf("http://www.geocaching.com/%s?id=%u",
454                                  type, id);
455    
456      if(url) {
457        browser_url(appdata, url);
458        g_free(url);
459      }
460    }
461    #endif
462    
463    GtkWidget *link_button_by_id(appdata_t *appdata, char *str,
464                                 const char *type, int id) {
465    
466    #ifdef ENABLE_BROWSER_INTERFACE
467      if(id) {
468        GtkWidget *ref = gtk_button_new_with_label(str);
469    #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR == 5)
470        //    hildon_gtk_widget_set_theme_size(ref,
471        //         (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH));
472    #endif
473        g_object_set_data(G_OBJECT(ref), "id", (gpointer)id);
474        g_object_set_data(G_OBJECT(ref), "type", (gpointer)type);
475        gtk_signal_connect(GTK_OBJECT(ref), "clicked",
476                           GTK_SIGNAL_FUNC(on_link_id_clicked), appdata);
477    
478        return ref;
479      }
480    #endif
481      return gtk_label_new(str);
482    }
483    
484    
485    GtkWidget *link_icon_button_by_id(appdata_t *appdata, GtkWidget *icon,
486                                 const char *type, int id) {
487    
488    #ifdef ENABLE_BROWSER_INTERFACE
489      if(id) {
490        GtkWidget *ref = gtk_button_new();
491        gtk_button_set_image(GTK_BUTTON(ref), icon);
492    
493    #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR == 5)
494        //    hildon_gtk_widget_set_theme_size(ref,
495        //         (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH));
496    #endif
497        g_object_set_data(G_OBJECT(ref), "id", (gpointer)id);
498        g_object_set_data(G_OBJECT(ref), "type", (gpointer)type);
499        gtk_signal_connect(GTK_OBJECT(ref), "clicked",
500                           GTK_SIGNAL_FUNC(on_link_id_clicked), appdata);
501    
502        return ref;
503      }
504    #endif
505      return icon;
506    }
507    
508    /* left aligned, word wrapped multiline widget */
509    GtkWidget *simple_text_widget(char *text) {
510      GtkWidget *label = gtk_label_new(text);
511    
512      gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
513      gtk_label_set_line_wrap_mode(GTK_LABEL(label), PANGO_WRAP_WORD);
514      gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
515    
516      return label;
517    }

Legend:
Removed from v.1  
changed lines
  Added in v.185