Coloring a GTK::TreeView Cell



I have figured out the way to color a cell:

1. Have a Model Column Class, such as - class CAllTreeModelColumns : public Gtk::TreeModel::ColumnRecord CAllTreeModelColumns atmcAllTreeModelColumns; // w/in the Object so that it's easy to access

2. Have these 2 types of columns in it:
Gtk::TreeModelColumn<std::string> m_colThreat1; ///< Threat Number associated with Sensor or Gun Gtk::TreeModelColumn<Glib::RefPtr<Gdk::Pixbuf>> m_colThreat1Color; ///< the color of a legend object

3. Then, have a TreeView & a List/TreeStore
Gtk::TreeView *m_tvpSensorTreeView;///< Sensors Tree View Glib::RefPtr<Gtk::ListStore> m_lststorSensors; ///< list store for Sensors

4. then set them up:
m_lststorSensors = Gtk::ListStore::create( m_atmcAllTreeModelColumns ); // construct "MODEL" m_tvpSensorTreeView->set_model(m_lststorSensors); // Add Model to TreeView
  m_tvpSensorTreeView->property_enable_grid_lines();
  m_tvpSensorTreeView->set_rules_hint();

5. Then Add 2 Model Columns to a Singular TreeView Column: Render is optional since both of these "types" are default types

Gtk::TreeView::Column* pColumn = Gtk::manage( new Gtk::TreeView::Column(gus_ColumnTitle) ); // Construct Model Column pColumn->pack_start( p_tmcstrThreatNumber, false ); // Threat Number Model Column pColumn->pack_start( p_tmcpixDetectionColor, false ); // Sensor Detection Color Moldel Column


 // Set up Renderers for these Model Colums w/in the TreeView Column
 //
 Gtk::CellRendererPixbuf rendPix;
 Gtk::CellRendererText   rendTxt;
pColumn->pack_start( rendPix, false); // false so that the icon will consume as little space as needed w/in the cell
 pColumn->pack_start( rendTxt);

 Glib::PropertyProxy<Pango::Alignment> ali = rendTxt.property_alignment();
 ali.set_value(Pango::ALIGN_RIGHT);

// Now, actually Add Model Column to Tree VIEW
 //
TreeView->append_column( *pColumn ); // Passes a Reference (&) to what (*pColumn) points to.


6.  FINALLY     Add Data to the view

for (int i_row=0; i_row<N; i_row++)
 {
Gtk::TreeModel::iterator itr = p_lststorList->append(); // append New Row to List Store used by TreeView: // whatever's in the liststore the treeview shows. Gtk::TreeModel::Row row = *itr; // pointer to the actual Row in the List Store that was just instantiated // convert number to string number
   stringstream strstrm;
   strstrm << i_row+1;
   string str_row = strstrm.str();


   // THREAT  IMAGE COLOR
   //
Glib::RefPtr<Gdk::Pixbuf> pic = m_imageSensorLegendAreaClear->get_pixbuf(); // some Image file or already loaded image Glib::RefPtr<Gdk::Pixbuf> scaledPic = pic->scale_simple(12,12,Gdk::INTERP_NEAREST); // 12px X 12px // THREATs
   //
    row[m_atmcAllTreeModelColumns.m_colThreat1]           = str_row ;
    row[m_atmcAllTreeModelColumns.m_colThreat1Color]  = scaledPic;
 }

--
Sincerely, Allen

Gene Allen Saucier, Jr
Senior Software Engineer
CAS, Inc
100 Quality Circle
Huntsville, AL  35806
or
PO Box 11190
Huntsville, AL  35814
(256) 922-6453 (w)
"...As for I and my house, we shall follow the Lord"  Joshua 24:15




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]