Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > 345aa895e80053137c21f8693106c3a0 > files > 195

gtkmm2.4-documentation-2.17.4-1mdv2010.0.noarch.rpm

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sorting</title>
<link rel="stylesheet" href="style.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.75.1">
<link rel="home" href="index.html" title="Programming with gtkmm">
<link rel="up" href="chapter-treeview.html" title="Chapter 8. The TreeView widget">
<link rel="prev" href="sec-treeview-selection.html" title="The Selection">
<link rel="next" href="sec-treeview-draganddrop.html" title="Drag and Drop">
</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">Sorting</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-treeview-selection.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 8. The TreeView widget</th>
<td width="20%" align="right"> <a accesskey="n" href="sec-treeview-draganddrop.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="Sorting">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-treeview-sort"></a>Sorting</h2></div></div></div>
<p>
The standard tree models (<code class="classname">TreeStore</code> and <code class="classname">ListStore</code> derive from <code class="classname">TreeSortable</code>, so they offer sorting functionality. For instance, call <code class="methodname">set_sort_column()</code>, to sort the model by the specified column. Or supply a callback function to <code class="methodname">set_sort_func()</code> to implement a more complicated sorting algorithm.
</p>
<p><a class="ulink" href="http://library.gnome.org/devel/gtkmm/unstable/classGtk_1_1TreeSortable.html" target="_top">TreeSortable Reference</a></p>
<div class="sect2" title="Sorting by clicking on columns">
<div class="titlepage"><div><div><h3 class="title">
<a name="treeview-sort-headers"></a>Sorting by clicking on columns</h3></div></div></div>
<p>
So that a user can click on a <code class="classname">TreeView</code>'s column header to sort the <code class="classname">TreeView</code>'s contents, call <code class="methodname">Gtk::TreeViewModel::set_sort_column()</code>, supplying the model column on which model should be sorted when the header is clicked. For instance:
</p>
<pre class="programlisting">Gtk::TreeView::Column* pColumn = treeview.get_column(0);
if(pColumn)
  pColumn-&gt;set_sort_column(m_columns.m_col_id);</pre>
</div>
<div class="sect2" title="Independently sorted views of the same model">
<div class="titlepage"><div><div><h3 class="title">
<a name="treeview-sort-independent-views"></a>Independently sorted views of the same model</h3></div></div></div>
<p>
The <code class="classname">TreeView</code> already allows you to show the same <code class="classname">TreeModel</code> in two <code class="classname">TreeView</code> widgets. If you need one of these TreeViews to sort the model differently than the other then you should use a <code class="classname">TreeModelSort</code> instead of just, for instance, <code class="methodname">Gtk::TreeViewModel::set_sort_column()</code>. <code class="classname">TreeModelSort</code> 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 <code class="classname">TreeView</code> like so:
</p>
<pre class="programlisting">Glib::RefPtr&lt;Gtk::TreeModelSort&gt; sorted_model =
    Gtk::TreeModelSort::create(model);
sorted_model-&gt;set_sort_column(columns.m_col_name, Gtk::SORT_ASCENDING);
treeview.set_model(sorted_model);</pre>
<p>Note, however, that the TreeView will provide iterators to the sorted model. You must convert them to iterators to the underlying child model in order to perform actions on that model. For instance:
</p>
<pre class="programlisting">void ExampleWindow::on_button_delete()
{
  Glib::RefPtr&lt;Gtk::TreeSelection&gt; refTreeSelection =
      m_treeview.get_selection();
  if(refTreeSelection)
  {
    Gtk::TreeModel::iterator sorted_iter =
        m_refTreeSelection-&gt;get_selected();
    if(sorted_iter)
    {
      Gtk::TreeModel::iterator iter =
          m_refModelSort-&gt;convert_iter_to_child_iter(sorted_iter);
      m_refModel-&gt;erase(iter);
    }
  }
}</pre>
<p><a class="ulink" href="http://library.gnome.org/devel/gtkmm/unstable/classGtk_1_1TreeModelSort.html" target="_top">TreeModelSort Reference</a></p>
</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-treeview-selection.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-draganddrop.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">The Selection </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"> Drag and Drop</td>
</tr>
</table>
</div>
</body>
</html>