libglademm proposed change: arbitrary constructors for get_widget_derived



Hello,

It's convenient to code window's functionality in classes derived from
Gtk::Window or Gtk::Dialog. Gtkmm makes it easy to do so, but libglademm
puts a restriction on this. It requires, that the derived widgets
constructed from gtkmm should have two specific parameters: pointer to
GtkWidget and RefPtr to Glade::Xml.

The attached patch adds overloaded version of get_widget_derived, which
allows arbitrary constructors in derived widgets. The last parameter of
this function is a construction functor, which receives the two original
construction parameters (GtkWidget* and RefPtr<Glade::Xml>). There exist
facilities allowing to construct such a functor - for example
boost::bind or sigc::bind. The ConstructDerived functor in the patch is
just a wrapper, added for the purpose of not duplicating the code of
get_widget_derived function. ConstructDerived should not be part of the
interface, so it probably should be hidden somewhere (maybe in private
part of Glade::Xml?)

What do you think about it?

Marek Materzok

--- xml.h.old   2005-05-02 00:15:23.000000000 +0200
+++ xml.h       2005-04-23 00:42:48.000000000 +0200
@@ -61,6 +61,17 @@
   Glib::ustring message_;
 };

+class Xml;
+
+template <class T_Widget>
+struct ConstructDerived
+{
+  typedef  typename T_Widget::BaseObjectType cwidget_type;
+  T_Widget *operator()(cwidget_type *pCWidget, Glib::RefPtr<Gnome::Glade::Xml> refThis) const
+  {
+    return new T_Widget(pCWidget, refThis);
+  }
+};

 class Xml : public Glib::Object
 {
@@ -228,6 +239,12 @@
   template <class T_Widget> inline
   T_Widget* get_widget_derived(const Glib::ustring& name, T_Widget*& widget)
   {
+         return get_widget_derived<T_Widget, ConstructDerived<T_Widget> >(name, widget, ConstructDerived<T_Widget>());
+  }
+
+  template <class T_Widget, typename T_Construct> inline
+  T_Widget* get_widget_derived(const Glib::ustring& name, T_Widget*& widget, const T_Construct &construct)
+  {
      // initialize input parameter
      widget = 0;

@@ -260,7 +277,7 @@
        //Set the output variable. We needed to do this because we can not template the return type.
        Glib::RefPtr<Gnome::Glade::Xml> refThis(this);
        refThis->reference(); //take a copy.
-       widget = new T_Widget(pCWidget, refThis);
+       widget = construct(pCWidget, refThis);
      }

      //We return it as well (we need the parameter as well, because C++ can not just template the return type.)




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