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

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

revision 42 by harbaum, Mon Aug 3 19:18:13 2009 UTC revision 47 by harbaum, Thu Aug 6 20:23:12 2009 UTC
# Line 122  static GtkWidget Line 122  static GtkWidget
122                  char *tooltip) {                  char *tooltip) {
123    GtkWidget *button = gtk_button_new();    GtkWidget *button = gtk_button_new();
124    gtk_button_set_image(GTK_BUTTON(button),    gtk_button_set_image(GTK_BUTTON(button),
125                         gtk_image_new_from_stock(icon, GTK_ICON_SIZE_MENU));                         gtk_image_new_from_stock(icon, GTK_ICON_SIZE_BUTTON));
126    g_signal_connect(button, "clicked", cb, data);    g_signal_connect(button, "clicked", cb, data);
127  #ifndef USE_MAEMO  #ifndef USE_MAEMO
128    gtk_widget_set_tooltip_text(button, tooltip);    gtk_widget_set_tooltip_text(button, tooltip);
# Line 169  typedef struct { Line 169  typedef struct {
169    GMainLoop *loop;    GMainLoop *loop;
170  } popup_context_t;  } popup_context_t;
171    
172    /* draw shape */
173    #define ARROW_BORDER   20
174    #define CORNER_RADIUS  10
175    
176  #ifndef USE_HILDON  #ifndef USE_MAEMO
177  #define POPUP_WIDTH  300  #define POPUP_WIDTH  300
178  #define POPUP_HEIGHT 100  #define POPUP_HEIGHT 100
179  #else  #else
180  #define POPUP_WIDTH  600  #define POPUP_WIDTH  350
181  #define POPUP_HEIGHT 200  #define POPUP_HEIGHT 120
182  #endif  #endif
183    
184  static gboolean  static gboolean
# Line 199  on_button_press_event(GtkWidget *widget, Line 202  on_button_press_event(GtkWidget *widget,
202                            GdkEventButton *event, popup_context_t *context) {                            GdkEventButton *event, popup_context_t *context) {
203    gboolean in = pointer_in_window(widget, event->x_root, event->y_root);    gboolean in = pointer_in_window(widget, event->x_root, event->y_root);
204    
205    printf("overlay button press(in = %d)\n", in);    printf("overlay button press (in = %d)\n", in);
206    return !in;    return !in;
207  }  }
208    
# Line 208  on_button_release_event(GtkWidget *widge Line 211  on_button_release_event(GtkWidget *widge
211                            GdkEventButton *event, popup_context_t *context) {                            GdkEventButton *event, popup_context_t *context) {
212    gboolean in = pointer_in_window(widget, event->x_root, event->y_root);    gboolean in = pointer_in_window(widget, event->x_root, event->y_root);
213    
214    printf("overlay button release(in = %d)\n", in);    printf("overlay button release (in = %d)\n", in);
215    
216    if(!in) {    if(!in) {
217      printf("destroying popup\n");      printf("destroying popup\n");
# Line 242  run_unmap_handler(GtkWindow *window, pop Line 245  run_unmap_handler(GtkWindow *window, pop
245    shutdown_loop(context);    shutdown_loop(context);
246  }  }
247    
248    static void popup_window_shape(GtkWidget *window, int tip_x, int tip_y) {
249      GdkBitmap *mask = gdk_pixmap_new(NULL, POPUP_WIDTH, POPUP_HEIGHT, 1);
250    
251      GdkGC *gc = gdk_gc_new(mask);
252      GdkColormap *colormap;
253      GdkColor black;
254      GdkColor white;
255    
256      /* get black/white color values */
257      colormap = gdk_colormap_get_system();
258      gdk_color_black(colormap, &black);
259      gdk_color_white(colormap, &white);
260    
261      /* erase */
262      gdk_gc_set_foreground(gc, &black);
263      gdk_gc_set_background(gc, &white);
264    
265      /* erase background */
266      gdk_draw_rectangle(mask, gc, TRUE, 0, 0, POPUP_WIDTH, POPUP_HEIGHT);
267    
268      gdk_gc_set_foreground(gc, &white);
269      gdk_gc_set_background(gc, &black);
270    
271      /* the tip is always above or below the "bubble" but never at its side */
272      guint tip_offset = (tip_y == 0)?ARROW_BORDER:0;
273    
274      gdk_draw_rectangle(mask, gc, TRUE,
275                         0, tip_offset + CORNER_RADIUS,
276                         POPUP_WIDTH,
277                         POPUP_HEIGHT - 2*CORNER_RADIUS - ARROW_BORDER);
278    
279      gdk_draw_rectangle(mask, gc, TRUE,
280                         CORNER_RADIUS, tip_offset,
281                         POPUP_WIDTH  - 2*CORNER_RADIUS,
282                         POPUP_HEIGHT - ARROW_BORDER);
283    
284      int off[][2] = {
285              { CORNER_RADIUS,               tip_offset + CORNER_RADIUS },
286              { POPUP_WIDTH - CORNER_RADIUS, tip_offset + CORNER_RADIUS },
287              { POPUP_WIDTH - CORNER_RADIUS,
288                POPUP_HEIGHT - CORNER_RADIUS - ARROW_BORDER + tip_offset},
289              { CORNER_RADIUS,
290                POPUP_HEIGHT - CORNER_RADIUS  - ARROW_BORDER + tip_offset}};
291    
292      int i;
293      for(i=0;i<4;i++) {
294        gdk_draw_arc(mask, gc, TRUE,
295                     off[i][0]-CORNER_RADIUS, off[i][1]-CORNER_RADIUS,
296                     2*CORNER_RADIUS,         2*CORNER_RADIUS,
297                     0, 360*64);
298      }
299    
300      GdkPoint points[3] = { {POPUP_WIDTH*1/3, POPUP_HEIGHT/2},
301                             {POPUP_WIDTH*2/3, POPUP_HEIGHT/2},
302                             {tip_x,tip_y} };
303      gdk_draw_polygon(mask, gc, TRUE, points, 3);
304    
305    
306      gdk_window_shape_combine_mask(window->window, mask, 0, 0);
307    }
308    
309    /* create a left aligned label (normal ones are centered) */
310    static GtkWidget *gtk_label_left_new(char *str) {
311      GtkWidget *label = gtk_label_new(str);
312      gtk_misc_set_alignment(GTK_MISC(label), 0.f, .5f);
313      return label;
314    }
315    
316    /* the small labels are actually only on maemo small */
317    #ifdef USE_MAEMO
318    #define MARKUP_SMALL "<span size='small'>%s</span>"
319    GtkWidget *gtk_label_small_left_new(char *str) {
320      GtkWidget *label = gtk_label_new("");
321      char *markup = g_markup_printf_escaped(MARKUP_SMALL, str);
322      gtk_label_set_markup(GTK_LABEL(label), markup);
323      g_free(markup);
324      gtk_misc_set_alignment(GTK_MISC(label), 0.f, .5f);
325      return label;
326    }
327    #define gtk_label_big_left_new(a) gtk_label_left_new(a)
328    #else
329    #define gtk_label_small_left_new(a) gtk_label_left_new(a)
330    #define MARKUP_BIG "<span size='x-large'>%s</span>"
331    GtkWidget *gtk_label_big_left_new(char *str) {
332      GtkWidget *label = gtk_label_new("");
333      char *markup = g_markup_printf_escaped(MARKUP_BIG, str);
334      gtk_label_set_markup(GTK_LABEL(label), markup);
335      g_free(markup);
336      gtk_misc_set_alignment(GTK_MISC(label), 0.f, .5f);
337      return label;
338    }
339    #endif
340    
341  void cache_popup(map_context_t *mcontext, cache_t *cache) {  void cache_popup(map_context_t *mcontext, cache_t *cache) {
342    popup_context_t pcontext;    popup_context_t pcontext;
343    pcontext.appdata = mcontext->appdata;    pcontext.appdata = mcontext->appdata;
# Line 250  void cache_popup(map_context_t *mcontext Line 346  void cache_popup(map_context_t *mcontext
346    gtk_widget_realize(pcontext.window);    gtk_widget_realize(pcontext.window);
347    gtk_window_set_default_size(GTK_WINDOW(pcontext.window),    gtk_window_set_default_size(GTK_WINDOW(pcontext.window),
348                                POPUP_WIDTH, POPUP_HEIGHT);                                POPUP_WIDTH, POPUP_HEIGHT);
349    gtk_window_resize(GTK_WINDOW(pcontext.window),    gtk_window_resize(GTK_WINDOW(pcontext.window), POPUP_WIDTH, POPUP_HEIGHT);
                     POPUP_WIDTH, POPUP_HEIGHT);  
350    //  gtk_window_set_resizable(GTK_WINDOW(pcontext.window), FALSE);    //  gtk_window_set_resizable(GTK_WINDOW(pcontext.window), FALSE);
351    gtk_window_set_transient_for(GTK_WINDOW(pcontext.window),    gtk_window_set_transient_for(GTK_WINDOW(pcontext.window),
352                                 GTK_WINDOW(mcontext->appdata->window));                                 GTK_WINDOW(mcontext->appdata->window));
# Line 278  void cache_popup(map_context_t *mcontext Line 373  void cache_popup(map_context_t *mcontext
373    gtk_grab_add(pcontext.window);    gtk_grab_add(pcontext.window);
374    
375    /* check whether cache is in upper or lower half of window */    /* check whether cache is in upper or lower half of window */
376    gint x, y;    gint x, y, sx, sy;
377    osm_gps_map_geographic_to_screen(OSM_GPS_MAP(mcontext->widget),    osm_gps_map_geographic_to_screen(OSM_GPS_MAP(mcontext->widget),
378                                     cache->pos.lat, cache->pos.lon,                                     cache->pos.lat, cache->pos.lon,
379                                     &x, &y);                                     &sx, &sy);
380    
   printf("screen pos %d/%d\n", x, y);  
   
381    gdk_window_get_origin(mcontext->widget->window, &x, &y);    gdk_window_get_origin(mcontext->widget->window, &x, &y);
   printf("window = %d/%d %d/%d\n", x, y,  
          mcontext->widget->allocation.x,  
          mcontext->widget->allocation.y);  
   
   //  gtk_window_move(GTK_WINDOW(pcontext.window),  
   //              x + button->allocation.x,  
   //              y + button->allocation.y - HEIGHT);  
382    
383      gint ax = 0, ay = 0;
384      if(sx > mcontext->widget->allocation.width/2)
385        ax = POPUP_WIDTH;
386    
387      if(sy > mcontext->widget->allocation.height/2)
388        ay = POPUP_HEIGHT;
389    
390    #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5)
391      GdkColor color;
392      gdk_color_parse("darkgray", &color);
393      gtk_widget_modify_bg(GTK_WIDGET(pcontext.window), GTK_STATE_NORMAL, &color);
394    #endif
395    
396    gtk_window_move(GTK_WINDOW(pcontext.window),    gtk_window_move(GTK_WINDOW(pcontext.window),
397                    100,                    x + mcontext->widget->allocation.x + sx - ax,
398                    100);                    y + mcontext->widget->allocation.y + sy - ay);
399    
400    
401      GtkWidget *alignment = gtk_alignment_new(0.5, 0.5, 1.0, 1.0);
402      gtk_alignment_set_padding(GTK_ALIGNMENT(alignment),
403                                CORNER_RADIUS/2 + (ay?0:ARROW_BORDER),
404                                CORNER_RADIUS/2 + (ay?ARROW_BORDER:0),
405                                CORNER_RADIUS, CORNER_RADIUS);
406    
407      /* --- actual content ---- */
408      GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
409    
410    /* a frame with a vscale inside */    if(cache->id) {
411    GtkWidget *frame = gtk_frame_new(NULL);      GtkWidget *ihbox = gtk_hbox_new(FALSE, 0);
   gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT);  
412    
413    gtk_container_add(GTK_CONTAINER(frame), gtk_label_new(cache->name));      gtk_box_pack_start(GTK_BOX(ihbox),
414    gtk_container_add(GTK_CONTAINER(pcontext.window), frame);         icon_get_widget(ICON_CACHE_TYPE, cache->type),
415           FALSE, FALSE, 5);
416    
417        gtk_box_pack_start_defaults(GTK_BOX(ihbox),
418                    gtk_label_big_left_new(cache->id));
419    
420        gtk_box_pack_start_defaults(GTK_BOX(vbox), ihbox);
421      }
422    
423      if(cache->name) {
424        GtkWidget *label = gtk_label_small_left_new(cache->name);
425        gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_END);
426        gtk_box_pack_start_defaults(GTK_BOX(vbox), label);
427      }
428    
429      GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
430      if(cache->terrain) {
431        GtkWidget *ihbox = gtk_hbox_new(FALSE, 0);
432        gtk_box_pack_start(GTK_BOX(ihbox),
433           gtk_label_small_left_new(_("Terrain:")), FALSE, FALSE, 0);
434        gtk_box_pack_start(GTK_BOX(ihbox),
435           icon_get_widget(ICON_STARS, (int)(cache->terrain*2-2)),
436           FALSE, FALSE, 5);
437        gtk_box_pack_start_defaults(GTK_BOX(hbox), ihbox);
438      }
439    
440      if(cache->difficulty) {
441        GtkWidget *ihbox = gtk_hbox_new(FALSE, 0);
442        gtk_box_pack_start(GTK_BOX(ihbox),
443           gtk_label_small_left_new(_("Difficulty:")), FALSE, FALSE, 0);
444        gtk_box_pack_start(GTK_BOX(ihbox),
445           icon_get_widget(ICON_STARS, (int)(cache->difficulty*2-2)),
446           FALSE, FALSE, 5);
447        gtk_box_pack_start_defaults(GTK_BOX(hbox), ihbox);
448      }
449    
450      gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
451    
452      gtk_container_add(GTK_CONTAINER(alignment), vbox);
453      /* ----------------------- */
454    
455    
456      gtk_container_add(GTK_CONTAINER(pcontext.window), alignment);
457    
458      /* give window its shape */
459      popup_window_shape(pcontext.window, ax, ay);
460    
461    gtk_widget_show_all(pcontext.window);    gtk_widget_show_all(pcontext.window);
462    
463    /* handle this popup until it's gone */    /* handle this popup until it's gone */
# Line 380  static int dist2pixel(map_context_t *con Line 532  static int dist2pixel(map_context_t *con
532    return 1000.0*km/m_per_pix;    return 1000.0*km/m_per_pix;
533  }  }
534    
535  #define CLICK_FUZZ (10)  #define CLICK_FUZZ (24)
536    
537  static gboolean  static gboolean
538  on_map_button_press_event(GtkWidget *widget,  on_map_button_press_event(GtkWidget *widget,
539                              GdkEventButton *event, map_context_t *context) {                              GdkEventButton *event, map_context_t *context) {
540    OsmGpsMap *map = OSM_GPS_MAP(context->widget);    OsmGpsMap *map = OSM_GPS_MAP(context->widget);
541    
542      /* got a press event without release event? eat it! */
543      if(context->press_on != NULL) {
544        printf("PRESS: already\n");
545        return TRUE;
546      }
547    
548    pos_t pos =    pos_t pos =
549      coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y));      coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y));
550    
551    cache_t *nearest = map_closest(context, &pos);    cache_t *nearest = map_closest(context, &pos);
552    if(nearest) {    if(nearest) {
553      float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE);      float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE);
554      if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ) {      if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ)
555        context->press_on = nearest;        context->press_on = nearest;
       return TRUE;  
     }  
556    }    }
557    
   context->press_on = NULL;  
558    return FALSE;    return FALSE;
559  }  }
560    
# Line 415  on_map_button_release_event(GtkWidget *w Line 570  on_map_button_release_event(GtkWidget *w
570      cache_t *nearest = map_closest(context, &pos);      cache_t *nearest = map_closest(context, &pos);
571      if(nearest && nearest == context->press_on) {      if(nearest && nearest == context->press_on) {
572        float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE);        float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE);
573        if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ) {        if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ)
   
574          cache_popup(context, nearest);          cache_popup(context, nearest);
   
         context->press_on = NULL;  
         return TRUE;  
       }  
575      }      }
576        context->press_on = NULL;
577    }    }
578    
   context->press_on = NULL;  
579    return FALSE;    return FALSE;
580  }  }
581    
582    
583  #if MAEMO_VERSION_MAJOR == 5  #if MAEMO_VERSION_MAJOR == 5
584  static void on_window_destroy(GtkWidget *widget, map_context_t *context) {  static void on_window_destroy(GtkWidget *widget, map_context_t *context) {
585    printf("destroy map view\n");    printf("destroy map view\n");
# Line 472  void map(appdata_t *appdata) { Line 623  void map(appdata_t *appdata) {
623  #ifdef USE_MAEMO  #ifdef USE_MAEMO
624    } else {    } else {
625      map_draw_cachelist(context->widget, appdata->cur_gpx->cache);      map_draw_cachelist(context->widget, appdata->cur_gpx->cache);
626      name = g_strdup(_("appdata->cur_gpx->name"));      name = g_strdup(appdata->cur_gpx->name);
627    }    }
628  #endif  #endif
629    
# Line 490  void map(appdata_t *appdata) { Line 641  void map(appdata_t *appdata) {
641                            NULL);                            NULL);
642    
643  #ifndef USE_MAEMO  #ifndef USE_MAEMO
644    gtk_window_set_default_size(GTK_WINDOW(dialog), 400, 350);    gtk_window_set_default_size(GTK_WINDOW(dialog), 640, 480);
645  #else  #else
646    gtk_window_set_default_size(GTK_WINDOW(dialog), 800, 480);    gtk_window_set_default_size(GTK_WINDOW(dialog), 800, 480);
647  #endif  #endif

Legend:
Removed from v.42  
changed lines
  Added in v.47