--- trunk/src/map-tool.c 2009/08/04 14:07:18 44
+++ trunk/src/map-tool.c 2009/08/07 07:57:33 48
@@ -24,6 +24,8 @@
#include "osm-gps-map.h"
#endif
+#define GPS_DEFAULT_ZOOM 13
+
/* equatorial radius in meters */
#define EQ_RADIUS (6378137.0)
@@ -80,7 +82,7 @@
}
static void map_zoom(map_context_t *context, int step) {
- int zoom;
+ gint zoom;
OsmGpsMap *map = OSM_GPS_MAP(context->widget);
g_object_get(map, "zoom", &zoom, NULL);
zoom = osm_gps_map_set_zoom(map, zoom+step);
@@ -88,6 +90,9 @@
/* enable/disable zoom buttons as required */
gtk_widget_set_sensitive(context->zoomin, zoom<17);
gtk_widget_set_sensitive(context->zoomout, zoom>1);
+
+ /* save new zoom */
+ context->appdata->map.zoom = zoom;
}
static gboolean
@@ -107,7 +112,7 @@
pos_t *refpos = get_pos(context->appdata);
if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) {
osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),
- refpos->lat, refpos->lon, 14);
+ refpos->lat, refpos->lon, GPS_DEFAULT_ZOOM);
} else {
/* no coordinates given: display the entire world */
osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),
@@ -122,7 +127,7 @@
char *tooltip) {
GtkWidget *button = gtk_button_new();
gtk_button_set_image(GTK_BUTTON(button),
- gtk_image_new_from_stock(icon, GTK_ICON_SIZE_MENU));
+ gtk_image_new_from_stock(icon, GTK_ICON_SIZE_BUTTON));
g_signal_connect(button, "clicked", cb, data);
#ifndef USE_MAEMO
gtk_widget_set_tooltip_text(button, tooltip);
@@ -146,8 +151,31 @@
GdkEventConfigure *event,
map_context_t *context) {
- cb_map_gps(NULL, context);
+ /* set default values if they are invalid */
+ if(!context->appdata->map.zoom ||
+ isnan(context->appdata->map.pos.lat) ||
+ isnan(context->appdata->map.pos.lon)) {
+ printf("no valid map position found\n");
+
+ pos_t *refpos = get_pos(context->appdata);
+ if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) {
+ /* use gps position if present */
+ context->appdata->map.pos = *refpos;
+ context->appdata->map.zoom = GPS_DEFAULT_ZOOM;
+ } else {
+ /* use world map otherwise */
+ context->appdata->map.pos.lat = 0.0;
+ context->appdata->map.pos.lon = 0.0;
+ context->appdata->map.zoom = 1;
+ }
+ }
+ /* jump to initial position */
+ osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),
+ context->appdata->map.pos.lat,
+ context->appdata->map.pos.lon,
+ context->appdata->map.zoom);
+
return FALSE;
}
@@ -169,13 +197,16 @@
GMainLoop *loop;
} popup_context_t;
+/* draw shape */
+#define ARROW_BORDER 20
+#define CORNER_RADIUS 10
-#ifndef USE_HILDON
+#ifndef USE_MAEMO
#define POPUP_WIDTH 300
#define POPUP_HEIGHT 100
#else
-#define POPUP_WIDTH 600
-#define POPUP_HEIGHT 200
+#define POPUP_WIDTH 350
+#define POPUP_HEIGHT 120
#endif
static gboolean
@@ -199,7 +230,7 @@
GdkEventButton *event, popup_context_t *context) {
gboolean in = pointer_in_window(widget, event->x_root, event->y_root);
- printf("overlay button press(in = %d)\n", in);
+ printf("overlay button press (in = %d)\n", in);
return !in;
}
@@ -208,7 +239,7 @@
GdkEventButton *event, popup_context_t *context) {
gboolean in = pointer_in_window(widget, event->x_root, event->y_root);
- printf("overlay button release(in = %d)\n", in);
+ printf("overlay button release (in = %d)\n", in);
if(!in) {
printf("destroying popup\n");
@@ -242,6 +273,99 @@
shutdown_loop(context);
}
+static void popup_window_shape(GtkWidget *window, int tip_x, int tip_y) {
+ GdkBitmap *mask = gdk_pixmap_new(NULL, POPUP_WIDTH, POPUP_HEIGHT, 1);
+
+ GdkGC *gc = gdk_gc_new(mask);
+ GdkColormap *colormap;
+ GdkColor black;
+ GdkColor white;
+
+ /* get black/white color values */
+ colormap = gdk_colormap_get_system();
+ gdk_color_black(colormap, &black);
+ gdk_color_white(colormap, &white);
+
+ /* erase */
+ gdk_gc_set_foreground(gc, &black);
+ gdk_gc_set_background(gc, &white);
+
+ /* erase background */
+ gdk_draw_rectangle(mask, gc, TRUE, 0, 0, POPUP_WIDTH, POPUP_HEIGHT);
+
+ gdk_gc_set_foreground(gc, &white);
+ gdk_gc_set_background(gc, &black);
+
+ /* the tip is always above or below the "bubble" but never at its side */
+ guint tip_offset = (tip_y == 0)?ARROW_BORDER:0;
+
+ gdk_draw_rectangle(mask, gc, TRUE,
+ 0, tip_offset + CORNER_RADIUS,
+ POPUP_WIDTH,
+ POPUP_HEIGHT - 2*CORNER_RADIUS - ARROW_BORDER);
+
+ gdk_draw_rectangle(mask, gc, TRUE,
+ CORNER_RADIUS, tip_offset,
+ POPUP_WIDTH - 2*CORNER_RADIUS,
+ POPUP_HEIGHT - ARROW_BORDER);
+
+ int off[][2] = {
+ { CORNER_RADIUS, tip_offset + CORNER_RADIUS },
+ { POPUP_WIDTH - CORNER_RADIUS, tip_offset + CORNER_RADIUS },
+ { POPUP_WIDTH - CORNER_RADIUS,
+ POPUP_HEIGHT - CORNER_RADIUS - ARROW_BORDER + tip_offset},
+ { CORNER_RADIUS,
+ POPUP_HEIGHT - CORNER_RADIUS - ARROW_BORDER + tip_offset}};
+
+ int i;
+ for(i=0;i<4;i++) {
+ gdk_draw_arc(mask, gc, TRUE,
+ off[i][0]-CORNER_RADIUS, off[i][1]-CORNER_RADIUS,
+ 2*CORNER_RADIUS, 2*CORNER_RADIUS,
+ 0, 360*64);
+ }
+
+ GdkPoint points[3] = { {POPUP_WIDTH*1/3, POPUP_HEIGHT/2},
+ {POPUP_WIDTH*2/3, POPUP_HEIGHT/2},
+ {tip_x,tip_y} };
+ gdk_draw_polygon(mask, gc, TRUE, points, 3);
+
+
+ gdk_window_shape_combine_mask(window->window, mask, 0, 0);
+}
+
+/* create a left aligned label (normal ones are centered) */
+static GtkWidget *gtk_label_left_new(char *str) {
+ GtkWidget *label = gtk_label_new(str);
+ gtk_misc_set_alignment(GTK_MISC(label), 0.f, .5f);
+ return label;
+}
+
+/* the small labels are actually only on maemo small */
+#ifdef USE_MAEMO
+#define MARKUP_SMALL "%s"
+GtkWidget *gtk_label_small_left_new(char *str) {
+ GtkWidget *label = gtk_label_new("");
+ char *markup = g_markup_printf_escaped(MARKUP_SMALL, str);
+ gtk_label_set_markup(GTK_LABEL(label), markup);
+ g_free(markup);
+ gtk_misc_set_alignment(GTK_MISC(label), 0.f, .5f);
+ return label;
+}
+#define gtk_label_big_left_new(a) gtk_label_left_new(a)
+#else
+#define gtk_label_small_left_new(a) gtk_label_left_new(a)
+#define MARKUP_BIG "%s"
+GtkWidget *gtk_label_big_left_new(char *str) {
+ GtkWidget *label = gtk_label_new("");
+ char *markup = g_markup_printf_escaped(MARKUP_BIG, str);
+ gtk_label_set_markup(GTK_LABEL(label), markup);
+ g_free(markup);
+ gtk_misc_set_alignment(GTK_MISC(label), 0.f, .5f);
+ return label;
+}
+#endif
+
void cache_popup(map_context_t *mcontext, cache_t *cache) {
popup_context_t pcontext;
pcontext.appdata = mcontext->appdata;
@@ -250,8 +374,7 @@
gtk_widget_realize(pcontext.window);
gtk_window_set_default_size(GTK_WINDOW(pcontext.window),
POPUP_WIDTH, POPUP_HEIGHT);
- gtk_window_resize(GTK_WINDOW(pcontext.window),
- POPUP_WIDTH, POPUP_HEIGHT);
+ gtk_window_resize(GTK_WINDOW(pcontext.window), POPUP_WIDTH, POPUP_HEIGHT);
// gtk_window_set_resizable(GTK_WINDOW(pcontext.window), FALSE);
gtk_window_set_transient_for(GTK_WINDOW(pcontext.window),
GTK_WINDOW(mcontext->appdata->window));
@@ -278,34 +401,91 @@
gtk_grab_add(pcontext.window);
/* check whether cache is in upper or lower half of window */
- gint x, y;
+ gint x, y, sx, sy;
osm_gps_map_geographic_to_screen(OSM_GPS_MAP(mcontext->widget),
cache->pos.lat, cache->pos.lon,
- &x, &y);
+ &sx, &sy);
- printf("screen pos %d/%d\n", 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);
+ gint ax = 0, ay = 0;
+ if(sx > mcontext->widget->allocation.width/2)
+ ax = POPUP_WIDTH;
+
+ if(sy > mcontext->widget->allocation.height/2)
+ ay = POPUP_HEIGHT;
+
+#if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5)
+ GdkColor color;
+ gdk_color_parse("darkgray", &color);
+ gtk_widget_modify_bg(GTK_WIDGET(pcontext.window), GTK_STATE_NORMAL, &color);
+#endif
gtk_window_move(GTK_WINDOW(pcontext.window),
- 100,
- 100);
+ x + mcontext->widget->allocation.x + sx - ax,
+ y + mcontext->widget->allocation.y + sy - ay);
+
- /* a frame with a vscale inside */
- GtkWidget *frame = gtk_frame_new(NULL);
- gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT);
+ GtkWidget *alignment = gtk_alignment_new(0.5, 0.5, 1.0, 1.0);
+ gtk_alignment_set_padding(GTK_ALIGNMENT(alignment),
+ CORNER_RADIUS/2 + (ay?0:ARROW_BORDER),
+ CORNER_RADIUS/2 + (ay?ARROW_BORDER:0),
+ CORNER_RADIUS, CORNER_RADIUS);
- gtk_container_add(GTK_CONTAINER(frame), gtk_label_new(cache->name));
- gtk_container_add(GTK_CONTAINER(pcontext.window), frame);
+ /* --- actual content ---- */
+ GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
+
+ if(cache->id) {
+ GtkWidget *ihbox = gtk_hbox_new(FALSE, 0);
+
+ gtk_box_pack_start(GTK_BOX(ihbox),
+ icon_get_widget(ICON_CACHE_TYPE, cache->type),
+ FALSE, FALSE, 5);
+
+ gtk_box_pack_start_defaults(GTK_BOX(ihbox),
+ gtk_label_big_left_new(cache->id));
+
+ gtk_box_pack_start_defaults(GTK_BOX(vbox), ihbox);
+ }
+
+ if(cache->name) {
+ GtkWidget *label = gtk_label_small_left_new(cache->name);
+ gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_END);
+ gtk_box_pack_start_defaults(GTK_BOX(vbox), label);
+ }
+
+ GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
+ if(cache->terrain) {
+ GtkWidget *ihbox = gtk_hbox_new(FALSE, 0);
+ gtk_box_pack_start(GTK_BOX(ihbox),
+ gtk_label_small_left_new(_("Terrain:")), FALSE, FALSE, 0);
+ gtk_box_pack_start(GTK_BOX(ihbox),
+ icon_get_widget(ICON_STARS, (int)(cache->terrain*2-2)),
+ FALSE, FALSE, 5);
+ gtk_box_pack_start_defaults(GTK_BOX(hbox), ihbox);
+ }
+
+ if(cache->difficulty) {
+ GtkWidget *ihbox = gtk_hbox_new(FALSE, 0);
+ gtk_box_pack_start(GTK_BOX(ihbox),
+ gtk_label_small_left_new(_("Difficulty:")), FALSE, FALSE, 0);
+ gtk_box_pack_start(GTK_BOX(ihbox),
+ icon_get_widget(ICON_STARS, (int)(cache->difficulty*2-2)),
+ FALSE, FALSE, 5);
+ gtk_box_pack_start_defaults(GTK_BOX(hbox), ihbox);
+ }
+
+ gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
+
+ gtk_container_add(GTK_CONTAINER(alignment), vbox);
+ /* ----------------------- */
+
+
+ gtk_container_add(GTK_CONTAINER(pcontext.window), alignment);
+ /* give window its shape */
+ popup_window_shape(pcontext.window, ax, ay);
+
gtk_widget_show_all(pcontext.window);
/* handle this popup until it's gone */
@@ -370,7 +550,7 @@
}
static int dist2pixel(map_context_t *context, float km, float lat) {
- int zoom;
+ gint zoom;
g_object_get(OSM_GPS_MAP(context->widget), "zoom", &zoom, NULL);
/* world at zoom 1 == 512 pixels */
@@ -380,7 +560,7 @@
return 1000.0*km/m_per_pix;
}
-#define CLICK_FUZZ (16)
+#define CLICK_FUZZ (24)
static gboolean
on_map_button_press_event(GtkWidget *widget,
@@ -409,9 +589,9 @@
static gboolean
on_map_button_release_event(GtkWidget *widget,
GdkEventButton *event, map_context_t *context) {
- if(context->press_on) {
- OsmGpsMap *map = OSM_GPS_MAP(context->widget);
+ OsmGpsMap *map = OSM_GPS_MAP(context->widget);
+ if(context->press_on) {
pos_t pos =
coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y));
@@ -422,16 +602,36 @@
cache_popup(context, nearest);
}
context->press_on = NULL;
+ } else {
+ /* save new map position */
+ gfloat lat, lon;
+ g_object_get(map, "latitude", &lat, "longitude", &lon, NULL);
+ context->appdata->map.pos.lat = lat;
+ context->appdata->map.pos.lon = lon;
}
return FALSE;
}
+static void save_map_state(map_context_t *context) {
+ /* save map parameters */
+ OsmGpsMap *map = OSM_GPS_MAP(context->widget);
+ gint zoom;
+ g_object_get(map, "zoom", &zoom, NULL);
+ context->appdata->map.zoom = zoom;
+
+ gfloat lat, lon;
+ g_object_get(map, "latitude", &lat, "longitude", &lon, NULL);
+ context->appdata->map.pos.lat = lat;
+ context->appdata->map.pos.lon = lon;
+}
#if MAEMO_VERSION_MAJOR == 5
static void on_window_destroy(GtkWidget *widget, map_context_t *context) {
printf("destroy map view\n");
+ save_map_state(context);
+
/* restore cur_view */
context->appdata->cur_view = context->old_view;
@@ -489,7 +689,7 @@
NULL);
#ifndef USE_MAEMO
- gtk_window_set_default_size(GTK_WINDOW(dialog), 400, 350);
+ gtk_window_set_default_size(GTK_WINDOW(dialog), 640, 480);
#else
gtk_window_set_default_size(GTK_WINDOW(dialog), 800, 480);
#endif
@@ -546,6 +746,7 @@
gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE);
gtk_widget_show_all(dialog);
gtk_dialog_run(GTK_DIALOG(dialog));
+ save_map_state(context);
gtk_timeout_remove(context->handler_id);
gtk_widget_destroy(dialog);
g_free(context);