fix column expansion behaviour of editor region list (#4489)
[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         static const int column_width = 22;
81
82         _display.set_size_request (100, -1);
83         _display.set_name ("RegionListDisplay");
84         _display.set_rules_hint (true);
85
86         /* Try to prevent single mouse presses from initiating edits.
87            This relies on a hack in gtktreeview.c:gtk_treeview_button_press()
88         */
89         _display.set_data ("mouse-edits-require-mod1", (gpointer) 0x1);
90
91         _model = TreeStore::create (_columns);
92         _model->set_sort_func (0, sigc::mem_fun (*this, &EditorRegions::sorter));
93         _model->set_sort_column (0, SORT_ASCENDING);
94
95         _display.set_model (_model);
96
97         _display.append_column ("", _columns.name);
98         _display.append_column ("", _columns.position);
99         _display.append_column ("", _columns.end);
100         _display.append_column ("", _columns.length);
101         _display.append_column ("", _columns.sync);
102         _display.append_column ("", _columns.fadein);
103         _display.append_column ("", _columns.fadeout);
104         _display.append_column ("", _columns.locked);
105         _display.append_column ("", _columns.glued);
106         _display.append_column ("", _columns.muted);
107         _display.append_column ("", _columns.opaque);
108
109         TreeViewColumn* col;
110         Gtk::Label* l;
111
112         ColumnInfo ci[] = {
113                 { 0, _("Region"), _("Region name, with number of channels in []'s") },
114                 { 1, _("Position"),  _("Length of the region") },
115                 { 2, _("End"),  _("Length of the region") },
116                 { 3, _("End"),  _("Length of the region") },
117                 { 4, _("Sync"),  _("Position of region sync point, relative to start of the region") },
118                 { 5, _("Sync"),  _("Region fade-in enabled?") },
119                 { 6, _("Fade In"),  _("Region fade-out enabled?") },
120                 { 7, _("L"),  _("Region position locked?") },
121                 { 8, _("G"),  _("Region position glued to Bars|Beats time?") },
122                 { 9, _("M"),  _("Region muted?") },
123                 { 10, _("O"),  _("Region opaque (blocks regions below it from being heard)?") },
124                 { -1, 0, 0 }
125         };
126         
127         for (int i = 0; ci[i].index >= 0; ++i) {
128                 col = _display.get_column (ci[i].index);
129                 l = manage (new Label (ci[i].label));
130                 ARDOUR_UI::instance()->set_tip (*l, ci[i].tooltip);
131                 col->set_widget (*l);
132                 l->show ();
133
134                 if (ci[i].index > 6) {
135                         col->set_fixed_width(column_width);
136                         col->set_expand (false);
137                         col->set_sizing (TREE_VIEW_COLUMN_FIXED);
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, ui_bind (&EditorRegions::region_changed, this, _1, _2), gui_context());
221         ARDOUR::RegionFactory::CheckNewRegion.connect (check_new_region_connection, MISSING_INVALIDATOR, ui_bind (&EditorRegions::add_region, this, _1), gui_context());
222
223         e->EditorFreeze.connect (editor_freeze_connection, MISSING_INVALIDATOR, ui_bind (&EditorRegions::freeze_tree_model, this), gui_context());
224         e->EditorThaw.connect (editor_thaw_connection, MISSING_INVALIDATOR, ui_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());
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                 TreeModel::Row child;
312
313                 if (!iter) {
314                         parent = *(_model->append());
315                         parent[_columns.name] = _("Hidden");
316                         boost::shared_ptr<Region> proxy = parent[_columns.region];
317                         proxy.reset ();
318                 } else {
319                         string s = (*iter)[_columns.name];
320                         if (s != _("Hidden")) {
321                                 parent = *(_model->insert(iter));
322                                 parent[_columns.name] = _("Hidden");
323                                 boost::shared_ptr<Region> proxy = parent[_columns.region];
324                                 proxy.reset ();
325                         } else {
326                                 parent = *iter;
327                         }
328                 }
329
330                 row = *(_model->append (parent.children()));
331
332         } else if (region->whole_file()) {
333
334                 TreeModel::iterator i;
335                 TreeModel::Children rows = _model->children();
336
337                 for (i = rows.begin(); i != rows.end(); ++i) {
338                         boost::shared_ptr<Region> rr = (*i)[_columns.region];
339
340                         if (rr && region->region_list_equivalent (rr)) {
341                                 return;
342                         }
343                 }
344
345                 row = *(_model->append());
346
347                 if (missing_source) {
348                         c.set_rgb(65535,0,0);     // FIXME: error color from style
349
350                 } else if (region->automatic()){
351                         c.set_rgb(0,65535,0);     // FIXME: error color from style
352
353                 } else {
354                         set_color(c, rgba_from_style ("RegionListWholeFile", 0xff, 0, 0, 0, "fg", Gtk::STATE_NORMAL, false ));
355
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) ") + 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] = fs->path();
400                         } else {
401                                 row[_columns.path] = 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
494         if (what_changed.contains (our_interests)) {
495
496                 if (last_row != 0) {
497
498                         TreeModel::iterator j = _model->get_iter (last_row.get_path());
499                         boost::shared_ptr<Region> c = (*j)[_columns.region];
500
501                         if (c == r) {
502                                 populate_row (r, (*j));
503
504                                 if (what_changed.contains (ARDOUR::Properties::hidden)) {
505                                         redisplay ();
506                                 }
507
508                                 return;
509                         }
510                 }
511
512                 RegionRowMap::iterator it;
513
514                 it = region_row_map.find (r);
515
516                 if (it != region_row_map.end()){
517
518                         TreeModel::iterator j = _model->get_iter ((*it).second.get_path());
519                         boost::shared_ptr<Region> c = (*j)[_columns.region];
520
521                         if (c == r) {
522                                 populate_row (r, (*j));
523
524                                 if (what_changed.contains (ARDOUR::Properties::hidden)) {
525                                         redisplay ();
526                                 }
527
528                                 return;
529                         }
530                 }
531         }
532
533         if (what_changed.contains (ARDOUR::Properties::hidden)) {
534                 redisplay ();
535         }
536 }
537
538 void
539 EditorRegions::selection_changed ()
540 {
541         if (ignore_region_list_selection_change) {
542                 return;
543         }
544
545         _editor->_region_selection_change_updates_region_list = false;
546
547         if (_display.get_selection()->count_selected_rows() > 0) {
548
549                 TreeIter iter;
550                 TreeView::Selection::ListHandle_Path rows = _display.get_selection()->get_selected_rows ();
551
552                 _editor->get_selection().clear_regions ();
553
554                 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
555
556                         if (iter = _model->get_iter (*i)) {
557                                 boost::shared_ptr<Region> region = (*iter)[_columns.region];
558
559                                 // they could have clicked on a row that is just a placeholder, like "Hidden"
560                                 // although that is not allowed by our selection filter. check it anyway
561                                 // since we need a region ptr.
562
563                                 if (region) {
564
565                                         if (region->automatic()) {
566
567                                                 _display.get_selection()->unselect(*i);
568
569                                         } else {
570                                                 _change_connection.block (true);
571                                                 _editor->set_selected_regionview_from_region_list (region, Selection::Add);
572
573                                                 _change_connection.block (false);
574                                         }
575                                 }
576                         }
577                 }
578         } else {
579                 _editor->get_selection().clear_regions ();
580         }
581
582         _editor->_region_selection_change_updates_region_list = true;
583 }
584
585 void
586 EditorRegions::set_selected (RegionSelection& regions)
587 {
588         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
589
590                 boost::shared_ptr<Region> r ((*i)->region());
591
592                 RegionRowMap::iterator it;
593
594                 it = region_row_map.find (r);
595
596                 if (it != region_row_map.end()){
597                         TreeModel::iterator j = _model->get_iter ((*it).second.get_path());
598                         _display.get_selection()->select(*j);
599                 }
600         }
601 }
602
603 void
604 EditorRegions::redisplay ()
605 {
606         if (_no_redisplay || !_session) {
607                 return;
608         }
609
610         bool tree_expanded = false;
611
612         /* If the list was expanded prior to rebuilding, expand it again afterwards */
613         if (toggle_full_action()->get_active()) {
614                 tree_expanded = true;
615         }
616
617         _display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
618         _model->clear ();
619         _model->set_sort_column (-2, SORT_ASCENDING); //Disable sorting to gain performance
620
621
622         region_row_map.clear();
623         parent_regions_sources_map.clear();
624
625         /* now add everything we have, via a temporary list used to help with sorting */
626
627         const RegionFactory::RegionMap& regions (RegionFactory::regions());
628
629         for (RegionFactory::RegionMap::const_iterator i = regions.begin(); i != regions.end(); ++i) {
630
631                 if ( i->second->whole_file()) {
632                         /* add automatic regions first so that children can find their parents as we add them */
633                         add_region (i->second);
634                         continue;
635                 }
636
637                 tmp_region_list.push_front (i->second);
638         }
639
640         for (list<boost::shared_ptr<Region> >::iterator r = tmp_region_list.begin(); r != tmp_region_list.end(); ++r) {
641                 add_region (*r);
642         }
643
644         _model->set_sort_column (0, SORT_ASCENDING); // renabale sorting
645         _display.set_model (_model);
646
647         tmp_region_list.clear();
648
649         if (tree_expanded) {
650                 _display.expand_all();
651         }
652 }
653
654 void
655 EditorRegions::update_row (boost::shared_ptr<Region> region)
656 {
657         if (!region || !_session) {
658                 return;
659         }
660
661         RegionRowMap::iterator it;
662
663         it = region_row_map.find (region);
664
665         if (it != region_row_map.end()){
666
667                 TreeModel::iterator j = _model->get_iter ((*it).second.get_path());
668                 populate_row(region, (*j));
669         }
670 }
671
672 void
673 EditorRegions::update_all_rows ()
674 {
675         if (!_session) {
676                 return;
677         }
678
679         RegionRowMap::iterator i;
680
681         for (i = region_row_map.begin(); i != region_row_map.end(); ++i) {
682
683                 TreeModel::iterator j = _model->get_iter ((*i).second.get_path());
684
685                 boost::shared_ptr<Region> region = (*j)[_columns.region];
686
687                 if (!region->automatic()) {
688                         populate_row(region, (*j));
689                 }
690         }
691 }
692
693 void
694 EditorRegions::format_position (framepos_t pos, char* buf, size_t bufsize)
695 {
696         Timecode::BBT_Time bbt;
697         Timecode::Time timecode;
698
699         switch (ARDOUR_UI::instance()->secondary_clock->mode ()) {
700         case AudioClock::BBT:
701                 _session->tempo_map().bbt_time (pos, bbt);
702                 snprintf (buf, bufsize, "%03d|%02d|%04d" , bbt.bars, bbt.beats, bbt.ticks);
703                 break;
704
705         case AudioClock::MinSec:
706                 framepos_t left;
707                 int hrs;
708                 int mins;
709                 float secs;
710
711                 left = pos;
712                 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
713                 left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
714                 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
715                 left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
716                 secs = left / (float) _session->frame_rate();
717                 snprintf (buf, bufsize, "%02d:%02d:%06.3f", hrs, mins, secs);
718                 break;
719
720         case AudioClock::Frames:
721                 snprintf (buf, bufsize, "%" PRId64, pos);
722                 break;
723
724         case AudioClock::Timecode:
725         default:
726                 _session->timecode_time (pos, timecode);
727                 snprintf (buf, bufsize, "%02d:%02d:%02d:%02d", timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
728                 break;
729         }
730 }
731
732 void
733 EditorRegions::populate_row (boost::shared_ptr<Region> region, TreeModel::Row const &row)
734 {
735         boost::shared_ptr<AudioRegion> audioregion = boost::dynamic_pointer_cast<AudioRegion>(region);
736         //uint32_t used = _session->playlists->region_use_count (region);
737         /* Presently a region is only used once so let's save on the sequential scan to determine use count */
738         uint32_t used = 1;
739
740         populate_row_position (region, row, used);
741         populate_row_end (region, row, used);
742         populate_row_sync (region, row, used);
743         populate_row_fade_in (region, row, used, audioregion);
744         populate_row_fade_out (region, row, used, audioregion);
745         populate_row_locked (region, row, used);
746         populate_row_glued (region, row, used);
747         populate_row_muted (region, row, used);
748         populate_row_opaque (region, row, used);
749         populate_row_length (region, row);
750         populate_row_source (region, row);
751         populate_row_name (region, row);
752         populate_row_used (region, row, used);
753 }
754
755 #if 0
756         if (audioRegion && fades_in_seconds) {
757
758                 framepos_t left;
759                 int mins;
760                 int millisecs;
761
762                 left = audioRegion->fade_in()->back()->when;
763                 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
764                 left -= (framepos_t) floor (mins * _session->frame_rate() * 60.0f);
765                 millisecs = (int) floor ((left * 1000.0f) / _session->frame_rate());
766
767                 if (audioRegion->fade_in()->back()->when >= _session->frame_rate()) {
768                         sprintf (fadein_str, "%01dM %01dmS", mins, millisecs);
769                 } else {
770                         sprintf (fadein_str, "%01dmS", millisecs);
771                 }
772
773                 left = audioRegion->fade_out()->back()->when;
774                 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
775                 left -= (framepos_t) floor (mins * _session->frame_rate() * 60.0f);
776                 millisecs = (int) floor ((left * 1000.0f) / _session->frame_rate());
777
778                 if (audioRegion->fade_out()->back()->when >= _session->frame_rate()) {
779                         sprintf (fadeout_str, "%01dM %01dmS", mins, millisecs);
780                 } else {
781                         sprintf (fadeout_str, "%01dmS", millisecs);
782                 }
783         }
784 #endif
785
786 void
787 EditorRegions::populate_row_used (boost::shared_ptr<Region>, TreeModel::Row const& row, uint32_t used)
788 {
789         char buf[8];
790         snprintf (buf, sizeof (buf), "%4d" , used);
791         row[_columns.used] = buf;
792 }
793
794 void
795 EditorRegions::populate_row_length (boost::shared_ptr<Region> region, TreeModel::Row const &row)
796 {
797         char buf[16];
798         format_position (region->length(), buf, sizeof (buf));
799         row[_columns.length] = buf;
800 }
801
802 void
803 EditorRegions::populate_row_end (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
804 {
805         if (region->whole_file()) {
806                 row[_columns.end] = "";
807         } else if (used > 1) {
808                 row[_columns.end] = _("Mult.");
809         } else {
810                 char buf[16];
811                 format_position (region->last_frame(), buf, sizeof (buf));
812                 row[_columns.end] = buf;
813         }
814 }
815
816 void
817 EditorRegions::populate_row_position (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
818 {
819         if (region->whole_file()) {
820                 row[_columns.position] = "";
821         } else if (used > 1) {
822                 row[_columns.position] = _("Mult.");
823         } else {
824                 char buf[16];
825                 format_position (region->position(), buf, sizeof (buf));
826                 row[_columns.position] = buf;
827         }
828 }
829
830 void
831 EditorRegions::populate_row_sync (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
832 {
833         if (region->whole_file()) {
834                 row[_columns.sync] = "";
835         } else if (used > 1) {
836                 row[_columns.sync] = _("Mult."); /* translators: a short phrase for "multiple" as in "many" */
837         } else {
838                 if (region->sync_position() == region->position()) {
839                         row[_columns.sync] = _("Start");
840                 } else if (region->sync_position() == (region->last_frame())) {
841                         row[_columns.sync] = _("End");
842                 } else {
843                         char buf[16];
844                         format_position (region->sync_position(), buf, sizeof (buf));
845                         row[_columns.sync] = buf;
846                 }
847         }
848 }
849
850 void
851 EditorRegions::populate_row_fade_in (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used, boost::shared_ptr<AudioRegion> audioregion)
852 {
853         if (!audioregion || region->whole_file()) {
854                 row[_columns.fadein] = "";
855         } else {
856                 if (used > 1) {
857                         row[_columns.fadein] = _("Multiple");
858                 } else {
859
860                         char buf[16];
861                         format_position (audioregion->fade_in()->back()->when, buf, sizeof (buf));
862                         row[_columns.fadein] = buf;
863
864                         if (audioregion->fade_in_active()) {
865                                 row[_columns.fadein] = string_compose("%1%2%3", " ", buf, " ");
866                         } else {
867                                 row[_columns.fadein] = string_compose("%1%2%3", "(", buf, ")");
868                         }
869                 }
870         }
871 }
872
873 void
874 EditorRegions::populate_row_fade_out (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used, boost::shared_ptr<AudioRegion> audioregion)
875 {
876         if (!audioregion || region->whole_file()) {
877                 row[_columns.fadeout] = "";
878         } else {
879                 if (used > 1) {
880                         row[_columns.fadeout] = _("Multiple");
881                 } else {
882                         char buf[16];
883                         format_position (audioregion->fade_out()->back()->when, buf, sizeof (buf));
884
885                         if (audioregion->fade_out_active()) {
886                                 row[_columns.fadeout] = string_compose("%1%2%3", " ", buf, " ");
887                         } else {
888                                 row[_columns.fadeout] = string_compose("%1%2%3", "(", buf, ")");
889                         }
890                 }
891         }
892 }
893
894 void
895 EditorRegions::populate_row_locked (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
896 {
897         if (region->whole_file()) {
898                 row[_columns.locked] = false;
899         } else if (used > 1) {
900                 row[_columns.locked] = false;
901         } else {
902                 row[_columns.locked] = region->locked();
903         }
904 }
905
906 void
907 EditorRegions::populate_row_glued (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
908 {
909         if (region->whole_file() || used > 1) {
910                 row[_columns.glued] = false;
911         } else {
912                 if (region->position_lock_style() == MusicTime) {
913                         row[_columns.glued] = true;
914                 } else {
915                         row[_columns.glued] = false;
916                 }
917         }
918 }
919
920 void
921 EditorRegions::populate_row_muted (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
922 {
923         if (region->whole_file() || used > 1) {
924                 row[_columns.muted] = false;
925         } else {
926                 row[_columns.muted] = region->muted();
927         }
928 }
929
930 void
931 EditorRegions::populate_row_opaque (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
932 {
933         if (region->whole_file() || used > 1) {
934                 row[_columns.opaque] = false;
935         } else {
936                 row[_columns.opaque] = region->opaque();
937         }
938 }
939
940 void
941 EditorRegions::populate_row_name (boost::shared_ptr<Region> region, TreeModel::Row const &row)
942 {
943         if (region->n_channels() > 1) {
944                 row[_columns.name] = string_compose("%1  [%2]", region->name(), region->n_channels());
945         } else {
946                 row[_columns.name] = region->name();
947         }
948 }
949
950 void
951 EditorRegions::populate_row_source (boost::shared_ptr<Region> region, TreeModel::Row const &row)
952 {
953         if (boost::dynamic_pointer_cast<SilentFileSource>(region->source())) {
954                 row[_columns.path] = _("MISSING ") + region->source()->name();
955         } else {
956                 row[_columns.path] = region->source()->name();
957         }
958 }
959
960 void
961 EditorRegions::toggle_show_auto_regions ()
962 {
963         _show_automatic_regions = toggle_show_auto_regions_action()->get_active();
964         redisplay ();
965 }
966
967 void
968 EditorRegions::toggle_full ()
969 {
970         set_full (toggle_full_action()->get_active ());
971 }
972
973 void
974 EditorRegions::set_full (bool f)
975 {
976         if (f) {
977                 _display.expand_all ();
978                 expanded = true;
979         } else {
980                 _display.collapse_all ();
981                 expanded = false;
982         }
983 }
984
985 void
986 EditorRegions::show_context_menu (int button, int time)
987 {
988         if (_menu == 0) {
989                 _menu = dynamic_cast<Menu*> (ActionManager::get_widget ("/RegionListMenu"));
990         }
991
992         if (_display.get_selection()->count_selected_rows() > 0) {
993                 ActionManager::set_sensitive (ActionManager::region_list_selection_sensitive_actions, true);
994         } else {
995                 ActionManager::set_sensitive (ActionManager::region_list_selection_sensitive_actions, false);
996         }
997
998         /* Enable the "Show" option if any selected regions are hidden, and vice versa for "Hide" */
999
1000         bool have_shown = false;
1001         bool have_hidden = false;
1002
1003         TreeView::Selection::ListHandle_Path rows = _display.get_selection()->get_selected_rows ();
1004         for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
1005                 TreeIter t = _model->get_iter (*i);
1006                 boost::shared_ptr<Region> r = (*t)[_columns.region];
1007                 if (r) {
1008                         if (r->hidden ()) {
1009                                 have_hidden = true;
1010                         } else {
1011                                 have_shown = true;
1012                         }
1013                 }
1014         }
1015
1016         hide_action()->set_sensitive (have_shown);
1017         show_action()->set_sensitive (have_hidden);
1018
1019         _menu->popup (button, time);
1020 }
1021
1022 bool
1023 EditorRegions::key_press (GdkEventKey* ev)
1024 {
1025         TreeViewColumn *col;
1026
1027         switch (ev->keyval) {
1028         case GDK_Tab:
1029         case GDK_ISO_Left_Tab:
1030
1031                 if (name_editable) {
1032                         name_editable->editing_done ();
1033                         name_editable = 0;
1034                 }
1035
1036                 col = _display.get_column (0); // select&focus on name column
1037
1038                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
1039                         treeview_select_previous (_display, _model, col);
1040                 } else {
1041                         treeview_select_next (_display, _model, col);
1042                 }
1043
1044                 return true;
1045                 break;
1046
1047         default:
1048                 break;
1049         }
1050
1051         return false;
1052 }
1053
1054 bool
1055 EditorRegions::button_press (GdkEventButton *ev)
1056 {
1057         boost::shared_ptr<Region> region;
1058         TreeIter iter;
1059         TreeModel::Path path;
1060         TreeViewColumn* column;
1061         int cellx;
1062         int celly;
1063
1064         if (_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
1065                 if ((iter = _model->get_iter (path))) {
1066                         region = (*iter)[_columns.region];
1067                 }
1068         }
1069
1070         if (Keyboard::is_context_menu_event (ev)) {
1071                 show_context_menu (ev->button, ev->time);
1072                 return false;
1073         }
1074
1075         if (region != 0 && Keyboard::is_button2_event (ev)) {
1076                 // start/stop audition
1077                 if (!Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1078                         _editor->consider_auditioning (region);
1079                 }
1080                 return true;
1081         }
1082
1083         return false;
1084 }
1085
1086 int
1087 EditorRegions::sorter (TreeModel::iterator a, TreeModel::iterator b)
1088 {
1089         int cmp = 0;
1090
1091         boost::shared_ptr<Region> r1 = (*a)[_columns.region];
1092         boost::shared_ptr<Region> r2 = (*b)[_columns.region];
1093
1094         /* handle rows without regions, like "Hidden" */
1095
1096         if (r1 == 0) {
1097                 return -1;
1098         }
1099
1100         if (r2 == 0) {
1101                 return 1;
1102         }
1103
1104         boost::shared_ptr<AudioRegion> region1 = boost::dynamic_pointer_cast<AudioRegion> (r1);
1105         boost::shared_ptr<AudioRegion> region2 = boost::dynamic_pointer_cast<AudioRegion> (r2);
1106
1107         if (region1 == 0 || region2 == 0) {
1108                 std::string s1;
1109                 std::string s2;
1110                 switch (_sort_type) {
1111                 case ByName:
1112                         s1 = (*a)[_columns.name];
1113                         s2 = (*b)[_columns.name];
1114                         return (s1.compare (s2));
1115                 default:
1116                         return 0;
1117                 }
1118         }
1119
1120         switch (_sort_type) {
1121         case ByName:
1122                 cmp = strcasecmp (region1->name().c_str(), region2->name().c_str());
1123                 break;
1124
1125         case ByLength:
1126                 cmp = region1->length() - region2->length();
1127                 break;
1128
1129         case ByPosition:
1130                 cmp = region1->position() - region2->position();
1131                 break;
1132
1133         case ByTimestamp:
1134                 cmp = region1->source()->timestamp() - region2->source()->timestamp();
1135                 break;
1136
1137         case ByStartInFile:
1138                 cmp = region1->start() - region2->start();
1139                 break;
1140
1141         case ByEndInFile:
1142                 // cerr << "Compare " << (region1->start() + region1->length()) << " and " << (region2->start() + region2->length()) << endl;
1143                 cmp = (region1->start() + region1->length()) - (region2->start() + region2->length());
1144                 break;
1145
1146         case BySourceFileName:
1147                 cmp = strcasecmp (region1->source()->name().c_str(), region2->source()->name().c_str());
1148                 break;
1149
1150         case BySourceFileLength:
1151                 cmp = region1->source_length(0) - region2->source_length(0);
1152                 break;
1153
1154         case BySourceFileCreationDate:
1155                 cmp = region1->source()->timestamp() - region2->source()->timestamp();
1156                 break;
1157
1158         case BySourceFileFS:
1159                 if (region1->source()->name() == region2->source()->name()) {
1160                         cmp = strcasecmp (region1->name().c_str(),  region2->name().c_str());
1161                 } else {
1162                         cmp = strcasecmp (region1->source()->name().c_str(),  region2->source()->name().c_str());
1163                 }
1164                 break;
1165         }
1166
1167         // cerr << "Comparison on " << enum_2_string (_sort_type) << " gives " << cmp << endl;
1168
1169         if (cmp < 0) {
1170                 return -1;
1171         } else if (cmp > 0) {
1172                 return 1;
1173         } else {
1174                 return 0;
1175         }
1176 }
1177
1178 void
1179 EditorRegions::reset_sort_type (RegionListSortType type, bool force)
1180 {
1181         if (type != _sort_type || force) {
1182                 _sort_type = type;
1183                 _model->set_sort_func (0, (sigc::mem_fun (*this, &EditorRegions::sorter)));
1184         }
1185 }
1186
1187 void
1188 EditorRegions::reset_sort_direction (bool up)
1189 {
1190         _model->set_sort_column (0, up ? SORT_ASCENDING : SORT_DESCENDING);
1191 }
1192
1193 void
1194 EditorRegions::selection_mapover (sigc::slot<void,boost::shared_ptr<Region> > sl)
1195 {
1196         Glib::RefPtr<TreeSelection> selection = _display.get_selection();
1197         TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
1198         TreeView::Selection::ListHandle_Path::iterator i = rows.begin();
1199
1200         if (selection->count_selected_rows() == 0 || _session == 0) {
1201                 return;
1202         }
1203
1204         for (; i != rows.end(); ++i) {
1205                 TreeIter iter;
1206
1207                 if ((iter = _model->get_iter (*i))) {
1208
1209                         /* some rows don't have a region associated with them, but can still be
1210                            selected (XXX maybe prevent them from being selected)
1211                         */
1212
1213                         boost::shared_ptr<Region> r = (*iter)[_columns.region];
1214
1215                         if (r) {
1216                                 sl (r);
1217                         }
1218                 }
1219         }
1220 }
1221
1222
1223 void
1224 EditorRegions::drag_data_received (const RefPtr<Gdk::DragContext>& context,
1225                                    int x, int y,
1226                                    const SelectionData& data,
1227                                    guint info, guint time)
1228 {
1229         vector<string> paths;
1230
1231         if (data.get_target() == "GTK_TREE_MODEL_ROW") {
1232                 /* something is being dragged over the region list */
1233                 _editor->_drags->abort ();
1234                 _display.on_drag_data_received (context, x, y, data, info, time);
1235                 return;
1236         }
1237
1238         if (_editor->convert_drop_to_paths (paths, context, x, y, data, info, time) == 0) {
1239                 framepos_t pos = 0;
1240                 if (Profile->get_sae() || Config->get_only_copy_imported_files()) {
1241                         _editor->do_import (paths, Editing::ImportDistinctFiles, Editing::ImportAsRegion, SrcBest, pos);
1242                 } else {
1243                         _editor->do_embed (paths, Editing::ImportDistinctFiles, ImportAsRegion, pos);
1244                 }
1245                 context->drag_finish (true, false, time);
1246         }
1247 }
1248
1249 bool
1250 EditorRegions::selection_filter (const RefPtr<TreeModel>& model, const TreeModel::Path& path, bool already_selected)
1251 {
1252         /* not possible to select rows that do not represent regions, like "Hidden" */
1253
1254         if (already_selected) {
1255                 /* deselecting anything is OK with us */
1256                 return true;
1257         }
1258
1259         TreeModel::iterator iter = model->get_iter (path);
1260
1261         if (iter) {
1262                 boost::shared_ptr<Region> r =(*iter)[_columns.region];
1263                 if (!r) {
1264                         return false;
1265                 }
1266         }
1267
1268         return true;
1269 }
1270
1271 void
1272 EditorRegions::name_editing_started (CellEditable* ce, const Glib::ustring&)
1273 {
1274         name_editable = ce;
1275
1276         /* give it a special name */
1277
1278         Gtk::Entry *e = dynamic_cast<Gtk::Entry*> (ce);
1279
1280         if (e) {
1281                 e->set_name (X_("RegionNameEditorEntry"));
1282         }
1283 }
1284
1285 void
1286 EditorRegions::name_edit (const std::string& path, const std::string& new_text)
1287 {
1288         name_editable = 0;
1289
1290         boost::shared_ptr<Region> region;
1291         TreeIter iter;
1292
1293         if ((iter = _model->get_iter (path))) {
1294                 region = (*iter)[_columns.region];
1295                 (*iter)[_columns.name] = new_text;
1296         }
1297
1298         /* now mapover everything */
1299
1300         if (region) {
1301                 vector<RegionView*> equivalents;
1302                 _editor->get_regions_corresponding_to (region, equivalents);
1303
1304                 for (vector<RegionView*>::iterator i = equivalents.begin(); i != equivalents.end(); ++i) {
1305                         if (new_text != (*i)->region()->name()) {
1306                                 (*i)->region()->set_name (new_text);
1307                         }
1308                 }
1309         }
1310
1311 }
1312
1313 /** @return Region that has been dragged out of the list, or 0 */
1314 boost::shared_ptr<Region>
1315 EditorRegions::get_dragged_region ()
1316 {
1317         list<boost::shared_ptr<Region> > regions;
1318         TreeView* source;
1319         _display.get_object_drag_data (regions, &source);
1320
1321         if (regions.empty()) {
1322                 return boost::shared_ptr<Region> ();
1323         }
1324
1325         assert (regions.size() == 1);
1326         return regions.front ();
1327 }
1328
1329 void
1330 EditorRegions::clear ()
1331 {
1332         _display.set_model (Glib::RefPtr<Gtk::TreeStore> (0));
1333         _model->clear ();
1334         _display.set_model (_model);
1335
1336         /* Clean up the maps */
1337         region_row_map.clear();
1338         parent_regions_sources_map.clear();
1339 }
1340
1341 boost::shared_ptr<Region>
1342 EditorRegions::get_single_selection ()
1343 {
1344         Glib::RefPtr<TreeSelection> selected = _display.get_selection();
1345
1346         if (selected->count_selected_rows() != 1) {
1347                 return boost::shared_ptr<Region> ();
1348         }
1349
1350         TreeView::Selection::ListHandle_Path rows = selected->get_selected_rows ();
1351
1352         /* only one row selected, so rows.begin() is it */
1353
1354         TreeIter iter = _model->get_iter (*rows.begin());
1355
1356         if (!iter) {
1357                 return boost::shared_ptr<Region> ();
1358         }
1359
1360         return (*iter)[_columns.region];
1361 }
1362
1363 void
1364 EditorRegions::freeze_tree_model (){
1365
1366         _display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
1367         _model->set_sort_column (-2, SORT_ASCENDING); //Disable sorting to gain performance
1368
1369 }
1370
1371 void
1372 EditorRegions::thaw_tree_model (){
1373
1374         _model->set_sort_column (0, SORT_ASCENDING); // renabale sorting
1375         _display.set_model (_model);
1376
1377         if (toggle_full_action()->get_active()) {
1378                 _display.expand_all();
1379         }
1380 }
1381
1382 void
1383 EditorRegions::locked_changed (std::string const & path)
1384 {
1385         TreeIter i = _model->get_iter (path);
1386         if (i) {
1387                 boost::shared_ptr<ARDOUR::Region> region = (*i)[_columns.region];
1388                 if (region) {
1389                         region->set_locked (!(*i)[_columns.locked]);
1390                 }
1391         }
1392 }
1393
1394 void
1395 EditorRegions::glued_changed (std::string const & path)
1396 {
1397         TreeIter i = _model->get_iter (path);
1398         if (i) {
1399                 boost::shared_ptr<ARDOUR::Region> region = (*i)[_columns.region];
1400                 if (region) {
1401                         /* `glued' means MusicTime, and we're toggling here */
1402                         region->set_position_lock_style ((*i)[_columns.glued] ? AudioTime : MusicTime);
1403                 }
1404         }
1405
1406 }
1407
1408 void
1409 EditorRegions::muted_changed (std::string const & path)
1410 {
1411         TreeIter i = _model->get_iter (path);
1412         if (i) {
1413                 boost::shared_ptr<ARDOUR::Region> region = (*i)[_columns.region];
1414                 if (region) {
1415                         region->set_muted (!(*i)[_columns.muted]);
1416                 }
1417         }
1418
1419 }
1420
1421 void
1422 EditorRegions::opaque_changed (std::string const & path)
1423 {
1424         TreeIter i = _model->get_iter (path);
1425         if (i) {
1426                 boost::shared_ptr<ARDOUR::Region> region = (*i)[_columns.region];
1427                 if (region) {
1428                         region->set_opaque (!(*i)[_columns.opaque]);
1429                 }
1430         }
1431
1432 }
1433
1434 XMLNode &
1435 EditorRegions::get_state () const
1436 {
1437         XMLNode* node = new XMLNode (X_("RegionList"));
1438
1439         node->add_property (X_("sort-type"), enum_2_string (_sort_type));
1440
1441         RefPtr<Action> act = ActionManager::get_action (X_("RegionList"), X_("SortAscending"));
1442         bool const ascending = RefPtr<RadioAction>::cast_dynamic(act)->get_active ();
1443         node->add_property (X_("sort-ascending"), ascending ? "yes" : "no");
1444         node->add_property (X_("show-all"), toggle_full_action()->get_active() ? "yes" : "no");
1445         node->add_property (X_("show-automatic-regions"), _show_automatic_regions ? "yes" : "no");
1446
1447         return *node;
1448 }
1449
1450 void
1451 EditorRegions::set_state (const XMLNode & node)
1452 {
1453         bool changed = false;
1454
1455         if (node.name() != X_("RegionList")) {
1456                 return;
1457         }
1458
1459         XMLProperty const * p = node.property (X_("sort-type"));
1460
1461         if (p) {
1462                 Editing::RegionListSortType const t = static_cast<Editing::RegionListSortType> (string_2_enum (p->value(), _sort_type));
1463
1464                 if (_sort_type != t) {
1465                         changed = true;
1466                 }
1467
1468                 reset_sort_type (t, true);
1469                 RefPtr<RadioAction> ract = sort_type_action (t);
1470                 ract->set_active ();
1471         }
1472
1473         p = node.property (X_("sort-ascending"));
1474
1475         if (p) {
1476                 bool const yn = string_is_affirmative (p->value ());
1477                 SortType old_sort_type;
1478                 int old_sort_column;
1479
1480                 _model->get_sort_column_id (old_sort_column, old_sort_type);
1481
1482                 if (old_sort_type != (yn ? SORT_ASCENDING : SORT_DESCENDING)) {
1483                         changed = true;
1484                 }
1485
1486                 reset_sort_direction (yn);
1487                 RefPtr<Action> act;
1488
1489                 if (yn) {
1490                         act = ActionManager::get_action (X_("RegionList"), X_("SortAscending"));
1491                 } else {
1492                         act = ActionManager::get_action (X_("RegionList"), X_("SortDescending"));
1493                 }
1494
1495                 RefPtr<RadioAction>::cast_dynamic(act)->set_active ();
1496         }
1497
1498         p = node.property (X_("show-all"));
1499         if (p) {
1500                 bool const yn = string_is_affirmative (p->value ());
1501
1502                 if (expanded != yn) {
1503                         changed = true;
1504                 }
1505
1506                 set_full (yn);
1507                 toggle_full_action()->set_active (yn);
1508         }
1509
1510         p = node.property (X_("show-automatic-regions"));
1511         if (p) {
1512                 bool const yn = string_is_affirmative (p->value ());
1513
1514                 if (yn != _show_automatic_regions) {
1515                         _show_automatic_regions = yn;
1516                         toggle_show_auto_regions_action()->set_active (yn);
1517                         changed = true;
1518                 }
1519         }
1520
1521         if (changed) {
1522                 redisplay ();
1523         }
1524 }
1525
1526 RefPtr<RadioAction>
1527 EditorRegions::sort_type_action (Editing::RegionListSortType t) const
1528 {
1529         const char* action = 0;
1530
1531         switch (t) {
1532         case Editing::ByName:
1533                 action = X_("SortByRegionName");
1534                 break;
1535         case Editing::ByLength:
1536                 action = X_("SortByRegionLength");
1537                 break;
1538         case Editing::ByPosition:
1539                 action = X_("SortByRegionPosition");
1540                 break;
1541         case Editing::ByTimestamp:
1542                 action = X_("SortByRegionTimestamp");
1543                 break;
1544         case Editing::ByStartInFile:
1545                 action = X_("SortByRegionStartinFile");
1546                 break;
1547         case Editing::ByEndInFile:
1548                 action = X_("SortByRegionEndinFile");
1549                 break;
1550         case Editing::BySourceFileName:
1551                 action = X_("SortBySourceFileName");
1552                 break;
1553         case Editing::BySourceFileLength:
1554                 action = X_("SortBySourceFileLength");
1555                 break;
1556         case Editing::BySourceFileCreationDate:
1557                 action = X_("SortBySourceFileCreationDate");
1558                 break;
1559         case Editing::BySourceFileFS:
1560                 action = X_("SortBySourceFilesystem");
1561                 break;
1562         default:
1563                 fatal << string_compose (_("programming error: %1: %2"), "EditorRegions: impossible sort type", (int) t) << endmsg;
1564                 /*NOTREACHED*/
1565         }
1566
1567         RefPtr<Action> act = ActionManager::get_action (X_("RegionList"), action);
1568         assert (act);
1569
1570         return RefPtr<RadioAction>::cast_dynamic (act);
1571 }
1572
1573 RefPtr<Action>
1574 EditorRegions::hide_action () const
1575 {
1576         return ActionManager::get_action (X_("RegionList"), X_("rlHide"));
1577
1578 }
1579
1580 RefPtr<Action>
1581 EditorRegions::show_action () const
1582 {
1583         return ActionManager::get_action (X_("RegionList"), X_("rlShow"));
1584 }
1585
1586 RefPtr<Action>
1587 EditorRegions::remove_unused_regions_action () const
1588 {
1589         return ActionManager::get_action (X_("RegionList"), X_("removeUnusedRegions"));
1590 }
1591
1592 RefPtr<ToggleAction>
1593 EditorRegions::toggle_full_action () const
1594 {
1595         Glib::RefPtr<Action> act = ActionManager::get_action (X_("RegionList"), X_("rlShowAll"));
1596         assert (act);
1597         return Glib::RefPtr<ToggleAction>::cast_dynamic (act);
1598 }
1599
1600 RefPtr<ToggleAction>
1601 EditorRegions::toggle_show_auto_regions_action () const
1602 {
1603         Glib::RefPtr<Action> act = ActionManager::get_action (X_("RegionList"), X_("rlShowAuto"));
1604         assert (act);
1605         return Glib::RefPtr<ToggleAction>::cast_dynamic (act);
1606 }