[gtkmm-documentation] combobox/complex example: Show use of a cell_data_func().



commit b7284b8218462909b94fce3576d86dcfecf0796a
Author: Murray Cumming <murrayc murrayc com>
Date:   Thu Sep 10 14:57:57 2015 +0200

    combobox/complex example: Show use of a cell_data_func().

 examples/book/combobox/complex/examplewindow.cc |   27 +++++++++++++++++++++++
 examples/book/combobox/complex/examplewindow.h  |    9 ++++++-
 2 files changed, 34 insertions(+), 2 deletions(-)
---
diff --git a/examples/book/combobox/complex/examplewindow.cc b/examples/book/combobox/complex/examplewindow.cc
index 6a9a8e8..0648ed8 100644
--- a/examples/book/combobox/complex/examplewindow.cc
+++ b/examples/book/combobox/complex/examplewindow.cc
@@ -32,6 +32,7 @@ ExampleWindow::ExampleWindow()
   Gtk::TreeModel::Row row = *(m_refTreeModel->append());
   row[m_Columns.m_col_id] = 1;
   row[m_Columns.m_col_name] = "Billy Bob";
+  row[m_Columns.m_col_extra] = "something";
   m_Combo.set_active(row);
   /*
   Gtk::TreeModel::Row childrow = *(m_refTreeModel->append(row.children()));
@@ -46,11 +47,13 @@ ExampleWindow::ExampleWindow()
   row = *(m_refTreeModel->append());
   row[m_Columns.m_col_id] = 2;
   row[m_Columns.m_col_name] = "Joey Jojo";
+  row[m_Columns.m_col_extra] = "yadda";
 
 
   row = *(m_refTreeModel->append());
   row[m_Columns.m_col_id] = 3;
   row[m_Columns.m_col_name] = "Rob McRoberts";
+  row[m_Columns.m_col_extra] = "";
 
   /*
   childrow = *(m_refTreeModel->append(row.children()));
@@ -63,6 +66,13 @@ ExampleWindow::ExampleWindow()
   m_Combo.pack_start(m_Columns.m_col_id);
   m_Combo.pack_start(m_Columns.m_col_name);
 
+  //An example of adding a cell renderer manually,
+  //instead of using pack_start(model_column)
+  //so we have more control:
+  m_Combo.set_cell_data_func(m_cell,
+    sigc::mem_fun(*this, &ExampleWindow::on_cell_data_extra));
+  m_Combo.pack_start(m_cell);
+
   //Add the ComboBox to the window.
   add(m_Combo);
 
@@ -76,6 +86,23 @@ ExampleWindow::~ExampleWindow()
 {
 }
 
+void ExampleWindow::on_cell_data_extra(const Gtk::TreeModel::const_iterator& iter)
+{
+  auto row = *iter;
+  const Glib::ustring extra = row[m_Columns.m_col_extra];
+
+  //Some arbitrary logic just to show that this is where you can do such things:
+
+  //Transform the value, deciding how to represent it as text:
+  if(extra.empty())
+    m_cell.property_text() = "(none)";
+  else
+    m_cell.property_text() = "-" + extra + "-";
+
+  //Change other cell renderer properties too:
+  m_cell.property_foreground() = (extra == "yadda" ? "red" : "green");
+}
+
 void ExampleWindow::on_combo_changed()
 {
   Gtk::TreeModel::iterator iter = m_Combo.get_active();
diff --git a/examples/book/combobox/complex/examplewindow.h b/examples/book/combobox/complex/examplewindow.h
index 2c0cf4b..60118e4 100644
--- a/examples/book/combobox/complex/examplewindow.h
+++ b/examples/book/combobox/complex/examplewindow.h
@@ -29,26 +29,31 @@ public:
   ExampleWindow();
   virtual ~ExampleWindow();
 
-protected:  
+protected:
+  void on_cell_data_extra(const Gtk::TreeModel::const_iterator& iter);
+
   //Signal handlers:
   void on_combo_changed();
 
+
   //Tree model columns:
   class ModelColumns : public Gtk::TreeModel::ColumnRecord
   {
   public:
 
     ModelColumns()
-    { add(m_col_id); add(m_col_name); }
+    { add(m_col_id); add(m_col_name); add(m_col_extra);}
 
     Gtk::TreeModelColumn<int> m_col_id;
     Gtk::TreeModelColumn<Glib::ustring> m_col_name;
+    Gtk::TreeModelColumn<Glib::ustring> m_col_extra;
   };
 
   ModelColumns m_Columns;
 
   //Child widgets:
   Gtk::ComboBox m_Combo;
+  Gtk::CellRendererText m_cell;
   Glib::RefPtr<Gtk::ListStore> m_refTreeModel;
 };
 


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