14 #include <glib/gstdio.h>
20 #include "hildon-mapper.h"
32 #include "mapper-types.h"
33 #include "ui-common.h"
38 #include "map-download.h"
40 #include "gtkcompass.h"
42 /** The "base tile" is the upper-left tile in the pixmap. */
43 guint _base_tilex = -5;
44 guint _base_tiley = -5;
46 guint _zoom = 3; /* zoom level, from 0 to MAX_ZOOM. */
48 Point _min_center = { -1, -1 };
49 Point _max_center = { -1, -1 };
50 Point _focus = { -1, -1 };
51 Point _center = { -1, -1 }; /* current center location, X. */
53 CenterMode _center_mode = CENTER_LEAD;
56 static guint press[2] = { 0, 0 };
57 static guint release[2] = { 0, 0 };
58 static guint before[2] = { 0, 0 };
59 static guint map_drag_id = 0;
61 /** VARIABLES FOR ACCESSING THE LOCATION/BOUNDS OF THE CURRENT MARK. */
66 static gint _mark_minx;
67 static gint _mark_miny;
68 static gint _mark_width;
69 static gint _mark_height;
71 static gint zoom_timeout_sid=0;
72 static gint map_mode=0;
74 static osm_location map_loc = {NULL, NULL, NULL, FALSE, FALSE, 0, 0, 0.0, 0.0, 0 };
76 typedef struct _map_tile_rdata map_tile_rdata;
77 struct _map_tile_rdata {
78 guint tilex, tiley, zoom;
82 /* Tile max age, 1 week */
83 #define TILE_MAX_AGE (604800)
85 void map_render_paths();
86 void map_force_redraw();
87 static void map_update_location(gint x, gint y, gboolean force);
88 void map_draw_position_icon(Position *pos);
90 /******************************************************************************/
95 return GTK_WIDGET(gtk_drawing_area_new());
99 map_cb_configure(GtkWidget *widget, GdkEventConfigure *event)
101 _screen_width_pixels = _map_widget->allocation.width;
102 _screen_height_pixels = _map_widget->allocation.height;
103 _screen_grids_halfwidth = pixel2grid(_screen_width_pixels) / 2;
104 _screen_grids_halfheight = pixel2grid(_screen_height_pixels) / 2;
106 /* Set _scale_rect. */
107 _scale_rect.x = (_screen_width_pixels - SCALE_WIDTH) / 2;
108 _scale_rect.width = SCALE_WIDTH;
109 pango_layout_set_text(_scale_layout, "0", -1);
110 pango_layout_get_pixel_size(_scale_layout, NULL, &_scale_rect.height);
111 _scale_rect.y = _screen_height_pixels - _scale_rect.height - 1;
113 MACRO_RECALC_FOCUS_BASE();
114 MACRO_RECALC_FOCUS_SIZE();
116 _min_center.unitx = pixel2unit(grid2pixel(_screen_grids_halfwidth));
117 _min_center.unity = pixel2unit(grid2pixel(_screen_grids_halfheight));
118 _max_center.unitx = WORLD_SIZE_UNITS - grid2unit(_screen_grids_halfwidth) - 1;
119 _max_center.unity = WORLD_SIZE_UNITS - grid2unit(_screen_grids_halfheight) - 1;
121 map_center_unit(_center.unitx, _center.unity);
127 * Draw the current mark (representing the current GPS location) onto
128 * _map_widget. This method does not queue the draw area.
133 gdk_draw_arc(_map_widget->window, _conn_state == RCVR_FIXED ? _gc[COLORABLE_MARK] : _gc[COLORABLE_MARK_OLD], FALSE,
134 _mark_x1 - _draw_width, _mark_y1 - _draw_width, 2 * _draw_width, 2 * _draw_width, 0, 360 * 64);
135 gdk_draw_line(_map_widget->window, _conn_state == RCVR_FIXED ? (_show_velvec ? _gc[COLORABLE_MARK_VELOCITY] : _gc[COLORABLE_MARK]) : _gc[COLORABLE_MARK_OLD],
136 _mark_x1, _mark_y1, _mark_x2, _mark_y2);
140 * "Set" the mark, which translates the current GPS position into on-screen
141 * units in preparation for drawing the mark with map_draw_mark().
146 _mark_x1 = unit2x(_pos.unitx);
147 _mark_y1 = unit2y(_pos.unity);
148 _mark_x2 = _mark_x1 + (_show_velvec ? _gps.vel_offsetx : 0);
149 _mark_y2 = _mark_y1 + (_show_velvec ? _gps.vel_offsety : 0);
150 _mark_minx = MIN(_mark_x1, _mark_x2) - (2 * _draw_width);
151 _mark_miny = MIN(_mark_y1, _mark_y2) - (2 * _draw_width);
152 _mark_width = abs(_mark_x1 - _mark_x2) + (4 * _draw_width);
153 _mark_height = abs(_mark_y1 - _mark_y2) + (4 * _draw_width);
157 * Do an in-place scaling of a pixbuf's pixels at the given ratio from the
158 * given source location. It would have been nice if gdk_pixbuf provided
159 * this method, but I guess it's not general-purpose enough.
162 map_pixbuf_scale_inplace(GdkPixbuf * pixbuf, guint ratio_p2, guint src_x, guint src_y)
164 guint dest_x = 0, dest_y = 0, dest_dim = TILE_SIZE_PIXELS;
165 guint rowstride = gdk_pixbuf_get_rowstride(pixbuf);
166 guint n_channels = gdk_pixbuf_get_n_channels(pixbuf);
167 guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
169 /* Sweep through the entire dest area, copying as necessary, but
170 * DO NOT OVERWRITE THE SOURCE AREA. We'll copy it afterward. */
172 guint src_dim = dest_dim >> ratio_p2;
173 guint src_endx = src_x - dest_x + src_dim;
176 for (y = dest_dim - 1; y >= 0; y--) {
177 guint src_offset_y, dest_offset_y;
179 src_offset_y = (src_y + (y >> ratio_p2)) * rowstride;
180 dest_offset_y = (dest_y + y) * rowstride;
183 if ((unsigned)(dest_y + y - src_y) < src_dim && (unsigned)(dest_x + x - src_x) < src_dim)
186 for (; x >= 0; x--) {
187 guint src_offset, dest_offset, i;
189 src_offset = src_offset_y + (src_x + (x >> ratio_p2)) * n_channels;
190 dest_offset = dest_offset_y + (dest_x + x) * n_channels;
192 pixels[dest_offset] = pixels[src_offset];
193 for (i = n_channels - 1; i; i--)
194 pixels[dest_offset + i] = pixels[src_offset + i];
196 if ((unsigned)(dest_y + y - src_y) < src_dim && x == src_endx)
201 /* Reuse src_dim and src_endx to store new src_x and src_y. */
202 src_dim = src_x + ((src_x - dest_x) >> ratio_p2);
203 src_endx = src_y + ((src_y - dest_y) >> ratio_p2);
209 while ((dest_dim >>= ratio_p2) > 1);
213 * Trim pixbufs that are bigger than tiles. (Those pixbufs result, when
214 * captions should be cut off.)
217 pixbuf_trim(GdkPixbuf * pixbuf)
219 GdkPixbuf *mpixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, gdk_pixbuf_get_has_alpha(pixbuf),
220 8, TILE_SIZE_PIXELS, TILE_SIZE_PIXELS);
222 gdk_pixbuf_copy_area(pixbuf,
223 (gdk_pixbuf_get_width(pixbuf) - TILE_SIZE_PIXELS) / 2,
224 (gdk_pixbuf_get_height(pixbuf) - TILE_SIZE_PIXELS) / 2,
226 TILE_SIZE_PIXELS, mpixbuf, 0, 0);
228 g_object_unref(pixbuf);
233 map_tile_load(guint tilex, guint tiley, gint zoff, gboolean download)
236 GError *error = NULL;
237 gchar buffer[BUFFER_SIZE];
241 snprintf(buffer, sizeof(buffer), "%s/%u/%u/%u.jpg", _curr_repo->cache_dir, _zoom + zoff, (tilex >> zoff), (tiley >> zoff));
243 pixbuf=gdk_pixbuf_new_from_file(buffer, &error);
244 if (error || !pixbuf) {
247 if (_auto_download && _curr_repo->type != REPOTYPE_NONE && !((_zoom + zoff - (_curr_repo->double_size ? 1 : 0)) % _curr_repo->dl_zoom_steps)) {
249 map_initiate_download(tilex >> zoff, tiley >> zoff, _zoom + zoff, -INITIAL_DOWNLOAD_RETRIES);
255 /* Check tile age, if file date is ower a week old, redownload if autodownload enabled */
256 se=stat(buffer, &tstat);
260 if (t-tstat.st_mtime>TILE_MAX_AGE) {
261 if (_auto_download && _curr_repo->type != REPOTYPE_NONE && !((_zoom + zoff - (_curr_repo->double_size ? 1 : 0)) % _curr_repo->dl_zoom_steps)) {
263 g_printf("Tile: %s is old, re-downloading\n", buffer);
264 map_initiate_download(tilex >> zoff, tiley >> zoff, _zoom + zoff, -INITIAL_DOWNLOAD_RETRIES);
274 map_render_tile(guint tilex, guint tiley, guint destx, guint desty, gboolean fast_fail)
276 GdkPixbuf *pixbuf=NULL;
279 if (tilex<_world_size_tiles && tiley<_world_size_tiles) {
280 /* The tile is possible. */
281 for (zoff = (_curr_repo->double_size ? 1 : 0); !pixbuf && (_zoom + zoff) <= MAX_ZOOM && zoff <= TILE_SIZE_P2; zoff += 1) {
282 pixbuf=map_tile_load(tilex, tiley, zoff, !fast_fail);
287 /* Check if we need to trim. */
288 if (gdk_pixbuf_get_width(pixbuf) != TILE_SIZE_PIXELS || gdk_pixbuf_get_height(pixbuf) != TILE_SIZE_PIXELS)
289 pixbuf = pixbuf_trim(pixbuf);
291 /* Check if we need to blit. */
293 map_pixbuf_scale_inplace(pixbuf, zoff,
294 (tilex - ((tilex >> zoff) << zoff)) << (TILE_SIZE_P2 - zoff),
295 (tiley - ((tiley >> zoff) << zoff)) << (TILE_SIZE_P2 - zoff));
302 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);
303 g_object_unref(pixbuf);
306 gdk_draw_rectangle(_map_pixmap, _map_widget->style->black_gc, TRUE, destx, desty, TILE_SIZE_PIXELS, TILE_SIZE_PIXELS);
310 map_render_tile_idle(map_tile_rdata *mtr)
312 map_render_tile(mtr->tilex, mtr->tiley, mtr->destx, mtr->desty, FALSE);
313 gtk_widget_queue_draw_area(_map_widget, mtr->destx, mtr->desty, TILE_SIZE_PIXELS, TILE_SIZE_PIXELS);
315 g_slice_free(map_tile_rdata, mtr);
320 map_render_data(void)
322 /* Don't update if dragging, too much lag */
327 map_draw_position_icon(&_home);
335 * Force a redraw of the entire _map_pixmap, including fetching the
336 * background maps from disk and redrawing the tracks on top of them.
339 map_force_redraw(void)
343 for (new_y = 0; new_y < BUF_HEIGHT_TILES; ++new_y)
344 for (new_x = 0; new_x < BUF_WIDTH_TILES; ++new_x) {
345 map_render_tile(_base_tilex + new_x,
347 new_x * TILE_SIZE_PIXELS,
348 new_y * TILE_SIZE_PIXELS, FALSE);
351 MACRO_QUEUE_DRAW_AREA();
355 * Set the current zoom level. If the given zoom level is the same as the
356 * current zoom level, or if the new zoom is invalid
357 * (not MIN_ZOOM <= new_zoom < MAX_ZOOM), then this method does nothing.
360 map_set_zoom(guint new_zoom)
362 /* Note that, since new_zoom is a guint and MIN_ZOOM is 0, this if
363 * condition also checks for new_zoom >= MIN_ZOOM. */
364 if (new_zoom > (MAX_ZOOM - 1))
366 if (new_zoom == _zoom)
369 _zoom = new_zoom / _curr_repo->view_zoom_steps * _curr_repo->view_zoom_steps;
370 _world_size_tiles = unit2tile(WORLD_SIZE_UNITS);
372 /* If we're leading, update the center to reflect new zoom level. */
373 MACRO_RECALC_CENTER(_center.unitx, _center.unity);
375 /* Update center bounds to reflect new zoom level. */
376 _min_center.unitx = pixel2unit(grid2pixel(_screen_grids_halfwidth));
377 _min_center.unity = pixel2unit(grid2pixel(_screen_grids_halfheight));
378 _max_center.unitx = WORLD_SIZE_UNITS - grid2unit(_screen_grids_halfwidth) - 1;
379 _max_center.unity = WORLD_SIZE_UNITS - grid2unit(_screen_grids_halfheight) - 1;
381 BOUND(_center.unitx, _min_center.unitx, _max_center.unitx);
382 BOUND(_center.unity, _min_center.unity, _max_center.unity);
384 _base_tilex = grid2tile((gint) pixel2grid((gint) unit2pixel((gint) _center.unitx)) - (gint) _screen_grids_halfwidth);
385 _base_tiley = grid2tile(pixel2grid(unit2pixel(_center.unity)) - _screen_grids_halfheight);
387 /* New zoom level, so we can't reuse the old buffer's pixels. */
388 /* Update state variables. */
389 MACRO_RECALC_OFFSET();
390 MACRO_RECALC_FOCUS_BASE();
391 MACRO_RECALC_FOCUS_SIZE();
398 * Center the view on the given unitx/unity.
401 map_center_unit(guint new_center_unitx, guint new_center_unity)
403 gint new_base_tilex, new_base_tiley;
405 guint j, k, base_new_x, base_old_x, old_x, old_y, iox, ioy;
407 /* Assure that _center.unitx/y are bounded. */
408 BOUND(new_center_unitx, _min_center.unitx, _max_center.unitx);
409 BOUND(new_center_unity, _min_center.unity, _max_center.unity);
411 _center.unitx = new_center_unitx;
412 _center.unity = new_center_unity;
414 new_base_tilex = grid2tile((gint) pixel2grid((gint)unit2pixel((gint) _center.unitx))
415 - (gint)_screen_grids_halfwidth);
416 new_base_tiley = grid2tile(pixel2grid(unit2pixel(_center.unity))
417 - _screen_grids_halfheight);
419 /* Same zoom level, so it's likely that we can reuse some of the old
420 * buffer's pixels. */
422 if (new_base_tilex != _base_tilex || new_base_tiley != _base_tiley) {
423 /* If copying from old parts to new parts, we need to make sure we
424 * don't overwrite the old parts when copying, so set up new_x,
425 * new_y, old_x, old_y, iox, and ioy with that in mind. */
426 if (new_base_tiley < _base_tiley) {
427 /* New is lower than old - start at bottom and go up. */
428 new_y = BUF_HEIGHT_TILES - 1;
431 /* New is higher than old - start at top and go down. */
435 if (new_base_tilex < _base_tilex) {
436 /* New is righter than old - start at right and go left. */
437 base_new_x = BUF_WIDTH_TILES - 1;
440 /* New is lefter than old - start at left and go right. */
445 /* Iterate over the y tile values. */
446 old_y = new_y + new_base_tiley - _base_tiley;
447 base_old_x = base_new_x + new_base_tilex - _base_tilex;
448 _base_tilex = new_base_tilex;
449 _base_tiley = new_base_tiley;
450 for (j = 0; j < BUF_HEIGHT_TILES; ++j, new_y += ioy, old_y += ioy) {
453 /* Iterate over the x tile values. */
454 for (k = 0; k < BUF_WIDTH_TILES;
455 ++k, new_x += iox, old_x += iox) {
456 /* Can we get this grid block from the old buffer?. */
457 if (old_x >= 0 && old_x < BUF_WIDTH_TILES
458 && old_y >= 0 && old_y < BUF_HEIGHT_TILES) {
459 /* Copy from old buffer to new buffer. */
460 gdk_draw_drawable(_map_pixmap,
463 old_x * TILE_SIZE_PIXELS,
464 old_y * TILE_SIZE_PIXELS,
465 new_x * TILE_SIZE_PIXELS,
466 new_y * TILE_SIZE_PIXELS,
470 map_render_tile(new_base_tilex + new_x,
471 new_base_tiley + new_y,
472 new_x * TILE_SIZE_PIXELS,
473 new_y * TILE_SIZE_PIXELS,
481 MACRO_RECALC_OFFSET();
482 MACRO_RECALC_FOCUS_BASE();
485 MACRO_QUEUE_DRAW_AREA();
489 * Pan the view by the given number of units in the X and Y directions.
492 map_pan(gint delta_unitx, gint delta_unity)
494 if (_center_mode > 0)
495 set_action_activate("autocenter_none", TRUE);
497 map_center_unit(_center.unitx + delta_unitx, _center.unity + delta_unity);
501 * Helper to center map on given lat/lon
504 map_center_latlon(gdouble lat, gdouble lon)
508 latlon2unit(lat, lon, unitx, unity);
509 map_center_unit(unitx, unity);
513 * Helper to goto given point and update location
517 map_goto_position(Position *pos)
519 if (pos->valid==FALSE)
522 _center_mode=CENTER_MANUAL;
523 map_center_latlon(pos->lat, pos->lon);
524 map_set_autozoom(FALSE);
525 g_idle_add((GSourceFunc)map_update_location_from_center, NULL);
530 * Initiate a move of the mark from the old location to the current
531 * location. This function queues the draw area of the old mark (to force
532 * drawing of the background map), then updates the mark, then queus the
533 * draw area of the new mark.
538 /* Just queue the old and new draw areas. */
539 gtk_widget_queue_draw_area(_map_widget,
540 _mark_minx<0 ? 0 : _mark_minx,
541 _mark_miny<0 ? 0 : _mark_miny,
542 _mark_width, _mark_height);
544 gtk_widget_queue_draw_area(_map_widget,
545 _mark_minx<0 ? 0 : _mark_minx,
546 _mark_miny<0 ? 0 : _mark_miny,
547 _mark_width, _mark_height);
551 map_update_location_from_gps(void)
553 map_update_location(_pos.unitx, _pos.unity, FALSE);
558 map_update_location_from_center(void)
560 /* Force re-validation of place if user is clicking around */
562 /* XXX: hmm, not the right place for this */
564 _pos.unitx=_center.unitx;
565 _pos.unity=_center.unity;
566 unit2latlon(_pos.unitx, _pos.unity, _gps.lat, _gps.lon);
568 map_update_location(_center.unitx, _center.unity, TRUE);
573 * Draw given pixbuf on map, centered on x,y
576 map_draw_pixbuf(guint unitx, guint unity, GdkPixbuf *p)
579 x = unit2bufx(unitx);
580 y = unit2bufy(unity);
582 gdk_draw_pixbuf(_map_pixmap, _gc[COLORABLE_POI],
584 x - gdk_pixbuf_get_width(p) / 2,
585 y - gdk_pixbuf_get_height(p) / 2,
586 -1, -1, GDK_RGB_DITHER_NONE, 0, 0);
590 * Draw an icon on given Position.
592 * XXX: don't hardcode as home.png !
595 map_draw_position_icon(Position *pos)
600 latlon2unit(pos->lat, pos->lon, x, y);
604 if ((x1 > BUF_WIDTH_PIXELS) || (y1 > BUF_HEIGHT_PIXELS))
607 p=gdk_pixbuf_new_from_file(DATADIR "/pixmaps/mapper/home.png", NULL);
609 map_draw_pixbuf(x,y,p);
616 * Make sure the mark is up-to-date. This function triggers a panning of
617 * the view if the mark is appropriately close to the edge of the view.
620 map_refresh_mark(void)
622 guint new_center_unitx;
623 guint new_center_unity;
625 MACRO_RECALC_CENTER(new_center_unitx, new_center_unity);
627 if ((new_center_unitx - _focus.unitx) < _focus_unitwidth
628 && (new_center_unity - _focus.unity) < _focus_unitheight)
629 /* We're not changing the view - just move the mark. */
632 map_center_unit(new_center_unitx, new_center_unity);
634 /* Draw speed info */
639 g_idle_add((GSourceFunc)map_update_location_from_gps, NULL);
643 * Render a single track line to _map_pixmap. If either point on the line
644 * is a break (defined as unity == 0), a circle is drawn at the other point.
645 * IT IS AN ERROR FOR BOTH POINTS TO INDICATE A BREAK.
648 map_render_segment(GdkGC * gc_norm, GdkGC * gc_alt, guint unitx1, guint unity1, guint unitx2, guint unity2)
652 x2 = unit2bufx(unitx2);
653 y2 = unit2bufy(unity2);
654 /* Make sure this circle will be visible. */
655 if ((x2 < BUF_WIDTH_PIXELS) && (y2 < BUF_HEIGHT_PIXELS))
656 gdk_draw_arc(_map_pixmap, gc_alt, FALSE, /* FALSE: not filled. */
657 x2 - _draw_width, y2 - _draw_width, 2 * _draw_width, 2 * _draw_width, 0, /* start at 0 degrees. */
659 } else if (!unity2) {
661 x1 = unit2bufx(unitx1);
662 y1 = unit2bufy(unity1);
663 /* Make sure this circle will be visible. */
664 if ((x1 < BUF_WIDTH_PIXELS) && ((unsigned)y1 < BUF_HEIGHT_PIXELS))
665 gdk_draw_arc(_map_pixmap, gc_alt, FALSE, /* FALSE: not filled. */
666 x1 - _draw_width, y1 - _draw_width, 2 * _draw_width, 2 * _draw_width, 0, /* start at 0 degrees. */
670 x1 = unit2bufx(unitx1);
671 y1 = unit2bufy(unity1);
672 x2 = unit2bufx(unitx2);
673 y2 = unit2bufy(unity2);
674 /* Make sure this line could possibly be visible. */
675 if (!((x1 > BUF_WIDTH_PIXELS && x2 > BUF_WIDTH_PIXELS)
676 || (x1 < 0 && x2 < 0)
677 || (y1 > BUF_HEIGHT_PIXELS && y2 > BUF_HEIGHT_PIXELS)
678 || (y1 < 0 && y2 < 0)))
679 gdk_draw_line(_map_pixmap, gc_norm, x1, y1, x2, y2);
684 * Render all track data onto the _map_pixmap. Note that this does not
685 * clear the pixmap of previous track data (use map_force_redraw() for
686 * that), and also note that this method does not queue any redraws, so it
687 * is up to the caller to decide which part of the track really needs to be
691 map_render_path(Path * path, GdkGC ** gc)
696 /* gc is a pointer to the first GC to use (for plain points). (gc + 1)
697 * is a pointer to the GC to use for waypoints, and (gc + 2) is a pointer
698 * to the GC to use for breaks. */
700 /* else there is a route to draw. */
701 for (curr = path->head, wcurr = path->whead; curr++ != path->tail;) {
702 /* Draw the line from (curr - 1) to (curr). */
703 map_render_segment(gc[0], gc[2],
704 curr[-1].unitx, curr[-1].unity, curr->unitx,
707 /* Now, check if curr is a waypoint. */
708 if (wcurr && wcurr <= path->wtail && wcurr->point == curr) {
709 guint x1 = unit2bufx(wcurr->point->unitx);
710 guint y1 = unit2bufy(wcurr->point->unity);
711 if ((x1 < BUF_WIDTH_PIXELS)
712 && (y1 < BUF_HEIGHT_PIXELS)) {
713 gdk_draw_arc(_map_pixmap, gc[1], FALSE, /* FALSE: not filled. */
714 x1 - _draw_width, y1 - _draw_width, 2 * _draw_width, 2 * _draw_width, 0, /* start at 0 degrees. */
723 map_render_paths(void)
725 if ((_show_tracks & ROUTES_MASK) && _route.head != _route.tail) {
726 map_render_path(&_route, _gc + COLORABLE_ROUTE);
728 /* Now, draw the next waypoint on top of all other waypoints. */
730 guint x1 = unit2bufx(_next_way->point->unitx);
731 guint y1 = unit2bufy(_next_way->point->unity);
733 if ((x1 < BUF_WIDTH_PIXELS) && (y1 < BUF_HEIGHT_PIXELS)) {
734 /* Draw the next waypoint as a break. */
735 gdk_draw_arc(_map_pixmap, _gc[COLORABLE_ROUTE_BREAK], FALSE, /* FALSE: not filled. */
736 x1 - _draw_width, y1 - _draw_width, 4 * _draw_width, 4 * _draw_width, 0, /* start at 0 degrees. */
741 if (_show_tracks & TRACKS_MASK)
742 map_render_path(&_track, _gc + COLORABLE_TRACK);
750 map_follow_move_cb(GtkWidget *widget, GdkEventMotion *event)
752 GdkModifierType state;
757 if (event->is_hint) {
758 gdk_window_get_pointer(event->window, &xx, &yy, &state);
759 state = event->state;
763 state = event->state;
766 unitx = x2unit((gint) (xx - before[0]));
767 unity = y2unit((gint) (yy - before[1]));
768 cx = unit2x(_center.unitx);
769 cy = unit2y(_center.unity);
771 map_center_unit(x2unit((gint) (cx - (xx - before[0]))), y2unit((gint) (cy - (yy - before[1]))));
779 map_key_zoom_timeout(void)
781 if (_key_zoom_new < _zoom) {
782 /* We're currently zooming in (_zoom is decreasing). */
783 guint test = _key_zoom_new - _curr_repo->view_zoom_steps;
785 _key_zoom_new = test;
789 /* We're currently zooming out (_zoom is increasing). */
790 guint test = _key_zoom_new + _curr_repo->view_zoom_steps;
792 _key_zoom_new = test;
797 /* We can zoom more - tell them how much they're zooming. */
800 snprintf(buffer, sizeof(buffer), "%s %d", _("Zoom to Level"), _key_zoom_new);
801 MACRO_BANNER_SHOW_INFO(_window, buffer);
807 map_scale_draw(GdkEventExpose *event)
811 gdouble lat1, lon1, lat2, lon2;
814 gdk_rectangle_intersect(&event->area, &_scale_rect, &event->area);
816 if (event->area.width && event->area.height) {
817 gdk_draw_rectangle(_map_widget->window,
819 bg_gc[GTK_WIDGET_STATE(_map_widget)],
820 TRUE, _scale_rect.x, _scale_rect.y,
823 gdk_draw_rectangle(_map_widget->window,
825 fg_gc[GTK_WIDGET_STATE(_map_widget)],
826 FALSE, _scale_rect.x, _scale_rect.y,
830 /* Now calculate and draw the distance. */
831 unit2latlon(_center.unitx - pixel2unit(SCALE_WIDTH / 2 - 4), _center.unity, lat1, lon1);
832 unit2latlon(_center.unitx + pixel2unit(SCALE_WIDTH / 2 - 4), _center.unity, lat2, lon2);
833 distance = calculate_distance(lat1, lon1, lat2, lon2) * UNITS_CONVERT[_units];
836 snprintf(buffer, sizeof(buffer),
837 "%0.2f %s", distance,
839 else if (distance < 10.f)
840 snprintf(buffer, sizeof(buffer),
841 "%0.1f %s", distance,
844 snprintf(buffer, sizeof(buffer),
848 pango_layout_set_text(_scale_layout, buffer, -1);
849 pango_layout_get_pixel_size(_scale_layout, &width, NULL);
851 /* Draw the layout itself. */
852 gdk_draw_layout(_map_widget->window,
854 fg_gc[GTK_WIDGET_STATE(_map_widget)],
856 (_scale_rect.width - width) / 2,
857 _scale_rect.y, _scale_layout);
859 /* Draw little hashes on the ends. */
860 gdk_draw_line(_map_widget->window,
862 fg_gc[GTK_WIDGET_STATE(_map_widget)],
865 _scale_rect.height / 2 - 4,
868 _scale_rect.height / 2 + 4);
869 gdk_draw_line(_map_widget->window,
871 fg_gc[GTK_WIDGET_STATE(_map_widget)],
874 _scale_rect.height / 2,
876 (_scale_rect.width - width) / 2 - 4,
878 _scale_rect.height / 2);
879 gdk_draw_line(_map_widget->window,
881 fg_gc[GTK_WIDGET_STATE(_map_widget)],
883 _scale_rect.width - 4,
885 _scale_rect.height / 2 - 4,
887 _scale_rect.width - 4,
889 _scale_rect.height / 2 + 4);
890 gdk_draw_line(_map_widget->window,
892 fg_gc[GTK_WIDGET_STATE(_map_widget)],
894 _scale_rect.width - 4,
896 _scale_rect.height / 2,
898 (_scale_rect.width + width) / 2 + 4,
900 _scale_rect.height / 2);
905 map_cb_expose(GtkWidget * widget, GdkEventExpose * event)
907 gdk_draw_drawable(GDK_DRAWABLE(_map_widget->window),
910 event->area.x + _offsetx, event->area.y + _offsety,
911 event->area.x, event->area.y,
912 event->area.width, event->area.height);
915 /* Draw scale, if necessary. */
917 map_scale_draw(event);
928 nzoom = _zoom + zdir;
930 if ((nzoom > 0) && (nzoom < MAX_ZOOM - 1)) {
931 snprintf(buffer, sizeof(buffer), "%s %d", _("Zoom to Level"), nzoom);
932 MACRO_BANNER_SHOW_INFO(_window, buffer);
946 if (zoom_timeout_sid==0)
949 z=(z+_gps.speed+1)/5;
950 if (z>5) z=5.0; else if (z<1) z=1.0;
953 g_printf("Setting autozoom to: %f %d S:%f\n", z, iz, _gps.speed);
966 map_set_autozoom(gboolean az)
969 MACRO_BANNER_SHOW_INFO(_window, "Autozoom enabled");
970 zoom_timeout_sid=g_timeout_add(_gps.speed<5 ? 2000 : 5000, (GSourceFunc) map_autozoomer, NULL);
973 if (zoom_timeout_sid) {
974 g_source_remove(zoom_timeout_sid);
976 MACRO_BANNER_SHOW_INFO(_window, "Autozoom disabled");
984 map_draw_route(gint x, gint y)
986 cmenu_route_add_way(x, y);
990 map_draw_track(gint x, gint y)
992 _pos.unitx = x2unit((gint) (x + 0.5));
993 _pos.unity = y2unit((gint) (y + 0.5));
994 unit2latlon(_pos.unitx, _pos.unity, _gps.lat, _gps.lon);
996 integerize_data(&_gps, &_pos);
997 track_add(time(NULL), FALSE);
1002 map_set_place_information(osm_way *s, osm_place *mp, osm_place *sp)
1006 if (!s && !mp && !sp) {
1007 snprintf(buffer, sizeof(buffer), _("Unknown location"));
1010 snprintf(buffer, sizeof(buffer), "%s%s%s%s%s%s%s%s%s%s",
1011 s ? s->name ? s->name : "" : "",
1012 s ? s->ref ? ", " : "" : "",
1013 s ? s->ref ? s->ref : "" : "",
1014 s ? s->int_ref ? " (" : "" : "",
1015 s ? s->int_ref ? s->int_ref : "" : "",
1016 s ? s->int_ref ? ") " : "" : "",
1017 (sp && sp->name) ? " in " : "",
1018 (sp && sp->name) ? sp->name : "",
1019 (mp && mp->name) ? " in " : "",
1020 (mp && mp->name) ? mp->name : "");
1022 gtk_label_set_label(GTK_LABEL(info_banner.location), buffer);
1025 #define KM10KNOTS 5.39956803
1028 map_update_destination(gdouble lat, gdouble lon)
1032 static gdouble prev_dt=99999.0;
1033 static gboolean dest_reached=FALSE;
1037 dt=calculate_distance(lat, lon, _dest.lat, _dest.lon);
1038 dh=calculate_course(lat, lon, _dest.lat, _dest.lon);
1039 snprintf(buffer, sizeof(buffer), "%.02f %s (%0.02f)", dt * UNITS_CONVERT[_units], UNITS_TEXT[_units], dh<0 ? 360+dh : dh);
1040 gtk_label_set_label(GTK_LABEL(info_banner.distance), buffer);
1041 if (dt<0.005 && dest_reached==FALSE) {
1043 speak_text("You have reached your destination.");
1045 } else if (dt<prev_dt-KM10KNOTS) {
1046 if (_center_mode>0) {
1047 snprintf(buffer, sizeof(buffer), "Distance to destination: %.02f %s", dt * UNITS_CONVERT[_units], UNITS_TEXT[_units]);
1051 } else if (dt>prev_dt+KM10KNOTS/4) {
1058 g_printf("%f (Prev:%f)\n", prev_dt, dt);
1063 gtk_label_set_label(GTK_LABEL(info_banner.distance), "");
1065 gtk_compass_set_dest_heading(_gps_compass, _dest.valid, (gfloat)dh);
1066 gtk_compass_set_dest_heading(_tab_compass, _dest.valid, (gfloat)dh);
1068 if (_next_way && _next_way->point) {
1069 gdouble wp_lat, wp_lon;
1072 unit2latlon(_next_way->point->unitx,_next_way->point->unity, wp_lat, wp_lon);
1073 wc=calculate_course(lat, lon, wp_lat, wp_lon);
1074 gtk_compass_set_way_heading(_gps_compass, TRUE, wc);
1075 gtk_compass_set_way_heading(_tab_compass, TRUE, wc);
1077 gtk_compass_set_way_heading(_gps_compass, FALSE, 0);
1078 gtk_compass_set_way_heading(_tab_compass, FALSE, 0);
1084 map_update_location(gint x, gint y, gboolean force)
1088 static gboolean inp=FALSE;
1090 /* We run the gtk mainloop in progress callback so we can be called again, we don't like that */
1095 unit2latlon(x, y, lat, lon);
1096 ilat=lat2mp_int(lat);
1097 ilon=lon2mp_int(lon);
1099 if (_gps.fix>1 && !force)
1100 osm_set_way_range_from_speed(_gps.speed);
1102 osm_set_way_range(OSM_RANGE_WAY/4);
1104 osm_progress_set_widget(_db, _progress_item);
1105 _map_location_known=osm_get_location_data(ilat, ilon, &map_loc);
1106 _map_location_dist=map_loc.street ? map_loc.street->dist : 900000.0;
1108 map_set_place_information(map_loc.street, map_loc.primary, map_loc.secondary);
1110 map_set_place_information(NULL, NULL, NULL);
1111 osm_progress_set_widget(_db, NULL);
1113 map_update_destination(lat, lon);
1119 * Mouse scroller zoom in/out callback
1122 map_cb_scroll_event(GtkWidget * widget, GdkEventScroll * event)
1124 if (event->direction == GDK_SCROLL_UP) {
1125 map_center_unit(x2unit((gint) (event->x + 0.5)), y2unit((gint) (event->y + 0.5)));
1126 map_set_zoom(_zoom - 1);
1127 } else if (event->direction == GDK_SCROLL_DOWN) {
1128 map_center_unit(x2unit((gint) (event->x + 0.5)), y2unit((gint) (event->y + 0.5)));
1129 map_set_zoom(_zoom + 1);
1135 * Start map drag operation
1138 map_drag_start(gint x,gint y)
1142 before[0] = press[0];
1143 before[1] = press[1];
1144 _center_mode=CENTER_MANUAL;
1145 map_drag_id=g_signal_connect(G_OBJECT(_map_widget), "motion_notify_event", G_CALLBACK(map_follow_move_cb), NULL);
1149 * Stop map drag operation
1152 map_drag_stop(gint x, gint y)
1159 g_signal_handler_disconnect(G_OBJECT(_map_widget), map_drag_id);
1170 map_cb_button_press(GtkWidget * widget, GdkEventButton * event)
1176 _cmenu_position_x = event->x + 0.5;
1177 _cmenu_position_y = event->y + 0.5;
1179 switch (event->button) {
1181 if (event->type==GDK_2BUTTON_PRESS) {
1182 map_center_unit(x2unit((gint) (event->x + 0.5)), y2unit((gint) (event->y + 0.5)));
1183 map_set_zoom(_zoom - 1);
1186 if (event->type==GDK_3BUTTON_PRESS)
1189 ux=x2unit(_cmenu_position_x);
1190 uy=y2unit(_cmenu_position_y);
1192 unit2latlon(ux, uy, lat, lon);
1193 if (map_poi_find_at_latlon(lat, lon, &poi_id)==TRUE) {
1194 g_printf("POI: %d\n", poi_id);
1196 map_drag_start(event->x, event->y);
1200 map_set_zoom(_zoom - 1);
1204 gtk_menu_popup(GTK_MENU(_menu_map), NULL, NULL, NULL, NULL, event->button, gtk_get_current_event_time());
1208 /* Return FALSE to allow context menu to work. */
1213 map_cb_button_release(GtkWidget *widget, GdkEventButton *event)
1215 switch (event->button) {
1218 set_action_activate("autocenter_none", TRUE);
1221 case MAP_MODE_DRAW_TRACK:
1222 map_draw_track(event->x, event->y);
1224 case MAP_MODE_DRAW_ROUTE:
1225 map_draw_route(event->x, event->y);
1227 case MAP_MODE_SET_ROUTE_FROM:
1228 case MAP_MODE_SET_ROUTE_POINT:
1229 case MAP_MODE_SET_ROUTE_TO:
1232 map_drag_stop(event->x, event->y);
1234 g_idle_add_full(G_PRIORITY_HIGH_IDLE,(GSourceFunc)map_update_location_from_center, NULL, NULL);
1245 /* Return FALSE to avoid context menu (if it hasn't popped up already). */