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

Contents of /navit/navit/layout.c

Parent Directory Parent Directory | Revision Log Revision Log


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

   
Visit the ZANavi Wiki