Fix:Core:Cleaned up map api a bit
authormartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Sun, 21 Sep 2008 18:38:29 +0000 (18:38 +0000)
committermartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Sun, 21 Sep 2008 18:38:29 +0000 (18:38 +0000)
git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk@1391 ffa7fe5e-494d-0410-b361-a75ebd5db220

13 files changed:
navit/navit/attr.c
navit/navit/attr.h
navit/navit/graphics.c
navit/navit/gui.c
navit/navit/gui/gtk/gui_gtk_window.c
navit/navit/gui/internal/gui_internal.c
navit/navit/log.c
navit/navit/map.c
navit/navit/mapset.c
navit/navit/navigation.c
navit/navit/navit.c
navit/navit/route.c
navit/navit/xmlconfig.c

index 7d6448e..098f78e 100644 (file)
@@ -141,9 +141,14 @@ attr_to_text(struct attr *attr, struct map *map, int pretty)
 
        if (type >= attr_type_item_begin && type <= attr_type_item_end) {
                struct item *item=attr->u.item;
+               struct attr type, data;
                if (! item)
                        return g_strdup("(nil)");
-               return g_strdup_printf("type=0x%x id=0x%x,0x%x map=%p (%s:%s)", item->type, item->id_hi, item->id_lo, item->map, item->map ? map_get_type(item->map) : "", item->map ? map_get_filename(item->map) : "");
+               if (! item->map || !map_get_attr(item->map, attr_type, &type, NULL))
+                       type.u.str="";
+               if (! item->map || !map_get_attr(item->map, attr_data, &data, NULL))
+                       data.u.str="";
+               return g_strdup_printf("type=0x%x id=0x%x,0x%x map=%p (%s:%s)", item->type, item->id_hi, item->id_lo, item->map, type.u.str, data.u.str);
        }
        if (type >= attr_type_string_begin && type <= attr_type_string_end) {
                if (map) {
@@ -173,18 +178,48 @@ 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)
+attr_generic_get_attr(struct attr **attrs, struct attr **def_attrs, enum attr_type type, struct attr *attr, struct attr_iter *iter)
 {
-       while (*attrs) {
+       while (attrs && *attrs) {
                if ((*attrs)->type == type) {
                        *attr=**attrs;
                        return 1;
                }
                attrs++;
        }
+       while (def_attrs && *def_attrs) {
+               if ((*def_attrs)->type == type) {
+                       *attr=**def_attrs;
+                       return 1;
+               }
+               def_attrs++;
+       }
        return 0;
 }
 
+struct attr **
+attr_generic_set_attr(struct attr **attrs, struct attr *attr)
+{
+       struct attr **curr=attrs;
+       int i,count=0;
+       while (curr && *curr) {
+               if ((*curr)->type == attr->type) {
+                       attr_free(*curr);
+                       *curr=attr_dup(attr);
+                       return attrs;
+               }
+               curr++;
+               count++;
+       }
+       curr=g_new0(struct attr *, count+2);
+       for (i = 0 ; i < count ; i++)
+               curr[i]=attrs[i];
+       curr[count]=attr_dup(attr);
+       curr[count+1]=NULL;
+       g_free(attrs);
+       return curr;
+}
+
 int
 attr_data_size(struct attr *attr)
 {
index 5ed2e25..25bd04e 100644 (file)
@@ -82,7 +82,7 @@ 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_generic_get_attr(struct attr **attrs, struct attr **def_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);
index d5f0e28..33c4aef 100644 (file)
@@ -104,7 +104,7 @@ struct graphics * graphics_new(struct attr *parent, struct attr **attrs)
 */
 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);
+       return attr_generic_get_attr(this_->attrs, NULL, type, attr, iter);
 }
 
 /**
index 072e026..662fa38 100644 (file)
@@ -55,7 +55,7 @@ gui_new(struct attr *parent, struct attr **attrs)
 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);
+       return attr_generic_get_attr(this_->attrs, NULL, type, attr, iter);
 }
 
 struct menu *
index 869ccbd..1933054 100644 (file)
@@ -266,7 +266,10 @@ gui_gtk_add_menu(struct gui_priv *this, char *name, char *label, char *path, int
 static void
 gui_gtk_action_toggled(GtkToggleAction *action, struct action_cb_data *data)
 {
-       map_set_active(data->attr.u.map, gtk_toggle_action_get_active(action));
+       struct attr active;
+       active.type=attr_active;
+       active.u.num=gtk_toggle_action_get_active(action);
+       map_set_attr(data->attr.u.map, &active);
        navit_draw(data->gui->nav);
 }
 
@@ -374,20 +377,26 @@ static void
 gui_gtk_maps_init(struct gui_priv *this)
 {
        struct attr_iter *iter;
-       struct attr attr;
-       struct action_cb_data *data;
+       struct attr attr,active,type,data;
+       struct action_cb_data *cb_data;
        int count=0;
        char *name, *label;
 
        iter=navit_attr_iter_new();
        while(navit_get_attr(this->nav, attr_map, &attr, iter)) {
                name=g_strdup_printf("Map %d", count++);
-               label=g_strdup_printf("%s:%s", map_get_type(attr.u.map), map_get_filename(attr.u.map));
-               data=g_new(struct action_cb_data, 1);
-               data->gui=this;
-               data->attr.type=attr_map;
-               data->attr.u.map=attr.u.map;
-               gui_gtk_add_toggle_menu(this, name, label, "/ui/MenuBar/Map/MapMenuAdditions", data, map_get_active(attr.u.map));
+               if (! map_get_attr(attr.u.map, attr_type, &type, NULL))
+                       type.u.str="";
+               if (! map_get_attr(attr.u.map, attr_data, &data, NULL))
+                       data.u.str="";
+               label=g_strdup_printf("%s:%s", type.u.str, data.u.str);
+               cb_data=g_new(struct action_cb_data, 1);
+               cb_data->gui=this;
+               cb_data->attr.type=attr_map;
+               cb_data->attr.u.map=attr.u.map;
+               if (! map_get_attr(attr.u.map, attr_active, &active, NULL))
+                       active.u.num=1;
+               gui_gtk_add_toggle_menu(this, name, label, "/ui/MenuBar/Map/MapMenuAdditions", cb_data, active.u.num);
                g_free(name);
                g_free(label);
        }
index 78445be..e01e6c3 100644 (file)
@@ -132,6 +132,7 @@ struct widget {
        struct callback *cb;
        struct attr on;
        struct attr off;
+       int deflt;
        int is_on;
        int redraw;
        GList *children;
@@ -413,6 +414,8 @@ gui_internal_button_attr_update(struct gui_priv *this, struct widget *w)
 
        if (w->get_attr(w->instance, w->on.type, &curr, NULL))
                is_on=curr.u.data == w->on.u.data;
+       else
+               is_on=w->deflt;
        if (is_on != w->is_on) {
                if (w->redraw)
                        this->redraw=1;
@@ -471,7 +474,7 @@ gui_internal_button_navit_attr_new(struct gui_priv *this, char *text, enum flags
 }
 
 static struct widget *
-gui_internal_button_map_attr_new(struct gui_priv *this, char *text, enum flags flags, struct map *map, struct attr *on, struct attr *off)
+gui_internal_button_map_attr_new(struct gui_priv *this, char *text, enum flags flags, struct map *map, struct attr *on, struct attr *off, int deflt)
 {
        struct graphics_image *image=image_new_xs(this, "gui_inactive");
        struct widget *ret;
@@ -480,6 +483,7 @@ gui_internal_button_map_attr_new(struct gui_priv *this, char *text, enum flags f
                ret->on=*on;
        if (off)
                ret->off=*off;
+       ret->deflt=deflt;
        ret->get_attr=map_get_attr;
        ret->set_attr=map_set_attr;
        ret->remove_cb=map_remove_callback;
@@ -2125,11 +2129,10 @@ gui_internal_cmd_actions(struct gui_priv *this, struct widget *wm)
 static void
 gui_internal_cmd_maps(struct gui_priv *this, struct widget *wm)
 {
-       struct attr attr;
+       struct attr attr, on, off, description, type, data;
        struct widget *w,*wb,*wma;
        char *label;
        struct attr_iter *iter;
-       struct attr on, off;
 
 
        wb=gui_internal_menu(this, "Maps");
@@ -2141,9 +2144,17 @@ gui_internal_cmd_maps(struct gui_priv *this, struct widget *wm)
        on.u.num=1;
        off.u.num=0;
        while(navit_get_attr(this->nav, attr_map, &attr, iter)) {
-               label=g_strdup_printf("%s:%s", map_get_type(attr.u.map), map_get_filename(attr.u.map));
+               if (map_get_attr(attr.u.map, attr_description, &description, NULL)) {
+                       label=g_strdup(description.u.str);
+               } else {
+                       if (!map_get_attr(attr.u.map, attr_type, &type, NULL))
+                               type.u.str="";
+                       if (!map_get_attr(attr.u.map, attr_data, &data, NULL))
+                               data.u.str="";
+                       label=g_strdup_printf("%s:%s", type.u.str, data.u.str);
+               }
                wma=gui_internal_button_map_attr_new(this, label, gravity_left_center|orientation_horizontal|flags_fill,
-                       attr.u.map, &on, &off);
+                       attr.u.map, &on, &off, 1);
                gui_internal_widget_append(w, wma);
                g_free(label);
        }
index 97c9a87..f571b79 100644 (file)
@@ -193,7 +193,7 @@ log_timer(gpointer data)
 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);
+        return attr_generic_get_attr(this_->attrs, NULL, type, attr, iter);
 }
 
 
index e49ba09..26790bf 100644 (file)
 struct map {
        struct map_methods meth;
        struct map_priv *priv;
-       char *type;
-       char *filename;
+       struct attr **attrs;
        struct callback_list *attr_cbl;
-       int active;
 };
 
 struct map_rect {
@@ -50,7 +48,6 @@ map_new(struct attr **attrs)
 {
        struct map *m;
        struct map_priv *(*maptype_new)(struct map_methods *meth, struct attr **attrs);
-       struct attr *data=attr_search(attrs, NULL, attr_data);
        struct attr *type=attr_search(attrs, NULL, attr_type);
 
        if (! type) {
@@ -64,10 +61,7 @@ map_new(struct attr **attrs)
        }
 
        m=g_new0(struct map, 1);
-       m->active=1;
-       m->type=g_strdup(type->u.str);
-       if (data) 
-               m->filename=g_strdup(data->u.str);
+       m->attrs=attr_list_dup(attrs);
        m->priv=maptype_new(&m->meth, attrs);
        if (! m->priv) {
                g_free(m);
@@ -78,60 +72,20 @@ map_new(struct attr **attrs)
        return m;
 }
 
-char *
-map_get_filename(struct map *this_)
-{
-       return this_->filename;
-}
-
-char *
-map_get_type(struct map *this_)
-{
-       return this_->type;
-}
-
-int
-map_get_active(struct map *this_)
-{
-       return this_->active;
-}
-
-void
-map_set_active(struct map *this_, int active)
-{
-       this_->active=active;
-}
-
 int
 map_get_attr(struct map *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
 {
-       switch (type) {
-       case attr_active:
-               attr->u.num=this_->active;
-               break;
-       default:
-               return 0;
-       }
-       return 1;
+       return xxx(this_, type, attr, iter);
+#if 0
+       return attr_generic_get_attr(this_->attrs, NULL, type, attr, iter);
+#endif
 }
 
 int
 map_set_attr(struct map *this_, struct attr *attr)
 {
-       int attr_updated=0;
-
-       switch (attr->type) {
-       case attr_active:
-               if (this_->active != !!attr->u.num) {
-                       this_->active=!!attr->u.num;
-                       attr_updated=1;
-               }
-               break;
-       default:
-               return 0;
-       }
-       if (attr_updated)
-               callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
+       this_->attrs=attr_generic_set_attr(this_->attrs, attr);
+       callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
        return 1;
 }
 
@@ -183,6 +137,7 @@ void
 map_destroy(struct map *m)
 {
        m->meth.map_destroy(m->priv);
+       attr_list_free(m->attrs);
        g_free(m);
 }
 
index eed7539..e360681 100644 (file)
@@ -74,13 +74,18 @@ mapset_open(struct mapset *ms)
 struct map * mapset_next(struct mapset_handle *msh, int active)
 {
        struct map *ret;
+       struct attr active_attr;
 
        for (;;) {
                if (!msh->l)
                        return NULL;
                ret=msh->l->data;
                msh->l=g_list_next(msh->l);
-               if (!active || map_get_active(ret))
+               if (!active)
+                       return ret;                     
+               if (!map_get_attr(ret, attr_active, &active_attr, NULL))
+                       return ret;
+               if (active_attr.u.num)
                        return ret;
        }
 }
@@ -117,12 +122,20 @@ struct item *
 mapset_search_get_item(struct mapset_search *this)
 {
        struct item *ret=NULL;
+       struct attr active_attr;
+
        while (!this->ms || !(ret=map_search_get_item(this->ms))) {
                if (this->search_attr->type >= attr_country_all && this->search_attr->type <= attr_country_name)
                        break;
-               do {
+               for (;;) {
                        this->map=g_list_next(this->map);
-               } while (this->map && ! map_get_active(this->map->data));
+                       if (! this->map)
+                               break;
+                       if (!map_get_attr(this->map, attr_active, &active_attr, NULL))
+                               break;
+                       if (active_attr.u.num)
+                               break;
+               }
                if (! this->map)
                        break;
                map_search_destroy(this->ms);
index a419e5e..67a78ff 100644 (file)
@@ -847,14 +847,14 @@ navigation_unregister_callback(struct navigation *this_, enum attr_type type, st
 struct map *
 navigation_get_map(struct navigation *this_)
 {
-       struct attr type_attr={attr_type, {"navigation"}};
-       struct attr navigation_attr={attr_navigation, .u.navigation=this_};
-        struct attr data_attr={attr_data, {""}};
-        struct attr *attrs_navigation[]={&type_attr, &navigation_attr, &data_attr, NULL};
-
        if (! this_->map)
-               this_->map=map_new(attrs_navigation);
-       return this_->map;
+               this_->map=map_new((struct attr*[]){
+                       &(struct attr){attr_type,{"navigation"}},
+                       &(struct attr){attr_navigation,.u.navigation=this_},
+                       &(struct attr){attr_data,{""}},
+                       &(struct attr){attr_description,{"Navigation"}},
+                       NULL});
+        return this_->map;
 }
 
 struct map_priv {
index 60d3339..1dbb883 100644 (file)
@@ -1176,7 +1176,7 @@ navit_init(struct navit *this_)
                                mapset_add(ms, map);
                        if ((map=route_get_graph_map(this_->route))) {
                                mapset_add(ms, map);
-                               map_set_active(map, 0);
+                               map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
                        }
                        route_set_mapset(this_->route, ms);
                }
@@ -1185,7 +1185,7 @@ navit_init(struct navit *this_)
                if (this_->navigation) {
                        if ((map=navigation_get_map(this_->navigation))) {
                                mapset_add(ms, map);
-                               map_set_active(map, 0);
+                               map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
                        }
                }
                navit_add_bookmarks_from_file(this_);
index 0e529d1..8345617 100644 (file)
@@ -1534,13 +1534,14 @@ route_graph_map_new(struct map_methods *meth, struct attr **attrs)
 }
 
 static struct map *
-route_get_map_helper(struct route *this_, struct map **map, char *type)
+route_get_map_helper(struct route *this_, struct map **map, char *type, char *description)
 {
        if (! *map) 
                *map=map_new((struct attr*[]){
                                 &(struct attr){attr_type,{type}},
                                 &(struct attr){attr_route,.u.route=this_},
                                 &(struct attr){attr_data,{""}},
+                                &(struct attr){attr_description,{description}},
                                 NULL});
        return *map;
 }
@@ -1548,14 +1549,14 @@ route_get_map_helper(struct route *this_, struct map **map, char *type)
 struct map *
 route_get_map(struct route *this_)
 {
-       return route_get_map_helper(this_, &this_->map, "route");
+       return route_get_map_helper(this_, &this_->map, "route","Route");
 }
 
 
 struct map *
 route_get_graph_map(struct route *this_)
 {
-       return route_get_map_helper(this_, &this_->graph_map, "route_graph");
+       return route_get_map_helper(this_, &this_->graph_map, "route_graph","Route Graph");
 }
 
 void
index 69541d9..cb5b258 100644 (file)
@@ -455,8 +455,6 @@ xmlconfig_map(struct xmlstate *state)
        state->element_attr.u.data = map_new(attrs);
        if (! state->element_attr.u.data)
                return 0;
-       if (!find_boolean(state, "active", 1, 0))
-               map_set_active(state->element_attr.u.data, 0);
        mapset_add(state->parent->element_attr.u.data, state->element_attr.u.data);
 
        return 1;