gtk_combo_box_new_with_model_and_entry with gtk_cell_layout_set_cell_data_func
- From: Pierre-Louis Bonicoli <pierre-louis bonicoli gmx fr>
- To: gtk-app-devel-list gnome org
- Subject: gtk_combo_box_new_with_model_and_entry with gtk_cell_layout_set_cell_data_func
- Date: Thu, 08 Sep 2011 02:09:45 +0200
Hi !
I try to use gtk_cell_layout_set_cell_data_func with a combobox which
has an entry.
gtk_cell_layout_set_cell_data_func method allows to map an item in my
model with an entry in the combobox. How to map an index of an entry
with an item in the model ? It seems that using
gtk_combo_box_set_entry_text_column is the right way to do that but my
model doesn't contain a G_TYPE_STRING columns (entries in the combobox
are dynamically computed with a GtkCellLayoutDataFunc which modify
"text" property of the CellRenderer).
So when an entry in the combobox is clicked, entry doesn't become
selected and a warning message is displayed:
"GLib-GObject-WARNING **: unable to set property `text' of type
`gchararray' from value of type `glong'"
Here is complete test code (available here too:
http://pastebin.drouet.eu/pastebin.php?show=83 )
------------------------------------------------
#include <stdlib.h>
#include <gtk/gtk.h>
/*
Build with the following Makefile
all: test
test: test.c
gcc `pkg-config --libs --cflags gtk+-2.0` test.c -o test
*/
typedef struct _wrapper {
char * text;
} Wrapper;
static void
wrapper2text(GtkCellLayout *cell_layout, GtkCellRenderer *cell,
GtkTreeModel *model, GtkTreeIter *iter, gpointer data) {
Wrapper * w;
gtk_tree_model_get (model, iter, 0, &w, -1);
g_object_set (cell, "text", w->text, NULL);
}
static void
add_text(GtkListStore *model, char *text) {
GtkTreeIter new_row;
Wrapper * w;
w = g_new0(Wrapper, 1);
w->text = g_strdup(text);
gtk_list_store_append(model, &new_row);
gtk_list_store_set(model, &new_row, 0, w, -1);
}
int main(int argc, char **argv)
{
GtkWidget * window = NULL;
GtkListStore * store;
GtkListStore * model;
GType *columns;
GtkWidget * combo;
GtkCellRenderer *renderer;;
gtk_init(&argc, &argv);
columns = g_new0 (GType, 1);
columns[0] = G_TYPE_POINTER;
model = gtk_list_store_newv(1, columns);
add_text(model, "test1");
add_text(model, "test2");
add_text(model, "test3");
g_free(columns);
combo = gtk_combo_box_new_with_model_and_entry(GTK_TREE_MODEL(model));
renderer = gtk_cell_renderer_text_new();
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, TRUE);
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT(combo), renderer, NULL);
gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(combo), renderer,
wrapper2text, NULL, NULL);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_container_add(GTK_CONTAINER(window), combo);
gtk_widget_show_all(window);
gtk_main();
return EXIT_SUCCESS;
}
------------------------------------------------
Is there a way to avoid calling gtk_combo_box_set_entry_text_column
(which implies having one useless column of type G_TYPE_STRING in the
model) ?
Using
gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(combo), renderer,
wrapper2text, combo, NULL);
and
g_object_set (gtk_bin_get_child(GTK_COMBO_BOX(data)), "text", w->text,
NULL);
in wrapper2text function works but the warning remains.
Thanks,
Pierre-Louis
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]