14 #include <glib/gstdio.h>
17 #include <libgnomevfs/gnome-vfs.h>
18 #include <gconf/gconf-client.h>
19 #include <libxml/parser.h>
20 #include <curl/multi.h>
25 #include "hildon-mapper.h"
37 #include "mapper-types.h"
38 #include "ui-common.h"
42 #include "map-download.h"
44 Point _min_center = { -1, -1 };
45 Point _max_center = { -1, -1 };
46 Point _focus = { -1, -1 };
48 /** The "base tile" is the upper-left tile in the pixmap. */
49 guint _base_tilex = -5;
50 guint _base_tiley = -5;
52 guint _zoom = 3; /* zoom level, from 0 to MAX_ZOOM. */
53 Point _center = { -1, -1 }; /* current center location, X. */
55 CenterMode _center_mode = CENTER_LEAD;
57 static guint press[2] = { 0, 0 };
58 static guint release[2] = { 0, 0 };
59 static guint before[2] = { 0, 0 };
62 /** VARIABLES FOR ACCESSING THE LOCATION/BOUNDS OF THE CURRENT MARK. */
67 static gint _mark_minx;
68 static gint _mark_miny;
69 static gint _mark_width;
70 static gint _mark_height;
72 static gint zoom_timeout_sid=0;
73 static gint map_mode=0;
75 static osm_location map_loc = {NULL, NULL, NULL, FALSE, FALSE, 0, 0, 0.0, 0.0, 0 };
77 typedef struct _map_tile_rdata map_tile_rdata;
78 struct _map_tile_rdata {
79 guint tilex, tiley, zoom;
83 void map_render_paths();
84 void map_force_redraw();
85 static void map_update_location(gint x, gint y, gboolean force);
86 void map_draw_position_icon(Position *pos);
89 map_cb_configure(GtkWidget * widget, GdkEventConfigure * event)
91 _screen_width_pixels = _map_widget->allocation.width;
92 _screen_height_pixels = _map_widget->allocation.height;
93 _screen_grids_halfwidth = pixel2grid(_screen_width_pixels) / 2;
94 _screen_grids_halfheight = pixel2grid(_screen_height_pixels) / 2;
96 /* Set _scale_rect. */
97 _scale_rect.x = (_screen_width_pixels - SCALE_WIDTH) / 2;
98 _scale_rect.width = SCALE_WIDTH;
99 pango_layout_set_text(_scale_layout, "0", -1);
100 pango_layout_get_pixel_size(_scale_layout, NULL, &_scale_rect.height);
101 _scale_rect.y = _screen_height_pixels - _scale_rect.height - 1;
103 MACRO_RECALC_FOCUS_BASE();
104 MACRO_RECALC_FOCUS_SIZE();
106 _min_center.unitx = pixel2unit(grid2pixel(_screen_grids_halfwidth));
107 _min_center.unity = pixel2unit(grid2pixel(_screen_grids_halfheight));
108 _max_center.unitx = WORLD_SIZE_UNITS - grid2unit(_screen_grids_halfwidth) - 1;
109 _max_center.unity = WORLD_SIZE_UNITS - grid2unit(_screen_grids_halfheight) - 1;
111 map_center_unit(_center.unitx, _center.unity);
117 * Draw the current mark (representing the current GPS location) onto
118 * _map_widget. This method does not queue the draw area.
123 gdk_draw_arc(_map_widget->window, _conn_state == RCVR_FIXED ? _gc[COLORABLE_MARK] : _gc[COLORABLE_MARK_OLD], FALSE,
124 _mark_x1 - _draw_width, _mark_y1 - _draw_width,
125 2 * _draw_width, 2 * _draw_width, 0, 360 * 64);
126 gdk_draw_line(_map_widget->window,
127 _conn_state == RCVR_FIXED ? (_show_velvec ? _gc[COLORABLE_MARK_VELOCITY] : _gc[COLORABLE_MARK]) : _gc[COLORABLE_MARK_OLD],
128 _mark_x1, _mark_y1, _mark_x2, _mark_y2);
132 * "Set" the mark, which translates the current GPS position into on-screen
133 * units in preparation for drawing the mark with map_draw_mark().
138 _mark_x1 = unit2x(_pos.unitx);
139 _mark_y1 = unit2y(_pos.unity);
140 _mark_x2 = _mark_x1 + (_show_velvec ? _gps.vel_offsetx : 0);
141 _mark_y2 = _mark_y1 + (_show_velvec ? _gps.vel_offsety : 0);
142 _mark_minx = MIN(_mark_x1, _mark_x2) - (2 * _draw_width);
143 _mark_miny = MIN(_mark_y1, _mark_y2) - (2 * _draw_width);
144 _mark_width = abs(_mark_x1 - _mark_x2) + (4 * _draw_width);
145 _mark_height = abs(_mark_y1 - _mark_y2) + (4 * _draw_width);
149 * Do an in-place scaling of a pixbuf's pixels at the given ratio from the
150 * given source location. It would have been nice if gdk_pixbuf provided
151 * this method, but I guess it's not general-purpose enough.
154 map_pixbuf_scale_inplace(GdkPixbuf * pixbuf, guint ratio_p2, guint src_x, guint src_y)
156 guint dest_x = 0, dest_y = 0, dest_dim = TILE_SIZE_PIXELS;
157 guint rowstride = gdk_pixbuf_get_rowstride(pixbuf);
158 guint n_channels = gdk_pixbuf_get_n_channels(pixbuf);
159 guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
161 /* Sweep through the entire dest area, copying as necessary, but
162 * DO NOT OVERWRITE THE SOURCE AREA. We'll copy it afterward. */
164 guint src_dim = dest_dim >> ratio_p2;
165 guint src_endx = src_x - dest_x + src_dim;
168 for (y = dest_dim - 1; y >= 0; y--) {
169 guint src_offset_y, dest_offset_y;
171 src_offset_y = (src_y + (y >> ratio_p2)) * rowstride;
172 dest_offset_y = (dest_y + y) * rowstride;
175 if ((unsigned)(dest_y + y - src_y) < src_dim && (unsigned)(dest_x + x - src_x) < src_dim)
178 for (; x >= 0; x--) {
179 guint src_offset, dest_offset, i;
181 src_offset = src_offset_y + (src_x + (x >> ratio_p2)) * n_channels;
182 dest_offset = dest_offset_y + (dest_x + x) * n_channels;
184 pixels[dest_offset] = pixels[src_offset];
185 for (i = n_channels - 1; i; i--)
186 pixels[dest_offset + i] = pixels[src_offset + i];
188 if ((unsigned)(dest_y + y - src_y) < src_dim && x == src_endx)
192 /* Reuse src_dim and src_endx to store new src_x and src_y. */
193 src_dim = src_x + ((src_x - dest_x) >> ratio_p2);
194 src_endx = src_y + ((src_y - dest_y) >> ratio_p2);
200 while ((dest_dim >>= ratio_p2) > 1);
204 * Trim pixbufs that are bigger than tiles. (Those pixbufs result, when
205 * captions should be cut off.)
208 pixbuf_trim(GdkPixbuf * pixbuf)
210 GdkPixbuf *mpixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, gdk_pixbuf_get_has_alpha(pixbuf),
211 8, TILE_SIZE_PIXELS, TILE_SIZE_PIXELS);
213 gdk_pixbuf_copy_area(pixbuf,
214 (gdk_pixbuf_get_width(pixbuf) - TILE_SIZE_PIXELS) / 2,
215 (gdk_pixbuf_get_height(pixbuf) - TILE_SIZE_PIXELS) / 2,
217 TILE_SIZE_PIXELS, mpixbuf, 0, 0);
219 g_object_unref(pixbuf);
224 map_render_tile(guint tilex, guint tiley, guint destx, guint desty, gboolean fast_fail)
226 GdkPixbuf *pixbuf = NULL;
227 gchar buffer[BUFFER_SIZE];
228 GError *error = NULL;
231 if (tilex < _world_size_tiles && tiley < _world_size_tiles) {
232 /* The tile is possible. */
233 for (zoff = (_curr_repo->double_size ? 1 : 0);
234 !pixbuf && (_zoom + zoff) <= MAX_ZOOM
235 && zoff <= TILE_SIZE_P2; zoff += 1) {
236 snprintf(buffer, sizeof(buffer), "%s/%u/%u/%u.jpg",
237 _curr_repo->cache_dir, _zoom + zoff,
238 (tilex >> zoff), (tiley >> zoff));
240 pixbuf = gdk_pixbuf_new_from_file(buffer, &error);
241 if (error || !pixbuf) {
242 /* Delete so we try again some other day. */
247 /* Download, if we should. */
248 if (_auto_download && _curr_repo->type != REPOTYPE_NONE &&
249 !((_zoom + zoff - (_curr_repo->double_size ? 1 : 0)) % _curr_repo->dl_zoom_steps)) {
251 map_initiate_download(tilex >> zoff,
254 -INITIAL_DOWNLOAD_RETRIES);
258 /* Check if we need to trim. */
259 if (gdk_pixbuf_get_width(pixbuf) != TILE_SIZE_PIXELS || gdk_pixbuf_get_height(pixbuf) != TILE_SIZE_PIXELS)
260 pixbuf = pixbuf_trim(pixbuf);
262 /* Check if we need to blit. */
264 map_pixbuf_scale_inplace(pixbuf, zoff,
265 (tilex - ((tilex >> zoff) << zoff)) << (TILE_SIZE_P2 - zoff),
266 (tiley - ((tiley >> zoff) << zoff)) << (TILE_SIZE_P2 - zoff));
273 gdk_draw_pixbuf(_map_pixmap,
275 pixbuf, 0, 0, destx, desty,
276 TILE_SIZE_PIXELS, TILE_SIZE_PIXELS,
277 GDK_RGB_DITHER_NONE, 0, 0);
278 g_object_unref(pixbuf);
280 gdk_draw_rectangle(_map_pixmap, _map_widget->style->black_gc,
281 TRUE, destx, desty, TILE_SIZE_PIXELS, TILE_SIZE_PIXELS);
286 map_render_tile_idle(map_tile_rdata *mtr)
288 map_render_tile(mtr->tilex, mtr->tiley, mtr->destx, mtr->desty, FALSE);
289 gtk_widget_queue_draw_area(_map_widget, mtr->destx, mtr->desty, TILE_SIZE_PIXELS, TILE_SIZE_PIXELS);
291 g_slice_free(map_tile_rdata, mtr);
296 map_render_data(void)
303 map_draw_position_icon(&_home);
307 * Force a redraw of the entire _map_pixmap, including fetching the
308 * background maps from disk and redrawing the tracks on top of them.
315 for (new_y = 0; new_y < BUF_HEIGHT_TILES; ++new_y)
316 for (new_x = 0; new_x < BUF_WIDTH_TILES; ++new_x) {
317 map_render_tile(_base_tilex + new_x,
319 new_x * TILE_SIZE_PIXELS,
320 new_y * TILE_SIZE_PIXELS, FALSE);
323 MACRO_QUEUE_DRAW_AREA();
327 * Set the current zoom level. If the given zoom level is the same as the
328 * current zoom level, or if the new zoom is invalid
329 * (not MIN_ZOOM <= new_zoom < MAX_ZOOM), then this method does nothing.
332 map_set_zoom(guint new_zoom)
334 /* Note that, since new_zoom is a guint and MIN_ZOOM is 0, this if
335 * condition also checks for new_zoom >= MIN_ZOOM. */
336 if (new_zoom > (MAX_ZOOM - 1))
338 if (new_zoom == _zoom)
341 _zoom = new_zoom / _curr_repo->view_zoom_steps * _curr_repo->view_zoom_steps;
342 _world_size_tiles = unit2tile(WORLD_SIZE_UNITS);
344 /* If we're leading, update the center to reflect new zoom level. */
345 MACRO_RECALC_CENTER(_center.unitx, _center.unity);
347 /* Update center bounds to reflect new zoom level. */
348 _min_center.unitx = pixel2unit(grid2pixel(_screen_grids_halfwidth));
349 _min_center.unity = pixel2unit(grid2pixel(_screen_grids_halfheight));
350 _max_center.unitx = WORLD_SIZE_UNITS - grid2unit(_screen_grids_halfwidth) - 1;
351 _max_center.unity = WORLD_SIZE_UNITS - grid2unit(_screen_grids_halfheight) - 1;
353 BOUND(_center.unitx, _min_center.unitx, _max_center.unitx);
354 BOUND(_center.unity, _min_center.unity, _max_center.unity);
356 _base_tilex = grid2tile((gint) pixel2grid((gint) unit2pixel((gint) _center.unitx))
357 - (gint) _screen_grids_halfwidth);
358 _base_tiley = grid2tile(pixel2grid(unit2pixel(_center.unity))
359 - _screen_grids_halfheight);
361 /* New zoom level, so we can't reuse the old buffer's pixels. */
362 /* Update state variables. */
363 MACRO_RECALC_OFFSET();
364 MACRO_RECALC_FOCUS_BASE();
365 MACRO_RECALC_FOCUS_SIZE();
372 * Center the view on the given unitx/unity.
375 map_center_unit(guint new_center_unitx, guint new_center_unity)
377 gint new_base_tilex, new_base_tiley;
379 guint j, k, base_new_x, base_old_x, old_x, old_y, iox, ioy;
381 /* Assure that _center.unitx/y are bounded. */
382 BOUND(new_center_unitx, _min_center.unitx, _max_center.unitx);
383 BOUND(new_center_unity, _min_center.unity, _max_center.unity);
385 _center.unitx = new_center_unitx;
386 _center.unity = new_center_unity;
388 new_base_tilex = grid2tile((gint) pixel2grid((gint)unit2pixel((gint) _center.unitx))
389 - (gint)_screen_grids_halfwidth);
390 new_base_tiley = grid2tile(pixel2grid(unit2pixel(_center.unity))
391 - _screen_grids_halfheight);
393 /* Same zoom level, so it's likely that we can reuse some of the old
394 * buffer's pixels. */
396 if (new_base_tilex != _base_tilex || new_base_tiley != _base_tiley) {
397 /* If copying from old parts to new parts, we need to make sure we
398 * don't overwrite the old parts when copying, so set up new_x,
399 * new_y, old_x, old_y, iox, and ioy with that in mind. */
400 if (new_base_tiley < _base_tiley) {
401 /* New is lower than old - start at bottom and go up. */
402 new_y = BUF_HEIGHT_TILES - 1;
405 /* New is higher than old - start at top and go down. */
409 if (new_base_tilex < _base_tilex) {
410 /* New is righter than old - start at right and go left. */
411 base_new_x = BUF_WIDTH_TILES - 1;
414 /* New is lefter than old - start at left and go right. */
419 /* Iterate over the y tile values. */
420 old_y = new_y + new_base_tiley - _base_tiley;
421 base_old_x = base_new_x + new_base_tilex - _base_tilex;
422 _base_tilex = new_base_tilex;
423 _base_tiley = new_base_tiley;
424 for (j = 0; j < BUF_HEIGHT_TILES; ++j, new_y += ioy, old_y += ioy) {
427 /* Iterate over the x tile values. */
428 for (k = 0; k < BUF_WIDTH_TILES;
429 ++k, new_x += iox, old_x += iox) {
430 /* Can we get this grid block from the old buffer?. */
431 if (old_x >= 0 && old_x < BUF_WIDTH_TILES
432 && old_y >= 0 && old_y < BUF_HEIGHT_TILES) {
433 /* Copy from old buffer to new buffer. */
434 gdk_draw_drawable(_map_pixmap,
437 old_x * TILE_SIZE_PIXELS,
438 old_y * TILE_SIZE_PIXELS,
439 new_x * TILE_SIZE_PIXELS,
440 new_y * TILE_SIZE_PIXELS,
444 map_render_tile(new_base_tilex + new_x,
445 new_base_tiley + new_y,
446 new_x * TILE_SIZE_PIXELS,
447 new_y * TILE_SIZE_PIXELS,
455 MACRO_RECALC_OFFSET();
456 MACRO_RECALC_FOCUS_BASE();
459 MACRO_QUEUE_DRAW_AREA();
463 * Pan the view by the given number of units in the X and Y directions.
466 map_pan(gint delta_unitx, gint delta_unity)
468 if (_center_mode > 0)
469 set_action_activate("autocenter_none", TRUE);
471 map_center_unit(_center.unitx + delta_unitx, _center.unity + delta_unity);
475 * Initiate a move of the mark from the old location to the current
476 * location. This function queues the draw area of the old mark (to force
477 * drawing of the background map), then updates the mark, then queus the
478 * draw area of the new mark.
483 /* Just queue the old and new draw areas. */
484 gtk_widget_queue_draw_area(_map_widget,
485 _mark_minx<0 ? 0 : _mark_minx,
486 _mark_miny<0 ? 0 : _mark_miny,
487 _mark_width, _mark_height);
489 gtk_widget_queue_draw_area(_map_widget,
490 _mark_minx<0 ? 0 : _mark_minx,
491 _mark_miny<0 ? 0 : _mark_miny,
492 _mark_width, _mark_height);
496 map_update_location_from_gps()
498 map_update_location(_pos.unitx, _pos.unity, FALSE);
503 map_update_location_from_center()
505 /* Force re-validation of place if user is clicking around */
507 map_update_location(_center.unitx, _center.unity, TRUE);
512 * Draw given pixbuf on map, centered on x,y
515 map_draw_pixbuf(guint unitx, guint unity, GdkPixbuf *p)
518 x = unit2bufx(unitx);
519 y = unit2bufy(unity);
521 gdk_draw_pixbuf(_map_pixmap, _gc[COLORABLE_POI],
523 x - gdk_pixbuf_get_width(p) / 2,
524 y - gdk_pixbuf_get_height(p) / 2,
525 -1, -1, GDK_RGB_DITHER_NONE, 0, 0);
529 * Draw an icon on given Position.
531 * XXX: don't hardcode as home.png !
534 map_draw_position_icon(Position *pos)
539 latlon2unit(pos->lat, pos->lon, x, y);
540 p=gdk_pixbuf_new_from_file(DATADIR "/pixmaps/mapper/home.png", NULL);
542 map_draw_pixbuf(x,y,p);
549 * Make sure the mark is up-to-date. This function triggers a panning of
550 * the view if the mark is appropriately close to the edge of the view.
555 guint new_center_unitx;
556 guint new_center_unity;
558 MACRO_RECALC_CENTER(new_center_unitx, new_center_unity);
560 if ((new_center_unitx - _focus.unitx) < _focus_unitwidth
561 && (new_center_unity - _focus.unity) < _focus_unitheight)
562 /* We're not changing the view - just move the mark. */
565 map_center_unit(new_center_unitx, new_center_unity);
567 /* Draw speed info */
572 g_idle_add((GSourceFunc)map_update_location_from_gps, NULL);
576 * Render a single track line to _map_pixmap. If either point on the line
577 * is a break (defined as unity == 0), a circle is drawn at the other point.
578 * IT IS AN ERROR FOR BOTH POINTS TO INDICATE A BREAK.
581 map_render_segment(GdkGC * gc_norm, GdkGC * gc_alt,
582 guint unitx1, guint unity1, guint unitx2, guint unity2)
584 vprintf("%s()\n", __PRETTY_FUNCTION__);
588 x2 = unit2bufx(unitx2);
589 y2 = unit2bufy(unity2);
590 /* Make sure this circle will be visible. */
591 if ((x2 < BUF_WIDTH_PIXELS)
592 && (y2 < BUF_HEIGHT_PIXELS))
593 gdk_draw_arc(_map_pixmap, gc_alt, FALSE, /* FALSE: not filled. */
594 x2 - _draw_width, y2 - _draw_width, 2 * _draw_width, 2 * _draw_width, 0, /* start at 0 degrees. */
596 } else if (!unity2) {
598 x1 = unit2bufx(unitx1);
599 y1 = unit2bufy(unity1);
600 /* Make sure this circle will be visible. */
601 if ((x1 < BUF_WIDTH_PIXELS)
602 && ((unsigned)y1 < BUF_HEIGHT_PIXELS))
603 gdk_draw_arc(_map_pixmap, gc_alt, FALSE, /* FALSE: not filled. */
604 x1 - _draw_width, y1 - _draw_width, 2 * _draw_width, 2 * _draw_width, 0, /* start at 0 degrees. */
608 x1 = unit2bufx(unitx1);
609 y1 = unit2bufy(unity1);
610 x2 = unit2bufx(unitx2);
611 y2 = unit2bufy(unity2);
612 /* Make sure this line could possibly be visible. */
613 if (!((x1 > BUF_WIDTH_PIXELS && x2 > BUF_WIDTH_PIXELS)
614 || (x1 < 0 && x2 < 0)
615 || (y1 > BUF_HEIGHT_PIXELS && y2 > BUF_HEIGHT_PIXELS)
616 || (y1 < 0 && y2 < 0)))
617 gdk_draw_line(_map_pixmap, gc_norm, x1, y1, x2, y2);
620 vprintf("%s(): return\n", __PRETTY_FUNCTION__);
624 * Render all track data onto the _map_pixmap. Note that this does not
625 * clear the pixmap of previous track data (use map_force_redraw() for
626 * that), and also note that this method does not queue any redraws, so it
627 * is up to the caller to decide which part of the track really needs to be
631 map_render_path(Path * path, GdkGC ** gc)
635 printf("%s()\n", __PRETTY_FUNCTION__);
637 /* gc is a pointer to the first GC to use (for plain points). (gc + 1)
638 * is a pointer to the GC to use for waypoints, and (gc + 2) is a pointer
639 * to the GC to use for breaks. */
641 /* else there is a route to draw. */
642 for (curr = path->head, wcurr = path->whead; curr++ != path->tail;) {
643 /* Draw the line from (curr - 1) to (curr). */
644 map_render_segment(gc[0], gc[2],
645 curr[-1].unitx, curr[-1].unity, curr->unitx,
648 /* Now, check if curr is a waypoint. */
649 if (wcurr && wcurr <= path->wtail && wcurr->point == curr) {
650 guint x1 = unit2bufx(wcurr->point->unitx);
651 guint y1 = unit2bufy(wcurr->point->unity);
652 if ((x1 < BUF_WIDTH_PIXELS)
653 && (y1 < BUF_HEIGHT_PIXELS)) {
654 gdk_draw_arc(_map_pixmap, gc[1], FALSE, /* FALSE: not filled. */
655 x1 - _draw_width, y1 - _draw_width, 2 * _draw_width, 2 * _draw_width, 0, /* start at 0 degrees. */
662 vprintf("%s(): return\n", __PRETTY_FUNCTION__);
668 printf("%s()\n", __PRETTY_FUNCTION__);
670 if ((_show_tracks & ROUTES_MASK) && _route.head != _route.tail) {
671 map_render_path(&_route, _gc + COLORABLE_ROUTE);
673 /* Now, draw the next waypoint on top of all other waypoints. */
675 guint x1 = unit2bufx(_next_way->point->unitx);
676 guint y1 = unit2bufy(_next_way->point->unity);
677 if ((x1 < BUF_WIDTH_PIXELS)
678 && (y1 < BUF_HEIGHT_PIXELS)) {
679 /* Draw the next waypoint as a break. */
680 gdk_draw_arc(_map_pixmap, _gc[COLORABLE_ROUTE_BREAK], FALSE, /* FALSE: not filled. */
681 x1 - _draw_width, y1 - _draw_width, 2 * _draw_width, 2 * _draw_width, 0, /* start at 0 degrees. */
686 if (_show_tracks & TRACKS_MASK)
687 map_render_path(&_track, _gc + COLORABLE_TRACK);
689 vprintf("%s(): return\n", __PRETTY_FUNCTION__);
693 gboolean map_follow_move(GtkWidget * widget, GdkEventMotion * event)
696 GdkModifierType state;
700 if (event->is_hint) {
701 gdk_window_get_pointer(event->window, &xx, &yy, &state);
702 state = event->state;
706 state = event->state;
709 unitx = x2unit((gint) (xx - before[0]));
710 unity = y2unit((gint) (yy - before[1]));
711 cx = unit2x(_center.unitx);
712 cy = unit2y(_center.unity);
714 map_center_unit(x2unit((gint) (cx - (xx - before[0]))),
715 y2unit((gint) (cy - (yy - before[1]))));
723 map_key_zoom_timeout()
725 printf("%s()\n", __PRETTY_FUNCTION__);
726 if (_key_zoom_new < _zoom) {
727 /* We're currently zooming in (_zoom is decreasing). */
728 guint test = _key_zoom_new - _curr_repo->view_zoom_steps;
730 _key_zoom_new = test;
734 /* We're currently zooming out (_zoom is increasing). */
735 guint test = _key_zoom_new + _curr_repo->view_zoom_steps;
737 _key_zoom_new = test;
742 /* We can zoom more - tell them how much they're zooming. */
745 snprintf(buffer, sizeof(buffer), "%s %d", _("Zoom to Level"), _key_zoom_new);
746 MACRO_BANNER_SHOW_INFO(_window, buffer);
748 vprintf("%s(): return\n", __PRETTY_FUNCTION__);
753 map_scale_draw(GdkEventExpose *event)
755 gdk_rectangle_intersect(&event->area, &_scale_rect, &event->area);
757 if (event->area.width && event->area.height) {
758 gdk_draw_rectangle(_map_widget->window,
760 bg_gc[GTK_WIDGET_STATE(_map_widget)],
761 TRUE, _scale_rect.x, _scale_rect.y,
764 gdk_draw_rectangle(_map_widget->window,
766 fg_gc[GTK_WIDGET_STATE(_map_widget)],
767 FALSE, _scale_rect.x, _scale_rect.y,
771 /* Now calculate and draw the distance. */
775 gdouble lat1, lon1, lat2, lon2;
778 unit2latlon(_center.unitx - pixel2unit(SCALE_WIDTH / 2 - 4),
779 _center.unity, lat1, lon1);
780 unit2latlon(_center.unitx + pixel2unit(SCALE_WIDTH / 2 - 4),
781 _center.unity, lat2, lon2);
782 distance = calculate_distance(lat1, lon1, lat2, lon2) * UNITS_CONVERT[_units];
785 snprintf(buffer, sizeof(buffer),
786 "%0.2f %s", distance,
788 else if (distance < 10.f)
789 snprintf(buffer, sizeof(buffer),
790 "%0.1f %s", distance,
793 snprintf(buffer, sizeof(buffer),
797 pango_layout_set_text(_scale_layout, buffer, -1);
798 pango_layout_get_pixel_size(_scale_layout, &width, NULL);
800 /* Draw the layout itself. */
801 gdk_draw_layout(_map_widget->window,
803 fg_gc[GTK_WIDGET_STATE
806 (_scale_rect.width - width) / 2,
807 _scale_rect.y, _scale_layout);
809 /* Draw little hashes on the ends. */
810 gdk_draw_line(_map_widget->window,
812 fg_gc[GTK_WIDGET_STATE
816 _scale_rect.height / 2 - 4,
819 _scale_rect.height / 2 + 4);
820 gdk_draw_line(_map_widget->window,
822 fg_gc[GTK_WIDGET_STATE
826 _scale_rect.height / 2,
828 (_scale_rect.width - width) / 2 -
831 _scale_rect.height / 2);
832 gdk_draw_line(_map_widget->window,
834 fg_gc[GTK_WIDGET_STATE
837 _scale_rect.width - 4,
839 _scale_rect.height / 2 - 4,
841 _scale_rect.width - 4,
843 _scale_rect.height / 2 + 4);
844 gdk_draw_line(_map_widget->window,
846 fg_gc[GTK_WIDGET_STATE
849 _scale_rect.width - 4,
851 _scale_rect.height / 2,
853 (_scale_rect.width + width) / 2 +
856 _scale_rect.height / 2);
862 map_cb_expose(GtkWidget * widget, GdkEventExpose * event)
864 g_printf("%s(%d, %d, %d, %d)\n", __PRETTY_FUNCTION__,
865 event->area.x, event->area.y,
866 event->area.width, event->area.height);
868 gdk_draw_drawable(GDK_DRAWABLE(_map_widget->window),
871 event->area.x + _offsetx, event->area.y + _offsety,
872 event->area.x, event->area.y,
873 event->area.width, event->area.height);
876 /* Draw scale, if necessary. */
878 map_scale_draw(event);
889 nzoom = _zoom + zdir;
891 if ((nzoom > 0) && (nzoom < MAX_ZOOM - 1)) {
892 snprintf(buffer, sizeof(buffer), "%s %d", _("Zoom to Level"), nzoom);
893 MACRO_BANNER_SHOW_INFO(_window, buffer);
907 if (zoom_timeout_sid==0)
910 z=(z+_gps.speed+1)/5;
911 if (z>5) z=5.0; else if (z<1) z=1.0;
914 g_printf("Setting autozoom to: %f %d S:%f\n", z, iz, _gps.speed);
927 map_set_autozoom(gboolean az)
930 MACRO_BANNER_SHOW_INFO(_window, "Autozoom enabled");
931 zoom_timeout_sid=g_timeout_add(_gps.speed<5 ? 2000 : 5000, (GSourceFunc) map_autozoomer, NULL);
934 if (zoom_timeout_sid) {
935 g_source_remove(zoom_timeout_sid);
937 MACRO_BANNER_SHOW_INFO(_window, "Autozoom disabled");
945 map_draw_route(gint x, gint y)
947 cmenu_route_add_way(x, y);
951 map_draw_track(gint x, gint y)
953 _pos.unitx = x2unit((gint) (x + 0.5));
954 _pos.unity = y2unit((gint) (y + 0.5));
955 unit2latlon(_pos.unitx, _pos.unity, _gps.lat, _gps.lon);
957 integerize_data(&_gps, &_pos);
958 track_add(time(NULL), FALSE);
963 map_set_place_information(osm_way *s, osm_place *mp, osm_place *sp)
967 if (!s && !mp && !sp) {
968 snprintf(buffer, sizeof(buffer), _("Unknown location"));
971 snprintf(buffer, sizeof(buffer), "%s%s%s%s%s%s%s%s%s%s",
972 s ? s->name ? s->name : _("unknown") : "?",
973 s ? s->ref ? ", " : "" : "",
974 s ? s->ref ? s->ref : "" : "",
975 s ? s->int_ref ? " (" : "" : "",
976 s ? s->int_ref ? s->int_ref : "" : "",
977 s ? s->int_ref ? ") " : "" : "",
978 (sp && sp->name) ? " in " : "",
979 (sp && sp->name) ? sp->name : "",
980 (mp && mp->name) ? " in " : "",
981 (mp && mp->name) ? mp->name : "");
983 gtk_label_set_label(GTK_LABEL(info_banner.location), buffer);
987 map_update_destination(gdouble lat, gdouble lon)
991 gdouble dt=calculate_distance(lat, lon, _dest.lat, _dest.lon);
992 gdouble dh=calculate_course(lat, lon, _dest.lat, _dest.lon);
993 snprintf(buffer, sizeof(buffer), "%.02f %s (%0.02f)", dt * UNITS_CONVERT[_units], UNITS_TEXT[_units], dh);
994 gtk_label_set_label(GTK_LABEL(info_banner.distance), buffer);
996 gtk_label_set_label(GTK_LABEL(info_banner.distance), "");
1001 map_update_location(gint x, gint y, gboolean force)
1005 static gboolean inp=FALSE;
1007 /* We run the gtk mainloop in progress callback so we can be called again, we don't like that */
1012 unit2latlon(x, y, lat, lon);
1013 ilat=lat2mp_int(lat);
1014 ilon=lon2mp_int(lon);
1016 if (_gps.fix>1 && !force)
1017 osm_set_way_range_from_speed(_gps.speed);
1019 osm_set_way_range(OSM_RANGE_WAY/4);
1021 osm_progress_set_widget(_db, _progress_item);
1022 _map_location_known=osm_get_location_data(ilat, ilon, &map_loc);
1023 _map_location_dist=map_loc.street ? map_loc.street->dist : 900000.0;
1025 map_set_place_information(map_loc.street, map_loc.primary, map_loc.secondary);
1027 map_set_place_information(NULL, NULL, NULL);
1028 osm_progress_set_widget(_db, NULL);
1030 map_update_destination(lat, lon);
1036 map_cb_scroll_event(GtkWidget * widget, GdkEventScroll * event)
1038 if (event->direction == GDK_SCROLL_UP) {
1039 map_center_unit(x2unit((gint) (event->x + 0.5)), y2unit((gint) (event->y + 0.5)));
1040 map_set_zoom(_zoom - 1);
1041 } else if (event->direction == GDK_SCROLL_DOWN) {
1042 map_center_unit(x2unit((gint) (event->x + 0.5)), y2unit((gint) (event->y + 0.5)));
1043 map_set_zoom(_zoom + 1);
1049 map_cb_button_press(GtkWidget * widget, GdkEventButton * event)
1051 printf("%s()\n", __PRETTY_FUNCTION__);
1053 _cmenu_position_x = event->x + 0.5;
1054 _cmenu_position_y = event->y + 0.5;
1056 switch (event->button) {
1058 if (event->type == GDK_2BUTTON_PRESS) {
1059 map_set_zoom(_zoom - 1);
1062 if (event->type == GDK_3BUTTON_PRESS)
1066 press[0] = event->x;
1067 press[1] = event->y;
1068 before[0] = press[0];
1069 before[1] = press[1];
1071 _id = g_signal_connect(G_OBJECT(_map_widget),
1072 "motion_notify_event",
1073 G_CALLBACK(map_follow_move), NULL);
1077 gtk_menu_popup(GTK_MENU(_menu_map), NULL, NULL, NULL, NULL,
1078 event->button, gtk_get_current_event_time());
1083 /* Return FALSE to allow context menu to work. */
1084 vprintf("%s(): return FALSE\n", __PRETTY_FUNCTION__);
1089 map_cb_button_release(GtkWidget * widget, GdkEventButton * event)
1091 switch (event->button) {
1093 if (_center_mode > 0)
1094 set_action_activate("autocenter_none", TRUE);
1097 case MAP_MODE_DRAW_TRACK:
1098 map_draw_track(event->x, event->y);
1100 case MAP_MODE_DRAW_ROUTE:
1101 map_draw_route(event->x, event->y);
1103 case MAP_MODE_SET_ROUTE_FROM:
1104 case MAP_MODE_SET_ROUTE_POINT:
1105 case MAP_MODE_SET_ROUTE_TO:
1110 ux = x2unit((gint) (event->x + 0.5));
1111 uy = y2unit((gint) (event->y + 0.5));
1112 map_update_location(ux, uy);
1114 g_idle_add((GSourceFunc)map_update_location_from_center, NULL);
1117 map_center_unit(x2unit((gint) (event->x + 0.5)),
1118 y2unit((gint) (event->y + 0.5)));
1122 release[0] = event->x;
1123 release[1] = event->y;
1125 g_signal_handler_disconnect(G_OBJECT(_map_widget), _id);
1138 /* Return FALSE to avoid context menu (if it hasn't popped up already). */