--- trunk/src/misc.c 2009/11/26 14:32:41 216 +++ trunk/src/misc.c 2009/11/26 21:21:03 217 @@ -33,6 +33,10 @@ #include #endif +#ifdef FREMANTLE +#define PICKER_DIALOG +#endif + char strlastchr(char *str) { return str[strlen(str)]-1; } @@ -535,7 +539,7 @@ return widget; } -static void pos_set(GtkMenuItem *item, float lat, float lon) { +static void pos_set(GtkWidget *item, float lat, float lon) { char str[32]; pos_lat_str(str, sizeof(str)-1, lat); @@ -547,7 +551,7 @@ gtk_entry_set_text(GTK_ENTRY(lon_entry), str); } -static void cb_gps(GtkMenuItem *item, gpointer data) { +static void cb_gps(GtkWidget *item, gpointer data) { appdata_t *appdata = (appdata_t*)data; pos_t *refpos = get_pos(appdata); @@ -555,59 +559,46 @@ else pos_set(item, refpos->lat, refpos->lon); } -static void cb_geomath(GtkMenuItem *item, gpointer data) { +static void cb_geomath(GtkWidget *item, gpointer data) { appdata_t *appdata = (appdata_t*)data; pos_set(item, appdata->geomath.lat, appdata->geomath.lon); } #ifdef ENABLE_OSM_GPS_MAP -static void cb_map(GtkMenuItem *item, gpointer data) { +static void cb_map(GtkWidget *item, gpointer data) { appdata_t *appdata = (appdata_t*)data; pos_set(item, appdata->map.pos.lat, appdata->map.pos.lon); } #endif -static const gchar *menu_item_get_label(GtkMenuItem *menu_item) { - GList *children, *l; - GtkWidget *child; - children = gtk_container_get_children (GTK_CONTAINER (menu_item)); - for (l = g_list_first (children); l != NULL; - l = g_list_next (l)) { - child = (GtkWidget *)l->data; - if (GTK_IS_LABEL (child)) { - return gtk_label_get_label (GTK_LABEL (child)); - } - } - return NULL; -} - -static void cb_cache(GtkMenuItem *item, gpointer data) { - const char *label = menu_item_get_label(item); +static void cb_cache(GtkWidget *item, gpointer data) { appdata_t *appdata = (appdata_t*)data; - + cache_t *cache = appdata->cur_cache; g_assert(cache); - if(!strcmp(label, cache->id)) + gint id = (gint)g_object_get_data(G_OBJECT(item), "id"); + + if(!id) pos_set(item, cache->pos.lat, cache->pos.lon); else { wpt_t *wpt = cache->wpt; - while(wpt) { - if(!strcmp(label, wpt->id)) { - pos_set(item, wpt->pos.lat, wpt->pos.lon); - return; - } - + while(wpt && id > 1) { wpt = wpt->next; + id--; } + + if(id == 1) + pos_set(item, wpt->pos.lat, wpt->pos.lon); } } +#ifndef PICKER_DIALOG static GtkWidget *menu_add(GtkWidget *menu, appdata_t *appdata, GtkWidget *icon, char *menu_str, - void(*func)(GtkMenuItem*, gpointer), + void(*func)(GtkWidget*, gpointer), gint id, GtkWidget *lon_entry, GtkWidget *lat_entry) { GtkWidget *item = gtk_image_menu_item_new_with_label(menu_str); @@ -617,6 +608,7 @@ g_object_set_data(G_OBJECT(item), "lat_entry", (gpointer)lat_entry); g_object_set_data(G_OBJECT(item), "lon_entry", (gpointer)lon_entry); + g_object_set_data(G_OBJECT(item), "id", (gpointer)id); if(func) gtk_signal_connect(GTK_OBJECT(item), "activate", @@ -632,32 +624,37 @@ GtkWidget *menu = gtk_menu_new(); menu_add(menu, appdata, icon_get_widget(ICON_POS, 18), - _("Current position (GPS)"), cb_gps, lon_entry, lat_entry); + _("Current position (GPS)"), cb_gps, 0, lon_entry, lat_entry); menu_add(menu, appdata, icon_get_widget(ICON_POS, 19), - _("Geomath projection"), cb_geomath, lon_entry, lat_entry); + _("Geomath projection"), cb_geomath, 0, lon_entry, lat_entry); #ifdef ENABLE_OSM_GPS_MAP menu_add(menu, appdata, icon_get_widget(ICON_POS, 20), - _("Map position"), cb_map, lon_entry, lat_entry); + _("Map position"), cb_map, 0, lon_entry, lat_entry); #endif - printf("popup cache present: %s\n", appdata->cur_cache?"Yes":"No"); - if(appdata->cur_cache) { cache_t *cache = appdata->cur_cache; + char *name = cache->name; + if(!name) name = cache->id; + if(!isnan(cache->pos.lat) && !isnan(cache->pos.lon)) { menu_add(menu, appdata, icon_get_widget(ICON_POS, cache->type + 6), - cache->id, cb_cache, lon_entry, lat_entry); + name, cb_cache, 0, lon_entry, lat_entry); } - printf("appending cache waypoints\n"); wpt_t *wpt = cache->wpt; + gint id = 1; while(wpt) { GtkWidget *icon = NULL; if(wpt->sym != WPT_SYM_UNKNOWN) icon = icon_get_widget(ICON_POS, wpt->sym); - menu_add(menu, appdata, icon, wpt->id, cb_cache, + char *name = wpt->desc; + if(!name) name = wpt->cmt; + if(!name) name = wpt->id; + + menu_add(menu, appdata, icon, name, cb_cache, id++, lon_entry, lat_entry); wpt = wpt->next; @@ -687,108 +684,167 @@ GtkWidget *menu = g_object_get_data(G_OBJECT(widget), "menu"); gtk_widget_destroy(menu); } - -GtkWidget *coo_popup(appdata_t *appdata, - GtkWidget *lat_entry, GtkWidget *lon_entry) { - - GtkWidget *button = gtk_button_new(); -#ifdef FREMANTLE - hildon_gtk_widget_set_theme_size(button, - (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH)); #endif - gtk_button_set_image(GTK_BUTTON(button), icon_get_widget(ICON_POS, 17)); +#ifdef PICKER_DIALOG - gtk_widget_set_tooltip_text(button, _("Preset coordinates")); +enum { + PICKER_COL_ICON = 0, + PICKER_COL_NAME, + PICKER_COL_ID, + PICKER_COL_CB, + PICKER_NUM_COLS +}; + +static void picker_add(GtkListStore *store, appdata_t *appdata, + GdkPixbuf *icon, char *menu_str, + void(*func)(GtkWidget*, gpointer), gint id) { + GtkTreeIter iter; + + /* Append a row and fill in some data */ + gtk_list_store_append (store, &iter); + + gtk_list_store_set(store, &iter, + PICKER_COL_ICON, icon, + PICKER_COL_NAME, menu_str, + PICKER_COL_ID, id, + PICKER_COL_CB, func, + -1); +} + +static void on_picker_activated(GtkTreeView *treeview, + GtkTreePath *path, + GtkTreeViewColumn *col, + gpointer userdata) { + GtkTreeIter iter; + GtkTreeModel *model = gtk_tree_view_get_model(treeview); + + if(gtk_tree_model_get_iter(model, &iter, path)) { + gint id; + void(*func)(GtkWidget*, gpointer); + gtk_tree_model_get(model, &iter, + PICKER_COL_ID, &id, + PICKER_COL_CB, &func, + -1); + + /* set id on widget as callbacks expect it this way */ + g_object_set_data(G_OBJECT(treeview), "id", (gpointer)id); + func(GTK_WIDGET(treeview), userdata); + } +} - gtk_signal_connect(GTK_OBJECT(button), "button-press-event", - (GtkSignalFunc)on_popup_button_press, appdata); +static GtkWidget *picker_create(appdata_t *appdata, + GtkWidget *lat_entry, GtkWidget *lon_entry) { + GtkCellRenderer *renderer; + GtkListStore *store; - gtk_signal_connect(GTK_OBJECT(button), "destroy", - (GtkSignalFunc)on_popup_destroy, appdata); + GtkWidget *view = gtk_tree_view_new(); - g_object_set_data(G_OBJECT(button), "menu", - popup_menu_create(appdata, lat_entry, lon_entry)); - - return button; -} + g_object_set_data(G_OBJECT(view), "lat_entry", (gpointer)lat_entry); + g_object_set_data(G_OBJECT(view), "lon_entry", (gpointer)lon_entry); -GtkWidget *entry_new(void) { -#if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5) - return gtk_entry_new(); -#else - return hildon_entry_new(HILDON_SIZE_AUTO); + /* --- "Icon" column --- */ + renderer = gtk_cell_renderer_pixbuf_new(); + gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), + -1, "Icon", renderer, "pixbuf", PICKER_COL_ICON, NULL); + + /* --- "Name" column --- */ + renderer = gtk_cell_renderer_text_new(); + g_object_set(renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL ); + GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes( + "Name", renderer, "text", PICKER_COL_NAME, NULL); + gtk_tree_view_column_set_expand(column, TRUE); + gtk_tree_view_insert_column(GTK_TREE_VIEW(view), column, -1); + + store = gtk_list_store_new(PICKER_NUM_COLS, + GDK_TYPE_PIXBUF, + G_TYPE_STRING, + G_TYPE_INT, + G_TYPE_POINTER); + + picker_add(store, appdata, icon_get(ICON_POS, 18), + _("Current position (GPS)"), cb_gps, 0); + picker_add(store, appdata, icon_get(ICON_POS, 19), + _("Geomath projection"), cb_geomath, 0); +#ifdef ENABLE_OSM_GPS_MAP + picker_add(store, appdata, icon_get(ICON_POS, 20), + _("Map position"), cb_map, 0); #endif -} -gboolean pos_differ(pos_t *pos1, pos_t *pos2) { - int lat1 = (60000 * pos1->lat)+0.5, lon1 = (60000 * pos1->lon)+0.5; - int lat2 = (60000 * pos2->lat)+0.5, lon2 = (60000 * pos2->lon)+0.5; - - return((lat1 != lat2) || (lon1 != lon2)); -} - -#ifdef FREMANTLE -void selection_changed (HildonTouchSelector * selector, - gpointer *user_data) { - gchar *current_selection = NULL; - - current_selection = hildon_touch_selector_get_current_text (selector); - g_debug ("Current selection : %s", current_selection); -} + if(appdata->cur_cache) { + cache_t *cache = appdata->cur_cache; -static GtkWidget *create_customized_selector() { - GtkWidget *selector = NULL; - GSList *icon_list = NULL; - GtkListStore *store_icons = NULL; - GSList *item = NULL; - GtkCellRenderer *renderer = NULL; - HildonTouchSelectorColumn *column = NULL; + char *name = cache->name; + if(!name) name = cache->id; - selector = hildon_touch_selector_new (); + if(!isnan(cache->pos.lat) && !isnan(cache->pos.lon)) { + picker_add(store, appdata, icon_get(ICON_POS, cache->type + 6), + name, cb_cache, 0); + } - icon_list = gtk_stock_list_ids (); + wpt_t *wpt = cache->wpt; + gint id = 1; + while(wpt) { + GdkPixbuf *icon = NULL; + if(wpt->sym != WPT_SYM_UNKNOWN) + icon = icon_get(ICON_POS, wpt->sym); - store_icons = gtk_list_store_new (1, G_TYPE_STRING); - for (item = icon_list; item; item = g_slist_next (item)) { - GtkTreeIter iter; - gchar *label = item->data; + char *name = wpt->desc; + if(!name) name = wpt->cmt; + if(!name) name = wpt->id; - gtk_list_store_append (store_icons, &iter); - gtk_list_store_set (store_icons, &iter, 0, label, -1); - g_free (label); + picker_add(store, appdata, icon, name, cb_cache, id++); + wpt = wpt->next; + } } - g_slist_free (icon_list); - renderer = gtk_cell_renderer_pixbuf_new (); - gtk_cell_renderer_set_fixed_size (renderer, -1, 100); - column = hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector), - GTK_TREE_MODEL (store_icons), - renderer, "stock-id", 0, NULL); + gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store)); + g_object_unref(store); - g_object_set (G_OBJECT (column), "text-column", 0, NULL); - - hildon_touch_selector_set_column_selection_mode (HILDON_TOUCH_SELECTOR (selector), - HILDON_TOUCH_SELECTOR_SELECTION_MODE_SINGLE); - - - g_signal_connect (G_OBJECT (selector), "changed", - G_CALLBACK (selection_changed), NULL); - - return selector; + /* make list react on clicks */ + g_signal_connect(view, "row-activated", + (GCallback)on_picker_activated, appdata); + +#if 0 + g_signal_connect(view, "destroy", + (GCallback)cachelist_destroy, ce); +#endif + + /* put this inside a scrolled view */ +#ifndef USE_PANNABLE_AREA + GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL); + gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), + GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); + gtk_container_add(GTK_CONTAINER(scrolled_window), view); + return scrolled_window; +#else + GtkWidget *pannable_area = hildon_pannable_area_new(); + gtk_container_add(GTK_CONTAINER(pannable_area), view); + return pannable_area; +#endif } -static gint on_picker_request(GtkWidget *button, GdkEventButton *event, - gpointer data) { +static gint on_picker_button_press(GtkWidget *button, + GdkEventButton *event, gpointer data) { + appdata_t *appdata = (appdata_t*)data; + gpointer lat_entry = g_object_get_data(G_OBJECT(button), "lat_entry"); + gpointer lon_entry = g_object_get_data(G_OBJECT(button), "lon_entry"); + if(event->type == GDK_BUTTON_PRESS) { - GtkWidget *dialog = hildon_picker_dialog_new(NULL); - GtkWidget *selector = create_customized_selector(); - hildon_picker_dialog_set_selector(HILDON_PICKER_DIALOG(dialog), - HILDON_TOUCH_SELECTOR(selector)); + GtkWidget *dialog = + gtk_dialog_new_with_buttons(_("Preset coordinates"), + GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(button))), + GTK_DIALOG_MODAL, + GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, + GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, + NULL); + + gtk_window_set_default_size(GTK_WINDOW(dialog), 400, 200); - hildon_touch_selector_set_active(HILDON_TOUCH_SELECTOR (selector), 1, -1); + gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), + picker_create(appdata, lat_entry, lon_entry)); gtk_widget_show_all(dialog); gtk_dialog_run(GTK_DIALOG(dialog)); @@ -798,29 +854,54 @@ } return FALSE; } +#endif -GtkWidget *picker_button_new(void) { - GtkWidget *button; +GtkWidget *coo_popup(appdata_t *appdata, + GtkWidget *lat_entry, GtkWidget *lon_entry) { -#if 1 - button = gtk_button_new(); - hildon_gtk_widget_set_theme_size(button, - (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH)); + GtkWidget *button = gtk_button_new(); gtk_button_set_image(GTK_BUTTON(button), icon_get_widget(ICON_POS, 17)); - + + gtk_widget_set_tooltip_text(button, _("Preset coordinates")); + +#ifndef PICKER_DIALOG gtk_signal_connect(GTK_OBJECT(button), "button-press-event", - (GtkSignalFunc)on_picker_request, NULL); + (GtkSignalFunc)on_popup_button_press, appdata); -#else - button = hildon_picker_button_new(HILDON_SIZE_AUTO, - HILDON_BUTTON_ARRANGEMENT_VERTICAL); + gtk_signal_connect(GTK_OBJECT(button), "destroy", + (GtkSignalFunc)on_popup_destroy, appdata); - hildon_button_set_title(HILDON_BUTTON(button), "Test"); - hildon_picker_button_set_selector(HILDON_PICKER_BUTTON(button), - HILDON_TOUCH_SELECTOR(create_customized_selector())); + g_object_set_data(G_OBJECT(button), "menu", + popup_menu_create(appdata, lat_entry, lon_entry)); +#else +#ifdef FREMANTLE + hildon_gtk_widget_set_theme_size(button, + (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH)); #endif + g_object_set_data(G_OBJECT(button), "lat_entry", (gpointer)lat_entry); + g_object_set_data(G_OBJECT(button), "lon_entry", (gpointer)lon_entry); + + gtk_signal_connect(GTK_OBJECT(button), "button-press-event", + (GtkSignalFunc)on_picker_button_press, appdata); +#endif + return button; } + +GtkWidget *entry_new(void) { +#if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5) + return gtk_entry_new(); +#else + return hildon_entry_new(HILDON_SIZE_AUTO); #endif +} + +gboolean pos_differ(pos_t *pos1, pos_t *pos2) { + int lat1 = (60000 * pos1->lat)+0.5, lon1 = (60000 * pos1->lon)+0.5; + int lat2 = (60000 * pos2->lat)+0.5, lon2 = (60000 * pos2->lon)+0.5; + + return((lat1 != lat2) || (lon1 != lon2)); +} +