Gergely Polonkai
aa71f8211d
This is the latest version found at http://scentric.net/tutorial/treeview-tutorial-xml.tar.gz
106 lines
2.6 KiB
C
106 lines
2.6 KiB
C
#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)
|
|
{
|
|
GtkTreeViewColumn *col;
|
|
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 --- */
|
|
|
|
col = gtk_tree_view_column_new();
|
|
|
|
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);
|
|
|
|
g_object_unref (model); /* destroy model automatically with view */
|
|
|
|
return view;
|
|
}
|
|
|
|
|
|
int
|
|
main (int argc, char **argv)
|
|
{
|
|
GtkWidget *window;
|
|
GtkWidget *view;
|
|
|
|
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 ();
|
|
|
|
gtk_container_add (GTK_CONTAINER (window), view);
|
|
|
|
gtk_widget_show_all (window);
|
|
|
|
gtk_main ();
|
|
|
|
return 0;
|
|
}
|