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