Re: edit and use treeview cell



On Thursday 24 January 2013 11:02:37 Rudra Banerjee wrote:
On Thu, 2013-01-24 at 10:45 +0100, David NeÄas wrote:
Have you read the tutorial?

    http://scentric.net/tutorial/sec-editable-cells.html
David,
Thanks for your reply.
Yes, I have read that tutorial. 
After reading that I have learned how to make the cell editable. But the
problem still confuses me. And also, as I said, how to use this.
 That tut says I need a function "cell_edited_callback" to make the edit
in file. But bit clueless about how to do that.

_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Hi,
i used something like that in gtk2 and it used to work,
hope it helps and is understandable as my code is sometimes
a little bit convoluted as I'm self-taught,

Ciao,
ZZ

static void list_edited(GtkCellRendererText * cell, gchar * path_string, gchar * new_text, gpointer user_data)
{
        GtkTreeView *treeview = (GtkTreeView *)user_data;
        GtkTreeModel *model;
        GtkTreeIter iter;
        guint column;
        
        /* Column number is passed as renderer object data */
        gpointer columnptr = g_object_get_data(G_OBJECT(cell), "column");
        
        column = GPOINTER_TO_UINT(columnptr);

        /* Get the iterator */
        model = gtk_tree_view_get_model(treeview);
        gtk_tree_model_get_iter_from_string(model, &iter, path_string);
        
        /* Update the model */
        gtk_list_store_set(GTK_LIST_STORE(model), &iter, column, new_text, -1);
}

static void text_editing_started (GtkCellRenderer ATTRIBUTE_UNUSED *cell, GtkCellEditable *editable,
                      const gchar *path, GCallback data)
{
        debug_err_msg("%s: started", __FUNCTION__);

        if (GTK_IS_ENTRY (editable)) {
                GtkEntry *entry = GTK_ENTRY (editable);
                GCallback cb_func = data;
                g_signal_connect(GTK_OBJECT(entry), "activate", (GCallback)cb_func, (char *)xstrdup(path));
        }
}


GtkWidget *string_list_create(int num, GCallback func, int show_hide, int editable,...)
{
        GtkCellRenderer *renderer;
        GtkTreeViewColumn *column;
        GtkListStore *store;
        GtkWidget *tree;
        GtkTreeSelection *selection;
        GType *types;
        va_list titles;
        int i;
        char *tmp;
        char *label;
        double align;

        types = (GType *) malloc((num + 1) * sizeof(GType));
        for (i = 0; i < num; i++) {
                types[i] = G_TYPE_STRING;
        }

        store = gtk_list_store_newv(num, types);
        xfree(&types);
        tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
        gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
        /* Setup the selection handler */
        selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
        gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);

        if (func) {
                g_signal_connect(selection, "changed", G_CALLBACK(func), NULL);
        }
        /* The view now holds a reference.  We can get rid of our own reference */
        g_object_unref(G_OBJECT(store));

        va_start(titles, editable);
        for (i = 0; i < num; i++) {
                /* Create a cell renderer */
                renderer = gtk_cell_renderer_text_new();

                tmp = va_arg(titles, char *);
                align = va_arg(titles, double);

                if (editable == EDITABLE || (editable == CUSTOM && va_arg(titles, int) == EDITABLE)) {
                        g_object_set(renderer,"editable", TRUE, NULL);
                        g_object_set_data(G_OBJECT(renderer), "column", GUINT_TO_POINTER(i));   
                        g_signal_connect(GTK_OBJECT(renderer), "edited", G_CALLBACK(list_edited), tree);
                        if (editable == CUSTOM) {
                                g_signal_connect(GTK_OBJECT(renderer), "editing-started", 
G_CALLBACK(text_editing_started),
                                (gpointer) va_arg(titles, void*));
                        }
                }

                if (!tmp || show_hide == HIDE ) tmp = "";

                if (g_utf8_validate(tmp, -1, NULL) != TRUE) {
                        label = g_locale_to_utf8(tmp , -1, NULL, NULL, NULL);
                        /* Create a column, associating the "text" attribute of the  cell_renderer to the 
column of the model */
                        column = gtk_tree_view_column_new_with_attributes(label, renderer, "text", i, NULL);
                        xfree(&label);
                } else {
                        column = gtk_tree_view_column_new_with_attributes(tmp, renderer, "text", i, NULL);
                }
                g_object_set (renderer,"xalign", align, NULL);
                /* Add the column to the view. */
                gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
        }
        va_end(titles);
        return tree;
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]