summaryrefslogtreecommitdiff
path: root/gtk2_ardour/stripable_treemodel.cc
blob: 7adc19e6b65d1f33cbe204c735cd79d7b3f189a0 (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 * Copyright (C) 2017 Paul Davis <paul@linuxaudiosystems.com>
 * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <iostream>

#include "ardour/session.h"
#include "ardour/types.h"

#include "axis_provider.h"
#include "stripable_treemodel.h"

using namespace ARDOUR;

StripableTreeModel::Glue::Glue (boost::shared_ptr<Stripable> s)
	: stripable (s)
{
}

StripableTreeModel::StripableTreeModel (AxisViewProvider& avp)
	: Glib::ObjectBase( typeid(StripableTreeModel) ) //register a custom GType.
	, Glib::Object() //The custom GType is actually registered here.
	, axis_view_provider (avp)
{
	n_columns = columns.size();
}

void
StripableTreeModel::set_session (Session& s)
{
	_session = &s;
}

StripableTreeModel::~StripableTreeModel()
{
}

Glib::RefPtr<StripableTreeModel>
StripableTreeModel::create (AxisViewProvider& avp)
{
	return Glib::RefPtr<StripableTreeModel> (new StripableTreeModel (avp));
}

Gtk::TreeModelFlags
StripableTreeModel::get_flags_vfunc() const
{
	return Gtk::TREE_MODEL_LIST_ONLY;
}

int
StripableTreeModel::get_n_columns_vfunc() const
{
	return n_columns;
}

GType
StripableTreeModel::get_column_type_vfunc (int index) const
{
	if (index <= n_columns) {
		return columns.types()[index];
	}
	return 0;
}

void
StripableTreeModel::get_value_vfunc (const TreeModel::iterator& iter, int column, Glib::ValueBase& value) const
{
	if (!_session) {
		return;
	}

	if (column > n_columns) {
		return;
	}

	const Glue* glue = (const Glue*)iter.gobj()->user_data;
	boost::shared_ptr<Stripable> iter_stripable = glue->stripable.lock();

	if (!iter_stripable) {
		return;
	}

	switch (column) {
	case 0:
		return text_value (iter_stripable, value);
	}
}

void
StripableTreeModel::text_value (boost::shared_ptr<Stripable> stripable, Glib::ValueBase& value) const
{
	StringColumn::ValueType val;
	val.set (stripable->name());
	value = val;
}

bool
StripableTreeModel::iter_next_vfunc (const iterator& iter, iterator& iter_next) const
{
	if (!_session) {
		return false;
	}

	const Glue* glue = (const Glue*)iter.gobj()->user_data;
	boost::shared_ptr<Stripable> iter_stripable = glue->stripable.lock();

	if (!iter_stripable) {
		return false;
	}

	//initialize the next iterator:
	iter_next = iterator();

	StripableList sl;
	_session->get_stripables (sl);
	if (sl.empty()) {
		return false;
	}
	sl.sort (Stripable::Sorter());

	for (StripableList::const_iterator s = sl.begin(); s != sl.end(); ++s) {

		if (*s == iter_stripable) {
			++s;
			if (s != sl.end()) {
				Glue* new_glue = new Glue (iter_stripable);
				iter_next.gobj()->user_data = (void*)new_glue;
				remember_glue_item (new_glue);
				return true; //success
			}
			break;
		}
	}

	return false; //There is no next row.
}

bool
StripableTreeModel::iter_children_vfunc(const iterator& parent, iterator& iter) const
{
	return false;
}

bool
StripableTreeModel::iter_has_child_vfunc(const iterator& iter) const
{
	return false;
}

int
StripableTreeModel::iter_n_children_vfunc(const iterator& iter) const
{
	return 0;
}

int
StripableTreeModel::iter_n_root_children_vfunc() const
{
	if (_session) {
		StripableList sl;
		_session->get_stripables (sl);
		return sl.size();
	}
	return 0;
}

bool
StripableTreeModel::iter_nth_child_vfunc(const iterator& parent, int /* n */, iterator& iter) const
{
	iter = iterator(); //Set is as invalid, as the TreeModel documentation says that it should be.
	return false; //There are no children.
}

bool
StripableTreeModel::iter_nth_root_child_vfunc(int n, iterator& iter) const
{
	iter = iterator(); //clear the input parameter.
	if (!_session) {
		return false;
	}

	StripableList sl;
	_session->get_stripables (sl);

	if (sl.empty()) {
		return false;
	}

	sl.sort (Stripable::Sorter());

	StripableList::const_iterator s;

	for (s = sl.begin(); s != sl.end() && n > 0; ++s, --n);

	if (s != sl.end()) {
		Glue* new_glue = new Glue (*s);
		iter.gobj()->user_data = new_glue;
		remember_glue_item (new_glue);
		return true;
	}

	return false; //There are no children.
}

bool
StripableTreeModel::iter_parent_vfunc(const iterator& child, iterator& iter) const
{
	iter = iterator(); //Set is as invalid, as the TreeModel documentation says that it should be.
	return false; //There are no children, so no parents.
}

Gtk::TreeModel::Path
StripableTreeModel::get_path_vfunc(const iterator& /* iter */) const
{
	//TODO:
	return Path();
}

bool
StripableTreeModel::get_iter_vfunc (const Path& path, iterator& iter) const
{
	unsigned sz = path.size();

	if (!sz || sz > 1) {
		/* path must refer to something, but not children since we
		   don't do children.
		*/
		iter = iterator(); //Set is as invalid, as the TreeModel documentation says that it should be.
		return false;
	}

	return iter_nth_root_child_vfunc (path[0], iter);
}

bool
StripableTreeModel::iter_is_valid(const iterator& iter) const
{
	const Glue* glue = (const Glue*)iter.gobj()->user_data;

	if (!glue->stripable.lock()) {
		return false;
	}

	return Gtk::TreeModel::iter_is_valid(iter);
}

void
StripableTreeModel::remember_glue_item (Glue* item) const
{
	glue_list.insert (item);
}