NOOP, remove trailing tabs/whitespace.
[ardour.git] / gtk2_ardour / editor_regions.cc
1 /*
2     Copyright (C) 2000-2005 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <cstdlib>
21 #include <cmath>
22 #include <algorithm>
23 #include <string>
24 #include <sstream>
25
26 #include "pbd/basename.h"
27 #include "pbd/enumwriter.h"
28
29 #include "ardour/audioregion.h"
30 #include "ardour/audiofilesource.h"
31 #include "ardour/silentfilesource.h"
32 #include "ardour/region_factory.h"
33 #include "ardour/session.h"
34 #include "ardour/profile.h"
35
36 #include "gtkmm2ext/choice.h"
37 #include "gtkmm2ext/treeutils.h"
38
39 #include "audio_clock.h"
40 #include "editor.h"
41 #include "editing.h"
42 #include "keyboard.h"
43 #include "ardour_ui.h"
44 #include "gui_thread.h"
45 #include "actions.h"
46 #include "region_view.h"
47 #include "utils.h"
48 #include "editor_regions.h"
49 #include "editor_drag.h"
50 #include "main_clock.h"
51 #include "tooltips.h"
52 #include "ui_config.h"
53
54 #include "i18n.h"
55
56 using namespace std;
57 using namespace ARDOUR;
58 using namespace ARDOUR_UI_UTILS;
59 using namespace PBD;
60 using namespace Gtk;
61 using namespace Glib;
62 using namespace Editing;
63 using Gtkmm2ext::Keyboard;
64
65 struct ColumnInfo {
66     int         index;
67     const char* label;
68     const char* tooltip;
69 };
70
71 EditorRegions::EditorRegions (Editor* e)
72         : EditorComponent (e)
73         , old_focus (0)
74         , name_editable (0)
75         , _menu (0)
76         , _show_automatic_regions (true)
77         , ignore_region_list_selection_change (false)
78         , ignore_selected_region_change (false)
79         , _no_redisplay (false)
80         , _sort_type ((Editing::RegionListSortType) 0)
81         , expanded (false)
82 {
83         _display.set_size_request (100, -1);
84         _display.set_rules_hint (true);
85         _display.set_name ("EditGroupList");
86
87         /* Try to prevent single mouse presses from initiating edits.
88            This relies on a hack in gtktreeview.c:gtk_treeview_button_press()
89         */
90         _display.set_data ("mouse-edits-require-mod1", (gpointer) 0x1);
91
92         _model = TreeStore::create (_columns);
93         _model->set_sort_func (0, sigc::mem_fun (*this, &EditorRegions::sorter));
94         _model->set_sort_column (0, SORT_ASCENDING);
95
96         _display.set_model (_model);
97
98         _display.append_column ("", _columns.name);
99         _display.append_column ("", _columns.position);
100         _display.append_column ("", _columns.end);
101         _display.append_column ("", _columns.length);
102         _display.append_column ("", _columns.sync);
103         _display.append_column ("", _columns.fadein);
104         _display.append_column ("", _columns.fadeout);
105         _display.append_column ("", _columns.locked);
106         _display.append_column ("", _columns.glued);
107         _display.append_column ("", _columns.muted);
108         _display.append_column ("", _columns.opaque);
109
110         TreeViewColumn* col;
111         Gtk::Label* l;
112
113         ColumnInfo ci[] = {
114                 { 0,   _("Region"),    _("Region name, with number of channels in []'s") },
115                 { 1,   _("Position"),  _("Position of start of region") },
116                 { 2,   _("End"),       _("Position of end of region") },
117                 { 3,   _("Length"),    _("Length of the region") },
118                 { 4,   _("Sync"),      _("Position of region sync point, relative to start of the region") },
119                 { 5,   _("Fade In"),   _("Length of region fade-in (units: secondary clock), () if disabled") },
120                 { 6,   _("Fade Out"),  _("Length of region fade-out (units: secondary clock), () if disabled") },
121                 { 7,  S_("Lock|L"),    _("Region position locked?") },
122                 { 8,  S_("Gain|G"),    _("Region position glued to Bars|Beats time?") },
123                 { 9,  S_("Mute|M"),    _("Region muted?") },
124                 { 10, S_("Opaque|O"),  _("Region opaque (blocks regions below it from being heard)?") },
125                 { -1, 0, 0 }
126         };
127
128         for (int i = 0; ci[i].index >= 0; ++i) {
129                 col = _display.get_column (ci[i].index);
130                 l = manage (new Label (ci[i].label));
131                 set_tooltip (*l, ci[i].tooltip);
132                 col->set_widget (*l);
133                 l->show ();
134
135                 if (ci[i].index > 6) {
136                         col->set_expand (false);
137                         col->set_alignment (ALIGN_CENTER);
138                 }
139         }
140
141         _display.set_headers_visible (true);
142         _display.set_rules_hint ();
143
144         /* show path as the row tooltip */
145         _display.set_tooltip_column (14); /* path */
146
147         CellRendererText* region_name_cell = dynamic_cast<CellRendererText*>(_display.get_column_cell_renderer (0));
148         region_name_cell->property_editable() = true;
149         region_name_cell->signal_edited().connect (sigc::mem_fun (*this, &EditorRegions::name_edit));
150         region_name_cell->signal_editing_started().connect (sigc::mem_fun (*this, &EditorRegions::name_editing_started));
151
152         _display.get_selection()->set_select_function (sigc::mem_fun (*this, &EditorRegions::selection_filter));
153
154         TreeViewColumn* tv_col = _display.get_column(0);
155         CellRendererText* renderer = dynamic_cast<CellRendererText*>(_display.get_column_cell_renderer (0));
156         tv_col->add_attribute(renderer->property_text(), _columns.name);
157         tv_col->add_attribute(renderer->property_foreground_gdk(), _columns.color_);
158         tv_col->set_expand (true);
159
160         CellRendererToggle* locked_cell = dynamic_cast<CellRendererToggle*> (_display.get_column_cell_renderer (7));
161         locked_cell->property_activatable() = true;
162         locked_cell->signal_toggled().connect (sigc::mem_fun (*this, &EditorRegions::locked_changed));
163
164         TreeViewColumn* locked_col = _display.get_column (7);
165         locked_col->add_attribute (locked_cell->property_visible(), _columns.property_toggles_visible);
166
167         CellRendererToggle* glued_cell = dynamic_cast<CellRendererToggle*> (_display.get_column_cell_renderer (8));
168         glued_cell->property_activatable() = true;
169         glued_cell->signal_toggled().connect (sigc::mem_fun (*this, &EditorRegions::glued_changed));
170
171         TreeViewColumn* glued_col = _display.get_column (8);
172         glued_col->add_attribute (glued_cell->property_visible(), _columns.property_toggles_visible);
173
174         CellRendererToggle* muted_cell = dynamic_cast<CellRendererToggle*> (_display.get_column_cell_renderer (9));
175         muted_cell->property_activatable() = true;
176         muted_cell->signal_toggled().connect (sigc::mem_fun (*this, &EditorRegions::muted_changed));
177
178         TreeViewColumn* muted_col = _display.get_column (9);
179         muted_col->add_attribute (muted_cell->property_visible(), _columns.property_toggles_visible);
180
181         CellRendererToggle* opaque_cell = dynamic_cast<CellRendererToggle*> (_display.get_column_cell_renderer (10));
182         opaque_cell->property_activatable() = true;
183         opaque_cell->signal_toggled().connect (sigc::mem_fun (*this, &EditorRegions::opaque_changed));
184
185         TreeViewColumn* opaque_col = _display.get_column (10);
186         opaque_col->add_attribute (opaque_cell->property_visible(), _columns.property_toggles_visible);
187
188         _display.get_selection()->set_mode (SELECTION_MULTIPLE);
189         _display.add_object_drag (_columns.region.index(), "regions");
190
191         /* setup DnD handling */
192
193         list<TargetEntry> region_list_target_table;
194
195         region_list_target_table.push_back (TargetEntry ("text/plain"));
196         region_list_target_table.push_back (TargetEntry ("text/uri-list"));
197         region_list_target_table.push_back (TargetEntry ("application/x-rootwin-drop"));
198
199         _display.add_drop_targets (region_list_target_table);
200         _display.signal_drag_data_received().connect (sigc::mem_fun(*this, &EditorRegions::drag_data_received));
201
202         _scroller.add (_display);
203         _scroller.set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC);
204
205         _display.signal_button_press_event().connect (sigc::mem_fun(*this, &EditorRegions::button_press), false);
206         _change_connection = _display.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &EditorRegions::selection_changed));
207
208         _scroller.signal_key_press_event().connect (sigc::mem_fun(*this, &EditorRegions::key_press), false);
209         _scroller.signal_focus_in_event().connect (sigc::mem_fun (*this, &EditorRegions::focus_in), false);
210         _scroller.signal_focus_out_event().connect (sigc::mem_fun (*this, &EditorRegions::focus_out));
211
212         _display.signal_enter_notify_event().connect (sigc::mem_fun (*this, &EditorRegions::enter_notify), false);
213         _display.signal_leave_notify_event().connect (sigc::mem_fun (*this, &EditorRegions::leave_notify), false);
214
215         // _display.signal_popup_menu().connect (sigc::bind (sigc::mem_fun (*this, &Editor::show__display_context_menu), 1, 0));
216
217         //ARDOUR_UI::instance()->secondary_clock.mode_changed.connect (sigc::mem_fun(*this, &Editor::redisplay_regions));
218         ARDOUR_UI::instance()->secondary_clock->mode_changed.connect (sigc::mem_fun(*this, &EditorRegions::update_all_rows));
219         ARDOUR::Region::RegionPropertyChanged.connect (region_property_connection, MISSING_INVALIDATOR, boost::bind (&EditorRegions::region_changed, this, _1, _2), gui_context());
220         ARDOUR::RegionFactory::CheckNewRegion.connect (check_new_region_connection, MISSING_INVALIDATOR, boost::bind (&EditorRegions::add_region, this, _1), gui_context());
221
222         e->EditorFreeze.connect (editor_freeze_connection, MISSING_INVALIDATOR, boost::bind (&EditorRegions::freeze_tree_model, this), gui_context());
223         e->EditorThaw.connect (editor_thaw_connection, MISSING_INVALIDATOR, boost::bind (&EditorRegions::thaw_tree_model, this), gui_context());
224 }
225
226 bool
227 EditorRegions::focus_in (GdkEventFocus*)
228 {
229         Window* win = dynamic_cast<Window*> (_scroller.get_toplevel ());
230
231         if (win) {
232                 old_focus = win->get_focus ();
233         } else {
234                 old_focus = 0;
235         }
236
237         name_editable = 0;
238
239         /* try to do nothing on focus in (doesn't work, hence selection_count nonsense) */
240         return true;
241 }
242
243 bool
244 EditorRegions::focus_out (GdkEventFocus*)
245 {
246         if (old_focus) {
247                 old_focus->grab_focus ();
248                 old_focus = 0;
249         }
250
251         name_editable = 0;
252
253         return false;
254 }
255
256 bool
257 EditorRegions::enter_notify (GdkEventCrossing*)
258 {
259         if (name_editable) {
260                 return true;
261         }
262
263         /* arm counter so that ::selection_filter() will deny selecting anything for the
264            next two attempts to change selection status.
265         */
266         _scroller.grab_focus ();
267         Keyboard::magic_widget_grab_focus ();
268         return false;
269 }
270
271 bool
272 EditorRegions::leave_notify (GdkEventCrossing*)
273 {
274         if (old_focus) {
275                 old_focus->grab_focus ();
276                 old_focus = 0;
277         }
278
279         Keyboard::magic_widget_drop_focus ();
280         return false;
281 }
282
283 void
284 EditorRegions::set_session (ARDOUR::Session* s)
285 {
286         SessionHandlePtr::set_session (s);
287         redisplay ();
288 }
289
290 void
291 EditorRegions::add_region (boost::shared_ptr<Region> region)
292 {
293         if (!region || !_session) {
294                 return;
295         }
296
297         string str;
298         TreeModel::Row row;
299         Gdk::Color c;
300         bool missing_source = boost::dynamic_pointer_cast<SilentFileSource>(region->source()) != NULL;
301
302         if (!_show_automatic_regions && region->automatic()) {
303                 return;
304         }
305
306         if (region->hidden()) {
307
308                 TreeModel::iterator iter = _model->get_iter ("0");
309                 TreeModel::Row parent;
310
311                 if (!iter) {
312                         parent = *(_model->append());
313                         parent[_columns.name] = _("Hidden");
314                         boost::shared_ptr<Region> proxy = parent[_columns.region];
315                         proxy.reset ();
316                 } else {
317                         string s = (*iter)[_columns.name];
318                         if (s != _("Hidden")) {
319                                 parent = *(_model->insert(iter));
320                                 parent[_columns.name] = _("Hidden");
321                                 boost::shared_ptr<Region> proxy = parent[_columns.region];
322                                 proxy.reset ();
323                         } else {
324                                 parent = *iter;
325                         }
326                 }
327
328                 row = *(_model->append (parent.children()));
329
330         } else if (region->whole_file()) {
331
332                 TreeModel::iterator i;
333                 TreeModel::Children rows = _model->children();
334
335                 for (i = rows.begin(); i != rows.end(); ++i) {
336                         boost::shared_ptr<Region> rr = (*i)[_columns.region];
337
338                         if (rr && region->region_list_equivalent (rr)) {
339                                 return;
340                         }
341                 }
342
343                 row = *(_model->append());
344
345                 if (missing_source) {
346                         // c.set_rgb(65535,0,0);     // FIXME: error color from style
347                         set_color_from_rgba (c, UIConfiguration::instance().color ("region list missing source"));
348
349                 } else if (region->automatic()){
350                         // c.set_rgb(0,65535,0);     // FIXME: error color from style
351                         set_color_from_rgba (c, UIConfiguration::instance().color ("region list automatic"));
352
353                 } else {
354                         set_color_from_rgba (c, UIConfiguration::instance().color ("region list whole file"));
355                 }
356
357                 row[_columns.color_] = c;
358
359                 if (region->source()->name()[0] == '/') { // external file
360
361                         if (region->whole_file()) {
362
363                                 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(region->source());
364                                 str = ".../";
365
366                                 if (afs) {
367                                         str = region_name_from_path (afs->path(), region->n_channels() > 1);
368                                 } else {
369                                         str += region->source()->name();
370                                 }
371
372                         } else {
373                                 str = region->name();
374                         }
375
376                 } else {
377                         str = region->name();
378                 }
379
380                 if (region->n_channels() > 1) {
381                         std::stringstream foo;
382                         foo << region->n_channels ();
383                         str += " [";
384                         str += foo.str();
385                         str += "]";
386                 }
387
388                 row[_columns.name] = str;
389                 row[_columns.region] = region;
390                 row[_columns.property_toggles_visible] = false;
391
392                 if (missing_source) {
393                         row[_columns.path] = _("(MISSING) ") + Glib::Markup::escape_text (region->source()->name());
394
395                 } else {
396                         boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource>(region->source());
397                         if (fs) {
398                                 row[_columns.path] = Glib::Markup::escape_text (fs->path());
399                         } else {
400                                 row[_columns.path] = Glib::Markup::escape_text (region->source()->name());
401                         }
402                 }
403
404                 region_row_map.insert(pair<boost::shared_ptr<ARDOUR::Region>, Gtk::TreeModel::RowReference>(region, TreeRowReference(_model, TreePath (row))) );
405                 parent_regions_sources_map.insert(pair<string, Gtk::TreeModel::RowReference>(region->source_string(), TreeRowReference(_model, TreePath (row))) );
406
407                 return;
408
409         } else {
410                 // find parent node, add as new child
411                 TreeModel::iterator i;
412
413                 boost::unordered_map<string, Gtk::TreeModel::RowReference>::iterator it;
414
415                 it = parent_regions_sources_map.find (region->source_string());
416
417                 if (it != parent_regions_sources_map.end()){
418
419                         TreeModel::iterator j = _model->get_iter ((*it).second.get_path());
420
421                         TreeModel::iterator ii;
422                         TreeModel::Children subrows = (*j).children();
423
424                         /* XXXX: should we be accounting for all regions? */
425                         /*
426                         for (ii = subrows.begin(); ii != subrows.end(); ++ii) {
427                                 boost::shared_ptr<Region> rr = (*ii)[_columns.region];
428
429                                 if (region->region_list_equivalent (rr)) {
430                                         return;
431                                 }
432                         }
433                         */
434
435                         row = *(_model->insert (subrows.end()));
436
437                 } else {
438                         row = *(_model->append());
439                 }
440
441                 row[_columns.property_toggles_visible] = true;
442         }
443
444         row[_columns.region] = region;
445
446         region_row_map.insert(pair<boost::shared_ptr<ARDOUR::Region>, Gtk::TreeModel::RowReference>(region, TreeRowReference(_model, TreePath (row))) );
447
448         populate_row(region, (*row));
449 }
450
451 void
452 EditorRegions::remove_unused_regions ()
453 {
454         vector<string> choices;
455         string prompt;
456
457         if (!_session) {
458                 return;
459         }
460
461         prompt  = _("Do you really want to remove unused regions?"
462                     "\n(This is destructive and cannot be undone)");
463
464         choices.push_back (_("No, do nothing."));
465         choices.push_back (_("Yes, remove."));
466
467         Gtkmm2ext::Choice prompter (_("Remove unused regions"), prompt, choices);
468
469         if (prompter.run () == 1) {
470                 _no_redisplay = true;
471                 _session->cleanup_regions ();
472                 _no_redisplay = false;
473                 redisplay ();
474         }
475 }
476
477 void
478 EditorRegions::region_changed (boost::shared_ptr<Region> r, const PropertyChange& what_changed)
479 {
480         PropertyChange our_interests;
481
482         our_interests.add (ARDOUR::Properties::name);
483         our_interests.add (ARDOUR::Properties::position);
484         our_interests.add (ARDOUR::Properties::length);
485         our_interests.add (ARDOUR::Properties::start);
486         our_interests.add (ARDOUR::Properties::locked);
487         our_interests.add (ARDOUR::Properties::position_lock_style);
488         our_interests.add (ARDOUR::Properties::muted);
489         our_interests.add (ARDOUR::Properties::opaque);
490         our_interests.add (ARDOUR::Properties::fade_in);
491         our_interests.add (ARDOUR::Properties::fade_out);
492         our_interests.add (ARDOUR::Properties::fade_in_active);
493         our_interests.add (ARDOUR::Properties::fade_out_active);
494
495         if (what_changed.contains (our_interests)) {
496
497                 if (last_row != 0) {
498
499                         TreeModel::iterator j = _model->get_iter (last_row.get_path());
500                         boost::shared_ptr<Region> c = (*j)[_columns.region];
501
502                         if (c == r) {
503                                 populate_row (r, (*j));
504
505                                 if (what_changed.contains (ARDOUR::Properties::hidden)) {
506                                         redisplay ();
507                                 }
508
509                                 return;
510                         }
511                 }
512
513                 RegionRowMap::iterator it;
514
515                 it = region_row_map.find (r);
516
517                 if (it != region_row_map.end()){
518
519                         TreeModel::iterator j = _model->get_iter ((*it).second.get_path());
520                         boost::shared_ptr<Region> c = (*j)[_columns.region];
521
522                         if (c == r) {
523                                 populate_row (r, (*j));
524
525                                 if (what_changed.contains (ARDOUR::Properties::hidden)) {
526                                         redisplay ();
527                                 }
528
529                                 return;
530                         }
531                 }
532         }
533
534         if (what_changed.contains (ARDOUR::Properties::hidden)) {
535                 redisplay ();
536         }
537 }
538
539 void
540 EditorRegions::selection_changed ()
541 {
542         if (ignore_region_list_selection_change) {
543                 return;
544         }
545
546         _editor->_region_selection_change_updates_region_list = false;
547
548         if (_display.get_selection()->count_selected_rows() > 0) {
549
550                 TreeIter iter;
551                 TreeView::Selection::ListHandle_Path rows = _display.get_selection()->get_selected_rows ();
552
553                 _editor->get_selection().clear_regions ();
554
555                 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
556
557                         if ((iter = _model->get_iter (*i))) {
558
559                                 boost::shared_ptr<Region> region = (*iter)[_columns.region];
560
561                                 // they could have clicked on a row that is just a placeholder, like "Hidden"
562                                 // although that is not allowed by our selection filter. check it anyway
563                                 // since we need a region ptr.
564
565                                 if (region) {
566
567                                         _change_connection.block (true);
568                                         _editor->set_selected_regionview_from_region_list (region, Selection::Add);
569                                         _change_connection.block (false);
570                                 }
571                         }
572
573                 }
574         } else {
575                 _editor->get_selection().clear_regions ();
576         }
577
578         _editor->_region_selection_change_updates_region_list = true;
579 }
580
581 void
582 EditorRegions::set_selected (RegionSelection& regions)
583 {
584         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
585
586                 boost::shared_ptr<Region> r ((*i)->region());
587
588                 RegionRowMap::iterator it;
589
590                 it = region_row_map.find (r);
591
592                 if (it != region_row_map.end()){
593                         TreeModel::iterator j = _model->get_iter ((*it).second.get_path());
594                         _display.get_selection()->select(*j);
595                 }
596         }
597 }
598
599 void
600 EditorRegions::redisplay ()
601 {
602         if (_no_redisplay || !_session) {
603                 return;
604         }
605
606         bool tree_expanded = false;
607
608         /* If the list was expanded prior to rebuilding, expand it again afterwards */
609         if (toggle_full_action()->get_active()) {
610                 tree_expanded = true;
611         }
612
613         _display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
614         _model->clear ();
615         _model->set_sort_column (-2, SORT_ASCENDING); //Disable sorting to gain performance
616
617
618         region_row_map.clear();
619         parent_regions_sources_map.clear();
620
621         /* now add everything we have, via a temporary list used to help with sorting */
622
623         const RegionFactory::RegionMap& regions (RegionFactory::regions());
624
625         for (RegionFactory::RegionMap::const_iterator i = regions.begin(); i != regions.end(); ++i) {
626
627                 if ( i->second->whole_file()) {
628                         /* add automatic regions first so that children can find their parents as we add them */
629                         add_region (i->second);
630                         continue;
631                 }
632
633                 tmp_region_list.push_front (i->second);
634         }
635
636         for (list<boost::shared_ptr<Region> >::iterator r = tmp_region_list.begin(); r != tmp_region_list.end(); ++r) {
637                 add_region (*r);
638         }
639
640         _model->set_sort_column (0, SORT_ASCENDING); // renabale sorting
641         _display.set_model (_model);
642
643         tmp_region_list.clear();
644
645         if (tree_expanded) {
646                 _display.expand_all();
647         }
648 }
649
650 void
651 EditorRegions::update_row (boost::shared_ptr<Region> region)
652 {
653         if (!region || !_session) {
654                 return;
655         }
656
657         RegionRowMap::iterator it;
658
659         it = region_row_map.find (region);
660
661         if (it != region_row_map.end()){
662
663                 TreeModel::iterator j = _model->get_iter ((*it).second.get_path());
664                 populate_row(region, (*j));
665         }
666 }
667
668 void
669 EditorRegions::update_all_rows ()
670 {
671         if (!_session) {
672                 return;
673         }
674
675         RegionRowMap::iterator i;
676
677         for (i = region_row_map.begin(); i != region_row_map.end(); ++i) {
678
679                 TreeModel::iterator j = _model->get_iter ((*i).second.get_path());
680
681                 boost::shared_ptr<Region> region = (*j)[_columns.region];
682
683                 if (!region->automatic()) {
684                         populate_row(region, (*j));
685                 }
686         }
687 }
688
689 void
690 EditorRegions::format_position (framepos_t pos, char* buf, size_t bufsize, bool onoff)
691 {
692         Timecode::BBT_Time bbt;
693         Timecode::Time timecode;
694
695         if (pos < 0) {
696                 error << string_compose (_("EditorRegions::format_position: negative timecode position: %1"), pos) << endmsg;
697                 snprintf (buf, bufsize, "invalid");
698                 return;
699         }
700
701         switch (ARDOUR_UI::instance()->secondary_clock->mode ()) {
702         case AudioClock::BBT:
703                 _session->tempo_map().bbt_time (pos, bbt);
704                 if (onoff) {
705                         snprintf (buf, bufsize, "%03d|%02d|%04d" , bbt.bars, bbt.beats, bbt.ticks);
706                 } else {
707                         snprintf (buf, bufsize, "(%03d|%02d|%04d)" , bbt.bars, bbt.beats, bbt.ticks);
708                 }
709                 break;
710
711         case AudioClock::MinSec:
712                 framepos_t left;
713                 int hrs;
714                 int mins;
715                 float secs;
716
717                 left = pos;
718                 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
719                 left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
720                 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
721                 left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
722                 secs = left / (float) _session->frame_rate();
723                 if (onoff) {
724                         snprintf (buf, bufsize, "%02d:%02d:%06.3f", hrs, mins, secs);
725                 } else {
726                         snprintf (buf, bufsize, "(%02d:%02d:%06.3f)", hrs, mins, secs);
727                 }
728                 break;
729
730         case AudioClock::Frames:
731                 if (onoff) {
732                         snprintf (buf, bufsize, "%" PRId64, pos);
733                 } else {
734                         snprintf (buf, bufsize, "(%" PRId64 ")", pos);
735                 }
736                 break;
737
738         case AudioClock::Timecode:
739         default:
740                 _session->timecode_time (pos, timecode);
741                 if (onoff) {
742                         snprintf (buf, bufsize, "%02d:%02d:%02d:%02d", timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
743                 } else {
744                         snprintf (buf, bufsize, "(%02d:%02d:%02d:%02d)", timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
745                 }
746                 break;
747         }
748 }
749
750 void
751 EditorRegions::populate_row (boost::shared_ptr<Region> region, TreeModel::Row const &row)
752 {
753         boost::shared_ptr<AudioRegion> audioregion = boost::dynamic_pointer_cast<AudioRegion>(region);
754         //uint32_t used = _session->playlists->region_use_count (region);
755         /* Presently a region is only used once so let's save on the sequential scan to determine use count */
756         uint32_t used = 1;
757
758         populate_row_position (region, row, used);
759         populate_row_end (region, row, used);
760         populate_row_sync (region, row, used);
761         populate_row_fade_in (region, row, used, audioregion);
762         populate_row_fade_out (region, row, used, audioregion);
763         populate_row_locked (region, row, used);
764         populate_row_glued (region, row, used);
765         populate_row_muted (region, row, used);
766         populate_row_opaque (region, row, used);
767         populate_row_length (region, row);
768         populate_row_source (region, row);
769         populate_row_name (region, row);
770         populate_row_used (region, row, used);
771 }
772
773 #if 0
774         if (audioRegion && fades_in_seconds) {
775
776                 framepos_t left;
777                 int mins;
778                 int millisecs;
779
780                 left = audioRegion->fade_in()->back()->when;
781                 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
782                 left -= (framepos_t) floor (mins * _session->frame_rate() * 60.0f);
783                 millisecs = (int) floor ((left * 1000.0f) / _session->frame_rate());
784
785                 if (audioRegion->fade_in()->back()->when >= _session->frame_rate()) {
786                         sprintf (fadein_str, "%01dM %01dmS", mins, millisecs);
787                 } else {
788                         sprintf (fadein_str, "%01dmS", millisecs);
789                 }
790
791                 left = audioRegion->fade_out()->back()->when;
792                 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
793                 left -= (framepos_t) floor (mins * _session->frame_rate() * 60.0f);
794                 millisecs = (int) floor ((left * 1000.0f) / _session->frame_rate());
795
796                 if (audioRegion->fade_out()->back()->when >= _session->frame_rate()) {
797                         sprintf (fadeout_str, "%01dM %01dmS", mins, millisecs);
798                 } else {
799                         sprintf (fadeout_str, "%01dmS", millisecs);
800                 }
801         }
802 #endif
803
804 void
805 EditorRegions::populate_row_used (boost::shared_ptr<Region>, TreeModel::Row const& row, uint32_t used)
806 {
807         char buf[8];
808         snprintf (buf, sizeof (buf), "%4d" , used);
809         row[_columns.used] = buf;
810 }
811
812 void
813 EditorRegions::populate_row_length (boost::shared_ptr<Region> region, TreeModel::Row const &row)
814 {
815         char buf[16];
816         format_position (region->length(), buf, sizeof (buf));
817         row[_columns.length] = buf;
818 }
819
820 void
821 EditorRegions::populate_row_end (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
822 {
823         if (region->whole_file()) {
824                 row[_columns.end] = "";
825         } else if (used > 1) {
826                 row[_columns.end] = _("Mult.");
827         } else if (region->last_frame() >= region->first_frame()) {
828                 char buf[16];
829                 format_position (region->last_frame(), buf, sizeof (buf));
830                 row[_columns.end] = buf;
831         } else {
832                 row[_columns.end] = "empty";
833         }
834 }
835
836 void
837 EditorRegions::populate_row_position (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
838 {
839         if (region->whole_file()) {
840                 row[_columns.position] = "";
841         } else if (used > 1) {
842                 row[_columns.position] = _("Mult.");
843         } else {
844                 char buf[16];
845                 format_position (region->position(), buf, sizeof (buf));
846                 row[_columns.position] = buf;
847         }
848 }
849
850 void
851 EditorRegions::populate_row_sync (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
852 {
853         if (region->whole_file()) {
854                 row[_columns.sync] = "";
855         } else if (used > 1) {
856                 row[_columns.sync] = _("Mult."); /* translators: a short phrase for "multiple" as in "many" */
857         } else {
858                 if (region->sync_position() == region->position()) {
859                         row[_columns.sync] = _("Start");
860                 } else if (region->sync_position() == (region->last_frame())) {
861                         row[_columns.sync] = _("End");
862                 } else {
863                         char buf[16];
864                         format_position (region->sync_position(), buf, sizeof (buf));
865                         row[_columns.sync] = buf;
866                 }
867         }
868 }
869
870 void
871 EditorRegions::populate_row_fade_in (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used, boost::shared_ptr<AudioRegion> audioregion)
872 {
873         if (!audioregion || region->whole_file()) {
874                 row[_columns.fadein] = "";
875         } else {
876                 if (used > 1) {
877                         row[_columns.fadein] = _("Multiple");
878                 } else {
879                         char buf[32];
880                         format_position (audioregion->fade_in()->back()->when, buf, sizeof (buf), audioregion->fade_in_active());
881                         row[_columns.fadein] = buf;
882                 }
883         }
884 }
885
886 void
887 EditorRegions::populate_row_fade_out (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used, boost::shared_ptr<AudioRegion> audioregion)
888 {
889         if (!audioregion || region->whole_file()) {
890                 row[_columns.fadeout] = "";
891         } else {
892                 if (used > 1) {
893                         row[_columns.fadeout] = _("Multiple");
894                 } else {
895                         char buf[32];
896                         format_position (audioregion->fade_out()->back()->when, buf, sizeof (buf), audioregion->fade_out_active());
897                         row[_columns.fadeout] = buf;
898                 }
899         }
900 }
901
902 void
903 EditorRegions::populate_row_locked (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
904 {
905         if (region->whole_file()) {
906                 row[_columns.locked] = false;
907         } else if (used > 1) {
908                 row[_columns.locked] = false;
909         } else {
910                 row[_columns.locked] = region->locked();
911         }
912 }
913
914 void
915 EditorRegions::populate_row_glued (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
916 {
917         if (region->whole_file() || used > 1) {
918                 row[_columns.glued] = false;
919         } else {
920                 if (region->position_lock_style() == MusicTime) {
921                         row[_columns.glued] = true;
922                 } else {
923                         row[_columns.glued] = false;
924                 }
925         }
926 }
927
928 void
929 EditorRegions::populate_row_muted (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
930 {
931         if (region->whole_file() || used > 1) {
932                 row[_columns.muted] = false;
933         } else {
934                 row[_columns.muted] = region->muted();
935         }
936 }
937
938 void
939 EditorRegions::populate_row_opaque (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
940 {
941         if (region->whole_file() || used > 1) {
942                 row[_columns.opaque] = false;
943         } else {
944                 row[_columns.opaque] = region->opaque();
945         }
946 }
947
948 void
949 EditorRegions::populate_row_name (boost::shared_ptr<Region> region, TreeModel::Row const &row)
950 {
951         if (region->n_channels() > 1) {
952                 row[_columns.name] = string_compose("%1  [%2]", Glib::Markup::escape_text (region->name()), region->n_channels());
953         } else {
954                 row[_columns.name] = Glib::Markup::escape_text (region->name());
955         }
956 }
957
958 void
959 EditorRegions::populate_row_source (boost::shared_ptr<Region> region, TreeModel::Row const &row)
960 {
961         if (boost::dynamic_pointer_cast<SilentFileSource>(region->source())) {
962                 row[_columns.path] = _("MISSING ") + Glib::Markup::escape_text (region->source()->name());
963         } else {
964                 row[_columns.path] = Glib::Markup::escape_text (region->source()->name());
965         }
966 }
967
968 void
969 EditorRegions::toggle_show_auto_regions ()
970 {
971         _show_automatic_regions = toggle_show_auto_regions_action()->get_active();
972         redisplay ();
973 }
974
975 void
976 EditorRegions::toggle_full ()
977 {
978         set_full (toggle_full_action()->get_active ());
979 }
980
981 void
982 EditorRegions::set_full (bool f)
983 {
984         if (f) {
985                 _display.expand_all ();
986                 expanded = true;
987         } else {
988                 _display.collapse_all ();
989                 expanded = false;
990         }
991 }
992
993 void
994 EditorRegions::show_context_menu (int button, int time)
995 {
996         if (_menu == 0) {
997                 _menu = dynamic_cast<Menu*> (ActionManager::get_widget ("/RegionListMenu"));
998         }
999
1000         if (_display.get_selection()->count_selected_rows() > 0) {
1001                 ActionManager::set_sensitive (ActionManager::region_list_selection_sensitive_actions, true);
1002         } else {
1003                 ActionManager::set_sensitive (ActionManager::region_list_selection_sensitive_actions, false);
1004         }
1005
1006         /* Enable the "Show" option if any selected regions are hidden, and vice versa for "Hide" */
1007
1008         bool have_shown = false;
1009         bool have_hidden = false;
1010
1011         TreeView::Selection::ListHandle_Path rows = _display.get_selection()->get_selected_rows ();
1012         for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
1013                 TreeIter t = _model->get_iter (*i);
1014                 boost::shared_ptr<Region> r = (*t)[_columns.region];
1015                 if (r) {
1016                         if (r->hidden ()) {
1017                                 have_hidden = true;
1018                         } else {
1019                                 have_shown = true;
1020                         }
1021                 }
1022         }
1023
1024         hide_action()->set_sensitive (have_shown);
1025         show_action()->set_sensitive (have_hidden);
1026
1027         _menu->popup (button, time);
1028 }
1029
1030 bool
1031 EditorRegions::key_press (GdkEventKey* ev)
1032 {
1033         TreeViewColumn *col;
1034
1035         switch (ev->keyval) {
1036         case GDK_Tab:
1037         case GDK_ISO_Left_Tab:
1038
1039                 if (name_editable) {
1040                         name_editable->editing_done ();
1041                         name_editable = 0;
1042                 }
1043
1044                 col = _display.get_column (0); // select&focus on name column
1045
1046                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
1047                         treeview_select_previous (_display, _model, col);
1048                 } else {
1049                         treeview_select_next (_display, _model, col);
1050                 }
1051
1052                 return true;
1053                 break;
1054
1055         default:
1056                 break;
1057         }
1058
1059         return false;
1060 }
1061
1062 bool
1063 EditorRegions::button_press (GdkEventButton *ev)
1064 {
1065         boost::shared_ptr<Region> region;
1066         TreeIter iter;
1067         TreeModel::Path path;
1068         TreeViewColumn* column;
1069         int cellx;
1070         int celly;
1071
1072         if (_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
1073                 if ((iter = _model->get_iter (path))) {
1074                         region = (*iter)[_columns.region];
1075                 }
1076         }
1077
1078         if (Keyboard::is_context_menu_event (ev)) {
1079                 show_context_menu (ev->button, ev->time);
1080                 return false;
1081         }
1082
1083         if (region != 0 && Keyboard::is_button2_event (ev)) {
1084                 // start/stop audition
1085                 if (!Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1086                         _editor->consider_auditioning (region);
1087                 }
1088                 return true;
1089         }
1090
1091         return false;
1092 }
1093
1094 int
1095 EditorRegions::sorter (TreeModel::iterator a, TreeModel::iterator b)
1096 {
1097         int cmp = 0;
1098
1099         boost::shared_ptr<Region> r1 = (*a)[_columns.region];
1100         boost::shared_ptr<Region> r2 = (*b)[_columns.region];
1101
1102         /* handle rows without regions, like "Hidden" */
1103
1104         if (r1 == 0) {
1105                 return -1;
1106         }
1107
1108         if (r2 == 0) {
1109                 return 1;
1110         }
1111
1112         boost::shared_ptr<AudioRegion> region1 = boost::dynamic_pointer_cast<AudioRegion> (r1);
1113         boost::shared_ptr<AudioRegion> region2 = boost::dynamic_pointer_cast<AudioRegion> (r2);
1114
1115         if (region1 == 0 || region2 == 0) {
1116                 std::string s1;
1117                 std::string s2;
1118                 switch (_sort_type) {
1119                 case ByName:
1120                         s1 = (*a)[_columns.name];
1121                         s2 = (*b)[_columns.name];
1122                         return (s1.compare (s2));
1123                 default:
1124                         return 0;
1125                 }
1126         }
1127
1128         switch (_sort_type) {
1129         case ByName:
1130                 cmp = region1->name().compare(region2->name());
1131                 break;
1132
1133         case ByLength:
1134                 cmp = region1->length() - region2->length();
1135                 break;
1136
1137         case ByPosition:
1138                 cmp = region1->position() - region2->position();
1139                 break;
1140
1141         case ByTimestamp:
1142                 cmp = region1->source()->timestamp() - region2->source()->timestamp();
1143                 break;
1144
1145         case ByStartInFile:
1146                 cmp = region1->start() - region2->start();
1147                 break;
1148
1149         case ByEndInFile:
1150                 // cerr << "Compare " << (region1->start() + region1->length()) << " and " << (region2->start() + region2->length()) << endl;
1151                 cmp = (region1->start() + region1->length()) - (region2->start() + region2->length());
1152                 break;
1153
1154         case BySourceFileName:
1155                 cmp = region1->source()->name().compare(region2->source()->name());
1156                 break;
1157
1158         case BySourceFileLength:
1159                 cmp = region1->source_length(0) - region2->source_length(0);
1160                 break;
1161
1162         case BySourceFileCreationDate:
1163                 cmp = region1->source()->timestamp() - region2->source()->timestamp();
1164                 break;
1165
1166         case BySourceFileFS:
1167                 if (region1->source()->name() == region2->source()->name()) {
1168                         cmp = region1->name().compare(region2->name());
1169                 } else {
1170                         cmp = region1->source()->name().compare(region2->source()->name());
1171                 }
1172                 break;
1173         }
1174
1175         // cerr << "Comparison on " << enum_2_string (_sort_type) << " gives " << cmp << endl;
1176
1177         if (cmp < 0) {
1178                 return -1;
1179         } else if (cmp > 0) {
1180                 return 1;
1181         } else {
1182                 return 0;
1183         }
1184 }
1185
1186 void
1187 EditorRegions::reset_sort_type (RegionListSortType type, bool force)
1188 {
1189         if (type != _sort_type || force) {
1190                 _sort_type = type;
1191                 _model->set_sort_func (0, (sigc::mem_fun (*this, &EditorRegions::sorter)));
1192         }
1193 }
1194
1195 void
1196 EditorRegions::reset_sort_direction (bool up)
1197 {
1198         _model->set_sort_column (0, up ? SORT_ASCENDING : SORT_DESCENDING);
1199 }
1200
1201 void
1202 EditorRegions::selection_mapover (sigc::slot<void,boost::shared_ptr<Region> > sl)
1203 {
1204         Glib::RefPtr<TreeSelection> selection = _display.get_selection();
1205         TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
1206         TreeView::Selection::ListHandle_Path::iterator i = rows.begin();
1207
1208         if (selection->count_selected_rows() == 0 || _session == 0) {
1209                 return;
1210         }
1211
1212         for (; i != rows.end(); ++i) {
1213                 TreeIter iter;
1214
1215                 if ((iter = _model->get_iter (*i))) {
1216
1217                         /* some rows don't have a region associated with them, but can still be
1218                            selected (XXX maybe prevent them from being selected)
1219                         */
1220
1221                         boost::shared_ptr<Region> r = (*iter)[_columns.region];
1222
1223                         if (r) {
1224                                 sl (r);
1225                         }
1226                 }
1227         }
1228 }
1229
1230
1231 void
1232 EditorRegions::drag_data_received (const RefPtr<Gdk::DragContext>& context,
1233                                    int x, int y,
1234                                    const SelectionData& data,
1235                                    guint info, guint time)
1236 {
1237         vector<string> paths;
1238
1239         if (data.get_target() == "GTK_TREE_MODEL_ROW") {
1240                 /* something is being dragged over the region list */
1241                 _editor->_drags->abort ();
1242                 _display.on_drag_data_received (context, x, y, data, info, time);
1243                 return;
1244         }
1245
1246         if (_editor->convert_drop_to_paths (paths, context, x, y, data, info, time) == 0) {
1247                 framepos_t pos = 0;
1248                 bool copy = ((context->get_actions() & (Gdk::ACTION_COPY | Gdk::ACTION_LINK | Gdk::ACTION_MOVE)) == Gdk::ACTION_COPY);
1249
1250                 if (Profile->get_sae() || UIConfiguration::instance().get_only_copy_imported_files() || copy) {
1251                         _editor->do_import (paths, Editing::ImportDistinctFiles, Editing::ImportAsRegion, SrcBest, pos);
1252                 } else {
1253                         _editor->do_embed (paths, Editing::ImportDistinctFiles, ImportAsRegion, pos);
1254                 }
1255                 context->drag_finish (true, false, time);
1256         }
1257 }
1258
1259 bool
1260 EditorRegions::selection_filter (const RefPtr<TreeModel>& model, const TreeModel::Path& path, bool already_selected)
1261 {
1262         /* not possible to select rows that do not represent regions, like "Hidden" */
1263
1264         if (already_selected) {
1265                 /* deselecting anything is OK with us */
1266                 return true;
1267         }
1268
1269         TreeModel::iterator iter = model->get_iter (path);
1270
1271         if (iter) {
1272                 boost::shared_ptr<Region> r =(*iter)[_columns.region];
1273                 if (!r) {
1274                         return false;
1275                 }
1276         }
1277
1278         return true;
1279 }
1280
1281 void
1282 EditorRegions::name_editing_started (CellEditable* ce, const Glib::ustring&)
1283 {
1284         name_editable = ce;
1285
1286         /* give it a special name */
1287
1288         Gtk::Entry *e = dynamic_cast<Gtk::Entry*> (ce);
1289
1290         if (e) {
1291                 e->set_name (X_("RegionNameEditorEntry"));
1292         }
1293 }
1294
1295 void
1296 EditorRegions::name_edit (const std::string& path, const std::string& new_text)
1297 {
1298         name_editable = 0;
1299
1300         boost::shared_ptr<Region> region;
1301         TreeIter iter;
1302
1303         if ((iter = _model->get_iter (path))) {
1304                 region = (*iter)[_columns.region];
1305                 (*iter)[_columns.name] = new_text;
1306         }
1307
1308         /* now mapover everything */
1309
1310         if (region) {
1311                 vector<RegionView*> equivalents;
1312                 _editor->get_regions_corresponding_to (region, equivalents, false);
1313
1314                 for (vector<RegionView*>::iterator i = equivalents.begin(); i != equivalents.end(); ++i) {
1315                         if (new_text != (*i)->region()->name()) {
1316                                 (*i)->region()->set_name (new_text);
1317                         }
1318                 }
1319         }
1320
1321 }
1322
1323 /** @return Region that has been dragged out of the list, or 0 */
1324 boost::shared_ptr<Region>
1325 EditorRegions::get_dragged_region ()
1326 {
1327         list<boost::shared_ptr<Region> > regions;
1328         TreeView* source;
1329         _display.get_object_drag_data (regions, &source);
1330
1331         if (regions.empty()) {
1332                 return boost::shared_ptr<Region> ();
1333         }
1334
1335         assert (regions.size() == 1);
1336         return regions.front ();
1337 }
1338
1339 void
1340 EditorRegions::clear ()
1341 {
1342         _display.set_model (Glib::RefPtr<Gtk::TreeStore> (0));
1343         _model->clear ();
1344         _display.set_model (_model);
1345
1346         /* Clean up the maps */
1347         region_row_map.clear();
1348         parent_regions_sources_map.clear();
1349 }
1350
1351 boost::shared_ptr<Region>
1352 EditorRegions::get_single_selection ()
1353 {
1354         Glib::RefPtr<TreeSelection> selected = _display.get_selection();
1355
1356         if (selected->count_selected_rows() != 1) {
1357                 return boost::shared_ptr<Region> ();
1358         }
1359
1360         TreeView::Selection::ListHandle_Path rows = selected->get_selected_rows ();
1361
1362         /* only one row selected, so rows.begin() is it */
1363
1364         TreeIter iter = _model->get_iter (*rows.begin());
1365
1366         if (!iter) {
1367                 return boost::shared_ptr<Region> ();
1368         }
1369
1370         return (*iter)[_columns.region];
1371 }
1372
1373 void
1374 EditorRegions::freeze_tree_model (){
1375
1376         _display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
1377         _model->set_sort_column (-2, SORT_ASCENDING); //Disable sorting to gain performance
1378
1379 }
1380
1381 void
1382 EditorRegions::thaw_tree_model (){
1383
1384         _model->set_sort_column (0, SORT_ASCENDING); // renabale sorting
1385         _display.set_model (_model);
1386
1387         if (toggle_full_action()->get_active()) {
1388                 _display.expand_all();
1389         }
1390 }
1391
1392 void
1393 EditorRegions::locked_changed (std::string const & path)
1394 {
1395         TreeIter i = _model->get_iter (path);
1396         if (i) {
1397                 boost::shared_ptr<ARDOUR::Region> region = (*i)[_columns.region];
1398                 if (region) {
1399                         region->set_locked (!(*i)[_columns.locked]);
1400                 }
1401         }
1402 }
1403
1404 void
1405 EditorRegions::glued_changed (std::string const & path)
1406 {
1407         TreeIter i = _model->get_iter (path);
1408         if (i) {
1409                 boost::shared_ptr<ARDOUR::Region> region = (*i)[_columns.region];
1410                 if (region) {
1411                         /* `glued' means MusicTime, and we're toggling here */
1412                         region->set_position_lock_style ((*i)[_columns.glued] ? AudioTime : MusicTime);
1413                 }
1414         }
1415
1416 }
1417
1418 void
1419 EditorRegions::muted_changed (std::string const & path)
1420 {
1421         TreeIter i = _model->get_iter (path);
1422         if (i) {
1423                 boost::shared_ptr<ARDOUR::Region> region = (*i)[_columns.region];
1424                 if (region) {
1425                         region->set_muted (!(*i)[_columns.muted]);
1426                 }
1427         }
1428
1429 }
1430
1431 void
1432 EditorRegions::opaque_changed (std::string const & path)
1433 {
1434         TreeIter i = _model->get_iter (path);
1435         if (i) {
1436                 boost::shared_ptr<ARDOUR::Region> region = (*i)[_columns.region];
1437                 if (region) {
1438                         region->set_opaque (!(*i)[_columns.opaque]);
1439                 }
1440         }
1441
1442 }
1443
1444 XMLNode &
1445 EditorRegions::get_state () const
1446 {
1447         XMLNode* node = new XMLNode (X_("RegionList"));
1448
1449         node->add_property (X_("sort-type"), enum_2_string (_sort_type));
1450
1451         RefPtr<Action> act = ActionManager::get_action (X_("RegionList"), X_("SortAscending"));
1452         bool const ascending = RefPtr<RadioAction>::cast_dynamic(act)->get_active ();
1453         node->add_property (X_("sort-ascending"), ascending ? "yes" : "no");
1454         node->add_property (X_("show-all"), toggle_full_action()->get_active() ? "yes" : "no");
1455         node->add_property (X_("show-automatic-regions"), _show_automatic_regions ? "yes" : "no");
1456
1457         return *node;
1458 }
1459
1460 void
1461 EditorRegions::set_state (const XMLNode & node)
1462 {
1463         bool changed = false;
1464
1465         if (node.name() != X_("RegionList")) {
1466                 return;
1467         }
1468
1469         XMLProperty const * p = node.property (X_("sort-type"));
1470
1471         if (p) {
1472                 Editing::RegionListSortType const t = static_cast<Editing::RegionListSortType> (string_2_enum (p->value(), _sort_type));
1473
1474                 if (_sort_type != t) {
1475                         changed = true;
1476                 }
1477
1478                 reset_sort_type (t, true);
1479                 RefPtr<RadioAction> ract = sort_type_action (t);
1480                 ract->set_active ();
1481         }
1482
1483         p = node.property (X_("sort-ascending"));
1484
1485         if (p) {
1486                 bool const yn = string_is_affirmative (p->value ());
1487                 SortType old_sort_type;
1488                 int old_sort_column;
1489
1490                 _model->get_sort_column_id (old_sort_column, old_sort_type);
1491
1492                 if (old_sort_type != (yn ? SORT_ASCENDING : SORT_DESCENDING)) {
1493                         changed = true;
1494                 }
1495
1496                 reset_sort_direction (yn);
1497                 RefPtr<Action> act;
1498
1499                 if (yn) {
1500                         act = ActionManager::get_action (X_("RegionList"), X_("SortAscending"));
1501                 } else {
1502                         act = ActionManager::get_action (X_("RegionList"), X_("SortDescending"));
1503                 }
1504
1505                 RefPtr<RadioAction>::cast_dynamic(act)->set_active ();
1506         }
1507
1508         p = node.property (X_("show-all"));
1509         if (p) {
1510                 bool const yn = string_is_affirmative (p->value ());
1511
1512                 if (expanded != yn) {
1513                         changed = true;
1514                 }
1515
1516                 set_full (yn);
1517                 toggle_full_action()->set_active (yn);
1518         }
1519
1520         p = node.property (X_("show-automatic-regions"));
1521         if (p) {
1522                 bool const yn = string_is_affirmative (p->value ());
1523
1524                 if (yn != _show_automatic_regions) {
1525                         _show_automatic_regions = yn;
1526                         toggle_show_auto_regions_action()->set_active (yn);
1527                         changed = true;
1528                 }
1529         }
1530
1531         if (changed) {
1532                 redisplay ();
1533         }
1534 }
1535
1536 RefPtr<RadioAction>
1537 EditorRegions::sort_type_action (Editing::RegionListSortType t) const
1538 {
1539         const char* action = 0;
1540
1541         switch (t) {
1542         case Editing::ByName:
1543                 action = X_("SortByRegionName");
1544                 break;
1545         case Editing::ByLength:
1546                 action = X_("SortByRegionLength");
1547                 break;
1548         case Editing::ByPosition:
1549                 action = X_("SortByRegionPosition");
1550                 break;
1551         case Editing::ByTimestamp:
1552                 action = X_("SortByRegionTimestamp");
1553                 break;
1554         case Editing::ByStartInFile:
1555                 action = X_("SortByRegionStartinFile");
1556                 break;
1557         case Editing::ByEndInFile:
1558                 action = X_("SortByRegionEndinFile");
1559                 break;
1560         case Editing::BySourceFileName:
1561                 action = X_("SortBySourceFileName");
1562                 break;
1563         case Editing::BySourceFileLength:
1564                 action = X_("SortBySourceFileLength");
1565                 break;
1566         case Editing::BySourceFileCreationDate:
1567                 action = X_("SortBySourceFileCreationDate");
1568                 break;
1569         case Editing::BySourceFileFS:
1570                 action = X_("SortBySourceFilesystem");
1571                 break;
1572         default:
1573                 fatal << string_compose (_("programming error: %1: %2"), "EditorRegions: impossible sort type", (int) t) << endmsg;
1574                 abort(); /*NOTREACHED*/
1575         }
1576
1577         RefPtr<Action> act = ActionManager::get_action (X_("RegionList"), action);
1578         assert (act);
1579
1580         return RefPtr<RadioAction>::cast_dynamic (act);
1581 }
1582
1583 RefPtr<Action>
1584 EditorRegions::hide_action () const
1585 {
1586         return ActionManager::get_action (X_("RegionList"), X_("rlHide"));
1587
1588 }
1589
1590 RefPtr<Action>
1591 EditorRegions::show_action () const
1592 {
1593         return ActionManager::get_action (X_("RegionList"), X_("rlShow"));
1594 }
1595
1596 RefPtr<Action>
1597 EditorRegions::remove_unused_regions_action () const
1598 {
1599         return ActionManager::get_action (X_("RegionList"), X_("removeUnusedRegions"));
1600 }
1601
1602 RefPtr<ToggleAction>
1603 EditorRegions::toggle_full_action () const
1604 {
1605         Glib::RefPtr<Action> act = ActionManager::get_action (X_("RegionList"), X_("rlShowAll"));
1606         assert (act);
1607         return Glib::RefPtr<ToggleAction>::cast_dynamic (act);
1608 }
1609
1610 RefPtr<ToggleAction>
1611 EditorRegions::toggle_show_auto_regions_action () const
1612 {
1613         Glib::RefPtr<Action> act = ActionManager::get_action (X_("RegionList"), X_("rlShowAuto"));
1614         assert (act);
1615         return Glib::RefPtr<ToggleAction>::cast_dynamic (act);
1616 }