From: martin-s Date: Thu, 10 Apr 2008 22:45:40 +0000 (+0000) Subject: Fix:Core:Cleaned up attribute handling a bit X-Git-Tag: navit-0.5.0.5194svn~4152 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c270260440e6bae4b6bed2efd258b7f80070d9ac;p=profile%2Fivi%2Fnavit.git Fix:Core:Cleaned up attribute handling a bit git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk@1000 ffa7fe5e-494d-0410-b361-a75ebd5db220 --- diff --git a/navit/src/attr.c b/navit/src/attr.c index 3d65c85..be0c8f2 100644 --- a/navit/src/attr.c +++ b/navit/src/attr.c @@ -148,6 +148,19 @@ attr_search(struct attr **attrs, struct attr *last, enum attr_type attr) } int +attr_generic_get_attr(struct attr **attrs, enum attr_type type, struct attr *attr, struct attr_iter *iter) +{ + while (*attrs) { + if ((*attrs)->type == type) { + *attr=**attrs; + return 1; + } + attrs++; + } + return 0; +} + +int attr_data_size(struct attr *attr) { if (attr->type >= attr_type_string_begin && attr->type <= attr_type_string_end) { @@ -191,3 +204,45 @@ attr_free(struct attr *attr) g_free(attr->u.color); g_free(attr); } + +struct attr * +attr_dup(struct attr *attr) +{ + int size; + struct attr *ret=g_new0(struct attr, 1); + ret->type=attr->type; + if (attr->type >= attr_type_int_begin && attr->type <= attr_type_int_end) { + ret->u.num=attr->u.num; + } else { + size=attr_data_size(attr); + if (size) { + ret->u.data=g_malloc(size); + memcpy(ret->u.data, attr->u.data, size); + } + } + return ret; +} + +void +attr_list_free(struct attr **attrs) +{ + int count=0; + while (attrs[count]) { + attr_free(attrs[count++]); + } + g_free(attrs); +} + +struct attr ** +attr_list_dup(struct attr **attrs) +{ + struct attr **ret=attrs; + int i,count=0; + + while (attrs[count]) + count++; + ret=g_new0(struct attr *, count+1); + for (i = 0 ; i < count ; i++) + ret[i]=attr_dup(attrs[i]); + return ret; +} diff --git a/navit/src/attr.h b/navit/src/attr.h index 58147b2..dda3116 100644 --- a/navit/src/attr.h +++ b/navit/src/attr.h @@ -30,6 +30,7 @@ struct attr { enum attr_type type; union { char *str; + void *data; int num; struct item *item; enum item_type item_type; @@ -47,22 +48,29 @@ struct attr { struct navigation *navigation; struct coord *coord; struct pcoord *pcoord; + struct gui *gui; + struct graphics *graphics; } u; }; /* prototypes */ enum attr_type; struct attr; +struct attr_iter; struct map; enum attr_type attr_from_name(const char *name); char *attr_to_name(enum attr_type attr); struct attr *attr_new_from_text(const char *name, const char *value); char *attr_to_text(struct attr *attr, struct map *map, int pretty); struct attr *attr_search(struct attr **attrs, struct attr *last, enum attr_type attr); +int attr_generic_get_attr(struct attr **attrs, enum attr_type type, struct attr *attr, struct attr_iter *iter); int attr_data_size(struct attr *attr); void *attr_data_get(struct attr *attr); void attr_data_set(struct attr *attr, void *data); void attr_free(struct attr *attr); +struct attr *attr_dup(struct attr *attr); +void attr_list_free(struct attr **attrs); +struct attr **attr_list_dup(struct attr **attrs); /* end of prototypes */ #endif #ifdef __cplusplus diff --git a/navit/src/attr_def.h b/navit/src/attr_def.h index 72e8a08..8480aae 100644 --- a/navit/src/attr_def.h +++ b/navit/src/attr_def.h @@ -119,6 +119,8 @@ ATTR(vehicle) ATTR(map) ATTR(bookmark_map) ATTR(former_destination_map) +ATTR(graphics) +ATTR(gui) ATTR2(0x0008ffff,type_object_end) ATTR2(0x00090000,type_coord_begin) ATTR2(0x0009ffff,type_coord_end) diff --git a/navit/src/graphics.c b/navit/src/graphics.c index 6d76d1b..4307a55 100644 --- a/navit/src/graphics.c +++ b/navit/src/graphics.c @@ -24,6 +24,7 @@ struct graphics struct graphics_methods meth; struct graphics_font *font[16]; struct graphics_gc *gc[3]; + struct attr **attrs; int ready; }; @@ -42,9 +43,17 @@ graphics_new(const char *type, struct attr **attrs) return NULL; this_=g_new0(struct graphics, 1); this_->priv=(*new)(&this_->meth, attrs); + this_->attrs=attr_list_dup(attrs); return this_; } +int +graphics_get_attr(struct graphics *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter) +{ + return attr_generic_get_attr(this_->attrs, type, attr, iter); +} + + struct graphics * graphics_overlay_new(struct graphics *parent, struct point *p, int w, int h) { diff --git a/navit/src/graphics.h b/navit/src/graphics.h index 067f588..9105c1e 100644 --- a/navit/src/graphics.h +++ b/navit/src/graphics.h @@ -85,8 +85,10 @@ struct graphics_image { }; /* prototypes */ +enum attr_type; enum draw_mode_num; struct attr; +struct attr_iter; struct color; struct displayitem; struct displaylist; @@ -100,6 +102,7 @@ struct layout; struct point; struct transformation; struct graphics *graphics_new(const char *type, struct attr **attrs); +int graphics_get_attr(struct graphics *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter); struct graphics *graphics_overlay_new(struct graphics *parent, struct point *p, int w, int h); void graphics_init(struct graphics *this_); void *graphics_get_data(struct graphics *this_, char *type); diff --git a/navit/src/graphics/qt_qpainter/graphics_qt_qpainter.cpp b/navit/src/graphics/qt_qpainter/graphics_qt_qpainter.cpp index 446fdd4..8acb37c 100644 --- a/navit/src/graphics/qt_qpainter/graphics_qt_qpainter.cpp +++ b/navit/src/graphics/qt_qpainter/graphics_qt_qpainter.cpp @@ -1,6 +1,7 @@ #include #include "config.h" #include "point.h" +#include "item.h" #include "graphics.h" #include "color.h" #include "debug.h" diff --git a/navit/src/gui.c b/navit/src/gui.c index f63d330..45fa5ab 100644 --- a/navit/src/gui.c +++ b/navit/src/gui.c @@ -4,8 +4,15 @@ #include "gui.h" #include "menu.h" #include "data_window.h" +#include "item.h" #include "plugin.h" +struct gui { + struct gui_methods meth; + struct gui_priv *priv; + struct attr **attrs; +}; + struct gui * gui_new(struct navit *nav, const char *type, struct attr **attrs) { @@ -18,9 +25,16 @@ gui_new(struct navit *nav, const char *type, struct attr **attrs) this_=g_new0(struct gui, 1); this_->priv=guitype_new(nav, &this_->meth, attrs); + this_->attrs=attr_list_dup(attrs); return this_; } +int +gui_get_attr(struct gui *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter) +{ + return attr_generic_get_attr(this_->attrs, type, attr, iter); +} + struct menu * gui_menubar_new(struct gui *gui) { diff --git a/navit/src/gui.h b/navit/src/gui.h index c204b05..5dc1675 100644 --- a/navit/src/gui.h +++ b/navit/src/gui.h @@ -20,21 +20,19 @@ struct gui_methods { }; -struct gui { - struct gui_methods meth; - struct gui_priv *priv; -}; - /* prototypes */ +enum attr_type; struct attr; +struct attr_iter; struct callback; -struct coord; struct datawindow; struct graphics; struct gui; struct menu; struct navit; +struct pcoord; struct gui *gui_new(struct navit *nav, const char *type, struct attr **attrs); +int gui_get_attr(struct gui *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter); struct menu *gui_menubar_new(struct gui *gui); struct menu *gui_popup_new(struct gui *gui); struct datawindow *gui_datawindow_new(struct gui *gui, char *name, struct callback *click, struct callback *close); diff --git a/navit/src/log.c b/navit/src/log.c index 003b8fc..03d7f59 100644 --- a/navit/src/log.c +++ b/navit/src/log.c @@ -31,6 +31,7 @@ struct log { struct log_data header; struct log_data data; struct log_data trailer; + struct attr **attrs; }; static void @@ -152,6 +153,13 @@ log_timer(gpointer data) return TRUE; } +int +log_get_attr(struct log *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter) +{ + return attr_generic_get_attr(this_->attrs, type, attr, iter); +} + + struct log * log_new(struct attr **attrs) { @@ -179,6 +187,7 @@ log_new(struct attr **attrs) expand_filenames(ret); log_open(ret); gettimeofday(&ret->last_flush, NULL); + ret->attrs=attr_list_dup(attrs); return ret; } diff --git a/navit/src/log.h b/navit/src/log.h index dd11677..e9dabf3 100644 --- a/navit/src/log.h +++ b/navit/src/log.h @@ -1,8 +1,11 @@ #ifndef NAVIT_LOG_H #define NAVIT_LOG_H /* prototypes */ +enum attr_type; struct attr; +struct attr_iter; struct log; +int log_get_attr(struct log *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter); struct log *log_new(struct attr **attrs); void log_set_header(struct log *this_, char *data, int len); void log_set_trailer(struct log *this_, char *data, int len); diff --git a/navit/src/navit.c b/navit/src/navit.c index 1c415fd..89fa077 100644 --- a/navit/src/navit.c +++ b/navit/src/navit.c @@ -62,10 +62,8 @@ struct navit { GList *mapsets; GList *layouts; struct gui *gui; - char *gui_type; struct layout *layout_current; struct graphics *gra; - char *gra_type; struct action *action; struct transformation *trans; struct compass *compass; @@ -416,29 +414,33 @@ navit_new(struct attr **attrs) return this_; } -void -navit_set_gui(struct navit *this_, struct gui *gui, char *type) +static int +navit_set_gui(struct navit *this_, struct gui *gui) { + if (this_->gui) + return 0; this_->gui=gui; - this_->gui_type=g_strdup(type); if (gui_has_main_loop(this_->gui)) { if (! main_loop_gui) { main_loop_gui=this_->gui; } else { g_warning("gui with main loop already active, ignoring this instance"); - return; + return 0; } } + return 1; } -void -navit_set_graphics(struct navit *this_, struct graphics *gra, char *type) +static int +navit_set_graphics(struct navit *this_, struct graphics *gra) { + if (this_->gra) + return 0; this_->gra=gra; - this_->gra_type=g_strdup(type); graphics_register_resize_callback(this_->gra, navit_resize, this_); graphics_register_button_callback(this_->gra, navit_button, this_); graphics_register_motion_callback(this_->gra, navit_motion, this_); + return 1; } struct graphics * @@ -1014,17 +1016,20 @@ navit_init(struct navit *this_) struct navit_vehicle *nv; if (!this_->gui) { - g_warning("failed to instantiate gui '%s'\n",this_->gui_type); + g_warning("no gui\n"); navit_destroy(this_); return; } if (!this_->gra) { - g_warning("failed to instantiate graphics '%s'\n",this_->gra_type); + g_warning("no graphics\n"); navit_destroy(this_); return; } if (gui_set_graphics(this_->gui, this_->gra)) { - g_warning("failed to connect graphics '%s' to gui '%s'\n", this_->gra_type, this_->gui_type); + struct attr attr_type_gui, attr_type_graphics; + gui_get_attr(this_->gui, attr_type, &attr_type_gui, NULL); + graphics_get_attr(this_->gra, attr_type, &attr_type_graphics, NULL); + g_warning("failed to connect graphics '%s' to gui '%s'\n", attr_type_graphics.u.str, attr_type_gui.u.str); g_warning(" Please see http://navit.sourceforge.net/wiki/index.php/Failed_to_connect_graphics_to_gui\n"); g_warning(" for explanations and solutions\n"); @@ -1295,19 +1300,18 @@ navit_get_attr(struct navit *this_, enum attr_type type, struct attr *attr, stru } static int -navit_add_log(struct navit *this_, struct log *log, struct attr **attrs) +navit_add_log(struct navit *this_, struct log *log) { - struct attr *type; - type = attr_search(attrs, NULL, attr_type); - if (!type) - return 1; - if (!strcmp(type->u.str, "textfile_debug")) { + struct attr type_attr; + if (!log_get_attr(log, attr_type, &type_attr, NULL)) + return 0; + if (!strcmp(type_attr.u.str, "textfile_debug")) { if (this_->textfile_debug_log) - return 1; + return 0; this_->textfile_debug_log=log; - return 0; + return 1; } - return 1; + return 0; } int @@ -1315,7 +1319,11 @@ navit_add_attr(struct navit *this_, struct attr *attr, struct attr **attrs) { switch (attr->type) { case attr_log: - return navit_add_log(this_, attr->u.log, attrs); + return navit_add_log(this_, attr->u.log); + case attr_gui: + return navit_set_gui(this_, attr->u.gui); + case attr_graphics: + return navit_set_graphics(this_, attr->u.graphics); default: return 0; } diff --git a/navit/src/navit.h b/navit/src/navit.h index ac06e8d..cc390bb 100644 --- a/navit/src/navit.h +++ b/navit/src/navit.h @@ -6,19 +6,16 @@ extern "C" { #endif extern struct gui *main_loop_gui; /* prototypes */ -enum item_type; enum attr_type; +enum item_type; struct attr; struct attr_iter; -struct attr_iter; struct callback; -struct coord; struct displaylist; struct graphics; struct gui; struct layout; struct mapset; -struct menu; struct navigation; struct navit; struct navit_vehicle; @@ -36,11 +33,12 @@ struct tracking *navit_get_tracking(struct navit *this_); void navit_add_layout(struct navit *this_, struct layout *lay); void navit_draw(struct navit *this_); void navit_draw_displaylist(struct navit *this_); +void navit_resize(void *data, int w, int h); +int navit_handle_button(struct navit *this_, int pressed, int button, struct point *p, struct callback *popup_callback); +void navit_handle_motion(struct navit *this_, struct point *p); void navit_zoom_in(struct navit *this_, int factor, struct point *p); void navit_zoom_out(struct navit *this_, int factor, struct point *p); struct navit *navit_new(struct attr **attrs); -void navit_set_gui(struct navit *this_, struct gui *gui, char *type); -void navit_set_graphics(struct navit *this_, struct graphics *gra, char *type); struct graphics *navit_get_graphics(struct navit *this_); void navit_set_destination(struct navit *this_, struct pcoord *c, char *description); void navit_add_bookmark(struct navit *this_, struct pcoord *c, const char *description); @@ -65,7 +63,6 @@ struct navit_vehicle *navit_add_vehicle(struct navit *this_, struct vehicle *v, void navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv); void navit_tracking_add(struct navit *this_, struct tracking *tracking); void navit_route_add(struct navit *this_, struct route *route); -struct map *navit_get_route_map(struct navit *this_); void navit_navigation_add(struct navit *this_, struct navigation *navigation); void navit_set_speech(struct navit *this_, struct speech *speech); struct gui *navit_get_gui(struct navit *this_); @@ -74,7 +71,6 @@ struct route *navit_get_route(struct navit *this_); struct navigation *navit_get_navigation(struct navit *this_); struct displaylist *navit_get_displaylist(struct navit *this_); void navit_destroy(struct navit *this_); -void navit_toggle_routegraph_display(struct navit *nav); /* end of prototypes */ #ifdef __cplusplus } diff --git a/navit/src/xmlconfig.c b/navit/src/xmlconfig.c index 49df767..f342205 100644 --- a/navit/src/xmlconfig.c +++ b/navit/src/xmlconfig.c @@ -236,6 +236,7 @@ static int xmlconfig_graphics(struct xmlstate *state) { struct attr **attrs; + struct attr graphics_attr; const char *type=find_attribute(state, "type", 1); if (! type) return 0; @@ -245,14 +246,16 @@ xmlconfig_graphics(struct xmlstate *state) dbg(0,"Failed to create graphics '%s'\n", type); return 0; } - navit_set_graphics(state->parent->element_object, state->element_object, type); - return 1; + graphics_attr.type=attr_graphics; + graphics_attr.u.graphics=state->element_object; + return navit_add_attr(state->parent->element_object, &graphics_attr, NULL); } static int xmlconfig_gui(struct xmlstate *state) { struct attr **attrs; + struct attr gui_attr; const char *type=find_attribute(state, "type", 1); if (! type) return 0; @@ -262,8 +265,9 @@ xmlconfig_gui(struct xmlstate *state) dbg(0,"Failed to create gui '%s'\n", type); return 0; } - navit_set_gui(state->parent->element_object, state->element_object, type); - return 1; + gui_attr.type=attr_gui; + gui_attr.u.gui=state->element_object; + return navit_add_attr(state->parent->element_object, &gui_attr, NULL); } static int