GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sun Aug 28, 2016 8:36 pm

All times are UTC

Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Check box selection on GTK treeview colum
PostPosted: Thu Aug 04, 2011 1:56 pm 

Joined: Thu Aug 04, 2011 11:04 am
Posts: 2
Dear all,

I am a new-bee for GTK, I am developing an gui front end for my project I have 3 columns and one column has selection checkbox used to select one of 3 check box. Presently all are manually eigther selected or de-selected.. I am unbale to make them mutually exclussive.

Can any one of you help me.. give me some example code.

Manjunath AM

 Post subject: Re: Check box selection on GTK treeview colum
PostPosted: Sat Aug 06, 2011 10:46 pm 
Familiar Face

Joined: Sun Jul 31, 2011 9:42 pm
Posts: 7
Location: Italy
Hi, maybe this kind of posts must stay in "GTK+ Programming and Discussion" forum.


If I understood your need correctly, I think you should use radio buttons instead of check boxes.
Radio buttons of the same group are mutually exclusive.
Code could be something like this (I didn't test it):

GSList *group = NULL;

GtkWidget *radio_button1 = gtk_radio_button_new_with_mnemonic (NULL, "Choice 1"));
gtk_radio_button_set_group (GTK_RADIO_BUTTON (radio_button1), group);
group = g_slist_append (group, radio_button1);

GtkWidget *radio_button2 = gtk_radio_button_new_with_mnemonic (NULL, "Choice 2"));
gtk_radio_button_set_group (GTK_RADIO_BUTTON (radio_button2), group);
group = g_slist_append (group, radio_button2);

GtkWidget *radio_button3 = gtk_radio_button_new_with_mnemonic (NULL, "Choice 3"));
gtk_radio_button_set_group (GTK_RADIO_BUTTON (radio_button3), group);
group = g_slist_append (group, radio_button3);

gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radio_button1), TRUE);

 Post subject: Re: Check box selection on GTK treeview colum
PostPosted: Mon Aug 08, 2011 2:04 pm 

Joined: Thu Aug 04, 2011 11:04 am
Posts: 2
Thanks a lot for your reply, even I was thinking so but I could not get any clue on how to add radio button widget to the column cells.
My code is like this and for this how I can add radio button. I mean how can I add a radio button widget to the selected column.
Please guide me to resolve this.

Thanks a lot in advance.

Manjunath AM
#include <gtk/gtk.h>

  GtkTreeStore *company;

/* gpio_t data structure */
typedef struct _gpio_t {
  gchar *path;
  gchar *gpio_name;
  gchar *gpio_alternate;
  gboolean on_select;
  gchar *col_title;
  gint room_no; /* -1 if gpio_t is off-site */
} gpio_t;

/* gpio_t data */
gpio_t staff[] = {
  { "0", "GPIO1", "GPIO", TRUE, "INPUT", 0 },
  { "0:0", "GPIO1", "GPIO", FALSE, "OUTPUT", 0 },
  { "1", "GPIO2", "GPIO", TRUE, "INPUT", 1 },
  { "1:0", "GPIO2", "GPIO", FALSE, "OUTPUT", 1 },
  { "2", "I2C", "SCL", TRUE, "SCL", 2 },
  { "2:0", "I2C", "SCL", FALSE, "GPIO", 2 },
  { "3", "I2C", "SDA", TRUE, "SDA", 3 },
  { "3:0", "I2C", "SDA", FALSE, "GPIO", 3 },

//The view columns use the following column identifiers. The path (in the preceding code) is not in the column; the organization will appear under the title column.

/* column identifiers in tree view */
enum {

void        cell_edited_callback (GtkCellRendererText *cell,
                                  gchar               *path_string,
                                  gchar               *new_text,
                                  gpointer             user_data)
   g_print ("Do something!\n");
    /* Print a string when a menu item is selected */

    static void menuitem_response( gchar *string )
   GtkTreePath *path;
        GtkTreeIter iter;
   printf ("%s\n", string);

/* Modify a particular row */
   path = gtk_tree_path_new_from_string (string);
   gtk_tree_model_get_iter (GTK_TREE_MODEL (company),
   gtk_tree_path_free (path);
   gtk_tree_store_set (company, &iter,
                SELECT_FUN_COL, TRUE,

  view_popup_menu (GtkWidget *treeview, GdkEventButton *event, gpointer userdata)
    GtkWidget *menu, *menu_items;
    char buf[128];
    int i;

    menu = gtk_menu_new();

   for (i = 0; i < 3; i++)
                /* Copy the names to the buf. */
                //sprintf (buf, "GPIO_INPUT - %d", i);
      if (i==0)
         sprintf (buf, "GPIO_INPUT");
      if (i==1)
         sprintf (buf, "GPIO_OUTPUT");
      if (i==2)
         sprintf (buf, "NOT SUPPORTED");

                /* Create a new menu-item with a name... */
                menu_items = gtk_menu_item_new_with_label (buf);

                /* ...and add it to the menu. */
                gtk_menu_append (GTK_MENU (menu), menu_items);

                /* Do something interesting when the menuitem is selected */
                gtk_signal_connect_object (GTK_OBJECT (menu_items), "activate",
                    GTK_SIGNAL_FUNC (menuitem_response), (gpointer) g_strdup (buf));

                /* Show the widget */
                gtk_widget_show (menu_items);

    /* Note: event can be NULL here when called from view_onPopupMenu;
     *  gdk_event_get_time() accepts a NULL argument */
    gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
                   (event != NULL) ? event->button : 0,

  view_onButtonPressed (GtkWidget *treeview, GdkEventButton *event, gpointer userdata)
    /* single click with the right mouse button? */
    if (event->type == GDK_BUTTON_PRESS  &&  event->button == 3)
      g_print ("Single right click on the tree view.\n");

      if (1)
        GtkTreeSelection *selection;

        selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));

        /* Note: gtk_tree_selection_count_selected_rows() does not
         *   exist in gtk+-2.0, only in gtk+ >= v2.2 ! */
        if (gtk_tree_selection_count_selected_rows(selection)  <= 1)
           GtkTreePath *path;

           /* Get tree path for row that was clicked */
           if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(treeview),
                                             (gint) event->x,
                                             (gint) event->y,
                                             &path, NULL, NULL, NULL))
             gtk_tree_selection_select_path(selection, path);
      } /* end of optional bit */

      view_popup_menu(treeview, event, userdata);

      return TRUE; /* we handled this */

    return FALSE; /* we did not handle this */

  view_onPopupMenu (GtkWidget *treeview, gpointer userdata)
    view_popup_menu(treeview, NULL, userdata);

    return TRUE; /* we handled this */

/* verify that path is a valid node in tree */
gboolean node_exists(GtkTreeStore *tree, GtkTreePath *path)
  GtkTreeIter iter;

  return(gtk_tree_model_get_iter(GTK_TREE_MODEL(tree), &iter, path));

/* inserts a new node at path in tree */
void node_insert(GtkTreeStore *tree, GtkTreePath *path)
  gint depth;
  gint *indices;
  gint index;
  GtkTreeIter iter;

  /* determine depth and last index of path */
  depth = gtk_tree_path_get_depth(path);
  indices = gtk_tree_path_get_indices(path);
  index = indices[depth-1];

  if (!node_exists(tree, path))
    if (depth == 1)
    {  /* if this is a child of the root node, use NULL instead of iter */
       while (!(gtk_tree_model_iter_n_children(GTK_TREE_MODEL(tree), NULL)
               == (index+1)))
         gtk_tree_store_append(tree, &iter, NULL);
    } else {
       GtkTreePath *parent_path;
       GtkTreeIter parent;

       /* determine parent node, creating parent if it does not exist */
       parent_path = gtk_tree_path_copy(path);
       if (!node_exists(tree, parent_path))
         node_insert(tree, parent_path);
       /* append new nodes up to index-th child of parent */
       gtk_tree_model_get_iter(GTK_TREE_MODEL(tree), &parent, parent_path);
                GTK_TREE_MODEL(tree), &parent) == (index+1)))
        gtk_tree_store_append(tree, &iter, &parent);


/* read the room number column, and if the number is -1, send a dash (-)
   to the renderer instead */
void room_convert(GtkTreeViewColumn *column,
                  GtkCellRenderer *renderer,
                  GtkTreeModel *model,
                  GtkTreeIter *iter,
                  gpointer data)
  gint num;
  gchar *str;

  gtk_tree_model_get(model, iter, SR_NO_COL, &num, -1);

  if (num == -1)
    str = g_strdup(" - ");
  } else {
    str = g_strdup_printf("%d", num);

  g_object_set(renderer, "text", str, NULL);

static void
cb_toggled( GtkCellRendererToggle *cell,
         gchar                 *path,
         GtkTreeModel          *model )
   GtkTreeIter iter;
   gboolean    active;

   g_object_get( G_OBJECT( cell ), "active", &active, NULL );

   gtk_tree_model_get_iter_from_string( model, &iter, path );

   gtk_tree_store_set( company, &iter,
                  SELECT_COL, ! active, -1 );


int main(int argc, char **argv)
  GtkWindow *window;

  gpio_t *person;
  GtkTreePath *path;
  GtkTreeView *view;
  GtkScrolledWindow *scroller;

  GtkCellRenderer *SoDim_renderer, *text_renderer,
                  *bold_renderer, *check_renderer;

  GtkTreeViewColumn *Sr_no_col, *Select_func_col, *Func_name_col,
                    *Description_col, *Selection_col;

  /* initialize GTK+, create main window */
  gtk_init(&argc, &argv);
  window = g_object_new(GTK_TYPE_WINDOW,
                        "title", "GPIO_CONFIG_TOOL.",
                        "default-width", 600,
                        "default-height", 400,

/* create tree model */
  company = gtk_tree_store_new(NUM_COLS,

  /* point at start of personnel data */
  person = staff;
  /* fill model with personnel data */
  while (person->path != NULL)
    GtkTreeIter iter;

    path = gtk_tree_path_new_from_string(person->path);
    node_insert(company, path);
    gtk_tree_model_get_iter(GTK_TREE_MODEL(company), &iter, path);
    gtk_tree_store_set(company, &iter,
                       MAIN_FUNC_COL, person->gpio_name,
                       DESC_COL, person->gpio_alternate,
                       SELECT_COL, person->on_select,
                       SELECT_FUN_COL, person->col_title,
                       SR_NO_COL, person->room_no,
/* create a right-justified renderer for room numbers */
  SoDim_renderer = gtk_cell_renderer_text_new();
  g_object_set(SoDim_renderer, "xalign", 0.1, NULL);

  /* a normal text renderer */
  text_renderer = gtk_cell_renderer_text_new();

  /* a renderer for text in boldface (for last name column) */
  bold_renderer = gtk_cell_renderer_text_new();
  g_object_set(bold_renderer, "weight", 500, NULL);

  /* a check box renderer */
  check_renderer = gtk_cell_renderer_toggle_new();

  /* create view columns */
  Sr_no_col = gtk_tree_view_column_new_with_attributes(
                   "SoDimNo", SoDim_renderer,

  gtk_tree_view_column_set_cell_data_func(Sr_no_col, SoDim_renderer,
                                          NULL, NULL);

//The title is the only column that the user can move. The rest of the columns are relatively mundane:

  Select_func_col = gtk_tree_view_column_new_with_attributes(
               "Select Function",

  /* allow user to move this column around */

  g_object_set(Select_func_col, "reorderable", TRUE, NULL);

  Func_name_col = gtk_tree_view_column_new_with_attributes(
                     "Descreption", bold_renderer,
                     "text", MAIN_FUNC_COL,

  Description_col = gtk_tree_view_column_new_with_attributes(
                      "Main Function", text_renderer,
                      "text", DESC_COL,

  g_signal_connect( G_OBJECT( check_renderer ), "toggled",
                 G_CALLBACK( cb_toggled ), GTK_TREE_MODEL( company ) );

  Selection_col = gtk_tree_view_column_new_with_attributes(
                   "Select", check_renderer,
                   "active", SELECT_COL,

/* create overall view */
  view = g_object_new(GTK_TYPE_TREE_VIEW,
                      "model", company,
                      "rules-hint", TRUE,
                      "enable-search", TRUE,
                      "search-column", MAIN_FUNC_COL,

  g_signal_connect(view, "button-press-event", (GCallback) view_onButtonPressed, NULL);
  g_signal_connect(view, "popup-menu", (GCallback) view_onPopupMenu, NULL);

  /* put all columns into the view */
  gtk_tree_view_append_column(view, Sr_no_col);
  gtk_tree_view_append_column(view, Func_name_col);
  gtk_tree_view_append_column(view, Description_col);
  gtk_tree_view_append_column(view, Select_func_col);
  gtk_tree_view_append_column(view, Selection_col);

/* set drop arrow at front of the title column */
  g_object_set(view, "expander-column", Select_func_col, NULL);

/* put everything into a scrolled window */
  scroller = g_object_new(GTK_TYPE_SCROLLED_WINDOW, NULL);
  gtk_container_add(GTK_CONTAINER(scroller), GTK_WIDGET(view));
  gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(scroller));

  /* show everything and start GTK+ main loop */
  return 0;

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

All times are UTC

Who is online

Users browsing this forum: No registered users and 2 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