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