fix mistaken "do not roll" conclusion in TransportFSM::compute_should_roll()
[ardour.git] / gtk2_ardour / editor_sources.cc
1 /*
2  * Copyright (C) 2018-2019 Ben Loftis <ben@harrisonconsoles.com>
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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <cstdlib>
20 #include <cmath>
21 #include <algorithm>
22 #include <string>
23 #include <sstream>
24
25 #include "pbd/basename.h"
26 #include "pbd/enumwriter.h"
27 #include "pbd/file_utils.h"
28
29 #include "ardour/audioregion.h"
30 #include "ardour/source.h"
31 #include "ardour/audiofilesource.h"
32 #include "ardour/silentfilesource.h"
33 #include "ardour/smf_source.h"
34 #include "ardour/region_factory.h"
35 #include "ardour/session.h"
36 #include "ardour/session_directory.h"
37 #include "ardour/profile.h"
38
39 #include "gtkmm2ext/treeutils.h"
40 #include "gtkmm2ext/utils.h"
41
42 #include "widgets/choice.h"
43 #include "widgets/tooltips.h"
44
45 #include "audio_clock.h"
46 #include "context_menu_helper.h"
47 #include "editor.h"
48 #include "editing.h"
49 #include "editing_convert.h"
50 #include "keyboard.h"
51 #include "ardour_ui.h"
52 #include "gui_thread.h"
53 #include "actions.h"
54 #include "region_view.h"
55 #include "utils.h"
56 #include "editor_drag.h"
57 #include "main_clock.h"
58 #include "ui_config.h"
59
60 #include "pbd/i18n.h"
61
62 #include "editor_sources.h"
63
64 using namespace std;
65 using namespace ARDOUR;
66 using namespace ArdourWidgets;
67 using namespace ARDOUR_UI_UTILS;
68 using namespace PBD;
69 using namespace Gtk;
70 using namespace Glib;
71 using namespace Editing;
72 using Gtkmm2ext::Keyboard;
73
74 struct ColumnInfo {
75         int         index;
76         const char* label;
77         const char* tooltip;
78 };
79
80 EditorSources::EditorSources (Editor* e)
81         : EditorComponent (e)
82         , old_focus (0)
83         , tags_editable (0)
84         , _menu (0)
85         , _selection (0)
86         , _no_redisplay (false)
87 {
88         _display.set_size_request (100, -1);
89         _display.set_rules_hint (true);
90         _display.set_name ("SourcesList");
91         _display.set_fixed_height_mode (true);
92
93         /* Try to prevent single mouse presses from initiating edits.
94            This relies on a hack in gtktreeview.c:gtk_treeview_button_press()
95         */
96         _display.set_data ("mouse-edits-require-mod1", (gpointer) 0x1);
97
98         _model = TreeStore::create (_columns);
99         _model->set_sort_column (0, SORT_ASCENDING);
100
101         /* column widths */
102         int bbt_width, date_width, height;
103
104         Glib::RefPtr<Pango::Layout> layout = _display.create_pango_layout (X_("000|000|000"));
105         Gtkmm2ext::get_pixel_size (layout, bbt_width, height);
106
107         Glib::RefPtr<Pango::Layout> layout2 = _display.create_pango_layout (X_("2018-10-14 12:12:30"));
108         Gtkmm2ext::get_pixel_size (layout2, date_width, height);
109
110         TreeViewColumn* col_name = manage (new TreeViewColumn ("", _columns.name));
111         col_name->set_fixed_width (bbt_width*2);
112         col_name->set_sizing (TREE_VIEW_COLUMN_FIXED);
113         col_name->set_sort_column(0);
114
115         TreeViewColumn* col_tags = manage (new TreeViewColumn ("", _columns.tags));
116         col_tags->set_fixed_width (bbt_width*2);
117         col_tags->set_sizing (TREE_VIEW_COLUMN_FIXED);
118
119         TreeViewColumn* col_take_id = manage (new TreeViewColumn ("", _columns.take_id));
120         col_take_id->set_fixed_width (date_width);
121         col_take_id->set_sizing (TREE_VIEW_COLUMN_FIXED);
122         col_take_id->set_sort_column(1);
123
124         TreeViewColumn* col_nat_pos = manage (new TreeViewColumn ("", _columns.natural_pos));
125         col_nat_pos->set_fixed_width (bbt_width);
126         col_nat_pos->set_sizing (TREE_VIEW_COLUMN_FIXED);
127         col_nat_pos->set_sort_column(6);
128
129         TreeViewColumn* col_path = manage (new TreeViewColumn ("", _columns.path));
130         col_path->set_fixed_width (bbt_width);
131         col_path->set_sizing (TREE_VIEW_COLUMN_FIXED);
132         col_path->set_sort_column(3);
133
134         _display.append_column (*col_name);
135         _display.append_column (*col_tags);
136         _display.append_column (*col_take_id);
137         _display.append_column (*col_nat_pos);
138         _display.append_column (*col_path);
139
140         TreeViewColumn* col;
141         Gtk::Label* l;
142
143         ColumnInfo ci[] = {
144                 { 0,   _("Source"),    _("Source name, with number of channels in []'s") },
145                 { 1,   _("Tags"),      _("Tags") },
146                 { 2,   _("Take ID"),   _("Take ID") },
147                 { 3,   _("Orig Pos"),  _("Original Position of the file on timeline, when it was recorded") },
148                 { 4,   _("Path"),      _("Path (folder) of the file locationlosition of end of region") },
149                 { -1, 0, 0 }
150         };
151
152         for (int i = 0; ci[i].index >= 0; ++i) {
153                 col = _display.get_column (ci[i].index);
154                 l = manage (new Label (ci[i].label));
155                 set_tooltip (*l, ci[i].tooltip);
156                 col->set_widget (*l);
157                 l->show ();
158         }
159         _display.set_model (_model);
160
161         _display.set_headers_visible (true);
162         _display.set_rules_hint ();
163
164         //set the color of the name field
165         TreeViewColumn* tv_col = _display.get_column(0);
166         CellRendererText* renderer = dynamic_cast<CellRendererText*>(_display.get_column_cell_renderer (0));
167         tv_col->add_attribute(renderer->property_text(), _columns.name);
168         tv_col->add_attribute(renderer->property_foreground_gdk(), _columns.color_);
169
170         //Tags cell: make editable
171         CellRendererText* region_tags_cell = dynamic_cast<CellRendererText*>(_display.get_column_cell_renderer (1));
172         region_tags_cell->property_editable() = true;
173         region_tags_cell->signal_edited().connect (sigc::mem_fun (*this, &EditorSources::tag_edit));
174         region_tags_cell->signal_editing_started().connect (sigc::mem_fun (*this, &EditorSources::tag_editing_started));
175
176         //right-align the Natural Pos column
177         TreeViewColumn* nat_col = _display.get_column(3);
178         nat_col->set_alignment (ALIGN_RIGHT);
179         renderer = dynamic_cast<CellRendererText*>(_display.get_column_cell_renderer (3));
180         if (renderer) {
181                 renderer->property_xalign() = ( 1.0 );
182         }
183
184         //the PATH field should expand when the pane is opened wider
185         tv_col = _display.get_column(4);
186         renderer = dynamic_cast<CellRendererText*>(_display.get_column_cell_renderer (4));
187         tv_col->add_attribute(renderer->property_text(), _columns.path);
188         tv_col->set_expand (true);
189
190         _display.get_selection()->set_mode (SELECTION_MULTIPLE);
191         _display.add_object_drag (_columns.region.index(), "regions");
192         _display.set_drag_column (_columns.name.index());
193
194         /* setup DnD handling */
195
196         list<TargetEntry> source_list_target_table;
197
198         source_list_target_table.push_back (TargetEntry ("text/plain"));
199         source_list_target_table.push_back (TargetEntry ("text/uri-list"));
200         source_list_target_table.push_back (TargetEntry ("application/x-rootwin-drop"));
201
202         _display.add_drop_targets (source_list_target_table);
203         _display.signal_drag_data_received().connect (sigc::mem_fun(*this, &EditorSources::drag_data_received));
204
205         _scroller.add (_display);
206         _scroller.set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC);
207
208         _display.signal_button_press_event().connect (sigc::mem_fun(*this, &EditorSources::button_press), false);
209         _change_connection = _display.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &EditorSources::selection_changed));
210
211         _scroller.signal_key_press_event().connect (sigc::mem_fun(*this, &EditorSources::key_press), false);
212         _scroller.signal_focus_in_event().connect (sigc::mem_fun (*this, &EditorSources::focus_in), false);
213         _scroller.signal_focus_out_event().connect (sigc::mem_fun (*this, &EditorSources::focus_out));
214
215         _display.signal_enter_notify_event().connect (sigc::mem_fun (*this, &EditorSources::enter_notify), false);
216         _display.signal_leave_notify_event().connect (sigc::mem_fun (*this, &EditorSources::leave_notify), false);
217
218         ARDOUR_UI::instance()->primary_clock->mode_changed.connect (sigc::mem_fun(*this, &EditorSources::clock_format_changed));
219
220         e->EditorFreeze.connect (editor_freeze_connection, MISSING_INVALIDATOR, boost::bind (&EditorSources::freeze_tree_model, this), gui_context());
221         e->EditorThaw.connect (editor_thaw_connection, MISSING_INVALIDATOR, boost::bind (&EditorSources::thaw_tree_model, this), gui_context());
222 }
223
224 bool
225 EditorSources::focus_in (GdkEventFocus*)
226 {
227         Window* win = dynamic_cast<Window*> (_scroller.get_toplevel ());
228
229         if (win) {
230                 old_focus = win->get_focus ();
231         } else {
232                 old_focus = 0;
233         }
234
235         tags_editable = 0;
236
237         /* try to do nothing on focus in (doesn't work, hence selection_count nonsense) */
238         return true;
239 }
240
241 bool
242 EditorSources::focus_out (GdkEventFocus*)
243 {
244         if (old_focus) {
245                 old_focus->grab_focus ();
246                 old_focus = 0;
247         }
248
249         tags_editable = 0;
250
251         return false;
252 }
253
254 bool
255 EditorSources::enter_notify (GdkEventCrossing*)
256 {
257         if (tags_editable) {
258                 return true;
259         }
260
261         Keyboard::magic_widget_grab_focus ();
262         return false;
263 }
264
265 bool
266 EditorSources::leave_notify (GdkEventCrossing*)
267 {
268         if (old_focus) {
269                 old_focus->grab_focus ();
270                 old_focus = 0;
271         }
272
273         Keyboard::magic_widget_drop_focus ();
274         return false;
275 }
276
277 void
278 EditorSources::set_session (ARDOUR::Session* s)
279 {
280         SessionHandlePtr::set_session (s);
281
282         if (s) {
283
284                 /*  Currently, none of the displayed properties are mutable, so there is no reason to register for changes
285                  * ARDOUR::Region::RegionPropertyChanged.connect (source_property_connection, MISSING_INVALIDATOR, boost::bind (&EditorSources::source_changed, this, _1, _2), gui_context());
286                 */
287                 
288                 ARDOUR::RegionFactory::CheckNewRegion.connect (add_source_connection, MISSING_INVALIDATOR, boost::bind (&EditorSources::add_source, this, _1), gui_context());
289
290                 s->SourceRemoved.connect (remove_source_connection, MISSING_INVALIDATOR, boost::bind (&EditorSources::remove_weak_source, this, _1), gui_context());
291
292                 redisplay();
293
294         } else {
295                 clear();
296         }
297 }
298
299 void
300 EditorSources::remove_weak_source (boost::weak_ptr<ARDOUR::Source> src)
301 {
302         boost::shared_ptr<ARDOUR::Source> source = src.lock();
303         if (source) {
304                 remove_source (source);
305         }
306 }
307
308 void
309 EditorSources::remove_source (boost::shared_ptr<ARDOUR::Source> source)
310 {
311         TreeModel::iterator i;
312         TreeModel::Children rows = _model->children();
313         for (i = rows.begin(); i != rows.end(); ++i) {
314                 boost::shared_ptr<ARDOUR::Region> rr = (*i)[_columns.region];
315                 if (rr->source() == source) {
316                         _model->erase(i);
317                         break;
318                 }
319         }
320 }
321
322 void
323 EditorSources::populate_row (TreeModel::Row row, boost::shared_ptr<ARDOUR::Region> region)
324 {
325         ENSURE_GUI_THREAD (*this, &ARDOUR_UI::record_state_changed, row, region);
326
327         if (!region) {
328                 return;
329         }
330
331         boost::shared_ptr<ARDOUR::Source> source = region->source();  //ToDo:  is it OK to use only the first source?
332
333         //COLOR  (for missing files)
334         Gdk::Color c;
335         bool missing_source = boost::dynamic_pointer_cast<SilentFileSource>(source) != NULL;
336         if (missing_source) {
337                 set_color_from_rgba (c, UIConfiguration::instance().color ("region list missing source"));
338         } else {
339                 set_color_from_rgba (c, UIConfiguration::instance().color ("region list whole file"));
340         }
341         row[_columns.color_] = c;
342
343         //NAME
344         std::string str = region->name();
345         //if a multichannel region, show the number of channels  ToDo:  make a sortable column for this?
346         if ( region->n_channels() > 1 ) {
347                 str += string_compose("[%1]", region->n_channels());
348         }
349         row[_columns.name] = str;
350
351         //TAGS
352         row[_columns.tags] = region->tags();
353
354         row[_columns.region] = region;
355         row[_columns.take_id] = source->take_id();
356
357         //PATH
358         string pathstr = source->name();
359         if (missing_source) {
360                 pathstr = _("(MISSING) ") + Gtkmm2ext::markup_escape_text (source->name());
361         } else {
362
363                 //is it a file?
364                 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource>(source);
365                 if (!fs) {
366                         pathstr = Gtkmm2ext::markup_escape_text (source->name());  //someday:  sequence region(?)
367                 } else {
368         
369                         //audio file?
370                         boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source);
371                         if (afs) {
372                                 const string audio_directory = _session->session_directory().sound_path();
373                                 if ( !PBD::path_is_within(audio_directory, fs->path())) {
374                                         pathstr = Gtkmm2ext::markup_escape_text (fs->path());
375                                 }
376                         }
377                         
378                         //midi file?
379                         boost::shared_ptr<SMFSource> mfs = boost::dynamic_pointer_cast<SMFSource>(source);
380                         if (mfs) {
381                                 const string midi_directory = _session->session_directory().midi_path();
382                                 if ( !PBD::path_is_within(midi_directory, fs->path())) {
383                                         pathstr = Gtkmm2ext::markup_escape_text (fs->path());
384                                 }
385                         }
386                         
387                 }
388         }
389         
390         row[_columns.path] = pathstr;
391
392         //Natural Position (samples, an invisible column for sorting)
393         row[_columns.natural_s] = source->natural_position();
394
395         //Natural Position (text representation)
396         if (source->have_natural_position()) {
397                 char buf[64];
398                 format_position (source->natural_position(), buf, sizeof (buf));
399                 row[_columns.natural_pos] = buf;
400         } else {
401                 row[_columns.natural_pos] = X_("--");
402         }
403 }
404
405 void
406 EditorSources::redisplay ()
407 {
408         if (_no_redisplay || !_session) {
409                 return;
410         }
411
412         _display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
413         _model->clear ();
414         _model->set_sort_column (-2, SORT_ASCENDING); //Disable sorting to gain performance
415
416         //Ask the region factory to fill our list of whole-file regions
417         RegionFactory::foreach_region (sigc::mem_fun (*this, &EditorSources::add_source));
418
419         _model->set_sort_column (0, SORT_ASCENDING); // re-enable sorting
420         _display.set_model (_model);
421 }
422
423 void
424 EditorSources::add_source (boost::shared_ptr<ARDOUR::Region> region)
425 {
426         if (!region || !_session ) {
427                 return;
428         }
429
430         //by definition, the Source List only shows whole-file regions
431         //this roughly equates to Source objects, but preserves the stereo-ness (or multichannel-ness) of a stereo source file.
432         if ( !region->whole_file() ) {
433                 return;
434         }
435         
436         //we only show files-on-disk.  if there's some other kind of source, we ignore it (for now)
437         boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (region->source());
438         if (!fs || fs->empty()) {
439                 return;
440         }
441
442         TreeModel::Row row = *(_model->append());
443         populate_row (row, region);
444 }
445
446 void
447 EditorSources::source_changed (boost::shared_ptr<ARDOUR::Region> region)
448 {
449         /* Currently never reached .. we have no mutable properties shown in the list*/
450         
451         TreeModel::iterator i;
452         TreeModel::Children rows = _model->children();
453
454         for (i = rows.begin(); i != rows.end(); ++i) {
455                 boost::shared_ptr<ARDOUR::Region> rr = (*i)[_columns.region];
456                 if (region == rr) {
457                         populate_row(*i, region);
458                         break;
459                 }
460         }
461 }
462
463 void
464 EditorSources::selection_changed ()
465 {
466
467         if (_display.get_selection()->count_selected_rows() > 0) {
468
469                 TreeIter iter;
470                 TreeView::Selection::ListHandle_Path rows = _display.get_selection()->get_selected_rows ();
471
472                 _editor->get_selection().clear_regions ();
473
474                 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
475
476                         if ((iter = _model->get_iter (*i))) {
477
478                                 //highlight any regions in the editor that use this region's source
479                                 boost::shared_ptr<ARDOUR::Region> region = (*iter)[_columns.region];
480                                 if (!region) continue;
481
482                                 boost::shared_ptr<ARDOUR::Source> source = region->source();
483                                 if (source) {
484
485                                         set<boost::shared_ptr<Region> > regions;
486                                         RegionFactory::get_regions_using_source ( source, regions );
487
488                                         for (set<boost::shared_ptr<Region> >::iterator region = regions.begin(); region != regions.end(); region++ ) {
489                                                 _change_connection.block (true);
490                                                 _editor->set_selected_regionview_from_region_list (*region, Selection::Add);
491                                                 _change_connection.block (false);
492
493                                         }
494                                 }
495                         }
496                 }
497         } else {
498                 _editor->get_selection().clear_regions ();
499         }
500
501 }
502
503 void
504 EditorSources::clock_format_changed ()
505 {
506         TreeModel::iterator i;
507         TreeModel::Children rows = _model->children();
508         for (i = rows.begin(); i != rows.end(); ++i) {
509                 boost::shared_ptr<ARDOUR::Region> rr = (*i)[_columns.region];
510                 populate_row(*i, rr);
511         }
512 }
513
514 void
515 EditorSources::format_position (samplepos_t pos, char* buf, size_t bufsize, bool onoff)
516 {
517         Timecode::BBT_Time bbt;
518         Timecode::Time timecode;
519
520         if (pos < 0) {
521                 error << string_compose (_("EditorSources::format_position: negative timecode position: %1"), pos) << endmsg;
522                 snprintf (buf, bufsize, "invalid");
523                 return;
524         }
525
526         switch (ARDOUR_UI::instance()->primary_clock->mode ()) {
527         case AudioClock::BBT:
528                 bbt = _session->tempo_map().bbt_at_sample (pos);
529                 if (onoff) {
530                         snprintf (buf, bufsize, "%03d|%02d|%04d" , bbt.bars, bbt.beats, bbt.ticks);
531                 } else {
532                         snprintf (buf, bufsize, "(%03d|%02d|%04d)" , bbt.bars, bbt.beats, bbt.ticks);
533                 }
534                 break;
535
536         case AudioClock::MinSec:
537                 samplepos_t left;
538                 int hrs;
539                 int mins;
540                 float secs;
541
542                 left = pos;
543                 hrs = (int) floor (left / (_session->sample_rate() * 60.0f * 60.0f));
544                 left -= (samplecnt_t) floor (hrs * _session->sample_rate() * 60.0f * 60.0f);
545                 mins = (int) floor (left / (_session->sample_rate() * 60.0f));
546                 left -= (samplecnt_t) floor (mins * _session->sample_rate() * 60.0f);
547                 secs = left / (float) _session->sample_rate();
548                 if (onoff) {
549                         snprintf (buf, bufsize, "%02d:%02d:%06.3f", hrs, mins, secs);
550                 } else {
551                         snprintf (buf, bufsize, "(%02d:%02d:%06.3f)", hrs, mins, secs);
552                 }
553                 break;
554
555         case AudioClock::Seconds:
556                 if (onoff) {
557                         snprintf (buf, bufsize, "%.1f", pos / (float)_session->sample_rate());
558                 } else {
559                         snprintf (buf, bufsize, "(%.1f)", pos / (float)_session->sample_rate());
560                 }
561                 break;
562
563         case AudioClock::Samples:
564                 if (onoff) {
565                         snprintf (buf, bufsize, "%" PRId64, pos);
566                 } else {
567                         snprintf (buf, bufsize, "(%" PRId64 ")", pos);
568                 }
569                 break;
570
571         case AudioClock::Timecode:
572         default:
573                 _session->timecode_time (pos, timecode);
574                 if (onoff) {
575                         snprintf (buf, bufsize, "%02d:%02d:%02d:%02d", timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
576                 } else {
577                         snprintf (buf, bufsize, "(%02d:%02d:%02d:%02d)", timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
578                 }
579                 break;
580         }
581 }
582
583 void
584 EditorSources::show_context_menu (int button, int time)
585 {
586         using namespace Gtk::Menu_Helpers;
587         Gtk::Menu* menu = ARDOUR_UI_UTILS::shared_popup_menu ();
588         MenuList&  items = menu->items();
589 #ifdef RECOVER_REGIONS_IS_WORKING
590         items.push_back(MenuElem(_("Recover the selected Sources to their original Track & Position"),
591                                                          sigc::mem_fun(*this, &EditorSources::recover_selected_sources)));
592 #endif
593         items.push_back(MenuElem(_("Remove the selected Sources"),
594                                                          sigc::mem_fun(*this, &EditorSources::remove_selected_sources)));
595         menu->popup(1, time);
596 }
597
598 void
599 EditorSources::recover_selected_sources ()
600 {
601         ARDOUR::RegionList to_be_recovered;
602         
603         if (_display.get_selection()->count_selected_rows() > 0) {
604
605                 TreeIter iter;
606                 TreeView::Selection::ListHandle_Path rows = _display.get_selection()->get_selected_rows ();
607                 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
608                         if ((iter = _model->get_iter (*i))) {
609                                 boost::shared_ptr<ARDOUR::Region> region = (*iter)[_columns.region];
610                                 if (region) {
611                                         to_be_recovered.push_back(region);
612                                 }
613                         }
614                 }
615         }
616
617
618         /* ToDo */
619         _editor->recover_regions(to_be_recovered);  //this operation should be undo-able
620 }
621
622
623 void
624 EditorSources::remove_selected_sources ()
625 {
626         vector<string> choices;
627         string prompt;
628
629         prompt  = _("Do you want to remove the selected Sources?"
630                                 "\nThis operation cannot be undone."
631                                 "\nThe source files will not actually be deleted until you execute Session->Cleanup.");
632
633         choices.push_back (_("No, do nothing."));
634         choices.push_back (_("Only remove the Regions that use these Sources."));
635         choices.push_back (_("Yes, remove the Regions and Sources (cannot be undone!"));
636
637         Choice prompter (_("Remove selected Sources"), prompt, choices);
638
639         int opt = prompter.run ();
640
641         if ( opt >= 1) {
642                 
643                 std::list<boost::weak_ptr<ARDOUR::Source> > to_be_removed;
644                 
645                 if (_display.get_selection()->count_selected_rows() > 0) {
646
647                         TreeIter iter;
648                         TreeView::Selection::ListHandle_Path rows = _display.get_selection()->get_selected_rows ();
649
650                         _editor->get_selection().clear_regions ();
651
652                         for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
653
654                                 if ((iter = _model->get_iter (*i))) {
655
656                                         boost::shared_ptr<ARDOUR::Region> region = (*iter)[_columns.region];
657         
658                                         if (!region) continue;
659
660                                         boost::shared_ptr<ARDOUR::Source> source = region->source();
661                                         if (source) {
662                                                 set<boost::shared_ptr<Region> > regions;
663                                                 RegionFactory::get_regions_using_source ( source, regions );
664
665                                                 for (set<boost::shared_ptr<Region> >::iterator region = regions.begin(); region != regions.end(); region++ ) {
666                                                         _change_connection.block (true);
667                                                         _editor->set_selected_regionview_from_region_list (*region, Selection::Add);
668                                                         _change_connection.block (false);
669                                                 }
670                                                 
671                                                 to_be_removed.push_back(source);
672                                         }
673                                 }
674
675                         }
676
677                         _editor->remove_selected_regions();  //this operation is undo-able
678
679                         if (opt==2) {   
680                                 for (std::list<boost::weak_ptr<ARDOUR::Source> >::iterator i = to_be_removed.begin(); i != to_be_removed.end(); ++i) {
681                                                 _session->remove_source(*i);  //this operation is (currently) not undo-able
682                                 }
683                         }
684                 }
685         }
686
687 }
688
689
690 bool
691 EditorSources::key_press (GdkEventKey* ev)
692 {
693         TreeViewColumn *col;
694
695         switch (ev->keyval) {
696         case GDK_Tab:
697         case GDK_ISO_Left_Tab:
698
699                 if (tags_editable) {
700                         tags_editable->editing_done ();
701                         tags_editable = 0;
702                 }
703
704                 col = _display.get_column (1); // select&focus on tags column
705
706                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
707                         treeview_select_previous (_display, _model, col);
708                 } else {
709                         treeview_select_next (_display, _model, col);
710                 }
711
712                 return true;
713                 break;
714
715         case GDK_BackSpace:
716                 remove_selected_sources();
717                 return true; 
718
719         default:
720                 break;
721         }
722
723         return false;
724 }
725
726 bool
727 EditorSources::button_press (GdkEventButton *ev)
728 {
729         boost::shared_ptr<ARDOUR::Region> region;
730         TreeIter iter;
731         TreeModel::Path path;
732         TreeViewColumn* column;
733         int cellx;
734         int celly;
735
736         if (_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
737                 if ((iter = _model->get_iter (path))) {
738                         region = (*iter)[_columns.region];
739                 }
740         }
741
742         if (Keyboard::is_context_menu_event (ev)) {
743                 show_context_menu (ev->button, ev->time);
744                 return false;
745         }
746
747         return false;
748 }
749
750 void
751 EditorSources::tag_editing_started (CellEditable* ce, const Glib::ustring& path)
752 {
753         tags_editable = ce;
754
755         /* give it a special name */
756
757         Gtk::Entry *e = dynamic_cast<Gtk::Entry*> (ce);
758
759         if (e) {
760                 e->set_name (X_("SourceTagEditorEntry"));
761
762                 TreeIter iter;
763                 if ((iter = _model->get_iter (path))) {
764                         boost::shared_ptr<Region> region = (*iter)[_columns.region];
765
766                         if(region) {
767                                 e->set_text(region->tags());
768                         }
769                 }
770         }
771 }
772
773 void
774 EditorSources::tag_edit (const std::string& path, const std::string& new_text)
775 {
776         tags_editable = 0;
777
778         boost::shared_ptr<Region> region;
779         TreeIter row_iter;
780
781         if ((row_iter = _model->get_iter (path))) {
782                 region = (*row_iter)[_columns.region];
783                 (*row_iter)[_columns.tags] = new_text;
784         }
785
786         if (region) {
787                 region->set_tags (new_text);
788
789                 _session->set_dirty();  //whole-file regions aren't in a playlist to catch property changes, so we need to explicitly set the session dirty
790
791                 populate_row ((*row_iter), region);
792         }
793 }
794
795
796 void
797 EditorSources::selection_mapover (sigc::slot<void,boost::shared_ptr<Region> > sl)
798 {
799
800 }
801
802
803 void
804 EditorSources::drag_data_received (const RefPtr<Gdk::DragContext>& context,
805                                    int x, int y,
806                                    const SelectionData& data,
807                                    guint info, guint time)
808 {
809         /* ToDo:  allow dropping files/loops into the source list?  */
810 }
811
812 /** @return Region that has been dragged out of the list, or 0 */
813 boost::shared_ptr<ARDOUR::Region>
814 EditorSources::get_dragged_region ()
815 {
816         list<boost::shared_ptr<ARDOUR::Region> > regions;
817         TreeView* region;
818         _display.get_object_drag_data (regions, &region);
819
820         if (regions.empty()) {
821                 return boost::shared_ptr<ARDOUR::Region> ();
822         }
823
824         assert (regions.size() == 1);
825         return regions.front ();
826 }
827
828 void
829 EditorSources::clear ()
830 {
831         _display.set_model (Glib::RefPtr<Gtk::TreeStore> (0));
832         _model->clear ();
833         _display.set_model (_model);
834 }
835
836 boost::shared_ptr<ARDOUR::Region>
837 EditorSources::get_single_selection ()
838 {
839         Glib::RefPtr<TreeSelection> selected = _display.get_selection();
840
841         if (selected->count_selected_rows() != 1) {
842                 return boost::shared_ptr<ARDOUR::Region> ();
843         }
844
845         TreeView::Selection::ListHandle_Path rows = selected->get_selected_rows ();
846
847         /* only one row selected, so rows.begin() is it */
848
849         TreeIter iter = _model->get_iter (*rows.begin());
850
851         if (!iter) {
852                 return boost::shared_ptr<ARDOUR::Region> ();
853         }
854
855         return (*iter)[_columns.region];
856 }
857
858 void
859 EditorSources::freeze_tree_model ()
860 {
861         _display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
862         _model->set_sort_column (-2, SORT_ASCENDING); //Disable sorting to gain performance
863 }
864
865 void
866 EditorSources::thaw_tree_model (){
867
868         _model->set_sort_column (0, SORT_ASCENDING); // renabale sorting
869         _display.set_model (_model);
870 }
871
872 XMLNode &
873 EditorSources::get_state () const
874 {
875         XMLNode* node = new XMLNode (X_("SourcesList"));
876
877         //TODO:  save sort state?
878
879         return *node;
880 }
881
882 void
883 EditorSources::set_state (const XMLNode & node)
884 {
885
886 }