setting check buttons in treeview
- From: Rob Clack <rnc sanger ac uk>
- To: gtk-app-devel-list gnome org
- Subject: setting check buttons in treeview
- Date: Thu, 09 Dec 2004 15:21:29 +0000
I'm using gtk 2.2.4 and Red Hat 9.
I have a variable list of "things" my users can switch on and off, so I
populated a treeview with check buttons and text, stuck that in a
scrolled window and put that in the new window.
This may not be the best way to do what I want, so I'm quite happy to
hear suggestions of alternatives, which is why I'm posting to this
newsgroup. However, assuming I'm headed in the right direction, I now
have a problem.
The list displays fine and I can preset the check buttons to ON or OFF,
and when I click a button my callback gets called and I can detect the
current setting.
The difficulty is setting the button to it's opposite toggle value. ie
if it's ON and I click it, it should be set to OFF. I found a posting
by Bijoy Chandrasekharan dated May 2003 in which he did what I want, thus
gtk_tree_store_set(GTK_TREE_STORE(model), &iter, BUTTON, FALSE);
but when I run this I get gtk errors:
(cols:29538): GLib-GObject-WARNING **: invalid cast from
`GtkTreeModelSort' to `GtkTreeStore'
(cols:29538): Gtk-CRITICAL **: file gtktreestore.c: line 1039
(gtk_tree_store_set): assertion `GTK_IS_TREE_STORE (tree_store)' failed
Anybody know why it works for him and not for me?
I attach a cut-down example of my app.
Thanks in advance!
Rob Clack
=======================================================================
#include <stdio.h>
#include <glib.h>
#include <gtk/gtk.h>
enum { BUTTON, NAME, N_COLUMNS };
/* function prototypes ***************************************************/
static void zMapWindowCreateColumnWindow();
static void addColToTree (char *column,gboolean active,
GtkTreeStore *tree);
static void quitListCB (GtkWidget *window, gpointer data);
static int tree_selection_changed_cb (GtkTreeSelection *selection,
gpointer data);
/* functions *************************************************************/
/** \Brief Displays a list of columns
*
* Each column can be selected as hidden or visible.
*/
static void zMapWindowCreateColumnWindow()
{
GtkWidget *window, *columnList, *scrolledWindow;
GtkTreeModel *model;
GtkCellRenderer *renderer;
GtkTreeViewColumn *buttonColumn, *column;
GtkTreeSelection *select;
GtkTreeStore *tree;
int i;
char *columns[] = {"one", "two", "three", "four"};
tree = gtk_tree_store_new(N_COLUMNS,
G_TYPE_BOOLEAN,
G_TYPE_STRING);
/* set up the top level window */
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_container_border_width(GTK_CONTAINER(window), 5) ;
gtk_window_set_title(GTK_WINDOW(window), "Columns") ;
gtk_window_set_default_size(GTK_WINDOW(window), -1, 600);
scrolledWindow = gtk_scrolled_window_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER(window), scrolledWindow);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledWindow),
GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
/* Build and populate the list of features */
for (i = 0; i < 4; i++)
addColToTree(columns[i], FALSE, tree) ;
model = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL(tree));
columnList = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
/* finished with tree now */
g_object_unref(G_OBJECT(tree));
/* render the columns. */
renderer = gtk_cell_renderer_toggle_new();
buttonColumn = gtk_tree_view_column_new_with_attributes ("Button",
renderer,
"active", BUTTON,
NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (columnList), buttonColumn);
gtk_tree_view_column_set_visible(buttonColumn, TRUE);
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("Text",
renderer,
"text", NAME,
NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (columnList), column);
gtk_tree_view_column_set_visible(column, TRUE);
/* Setup the selection handler */
select = gtk_tree_view_get_selection (GTK_TREE_VIEW (columnList));
gtk_tree_selection_set_mode (select, GTK_SELECTION_SINGLE);
g_signal_connect (G_OBJECT (select), "changed",
G_CALLBACK (tree_selection_changed_cb),
buttonColumn);
g_signal_connect(GTK_OBJECT(window), "destroy",
GTK_SIGNAL_FUNC(quitListCB), columnList);
gtk_container_add(GTK_CONTAINER(scrolledWindow), columnList);
gtk_widget_show_all(window);
return;
}
/***************** Internal functions ************************************/
/** \Brief Load a column into the tree.
*
* Loads the tree with a column's info and a button to hide/show it.
*/
static void addColToTree(char *column, gboolean active, GtkTreeStore *tree)
{
GtkTreeIter iter;
gtk_tree_store_append(GTK_TREE_STORE(tree), &iter, NULL);
gtk_tree_store_set (GTK_TREE_STORE(tree), &iter,
BUTTON, active,
NAME, column,
-1 );
return;
}
static int tree_selection_changed_cb (GtkTreeSelection *selection,
gpointer data)
{
GtkTreeIter iter;
GtkTreeModel *model;
gchar *name;
gboolean active;
if (gtk_tree_selection_get_selected (selection, &model, &iter))
{
gtk_tree_model_get (model, &iter, BUTTON, &active, NAME, &name, -1);
}
if (active)
gtk_tree_store_set(GTK_TREE_STORE(model), &iter, BUTTON, FALSE);
else
gtk_tree_store_set(GTK_TREE_STORE(model), &iter, BUTTON, TRUE);
if (name) g_free(name);
return TRUE;
}
static void quitListCB(GtkWidget *window, gpointer data)
{
gtk_widget_destroy(GTK_WIDGET(window));
gtk_main_quit();
return;
}
int main( int argc,
char *argv[] )
{
gtk_init (&argc, &argv);
zMapWindowCreateColumnWindow();
gtk_main ();
return 0;
}
/*************************** end of file *********************************/
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The mouth: natural home of the foot
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Rob Clack Acedb Development, Informatics Group
email: rnc sanger ac uk Wellcome Trust Sanger Institute
Tel: +44 1223 494883 Wellcome Trust Genome Campus
Fax: +44 1223 494919 Hinxton Cambridge CB10 1SA
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]