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
|
<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>The Selection</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-treeview.html" title="Chapter 11. The TreeView widget">
<link rel="prev" href="sec-iterating-over-model-rows.html" title="Iterating over Model Rows">
<link rel="next" href="sec-treeview-sort.html" title="Sorting">
</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">The Selection</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-iterating-over-model-rows.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 11. The TreeView widget</th>
<td width="20%" align="right"> <a accesskey="n" href="sec-treeview-sort.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-treeview-selection"></a>The Selection</h2></div></div></div>
<p>
To find out what rows the user has selected, get the
<code class="classname">Gtk::TreeView::Selection</code> object from the
<code class="classname">TreeView</code>, like so:
</p>
<pre class="programlisting"><code class="code">auto refTreeSelection = m_TreeView.get_selection();</code></pre>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="treeview-selection-mode"></a>Single or multiple selection</h3></div></div></div>
<p>
By default, only single rows can be selected, but you can allow
multiple selection by setting the mode, like so:
</p>
<pre class="programlisting"><code class="code">refTreeSelection->set_mode(Gtk::SelectionMode::MULTIPLE);</code></pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="treeview-selected-rows"></a>The selected rows</h3></div></div></div>
<p>
For single-selection, you can just call <code class="methodname">get_selected()</code>,
like so:
</p>
<pre class="programlisting"><code class="code">auto iter = refTreeSelection->get_selected();
if(iter) //If anything is selected
{
auto row = *iter;
//Do something with the row.
}</code></pre>
<p>
For multiple-selection, you need to call <code class="methodname">get_selected_rows()</code>
or define a callback, and give it to
<code class="methodname">selected_foreach()</code>,
<code class="methodname">selected_foreach_path()</code>, or
<code class="methodname">selected_foreach_iter()</code>, like so:
</p>
<pre class="programlisting"><code class="code">refTreeSelection->selected_foreach_iter(
sigc::mem_fun(*this, &TheClass::selected_row_callback) );
void TheClass::selected_row_callback(
const Gtk::TreeModel::const_iterator& iter)
{
auto row = *iter;
//Do something with the row.
}</code></pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="treeview-selection-changed-signal"></a>The "changed" signal</h3></div></div></div>
<p>
To respond to the user clicking on a row or range of rows, connect to the
signal like so:
</p>
<pre class="programlisting"><code class="code">refTreeSelection->signal_changed().connect(
sigc::mem_fun(*this, &Example_IconTheme::on_selection_changed)
);</code></pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="treeview-selection-preventing"></a>Preventing row selection</h3></div></div></div>
<p>
Maybe the user should not be able to select every item in your list or tree.
For instance, in the gtk-demo, you can select a demo to see the source code,
but it doesn't make any sense to select a demo category.
</p>
<p>
To control which rows can be selected, use the
<code class="methodname">set_select_function()</code> method, providing a
<code class="classname">sigc::slot</code> callback. For instance:
</p>
<pre class="programlisting"><code class="code">m_refTreeSelection->set_select_function( sigc::mem_fun(*this,
&DemoWindow::select_function) );</code></pre>
<p>
and then
</p>
<pre class="programlisting"><code class="code">bool DemoWindow::select_function(
const Glib::RefPtr<Gtk::TreeModel>& model,
const Gtk::TreeModel::Path& path, bool)
{
const auto iter = model->get_iter(path);
return iter->children().empty(); // only allow leaf nodes to be selected
}</code></pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="treeview-selection-changing"></a>Changing the selection</h3></div></div></div>
<p>
To change the selection, specify a
<code class="classname">Gtk::TreeModel::iterator</code> or
<code class="classname">Gtk::TreeModel::Row</code>, like so:
</p>
<pre class="programlisting"><code class="code">auto row = m_refModel->children()[5]; //The sixth row.
if(row)
refTreeSelection->select(row.get_iter());</code></pre>
<p>
or
</p>
<pre class="programlisting"><code class="code">auto iter = m_refModel->children().begin()
if(iter)
refTreeSelection->select(iter);</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-iterating-over-model-rows.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-treeview.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="sec-treeview-sort.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Iterating over Model Rows </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"> Sorting</td>
</tr>
</table>
</div>
</body>
</html>
|