I made two examples which modifying a cell inside of a GtkTreeView.
For the first example, I'm using a GtkTreeModel with two columns (G_TYPE_STRING and G_TYPE_UINT) and the rendering is direct.
For the second example, I'm using a struct which have the two data (I convert the uint to string to ease the second example). The GtkTreeModel have only one column, the pointer of the structure and showing the data using gtk_tree_view_insert_column_with_data_func.
When I'm trying to modify the first column in the first example, the column is auto-sizing.
When I'm trying to modify the first column in the second example, the column is not auto-sizing.
My question is : Is it on purpose or not ???
Thanks (and sorry for my not really good English),
The two examples are under. You just have to press the button.
Example 1 :
#include <gtk/gtk.h>
enum
{
COL_NAME = 0,
COL_AGE,
NUM_COLS
} ;
static GtkTreeModel *
create_and_fill_model (void)
{
GtkListStore *store;
GtkTreeIter iter;
store = gtk_list_store_new (NUM_COLS, G_TYPE_STRING, G_TYPE_UINT);
/* Append a row and fill in some data */
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter,
COL_NAME, "Heinz El-Mann",
COL_AGE, 51,
-1);
/* append another row and fill in some data */
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter,
COL_NAME, "Jane Doe",
COL_AGE, 23,
-1);
/* ... and a third row */
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter,
COL_NAME, "Joe Bungop",
COL_AGE, 91,
-1);
return GTK_TREE_MODEL (store);
}
static GtkWidget *
create_view_and_model (void)
{
GtkCellRenderer *renderer;
GtkTreeModel *model;
GtkWidget *view;
view = gtk_tree_view_new ();
/* --- Column #1 --- */
renderer = gtk_cell_renderer_text_new ();
gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (view),
-1,
"Name",
renderer,
"text", COL_NAME,
NULL);
/* --- Column #2 --- */
renderer = gtk_cell_renderer_text_new ();
gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (view),
-1,
"Age",
renderer,
"text", COL_AGE,
NULL);
model = create_and_fill_model ();
gtk_tree_view_set_model (GTK_TREE_VIEW (view), model);
/* The tree view has acquired its own reference to the
* model, so we can drop ours. That way the model will
* be freed automatically when the tree view is destroyed */
g_object_unref (model);
return view;
}
void button_clicked(GtkButton *button, GtkTreeView *user_data)
{
GtkTreeModel *model = gtk_tree_view_get_model (user_data);
GtkTreeIter iter;
GtkTreeViewColumn *column = gtk_tree_view_get_column (user_data, 0);
gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
gtk_tree_view_column_set_fixed_width (column, 100);
gtk_tree_model_get_iter_first (model, &iter);
gtk_list_store_set (GTK_LIST_STORE(model), &iter, 0, "A very very very very long text", -1);
return;
}
int
main (int argc, char **argv)
{
GtkWidget *window;
GtkWidget *view;
GtkWidget *grid;
GtkWidget *button;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (window, "delete_event", gtk_main_quit, NULL); /* dirty */
view = create_view_and_model ();
grid = gtk_grid_new ();
gtk_grid_attach (GTK_GRID (grid), view, 0, 0, 1, 1);
button = gtk_button_new_with_label ("Modify");
gtk_grid_attach (GTK_GRID (grid), button, 0, 1, 1, 1);
g_signal_connect (button, "clicked", (GCallback)button_clicked, view);
gtk_container_add (GTK_CONTAINER (window), grid);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
Example 2 :
#include <gtk/gtk.h>
typedef struct
{
char *name;
char *age;
} Person;
static GtkTreeModel *
create_and_fill_model (void)
{
GtkListStore *store;
GtkTreeIter iter;
Person *person;
store = gtk_list_store_new (1, G_TYPE_POINTER);
/* Append a row and fill in some data */
gtk_list_store_append (store, &iter);
person = malloc(sizeof(Person));
person->name = "Heinz El-Mann";
person->age = "51";
gtk_list_store_set (store, &iter,
0, person,
-1);
/* append another row and fill in some data */
gtk_list_store_append (store, &iter);
person = malloc(sizeof(Person));
person->name = "Jane Doe";
person->age = "23";
gtk_list_store_set (store, &iter,
0, person,
-1);
/* ... and a third row */
gtk_list_store_append (store, &iter);
person = malloc(sizeof(Person));
person->name = "Joe Bungop";
person->age = "91";
gtk_list_store_set (store, &iter,
0, person,
-1);
return GTK_TREE_MODEL (store);
}
void show_name(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell, GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
{
Person *pers;
gtk_tree_model_get (tree_model, iter, 0, &pers, -1);
g_object_set (G_OBJECT(cell), "text", pers->name, NULL);
}
void show_age(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell, GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
{
Person *pers;
gtk_tree_model_get (tree_model, iter, 0, &pers, -1);
g_object_set (G_OBJECT(cell), "text", pers->age, NULL);
}
static GtkWidget *
create_view_and_model (void)
{
GtkCellRenderer *renderer;
GtkTreeModel *model;
GtkWidget *view;
view = gtk_tree_view_new ();
/* --- Column #1 --- */
renderer = gtk_cell_renderer_text_new ();
gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (view),
-1,
"Name",
renderer,
show_name,
NULL,
NULL);
/* --- Column #2 --- */
renderer = gtk_cell_renderer_text_new ();
gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (view),
-1,
"Age",
renderer,
show_age,
NULL,
NULL);
model = create_and_fill_model ();
gtk_tree_view_set_model (GTK_TREE_VIEW (view), model);
/* The tree view has acquired its own reference to the
* model, so we can drop ours. That way the model will
* be freed automatically when the tree view is destroyed */
g_object_unref (model);
return view;
}
void button_clicked(GtkButton *button, GtkTreeView *user_data)
{
GtkTreeModel *model = gtk_tree_view_get_model (user_data);
GtkTreeIter iter;
GtkTreeViewColumn *column = gtk_tree_view_get_column (user_data, 0);
Person *pers;
gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
gtk_tree_view_column_set_fixed_width (column, 100);
gtk_tree_model_get_iter_first (model, &iter);
gtk_tree_model_get (model, &iter, 0, &pers, -1);
pers->name = "A very very very very long text";
return;
}
int
main (int argc, char **argv)
{
GtkWidget *window;
GtkWidget *view;
GtkWidget *grid;
GtkWidget *button;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (window, "delete_event", gtk_main_quit, NULL); /* dirty */
view = create_view_and_model ();
grid = gtk_grid_new ();
gtk_grid_attach (GTK_GRID (grid), view, 0, 0, 1, 1);
button = gtk_button_new_with_label ("Modify");
gtk_grid_attach (GTK_GRID (grid), button, 0, 1, 1, 1);
g_signal_connect (button, "clicked", (GCallback)button_clicked, view);
gtk_container_add (GTK_CONTAINER (window), grid);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}