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