dive computer nicknames
Dirk Hohndel
dirk at hohndel.org
Wed Dec 19 22:27:07 PST 2012
Any takers? We have almost 80 people subscribed here, many of whom I
/know/ could add this to Subsurface in a couple of hours... Lubomir
doesn't even dive and he is the #3 contributor to Subsurface at this
stage... I would love to see a few more people become more active and
provide code...
We really need to find a gtk hacker who dives :-)
/D
"Lubomir I. Ivanov" <neolit123 at gmail.com> writes:
> no idea if this is the correct direction, but here is something to get
> this thing started.
> i'm _quite busy_ at the moment - outside of work, barely have time to
> go out to throw the garbage and buy food for the house etc.
> so yeah...if someone wants to tackle this go ahead in the meantime.
>
> the dialog is in the provided .c file as a standalone app for now.
> it generates some data to populate a dummy dc list.
>
> lubomir
> --
> #include <gtk/gtk.h>
>
> /* list columns for nickname edit treeview */
> enum {
> NE_IDX, /* hidden, list index */
> NE_VENDOR,
> NE_MODEL,
> NE_ID, /* hidden */
> NE_ID_STR, /* deviceid as a hex string */
> NE_NICKNAME,
> NE_NCOL
> };
>
> /* dummy dc struct */
> typedef struct {
> guint deviceid;
> gchar *vendor;
> gchar *model;
> gchar *nickname;
> } dc_info_t;
>
> /* dummy dc list */
> #define DC_TOTAL 100
> static dc_info_t *dc_list;
>
> static void set_dc(guint idx,
> gchar *vendor,
> gchar *model,
> gchar *nickname,
> guint deviceid)
> {
> dc_list[idx].deviceid = deviceid;
> dc_list[idx].vendor = g_strdup(vendor);
> dc_list[idx].model = g_strdup(model);
> dc_list[idx].nickname = g_strdup(nickname);
> }
>
> /* delete a selection of nicknames */
> static void edit_dc_delete_rows(GtkTreeView *view)
> {
> GtkTreeModel *model;
> GtkTreePath *path;
> GtkTreeIter iter;
> GtkTreeRowReference *ref;
> GtkTreeSelection *selection;
> GList *selected_rows, *list, *row_references = NULL;
>
> selection = gtk_tree_view_get_selection(view);
> /* this isn't compatible bellow gtk 2.2, so should be done manually using 'for_each' ! */
> selected_rows = gtk_tree_selection_get_selected_rows(selection, &model);
>
> for (list = selected_rows; list; list = g_list_next(list)) {
> path = list->data;
> ref = gtk_tree_row_reference_new(model, path);
> row_references = g_list_append(row_references, ref);
> }
>
> for (list = row_references; list; list = g_list_next(list)) {
> path = gtk_tree_row_reference_get_path((GtkTreeRowReference *)(list->data));
> gtk_tree_model_get_iter(model, &iter, path);
> gtk_list_store_remove(GTK_LIST_STORE (model), &iter);
> gtk_tree_path_free(path);
> }
>
> g_list_free(selected_rows);
> g_list_free(row_references);
> g_list_free(list);
>
> if (gtk_tree_model_get_iter_first(model, &iter))
> gtk_tree_selection_select_iter(selection, &iter);
> }
>
> /* repopulate the edited nickname cell of a DC */
> static void cell_edited_cb(GtkCellRendererText *cell,
> gchar *path_string,
> gchar *new_text,
> gpointer user_data)
> {
> GtkTreeIter iter;
> guint id;
> gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(user_data), &iter, path_string);
> gtk_list_store_set(GTK_LIST_STORE(user_data), &iter, NE_NICKNAME, new_text, -1);
> gtk_tree_model_get(GTK_TREE_MODEL(user_data), &iter, NE_IDX, &id, -1);
> /* free old / set new */
> g_free(dc_list[id].nickname);
> dc_list[id].nickname = g_strdup(new_text);
> }
>
> /* show the dialog to edit dc nicknames */
> static void edit_dc_nicknames(GtkWidget *w, gpointer data)
> {
> const gint RESPONSE_DELETE = 1;
> const gchar *C_INACTIVE = "#e8e8ee", *C_ACTIVE = "#ffffff";
> GtkWidget *dialog, *confirm, *view, *scroll, *vbox;
> GtkCellRenderer *renderer;
> GtkTreeSelection *selection;
> GtkTreeModel *model;
> GtkListStore *store;
> GtkTreeIter iter;
> gint res = -1;
>
> dialog = gtk_dialog_new_with_buttons("Edit DC Nicknames",
> GTK_WINDOW(w),
> GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
> GTK_STOCK_DELETE,
> RESPONSE_DELETE,
> GTK_STOCK_CLOSE,
> GTK_RESPONSE_CLOSE,
> NULL);
> gtk_widget_set_size_request(dialog, 500, 400);
>
> scroll = gtk_scrolled_window_new(NULL, NULL);
> gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
>
> view = gtk_tree_view_new();
> store = gtk_list_store_new(NE_NCOL, G_TYPE_UINT, G_TYPE_STRING, G_TYPE_STRING,
> G_TYPE_UINT, G_TYPE_STRING, G_TYPE_STRING);
> model = GTK_TREE_MODEL(store);
>
> /* columns */
> renderer = gtk_cell_renderer_text_new();
> g_object_set(renderer, "background", C_INACTIVE, NULL);
> gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
> -1, "Vendor", renderer, "text", NE_VENDOR, NULL);
> renderer = gtk_cell_renderer_text_new();
> g_object_set(renderer, "background", C_INACTIVE, NULL);
> gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
> -1, "Model", renderer, "text", NE_MODEL, NULL);
> renderer = gtk_cell_renderer_text_new();
> g_object_set(renderer, "background", C_INACTIVE, NULL);
> gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
> -1, "ID", renderer, "text", NE_ID_STR, NULL);
> renderer = gtk_cell_renderer_text_new();
> g_object_set(renderer, "background", C_INACTIVE, NULL);
> gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
> -1, "Nickname", renderer, "text", NE_NICKNAME, NULL);
> g_object_set(renderer, "editable", TRUE, NULL);
> g_object_set(renderer, "background", C_ACTIVE, NULL);
> g_signal_connect(renderer, "edited", G_CALLBACK(cell_edited_cb), store);
>
> gtk_tree_view_set_model(GTK_TREE_VIEW(view), model);
> g_object_unref(model);
>
> /* example populate list store */
> int i = 0;
> char id_string[11] = {0}; /* this buffer holds the id as hex string */
>
> while (i < DC_TOTAL) {
> sprintf(&id_string[0], "%#08x", dc_list[i].deviceid);
> gtk_list_store_append(store, &iter);
> gtk_list_store_set(store, &iter,
> NE_IDX, i,
> NE_VENDOR, dc_list[i].vendor,
> NE_MODEL, dc_list[i].model,
> NE_ID, dc_list[i].deviceid,
> NE_ID_STR, id_string,
> NE_NICKNAME, dc_list[i].nickname,
> -1);
> i++;
> }
>
> selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
> gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_MULTIPLE);
>
> vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
> gtk_container_add(GTK_CONTAINER(scroll), view);
> gtk_container_add(GTK_CONTAINER(vbox),
> gtk_label_new("Edit a DC's nickname by douoble-clicking inside the 'Nickname' column"));
> gtk_container_add(GTK_CONTAINER(vbox), scroll);
> gtk_widget_set_size_request(scroll, 500, 300);
> gtk_widget_show_all(dialog);
>
> do {
> res = gtk_dialog_run(GTK_DIALOG(dialog));
> if (res == RESPONSE_DELETE) {
> confirm = gtk_dialog_new_with_buttons("Delete DC nicknames",
> GTK_WINDOW(dialog),
> GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
> GTK_STOCK_YES,
> GTK_RESPONSE_YES,
> GTK_STOCK_NO,
> GTK_RESPONSE_NO,
> NULL);
> gtk_widget_set_size_request(confirm, 350, 90);
> vbox = gtk_dialog_get_content_area(GTK_DIALOG(confirm));
> gtk_container_add(GTK_CONTAINER(vbox),
> gtk_label_new("Are you sure you want to delete the selected entries?"));
> gtk_widget_show_all(confirm);
> if (gtk_dialog_run(GTK_DIALOG(confirm)) == GTK_RESPONSE_YES)
> edit_dc_delete_rows(GTK_TREE_VIEW(view));
> gtk_widget_destroy(confirm);
> }
> } while (res != GTK_RESPONSE_CLOSE && res != GTK_RESPONSE_DELETE_EVENT);
> gtk_widget_destroy(dialog);
> }
>
> int main(int argc, char **argv)
> {
> GtkWidget *window;
> /* populate the dummy list */
> int i = 0;
> char vbuf[11] = {0}; /* temp. buffer */
> char mbuf[11] = {0}; /* temp. buffer */
> dc_list = (dc_info_t *)g_malloc(DC_TOTAL * sizeof(dc_info_t));
> while (i < DC_TOTAL) {
> sprintf(&vbuf[0], "%s%d", "vendor", i);
> sprintf(&mbuf[0], "%s%d", "model", i);
> set_dc(i, &vbuf[0], &mbuf[0], (!i) ? "nickname" : "", 0xffffffff & i);
> i++;
> }
>
> /* init ui */
> gtk_init(&argc, &argv);
> window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
> g_signal_connect(window, "delete_event", gtk_main_quit, NULL);
> g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
> gtk_widget_show_all(window);
> edit_dc_nicknames(window, NULL);
> gtk_main();
>
> /* free dummy list */
> i = 0;
> while (i < DC_TOTAL) {
> g_free(dc_list[i].vendor);
> g_free(dc_list[i].model);
> g_free(dc_list[i].nickname);
> i++;
> }
> g_free(dc_list);
> return 0;
> }
> _______________________________________________
> subsurface mailing list
> subsurface at hohndel.org
> http://lists.hohndel.org/cgi-bin/mailman/listinfo/subsurface
--
Dirk Hohndel
Intel Open Source Technology Center
More information about the subsurface
mailing list