2 * This file is part of mapper
4 * Copyright (C) 2007 Kaj-Michael Lang
5 * Copyright (C) 2006-2007 John Costigan.
7 * POI and GPS-Info code originally written by Cezary Jackiewicz.
9 * Default map data provided by http://www.openstreetmap.org/
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
36 #include <glib/gstdio.h>
42 #include "hildon-mapper.h"
53 #include "mapper-types.h"
54 #include "ui-common.h"
60 #include "map-download.h"
61 #include "announcements.h"
62 #include "gtkcompass.h"
64 #include "image-cache.h"
66 #define DEBUG_MAP_TIME 1
68 #define MAP_THUMB_MARGIN_X (100)
69 #define MAP_THUMB_MARGIN_Y (75)
72 #define BUF_WIDTH_TILES (4)
73 #define BUF_HEIGHT_TILES (3)
74 #define BUF_WIDTH_PIXELS (1024)
75 #define BUF_HEIGHT_PIXELS (768)
77 static guint buf_width_tiles=BUF_WIDTH_TILES;
78 static guint buf_height_tiles=BUF_HEIGHT_TILES;
79 static guint buf_width_pixels=BUF_WIDTH_PIXELS;
80 static guint buf_height_pixels=BUF_HEIGHT_PIXELS;
84 #include <gtk/gtkgl.h>
85 GdkGLConfig* map_gl_config=NULL;
87 gboolean map_gl=FALSE;
93 /** The backing pixmap of map_widget. */
94 static GdkPixmap *map_pixmap;
96 /* Tile cache, this might need some adjustment */
97 #define MAP_CACHE_MAX (64)
98 static ImageCache *map_ic;
100 /** The "base tile" is the upper-left tile in the pixmap. */
101 guint _base_tilex = -5;
102 guint _base_tiley = -5;
104 static guint map_max_zoom=MAX_ZOOM;
105 guint _zoom = 3; /* zoom level, from 0 to MAX_ZOOM. */
107 Point _min_center = { -1, -1 };
108 Point _max_center = { -1, -1 };
109 Point _focus = { -1, -1 };
110 /* current center location, X. */
111 Point _center = { -1, -1 };
113 CenterMode _center_mode = CENTER_LEAD;
116 static guint press[2] = { 0, 0 };
117 static guint release[2] = { 0, 0 };
118 static guint before[2] = { 0, 0 };
119 static guint map_drag_id = 0;
120 static gboolean map_dragged;
122 /** VARIABLES FOR ACCESSING THE LOCATION/BOUNDS OF THE CURRENT MARK. */
127 static gint mark_minx;
128 static gint mark_miny;
129 static gint mark_width;
130 static gint mark_height;
132 static GdkRectangle scale_rect;
133 static PangoContext *scale_context;
134 static PangoFontDescription *scale_font;
135 static PangoLayout *scale_layout;
137 static GdkGC *speed_gc1;
138 static GdkGC *speed_gc2;
139 static PangoContext *speed_context;
140 static PangoLayout *speed_layout;
141 static PangoFontDescription *speed_fontdesc;
143 static gint zoom_timeout_sid=0;
144 static gint map_mode=0;
145 static gboolean map_data_needs_refresh=FALSE;
147 osm_location map_loc = {NULL, NULL, NULL, FALSE, FALSE, 0, 0, 0.0, 0.0, 0 };
149 static GTimer *map_timer;
151 /* Tile max age, 1 week */
152 #define TILE_MAX_AGE (604800)
154 #define KM10KNOTS (5.39956803)
156 static void map_update_location(gdouble lat, gdouble lon, gboolean force);
157 static void map_speed_draw(void);
158 static gboolean map_cb_after_realize(GtkWidget *map_widget, gpointer data);
159 static gboolean map_cb_configure(GtkWidget *widget, GdkEventConfigure *event);
160 static gboolean map_cb_expose(GtkWidget * widget, GdkEventExpose * event);
161 static gboolean map_cb_button_press(GtkWidget * widget, GdkEventButton * event);
162 static gboolean map_cb_button_release(GtkWidget * widget, GdkEventButton * event);
163 static gboolean map_cb_scroll_event(GtkWidget * widget, GdkEventScroll * event);
165 /******************************************************************************/
170 GtkWidget *map_widget;
172 map_widget=gtk_drawing_area_new();
173 map_timer=g_timer_new();
175 map_ic=image_cache_new(MAP_CACHE_MAX);
176 g_debug("World size: %u", WORLD_SIZE_UNITS);
179 map_gl_config=gdk_gl_config_new_by_mode(GDK_GL_MODE_RGB | GDK_GL_MODE_DEPTH);
181 g_print("OpenGL version: %s\n", glGetString (GL_VERSION));
182 g_print("OpenGL vendor: %s\n", glGetString (GL_VENDOR));
183 g_print("OpenGL renderer: %s\n", glGetString (GL_RENDERER));
184 gtk_widget_set_gl_capability(map_widget, map_gl_config, NULL, TRUE, GDK_GL_RGBA_TYPE);
189 g_signal_connect_after(G_OBJECT(map_widget), "realize", G_CALLBACK(map_cb_after_realize), NULL);
190 g_signal_connect(G_OBJECT(map_widget), "configure_event", G_CALLBACK(map_cb_configure), NULL);
196 map_cb_after_realize(GtkWidget *map_widget, gpointer data)
200 g_debug("MAP: after_realize");
202 scale_context=gtk_widget_get_pango_context(map_widget);
203 scale_layout=pango_layout_new(scale_context);
204 scale_font=pango_font_description_new();
205 pango_font_description_set_size(scale_font, 12 * PANGO_SCALE);
206 pango_layout_set_font_description(scale_layout, scale_font);
208 /* Speed limit, over limit color */
209 speed_gc1=gdk_gc_new(map_widget->window);
213 gdk_gc_set_rgb_fg_color(speed_gc1, &color);
215 /* Speed limit, under limit color */
216 speed_gc2=gdk_gc_new(map_widget->window);
220 gdk_gc_set_rgb_fg_color(speed_gc2, &color);
222 speed_context=gtk_widget_get_pango_context(map_widget);
223 speed_layout=pango_layout_new(speed_context);
224 speed_fontdesc=pango_font_description_new();
225 pango_font_description_set_size(speed_fontdesc, 48 * PANGO_SCALE);
226 pango_layout_set_font_description(speed_layout, speed_fontdesc);
227 pango_layout_set_alignment(speed_layout, PANGO_ALIGN_CENTER);
230 g_signal_connect(G_OBJECT(map_widget), "expose_event", G_CALLBACK(map_cb_expose), NULL);
231 g_signal_connect(G_OBJECT(map_widget), "button_press_event", G_CALLBACK(map_cb_button_press), NULL);
232 g_signal_connect(G_OBJECT(map_widget), "button_release_event",G_CALLBACK(map_cb_button_release), NULL);
233 g_signal_connect(G_OBJECT(map_widget), "scroll_event", G_CALLBACK(map_cb_scroll_event), NULL);
235 gtk_widget_add_events(map_widget, GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
236 | GDK_LEAVE_NOTIFY_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK);
238 gtk_widget_set_extension_events(GTK_WIDGET(map_widget), GDK_EXTENSION_EVENTS_ALL);
241 map_poi_init(map_widget);
247 map_cb_configure(GtkWidget *widget, GdkEventConfigure *event)
251 tw=TILE_SIZE_PIXELS*((widget->allocation.width/TILE_SIZE_PIXELS)+2);
252 th=TILE_SIZE_PIXELS*((widget->allocation.height/TILE_SIZE_PIXELS)+2);
254 if (map_pixmap==NULL) {
255 map_pixmap=gdk_pixmap_new(widget->window, tw, th, -1);
257 if (tw>buf_width_pixels || th>buf_height_pixels) {
258 g_object_unref(map_pixmap);
259 map_pixmap=gdk_pixmap_new(widget->window, tw, th, -1);
260 } else if (tw<buf_width_pixels-(TILE_SIZE_PIXELS*2) || th<buf_height_pixels-(TILE_SIZE_PIXELS*2)) {
261 g_object_unref(map_pixmap);
262 map_pixmap=gdk_pixmap_new(widget->window, tw, th, -1);
265 g_assert(map_pixmap);
268 buf_height_pixels=th;
269 buf_width_tiles=buf_width_pixels/TILE_SIZE_PIXELS;
270 buf_height_tiles=buf_height_pixels/TILE_SIZE_PIXELS;
272 _screen_width_pixels = widget->allocation.width;
273 _screen_height_pixels = widget->allocation.height;
274 _screen_grids_halfwidth = pixel2grid(_screen_width_pixels) / 2;
275 _screen_grids_halfheight = pixel2grid(_screen_height_pixels) / 2;
277 /* Set scale_rect. */
278 scale_rect.x = (_screen_width_pixels - SCALE_WIDTH) / 2;
279 scale_rect.width = SCALE_WIDTH;
281 MACRO_RECALC_FOCUS_BASE(_center_ratio);
282 MACRO_RECALC_FOCUS_SIZE(_center_ratio);
284 _min_center.unitx = pixel2unit(grid2pixel(_screen_grids_halfwidth));
285 _min_center.unity = pixel2unit(grid2pixel(_screen_grids_halfheight));
286 _max_center.unitx = WORLD_SIZE_UNITS - grid2unit(_screen_grids_halfwidth) - 1;
287 _max_center.unity = WORLD_SIZE_UNITS - grid2unit(_screen_grids_halfheight) - 1;
294 * Draw the current mark (representing the current GPS location) onto
295 * _map_widget. This method does not queue the draw area.
298 map_draw_mark(Gps *gps)
300 gdk_draw_arc(_map_widget->window, gps->io.conn == RCVR_FIXED ? _gc[COLORABLE_MARK] : _gc[COLORABLE_MARK_OLD], FALSE,
301 mark_x1 - _draw_width, mark_y1 - _draw_width, 2 * _draw_width, 2 * _draw_width, 0, 360 * 64);
302 gdk_draw_line(_map_widget->window, gps->io.conn == RCVR_FIXED ? (_show_velvec ? _gc[COLORABLE_MARK_VELOCITY] : _gc[COLORABLE_MARK]) : _gc[COLORABLE_MARK_OLD],
303 mark_x1, mark_y1, mark_x2, mark_y2);
307 * "Set" the mark, which translates the current GPS position into on-screen
308 * units in preparation for drawing the mark with map_draw_mark().
311 map_set_mark(GpsData *gps)
313 mark_x1 = unit2x(gps->unitx);
314 mark_y1 = unit2y(gps->unity);
315 mark_x2 = mark_x1 + (_show_velvec ? gps->vel_offsetx : 0);
316 mark_y2 = mark_y1 + (_show_velvec ? gps->vel_offsety : 0);
317 mark_minx = MIN(mark_x1, mark_x2) - (2 * _draw_width);
318 mark_miny = MIN(mark_y1, mark_y2) - (2 * _draw_width);
319 mark_width = abs(mark_x1 - mark_x2) + (4 * _draw_width);
320 mark_height = abs(mark_y1 - mark_y2) + (4 * _draw_width);
324 * Do an in-place scaling of a pixbuf's pixels at the given ratio from the
325 * given source location. It would have been nice if gdk_pixbuf provided
326 * this method, but I guess it's not general-purpose enough.
329 map_pixbuf_scale_inplace(GdkPixbuf *pixbuf, guint ratio_p2, guint src_x, guint src_y)
331 guint dest_x = 0, dest_y = 0, dest_dim = TILE_SIZE_PIXELS;
332 guint rowstride = gdk_pixbuf_get_rowstride(pixbuf);
333 guint n_channels = gdk_pixbuf_get_n_channels(pixbuf);
334 guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
336 /* Sweep through the entire dest area, copying as necessary, but
337 * DO NOT OVERWRITE THE SOURCE AREA. We'll copy it afterward. */
339 guint src_dim = dest_dim >> ratio_p2;
340 guint src_endx = src_x - dest_x + src_dim;
343 for (y = dest_dim - 1; y >= 0; y--) {
344 guint src_offset_y, dest_offset_y;
346 src_offset_y = (src_y + (y >> ratio_p2)) * rowstride;
347 dest_offset_y = (dest_y + y) * rowstride;
350 if ((unsigned)(dest_y + y - src_y) < src_dim && (unsigned)(dest_x + x - src_x) < src_dim)
353 for (; x >= 0; x--) {
354 guint src_offset, dest_offset, i;
356 src_offset = src_offset_y + (src_x + (x >> ratio_p2)) * n_channels;
357 dest_offset = dest_offset_y + (dest_x + x) * n_channels;
359 pixels[dest_offset] = pixels[src_offset];
360 for (i = n_channels - 1; i; i--)
361 pixels[dest_offset + i] = pixels[src_offset + i];
363 if ((unsigned)(dest_y + y - src_y) < src_dim && x == src_endx)
368 /* Reuse src_dim and src_endx to store new src_x and src_y. */
369 src_dim = src_x + ((src_x - dest_x) >> ratio_p2);
370 src_endx = src_y + ((src_y - dest_y) >> ratio_p2);
376 while ((dest_dim >>= ratio_p2) > 1);
380 * Trim pixbufs that are bigger than tiles. (Those pixbufs result, when
381 * captions should be cut off.)
384 pixbuf_trim(GdkPixbuf * pixbuf)
386 GdkPixbuf *mpixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, gdk_pixbuf_get_has_alpha(pixbuf),
387 8, TILE_SIZE_PIXELS, TILE_SIZE_PIXELS);
390 gdk_pixbuf_copy_area(pixbuf,
391 (gdk_pixbuf_get_width(pixbuf) - TILE_SIZE_PIXELS) / 2,
392 (gdk_pixbuf_get_height(pixbuf) - TILE_SIZE_PIXELS) / 2,
394 TILE_SIZE_PIXELS, mpixbuf, 0, 0);
396 g_object_unref(pixbuf);
407 map_tile_load(guint tilex, guint tiley, gint zoff, gboolean download)
410 GError *error = NULL;
411 gchar buffer[BUFFER_SIZE];
412 gchar key[BUFFER_SIZE];
416 g_snprintf(buffer, sizeof(buffer), "%s/%u/%u/%u.jpg", _curr_repo->cache_dir, _zoom + zoff, (tilex >> zoff), (tiley >> zoff));
417 g_snprintf(key, sizeof(key), "%s/%u/%u/%u", _curr_repo->cache_dir, _zoom + zoff, (tilex >> zoff), (tiley >> zoff));
419 pixbuf=image_cache_get(map_ic, key, buffer);
423 if (_auto_download && _curr_repo->type != REPOTYPE_NONE && !((_zoom + zoff - (_curr_repo->double_size ? 1 : 0)) % _curr_repo->dl_zoom_steps)) {
425 map_initiate_download(tilex >> zoff, tiley >> zoff, _zoom + zoff, -INITIAL_DOWNLOAD_RETRIES);
430 g_object_ref(pixbuf);
433 /* Check if we need to trim. */
434 if (gdk_pixbuf_get_width(pixbuf) != TILE_SIZE_PIXELS || gdk_pixbuf_get_height(pixbuf) != TILE_SIZE_PIXELS)
435 pixbuf=pixbuf_trim(pixbuf);
438 /* Check tile age, if file date is ower a week old, redownload if autodownload enabled */
439 se=stat(buffer, &tstat);
443 if (t-tstat.st_mtime>TILE_MAX_AGE) {
444 if (_auto_download && _curr_repo->type != REPOTYPE_NONE && !((_zoom + zoff - (_curr_repo->double_size ? 1 : 0)) % _curr_repo->dl_zoom_steps)) {
446 g_debug("Tile: %s is old, re-downloading\n", buffer);
447 map_initiate_download(tilex >> zoff, tiley >> zoff, _zoom + zoff, -INITIAL_DOWNLOAD_RETRIES);
448 image_cache_invalidate(map_ic, key);
458 map_render_tile(guint tilex, guint tiley, guint destx, guint desty, gboolean fast_fail)
460 GdkPixbuf *pixbuf=NULL;
463 if (destx > buf_width_pixels || desty > buf_height_pixels)
466 if (tilex > _world_size_tiles || tiley > _world_size_tiles)
469 g_debug("MAP RT: %u %u (%u) %u %u (%u, %u)", tilex, tiley, _world_size_tiles, destx, desty, buf_width_tiles, buf_height_tiles);
471 /* The tile is possible. */
472 for (zoff = (_curr_repo->double_size ? 1 : 0); !pixbuf && (_zoom + zoff) <= map_max_zoom && zoff <= TILE_SIZE_P2; zoff += 1) {
473 pixbuf=map_tile_load(tilex, tiley, zoff, !fast_fail);
478 /* Check if we need to blit. */
480 map_pixbuf_scale_inplace(pixbuf, zoff,
481 (tilex - ((tilex >> zoff) << zoff)) << (TILE_SIZE_P2 - zoff),
482 (tiley - ((tiley >> zoff) << zoff)) << (TILE_SIZE_P2 - zoff));
483 image_cache_invalidate_by_image(map_ic, pixbuf);
489 gdk_draw_pixbuf(map_pixmap, _gc[COLORABLE_MARK], pixbuf, 0, 0, destx, desty, TILE_SIZE_PIXELS, TILE_SIZE_PIXELS, GDK_RGB_DITHER_NONE, 0, 0);
490 g_object_unref(pixbuf);
493 gdk_draw_rectangle(map_pixmap, _map_widget->style->black_gc, TRUE, destx, desty, TILE_SIZE_PIXELS, TILE_SIZE_PIXELS);
498 map_render_data(void)
500 /* Don't update if dragging, too much lag */
505 map_render_all_pois(buf_width_pixels, buf_height_pixels);
508 map_draw_position_icon(&_home, "home");
511 map_draw_position_icon(&_dest, "destination");
518 * Force a redraw of the entire map_pixmap, including fetching the
519 * background maps from disk and redrawing the tracks on top of them.
522 map_force_redraw(void)
528 g_timer_start(map_timer);
530 for (new_y = 0; new_y < buf_height_tiles; ++new_y)
531 for (new_x = 0; new_x < buf_width_tiles; ++new_x) {
532 map_render_tile(_base_tilex + new_x, _base_tiley + new_y,
533 new_x * TILE_SIZE_PIXELS, new_y * TILE_SIZE_PIXELS, FALSE);
536 g_timer_stop(map_timer);
537 g_debug("Full redraw: %f sec\n", g_timer_elapsed(map_timer, &tms));
541 MACRO_QUEUE_DRAW_AREA();
551 * Set the current zoom level. If the given zoom level is the same as the
552 * current zoom level, or if the new zoom is invalid
553 * (not MIN_ZOOM <= new_zoom < MAX_ZOOM), then this method does nothing.
556 map_set_zoom(guint new_zoom)
558 /* Note that, since new_zoom is a guint and MIN_ZOOM is 0, this if
559 * condition also checks for new_zoom >= MIN_ZOOM. */
560 if (new_zoom > (map_max_zoom - 1))
562 if (new_zoom == _zoom)
565 _zoom = new_zoom / _curr_repo->view_zoom_steps * _curr_repo->view_zoom_steps;
566 _world_size_tiles = unit2tile(WORLD_SIZE_UNITS);
568 /* If we're leading, update the center to reflect new zoom level. */
569 MACRO_RECALC_CENTER(_gps->data, _center.unitx, _center.unity);
571 /* Update center bounds to reflect new zoom level. */
572 _min_center.unitx = pixel2unit(grid2pixel(_screen_grids_halfwidth));
573 _min_center.unity = pixel2unit(grid2pixel(_screen_grids_halfheight));
574 _max_center.unitx = WORLD_SIZE_UNITS - grid2unit(_screen_grids_halfwidth) - 1;
575 _max_center.unity = WORLD_SIZE_UNITS - grid2unit(_screen_grids_halfheight) - 1;
577 BOUND(_center.unitx, _min_center.unitx, _max_center.unitx);
578 BOUND(_center.unity, _min_center.unity, _max_center.unity);
580 _base_tilex = grid2tile((gint) pixel2grid((gint) unit2pixel((gint) _center.unitx)) - (gint) _screen_grids_halfwidth);
581 _base_tiley = grid2tile(pixel2grid(unit2pixel(_center.unity)) - _screen_grids_halfheight);
583 /* New zoom level, so we can't reuse the old buffer's pixels. */
584 /* Update state variables. */
585 MACRO_RECALC_OFFSET();
586 MACRO_RECALC_FOCUS_BASE(_center_ratio);
587 MACRO_RECALC_FOCUS_SIZE(_center_ratio);
589 map_set_mark(&_gps->data);
595 * Center the view on the given unitx/unity.
598 map_center_unit(guint new_center_unitx, guint new_center_unity)
600 gint new_base_tilex, new_base_tiley;
602 guint j, k, base_new_x, base_old_x, old_x, old_y, iox, ioy;
604 /* Assure that _center.unitx/y are bounded. */
605 BOUND(new_center_unitx, _min_center.unitx, _max_center.unitx);
606 BOUND(new_center_unity, _min_center.unity, _max_center.unity);
608 _center.unitx = new_center_unitx;
609 _center.unity = new_center_unity;
611 new_base_tilex = grid2tile((gint) pixel2grid((gint)unit2pixel((gint) _center.unitx)) - (gint)_screen_grids_halfwidth);
612 new_base_tiley = grid2tile(pixel2grid(unit2pixel(_center.unity)) - _screen_grids_halfheight);
614 /* Same zoom level, so it's likely that we can reuse some of the old
615 * buffer's pixels. */
617 if (new_base_tilex != _base_tilex || new_base_tiley != _base_tiley) {
618 /* If copying from old parts to new parts, we need to make sure we
619 * don't overwrite the old parts when copying, so set up new_x,
620 * new_y, old_x, old_y, iox, and ioy with that in mind. */
621 if (new_base_tiley < _base_tiley) {
622 /* New is lower than old - start at bottom and go up. */
623 new_y = buf_height_tiles - 1;
626 /* New is higher than old - start at top and go down. */
630 if (new_base_tilex < _base_tilex) {
631 /* New is righter than old - start at right and go left. */
632 base_new_x = buf_width_tiles - 1;
635 /* New is lefter than old - start at left and go right. */
640 /* Iterate over the y tile values. */
641 old_y = new_y + new_base_tiley - _base_tiley;
642 base_old_x = base_new_x + new_base_tilex - _base_tilex;
643 _base_tilex = new_base_tilex;
644 _base_tiley = new_base_tiley;
646 for (j = 0; j < buf_height_tiles; ++j, new_y += ioy, old_y += ioy) {
649 /* Iterate over the x tile values. */
650 for (k = 0; k < buf_width_tiles; ++k, new_x += iox, old_x += iox) {
651 /* Can we get this grid block from the old buffer?. */
652 if (old_x >= 0 && old_x < buf_width_tiles && old_y >= 0 && old_y < buf_height_tiles) {
653 /* Copy from old buffer to new buffer. */
654 gdk_draw_drawable(map_pixmap,
657 old_x * TILE_SIZE_PIXELS,
658 old_y * TILE_SIZE_PIXELS,
659 new_x * TILE_SIZE_PIXELS,
660 new_y * TILE_SIZE_PIXELS,
664 map_render_tile(new_base_tilex + new_x,
665 new_base_tiley + new_y,
666 new_x * TILE_SIZE_PIXELS,
667 new_y * TILE_SIZE_PIXELS,
668 map_drag_id!=0 ? TRUE : FALSE);
675 MACRO_RECALC_OFFSET();
676 MACRO_RECALC_FOCUS_BASE(_center_ratio);
678 map_set_mark(&_gps->data);
679 MACRO_QUEUE_DRAW_AREA();
683 * Pan the view by the given number of units in the X and Y directions.
686 map_pan(gint delta_unitx, gint delta_unity)
688 if (_center_mode > 0)
689 set_action_activate("autocenter_none", TRUE);
691 map_center_unit(_center.unitx + delta_unitx, _center.unity + delta_unity);
695 * Helper to center map on given lat/lon
698 map_center_latlon(gdouble lat, gdouble lon)
702 latlon2unit(lat, lon, unitx, unity);
703 map_center_unit(unitx, unity);
707 * Helper to goto given point and update location
711 map_goto_position(Position *pos)
713 if (pos->valid==FALSE)
716 _center_mode=CENTER_MANUAL;
717 map_center_latlon(pos->lat, pos->lon);
718 map_set_autozoom(FALSE, 0);
719 g_idle_add_full(G_PRIORITY_HIGH_IDLE,(GSourceFunc)map_update_location_from_center, NULL, NULL);
724 * Initiate a move of the mark from the old location to the current
725 * location. This function queues the draw area of the old mark (to force
726 * drawing of the background map), then updates the mark, then queus the
727 * draw area of the new mark.
732 /* Just queue the old and new draw areas. */
733 gtk_widget_queue_draw_area(_map_widget,
734 mark_minx<0 ? 0 : mark_minx,
735 mark_miny<0 ? 0 : mark_miny,
736 mark_width, mark_height);
737 map_set_mark(&_gps->data);
738 gtk_widget_queue_draw_area(_map_widget,
739 mark_minx<0 ? 0 : mark_minx,
740 mark_miny<0 ? 0 : mark_miny,
741 mark_width, mark_height);
745 map_update_location_from_gps(Gps *gps)
748 map_update_location(gps->data.lat, gps->data.lon, FALSE);
753 map_update_location_from_center(void)
756 /* Force re-validation of place if user is clicking around */
758 /* XXX: hmm, not the right place for this */
760 if (_gps->fix==FIX_NOFIX) {
761 _gps->data.unitx=_center.unitx;
762 _gps->data.unity=_center.unity;
763 unit2latlon(_gps->data.unitx, _gps->data.unity, _gps->data.lat, _gps->data.lon);
766 unit2latlon(_center.unitx, _center.unity, lat, lon);
767 map_update_location(lat, lon, TRUE);
772 map_draw_pixbuf_on_buffer(gint x, gint y, GdkPixbuf *p)
774 if ((x < 0) || (y < 0))
777 if ((x > buf_width_pixels) || (y > buf_height_pixels))
780 gdk_draw_pixbuf(map_pixmap, _gc[COLORABLE_POI],
782 x - gdk_pixbuf_get_width(p) / 2,
783 y - gdk_pixbuf_get_height(p) / 2,
784 -1, -1, GDK_RGB_DITHER_NONE, 0, 0);
788 * Draw given pixbuf on map, centered on x,y
791 map_draw_pixbuf(guint unitx, guint unity, GdkPixbuf *p)
797 map_draw_pixbuf_on_buffer(x, y, p);
801 * Draw an icon at given Position.
805 map_draw_position_icon(Position *pos, const gchar *icon)
812 latlon2unit(pos->lat, pos->lon, ux, uy);
816 if ((x < 0) || (y < 0))
819 if ((x > buf_width_pixels) || (y > buf_height_pixels))
822 g_snprintf(buffer, sizeof(buffer), "%s/pixmaps/mapper/%s.png", DATADIR, icon);
823 p=gdk_pixbuf_new_from_file(buffer, NULL);
827 map_draw_pixbuf_on_buffer(x, y, p);
832 * Make sure the mark is up-to-date. This function triggers a panning of
833 * the view if the mark is appropriately close to the edge of the view.
836 map_refresh_mark(void)
838 guint new_center_unitx;
839 guint new_center_unity;
841 MACRO_RECALC_CENTER(_gps->data, new_center_unitx, new_center_unity);
843 if ((new_center_unitx - _focus.unitx) < _focus_unitwidth && (new_center_unity - _focus.unity) < _focus_unitheight)
844 /* We're not changing the view - just move the mark. */
847 map_center_unit(new_center_unitx, new_center_unity);
849 /* Draw speed info */
854 g_idle_add_full(G_PRIORITY_HIGH_IDLE,(GSourceFunc)map_update_location_from_gps, _gps, NULL);
858 * Render a single track line to map_pixmap. If either point on the line
859 * is a break (defined as unity == 0), a circle is drawn at the other point.
860 * IT IS AN ERROR FOR BOTH POINTS TO INDICATE A BREAK.
863 map_render_segment(GdkGC *gc_norm, GdkGC *gc_alt, guint unitx1, guint unity1, guint unitx2, guint unity2)
868 x2 = unit2bufx(unitx2);
869 y2 = unit2bufy(unity2);
870 /* Make sure this circle will be visible. */
871 if ((x2 < buf_width_pixels) && (y2 < buf_height_pixels))
872 gdk_draw_arc(map_pixmap, gc_alt, FALSE, /* FALSE: not filled. */
873 x2 - _draw_width, y2 - _draw_width, 2 * _draw_width, 2 * _draw_width, 0, /* start at 0 degrees. */
875 } else if (!unity2) {
878 x1 = unit2bufx(unitx1);
879 y1 = unit2bufy(unity1);
880 /* Make sure this circle will be visible. */
881 if ((x1 < buf_width_pixels) && ((unsigned)y1 < buf_height_pixels))
882 gdk_draw_arc(map_pixmap, gc_alt, FALSE, /* FALSE: not filled. */
883 x1 - _draw_width, y1 - _draw_width, 2 * _draw_width, 2 * _draw_width, 0, /* start at 0 degrees. */
888 x1 = unit2bufx(unitx1);
889 y1 = unit2bufy(unity1);
890 x2 = unit2bufx(unitx2);
891 y2 = unit2bufy(unity2);
892 /* Make sure this line could possibly be visible. */
893 if (!((x1 > buf_width_pixels && x2 > buf_width_pixels)
894 || (x1 < 0 && x2 < 0)
895 || (y1 > buf_height_pixels && y2 > buf_height_pixels)
896 || (y1 < 0 && y2 < 0)))
897 gdk_draw_line(map_pixmap, gc_norm, x1, y1, x2, y2);
902 map_render_waypoint(guint x1, guint y1, GdkGC *gc)
904 if ((x1 > buf_width_pixels) || (y1 > buf_height_pixels))
906 gdk_draw_arc(map_pixmap, gc, FALSE, x1 - _draw_width, y1 - _draw_width, 2 * _draw_width, 2 * _draw_width, 0, 360 * 64);
910 * Render all track data onto the map_pixmap. Note that this does not
911 * clear the pixmap of previous track data (use map_force_redraw() for
912 * that), and also note that this method does not queue any redraws, so it
913 * is up to the caller to decide which part of the track really needs to be
917 map_render_path(Path * path, GdkGC ** gc)
922 /* gc is a pointer to the first GC to use (for plain points). (gc + 1)
923 * is a pointer to the GC to use for waypoints, and (gc + 2) is a pointer
924 * to the GC to use for breaks. */
926 /* else there is a route to draw. */
927 for (curr = path->head, wcurr = path->whead; curr++ != path->tail;) {
928 /* Draw the line from (curr - 1) to (curr). */
929 map_render_segment(gc[0], gc[2], curr[-1].unitx, curr[-1].unity, curr->unitx, curr->unity);
931 /* Now, check if curr is a waypoint. */
932 if (wcurr && wcurr <= path->wtail && wcurr->point == curr) {
933 guint x1 = unit2bufx(wcurr->point->unitx);
934 guint y1 = unit2bufy(wcurr->point->unity);
936 map_render_waypoint(x1, y1, _gc[COLORABLE_TRACK_BREAK]);
943 map_render_paths(void)
945 if ((_show_tracks & ROUTES_MASK) && _route->head != _route->tail) {
946 map_render_path(_route, _gc + COLORABLE_ROUTE);
948 /* Now, draw the next waypoint on top of all other waypoints. */
950 guint x1 = unit2bufx(_next_way->point->unitx);
951 guint y1 = unit2bufy(_next_way->point->unity);
953 if ((x1 < buf_width_pixels) && (y1 < buf_height_pixels)) {
954 /* Draw the next waypoint as a break. */
955 gdk_draw_arc(map_pixmap, _gc[COLORABLE_ROUTE_BREAK], FALSE, /* FALSE: not filled. */
956 x1 - _draw_width, y1 - _draw_width, 4 * _draw_width, 4 * _draw_width, 0, /* start at 0 degrees. */
961 if (_show_tracks & TRACKS_MASK)
962 map_render_path(_track, _gc + COLORABLE_TRACK);
970 map_follow_move_cb(GtkWidget *widget, GdkEventMotion *event, gpointer data)
972 GdkModifierType state;
974 guint unitx, unity, nunitx, nunity;
977 if (!(event->state & GDK_BUTTON1_MASK)) {
982 if (event->is_hint) {
983 gdk_window_get_pointer(event->window, &xx, &yy, &state);
984 state = event->state;
988 state = event->state;
991 unitx = x2unit((gint) (xx - before[0]));
992 unity = y2unit((gint) (yy - before[1]));
993 cx = unit2x(_center.unitx);
994 cy = unit2y(_center.unity);
996 nunitx=x2unit((gint) (cx - (xx - before[0])));
997 nunity=y2unit((gint) (cy - (yy - before[1])));
999 map_center_unit(nunitx, nunity);
1002 g_debug("MAP PAN: %d %d %d %d (%d, %d)", cx, cy, xx, yy, nunitx, nunity);
1009 map_key_zoom_timeout(void)
1011 if (_key_zoom_new < _zoom) {
1012 /* We're currently zooming in (_zoom is decreasing). */
1013 guint test = _key_zoom_new - _curr_repo->view_zoom_steps;
1014 if (test < map_max_zoom)
1015 _key_zoom_new = test;
1019 /* We're currently zooming out (_zoom is increasing). */
1020 guint test = _key_zoom_new + _curr_repo->view_zoom_steps;
1021 if (test < map_max_zoom)
1022 _key_zoom_new = test;
1031 map_information_text(guint x, guint y, GdkGC *gc, gchar *msg)
1033 guint width, height;
1035 pango_layout_set_text(speed_layout, msg, -1);
1036 pango_layout_get_pixel_size(speed_layout, &width, &height);
1037 gtk_widget_queue_draw_area(_map_widget, x - 5, y - 5, width * 3 + 15, height + 5);
1038 gdk_window_process_all_updates();
1039 gdk_draw_layout(_map_widget->window, gc, x, y, speed_layout);
1043 map_speed_draw(void)
1049 cur_speed = _gps->data.speed * UNITS_CONVERT[_units];
1050 gc=(cur_speed > _speed_limit) ? speed_gc1 : speed_gc2;
1051 buffer = g_strdup_printf("%0.0f", cur_speed);
1052 map_information_text(10, 10, gc, buffer);
1057 map_scale_draw(GdkEventExpose *event)
1061 gdouble lat1, lon1, lat2, lon2;
1064 pango_layout_set_text(scale_layout, "0", -1);
1065 pango_layout_get_pixel_size(scale_layout, NULL, &scale_rect.height);
1066 scale_rect.y = _screen_height_pixels - scale_rect.height - 1;
1068 gdk_rectangle_intersect(&event->area, &scale_rect, &event->area);
1070 if (event->area.width && event->area.height) {
1071 gdk_draw_rectangle(_map_widget->window,
1072 _map_widget->style->bg_gc[GTK_WIDGET_STATE(_map_widget)],
1073 TRUE, scale_rect.x, scale_rect.y,
1076 gdk_draw_rectangle(_map_widget->window,
1077 _map_widget->style->fg_gc[GTK_WIDGET_STATE(_map_widget)],
1078 FALSE, scale_rect.x, scale_rect.y,
1082 /* Now calculate and draw the distance. */
1083 unit2latlon(_center.unitx - pixel2unit(SCALE_WIDTH / 2 - 4), _center.unity, lat1, lon1);
1084 unit2latlon(_center.unitx + pixel2unit(SCALE_WIDTH / 2 - 4), _center.unity, lat2, lon2);
1085 distance=calculate_distance(lat1, lon1, lat2, lon2) * UNITS_CONVERT[_units];
1088 g_snprintf(buffer, sizeof(buffer), "%0.2f %s", distance, UNITS_TEXT[_units]);
1089 else if (distance < 10.f)
1090 g_snprintf(buffer, sizeof(buffer), "%0.1f %s", distance, UNITS_TEXT[_units]);
1092 g_snprintf(buffer, sizeof(buffer), "%0.f %s", distance, UNITS_TEXT[_units]);
1094 pango_layout_set_text(scale_layout, buffer, -1);
1095 pango_layout_get_pixel_size(scale_layout, &width, NULL);
1097 /* Draw the layout itself. */
1098 gdk_draw_layout(_map_widget->window,
1099 _map_widget->style->fg_gc[GTK_WIDGET_STATE(_map_widget)],
1100 scale_rect.x + (scale_rect.width - width) / 2,
1101 scale_rect.y, scale_layout);
1103 /* Draw little hashes on the ends. */
1104 gdk_draw_line(_map_widget->window,
1105 _map_widget->style->fg_gc[GTK_WIDGET_STATE(_map_widget)],
1107 scale_rect.y + scale_rect.height / 2 - 4,
1109 scale_rect.y + scale_rect.height / 2 + 4);
1110 gdk_draw_line(_map_widget->window,
1111 _map_widget->style->fg_gc[GTK_WIDGET_STATE(_map_widget)],
1113 scale_rect.y + scale_rect.height / 2,
1114 scale_rect.x + (scale_rect.width - width) / 2 - 4,
1115 scale_rect.y + scale_rect.height / 2);
1116 gdk_draw_line(_map_widget->window,
1117 _map_widget->style->fg_gc[GTK_WIDGET_STATE(_map_widget)],
1118 scale_rect.x + scale_rect.width - 4,
1119 scale_rect.y + scale_rect.height / 2 - 4,
1120 scale_rect.x + scale_rect.width - 4,
1121 scale_rect.y + scale_rect.height / 2 + 4);
1122 gdk_draw_line(_map_widget->window,
1123 _map_widget->style->fg_gc[GTK_WIDGET_STATE(_map_widget)],
1124 scale_rect.x + scale_rect.width - 4,
1125 scale_rect.y + scale_rect.height / 2,
1126 scale_rect.x + (scale_rect.width + width) / 2 + 4,
1127 scale_rect.y + scale_rect.height / 2);
1132 map_cb_expose(GtkWidget *widget, GdkEventExpose *event)
1134 gdk_draw_drawable(GDK_DRAWABLE(_map_widget->window),
1135 _gc[COLORABLE_MARK],
1137 event->area.x + _offsetx, event->area.y + _offsety,
1138 event->area.x, event->area.y,
1139 event->area.width, event->area.height);
1140 map_draw_mark(_gps);
1142 /* Draw scale, if necessary. */
1144 map_scale_draw(event);
1160 if ((nzoom >= 0) && (nzoom < map_max_zoom - 1)) {
1161 image_cache_clear(map_ic);
1162 map_set_zoom(nzoom);
1182 map_autozoomer(void)
1184 static gfloat z=5.0;
1189 if (zoom_timeout_sid==0)
1192 /* If location is known, use road type and speed for zoom setting */
1193 if (map_loc.valid && map_loc.street) {
1194 switch (map_loc.street->type) {
1209 z=(z+_gps->data.speed+1)/5;
1211 if (z>5) z=5.0; else if (z<1) z=1.0;
1213 g_debug("Setting autozoom to: %f %d S:%f\n", z, iz, _gps->data.speed);
1225 map_set_autozoom(gboolean az, gfloat speed)
1228 zoom_timeout_sid=g_timeout_add(speed<5 ? 2000 : 5000, (GSourceFunc) map_autozoomer, NULL);
1229 MACRO_BANNER_SHOW_INFO(_window, "Autozoom enabled");
1232 if (zoom_timeout_sid) {
1233 g_source_remove(zoom_timeout_sid);
1235 MACRO_BANNER_SHOW_INFO(_window, "Autozoom disabled");
1243 map_draw_route(gint x, gint y)
1245 cmenu_route_add_way(x, y);
1249 map_draw_track(gint x, gint y)
1251 _gps->data.unitx=x2unit((gint) (x + 0.5));
1252 _gps->data.unity=y2unit((gint) (y + 0.5));
1253 unit2latlon(_gps->data.unitx, _gps->data.unity, _gps->data.lat, _gps->data.lon);
1254 _gps->data.speed=20.f;
1255 gps_data_integerize(&_gps->data);
1256 _gps->data.time=time(NULL);
1257 track_add(&_gps->data);
1262 map_set_place_information(osm_way *s, osm_place *mp, osm_place *sp)
1266 if (!s && !mp && !sp) {
1267 g_snprintf(buffer, sizeof(buffer), _("Unknown location"));
1270 g_snprintf(buffer, sizeof(buffer), "%s%s%s%s%s%s%s%s%s%s",
1271 s ? s->name ? s->name : "" : "",
1272 s ? s->ref ? ", " : "" : "",
1273 s ? s->ref ? s->ref : "" : "",
1274 s ? s->int_ref ? " (" : "" : "",
1275 s ? s->int_ref ? s->int_ref : "" : "",
1276 s ? s->int_ref ? ") " : "" : "",
1277 (sp && sp->name) ? " in " : "",
1278 (sp && sp->name) ? sp->name : "",
1279 (mp && mp->name) ? " in " : "",
1280 (mp && mp->name) ? mp->name : "");
1282 gtk_label_set_label(GTK_LABEL(info_banner.location), buffer);
1286 map_update_destination(gdouble lat, gdouble lon)
1290 static gdouble prev_dt=99999.0;
1291 static gboolean dest_reached=FALSE;
1295 dt=calculate_distance(lat, lon, _dest.lat, _dest.lon);
1296 dh=calculate_course(lat, lon, _dest.lat, _dest.lon);
1297 cdist=dt*UNITS_CONVERT[_units];
1299 g_snprintf(buffer, sizeof(buffer), "%.02f %s (%0.02f)", cdist, UNITS_TEXT[_units], dh<0 ? 360+dh : dh);
1300 gtk_label_set_label(GTK_LABEL(info_banner.distance), buffer);
1302 if (dt<0.005 && dest_reached==FALSE) {
1304 announce_destination_reached();
1306 } else if (dt<prev_dt-KM10KNOTS) {
1307 if (_center_mode>0 && _announce_destination==TRUE)
1308 announce_distance_to_destination(cdist, UNITS_TEXT[_units], KM10KNOTS);
1310 } else if (dt>prev_dt+KM10KNOTS/4) {
1316 g_debug("%f (Prev:%f)\n", prev_dt, dt);
1320 gtk_label_set_label(GTK_LABEL(info_banner.distance), "");
1323 gtk_compass_set_dest_heading(_gps_compass, _dest.valid, (gfloat)dh);
1324 gtk_compass_set_dest_heading(_tab_compass, _dest.valid, (gfloat)dh);
1326 if (_next_way && _next_way->point) {
1327 gdouble wp_lat, wp_lon;
1330 unit2latlon(_next_way->point->unitx,_next_way->point->unity, wp_lat, wp_lon);
1331 wc=calculate_course(lat, lon, wp_lat, wp_lon);
1332 gtk_compass_set_way_heading(_gps_compass, TRUE, wc);
1333 gtk_compass_set_way_heading(_tab_compass, TRUE, wc);
1335 gtk_compass_set_way_heading(_gps_compass, FALSE, 0);
1336 gtk_compass_set_way_heading(_tab_compass, FALSE, 0);
1342 * Query the OSM database for where we are.
1344 * XXX: This is the wrong place for this function.
1347 map_update_location(gdouble lat, gdouble lon, gboolean force)
1350 static gboolean inp=FALSE;
1352 /* We run the gtk mainloop in progress callback so we can be called again, we don't like that */
1354 g_debug("LOC: Query in progress");
1359 ilat=lat2mp_int(lat);
1360 ilon=lon2mp_int(lon);
1362 if (_gps->data.fix>1 && !force)
1363 osm_set_way_range_from_speed(_gps->data.speed);
1365 osm_set_way_range(2800);
1367 map_update_destination(lat, lon);
1369 if (osm_check_location(&map_loc, ilat, ilon)==FALSE) {
1370 osm_progress_set_widget(_db, _progress_item);
1371 osm_get_location_data(ilat, ilon, _gps->data.heading, &map_loc);
1373 map_set_place_information(map_loc.street, map_loc.primary, map_loc.secondary);
1375 map_set_place_information(NULL, NULL, NULL);
1376 osm_progress_set_widget(_db, NULL);
1377 } else g_debug("IN PLACE");
1383 * Mouse scroller zoom in/out callback
1386 map_cb_scroll_event(GtkWidget * widget, GdkEventScroll * event)
1388 if (event->direction == GDK_SCROLL_UP) {
1389 map_center_unit(x2unit((gint) (event->x + 0.5)), y2unit((gint) (event->y + 0.5)));
1390 map_set_zoom(_zoom - 1);
1391 } else if (event->direction == GDK_SCROLL_DOWN) {
1392 map_center_unit(x2unit((gint) (event->x + 0.5)), y2unit((gint) (event->y + 0.5)));
1393 map_set_zoom(_zoom + 1);
1399 * Start map drag operation
1402 map_drag_start(gint x,gint y)
1406 before[0] = press[0];
1407 before[1] = press[1];
1408 _center_mode=CENTER_MANUAL;
1410 g_signal_handler_disconnect(G_OBJECT(_map_widget), map_drag_id);
1412 map_drag_id=g_signal_connect(G_OBJECT(_map_widget), "motion_notify_event", G_CALLBACK(map_follow_move_cb), NULL);
1416 * Stop map drag operation
1419 map_drag_stop(gint x, gint y)
1424 g_signal_handler_disconnect(G_OBJECT(_map_widget), map_drag_id);
1435 if (map_dragged==TRUE)
1439 /* Workaround hildon content menu problem */
1441 map_cb_show_poi_info_dialog(gpointer data)
1443 guint poi_id=GPOINTER_TO_INT(data);
1444 if (poi_info_dialog(_window, poi_id)==FALSE)
1445 g_printerr("Huh? Failed to display info dialog\n");
1450 map_cb_button_press(GtkWidget *widget, GdkEventButton *event)
1452 _cmenu_position_x = event->x + 0.5;
1453 _cmenu_position_y = event->y + 0.5;
1455 /* Handle map panning using clicks, use thumb on tablets and middle button on non-tablets */
1456 #if defined(WITH_HILDON_NEW)
1457 if (hildon_helper_event_button_is_finger(event)) {
1458 g_debug("Thumb down");
1460 if (event->button==2) {
1463 if (event->x<MAP_THUMB_MARGIN_X)
1465 else if (event->x>(_screen_width_pixels-MAP_THUMB_MARGIN_X))
1468 if (event->y<MAP_THUMB_MARGIN_Y)
1470 else if (event->y>(_screen_height_pixels-MAP_THUMB_MARGIN_Y))
1473 if (pns!=0 || pew!=0) {
1479 switch (event->button) {
1481 if (event->type==GDK_2BUTTON_PRESS) {
1482 map_center_unit(x2unit((gint) (event->x + 0.5)), y2unit((gint) (event->y + 0.5)));
1483 #ifdef MAP_ZOOM_ON_DOUBLE_CLICK
1484 map_set_zoom(_zoom-1);
1486 map_data_needs_refresh=TRUE;
1487 map_drag_stop(event->x, event->y);
1490 if (event->type==GDK_3BUTTON_PRESS) {
1491 map_drag_stop(event->x, event->y);
1495 map_drag_start(event->x, event->y);
1499 map_set_zoom(_zoom - 1);
1503 gtk_menu_popup(GTK_MENU(_menu_map), NULL, NULL, NULL, NULL, event->button, gtk_get_current_event_time());
1507 /* Return FALSE to allow context menu to work. */
1512 map_cb_button_release(GtkWidget *widget, GdkEventButton *event)
1518 #if defined(WITH_HILDON_NEW)
1519 if (hildon_helper_event_button_is_finger(event)) {
1520 g_debug("Thumb up");
1525 switch (event->button) {
1528 set_action_activate("autocenter_none", TRUE);
1531 case MAP_MODE_DRAW_TRACK:
1532 map_draw_track(event->x, event->y);
1534 case MAP_MODE_DRAW_ROUTE:
1535 map_draw_route(event->x, event->y);
1537 case MAP_MODE_SET_ROUTE_FROM:
1538 case MAP_MODE_SET_ROUTE_POINT:
1539 case MAP_MODE_SET_ROUTE_TO:
1542 ux=x2unit(_cmenu_position_x);
1543 uy=y2unit(_cmenu_position_y);
1545 unit2latlon(ux, uy, lat, lon);
1546 if (map_poi_find_at_latlon(lat, lon, &poi_id)==TRUE) {
1547 g_idle_add_full(G_PRIORITY_HIGH_IDLE,(GSourceFunc)map_cb_show_poi_info_dialog, GINT_TO_POINTER(poi_id), NULL);
1549 if (map_data_needs_refresh==TRUE) {
1550 map_data_needs_refresh=FALSE;
1552 g_idle_add_full(G_PRIORITY_HIGH_IDLE,(GSourceFunc)map_update_location_from_center, NULL, NULL);
1554 map_drag_stop(event->x, event->y);
1565 /* Return FALSE to avoid context menu (if it hasn't popped up already). */
1570 map_dialog_goto_latlon(void)
1578 dialog = gtk_dialog_new_with_buttons(_("Go to Lat/Lon"),
1579 GTK_WINDOW(_window),
1580 GTK_DIALOG_MODAL, GTK_STOCK_OK,
1581 GTK_RESPONSE_ACCEPT,
1583 GTK_RESPONSE_REJECT, NULL);
1585 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), table = gtk_table_new(2, 3, FALSE), TRUE, TRUE, 0);
1587 gtk_table_attach(GTK_TABLE(table), label = gtk_label_new(_("Latitude")),0, 1, 0, 1, GTK_FILL, 0, 2, 4);
1588 gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
1590 gtk_table_attach(GTK_TABLE(table), txt_lat = gtk_entry_new(), 1, 2, 0, 1, GTK_FILL, 0, 2, 4);
1591 gtk_misc_set_alignment(GTK_MISC(label), 0.0f, 0.5f);
1593 gtk_table_attach(GTK_TABLE(table), label = gtk_label_new(_("Longitude")), 0, 1, 1, 2, GTK_FILL, 0, 2, 4);
1594 gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
1596 gtk_table_attach(GTK_TABLE(table), txt_lon = gtk_entry_new(), 1, 2, 1, 2, GTK_FILL, 0, 2, 4);
1597 gtk_misc_set_alignment(GTK_MISC(label), 0.0f, 0.5f);
1599 #if defined (WITH_DEVICE_770) && !defined(WITH_HILDON_NEW)
1600 g_object_set(G_OBJECT(txt_lat), HILDON_INPUT_MODE_HINT, HILDON_INPUT_MODE_HINT_NUMERICSPECIAL, NULL);
1601 g_object_set(G_OBJECT(txt_lon), HILDON_INPUT_MODE_HINT, HILDON_INPUT_MODE_HINT_NUMERICSPECIAL, NULL);
1604 /* Initialize with the current center position. */
1608 unit2latlon(_center.unitx, _center.unity, lat, lon);
1609 g_snprintf(buffer, sizeof(buffer), "%.06f", lat);
1610 gtk_label_set_text(GTK_LABEL(txt_lat), buffer);
1611 g_snprintf(buffer, sizeof(buffer), "%.06f", lon);
1612 gtk_label_set_text(GTK_LABEL(txt_lon), buffer);
1615 gtk_widget_show_all(dialog);
1617 while (GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) {
1623 text = gtk_entry_get_text(GTK_ENTRY(txt_lat));
1624 lat = strtof(text, &error_check);
1625 if (text == error_check || lat < -90.f || lat > 90.f) {
1626 popup_error(dialog, _("Invalid Latitude"));
1630 text = gtk_entry_get_text(GTK_ENTRY(txt_lon));
1631 lon = strtof(text, &error_check);
1632 if (text == error_check || lon < -180.f || lon > 180.f) {
1633 popup_error(dialog, _("Invalid Longitude"));
1637 latlon2unit(lat, lon, unitx, unity);
1638 if (_center_mode > 0)
1639 set_action_activate("autocenter_none", TRUE);
1641 map_center_unit(unitx, unity);
1644 gtk_widget_destroy(dialog);