Diff of /trunk/src/map-tool.c

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

revision 46 by harbaum, Wed Aug 5 14:11:00 2009 UTC revision 48 by harbaum, Fri Aug 7 07:57:33 2009 UTC
# Line 24  Line 24 
24  #include "osm-gps-map.h"  #include "osm-gps-map.h"
25  #endif  #endif
26    
27    #define GPS_DEFAULT_ZOOM 13
28    
29  /* equatorial radius in meters */  /* equatorial radius in meters */
30  #define EQ_RADIUS     (6378137.0)  #define EQ_RADIUS     (6378137.0)
31    
# Line 80  static const char *get_proxy_uri(appdata Line 82  static const char *get_proxy_uri(appdata
82  }  }
83    
84  static void map_zoom(map_context_t *context, int step) {  static void map_zoom(map_context_t *context, int step) {
85    int zoom;    gint zoom;
86    OsmGpsMap *map = OSM_GPS_MAP(context->widget);    OsmGpsMap *map = OSM_GPS_MAP(context->widget);
87    g_object_get(map, "zoom", &zoom, NULL);    g_object_get(map, "zoom", &zoom, NULL);
88    zoom = osm_gps_map_set_zoom(map, zoom+step);    zoom = osm_gps_map_set_zoom(map, zoom+step);
# Line 88  static void map_zoom(map_context_t *cont Line 90  static void map_zoom(map_context_t *cont
90    /* enable/disable zoom buttons as required */    /* enable/disable zoom buttons as required */
91    gtk_widget_set_sensitive(context->zoomin, zoom<17);    gtk_widget_set_sensitive(context->zoomin, zoom<17);
92    gtk_widget_set_sensitive(context->zoomout, zoom>1);    gtk_widget_set_sensitive(context->zoomout, zoom>1);
93    
94      /* save new zoom */
95      context->appdata->map.zoom = zoom;
96  }  }
97    
98  static gboolean  static gboolean
# Line 107  cb_map_gps(GtkButton *button, map_contex Line 112  cb_map_gps(GtkButton *button, map_contex
112    pos_t *refpos = get_pos(context->appdata);    pos_t *refpos = get_pos(context->appdata);
113    if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) {    if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) {
114      osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),      osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),
115                                refpos->lat, refpos->lon, 14);                        refpos->lat, refpos->lon, GPS_DEFAULT_ZOOM);
116    } else {    } else {
117      /* no coordinates given: display the entire world */      /* no coordinates given: display the entire world */
118      osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),      osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),
# Line 146  static gboolean on_map_configure(GtkWidg Line 151  static gboolean on_map_configure(GtkWidg
151                                   GdkEventConfigure *event,                                   GdkEventConfigure *event,
152                                   map_context_t *context) {                                   map_context_t *context) {
153    
154    cb_map_gps(NULL, context);    /* set default values if they are invalid */
155      if(!context->appdata->map.zoom ||
156         isnan(context->appdata->map.pos.lat) ||
157         isnan(context->appdata->map.pos.lon)) {
158        printf("no valid map position found\n");
159    
160        pos_t *refpos = get_pos(context->appdata);
161        if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) {
162          /* use gps position if present */
163          context->appdata->map.pos = *refpos;
164          context->appdata->map.zoom = GPS_DEFAULT_ZOOM;
165        } else {
166          /* use world map otherwise */
167          context->appdata->map.pos.lat = 0.0;
168          context->appdata->map.pos.lon = 0.0;
169          context->appdata->map.zoom = 1;
170        }
171      }
172    
173      /* jump to initial position */
174      osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),
175                                context->appdata->map.pos.lat,
176                                context->appdata->map.pos.lon,
177                                context->appdata->map.zoom);
178    
179    return FALSE;    return FALSE;
180  }  }
181    
# Line 169  typedef struct { Line 197  typedef struct {
197    GMainLoop *loop;    GMainLoop *loop;
198  } popup_context_t;  } popup_context_t;
199    
200    /* draw shape */
201    #define ARROW_BORDER   20
202    #define CORNER_RADIUS  10
203    
204  #ifndef USE_HILDON  #ifndef USE_MAEMO
205  #define POPUP_WIDTH  300  #define POPUP_WIDTH  300
206  #define POPUP_HEIGHT 100  #define POPUP_HEIGHT 100
207  #else  #else
208  #define POPUP_WIDTH  600  #define POPUP_WIDTH  350
209  #define POPUP_HEIGHT 200  #define POPUP_HEIGHT 120
210  #endif  #endif
211    
212  static gboolean  static gboolean
# Line 242  run_unmap_handler(GtkWindow *window, pop Line 273  run_unmap_handler(GtkWindow *window, pop
273    shutdown_loop(context);    shutdown_loop(context);
274  }  }
275    
 /* draw shape */  
 #define ARROW_BORDER   20  
 #define CORNER_RADIUS  10  
   
276  static void popup_window_shape(GtkWidget *window, int tip_x, int tip_y) {  static void popup_window_shape(GtkWidget *window, int tip_x, int tip_y) {
277    GdkBitmap *mask = gdk_pixmap_new(NULL, POPUP_WIDTH, POPUP_HEIGHT, 1);    GdkBitmap *mask = gdk_pixmap_new(NULL, POPUP_WIDTH, POPUP_HEIGHT, 1);
278    
# Line 269  static void popup_window_shape(GtkWidget Line 296  static void popup_window_shape(GtkWidget
296    gdk_gc_set_foreground(gc, &white);    gdk_gc_set_foreground(gc, &white);
297    gdk_gc_set_background(gc, &black);    gdk_gc_set_background(gc, &black);
298    
299      /* the tip is always above or below the "bubble" but never at its side */
300      guint tip_offset = (tip_y == 0)?ARROW_BORDER:0;
301    
302    gdk_draw_rectangle(mask, gc, TRUE,    gdk_draw_rectangle(mask, gc, TRUE,
303                       0, ARROW_BORDER + CORNER_RADIUS,                       0, tip_offset + CORNER_RADIUS,
304                       POPUP_WIDTH,                       POPUP_WIDTH,
305                       POPUP_HEIGHT - 2*CORNER_RADIUS - 2*ARROW_BORDER);                       POPUP_HEIGHT - 2*CORNER_RADIUS - ARROW_BORDER);
306    
307    gdk_draw_rectangle(mask, gc, TRUE,    gdk_draw_rectangle(mask, gc, TRUE,
308                       CORNER_RADIUS, ARROW_BORDER,                       CORNER_RADIUS, tip_offset,
309                       POPUP_WIDTH  - 2*CORNER_RADIUS,                       POPUP_WIDTH  - 2*CORNER_RADIUS,
310                       POPUP_HEIGHT - 2*ARROW_BORDER);                       POPUP_HEIGHT - ARROW_BORDER);
311    
312    int off[][2] = {    int off[][2] = {
313            { CORNER_RADIUS, ARROW_BORDER + CORNER_RADIUS },            { CORNER_RADIUS,               tip_offset + CORNER_RADIUS },
314            { POPUP_WIDTH - CORNER_RADIUS,            { POPUP_WIDTH - CORNER_RADIUS, tip_offset + CORNER_RADIUS },
             ARROW_BORDER + CORNER_RADIUS },  
315            { POPUP_WIDTH - CORNER_RADIUS,            { POPUP_WIDTH - CORNER_RADIUS,
316              POPUP_HEIGHT - CORNER_RADIUS - ARROW_BORDER },              POPUP_HEIGHT - CORNER_RADIUS - ARROW_BORDER + tip_offset},
317            { CORNER_RADIUS,            { CORNER_RADIUS,
318              POPUP_HEIGHT - CORNER_RADIUS  - ARROW_BORDER}};              POPUP_HEIGHT - CORNER_RADIUS  - ARROW_BORDER + tip_offset}};
319    
320    int i;    int i;
321    for(i=0;i<4;i++) {    for(i=0;i<4;i++) {
# Line 305  static void popup_window_shape(GtkWidget Line 334  static void popup_window_shape(GtkWidget
334    gdk_window_shape_combine_mask(window->window, mask, 0, 0);    gdk_window_shape_combine_mask(window->window, mask, 0, 0);
335  }  }
336    
337    /* create a left aligned label (normal ones are centered) */
338    static GtkWidget *gtk_label_left_new(char *str) {
339      GtkWidget *label = gtk_label_new(str);
340      gtk_misc_set_alignment(GTK_MISC(label), 0.f, .5f);
341      return label;
342    }
343    
344    /* the small labels are actually only on maemo small */
345    #ifdef USE_MAEMO
346    #define MARKUP_SMALL "<span size='small'>%s</span>"
347    GtkWidget *gtk_label_small_left_new(char *str) {
348      GtkWidget *label = gtk_label_new("");
349      char *markup = g_markup_printf_escaped(MARKUP_SMALL, str);
350      gtk_label_set_markup(GTK_LABEL(label), markup);
351      g_free(markup);
352      gtk_misc_set_alignment(GTK_MISC(label), 0.f, .5f);
353      return label;
354    }
355    #define gtk_label_big_left_new(a) gtk_label_left_new(a)
356    #else
357    #define gtk_label_small_left_new(a) gtk_label_left_new(a)
358    #define MARKUP_BIG "<span size='x-large'>%s</span>"
359    GtkWidget *gtk_label_big_left_new(char *str) {
360      GtkWidget *label = gtk_label_new("");
361      char *markup = g_markup_printf_escaped(MARKUP_BIG, str);
362      gtk_label_set_markup(GTK_LABEL(label), markup);
363      g_free(markup);
364      gtk_misc_set_alignment(GTK_MISC(label), 0.f, .5f);
365      return label;
366    }
367    #endif
368    
369  void cache_popup(map_context_t *mcontext, cache_t *cache) {  void cache_popup(map_context_t *mcontext, cache_t *cache) {
370    popup_context_t pcontext;    popup_context_t pcontext;
371    pcontext.appdata = mcontext->appdata;    pcontext.appdata = mcontext->appdata;
# Line 345  void cache_popup(map_context_t *mcontext Line 406  void cache_popup(map_context_t *mcontext
406                                     cache->pos.lat, cache->pos.lon,                                     cache->pos.lat, cache->pos.lon,
407                                     &sx, &sy);                                     &sx, &sy);
408    
   printf("screen pos %d/%d\n", sx, sy);  
   
409    gdk_window_get_origin(mcontext->widget->window, &x, &y);    gdk_window_get_origin(mcontext->widget->window, &x, &y);
   printf("window = %d/%d +%d+%d %d*%d\n", x, y,  
          mcontext->widget->allocation.x,  
          mcontext->widget->allocation.y,  
          mcontext->widget->allocation.width,  
          mcontext->widget->allocation.height  
          );  
410    
411    gint ax = 0, ay = 0;    gint ax = 0, ay = 0;
412    if(sx > mcontext->widget->allocation.width/2)    if(sx > mcontext->widget->allocation.width/2)
# Line 362  void cache_popup(map_context_t *mcontext Line 415  void cache_popup(map_context_t *mcontext
415    if(sy > mcontext->widget->allocation.height/2)    if(sy > mcontext->widget->allocation.height/2)
416      ay = POPUP_HEIGHT;      ay = POPUP_HEIGHT;
417    
418  #if !defined(USE_HILDON) || (MAEMO_VERSION_MAJOR < 5)  #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5)
419    GdkColor color;    GdkColor color;
420    gdk_color_parse("darkgray", &color);    gdk_color_parse("darkgray", &color);
421    gtk_widget_modify_bg(GTK_WIDGET(pcontext.window), GTK_STATE_NORMAL, &color);    gtk_widget_modify_bg(GTK_WIDGET(pcontext.window), GTK_STATE_NORMAL, &color);
# Line 373  void cache_popup(map_context_t *mcontext Line 426  void cache_popup(map_context_t *mcontext
426                    y + mcontext->widget->allocation.y + sy - ay);                    y + mcontext->widget->allocation.y + sy - ay);
427    
428    
429    GtkWidget *frame = gtk_frame_new(NULL);    GtkWidget *alignment = gtk_alignment_new(0.5, 0.5, 1.0, 1.0);
430    gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE);    gtk_alignment_set_padding(GTK_ALIGNMENT(alignment),
431    gtk_container_set_border_width(GTK_CONTAINER(frame),                              CORNER_RADIUS/2 + (ay?0:ARROW_BORDER),
432                                   ARROW_BORDER+CORNER_RADIUS);                              CORNER_RADIUS/2 + (ay?ARROW_BORDER:0),
433    gtk_container_add(GTK_CONTAINER(frame),                              CORNER_RADIUS, CORNER_RADIUS);
434                      gtk_button_new_with_label(cache->name));  
435      /* --- actual content ---- */
436      GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
437    
438    gtk_container_add(GTK_CONTAINER(pcontext.window), frame);    if(cache->id) {
439        GtkWidget *ihbox = gtk_hbox_new(FALSE, 0);
440    
441        gtk_box_pack_start(GTK_BOX(ihbox),
442           icon_get_widget(ICON_CACHE_TYPE, cache->type),
443           FALSE, FALSE, 5);
444    
445        gtk_box_pack_start_defaults(GTK_BOX(ihbox),
446                    gtk_label_big_left_new(cache->id));
447    
448        gtk_box_pack_start_defaults(GTK_BOX(vbox), ihbox);
449      }
450    
451      if(cache->name) {
452        GtkWidget *label = gtk_label_small_left_new(cache->name);
453        gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_END);
454        gtk_box_pack_start_defaults(GTK_BOX(vbox), label);
455      }
456    
457      GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
458      if(cache->terrain) {
459        GtkWidget *ihbox = gtk_hbox_new(FALSE, 0);
460        gtk_box_pack_start(GTK_BOX(ihbox),
461           gtk_label_small_left_new(_("Terrain:")), FALSE, FALSE, 0);
462        gtk_box_pack_start(GTK_BOX(ihbox),
463           icon_get_widget(ICON_STARS, (int)(cache->terrain*2-2)),
464           FALSE, FALSE, 5);
465        gtk_box_pack_start_defaults(GTK_BOX(hbox), ihbox);
466      }
467    
468      if(cache->difficulty) {
469        GtkWidget *ihbox = gtk_hbox_new(FALSE, 0);
470        gtk_box_pack_start(GTK_BOX(ihbox),
471           gtk_label_small_left_new(_("Difficulty:")), FALSE, FALSE, 0);
472        gtk_box_pack_start(GTK_BOX(ihbox),
473           icon_get_widget(ICON_STARS, (int)(cache->difficulty*2-2)),
474           FALSE, FALSE, 5);
475        gtk_box_pack_start_defaults(GTK_BOX(hbox), ihbox);
476      }
477    
478      gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
479    
480      gtk_container_add(GTK_CONTAINER(alignment), vbox);
481      /* ----------------------- */
482    
483    
484      gtk_container_add(GTK_CONTAINER(pcontext.window), alignment);
485    
486    /* --------- give the window its shape ----------- */    /* give window its shape */
487    popup_window_shape(pcontext.window, ax, ay);    popup_window_shape(pcontext.window, ax, ay);
488    
489    gtk_widget_show_all(pcontext.window);    gtk_widget_show_all(pcontext.window);
# Line 449  pos_t coord2pos(coord_t coo) { Line 550  pos_t coord2pos(coord_t coo) {
550  }  }
551    
552  static int dist2pixel(map_context_t *context, float km, float lat) {  static int dist2pixel(map_context_t *context, float km, float lat) {
553    int zoom;    gint zoom;
554    g_object_get(OSM_GPS_MAP(context->widget), "zoom", &zoom, NULL);    g_object_get(OSM_GPS_MAP(context->widget), "zoom", &zoom, NULL);
555    
556    /* world at zoom 1 == 512 pixels */    /* world at zoom 1 == 512 pixels */
# Line 459  static int dist2pixel(map_context_t *con Line 560  static int dist2pixel(map_context_t *con
560    return 1000.0*km/m_per_pix;    return 1000.0*km/m_per_pix;
561  }  }
562    
563  #define CLICK_FUZZ (16)  #define CLICK_FUZZ (24)
564    
565  static gboolean  static gboolean
566  on_map_button_press_event(GtkWidget *widget,  on_map_button_press_event(GtkWidget *widget,
# Line 488  on_map_button_press_event(GtkWidget *wid Line 589  on_map_button_press_event(GtkWidget *wid
589  static gboolean  static gboolean
590  on_map_button_release_event(GtkWidget *widget,  on_map_button_release_event(GtkWidget *widget,
591                              GdkEventButton *event, map_context_t *context) {                              GdkEventButton *event, map_context_t *context) {
592    if(context->press_on) {    OsmGpsMap *map = OSM_GPS_MAP(context->widget);
     OsmGpsMap *map = OSM_GPS_MAP(context->widget);  
593    
594      if(context->press_on) {
595      pos_t pos =      pos_t pos =
596        coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y));        coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y));
597    
# Line 501  on_map_button_release_event(GtkWidget *w Line 602  on_map_button_release_event(GtkWidget *w
602          cache_popup(context, nearest);          cache_popup(context, nearest);
603      }      }
604      context->press_on = NULL;      context->press_on = NULL;
605      } else {
606        /* save new map position */
607        gfloat lat, lon;
608        g_object_get(map, "latitude", &lat, "longitude", &lon, NULL);
609        context->appdata->map.pos.lat = lat;
610        context->appdata->map.pos.lon = lon;
611    }    }
612    
613    return FALSE;    return FALSE;
614  }  }
615    
616    static void save_map_state(map_context_t *context) {
617      /* save map parameters */
618      OsmGpsMap *map = OSM_GPS_MAP(context->widget);
619      gint zoom;
620      g_object_get(map, "zoom", &zoom, NULL);
621      context->appdata->map.zoom = zoom;
622    
623      gfloat lat, lon;
624      g_object_get(map, "latitude", &lat, "longitude", &lon, NULL);
625      context->appdata->map.pos.lat = lat;
626      context->appdata->map.pos.lon = lon;
627    }
628    
629  #if MAEMO_VERSION_MAJOR == 5  #if MAEMO_VERSION_MAJOR == 5
630  static void on_window_destroy(GtkWidget *widget, map_context_t *context) {  static void on_window_destroy(GtkWidget *widget, map_context_t *context) {
631    printf("destroy map view\n");    printf("destroy map view\n");
632    
633      save_map_state(context);
634    
635    /* restore cur_view */    /* restore cur_view */
636    context->appdata->cur_view = context->old_view;    context->appdata->cur_view = context->old_view;
637    
# Line 625  void map(appdata_t *appdata) { Line 746  void map(appdata_t *appdata) {
746    gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE);    gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE);
747    gtk_widget_show_all(dialog);    gtk_widget_show_all(dialog);
748    gtk_dialog_run(GTK_DIALOG(dialog));    gtk_dialog_run(GTK_DIALOG(dialog));
749      save_map_state(context);
750    gtk_timeout_remove(context->handler_id);    gtk_timeout_remove(context->handler_id);
751    gtk_widget_destroy(dialog);    gtk_widget_destroy(dialog);
752    g_free(context);    g_free(context);

Legend:
Removed from v.46  
changed lines
  Added in v.48