Synopsis

This is a simple demo program showing how to set and unset the background color on individual rows in a treeview, so that you can, for example, highlight certain entries. You could also use it to create custom banding, etc.

Description

The trick here is to use a column in the model which does not appear in the view to store a color value. Treeview columns can hold any datatype, so we make the column a TreeModelColumn<Gdk::Color>, then set the property_cell_background_gdk for each column in the view to use this hidden column from the model as it's value (see line 24). This trick can be used for a lot of similar purposes, eg, if you have a column which is a CellRendererProgress, you set it's "value" attribute to an undisplayed int column and you can also replace the text label by setting it's "text" value to another undisplayed string column.

The demo works like this: when you double click on a row, it will be left highlighted in red (see line 39). When you double click it again, it will revert back to the normal background color. In order to do that, we need to have a record of what that "default" color is (see line 9).

Source

   1 #include <gtkmm.h>
   2 
   3 using namespace std;
   4 
   5 class demoview : public Gtk::TreeView {
   6         public:
   7 
   8                 demoview () {
   9                         // get the default background color
  10                         Glib::RefPtr<Gtk::Style> styleptr = get_default_style();
  11                         basecolor = styleptr->get_base(Gtk::STATE_NORMAL);
  12 
  13                         // add columns to model 
  14                         mrec.add(col1);
  15                         mrec.add(col2);
  16                         mrec.add(col3_hid);
  17                         lstore = Gtk::ListStore::create(mrec);
  18                         set_model(lstore);
  19 
  20                         // add columns to view
  21                         append_column("No.",col1);
  22                         append_column("Data",col2);
  23 
  24                         // use hidden column to set background property for cols 1 & 2 
  25                         Gtk::TreeViewColumn *cp;
  26                         Gtk::CellRenderer *cr;
  27                         for (int i=0;i<2;i++) {
  28                                 cp = get_column(i);
  29                                 cr = cp->get_first_cell_renderer();
  30                                 cp->add_attribute(cr->property_cell_background_gdk(),col3_hid);
  31                         }
  32 
  33                         // initialize TreeSelector and connect click signal 
  34                         tselector = get_selection();
  35                         signal_button_press_event().connect(
  36                                 sigc::mem_fun(*this,&demoview::buttonpress),false);
  37                 }
  38 
  39                 bool buttonpress(GdkEventButton *e) {
  40                         if (e->type != GDK_2BUTTON_PRESS)  return false;
  41                         Gtk::ListStore::iterator it = tselector->get_selected();
  42                         Gtk::ListStore::Row row = *it;
  43                         if (row[col3_hid] == basecolor) 
  44                                 row[col3_hid] = Gdk::Color("red");
  45                         else row[col3_hid] = basecolor;
  46                         return false;
  47                 }
  48 
  49                 void addrow (string data) {
  50                         static int n = 1;
  51                         Gtk::ListStore::iterator it = lstore->append();
  52                         Gtk::ListStore::Row row = *it;
  53                         row[col1] = n++;
  54                         row[col2] = data;
  55                         row[col3_hid] = basecolor;
  56                 }
  57 
  58         private:
  59                 Gtk::TreeModelColumnRecord mrec;
  60                 Glib::RefPtr<Gtk::ListStore> lstore;
  61                 Gtk::TreeModelColumn<int> col1;
  62                 Gtk::TreeModelColumn<string> col2;
  63                 Gtk::TreeModelColumn<Gdk::Color> col3_hid;
  64                 Gdk::Color basecolor;
  65                 Glib::RefPtr<Gtk::TreeSelection> tselector;
  66 };
  67 
  68 
  69 int main(int argc, char *argv[]) {
  70         Gtk::Main kit(argc, argv);
  71         Gtk::Window win;
  72         Gtk::VBox box;
  73         demoview eg;
  74 
  75         win.set_title("gtkmm demo");
  76         win.add(box);
  77         box.pack_start(eg);
  78 
  79         eg.addrow("first");
  80         eg.addrow("second");
  81         eg.addrow("third");
  82 
  83         win.show_all();
  84         kit.run(win);
  85 
  86         return 0;
  87 }                                      

Projects/gtkmm/TreeView_Color (last edited 2014-01-16 08:29:58 by KjellAhlstedt)