Diff of /trunk/src/misc.c

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

revision 13 by harbaum, Sat Jun 27 11:09:19 2009 UTC revision 190 by harbaum, Tue Nov 17 10:22:41 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 148  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 241  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 295  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 317  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 383  int browser_url(appdata_t *appdata, char Line 411  int browser_url(appdata_t *appdata, char
411  }  }
412  #endif  #endif
413  #endif  #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    }

Legend:
Removed from v.13  
changed lines
  Added in v.190