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.
20 #include "gtkmm2ext/treeutils.h"
26 Gtkmm2ext::treeview_select_one (RefPtr<TreeSelection> selection, RefPtr<TreeModel> model, TreeView& view,
27 TreeIter iter, TreePath path, TreeViewColumn* col)
29 if (!view.row_expanded (path)) {
30 // cerr << "!! selecting a row that isn't expanded! " << path.to_string() << endl;
33 selection->unselect_all ();
34 view.set_cursor (path, *col, true);
38 Gtkmm2ext::treeview_select_previous (TreeView& view, RefPtr<TreeModel> model, TreeViewColumn* col)
40 RefPtr<TreeSelection> selection = view.get_selection();
41 TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
43 if (selection->count_selected_rows() == 0 || !col || model->children().size() < 2) {
47 TreeView::Selection::ListHandle_Path::iterator i = rows.begin();
48 TreeModel::Path start = *i;
49 TreePath prev = start;
52 iter = model->get_iter (prev);
54 if (iter == model->children().begin()) {
56 /* at the start, go to the end */
59 while (iter != model->children().end()) {
64 /* "x" is now an iterator for the last row */
67 prev = model->get_path (iter);
75 /* can't go back, go up */
82 iter = model->get_iter (prev);
86 treeview_select_one (selection, model, view, iter, prev, col);
90 /* can't move to previous, so restart at selected and move up the tree */
96 iter = model->get_iter (prev);
99 /* can't move up the tree*/
102 /* moved up from child to parent, now move to ??? */
106 iter = model->get_iter (prev);
110 treeview_select_one (selection, model, view, iter, prev, col);
113 /* we could not forward, so wrap around to the first row */
115 /* grr: no nice way to get an iter for the
116 last row, because there is no operator--
120 TreeIter x = model->children().begin();
122 while (x != model->children().end()) {
126 prev = model->get_path (px);
127 treeview_select_one (selection, model, view, px, prev, col);
133 Gtkmm2ext::treeview_select_next (TreeView& view, RefPtr<TreeModel> model, TreeViewColumn* col)
135 RefPtr<TreeSelection> selection = view.get_selection();
136 TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
138 if (selection->count_selected_rows() == 0 || !col || model->children().size() < 2) {
142 /* start with the last selected row, not the first */
144 TreeView::Selection::ListHandle_Path::iterator i = rows.begin();
145 TreeView::Selection::ListHandle_Path::iterator p = i;
147 /* get the last selected row */
149 while (i != rows.end()) {
154 TreeModel::Path start = *p;
155 TreePath next = start;
158 /* if the row we intend to start from has children but it is not expanded,
159 do not try to go down.
162 iter = model->get_iter (start);
164 TreeRow row = (*iter);
165 bool down_allowed = true;
167 if (!row.children().empty()) {
168 TreePath tp = model->get_path (iter);
170 if (!view.row_expanded (tp)) {
171 down_allowed = false;
179 TreeIter iter = model->get_iter (next);
181 /* can't go down, so move to next */
189 iter = model->get_iter (next);
193 treeview_select_one (selection, model, view, iter, next, col);
197 /* can't move down/next, so restart at selected and move up the tree */
203 iter = model->get_iter (next);
206 /* can't move up the tree*/
209 /* moved up from child to parent, now move to next */
213 iter = model->get_iter (next);
217 treeview_select_one (selection, model, view, iter, next, col);
219 /* we could not forward, so wrap around to the first row */
220 next = model->get_path (model->children().begin());
221 treeview_select_one (selection, model, view, model->children().begin(), next, col);