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