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