Re: Dynamic changing values at a GtkTreeStore with pointers
First of all: Thank you very much for your reply. It was essential for me to understand what was going on. And you were right about the put some code part. I am too inexperienced yet. Ok, let's go: It took me all theses days to understand it and I am quite sure I didn't get it all. One point that took me most of my time was this thing that I considered a flaw: When we assume that one attribute of a model will be G_TYPE_POINTER and we want to store there a string pointer, we don't pass a pointer, but just a pointer. It is a little confusing to me because I assume that POINTERS are address and address are passed using key. But maybe (or for sure) my understanding of it is wrong. Now, I can change my pointers values and the values at the tree_view is updated. I did not implement yet the change_row signed, but I sure will. Just one thing still bothers me: I noticed that every time I pass my mouse pointer throw the row, the cells renderer functions are called. Is this right or I made something wrong? Everything is working ok, but I am a little concerned about performance. Here is my code: PS: I am sorry about the names of my variables, but I am developing code for my country mates, so It needed to mean something at my language, which is portuguese. /*Where I create my tree*/ enum { SEMESTRE, ANO, CODIGO, NOME, NUM_COLUNAS_DADOS }; GtkWidget *sc_arvore_dados = NULL; GtkTreeStore *armazena = NULL; GtkWidget *arvore_dados = NULL; /*arvore_dados***/ armazena = gtk_tree_store_new( NUM_COLUNAS_DADOS, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER ); sc_arvore_dados = gtk_scrolled_window_new( NULL, NULL ); gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sc_arvore_dados), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); arvore_dados = gtk_tree_view_new_with_model (GTK_TREE_MODEL (armazena)); gtk_container_add( GTK_CONTAINER( sc_arvore_dados ), arvore_dados ); /*Here I used your idea of adding columns. I had my own *function which would create a entire tree at once *but it was very bloated and your idea is much *elegant that my was*/ add_coluna( GTK_TREE_VIEW( arvore_dados ), Semestre, renderiza_semestre, NULL ); add_coluna( GTK_TREE_VIEW( arvore_dados ), Ano, renderiza_ano, NULL ); add_coluna( GTK_TREE_VIEW( arvore_dados ), Codigo, renderiza_codigo, NULL ); add_coluna( GTK_TREE_VIEW( arvore_dados ), Nome, renderiza_nome, NULL ); gtk_widget_show( arvore_dados ); gtk_widget_show( sc_arvore_dados ); /**/ /*Here is the add_coluna function, which is your add_column function*/ static void add_coluna(GtkTreeView *treeview, const gchar *title, GtkCellLayoutDataFunc func, gpointer func_data) { GtkTreeViewColumn *column; GtkCellRenderer *renderer; renderer = gtk_cell_renderer_text_new(); g_object_set(renderer, family, Monospace, NULL); column = gtk_tree_view_column_new(); gtk_tree_view_column_set_title(column, title); gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column), renderer, TRUE); gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(column), renderer, func, func_data, NULL); gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column); } /*And here are the cell data functions*/ static void renderiza_semestre(G_GNUC_UNUSED GtkCellLayout *layout, GtkCellRenderer *renderer, GtkTreeModel *model, GtkTreeIter *iter, G_GNUC_UNUSED gpointer user_data) { gchar *str = ( gchar* ) g_malloc( sizeof( gchar ) * 10 ); gint *num = NULL; gtk_tree_model_get(model, iter, SEMESTRE, num, -1); if( !num ) return; g_snprintf( str, 10, %d, *num ); g_object_set(renderer, text, str, NULL); } static void renderiza_ano(G_GNUC_UNUSED GtkCellLayout *layout, GtkCellRenderer *renderer, GtkTreeModel *model, GtkTreeIter *iter, G_GNUC_UNUSED gpointer user_data) { gchar *str = ( gchar* ) g_malloc( sizeof( gchar ) * 10 ); gint *num = NULL; gtk_tree_model_get(model, iter, ANO, num, -1); if( !num ) return; g_snprintf( str, 10, %d, *num ); g_object_set(renderer, text, str, NULL); } static void renderiza_codigo(G_GNUC_UNUSED GtkCellLayout *layout,
Re: Dynamic changing values at a GtkTreeStore with pointers
On Sun, Mar 25, 2007 at 05:17:50AM -0300, Diogo Ramos wrote: One point that took me most of my time was this thing that I considered a flaw: When we assume that one attribute of a model will be G_TYPE_POINTER and we want to store there a string pointer, we don't pass a pointer, but just a pointer. It is a little confusing to me because I assume that POINTERS are address and address are passed using key. is not some syntactic sugar, we are in C, so it is an *operator*. It produces the address of the thing it is applied to, therefore if you apply it to a pointer, you get a pointer to pointer. Just one thing still bothers me: I noticed that every time I pass my mouse pointer throw the row, the cells renderer functions are called. Is this right or I made something wrong? Everything is working ok, but I am a little concerned about performance. It is a feature. If your cell data functions do something that takes long time -- compared to all the other things that have to be done to render the contents of the cell -- consider some caching mechanism for visible cells. Yeti -- http://gwyddion.net/ ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list
Re: Dynamic changing values at a GtkTreeStore with pointers
On Sun, 25 Mar 2007 05:17:50 -0300 Diogo wrote: It is a little confusing to me because I assume that POINTERS are address and address are passed using key. No, wrong. Addresses are *passed* exactly like any other type is passed. Addresses can be *obtained* using the operator. Hence: char jim; char * fred; fred = jim; Means: jim is a variable of type char fred is a variable of type pointer to char Assign to fred the value obtained by applying the take address of operation on jim. Or make fred point to jim for short. Of course, C++ goes and f*£$s with your understanding by _also_ using to mean pass by reference, where reference *is not* the same as address. This is one of many places where C++ is a very bad OO language. Its use of polymorphism is horribly broken. ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list
Re: Dynamic changing values at a GtkTreeStore with pointers
No, wrong. Addresses are *passed* exactly like any other type is passed. Addresses can be *obtained* using the operator. Hence: char jim; char * fred; fred = jim; Means: jim is a variable of type char fred is a variable of type pointer to char Assign to fred the value obtained by applying the take address of operation on jim. Or make fred point to jim for short. Oh yes, I understand that. What made me crazy is because when I declare this: armazena = gtk_tree_store_new( NUM_COLUNAS_DADOS, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER ); I thought: Ok then, each column will store a pointer address, so, when I need to store one of then, I need to store it with the operator Finally, when I did this, it worked: int *a; gtk_tree_store_set( armazena, inter,0, a, -1); But, when I did this, I got it wrong and took me really long time to figure that it was wrong here: char *a; gtk_tree_store_set( armazena, inter,0, a, -1); The correct would be: gtk_tree_store_set( armazena, inter,0, a, -1); Robert Pearce escreveu: On Sun, 25 Mar 2007 05:17:50 -0300 Diogo wrote: It is a little confusing to me because I assume that POINTERS are address and address are passed using key. No, wrong. Addresses are *passed* exactly like any other type is passed. Addresses can be *obtained* using the operator. Hence: char jim; char * fred; fred = jim; Means: jim is a variable of type char fred is a variable of type pointer to char Assign to fred the value obtained by applying the take address of operation on jim. Or make fred point to jim for short. Of course, C++ goes and f*£$s with your understanding by _also_ using to mean pass by reference, where reference *is not* the same as address. This is one of many places where C++ is a very bad OO language. Its use of polymorphism is horribly broken. ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list -- Diogo F. S. Ramos ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list
Re: Dynamic changing values at a GtkTreeStore with pointers
On Sun, Mar 25, 2007 at 02:15:05PM -0300, Diogo Ramos wrote: Oh yes, I understand that. What made me crazy is because when I declare this: armazena = gtk_tree_store_new(NUM_COLUNAS_DADOS, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER ); I thought: Ok then, each column will store a pointer address, so, when I need to store one of then, I need to store it with the operator And this was correct. If you wanted to store the pointer address, as you write, then you would indeed have to use to obtain it (leaving aside what would that be good for). The point is that you do *not* want to store the address of the pointer (i.e. a pointer to the pointer), but the pointer itself. Now, if that's clear and you only didn't express yourself very clearly, I'm sorry for nitpicking. However if it confused you, you still don't understand (but I cannot explain it better). Yeti -- http://gwyddion.net/ ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list
Dynamic changing values at a GtkTreeStore with pointers
Hello to everybody, This question is cracking my head off. I am days at it and i can't figure it out, although I thing a got pretty close. Here is the deal: I have a GtkTreeStore that I use to show some values. This values can be changed. So, my idea is to store a pointer at a cell so, every time I change the value of my data, the data is changed at the GtkTreeStore throw the pointer. I don't know if it's possible, but I am trying, :-) My last idea was change the GtkCellRendererText using gtk_tree_view_column_set_cell_data_func so, every time the render is called, a function of mine would translate a pointer, which is stored in the tree, to a text. But it didn't work. It wont stop complaining. Here is the menssage: GLib-GObject-WARNING **: unable to set property `text' of type `gchararray' from value of type `gpointer' I think it's because I set G_TYPE_POINTER at the model but the render I am using is for text, although I change the value when It has to be rendered. Someone knows how to accomplish that or, at least, if it is ever possible to do? Thank you, Diogo ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list
Re: Dynamic changing values at a GtkTreeStore with pointers
On Wed, Mar 21, 2007 at 03:01:39PM -0300, Diogo Ramos wrote: This question is cracking my head off. I am days at it and i can't figure it out, although I thing a got pretty close. Here is the deal: I have a GtkTreeStore that I use to show some values. This values can be changed. So, my idea is to store a pointer at a cell so, every time I change the value of my data, the data is changed at the GtkTreeStore throw the pointer. I don't know if it's possible, but I am trying, :-) My last idea was change the GtkCellRendererText using gtk_tree_view_column_set_cell_data_func so, every time the render is called, a function of mine would translate a pointer, which is stored in the tree, to a text. But it didn't work. It wont stop complaining. Here is the menssage: GLib-GObject-WARNING **: unable to set property `text' of type `gchararray' from value of type `gpointer' I think it's because I set G_TYPE_POINTER at the model but the render I am using is for text, although I change the value when It has to be rendered. If I understand what you are trying to achieve (some code would be a better description), you probably call gtk_tree_view_column_add_attribute(column, renderer, text, id); *in addition* to setting up a cell data function. In that case don't. It tells the tree view to attempt to set the text property itself -- which inevitably fails as it doesn't know how to make a string from a pointer. On the other hand, if `translate' means just type-cast, then you should use a G_TYPE_STRING column directly. Anyway, working code using a G_TYPE_POINTER model column with a cell data function to render text in GtkCellRendererText view columns is attached. Yeti -- http://gwyddion.net/ #include gtk/gtk.h static void render_name(G_GNUC_UNUSED GtkCellLayout *layout, GtkCellRenderer *renderer, GtkTreeModel *model, GtkTreeIter *iter, G_GNUC_UNUSED gpointer user_data) { GParamSpec *prop; gtk_tree_model_get(model, iter, 0, prop, -1); g_object_set(renderer, text, prop-name, NULL); } static void render_value_type(G_GNUC_UNUSED GtkCellLayout *layout, GtkCellRenderer *renderer, GtkTreeModel *model, GtkTreeIter *iter, G_GNUC_UNUSED gpointer user_data) { GParamSpec *prop; gtk_tree_model_get(model, iter, 0, prop, -1); g_object_set(renderer, text, g_type_name(prop-value_type), NULL); } static void render_owner_type(G_GNUC_UNUSED GtkCellLayout *layout, GtkCellRenderer *renderer, GtkTreeModel *model, GtkTreeIter *iter, G_GNUC_UNUSED gpointer user_data) { GParamSpec *prop; gtk_tree_model_get(model, iter, 0, prop, -1); g_object_set(renderer, text, g_type_name(prop-owner_type), NULL); } static void add_column(GtkTreeView *treeview, const gchar *title, GtkCellLayoutDataFunc func, gpointer func_data) { GtkTreeViewColumn *column; GtkCellRenderer *renderer; renderer = gtk_cell_renderer_text_new(); g_object_set(renderer, family, Monospace, NULL); column = gtk_tree_view_column_new(); gtk_tree_view_column_set_title(column, title); gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column), renderer, TRUE); gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(column), renderer, func, func_data, NULL); gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column); } int main(int argc, char *argv[]) { GtkWidget *window, *treeview, *scwin; GtkListStore *store; GtkTreeIter iter; GParamSpec **props; GObjectClass *klass; GType type; guint i, n; gtk_init(argc, argv); type = GTK_TYPE_TREE_VIEW; store = gtk_list_store_new(1, G_TYPE_POINTER); klass = G_OBJECT_CLASS(g_type_class_ref(type)); props = g_object_class_list_properties(klass, n); for (i = 0; i n; i++) gtk_list_store_insert_with_values(store, iter, G_MAXINT, 0, props[i], -1); g_free(props); g_type_class_unref(klass); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_default_size(GTK_WINDOW(window), -1, 400); gtk_window_set_title(GTK_WINDOW(window), g_type_name(type)); g_signal_connect(window, destroy, G_CALLBACK(gtk_main_quit), NULL); scwin = gtk_scrolled_window_new(NULL, NULL); gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scwin), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); gtk_container_add(GTK_CONTAINER(window), scwin); treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)); gtk_container_add(GTK_CONTAINER(scwin), treeview); add_column(GTK_TREE_VIEW(treeview), Name, render_name, NULL);
Re: Dynamic changing values at a GtkTreeStore with pointers
...and to the `dynamic changing' part, if you change just something in the data the pointer points to (not the pointer itself), you have to emit row-changed signal on the corresponding row for the tree view to notice. Yeti -- http://gwyddion.net/ ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list