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