Contents of /trunk/src/map.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 78 - (show annotations)
Sun Feb 15 12:07:04 2009 UTC (15 years, 3 months ago) by harbaum
File MIME type: text/plain
File size: 62416 byte(s)
Removal of unused gnomecancas/xml parser code
1 /*
2 * Copyright (C) 2008 Till Harbaum <till@harbaum.org>.
3 *
4 * This file is part of OSM2Go.
5 *
6 * OSM2Go is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * OSM2Go is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with OSM2Go. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "appdata.h"
21
22 #include <gdk/gdkkeysyms.h>
23
24
25 #undef DESTROY_WAIT_FOR_GTK
26
27 static void map_statusbar(map_t *map, map_item_t *map_item) {
28 char *item_str = NULL;
29 item_id_t id = ID_ILLEGAL;
30 tag_t *tag = NULL;
31 char *str = NULL;
32
33 switch(map_item->type) {
34 case MAP_TYPE_NODE:
35 item_str = "Node";
36 id = map_item->node->id;
37 tag = map_item->node->tag;
38 break;
39
40 case MAP_TYPE_WAY:
41 item_str = "Way";
42 id = map_item->way->id;
43 tag = map_item->way->tag;
44 break;
45
46 default:
47 break;
48 }
49
50 gboolean collision = FALSE;
51 tag_t *tags = tag;
52
53 if(id == ID_ILLEGAL)
54 str = g_strdup_printf(_("Unknown item"));
55 else {
56 str = g_strdup_printf("%s #%ld", item_str, id);
57
58 /* add some tags ... */
59 /*
60 * XXX Should we just try to present only the name or the ref (or the
61 * alt_name, old_name, whatever) here? Hurling a load of tags in the
62 * user's face in some unpredictable, uninformative order isn't very
63 * friendly.
64 *
65 * Actually, a tag_short_desc() function would be useful in dialogs
66 * nd user messages too.
67 */
68 while(tag) {
69 if(!collision && info_tag_key_collision(tags, tag))
70 collision = TRUE;
71
72 /* we don't have much space, so ignore created_by tag */
73 if(!osm_is_creator_tag(tag)) {
74 char *old = str;
75 str = g_strdup_printf("%s, %s=%s", old, tag->key, tag->value);
76 g_free(old);
77 }
78 tag = tag->next;
79 }
80 }
81
82 statusbar_set(map->appdata, str, collision);
83 g_free(str);
84 }
85
86 void map_outside_error(appdata_t *appdata) {
87 errorf(GTK_WIDGET(appdata->window),
88 _("Items must not be placed outside the working area!"));
89 }
90
91 void map_item_chain_destroy(map_item_chain_t **chainP) {
92 if(!*chainP) {
93 printf("nothing to destroy!\n");
94 return;
95 }
96
97 #ifdef DESTROY_WAIT_FOR_GTK
98 map_item_chain_t *chain = *chainP;
99 while(chain) {
100 map_item_chain_t *next = chain->next;
101 canvas_item_destroy(chain->map_item->item);
102 chain = next;
103 }
104
105 /* wait until gtks event handling has actually destroyed this item */
106 printf("waiting for item destruction ");
107 while(gtk_events_pending() || *chainP) {
108 putchar('.');
109 gtk_main_iteration();
110 }
111 printf(" ok\n");
112
113 /* the callback routine connected to this item should have been */
114 /* called by now and it has set the chain to NULL */
115
116 #else
117 map_item_chain_t *chain = *chainP;
118 while(chain) {
119 map_item_chain_t *next = chain->next;
120 canvas_item_destroy(chain->map_item->item);
121
122 g_free(chain);
123 chain = next;
124 }
125 *chainP = NULL;
126 #endif
127 }
128
129 static void map_node_select(appdata_t *appdata, node_t *node) {
130 map_t *map = appdata->map;
131 map_item_t *map_item = &map->selected;
132
133 g_assert(!map->highlight);
134
135 map_item->type = MAP_TYPE_NODE;
136 map_item->node = node;
137 map_item->highlight = FALSE;
138
139 /* node may not have any visible representation at all */
140 if(node->map_item_chain)
141 map_item->item = node->map_item_chain->map_item->item;
142 else
143 map_item->item = NULL;
144
145 map_statusbar(map, map_item);
146 icon_bar_map_item_selected(appdata, map_item, TRUE);
147
148 /* highlight node */
149 gint x = map_item->node->lpos.x, y = map_item->node->lpos.y;
150
151 /* create a copy of this map item and mark it as being a highlight */
152 map_item_t *new_map_item = g_new0(map_item_t, 1);
153 memcpy(new_map_item, map_item, sizeof(map_item_t));
154 new_map_item->highlight = TRUE;
155
156 float radius = map->style->highlight.width + map->style->node.radius;
157 if(!node->ways) radius += map->style->node.border_radius;
158 if(node->icon_buf && map->style->icon.enable &&
159 !appdata->settings->no_icons) {
160 gint w = gdk_pixbuf_get_width(map_item->node->icon_buf);
161 gint h = gdk_pixbuf_get_height(map_item->node->icon_buf);
162 /* icons are technically square, so a radius slightly bigger */
163 /* than sqrt(2)*MAX(w,h) should fit nicely */
164 radius = 0.75 * map->style->icon.scale * ((w>h)?w:h);
165 }
166
167 map_hl_circle_new(map, CANVAS_GROUP_NODES_HL, new_map_item,
168 x, y, radius, map->style->highlight.color);
169
170 if(!map_item->item) {
171 /* and draw a fake node */
172 new_map_item = g_new0(map_item_t, 1);
173 memcpy(new_map_item, map_item, sizeof(map_item_t));
174 new_map_item->highlight = TRUE;
175 map_hl_circle_new(map, CANVAS_GROUP_NODES_HL, new_map_item,
176 x, y, map->style->node.radius,
177 map->style->highlight.node_color);
178 }
179 }
180
181 void map_way_select(appdata_t *appdata, way_t *way) {
182 map_t *map = appdata->map;
183 map_item_t *map_item = &map->selected;
184
185 g_assert(!map->highlight);
186
187 map_item->type = MAP_TYPE_WAY;
188 map_item->way = way;
189 map_item->highlight = FALSE;
190 map_item->item = way->map_item_chain->map_item->item;
191
192 map_statusbar(map, map_item);
193 icon_bar_map_item_selected(appdata, map_item, TRUE);
194 gtk_widget_set_sensitive(appdata->menu_item_map_hide_sel, TRUE);
195
196 gint arrow_width = (map_item->way->draw.flags & OSM_DRAW_FLAG_BG)?
197 map->style->highlight.width + map_item->way->draw.bg.width/2:
198 map->style->highlight.width + map_item->way->draw.width/2;
199
200 node_chain_t *node_chain = map_item->way->node_chain;
201 node_t *last = NULL;
202 while(node_chain) {
203 map_item_t item;
204 item.type = MAP_TYPE_NODE;
205 item.node = node_chain->node;
206
207 /* draw an arrow between every two nodes */
208 if(last) {
209 /* create a new map item for every arrow */
210 map_item_t *new_map_item = g_new0(map_item_t, 1);
211 new_map_item->type = MAP_TYPE_WAY;
212 new_map_item->way = way;
213 new_map_item->highlight = TRUE;
214
215 struct { float x, y;} center, diff;
216 center.x = (last->lpos.x + node_chain->node->lpos.x)/2;
217 center.y = (last->lpos.y + node_chain->node->lpos.y)/2;
218 diff.x = node_chain->node->lpos.x - last->lpos.x;
219 diff.y = node_chain->node->lpos.y - last->lpos.y;
220
221 /* only draw arrow if there's sufficient space */
222 /* TODO: what if there's not enough space anywhere? */
223 float len = sqrt(pow(diff.x, 2)+pow(diff.y, 2));
224 if(len > map->style->highlight.arrow_limit*arrow_width) {
225 len /= arrow_width;
226 diff.x = diff.x / len;
227 diff.y = diff.y / len;
228
229 canvas_points_t *points = canvas_points_new(4);
230 points->coords[2*0+0] = points->coords[2*3+0] = center.x + diff.x;
231 points->coords[2*0+1] = points->coords[2*3+1] = center.y + diff.y;
232 points->coords[2*1+0] = center.x + diff.y - diff.x;
233 points->coords[2*1+1] = center.y - diff.x - diff.y;
234 points->coords[2*2+0] = center.x - diff.y - diff.x;
235 points->coords[2*2+1] = center.y + diff.x - diff.y;
236
237 map_hl_polygon_new(map, CANVAS_GROUP_NODES_HL, new_map_item,
238 points, map->style->highlight.arrow_color);
239
240 canvas_points_free(points);
241 }
242 }
243
244 if(!map_hl_item_is_highlighted(map, &item)) {
245
246 /* create a new map item for every node */
247 map_item_t *new_map_item = g_new0(map_item_t, 1);
248 new_map_item->type = MAP_TYPE_NODE;
249 new_map_item->node = node_chain->node;
250 new_map_item->highlight = TRUE;
251
252 gint x = node_chain->node->lpos.x;
253 gint y = node_chain->node->lpos.y;
254
255 map_hl_circle_new(map, CANVAS_GROUP_NODES_HL, new_map_item,
256 x, y, map->style->node.radius,
257 map->style->highlight.node_color);
258 }
259
260 last = node_chain->node;
261 node_chain = node_chain->next;
262 }
263
264 /* a way needs at least 2 points to be drawn */
265 guint nodes = osm_way_number_of_nodes(way);
266 if(nodes > 1) {
267
268 /* allocate space for nodes */
269 canvas_points_t *points = canvas_points_new(nodes);
270
271 int node = 0;
272 node_chain = map_item->way->node_chain;
273 while(node_chain) {
274 canvas_point_set_pos(points, node++, &node_chain->node->lpos);
275 node_chain = node_chain->next;
276 }
277
278 /* create a copy of this map item and mark it as being a highlight */
279 map_item_t *new_map_item = g_new0(map_item_t, 1);
280 memcpy(new_map_item, map_item, sizeof(map_item_t));
281 new_map_item->highlight = TRUE;
282
283 map_hl_polyline_new(map, CANVAS_GROUP_WAYS_HL, new_map_item, points,
284 (map_item->way->draw.flags & OSM_DRAW_FLAG_BG)?
285 2*map->style->highlight.width + map_item->way->draw.bg.width:
286 2*map->style->highlight.width + map_item->way->draw.width,
287 map->style->highlight.color);
288
289 canvas_points_free(points);
290 }
291 }
292
293 static void map_item_select(appdata_t *appdata, map_item_t *map_item) {
294 switch(map_item->type) {
295 case MAP_TYPE_NODE:
296 map_node_select(appdata, map_item->node);
297 break;
298 case MAP_TYPE_WAY:
299 map_way_select(appdata, map_item->way);
300 break;
301 default:
302 g_assert((map_item->type == MAP_TYPE_NODE)||
303 (map_item->type == MAP_TYPE_WAY));
304 break;
305 }
306 }
307
308 void map_item_deselect(appdata_t *appdata) {
309
310 /* save tags for "last" function in info dialog */
311 if(appdata->map->selected.type == MAP_TYPE_NODE) {
312 if(appdata->map->last_node_tags)
313 osm_tags_free(appdata->map->last_node_tags);
314
315 appdata->map->last_node_tags =
316 osm_tags_copy(appdata->map->selected.node->tag, FALSE);
317 } else if(appdata->map->selected.type == MAP_TYPE_WAY) {
318 if(appdata->map->last_way_tags)
319 osm_tags_free(appdata->map->last_way_tags);
320
321 appdata->map->last_way_tags =
322 osm_tags_copy(appdata->map->selected.way->tag, FALSE);
323 }
324
325 /* remove statusbar message */
326 statusbar_set(appdata, NULL, FALSE);
327
328 /* disable/enable icons in icon bar */
329 icon_bar_map_item_selected(appdata, NULL, FALSE);
330 gtk_widget_set_sensitive(appdata->menu_item_map_hide_sel, FALSE);
331
332 /* remove highlight */
333 map_hl_remove(appdata);
334
335 /* forget about selection */
336 appdata->map->selected.type = MAP_TYPE_ILLEGAL;
337 }
338
339 /* called whenever a map item is to be destroyed */
340 static gint map_item_destroy_event(GtkWidget *widget, gpointer data) {
341 map_item_t *map_item = (map_item_t*)data;
342
343 // printf("destroying map_item @ %p\n", map_item);
344
345 #ifdef DESTROY_WAIT_FOR_GTK
346 /* remove item from nodes/ways map_item_chain */
347 map_item_chain_t **chain = NULL;
348 if(map_item->type == MAP_TYPE_NODE)
349 chain = &map_item->node->map_item_chain;
350 else if(map_item->type == MAP_TYPE_WAY)
351 chain = &map_item->way->map_item_chain;
352
353 /* there must be a chain with content, otherwise things are broken */
354 g_assert(chain);
355 g_assert(*chain);
356
357 /* search current map_item, ... */
358 while(*chain && (*chain)->map_item != map_item)
359 chain = &(*chain)->next;
360
361 g_assert(*chain);
362
363 /* ... remove it from chain and free it */
364 map_item_chain_t *tmp = *chain;
365 *chain = (*chain)->next;
366
367 g_free(tmp);
368 #endif
369
370 g_free(map_item);
371 return FALSE;
372 }
373
374 static canvas_item_t *map_node_new(map_t *map, node_t *node, gint radius,
375 gint width, canvas_color_t fill, canvas_color_t border) {
376
377 map_item_t *map_item = g_new0(map_item_t, 1);
378 map_item->type = MAP_TYPE_NODE;
379 map_item->node = node;
380
381 if(!node->icon_buf || !map->style->icon.enable ||
382 map->appdata->settings->no_icons)
383 map_item->item = canvas_circle_new(map, CANVAS_GROUP_NODES,
384 node->lpos.x, node->lpos.y, radius, width, fill, border);
385 else
386 map_item->item = canvas_image_new(map, CANVAS_GROUP_NODES,
387 node->icon_buf,
388 node->lpos.x - map->style->icon.scale/2 *
389 gdk_pixbuf_get_width(node->icon_buf),
390 node->lpos.y - map->style->icon.scale/2 *
391 gdk_pixbuf_get_height(node->icon_buf),
392 map->style->icon.scale,map->style->icon.scale);
393
394 canvas_item_set_zoom_max(map_item->item, node->zoom_max);
395
396 /* attach map_item to nodes map_item_chain */
397 map_item_chain_t **chain = &node->map_item_chain;
398 while(*chain) chain = &(*chain)->next;
399 *chain = g_new0(map_item_chain_t, 1);
400 (*chain)->map_item = map_item;
401
402 canvas_item_set_user_data(map_item->item, map_item);
403
404 canvas_item_destroy_connect(map_item->item,
405 G_CALLBACK(map_item_destroy_event), map_item);
406
407 return map_item->item;
408 }
409
410 /* in the rare case that a way consists of only one node, it is */
411 /* drawn as a circle. This e.g. happens when drawing a new way */
412 static canvas_item_t *map_way_single_new(map_t *map, way_t *way, gint radius,
413 gint width, canvas_color_t fill, canvas_color_t border) {
414
415 map_item_t *map_item = g_new0(map_item_t, 1);
416 map_item->type = MAP_TYPE_WAY;
417 map_item->way = way;
418 map_item->item = canvas_circle_new(map, CANVAS_GROUP_WAYS,
419 way->node_chain->node->lpos.x, way->node_chain->node->lpos.y,
420 radius, width, fill, border);
421
422 // TODO: decide: do we need canvas_item_set_zoom_max() here too?
423
424 /* attach map_item to nodes map_item_chain */
425 map_item_chain_t **chain = &way->map_item_chain;
426 while(*chain) chain = &(*chain)->next;
427 *chain = g_new0(map_item_chain_t, 1);
428 (*chain)->map_item = map_item;
429
430 canvas_item_set_user_data(map_item->item, map_item);
431
432 canvas_item_destroy_connect(map_item->item,
433 G_CALLBACK(map_item_destroy_event), map_item);
434
435 return map_item->item;
436 }
437
438 static canvas_item_t *map_way_new(map_t *map, canvas_group_t group,
439 way_t *way, canvas_points_t *points, gint width,
440 canvas_color_t color, canvas_color_t fill_color) {
441 map_item_t *map_item = g_new0(map_item_t, 1);
442 map_item->type = MAP_TYPE_WAY;
443 map_item->way = way;
444
445 if(way->draw.flags & OSM_DRAW_FLAG_AREA) {
446 if(map->style->area.color & 0xff)
447 map_item->item = canvas_polygon_new(map, group, points,
448 width, color, fill_color);
449 else
450 map_item->item = canvas_polyline_new(map, group, points,
451 width, color);
452 } else {
453 map_item->item = canvas_polyline_new(map, group, points, width, color);
454 }
455
456 canvas_item_set_zoom_max(map_item->item, way->draw.zoom_max);
457
458 if (group != CANVAS_GROUP_WAYS_OL)
459 if (way->draw.dashed)
460 canvas_item_set_dashed(map_item->item, width, way->draw.dash_length);
461
462 /* attach map_item to ways map_item_chain */
463 map_item_chain_t **chain = &way->map_item_chain;
464 while(*chain) chain = &(*chain)->next;
465 *chain = g_new0(map_item_chain_t, 1);
466 (*chain)->map_item = map_item;
467
468 canvas_item_set_user_data(map_item->item, map_item);
469
470 canvas_item_destroy_connect(map_item->item,
471 G_CALLBACK(map_item_destroy_event), map_item);
472
473 return map_item->item;
474 }
475
476 void map_show_node(map_t *map, node_t *node) {
477 map_node_new(map, node, map->style->node.radius, 0,
478 map->style->node.color, 0);
479 }
480
481 void map_way_draw(map_t *map, way_t *way) {
482
483 /* don't draw a way that's not there anymore */
484 if(way->flags & (OSM_FLAG_DELETED | OSM_FLAG_HIDDEN))
485 return;
486
487 /* allocate space for nodes */
488 /* a way needs at least 2 points to be drawn */
489 guint nodes = osm_way_number_of_nodes(way);
490 if(nodes == 1) {
491 /* draw a single dot where this single node is */
492 map_way_single_new(map, way, map->style->node.radius, 0,
493 map->style->node.color, 0);
494 } else {
495 canvas_points_t *points = canvas_points_new(nodes);
496
497 int node = 0;
498 node_chain_t *node_chain = way->node_chain;
499 while(node_chain) {
500 canvas_point_set_pos(points, node++, &node_chain->node->lpos);
501 node_chain = node_chain->next;
502 }
503
504 /* draw way */
505 if(way->draw.flags & OSM_DRAW_FLAG_AREA) {
506 map_way_new(map, CANVAS_GROUP_POLYGONS, way, points,
507 way->draw.width, way->draw.color, way->draw.area.color);
508 } else {
509 map_way_new(map, CANVAS_GROUP_WAYS, way, points,
510 way->draw.width, way->draw.color, NO_COLOR);
511
512 if(way->draw.flags & OSM_DRAW_FLAG_BG)
513 map_way_new(map, CANVAS_GROUP_WAYS_OL, way, points,
514 way->draw.bg.width, way->draw.bg.color, NO_COLOR);
515 }
516 canvas_points_free(points);
517 }
518 }
519
520 void map_node_draw(map_t *map, node_t *node) {
521 /* don't draw a node that's not there anymore */
522 if(node->flags & OSM_FLAG_DELETED)
523 return;
524
525 if(!node->ways)
526 map_node_new(map, node,
527 map->style->node.radius,
528 map->style->node.border_radius,
529 map->style->node.fill_color,
530 map->style->node.color);
531
532 else if(map->style->node.show_untagged || osm_node_has_tag(node))
533 map_node_new(map, node,
534 map->style->node.radius, 0,
535 map->style->node.color, 0);
536 }
537
538 static void map_item_draw(map_t *map, map_item_t *map_item) {
539 switch(map_item->type) {
540 case MAP_TYPE_NODE:
541 map_node_draw(map, map_item->node);
542 break;
543 case MAP_TYPE_WAY:
544 map_way_draw(map, map_item->way);
545 break;
546 default:
547 g_assert((map_item->type == MAP_TYPE_NODE) ||
548 (map_item->type == MAP_TYPE_WAY));
549 }
550 }
551
552 static void map_item_remove(map_t *map, map_item_t *map_item) {
553 map_item_chain_t **chainP = NULL;
554
555 switch(map_item->type) {
556 case MAP_TYPE_NODE:
557 chainP = &map_item->node->map_item_chain;
558 break;
559 case MAP_TYPE_WAY:
560 chainP = &map_item->way->map_item_chain;
561 break;
562 default:
563 g_assert((map_item->type == MAP_TYPE_NODE) ||
564 (map_item->type == MAP_TYPE_WAY));
565 }
566
567 map_item_chain_destroy(chainP);
568 }
569
570 static void map_item_init(style_t *style, map_item_t *map_item) {
571 switch (map_item->type){
572 case MAP_TYPE_WAY:
573 josm_elemstyles_colorize_way(style, map_item->way);
574 break;
575 case MAP_TYPE_NODE:
576 josm_elemstyles_colorize_node(style, map_item->node);
577 break;
578 default:
579 g_assert((map_item->type == MAP_TYPE_NODE) ||
580 (map_item->type == MAP_TYPE_WAY));
581 }
582 }
583
584 void map_item_redraw(appdata_t *appdata, map_item_t *map_item) {
585 map_item_t item = *map_item;
586
587 /* check if the item to be redrawn is the selected one */
588 gboolean is_selected = FALSE;
589 if(map_item->ptr == appdata->map->selected.ptr) {
590 map_item_deselect(appdata);
591 is_selected = TRUE;
592 }
593
594 map_item_remove(appdata->map, &item);
595 map_item_init(appdata->map->style, &item);
596 map_item_draw(appdata->map, &item);
597
598 /* restore selection if there was one */
599 if(is_selected)
600 map_item_select(appdata, &item);
601 }
602
603 static void map_frisket_rectangle(canvas_points_t *points,
604 gint x0, gint x1, gint y0, gint y1) {
605 points->coords[2*0+0] = points->coords[2*3+0] = points->coords[2*4+0] = x0;
606 points->coords[2*1+0] = points->coords[2*2+0] = x1;
607 points->coords[2*0+1] = points->coords[2*1+1] = points->coords[2*4+1] = y0;
608 points->coords[2*2+1] = points->coords[2*3+1] = y1;
609 }
610
611 /* Draw the frisket area which masks off areas it'd be unsafe to edit,
612 * plus its inner edge marker line */
613 void map_frisket_draw(map_t *map, bounds_t *bounds) {
614 canvas_points_t *points = canvas_points_new(5);
615
616 /* don't draw frisket at all if it's completely transparent */
617 if(map->style->frisket.color & 0xff) {
618 elemstyle_color_t color = map->style->frisket.color;
619
620 float mult = map->style->frisket.mult;
621
622 /* top rectangle */
623 map_frisket_rectangle(points, mult*bounds->min.x, mult*bounds->max.x,
624 mult*bounds->min.y, bounds->min.y);
625 canvas_polygon_new(map, CANVAS_GROUP_NODES, points, 1, NO_COLOR, color);
626
627 /* bottom rectangle */
628 map_frisket_rectangle(points, mult*bounds->min.x, mult*bounds->max.x,
629 bounds->max.y, mult*bounds->max.y);
630 canvas_polygon_new(map, CANVAS_GROUP_NODES, points, 1, NO_COLOR, color);
631
632 /* left rectangle */
633 map_frisket_rectangle(points, mult*bounds->min.x, bounds->min.x,
634 mult*bounds->min.y, mult*bounds->max.y);
635 canvas_polygon_new(map, CANVAS_GROUP_NODES, points, 1, NO_COLOR, color);
636
637 /* right rectangle */
638 map_frisket_rectangle(points, bounds->max.x, mult*bounds->max.x,
639 mult*bounds->min.y, mult*bounds->max.y);
640 canvas_polygon_new(map, CANVAS_GROUP_NODES, points, 1, NO_COLOR, color);
641
642 }
643
644 if(map->style->frisket.border.present) {
645 // Edge marker line
646 gint ew2 = map->style->frisket.border.width/2;
647 map_frisket_rectangle(points,
648 bounds->min.x-ew2, bounds->max.x+ew2,
649 bounds->min.y-ew2, bounds->max.y+ew2);
650
651 canvas_polyline_new(map, CANVAS_GROUP_NODES, points,
652 map->style->frisket.border.width,
653 map->style->frisket.border.color);
654
655 }
656 canvas_points_free(points);
657 }
658
659 static void map_draw(map_t *map, osm_t *osm) {
660 g_assert(map->canvas);
661
662 printf("drawing ways ...\n");
663 way_t *way = osm->way;
664 while(way) {
665 map_way_draw(map, way);
666 way = way->next;
667 }
668
669 printf("drawing single nodes ...\n");
670 node_t *node = osm->node;
671 while(node) {
672 map_node_draw(map, node);
673 node = node->next;
674 }
675
676 printf("drawing frisket...\n");
677 map_frisket_draw(map, osm->bounds);
678 }
679
680 void map_state_free(map_state_t *state) {
681 if(!state) return;
682
683 /* free state of noone else references it */
684 if(state->refcount > 1)
685 state->refcount--;
686 else
687 g_free(state);
688 }
689
690 void map_free_map_item_chains(appdata_t *appdata) {
691 if(!appdata->osm) return;
692
693 #ifndef DESTROY_WAIT_FOR_GTK
694 printf(" DESTROY_WAIT_FOR_GTK not set, removing all chains now\n");
695
696 /* free all map_item_chains */
697 node_t *node = appdata->osm->node;
698 while(node) {
699 map_item_chain_t *chain = node->map_item_chain;
700 while(chain) {
701 map_item_chain_t *next = chain->next;
702 g_free(chain);
703 chain = next;
704 }
705 node->map_item_chain = NULL;
706 node = node->next;
707 }
708
709 way_t *way = appdata->osm->way;
710 while(way) {
711 map_item_chain_t *chain = way->map_item_chain;
712 while(chain) {
713 map_item_chain_t *next = chain->next;
714 g_free(chain);
715 chain = next;
716 }
717 way->map_item_chain = NULL;
718 way = way->next;
719 }
720 #endif
721 }
722
723 static gint map_destroy_event(GtkWidget *widget, gpointer data) {
724 appdata_t *appdata = (appdata_t*)data;
725 map_t *map = appdata->map;
726
727 printf("destroying entire map\n");
728
729 map_free_map_item_chains(appdata);
730
731 /* free buffered tags */
732 if(map->last_node_tags) osm_tags_free(map->last_node_tags);
733 if(map->last_way_tags) osm_tags_free(map->last_way_tags);
734
735 map_state_free(map->state);
736
737 if(map->style)
738 style_free(map->style);
739
740 /* destroy existing highlight */
741 if(map->highlight) {
742 printf("removing highlight\n");
743
744 map_highlight_t *hl = map->highlight;
745 while(hl) {
746 map_highlight_t *next = hl->next;
747 g_free(hl);
748 hl = next;
749 }
750 }
751
752 g_free(map);
753
754 appdata->map = NULL;
755
756 return FALSE;
757 }
758
759 /* get the item at position x, y */
760 map_item_t *map_item_at(map_t *map, gint x, gint y) {
761 printf("map check at %d/%d\n", x, y);
762
763 canvas_window2world(map->canvas, x, y, &x, &y);
764
765 printf("world check at %d/%d\n", x, y);
766
767 canvas_item_t *item = canvas_get_item_at(map->canvas, x, y);
768
769 if(!item) {
770 printf(" there's no item\n");
771 return NULL;
772 }
773
774 printf(" there's an item (%p)\n", item);
775
776 map_item_t *map_item = canvas_item_get_user_data(item);
777
778 if(!map_item) {
779 printf(" item has no user data!\n");
780 return NULL;
781 }
782
783 if(map_item->highlight)
784 printf(" item is highlight\n");
785
786 switch(map_item->type) {
787 case MAP_TYPE_NODE:
788 printf(" item is node #%ld\n", map_item->node->id);
789 break;
790 case MAP_TYPE_WAY:
791 printf(" item is way #%ld\n", map_item->way->id);
792 break;
793 default:
794 printf(" unknown item\n");
795 break;
796 }
797
798 return map_item;
799 }
800
801 /* get the real item (no highlight) at x, y */
802 map_item_t *map_real_item_at(map_t *map, gint x, gint y) {
803 map_item_t *map_item = map_item_at(map, x, y);
804
805 /* no item or already a real one */
806 if(!map_item || !map_item->highlight) return map_item;
807
808 /* get the item (parent) this item is the highlight of */
809 map_item_t *parent = NULL;
810 switch(map_item->type) {
811
812 case MAP_TYPE_NODE:
813 if(map_item->node->map_item_chain)
814 parent = map_item->node->map_item_chain->map_item;
815
816 if(parent)
817 printf(" using parent item node #%ld\n", parent->node->id);
818 break;
819
820 case MAP_TYPE_WAY:
821 if(map_item->way->map_item_chain)
822 parent = map_item->way->map_item_chain->map_item;
823
824 if(parent)
825 printf(" using parent item way #%ld\n", parent->way->id);
826 break;
827
828 default:
829 g_assert((map_item->type == MAP_TYPE_NODE) ||
830 (map_item->type == MAP_TYPE_WAY));
831 break;
832 }
833
834 if(parent)
835 map_item = parent;
836 else
837 printf(" no parent, working on highlight itself\n");
838
839 return map_item;
840 }
841
842 /* Limitations on the amount by which we can scroll. Keeps part of the
843 * map visible at all times */
844 static void map_limit_scroll(map_t *map, gint *sx, gint *sy) {
845 gdouble zoom = goo_canvas_get_scale(GOO_CANVAS(map->canvas));
846
847 gint sx_cu = *sx / zoom;
848 gint sy_cu = *sy / zoom;
849
850 // Canvas viewport dimensions
851 GtkAllocation *a = &GTK_WIDGET(map->canvas)->allocation;
852 gint aw_cu = a->width / zoom;
853 gint ah_cu = a->height / zoom;
854
855 // Data rect minimum and maximum
856 gint min_x, min_y, max_x, max_y;
857 min_x = map->appdata->osm->bounds->min.x;
858 min_y = map->appdata->osm->bounds->min.y;
859 max_x = map->appdata->osm->bounds->max.x;
860 max_y = map->appdata->osm->bounds->max.y;
861
862 // limit stops - prevent scrolling beyond these
863 gint min_sy_cu = 0.95*(min_y - ah_cu);
864 gint min_sx_cu = 0.95*(min_x - aw_cu);
865 gint max_sy_cu = 0.95*(max_y);
866 gint max_sx_cu = 0.95*(max_x);
867 if (sy_cu < min_sy_cu) { *sy = min_sy_cu*zoom; }
868 if (sx_cu < min_sx_cu) { *sx = min_sx_cu*zoom; }
869 if (sy_cu > max_sy_cu) { *sy = max_sy_cu*zoom; }
870 if (sx_cu > max_sx_cu) { *sx = max_sx_cu*zoom; }
871 }
872
873
874 /* Limit a proposed zoom factor to sane ranges.
875 * Specifically the map is allowed to be no smaller than the viewport. */
876 static gboolean map_limit_zoom(map_t *map, gdouble *zoom) {
877 // Data rect minimum and maximum
878 gint min_x, min_y, max_x, max_y;
879 min_x = map->appdata->osm->bounds->min.x;
880 min_y = map->appdata->osm->bounds->min.y;
881 max_x = map->appdata->osm->bounds->max.x;
882 max_y = map->appdata->osm->bounds->max.y;
883
884 // Canvas viewport dimensions
885 GtkAllocation *a = &GTK_WIDGET(map->canvas)->allocation;
886 gint ah_cu = a->height / *zoom;
887 gint aw_cu = a->width / *zoom;
888
889 gdouble oldzoom = *zoom;
890 if (ah_cu < aw_cu) {
891 gint lim_h = ah_cu*0.95;
892 if (max_y-min_y < lim_h) {
893 gdouble corr = ((gdouble)max_y-min_y) / (gdouble)lim_h;
894 *zoom /= corr;
895 }
896 }
897 else {
898 gint lim_w = aw_cu*0.95;
899 if (max_x-min_x < lim_w) {
900 gdouble corr = ((gdouble)max_x-min_x) / (gdouble)lim_w;
901 *zoom /= corr;
902 }
903 }
904 if (*zoom != oldzoom) {
905 printf("Can't zoom further out\n");
906 return 1;
907 }
908 return 0;
909 }
910
911
912 /*
913 * Scroll the map to a point if that point is currently offscreen.
914 */
915 void map_scroll_to_if_offscreen(map_t *map, lpos_t *lpos) {
916
917 // Ignore anything outside the working area
918 if (!(map && map->appdata && map->appdata->osm)) {
919 return;
920 }
921 gint min_x, min_y, max_x, max_y;
922 min_x = map->appdata->osm->bounds->min.x;
923 min_y = map->appdata->osm->bounds->min.y;
924 max_x = map->appdata->osm->bounds->max.x;
925 max_y = map->appdata->osm->bounds->max.y;
926 if ( (lpos->x > max_x) || (lpos->x < min_x)
927 || (lpos->y > max_y) || (lpos->y < min_y)) {
928 printf("cannot scroll to (%d, %d): outside the working area\n",
929 lpos->x, lpos->y);
930 return;
931 }
932
933 // Viewport dimensions in canvas space
934 gdouble zoom = goo_canvas_get_scale(GOO_CANVAS(map->canvas));
935 GtkAllocation *a = &GTK_WIDGET(map->canvas)->allocation;
936 gdouble aw = a->width / zoom;
937 gdouble ah = a->height / zoom;
938
939 // Is the point still onscreen?
940 gboolean vert_recentre_needed = FALSE;
941 gboolean horiz_recentre_needed = FALSE;
942 gint sx, sy;
943 canvas_get_scroll_offsets(map->canvas, &sx, &sy);
944 gint viewport_left = (sx/zoom);
945 gint viewport_right = (sx/zoom)+aw;
946 gint viewport_top = (sy/zoom);
947 gint viewport_bottom = (sy/zoom)+ah;
948 if (lpos->x > viewport_right) {
949 printf("** off right edge (%d > %d)\n", lpos->x, viewport_right);
950 horiz_recentre_needed = TRUE;
951 }
952 if (lpos->x < viewport_left) {
953 printf("** off left edge (%d < %d)\n", lpos->x, viewport_left);
954 horiz_recentre_needed = TRUE;
955 }
956 if (lpos->y > viewport_bottom) {
957 printf("** off bottom edge (%d > %d)\n", lpos->y, viewport_bottom);
958 vert_recentre_needed = TRUE;
959 }
960 if (lpos->y < viewport_top) {
961 printf("** off top edge (%d < %d)\n", lpos->y, viewport_top);
962 vert_recentre_needed = TRUE;
963 }
964
965 if (horiz_recentre_needed || vert_recentre_needed) {
966 gint new_sx, new_sy;
967
968 // Just centre both at once
969 new_sx = zoom * (lpos->x - (aw/2));
970 new_sy = zoom * (lpos->y - (ah/2));
971
972 map_limit_scroll(map, &new_sx, &new_sy);
973 canvas_scroll_to(map->canvas, new_sx, new_sy);
974 }
975 }
976
977 /* Deselects the current way or node if its zoom_max
978 * means that it's not going to render at the current map zoom. */
979 void map_deselect_if_zoom_below_zoom_max(map_t *map) {
980 if (map->selected.type == MAP_TYPE_WAY) {
981 printf("will deselect way if zoomed below %f\n",
982 map->selected.way->draw.zoom_max);
983 if (map->state->zoom < map->selected.way->draw.zoom_max) {
984 printf(" deselecting way!\n");
985 map_item_deselect(map->appdata);
986 }
987 }
988 else if (map->selected.type == MAP_TYPE_NODE) {
989 printf("will deselect node if zoomed below %f\n",
990 map->selected.node->zoom_max);
991 if (map->state->zoom < map->selected.node->zoom_max) {
992 printf(" deselecting node!\n");
993 map_item_deselect(map->appdata);
994 }
995 }
996 }
997
998 void map_set_zoom(map_t *map, double zoom,
999 gboolean update_scroll_offsets) {
1000 gboolean at_zoom_limit = 0;
1001 at_zoom_limit = map_limit_zoom(map, &zoom);
1002
1003 map->state->zoom = zoom;
1004 canvas_set_zoom(map->canvas, map->state->zoom);
1005
1006 map_deselect_if_zoom_below_zoom_max(map);
1007
1008 if (update_scroll_offsets) {
1009 if (!at_zoom_limit) {
1010 /* zooming affects the scroll offsets */
1011 gint sx, sy;
1012 canvas_get_scroll_offsets(map->canvas, &sx, &sy);
1013 map_limit_scroll(map, &sx, &sy);
1014 canvas_scroll_to(map->canvas, sx, sy); // keep the map visible
1015 map->state->scroll_offset.x = sx;
1016 map->state->scroll_offset.y = sy;
1017 }
1018 }
1019 }
1020
1021 static gboolean map_scroll_event(GtkWidget *widget, GdkEventScroll *event,
1022 gpointer data) {
1023 appdata_t *appdata = (appdata_t*)data;
1024
1025 if(!appdata->osm) return FALSE;
1026
1027 if(event->type == GDK_SCROLL && appdata->map && appdata->map->state) {
1028 if(event->direction)
1029 map_set_zoom(appdata->map,
1030 appdata->map->state->zoom / ZOOM_FACTOR_WHEEL, TRUE);
1031 else
1032 map_set_zoom(appdata->map,
1033 appdata->map->state->zoom * ZOOM_FACTOR_WHEEL, TRUE);
1034 }
1035
1036 return TRUE;
1037 }
1038
1039 static gboolean distance_above(map_t *map, gint x, gint y, gint limit) {
1040 gint sx, sy;
1041
1042 #ifdef USE_GNOMECANVAS
1043 gnome_canvas_get_scroll_offsets(GNOME_CANVAS(map->canvas), &sx, &sy);
1044
1045 /* add offsets generated by mouse within map and map scrolling */
1046 sx = (x-map->pen_down.at.x) + (map->pen_down.so.x-sx);
1047 sy = (y-map->pen_down.at.y) + (map->pen_down.so.y-sy);
1048 #else
1049 /* add offsets generated by mouse within map and map scrolling */
1050 sx = (x-map->pen_down.at.x);
1051 sy = (y-map->pen_down.at.y);
1052 #endif
1053
1054 return(sx*sx + sy*sy > limit*limit);
1055 }
1056
1057 /* scroll with respect to two screen positions */
1058 static void map_do_scroll(map_t *map, gint x, gint y) {
1059 gint sx, sy;
1060
1061 canvas_get_scroll_offsets(map->canvas, &sx, &sy);
1062 sx -= x-map->pen_down.at.x;
1063 sy -= y-map->pen_down.at.y;
1064 map_limit_scroll(map, &sx, &sy);
1065 canvas_scroll_to(map->canvas, sx, sy);
1066 map->state->scroll_offset.x = sx;
1067 map->state->scroll_offset.y = sy;
1068 }
1069
1070
1071 /* scroll a certain step */
1072 static void map_do_scroll_step(map_t *map, gint x, gint y) {
1073 gint sx, sy;
1074 canvas_get_scroll_offsets(map->canvas, &sx, &sy);
1075 sx += x;
1076 sy += y;
1077 map_limit_scroll(map, &sx, &sy);
1078 canvas_scroll_to(map->canvas, sx, sy);
1079 map->state->scroll_offset.x = sx;
1080 map->state->scroll_offset.y = sy;
1081 }
1082
1083
1084 gboolean map_item_is_selected_node(map_t *map, map_item_t *map_item) {
1085 printf("check if item is a selected node\n");
1086
1087 if(!map_item) {
1088 printf(" no item requested\n");
1089 return FALSE;
1090 }
1091
1092 if(map->selected.type == MAP_TYPE_ILLEGAL) {
1093 printf(" nothing is selected\n");
1094 return FALSE;
1095 }
1096
1097 /* clicked the highlight directly */
1098 if(map_item->type != MAP_TYPE_NODE) {
1099 printf(" didn't click node\n");
1100 return FALSE;
1101 }
1102
1103 if(map->selected.type == MAP_TYPE_NODE) {
1104 printf(" selected item is a node\n");
1105
1106 if(map_item->node == map->selected.node) {
1107 printf(" requested item is a selected node\n");
1108 return TRUE;
1109 }
1110 printf(" but it's not the requested one\n");
1111 return FALSE;
1112
1113 } else if(map->selected.type == MAP_TYPE_WAY) {
1114 printf(" selected item is a way\n");
1115
1116 node_chain_t *node_chain = map->selected.way->node_chain;
1117 while(node_chain) {
1118 if(node_chain->node == map_item->node) {
1119 printf(" requested item is part of selected way\n");
1120 return TRUE;
1121 }
1122 node_chain = node_chain->next;
1123 }
1124 printf(" but it doesn't include the requested node\n");
1125 return FALSE;
1126
1127 } else {
1128 printf(" selected item is unknown\n");
1129 return FALSE;
1130 }
1131
1132 g_assert(0);
1133 return TRUE;
1134 }
1135
1136 /* return true if the item given is the currenly selected way */
1137 /* also return false if nothing is selected or the selection is no way */
1138 gboolean map_item_is_selected_way(map_t *map, map_item_t *map_item) {
1139 printf("check if item is the selected way\n");
1140
1141 if(!map_item) {
1142 printf(" no item requested\n");
1143 return FALSE;
1144 }
1145
1146 if(map->selected.type == MAP_TYPE_ILLEGAL) {
1147 printf(" nothing is selected\n");
1148 return FALSE;
1149 }
1150
1151 /* clicked the highlight directly */
1152 if(map_item->type != MAP_TYPE_WAY) {
1153 printf(" didn't click way\n");
1154 return FALSE;
1155 }
1156
1157 if(map->selected.type == MAP_TYPE_WAY) {
1158 printf(" selected item is a way\n");
1159
1160 if(map_item->way == map->selected.way) {
1161 printf(" requested item is a selected way\n");
1162 return TRUE;
1163 }
1164 printf(" but it's not the requested one\n");
1165 return FALSE;
1166 }
1167
1168 printf(" selected item is not a way\n");
1169 return FALSE;
1170 }
1171
1172
1173 void map_highlight_refresh(appdata_t *appdata) {
1174 map_t *map = appdata->map;
1175 map_item_t old = map->selected;
1176
1177 printf("type to refresh is %d\n", old.type);
1178 if(old.type == MAP_TYPE_ILLEGAL)
1179 return;
1180
1181 map_item_deselect(appdata);
1182 map_item_select(appdata, &old);
1183 }
1184
1185 void map_way_delete(appdata_t *appdata, way_t *way) {
1186 printf("deleting way #%ld from map and osm\n", way->id);
1187
1188 /* remove it visually from the screen */
1189 map_item_chain_destroy(&way->map_item_chain);
1190
1191 /* also remove the visible representation of all nodes (nodes with tags) */
1192 node_chain_t *chain = way->node_chain;
1193 while(chain) {
1194 if(chain->node->map_item_chain)
1195 map_item_chain_destroy(&chain->node->map_item_chain);
1196
1197 chain = chain->next;
1198 }
1199
1200 /* and mark it "deleted" in the database */
1201 osm_way_remove_from_relation(appdata->osm, way);
1202 osm_way_delete(appdata->osm, &appdata->icon, way, FALSE);
1203 }
1204
1205 static void map_handle_click(appdata_t *appdata, map_t *map) {
1206
1207 /* problem: on_item may be the highlight itself! So store it! */
1208 map_item_t map_item;
1209 if(map->pen_down.on_item) map_item = *map->pen_down.on_item;
1210 else map_item.type = MAP_TYPE_ILLEGAL;
1211
1212 /* if we aready have something selected, then de-select it */
1213 map_item_deselect(appdata);
1214
1215 /* select the clicked item (if there was one) */
1216 if(map_item.type != MAP_TYPE_ILLEGAL) {
1217 switch(map_item.type) {
1218 case MAP_TYPE_NODE:
1219 map_node_select(appdata, map_item.node);
1220 break;
1221
1222 case MAP_TYPE_WAY:
1223 map_way_select(appdata, map_item.way);
1224 break;
1225
1226 default:
1227 g_assert((map_item.type == MAP_TYPE_NODE) ||
1228 (map_item.type == MAP_TYPE_WAY));
1229 break;
1230 }
1231 }
1232 }
1233
1234 static void map_touchnode_update(appdata_t *appdata, gint x, gint y) {
1235 map_t *map = appdata->map;
1236
1237 map_hl_touchnode_clear(map);
1238
1239 node_t *cur_node = NULL;
1240
1241 /* the "current node" which is the one we are working on and which */
1242 /* should not be highlighted depends on the action */
1243 switch(map->action.type) {
1244
1245 /* in idle mode the dragged node is not highlighted */
1246 case MAP_ACTION_IDLE:
1247 g_assert(map->pen_down.on_item);
1248 g_assert(map->pen_down.on_item->type == MAP_TYPE_NODE);
1249 cur_node = map->pen_down.on_item->node;
1250 break;
1251
1252 default:
1253 break;
1254 }
1255
1256
1257 /* check if we are close to one of the other nodes */
1258 canvas_window2world(appdata->map->canvas, x, y, &x, &y);
1259 node_t *node = appdata->osm->node;
1260 while(!map->touchnode && node) {
1261 /* don't highlight the dragged node itself and don't highlight */
1262 /* deleted ones */
1263 if((node != cur_node) && (!(node->flags & OSM_FLAG_DELETED))) {
1264 gint nx = x - node->lpos.x;
1265 gint ny = y - node->lpos.y;
1266
1267 if((nx < map->style->node.radius) && (ny < map->style->node.radius) &&
1268 (nx*nx + ny*ny < map->style->node.radius * map->style->node.radius))
1269 map_hl_touchnode_draw(map, node);
1270 }
1271 node = node->next;
1272 }
1273
1274 /* during way creation also nodes of the new way */
1275 /* need to be searched */
1276 if(!map->touchnode && map->action.way) {
1277 node_chain_t *chain = map->action.way->node_chain;
1278 while(!map->touchnode && chain && chain->next) {
1279 gint nx = x - chain->node->lpos.x;
1280 gint ny = y - chain->node->lpos.y;
1281
1282 if((nx < map->style->node.radius) && (ny < map->style->node.radius) &&
1283 (nx*nx + ny*ny < map->style->node.radius * map->style->node.radius))
1284 map_hl_touchnode_draw(map, chain->node);
1285
1286 chain = chain->next;
1287 }
1288 }
1289 }
1290
1291 static void map_button_press(map_t *map, gint x, gint y) {
1292
1293 printf("left button pressed\n");
1294 map->pen_down.is = TRUE;
1295
1296 /* save press position */
1297 map->pen_down.at.x = x;
1298 map->pen_down.at.y = y;
1299 map->pen_down.drag = FALSE; // don't assume drag yet
1300
1301 #ifdef USE_GNOMECANVAS
1302 /* save initial scroll offset */
1303 gnome_canvas_get_scroll_offsets(GNOME_CANVAS(map->canvas),
1304 &map->pen_down.so.x, &map->pen_down.so.y);
1305 #endif
1306
1307 /* determine wether this press was on an item */
1308 map->pen_down.on_item = map_real_item_at(map, x, y);
1309
1310 /* check if the clicked item is a highlighted node as the user */
1311 /* might want to drag that */
1312 map->pen_down.on_selected_node = FALSE;
1313 if(map->pen_down.on_item)
1314 map->pen_down.on_selected_node =
1315 map_item_is_selected_node(map, map->pen_down.on_item);
1316
1317 /* button press */
1318 switch(map->action.type) {
1319
1320 case MAP_ACTION_WAY_NODE_ADD:
1321 map_edit_way_node_add_highlight(map, map->pen_down.on_item, x, y);
1322 break;
1323
1324 case MAP_ACTION_WAY_CUT:
1325 map_edit_way_cut_highlight(map, map->pen_down.on_item, x, y);
1326 break;
1327
1328 case MAP_ACTION_NODE_ADD:
1329 map_hl_cursor_draw(map, x, y, FALSE, map->style->node.radius);
1330 break;
1331
1332 case MAP_ACTION_WAY_ADD:
1333 map_hl_cursor_draw(map, x, y, FALSE, map->style->node.radius);
1334 map_touchnode_update(map->appdata, x, y);
1335 break;
1336
1337 default:
1338 break;
1339 }
1340 }
1341
1342 /* move the background image (wms data) during wms adjustment */
1343 static void map_bg_adjust(map_t *map, gint x, gint y) {
1344 g_assert(map->appdata);
1345 g_assert(map->appdata->osm);
1346 g_assert(map->appdata->osm->bounds);
1347
1348 x += map->appdata->osm->bounds->min.x + map->bg.offset.x -
1349 map->pen_down.at.x;
1350 y += map->appdata->osm->bounds->min.y + map->bg.offset.y -
1351 map->pen_down.at.y;
1352
1353 canvas_image_move(map->bg.item, x, y, map->bg.scale.x, map->bg.scale.y);
1354 }
1355
1356 static void map_button_release(map_t *map, gint x, gint y) {
1357 map->pen_down.is = FALSE;
1358
1359 /* before button release is handled */
1360 switch(map->action.type) {
1361 case MAP_ACTION_BG_ADJUST:
1362 map_bg_adjust(map, x, y);
1363 map->bg.offset.x += x - map->pen_down.at.x;
1364 map->bg.offset.y += y - map->pen_down.at.y;
1365 break;
1366
1367 case MAP_ACTION_IDLE:
1368 /* check if distance to press is above drag limit */
1369 if(!map->pen_down.drag)
1370 map->pen_down.drag = distance_above(map, x, y, MAP_DRAG_LIMIT);
1371
1372 if(!map->pen_down.drag) {
1373 printf("left button released after click\n");
1374
1375 map_item_t old_sel = map->selected;
1376 map_handle_click(map->appdata, map);
1377
1378 if((old_sel.type != MAP_TYPE_ILLEGAL) &&
1379 (old_sel.type == map->selected.type) &&
1380 (old_sel.ptr == map->selected.ptr)) {
1381 printf("re-selected same item of type %d, "
1382 "pushing it to the bottom\n", old_sel.type);
1383
1384 if(!map->selected.item) {
1385 printf(" item has no visible representation to push\n");
1386 } else {
1387 canvas_item_to_bottom(map->selected.item);
1388
1389 /* update clicked item, to correctly handle the click */
1390 map->pen_down.on_item =
1391 map_real_item_at(map, map->pen_down.at.x, map->pen_down.at.y);
1392
1393 map_handle_click(map->appdata, map);
1394 }
1395 }
1396 } else {
1397 printf("left button released after drag\n");
1398
1399 /* just scroll if we didn't drag an selected item */
1400 if(!map->pen_down.on_selected_node)
1401 map_do_scroll(map, x, y);
1402 else {
1403 printf("released after dragging node\n");
1404 map_hl_cursor_clear(map);
1405
1406 /* now actually move the node */
1407 map_edit_node_move(map->appdata, map->pen_down.on_item, x, y);
1408 }
1409 }
1410 break;
1411
1412 case MAP_ACTION_NODE_ADD:
1413 printf("released after NODE ADD\n");
1414 map_hl_cursor_clear(map);
1415
1416 /* convert mouse position to canvas (world) position */
1417 canvas_window2world(map->canvas, x, y, &x, &y);
1418
1419 node_t *node = NULL;
1420 if(!osm_position_within_bounds(map->appdata->osm, x, y))
1421 map_outside_error(map->appdata);
1422 else {
1423 node = osm_node_new(map->appdata->osm, x, y);
1424 osm_node_attach(map->appdata->osm, node);
1425 map_node_draw(map, node);
1426 }
1427 map_action_set(map->appdata, MAP_ACTION_IDLE);
1428
1429 map_item_deselect(map->appdata);
1430
1431 if(node) {
1432 map_node_select(map->appdata, node);
1433
1434 /* let the user specify some tags for the new node */
1435 info_dialog(GTK_WIDGET(map->appdata->window), map->appdata, NULL);
1436 }
1437 break;
1438
1439 case MAP_ACTION_WAY_ADD:
1440 printf("released after WAY ADD\n");
1441 map_hl_cursor_clear(map);
1442
1443 map_edit_way_add_segment(map, x, y);
1444 break;
1445
1446 case MAP_ACTION_WAY_NODE_ADD:
1447 printf("released after WAY NODE ADD\n");
1448 map_hl_cursor_clear(map);
1449
1450 map_edit_way_node_add(map, x, y);
1451 break;
1452
1453 case MAP_ACTION_WAY_CUT:
1454 printf("released after WAY CUT\n");
1455 map_hl_cursor_clear(map);
1456
1457 map_edit_way_cut(map, x, y);
1458 break;
1459
1460
1461 default:
1462 break;
1463 }
1464 }
1465
1466 static gboolean map_button_event(GtkWidget *widget, GdkEventButton *event,
1467 gpointer data) {
1468 appdata_t *appdata = (appdata_t*)data;
1469 map_t *map = appdata->map;
1470
1471 if(!appdata->osm) return FALSE;
1472
1473 if(event->button == 1) {
1474 gint x = event->x, y = event->y;
1475
1476 if(event->type == GDK_BUTTON_PRESS)
1477 map_button_press(map, x, y);
1478
1479 if(event->type == GDK_BUTTON_RELEASE)
1480 map_button_release(map, x, y);
1481 }
1482
1483 return FALSE; /* forward to further processing */
1484 }
1485
1486 static gboolean map_motion_notify_event(GtkWidget *widget,
1487 GdkEventMotion *event, gpointer data) {
1488 appdata_t *appdata = (appdata_t*)data;
1489 map_t *map = appdata->map;
1490 gint x, y;
1491 GdkModifierType state;
1492
1493 if(!appdata->osm) return FALSE;
1494
1495 #ifdef USE_HILDON
1496 /* reduce update frequency on hildon to keep screen update fluid */
1497 static guint32 last_time = 0;
1498
1499 if(event->time - last_time < 250) return FALSE;
1500 last_time = event->time;
1501 #endif
1502
1503 if(!map->pen_down.is)
1504 return FALSE;
1505
1506 #ifndef USE_GOOCANVAS
1507 /* handle hints, hints are handled by goocanvas directly */
1508 if(event->is_hint)
1509 gdk_window_get_pointer(event->window, &x, &y, &state);
1510 else
1511 #endif
1512 {
1513 x = event->x;
1514 y = event->y;
1515 state = event->state;
1516 }
1517
1518 /* check if distance to press is above drag limit */
1519 if(!map->pen_down.drag)
1520 map->pen_down.drag = distance_above(map, x, y, MAP_DRAG_LIMIT);
1521
1522 /* drag */
1523 switch(map->action.type) {
1524 case MAP_ACTION_BG_ADJUST:
1525 map_bg_adjust(map, x, y);
1526 break;
1527
1528 case MAP_ACTION_IDLE:
1529 if(map->pen_down.drag) {
1530 /* just scroll if we didn't drag an selected item */
1531 if(!map->pen_down.on_selected_node)
1532 map_do_scroll(map, x, y);
1533 else {
1534 map_hl_cursor_draw(map, x, y, FALSE, map->style->node.radius);
1535 map_touchnode_update(appdata, x, y);
1536 }
1537 }
1538 break;
1539
1540 case MAP_ACTION_NODE_ADD:
1541 map_hl_cursor_draw(map, x, y, FALSE, map->style->node.radius);
1542 break;
1543
1544 case MAP_ACTION_WAY_ADD:
1545 map_hl_cursor_draw(map, x, y, FALSE, map->style->node.radius);
1546 map_touchnode_update(appdata, x, y);
1547 break;
1548
1549 case MAP_ACTION_WAY_NODE_ADD:
1550 map_hl_cursor_clear(map);
1551 map_item_t *item = map_real_item_at(map, x, y);
1552 if(item) map_edit_way_node_add_highlight(map, item, x, y);
1553 break;
1554
1555 case MAP_ACTION_WAY_CUT:
1556 map_hl_cursor_clear(map);
1557 item = map_real_item_at(map, x, y);
1558 if(item) map_edit_way_cut_highlight(map, item, x, y);
1559 break;
1560
1561 default:
1562 break;
1563 }
1564
1565
1566 return FALSE; /* forward to further processing */
1567 }
1568
1569 gboolean map_key_press_event(appdata_t *appdata, GdkEventKey *event) {
1570
1571 if(!appdata->osm) return FALSE;
1572
1573 /* map needs to be there to handle buttons */
1574 if(!appdata->map->canvas)
1575 return FALSE;
1576
1577 if(event->type == GDK_KEY_PRESS) {
1578 gdouble zoom = 0;
1579 switch(event->keyval) {
1580
1581 case GDK_Left:
1582 map_do_scroll_step(appdata->map, -50, 0);
1583 break;
1584
1585 case GDK_Right:
1586 map_do_scroll_step(appdata->map, +50, 0);
1587 break;
1588
1589 case GDK_Up:
1590 map_do_scroll_step(appdata->map, 0, -50);
1591 break;
1592
1593 case GDK_Down:
1594 map_do_scroll_step(appdata->map, 0, +50);
1595 break;
1596
1597 case GDK_Return: // same as HILDON_HARDKEY_SELECT
1598 /* if the ok button is enabled, call its function */
1599 if(GTK_WIDGET_FLAGS(appdata->iconbar->ok) & GTK_SENSITIVE)
1600 map_action_ok(appdata);
1601 /* otherwise if info is enabled call that */
1602 else if(GTK_WIDGET_FLAGS(appdata->iconbar->info) & GTK_SENSITIVE)
1603 info_dialog(GTK_WIDGET(appdata->window), appdata, NULL);
1604 break;
1605
1606 case GDK_Escape: // same as HILDON_HARDKEY_ESC
1607 /* if the cancel button is enabled, call its function */
1608 if(GTK_WIDGET_FLAGS(appdata->iconbar->cancel) & GTK_SENSITIVE)
1609 map_action_cancel(appdata);
1610 break;
1611
1612 #ifdef USE_HILDON
1613 case HILDON_HARDKEY_INCREASE:
1614 #else
1615 case '+':
1616 #endif
1617 zoom = appdata->map->state->zoom;
1618 zoom *= ZOOM_FACTOR_BUTTON;
1619 map_set_zoom(appdata->map, zoom, TRUE);
1620 printf("zoom is now %f (1:%d)\n", zoom, (int)zoom_to_scaledn(zoom));
1621 return TRUE;
1622 break;
1623
1624 #ifdef USE_HILDON
1625 case HILDON_HARDKEY_DECREASE:
1626 #else
1627 case '-':
1628 #endif
1629 zoom = appdata->map->state->zoom;
1630 zoom /= ZOOM_FACTOR_BUTTON;
1631 map_set_zoom(appdata->map, zoom, TRUE);
1632 printf("zoom is now %f (1:%d)\n", zoom, (int)zoom_to_scaledn(zoom));
1633 return TRUE;
1634 break;
1635
1636 default:
1637 printf("key event %d\n", event->keyval);
1638 break;
1639 }
1640 }
1641 return FALSE;
1642 }
1643
1644 GtkWidget *map_new(appdata_t *appdata) {
1645 map_t *map = appdata->map = g_new0(map_t, 1);
1646
1647 map->style = style_load(appdata, appdata->settings->style);
1648 if(!map->style) {
1649 errorf(NULL, _("Unable to load valid style, terminating."));
1650 g_free(map);
1651 return NULL;
1652 }
1653
1654 if(appdata->project && appdata->project->map_state) {
1655 printf("Using projects map state\n");
1656 map->state = appdata->project->map_state;
1657 } else {
1658 printf("Creating new map state\n");
1659 map->state = g_new0(map_state_t, 1);
1660 map->state->zoom = 0.25;
1661 }
1662
1663 map->state->refcount++;
1664
1665 map->pen_down.at.x = -1;
1666 map->pen_down.at.y = -1;
1667 map->appdata = appdata;
1668 map->action.type = MAP_ACTION_IDLE;
1669
1670 #ifdef USE_GNOMECANVAS
1671 map->canvas = gnome_canvas_new_aa(); // _aa
1672
1673 /* create the groups */
1674 canvas_group_t group;
1675 for(group = 0; group < CANVAS_GROUPS; group++)
1676 map->group[group] = gnome_canvas_item_new(
1677 gnome_canvas_root(GNOME_CANVAS(map->canvas)),
1678 GNOME_TYPE_CANVAS_GROUP,
1679 NULL);
1680
1681 gtk_widget_modify_bg(map->canvas, GTK_STATE_NORMAL,
1682 &map->canvas->style->white);
1683
1684 #else
1685 map->canvas = goo_canvas_new();
1686
1687 g_object_set(G_OBJECT(map->canvas), "anchor", GTK_ANCHOR_CENTER, NULL);
1688 g_object_set(G_OBJECT(map->canvas), "background-color-rgb",
1689 map->style->background.color >> 8, NULL);
1690
1691 GooCanvasItem *root = goo_canvas_get_root_item(GOO_CANVAS(map->canvas));
1692 g_object_set(G_OBJECT(root), "antialias",
1693 appdata->settings->no_antialias?CAIRO_ANTIALIAS_NONE:
1694 CAIRO_ANTIALIAS_DEFAULT, NULL);
1695
1696 /* create the groups */
1697 canvas_group_t group;
1698 for(group = 0; group < CANVAS_GROUPS; group++)
1699 map->group[group] = goo_canvas_group_new(root, NULL);
1700
1701 #endif
1702
1703 gtk_widget_set_events(map->canvas,
1704 GDK_BUTTON_PRESS_MASK
1705 | GDK_BUTTON_RELEASE_MASK
1706 | GDK_SCROLL_MASK
1707 | GDK_POINTER_MOTION_MASK
1708 | GDK_POINTER_MOTION_HINT_MASK);
1709
1710 gtk_signal_connect(GTK_OBJECT(map->canvas),
1711 "button_press_event", G_CALLBACK(map_button_event), appdata);
1712 gtk_signal_connect(GTK_OBJECT(map->canvas),
1713 "button_release_event", G_CALLBACK(map_button_event), appdata);
1714 gtk_signal_connect(GTK_OBJECT(map->canvas),
1715 "motion_notify_event", G_CALLBACK(map_motion_notify_event), appdata);
1716 gtk_signal_connect(GTK_OBJECT(map->canvas),
1717 "scroll_event", G_CALLBACK(map_scroll_event), appdata);
1718
1719 gtk_signal_connect(GTK_OBJECT(map->canvas),
1720 "destroy", G_CALLBACK(map_destroy_event), appdata);
1721
1722 return map->canvas;
1723 }
1724
1725 void map_init(appdata_t *appdata) {
1726 map_t *map = appdata->map;
1727
1728 /* set initial zoom */
1729 map_set_zoom(map, map->state->zoom, FALSE);
1730 josm_elemstyles_colorize_world(map->style, appdata->osm);
1731
1732 map_draw(map, appdata->osm);
1733
1734 float mult = appdata->map->style->frisket.mult;
1735 canvas_set_bounds(map->canvas,
1736 mult*appdata->osm->bounds->min.x,
1737 mult*appdata->osm->bounds->min.y,
1738 mult*appdata->osm->bounds->max.x,
1739 mult*appdata->osm->bounds->max.y);
1740
1741 printf("restore scroll offsets %d/%d\n",
1742 map->state->scroll_offset.x, map->state->scroll_offset.y);
1743
1744 canvas_scroll_to(map->canvas,
1745 map->state->scroll_offset.x, map->state->scroll_offset.y);
1746 }
1747
1748
1749 void map_item_set_flags(map_item_t *map_item, int set, int clr) {
1750
1751 switch(map_item->type) {
1752 case MAP_TYPE_NODE:
1753 map_item->node->flags |= set;
1754 map_item->node->flags &= ~clr;
1755 break;
1756
1757 case MAP_TYPE_WAY:
1758 map_item->way->flags |= set;
1759 map_item->way->flags &= ~clr;
1760 break;
1761
1762 default:
1763 g_assert(0);
1764 break;
1765 }
1766 }
1767
1768 void map_clear(appdata_t *appdata, gint layer_mask) {
1769 map_t *map = appdata->map;
1770
1771 printf("freeing map contents\n");
1772
1773 map_free_map_item_chains(appdata);
1774
1775 /* remove a possibly existing highlight */
1776 map_item_deselect(appdata);
1777
1778 canvas_group_t group;
1779 for(group=0;group<CANVAS_GROUPS;group++) {
1780
1781 #ifdef USE_GNOMECANVAS
1782 /* destroy the entire group */
1783 canvas_item_destroy(map->group[group]);
1784
1785 /* and create an empty new one */
1786 map->group[group] = gnome_canvas_item_new(
1787 gnome_canvas_root(GNOME_CANVAS(map->canvas)),
1788 GNOME_TYPE_CANVAS_GROUP,
1789 NULL);
1790 #else
1791 if(layer_mask & (1<<group)) {
1792 gint children = goo_canvas_item_get_n_children(map->group[group]);
1793 printf("Removing %d children from layer %d\n", children, group);
1794 while(children--)
1795 goo_canvas_item_remove_child(map->group[group], children);
1796 }
1797 #endif
1798 }
1799 }
1800
1801 void map_paint(appdata_t *appdata) {
1802 map_t *map = appdata->map;
1803
1804 /* user may have changed antialias settings */
1805 GooCanvasItem *root = goo_canvas_get_root_item(GOO_CANVAS(map->canvas));
1806 g_object_set(G_OBJECT(root), "antialias",
1807 appdata->settings->no_antialias?CAIRO_ANTIALIAS_NONE:
1808 CAIRO_ANTIALIAS_DEFAULT, NULL);
1809
1810 josm_elemstyles_colorize_world(map->style, appdata->osm);
1811 map_draw(map, appdata->osm);
1812 }
1813
1814 /* called from several icons like e.g. "node_add" */
1815 void map_action_set(appdata_t *appdata, map_action_t action) {
1816 printf("map action set to %d\n", action);
1817
1818 appdata->map->action.type = action;
1819
1820 /* enable/disable ok/cancel buttons */
1821 // MAP_ACTION_IDLE=0, NODE_ADD, BG_ADJUST, WAY_ADD, WAY_NODE_ADD, WAY_CUT
1822 const gboolean ok_state[] = { FALSE, FALSE, TRUE, FALSE, FALSE, FALSE };
1823 const gboolean cancel_state[] = { FALSE, TRUE, TRUE, TRUE, TRUE, TRUE };
1824
1825 g_assert(MAP_ACTION_NUM == sizeof(ok_state)/sizeof(gboolean));
1826 g_assert(action < sizeof(ok_state)/sizeof(gboolean));
1827
1828 icon_bar_map_cancel_ok(appdata, cancel_state[action], ok_state[action]);
1829
1830 switch(action) {
1831 case MAP_ACTION_BG_ADJUST:
1832 /* an existing selection only causes confusion ... */
1833 map_item_deselect(appdata);
1834 break;
1835
1836 case MAP_ACTION_WAY_ADD:
1837 printf("starting new way\n");
1838
1839 /* remember if there was a way selected */
1840 way_t *way_sel = NULL;
1841 if(appdata->map->selected.type == MAP_TYPE_WAY)
1842 way_sel = appdata->map->selected.way;
1843
1844 map_item_deselect(appdata);
1845 map_edit_way_add_begin(appdata->map, way_sel);
1846 break;
1847
1848 case MAP_ACTION_NODE_ADD:
1849 map_item_deselect(appdata);
1850 break;
1851
1852 default:
1853 break;
1854 }
1855
1856 icon_bar_map_action_idle(appdata, action == MAP_ACTION_IDLE);
1857 gtk_widget_set_sensitive(appdata->menu_item_wms_adjust,
1858 action == MAP_ACTION_IDLE);
1859
1860 const char *str_state[] = {
1861 NULL,
1862 _("Place a node"),
1863 _("Adjust background image position"),
1864 _("Place first node of new way"),
1865 _("Place node on selected way"),
1866 _("Select segment to cut way"),
1867 };
1868
1869 g_assert(MAP_ACTION_NUM == sizeof(str_state)/sizeof(char*));
1870
1871 statusbar_set(appdata, str_state[action], FALSE);
1872 }
1873
1874
1875 void map_action_cancel(appdata_t *appdata) {
1876 map_t *map = appdata->map;
1877
1878 switch(map->action.type) {
1879 case MAP_ACTION_WAY_ADD:
1880 map_edit_way_add_cancel(map);
1881 break;
1882
1883 case MAP_ACTION_BG_ADJUST:
1884 /* undo all changes to bg_offset */
1885 map->bg.offset.x = appdata->project->wms_offset.x;
1886 map->bg.offset.y = appdata->project->wms_offset.y;
1887
1888 gint x = appdata->osm->bounds->min.x + map->bg.offset.x;
1889 gint y = appdata->osm->bounds->min.y + map->bg.offset.y;
1890 canvas_image_move(map->bg.item, x, y, map->bg.scale.x, map->bg.scale.y);
1891 break;
1892
1893 default:
1894 break;
1895 }
1896
1897 map_action_set(appdata, MAP_ACTION_IDLE);
1898 }
1899
1900 void map_action_ok(appdata_t *appdata) {
1901 map_t *map = appdata->map;
1902
1903 /* reset action now as this erases the statusbar and some */
1904 /* of the actions may set it */
1905 map_action_t type = map->action.type;
1906 map_action_set(appdata, MAP_ACTION_IDLE);
1907
1908 switch(type) {
1909 case MAP_ACTION_WAY_ADD:
1910 map_edit_way_add_ok(map);
1911 break;
1912
1913 case MAP_ACTION_BG_ADJUST:
1914 /* save changes to bg_offset in project */
1915 appdata->project->wms_offset.x = map->bg.offset.x;
1916 appdata->project->wms_offset.y = map->bg.offset.y;
1917 appdata->project->dirty = TRUE;
1918 break;
1919
1920 default:
1921 break;
1922 }
1923 }
1924
1925 /* called from icon "trash" */
1926 void map_delete_selected(appdata_t *appdata) {
1927 map_t *map = appdata->map;
1928
1929 if(!yes_no_f(GTK_WIDGET(appdata->window),
1930 appdata, MISC_AGAIN_ID_DELETE, MISC_AGAIN_FLAG_DONT_SAVE_NO,
1931 _("Delete selected object?"),
1932 _("Do you really want to delete the selected object?")))
1933 return;
1934
1935 /* work on local copy since de-selecting destroys the selection */
1936 map_item_t item = map->selected;
1937
1938 /* deleting the selected item de-selects it ... */
1939 map_item_deselect(appdata);
1940
1941 undo_remember_delete(appdata, item.type, item.ptr);
1942
1943 switch(item.type) {
1944 case MAP_TYPE_NODE:
1945 printf("request to delete node #%ld\n", item.node->id);
1946
1947 /* check if this node is part of a way with two nodes only. */
1948 /* we cannot delete this as this would also delete the way */
1949 way_chain_t *way_chain = osm_node_to_way(appdata->osm, item.node);
1950 if(way_chain) {
1951 gboolean short_way = FALSE;
1952
1953 /* free the chain of ways */
1954 while(way_chain && !short_way) {
1955 way_chain_t *next = way_chain->next;
1956
1957 if(osm_way_number_of_nodes(way_chain->way) <= 2)
1958 short_way = TRUE;
1959
1960 g_free(way_chain);
1961 way_chain = next;
1962 }
1963
1964 if(short_way) {
1965 if(!yes_no_f(GTK_WIDGET(appdata->window), NULL, 0, 0,
1966 _("Delete node in short way(s)?"),
1967 _("Deleting this node will also delete one or more ways "
1968 "since they'll contain only one node afterwards. "
1969 "Do you really want this?")))
1970 return;
1971 }
1972 }
1973
1974 /* remove it visually from the screen */
1975 map_item_chain_destroy(&item.node->map_item_chain);
1976
1977 /* and mark it "deleted" in the database */
1978 osm_node_remove_from_relation(appdata->osm, item.node);
1979 way_chain_t *chain = osm_node_delete(appdata->osm,
1980 &appdata->icon, item.node, FALSE, TRUE);
1981
1982 /* redraw all affected ways */
1983 while(chain) {
1984 way_chain_t *next = chain->next;
1985
1986 if(osm_way_number_of_nodes(chain->way) == 1) {
1987 /* this way now only contains one node and thus isn't a valid */
1988 /* way anymore. So it'll also get deleted (which in turn may */
1989 /* cause other nodes to be deleted as well) */
1990 map_way_delete(appdata, chain->way);
1991 } else {
1992 map_item_t item;
1993 item.type = MAP_TYPE_WAY;
1994 item.way = chain->way;
1995 map_item_redraw(appdata, &item);
1996 }
1997
1998 g_free(chain);
1999
2000 chain = next;
2001 }
2002
2003 break;
2004
2005 case MAP_TYPE_WAY:
2006 printf("request to delete way #%ld\n", item.way->id);
2007 map_way_delete(appdata, item.way);
2008 break;
2009
2010 default:
2011 g_assert((item.type == MAP_TYPE_NODE) ||
2012 (item.type == MAP_TYPE_WAY));
2013 break;
2014 }
2015 }
2016
2017 /* ----------------------- track related stuff ----------------------- */
2018
2019 void map_track_draw_seg(map_t *map, track_seg_t *seg) {
2020 /* a track_seg needs at least 2 points to be drawn */
2021 guint pnum = track_seg_points(seg);
2022 printf("seg of length %d\n", pnum);
2023
2024 if(pnum == 1) {
2025 g_assert(!seg->item);
2026
2027 seg->item = canvas_circle_new(map, CANVAS_GROUP_TRACK,
2028 seg->track_point->lpos.x, seg->track_point->lpos.y,
2029 map->style->track.width/2.0, 0, map->style->track.color, NO_COLOR);
2030 }
2031
2032 if(pnum > 1) {
2033
2034 /* allocate space for nodes */
2035 canvas_points_t *points = canvas_points_new(pnum);
2036
2037 int point = 0;
2038 track_point_t *track_point = seg->track_point;
2039 while(track_point) {
2040 points->coords[point++] = track_point->lpos.x;
2041 points->coords[point++] = track_point->lpos.y;
2042 track_point = track_point->next;
2043 }
2044
2045 /* there may be a circle (one point line) */
2046 if(seg->item)
2047 canvas_item_destroy(seg->item);
2048
2049 seg->item = canvas_polyline_new(map, CANVAS_GROUP_TRACK,
2050 points, map->style->track.width, map->style->track.color);
2051
2052 canvas_points_free(points);
2053 }
2054 }
2055
2056 void map_track_update_seg(map_t *map, track_seg_t *seg) {
2057 /* a track_seg needs at least 2 points to be drawn */
2058 guint pnum = track_seg_points(seg);
2059 printf("seg of length %d\n", pnum);
2060
2061 if(pnum > 1) {
2062
2063 /* allocate space for nodes */
2064 canvas_points_t *points = canvas_points_new(pnum);
2065
2066 int point = 0;
2067 track_point_t *track_point = seg->track_point;
2068 while(track_point) {
2069 canvas_point_set_pos(points, point++, &track_point->lpos);
2070 track_point = track_point->next;
2071 }
2072
2073 g_assert(seg->item);
2074 canvas_item_set_points(seg->item, points);
2075 canvas_points_free(points);
2076 }
2077 }
2078
2079 void map_track_draw(map_t *map, track_t *track) {
2080 track_seg_t *seg = track->track_seg;
2081
2082 /* draw all segments */
2083 while(seg) {
2084 map_track_draw_seg(map, seg);
2085 seg = seg->next;
2086 }
2087 }
2088
2089 void map_track_remove(appdata_t *appdata) {
2090 track_t *track = appdata->track.track;
2091
2092 printf("removing track\n");
2093
2094 g_assert(track);
2095
2096 /* remove all segments */
2097 track_seg_t *seg = track->track_seg;
2098 while(seg) {
2099 if(seg->item) {
2100 canvas_item_destroy(seg->item);
2101 seg->item = NULL;
2102 }
2103
2104 seg = seg->next;
2105 }
2106 }
2107
2108 void map_track_pos(appdata_t *appdata, lpos_t *lpos) {
2109 if(appdata->track.gps_item) {
2110 canvas_item_destroy(appdata->track.gps_item);
2111 appdata->track.gps_item = NULL;
2112 }
2113
2114 if(lpos)
2115 appdata->track.gps_item = canvas_circle_new(appdata->map, CANVAS_GROUP_GPS,
2116 lpos->x, lpos->y, appdata->map->style->track.width/2.0, 0,
2117 appdata->map->style->track.gps_color, NO_COLOR);
2118 }
2119
2120 /* ------------------- map background ------------------ */
2121
2122 void map_remove_bg_image(map_t *map) {
2123 if(!map) return;
2124
2125 if(map->bg.item) {
2126 canvas_item_destroy(map->bg.item);
2127 map->bg.item = NULL;
2128 }
2129 }
2130
2131 static gint map_bg_item_destroy_event(GtkWidget *widget, gpointer data) {
2132 map_t *map = (map_t*)data;
2133
2134 /* destroying background item */
2135
2136 map->bg.item = NULL;
2137 if(map->bg.pix) {
2138 printf("destroying background item\n");
2139 gdk_pixbuf_unref(map->bg.pix);
2140 map->bg.pix = NULL;
2141 }
2142 return FALSE;
2143 }
2144
2145 void map_set_bg_image(map_t *map, char *filename) {
2146 bounds_t *bounds = map->appdata->osm->bounds;
2147
2148 map_remove_bg_image(map);
2149
2150 map->bg.pix = gdk_pixbuf_new_from_file(filename, NULL);
2151
2152 /* calculate required scale factor */
2153 map->bg.scale.x = (float)(bounds->max.x - bounds->min.x)/
2154 (float)gdk_pixbuf_get_width(map->bg.pix);
2155 map->bg.scale.y = (float)(bounds->max.y - bounds->min.y)/
2156 (float)gdk_pixbuf_get_height(map->bg.pix);
2157
2158 map->bg.item = canvas_image_new(map, CANVAS_GROUP_BG, map->bg.pix,
2159 bounds->min.x, bounds->min.y, map->bg.scale.x, map->bg.scale.y);
2160
2161 canvas_item_destroy_connect(map->bg.item,
2162 G_CALLBACK(map_bg_item_destroy_event), map);
2163 }
2164
2165
2166 /* -------- hide and show objects (for performance reasons) ------- */
2167
2168 void map_hide_selected(appdata_t *appdata) {
2169 map_t *map = appdata->map;
2170 if(!map) return;
2171
2172 if(map->selected.type != MAP_TYPE_WAY) {
2173 printf("selected item is not a way\n");
2174 return;
2175 }
2176
2177 way_t *way = map->selected.way;
2178 printf("hiding way #%ld\n", way->id);
2179
2180 map_item_deselect(appdata);
2181 way->flags |= OSM_FLAG_HIDDEN;
2182 map_item_chain_destroy(&way->map_item_chain);
2183
2184 gtk_widget_set_sensitive(appdata->menu_item_map_show_all, TRUE);
2185 }
2186
2187 void map_show_all(appdata_t *appdata) {
2188 map_t *map = appdata->map;
2189 if(!map) return;
2190
2191 way_t *way = appdata->osm->way;
2192 while(way) {
2193 if(way->flags & OSM_FLAG_HIDDEN) {
2194 way->flags &= ~OSM_FLAG_HIDDEN;
2195 map_way_draw(map, way);
2196 }
2197
2198 way = way->next;
2199 }
2200
2201 gtk_widget_set_sensitive(appdata->menu_item_map_show_all, FALSE);
2202 }
2203 // vim:et:ts=8:sw=2:sts=2:ai