Parent Directory | Revision Log
Display caches on internal map
1 | /* |
2 | * Copyright (C) 2008 Till Harbaum <till@harbaum.org>. |
3 | * |
4 | * This file is part of GPXView. |
5 | * |
6 | * GPXView is free software: you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation, either version 3 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * GPXView is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
17 | * along with GPXView. If not, see <http://www.gnu.org/licenses/>. |
18 | */ |
19 | |
20 | #include "gpxview.h" |
21 | #include <math.h> // for isnan |
22 | |
23 | #ifdef ENABLE_OSM_GPS_MAP |
24 | #include "osm-gps-map.h" |
25 | #endif |
26 | |
27 | typedef struct { |
28 | appdata_t *appdata; |
29 | GtkWidget *widget; |
30 | GtkWidget *zoomin, *zoomout, *gps; |
31 | gint handler_id; |
32 | } map_context_t; |
33 | |
34 | #define PROXY_KEY "/system/http_proxy/" |
35 | |
36 | static const char *get_proxy_uri(appdata_t *appdata) { |
37 | static char proxy_buffer[64] = ""; |
38 | |
39 | /* use environment settings if preset */ |
40 | const char *proxy = g_getenv("http_proxy"); |
41 | if(proxy) { |
42 | printf("http_proxy: %s\n", proxy); |
43 | return proxy; |
44 | } |
45 | |
46 | /* ------------- get proxy settings -------------------- */ |
47 | if(gconf_client_get_bool(appdata->gconf_client, |
48 | PROXY_KEY "use_http_proxy", NULL)) { |
49 | |
50 | /* we can savely ignore things like "ignore_hosts" since we */ |
51 | /* are pretty sure not inside the net of one of our map renderers */ |
52 | /* (unless the user works at google :-) */ |
53 | |
54 | /* get basic settings */ |
55 | char *host = |
56 | gconf_client_get_string(appdata->gconf_client, PROXY_KEY "host", NULL); |
57 | if(host) { |
58 | int port = |
59 | gconf_client_get_int(appdata->gconf_client, PROXY_KEY "port", NULL); |
60 | |
61 | snprintf(proxy_buffer, sizeof(proxy_buffer), |
62 | "http://%s:%u", host, port); |
63 | |
64 | g_free(host); |
65 | } |
66 | return proxy_buffer; |
67 | } |
68 | |
69 | return NULL; |
70 | } |
71 | |
72 | static void map_zoom(map_context_t *context, int step) { |
73 | int zoom; |
74 | OsmGpsMap *map = OSM_GPS_MAP(context->widget); |
75 | g_object_get(map, "zoom", &zoom, NULL); |
76 | zoom = osm_gps_map_set_zoom(map, zoom+step); |
77 | |
78 | /* enable/disable zoom buttons as required */ |
79 | gtk_widget_set_sensitive(context->zoomin, zoom<17); |
80 | gtk_widget_set_sensitive(context->zoomout, zoom>1); |
81 | } |
82 | |
83 | static gboolean |
84 | cb_map_zoomin(GtkButton *button, map_context_t *context) { |
85 | map_zoom(context, +1); |
86 | return FALSE; |
87 | } |
88 | |
89 | static gboolean |
90 | cb_map_zoomout(GtkButton *button, map_context_t *context) { |
91 | map_zoom(context, -1); |
92 | return FALSE; |
93 | } |
94 | |
95 | static gboolean |
96 | cb_map_gps(GtkButton *button, map_context_t *context) { |
97 | pos_t *refpos = get_pos(context->appdata); |
98 | if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) { |
99 | osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget), |
100 | refpos->lat, refpos->lon, 14); |
101 | } else { |
102 | /* no coordinates given: display the entire world */ |
103 | osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget), |
104 | 0.0, 0.0, 1); |
105 | } |
106 | |
107 | return FALSE; |
108 | } |
109 | |
110 | static GtkWidget |
111 | *map_add_button(const gchar *icon, GCallback cb, gpointer data, |
112 | char *tooltip) { |
113 | GtkWidget *button = gtk_button_new(); |
114 | gtk_button_set_image(GTK_BUTTON(button), |
115 | gtk_image_new_from_stock(icon, GTK_ICON_SIZE_MENU)); |
116 | g_signal_connect(button, "clicked", cb, data); |
117 | #ifndef USE_MAEMO |
118 | gtk_widget_set_tooltip_text(button, tooltip); |
119 | #endif |
120 | return button; |
121 | } |
122 | |
123 | static gboolean map_gps_update(gpointer data) { |
124 | map_context_t *context = (map_context_t*)data; |
125 | |
126 | pos_t *refpos = get_pos(context->appdata); |
127 | gboolean ok = (refpos!= NULL) && !isnan(refpos->lat) && !isnan(refpos->lon); |
128 | |
129 | /* get reference position and go there */ |
130 | gtk_widget_set_sensitive(context->gps, ok); |
131 | |
132 | return TRUE; |
133 | } |
134 | |
135 | static gboolean on_map_configure(GtkWidget *widget, |
136 | GdkEventConfigure *event, |
137 | map_context_t *context) { |
138 | |
139 | cb_map_gps(NULL, context); |
140 | |
141 | return FALSE; |
142 | } |
143 | |
144 | static void map_draw_cachelist(GtkWidget *map, cache_t *cache) { |
145 | while(cache) { |
146 | GdkPixbuf *icon = icon_get(ICON_CACHE_TYPE, cache->type); |
147 | |
148 | osm_gps_map_add_image(OSM_GPS_MAP(map), |
149 | cache->pos.lat, cache->pos.lon, icon); |
150 | |
151 | cache = cache->next; |
152 | } |
153 | } |
154 | |
155 | void map(appdata_t *appdata) { |
156 | map_context_t context; |
157 | context.appdata = appdata; |
158 | |
159 | GtkWidget *dialog = gtk_dialog_new_with_buttons(_("Map"), |
160 | GTK_WINDOW(appdata->window), |
161 | GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, |
162 | GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, |
163 | NULL); |
164 | |
165 | #ifndef USE_MAEMO |
166 | gtk_window_set_default_size(GTK_WINDOW(dialog), 400, 350); |
167 | #else |
168 | gtk_window_set_default_size(GTK_WINDOW(dialog), 800, 480); |
169 | #endif |
170 | |
171 | GtkWidget *hbox = gtk_hbox_new(FALSE, 0); |
172 | |
173 | char *path = g_strdup_printf("%s/map/", appdata->image_path); |
174 | const char *proxy = get_proxy_uri(appdata); |
175 | |
176 | context.widget = g_object_new(OSM_TYPE_GPS_MAP, |
177 | "repo-uri", MAP_SOURCE_OPENSTREETMAP, |
178 | "tile-cache", path, |
179 | proxy?"proxy-uri":NULL, proxy, |
180 | NULL); |
181 | |
182 | g_free(path); |
183 | |
184 | /* draw all geocaches */ |
185 | gpx_t *gpx = appdata->gpx; |
186 | while(gpx) { |
187 | map_draw_cachelist(context.widget, gpx->cache); |
188 | gpx = gpx->next; |
189 | } |
190 | |
191 | g_signal_connect(G_OBJECT(context.widget), "configure-event", |
192 | G_CALLBACK(on_map_configure), &context); |
193 | #if 0 |
194 | g_signal_connect(G_OBJECT(context.widget), "button-release-event", |
195 | G_CALLBACK(on_map_button_release_event), &context); |
196 | #endif |
197 | |
198 | gtk_box_pack_start_defaults(GTK_BOX(hbox), context.widget); |
199 | /* zoom button box */ |
200 | GtkWidget *vbox = gtk_vbox_new(FALSE,0); |
201 | |
202 | context.zoomin = |
203 | map_add_button(GTK_STOCK_ZOOM_IN, G_CALLBACK(cb_map_zoomin), |
204 | &context, _("Zoom in")); |
205 | gtk_box_pack_start(GTK_BOX(vbox), context.zoomin, FALSE, FALSE, 0); |
206 | |
207 | context.zoomout = |
208 | map_add_button(GTK_STOCK_ZOOM_OUT, G_CALLBACK(cb_map_zoomout), |
209 | &context, _("Zoom out")); |
210 | gtk_box_pack_start(GTK_BOX(vbox), context.zoomout, FALSE, FALSE, 0); |
211 | |
212 | context.gps = |
213 | map_add_button(GTK_STOCK_HOME, G_CALLBACK(cb_map_gps), |
214 | &context, _("Jump to GPS position")); |
215 | gtk_widget_set_sensitive(context.gps, FALSE); |
216 | /* install handler for timed updates of the gps button */ |
217 | context.handler_id = gtk_timeout_add(1000, map_gps_update, &context); |
218 | gtk_box_pack_start(GTK_BOX(vbox), context.gps, FALSE, FALSE, 0); |
219 | |
220 | gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0); |
221 | |
222 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox); |
223 | |
224 | gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE); |
225 | |
226 | gtk_widget_show_all(dialog); |
227 | |
228 | gtk_dialog_run(GTK_DIALOG(dialog)); |
229 | |
230 | gtk_timeout_remove(context.handler_id); |
231 | |
232 | gtk_widget_destroy(dialog); |
233 | } |