GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Aug 01, 2014 11:51 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: [SOLVED] Multiple CellRenderers in one column
PostPosted: Wed Aug 01, 2012 4:00 pm 
Offline

Joined: Wed Aug 01, 2012 3:42 pm
Posts: 2
Hi there!

Can someone give me an idea how to make TreeView where one of the columns will be rendered by CellRendererText OR CellRendererToggle (but not both!)
Maybe there is a signal or some callbact where I can plug-in and choose proper CellRenderer?

My code is actually in GTK# but any input will be appreciated!


Last edited by sajimon5544 on Fri Aug 03, 2012 6:48 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Multiple CellRenderers in one column
PostPosted: Thu Aug 02, 2012 12:50 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hello and welcome to the GTK+ forums!

This can be done by simply packing both cell renderers to the column and then selectively hiding one of them. There are different approaches to this selective hiding and the best one will depend on type of application being written. I wrote a simple implementation below that uses separate columns to hold visibility flags.

Code:
#include <gtk/gtk.h>

enum
{
  C_TEXT,
  C_TEXT_VIS,
  C_CHECK,
  C_CHECK_VIS,
  NO_COLS
};

static void
fill_treeview (GtkTreeView *view)
{
  GtkListStore *store;
  GtkCellRenderer *cell;
  GtkTreeViewColumn *col;

  char string[] = "Line 0";
  int i;


  store = gtk_list_store_new (NO_COLS, G_TYPE_STRING, G_TYPE_BOOLEAN,
                              G_TYPE_BOOLEAN, G_TYPE_BOOLEAN);
  for (i = 0; i < 10; i++)
    {
      GtkTreeIter iter;

      gtk_list_store_append (store, &iter);

      if (i % 2)
        {
          g_print ("%s\n", string);
          string[5] = i + '0';
          gtk_list_store_set (store, &iter,
                              C_TEXT, string,
                              C_TEXT_VIS, TRUE,
                              C_CHECK_VIS, FALSE,
                              -1);
        }
      else
        {
          gtk_list_store_set (store, &iter,
                              C_CHECK, !(i % 4),
                              C_CHECK_VIS, TRUE,
                              C_TEXT_VIS, FALSE,
                              -1);
        }
    }

  col = gtk_tree_view_column_new ();

  cell = gtk_cell_renderer_text_new ();
  gtk_tree_view_column_pack_start (col, cell, TRUE);
  gtk_tree_view_column_set_attributes (col, cell,
                                       "text", C_TEXT,
                                       "visible", C_TEXT_VIS,
                                       NULL);

  cell = gtk_cell_renderer_toggle_new ();
  gtk_tree_view_column_pack_start (col, cell, TRUE);
  gtk_tree_view_column_set_attributes (col, cell,
                                       "active", C_CHECK,
                                       "visible", C_CHECK_VIS,
                                       NULL);

  gtk_tree_view_append_column (view, col);
  gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
  g_object_unref (store);
}

int
main (int    argc,
      char **argv)
{
  GtkWidget *window,
            *treeview;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (window, "destroy", gtk_main_quit, NULL);

  treeview = gtk_tree_view_new();
  gtk_container_add (GTK_CONTAINER (window), treeview);

  fill_treeview (GTK_TREE_VIEW (treeview));

  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}


Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: Multiple CellRenderers in one column
PostPosted: Fri Aug 03, 2012 12:41 pm 
Offline

Joined: Wed Aug 01, 2012 3:42 pm
Posts: 2
Thank You very much for Your effort, I wasn't expecting whole code:)
I get a principle now, just have port it to gtk#, shouldn't be too hard.

Cheers.

EDIT
Port'd!


Top
 Profile  
 
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: Google [Bot] and 1 guest


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