On Sunday 19 August 2007 18:34:32 David Nečas wrote:
> On Sun, Aug 19, 2007 at 05:16:49PM +0100, Tony Cowderoy wrote:
> > 
> > First question - is there any (preferably easy) way with a GtkTreeView 
> > to make any editable cell that is selected automatically enter edit mode 
> > and automatically commit the changes when focus moves elsewhere?
> 
> IIRC this is tricky to get right, so easy probably no.
> Hopefuly someone who has a working example will post it...
> 
> > Second - in the application that I'm trying to develop I have a 
> > GtkTreeview with data in a GtkListStore and some, but not all, of the 
> > cells editable.  If I have a cell selected, but not opened for editing 
> > (this includes non-editable cells) and I then start typing, an editable 
> > box appears in the bottom right-hand corner of the GtkTreeView, which 
> > displays what I'm typing.  If I press enter, it goes away and the text 
> > of the selected cell is not updated.  Does anyone know why this odd 
> > behaviour is happening and what I can do to stop it?
> 
> I suppose it's the search box.  In such case:
> 
>   gtk_tree_view_set_enable_search(treeview, FALSE);
> 
> (or set the corresponding property).
> 
> Yeti
> 
> --
> http://gwyddion.net/
> _______________________________________________
> gtk-app-devel-list mailing list
> gtk-app-devel-list@gnome.org
> http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
> 

Hi,
this should do something similar to what you are looking for:


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, GtkSignalFunc 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", 
GTK_SIGNAL_FUNC(list_edited), tree);
                        if (editable == CUSTOM) {
                                g_signal_connect(GTK_OBJECT(renderer), 
"editing-started", GTK_SIGNAL_FUNC(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;
}



Ciao,
Tito

PS.: Hope it helps, cannot post an example at the moment 'cause i'm in a hurry. 
If you need, ask and i'll post it later.

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

Reply via email to