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

Contents of /navit/navit/layout.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 27 - (show annotations) (download)
Mon Apr 9 21:27:36 2012 UTC (10 years, 7 months ago) by zoff99
File MIME type: text/plain
File size: 13263 byte(s)
lots of new stuff, tranlsations, bug fixes ...
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 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
314 itm = g_new0(struct itemgra, 1);
315 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 type++;
345 }
346 }
347 return itm;
348 }
349 int itemgra_add_attr(struct itemgra *itemgra, struct attr *attr)
350 {
351 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 }
366 }
367
368 static void element_set_color(struct element *e, struct attr **attrs)
369 {
370 struct attr *color;
371 color = attr_search(attrs, NULL, attr_color);
372 if (color)
373 e->color = *color->u.color;
374 }
375
376 static void element_set_background_color(struct color *c, struct attr **attrs)
377 {
378 struct attr *color;
379 color = attr_search(attrs, NULL, attr_background_color);
380 if (color)
381 *c = *color->u.color;
382 }
383
384 static void element_set_text_size(struct element *e, struct attr **attrs)
385 {
386 struct attr *text_size;
387 text_size = attr_search(attrs, NULL, attr_text_size);
388 if (text_size)
389 e->text_size = text_size->u.num;
390 }
391
392 static void element_set_polyline_width(struct element *e, struct attr **attrs)
393 {
394 struct attr *width;
395 width = attr_search(attrs, NULL, attr_width);
396 if (width)
397 e->u.polyline.width = width->u.num;
398 }
399
400 static void element_set_polyline_directed(struct element *e, struct attr **attrs)
401 {
402 struct attr *directed;
403 directed = attr_search(attrs, NULL, attr_directed);
404 if (directed)
405 e->u.polyline.directed = directed->u.num;
406 }
407
408 static void element_set_polyline_dash(struct element *e, struct attr **attrs)
409 {
410 struct attr *dash;
411 int i;
412
413 dash = attr_search(attrs, NULL, attr_dash);
414 if (dash)
415 {
416 /*
417 for (i = 0; i < 4; i++)
418 {
419 if (!dash->u.dash[i])
420 {
421 break;
422 }
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 e->u.polyline.dash_table[i] = dash->u.dash[i];
426 }
427 */
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 }
439 }
440
441 static void element_set_polyline_offset(struct element *e, struct attr **attrs)
442 {
443 struct attr *offset;
444 offset = attr_search(attrs, NULL, attr_offset);
445 if (offset)
446 {
447 e->u.polyline.offset = offset->u.num;
448 }
449 }
450
451 static void element_set_circle_width(struct element *e, struct attr **attrs)
452 {
453 struct attr *width;
454 width = attr_search(attrs, NULL, attr_width);
455 if (width)
456 e->u.circle.width = width->u.num;
457 }
458
459 static void element_set_circle_radius(struct element *e, struct attr **attrs)
460 {
461 struct attr *radius;
462 radius = attr_search(attrs, NULL, attr_radius);
463 if (radius)
464 e->u.circle.radius = radius->u.num;
465 }
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 e->type = element_polygon;
473 element_set_color(e, attrs);
474
475 return (struct polygon *) e;
476 }
477
478 struct polyline *
479 polyline_new(struct attr *parent, struct attr **attrs)
480 {
481 struct element *e;
482
483 e = g_new0(struct element, 1);
484 e->type = element_polyline;
485 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 return (struct polyline *) e;
491 }
492
493 struct circle *
494 circle_new(struct attr *parent, struct attr **attrs)
495 {
496 struct element *e;
497 struct color color_black =
498 { COLOR_BLACK_ };
499 struct color color_white =
500 { COLOR_WHITE_ };
501
502 e = g_new0(struct element, 1);
503 e->type = element_circle;
504 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 return (struct circle *) e;
513 }
514
515 struct text *
516 text_new(struct attr *parent, struct attr **attrs)
517 {
518 struct element *e;
519 struct color color_black =
520 { COLOR_BLACK_ };
521 struct color color_white =
522 { COLOR_WHITE_ };
523
524 e = g_new0(struct element, 1);
525 e->type = element_text;
526 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 return (struct text *) e;
533 }
534
535 struct icon *
536 icon_new(struct attr *parent, struct attr **attrs)
537 {
538 struct element *e;
539 struct attr *src, *w, *h, *rotation;
540 src = attr_search(attrs, NULL, attr_src);
541 if (!src)
542 return NULL;
543
544 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 else
550 e->u.icon.width = -1;
551 if ((h = attr_search(attrs, NULL, attr_h)))
552 e->u.icon.height = h->u.num;
553 else
554 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
559 return (struct icon *) e;
560 }
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 e->type = element_image;
569
570 return (struct image *) e;
571 }
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 e->type = element_arrows;
579 element_set_color(e, attrs);
580 return (struct arrows *) e;
581 }
582
583 int element_add_attr(struct element *e, struct attr *attr)
584 {
585 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 }
594 }

   
Visit the ZANavi Wiki