2 * Copyright (C) 2010-2015 Paul Davis <paul@linuxaudiosystems.com>
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 along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "gtkmm2ext/treeutils.h"
27 Gtkmm2ext::treeview_select_one (RefPtr<TreeSelection> selection, RefPtr<TreeModel> /*model*/, TreeView& view,
28 TreeIter /*iter*/, TreePath path, TreeViewColumn* col)
30 if (!view.row_expanded (path)) {
31 // cerr << "!! selecting a row that isn't expanded! " << path.to_string() << endl;
34 selection->unselect_all ();
35 view.set_cursor (path, *col, true);
39 Gtkmm2ext::treeview_select_previous (TreeView& view, RefPtr<TreeModel> model, TreeViewColumn* col)
41 RefPtr<TreeSelection> selection = view.get_selection();
42 TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
44 if (selection->count_selected_rows() == 0 || !col || model->children().size() < 2) {
48 TreeView::Selection::ListHandle_Path::iterator i = rows.begin();
49 TreeModel::Path start = *i;
50 TreePath prev = start;
53 iter = model->get_iter (prev);
55 if (iter == model->children().begin()) {
57 /* at the start, go to the end */
60 while (iter != model->children().end()) {
65 /* "x" is now an iterator for the last row */
68 prev = model->get_path (iter);
76 /* can't go back, go up */
83 iter = model->get_iter (prev);
87 treeview_select_one (selection, model, view, iter, prev, col);
91 /* can't move to previous, so restart at selected and move up the tree */
97 iter = model->get_iter (prev);
100 /* can't move up the tree*/
103 /* moved up from child to parent, now move to ??? */
107 iter = model->get_iter (prev);
111 treeview_select_one (selection, model, view, iter, prev, col);
114 /* we could not forward, so wrap around to the first row */
116 /* grr: no nice way to get an iter for the
117 last row, because there is no operator--
121 TreeIter x = model->children().begin();
123 while (x != model->children().end()) {
127 prev = model->get_path (px);
128 treeview_select_one (selection, model, view, px, prev, col);
134 Gtkmm2ext::treeview_select_next (TreeView& view, RefPtr<TreeModel> model, TreeViewColumn* col)
136 RefPtr<TreeSelection> selection = view.get_selection();
137 TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
139 if (selection->count_selected_rows() == 0 || !col || model->children().size() < 2) {
143 /* start with the last selected row, not the first */
145 TreeView::Selection::ListHandle_Path::iterator i = rows.begin();
146 TreeView::Selection::ListHandle_Path::iterator p = i;
148 /* get the last selected row */
150 while (i != rows.end()) {
155 TreeModel::Path start = *p;
156 TreePath next = start;
159 /* if the row we intend to start from has children but it is not expanded,
160 do not try to go down.
163 iter = model->get_iter (start);
165 TreeRow row = (*iter);
166 bool down_allowed = false;
168 if (!row.children().empty()) {
169 TreePath tp = model->get_path (iter);
171 if (!view.row_expanded (tp)) {
172 down_allowed = false;
182 TreeIter iter = model->get_iter (next);
184 /* can't go down, so move to next */
192 iter = model->get_iter (next);
196 treeview_select_one (selection, model, view, iter, next, col);
200 /* can't move down/next, so restart at selected and move up the tree */
206 iter = model->get_iter (next);
209 /* can't move up the tree*/
212 /* moved up from child to parent, now move to next */
216 iter = model->get_iter (next);
220 treeview_select_one (selection, model, view, iter, next, col);
222 /* we could not forward, so wrap around to the first row */
223 next = model->get_path (model->children().begin());
224 treeview_select_one (selection, model, view, model->children().begin(), next, col);