[gtkmm/wip/dboles/builder-remove-old-get-overloads] builder: Remove old, ugly get_widget*() overloads
- From: Daniel Boles <dboles src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtkmm/wip/dboles/builder-remove-old-get-overloads] builder: Remove old, ugly get_widget*() overloads
- Date: Sun, 19 May 2019 18:26:24 +0000 (UTC)
commit 833976a0423e20ce9d966940d80623a7d49228bf
Author: Daniel Boles <dboles src gnome org>
Date: Sun May 19 19:21:33 2019 +0100
builder: Remove old, ugly get_widget*() overloads
Kjell suggested this here, so while I originally kept these and
implemented the nicer overloads in terms of them to retain backwards
compatibility and richer git diffs, let's instead see if we can simply
get rid of them, to encourage the far nicer API they offer.
https://gitlab.gnome.org/GNOME/gtkmm/merge_requests/11#note_514243
demos/gtk-demo/example_shortcuts.cc | 27 ++++-------
gtk/src/builder.hg | 95 +++++++------------------------------
tests/builder/main.cc | 8 ++--
3 files changed, 30 insertions(+), 100 deletions(-)
---
diff --git a/demos/gtk-demo/example_shortcuts.cc b/demos/gtk-demo/example_shortcuts.cc
index a5975bbd..3920359b 100644
--- a/demos/gtk-demo/example_shortcuts.cc
+++ b/demos/gtk-demo/example_shortcuts.cc
@@ -38,8 +38,7 @@ Gtk::Window* do_shortcuts()
}
// Get the GtkBuilder-instantiated window:
- Example_Shortcuts* pWindow = nullptr;
- Gtk::Builder::get_widget_derived(builder, "window1", pWindow);
+ auto pWindow = Gtk::Builder::get_widget_derived<Example_Shortcuts>(builder, "window1");
if (!pWindow)
{
std::cout << "Could not get 'window1' from the builder." << std::endl;
@@ -53,44 +52,37 @@ Example_Shortcuts::Example_Shortcuts(
: Gtk::Window(cobject),
m_builder(builder)
{
- Gtk::Button* pButton = nullptr;
- builder->get_widget("button_builder", pButton);
+ auto pButton = builder->get_widget<Gtk::Button>("button_builder");
if (pButton)
pButton->signal_clicked().connect(sigc::bind(sigc::mem_fun(
*this, &Example_Shortcuts::on_button_clicked), "shortcuts_builder", ""));
- pButton = nullptr;
- builder->get_widget("button_gedit", pButton);
+ pButton = builder->get_widget<Gtk::Button>("button_gedit");
if (pButton)
pButton->signal_clicked().connect(sigc::bind(sigc::mem_fun(
*this, &Example_Shortcuts::on_button_clicked), "shortcuts_gedit", ""));
- pButton = nullptr;
- builder->get_widget("button_clocks", pButton);
+ pButton = builder->get_widget<Gtk::Button>("button_clocks");
if (pButton)
pButton->signal_clicked().connect(sigc::bind(sigc::mem_fun(
*this, &Example_Shortcuts::on_button_clicked), "shortcuts_clocks", ""));
- pButton = nullptr;
- builder->get_widget("button_clocks_stopwatch", pButton);
+ pButton = builder->get_widget<Gtk::Button>("button_clocks_stopwatch");
if (pButton)
pButton->signal_clicked().connect(sigc::bind(sigc::mem_fun(
*this, &Example_Shortcuts::on_button_clicked), "shortcuts_clocks", "stopwatch"));
- pButton = nullptr;
- builder->get_widget("button_boxes", pButton);
+ pButton = builder->get_widget<Gtk::Button>("button_boxes");
if (pButton)
pButton->signal_clicked().connect(sigc::bind(sigc::mem_fun(
*this, &Example_Shortcuts::on_button_clicked), "shortcuts_boxes", ""));
- pButton = nullptr;
- builder->get_widget("button_boxes_wizard", pButton);
+ pButton = builder->get_widget<Gtk::Button>("button_boxes_wizard");
if (pButton)
pButton->signal_clicked().connect(sigc::bind(sigc::mem_fun(
*this, &Example_Shortcuts::on_button_clicked), "shortcuts_boxes", "wizard"));
- pButton = nullptr;
- builder->get_widget("button_boxes_display", pButton);
+ pButton = builder->get_widget<Gtk::Button>("button_boxes_display");
if (pButton)
pButton->signal_clicked().connect(sigc::bind(sigc::mem_fun(
*this, &Example_Shortcuts::on_button_clicked), "shortcuts_boxes", "display"));
@@ -114,8 +106,7 @@ void Example_Shortcuts::on_button_clicked(const Glib::ustring& id, const Glib::u
}
// Get the GtkBuilder-instantiated shortcuts window:
- Gtk::ShortcutsWindow* pOverlay = nullptr;
- builder->get_widget(id, pOverlay);
+ auto pOverlay = builder->get_widget<Gtk::ShortcutsWindow>(id);
if (!pOverlay)
{
std::cout << "Could not get '"<< id << "' from the builder." << std::endl;
diff --git a/gtk/src/builder.hg b/gtk/src/builder.hg
index ece41e3c..98ce6327 100644
--- a/gtk/src/builder.hg
+++ b/gtk/src/builder.hg
@@ -474,11 +474,12 @@ public:
}
/** Gets a widget from the Builder file.
+ *
* For instance:
* @code
- * Gtk::Grid* pGrid = nullptr;
- * refBuilder->get_widget("mygrid", pGrid);
+ * auto pGrid = refBuilder->get_widget<Gtk::Grid>("mygrid");
* @endcode
+ *
* This method prints a warning message to the console if the widget
* doesn't exist or has the wrong type, so you don't need to check that
* manually.
@@ -489,49 +490,21 @@ public:
* container widget.
*
* @param name The name of the widget.
- * @param[out] widget A pointer to the widget, or <tt>nullptr</tt> on failure.
+ * @return A pointer to the widget, or <tt>nullptr</tt> on failure.
+ *
+ * @newin{3,96}
*/
template <class T_Widget> inline
- void get_widget(const Glib::ustring& name, T_Widget*& widget)
+ T_Widget* get_widget(const Glib::ustring& name)
{
- // Initialize output parameter:
- widget = nullptr;
+ T_Widget* widget = nullptr;
// The dynamic_cast<> should never fail if get_widget_checked() succeeded.
widget = dynamic_cast<T_Widget*>(this->get_widget_checked(name, T_Widget::get_base_type()));
if(!widget)
g_critical("Gtk::Builder::get_widget(): dynamic_cast<> failed.");
- }
- /** See the non-const version.
- * @newin{3,8}
- */
- template <class T_Widget> inline
- void get_widget(const Glib::ustring& name, const T_Widget*& widget) const
- {
- const_cast<Builder*>(this)->get_widget(name, widget);
- }
-
- /** Gets a widget from the Builder file.
- *
- * For instance:
- * @code
- * auto pGrid = refBuilder->get_widget<Gtk::Grid>("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,96}
- */
- template <class T_Widget> inline
- T_Widget* get_widget(const Glib::ustring& name)
- {
- T_Widget* widget;
- get_widget(name, widget);
return widget;
}
@@ -561,10 +534,8 @@ public:
*
* For instance:
* @code
- * MyDerivedDialog* pDialog1 = nullptr;
- * MyDerivedDialog* pDialog2 = nullptr;
- * Gtk::Builder::get_widget_derived(refBuilder, "mydialog1", pDialog1);
- * Gtk::Builder::get_widget_derived(refBuilder, "mydialog2", pDialog2, "A storm is imminent!", true);
+ * auto pDialog1 = Gtk::Builder::get_widget_derived<MyDerivedDialog>(refBuilder, "mydialog1");
+ * auto pDialog2 = Gtk::Builder::get_widget_derived<MyDerivedDialog>(refBuilder, "mydialog2", "A storm is
imminent!", true);
* @endcode
*
* @note
@@ -576,21 +547,22 @@ public:
* @param builder The Gtk::Builder from which to get the widget.
* @param name The name of the widget.
* @param args Additional arguments to pass to the constructor of the derived class.
- * @param[out] widget A pointer to the widget, or <tt>nullptr</tt> on failure.
+ * @return A pointer to the widget, or <tt>nullptr</tt> on failure.
+ *
+ * @newin{3,94}
*/
template <typename T_Widget, typename... Args> inline
- static void get_widget_derived(const Glib::RefPtr<Gtk::Builder>& builder, const Glib::ustring& name,
T_Widget*& widget, Args&&... args)
+ static T_Widget* get_widget_derived(const Glib::RefPtr<Gtk::Builder>& builder, const Glib::ustring& name,
Args&&... args)
{
- // Initialize output parameter:
- widget = nullptr;
-
// Get the widget from the GtkBuilder file.
using cwidget_type = typename T_Widget::BaseObjectType;
auto pCWidget = (cwidget_type*)builder->get_cwidget(name);
//The error was already reported by get_cwidget().
if(!pCWidget)
- return;
+ return nullptr;
+
+ T_Widget* widget = nullptr;
//Check whether there is already a C++ wrapper instance associated with this C instance:
Glib::ObjectBase* pObjectBase = ObjectBase::_get_current_wrapper((GObject*)pCWidget);
@@ -616,40 +588,7 @@ public:
// Don't add reference to widget. That's done only in methods that return
// a Glib::RefPtr<Something>.
}
- }
- /** See the non-const version.
- * @newin{3,8}
- */
- template <typename T_Widget, typename... Args> inline
- static void get_widget_derived(const Glib::RefPtr<const Gtk::Builder>& builder, const Glib::ustring& name,
const T_Widget*& widget, Args&&... args)
- {
- 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
- * auto pDialog1 = Gtk::Builder::get_widget_derived<MyDerivedDialog>(refBuilder, "mydialog1");
- * auto pDialog2 = Gtk::Builder::get_widget_derived<MyDerivedDialog>(refBuilder, "mydialog2", "A storm is
imminent!", true);
- * @endcode
- *
- * See get_widget_derived().
- *
- * @param builder The Gtk::Builder from which to get the widget.
- * @param name The name of the widget.
- * @param args Additional arguments to pass to the constructor of the derived class.
- * @return A pointer to the widget, or <tt>nullptr</tt> on failure.
- *
- * @newin{3,96}
- */
- 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;
}
diff --git a/tests/builder/main.cc b/tests/builder/main.cc
index ad7e9e91..f296ccf2 100644
--- a/tests/builder/main.cc
+++ b/tests/builder/main.cc
@@ -112,10 +112,10 @@ public:
std::cout << "MainWindow::ctor" << std::endl;
// Called twice just to see if two calls affect the ref count.
- Gtk::Builder::get_widget_derived(refBuilder, "derived_button", m_pDerivedButton, "face-smile");
- Gtk::Builder::get_widget_derived(refBuilder, "derived_button", m_pDerivedButton);
- refBuilder->get_widget("standard_button", m_pStandardButton);
- refBuilder->get_widget("standard_button", m_pStandardButton);
+ m_pDerivedButton = Gtk::Builder::get_widget_derived<DerivedButton>(refBuilder, "derived_button",
"face-smile");
+ m_pDerivedButton = Gtk::Builder::get_widget_derived<DerivedButton>(refBuilder, "derived_button");
+ m_pStandardButton = refBuilder->get_widget<Gtk::Button>("standard_button");
+ m_pStandardButton = refBuilder->get_widget<Gtk::Button>("standard_button");
m_pStandardButton->add_destroy_notify_callback(nullptr, on_managed_button_deleted);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]