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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 34 - (hide annotations)
Wed Jul 29 19:24:15 2009 UTC (14 years, 9 months ago) by harbaum
File MIME type: text/plain
File size: 5956 byte(s)
Invalid positions marked by NANs
1 harbaum 33 /*
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 harbaum 34 #include <math.h> // for isnan
22 harbaum 33
23     #ifdef ENABLE_OSM_GPS_MAP
24     #include "osm-gps-map.h"
25     #endif
26    
27     typedef struct {
28 harbaum 34 appdata_t *appdata;
29 harbaum 33 GtkWidget *widget;
30     GtkWidget *zoomin, *zoomout, *gps;
31     gint handler_id;
32     } map_context_t;
33    
34 harbaum 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 harbaum 33
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 harbaum 34 /* ------------- get proxy settings -------------------- */
47     if(gconf_client_get_bool(appdata->gconf_client,
48     PROXY_KEY "use_http_proxy", NULL)) {
49 harbaum 33
50 harbaum 34 /* 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 harbaum 33
61 harbaum 34 snprintf(proxy_buffer, sizeof(proxy_buffer),
62     "http://%s:%u", host, port);
63 harbaum 33
64 harbaum 34 g_free(host);
65     }
66     }
67    
68 harbaum 33 return proxy_buffer;
69     }
70    
71     static void map_zoom(map_context_t *context, int step) {
72     int zoom;
73     OsmGpsMap *map = OSM_GPS_MAP(context->widget);
74     g_object_get(map, "zoom", &zoom, NULL);
75     zoom = osm_gps_map_set_zoom(map, zoom+step);
76    
77     /* enable/disable zoom buttons as required */
78     gtk_widget_set_sensitive(context->zoomin, zoom<17);
79     gtk_widget_set_sensitive(context->zoomout, zoom>1);
80     }
81    
82     static gboolean
83     cb_map_zoomin(GtkButton *button, map_context_t *context) {
84     map_zoom(context, +1);
85     return FALSE;
86     }
87    
88     static gboolean
89     cb_map_zoomout(GtkButton *button, map_context_t *context) {
90     map_zoom(context, -1);
91     return FALSE;
92     }
93    
94 harbaum 34 #define DEG2RAD(a) ((a) * M_PI / 180.0)
95    
96 harbaum 33 static gboolean
97     cb_map_gps(GtkButton *button, map_context_t *context) {
98 harbaum 34 pos_t *refpos = get_pos(context->appdata);
99     if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) {
100     printf("pos is %f %f\n", refpos->lat, refpos->lon);
101 harbaum 33
102 harbaum 34 osm_gps_map_set_center(OSM_GPS_MAP(context->widget),
103     DEG2RAD(refpos->lat), DEG2RAD(refpos->lon));
104     }
105 harbaum 33
106     return FALSE;
107     }
108    
109     static GtkWidget
110     *map_add_button(const gchar *icon, GCallback cb, gpointer data,
111     char *tooltip) {
112     GtkWidget *button = gtk_button_new();
113     gtk_button_set_image(GTK_BUTTON(button),
114     gtk_image_new_from_stock(icon, GTK_ICON_SIZE_MENU));
115     g_signal_connect(button, "clicked", cb, data);
116     #ifndef USE_MAEMO
117     gtk_widget_set_tooltip_text(button, tooltip);
118     #endif
119     return button;
120     }
121    
122     static gboolean map_gps_update(gpointer data) {
123     map_context_t *context = (map_context_t*)data;
124    
125 harbaum 34 pos_t *refpos = get_pos(context->appdata);
126     gboolean ok = (refpos!= NULL) && !isnan(refpos->lat) && !isnan(refpos->lon);
127 harbaum 33
128 harbaum 34 /* get reference position and go there */
129     gtk_widget_set_sensitive(context->gps, ok);
130    
131 harbaum 33 return TRUE;
132     }
133    
134    
135     void map(appdata_t *appdata) {
136     map_context_t context;
137 harbaum 34 context.appdata = appdata;
138 harbaum 33
139     GtkWidget *dialog = gtk_dialog_new_with_buttons(_("Map"),
140     GTK_WINDOW(appdata->window),
141     GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
142     GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
143     NULL);
144    
145     #ifndef USE_MAEMO
146     gtk_window_set_default_size(GTK_WINDOW(dialog), 400, 350);
147     #else
148     gtk_window_set_default_size(GTK_WINDOW(dialog), 800, 480);
149     #endif
150    
151     GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
152    
153     char *path = g_strdup_printf("%s/map/", appdata->image_path);
154    
155     context.widget = g_object_new(OSM_TYPE_GPS_MAP,
156     "repo-uri", MAP_SOURCE_OPENSTREETMAP,
157 harbaum 34 "proxy-uri", get_proxy_uri(appdata),
158 harbaum 33 "tile-cache", path,
159     NULL);
160    
161     g_free(path);
162    
163     #if 0
164     g_signal_connect(G_OBJECT(context.widget), "button-release-event",
165     G_CALLBACK(on_map_button_release_event), &context);
166     #endif
167    
168     gtk_box_pack_start_defaults(GTK_BOX(hbox), context.widget);
169     /* zoom button box */
170     GtkWidget *vbox = gtk_vbox_new(FALSE,0);
171    
172     context.zoomin =
173     map_add_button(GTK_STOCK_ZOOM_IN, G_CALLBACK(cb_map_zoomin),
174     &context, _("Zoom in"));
175     gtk_box_pack_start(GTK_BOX(vbox), context.zoomin, FALSE, FALSE, 0);
176    
177     context.zoomout =
178     map_add_button(GTK_STOCK_ZOOM_OUT, G_CALLBACK(cb_map_zoomout),
179     &context, _("Zoom out"));
180     gtk_box_pack_start(GTK_BOX(vbox), context.zoomout, FALSE, FALSE, 0);
181    
182     context.gps =
183     map_add_button(GTK_STOCK_HOME, G_CALLBACK(cb_map_gps),
184     &context, _("Jump to GPS position"));
185     gtk_widget_set_sensitive(context.gps, FALSE);
186     /* install handler for timed updates of the gps button */
187     context.handler_id = gtk_timeout_add(1000, map_gps_update, &context);
188     gtk_box_pack_start(GTK_BOX(vbox), context.gps, FALSE, FALSE, 0);
189    
190     gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
191    
192     gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox);
193    
194     gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE);
195    
196     gtk_widget_show_all(dialog);
197    
198     gtk_dialog_run(GTK_DIALOG(dialog));
199    
200     gtk_widget_destroy(dialog);
201    
202     }