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

Contents of /navit/navit/layout.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 30 - (show annotations) (download)
Wed Aug 22 17:01:27 2012 UTC (11 years, 7 months ago) by zoff99
File MIME type: text/plain
File size: 13311 byte(s)
ZANavi 2.0, lots of changes, everything in its own thread, more performance
1 /**
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 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
35 if (!(name_attr = attr_search(attrs, NULL, attr_name)))
36 {
37 return NULL;
38 }
39 l = g_new0(struct layout, 1);
40 l->name = g_strdup(name_attr->u.str);
41 if ((font_attr = attr_search(attrs, NULL, attr_font)))
42 {
43 l->font = g_strdup(font_attr->u.str);
44 }
45 if ((day_attr = attr_search(attrs, NULL, attr_daylayout)))
46 {
47 l->dayname = g_strdup(day_attr->u.str);
48 }
49 if ((night_attr = attr_search(attrs, NULL, attr_nightlayout)))
50 {
51 l->nightname = g_strdup(night_attr->u.str);
52 }
53 if ((color_attr = attr_search(attrs, NULL, attr_color)))
54 {
55 l->color = *color_attr->u.color;
56 }
57 else
58 {
59 l->color = def_color;
60 }
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 l->active = active_attr->u.num;
68 }
69 return l;
70 }
71
72 struct attr_iter
73 {
74 GList *last;
75 };
76
77 struct attr_iter *
78 layout_attr_iter_new(void)
79 {
80 return g_new0(struct attr_iter, 1);
81 }
82
83 void layout_attr_iter_destroy(struct attr_iter *iter)
84 {
85 g_free(iter);
86 }
87
88 int layout_get_attr(struct layout *layout, enum attr_type type, struct attr *attr, struct attr_iter *iter)
89 {
90 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 }
107 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 }
121 break;
122 case attr_active:
123 attr->u.num = layout->active;
124 break;
125 default:
126 break;
127 }
128 return 0;
129 }
130
131 int layout_add_attr(struct layout *layout, struct attr *attr)
132 {
133 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 }
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 */
154 struct cursor *
155 layout_get_cursor(struct layout *this_, char *name)
156 {
157 GList *c;
158 struct cursor *d = NULL;
159
160 c = g_list_first(this_->cursors);
161 while (c)
162 {
163 if (!strcmp(((struct cursor *) c->data)->name, name))
164 return c->data;
165 if (!strcmp(((struct cursor *) c->data)->name, "default"))
166 d = c->data;
167 c = g_list_next(c);
168 }
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 w = attr_search(attrs, NULL, attr_w);
179 h = attr_search(attrs, NULL, attr_h);
180 if (!w || !h)
181 return NULL;
182
183 this=g_new0(struct cursor,1);
184 this->w = w->u.num;
185 this->h = h->u.num;
186 name = attr_search(attrs, NULL, attr_name);
187 if (name)
188 this->name = g_strdup(name->u.str);
189 else
190 this->name = g_strdup("default");
191 interval = attr_search(attrs, NULL, attr_interval);
192 if (interval)
193 this->interval = interval->u.num;
194 sequence_range = attr_search(attrs, NULL, attr_sequence_range);
195 if (sequence_range)
196 {
197 struct range *r=g_new0(struct range,1);
198 r->min = sequence_range->u.range.min;
199 r->max = sequence_range->u.range.max;
200 this->sequence_range = r;
201 }
202 else
203 {
204 this->sequence_range = NULL;
205 }
206 dbg(2, "ret=%p\n", this);
207 return this;
208 }
209
210 void cursor_destroy(struct cursor *this_)
211 {
212 if (this_->sequence_range)
213 g_free(this_->sequence_range);
214 if (this_->name)
215 {
216 g_free(this_->name);
217 }
218 g_free(this_);
219 }
220
221 int cursor_add_attr(struct cursor *this_, struct attr *attr)
222 {
223 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 }
231 return 0;
232 }
233
234 static int layer_set_attr_do(struct layer *l, struct attr *attr, int init)
235 {
236 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 }
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 l->active = 1;
259 for (; *attrs; attrs++)
260 {
261 layer_set_attr_do(l, *attrs, 1);
262 }
263 return l;
264 }
265
266 int layer_get_attr(struct layer *layer, enum attr_type type, struct attr *attr, struct attr_iter *iter)
267 {
268 attr->type = type;
269 switch (type)
270 {
271 case attr_active:
272 attr->u.num = layer->active;
273 return 1;
274 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 }
287 return 0;
288 }
289
290 int layer_add_attr(struct layer *layer, struct attr *attr)
291 {
292 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 }
300 }
301
302 int layer_set_attr(struct layer *layer, struct attr *attr)
303 {
304 return layer_set_attr_do(layer, attr, 0);
305 }
306
307 struct itemgra * itemgra_new(struct attr *parent, struct attr **attrs)
308 {
309
310 //dbg(0,"EEnter\n");
311
312 struct itemgra *itm;
313 struct attr *order, *item_types, *speed_range, *angle_range, *sequence_range;
314 enum item_type *type;
315 struct range defrange;
316
317 itm = g_new0(struct itemgra, 1);
318 order = attr_search(attrs, NULL, attr_order);
319 item_types = attr_search(attrs, NULL, attr_item_types);
320 speed_range = attr_search(attrs, NULL, attr_speed_range);
321 angle_range = attr_search(attrs, NULL, attr_angle_range);
322 sequence_range = attr_search(attrs, NULL, attr_sequence_range);
323 defrange.min = 0;
324 defrange.max = 32767;
325 if (order)
326 itm->order = order->u.range;
327 else
328 itm->order = defrange;
329 if (speed_range)
330 itm->speed_range = speed_range->u.range;
331 else
332 itm->speed_range = defrange;
333 if (angle_range)
334 itm->angle_range = angle_range->u.range;
335 else
336 itm->angle_range = defrange;
337 if (sequence_range)
338 itm->sequence_range = sequence_range->u.range;
339 else
340 itm->sequence_range = defrange;
341 if (item_types)
342 {
343 type = item_types->u.item_types;
344 while (type && *type != type_none)
345 {
346 itm->type = g_list_append(itm->type, GINT_TO_POINTER(*type));
347 type++;
348 }
349 }
350
351 //dbg(0,"return\n");
352
353 return itm;
354 }
355 int itemgra_add_attr(struct itemgra *itemgra, struct attr *attr)
356 {
357 switch (attr->type)
358 {
359 case attr_polygon:
360 case attr_polyline:
361 case attr_circle:
362 case attr_text:
363 case attr_icon:
364 case attr_image:
365 case attr_arrows:
366 itemgra->elements = g_list_append(itemgra->elements, attr->u.element);
367 return 1;
368 default:
369 dbg(0, "unknown: %s\n", attr_to_name(attr->type));
370 return 0;
371 }
372 }
373
374 static void element_set_color(struct element *e, struct attr **attrs)
375 {
376 struct attr *color;
377 color = attr_search(attrs, NULL, attr_color);
378 if (color)
379 e->color = *color->u.color;
380 }
381
382 static void element_set_background_color(struct color *c, struct attr **attrs)
383 {
384 struct attr *color;
385 color = attr_search(attrs, NULL, attr_background_color);
386 if (color)
387 *c = *color->u.color;
388 }
389
390 static void element_set_text_size(struct element *e, struct attr **attrs)
391 {
392 struct attr *text_size;
393 text_size = attr_search(attrs, NULL, attr_text_size);
394 if (text_size)
395 e->text_size = text_size->u.num;
396 }
397
398 static void element_set_polyline_width(struct element *e, struct attr **attrs)
399 {
400 struct attr *width;
401 width = attr_search(attrs, NULL, attr_width);
402 if (width)
403 e->u.polyline.width = width->u.num;
404 }
405
406 static void element_set_polyline_directed(struct element *e, struct attr **attrs)
407 {
408 struct attr *directed;
409 directed = attr_search(attrs, NULL, attr_directed);
410 if (directed)
411 e->u.polyline.directed = directed->u.num;
412 }
413
414 static void element_set_polyline_dash(struct element *e, struct attr **attrs)
415 {
416 struct attr *dash;
417 int i;
418
419 dash = attr_search(attrs, NULL, attr_dash);
420 if (dash)
421 {
422 /*
423 for (i = 0; i < 4; i++)
424 {
425 if (!dash->u.dash[i])
426 {
427 break;
428 }
429 dbg(0,"1 i=%d d=%d\n", i, dash->u.dash[i]);
430 //e->u.polyline.dash_table[i] = atoi(dash->u.dash[i]);
431 e->u.polyline.dash_table[i] = dash->u.dash[i];
432 }
433 */
434 i=0;
435 if (!dash->u.dash[i])
436 {
437 }
438 else
439 {
440 e->u.polyline.dash_table[i] = dash->u.dash[i];
441 //dbg(0,"1 i=%d d=%d\n", i, dash->u.dash[i]);
442 e->u.polyline.dash_num = (i + 1);
443 }
444 }
445 }
446
447 static void element_set_polyline_offset(struct element *e, struct attr **attrs)
448 {
449 struct attr *offset;
450 offset = attr_search(attrs, NULL, attr_offset);
451 if (offset)
452 {
453 e->u.polyline.offset = offset->u.num;
454 }
455 }
456
457 static void element_set_circle_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.circle.width = width->u.num;
463 }
464
465 static void element_set_circle_radius(struct element *e, struct attr **attrs)
466 {
467 struct attr *radius;
468 radius = attr_search(attrs, NULL, attr_radius);
469 if (radius)
470 e->u.circle.radius = radius->u.num;
471 }
472
473 struct polygon *
474 polygon_new(struct attr *parent, struct attr **attrs)
475 {
476 struct element *e;
477 e = g_new0(struct element, 1);
478 e->type = element_polygon;
479 element_set_color(e, attrs);
480
481 return (struct polygon *) e;
482 }
483
484 struct polyline *
485 polyline_new(struct attr *parent, struct attr **attrs)
486 {
487 struct element *e;
488
489 e = g_new0(struct element, 1);
490 e->type = element_polyline;
491 element_set_color(e, attrs);
492 element_set_polyline_width(e, attrs);
493 element_set_polyline_directed(e, attrs);
494 element_set_polyline_dash(e, attrs);
495 element_set_polyline_offset(e, attrs);
496 return (struct polyline *) e;
497 }
498
499 struct circle *
500 circle_new(struct attr *parent, struct attr **attrs)
501 {
502 struct element *e;
503 struct color color_black =
504 { COLOR_BLACK_ };
505 struct color color_white =
506 { COLOR_WHITE_ };
507
508 e = g_new0(struct element, 1);
509 e->type = element_circle;
510 e->color = color_black;
511 e->u.circle.background_color = color_white;
512 element_set_color(e, attrs);
513 element_set_background_color(&e->u.circle.background_color, attrs);
514 element_set_text_size(e, attrs);
515 element_set_circle_width(e, attrs);
516 element_set_circle_radius(e, attrs);
517
518 return (struct circle *) e;
519 }
520
521 struct text *
522 text_new(struct attr *parent, struct attr **attrs)
523 {
524 struct element *e;
525 struct color color_black =
526 { COLOR_BLACK_ };
527 struct color color_white =
528 { COLOR_WHITE_ };
529
530 e = g_new0(struct element, 1);
531 e->type = element_text;
532 element_set_text_size(e, attrs);
533 e->color = color_black;
534 e->u.text.background_color = color_white;
535 element_set_color(e, attrs);
536 element_set_background_color(&e->u.text.background_color, attrs);
537
538 return (struct text *) e;
539 }
540
541 struct icon *
542 icon_new(struct attr *parent, struct attr **attrs)
543 {
544 struct element *e;
545 struct attr *src, *w, *h, *rotation;
546 src = attr_search(attrs, NULL, attr_src);
547 if (!src)
548 return NULL;
549
550 e = g_malloc0(sizeof(*e) + strlen(src->u.str) + 1);
551 e->type = element_icon;
552 e->u.icon.src = (char *) (e + 1);
553 if ((w = attr_search(attrs, NULL, attr_w)))
554 e->u.icon.width = w->u.num;
555 else
556 e->u.icon.width = -1;
557 if ((h = attr_search(attrs, NULL, attr_h)))
558 e->u.icon.height = h->u.num;
559 else
560 e->u.icon.height = -1;
561 if ((rotation = attr_search(attrs, NULL, attr_rotation)))
562 e->u.icon.rotation = rotation->u.num;
563 strcpy(e->u.icon.src, src->u.str);
564
565 return (struct icon *) e;
566 }
567
568 struct image *
569 image_new(struct attr *parent, struct attr **attrs)
570 {
571 struct element *e;
572
573 e = g_malloc0(sizeof(*e));
574 e->type = element_image;
575
576 return (struct image *) e;
577 }
578
579 struct arrows *
580 arrows_new(struct attr *parent, struct attr **attrs)
581 {
582 struct element *e;
583 e = g_malloc0(sizeof(*e));
584 e->type = element_arrows;
585 element_set_color(e, attrs);
586 return (struct arrows *) e;
587 }
588
589 int element_add_attr(struct element *e, struct attr *attr)
590 {
591 switch (attr->type)
592 {
593 case attr_coord:
594 e->coord = g_realloc(e->coord, (e->coord_count + 1) * sizeof(struct coord));
595 e->coord[e->coord_count++] = *attr->u.coord;
596 return 1;
597 default:
598 return 0;
599 }
600 }

   
Visit the ZANavi Wiki