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