1 /*
2  * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
3  * Copyright (C) 2009-2012 David Robillard <d@drobilla.net>
4  * Copyright (C) 2009-2018 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
6  * Copyright (C) 2015-2016 Tim Mayberry <mojofunk@gmail.com>
7  * Copyright (C) 2016 Nick Mainsbridge <mainsbridge@gmail.com>
8  * Copyright (C) 2018-2019 Ben Loftis <ben@harrisonconsoles.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 
25 #include <algorithm>
26 #include <cmath>
27 #include <cstdlib>
28 #include <sstream>
29 #include <string>
30 
31 #include "pbd/basename.h"
32 #include "pbd/enumwriter.h"
33 
34 #include "ardour/audiofilesource.h"
35 #include "ardour/audioregion.h"
36 #include "ardour/profile.h"
37 #include "ardour/region_factory.h"
38 #include "ardour/session.h"
39 #include "ardour/session_playlist.h"
40 #include "ardour/silentfilesource.h"
41 
42 #include "gtkmm2ext/treeutils.h"
43 #include "gtkmm2ext/utils.h"
44 
45 #include "widgets/choice.h"
46 #include "widgets/tooltips.h"
47 
48 #include "actions.h"
49 #include "ardour_ui.h"
50 #include "audio_clock.h"
51 #include "editing.h"
52 #include "editing_convert.h"
53 #include "editor.h"
54 #include "editor_drag.h"
55 #include "editor_regions.h"
56 #include "gui_thread.h"
57 #include "keyboard.h"
58 #include "main_clock.h"
59 #include "region_view.h"
60 #include "ui_config.h"
61 #include "utils.h"
62 
63 #include "pbd/i18n.h"
64 
65 using namespace std;
66 using namespace ARDOUR;
67 using namespace ArdourWidgets;
68 using namespace ARDOUR_UI_UTILS;
69 using namespace PBD;
70 using namespace Gtk;
71 using namespace Glib;
72 using namespace Editing;
73 using Gtkmm2ext::Keyboard;
74 
75 //#define SHOW_REGION_EXTRAS
76 
EditorRegions(Editor * e)77 EditorRegions::EditorRegions (Editor* e)
78         : EditorComponent (e)
79         , old_focus (0)
80         , name_editable (0)
81         , tags_editable (0)
82         , _menu (0)
83         , _no_redisplay (false)
84 {
85 	_display.set_size_request (100, -1);
86 	_display.set_rules_hint (true);
87 	_display.set_name ("RegionList");
88 	_display.set_fixed_height_mode (true);
89 	_display.set_reorderable (false);
90 
91 	/* Try to prevent single mouse presses from initiating edits.
92 	 * This relies on a hack in gtktreeview.c:gtk_treeview_button_press() */
93 	_display.set_data ("mouse-edits-require-mod1", (gpointer)0x1);
94 
95 	_model = TreeStore::create (_columns);
96 	_model->set_sort_column (0, SORT_ASCENDING);
97 
98 	/* column widths */
99 	int bbt_width, date_width, chan_width, check_width, height;
100 
101 	Glib::RefPtr<Pango::Layout> layout = _display.create_pango_layout (X_ ("000|000|000"));
102 	Gtkmm2ext::get_pixel_size (layout, bbt_width, height);
103 
104 	Glib::RefPtr<Pango::Layout> layout2 = _display.create_pango_layout (X_ ("2099-10-10 10:10:30"));
105 	Gtkmm2ext::get_pixel_size (layout2, date_width, height);
106 
107 	Glib::RefPtr<Pango::Layout> layout3 = _display.create_pango_layout (X_ ("Chans    "));
108 	Gtkmm2ext::get_pixel_size (layout3, chan_width, height);
109 
110 	check_width = 20;
111 
112 	TreeViewColumn* col_name = manage (new TreeViewColumn ("", _columns.name));
113 	col_name->set_fixed_width (120);
114 	col_name->set_sizing (TREE_VIEW_COLUMN_FIXED);
115 	TreeViewColumn* col_chans = manage (new TreeViewColumn ("", _columns.channels));
116 	col_chans->set_fixed_width (chan_width);
117 	col_chans->set_sizing (TREE_VIEW_COLUMN_FIXED);
118 	TreeViewColumn* col_tags = manage (new TreeViewColumn ("", _columns.tags));
119 	col_tags->set_fixed_width (date_width);
120 	col_tags->set_sizing (TREE_VIEW_COLUMN_FIXED);
121 	TreeViewColumn* col_start = manage (new TreeViewColumn ("", _columns.start));
122 	col_start->set_fixed_width (bbt_width);
123 	col_start->set_sizing (TREE_VIEW_COLUMN_FIXED);
124 	TreeViewColumn* col_end = manage (new TreeViewColumn ("", _columns.end));
125 	col_end->set_fixed_width (bbt_width);
126 	col_end->set_sizing (TREE_VIEW_COLUMN_FIXED);
127 	TreeViewColumn* col_length = manage (new TreeViewColumn ("", _columns.length));
128 	col_length->set_fixed_width (bbt_width);
129 	col_length->set_sizing (TREE_VIEW_COLUMN_FIXED);
130 	TreeViewColumn* col_sync = manage (new TreeViewColumn ("", _columns.sync));
131 	col_sync->set_fixed_width (bbt_width);
132 	col_sync->set_sizing (TREE_VIEW_COLUMN_FIXED);
133 	TreeViewColumn* col_fadein = manage (new TreeViewColumn ("", _columns.fadein));
134 	col_fadein->set_fixed_width (bbt_width);
135 	col_fadein->set_sizing (TREE_VIEW_COLUMN_FIXED);
136 	TreeViewColumn* col_fadeout = manage (new TreeViewColumn ("", _columns.fadeout));
137 	col_fadeout->set_fixed_width (bbt_width);
138 	col_fadeout->set_sizing (TREE_VIEW_COLUMN_FIXED);
139 	TreeViewColumn* col_locked = manage (new TreeViewColumn ("", _columns.locked));
140 	col_locked->set_fixed_width (check_width);
141 	col_locked->set_sizing (TREE_VIEW_COLUMN_FIXED);
142 	TreeViewColumn* col_glued = manage (new TreeViewColumn ("", _columns.glued));
143 	col_glued->set_fixed_width (check_width);
144 	col_glued->set_sizing (TREE_VIEW_COLUMN_FIXED);
145 	TreeViewColumn* col_muted = manage (new TreeViewColumn ("", _columns.muted));
146 	col_muted->set_fixed_width (check_width);
147 	col_muted->set_sizing (TREE_VIEW_COLUMN_FIXED);
148 	TreeViewColumn* col_opaque = manage (new TreeViewColumn ("", _columns.opaque));
149 	col_opaque->set_fixed_width (check_width);
150 	col_opaque->set_sizing (TREE_VIEW_COLUMN_FIXED);
151 
152 	_display.append_column (*col_name);
153 	_display.append_column (*col_chans);
154 	_display.append_column (*col_tags);
155 	_display.append_column (*col_start);
156 	_display.append_column (*col_length);
157 	_display.append_column (*col_locked);
158 	_display.append_column (*col_glued);
159 	_display.append_column (*col_muted);
160 	_display.append_column (*col_opaque);
161 
162 #ifdef SHOW_REGION_EXTRAS
163 	_display.append_column (*col_end);
164 	_display.append_column (*col_sync);
165 	_display.append_column (*col_fadein);
166 	_display.append_column (*col_fadeout);
167 #endif
168 
169 	TreeViewColumn* col;
170 	Gtk::Label*     l;
171 
172 	struct ColumnInfo {
173 		int                index;
174 		int                sort_idx;
175 		Gtk::AlignmentEnum al;
176 		const char*        label;
177 		const char*        tooltip;
178 	} ci[] = {
179 	/* clang-format off */
180 		{ 0,  0,  ALIGN_LEFT,    _("Name"),      _("Region name") },
181 		{ 1,  1,  ALIGN_LEFT,    _("# Ch"),      _("# Channels in the region") },
182 		{ 2,  2,  ALIGN_LEFT,    _("Tags"),      _("Tags") },
183 		{ 3, 16,  ALIGN_RIGHT,   _("Start"),     _("Position of start of region") },
184 		{ 4,  4,  ALIGN_RIGHT,   _("Length"),    _("Length of the region") },
185 		{ 5, -1,  ALIGN_CENTER, S_("Lock|L"),    _("Region position locked?") },
186 		{ 6, -1,  ALIGN_CENTER, S_("Glued|G"),   _("Region position glued to Bars|Beats time?") },
187 		{ 7, -1,  ALIGN_CENTER, S_("Mute|M"),    _("Region muted?") },
188 		{ 8, -1,  ALIGN_CENTER, S_("Opaque|O"),  _("Region opaque (blocks regions below it from being heard)?") },
189 #ifdef SHOW_REGION_EXTRAS
190 		{ 9,  5,  ALIGN_RIGHT,  _("End"),       _("Position of end of region") },
191 		{ 10, -1,  ALIGN_RIGHT,  _("Sync"),      _("Position of region sync point, relative to start of the region") },
192 		{ 11,-1,  ALIGN_RIGHT,  _("Fade In"),   _("Length of region fade-in (units: secondary clock), () if disabled") },
193 		{ 12,-1,  ALIGN_RIGHT,  _("Fade Out"),  _("Length of region fade-out (units: secondary clock), () if disabled") },
194 #endif
195 		{ -1,-1,  ALIGN_CENTER, 0, 0 }
196 	};
197 	/* clang-format on */
198 
199 	for (int i = 0; ci[i].index >= 0; ++i) {
200 		col = _display.get_column (ci[i].index);
201 
202 		/* add the label */
203 		l = manage (new Label (ci[i].label));
204 		l->set_alignment (ci[i].al);
205 		set_tooltip (*l, ci[i].tooltip);
206 		col->set_widget (*l);
207 		l->show ();
208 
209 		col->set_sort_column (ci[i].sort_idx);
210 
211 		col->set_expand (false);
212 
213 		/* this sets the alignment of the column header... */
214 		col->set_alignment (ci[i].al);
215 
216 		/* ...and this sets the alignment for the data cells */
217 		CellRendererText* renderer = dynamic_cast<CellRendererText*> (_display.get_column_cell_renderer (i));
218 		if (renderer) {
219 			renderer->property_xalign () = (ci[i].al == ALIGN_RIGHT ? 1.0 : (ci[i].al == ALIGN_LEFT ? 0.0 : 0.5));
220 		}
221 	}
222 
223 	_display.set_model (_model);
224 
225 	_display.set_headers_visible (true);
226 	_display.set_rules_hint ();
227 
228 	if (UIConfiguration::instance ().get_use_tooltips ()) {
229 		/* show path as the row tooltip */
230 		_display.set_tooltip_column (13); /* path */
231 	}
232 	_display.get_selection ()->set_select_function (sigc::mem_fun (*this, &EditorRegions::selection_filter));
233 
234 	/* Name cell: make editable */
235 	CellRendererText* region_name_cell     = dynamic_cast<CellRendererText*> (_display.get_column_cell_renderer (0));
236 	region_name_cell->property_editable () = true;
237 	region_name_cell->signal_edited ().connect (sigc::mem_fun (*this, &EditorRegions::name_edit));
238 	region_name_cell->signal_editing_started ().connect (sigc::mem_fun (*this, &EditorRegions::name_editing_started));
239 
240 	/* Region Name: color turns red if source is missing. */
241 	TreeViewColumn*   tv_col   = _display.get_column (0);
242 	CellRendererText* renderer = dynamic_cast<CellRendererText*> (_display.get_column_cell_renderer (0));
243 	tv_col->add_attribute (renderer->property_text (), _columns.name);
244 	tv_col->add_attribute (renderer->property_foreground_gdk (), _columns.color_);
245 	tv_col->set_expand (true);
246 
247 	/* Tags cell: make editable */
248 	CellRendererText* region_tags_cell     = dynamic_cast<CellRendererText*> (_display.get_column_cell_renderer (2));
249 	region_tags_cell->property_editable () = true;
250 	region_tags_cell->signal_edited ().connect (sigc::mem_fun (*this, &EditorRegions::tag_edit));
251 	region_tags_cell->signal_editing_started ().connect (sigc::mem_fun (*this, &EditorRegions::tag_editing_started));
252 
253 	/* checkbox cells */
254 	int check_start_col = 5;
255 
256 	CellRendererToggle* locked_cell      = dynamic_cast<CellRendererToggle*> (_display.get_column_cell_renderer (check_start_col++));
257 	locked_cell->property_activatable () = true;
258 	locked_cell->signal_toggled ().connect (sigc::mem_fun (*this, &EditorRegions::locked_changed));
259 
260 	CellRendererToggle* glued_cell      = dynamic_cast<CellRendererToggle*> (_display.get_column_cell_renderer (check_start_col++));
261 	glued_cell->property_activatable () = true;
262 	glued_cell->signal_toggled ().connect (sigc::mem_fun (*this, &EditorRegions::glued_changed));
263 
264 	CellRendererToggle* muted_cell      = dynamic_cast<CellRendererToggle*> (_display.get_column_cell_renderer (check_start_col++));
265 	muted_cell->property_activatable () = true;
266 	muted_cell->signal_toggled ().connect (sigc::mem_fun (*this, &EditorRegions::muted_changed));
267 
268 	CellRendererToggle* opaque_cell      = dynamic_cast<CellRendererToggle*> (_display.get_column_cell_renderer (check_start_col));
269 	opaque_cell->property_activatable () = true;
270 	opaque_cell->signal_toggled ().connect (sigc::mem_fun (*this, &EditorRegions::opaque_changed));
271 
272 	_display.get_selection ()->set_mode (SELECTION_MULTIPLE);
273 	_display.add_object_drag (_columns.region.index (), "regions");
274 	_display.set_drag_column (_columns.name.index ());
275 
276 	/* setup DnD handling */
277 
278 	list<TargetEntry> region_list_target_table;
279 
280 	region_list_target_table.push_back (TargetEntry ("text/uri-list"));
281 	region_list_target_table.push_back (TargetEntry ("text/plain"));
282 	region_list_target_table.push_back (TargetEntry ("application/x-rootwin-drop"));
283 
284 	_display.add_drop_targets (region_list_target_table);
285 	_display.signal_drag_data_received ().connect (sigc::mem_fun (*this, &EditorRegions::drag_data_received));
286 
287 	_scroller.add (_display);
288 	_scroller.set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC);
289 
290 	_display.signal_button_press_event ().connect (sigc::mem_fun (*this, &EditorRegions::button_press), false);
291 	_change_connection = _display.get_selection ()->signal_changed ().connect (sigc::mem_fun (*this, &EditorRegions::selection_changed));
292 
293 	_scroller.signal_key_press_event ().connect (sigc::mem_fun (*this, &EditorRegions::key_press), false);
294 	_scroller.signal_focus_in_event ().connect (sigc::mem_fun (*this, &EditorRegions::focus_in), false);
295 	_scroller.signal_focus_out_event ().connect (sigc::mem_fun (*this, &EditorRegions::focus_out));
296 
297 	_display.signal_enter_notify_event ().connect (sigc::mem_fun (*this, &EditorRegions::enter_notify), false);
298 	_display.signal_leave_notify_event ().connect (sigc::mem_fun (*this, &EditorRegions::leave_notify), false);
299 
300 	ARDOUR_UI::instance ()->primary_clock->mode_changed.connect (sigc::mem_fun (*this, &EditorRegions::clock_format_changed));
301 
302 	e->EditorFreeze.connect (editor_freeze_connection, MISSING_INVALIDATOR, boost::bind (&EditorRegions::freeze_tree_model, this), gui_context ());
303 	e->EditorThaw.connect (editor_thaw_connection, MISSING_INVALIDATOR, boost::bind (&EditorRegions::thaw_tree_model, this), gui_context ());
304 }
305 
306 bool
focus_in(GdkEventFocus *)307 EditorRegions::focus_in (GdkEventFocus*)
308 {
309 	Window* win = dynamic_cast<Window*> (_scroller.get_toplevel ());
310 
311 	if (win) {
312 		old_focus = win->get_focus ();
313 	} else {
314 		old_focus = 0;
315 	}
316 
317 	name_editable = 0;
318 	tags_editable = 0;
319 
320 	/* try to do nothing on focus in (doesn't work, hence selection_count nonsense) */
321 	return true;
322 }
323 
324 bool
focus_out(GdkEventFocus *)325 EditorRegions::focus_out (GdkEventFocus*)
326 {
327 	if (old_focus) {
328 		old_focus->grab_focus ();
329 		old_focus = 0;
330 	}
331 
332 	name_editable = 0;
333 	tags_editable = 0;
334 
335 	return false;
336 }
337 
338 bool
enter_notify(GdkEventCrossing *)339 EditorRegions::enter_notify (GdkEventCrossing*)
340 {
341 	if (name_editable || tags_editable) {
342 		return true;
343 	}
344 
345 	Keyboard::magic_widget_grab_focus ();
346 	return false;
347 }
348 
349 bool
leave_notify(GdkEventCrossing *)350 EditorRegions::leave_notify (GdkEventCrossing*)
351 {
352 	if (old_focus) {
353 		old_focus->grab_focus ();
354 		old_focus = 0;
355 	}
356 
357 	Keyboard::magic_widget_drop_focus ();
358 	return false;
359 }
360 
361 void
set_session(ARDOUR::Session * s)362 EditorRegions::set_session (ARDOUR::Session* s)
363 {
364 	SessionHandlePtr::set_session (s);
365 
366 	ARDOUR::Region::RegionsPropertyChanged.connect (region_property_connection, MISSING_INVALIDATOR, boost::bind (&EditorRegions::regions_changed, this, _1, _2), gui_context ());
367 	ARDOUR::RegionFactory::CheckNewRegion.connect (check_new_region_connection, MISSING_INVALIDATOR, boost::bind (&EditorRegions::add_region, this, _1), gui_context ());
368 
369 	redisplay ();
370 }
371 
372 void
add_region(boost::shared_ptr<Region> region)373 EditorRegions::add_region (boost::shared_ptr<Region> region)
374 {
375 	if (!region || !_session) {
376 		return;
377 	}
378 
379 	/* whole-file regions are shown in the Source List */
380 	if (region->whole_file ()) {
381 		return;
382 	}
383 
384 	/* we only show files-on-disk.
385 	 * if there's some other kind of region, we ignore it (for now)
386 	 */
387 	boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (region->source());
388 	if (!fs || fs->empty()) {
389 		return;
390 	}
391 
392 	PropertyChange pc;
393 	boost::shared_ptr<RegionList> rl (new RegionList);
394 	rl->push_back (region);
395 	regions_changed (rl, pc);
396 }
397 
398 void
destroy_region(boost::shared_ptr<ARDOUR::Region> region)399 EditorRegions::destroy_region (boost::shared_ptr<ARDOUR::Region> region)
400 {
401 	//UNTESTED
402 	//At the time of writing, the only way to remove regions is "cleanup"
403 	//by definition, "cleanup" only removes regions that aren't on the timeline
404 	//so this would be a no-op anyway
405 	//perhaps someday we will allow users to manually destroy regions.
406 	RegionRowMap::iterator map_it = region_row_map.find (region);
407 	if (map_it != region_row_map.end ()) {
408 		region_row_map.erase (map_it);
409 		_model->erase (map_it->second);
410 	}
411 }
412 
413 void
remove_unused_regions()414 EditorRegions::remove_unused_regions ()
415 {
416 	vector<string> choices;
417 	string         prompt;
418 
419 	if (!_session) {
420 		return;
421 	}
422 
423 	prompt = _ ("Do you really want to remove unused regions?"
424 	            "\n(This is destructive and cannot be undone)");
425 
426 	choices.push_back (_ ("No, do nothing."));
427 	choices.push_back (_ ("Yes, remove."));
428 
429 	ArdourWidgets::Choice prompter (_ ("Remove unused regions"), prompt, choices);
430 
431 	if (prompter.run () == 1) {
432 		_no_redisplay = true;
433 		_session->cleanup_regions ();
434 		_no_redisplay = false;
435 		redisplay ();
436 	}
437 }
438 
439 void
regions_changed(boost::shared_ptr<RegionList> rl,const PropertyChange & what_changed)440 EditorRegions::regions_changed (boost::shared_ptr<RegionList> rl, const PropertyChange& what_changed)
441 {
442 	bool freeze = rl->size () > 2;
443 	if (freeze) {
444 		freeze_tree_model ();
445 	}
446 	for (RegionList::const_iterator i = rl->begin (); i != rl->end(); ++i) {
447 		boost::shared_ptr<Region> r = *i;
448 
449 		RegionRowMap::iterator map_it = region_row_map.find (r);
450 
451 		boost::shared_ptr<ARDOUR::Playlist> pl = r->playlist ();
452 		if (!(pl && _session && _session->playlist_is_active (pl))) {
453 			/* this region is not on an active playlist
454 			 * maybe it got deleted, or whatever */
455 			if (map_it != region_row_map.end ()) {
456 				Gtk::TreeModel::iterator r = map_it->second;
457 				region_row_map.erase (map_it);
458 				_model->erase (r);
459 			}
460 			break;
461 		}
462 
463 		if (map_it != region_row_map.end ()) {
464 			/* found the region, update its row properties */
465 			TreeModel::Row row = *(map_it->second);
466 			populate_row (r, row, what_changed);
467 
468 		} else {
469 			/* new region, add it to the list */
470 			TreeModel::iterator iter = _model->append ();
471 			TreeModel::Row      row  = *iter;
472 			region_row_map.insert (pair<boost::shared_ptr<ARDOUR::Region>, Gtk::TreeModel::iterator> (r, iter));
473 
474 			/* set the properties that don't change */
475 			row[_columns.region] = r;
476 
477 			/* now populate the properties that might change... */
478 			populate_row (r, row, PropertyChange ());
479 		}
480 	}
481 	if (freeze) {
482 		thaw_tree_model ();
483 	}
484 }
485 
486 void
selection_changed()487 EditorRegions::selection_changed ()
488 {
489 	_editor->_region_selection_change_updates_region_list = false;
490 
491 	if (_display.get_selection ()->count_selected_rows () > 0) {
492 		TreeIter                             iter;
493 		TreeView::Selection::ListHandle_Path rows = _display.get_selection ()->get_selected_rows ();
494 
495 		_editor->get_selection ().clear_regions ();
496 
497 		for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin (); i != rows.end (); ++i) {
498 			if ((iter = _model->get_iter (*i))) {
499 				boost::shared_ptr<Region> region = (*iter)[_columns.region];
500 
501 				// they could have clicked on a row that is just a placeholder, like "Hidden"
502 				// although that is not allowed by our selection filter. check it anyway
503 				// since we need a region ptr.
504 
505 				if (region) {
506 					_change_connection.block (true);
507 					_editor->set_selected_regionview_from_region_list (region, Selection::Add);
508 					_change_connection.block (false);
509 				}
510 			}
511 		}
512 	} else {
513 		_editor->get_selection ().clear_regions ();
514 	}
515 
516 	_editor->_region_selection_change_updates_region_list = true;
517 }
518 
519 void
set_selected(RegionSelection & regions)520 EditorRegions::set_selected (RegionSelection& regions)
521 {
522 	for (RegionSelection::iterator i = regions.begin (); i != regions.end (); ++i) {
523 		boost::shared_ptr<Region> r ((*i)->region ());
524 
525 		RegionRowMap::iterator it;
526 
527 		it = region_row_map.find (r);
528 
529 		if (it != region_row_map.end ()) {
530 			TreeModel::iterator j = it->second;
531 			_display.get_selection ()->select (*j);
532 		}
533 	}
534 }
535 
536 void
redisplay()537 EditorRegions::redisplay ()
538 {
539 	if (_no_redisplay || !_session) {
540 		return;
541 	}
542 
543 	/* store sort column id and type for later */
544 	_model->get_sort_column_id (_sort_col_id, _sort_type);
545 
546 	_display.set_model (Glib::RefPtr<Gtk::TreeStore> (0));
547 	_model->clear ();
548 	/* Disable sorting to gain performance */
549 	_model->set_sort_column (-2, SORT_ASCENDING);
550 
551 	region_row_map.clear ();
552 
553 	RegionFactory::foreach_region (sigc::mem_fun (*this, &EditorRegions::add_region));
554 
555 	_model->set_sort_column (_sort_col_id, _sort_type); // re-enabale sorting
556 	_display.set_model (_model);
557 }
558 
559 void
update_row(boost::shared_ptr<Region> region)560 EditorRegions::update_row (boost::shared_ptr<Region> region)
561 {
562 	if (!region || !_session) {
563 		return;
564 	}
565 
566 	RegionRowMap::iterator it;
567 
568 	it = region_row_map.find (region);
569 
570 	if (it != region_row_map.end ()) {
571 		PropertyChange      c;
572 		TreeModel::iterator j = it->second;
573 		populate_row (region, (*j), c);
574 	}
575 }
576 
577 void
clock_format_changed()578 EditorRegions::clock_format_changed ()
579 {
580 	if (!_session) {
581 		return;
582 	}
583 
584 	PropertyChange change;
585 	change.add (ARDOUR::Properties::start);
586 	change.add (ARDOUR::Properties::length);
587 	change.add (ARDOUR::Properties::position);
588 	change.add (ARDOUR::Properties::sync_position);
589 	change.add (ARDOUR::Properties::fade_in);
590 	change.add (ARDOUR::Properties::fade_out);
591 
592 	RegionRowMap::iterator i;
593 
594 	for (i = region_row_map.begin (); i != region_row_map.end (); ++i) {
595 		TreeModel::iterator j = i->second;
596 
597 		boost::shared_ptr<Region> region = (*j)[_columns.region];
598 
599 		populate_row (region, (*j), change);
600 	}
601 }
602 
603 void
format_position(samplepos_t pos,char * buf,size_t bufsize,bool onoff)604 EditorRegions::format_position (samplepos_t pos, char* buf, size_t bufsize, bool onoff)
605 {
606 	Timecode::BBT_Time bbt;
607 	Timecode::Time     timecode;
608 
609 	if (pos < 0) {
610 		error << string_compose (_ ("EditorRegions::format_position: negative timecode position: %1"), pos) << endmsg;
611 		snprintf (buf, bufsize, "invalid");
612 		return;
613 	}
614 
615 	switch (ARDOUR_UI::instance ()->primary_clock->mode ()) {
616 		case AudioClock::BBT:
617 			bbt = _session->tempo_map ().bbt_at_sample (pos);
618 			if (onoff) {
619 				snprintf (buf, bufsize, "%03d|%02d|%04d", bbt.bars, bbt.beats, bbt.ticks);
620 			} else {
621 				snprintf (buf, bufsize, "(%03d|%02d|%04d)", bbt.bars, bbt.beats, bbt.ticks);
622 			}
623 			break;
624 
625 		case AudioClock::MinSec:
626 			samplepos_t left;
627 			int         hrs;
628 			int         mins;
629 			float       secs;
630 
631 			left = pos;
632 			hrs  = (int)floor (left / (_session->sample_rate () * 60.0f * 60.0f));
633 			left -= (samplecnt_t)floor (hrs * _session->sample_rate () * 60.0f * 60.0f);
634 			mins = (int)floor (left / (_session->sample_rate () * 60.0f));
635 			left -= (samplecnt_t)floor (mins * _session->sample_rate () * 60.0f);
636 			secs = left / (float)_session->sample_rate ();
637 			if (onoff) {
638 				snprintf (buf, bufsize, "%02d:%02d:%06.3f", hrs, mins, secs);
639 			} else {
640 				snprintf (buf, bufsize, "(%02d:%02d:%06.3f)", hrs, mins, secs);
641 			}
642 			break;
643 
644 		case AudioClock::Seconds:
645 			if (onoff) {
646 				snprintf (buf, bufsize, "%.1f", pos / (float)_session->sample_rate ());
647 			} else {
648 				snprintf (buf, bufsize, "(%.1f)", pos / (float)_session->sample_rate ());
649 			}
650 			break;
651 
652 		case AudioClock::Samples:
653 			if (onoff) {
654 				snprintf (buf, bufsize, "%" PRId64, pos);
655 			} else {
656 				snprintf (buf, bufsize, "(%" PRId64 ")", pos);
657 			}
658 			break;
659 
660 		case AudioClock::Timecode:
661 		default:
662 			_session->timecode_time (pos, timecode);
663 			if (onoff) {
664 				snprintf (buf, bufsize, "%02d:%02d:%02d:%02d", timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
665 			} else {
666 				snprintf (buf, bufsize, "(%02d:%02d:%02d:%02d)", timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
667 			}
668 			break;
669 	}
670 }
671 
672 void
populate_row(boost::shared_ptr<Region> region,TreeModel::Row const & row,PBD::PropertyChange const & what_changed)673 EditorRegions::populate_row (boost::shared_ptr<Region> region, TreeModel::Row const& row, PBD::PropertyChange const& what_changed)
674 {
675 	/* the grid is most interested in the regions that are *visible* in the editor.
676 	 * this is a convenient place to flag changes to the grid cache, on a visible region */
677 	PropertyChange grid_interests;
678 	grid_interests.add (ARDOUR::Properties::position);
679 	grid_interests.add (ARDOUR::Properties::length);
680 	grid_interests.add (ARDOUR::Properties::sync_position);
681 	if (what_changed.contains (grid_interests)) {
682 		_editor->mark_region_boundary_cache_dirty ();
683 	}
684 
685 	{
686 		Gdk::Color c;
687 		bool       missing_source = boost::dynamic_pointer_cast<SilentFileSource> (region->source ()) != NULL;
688 		if (missing_source) {
689 			set_color_from_rgba (c, UIConfiguration::instance ().color ("region list missing source"));
690 		} else {
691 			set_color_from_rgba (c, UIConfiguration::instance ().color ("region list whole file"));
692 		}
693 		row[_columns.color_] = c;
694 	}
695 
696 	boost::shared_ptr<AudioRegion> audioregion = boost::dynamic_pointer_cast<AudioRegion> (region);
697 
698 	PropertyChange c;
699 	const bool     all = what_changed == c;
700 
701 	if (all || what_changed.contains (Properties::position)) {
702 		populate_row_position (region, row);
703 	}
704 	if (all || what_changed.contains (Properties::start) || what_changed.contains (Properties::sync_position)) {
705 		populate_row_sync (region, row);
706 	}
707 	if (all || what_changed.contains (Properties::fade_in)) {
708 		populate_row_fade_in (region, row, audioregion);
709 	}
710 	if (all || what_changed.contains (Properties::fade_out)) {
711 		populate_row_fade_out (region, row, audioregion);
712 	}
713 	if (all || what_changed.contains (Properties::locked)) {
714 		populate_row_locked (region, row);
715 	}
716 	if (all || what_changed.contains (Properties::position_lock_style)) {
717 		populate_row_glued (region, row);
718 	}
719 	if (all || what_changed.contains (Properties::muted)) {
720 		populate_row_muted (region, row);
721 	}
722 	if (all || what_changed.contains (Properties::opaque)) {
723 		populate_row_opaque (region, row);
724 	}
725 	if (all || what_changed.contains (Properties::length)) {
726 		populate_row_end (region, row);
727 		populate_row_length (region, row);
728 	}
729 	if (all) {
730 		populate_row_source (region, row);
731 	}
732 	if (all || what_changed.contains (Properties::name) || what_changed.contains (Properties::tags)) {
733 		populate_row_name (region, row);
734 	}
735 }
736 
737 #if 0
738 	if (audioRegion && fades_in_seconds) {
739 
740 		samplepos_t left;
741 		int mins;
742 		int millisecs;
743 
744 		left = audioRegion->fade_in()->back()->when;
745 		mins = (int) floor (left / (_session->sample_rate() * 60.0f));
746 		left -= (samplepos_t) floor (mins * _session->sample_rate() * 60.0f);
747 		millisecs = (int) floor ((left * 1000.0f) / _session->sample_rate());
748 
749 		if (audioRegion->fade_in()->back()->when >= _session->sample_rate()) {
750 			sprintf (fadein_str, "%01dM %01dmS", mins, millisecs);
751 		} else {
752 			sprintf (fadein_str, "%01dmS", millisecs);
753 		}
754 
755 		left = audioRegion->fade_out()->back()->when;
756 		mins = (int) floor (left / (_session->sample_rate() * 60.0f));
757 		left -= (samplepos_t) floor (mins * _session->sample_rate() * 60.0f);
758 		millisecs = (int) floor ((left * 1000.0f) / _session->sample_rate());
759 
760 		if (audioRegion->fade_out()->back()->when >= _session->sample_rate()) {
761 			sprintf (fadeout_str, "%01dM %01dmS", mins, millisecs);
762 		} else {
763 			sprintf (fadeout_str, "%01dmS", millisecs);
764 		}
765 	}
766 #endif
767 
768 void
populate_row_length(boost::shared_ptr<Region> region,TreeModel::Row const & row)769 EditorRegions::populate_row_length (boost::shared_ptr<Region> region, TreeModel::Row const& row)
770 {
771 	char buf[16];
772 
773 	if (ARDOUR_UI::instance ()->primary_clock->mode () == AudioClock::BBT) {
774 		TempoMap&          map (_session->tempo_map ());
775 		Timecode::BBT_Time bbt = map.bbt_at_beat (map.beat_at_sample (region->last_sample ()) - map.beat_at_sample (region->first_sample ()));
776 		snprintf (buf, sizeof (buf), "%03d|%02d|%04d", bbt.bars, bbt.beats, bbt.ticks);
777 	} else {
778 		format_position (region->length (), buf, sizeof (buf));
779 	}
780 
781 	row[_columns.length] = buf;
782 }
783 
784 void
populate_row_end(boost::shared_ptr<Region> region,TreeModel::Row const & row)785 EditorRegions::populate_row_end (boost::shared_ptr<Region> region, TreeModel::Row const& row)
786 {
787 #ifndef SHOW_REGION_EXTRAS
788 	return;
789 #endif
790 
791 	if (region->last_sample () >= region->first_sample ()) {
792 		char buf[16];
793 		format_position (region->last_sample (), buf, sizeof (buf));
794 		row[_columns.end] = buf;
795 	} else {
796 		row[_columns.end] = "empty";
797 	}
798 }
799 
800 void
populate_row_position(boost::shared_ptr<Region> region,TreeModel::Row const & row)801 EditorRegions::populate_row_position (boost::shared_ptr<Region> region, TreeModel::Row const& row)
802 {
803 	row[_columns.position] = region->position ();
804 
805 	char buf[16];
806 	format_position (region->position (), buf, sizeof (buf));
807 	row[_columns.start] = buf;
808 }
809 
810 void
populate_row_sync(boost::shared_ptr<Region> region,TreeModel::Row const & row)811 EditorRegions::populate_row_sync (boost::shared_ptr<Region> region, TreeModel::Row const& row)
812 {
813 #ifndef SHOW_REGION_EXTRAS
814 	return;
815 #endif
816 	if (region->sync_position () == region->position ()) {
817 		row[_columns.sync] = _ ("Start");
818 	} else if (region->sync_position () == (region->last_sample ())) {
819 		row[_columns.sync] = _ ("End");
820 	} else {
821 		char buf[16];
822 		format_position (region->sync_position (), buf, sizeof (buf));
823 		row[_columns.sync] = buf;
824 	}
825 }
826 
827 void
populate_row_fade_in(boost::shared_ptr<Region> region,TreeModel::Row const & row,boost::shared_ptr<AudioRegion> audioregion)828 EditorRegions::populate_row_fade_in (boost::shared_ptr<Region> region, TreeModel::Row const& row, boost::shared_ptr<AudioRegion> audioregion)
829 {
830 #ifndef SHOW_REGION_EXTRAS
831 	return;
832 #endif
833 	if (!audioregion) {
834 		row[_columns.fadein] = "";
835 	} else {
836 		char buf[32];
837 		format_position (audioregion->fade_in ()->back ()->when, buf, sizeof (buf), audioregion->fade_in_active ());
838 		row[_columns.fadein] = buf;
839 	}
840 }
841 
842 void
populate_row_fade_out(boost::shared_ptr<Region> region,TreeModel::Row const & row,boost::shared_ptr<AudioRegion> audioregion)843 EditorRegions::populate_row_fade_out (boost::shared_ptr<Region> region, TreeModel::Row const& row, boost::shared_ptr<AudioRegion> audioregion)
844 {
845 #ifndef SHOW_REGION_EXTRAS
846 	return;
847 #endif
848 	if (!audioregion) {
849 		row[_columns.fadeout] = "";
850 	} else {
851 		char buf[32];
852 		format_position (audioregion->fade_out ()->back ()->when, buf, sizeof (buf), audioregion->fade_out_active ());
853 		row[_columns.fadeout] = buf;
854 	}
855 }
856 
857 void
populate_row_locked(boost::shared_ptr<Region> region,TreeModel::Row const & row)858 EditorRegions::populate_row_locked (boost::shared_ptr<Region> region, TreeModel::Row const& row)
859 {
860 	row[_columns.locked] = region->locked ();
861 }
862 
863 void
populate_row_glued(boost::shared_ptr<Region> region,TreeModel::Row const & row)864 EditorRegions::populate_row_glued (boost::shared_ptr<Region> region, TreeModel::Row const& row)
865 {
866 	if (region->position_lock_style () == MusicTime) {
867 		row[_columns.glued] = true;
868 	} else {
869 		row[_columns.glued] = false;
870 	}
871 }
872 
873 void
populate_row_muted(boost::shared_ptr<Region> region,TreeModel::Row const & row)874 EditorRegions::populate_row_muted (boost::shared_ptr<Region> region, TreeModel::Row const& row)
875 {
876 	row[_columns.muted] = region->muted ();
877 }
878 
879 void
populate_row_opaque(boost::shared_ptr<Region> region,TreeModel::Row const & row)880 EditorRegions::populate_row_opaque (boost::shared_ptr<Region> region, TreeModel::Row const& row)
881 {
882 	row[_columns.opaque] = region->opaque ();
883 }
884 
885 void
populate_row_name(boost::shared_ptr<Region> region,TreeModel::Row const & row)886 EditorRegions::populate_row_name (boost::shared_ptr<Region> region, TreeModel::Row const& row)
887 {
888 	row[_columns.name] = Gtkmm2ext::markup_escape_text (region->name ());
889 
890 	if (region->data_type() == DataType::MIDI) {
891 		row[_columns.channels] = 0;  /*TODO: some better recognition of midi regions*/
892 	} else {
893 		row[_columns.channels] = region->n_channels();
894 	}
895 
896 	row[_columns.tags] = region->tags ();
897 }
898 
899 void
populate_row_source(boost::shared_ptr<Region> region,TreeModel::Row const & row)900 EditorRegions::populate_row_source (boost::shared_ptr<Region> region, TreeModel::Row const& row)
901 {
902 	if (boost::dynamic_pointer_cast<SilentFileSource> (region->source ())) {
903 		row[_columns.path] = _ ("MISSING ") + Gtkmm2ext::markup_escape_text (region->source ()->name ());
904 	} else {
905 		row[_columns.path] = Gtkmm2ext::markup_escape_text (region->source ()->name ());
906 	}
907 }
908 
909 void
show_context_menu(int button,int time)910 EditorRegions::show_context_menu (int button, int time)
911 {
912 	using namespace Gtk::Menu_Helpers;
913 	Gtk::Menu* menu = dynamic_cast<Menu*> (ActionManager::get_widget (X_ ("/PopupRegionMenu")));
914 	menu->popup (button, time);
915 }
916 
917 bool
key_press(GdkEventKey * ev)918 EditorRegions::key_press (GdkEventKey* ev)
919 {
920 	TreeViewColumn* col;
921 
922 	switch (ev->keyval) {
923 		case GDK_Tab:
924 		case GDK_ISO_Left_Tab:
925 
926 			if (name_editable) {
927 				name_editable->editing_done ();
928 				name_editable = 0;
929 			}
930 
931 			if (tags_editable) {
932 				tags_editable->editing_done ();
933 				tags_editable = 0;
934 			}
935 
936 			col = _display.get_column (0); // select&focus on name column
937 
938 			if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
939 				treeview_select_previous (_display, _model, col);
940 			} else {
941 				treeview_select_next (_display, _model, col);
942 			}
943 
944 			return true;
945 			break;
946 
947 		default:
948 			break;
949 	}
950 
951 	return false;
952 }
953 
954 bool
button_press(GdkEventButton * ev)955 EditorRegions::button_press (GdkEventButton* ev)
956 {
957 	boost::shared_ptr<Region> region;
958 	TreeIter                  iter;
959 	TreeModel::Path           path;
960 	TreeViewColumn*           column;
961 	int                       cellx;
962 	int                       celly;
963 
964 	if (_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
965 		if ((iter = _model->get_iter (path))) {
966 			region = (*iter)[_columns.region];
967 		}
968 	}
969 
970 	if (Keyboard::is_context_menu_event (ev)) {
971 		show_context_menu (ev->button, ev->time);
972 		return true;
973 	}
974 
975 	if (region != 0 && Keyboard::is_button2_event (ev)) {
976 		/* start/stop audition */
977 		if (!Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
978 			_editor->consider_auditioning (region);
979 		}
980 		return true;
981 	}
982 
983 	return false;
984 }
985 
986 void
selection_mapover(sigc::slot<void,boost::shared_ptr<Region>> sl)987 EditorRegions::selection_mapover (sigc::slot<void, boost::shared_ptr<Region> > sl)
988 {
989 	Glib::RefPtr<TreeSelection>                    selection = _display.get_selection ();
990 	TreeView::Selection::ListHandle_Path           rows      = selection->get_selected_rows ();
991 	TreeView::Selection::ListHandle_Path::iterator i         = rows.begin ();
992 
993 	if (selection->count_selected_rows () == 0 || _session == 0) {
994 		return;
995 	}
996 
997 	for (; i != rows.end (); ++i) {
998 		TreeIter iter;
999 
1000 		if ((iter = _model->get_iter (*i))) {
1001 			/* some rows don't have a region associated with them, but can still be
1002 			   selected (XXX maybe prevent them from being selected)
1003 			*/
1004 
1005 			boost::shared_ptr<Region> r = (*iter)[_columns.region];
1006 
1007 			if (r) {
1008 				sl (r);
1009 			}
1010 		}
1011 	}
1012 }
1013 
1014 void
drag_data_received(const RefPtr<Gdk::DragContext> & context,int x,int y,const SelectionData & data,guint info,guint time)1015 EditorRegions::drag_data_received (const RefPtr<Gdk::DragContext>& context,
1016                                    int x, int y,
1017                                    const SelectionData& data,
1018                                    guint info, guint time)
1019 {
1020 	vector<string> paths;
1021 
1022 	if (data.get_target () == "GTK_TREE_MODEL_ROW") {
1023 		/* something is being dragged over the region list */
1024 		_editor->_drags->abort ();
1025 		_display.on_drag_data_received (context, x, y, data, info, time);
1026 		return;
1027 	}
1028 
1029 	if (_editor->convert_drop_to_paths (paths, context, x, y, data, info, time) == 0) {
1030 		samplepos_t pos  = 0;
1031 		bool        copy = ((context->get_actions () & (Gdk::ACTION_COPY | Gdk::ACTION_LINK | Gdk::ACTION_MOVE)) == Gdk::ACTION_COPY);
1032 
1033 		if (UIConfiguration::instance ().get_only_copy_imported_files () || copy) {
1034 			_editor->do_import (paths, Editing::ImportDistinctFiles, Editing::ImportAsRegion,
1035 			                    SrcBest, SMFTrackName, SMFTempoIgnore, pos);
1036 		} else {
1037 			_editor->do_embed (paths, Editing::ImportDistinctFiles, ImportAsRegion, pos);
1038 		}
1039 		context->drag_finish (true, false, time);
1040 	}
1041 }
1042 
1043 bool
selection_filter(const RefPtr<TreeModel> & model,const TreeModel::Path & path,bool already_selected)1044 EditorRegions::selection_filter (const RefPtr<TreeModel>& model, const TreeModel::Path& path, bool already_selected)
1045 {
1046 	if (already_selected) {
1047 		/* deselecting path, if it is selected, is OK */
1048 		return true;
1049 	}
1050 
1051 	/* not possible to select rows that do not represent regions, like "Hidden" */
1052 	TreeModel::iterator iter = model->get_iter (path);
1053 	if (iter) {
1054 		boost::shared_ptr<Region> r = (*iter)[_columns.region];
1055 		if (!r) {
1056 			return false;
1057 		}
1058 	}
1059 
1060 	return true;
1061 }
1062 
1063 void
name_editing_started(CellEditable * ce,const Glib::ustring & path)1064 EditorRegions::name_editing_started (CellEditable* ce, const Glib::ustring& path)
1065 {
1066 	name_editable = ce;
1067 
1068 	/* give it a special name */
1069 
1070 	Gtk::Entry* e = dynamic_cast<Gtk::Entry*> (ce);
1071 
1072 	if (e) {
1073 		e->set_name (X_ ("RegionNameEditorEntry"));
1074 
1075 		TreeIter iter;
1076 		if ((iter = _model->get_iter (path))) {
1077 			boost::shared_ptr<Region> region = (*iter)[_columns.region];
1078 
1079 			if (region) {
1080 				e->set_text (region->name ());
1081 			}
1082 		}
1083 	}
1084 }
1085 
1086 void
name_edit(const std::string & path,const std::string & new_text)1087 EditorRegions::name_edit (const std::string& path, const std::string& new_text)
1088 {
1089 	name_editable = 0;
1090 
1091 	boost::shared_ptr<Region> region;
1092 	TreeIter                  row_iter;
1093 
1094 	if ((row_iter = _model->get_iter (path))) {
1095 		region                     = (*row_iter)[_columns.region];
1096 		(*row_iter)[_columns.name] = new_text;
1097 	}
1098 
1099 	if (region) {
1100 		region->set_name (new_text);
1101 
1102 		populate_row_name (region, (*row_iter));
1103 	}
1104 }
1105 
1106 void
tag_editing_started(CellEditable * ce,const Glib::ustring & path)1107 EditorRegions::tag_editing_started (CellEditable* ce, const Glib::ustring& path)
1108 {
1109 	tags_editable = ce;
1110 
1111 	/* give it a special name */
1112 
1113 	Gtk::Entry* e = dynamic_cast<Gtk::Entry*> (ce);
1114 
1115 	if (e) {
1116 		e->set_name (X_ ("RegionTagEditorEntry"));
1117 
1118 		TreeIter iter;
1119 		if ((iter = _model->get_iter (path))) {
1120 			boost::shared_ptr<Region> region = (*iter)[_columns.region];
1121 
1122 			if (region) {
1123 				e->set_text (region->tags ());
1124 			}
1125 		}
1126 	}
1127 }
1128 
1129 void
tag_edit(const std::string & path,const std::string & new_text)1130 EditorRegions::tag_edit (const std::string& path, const std::string& new_text)
1131 {
1132 	tags_editable = 0;
1133 
1134 	boost::shared_ptr<Region> region;
1135 	TreeIter                  row_iter;
1136 
1137 	if ((row_iter = _model->get_iter (path))) {
1138 		region                     = (*row_iter)[_columns.region];
1139 		(*row_iter)[_columns.tags] = new_text;
1140 	}
1141 
1142 	if (region) {
1143 		region->set_tags (new_text);
1144 
1145 		populate_row_name (region, (*row_iter));
1146 	}
1147 }
1148 
1149 /** @return Region that has been dragged out of the list, or 0 */
1150 boost::shared_ptr<Region>
get_dragged_region()1151 EditorRegions::get_dragged_region ()
1152 {
1153 	list<boost::shared_ptr<Region> > regions;
1154 	TreeView*                        source;
1155 	_display.get_object_drag_data (regions, &source);
1156 
1157 	if (regions.empty ()) {
1158 		return boost::shared_ptr<Region> ();
1159 	}
1160 
1161 	return regions.front ();
1162 }
1163 
1164 void
clear()1165 EditorRegions::clear ()
1166 {
1167 	_display.set_model (Glib::RefPtr<Gtk::TreeStore> (0));
1168 	_model->clear ();
1169 	_display.set_model (_model);
1170 
1171 	/* Clean up the maps */
1172 	region_row_map.clear ();
1173 }
1174 
1175 boost::shared_ptr<Region>
get_single_selection()1176 EditorRegions::get_single_selection ()
1177 {
1178 	Glib::RefPtr<TreeSelection> selected = _display.get_selection ();
1179 
1180 	if (selected->count_selected_rows () != 1) {
1181 		return boost::shared_ptr<Region> ();
1182 	}
1183 
1184 	TreeView::Selection::ListHandle_Path rows = selected->get_selected_rows ();
1185 
1186 	/* only one row selected, so rows.begin() is it */
1187 
1188 	TreeIter iter = _model->get_iter (*rows.begin ());
1189 
1190 	if (!iter) {
1191 		return boost::shared_ptr<Region> ();
1192 	}
1193 
1194 	return (*iter)[_columns.region];
1195 }
1196 
1197 void
freeze_tree_model()1198 EditorRegions::freeze_tree_model ()
1199 {
1200 	/* store sort column id and type for later */
1201 	_model->get_sort_column_id (_sort_col_id, _sort_type);
1202 	_change_connection.block (true);
1203 	_display.set_model (Glib::RefPtr<Gtk::TreeStore> (0));
1204 	_model->set_sort_column (-2, SORT_ASCENDING); //Disable sorting to gain performance
1205 }
1206 
1207 void
thaw_tree_model()1208 EditorRegions::thaw_tree_model ()
1209 {
1210 	_model->set_sort_column (_sort_col_id, _sort_type); // re-enabale sorting
1211 	_display.set_model (_model);
1212 	_change_connection.block (false);
1213 }
1214 
1215 void
locked_changed(std::string const & path)1216 EditorRegions::locked_changed (std::string const& path)
1217 {
1218 	TreeIter i = _model->get_iter (path);
1219 	if (i) {
1220 		boost::shared_ptr<ARDOUR::Region> region = (*i)[_columns.region];
1221 		if (region) {
1222 			region->set_locked (!(*i)[_columns.locked]);
1223 		}
1224 	}
1225 }
1226 
1227 void
glued_changed(std::string const & path)1228 EditorRegions::glued_changed (std::string const& path)
1229 {
1230 	TreeIter i = _model->get_iter (path);
1231 	if (i) {
1232 		boost::shared_ptr<ARDOUR::Region> region = (*i)[_columns.region];
1233 		if (region) {
1234 			/* `glued' means MusicTime, and we're toggling here */
1235 			region->set_position_lock_style ((*i)[_columns.glued] ? AudioTime : MusicTime);
1236 		}
1237 	}
1238 }
1239 
1240 void
muted_changed(std::string const & path)1241 EditorRegions::muted_changed (std::string const& path)
1242 {
1243 	TreeIter i = _model->get_iter (path);
1244 	if (i) {
1245 		boost::shared_ptr<ARDOUR::Region> region = (*i)[_columns.region];
1246 		if (region) {
1247 			region->set_muted (!(*i)[_columns.muted]);
1248 		}
1249 	}
1250 }
1251 
1252 void
opaque_changed(std::string const & path)1253 EditorRegions::opaque_changed (std::string const& path)
1254 {
1255 	TreeIter i = _model->get_iter (path);
1256 	if (i) {
1257 		boost::shared_ptr<ARDOUR::Region> region = (*i)[_columns.region];
1258 		if (region) {
1259 			region->set_opaque (!(*i)[_columns.opaque]);
1260 		}
1261 	}
1262 }
1263 
1264 XMLNode&
get_state() const1265 EditorRegions::get_state () const
1266 {
1267 	XMLNode* node = new XMLNode (X_ ("RegionList"));
1268 
1269 	//TODO:  save sort state?
1270 	//	node->set_property (X_("sort-col"), _sort_type);
1271 	//	node->set_property (X_("sort-asc"), _sort_type);
1272 
1273 	return *node;
1274 }
1275 
1276 void
set_state(const XMLNode & node)1277 EditorRegions::set_state (const XMLNode& node)
1278 {
1279 	bool changed = false;
1280 
1281 	if (node.name () != X_ ("RegionList")) {
1282 		return;
1283 	}
1284 
1285 	if (changed) {
1286 		redisplay ();
1287 	}
1288 }
1289 
1290 RefPtr<Action>
remove_unused_regions_action() const1291 EditorRegions::remove_unused_regions_action () const
1292 {
1293 	return ActionManager::get_action (X_ ("RegionList"), X_ ("removeUnusedRegions"));
1294 }
1295