/[zanavi_public1]/navit/navit/layout.c
ZANavi

Contents of /navit/navit/layout.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 41 - (hide annotations) (download)
Tue Aug 11 18:50:37 2015 UTC (8 years, 7 months ago) by zoff99
File MIME type: text/plain
File size: 14283 byte(s)
many fixes, and new features
1 zoff99 2 /**
2 zoff99 31 * ZANavi, Zoff Android Navigation system.
3     * Copyright (C) 2011-2012 Zoff <zoff@zoff.cc>
4     *
5     * This program is free software; you can redistribute it and/or
6     * modify it under the terms of the GNU General Public License
7     * version 2 as published by the Free Software Foundation.
8     *
9     * This program is distributed in the hope that it will be useful,
10     * but WITHOUT ANY WARRANTY; without even the implied warranty of
11     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     * GNU General Public License for more details.
13     *
14     * You should have received a copy of the GNU General Public License
15     * along with this program; if not, write to the
16     * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17     * Boston, MA 02110-1301, USA.
18     */
19    
20     /**
21 zoff99 2 * Navit, a modular navigation system.
22     * Copyright (C) 2005-2009 Navit Team
23     *
24     * This program is free software; you can redistribute it and/or
25     * modify it under the terms of the GNU General Public License
26     * version 2 as published by the Free Software Foundation.
27     *
28     * This program is distributed in the hope that it will be useful,
29     * but WITHOUT ANY WARRANTY; without even the implied warranty of
30     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31     * GNU General Public License for more details.
32     *
33     * You should have received a copy of the GNU General Public License
34     * along with this program; if not, write to the
35     * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
36     * Boston, MA 02110-1301, USA.
37     */
38    
39     #include <glib.h>
40     #include <string.h>
41     #include "item.h"
42     #include "attr.h"
43     #include "layout.h"
44     #include "coord.h"
45     #include "debug.h"
46    
47     struct layout * layout_new(struct attr *parent, struct attr **attrs)
48     {
49     struct layout *l;
50 zoff99 31 struct color def_color = { COLOR_BACKGROUND_ };
51 zoff99 27 struct attr *name_attr, *color_attr, *order_delta_attr, *font_attr, *day_attr, *night_attr, *active_attr;
52 zoff99 2
53 zoff99 27 if (!(name_attr = attr_search(attrs, NULL, attr_name)))
54     {
55 zoff99 2 return NULL;
56 zoff99 31 } l = g_new0(struct layout, 1);
57 zoff99 2 l->name = g_strdup(name_attr->u.str);
58 zoff99 27 if ((font_attr = attr_search(attrs, NULL, attr_font)))
59     {
60 zoff99 2 l->font = g_strdup(font_attr->u.str);
61     }
62 zoff99 27 if ((day_attr = attr_search(attrs, NULL, attr_daylayout)))
63     {
64 zoff99 2 l->dayname = g_strdup(day_attr->u.str);
65     }
66 zoff99 27 if ((night_attr = attr_search(attrs, NULL, attr_nightlayout)))
67     {
68 zoff99 2 l->nightname = g_strdup(night_attr->u.str);
69     }
70 zoff99 27 if ((color_attr = attr_search(attrs, NULL, attr_color)))
71     {
72 zoff99 2 l->color = *color_attr->u.color;
73 zoff99 27 }
74 zoff99 2 else
75 zoff99 27 {
76 zoff99 2 l->color = def_color;
77 zoff99 27 }
78     if ((order_delta_attr = attr_search(attrs, NULL, attr_order_delta)))
79     {
80     l->order_delta = order_delta_attr->u.num;
81     }
82     if ((active_attr = attr_search(attrs, NULL, attr_active)))
83     {
84 zoff99 2 l->active = active_attr->u.num;
85 zoff99 27 }
86 zoff99 2 return l;
87     }
88    
89 zoff99 27 struct attr_iter
90     {
91     GList *last;
92 zoff99 2 };
93    
94     struct attr_iter *
95     layout_attr_iter_new(void)
96     {
97 zoff99 27 return g_new0(struct attr_iter, 1);
98 zoff99 2 }
99    
100 zoff99 27 void layout_attr_iter_destroy(struct attr_iter *iter)
101 zoff99 2 {
102     g_free(iter);
103     }
104    
105 zoff99 27 int layout_get_attr(struct layout *layout, enum attr_type type, struct attr *attr, struct attr_iter *iter)
106 zoff99 2 {
107 zoff99 27 GList *cursor, *layer;
108     attr->type = type;
109     switch (type)
110     {
111     case attr_cursor:
112     cursor = layout->cursors;
113     while (cursor)
114     {
115     if (!iter || iter->last == g_list_previous(cursor))
116     {
117     attr->u.cursor = cursor->data;
118     if (iter)
119     iter->last = cursor;
120     return 1;
121     }
122     cursor = g_list_next(cursor);
123 zoff99 2 }
124 zoff99 27 break;
125     case attr_layer:
126     layer = layout->layers;
127     while (layer)
128     {
129     if (!iter || iter->last == g_list_previous(layer))
130     {
131     attr->u.layer = layer->data;
132     if (iter)
133     iter->last = layer;
134     return 1;
135     }
136     layer = g_list_next(layer);
137 zoff99 2 }
138 zoff99 27 break;
139     case attr_active:
140     attr->u.num = layout->active;
141     break;
142     default:
143     break;
144 zoff99 2 }
145     return 0;
146     }
147    
148 zoff99 27 int layout_add_attr(struct layout *layout, struct attr *attr)
149 zoff99 2 {
150 zoff99 27 switch (attr->type)
151     {
152     case attr_cursor:
153     layout->cursors = g_list_append(layout->cursors, attr->u.cursor);
154     return 1;
155     case attr_layer:
156     layout->layers = g_list_append(layout->layers, attr->u.layer);
157     return 1;
158     default:
159     return 0;
160 zoff99 2 }
161     }
162    
163     /**
164     * Searchs the layout for a cursor with the given name.
165     *
166     * @param layout The layout
167     * @param name The name
168     * @returns A pointer to cursor with the given name or the name default or NULL.
169     * @author Ralph Sennhauser (10/2009)
170 zoff99 27 */
171 zoff99 2 struct cursor *
172     layout_get_cursor(struct layout *this_, char *name)
173     {
174     GList *c;
175 zoff99 27 struct cursor *d = NULL;
176 zoff99 2
177 zoff99 27 c = g_list_first(this_->cursors);
178     while (c)
179     {
180     if (!strcmp(((struct cursor *) c->data)->name, name))
181 zoff99 2 return c->data;
182 zoff99 27 if (!strcmp(((struct cursor *) c->data)->name, "default"))
183     d = c->data;
184     c = g_list_next(c);
185 zoff99 2 }
186     return d;
187     }
188    
189     struct cursor *
190     cursor_new(struct attr *parent, struct attr **attrs)
191     {
192     struct attr *w, *h, *name, *interval, *sequence_range;
193     struct cursor *this;
194    
195 zoff99 27 w = attr_search(attrs, NULL, attr_w);
196     h = attr_search(attrs, NULL, attr_h);
197     if (!w || !h)
198 zoff99 2 return NULL;
199    
200     this=g_new0(struct cursor,1);
201 zoff99 27 this->w = w->u.num;
202     this->h = h->u.num;
203     name = attr_search(attrs, NULL, attr_name);
204 zoff99 2 if (name)
205 zoff99 27 this->name = g_strdup(name->u.str);
206 zoff99 2 else
207 zoff99 27 this->name = g_strdup("default");
208     interval = attr_search(attrs, NULL, attr_interval);
209 zoff99 2 if (interval)
210 zoff99 27 this->interval = interval->u.num;
211     sequence_range = attr_search(attrs, NULL, attr_sequence_range);
212     if (sequence_range)
213     {
214 zoff99 2 struct range *r=g_new0(struct range,1);
215 zoff99 27 r->min = sequence_range->u.range.min;
216     r->max = sequence_range->u.range.max;
217     this->sequence_range = r;
218 zoff99 2 }
219 zoff99 27 else
220     {
221     this->sequence_range = NULL;
222 zoff99 2 }
223 zoff99 31 //dbg(2, "ret=%p\n", this);
224 zoff99 2 return this;
225     }
226    
227 zoff99 27 void cursor_destroy(struct cursor *this_)
228 zoff99 2 {
229     if (this_->sequence_range)
230     g_free(this_->sequence_range);
231 zoff99 27 if (this_->name)
232     {
233 zoff99 2 g_free(this_->name);
234     }
235     g_free(this_);
236     }
237    
238 zoff99 27 int cursor_add_attr(struct cursor *this_, struct attr *attr)
239 zoff99 2 {
240 zoff99 27 switch (attr->type)
241     {
242     case attr_itemgra:
243     this_->attrs = attr_generic_add_attr(this_->attrs, attr);
244     return 1;
245     default:
246     break;
247 zoff99 2 }
248     return 0;
249     }
250    
251 zoff99 27 static int layer_set_attr_do(struct layer *l, struct attr *attr, int init)
252 zoff99 2 {
253 zoff99 27 switch (attr->type)
254     {
255     case attr_active:
256     l->active = attr->u.num;
257     return 1;
258     case attr_details:
259     l->details = attr->u.num;
260     return 1;
261     case attr_name:
262     g_free(l->name);
263     l->name = g_strdup(attr->u.str);
264     return 1;
265     default:
266     return 0;
267 zoff99 2 }
268     }
269    
270     struct layer * layer_new(struct attr *parent, struct attr **attrs)
271     {
272     struct layer *l;
273    
274     l = g_new0(struct layer, 1);
275 zoff99 27 l->active = 1;
276 zoff99 41
277 zoff99 27 for (; *attrs; attrs++)
278     {
279 zoff99 2 layer_set_attr_do(l, *attrs, 1);
280     }
281 zoff99 41
282 zoff99 2 return l;
283     }
284    
285 zoff99 27 int layer_get_attr(struct layer *layer, enum attr_type type, struct attr *attr, struct attr_iter *iter)
286 zoff99 2 {
287 zoff99 27 attr->type = type;
288     switch (type)
289     {
290     case attr_active:
291     attr->u.num = layer->active;
292 zoff99 2 return 1;
293 zoff99 27 case attr_details:
294     attr->u.num = layer->details;
295     return 1;
296     case attr_name:
297     if (layer->name)
298     {
299     attr->u.str = layer->name;
300     return 1;
301     }
302     break;
303     default:
304     return 0;
305 zoff99 2 }
306     return 0;
307     }
308    
309 zoff99 27 int layer_add_attr(struct layer *layer, struct attr *attr)
310 zoff99 2 {
311 zoff99 27 switch (attr->type)
312     {
313     case attr_itemgra:
314     layer->itemgras = g_list_append(layer->itemgras, attr->u.itemgra);
315     return 1;
316     default:
317     return 0;
318 zoff99 2 }
319     }
320    
321 zoff99 27 int layer_set_attr(struct layer *layer, struct attr *attr)
322 zoff99 2 {
323     return layer_set_attr_do(layer, attr, 0);
324     }
325    
326     struct itemgra * itemgra_new(struct attr *parent, struct attr **attrs)
327     {
328 zoff99 30
329     //dbg(0,"EEnter\n");
330    
331 zoff99 2 struct itemgra *itm;
332     struct attr *order, *item_types, *speed_range, *angle_range, *sequence_range;
333     enum item_type *type;
334     struct range defrange;
335 zoff99 27
336 zoff99 2 itm = g_new0(struct itemgra, 1);
337 zoff99 27 order = attr_search(attrs, NULL, attr_order);
338     item_types = attr_search(attrs, NULL, attr_item_types);
339     speed_range = attr_search(attrs, NULL, attr_speed_range);
340     angle_range = attr_search(attrs, NULL, attr_angle_range);
341     sequence_range = attr_search(attrs, NULL, attr_sequence_range);
342     defrange.min = 0;
343     defrange.max = 32767;
344 zoff99 41
345 zoff99 27 if (order)
346     itm->order = order->u.range;
347     else
348     itm->order = defrange;
349     if (speed_range)
350     itm->speed_range = speed_range->u.range;
351     else
352     itm->speed_range = defrange;
353     if (angle_range)
354     itm->angle_range = angle_range->u.range;
355     else
356     itm->angle_range = defrange;
357     if (sequence_range)
358     itm->sequence_range = sequence_range->u.range;
359     else
360     itm->sequence_range = defrange;
361 zoff99 41
362 zoff99 27 if (item_types)
363     {
364     type = item_types->u.item_types;
365 zoff99 41
366 zoff99 27 while (type && *type != type_none)
367     {
368     itm->type = g_list_append(itm->type, GINT_TO_POINTER(*type));
369 zoff99 2 type++;
370     }
371     }
372 zoff99 30
373     //dbg(0,"return\n");
374    
375 zoff99 2 return itm;
376     }
377 zoff99 41
378 zoff99 27 int itemgra_add_attr(struct itemgra *itemgra, struct attr *attr)
379 zoff99 2 {
380 zoff99 27 switch (attr->type)
381     {
382     case attr_polygon:
383     case attr_polyline:
384     case attr_circle:
385     case attr_text:
386     case attr_icon:
387     case attr_image:
388 zoff99 34 case attr_maptile:
389 zoff99 27 case attr_arrows:
390     itemgra->elements = g_list_append(itemgra->elements, attr->u.element);
391     return 1;
392 zoff99 41
393 zoff99 27 default:
394     dbg(0, "unknown: %s\n", attr_to_name(attr->type));
395     return 0;
396 zoff99 2 }
397     }
398    
399 zoff99 27 static void element_set_color(struct element *e, struct attr **attrs)
400 zoff99 2 {
401     struct attr *color;
402 zoff99 27 color = attr_search(attrs, NULL, attr_color);
403 zoff99 2 if (color)
404 zoff99 27 e->color = *color->u.color;
405 zoff99 2 }
406    
407 zoff99 27 static void element_set_background_color(struct color *c, struct attr **attrs)
408 zoff99 2 {
409     struct attr *color;
410 zoff99 27 color = attr_search(attrs, NULL, attr_background_color);
411 zoff99 2 if (color)
412 zoff99 27 *c = *color->u.color;
413 zoff99 2 }
414    
415 zoff99 27 static void element_set_text_size(struct element *e, struct attr **attrs)
416 zoff99 2 {
417     struct attr *text_size;
418 zoff99 27 text_size = attr_search(attrs, NULL, attr_text_size);
419 zoff99 2 if (text_size)
420 zoff99 27 e->text_size = text_size->u.num;
421 zoff99 2 }
422    
423 zoff99 27 static void element_set_polyline_width(struct element *e, struct attr **attrs)
424 zoff99 2 {
425     struct attr *width;
426 zoff99 27 width = attr_search(attrs, NULL, attr_width);
427 zoff99 2 if (width)
428 zoff99 27 e->u.polyline.width = width->u.num;
429 zoff99 2 }
430    
431 zoff99 27 static void element_set_polyline_directed(struct element *e, struct attr **attrs)
432 zoff99 2 {
433     struct attr *directed;
434 zoff99 27 directed = attr_search(attrs, NULL, attr_directed);
435 zoff99 2 if (directed)
436 zoff99 27 e->u.polyline.directed = directed->u.num;
437 zoff99 2 }
438    
439 zoff99 27 static void element_set_polyline_dash(struct element *e, struct attr **attrs)
440 zoff99 2 {
441     struct attr *dash;
442     int i;
443    
444 zoff99 27 dash = attr_search(attrs, NULL, attr_dash);
445     if (dash)
446     {
447     /*
448 zoff99 31 for (i = 0; i < 4; i++)
449     {
450     if (!dash->u.dash[i])
451     {
452     break;
453     }
454     dbg(0,"1 i=%d d=%d\n", i, dash->u.dash[i]);
455     //e->u.polyline.dash_table[i] = atoi(dash->u.dash[i]);
456     e->u.polyline.dash_table[i] = dash->u.dash[i];
457     }
458     */
459     i = 0;
460 zoff99 27 if (!dash->u.dash[i])
461     {
462     }
463     else
464     {
465     e->u.polyline.dash_table[i] = dash->u.dash[i];
466     //dbg(0,"1 i=%d d=%d\n", i, dash->u.dash[i]);
467     e->u.polyline.dash_num = (i + 1);
468     }
469 zoff99 2 }
470     }
471    
472 zoff99 27 static void element_set_polyline_offset(struct element *e, struct attr **attrs)
473 zoff99 2 {
474     struct attr *offset;
475 zoff99 27 offset = attr_search(attrs, NULL, attr_offset);
476 zoff99 2 if (offset)
477 zoff99 27 {
478     e->u.polyline.offset = offset->u.num;
479     }
480 zoff99 2 }
481    
482 zoff99 27 static void element_set_circle_width(struct element *e, struct attr **attrs)
483 zoff99 2 {
484     struct attr *width;
485 zoff99 27 width = attr_search(attrs, NULL, attr_width);
486 zoff99 2 if (width)
487 zoff99 27 e->u.circle.width = width->u.num;
488 zoff99 2 }
489    
490 zoff99 27 static void element_set_circle_radius(struct element *e, struct attr **attrs)
491 zoff99 2 {
492     struct attr *radius;
493 zoff99 27 radius = attr_search(attrs, NULL, attr_radius);
494 zoff99 2 if (radius)
495 zoff99 27 e->u.circle.radius = radius->u.num;
496 zoff99 2 }
497    
498     struct polygon *
499     polygon_new(struct attr *parent, struct attr **attrs)
500     {
501     struct element *e;
502     e = g_new0(struct element, 1);
503 zoff99 27 e->type = element_polygon;
504 zoff99 2 element_set_color(e, attrs);
505    
506 zoff99 27 return (struct polygon *) e;
507 zoff99 2 }
508    
509     struct polyline *
510     polyline_new(struct attr *parent, struct attr **attrs)
511     {
512     struct element *e;
513 zoff99 27
514 zoff99 2 e = g_new0(struct element, 1);
515 zoff99 27 e->type = element_polyline;
516 zoff99 2 element_set_color(e, attrs);
517     element_set_polyline_width(e, attrs);
518     element_set_polyline_directed(e, attrs);
519     element_set_polyline_dash(e, attrs);
520     element_set_polyline_offset(e, attrs);
521 zoff99 27 return (struct polyline *) e;
522 zoff99 2 }
523    
524     struct circle *
525     circle_new(struct attr *parent, struct attr **attrs)
526     {
527     struct element *e;
528 zoff99 31 struct color color_black = { COLOR_BLACK_ };
529     struct color color_white = { COLOR_WHITE_ };
530 zoff99 2
531     e = g_new0(struct element, 1);
532 zoff99 27 e->type = element_circle;
533 zoff99 2 e->color = color_black;
534     e->u.circle.background_color = color_white;
535     element_set_color(e, attrs);
536     element_set_background_color(&e->u.circle.background_color, attrs);
537     element_set_text_size(e, attrs);
538     element_set_circle_width(e, attrs);
539     element_set_circle_radius(e, attrs);
540    
541 zoff99 27 return (struct circle *) e;
542 zoff99 2 }
543    
544     struct text *
545     text_new(struct attr *parent, struct attr **attrs)
546     {
547     struct element *e;
548 zoff99 31 struct color color_black = { COLOR_BLACK_ };
549     struct color color_white = { COLOR_WHITE_ };
550 zoff99 27
551 zoff99 2 e = g_new0(struct element, 1);
552 zoff99 27 e->type = element_text;
553 zoff99 2 element_set_text_size(e, attrs);
554     e->color = color_black;
555     e->u.text.background_color = color_white;
556     element_set_color(e, attrs);
557     element_set_background_color(&e->u.text.background_color, attrs);
558    
559 zoff99 27 return (struct text *) e;
560 zoff99 2 }
561    
562     struct icon *
563     icon_new(struct attr *parent, struct attr **attrs)
564     {
565     struct element *e;
566 zoff99 27 struct attr *src, *w, *h, *rotation;
567     src = attr_search(attrs, NULL, attr_src);
568     if (!src)
569 zoff99 2 return NULL;
570    
571 zoff99 27 e = g_malloc0(sizeof(*e) + strlen(src->u.str) + 1);
572     e->type = element_icon;
573     e->u.icon.src = (char *) (e + 1);
574     if ((w = attr_search(attrs, NULL, attr_w)))
575     e->u.icon.width = w->u.num;
576 zoff99 2 else
577 zoff99 27 e->u.icon.width = -1;
578     if ((h = attr_search(attrs, NULL, attr_h)))
579     e->u.icon.height = h->u.num;
580 zoff99 2 else
581 zoff99 27 e->u.icon.height = -1;
582     if ((rotation = attr_search(attrs, NULL, attr_rotation)))
583     e->u.icon.rotation = rotation->u.num;
584     strcpy(e->u.icon.src, src->u.str);
585 zoff99 2
586 zoff99 27 return (struct icon *) e;
587 zoff99 2 }
588    
589     struct image *
590     image_new(struct attr *parent, struct attr **attrs)
591     {
592     struct element *e;
593    
594     e = g_malloc0(sizeof(*e));
595 zoff99 27 e->type = element_image;
596 zoff99 2
597 zoff99 27 return (struct image *) e;
598 zoff99 2 }
599    
600 zoff99 34 struct maptile *
601     maptile_new(struct attr *parent, struct attr **attrs)
602     {
603     struct element *e;
604    
605     e = g_malloc0(sizeof(*e));
606     e->type = element_maptile;
607    
608     return (struct maptile *) e;
609     }
610    
611 zoff99 2 struct arrows *
612     arrows_new(struct attr *parent, struct attr **attrs)
613     {
614     struct element *e;
615     e = g_malloc0(sizeof(*e));
616 zoff99 27 e->type = element_arrows;
617 zoff99 2 element_set_color(e, attrs);
618 zoff99 27 return (struct arrows *) e;
619 zoff99 2 }
620    
621 zoff99 27 int element_add_attr(struct element *e, struct attr *attr)
622 zoff99 2 {
623 zoff99 27 switch (attr->type)
624     {
625     case attr_coord:
626     e->coord = g_realloc(e->coord, (e->coord_count + 1) * sizeof(struct coord));
627     e->coord[e->coord_count++] = *attr->u.coord;
628     return 1;
629     default:
630     return 0;
631 zoff99 2 }
632     }

   
Visit the ZANavi Wiki