[gtkmm/wip/dboles/make_managed-3: 2/2] object: Add Gtk::make_managed<T>(args...) helper
- From: Daniel Boles <dboles src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtkmm/wip/dboles/make_managed-3: 2/2] object: Add Gtk::make_managed<T>(args...) helper
- Date: Sun, 7 Oct 2018 18:03:33 +0000 (UTC)
commit 225f05e836d0c761a0b1d99b4b800f7cd4525188
Author: Daniel Boles <dboles src gmail com>
Date: Sun Oct 7 13:09:49 2018 +0100
object: Add Gtk::make_managed<T>(args...) helper
To avoid the discouraged pattern of manually writing `new` and get
tidier syntax, follow the example of std::make_(unique|shared)() by
adding a new helper function that both constructs and manages a widget
in one call. The user will not need to `new` and can user fewer parens.
auto button = Gtk::manage(new Gtk::Button("_Hi", true));
becomes
auto button = Gtk::make_managed<Gtk::Button>("_Hi", true);
Switch to make_managed() in tests and demo, albeit nowhere else (yet?).
https://gitlab.gnome.org/GNOME/gtkmm/issues/33
demos/gtk-demo/demowindow.cc | 2 +-
demos/gtk-demo/example_appwindow.cc | 10 +++++-----
demos/gtk-demo/example_buttonbox.cc | 14 +++++++-------
demos/gtk-demo/example_change_display.cc | 4 ++--
demos/gtk-demo/example_dialog.cc | 2 +-
demos/gtk-demo/example_flowbox.cc | 4 ++--
demos/gtk-demo/example_iconbrowser.cc | 2 +-
demos/gtk-demo/example_images.cc | 4 ++--
demos/gtk-demo/example_menus.cc | 20 ++++++++++----------
demos/gtk-demo/example_panes.cc | 10 +++++-----
demos/gtk-demo/example_sizegroup.cc | 4 ++--
demos/gtk-demo/example_stacksidebar.cc | 4 ++--
demos/gtk-demo/example_textview.cc | 18 +++++++++---------
demos/gtk-demo/example_treeview_editable_cells.cc | 4 ++--
gtk/gtkmm/object.h | 22 ++++++++++++++++++++++
tests/child_widget_managed/main.cc | 2 +-
tests/delete_cpp_child/main.cc | 8 +++-----
tests/dialog/main.cc | 8 +++-----
tests/dialog_deletethis/main.cc | 2 +-
19 files changed, 81 insertions(+), 63 deletions(-)
---
diff --git a/demos/gtk-demo/demowindow.cc b/demos/gtk-demo/demowindow.cc
index a758819d..d6c41133 100644
--- a/demos/gtk-demo/demowindow.cc
+++ b/demos/gtk-demo/demowindow.cc
@@ -143,7 +143,7 @@ void DemoWindow::fill_tree()
}
}
- Gtk::CellRendererText* pCell = Gtk::manage(new Gtk::CellRendererText());
+ Gtk::CellRendererText* pCell = Gtk::make_managed<Gtk::CellRendererText>();
pCell->property_style() = Pango::STYLE_ITALIC;
Gtk::TreeViewColumn* pColumn = new Gtk::TreeViewColumn("Widget (double click for demo)", *pCell);
diff --git a/demos/gtk-demo/example_appwindow.cc b/demos/gtk-demo/example_appwindow.cc
index be6c4e5e..289d18e4 100644
--- a/demos/gtk-demo/example_appwindow.cc
+++ b/demos/gtk-demo/example_appwindow.cc
@@ -46,7 +46,7 @@ Example_AppWindow::Example_AppWindow()
using namespace Gtk::Menu_Helpers;
//File menu:
- Gtk::Menu* pMenuFile = Gtk::manage( new Gtk::Menu() );
+ Gtk::Menu* pMenuFile = Gtk::make_managed<Gtk::Menu>();
MenuList& list_file = pMenuFile->items();
list_file.push_back( MenuElem("_New", "<control>N", sigc::mem_fun(*this,
&Example_AppWindow::on_menu_item)) );
list_file.push_back( MenuElem("_Open", "<control>O", sigc::mem_fun(*this,
&Example_AppWindow::on_menu_item)) );
@@ -56,11 +56,11 @@ Example_AppWindow::Example_AppWindow()
list_file.push_back( MenuElem("_Quit", "<control>Q", sigc::mem_fun(*this,
&Example_AppWindow::on_menu_item)) );
//Preferences menu:
- Gtk::Menu* pMenuPreferences = Gtk::manage( new Gtk::Menu() );
+ Gtk::Menu* pMenuPreferences = Gtk::make_managed<Gtk::Menu>();
MenuList& list_preferences = pMenuPreferences->items();
// Create a submenu
- Gtk::Menu* pMenuSub_Color = Gtk::manage( new Gtk::Menu());
+ Gtk::Menu* pMenuSub_Color = Gtk::make_managed<Gtk::Menu>();
MenuList& list_sub = pMenuSub_Color->items();
list_sub.push_back( MenuElem("_Red", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );
list_sub.push_back( MenuElem("_Green", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );
@@ -69,7 +69,7 @@ Example_AppWindow::Example_AppWindow()
list_preferences.push_back( MenuElem("_Color", *pMenuSub_Color) );
// Create a submenu
- Gtk::Menu* pMenuSub_Shape = Gtk::manage( new Gtk::Menu());
+ Gtk::Menu* pMenuSub_Shape = Gtk::make_managed<Gtk::Menu>();
list_sub = pMenuSub_Shape->items();
list_sub.push_back( MenuElem("_Square", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );
list_sub.push_back( MenuElem("_Rectangle", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );
@@ -78,7 +78,7 @@ Example_AppWindow::Example_AppWindow()
list_preferences.push_back( MenuElem("_Shape", *pMenuSub_Shape) );
//Help menu:
- Gtk::Menu* pMenuHelp = Gtk::manage( new Gtk::Menu() );
+ Gtk::Menu* pMenuHelp = Gtk::make_managed<Gtk::Menu>();
MenuList& list_help = pMenuHelp->items();
list_help.push_back( MenuElem("_About", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );
diff --git a/demos/gtk-demo/example_buttonbox.cc b/demos/gtk-demo/example_buttonbox.cc
index bbc6163e..809e820e 100644
--- a/demos/gtk-demo/example_buttonbox.cc
+++ b/demos/gtk-demo/example_buttonbox.cc
@@ -69,13 +69,13 @@ Example_ButtonBox::~Example_ButtonBox()
Gtk::Frame* Example_ButtonBox::create_button_box(bool horizontal, const Glib::ustring& title, gint spacing,
Gtk::ButtonBoxStyle layout)
{
- Gtk::Frame* pFrame = Gtk::manage(new Gtk::Frame(title));
+ Gtk::Frame* pFrame = Gtk::make_managed<Gtk::Frame>(title);
Gtk::ButtonBox* pButtonBox = nullptr;
if (horizontal)
- pButtonBox = Gtk::manage(new Gtk::ButtonBox(Gtk::ORIENTATION_HORIZONTAL));
+ pButtonBox = Gtk::make_managed<Gtk::ButtonBox>(Gtk::ORIENTATION_HORIZONTAL);
else
- pButtonBox = Gtk::manage(new Gtk::ButtonBox(Gtk::ORIENTATION_VERTICAL));
+ pButtonBox = Gtk::make_managed<Gtk::ButtonBox>(Gtk::ORIENTATION_VERTICAL);
pButtonBox->set_border_width(5);
pFrame->add(*pButtonBox);
@@ -83,14 +83,14 @@ Gtk::Frame* Example_ButtonBox::create_button_box(bool horizontal, const Glib::us
pButtonBox->set_layout(layout);
pButtonBox->set_spacing(spacing);
- Gtk::Button* pButton = Gtk::manage(new Gtk::Button("_OK", true));
+ Gtk::Button* pButton = Gtk::make_managed<Gtk::Button>("_OK", true);
pButtonBox->add(*pButton);
- pButton = Gtk::manage(new Gtk::Button("_Cancel", true));
+ pButton = Gtk::make_managed<Gtk::Button>("_Cancel", true);
pButtonBox->add(*pButton);
- pButton = Gtk::manage(new Gtk::Button());
- Gtk::Image* pImage = Gtk::manage(new Gtk::Image());
+ pButton = Gtk::make_managed<Gtk::Button>();
+ Gtk::Image* pImage = Gtk::make_managed<Gtk::Image>();
pImage->set_from_icon_name("help-browser", Gtk::ICON_SIZE_BUTTON);
pButton->add(*pImage);
pButtonBox->add(*pButton);
diff --git a/demos/gtk-demo/example_change_display.cc b/demos/gtk-demo/example_change_display.cc
index a5f0fbf0..e5510d43 100644
--- a/demos/gtk-demo/example_change_display.cc
+++ b/demos/gtk-demo/example_change_display.cc
@@ -147,11 +147,11 @@ Example_ChangeDisplay::~Example_ChangeDisplay()
void Example_ChangeDisplay::setup_frame(Gtk::Frame& frame, Gtk::TreeView& treeview, Gtk::Box& buttonbox)
{
- Gtk::Box* pHBox = Gtk::manage( new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 8) );
+ Gtk::Box* pHBox = Gtk::make_managed<Gtk::Box>(Gtk::ORIENTATION_HORIZONTAL, 8);
pHBox->set_border_width(8);
frame.add(*pHBox);
- Gtk::ScrolledWindow* pScrolledWindow = Gtk::manage( new Gtk::ScrolledWindow() );
+ Gtk::ScrolledWindow* pScrolledWindow = Gtk::make_managed<Gtk::ScrolledWindow>();
pScrolledWindow->set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
pScrolledWindow->set_shadow_type(Gtk::SHADOW_IN);
pHBox->pack_start(*pScrolledWindow);
diff --git a/demos/gtk-demo/example_dialog.cc b/demos/gtk-demo/example_dialog.cc
index 8e3e7076..8b960fa9 100644
--- a/demos/gtk-demo/example_dialog.cc
+++ b/demos/gtk-demo/example_dialog.cc
@@ -79,7 +79,7 @@ Example_Dialog::Example_Dialog()
m_VBox.pack_start(m_HBox, Gtk::PACK_SHRINK);
m_Button_Message.signal_clicked().connect(sigc::mem_fun(*this, &Example_Dialog::on_button_message));
m_HBox.pack_start(m_Button_Message, Gtk::PACK_SHRINK);
- m_VBox.pack_start(*(Gtk::manage(new Gtk::Separator(Gtk::ORIENTATION_HORIZONTAL))), Gtk::PACK_SHRINK);
+ m_VBox.pack_start(*Gtk::make_managed<Gtk::Separator>(Gtk::ORIENTATION_HORIZONTAL), Gtk::PACK_SHRINK);
/* Interactive dialog*/
diff --git a/demos/gtk-demo/example_flowbox.cc b/demos/gtk-demo/example_flowbox.cc
index 350220e3..6fe779a4 100644
--- a/demos/gtk-demo/example_flowbox.cc
+++ b/demos/gtk-demo/example_flowbox.cc
@@ -71,8 +71,8 @@ Example_FlowBox::~Example_FlowBox()
Gtk::Button* Example_FlowBox::create_color_swatch(int swatch_i)
{
- Gtk::DrawingArea* drawing_area = Gtk::manage(new Gtk::DrawingArea());
- Gtk::Button* color_swatch = Gtk::manage(new Gtk::Button());
+ Gtk::DrawingArea* drawing_area = Gtk::make_managed<Gtk::DrawingArea>();
+ Gtk::Button* color_swatch = Gtk::make_managed<Gtk::Button>();
drawing_area->set_size_request(24, 24);
diff --git a/demos/gtk-demo/example_iconbrowser.cc b/demos/gtk-demo/example_iconbrowser.cc
index c8fd0a7e..ae2b818f 100644
--- a/demos/gtk-demo/example_iconbrowser.cc
+++ b/demos/gtk-demo/example_iconbrowser.cc
@@ -831,7 +831,7 @@ void Example_IconBrowser::populate()
void Example_IconBrowser::add_context(const Glib::ustring& id,
const Glib::ustring& name, const Glib::ustring& description)
{
- IconContextLabel* row = Gtk::manage(new IconContextLabel(id, name));
+ IconContextLabel* row = Gtk::make_managed<IconContextLabel>(id, name);
row->show();
row->property_margin() = 10;
m_context_list.append(*row);
diff --git a/demos/gtk-demo/example_images.cc b/demos/gtk-demo/example_images.cc
index 53fe4c5b..2cbd4081 100644
--- a/demos/gtk-demo/example_images.cc
+++ b/demos/gtk-demo/example_images.cc
@@ -66,7 +66,7 @@ Example_Images::Example_Images()
m_Frame_Image.set_valign(Gtk::ALIGN_CENTER);
m_VBox.pack_start(m_Frame_Image, Gtk::PACK_SHRINK);
- Gtk::Image* pImage = Gtk::manage(new Gtk::Image());
+ Gtk::Image* pImage = Gtk::make_managed<Gtk::Image>();
pImage->set_from_resource("/images/gtk-logo-rgb.gif");
m_Frame_Image.add(*pImage);
@@ -81,7 +81,7 @@ Example_Images::Example_Images()
m_Frame_Animation.set_valign(Gtk::ALIGN_CENTER);
m_VBox.pack_start(m_Frame_Animation, Gtk::PACK_SHRINK);
- pImage = Gtk::manage(new Gtk::Image());
+ pImage = Gtk::make_managed<Gtk::Image>();
pImage->set_from_resource("/images/floppybuddy.gif");
m_Frame_Animation.add(*pImage);
diff --git a/demos/gtk-demo/example_menus.cc b/demos/gtk-demo/example_menus.cc
index 9a5be0ae..e23b0eea 100644
--- a/demos/gtk-demo/example_menus.cc
+++ b/demos/gtk-demo/example_menus.cc
@@ -67,17 +67,17 @@ Example_Menus::Example_Menus()
{
//Note:: It's generally easier to use the Gtk::Builder API.
- Gtk::MenuItem* pMenuItem = Gtk::manage(new Gtk::MenuItem("test\nline2"));
+ Gtk::MenuItem* pMenuItem = Gtk::make_managed<Gtk::MenuItem>("test\nline2");
pMenuItem->set_submenu( *(create_menu(2)) );
m_MenuBar.append(*pMenuItem);
pMenuItem->show();
- pMenuItem = Gtk::manage(new Gtk::MenuItem("foo"));
+ pMenuItem = Gtk::make_managed<Gtk::MenuItem>("foo");
pMenuItem->set_submenu( *(create_menu(3)) );
m_MenuBar.append(*pMenuItem);
pMenuItem->show();
- pMenuItem = Gtk::manage(new Gtk::MenuItem("bar"));
+ pMenuItem = Gtk::make_managed<Gtk::MenuItem>("bar");
pMenuItem->set_submenu( *(create_menu(4)) );
m_MenuBar.append(*pMenuItem);
pMenuItem->show();
@@ -93,16 +93,16 @@ Example_Menus::Example_Menus()
auto pMenu = create_menu(1);
pMenu->set_accel_group(accel_group);
- Gtk::MenuItem* pMenuItem = Gtk::manage(new Gtk::SeparatorMenuItem());
+ Gtk::MenuItem* pMenuItem = Gtk::make_managed<Gtk::SeparatorMenuItem>();
pMenu->append(*pMenuItem);
pMenuItem->show();
- pMenuItem = Gtk::manage(new Gtk::MenuItem("accel"));
+ pMenuItem = Gtk::make_managed<Gtk::MenuItem>("accel");
pMenuItem->set_submenu(*pMenu);
m_MenuBar.append(*pMenuItem);
pMenuItem->show();
- pMenuItem = Gtk::manage(new Gtk::CheckMenuItem("Accelerate Me"));
+ pMenuItem = Gtk::make_managed<Gtk::CheckMenuItem>("Accelerate Me");
pMenuItem->add_accelerator("activate", accel_group, GDK_KEY_F1,
Gdk::ModifierType(0), Gtk::ACCEL_VISIBLE);
pMenu->append(*pMenuItem);
@@ -110,7 +110,7 @@ Example_Menus::Example_Menus()
pMenuItem->signal_activate().connect(
sigc::bind(sigc::mem_fun(*this, &Example_Menus::on_item_activated), "F1"));
- pMenuItem = Gtk::manage(new Gtk::CheckMenuItem("Accelerator Locked"));
+ pMenuItem = Gtk::make_managed<Gtk::CheckMenuItem>("Accelerator Locked");
pMenu->append(*pMenuItem);
pMenuItem->add_accelerator("activate", accel_group, GDK_KEY_F2,
Gdk::ModifierType(0), Gtk::ACCEL_VISIBLE | Gtk::ACCEL_LOCKED);
@@ -118,7 +118,7 @@ Example_Menus::Example_Menus()
pMenuItem->signal_activate().connect(
sigc::bind(sigc::mem_fun(*this, &Example_Menus::on_item_activated), "F2"));
- pMenuItem = Gtk::manage(new Gtk::CheckMenuItem("Accelerator Frozen"));
+ pMenuItem = Gtk::make_managed<Gtk::CheckMenuItem>("Accelerator Frozen");
pMenu->append(*pMenuItem);
pMenuItem->add_accelerator("activate", accel_group, GDK_KEY_F3,
Gdk::ModifierType(0), Gtk::ACCEL_VISIBLE);
@@ -150,7 +150,7 @@ Gtk::Menu* Example_Menus::create_menu(gint depth)
if (depth < 1)
return nullptr;
- Gtk::Menu* pMenu = Gtk::manage(new Gtk::Menu());
+ Gtk::Menu* pMenu = Gtk::make_managed<Gtk::Menu>();
{
Gtk::RadioMenuItem::Group radiogroup;
@@ -160,7 +160,7 @@ Gtk::Menu* Example_Menus::create_menu(gint depth)
char buf[32];
sprintf(buf, "item %2d - %d", depth, j);
- Gtk::MenuItem* pMenuItem = Gtk::manage(new Gtk::RadioMenuItem(radiogroup, buf));
+ Gtk::MenuItem* pMenuItem = Gtk::make_managed<Gtk::RadioMenuItem>(radiogroup, buf);
pMenu->append(*pMenuItem);
pMenuItem->show();
diff --git a/demos/gtk-demo/example_panes.cc b/demos/gtk-demo/example_panes.cc
index c49d88b0..1c26bb2b 100644
--- a/demos/gtk-demo/example_panes.cc
+++ b/demos/gtk-demo/example_panes.cc
@@ -63,7 +63,7 @@ Example_Panes::Example_Panes()
pHPaned->add1(*Gtk::manage(pFrame1));
pFrame1->set_shadow_type(Gtk::SHADOW_IN);
pFrame1->set_size_request(60, 60);
- pFrame1->add(*Gtk::manage(new Gtk::Button("_Hi there", true)));
+ pFrame1->add(*Gtk::make_managed<Gtk::Button>("_Hi there", true));
Gtk::Frame *const pFrame2 = new Gtk::Frame();
pHPaned->add2(*Gtk::manage(pFrame2));
@@ -76,8 +76,8 @@ Example_Panes::Example_Panes()
pFrame3->set_size_request(60, 80);
// Now create check buttons to control sizing
- pVBox->pack_start(*Gtk::manage(new PaneOptions(*pHPaned, "Horizontal", "Left", "Right")),
Gtk::PACK_SHRINK);
- pVBox->pack_start(*Gtk::manage(new PaneOptions(*pVPaned, "Vertical", "Top", "Bottom")),
Gtk::PACK_SHRINK);
+ pVBox->pack_start(*Gtk::make_managed<PaneOptions>(*pHPaned, "Horizontal", "Left", "Right"),
Gtk::PACK_SHRINK);
+ pVBox->pack_start(*Gtk::make_managed<PaneOptions>(*pVPaned, "Vertical", "Top", "Bottom"),
Gtk::PACK_SHRINK);
show_all();
}
@@ -101,8 +101,8 @@ PaneOptions::PaneOptions(Gtk::Paned& paned, const Glib::ustring& frame_label,
Gtk::Grid *const pGrid = new Gtk::Grid();
add(*Gtk::manage(pGrid));
- pGrid->attach(*Gtk::manage(new Gtk::Label(label1)), 0, 0, 1, 1);
- pGrid->attach(*Gtk::manage(new Gtk::Label(label2)), 1, 0, 1, 1);
+ pGrid->attach(*Gtk::make_managed<Gtk::Label>(label1), 0, 0, 1, 1);
+ pGrid->attach(*Gtk::make_managed<Gtk::Label>(label2), 1, 0, 1, 1);
pGrid->attach(m_CheckButton_resize1, 0, 1, 1, 1);
pGrid->attach(m_CheckButton_shrink1, 0, 2, 1, 1);
diff --git a/demos/gtk-demo/example_sizegroup.cc b/demos/gtk-demo/example_sizegroup.cc
index aa3e9040..065313c7 100644
--- a/demos/gtk-demo/example_sizegroup.cc
+++ b/demos/gtk-demo/example_sizegroup.cc
@@ -133,7 +133,7 @@ void Example_SizeGroup::add_row(Gtk::Grid& grid, int row,
const Glib::ustring& label_text,
const std::list<Glib::ustring>& options)
{
- Gtk::Label* pLabel = Gtk::manage(new Gtk::Label(label_text, Gtk::ALIGN_START, Gtk::ALIGN_END, true));
+ Gtk::Label* pLabel = Gtk::make_managed<Gtk::Label>(label_text, Gtk::ALIGN_START, Gtk::ALIGN_END, true);
grid.attach(*pLabel, 0, row, 1, 1);
pLabel->set_hexpand();
@@ -149,7 +149,7 @@ void Example_SizeGroup::add_row(Gtk::Grid& grid, int row,
*/
Gtk::ComboBoxText* Example_SizeGroup::create_combobox(const std::list<Glib::ustring>& strings)
{
- Gtk::ComboBoxText* pCombo = Gtk::manage( new Gtk::ComboBoxText() );
+ Gtk::ComboBoxText* pCombo = Gtk::make_managed<Gtk::ComboBoxText>();
for(const auto& str : strings)
{
diff --git a/demos/gtk-demo/example_stacksidebar.cc b/demos/gtk-demo/example_stacksidebar.cc
index 8fca65a2..4120839f 100644
--- a/demos/gtk-demo/example_stacksidebar.cc
+++ b/demos/gtk-demo/example_stacksidebar.cc
@@ -64,14 +64,14 @@ Example_StackSidebar::Example_StackSidebar()
Gtk::Widget* widget = nullptr;
if (i == 0)
{
- Gtk::Image* image = Gtk::manage(new Gtk::Image());
+ Gtk::Image* image = Gtk::make_managed<Gtk::Image>();
image->set_from_icon_name("help-about", Gtk::ICON_SIZE_MENU);
image->set_pixel_size(256);
widget = image;
}
else
{
- widget = Gtk::manage(new Gtk::Label(m_page_names[i]));
+ widget = Gtk::make_managed<Gtk::Label>(m_page_names[i]);
}
m_Stack.add(*widget, m_page_names[i], m_page_names[i]);
}
diff --git a/demos/gtk-demo/example_textview.cc b/demos/gtk-demo/example_textview.cc
index ea13b764..078c49aa 100644
--- a/demos/gtk-demo/example_textview.cc
+++ b/demos/gtk-demo/example_textview.cc
@@ -77,7 +77,7 @@ Example_TextView::Example_TextView()
* a view widget.
*/
Glib::RefPtr<Gtk::TextBuffer> refBuffer = m_View1.get_buffer();
- m_pView2 = Gtk::manage( new Gtk::TextView(refBuffer) );
+ m_pView2 = Gtk::make_managed<Gtk::TextView>(refBuffer);
m_ScrolledWindow1.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
m_VPaned.add1(m_ScrolledWindow1);
@@ -384,13 +384,13 @@ void Example_TextView::attach_widgets(Gtk::TextView& text_view)
Gtk::Widget* pWidget = nullptr;
if (i == 0)
{
- Gtk::Button* pButton = Gtk::manage( new Gtk::Button("Click Me") );
+ Gtk::Button* pButton = Gtk::make_managed<Gtk::Button>("Click Me");
pButton->signal_clicked().connect(sigc::mem_fun(*this, &Example_TextView::on_button_clicked));
pWidget = pButton;
}
else if (i == 1)
{
- Gtk::ComboBoxText* pCombo = Gtk::manage( new Gtk::ComboBoxText() );
+ Gtk::ComboBoxText* pCombo = Gtk::make_managed<Gtk::ComboBoxText>();
pCombo->append("Option 1");
pCombo->append("Option 2");
pCombo->append("Option 3");
@@ -399,7 +399,7 @@ void Example_TextView::attach_widgets(Gtk::TextView& text_view)
}
else if (i == 2)
{
- Gtk::Scale* pHScale = Gtk::manage( new Gtk::Scale(Gtk::ORIENTATION_HORIZONTAL) );
+ Gtk::Scale* pHScale = Gtk::make_managed<Gtk::Scale>(Gtk::ORIENTATION_HORIZONTAL);
pHScale->set_range(0, 100);
pHScale->set_size_request(70, -1);
@@ -407,13 +407,13 @@ void Example_TextView::attach_widgets(Gtk::TextView& text_view)
}
else if (i == 3)
{
- Gtk::Image* pImage = Gtk::manage( new Gtk::Image() );
+ Gtk::Image* pImage = Gtk::make_managed<Gtk::Image>();
pImage->set_from_resource("/textview/floppybuddy.gif");
pWidget = pImage;
}
else if (i == 4)
{
- pWidget = Gtk::manage( new Gtk::Entry() );
+ pWidget = Gtk::make_managed<Gtk::Entry>();
}
else
{
@@ -447,7 +447,7 @@ Window_EasterEgg::Window_EasterEgg()
Glib::RefPtr<Gtk::TextChildAnchor> refAnchor = refBuffer->create_child_anchor(iter);
refBuffer->insert(refBuffer->end(), "\nDon't do this in real applications, please.\n");
- m_pTextView = Gtk::manage( new Gtk::TextView(refBuffer) );
+ m_pTextView = Gtk::make_managed<Gtk::TextView>(refBuffer);
recursive_attach_view(0, *m_pTextView, refAnchor);
@@ -465,10 +465,10 @@ void Window_EasterEgg::recursive_attach_view(int depth, Gtk::TextView& view, Gli
if (depth > 4)
return;
- Gtk::TextView* pChildView = Gtk::manage( new Gtk::TextView(view.get_buffer()));
+ Gtk::TextView* pChildView = Gtk::make_managed<Gtk::TextView(view.get_buffer>());
/* Frame is to add a black border around each child view */
- Gtk::Frame* pFrame = Gtk::manage( new Gtk::Frame());
+ Gtk::Frame* pFrame = Gtk::make_managed<Gtk::Frame>();
pFrame->add(*pChildView);
diff --git a/demos/gtk-demo/example_treeview_editable_cells.cc
b/demos/gtk-demo/example_treeview_editable_cells.cc
index 9d7a306b..c80b1714 100644
--- a/demos/gtk-demo/example_treeview_editable_cells.cc
+++ b/demos/gtk-demo/example_treeview_editable_cells.cc
@@ -250,7 +250,7 @@ void Example_TreeView_EditableCells::add_columns()
//And this is the way that works with the IRIX MipsPro compiler too:
{
- Gtk::TreeView::Column* pViewColumn = Gtk::manage(new Gtk::TreeView::Column("Number", m_columns.number));
+ Gtk::TreeView::Column* pViewColumn = Gtk::make_managed<Gtk::TreeView::Column>("Number",
m_columns.number);
//connect signal handlers for auto-storing of edited cell data
Gtk::CellRenderer* pCellRenderer = pViewColumn->get_first_cell();
@@ -268,7 +268,7 @@ void Example_TreeView_EditableCells::add_columns()
}
{
- Gtk::TreeView::Column* pViewColumn = Gtk::manage(new Gtk::TreeView::Column("Product",
m_columns.product));
+ Gtk::TreeView::Column* pViewColumn = Gtk::make_managed<Gtk::TreeView::Column>("Product",
m_columns.product);
//connect signal handlers for auto-storing of edited cell data
Gtk::CellRenderer* pCellRenderer = pViewColumn->get_first_cell();
diff --git a/gtk/gtkmm/object.h b/gtk/gtkmm/object.h
index a9bd4af7..480a0387 100644
--- a/gtk/gtkmm/object.h
+++ b/gtk/gtkmm/object.h
@@ -22,6 +22,8 @@
#include <gtkmm/base.h>
#include <gtkmmconfig.h>
+#include <utility>
+
namespace Gtk
{
@@ -44,6 +46,26 @@ T* manage(T* obj)
return obj;
}
+/** Create a Gtk::Object and calls Gtk::manage() called on it, in a single step.
+ * This matches standard functions like std::make_unique<T>(args) and avoids you
+ * manually invoking the new operator, which is discouraged in modern C++ style.
+ *
+ * For instance,
+ * @code
+ * Gtk::Button* button = Gtk::make_managed<Gtk::Button>("Hello");
+ * vbox.pack_start(*button); //vbox will delete button when vbox is deleted.
+ * @endcode
+ *
+ * @param args Arguments to pass to the constructor of the given template type.
+ * @result A new, managed object of that type, constructed with those arguments.
+ */
+template<class T, class... T_Args>
+T* make_managed(T_Args&&... args)
+{
+ T* obj = new T(std::forward<T_Args>(args)...);
+ return manage(obj);
+}
+
/** Gtk::Object is the base class for all widgets, and for a few non-widget objects such as
* Gtk::Adjustment. Gtk::Object predates Glib::Object; non-widgets that derive from Gtk::Object
* rather than Glib::Object do so for backward compatibility reasons.
diff --git a/tests/child_widget_managed/main.cc b/tests/child_widget_managed/main.cc
index 04895ce3..c6f0ec53 100644
--- a/tests/child_widget_managed/main.cc
+++ b/tests/child_widget_managed/main.cc
@@ -31,7 +31,7 @@ ExampleWindow::ExampleWindow()
{
set_default_size(150, 150);
- m_button = manage(new MyButton);
+ m_button = Gtk::make_managed<MyButton>();
add(*m_button);
show_all_children();
diff --git a/tests/delete_cpp_child/main.cc b/tests/delete_cpp_child/main.cc
index 72ae7fb0..34321800 100644
--- a/tests/delete_cpp_child/main.cc
+++ b/tests/delete_cpp_child/main.cc
@@ -19,15 +19,13 @@ private:
AppWindow::AppWindow()
: m_label (nullptr)
{
- Gtk::Box* vbox = Gtk::manage(new Gtk::Box (Gtk::ORIENTATION_VERTICAL, 5));
+ Gtk::Box* vbox = Gtk::make_managed<Gtk::Box>(Gtk::ORIENTATION_VERTICAL, 5);
add(*vbox);
- Gtk::Button* button = Gtk::manage(new Gtk::Button("Delete Label"));
-
+ Gtk::Button* button = Gtk::make_managed<Gtk::Button>("Delete Label");
vbox->pack_start(*button, Gtk::PACK_SHRINK);
-
- //m_label = manage (new Gtk::Label ("test"));
+ //m_label = Gtk::make_managed<Gtk::Label>("test");
m_label = new Gtk::Label("test");
g_warning("m_label -> ref_count: %d\n", G_OBJECT(m_label->gobj())->ref_count);
vbox->pack_start(*m_label, Gtk::PACK_SHRINK);
diff --git a/tests/dialog/main.cc b/tests/dialog/main.cc
index 9d2e097b..39e7bea5 100644
--- a/tests/dialog/main.cc
+++ b/tests/dialog/main.cc
@@ -19,15 +19,13 @@ private:
AppWindow::AppWindow()
: m_label (nullptr)
{
- Gtk::Box* vbox = manage (new Gtk::Box (Gtk::ORIENTATION_VERTICAL, 5));
+ Gtk::Box* vbox = Gtk::make_managed<Gtk::Box>(Gtk::ORIENTATION_VERTICAL, 5);
add(*vbox);
- Gtk::Button* button = Gtk::manage (new Gtk::Button ("Delete Label"));
-
+ Gtk::Button* button = Gtk::make_managed<Gtk::Button>("Delete Label");
vbox->pack_start(*button, Gtk::PACK_SHRINK);
-
- //m_label = manage (new Gtk::Label ("test"));
+ //m_label = Gtk::make_managed<Gtk::Label>("test");
m_label = new Gtk::Label ("test");
g_warning("m_label -> ref_count: %d\n", G_OBJECT(m_label->gobj())->ref_count);
vbox->pack_start(*m_label, Gtk::PACK_SHRINK);
diff --git a/tests/dialog_deletethis/main.cc b/tests/dialog_deletethis/main.cc
index ad0f39cf..f71f5e2f 100644
--- a/tests/dialog_deletethis/main.cc
+++ b/tests/dialog_deletethis/main.cc
@@ -10,7 +10,7 @@ class Dlg : public sigc::trackable
Dlg()
{
dlg_ = new Gtk::Dialog("Test Dialog");
- Gtk::Button *btn = manage(new Gtk::Button("ClickMe"));
+ Gtk::Button *btn = Gtk::make_managed<Gtk::Button>("ClickMe");
btn->signal_clicked().connect(sigc::mem_fun(*this, &Dlg::on_button_clicked));
dlg_->get_content_area()->pack_start(*btn);
dlg_->add_button("_OK", Gtk::RESPONSE_OK);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]