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 198 by harbaum, Thu Nov 19 12:38:03 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    #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR >= 5)
30    #include <hildon/hildon-entry.h>
31    #endif
32    
33  char strlastchr(char *str) {  char strlastchr(char *str) {
34    return str[strlen(str)]-1;    return str[strlen(str)]-1;
35  }  }
# Line 65  void pos_lat_str(char *str, int len, flo Line 72  void pos_lat_str(char *str, int len, flo
72    char *c = _("N");    char *c = _("N");
73    float integral, fractional;    float integral, fractional;
74    
75    if(latitude < 0) { latitude = fabs(latitude); c = _("S"); }    if(isnan(latitude))
76    fractional = modff(latitude, &integral);      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);      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) {  GtkWidget *pos_lat(float latitude, int size, int strikethrough) {
# Line 82  void pos_lon_str(char *str, int len, flo Line 93  void pos_lon_str(char *str, int len, flo
93    char *c = _("E");    char *c = _("E");
94    float integral, fractional;    float integral, fractional;
95    
96    if(longitude < 0) { longitude = fabs(longitude); c = _("W"); }    if(isnan(longitude))
97    fractional = modff(longitude, &integral);      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);      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) {  GtkWidget *pos_lon(float longitude, int size, int strikethrough) {
# Line 140  float pos_parse_lon(char *str) { Line 155  float pos_parse_lon(char *str) {
155    
156  const char *pos_get_bearing_str(pos_t from, pos_t to) {  const char *pos_get_bearing_str(pos_t from, pos_t to) {
157    static const char *bear_str[]={    static const char *bear_str[]={
158      "N", "NE", "E", "SE", "S", "SW", "W", "NW" };      "N", "NE", "E", "SE", "S", "SW", "W", "NW", "" };
159    int idx = (gpx_pos_get_bearing(from, to)+22.5)/45.0;  
160    /* make sure we stay in icon bounds */    float bearing = gpx_pos_get_bearing(from, to);
161    while(idx < 0) idx += 8;    if(!isnan(bearing)) {
162    while(idx > 7) idx -= 8;      int idx = (bearing+22.5)/45.0;
163    return _(bear_str[idx]);      /* 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 */  /* the maemo font size is quite huge, so we adjust some fonts */
# Line 233  pos_t *get_pos(appdata_t *appdata) { Line 254  pos_t *get_pos(appdata_t *appdata) {
254  }  }
255    
256  void distance_str(char *str, int len, float dist, gboolean imperial) {  void distance_str(char *str, int len, float dist, gboolean imperial) {
257    if(imperial) {    if(isnan(dist))
258        snprintf(str, len, "---");
259      else if(imperial) {
260      /* 1 mil = 1760 yd = 5280 ft ... */      /* 1 mil = 1760 yd = 5280 ft ... */
261      if(dist<0.018)      snprintf(str, len, "%.1f ft", dist*5280.0);      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);      else if(dist<0.055) snprintf(str, len, "%.1f yd", dist*1760.0);
# Line 287  static void callback_modified_lat(GtkWid Line 310  static void callback_modified_lat(GtkWid
310  /* a entry that is colored red when being "active" */  /* a entry that is colored red when being "active" */
311  GtkWidget *lat_entry_new(float lat) {  GtkWidget *lat_entry_new(float lat) {
312    GdkColor color;    GdkColor color;
313    
314    #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5)
315    GtkWidget *widget = gtk_entry_new();    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);    gdk_color_parse("#ff0000", &color);
321    gtk_widget_modify_text(widget, TAG_STATE, &color);    gtk_widget_modify_text(widget, TAG_STATE, &color);
322    
# Line 309  static void callback_modified_lon(GtkWid Line 338  static void callback_modified_lon(GtkWid
338  /* a entry that is colored red when filled with invalid coordinate */  /* a entry that is colored red when filled with invalid coordinate */
339  GtkWidget *lon_entry_new(float lon) {  GtkWidget *lon_entry_new(float lon) {
340    GdkColor color;    GdkColor color;
341    
342    #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5)
343    GtkWidget *widget = gtk_entry_new();    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);    gdk_color_parse("#ff0000", &color);
350    gtk_widget_modify_text(widget, TAG_STATE, &color);    gtk_widget_modify_text(widget, TAG_STATE, &color);
351    
# Line 344  static void callback_modified_dist(GtkWi Line 380  static void callback_modified_dist(GtkWi
380  /* a entry that is colored red when filled with invalid distance */  /* a entry that is colored red when filled with invalid distance */
381  GtkWidget *dist_entry_new(float dist, gboolean mil) {  GtkWidget *dist_entry_new(float dist, gboolean mil) {
382    GdkColor color;    GdkColor color;
383    #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5)
384    GtkWidget *widget = gtk_entry_new();    GtkWidget *widget = gtk_entry_new();
385    #else
386      GtkWidget *widget = hildon_entry_new(HILDON_SIZE_AUTO);
387    #endif
388    gdk_color_parse("#ff0000", &color);    gdk_color_parse("#ff0000", &color);
389    gtk_widget_modify_text(widget, TAG_STATE, &color);    gtk_widget_modify_text(widget, TAG_STATE, &color);
390    
# Line 375  int browser_url(appdata_t *appdata, char Line 415  int browser_url(appdata_t *appdata, char
415  }  }
416  #endif  #endif
417  #endif  #endif
418    
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    #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    
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    /* 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    
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    }

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