[gtkmm-documentation/wip/dboles/make_managed-etc-4] Use auto more, removing lots of verbose type noise
- From: Daniel Boles <dboles src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtkmm-documentation/wip/dboles/make_managed-etc-4] Use auto more, removing lots of verbose type noise
- Date: Wed, 24 Oct 2018 19:25:31 +0000 (UTC)
commit b82ae4d3da00c1c34df1cc8d63f4fc2ea58b1ecb
Author: Daniel Boles <dboles src gmail com>
Date: Wed Oct 24 20:24:32 2018 +0100
Use auto more, removing lots of verbose type noise
docs/tutorial/C/index-in.docbook | 183 +++++++++++++++++++--------------------
1 file changed, 88 insertions(+), 95 deletions(-)
---
diff --git a/docs/tutorial/C/index-in.docbook b/docs/tutorial/C/index-in.docbook
index 7cc9341..9566929 100644
--- a/docs/tutorial/C/index-in.docbook
+++ b/docs/tutorial/C/index-in.docbook
@@ -491,8 +491,8 @@ To use a >kmm; instance with a C function that requires a C
<function>gobj()</function> function to obtain a pointer to the underlying C
instance. For example:
<programlisting>
-Gtk::Button* button = new Gtk::Button("example");
-gtk_button_do_something_that_gtkmm_cannot(button->gobj());
+Gtk::Button button("example");
+gtk_button_do_something_that_gtkmm_cannot(button.gobj());
</programlisting>
</para>
@@ -988,9 +988,9 @@ public:
RadioButtons::RadioButtons()
{
Gtk::RadioButton::Group group;
- Gtk::RadioButton* m_rb1 = Gtk::make_managed<Gtk::RadioButton>(group, "button1");
- Gtk::RadioButton* m_rb2 = Gtk::make_managed<Gtk::RadioButton>(group, "button2");
- Gtk::RadioButton* m_rb3 = Gtk::make_managed<Gtk::RadioButton>(group, "button3");
+ auto m_rb1 = Gtk::make_managed<Gtk::RadioButton>(group, "button1");
+ auto m_rb2 = Gtk::make_managed<Gtk::RadioButton>(group, "button2");
+ auto m_rb3 = Gtk::make_managed<Gtk::RadioButton>(group, "button3");
}</programlisting>
<para>
@@ -2408,17 +2408,17 @@ on-demand, whenever the first model accesses it.
Add rows to the model with the <methodname>append()</methodname>,
<methodname>prepend()</methodname>, or <methodname>insert()</methodname> methods.
</para>
-<programlisting>Gtk::TreeModel::iterator iter = m_refListStore->append();</programlisting>
+<programlisting>auto iter = m_refListStore->append();</programlisting>
<para>You can dereference the iterator to get the Row:
</para>
-<programlisting>Gtk::TreeModel::Row row = *iter;</programlisting>
+<programlisting>auto row = *iter;</programlisting>
<sect3 id="treeview-adding-child-rows"><title>Adding child rows</title>
<para>
<classname>Gtk::TreeStore</classname> models can have child items. Add them
with the <methodname>append()</methodname>, <methodname>prepend()</methodname>, or
<methodname>insert()</methodname> methods, like so:
</para>
-<programlisting>Gtk::TreeModel::iterator iter_child =
+<programlisting>auto iter_child =
m_refTreeStore->append(row.children());</programlisting>
</sect3>
@@ -2441,8 +2441,8 @@ You can use the <methodname>operator[]</methodname> overload to get the data in
particular column in a row, specifying the
<classname>TreeModelColumn</classname> used to create the model.
</para>
-<programlisting>Glib::ustring strText = row[m_Columns.m_col_text];
-int number = row[m_Columns.m_col_number];</programlisting>
+<programlisting>auto strText = row[m_Columns.m_col_text];
+auto number = row[m_Columns.m_col_number];</programlisting>
<para>
The compiler will complain if you use an inappropriate type. For
instance, this would generate a compiler error:
@@ -2523,8 +2523,8 @@ appropriate <classname>Gtk::TreeView::Column</classname> widget.
<para>
Here is some example code, which has a pixbuf icon and a text name in the same column:
</para>
-<programlisting>Gtk::TreeView::Column* pColumn =
- Gtk::make_managed<Gtk::TreeView::Column>("Icon Name");
+<programlisting>
+auto pColumn = Gtk::make_managed<Gtk::TreeView::Column>("Icon Name");
// m_columns.icon and m_columns.iconname are columns in the model.
// pColumn is the column in the TreeView:
@@ -2544,12 +2544,11 @@ instance, this example code from
<classname>Gtk::CellRenderer</classname> widget and instructs it to render the
data from various model columns through various aspects of its appearance.
</para>
-<programlisting>int cols_count = m_TreeView.append_column_editable("Alex", m_columns.alex);
-Gtk::TreeViewColumn* pColumn = m_TreeView.get_column(cols_count-1);
+<programlisting>auto cols_count = m_TreeView.append_column_editable("Alex", m_columns.alex);
+auto pColumn = m_TreeView.get_column(cols_count-1);
if(pColumn)
{
- Gtk::CellRendererToggle* pRenderer =
- static_cast<Gtk::CellRendererToggle*>(pColumn->get_first_cell());
+ auto pRenderer = static_cast<Gtk::CellRendererToggle*>(pColumn->get_first_cell());
pColumn->add_attribute(pRenderer->property_visible(), m_columns.visible);
pColumn->add_attribute(pRenderer->property_activatable(), m_columns.world);</programlisting>
@@ -2557,8 +2556,8 @@ if(pColumn)
You can also connect to <classname>CellRenderer</classname> signals to detect user
actions. For instance:
</para>
-<programlisting>Gtk::CellRendererToggle* pRenderer =
- Gtk::make_managed<Gtk::CellRendererToggle>();
+<programlisting>
+auto pRenderer = Gtk::make_managed<Gtk::CellRendererToggle>();
pRenderer->signal_toggled().connect(
sigc::bind( sigc::mem_fun(*this,
&Example_TreeView_TreeStore::on_cell_toggled), m_columns.dave)
@@ -2635,12 +2634,18 @@ children, via the <methodname>children()</methodname> method. You can use the
familiar <methodname>begin()</methodname> and <methodname>end()</methodname> methods
iterator incrementing, like so:
</para>
-<programlisting>typedef Gtk::TreeModel::Children type_children; //minimise code length.
-type_children children = refModel->children();
-for(type_children::iterator iter = children.begin();
- iter != children.end(); ++iter)
+<programlisting>
+auto children = refModel->children();
+for (auto iter = children.begin(), end = children.end(); iter != end; ++iter)
+{
+ auto row = *iter;
+ //Do something with the row - see above for set/get.
+}</programlisting>
+If you always want to iterate across the entire range, much more succinct syntax
+is possible using C++'s range-based <literal>for</literal> loop:
+<programlisting>
+for (auto row: refModel->children())
{
- Gtk::TreeModel::Row row = *iter;
//Do something with the row - see above for set/get.
}</programlisting>
@@ -2663,8 +2668,7 @@ To find out what rows the user has selected, get the
<classname>Gtk::TreeView::Selection</classname> object from the
<classname>TreeView</classname>, like so:
</para>
-<programlisting>Glib::RefPtr<Gtk::TreeSelection> refTreeSelection =
- m_TreeView.get_selection();</programlisting>
+<programlisting>auto refTreeSelection = m_TreeView.get_selection();</programlisting>
<sect2 id="treeview-selection-mode">
<title>Single or multiple selection</title>
@@ -2681,10 +2685,10 @@ multiple selection by setting the mode, like so:
For single-selection, you can just call <methodname>get_selected()</methodname>,
like so:
</para>
-<programlisting>Gtk::TreeModel::iterator iter = refTreeSelection->get_selected();
+<programlisting>auto iter = refTreeSelection->get_selected();
if(iter) //If anything is selected
{
- Gtk::TreeModel::Row row = *iter;
+ auto row = *iter;
//Do something with the row.
}</programlisting>
@@ -2701,7 +2705,7 @@ or define a callback, and give it to
void TheClass::selected_row_callback(
const Gtk::TreeModel::const_iterator& iter)
{
- TreeModel::ConstRow row = *iter;
+ auto row = *iter;
//Do something with the row.
}</programlisting>
@@ -2739,7 +2743,7 @@ and then
const Glib::RefPtr<Gtk::TreeModel>& model,
const Gtk::TreeModel::Path& path, bool)
{
- const Gtk::TreeModel::iterator iter = model->get_iter(path);
+ const auto iter = model->get_iter(path);
return iter->children().empty(); // only allow leaf nodes to be selected
}</programlisting>
</sect2>
@@ -2751,13 +2755,13 @@ To change the selection, specify a
<classname>Gtk::TreeModel::iterator</classname> or
<classname>Gtk::TreeModel::Row</classname>, like so:
</para>
-<programlisting>Gtk::TreeModel::Row row = m_refModel->children()[5]; //The sixth row.
+<programlisting>auto row = m_refModel->children()[5]; //The sixth row.
if(row)
refTreeSelection->select(row.get_iter());</programlisting>
<para>
or
</para>
-<programlisting>Gtk::TreeModel::iterator iter = m_refModel->children().begin()
+<programlisting>auto iter = m_refModel->children().begin()
if(iter)
refTreeSelection->select(iter);</programlisting>
</sect2>
@@ -2778,7 +2782,7 @@ The standard tree models (<classname>TreeStore</classname> and <classname>ListSt
<para>
So that a user can click on a <classname>TreeView</classname>'s column header to sort the
<classname>TreeView</classname>'s contents, call
<methodname>Gtk::TreeView::Column::set_sort_column()</methodname>, supplying the model column on which model
should be sorted when the header is clicked. For instance:
</para>
-<programlisting>Gtk::TreeView::Column* pColumn = treeview.get_column(0);
+<programlisting>auto pColumn = treeview.get_column(0);
if(pColumn)
pColumn->set_sort_column(m_columns.m_col_id);</programlisting>
</sect2>
@@ -2793,8 +2797,7 @@ for instance, <methodname>Gtk::TreeViewColumn::set_sort_column()</methodname>.
<classname>TreeModelSort</classname> is a model that contains another model, presenting a sorted version
of that model. For instance, you might add a sorted version of a model to a <classname>TreeView</classname>
like so:
</para>
-<programlisting>Glib::RefPtr<Gtk::TreeModelSort> sorted_model =
- Gtk::TreeModelSort::create(model);
+<programlisting>auto sorted_model = Gtk::TreeModelSort::create(model);
sorted_model->set_sort_column(columns.m_col_name, Gtk::SORT_ASCENDING);
treeview.set_model(sorted_model);</programlisting>
@@ -2802,16 +2805,13 @@ treeview.set_model(sorted_model);</programlisting>
</para>
<programlisting>void ExampleWindow::on_button_delete()
{
- Glib::RefPtr<Gtk::TreeSelection> refTreeSelection =
- m_treeview.get_selection();
+ auto refTreeSelection = m_treeview.get_selection();
if(refTreeSelection)
{
- Gtk::TreeModel::iterator sorted_iter =
- m_refTreeSelection->get_selected();
+ auto sorted_iter = m_refTreeSelection->get_selected();
if(sorted_iter)
{
- Gtk::TreeModel::iterator iter =
- m_refModelSort->convert_iter_to_child_iter(sorted_iter);
+ auto iter = m_refModelSort->convert_iter_to_child_iter(sorted_iter);
m_refModel->erase(iter);
}
}
@@ -3023,11 +3023,11 @@ ModelColumns m_columns;</programlisting>
<programlisting>Gtk::TreeModel::iterator iter = m_Combo.get_active();
if(iter)
{
- Gtk::TreeModel::Row row = *iter;
+ auto row = *iter;
//Get the data for the selected row, using our knowledge
//of the tree model:
- int id = row[m_Columns.m_col_id];
+ auto id = row[m_Columns.m_col_id];
set_something_id_chosen(id); //Your own function.
}
else
@@ -3100,7 +3100,7 @@ moving the keyboard focus to another widget may signal that the user has finishe
entering text. To be notified of these events, connect to the
<classname>Entry</classname>'s <literal>activate</literal> and
<literal>focus_out_event</literal> signals, like so
-<programlisting>Gtk::Entry* entry = m_Combo.get_entry();
+<programlisting>auto entry = m_Combo.get_entry();
if (entry)
{
// Alternatively you can connect to m_Combo.signal_changed().
@@ -3207,8 +3207,7 @@ across buffer modifications. To preserve a position, use <classname>Gtk::TextBuf
<para>
To specify that some text in the buffer should have specific formatting, you must define a tag to hold that
formatting information, and then apply that tag to the region of text. For instance, to define the tag and
its properties:
</para>
-<programlisting>Glib::RefPtr<Gtk::TextBuffer::Tag> refTagMatch =
- Gtk::TextBuffer::Tag::create();
+<programlisting>auto refTagMatch = Gtk::TextBuffer::Tag::create();
refTagMatch->property_background() = "orange";</programlisting>
<para>
You can specify a name for the <classname>Tag</classname> when using the
@@ -3234,13 +3233,11 @@ Each <classname>Gtk::TextBuffer</classname> uses a
<classname>TagTable</classname>. When you create <classname>Tag</classname>s
you should add them to the <classname>TagTable</classname>. For instance:
</para>
-<programlisting>Glib::RefPtr<Gtk::TextBuffer::TagTable> refTagTable =
- Gtk::TextBuffer::TagTable::create();
+<programlisting>auto refTagTable = Gtk::TextBuffer::TagTable::create();
refTagTable->add(refTagMatch);
//Hopefully a future version of >kmm; will have a set_tag_table() method,
//for use after creation of the buffer.
-Glib::RefPtr<Gtk::TextBuffer> refBuffer =
- Gtk::TextBuffer::create(refTagTable);</programlisting>
+auto refBuffer = Gtk::TextBuffer::create(refTagTable);</programlisting>
<para>
You can also use <methodname>get_tag_table()</methodname> to get, and maybe modify,
@@ -3285,8 +3282,7 @@ conflicts by using <methodname>Tag::set_priority()</methodname>.
text changes, but you can use a <classname>Gtk::TextBuffer::Mark</classname> to
remember a position in these situations. For instance,
</para>
-<programlisting>Glib::RefPtr<Gtk::TextBuffer::Mark> refMark =
- refBuffer->create_mark(iter);</programlisting>
+<programlisting>auto refMark = refBuffer->create_mark(iter);</programlisting>
<para>
You can then use the <methodname>get_iter()</methodname> method later to create an
@@ -3359,8 +3355,7 @@ ChildAnchors are associated with <classname>iterators</classname>. For
instance, to create a child anchor at a particular position, use
<methodname>Gtk::TextBuffer::create_child_anchor()</methodname>:
</para>
-<programlisting>Glib::RefPtr<Gtk::TextChildAnchor> refAnchor =
- refBuffer->create_child_anchor(iter);</programlisting>
+<programlisting>auto refAnchor = refBuffer->create_child_anchor(iter);</programlisting>
<para>
Then, to add a widget at that position, use
@@ -3460,7 +3455,7 @@ by using <methodname>Gtk::Application::set_accel_for_action()</methodname>.
<para>For instance,
</para>
<programlisting>
-<![CDATA[Glib::RefPtr<Gtk::Builder> m_refBuilder = Gtk::Builder::create();
+<![CDATA[m_refBuilder = Gtk::Builder::create();
app->set_accel_for_action("example.new", "<Primary>n");
app->set_accel_for_action("example.quit", "<Primary>q");
@@ -3482,7 +3477,7 @@ string" uses an XML format, in which you should mention the names of the
actions that you have already created. For instance:
</para>
<programlisting>
-<![CDATA[const char* ui_info =
+<![CDATA[Glib::ustring ui_info =
"<interface>"
" <menu id='menubar'>"
" <submenu>"
@@ -3534,9 +3529,9 @@ the <methodname>Builder::get_object()</methodname> and
to a container. For instance:
</para>
<programlisting>
-<![CDATA[Glib::RefPtr<Glib::Object> object = m_refBuilder->get_object("menubar");
-Glib::RefPtr<Gio::Menu> gmenu = Glib::RefPtr<Gio::Menu>::cast_dynamic(object);
-Gtk::MenuBar* pMenuBar = Gtk::make_managed<Gtk::MenuBar>(gmenu);
+<![CDATA[auto object = m_refBuilder->get_object("menubar");
+auto gmenu = Glib::RefPtr<Gio::Menu>::cast_dynamic(object);
+auto pMenuBar = Gtk::make_managed<Gtk::MenuBar>(gmenu);
m_Box.pack_start(*pMenuBar, Gtk::PACK_SHRINK);
Gtk::Toolbar* toolbar = nullptr;
@@ -3580,8 +3575,8 @@ mouse button.
m_refBuilder->add_from_string(ui_info);
-Glib::RefPtr<Glib::Object> object = m_refBuilder->get_object("menu-examplepopup");
-Glib::RefPtr<Gio::Menu> gmenu = Glib::RefPtr<Gio::Menu>::cast_dynamic(object);
+auto object = m_refBuilder->get_object("menu-examplepopup");
+auto gmenu = Glib::RefPtr<Gio::Menu>::cast_dynamic(object);
m_pMenuPopup = new Gtk::Menu(gmenu);]]>
</programlisting>
@@ -4128,8 +4123,8 @@ search-and-replace dialog is non-modal.
</para>
<programlisting>
<![CDATA[Gtk::DrawingArea myArea;
-Glib::RefPtr<Gdk::CairoContext> gdkCairoContext = myArea.get_surface()->create_cairo_context();
-Cairo::RefPtr<Cairo::Context> myContext = gdkCairoContext->cairo_create();
+auto gdkCairoContext = myArea.get_surface()->create_cairo_context();
+auto myContext = gdkCairoContext->cairo_create();
myContext->set_source_rgb(1.0, 0.0, 0.0);
myContext->set_line_width(2.0);]]>
</programlisting>
@@ -4570,7 +4565,7 @@ context->restore();</programlisting>
</para>
<programlisting>void MyArea::on_draw(const Cairo::RefPtr<Cairo::Context>& cr, int width,
int height)
{
- Glib::RefPtr<Gdk::Pixbuf> image = Gdk::Pixbuf::create_from_file("myimage.png");
+ auto image = Gdk::Pixbuf::create_from_file("myimage.png");
// Draw the image at 110, 90, except for the outermost 10 pixels.
Gdk::Cairo::set_source_pixbuf(cr, image, 100, 80);
cr->rectangle(110, 90, image->get_width()-20, image->get_height()-20);
@@ -4912,7 +4907,7 @@ called with the actual data. For instance:
</para>
<programlisting>void ExampleWindow::on_clipboard_received(Glib::RefPtr<Gio::AsyncResult>& result)
{
- Glib::ustring text = get_clipboard()->read_text_finish(result);
+ auto text = get_clipboard()->read_text_finish(result);
//Do something with the pasted data.
}</programlisting>
@@ -5110,7 +5105,7 @@ so you can use it again if the page setup dialog is shown again.</para>
<para>For instance,
<programlisting>
//Within a class that inherits from Gtk::Window and keeps m_refPageSetup and m_refSettings as members...
-Glib::RefPtr<Gtk::PageSetup> new_page_setup = Gtk::run_page_setup_dialog(*this, m_refPageSetup,
m_refSettings);
+auto new_page_setup = Gtk::run_page_setup_dialog(*this, m_refPageSetup, m_refSettings);
m_refPageSetup = new_page_setup;
</programlisting>
</para>
@@ -5175,7 +5170,7 @@ the <literal>done</literal> and <literal>status_changed</literal> signals:
<para>For instance,
<programlisting>
// in class ExampleWindow's method...
-Glib::RefPtr<PrintOperation> op = PrintOperation::create();
+auto op = PrintOperation::create();
// ...set up op...
op->signal_done().connect(sigc::bind(sigc::mem_fun(*this, &ExampleWindow::on_printoperation_done), op));
// run the op
@@ -5219,10 +5214,10 @@ void ExampleWindow::on_printoperation_status_changed(const Glib::RefPtr<Print
The 'Print to file' option is available in the print dialog, without the need for extra implementation.
However, it is sometimes useful to generate a pdf file directly from code. For instance,
<programlisting>
-Glib::RefPtr<Gtk::PrintOperation> op = Gtk::PrintOperation::create();
+auto op = Gtk::PrintOperation::create();
// ...set up op...
op->set_export_filename("test.pdf");
-Gtk::PrintOperationResult res = op->run(Gtk::PRINT_OPERATION_ACTION_EXPORT);
+auto res = op->run(Gtk::PRINT_OPERATION_ACTION_EXPORT);
</programlisting>
</para>
@@ -5267,10 +5262,10 @@ Gtk::Widget* CustomPrintOperation::on_create_custom_widget()
{
set_custom_tab_label("My custom tab");
- Gtk::Box* hbox = new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 8);
+ auto hbox = new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 8);
hbox->set_margin(6);
- Gtk::Label* label = Gtk::make_managed<Gtk::Label>("Enter some text: ");
+ auto label = Gtk::make_managed<Gtk::Label>("Enter some text: ");
hbox->pack_start(*label, false, false);
hbox->pack_start(m_Entry, false, false);
@@ -5280,7 +5275,7 @@ Gtk::Widget* CustomPrintOperation::on_create_custom_widget()
void CustomPrintOperation::on_custom_widget_apply(Gtk::Widget* /* widget */)
{
- Glib::ustring user_input = m_Entry.get_text();
+ auto user_input = m_Entry.get_text();
//...
}
</programlisting>
@@ -5302,7 +5297,7 @@ a preview directly from an application:
<programlisting>
// in a class that inherits from Gtk::Window...
-Glib::RefPtr<PrintOperation> op = PrintOperation::create();
+auto op = PrintOperation::create();
// ...set up op...
op->run(Gtk::PRINT_OPERATION_ACTION_PREVIEW, *this);
</programlisting>
@@ -5379,7 +5374,7 @@ and update the print settings.
To add a new file to the list of recent documents, in the simplest case,
you only need to provide the URI. For example:
</para>
- <programlisting>Glib::RefPtr<Gtk::RecentManager> recent_manager =
Gtk::RecentManager::get_default();
+ <programlisting>auto recent_manager = Gtk::RecentManager::get_default();
recent_manager->add_item(uri);</programlisting>
<para>
If you want to register a file with metadata, you can pass a
@@ -5463,7 +5458,7 @@ if (info)
recently used files. The following code demonstrates how you might get a
list of recently used files:
</para>
- <programlisting>std::vector< Glib::RefPtr<Gtk::RecentInfo> > info_list =
recent_manager->get_items();</programlisting>
+ <programlisting>auto info_list = recent_manager->get_items();</programlisting>
<para>
The maximum age of items in the recently used files list can be set with
<methodname>Gtk::Settings::property_gtk_recent_files_max_age()</methodname>.
@@ -6009,7 +6004,7 @@ discourages those in favour of safer models of memory management, so it is
better to create widgets using <function>Gtk::make_managed()</function> and
let their parent destroy them, than to manually perform dynamic allocation.
<programlisting>
-Gtk::Button* pButton = new Gtk::Button("Test");
+auto pButton = new Gtk::Button("Test");
// do something useful with pButton
@@ -6067,7 +6062,7 @@ modern C++ style, and more clearly expresses intent to create a managed widget.
<programlisting>
MyContainer::MyContainer()
{
- Gtk::Button* pButton = Gtk::make_managed<Gtk::Button>("Test");
+ auto pButton = Gtk::make_managed<Gtk::Button>("Test");
add(*pButton); //add *pButton to MyContainer
}
</programlisting>
@@ -6105,7 +6100,7 @@ smartpointer, <classname>Cairo::RefPtr<></classname>.
Objects such as <classname>Gdk::Pixbuf</classname> can only be instantiated
with a <methodname>create()</methodname> function. For instance,
<programlisting>
-Glib::RefPtr<Gdk::Pixbuf> pixbuf = Gdk::Pixbuf::create_from_file(filename);
+auto pixbuf = Gdk::Pixbuf::create_from_file(filename);
</programlisting>
</para>
@@ -6114,7 +6109,7 @@ You have no way of getting a bare <classname>Gdk::Pixbuf</classname>. In the
example, <varname>pixbuf</varname> is a smart pointer, so you can do this, much
like a normal pointer:
<programlisting>
-int width = 0;
+auto width = 0;
if(pixbuf)
{
width = pixbuf->get_width();
@@ -6131,7 +6126,7 @@ to worry about it anymore. There's no <literal>new</literal> so there's no
<para>
If you copy a <classname>RefPtr</classname>, for instance
<programlisting>
-Glib::RefPtr<Gdk::Pixbuf> pixbuf2 = pixbuf;
+auto pixbuf2 = pixbuf;
</programlisting>
, or if you pass it as a method argument or a return type, then
<classname>RefPtr</classname> will do any necessary referencing to ensure that
@@ -6190,7 +6185,7 @@ layout allows you to focus on implementing that functionality.
<classname>Glib::RefPtr</classname>. Like all such classes, you need to use a
<methodname>create()</methodname> method to instantiate it. For instance,
<programlisting>
-Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file("basic.glade");
+auto builder = Gtk::Builder::create_from_file("basic.glade");
</programlisting>
This will instantiate the windows defined in the .glade file, though they will
not be shown immediately unless you have specified that via the <guilabel>Properties</guilabel>
@@ -6199,7 +6194,7 @@ window in <application>Glade</application>.
<para>To instantiate just one window, or just one of the child widgets, you can specify the name of a widget
as the second parameter. For instance,
<programlisting>
-Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file("basic.glade",
"treeview_products");
+auto builder = Gtk::Builder::create_from_file("basic.glade", "treeview_products");
</programlisting>
</para>
@@ -8040,8 +8035,8 @@ instance.
</para>
<para>
<programlisting>
-Glib::RefPtr<Gdk::Pixbuf> refPixbuf = Gdk::Pixbuf::create_from_file(filename);
-Glib::RefPtr<Gdk::Pixbuf> refPixbuf2 = refPixbuf;
+auto refPixbuf = Gdk::Pixbuf::create_from_file(filename);
+auto refPixbuf2 = refPixbuf;
</programlisting>
</para>
<para>
@@ -8051,7 +8046,7 @@ standard containers, such as <classname>std::vector</classname> or
<para>
<programlisting>
std::list<Glib::RefPtr<Gdk::Pixbuf>> listPixbufs;
-Glib::RefPtr<Gdk::Pixbuf> refPixbuf = Gdk::Pixbuf::create_from_file(filename);
+auto refPixbuf = Gdk::Pixbuf::create_from_file(filename);
listPixbufs.push_back(refPixbuf);
</programlisting>
</para>
@@ -8063,8 +8058,8 @@ call the methods of the underlying instance, just like a normal pointer.
</para>
<para>
<programlisting>
-Glib::RefPtr<Gdk::Pixbuf> refPixbuf = Gdk::Pixbuf::create_from_file(filename);
-int width = refPixbuf->get_width();
+auto refPixbuf = Gdk::Pixbuf::create_from_file(filename);
+auto width = refPixbuf->get_width();
</programlisting>
</para>
<para>You can also use the * operator and the <methodname>get()</methodname> method
@@ -8074,8 +8069,8 @@ in the reference count.
</para>
<para>
<programlisting>
-Glib::RefPtr<Gdk::Pixbuf> refPixbuf = Gdk::Pixbuf::create_from_file(filename);
-Gdk::Pixbuf& underlying = *refPixbuf; // Possible, but not recommended
+auto refPixbuf = Gdk::Pixbuf::create_from_file(filename);
+auto& underlying = *refPixbuf; // Possible, but not recommended
</programlisting>
</para>
</sect1>
@@ -8087,7 +8082,7 @@ pointers.
</para>
<para>
<programlisting>
-Glib::RefPtr<Gtk::TreeStore> refStore = Gtk::TreeStore::create(columns);
+auto refStore = Gtk::TreeStore::create(columns);
Glib::RefPtr<Gtk::TreeModel> refModel = refStore;
</programlisting>
</para>
@@ -8100,10 +8095,8 @@ a little different than with a normal pointer.
</para>
<para>
<programlisting>
-Glib::RefPtr<Gtk::TreeStore> refStore =
- std::dynamic_pointer_cast<Gtk::TreeStore>(refModel);
-Glib::RefPtr<Gtk::TreeStore> refStore2 =
- std::static_pointer_cast<Gtk::TreeStore>(refModel);
+auto refStore = std::dynamic_pointer_cast<Gtk::TreeStore>(refModel);
+auto refStore2 = std::static_pointer_cast<Gtk::TreeStore>(refModel);
</programlisting>
</para>
</sect1>
@@ -8115,10 +8108,10 @@ Just like normal pointers, you can check whether a
</para>
<para>
<programlisting>
-Glib::RefPtr<Gtk::TreeModel> refModel = m_TreeView.get_model();
+auto refModel = m_TreeView.get_model();
if (refModel)
{
- int cols_count = refModel->get_n_columns();
+ auto cols_count = refModel->get_n_columns();
...
}
</programlisting>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]