GtkCellRenderer and stock images



Hello, I'm trying to use a GtkCellRenderer of type
gtk_cell_renderer_pixbuf_new to render stock images (GTK_STOCK_...).
The code I have so far only segfaults, I've copied code off google
codesearch from gnomescan and they set the pixbuf's attribute to
"stock-id" then when storing elements in a ListStore they simply add
(GTK_STOCK_...) instead of using a GdkPixbuf.

Here is my code, I've kept it short so it could fit into the body of
an email nicely.
I think everything else works fine, remove line "COL_ICON,
GTK_STOCK_HOME," to see that the treeview works properly
Thank you

#include <gtk/gtk.h>

enum {
       COL_ICON = 0, COL_TEXT, NUM_COLS
};

GtkListStore *create_liststore(void) {
       GtkListStore  *store;
       GtkTreeIter    iter;

       store = gtk_list_store_new(2, GDK_TYPE_PIXBUF, G_TYPE_STRING);

       gtk_list_store_append(store, &iter);
       gtk_list_store_set(store, &iter,
                       COL_ICON, GTK_STOCK_HOME,
                       COL_TEXT, "Home",
                       -1);

       return store;
}

GtkWidget *create_treeview(void) {
       GtkTreeModel      *model;
       GtkTreeViewColumn *col;
       GtkCellRenderer   *renderer;
       GtkWidget         *view;

       model = GTK_TREE_MODEL(create_liststore());

       view = gtk_tree_view_new_with_model(model);

       col = gtk_tree_view_column_new();
       gtk_tree_view_column_set_title(col, "Column #1");

       renderer = gtk_cell_renderer_pixbuf_new();
       gtk_tree_view_column_pack_start(col, renderer, FALSE);
       gtk_tree_view_column_add_attribute(col, renderer, "stock-id", COL_ICON);
       gtk_cell_renderer_set_fixed_size (GTK_CELL_RENDERER (renderer), 16, 16);

       renderer = gtk_cell_renderer_text_new();
       gtk_tree_view_column_pack_start(col, renderer, TRUE);
       gtk_tree_view_column_set_attributes(col, renderer,
                       "text", COL_TEXT,
                       NULL);

       gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);


       return view;
}

gint main (gint argc, gchar **argv) {
       GtkWidget *mainwindow, *vbox;

       gtk_init(&argc, &argv);

       mainwindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
       g_signal_connect(mainwindow, "delete_event", gtk_main_quit, NULL);
       gtk_container_add(GTK_CONTAINER(mainwindow), create_treeview());
       gtk_widget_show_all(mainwindow);
       gtk_main();

       return 0;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]