1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="highlight.min.css">
<script src="highlight.min.js"></script><script>
hljs.configure({languages: ['cpp']});
hljs.highlightAll();
</script><title>Displaying Trees</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="Programming with gtkmm 4">
<link rel="up" href="chapter-listmodel.html" title="Chapter 10. ListView, GridView, ColumnView">
<link rel="prev" href="sec-listmodel-filtering.html" title="Filtering">
<link rel="next" href="chapter-treeview.html" title="Chapter 11. The TreeView widget">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Displaying Trees</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-listmodel-filtering.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 10. ListView, GridView, ColumnView</th>
<td width="20%" align="right"> <a accesskey="n" href="chapter-treeview.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-listmodel-trees"></a>Displaying Trees</h2></div></div></div>
<p>
While the deprecated <code class="classname">TreeView</code> provided built-in support for trees,
the list widgets, and in particular <code class="classname">Gio::ListModel</code>, do not.
However, <span class="application">gtkmm</span> provides functionality to make trees look and behave like lists for
the people who still want to display lists. This is achieved by using the
<code class="classname">TreeListModel</code> to flatten a tree into a list.
The <code class="classname">TreeExpander</code> widget can then be used inside a listitem
to allow users to expand and collapse rows.
</p>
<p><a class="ulink" href="https://gnome.pages.gitlab.gnome.org/gtkmm/classGtk_1_1TreeListModel.html" target="_top">TreeListModel Reference</a></p>
<p><a class="ulink" href="https://gnome.pages.gitlab.gnome.org/gtkmm/classGtk_1_1TreeExpander.html" target="_top">TreeExpander Reference</a></p>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="listmodel-tree-example"></a>Example</h3></div></div></div>
<div class="figure">
<a name="figure-listmodel-tree"></a><p class="title"><b>Figure 10.6. TreeListModel</b></p>
<div class="figure-contents">
<div class="screenshot">
<div class="mediaobject"><img src="figures/listmodel_tree.png" alt="TreeListModel"></div>
</div>
</div>
</div>
<br class="figure-break">
<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/listmodelviews/tree_columnview" target="_top">Source Code</a></p>
<p>File: <code class="filename">examplewindow.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include <gtkmm.h>
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow();
~ExampleWindow() override;
protected:
// Signal handlers:
void on_button_quit();
void on_setup_id(const Glib::RefPtr<Gtk::ListItem>& list_item);
void on_setup_name(const Glib::RefPtr<Gtk::ListItem>& list_item);
void on_bind_id(const Glib::RefPtr<Gtk::ListItem>& list_item);
void on_bind_name(const Glib::RefPtr<Gtk::ListItem>& list_item);
Glib::RefPtr<Gio::ListModel> create_model(
const Glib::RefPtr<Glib::ObjectBase>& item = {});
// A Gio::ListStore item.
class ModelColumns : public Glib::Object
{
public:
int m_col_id;
Glib::ustring m_col_name;
static Glib::RefPtr<ModelColumns> create(int col_id, const Glib::ustring& col_name)
{
return Glib::make_refptr_for_instance<ModelColumns>(
new ModelColumns(col_id, col_name));
}
protected:
ModelColumns(int col_id, const Glib::ustring& col_name)
: m_col_id(col_id), m_col_name(col_name)
{}
}; // ModelColumns
// Child widgets:
Gtk::Box m_VBox;
Gtk::ScrolledWindow m_ScrolledWindow;
Gtk::ColumnView m_ColumnView;
Gtk::Box m_ButtonBox;
Gtk::Button m_Button_Quit;
Glib::RefPtr<Gtk::TreeListModel> m_TreeListModel;
};
#endif //GTKMM_EXAMPLEWINDOW_H
</code></pre>
<p>File: <code class="filename">examplewindow.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "examplewindow.h"
ExampleWindow::ExampleWindow()
: m_VBox(Gtk::Orientation::VERTICAL),
m_Button_Quit("Quit")
{
set_title("Gtk::ColumnView (Gtk::TreeListModel) example");
set_default_size(300, 350);
m_VBox.set_margin(5);
set_child(m_VBox);
// Add the ColumnView, inside a ScrolledWindow, with the button underneath:
m_ScrolledWindow.set_child(m_ColumnView);
// Only show the scrollbars when they are necessary:
m_ScrolledWindow.set_policy(Gtk::PolicyType::AUTOMATIC, Gtk::PolicyType::AUTOMATIC);
m_ScrolledWindow.set_expand();
m_VBox.append(m_ScrolledWindow);
m_VBox.append(m_ButtonBox);
m_ButtonBox.append(m_Button_Quit);
m_ButtonBox.set_margin(5);
m_Button_Quit.set_hexpand(true);
m_Button_Quit.set_halign(Gtk::Align::END);
m_Button_Quit.signal_clicked().connect(
sigc::mem_fun(*this, &ExampleWindow::on_button_quit));
// Create the root model:
auto root = create_model();
// Set list model and selection model.
// passthrough must be false when Gtk::TreeExpander is used in the view.
m_TreeListModel = Gtk::TreeListModel::create(root,
sigc::mem_fun(*this, &ExampleWindow::create_model),
/* passthrough */ false, /* autoexpand */ false);
auto selection_model = Gtk::MultiSelection::create(m_TreeListModel);
m_ColumnView.set_model(selection_model);
m_ColumnView.add_css_class("data-table"); // high density table
// Make the columns reorderable.
// This is not necessary, but it's nice to show the feature.
m_ColumnView.set_reorderable(true);
// Add the ColumnView's columns:
// Id column
auto factory = Gtk::SignalListItemFactory::create();
factory->signal_setup().connect(
sigc::mem_fun(*this, &ExampleWindow::on_setup_id));
factory->signal_bind().connect(
sigc::mem_fun(*this, &ExampleWindow::on_bind_id));
auto column = Gtk::ColumnViewColumn::create("ID", factory);
m_ColumnView.append_column(column);
// Name column
factory = Gtk::SignalListItemFactory::create();
factory->signal_setup().connect(
sigc::mem_fun(*this, &ExampleWindow::on_setup_name));
factory->signal_bind().connect(
sigc::mem_fun(*this, &ExampleWindow::on_bind_name));
column = Gtk::ColumnViewColumn::create("Name", factory);
m_ColumnView.append_column(column);
}
ExampleWindow::~ExampleWindow()
{
}
void ExampleWindow::on_button_quit()
{
set_visible(false);
}
Glib::RefPtr<Gio::ListModel> ExampleWindow::create_model(
const Glib::RefPtr<Glib::ObjectBase>& item)
{
auto col = std::dynamic_pointer_cast<ModelColumns>(item);
auto result = Gio::ListStore<ModelColumns>::create();
if (!col)
{
// Top names
result->append(ModelColumns::create(1, "Billy Bob"));
result->append(ModelColumns::create(2, "Joey Jojo"));
result->append(ModelColumns::create(3, "Rob McRoberts"));
}
else
{
switch (col->m_col_id)
{
case 1:
result->append(ModelColumns::create(11, "Billy Bob Junior"));
result->append(ModelColumns::create(12, "Sue Bob"));
break;
case 3:
result->append(ModelColumns::create(31, "Xavier McRoberts"));
break;
}
}
// If result is empty, it's a leaf in the tree, i.e. an item without children.
// Returning an empty RefPtr (not a RefPtr with an empty Gio::ListModel)
// signals that the item is not expandable.
return (result->get_n_items() > 0) ? result : Glib::RefPtr<Gio::ListModel>();
}
void ExampleWindow::on_setup_id(
const Glib::RefPtr<Gtk::ListItem>& list_item)
{
// Each ListItem contains a TreeExpander, which contains a Label.
// The Label shows the ModelColumns::m_col_id. That's done in on_bind_id().
auto expander = Gtk::make_managed<Gtk::TreeExpander>();
auto label = Gtk::make_managed<Gtk::Label>();
label->set_halign(Gtk::Align::END);
expander->set_child(*label);
list_item->set_child(*expander);
}
void ExampleWindow::on_setup_name(
const Glib::RefPtr<Gtk::ListItem>& list_item)
{
list_item->set_child(*Gtk::make_managed<Gtk::Label>("", Gtk::Align::START));
}
void ExampleWindow::on_bind_id(const Glib::RefPtr<Gtk::ListItem>& list_item)
{
// When TreeListModel::property_passthrough() is false, ListItem::get_item()
// is a TreeListRow. TreeExpander needs the TreeListRow.
// The ModelColumns item is returned by TreeListRow::get_item().
auto row = std::dynamic_pointer_cast<Gtk::TreeListRow>(list_item->get_item());
if (!row)
return;
auto col = std::dynamic_pointer_cast<ModelColumns>(row->get_item());
if (!col)
return;
auto expander = dynamic_cast<Gtk::TreeExpander*>(list_item->get_child());
if (!expander)
return;
expander->set_list_row(row);
auto label = dynamic_cast<Gtk::Label*>(expander->get_child());
if (!label)
return;
label->set_text(Glib::ustring::format(col->m_col_id));
}
void ExampleWindow::on_bind_name(const Glib::RefPtr<Gtk::ListItem>& list_item)
{
auto row = std::dynamic_pointer_cast<Gtk::TreeListRow>(list_item->get_item());
if (!row)
return;
auto col = std::dynamic_pointer_cast<ModelColumns>(row->get_item());
if (!col)
return;
auto label = dynamic_cast<Gtk::Label*>(list_item->get_child());
if (!label)
return;
label->set_text(col->m_col_name);
}
</code></pre>
<p>File: <code class="filename">main.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "examplewindow.h"
#include <gtkmm/application.h>
int main(int argc, char* argv[])
{
auto app = Gtk::Application::create("org.gtkmm.example");
// Shows the window and returns when it is closed.
return app->make_window_and_run<ExampleWindow>(argc, argv);
}
</code></pre>
</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-listmodel-filtering.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-listmodel.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="chapter-treeview.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Filtering </td>
<td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td>
<td width="40%" align="right" valign="top"> Chapter 11. The TreeView widget</td>
</tr>
</table>
</div>
</body>
</html>
|