Re: Dynamic changing values at a GtkTreeStore with pointers
- From: Diogo Ramos <diogofsr gmail com>
- To: gtk-list gnome org
- Subject: Re: Dynamic changing values at a GtkTreeStore with pointers
- Date: Sun, 25 Mar 2007 05:17:50 -0300
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,
GtkCellRenderer *renderer,
GtkTreeModel *model,
GtkTreeIter *iter,
G_GNUC_UNUSED gpointer user_data)
{
gchar *str = NULL;
gtk_tree_model_get(model, iter, CODIGO, &str, -1);
if( !str ) g_object_set(renderer, "text", "", NULL);
g_object_set(renderer, "text", str, NULL);
}
static void
renderiza_nome(G_GNUC_UNUSED GtkCellLayout *layout,
GtkCellRenderer *renderer,
GtkTreeModel *model,
GtkTreeIter *iter,
G_GNUC_UNUSED gpointer user_data)
{
gchar *str = NULL;
gtk_tree_model_get(model, iter, NOME, &str, -1);
if( !str ) g_object_set(renderer, "text", "", NULL);
g_object_set(renderer, "text", str, NULL);
}
So, thats it.
Again, thank you very much for your reply.
David Nečas (Yeti) escreveu:
> 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/
--
Diogo F. S. Ramos
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]