Improved items window
authormartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Sun, 22 Jul 2007 18:04:31 +0000 (18:04 +0000)
committermartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Sun, 22 Jul 2007 18:04:31 +0000 (18:04 +0000)
git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk@373 ffa7fe5e-494d-0410-b361-a75ebd5db220

navit/src/callback.c
navit/src/callback.h
navit/src/gui/gtk/datawindow.c
navit/src/gui/gtk/gui_gtk_action.c
navit/src/navit.c
navit/src/xmlconfig.c

index 8985b8d..ea98c50 100644 (file)
@@ -71,8 +71,15 @@ callback_call(struct callback *cb, int pcount, void **p)
 {
        int i;
        void *pf[8];
+       if (! cb)
+               return;
        if (cb->pcount + pcount <= 8) {
-               dbg(1,"cb->pcount=%d %p pcount=%d %p\n", cb->pcount, cb->p[0], pcount, p[0]);
+               dbg(1,"cb->pcount=%d\n", cb->pcount);
+               if (cb->pcount && cb->p) 
+                       dbg(1,"cb->p[0]=%p\n", cb->p[0]);
+               dbg(1,"pcount=%d\n", pcount);
+               if (pcount && p) 
+                       dbg(1,"p[0]=%p\n", p[0]);
                for (i = 0 ; i < cb->pcount ; i++) 
                        pf[i]=cb->p[i];
                for (i = 0 ; i < pcount ; i++)
@@ -107,7 +114,7 @@ callback_call(struct callback *cb, int pcount, void **p)
                        break;
                }
        } else {
-               g_warning("too many parameters for callback\n");
+               g_warning("too many parameters for callback (%d+%d)\n", cb->pcount, pcount);
        }
 }
 
index aad5c4b..c3fe401 100644 (file)
@@ -27,11 +27,26 @@ static inline struct callback *callback_new_1(void (*func)(void), void *p1)
        return callback_new(func, 1, p);
 }
 
+static inline struct callback *callback_new_2(void (*func)(void), void *p1, void *p2)
+{
+       void *p[2];
+       p[0]=p1;
+       p[1]=p2;
+       return callback_new(func, 2, p);
+}
+
 static inline void callback_call_0(struct callback *cb)
 {
        callback_call(cb, 0, NULL);
 }
 
+static inline void callback_call_1(struct callback *cb, void *p1)
+{
+       void *p[1];
+       p[0]=p1;
+       callback_call(cb, 1, p);
+}
+
 static inline void callback_list_call_0(struct callback_list *l)
 {
        callback_list_call(l, 0, NULL);
index ade86df..f8fc267 100644 (file)
@@ -2,6 +2,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <gtk/gtk.h>
+#include "debug.h"
 #include "callback.h"
 #include "param.h"
 #include "data_window.h"
@@ -22,6 +23,29 @@ gui_gtk_datawindow_destroy(struct datawindow_priv *win)
        return;
 }
 
+static GValue value;
+static void
+select_row(GtkTreeView *tree, GtkTreePath *path, GtkTreeViewColumn *column, struct datawindow_priv *win)
+{
+       char *cols[20];
+       GtkTreeIter iter;
+       GtkTreeModel *model;
+       int i;
+
+       dbg(0,"win=%p\n", win); 
+
+       model=gtk_tree_view_get_model(tree);
+       gtk_tree_model_get_iter(model, &iter, path);
+
+       for (i=0;i<gtk_tree_model_get_n_columns(model);i++) {
+               gtk_tree_model_get_value(model, &iter, i, &value);
+               cols[i]=g_strdup_value_contents(&value)+1;
+               cols[i][strlen(cols[i])-1]='\0';
+               g_value_unset(&value);
+       }
+       callback_call_1(win->click, cols);
+}
+
 static void
 gui_gtk_datawindow_add(struct datawindow_priv *win, struct param_list *param, int count)
 {
@@ -38,20 +62,22 @@ gui_gtk_datawindow_add(struct datawindow_priv *win, struct param_list *param, in
                gtk_widget_show_all(GTK_WIDGET(win->window));
                /* add column names to treeview */
                for(i=0;i<count;i++) {
-                       cell=gtk_cell_renderer_text_new();
-                       gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (win->treeview),-1,param[i].name,
+                       if (param[i].name) {
+                               cell=gtk_cell_renderer_text_new();
+                               gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (win->treeview),-1,param[i].name,
                                        cell,"text",i, NULL);
+                       }
                }
 #if 0
                g_signal_connect(G_OBJECT(win->treeview), "click-column", G_CALLBACK(click_column), NULL);
-               g_signal_connect(G_OBJECT(win->treeview), "row-activated", G_CALLBACK(select_row), win);
 #endif
+               g_signal_connect(G_OBJECT(win->treeview), "row-activated", G_CALLBACK(select_row), win);
        }
 
        /* find data storage and create a new one if none is there */
        if (gtk_tree_view_get_model(GTK_TREE_VIEW (win->treeview)) == NULL) {
                for(i=0;i<count;i++) {
-                       if (strcmp(param[i].name, "Distance")) 
+                       if (param[i].name && !strcmp(param[i].name, "Distance")) 
                                types[i]=G_TYPE_INT;
                        else
                                types[i]=G_TYPE_STRING;
@@ -69,7 +95,7 @@ gui_gtk_datawindow_add(struct datawindow_priv *win, struct param_list *param, in
 
        /* add data to data storage */
        for(i=0;i<count;i++) {
-               if (strcmp(param[i].name, "Distance")) {
+               if (param[i].name && !strcmp(param[i].name, "Distance")) {
                        gtk_list_store_set(win->liststore,&iter,i,atoi(param[i].value),-1);
                } else {
                        utf8=g_locale_to_utf8(param[i].value,-1,NULL,NULL,NULL);
@@ -88,10 +114,12 @@ gui_gtk_datawindow_mode(struct datawindow_priv *win, int start)
        }
 }
 
-static void
+static gboolean
 gui_gtk_datawindow_delete(GtkWidget *widget, GdkEvent *event, struct datawindow_priv *win)
 {
        callback_call_0(win->close);
+
+       return FALSE;
 }
 
 
index 6ed61d2..f84eaee 100644 (file)
@@ -372,7 +372,7 @@ static char layout[] =
                                <toolitem name=\"Cursor\" action=\"CursorAction\"/>\
                                <toolitem name=\"Orientation\" action=\"OrientationAction\"/>\
                                <toolitem name=\"Destination\" action=\"DestinationAction\"/>\
-                               <toolitem name=\"Info\" action=\"InfoAction\"/>\
+                               <!-- <toolitem name=\"Info\" action=\"InfoAction\"/> -->\
                                <toolitem name=\"Roadbook\" action=\"RoadbookAction\"/>\
                                <toolitem name=\"Quit\" action=\"QuitAction\"/>\
                                <separator/>\
@@ -402,6 +402,8 @@ add_menu(struct menu_priv *menu, struct menu_methods *meth, char *name, enum men
        *meth=menu_methods;
        if (! strcmp(menu->path, "/ui/MenuBar") && !strcmp(name,"Route")) {
                dynname=g_strdup("Route");
+       } else if (! strcmp(menu->path, "/ui/MenuBar") && !strcmp(name,"Data")) {
+               dynname=g_strdup("Data");
        } else {
                dynname=g_strdup_printf("%d", menu->gui->dyn_counter++);
                if (type == menu_type_toggle)
index dc42306..9bb1dd2 100644 (file)
@@ -67,6 +67,7 @@ struct navit {
        int cursor_flag;
        int tracking_flag;
        GList *vehicles;
+       GList *windows_items;
        struct navit_vehicle *vehicle;
        struct callback_list *vehicle_cbl;
        int pid;
@@ -574,11 +575,19 @@ navit_window_roadbook_update(struct navit *this_)
 }
 
 void
+navit_window_roadbook_destroy(struct navit *this_)
+{
+       dbg(0, "enter\n");
+       navigation_unregister_callback(this_->navigation, navigation_mode_long, this_->roadbook_callback);
+       this_->roadbook_window=NULL;
+       this_->roadbook_callback=NULL;
+}
+void
 navit_window_roadbook_new(struct navit *this_)
 {
        this_->roadbook_callback=callback_new_1(callback_cast(navit_window_roadbook_update), this_);
        navigation_register_callback(this_->navigation, navigation_mode_long, this_->roadbook_callback);
-       this_->roadbook_window=gui_datawindow_new(this_->gui, "Roadbook", NULL, NULL);
+       this_->roadbook_window=gui_datawindow_new(this_->gui, "Roadbook", NULL, callback_new_1(callback_cast(navit_window_roadbook_destroy), this_));
 }
 
 static void
@@ -609,10 +618,32 @@ get_direction(char *buffer, int angle, int mode)
        }
 }
 
+struct navit_window_items {
+       struct datawindow *win;
+       struct callback *click;
+       char *name;
+       int distance;
+       GHashTable *hash;
+       GList *list;
+};
+
 static void
-navit_window_items_new(struct navit *this_)
+navit_window_items_click(struct navit *this_, struct navit_window_items *nwi, char **col)
+{
+       struct coord c;
+       char *description;
+
+       dbg(0,"enter col=%s,%s,%s,%s,%s\n", col[0], col[1], col[2], col[3], col[4]);
+       sscanf(col[4], "0x%x,0x%x", &c.x, &c.y);
+       dbg(0,"0x%x,0x%x\n", c.x, c.y);
+       description=g_strdup_printf("%s %s", nwi->name, col[3]);
+       navit_set_destination(this_, &c, description);
+       g_free(description);
+}
+
+static void
+navit_window_items_open(struct menu *men, struct navit *this_, struct navit_window_items *nwi)
 {
-       struct datawindow *win;
        struct map_selection sel;
        struct coord c,*center;
        struct mapset_handle *h;
@@ -620,35 +651,40 @@ navit_window_items_new(struct navit *this_)
        struct map_rect *mr;
        struct item *item;
        struct attr attr;
-       GHashTable *hash;
-       int idist,dist=100000;
-       enum item_type type1,type2,type3;
-       struct param_list param[4];
+       int idist,dist;
+       struct param_list param[5];
        char distbuf[32];
        char dirbuf[32];
-
+       char coordbuf[64];
+       
+       dbg(0, "distance=%d\n", nwi->distance);
+       if (nwi->distance == -1) 
+               dist=40000000;
+       else
+               dist=nwi->distance*1000;
        param[0].name="Distance";
        param[1].name="Direction";
        param[2].name="Type";
        param[3].name="Name";
+       param[4].name=NULL;
        sel.next=NULL;
+#if 0
        sel.order[layer_town]=18;
        sel.order[layer_street]=18;
        sel.order[layer_poly]=18;
+#else
+       sel.order[layer_town]=0;
+       sel.order[layer_street]=0;
+       sel.order[layer_poly]=0;
+#endif
        center=transform_center(this_->trans);
        sel.rect.lu.x=center->x-dist;
        sel.rect.lu.y=center->y+dist;
        sel.rect.rl.x=center->x+dist;
        sel.rect.rl.y=center->y-dist;
-       hash=g_hash_table_new(g_int_hash, g_int_equal);
-       type1=type_bookmark;
-       g_hash_table_insert(hash, &type1, (void *)1);
-       type2=type_poi_camping;
-       g_hash_table_insert(hash, &type2, (void *)1);
-       type3=type_roadbook;
-       g_hash_table_insert(hash, &type3, (void *)1);
        dbg(2,"0x%x,0x%x - 0x%x,0x%x\n", sel.rect.lu.x, sel.rect.lu.y, sel.rect.rl.x, sel.rect.rl.y);
-       win=gui_datawindow_new(this_->gui, "Itemlist", NULL, NULL);
+       nwi->click=callback_new_2(navit_window_items_click, this_, nwi);
+       nwi->win=gui_datawindow_new(this_->gui, nwi->name, nwi->click, NULL);
        h=mapset_open(navit_get_mapset(this_));
         while ((m=mapset_next(h, 1))) {
                dbg(2,"m=%p %s\n", m, map_get_filename(m));
@@ -656,17 +692,21 @@ navit_window_items_new(struct navit *this_)
                dbg(2,"mr=%p\n", mr);
                while ((item=map_rect_get_item(mr))) {
                        if (item_coord_get(item, &c, 1)) {
-                               if (coord_rect_contains(&sel.rect, &c) && g_hash_table_lookup(hash, &item->type)) {
+                               if (coord_rect_contains(&sel.rect, &c) && g_hash_table_lookup(nwi->hash, &item->type)) {
                                        if (! item_attr_get(item, attr_label, &attr)) 
                                                attr.u.str=NULL;
-                                       idist=transform_distance(center, &c);   
-                                       get_direction(dirbuf, transform_get_angle_delta(center, &c, 0), 2);
-                                       param[0].value=distbuf; 
-                                       param[1].value=dirbuf;
-                                       param[2].value=item_to_name(item->type);
-                                       sprintf(distbuf,"%d", idist/1000);
-                                       param[3].value=attr.u.str;
-                                       datawindow_add(win, param, 4);
+                                       idist=transform_distance(center, &c);
+                                       if (idist < dist) {
+                                               get_direction(dirbuf, transform_get_angle_delta(center, &c, 0), 1);
+                                               param[0].value=distbuf; 
+                                               param[1].value=dirbuf;
+                                               param[2].value=item_to_name(item->type);
+                                               sprintf(distbuf,"%d", idist/1000);
+                                               param[3].value=attr.u.str;
+                                               sprintf(coordbuf, "0x%x,0x%x", c.x, c.y);
+                                               param[4].value=coordbuf;
+                                               datawindow_add(nwi->win, param, 5);
+                                       }
                                        /* printf("gefunden %s %s %d\n",item_to_name(item->type), attr.u.str, idist/1000); */
                                }
                                if (item->type >= type_line) 
@@ -676,9 +716,44 @@ navit_window_items_new(struct navit *this_)
                map_rect_destroy(mr);   
        }
        mapset_close(h);
-       g_hash_table_destroy(hash);
 }
 
+struct navit_window_items *
+navit_window_items_new(char *name, int distance)
+{
+       struct navit_window_items *nwi=g_new0(struct navit_window_items, 1);
+       nwi->name=g_strdup(name);
+       nwi->distance=distance;
+       nwi->hash=g_hash_table_new(g_int_hash, g_int_equal);
+
+       return nwi;
+}
+
+void
+navit_window_items_add_item(struct navit_window_items *nwi, enum item_type type)
+{
+       nwi->list=g_list_prepend(nwi->list, (void *)type);
+       g_hash_table_insert(nwi->hash, &nwi->list->data, (void *)1);
+}
+
+void
+navit_add_window_items(struct navit *this_, struct navit_window_items *nwi)
+{
+       this_->windows_items=g_list_append(this_->windows_items, nwi);
+}
+
+void
+navit_add_menu_windows_items(struct navit *this_, struct menu *men)
+{
+       struct navit_window_items *nwi;
+       GList *l;
+       l=this_->windows_items;
+       while (l) {
+               nwi=l->data;
+               menu_add(men, nwi->name, menu_type_menu, navit_window_items_open, this_, nwi);
+               l=g_list_next(l);
+       }
+}
 
 void
 navit_init(struct navit *this_)
@@ -716,6 +791,12 @@ navit_init(struct navit *this_)
 #if 0
 #endif
        }
+       if (this_->menubar) {
+               men=menu_add(this_->menubar, "Data", menu_type_submenu, NULL, NULL, NULL);
+               if (men) {
+                       navit_add_menu_windows_items(this_, men);
+               }
+       }
        global_navit=this_;
 #if 0
        navit_window_roadbook_new(this_);
index f102786..0eff60b 100644 (file)
@@ -215,6 +215,35 @@ xmlconfig_vehicle(struct xmlstate *state)
 }
 
 static int
+xmlconfig_window_items(struct xmlstate *state)
+{
+       int distance=-1;
+       enum item_type itype;
+       const char *name=find_attribute(state, "name", 1);
+       const char *value=find_attribute(state, "distance", 0);
+       const char *type=find_attribute(state, "type", 1);
+       char *tok,*str,*type_str,*saveptr;
+       if (! name || !type)
+               return 0;
+       if (value) 
+               distance=convert_number(value);
+       state->element_object = navit_window_items_new(name, distance);
+       type_str=g_strdup(type);
+       str=type_str;
+       while ((tok=strtok_r(str, ",", &saveptr))) {
+               itype=item_from_name(tok);
+               navit_window_items_add_item(state->element_object, itype);
+               str=NULL;
+       }
+       g_free(type_str);
+
+       navit_add_window_items(state->parent->element_object, state->element_object);
+
+       return 1;
+}
+
+
+static int
 xmlconfig_tracking(struct xmlstate *state)
 {
        state->element_object = tracking_new(NULL);
@@ -511,6 +540,7 @@ struct element_func {
        { "route", "navit", xmlconfig_route},
        { "speed", "route", xmlconfig_speed},
        { "vehicle", "navit", xmlconfig_vehicle},
+       { "window_items", "navit", xmlconfig_window_items},
        { "plugins", NULL, xmlconfig_plugins},
        { "plugin", "plugins", xmlconfig_plugin},
        {},