File: sec-listmodel-trees.html

package info (click to toggle)
gtkmm-documentation 4.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,772 kB
  • sloc: cpp: 15,541; javascript: 1,208; makefile: 1,080; python: 401; xml: 106; perl: 67; sh: 8
file content (315 lines) | stat: -rw-r--r-- 11,393 bytes parent folder | download
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 &lt;gtkmm.h&gt;

class ExampleWindow : public Gtk::Window
{
public:
  ExampleWindow();
  ~ExampleWindow() override;

protected:
  // Signal handlers:
  void on_button_quit();
  void on_setup_id(const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; list_item);
  void on_setup_name(const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; list_item);
  void on_bind_id(const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; list_item);
  void on_bind_name(const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; list_item);

  Glib::RefPtr&lt;Gio::ListModel&gt; create_model(
    const Glib::RefPtr&lt;Glib::ObjectBase&gt;&amp; item = {});

  // A Gio::ListStore item.
  class ModelColumns : public Glib::Object
  {
  public:
    int m_col_id;
    Glib::ustring m_col_name;

    static Glib::RefPtr&lt;ModelColumns&gt; create(int col_id, const Glib::ustring&amp; col_name)
    {
      return Glib::make_refptr_for_instance&lt;ModelColumns&gt;(
        new ModelColumns(col_id, col_name));
    }

  protected:
    ModelColumns(int col_id, const Glib::ustring&amp; 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&lt;Gtk::TreeListModel&gt; 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, &amp;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, &amp;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-&gt;signal_setup().connect(
    sigc::mem_fun(*this, &amp;ExampleWindow::on_setup_id));
  factory-&gt;signal_bind().connect(
    sigc::mem_fun(*this, &amp;ExampleWindow::on_bind_id));
  auto column = Gtk::ColumnViewColumn::create("ID", factory);
  m_ColumnView.append_column(column);

  // Name column
  factory = Gtk::SignalListItemFactory::create();
  factory-&gt;signal_setup().connect(
    sigc::mem_fun(*this, &amp;ExampleWindow::on_setup_name));
  factory-&gt;signal_bind().connect(
    sigc::mem_fun(*this, &amp;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&lt;Gio::ListModel&gt; ExampleWindow::create_model(
  const Glib::RefPtr&lt;Glib::ObjectBase&gt;&amp; item)
{
  auto col = std::dynamic_pointer_cast&lt;ModelColumns&gt;(item);
  auto result = Gio::ListStore&lt;ModelColumns&gt;::create();
  if (!col)
  {
    // Top names
    result-&gt;append(ModelColumns::create(1, "Billy Bob"));
    result-&gt;append(ModelColumns::create(2, "Joey Jojo"));
    result-&gt;append(ModelColumns::create(3, "Rob McRoberts"));
  }
  else
  {
    switch (col-&gt;m_col_id)
    {
    case 1:
      result-&gt;append(ModelColumns::create(11, "Billy Bob Junior"));
      result-&gt;append(ModelColumns::create(12, "Sue Bob"));
      break;
    case 3:
      result-&gt;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-&gt;get_n_items() &gt; 0) ? result : Glib::RefPtr&lt;Gio::ListModel&gt;();
}

void ExampleWindow::on_setup_id(
  const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; 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&lt;Gtk::TreeExpander&gt;();
  auto label = Gtk::make_managed&lt;Gtk::Label&gt;();
  label-&gt;set_halign(Gtk::Align::END);
  expander-&gt;set_child(*label);
  list_item-&gt;set_child(*expander);
}

void ExampleWindow::on_setup_name(
  const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; list_item)
{
  list_item-&gt;set_child(*Gtk::make_managed&lt;Gtk::Label&gt;("", Gtk::Align::START));
}

void ExampleWindow::on_bind_id(const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; 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&lt;Gtk::TreeListRow&gt;(list_item-&gt;get_item());
  if (!row)
    return;
  auto col = std::dynamic_pointer_cast&lt;ModelColumns&gt;(row-&gt;get_item());
  if (!col)
    return;
  auto expander = dynamic_cast&lt;Gtk::TreeExpander*&gt;(list_item-&gt;get_child());
  if (!expander)
    return;
  expander-&gt;set_list_row(row);
  auto label = dynamic_cast&lt;Gtk::Label*&gt;(expander-&gt;get_child());
  if (!label)
    return;
  label-&gt;set_text(Glib::ustring::format(col-&gt;m_col_id));
}

void ExampleWindow::on_bind_name(const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; list_item)
{
  auto row = std::dynamic_pointer_cast&lt;Gtk::TreeListRow&gt;(list_item-&gt;get_item());
  if (!row)
    return;
  auto col = std::dynamic_pointer_cast&lt;ModelColumns&gt;(row-&gt;get_item());
  if (!col)
    return;
  auto label = dynamic_cast&lt;Gtk::Label*&gt;(list_item-&gt;get_child());
  if (!label)
    return;
  label-&gt;set_text(col-&gt;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 &lt;gtkmm/application.h&gt;

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-&gt;make_window_and_run&lt;ExampleWindow&gt;(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>