GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Aug 27, 2016 1:23 pm

All times are UTC

Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: GtkTreeView with row selection and pointers to row functions
PostPosted: Sun Apr 13, 2008 8:24 pm 
Familiar Face

Joined: Tue Mar 25, 2008 9:41 pm
Posts: 7
[code=]#include <stdlib.h>
#include <gtk/gtk.h>

typedef void (* my_function) (void *data);

static void function_fruit (void *data)
printf ("function Fruit with data %p\n", data);

static void function_strawberry (void *data)
printf ("function Strawberry with data %p\n", data);

static void function_cherry (void *data)
printf ("function Cherry with data %p\n", data);

static void single_clicked (GtkWidget *widget, void *data)
GtkTreeSelection *selection;
GtkTreeModel *model;
GtkTreeIter iter;
char *name;
my_function function;

selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
if (gtk_tree_selection_get_selected (selection, &model, &iter))
gtk_tree_model_get (model, &iter, 0, &name, -1);
printf ("row %s with data: %p, ", name, data);
free (name);

gtk_tree_model_get (model, &iter, 1, &function, -1);
(* function) (data);


static GtkTreeIter static_row (GtkTreeStore *store,
GtkTreeIter *parent, char *label, my_function function)
GtkTreeIter iter;

gtk_tree_store_append (store, &iter, parent);
gtk_tree_store_set (store, &iter, 0, label, 1, function, -1);

return iter;

static void static_store (GtkWidget *treeview)
GtkTreeStore *store;
GtkTreeIter iter;

store = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);

iter = static_row (store, NULL, "Fruit", function_fruit);
static_row (store, &iter, "Strawberry", function_strawberry);
static_row (store, &iter, "Cherry", function_cherry);

gtk_tree_view_set_model (GTK_TREE_VIEW (treeview), GTK_TREE_MODEL (store));
g_object_unref (store);

int main (int argc, char **argv)
GtkWidget *window;
GtkWidget *treeview;
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
GtkTreeSelection *selection;

gtk_init (&argc, &argv);

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size (GTK_WINDOW (window), 280, 250);

treeview = gtk_tree_view_new ();
gtk_container_add (GTK_CONTAINER (window), treeview);
gtk_widget_set_size_request (treeview, 400, 400);
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview));
gtk_tree_selection_set_mode (selection, GTK_SELECTION_NONE);
g_signal_connect (GTK_OBJECT (treeview), "cursor_changed",
G_CALLBACK (single_clicked), window);
gtk_widget_show (treeview);

renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("Fruit", renderer, "text", 0, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (treeview), FALSE);

static_store (treeview);

gtk_widget_show_all (window);
gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);

gtk_main ();
return 0;

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC

Who is online

Users browsing this forum: No registered users and 4 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group