2 Copyright (C) 2010 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "gtkmm2ext/treeutils.h"
28 Gtkmm2ext::treeview_select_one (RefPtr
<TreeSelection
> selection
, RefPtr
<TreeModel
> model
, TreeView
& view
,
29 TreeIter iter
, TreePath path
, TreeViewColumn
* col
)
31 if (!view
.row_expanded (path
)) {
32 // cerr << "!! selecting a row that isn't expanded! " << path.to_string() << endl;
35 selection
->unselect_all ();
36 view
.set_cursor (path
, *col
, true);
40 Gtkmm2ext::treeview_select_previous (TreeView
& view
, RefPtr
<TreeModel
> model
, TreeViewColumn
* col
)
42 RefPtr
<TreeSelection
> selection
= view
.get_selection();
43 TreeView::Selection::ListHandle_Path rows
= selection
->get_selected_rows ();
45 if (selection
->count_selected_rows() == 0 || !col
|| model
->children().size() < 2) {
49 TreeView::Selection::ListHandle_Path::iterator i
= rows
.begin();
50 TreeModel::Path start
= *i
;
51 TreePath prev
= start
;
54 iter
= model
->get_iter (prev
);
56 if (iter
== model
->children().begin()) {
58 /* at the start, go to the end */
61 while (iter
!= model
->children().end()) {
66 /* "x" is now an iterator for the last row */
69 prev
= model
->get_path (iter
);
77 /* can't go back, go up */
84 iter
= model
->get_iter (prev
);
88 treeview_select_one (selection
, model
, view
, iter
, prev
, col
);
92 /* can't move to previous, so restart at selected and move up the tree */
98 iter
= model
->get_iter (prev
);
101 /* can't move up the tree*/
104 /* moved up from child to parent, now move to ??? */
108 iter
= model
->get_iter (prev
);
112 treeview_select_one (selection
, model
, view
, iter
, prev
, col
);
115 /* we could not forward, so wrap around to the first row */
117 /* grr: no nice way to get an iter for the
118 last row, because there is no operator--
122 TreeIter x
= model
->children().begin();
124 while (x
!= model
->children().end()) {
128 prev
= model
->get_path (px
);
129 treeview_select_one (selection
, model
, view
, px
, prev
, col
);
135 Gtkmm2ext::treeview_select_next (TreeView
& view
, RefPtr
<TreeModel
> model
, TreeViewColumn
* col
)
137 RefPtr
<TreeSelection
> selection
= view
.get_selection();
138 TreeView::Selection::ListHandle_Path rows
= selection
->get_selected_rows ();
140 if (selection
->count_selected_rows() == 0 || !col
|| model
->children().size() < 2) {
144 /* start with the last selected row, not the first */
146 TreeView::Selection::ListHandle_Path::iterator i
= rows
.begin();
147 TreeView::Selection::ListHandle_Path::iterator p
= i
;
149 /* get the last selected row */
151 while (i
!= rows
.end()) {
156 TreeModel::Path start
= *p
;
157 TreePath next
= start
;
160 /* if the row we intend to start from has children but it is not expanded,
161 do not try to go down.
164 iter
= model
->get_iter (start
);
166 TreeRow row
= (*iter
);
167 bool down_allowed
= false;
169 if (!row
.children().empty()) {
170 TreePath tp
= model
->get_path (iter
);
172 if (!view
.row_expanded (tp
)) {
173 down_allowed
= false;
183 TreeIter iter
= model
->get_iter (next
);
185 /* can't go down, so move to next */
193 iter
= model
->get_iter (next
);
197 treeview_select_one (selection
, model
, view
, iter
, next
, col
);
201 /* can't move down/next, so restart at selected and move up the tree */
207 iter
= model
->get_iter (next
);
210 /* can't move up the tree*/
213 /* moved up from child to parent, now move to next */
217 iter
= model
->get_iter (next
);
221 treeview_select_one (selection
, model
, view
, iter
, next
, col
);
223 /* we could not forward, so wrap around to the first row */
224 next
= model
->get_path (model
->children().begin());
225 treeview_select_one (selection
, model
, view
, model
->children().begin(), next
, col
);