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