Contents of /trunk/src/map.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 103 - (hide annotations)
Tue Mar 3 12:12:53 2009 UTC (15 years, 2 months ago) by harbaum
File MIME type: text/plain
File size: 61541 byte(s)
Canvas code cleanups
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     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 achadwick 28 /*
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 harbaum 1 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 harbaum 14 if(node->icon_buf && map->style->icon.enable &&
159     !appdata->settings->no_icons) {
160 harbaum 1 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 harbaum 103
170 harbaum 1 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 harbaum 15 /* only draw arrow if there's sufficient space */
222     /* TODO: what if there's not enough space anywhere? */
223 harbaum 1 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 harbaum 14 if(!node->icon_buf || !map->style->icon.enable ||
382 harbaum 64 map->appdata->settings->no_icons)
383 harbaum 103 map_item->item = canvas_circle_new(map->canvas, CANVAS_GROUP_NODES,
384 harbaum 1 node->lpos.x, node->lpos.y, radius, width, fill, border);
385     else
386 harbaum 103 map_item->item = canvas_image_new(map->canvas, CANVAS_GROUP_NODES,
387 harbaum 1 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 harbaum 103 map_item->item = canvas_circle_new(map->canvas, CANVAS_GROUP_WAYS,
419 harbaum 1 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 harbaum 22 if(map->style->area.color & 0xff)
447 harbaum 103 map_item->item = canvas_polygon_new(map->canvas, group, points,
448 harbaum 1 width, color, fill_color);
449     else
450 harbaum 103 map_item->item = canvas_polyline_new(map->canvas, group, points,
451 harbaum 1 width, color);
452     } else {
453 harbaum 103 map_item->item = canvas_polyline_new(map->canvas, group, points, width, color);
454 harbaum 1 }
455    
456     canvas_item_set_zoom_max(map_item->item, way->draw.zoom_max);
457    
458 achadwick 13 if (group != CANVAS_GROUP_WAYS_OL)
459     if (way->draw.dashed)
460 achadwick 46 canvas_item_set_dashed(map_item->item, width, way->draw.dash_length);
461 achadwick 13
462 harbaum 1 /* 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 harbaum 22 map->style->node.color, 0);
479 harbaum 1 }
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 harbaum 22 map->style->node.color, 0);
494 harbaum 1 } 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 harbaum 22 map->style->node.fill_color,
530     map->style->node.color);
531 harbaum 1
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 harbaum 22 map->style->node.color, 0);
536 harbaum 1 }
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 harbaum 22 if(map->style->frisket.color & 0xff) {
618     elemstyle_color_t color = map->style->frisket.color;
619 harbaum 1
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 harbaum 103 canvas_polygon_new(map->canvas, CANVAS_GROUP_FRISKET, points,
626     1, NO_COLOR, color);
627 harbaum 1
628     /* bottom rectangle */
629     map_frisket_rectangle(points, mult*bounds->min.x, mult*bounds->max.x,
630     bounds->max.y, mult*bounds->max.y);
631 harbaum 103 canvas_polygon_new(map->canvas, CANVAS_GROUP_FRISKET, points,
632     1, NO_COLOR, color);
633 harbaum 1
634     /* left rectangle */
635     map_frisket_rectangle(points, mult*bounds->min.x, bounds->min.x,
636     mult*bounds->min.y, mult*bounds->max.y);
637 harbaum 103 canvas_polygon_new(map->canvas, CANVAS_GROUP_FRISKET, points,
638     1, NO_COLOR, color);
639 harbaum 1
640     /* right rectangle */
641     map_frisket_rectangle(points, bounds->max.x, mult*bounds->max.x,
642     mult*bounds->min.y, mult*bounds->max.y);
643 harbaum 103 canvas_polygon_new(map->canvas, CANVAS_GROUP_FRISKET, points,
644     1, NO_COLOR, color);
645 harbaum 1
646     }
647    
648     if(map->style->frisket.border.present) {
649     // Edge marker line
650     gint ew2 = map->style->frisket.border.width/2;
651     map_frisket_rectangle(points,
652     bounds->min.x-ew2, bounds->max.x+ew2,
653     bounds->min.y-ew2, bounds->max.y+ew2);
654    
655 harbaum 103 canvas_polyline_new(map->canvas, CANVAS_GROUP_FRISKET, points,
656 harbaum 1 map->style->frisket.border.width,
657     map->style->frisket.border.color);
658    
659     }
660     canvas_points_free(points);
661     }
662    
663     static void map_draw(map_t *map, osm_t *osm) {
664     g_assert(map->canvas);
665    
666     printf("drawing ways ...\n");
667     way_t *way = osm->way;
668     while(way) {
669     map_way_draw(map, way);
670     way = way->next;
671     }
672    
673     printf("drawing single nodes ...\n");
674     node_t *node = osm->node;
675     while(node) {
676     map_node_draw(map, node);
677     node = node->next;
678     }
679    
680     printf("drawing frisket...\n");
681     map_frisket_draw(map, osm->bounds);
682     }
683    
684     void map_state_free(map_state_t *state) {
685     if(!state) return;
686    
687     /* free state of noone else references it */
688     if(state->refcount > 1)
689     state->refcount--;
690     else
691     g_free(state);
692     }
693    
694     void map_free_map_item_chains(appdata_t *appdata) {
695     if(!appdata->osm) return;
696    
697     #ifndef DESTROY_WAIT_FOR_GTK
698     printf(" DESTROY_WAIT_FOR_GTK not set, removing all chains now\n");
699    
700     /* free all map_item_chains */
701     node_t *node = appdata->osm->node;
702     while(node) {
703     map_item_chain_t *chain = node->map_item_chain;
704     while(chain) {
705     map_item_chain_t *next = chain->next;
706     g_free(chain);
707     chain = next;
708     }
709     node->map_item_chain = NULL;
710     node = node->next;
711     }
712    
713     way_t *way = appdata->osm->way;
714     while(way) {
715     map_item_chain_t *chain = way->map_item_chain;
716     while(chain) {
717     map_item_chain_t *next = chain->next;
718     g_free(chain);
719     chain = next;
720     }
721     way->map_item_chain = NULL;
722     way = way->next;
723     }
724     #endif
725     }
726    
727     static gint map_destroy_event(GtkWidget *widget, gpointer data) {
728     appdata_t *appdata = (appdata_t*)data;
729     map_t *map = appdata->map;
730    
731     printf("destroying entire map\n");
732    
733     map_free_map_item_chains(appdata);
734    
735     /* free buffered tags */
736     if(map->last_node_tags) osm_tags_free(map->last_node_tags);
737     if(map->last_way_tags) osm_tags_free(map->last_way_tags);
738    
739     map_state_free(map->state);
740    
741     if(map->style)
742     style_free(map->style);
743    
744     /* destroy existing highlight */
745     if(map->highlight) {
746     printf("removing highlight\n");
747    
748     map_highlight_t *hl = map->highlight;
749     while(hl) {
750     map_highlight_t *next = hl->next;
751     g_free(hl);
752     hl = next;
753     }
754     }
755    
756     g_free(map);
757    
758     appdata->map = NULL;
759    
760     return FALSE;
761     }
762    
763     /* get the item at position x, y */
764     map_item_t *map_item_at(map_t *map, gint x, gint y) {
765     printf("map check at %d/%d\n", x, y);
766    
767     canvas_window2world(map->canvas, x, y, &x, &y);
768    
769     printf("world check at %d/%d\n", x, y);
770    
771     canvas_item_t *item = canvas_get_item_at(map->canvas, x, y);
772    
773     if(!item) {
774     printf(" there's no item\n");
775     return NULL;
776     }
777    
778     printf(" there's an item (%p)\n", item);
779    
780     map_item_t *map_item = canvas_item_get_user_data(item);
781    
782     if(!map_item) {
783     printf(" item has no user data!\n");
784     return NULL;
785     }
786    
787 harbaum 15 if(map_item->highlight)
788 harbaum 1 printf(" item is highlight\n");
789    
790     switch(map_item->type) {
791     case MAP_TYPE_NODE:
792     printf(" item is node #%ld\n", map_item->node->id);
793     break;
794     case MAP_TYPE_WAY:
795     printf(" item is way #%ld\n", map_item->way->id);
796     break;
797     default:
798     printf(" unknown item\n");
799     break;
800     }
801    
802     return map_item;
803     }
804    
805     /* get the real item (no highlight) at x, y */
806 harbaum 15 map_item_t *map_real_item_at(map_t *map, gint x, gint y) {
807 harbaum 1 map_item_t *map_item = map_item_at(map, x, y);
808    
809     /* no item or already a real one */
810     if(!map_item || !map_item->highlight) return map_item;
811    
812     /* get the item (parent) this item is the highlight of */
813     map_item_t *parent = NULL;
814     switch(map_item->type) {
815    
816     case MAP_TYPE_NODE:
817     if(map_item->node->map_item_chain)
818     parent = map_item->node->map_item_chain->map_item;
819    
820     if(parent)
821     printf(" using parent item node #%ld\n", parent->node->id);
822     break;
823    
824     case MAP_TYPE_WAY:
825     if(map_item->way->map_item_chain)
826     parent = map_item->way->map_item_chain->map_item;
827    
828     if(parent)
829     printf(" using parent item way #%ld\n", parent->way->id);
830     break;
831    
832     default:
833     g_assert((map_item->type == MAP_TYPE_NODE) ||
834     (map_item->type == MAP_TYPE_WAY));
835     break;
836     }
837    
838     if(parent)
839     map_item = parent;
840     else
841     printf(" no parent, working on highlight itself\n");
842    
843     return map_item;
844     }
845    
846     /* Limitations on the amount by which we can scroll. Keeps part of the
847     * map visible at all times */
848 harbaum 103 static void map_limit_scroll(map_t *map, canvas_unit_t unit,
849     gint *sx, gint *sy) {
850 harbaum 1
851 harbaum 103 /* get scale factor for pixel->meter conversion. set to 1 if */
852     /* given coordinates are already in meters */
853     gdouble scale = (unit == CANVAS_UNIT_METER)?1.0:canvas_get_zoom(map->canvas);
854 harbaum 1
855 harbaum 103 /* convert pixels to meters if necessary */
856     gdouble sx_cu = *sx / scale;
857     gdouble sy_cu = *sy / scale;
858    
859     /* get size of visible area in canvas units (meters) */
860     gint aw_cu = canvas_get_viewport_width(map->canvas, CANVAS_UNIT_METER);
861     gint ah_cu = canvas_get_viewport_height(map->canvas, CANVAS_UNIT_METER);
862    
863     // Data rect minimum and maximum
864     gint min_x, min_y, max_x, max_y;
865     min_x = map->appdata->osm->bounds->min.x;
866     min_y = map->appdata->osm->bounds->min.y;
867     max_x = map->appdata->osm->bounds->max.x;
868     max_y = map->appdata->osm->bounds->max.y;
869    
870     // limit stops - prevent scrolling beyond these
871     gint min_sy_cu = 0.95*(min_y - ah_cu);
872     gint min_sx_cu = 0.95*(min_x - aw_cu);
873     gint max_sy_cu = 0.95*(max_y);
874     gint max_sx_cu = 0.95*(max_x);
875     if (sy_cu < min_sy_cu) { *sy = min_sy_cu * scale; }
876     if (sx_cu < min_sx_cu) { *sx = min_sx_cu * scale; }
877     if (sy_cu > max_sy_cu) { *sy = max_sy_cu * scale; }
878     if (sx_cu > max_sx_cu) { *sx = max_sx_cu * scale; }
879 harbaum 1 }
880    
881    
882     /* Limit a proposed zoom factor to sane ranges.
883     * Specifically the map is allowed to be no smaller than the viewport. */
884     static gboolean map_limit_zoom(map_t *map, gdouble *zoom) {
885     // Data rect minimum and maximum
886     gint min_x, min_y, max_x, max_y;
887     min_x = map->appdata->osm->bounds->min.x;
888     min_y = map->appdata->osm->bounds->min.y;
889     max_x = map->appdata->osm->bounds->max.x;
890     max_y = map->appdata->osm->bounds->max.y;
891    
892 harbaum 103 /* get size of visible area in pixels and convert to meters of intended */
893     /* zoom by deviding by zoom (which is basically pix/m) */
894     gint aw_cu =
895     canvas_get_viewport_width(map->canvas, CANVAS_UNIT_PIXEL) / *zoom;
896     gint ah_cu =
897     canvas_get_viewport_height(map->canvas, CANVAS_UNIT_PIXEL) / *zoom;
898 harbaum 1
899     gdouble oldzoom = *zoom;
900     if (ah_cu < aw_cu) {
901     gint lim_h = ah_cu*0.95;
902     if (max_y-min_y < lim_h) {
903     gdouble corr = ((gdouble)max_y-min_y) / (gdouble)lim_h;
904     *zoom /= corr;
905     }
906     }
907     else {
908     gint lim_w = aw_cu*0.95;
909     if (max_x-min_x < lim_w) {
910     gdouble corr = ((gdouble)max_x-min_x) / (gdouble)lim_w;
911     *zoom /= corr;
912     }
913     }
914     if (*zoom != oldzoom) {
915     printf("Can't zoom further out\n");
916     return 1;
917     }
918     return 0;
919     }
920    
921    
922 achadwick 12 /*
923     * Scroll the map to a point if that point is currently offscreen.
924     */
925     void map_scroll_to_if_offscreen(map_t *map, lpos_t *lpos) {
926    
927     // Ignore anything outside the working area
928     if (!(map && map->appdata && map->appdata->osm)) {
929     return;
930     }
931     gint min_x, min_y, max_x, max_y;
932     min_x = map->appdata->osm->bounds->min.x;
933     min_y = map->appdata->osm->bounds->min.y;
934     max_x = map->appdata->osm->bounds->max.x;
935     max_y = map->appdata->osm->bounds->max.y;
936     if ( (lpos->x > max_x) || (lpos->x < min_x)
937     || (lpos->y > max_y) || (lpos->y < min_y)) {
938 harbaum 14 printf("cannot scroll to (%d, %d): outside the working area\n",
939     lpos->x, lpos->y);
940 achadwick 12 return;
941     }
942    
943     // Viewport dimensions in canvas space
944    
945 harbaum 103 /* get size of visible area in canvas units (meters) */
946     gdouble pix_per_meter = canvas_get_zoom(map->canvas);
947     gdouble aw = canvas_get_viewport_width(map->canvas, CANVAS_UNIT_METER);
948     gdouble ah = canvas_get_viewport_height(map->canvas, CANVAS_UNIT_METER);
949    
950 achadwick 12 // Is the point still onscreen?
951     gboolean vert_recentre_needed = FALSE;
952     gboolean horiz_recentre_needed = FALSE;
953     gint sx, sy;
954 harbaum 103 canvas_scroll_get(map->canvas, CANVAS_UNIT_PIXEL, &sx, &sy);
955     gint viewport_left = (sx/pix_per_meter);
956     gint viewport_right = (sx/pix_per_meter)+aw;
957     gint viewport_top = (sy/pix_per_meter);
958     gint viewport_bottom = (sy/pix_per_meter)+ah;
959 achadwick 12 if (lpos->x > viewport_right) {
960     printf("** off right edge (%d > %d)\n", lpos->x, viewport_right);
961     horiz_recentre_needed = TRUE;
962     }
963     if (lpos->x < viewport_left) {
964     printf("** off left edge (%d < %d)\n", lpos->x, viewport_left);
965     horiz_recentre_needed = TRUE;
966     }
967     if (lpos->y > viewport_bottom) {
968     printf("** off bottom edge (%d > %d)\n", lpos->y, viewport_bottom);
969     vert_recentre_needed = TRUE;
970     }
971     if (lpos->y < viewport_top) {
972     printf("** off top edge (%d < %d)\n", lpos->y, viewport_top);
973     vert_recentre_needed = TRUE;
974     }
975    
976     if (horiz_recentre_needed || vert_recentre_needed) {
977     gint new_sx, new_sy;
978 harbaum 78
979 achadwick 12 // Just centre both at once
980 harbaum 103 new_sx = pix_per_meter * (lpos->x - (aw/2));
981     new_sy = pix_per_meter * (lpos->y - (ah/2));
982 harbaum 78
983 harbaum 103 map_limit_scroll(map, CANVAS_UNIT_PIXEL, &new_sx, &new_sy);
984     canvas_scroll_to(map->canvas, CANVAS_UNIT_PIXEL, new_sx, new_sy);
985 achadwick 12 }
986     }
987    
988 harbaum 1 /* Deselects the current way or node if its zoom_max
989     * means that it's not going to render at the current map zoom. */
990     void map_deselect_if_zoom_below_zoom_max(map_t *map) {
991     if (map->selected.type == MAP_TYPE_WAY) {
992     printf("will deselect way if zoomed below %f\n",
993     map->selected.way->draw.zoom_max);
994     if (map->state->zoom < map->selected.way->draw.zoom_max) {
995     printf(" deselecting way!\n");
996     map_item_deselect(map->appdata);
997     }
998     }
999     else if (map->selected.type == MAP_TYPE_NODE) {
1000     printf("will deselect node if zoomed below %f\n",
1001     map->selected.node->zoom_max);
1002     if (map->state->zoom < map->selected.node->zoom_max) {
1003     printf(" deselecting node!\n");
1004     map_item_deselect(map->appdata);
1005     }
1006     }
1007     }
1008    
1009     void map_set_zoom(map_t *map, double zoom,
1010     gboolean update_scroll_offsets) {
1011     gboolean at_zoom_limit = 0;
1012     at_zoom_limit = map_limit_zoom(map, &zoom);
1013 harbaum 78
1014 harbaum 1 map->state->zoom = zoom;
1015     canvas_set_zoom(map->canvas, map->state->zoom);
1016    
1017     map_deselect_if_zoom_below_zoom_max(map);
1018    
1019 harbaum 103 if(update_scroll_offsets) {
1020 harbaum 1 if (!at_zoom_limit) {
1021     /* zooming affects the scroll offsets */
1022     gint sx, sy;
1023 harbaum 103 canvas_scroll_get(map->canvas, CANVAS_UNIT_PIXEL, &sx, &sy);
1024     map_limit_scroll(map, CANVAS_UNIT_PIXEL, &sx, &sy);
1025    
1026     // keep the map visible
1027     canvas_scroll_to(map->canvas, CANVAS_UNIT_PIXEL, sx, sy);
1028 harbaum 1 }
1029 harbaum 103
1030     canvas_scroll_get(map->canvas, CANVAS_UNIT_METER,
1031     &map->state->scroll_offset.x,
1032     &map->state->scroll_offset.y);
1033 harbaum 1 }
1034     }
1035    
1036     static gboolean map_scroll_event(GtkWidget *widget, GdkEventScroll *event,
1037     gpointer data) {
1038     appdata_t *appdata = (appdata_t*)data;
1039    
1040     if(!appdata->osm) return FALSE;
1041    
1042     if(event->type == GDK_SCROLL && appdata->map && appdata->map->state) {
1043     if(event->direction)
1044     map_set_zoom(appdata->map,
1045     appdata->map->state->zoom / ZOOM_FACTOR_WHEEL, TRUE);
1046     else
1047     map_set_zoom(appdata->map,
1048     appdata->map->state->zoom * ZOOM_FACTOR_WHEEL, TRUE);
1049     }
1050    
1051     return TRUE;
1052     }
1053    
1054     static gboolean distance_above(map_t *map, gint x, gint y, gint limit) {
1055     gint sx, sy;
1056    
1057     /* add offsets generated by mouse within map and map scrolling */
1058     sx = (x-map->pen_down.at.x);
1059     sy = (y-map->pen_down.at.y);
1060    
1061     return(sx*sx + sy*sy > limit*limit);
1062     }
1063    
1064     /* scroll with respect to two screen positions */
1065     static void map_do_scroll(map_t *map, gint x, gint y) {
1066     gint sx, sy;
1067    
1068 harbaum 103 canvas_scroll_get(map->canvas, CANVAS_UNIT_PIXEL, &sx, &sy);
1069 harbaum 1 sx -= x-map->pen_down.at.x;
1070     sy -= y-map->pen_down.at.y;
1071 harbaum 103 map_limit_scroll(map, CANVAS_UNIT_PIXEL, &sx, &sy);
1072     canvas_scroll_to(map->canvas, CANVAS_UNIT_PIXEL, sx, sy);
1073    
1074     canvas_scroll_get(map->canvas, CANVAS_UNIT_METER,
1075     &map->state->scroll_offset.x,
1076     &map->state->scroll_offset.y);
1077 harbaum 1 }
1078    
1079    
1080     /* scroll a certain step */
1081     static void map_do_scroll_step(map_t *map, gint x, gint y) {
1082     gint sx, sy;
1083 harbaum 103 canvas_scroll_get(map->canvas, CANVAS_UNIT_PIXEL, &sx, &sy);
1084 harbaum 1 sx += x;
1085     sy += y;
1086 harbaum 103 map_limit_scroll(map, CANVAS_UNIT_PIXEL, &sx, &sy);
1087     canvas_scroll_to(map->canvas, CANVAS_UNIT_PIXEL, sx, sy);
1088    
1089     canvas_scroll_get(map->canvas, CANVAS_UNIT_METER,
1090     &map->state->scroll_offset.x,
1091     &map->state->scroll_offset.y);
1092 harbaum 1 }
1093    
1094     gboolean map_item_is_selected_node(map_t *map, map_item_t *map_item) {
1095     printf("check if item is a selected node\n");
1096    
1097     if(!map_item) {
1098     printf(" no item requested\n");
1099     return FALSE;
1100     }
1101    
1102     if(map->selected.type == MAP_TYPE_ILLEGAL) {
1103     printf(" nothing is selected\n");
1104     return FALSE;
1105     }
1106    
1107     /* clicked the highlight directly */
1108     if(map_item->type != MAP_TYPE_NODE) {
1109     printf(" didn't click node\n");
1110     return FALSE;
1111     }
1112    
1113     if(map->selected.type == MAP_TYPE_NODE) {
1114     printf(" selected item is a node\n");
1115    
1116     if(map_item->node == map->selected.node) {
1117     printf(" requested item is a selected node\n");
1118     return TRUE;
1119     }
1120     printf(" but it's not the requested one\n");
1121     return FALSE;
1122    
1123     } else if(map->selected.type == MAP_TYPE_WAY) {
1124     printf(" selected item is a way\n");
1125    
1126     node_chain_t *node_chain = map->selected.way->node_chain;
1127     while(node_chain) {
1128     if(node_chain->node == map_item->node) {
1129     printf(" requested item is part of selected way\n");
1130     return TRUE;
1131     }
1132     node_chain = node_chain->next;
1133     }
1134     printf(" but it doesn't include the requested node\n");
1135     return FALSE;
1136    
1137     } else {
1138     printf(" selected item is unknown\n");
1139     return FALSE;
1140     }
1141    
1142     g_assert(0);
1143     return TRUE;
1144     }
1145    
1146     /* return true if the item given is the currenly selected way */
1147     /* also return false if nothing is selected or the selection is no way */
1148     gboolean map_item_is_selected_way(map_t *map, map_item_t *map_item) {
1149     printf("check if item is the selected way\n");
1150    
1151     if(!map_item) {
1152     printf(" no item requested\n");
1153     return FALSE;
1154     }
1155    
1156     if(map->selected.type == MAP_TYPE_ILLEGAL) {
1157     printf(" nothing is selected\n");
1158     return FALSE;
1159     }
1160    
1161     /* clicked the highlight directly */
1162     if(map_item->type != MAP_TYPE_WAY) {
1163     printf(" didn't click way\n");
1164     return FALSE;
1165     }
1166    
1167     if(map->selected.type == MAP_TYPE_WAY) {
1168     printf(" selected item is a way\n");
1169    
1170     if(map_item->way == map->selected.way) {
1171     printf(" requested item is a selected way\n");
1172     return TRUE;
1173     }
1174     printf(" but it's not the requested one\n");
1175     return FALSE;
1176     }
1177    
1178     printf(" selected item is not a way\n");
1179     return FALSE;
1180     }
1181    
1182    
1183     void map_highlight_refresh(appdata_t *appdata) {
1184     map_t *map = appdata->map;
1185     map_item_t old = map->selected;
1186    
1187     printf("type to refresh is %d\n", old.type);
1188     if(old.type == MAP_TYPE_ILLEGAL)
1189     return;
1190    
1191     map_item_deselect(appdata);
1192     map_item_select(appdata, &old);
1193     }
1194    
1195     void map_way_delete(appdata_t *appdata, way_t *way) {
1196     printf("deleting way #%ld from map and osm\n", way->id);
1197    
1198     /* remove it visually from the screen */
1199     map_item_chain_destroy(&way->map_item_chain);
1200    
1201     /* also remove the visible representation of all nodes (nodes with tags) */
1202     node_chain_t *chain = way->node_chain;
1203     while(chain) {
1204     if(chain->node->map_item_chain)
1205     map_item_chain_destroy(&chain->node->map_item_chain);
1206    
1207     chain = chain->next;
1208     }
1209    
1210     /* and mark it "deleted" in the database */
1211     osm_way_remove_from_relation(appdata->osm, way);
1212     osm_way_delete(appdata->osm, &appdata->icon, way, FALSE);
1213     }
1214    
1215     static void map_handle_click(appdata_t *appdata, map_t *map) {
1216    
1217     /* problem: on_item may be the highlight itself! So store it! */
1218     map_item_t map_item;
1219     if(map->pen_down.on_item) map_item = *map->pen_down.on_item;
1220     else map_item.type = MAP_TYPE_ILLEGAL;
1221    
1222     /* if we aready have something selected, then de-select it */
1223     map_item_deselect(appdata);
1224    
1225     /* select the clicked item (if there was one) */
1226     if(map_item.type != MAP_TYPE_ILLEGAL) {
1227     switch(map_item.type) {
1228     case MAP_TYPE_NODE:
1229     map_node_select(appdata, map_item.node);
1230     break;
1231    
1232     case MAP_TYPE_WAY:
1233     map_way_select(appdata, map_item.way);
1234     break;
1235    
1236     default:
1237     g_assert((map_item.type == MAP_TYPE_NODE) ||
1238     (map_item.type == MAP_TYPE_WAY));
1239     break;
1240     }
1241     }
1242     }
1243    
1244     static void map_touchnode_update(appdata_t *appdata, gint x, gint y) {
1245     map_t *map = appdata->map;
1246    
1247     map_hl_touchnode_clear(map);
1248    
1249     node_t *cur_node = NULL;
1250    
1251     /* the "current node" which is the one we are working on and which */
1252     /* should not be highlighted depends on the action */
1253     switch(map->action.type) {
1254    
1255     /* in idle mode the dragged node is not highlighted */
1256     case MAP_ACTION_IDLE:
1257     g_assert(map->pen_down.on_item);
1258     g_assert(map->pen_down.on_item->type == MAP_TYPE_NODE);
1259     cur_node = map->pen_down.on_item->node;
1260     break;
1261    
1262     default:
1263     break;
1264     }
1265    
1266    
1267     /* check if we are close to one of the other nodes */
1268     canvas_window2world(appdata->map->canvas, x, y, &x, &y);
1269     node_t *node = appdata->osm->node;
1270     while(!map->touchnode && node) {
1271     /* don't highlight the dragged node itself and don't highlight */
1272     /* deleted ones */
1273     if((node != cur_node) && (!(node->flags & OSM_FLAG_DELETED))) {
1274     gint nx = x - node->lpos.x;
1275     gint ny = y - node->lpos.y;
1276    
1277     if((nx < map->style->node.radius) && (ny < map->style->node.radius) &&
1278     (nx*nx + ny*ny < map->style->node.radius * map->style->node.radius))
1279     map_hl_touchnode_draw(map, node);
1280     }
1281     node = node->next;
1282     }
1283    
1284     /* during way creation also nodes of the new way */
1285     /* need to be searched */
1286     if(!map->touchnode && map->action.way) {
1287     node_chain_t *chain = map->action.way->node_chain;
1288     while(!map->touchnode && chain && chain->next) {
1289     gint nx = x - chain->node->lpos.x;
1290     gint ny = y - chain->node->lpos.y;
1291    
1292     if((nx < map->style->node.radius) && (ny < map->style->node.radius) &&
1293     (nx*nx + ny*ny < map->style->node.radius * map->style->node.radius))
1294     map_hl_touchnode_draw(map, chain->node);
1295    
1296     chain = chain->next;
1297     }
1298     }
1299     }
1300    
1301     static void map_button_press(map_t *map, gint x, gint y) {
1302    
1303     printf("left button pressed\n");
1304     map->pen_down.is = TRUE;
1305    
1306     /* save press position */
1307     map->pen_down.at.x = x;
1308     map->pen_down.at.y = y;
1309     map->pen_down.drag = FALSE; // don't assume drag yet
1310    
1311     /* determine wether this press was on an item */
1312     map->pen_down.on_item = map_real_item_at(map, x, y);
1313    
1314     /* check if the clicked item is a highlighted node as the user */
1315     /* might want to drag that */
1316     map->pen_down.on_selected_node = FALSE;
1317     if(map->pen_down.on_item)
1318     map->pen_down.on_selected_node =
1319     map_item_is_selected_node(map, map->pen_down.on_item);
1320    
1321     /* button press */
1322     switch(map->action.type) {
1323    
1324     case MAP_ACTION_WAY_NODE_ADD:
1325     map_edit_way_node_add_highlight(map, map->pen_down.on_item, x, y);
1326     break;
1327    
1328     case MAP_ACTION_WAY_CUT:
1329     map_edit_way_cut_highlight(map, map->pen_down.on_item, x, y);
1330     break;
1331    
1332     case MAP_ACTION_NODE_ADD:
1333     map_hl_cursor_draw(map, x, y, FALSE, map->style->node.radius);
1334     break;
1335    
1336     case MAP_ACTION_WAY_ADD:
1337     map_hl_cursor_draw(map, x, y, FALSE, map->style->node.radius);
1338     map_touchnode_update(map->appdata, x, y);
1339     break;
1340    
1341     default:
1342     break;
1343     }
1344     }
1345    
1346     /* move the background image (wms data) during wms adjustment */
1347     static void map_bg_adjust(map_t *map, gint x, gint y) {
1348     g_assert(map->appdata);
1349     g_assert(map->appdata->osm);
1350     g_assert(map->appdata->osm->bounds);
1351    
1352     x += map->appdata->osm->bounds->min.x + map->bg.offset.x -
1353     map->pen_down.at.x;
1354     y += map->appdata->osm->bounds->min.y + map->bg.offset.y -
1355     map->pen_down.at.y;
1356    
1357     canvas_image_move(map->bg.item, x, y, map->bg.scale.x, map->bg.scale.y);
1358     }
1359    
1360     static void map_button_release(map_t *map, gint x, gint y) {
1361     map->pen_down.is = FALSE;
1362    
1363     /* before button release is handled */
1364     switch(map->action.type) {
1365     case MAP_ACTION_BG_ADJUST:
1366     map_bg_adjust(map, x, y);
1367     map->bg.offset.x += x - map->pen_down.at.x;
1368     map->bg.offset.y += y - map->pen_down.at.y;
1369     break;
1370    
1371     case MAP_ACTION_IDLE:
1372     /* check if distance to press is above drag limit */
1373     if(!map->pen_down.drag)
1374     map->pen_down.drag = distance_above(map, x, y, MAP_DRAG_LIMIT);
1375    
1376     if(!map->pen_down.drag) {
1377     printf("left button released after click\n");
1378    
1379     map_item_t old_sel = map->selected;
1380     map_handle_click(map->appdata, map);
1381    
1382     if((old_sel.type != MAP_TYPE_ILLEGAL) &&
1383     (old_sel.type == map->selected.type) &&
1384     (old_sel.ptr == map->selected.ptr)) {
1385     printf("re-selected same item of type %d, "
1386     "pushing it to the bottom\n", old_sel.type);
1387    
1388     if(!map->selected.item) {
1389     printf(" item has no visible representation to push\n");
1390     } else {
1391     canvas_item_to_bottom(map->selected.item);
1392    
1393     /* update clicked item, to correctly handle the click */
1394     map->pen_down.on_item =
1395     map_real_item_at(map, map->pen_down.at.x, map->pen_down.at.y);
1396    
1397     map_handle_click(map->appdata, map);
1398     }
1399     }
1400     } else {
1401     printf("left button released after drag\n");
1402    
1403     /* just scroll if we didn't drag an selected item */
1404     if(!map->pen_down.on_selected_node)
1405     map_do_scroll(map, x, y);
1406     else {
1407     printf("released after dragging node\n");
1408     map_hl_cursor_clear(map);
1409    
1410     /* now actually move the node */
1411     map_edit_node_move(map->appdata, map->pen_down.on_item, x, y);
1412     }
1413     }
1414     break;
1415    
1416     case MAP_ACTION_NODE_ADD:
1417     printf("released after NODE ADD\n");
1418     map_hl_cursor_clear(map);
1419    
1420     /* convert mouse position to canvas (world) position */
1421     canvas_window2world(map->canvas, x, y, &x, &y);
1422    
1423     node_t *node = NULL;
1424     if(!osm_position_within_bounds(map->appdata->osm, x, y))
1425     map_outside_error(map->appdata);
1426     else {
1427     node = osm_node_new(map->appdata->osm, x, y);
1428     osm_node_attach(map->appdata->osm, node);
1429     map_node_draw(map, node);
1430     }
1431     map_action_set(map->appdata, MAP_ACTION_IDLE);
1432    
1433     map_item_deselect(map->appdata);
1434    
1435     if(node) {
1436     map_node_select(map->appdata, node);
1437    
1438     /* let the user specify some tags for the new node */
1439     info_dialog(GTK_WIDGET(map->appdata->window), map->appdata, NULL);
1440     }
1441     break;
1442    
1443     case MAP_ACTION_WAY_ADD:
1444     printf("released after WAY ADD\n");
1445     map_hl_cursor_clear(map);
1446    
1447     map_edit_way_add_segment(map, x, y);
1448     break;
1449    
1450     case MAP_ACTION_WAY_NODE_ADD:
1451     printf("released after WAY NODE ADD\n");
1452     map_hl_cursor_clear(map);
1453    
1454     map_edit_way_node_add(map, x, y);
1455     break;
1456    
1457     case MAP_ACTION_WAY_CUT:
1458     printf("released after WAY CUT\n");
1459     map_hl_cursor_clear(map);
1460    
1461     map_edit_way_cut(map, x, y);
1462     break;
1463    
1464    
1465     default:
1466     break;
1467     }
1468     }
1469    
1470     static gboolean map_button_event(GtkWidget *widget, GdkEventButton *event,
1471     gpointer data) {
1472     appdata_t *appdata = (appdata_t*)data;
1473     map_t *map = appdata->map;
1474    
1475     if(!appdata->osm) return FALSE;
1476    
1477     if(event->button == 1) {
1478     gint x = event->x, y = event->y;
1479    
1480     if(event->type == GDK_BUTTON_PRESS)
1481     map_button_press(map, x, y);
1482    
1483     if(event->type == GDK_BUTTON_RELEASE)
1484     map_button_release(map, x, y);
1485     }
1486    
1487     return FALSE; /* forward to further processing */
1488     }
1489    
1490     static gboolean map_motion_notify_event(GtkWidget *widget,
1491     GdkEventMotion *event, gpointer data) {
1492     appdata_t *appdata = (appdata_t*)data;
1493     map_t *map = appdata->map;
1494     gint x, y;
1495     GdkModifierType state;
1496    
1497     if(!appdata->osm) return FALSE;
1498    
1499     #ifdef USE_HILDON
1500     /* reduce update frequency on hildon to keep screen update fluid */
1501     static guint32 last_time = 0;
1502    
1503 harbaum 24 if(event->time - last_time < 250) return FALSE;
1504 harbaum 1 last_time = event->time;
1505     #endif
1506    
1507     if(!map->pen_down.is)
1508     return FALSE;
1509    
1510 harbaum 78 #ifndef USE_GOOCANVAS
1511 harbaum 1 /* handle hints, hints are handled by goocanvas directly */
1512     if(event->is_hint)
1513     gdk_window_get_pointer(event->window, &x, &y, &state);
1514     else
1515     #endif
1516     {
1517     x = event->x;
1518     y = event->y;
1519     state = event->state;
1520     }
1521    
1522     /* check if distance to press is above drag limit */
1523     if(!map->pen_down.drag)
1524     map->pen_down.drag = distance_above(map, x, y, MAP_DRAG_LIMIT);
1525    
1526     /* drag */
1527     switch(map->action.type) {
1528     case MAP_ACTION_BG_ADJUST:
1529     map_bg_adjust(map, x, y);
1530     break;
1531    
1532     case MAP_ACTION_IDLE:
1533     if(map->pen_down.drag) {
1534     /* just scroll if we didn't drag an selected item */
1535     if(!map->pen_down.on_selected_node)
1536     map_do_scroll(map, x, y);
1537     else {
1538     map_hl_cursor_draw(map, x, y, FALSE, map->style->node.radius);
1539     map_touchnode_update(appdata, x, y);
1540     }
1541     }
1542     break;
1543    
1544     case MAP_ACTION_NODE_ADD:
1545     map_hl_cursor_draw(map, x, y, FALSE, map->style->node.radius);
1546     break;
1547 harbaum 15
1548 harbaum 1 case MAP_ACTION_WAY_ADD:
1549     map_hl_cursor_draw(map, x, y, FALSE, map->style->node.radius);
1550     map_touchnode_update(appdata, x, y);
1551     break;
1552    
1553     case MAP_ACTION_WAY_NODE_ADD:
1554     map_hl_cursor_clear(map);
1555 harbaum 15 map_item_t *item = map_real_item_at(map, x, y);
1556 harbaum 1 if(item) map_edit_way_node_add_highlight(map, item, x, y);
1557     break;
1558    
1559     case MAP_ACTION_WAY_CUT:
1560     map_hl_cursor_clear(map);
1561 harbaum 15 item = map_real_item_at(map, x, y);
1562 harbaum 1 if(item) map_edit_way_cut_highlight(map, item, x, y);
1563     break;
1564    
1565     default:
1566     break;
1567     }
1568    
1569    
1570     return FALSE; /* forward to further processing */
1571     }
1572    
1573     gboolean map_key_press_event(appdata_t *appdata, GdkEventKey *event) {
1574    
1575     if(!appdata->osm) return FALSE;
1576    
1577     /* map needs to be there to handle buttons */
1578     if(!appdata->map->canvas)
1579     return FALSE;
1580    
1581     if(event->type == GDK_KEY_PRESS) {
1582     gdouble zoom = 0;
1583     switch(event->keyval) {
1584    
1585     case GDK_Left:
1586     map_do_scroll_step(appdata->map, -50, 0);
1587     break;
1588    
1589     case GDK_Right:
1590     map_do_scroll_step(appdata->map, +50, 0);
1591     break;
1592    
1593     case GDK_Up:
1594     map_do_scroll_step(appdata->map, 0, -50);
1595     break;
1596    
1597     case GDK_Down:
1598     map_do_scroll_step(appdata->map, 0, +50);
1599     break;
1600    
1601     case GDK_Return: // same as HILDON_HARDKEY_SELECT
1602     /* if the ok button is enabled, call its function */
1603     if(GTK_WIDGET_FLAGS(appdata->iconbar->ok) & GTK_SENSITIVE)
1604     map_action_ok(appdata);
1605     /* otherwise if info is enabled call that */
1606     else if(GTK_WIDGET_FLAGS(appdata->iconbar->info) & GTK_SENSITIVE)
1607     info_dialog(GTK_WIDGET(appdata->window), appdata, NULL);
1608     break;
1609    
1610     case GDK_Escape: // same as HILDON_HARDKEY_ESC
1611     /* if the cancel button is enabled, call its function */
1612     if(GTK_WIDGET_FLAGS(appdata->iconbar->cancel) & GTK_SENSITIVE)
1613     map_action_cancel(appdata);
1614     break;
1615    
1616     #ifdef USE_HILDON
1617     case HILDON_HARDKEY_INCREASE:
1618     #else
1619     case '+':
1620     #endif
1621     zoom = appdata->map->state->zoom;
1622     zoom *= ZOOM_FACTOR_BUTTON;
1623     map_set_zoom(appdata->map, zoom, TRUE);
1624     printf("zoom is now %f (1:%d)\n", zoom, (int)zoom_to_scaledn(zoom));
1625     return TRUE;
1626     break;
1627    
1628     #ifdef USE_HILDON
1629     case HILDON_HARDKEY_DECREASE:
1630     #else
1631     case '-':
1632     #endif
1633     zoom = appdata->map->state->zoom;
1634     zoom /= ZOOM_FACTOR_BUTTON;
1635     map_set_zoom(appdata->map, zoom, TRUE);
1636     printf("zoom is now %f (1:%d)\n", zoom, (int)zoom_to_scaledn(zoom));
1637     return TRUE;
1638     break;
1639    
1640     default:
1641     printf("key event %d\n", event->keyval);
1642     break;
1643     }
1644     }
1645     return FALSE;
1646     }
1647    
1648     GtkWidget *map_new(appdata_t *appdata) {
1649     map_t *map = appdata->map = g_new0(map_t, 1);
1650    
1651     map->style = style_load(appdata, appdata->settings->style);
1652 harbaum 22 if(!map->style) {
1653     errorf(NULL, _("Unable to load valid style, terminating."));
1654     g_free(map);
1655     return NULL;
1656     }
1657 harbaum 1
1658     if(appdata->project && appdata->project->map_state) {
1659     printf("Using projects map state\n");
1660     map->state = appdata->project->map_state;
1661     } else {
1662     printf("Creating new map state\n");
1663     map->state = g_new0(map_state_t, 1);
1664     map->state->zoom = 0.25;
1665     }
1666    
1667     map->state->refcount++;
1668    
1669     map->pen_down.at.x = -1;
1670     map->pen_down.at.y = -1;
1671     map->appdata = appdata;
1672     map->action.type = MAP_ACTION_IDLE;
1673    
1674 harbaum 103 map->canvas = canvas_new(map->style->background.color);
1675     canvas_set_antialias(map->canvas, !appdata->settings->no_antialias);
1676 harbaum 1
1677 harbaum 103 GtkWidget *canvas_widget = canvas_get_widget(map->canvas);
1678 harbaum 1
1679 harbaum 103 gtk_widget_set_events(canvas_widget,
1680 harbaum 1 GDK_BUTTON_PRESS_MASK
1681     | GDK_BUTTON_RELEASE_MASK
1682     | GDK_SCROLL_MASK
1683     | GDK_POINTER_MOTION_MASK
1684     | GDK_POINTER_MOTION_HINT_MASK);
1685    
1686 harbaum 103 gtk_signal_connect(GTK_OBJECT(canvas_widget),
1687 harbaum 1 "button_press_event", G_CALLBACK(map_button_event), appdata);
1688 harbaum 103 gtk_signal_connect(GTK_OBJECT(canvas_widget),
1689 harbaum 1 "button_release_event", G_CALLBACK(map_button_event), appdata);
1690 harbaum 103 gtk_signal_connect(GTK_OBJECT(canvas_widget),
1691 harbaum 1 "motion_notify_event", G_CALLBACK(map_motion_notify_event), appdata);
1692 harbaum 103 gtk_signal_connect(GTK_OBJECT(canvas_widget),
1693 harbaum 1 "scroll_event", G_CALLBACK(map_scroll_event), appdata);
1694    
1695 harbaum 103 gtk_signal_connect(GTK_OBJECT(canvas_widget),
1696 harbaum 1 "destroy", G_CALLBACK(map_destroy_event), appdata);
1697    
1698 harbaum 103 return canvas_widget;
1699 harbaum 1 }
1700    
1701     void map_init(appdata_t *appdata) {
1702     map_t *map = appdata->map;
1703    
1704     /* set initial zoom */
1705     map_set_zoom(map, map->state->zoom, FALSE);
1706     josm_elemstyles_colorize_world(map->style, appdata->osm);
1707    
1708     map_draw(map, appdata->osm);
1709    
1710     float mult = appdata->map->style->frisket.mult;
1711     canvas_set_bounds(map->canvas,
1712     mult*appdata->osm->bounds->min.x,
1713     mult*appdata->osm->bounds->min.y,
1714     mult*appdata->osm->bounds->max.x,
1715     mult*appdata->osm->bounds->max.y);
1716    
1717 harbaum 103 printf("restore scroll position %d/%d\n",
1718 harbaum 1 map->state->scroll_offset.x, map->state->scroll_offset.y);
1719    
1720 harbaum 103 map_limit_scroll(map, CANVAS_UNIT_METER,
1721     &map->state->scroll_offset.x, &map->state->scroll_offset.y);
1722     canvas_scroll_to(map->canvas, CANVAS_UNIT_METER,
1723     map->state->scroll_offset.x, map->state->scroll_offset.y);
1724 harbaum 1 }
1725    
1726    
1727     void map_item_set_flags(map_item_t *map_item, int set, int clr) {
1728    
1729     switch(map_item->type) {
1730     case MAP_TYPE_NODE:
1731     map_item->node->flags |= set;
1732     map_item->node->flags &= ~clr;
1733     break;
1734    
1735     case MAP_TYPE_WAY:
1736     map_item->way->flags |= set;
1737     map_item->way->flags &= ~clr;
1738     break;
1739    
1740     default:
1741     g_assert(0);
1742     break;
1743     }
1744     }
1745    
1746 harbaum 103 void map_clear(appdata_t *appdata, gint group_mask) {
1747 harbaum 1 map_t *map = appdata->map;
1748    
1749     printf("freeing map contents\n");
1750    
1751     map_free_map_item_chains(appdata);
1752    
1753     /* remove a possibly existing highlight */
1754     map_item_deselect(appdata);
1755    
1756 harbaum 103 canvas_erase(map->canvas, group_mask);
1757 harbaum 1 }
1758    
1759     void map_paint(appdata_t *appdata) {
1760     map_t *map = appdata->map;
1761    
1762 harbaum 78 /* user may have changed antialias settings */
1763 harbaum 103 canvas_set_antialias(map->canvas, !appdata->settings->no_antialias);
1764 harbaum 24
1765 harbaum 1 josm_elemstyles_colorize_world(map->style, appdata->osm);
1766     map_draw(map, appdata->osm);
1767     }
1768    
1769     /* called from several icons like e.g. "node_add" */
1770     void map_action_set(appdata_t *appdata, map_action_t action) {
1771     printf("map action set to %d\n", action);
1772    
1773     appdata->map->action.type = action;
1774    
1775     /* enable/disable ok/cancel buttons */
1776     // MAP_ACTION_IDLE=0, NODE_ADD, BG_ADJUST, WAY_ADD, WAY_NODE_ADD, WAY_CUT
1777     const gboolean ok_state[] = { FALSE, FALSE, TRUE, FALSE, FALSE, FALSE };
1778     const gboolean cancel_state[] = { FALSE, TRUE, TRUE, TRUE, TRUE, TRUE };
1779    
1780     g_assert(MAP_ACTION_NUM == sizeof(ok_state)/sizeof(gboolean));
1781     g_assert(action < sizeof(ok_state)/sizeof(gboolean));
1782    
1783     icon_bar_map_cancel_ok(appdata, cancel_state[action], ok_state[action]);
1784    
1785     switch(action) {
1786     case MAP_ACTION_BG_ADJUST:
1787     /* an existing selection only causes confusion ... */
1788     map_item_deselect(appdata);
1789     break;
1790    
1791     case MAP_ACTION_WAY_ADD:
1792     printf("starting new way\n");
1793    
1794     /* remember if there was a way selected */
1795     way_t *way_sel = NULL;
1796     if(appdata->map->selected.type == MAP_TYPE_WAY)
1797     way_sel = appdata->map->selected.way;
1798    
1799     map_item_deselect(appdata);
1800     map_edit_way_add_begin(appdata->map, way_sel);
1801     break;
1802    
1803     case MAP_ACTION_NODE_ADD:
1804     map_item_deselect(appdata);
1805     break;
1806    
1807     default:
1808     break;
1809     }
1810    
1811     icon_bar_map_action_idle(appdata, action == MAP_ACTION_IDLE);
1812     gtk_widget_set_sensitive(appdata->menu_item_wms_adjust,
1813     action == MAP_ACTION_IDLE);
1814    
1815     const char *str_state[] = {
1816     NULL,
1817     _("Place a node"),
1818     _("Adjust background image position"),
1819     _("Place first node of new way"),
1820     _("Place node on selected way"),
1821     _("Select segment to cut way"),
1822     };
1823    
1824     g_assert(MAP_ACTION_NUM == sizeof(str_state)/sizeof(char*));
1825    
1826     statusbar_set(appdata, str_state[action], FALSE);
1827     }
1828    
1829    
1830     void map_action_cancel(appdata_t *appdata) {
1831     map_t *map = appdata->map;
1832    
1833     switch(map->action.type) {
1834     case MAP_ACTION_WAY_ADD:
1835     map_edit_way_add_cancel(map);
1836     break;
1837    
1838     case MAP_ACTION_BG_ADJUST:
1839     /* undo all changes to bg_offset */
1840     map->bg.offset.x = appdata->project->wms_offset.x;
1841     map->bg.offset.y = appdata->project->wms_offset.y;
1842    
1843     gint x = appdata->osm->bounds->min.x + map->bg.offset.x;
1844     gint y = appdata->osm->bounds->min.y + map->bg.offset.y;
1845     canvas_image_move(map->bg.item, x, y, map->bg.scale.x, map->bg.scale.y);
1846     break;
1847    
1848     default:
1849     break;
1850     }
1851    
1852     map_action_set(appdata, MAP_ACTION_IDLE);
1853     }
1854    
1855     void map_action_ok(appdata_t *appdata) {
1856     map_t *map = appdata->map;
1857    
1858     /* reset action now as this erases the statusbar and some */
1859     /* of the actions may set it */
1860     map_action_t type = map->action.type;
1861     map_action_set(appdata, MAP_ACTION_IDLE);
1862    
1863     switch(type) {
1864     case MAP_ACTION_WAY_ADD:
1865     map_edit_way_add_ok(map);
1866     break;
1867    
1868     case MAP_ACTION_BG_ADJUST:
1869     /* save changes to bg_offset in project */
1870     appdata->project->wms_offset.x = map->bg.offset.x;
1871     appdata->project->wms_offset.y = map->bg.offset.y;
1872     appdata->project->dirty = TRUE;
1873     break;
1874    
1875     default:
1876     break;
1877     }
1878     }
1879    
1880     /* called from icon "trash" */
1881     void map_delete_selected(appdata_t *appdata) {
1882     map_t *map = appdata->map;
1883    
1884     if(!yes_no_f(GTK_WIDGET(appdata->window),
1885     appdata, MISC_AGAIN_ID_DELETE, MISC_AGAIN_FLAG_DONT_SAVE_NO,
1886     _("Delete selected object?"),
1887     _("Do you really want to delete the selected object?")))
1888     return;
1889    
1890     /* work on local copy since de-selecting destroys the selection */
1891     map_item_t item = map->selected;
1892    
1893     /* deleting the selected item de-selects it ... */
1894     map_item_deselect(appdata);
1895    
1896 harbaum 54 undo_remember_delete(appdata, item.type, item.ptr);
1897    
1898 harbaum 1 switch(item.type) {
1899     case MAP_TYPE_NODE:
1900     printf("request to delete node #%ld\n", item.node->id);
1901    
1902     /* check if this node is part of a way with two nodes only. */
1903     /* we cannot delete this as this would also delete the way */
1904     way_chain_t *way_chain = osm_node_to_way(appdata->osm, item.node);
1905     if(way_chain) {
1906     gboolean short_way = FALSE;
1907    
1908     /* free the chain of ways */
1909     while(way_chain && !short_way) {
1910     way_chain_t *next = way_chain->next;
1911    
1912     if(osm_way_number_of_nodes(way_chain->way) <= 2)
1913     short_way = TRUE;
1914    
1915     g_free(way_chain);
1916     way_chain = next;
1917     }
1918    
1919     if(short_way) {
1920     if(!yes_no_f(GTK_WIDGET(appdata->window), NULL, 0, 0,
1921     _("Delete node in short way(s)?"),
1922     _("Deleting this node will also delete one or more ways "
1923     "since they'll contain only one node afterwards. "
1924     "Do you really want this?")))
1925     return;
1926     }
1927     }
1928    
1929     /* remove it visually from the screen */
1930     map_item_chain_destroy(&item.node->map_item_chain);
1931    
1932     /* and mark it "deleted" in the database */
1933     osm_node_remove_from_relation(appdata->osm, item.node);
1934     way_chain_t *chain = osm_node_delete(appdata->osm,
1935     &appdata->icon, item.node, FALSE, TRUE);
1936    
1937     /* redraw all affected ways */
1938     while(chain) {
1939     way_chain_t *next = chain->next;
1940    
1941     if(osm_way_number_of_nodes(chain->way) == 1) {
1942     /* this way now only contains one node and thus isn't a valid */
1943     /* way anymore. So it'll also get deleted (which in turn may */
1944     /* cause other nodes to be deleted as well) */
1945     map_way_delete(appdata, chain->way);
1946     } else {
1947     map_item_t item;
1948     item.type = MAP_TYPE_WAY;
1949     item.way = chain->way;
1950     map_item_redraw(appdata, &item);
1951     }
1952    
1953     g_free(chain);
1954    
1955     chain = next;
1956     }
1957    
1958     break;
1959    
1960     case MAP_TYPE_WAY:
1961     printf("request to delete way #%ld\n", item.way->id);
1962     map_way_delete(appdata, item.way);
1963     break;
1964    
1965     default:
1966     g_assert((item.type == MAP_TYPE_NODE) ||
1967     (item.type == MAP_TYPE_WAY));
1968     break;
1969     }
1970     }
1971    
1972     /* ----------------------- track related stuff ----------------------- */
1973    
1974     void map_track_draw_seg(map_t *map, track_seg_t *seg) {
1975     /* a track_seg needs at least 2 points to be drawn */
1976     guint pnum = track_seg_points(seg);
1977     printf("seg of length %d\n", pnum);
1978    
1979     if(pnum == 1) {
1980     g_assert(!seg->item);
1981    
1982 harbaum 103 seg->item = canvas_circle_new(map->canvas, CANVAS_GROUP_TRACK,
1983 harbaum 1 seg->track_point->lpos.x, seg->track_point->lpos.y,
1984     map->style->track.width/2.0, 0, map->style->track.color, NO_COLOR);
1985     }
1986    
1987     if(pnum > 1) {
1988    
1989     /* allocate space for nodes */
1990     canvas_points_t *points = canvas_points_new(pnum);
1991    
1992     int point = 0;
1993     track_point_t *track_point = seg->track_point;
1994     while(track_point) {
1995     points->coords[point++] = track_point->lpos.x;
1996     points->coords[point++] = track_point->lpos.y;
1997     track_point = track_point->next;
1998     }
1999    
2000     /* there may be a circle (one point line) */
2001     if(seg->item)
2002     canvas_item_destroy(seg->item);
2003    
2004 harbaum 103 seg->item = canvas_polyline_new(map->canvas, CANVAS_GROUP_TRACK,
2005 harbaum 1 points, map->style->track.width, map->style->track.color);
2006    
2007     canvas_points_free(points);
2008     }
2009     }
2010    
2011     void map_track_update_seg(map_t *map, track_seg_t *seg) {
2012     /* a track_seg needs at least 2 points to be drawn */
2013     guint pnum = track_seg_points(seg);
2014     printf("seg of length %d\n", pnum);
2015    
2016     if(pnum > 1) {
2017    
2018     /* allocate space for nodes */
2019     canvas_points_t *points = canvas_points_new(pnum);
2020    
2021     int point = 0;
2022     track_point_t *track_point = seg->track_point;
2023     while(track_point) {
2024     canvas_point_set_pos(points, point++, &track_point->lpos);
2025     track_point = track_point->next;
2026     }
2027    
2028     g_assert(seg->item);
2029     canvas_item_set_points(seg->item, points);
2030     canvas_points_free(points);
2031     }
2032     }
2033    
2034     void map_track_draw(map_t *map, track_t *track) {
2035     track_seg_t *seg = track->track_seg;
2036    
2037     /* draw all segments */
2038     while(seg) {
2039     map_track_draw_seg(map, seg);
2040     seg = seg->next;
2041     }
2042     }
2043    
2044     void map_track_remove(appdata_t *appdata) {
2045     track_t *track = appdata->track.track;
2046    
2047     printf("removing track\n");
2048    
2049     g_assert(track);
2050    
2051     /* remove all segments */
2052     track_seg_t *seg = track->track_seg;
2053     while(seg) {
2054     if(seg->item) {
2055     canvas_item_destroy(seg->item);
2056     seg->item = NULL;
2057     }
2058    
2059     seg = seg->next;
2060     }
2061     }
2062    
2063     void map_track_pos(appdata_t *appdata, lpos_t *lpos) {
2064     if(appdata->track.gps_item) {
2065     canvas_item_destroy(appdata->track.gps_item);
2066     appdata->track.gps_item = NULL;
2067     }
2068    
2069     if(lpos)
2070 harbaum 103 appdata->track.gps_item =
2071     canvas_circle_new(appdata->map->canvas, CANVAS_GROUP_GPS,
2072 harbaum 1 lpos->x, lpos->y, appdata->map->style->track.width/2.0, 0,
2073 harbaum 22 appdata->map->style->track.gps_color, NO_COLOR);
2074 harbaum 1 }
2075    
2076     /* ------------------- map background ------------------ */
2077    
2078     void map_remove_bg_image(map_t *map) {
2079     if(!map) return;
2080    
2081     if(map->bg.item) {
2082     canvas_item_destroy(map->bg.item);
2083     map->bg.item = NULL;
2084     }
2085     }
2086    
2087     static gint map_bg_item_destroy_event(GtkWidget *widget, gpointer data) {
2088     map_t *map = (map_t*)data;
2089    
2090     /* destroying background item */
2091    
2092     map->bg.item = NULL;
2093     if(map->bg.pix) {
2094     printf("destroying background item\n");
2095     gdk_pixbuf_unref(map->bg.pix);
2096     map->bg.pix = NULL;
2097     }
2098     return FALSE;
2099     }
2100    
2101     void map_set_bg_image(map_t *map, char *filename) {
2102     bounds_t *bounds = map->appdata->osm->bounds;
2103    
2104     map_remove_bg_image(map);
2105    
2106     map->bg.pix = gdk_pixbuf_new_from_file(filename, NULL);
2107    
2108     /* calculate required scale factor */
2109     map->bg.scale.x = (float)(bounds->max.x - bounds->min.x)/
2110     (float)gdk_pixbuf_get_width(map->bg.pix);
2111     map->bg.scale.y = (float)(bounds->max.y - bounds->min.y)/
2112     (float)gdk_pixbuf_get_height(map->bg.pix);
2113    
2114 harbaum 103 map->bg.item = canvas_image_new(map->canvas, CANVAS_GROUP_BG, map->bg.pix,
2115 harbaum 1 bounds->min.x, bounds->min.y, map->bg.scale.x, map->bg.scale.y);
2116    
2117     canvas_item_destroy_connect(map->bg.item,
2118     G_CALLBACK(map_bg_item_destroy_event), map);
2119     }
2120    
2121    
2122     /* -------- hide and show objects (for performance reasons) ------- */
2123    
2124     void map_hide_selected(appdata_t *appdata) {
2125     map_t *map = appdata->map;
2126     if(!map) return;
2127    
2128     if(map->selected.type != MAP_TYPE_WAY) {
2129     printf("selected item is not a way\n");
2130     return;
2131     }
2132    
2133     way_t *way = map->selected.way;
2134     printf("hiding way #%ld\n", way->id);
2135    
2136     map_item_deselect(appdata);
2137     way->flags |= OSM_FLAG_HIDDEN;
2138     map_item_chain_destroy(&way->map_item_chain);
2139    
2140     gtk_widget_set_sensitive(appdata->menu_item_map_show_all, TRUE);
2141     }
2142    
2143     void map_show_all(appdata_t *appdata) {
2144     map_t *map = appdata->map;
2145     if(!map) return;
2146    
2147     way_t *way = appdata->osm->way;
2148     while(way) {
2149     if(way->flags & OSM_FLAG_HIDDEN) {
2150     way->flags &= ~OSM_FLAG_HIDDEN;
2151     map_way_draw(map, way);
2152     }
2153    
2154     way = way->next;
2155     }
2156    
2157     gtk_widget_set_sensitive(appdata->menu_item_map_show_all, FALSE);
2158     }
2159 achadwick 13 // vim:et:ts=8:sw=2:sts=2:ai