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

Contents of /navit/navit/layout.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 41 - (show 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 /**
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 } l = g_new0(struct layout, 1);
57 l->name = g_strdup(name_attr->u.str);
58 if ((font_attr = attr_search(attrs, NULL, attr_font)))
59 {
60 l->font = g_strdup(font_attr->u.str);
61 }
62 if ((day_attr = attr_search(attrs, NULL, attr_daylayout)))
63 {
64 l->dayname = g_strdup(day_attr->u.str);
65 }
66 if ((night_attr = attr_search(attrs, NULL, attr_nightlayout)))
67 {
68 l->nightname = g_strdup(night_attr->u.str);
69 }
70 if ((color_attr = attr_search(attrs, NULL, attr_color)))
71 {
72 l->color = *color_attr->u.color;
73 }
74 else
75 {
76 l->color = def_color;
77 }
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 l->active = active_attr->u.num;
85 }
86 return l;
87 }
88
89 struct attr_iter
90 {
91 GList *last;
92 };
93
94 struct attr_iter *
95 layout_attr_iter_new(void)
96 {
97 return g_new0(struct attr_iter, 1);
98 }
99
100 void layout_attr_iter_destroy(struct attr_iter *iter)
101 {
102 g_free(iter);
103 }
104
105 int layout_get_attr(struct layout *layout, enum attr_type type, struct attr *attr, struct attr_iter *iter)
106 {
107 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 }
124 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 }
138 break;
139 case attr_active:
140 attr->u.num = layout->active;
141 break;
142 default:
143 break;
144 }
145 return 0;
146 }
147
148 int layout_add_attr(struct layout *layout, struct attr *attr)
149 {
150 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 }
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 */
171 struct cursor *
172 layout_get_cursor(struct layout *this_, char *name)
173 {
174 GList *c;
175 struct cursor *d = NULL;
176
177 c = g_list_first(this_->cursors);
178 while (c)
179 {
180 if (!strcmp(((struct cursor *) c->data)->name, name))
181 return c->data;
182 if (!strcmp(((struct cursor *) c->data)->name, "default"))
183 d = c->data;
184 c = g_list_next(c);
185 }
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 w = attr_search(attrs, NULL, attr_w);
196 h = attr_search(attrs, NULL, attr_h);
197 if (!w || !h)
198 return NULL;
199
200 this=g_new0(struct cursor,1);
201 this->w = w->u.num;
202 this->h = h->u.num;
203 name = attr_search(attrs, NULL, attr_name);
204 if (name)
205 this->name = g_strdup(name->u.str);
206 else
207 this->name = g_strdup("default");
208 interval = attr_search(attrs, NULL, attr_interval);
209 if (interval)
210 this->interval = interval->u.num;
211 sequence_range = attr_search(attrs, NULL, attr_sequence_range);
212 if (sequence_range)
213 {
214 struct range *r=g_new0(struct range,1);
215 r->min = sequence_range->u.range.min;
216 r->max = sequence_range->u.range.max;
217 this->sequence_range = r;
218 }
219 else
220 {
221 this->sequence_range = NULL;
222 }
223 //dbg(2, "ret=%p\n", this);
224 return this;
225 }
226
227 void cursor_destroy(struct cursor *this_)
228 {
229 if (this_->sequence_range)
230 g_free(this_->sequence_range);
231 if (this_->name)
232 {
233 g_free(this_->name);
234 }
235 g_free(this_);
236 }
237
238 int cursor_add_attr(struct cursor *this_, struct attr *attr)
239 {
240 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 }
248 return 0;
249 }
250
251 static int layer_set_attr_do(struct layer *l, struct attr *attr, int init)
252 {
253 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 }
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 l->active = 1;
276
277 for (; *attrs; attrs++)
278 {
279 layer_set_attr_do(l, *attrs, 1);
280 }
281
282 return l;
283 }
284
285 int layer_get_attr(struct layer *layer, enum attr_type type, struct attr *attr, struct attr_iter *iter)
286 {
287 attr->type = type;
288 switch (type)
289 {
290 case attr_active:
291 attr->u.num = layer->active;
292 return 1;
293 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 }
306 return 0;
307 }
308
309 int layer_add_attr(struct layer *layer, struct attr *attr)
310 {
311 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 }
319 }
320
321 int layer_set_attr(struct layer *layer, struct attr *attr)
322 {
323 return layer_set_attr_do(layer, attr, 0);
324 }
325
326 struct itemgra * itemgra_new(struct attr *parent, struct attr **attrs)
327 {
328
329 //dbg(0,"EEnter\n");
330
331 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
336 itm = g_new0(struct itemgra, 1);
337 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
345 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
362 if (item_types)
363 {
364 type = item_types->u.item_types;
365
366 while (type && *type != type_none)
367 {
368 itm->type = g_list_append(itm->type, GINT_TO_POINTER(*type));
369 type++;
370 }
371 }
372
373 //dbg(0,"return\n");
374
375 return itm;
376 }
377
378 int itemgra_add_attr(struct itemgra *itemgra, struct attr *attr)
379 {
380 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 case attr_maptile:
389 case attr_arrows:
390 itemgra->elements = g_list_append(itemgra->elements, attr->u.element);
391 return 1;
392
393 default:
394 dbg(0, "unknown: %s\n", attr_to_name(attr->type));
395 return 0;
396 }
397 }
398
399 static void element_set_color(struct element *e, struct attr **attrs)
400 {
401 struct attr *color;
402 color = attr_search(attrs, NULL, attr_color);
403 if (color)
404 e->color = *color->u.color;
405 }
406
407 static void element_set_background_color(struct color *c, struct attr **attrs)
408 {
409 struct attr *color;
410 color = attr_search(attrs, NULL, attr_background_color);
411 if (color)
412 *c = *color->u.color;
413 }
414
415 static void element_set_text_size(struct element *e, struct attr **attrs)
416 {
417 struct attr *text_size;
418 text_size = attr_search(attrs, NULL, attr_text_size);
419 if (text_size)
420 e->text_size = text_size->u.num;
421 }
422
423 static void element_set_polyline_width(struct element *e, struct attr **attrs)
424 {
425 struct attr *width;
426 width = attr_search(attrs, NULL, attr_width);
427 if (width)
428 e->u.polyline.width = width->u.num;
429 }
430
431 static void element_set_polyline_directed(struct element *e, struct attr **attrs)
432 {
433 struct attr *directed;
434 directed = attr_search(attrs, NULL, attr_directed);
435 if (directed)
436 e->u.polyline.directed = directed->u.num;
437 }
438
439 static void element_set_polyline_dash(struct element *e, struct attr **attrs)
440 {
441 struct attr *dash;
442 int i;
443
444 dash = attr_search(attrs, NULL, attr_dash);
445 if (dash)
446 {
447 /*
448 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 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 }
470 }
471
472 static void element_set_polyline_offset(struct element *e, struct attr **attrs)
473 {
474 struct attr *offset;
475 offset = attr_search(attrs, NULL, attr_offset);
476 if (offset)
477 {
478 e->u.polyline.offset = offset->u.num;
479 }
480 }
481
482 static void element_set_circle_width(struct element *e, struct attr **attrs)
483 {
484 struct attr *width;
485 width = attr_search(attrs, NULL, attr_width);
486 if (width)
487 e->u.circle.width = width->u.num;
488 }
489
490 static void element_set_circle_radius(struct element *e, struct attr **attrs)
491 {
492 struct attr *radius;
493 radius = attr_search(attrs, NULL, attr_radius);
494 if (radius)
495 e->u.circle.radius = radius->u.num;
496 }
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 e->type = element_polygon;
504 element_set_color(e, attrs);
505
506 return (struct polygon *) e;
507 }
508
509 struct polyline *
510 polyline_new(struct attr *parent, struct attr **attrs)
511 {
512 struct element *e;
513
514 e = g_new0(struct element, 1);
515 e->type = element_polyline;
516 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 return (struct polyline *) e;
522 }
523
524 struct circle *
525 circle_new(struct attr *parent, struct attr **attrs)
526 {
527 struct element *e;
528 struct color color_black = { COLOR_BLACK_ };
529 struct color color_white = { COLOR_WHITE_ };
530
531 e = g_new0(struct element, 1);
532 e->type = element_circle;
533 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 return (struct circle *) e;
542 }
543
544 struct text *
545 text_new(struct attr *parent, struct attr **attrs)
546 {
547 struct element *e;
548 struct color color_black = { COLOR_BLACK_ };
549 struct color color_white = { COLOR_WHITE_ };
550
551 e = g_new0(struct element, 1);
552 e->type = element_text;
553 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 return (struct text *) e;
560 }
561
562 struct icon *
563 icon_new(struct attr *parent, struct attr **attrs)
564 {
565 struct element *e;
566 struct attr *src, *w, *h, *rotation;
567 src = attr_search(attrs, NULL, attr_src);
568 if (!src)
569 return NULL;
570
571 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 else
577 e->u.icon.width = -1;
578 if ((h = attr_search(attrs, NULL, attr_h)))
579 e->u.icon.height = h->u.num;
580 else
581 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
586 return (struct icon *) e;
587 }
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 e->type = element_image;
596
597 return (struct image *) e;
598 }
599
600 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 struct arrows *
612 arrows_new(struct attr *parent, struct attr **attrs)
613 {
614 struct element *e;
615 e = g_malloc0(sizeof(*e));
616 e->type = element_arrows;
617 element_set_color(e, attrs);
618 return (struct arrows *) e;
619 }
620
621 int element_add_attr(struct element *e, struct attr *attr)
622 {
623 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 }
632 }

   
Visit the ZANavi Wiki