[gtkmm/wip/dboles/builder-nicer-4: 1/4] Builder: Add returning vers of get_widget|_derived



commit af65f1129e0a2ff77575544adedfa193abae0927
Author: Daniel Boles <dboles src gnome org>
Date:   Sun Dec 16 22:29:47 2018 +0000

    Builder: Add returning vers of get_widget|_derived
    
    To avoid the cumbersome two-step process of declaring a pointer and then
    passing it to get_widget[_derived]() for use as an output argument, add
    overloads that return the pointer directly. Of course, now we have no
    arg from which to deduce the type, so pass that as a template argument.
    
    https://gitlab.gnome.org/GNOME/gtkmm/issues/43

 gtk/src/builder.hg | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)
---
diff --git a/gtk/src/builder.hg b/gtk/src/builder.hg
index f11c4fe4..264648d1 100644
--- a/gtk/src/builder.hg
+++ b/gtk/src/builder.hg
@@ -481,6 +481,37 @@ public:
     const_cast<Builder*>(this)->get_widget(name, widget);
   }
 
+  /** Gets a widget from the Builder file.
+   *
+   * For instance:
+   * @code
+   * Gtk::Grid* pGrid = refXml->get_widget("mygrid");
+   * @endcode
+   *
+   * See get_widget().
+   *
+   * @param name The name of the widget.
+   * @return A pointer to the widget, or <tt>nullptr</tt> on failure.
+   *
+   * @newin{3,94}
+   */
+  template <class T_Widget> inline
+  T_Widget* get_widget(const Glib::ustring& name)
+  {
+    T_Widget* widget;
+    get_widget(name, widget);
+    return widget;
+  }
+
+  /** See the non-const version.
+   * @newin{3,94}
+   */
+  template <class T_Widget> inline
+  const T_Widget* get_widget(const Glib::ustring& name) const
+  {
+    return const_cast<Builder*>(this)->get_widget<const T_Widget>(name);
+  }
+
   /** Gets a widget whose details are specified in the GtkBuilder file,
    * but which is implemented by your own derived class.
    *
@@ -563,6 +594,40 @@ public:
     get_widget_derived(std::const_pointer_cast<Gtk::Builder>(builder), name, widget, 
std::forward<Args>(args)...);
   }
 
+  /** Gets a widget whose details are specified in the GtkBuilder file,
+   * but which is implemented by your own derived class.
+   *
+   * For instance:
+   * @code
+   * MyDerivedDialog* pDialog1 = get_widget_derived(refBuilder, "mydialog1");
+   * MyDerivedDialog* pDialog2 = get_widget_derived(refBuilder, "mydialog2", "A storm is imminent!", true);
+   * @endcode
+   *
+   * See get_widget_derived().
+   *
+   * @param name The name of the widget.
+   * @param args Additional arguments to pass to the constructor of the derived class.
+   * @return widget A pointer to the widget, or <tt>nullptr</tt> on failure.
+   *
+   * @newin{3,94}
+   */
+  template <typename T_Widget, typename... Args> inline
+  static T_Widget* get_widget_derived(const Glib::RefPtr<Gtk::Builder>& builder, const Glib::ustring& name, 
Args&&... args)
+  {
+    T_Widget* widget;
+    get_widget_derived(builder, name, widget, std::forward<Args>(args)...);
+    return widget;
+  }
+
+  /** See the non-const version.
+   * @newin{3,94}
+   */
+  template <typename T_Widget, typename... Args> inline
+  static const T_Widget* get_widget_derived(const Glib::RefPtr<const Gtk::Builder>& builder, const 
Glib::ustring& name, Args&&... args)
+  {
+    return get_widget_derived<const T_Widget>(std::const_pointer_cast<Gtk::Builder>(builder), name, 
std::forward<Args>(args)...);
+  }
+
 #m4 _CONVERSION(`GSList*',`std::vector<Glib::RefPtr<Glib::Object> 
',`Glib::SListHandler<Glib::RefPtr<Glib::Object> >::slist_to_vector($3, Glib::OWNERSHIP_SHALLOW)')
   _WRAP_METHOD(std::vector<Glib::RefPtr<Glib::Object> > get_objects(), gtk_builder_get_objects)
 


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