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

Contents of /navit/navit/layout.c

Parent Directory Parent Directory | Revision Log Revision Log


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

   
Visit the ZANavi Wiki