2006-07-26

GtkTreeView, GtkListStore basics

enum
  {
    COLUMN_ID,
    N_COLUMNS
  };

GtkListStore *store;
GtkTreeIter iter;
GtkWidget *view;
GtkCellRenderer *renderer;
GtkTreeSelection *selection;


store =gtk_list_store_new( N_COLUMNS, G_TYPE_INT);

// gtk_list_store_clear( store);

gtk_list_store_append( store, &iter);
gtk_list_store_set( store, &iter,
      COLUMN_ID, ( guint) some_int_value,
      -1);


/* ... */


view =gtk_tree_view_new_with_model( GTK_TREE_MODEL( store));

renderer =gtk_cell_renderer_text_new( );

gtk_tree_view_insert_column_with_attributes( GTK_TREE_VIEW( view),
          -1,
          "Id",
          renderer,
          "text",
          COLUMN_ID,
          NULL);

selection = gtk_tree_view_get_selection( GTK_TREE_VIEW( view));

gtk_tree_selection_set_mode( selection, GTK_SELECTION_SINGLE);

g_signal_connect( G_OBJECT( selection), "changed",
    G_CALLBACK( selection_changed_cb),
    NULL);


/* ... */


static void selection_changed_cb( GtkTreeSelection *selection, gpointer data)
{
  GtkTreeIter iter;
  GtkTreeModel *model;
  int id;

    if( gtk_tree_selection_get_selected( selection, &model, &iter))
    {
      gtk_tree_model_get( model, &iter, COLUMN_ID, &id, -1);
      /* do something w/id... */
    }
}