=?utf-8?q?=5Bglom/glom-1-20=5D_Details=EF=BC=9A_Make_restricted_choices_w?= =?utf-8?q?ork=2E?=



commit a910327e5362ad7b4cb76b8683d7f5fbf006b2e2
Author: Murray Cumming <murrayc murrayc com>
Date:   Wed Jan 4 22:08:40 2012 +0100

    Detailsï Make restricted choices work.
    
    * glom/mode_data/datawidget/combochoiceswithtreemode.[h|cc]:
    on_cell_data(): Move some code into set_cell_for_field_value().
    * glom/mode_data/datawidget/combo.[h|cc]: on_fixed_cell_data():
    Connect a cell_data slot that uses set_cell_for_field_value(),
    instead of trying to associate a Gnome::Gda::Value with a cell's text
    property.
    
    Without this, the choice combobox's rows were empty when the field's
    value should be restricted to the available custom (fixed) choices.

 ChangeLog                                          |   14 ++
 glom/mode_data/datawidget/combo.cc                 |   26 ++++-
 glom/mode_data/datawidget/combo.h                  |    2 +
 .../datawidget/combochoiceswithtreemodel.cc        |  144 +++++++++++---------
 .../datawidget/combochoiceswithtreemodel.h         |    4 +
 5 files changed, 122 insertions(+), 68 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 5dd2472..b2bbbdd 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2012-01-04  Murray Cumming  <murrayc murrayc com>
+
+	Detailsï Make restricted choices work.
+
+	* glom/mode_data/datawidget/combochoiceswithtreemode.[h|cc]:
+	on_cell_data(): Move some code into set_cell_for_field_value().
+	* glom/mode_data/datawidget/combo.[h|cc]: on_fixed_cell_data():
+	Connect a cell_data slot that uses set_cell_for_field_value(), 
+	instead of trying to associate a Gnome::Gda::Value with a cell's text 
+	property. 
+
+	Without this, the choice combobox's rows were empty when the field's
+	value should be restricted to the available custom (fixed) choices.
+
 1.20.4:
 
 2011-12-30  Murray Cumming  <murrayc murrayc com>
diff --git a/glom/mode_data/datawidget/combo.cc b/glom/mode_data/datawidget/combo.cc
index c0fdd20..fd3e395 100644
--- a/glom/mode_data/datawidget/combo.cc
+++ b/glom/mode_data/datawidget/combo.cc
@@ -62,6 +62,29 @@ ComboGlom::~ComboGlom()
 {
 }
 
+void ComboGlom::on_fixed_cell_data(const Gtk::TreeModel::iterator& iter, Gtk::CellRenderer* cell, guint model_column_index)
+{
+  if(!cell)
+  {
+    std::cerr << G_STRFUNC << ": cell is null." << std::endl;
+    return;
+  }
+
+  if(!iter)
+    return;
+
+  const sharedptr<const LayoutItem>& layout_item = get_layout_item();
+  sharedptr<const LayoutItem_Field> field = sharedptr<const LayoutItem_Field>::cast_dynamic(layout_item);
+  if(!field)
+    return;
+
+  Gnome::Gda::Value value;
+  Gtk::TreeModel::Row treerow = *iter;
+  treerow->get_value(model_column_index, value);
+
+  set_cell_for_field_value(cell, field, value);
+}
+
 void ComboGlom::set_choices_fixed(const FieldFormatting::type_list_values& list_values)
 {
   ComboChoicesWithTreeModel::set_choices_fixed(list_values);
@@ -113,7 +136,8 @@ void ComboGlom::set_choices_fixed(const FieldFormatting::type_list_values& list_
     cell_area->pack_start(*cell, true /* expand */, true /* align */, true /* fixed */);
 
     //Make the renderer render the column:
-    add_attribute(*cell, "text", i);
+    set_cell_data_func(*cell,
+      sigc::bind( sigc::mem_fun(*this, &ComboGlom::on_fixed_cell_data), cell, i));
   }
 }
 
diff --git a/glom/mode_data/datawidget/combo.h b/glom/mode_data/datawidget/combo.h
index 0b0bfba..021bb31 100644
--- a/glom/mode_data/datawidget/combo.h
+++ b/glom/mode_data/datawidget/combo.h
@@ -65,6 +65,8 @@ public:
 
 private:
 
+  void on_fixed_cell_data(const Gtk::TreeModel::iterator& iter, Gtk::CellRenderer* cell, guint model_column_index);
+
   // Note that this is a normal signal handler when glibmm was complied
   // without default signal handlers
   virtual void on_changed(); //From Gtk::ComboBox
diff --git a/glom/mode_data/datawidget/combochoiceswithtreemodel.cc b/glom/mode_data/datawidget/combochoiceswithtreemodel.cc
index eb7eccc..ee04f3c 100644
--- a/glom/mode_data/datawidget/combochoiceswithtreemodel.cc
+++ b/glom/mode_data/datawidget/combochoiceswithtreemodel.cc
@@ -301,6 +301,73 @@ Glib::RefPtr<Gtk::TreeModel> ComboChoicesWithTreeModel::get_choices_model()
   return m_refModel;
 }
 
+void ComboChoicesWithTreeModel::set_cell_for_field_value(Gtk::CellRenderer* cell, const sharedptr<const LayoutItem_Field>& field, const Gnome::Gda::Value& value)
+{
+  if(!field)
+    return;
+
+  if(!cell)
+    return;
+
+  const Field::glom_field_type type = field->get_glom_type();
+  switch(type)
+  {
+    case(Field::TYPE_BOOLEAN):
+    {
+      Gtk::CellRendererToggle* pDerived = dynamic_cast<Gtk::CellRendererToggle*>(cell);
+      if(pDerived)
+        pDerived->set_active( (value.get_value_type() == G_TYPE_BOOLEAN) && value.get_boolean() );
+
+      break;
+    }
+    case(Field::TYPE_IMAGE):
+    {
+      Gtk::CellRendererPixbuf* pDerived = dynamic_cast<Gtk::CellRendererPixbuf*>(cell);
+      if(pDerived)
+      {
+        const Glib::RefPtr<Gdk::Pixbuf> pixbuf = Utils::get_pixbuf_for_gda_value(value);
+
+        //Scale it down to a sensible size.
+        //TODO: if(pixbuf)
+        //  pixbuf = Utils::image_scale_keeping_ratio(pixbuf,  get_fixed_cell_height(), pixbuf->get_width());
+        g_object_set(pDerived->gobj(), "pixbuf", pixbuf ? pixbuf->gobj() : 0, (gpointer)0);
+      }
+      else
+        std::cerr << "Field::sql(): glom_type is TYPE_IMAGE but gda type is not VALUE_TYPE_BINARY" << std::endl;
+
+      break;
+    }
+    default:
+    {
+      //TODO: Maybe we should have custom cellcells for time, date, and numbers.
+      Gtk::CellRendererText* pDerived = dynamic_cast<Gtk::CellRendererText*>(cell);
+      if(pDerived)
+      {
+        //std::cout << "debug: " << G_STRFUNC << ": field name=" << field->get_name() << ", glom type=" << field->get_glom_type() << std::endl;
+        const Glib::ustring text = Conversions::get_text_for_gda_value(field->get_glom_type(), value, field->get_formatting_used().m_numeric_format);
+        pDerived->property_text() = text;
+      }
+      else
+      {
+         std::cerr << G_STRFUNC << ": cell has an unexpected type: " << typeid(cell).name() << std::endl;
+      }
+
+      //Show a different color if the value is numeric, if that's specified:
+      if(type == Field::TYPE_NUMERIC)
+      {
+        const Glib::ustring fg_color =
+        field->get_formatting_used().get_text_format_color_foreground_to_use(value);
+        if(!fg_color.empty())
+          g_object_set(pDerived->gobj(), "foreground", fg_color.c_str(), (gpointer)0);
+        else
+          g_object_set(pDerived->gobj(), "foreground", (const char*)0, (gpointer)0);
+      }
+
+      break;
+    }
+  }
+}
+
 void ComboChoicesWithTreeModel::on_cell_data(const Gtk::TreeModel::iterator& iter, Gtk::CellRenderer* cell, guint model_column_index)
 {
   //std::cout << G_STRFUNC << ": DEBUG: model_column_index=" << model_column_index << std::endl;
@@ -316,76 +383,19 @@ void ComboChoicesWithTreeModel::on_cell_data(const Gtk::TreeModel::iterator& ite
     return;
   }
 
-  if(iter)
-  {
-    const sharedptr<const LayoutItem>& layout_item = m_db_layout_items[model_column_index];
-
-    sharedptr<const LayoutItem_Field> field = sharedptr<const LayoutItem_Field>::cast_dynamic(layout_item);
-    if(field)
-    {
-      Gtk::TreeModel::Row treerow = *iter;
-      Gnome::Gda::Value value;
-      treerow->get_value(model_column_index, value);
-
-      const Field::glom_field_type type = field->get_glom_type();
-      switch(type)
-      {
-        case(Field::TYPE_BOOLEAN):
-        {
-          Gtk::CellRendererToggle* pDerived = dynamic_cast<Gtk::CellRendererToggle*>(cell);
-          if(pDerived)
-            pDerived->set_active( (value.get_value_type() == G_TYPE_BOOLEAN) && value.get_boolean() );
-
-          break;
-        }
-        case(Field::TYPE_IMAGE):
-        {
-          Gtk::CellRendererPixbuf* pDerived = dynamic_cast<Gtk::CellRendererPixbuf*>(cell);
-          if(pDerived)
-          {
-            const Glib::RefPtr<Gdk::Pixbuf> pixbuf = Utils::get_pixbuf_for_gda_value(value);
-
-            //Scale it down to a sensible size.
-            //TODO: if(pixbuf)
-            //  pixbuf = Utils::image_scale_keeping_ratio(pixbuf,  get_fixed_cell_height(), pixbuf->get_width());
-            g_object_set(pDerived->gobj(), "pixbuf", pixbuf ? pixbuf->gobj() : 0, (gpointer)0);
-          }
-          else
-            std::cerr << "Field::sql(): glom_type is TYPE_IMAGE but gda type is not VALUE_TYPE_BINARY" << std::endl;
+  if(!iter)
+    return;
 
-          break;
-        }
-        default:
-        {
-          //TODO: Maybe we should have custom cellcells for time, date, and numbers.
-          Gtk::CellRendererText* pDerived = dynamic_cast<Gtk::CellRendererText*>(cell);
-          if(pDerived)
-          {
-            //std::cout << "debug: " << G_STRFUNC << ": field name=" << field->get_name() << ", glom type=" << field->get_glom_type() << std::endl;
-            const Glib::ustring text = Conversions::get_text_for_gda_value(field->get_glom_type(), value, field->get_formatting_used().m_numeric_format);
-            pDerived->property_text() = text;
-          }
-          else
-          {
-             std::cerr << G_STRFUNC << ": cell has an unexpected type: " << typeid(cell).name() << std::endl;
-          }
+  const sharedptr<const LayoutItem>& layout_item = m_db_layout_items[model_column_index];
+  sharedptr<const LayoutItem_Field> field = sharedptr<const LayoutItem_Field>::cast_dynamic(layout_item);
+  if(!field)
+    return;
 
-          //Show a different color if the value is numeric, if that's specified:
-          if(type == Field::TYPE_NUMERIC)
-          {
-             const Glib::ustring fg_color =
-               field->get_formatting_used().get_text_format_color_foreground_to_use(value);
-             if(!fg_color.empty())
-                 g_object_set(pDerived->gobj(), "foreground", fg_color.c_str(), (gpointer)0);
-             else
-                 g_object_set(pDerived->gobj(), "foreground", (const char*)0, (gpointer)0);
-          }
+  Gnome::Gda::Value value;
+  Gtk::TreeModel::Row treerow = *iter;
+  treerow->get_value(model_column_index, value);
 
-          break;
-        }
-      }
-    }
-  }
+  set_cell_for_field_value(cell, field, value);
 }
 
 void ComboChoicesWithTreeModel::cell_connect_cell_data_func(Gtk::CellLayout* celllayout, Gtk::CellRenderer* cell, guint model_column_index)
diff --git a/glom/mode_data/datawidget/combochoiceswithtreemodel.h b/glom/mode_data/datawidget/combochoiceswithtreemodel.h
index 23ed351..e33c1c6 100644
--- a/glom/mode_data/datawidget/combochoiceswithtreemodel.h
+++ b/glom/mode_data/datawidget/combochoiceswithtreemodel.h
@@ -81,6 +81,10 @@ protected:
   //though that shouldn't be necessary anyway.
   void cell_connect_cell_data_func(Gtk::CellLayout* celllayout, Gtk::CellRenderer* cell, guint model_column_index);
 
+  /** Display the value in the cell according to the layout field's type and formatting.
+   */
+  void set_cell_for_field_value(Gtk::CellRenderer* cell, const sharedptr<const LayoutItem_Field>& field, const Gnome::Gda::Value& value);
+
 private:
   /// Render the model data to the cells in the view.
   void on_cell_data(const Gtk::TreeModel::iterator& iter, Gtk::CellRenderer* cell, guint model_column_index);



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