Contents of /trunk/src/map.c

Parent Directory Parent Directory | Revision Log Revision Log


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