[glom/glom-1-26] Adapt to recent Gtk::Builder child-widget ref-counting fixes.



commit 67e1eef5c8c10b245016de7a8468b486b42ea93f
Author: Murray Cumming <murrayc murrayc com>
Date:   Wed Jun 25 11:37:39 2014 +0200

    Adapt to recent Gtk::Builder child-widget ref-counting fixes.

 glom/frame_glom.cc                                 |    9 ++++--
 glom/glade_utils.h                                 |   29 ++++++++++++++++---
 .../mode_design/fields/dialog_defaultformatting.cc |    6 +---
 .../layout_item_dialogs/dialog_field_layout.cc     |    8 +----
 .../layout_item_dialogs/dialog_formatting.cc       |    4 +--
 .../print_layouts/dialog_text_formatting.cc        |    3 +-
 6 files changed, 35 insertions(+), 24 deletions(-)
---
diff --git a/glom/frame_glom.cc b/glom/frame_glom.cc
index 082bfba..17cd18f 100644
--- a/glom/frame_glom.cc
+++ b/glom/frame_glom.cc
@@ -1232,7 +1232,8 @@ void Frame_Glom::do_menu_Navigate_Table(bool open_default)
   //Create the dialog, if it has not already been created:
   if(!m_pBox_Tables)
   {
-    Utils::get_glade_child_widget_derived_with_warning(m_pBox_Tables);
+    const Glib::RefPtr<Gtk::Builder> builderToKeepWidgetAlive =
+      Utils::get_glade_child_widget_derived_with_warning(m_pBox_Tables);
     m_pDialog_Tables = new Window_BoxHolder(m_pBox_Tables, _("Edit Tables"));
     m_pDialog_Tables->signal_hide().connect(sigc::mem_fun(*this, &Frame_Glom::on_dialog_tables_hide));
 
@@ -1698,7 +1699,8 @@ void Frame_Glom::on_menu_developer_reports()
   //Create the widget if necessary:
   if(!m_pBox_Reports)
   {
-    Utils::get_glade_child_widget_derived_with_warning(m_pBox_Reports);
+    const Glib::RefPtr<Gtk::Builder> builderToKeepWidgetAlive =
+      Utils::get_glade_child_widget_derived_with_warning(m_pBox_Reports);
     if(!m_pBox_Reports)
     {
       std::cerr << G_STRFUNC << ": m_pBox_Reports is null." << std::cerr;
@@ -1740,7 +1742,8 @@ void Frame_Glom::on_menu_developer_print_layouts()
   //Create the widget if necessary:
   if(!m_pBox_PrintLayouts)
   {
-    Utils::get_glade_child_widget_derived_with_warning(m_pBox_PrintLayouts);
+    const Glib::RefPtr<Gtk::Builder> builderToKeepWidgetAlive =
+      Utils::get_glade_child_widget_derived_with_warning(m_pBox_PrintLayouts);
     if(!m_pBox_PrintLayouts)
     {
       std::cerr << G_STRFUNC << ": m_pBox_PrintLayouts is null." << std::endl;
diff --git a/glom/glade_utils.h b/glom/glade_utils.h
index 9407c5d..501fa14 100644
--- a/glom/glade_utils.h
+++ b/glom/glade_utils.h
@@ -23,6 +23,7 @@
 
 #include <iostream> // For std::cerr
 #include <gtkmm/builder.h>
+#include <gtkmm/box.h>
 #include <gtkmm/window.h>
 #include <giomm/file.h>
 #include <glibmm/miscutils.h>
@@ -46,7 +47,7 @@ inline std::string get_glade_resource_path(const std::string& filename)
  * such as a GtkSpinButton's GtkAdjustment.
  */
 template<class T_Widget>
-void helper_get_glade_widget_derived_with_warning(const std::string& filename, const Glib::ustring& id, 
T_Widget*& widget, bool load_all)
+Glib::RefPtr<Gtk::Builder> helper_get_glade_widget_derived_with_warning(const std::string& filename, const 
Glib::ustring& id, T_Widget*& widget, bool load_all)
 {
   Glib::RefPtr<Gtk::Builder> refXml;
 
@@ -79,16 +80,20 @@ void helper_get_glade_widget_derived_with_warning(const std::string& filename, c
   {
     refXml->get_widget_derived(id, widget);
   }
+
+  return refXml;
 }
 
 /** This should be used with classes that have a static glade_id member.
  * This loads only the widget's own object, as specified by its ID.
+ *
+ * You must keep the returned RefPtr<Gtk::Builder> until you have added
+ * this widget to a container that will own it. Otherwise the widget
+ * will be deleted immediately.
  */
 template<class T_Widget>
-void get_glade_child_widget_derived_with_warning(T_Widget*& widget)
+Glib::RefPtr<Gtk::Builder> get_glade_child_widget_derived_with_warning(T_Widget*& widget)
 {
-  widget = 0;
-
   // Check the path to the installed .glade file:
   // The id is the same as the filename, in a developer/operator sub-directory:
   // TODO: Should we use build_filename()?
@@ -96,7 +101,21 @@ void get_glade_child_widget_derived_with_warning(T_Widget*& widget)
       (T_Widget::glade_developer ? "developer" : "operator"),
       std::string(T_Widget::glade_id) + ".glade"); 
   
-  helper_get_glade_widget_derived_with_warning(filename, T_Widget::glade_id, widget, false /* load just this 
*/);
+  return helper_get_glade_widget_derived_with_warning(filename, T_Widget::glade_id, widget, false /* load 
just this */);
+}
+
+/** This should be used with classes that have a static glade_id member.
+ * This loads only the widget's own object, as specified by its ID,
+ * and adds it to the parent box, which will then own it.
+ */
+template<class T_Widget>
+void box_pack_start_glade_child_widget_derived_with_warning(Gtk::Box* parent_box, T_Widget*& widget)
+{
+  Glib::RefPtr<Gtk::Builder> builder = get_glade_child_widget_derived_with_warning(widget);
+  widget = 0;
+
+  if(widget)
+    parent_box->pack_start(*widget);
 }
 
 /** This should be used with classes that have a static glade_id member.
diff --git a/glom/mode_design/fields/dialog_defaultformatting.cc 
b/glom/mode_design/fields/dialog_defaultformatting.cc
index d6d363d..2a3ed74 100644
--- a/glom/mode_design/fields/dialog_defaultformatting.cc
+++ b/glom/mode_design/fields/dialog_defaultformatting.cc
@@ -42,11 +42,7 @@ Dialog_DefaultFormatting::Dialog_DefaultFormatting(BaseObjectType* cobject, cons
   builder->get_widget("box_formatting_placeholder", m_box_formatting_placeholder);
 
   //Get the formatting stuff:
-  Utils::get_glade_child_widget_derived_with_warning(m_box_formatting);
-
-  if(m_box_formatting) //Unlikely to fail and it already warns on stderr.
-    m_box_formatting_placeholder->pack_start(*m_box_formatting);
-
+  Utils::box_pack_start_glade_child_widget_derived_with_warning(m_box_formatting_placeholder, 
m_box_formatting);
   add_view(m_box_formatting);
 
   if(m_box_formatting)
diff --git a/glom/mode_design/layout/layout_item_dialogs/dialog_field_layout.cc 
b/glom/mode_design/layout/layout_item_dialogs/dialog_field_layout.cc
index 21fa116..61d6528 100644
--- a/glom/mode_design/layout/layout_item_dialogs/dialog_field_layout.cc
+++ b/glom/mode_design/layout/layout_item_dialogs/dialog_field_layout.cc
@@ -55,12 +55,8 @@ Dialog_FieldLayout::Dialog_FieldLayout(BaseObjectType* cobject, const Glib::RefP
   builder->get_widget("box_formatting_placeholder", m_box_formatting_placeholder);
 
   //Get the formatting stuff:
-  Utils::get_glade_child_widget_derived_with_warning(m_box_formatting);
-  if(m_box_formatting && m_box_formatting_placeholder)
-  {
-    m_box_formatting_placeholder->pack_start(*m_box_formatting);
-    add_view(m_box_formatting);
-  }
+  Utils::box_pack_start_glade_child_widget_derived_with_warning(m_box_formatting_placeholder, 
m_box_formatting);
+  add_view(m_box_formatting);
 
   m_radiobutton_custom_formatting->signal_toggled().connect(sigc::mem_fun(*this, 
&Dialog_FieldLayout::on_radiobutton_custom_formatting));
 
diff --git a/glom/mode_design/layout/layout_item_dialogs/dialog_formatting.cc 
b/glom/mode_design/layout/layout_item_dialogs/dialog_formatting.cc
index a75b9ea..3a24a58 100644
--- a/glom/mode_design/layout/layout_item_dialogs/dialog_formatting.cc
+++ b/glom/mode_design/layout/layout_item_dialogs/dialog_formatting.cc
@@ -33,9 +33,7 @@ Dialog_Formatting::Dialog_Formatting()
   set_border_width(6);
 
   //Get the formatting stuff:
-  Utils::get_glade_child_widget_derived_with_warning(m_box_formatting);
-
-  get_content_area()->pack_start(*m_box_formatting, Gtk::PACK_EXPAND_WIDGET);
+  Utils::box_pack_start_glade_child_widget_derived_with_warning(get_content_area(), m_box_formatting);
   add_view(m_box_formatting);
 
   add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
diff --git a/glom/mode_design/print_layouts/dialog_text_formatting.cc 
b/glom/mode_design/print_layouts/dialog_text_formatting.cc
index 0de1b5d..36d316f 100644
--- a/glom/mode_design/print_layouts/dialog_text_formatting.cc
+++ b/glom/mode_design/print_layouts/dialog_text_formatting.cc
@@ -45,10 +45,9 @@ Dialog_TextFormatting::Dialog_TextFormatting(BaseObjectType* cobject, const Glib
   //Get the place to put the Formatting stuff:
   builder->get_widget("box_formatting_placeholder", m_box_formatting_placeholder);
  
-  Utils::get_glade_child_widget_derived_with_warning(m_box_formatting);
+  Utils::box_pack_start_glade_child_widget_derived_with_warning(m_box_formatting_placeholder, 
m_box_formatting);
   if(m_box_formatting)
   {
-    m_box_formatting_placeholder->pack_start(*m_box_formatting);
     add_view(m_box_formatting);
     m_box_formatting->set_is_for_non_editable();
   }


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