Parent Directory | Revision Log
Basic map working
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 | void map(appdata_t *appdata) { |
145 | map_context_t context; |
146 | context.appdata = appdata; |
147 | |
148 | GtkWidget *dialog = gtk_dialog_new_with_buttons(_("Map"), |
149 | GTK_WINDOW(appdata->window), |
150 | GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, |
151 | GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, |
152 | NULL); |
153 | |
154 | #ifndef USE_MAEMO |
155 | gtk_window_set_default_size(GTK_WINDOW(dialog), 400, 350); |
156 | #else |
157 | gtk_window_set_default_size(GTK_WINDOW(dialog), 800, 480); |
158 | #endif |
159 | |
160 | GtkWidget *hbox = gtk_hbox_new(FALSE, 0); |
161 | |
162 | char *path = g_strdup_printf("%s/map/", appdata->image_path); |
163 | const char *proxy = get_proxy_uri(appdata); |
164 | |
165 | context.widget = g_object_new(OSM_TYPE_GPS_MAP, |
166 | "repo-uri", MAP_SOURCE_OPENSTREETMAP, |
167 | "tile-cache", path, |
168 | proxy?"proxy-uri":NULL, proxy, |
169 | NULL); |
170 | |
171 | g_free(path); |
172 | |
173 | g_signal_connect(G_OBJECT(context.widget), "configure-event", |
174 | G_CALLBACK(on_map_configure), &context); |
175 | #if 0 |
176 | g_signal_connect(G_OBJECT(context.widget), "button-release-event", |
177 | G_CALLBACK(on_map_button_release_event), &context); |
178 | #endif |
179 | |
180 | gtk_box_pack_start_defaults(GTK_BOX(hbox), context.widget); |
181 | /* zoom button box */ |
182 | GtkWidget *vbox = gtk_vbox_new(FALSE,0); |
183 | |
184 | context.zoomin = |
185 | map_add_button(GTK_STOCK_ZOOM_IN, G_CALLBACK(cb_map_zoomin), |
186 | &context, _("Zoom in")); |
187 | gtk_box_pack_start(GTK_BOX(vbox), context.zoomin, FALSE, FALSE, 0); |
188 | |
189 | context.zoomout = |
190 | map_add_button(GTK_STOCK_ZOOM_OUT, G_CALLBACK(cb_map_zoomout), |
191 | &context, _("Zoom out")); |
192 | gtk_box_pack_start(GTK_BOX(vbox), context.zoomout, FALSE, FALSE, 0); |
193 | |
194 | context.gps = |
195 | map_add_button(GTK_STOCK_HOME, G_CALLBACK(cb_map_gps), |
196 | &context, _("Jump to GPS position")); |
197 | gtk_widget_set_sensitive(context.gps, FALSE); |
198 | /* install handler for timed updates of the gps button */ |
199 | context.handler_id = gtk_timeout_add(1000, map_gps_update, &context); |
200 | gtk_box_pack_start(GTK_BOX(vbox), context.gps, FALSE, FALSE, 0); |
201 | |
202 | gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0); |
203 | |
204 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox); |
205 | |
206 | gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE); |
207 | |
208 | gtk_widget_show_all(dialog); |
209 | |
210 | gtk_dialog_run(GTK_DIALOG(dialog)); |
211 | |
212 | gtk_timeout_remove(context.handler_id); |
213 | |
214 | gtk_widget_destroy(dialog); |
215 | } |