3ba9468c49c4fb017dabd02239d88cf30705762c
[ardour.git] / gtk2_ardour / editor_sources.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/source.h"
31 #include "ardour/audiofilesource.h"
32 #include "ardour/silentfilesource.h"
33 #include "ardour/region_factory.h"
34 #include "ardour/session.h"
35 #include "ardour/profile.h"
36
37 #include "gtkmm2ext/treeutils.h"
38 #include "gtkmm2ext/utils.h"
39
40 #include "widgets/choice.h"
41 #include "widgets/tooltips.h"
42
43 #include "audio_clock.h"
44 #include "editor.h"
45 #include "editing.h"
46 #include "editing_convert.h"
47 #include "keyboard.h"
48 #include "ardour_ui.h"
49 #include "gui_thread.h"
50 #include "actions.h"
51 #include "region_view.h"
52 #include "utils.h"
53 #include "editor_drag.h"
54 #include "main_clock.h"
55 #include "ui_config.h"
56
57 #include "pbd/i18n.h"
58
59 #include "editor_sources.h"
60
61 using namespace std;
62 using namespace ARDOUR;
63 using namespace ArdourWidgets;
64 using namespace ARDOUR_UI_UTILS;
65 using namespace PBD;
66 using namespace Gtk;
67 using namespace Glib;
68 using namespace Editing;
69 using Gtkmm2ext::Keyboard;
70
71 struct ColumnInfo {
72         int         index;
73         const char* label;
74         const char* tooltip;
75 };
76
77 EditorSources::EditorSources (Editor* e)
78         : EditorComponent (e)
79         , old_focus (0)
80         , _menu (0)
81         , _selection (0)
82 {
83         _display.set_size_request (100, -1);
84         _display.set_rules_hint (true);
85         _display.set_name ("SourcesList");
86         _display.set_fixed_height_mode (true);
87
88         /* Try to prevent single mouse presses from initiating edits.
89            This relies on a hack in gtktreeview.c:gtk_treeview_button_press()
90         */
91         _display.set_data ("mouse-edits-require-mod1", (gpointer) 0x1);
92
93         _model = TreeStore::create (_columns);
94         _model->set_sort_column (0, SORT_ASCENDING);
95
96         /* column widths */
97         int bbt_width, date_width, height;
98         
99         Glib::RefPtr<Pango::Layout> layout = _display.create_pango_layout (X_("000|000|000"));
100         Gtkmm2ext::get_pixel_size (layout, bbt_width, height);
101
102         Glib::RefPtr<Pango::Layout> layout2 = _display.create_pango_layout (X_("2018-10-14 12:12:30"));
103         Gtkmm2ext::get_pixel_size (layout2, date_width, height);
104
105         TreeViewColumn* col_name = manage (new TreeViewColumn ("", _columns.name));
106         col_name->set_fixed_width (bbt_width*2);
107         col_name->set_sizing (TREE_VIEW_COLUMN_FIXED);
108         col_name->set_sort_column(0);
109
110         TreeViewColumn* col_take_id = manage (new TreeViewColumn ("", _columns.take_id));
111         col_take_id->set_fixed_width (date_width);
112         col_take_id->set_sizing (TREE_VIEW_COLUMN_FIXED);
113         col_take_id->set_sort_column(1);
114
115         TreeViewColumn* col_nat_pos = manage (new TreeViewColumn ("", _columns.natural_pos));
116         col_nat_pos->set_fixed_width (bbt_width);
117         col_nat_pos->set_sizing (TREE_VIEW_COLUMN_FIXED);
118         col_nat_pos->set_sort_column(6);
119
120         TreeViewColumn* col_path = manage (new TreeViewColumn ("", _columns.path));
121         col_path->set_fixed_width (bbt_width);
122         col_path->set_sizing (TREE_VIEW_COLUMN_FIXED);
123         col_path->set_sort_column(3);
124
125         _display.append_column (*col_name);
126         _display.append_column (*col_take_id);
127         _display.append_column (*col_nat_pos);
128         _display.append_column (*col_path);
129
130         TreeViewColumn* col;
131         Gtk::Label* l;
132
133         ColumnInfo ci[] = {
134                 { 0,   _("Source"),    _("Source name, with number of channels in []'s") },
135                 { 1,   _("Take ID"),   _("Take ID") },
136                 { 2,   _("Orig Pos"),  _("Original Position of the file on timeline, when it was recorded") },
137                 { 3,   _("Path"),      _("Path (folder) of the file locationlosition of end of region") },
138                 { -1, 0, 0 }
139         };
140
141         for (int i = 0; ci[i].index >= 0; ++i) {
142                 col = _display.get_column (ci[i].index);
143                 l = manage (new Label (ci[i].label));
144                 set_tooltip (*l, ci[i].tooltip);
145                 col->set_widget (*l);
146                 l->show ();
147         }
148         _display.set_model (_model);
149
150         _display.set_headers_visible (true);
151         _display.set_rules_hint ();
152
153         _display.get_selection()->set_select_function (sigc::mem_fun (*this, &EditorSources::selection_filter));
154
155         //set the color of the name field
156         TreeViewColumn* tv_col = _display.get_column(0);
157         CellRendererText* renderer = dynamic_cast<CellRendererText*>(_display.get_column_cell_renderer (0));
158         tv_col->add_attribute(renderer->property_text(), _columns.name);
159         tv_col->add_attribute(renderer->property_foreground_gdk(), _columns.color_);
160
161         //right-align the Natural Pos column
162         TreeViewColumn* nat_col = _display.get_column(2);
163         nat_col->set_alignment (ALIGN_RIGHT);
164         renderer = dynamic_cast<CellRendererText*>(_display.get_column_cell_renderer (2));
165         if (renderer) {
166                 renderer->property_xalign() = ( ALIGN_RIGHT );
167         }
168
169         //the PATH field should expand when the pane is opened wider
170         tv_col = _display.get_column(3);
171         renderer = dynamic_cast<CellRendererText*>(_display.get_column_cell_renderer (3));
172         tv_col->add_attribute(renderer->property_text(), _columns.path);
173         tv_col->set_expand (true);
174
175         _display.get_selection()->set_mode (SELECTION_MULTIPLE);
176         _display.add_object_drag (_columns.source.index(), "regions");
177         _display.set_drag_column (_columns.name.index());
178
179         /* setup DnD handling */
180
181         list<TargetEntry> region_list_target_table;
182
183         region_list_target_table.push_back (TargetEntry ("text/plain"));
184         region_list_target_table.push_back (TargetEntry ("text/uri-list"));
185         region_list_target_table.push_back (TargetEntry ("application/x-rootwin-drop"));
186
187         _display.add_drop_targets (region_list_target_table);
188         _display.signal_drag_data_received().connect (sigc::mem_fun(*this, &EditorSources::drag_data_received));
189
190         _scroller.add (_display);
191         _scroller.set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC);
192
193         _display.signal_button_press_event().connect (sigc::mem_fun(*this, &EditorSources::button_press), false);
194         _change_connection = _display.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &EditorSources::selection_changed));
195
196         _scroller.signal_key_press_event().connect (sigc::mem_fun(*this, &EditorSources::key_press), false);
197         _scroller.signal_focus_in_event().connect (sigc::mem_fun (*this, &EditorSources::focus_in), false);
198         _scroller.signal_focus_out_event().connect (sigc::mem_fun (*this, &EditorSources::focus_out));
199
200         _display.signal_enter_notify_event().connect (sigc::mem_fun (*this, &EditorSources::enter_notify), false);
201         _display.signal_leave_notify_event().connect (sigc::mem_fun (*this, &EditorSources::leave_notify), false);
202
203         ARDOUR_UI::instance()->primary_clock->mode_changed.connect (sigc::mem_fun(*this, &EditorSources::clock_format_changed));
204
205         e->EditorFreeze.connect (editor_freeze_connection, MISSING_INVALIDATOR, boost::bind (&EditorSources::freeze_tree_model, this), gui_context());
206         e->EditorThaw.connect (editor_thaw_connection, MISSING_INVALIDATOR, boost::bind (&EditorSources::thaw_tree_model, this), gui_context());
207 }
208
209 bool
210 EditorSources::focus_in (GdkEventFocus*)
211 {
212         Window* win = dynamic_cast<Window*> (_scroller.get_toplevel ());
213
214         if (win) {
215                 old_focus = win->get_focus ();
216         } else {
217                 old_focus = 0;
218         }
219
220         /* try to do nothing on focus in (doesn't work, hence selection_count nonsense) */
221         return true;
222 }
223
224 bool
225 EditorSources::focus_out (GdkEventFocus*)
226 {
227         if (old_focus) {
228                 old_focus->grab_focus ();
229                 old_focus = 0;
230         }
231
232         return false;
233 }
234
235 bool
236 EditorSources::enter_notify (GdkEventCrossing*)
237 {
238         /* arm counter so that ::selection_filter() will deny selecting anything for the
239            next two attempts to change selection status.
240         */
241         _scroller.grab_focus ();
242         Keyboard::magic_widget_grab_focus ();
243         return false;
244 }
245
246 bool
247 EditorSources::leave_notify (GdkEventCrossing*)
248 {
249         if (old_focus) {
250                 old_focus->grab_focus ();
251                 old_focus = 0;
252         }
253
254         Keyboard::magic_widget_drop_focus ();
255         return false;
256 }
257
258 void
259 EditorSources::set_session (ARDOUR::Session* s)
260 {
261         SessionHandlePtr::set_session (s);
262         
263         if (s) {
264                 //get all existing sources
265                 s->foreach_source (sigc::mem_fun (*this, &EditorSources::add_source));
266         
267                 //register to get new sources that are recorded/imported
268                 s->SourceAdded.connect (source_added_connection, MISSING_INVALIDATOR, boost::bind (&EditorSources::add_source, this, _1), gui_context());
269                 s->SourceRemoved.connect (source_removed_connection, MISSING_INVALIDATOR, boost::bind (&EditorSources::remove_source, this, _1), gui_context());
270
271                 //register for source property changes ( some things like take_id aren't immediately available at construction )
272                 ARDOUR::Source::SourcePropertyChanged.connect (source_property_connection, MISSING_INVALIDATOR, boost::bind (&EditorSources::source_changed, this, _1), gui_context());
273         } else {
274                 clear();        
275         }
276 }
277
278 void
279 EditorSources::remove_source (boost::shared_ptr<ARDOUR::Source> source)
280 {
281         TreeModel::iterator i;
282         TreeModel::Children rows = _model->children();
283         for (i = rows.begin(); i != rows.end(); ++i) {
284                 boost::shared_ptr<ARDOUR::Source> ss = (*i)[_columns.source];
285                 if (source == ss) {
286                         _model->erase(i);
287                         break;
288                 }
289         }
290 }
291
292 void
293 EditorSources::populate_row (TreeModel::Row row, boost::shared_ptr<ARDOUR::Source> source)
294 {
295         ENSURE_GUI_THREAD (*this, &ARDOUR_UI::record_state_changed, row, source);
296
297         if (!source) {
298                 return;
299         }
300         
301         string str;
302         Gdk::Color c;
303
304         bool missing_source = boost::dynamic_pointer_cast<SilentFileSource>(source) != NULL;
305         if (missing_source) {
306                 set_color_from_rgba (c, UIConfiguration::instance().color ("region list missing source"));
307         } else {
308                 set_color_from_rgba (c, UIConfiguration::instance().color ("region list whole file"));
309         }
310
311         row[_columns.color_] = c;
312
313         if (source->name()[0] == '/') { // external file
314
315                 str = ".../";
316
317                 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source);
318                 if (afs) {
319                         str += source->name();
320                         str += "[";
321                         str += afs->n_channels();  //ToDo:   num channels may be its own column?
322                         str += "]";
323                 } else {
324                         str += source->name();
325                 }
326
327         } else {
328                 str = source->name();
329         }
330
331         row[_columns.name] = str;
332         row[_columns.source] = source;
333
334         if (missing_source) {
335                 row[_columns.path] = _("(MISSING) ") + Gtkmm2ext::markup_escape_text (source->name());
336
337         } else {
338                 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource>(source);
339                 if (fs) {
340                         row[_columns.path] = Gtkmm2ext::markup_escape_text (fs->path());
341                 } else {
342                         row[_columns.path] = Gtkmm2ext::markup_escape_text (source->name());
343                 }
344         }
345
346         row[_columns.take_id] = source->take_id();
347
348
349         //Natural Position (samples, an invisible column for sorting)
350         row[_columns.natural_s] = source->natural_position();
351
352         //Natural Position (text representation)
353         char buf[64];
354         snprintf(buf, 16, "--" );
355         if (source->natural_position() > 0) {
356                 format_position (source->natural_position(), buf, sizeof (buf));
357         }
358         row[_columns.natural_pos] = buf;
359 }
360
361 void
362 EditorSources::add_source (boost::shared_ptr<ARDOUR::Source> source)
363 {
364         if (!source || !_session ) {
365                 return;
366         }
367
368         boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (source);
369
370         if (!fs || fs->empty()) {
371                 return;
372         }
373
374         TreeModel::Row row = *(_model->append());
375         populate_row (row, source);
376 }
377
378
379 void
380 EditorSources::remove_unused_regions ()
381 {
382 /*      vector<string> choices;
383         string prompt;
384
385         if (!_session) {
386                 return;
387         }
388
389         prompt = _("Do you really want to remove unused regions?"
390                    "\n(This is destructive and cannot be undone)");
391
392         choices.push_back (_("No, do nothing."));
393         choices.push_back (_("Yes, remove."));
394
395         ArdourWidgets::Choice prompter (_("Remove unused regions"), prompt, choices);
396
397         if (prompter.run () == 1) {
398                 _no_redisplay = true;
399                 _session->cleanup_regions ();
400                 _no_redisplay = false;
401                 redisplay ();
402         }
403 */
404 }
405
406 void
407 EditorSources::source_changed (boost::shared_ptr<ARDOUR::Source> source)
408 {
409         TreeModel::iterator i;
410         TreeModel::Children rows = _model->children();
411         for (i = rows.begin(); i != rows.end(); ++i) {
412                 boost::shared_ptr<ARDOUR::Source> ss = (*i)[_columns.source];
413                 if (source == ss) {
414                         populate_row(*i, source);
415                         break;
416                 }
417         }
418 }
419
420 void
421 EditorSources::selection_changed ()
422 {
423 //      _editor->_region_selection_change_updates_region_list = false;
424
425         if (_display.get_selection()->count_selected_rows() > 0) {
426
427                 TreeIter iter;
428                 TreeView::Selection::ListHandle_Path rows = _display.get_selection()->get_selected_rows ();
429
430                 _editor->get_selection().clear_regions ();
431
432                 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
433
434                         if ((iter = _model->get_iter (*i))) {
435
436                                 boost::shared_ptr<ARDOUR::Source> source = (*iter)[_columns.source];
437                                 if (source) {
438
439                                         set<boost::shared_ptr<Region> > regions;
440                                         RegionFactory::get_regions_using_source ( source, regions );
441
442                                         for (set<boost::shared_ptr<Region> >::iterator region = regions.begin(); region != regions.end(); region++ ) {
443                                                 _change_connection.block (true);
444                                                 _editor->set_selected_regionview_from_region_list (*region, Selection::Add);
445                                                 _change_connection.block (false);
446         
447                                         }
448                                 }
449                         }
450
451                 }
452         } else {
453                 _editor->get_selection().clear_regions ();
454         }
455
456 //      _editor->_region_selection_change_updates_region_list = true;
457 }
458
459 void
460 EditorSources::clock_format_changed ()
461 {
462         TreeModel::iterator i;
463         TreeModel::Children rows = _model->children();
464         for (i = rows.begin(); i != rows.end(); ++i) {
465                 boost::shared_ptr<ARDOUR::Source> ss = (*i)[_columns.source];
466                 populate_row(*i, ss);
467         }
468 }
469
470 void
471 EditorSources::format_position (samplepos_t pos, char* buf, size_t bufsize, bool onoff)
472 {
473         Timecode::BBT_Time bbt;
474         Timecode::Time timecode;
475
476         if (pos < 0) {
477                 error << string_compose (_("EditorSources::format_position: negative timecode position: %1"), pos) << endmsg;
478                 snprintf (buf, bufsize, "invalid");
479                 return;
480         }
481
482         switch (ARDOUR_UI::instance()->primary_clock->mode ()) {
483         case AudioClock::BBT:
484                 bbt = _session->tempo_map().bbt_at_sample (pos);
485                 if (onoff) {
486                         snprintf (buf, bufsize, "%03d|%02d|%04d" , bbt.bars, bbt.beats, bbt.ticks);
487                 } else {
488                         snprintf (buf, bufsize, "(%03d|%02d|%04d)" , bbt.bars, bbt.beats, bbt.ticks);
489                 }
490                 break;
491
492         case AudioClock::MinSec:
493                 samplepos_t left;
494                 int hrs;
495                 int mins;
496                 float secs;
497
498                 left = pos;
499                 hrs = (int) floor (left / (_session->sample_rate() * 60.0f * 60.0f));
500                 left -= (samplecnt_t) floor (hrs * _session->sample_rate() * 60.0f * 60.0f);
501                 mins = (int) floor (left / (_session->sample_rate() * 60.0f));
502                 left -= (samplecnt_t) floor (mins * _session->sample_rate() * 60.0f);
503                 secs = left / (float) _session->sample_rate();
504                 if (onoff) {
505                         snprintf (buf, bufsize, "%02d:%02d:%06.3f", hrs, mins, secs);
506                 } else {
507                         snprintf (buf, bufsize, "(%02d:%02d:%06.3f)", hrs, mins, secs);
508                 }
509                 break;
510
511         case AudioClock::Seconds:
512                 if (onoff) {
513                         snprintf (buf, bufsize, "%.1f", pos / (float)_session->sample_rate());
514                 } else {
515                         snprintf (buf, bufsize, "(%.1f)", pos / (float)_session->sample_rate());
516                 }
517                 break;
518
519         case AudioClock::Samples:
520                 if (onoff) {
521                         snprintf (buf, bufsize, "%" PRId64, pos);
522                 } else {
523                         snprintf (buf, bufsize, "(%" PRId64 ")", pos);
524                 }
525                 break;
526
527         case AudioClock::Timecode:
528         default:
529                 _session->timecode_time (pos, timecode);
530                 if (onoff) {
531                         snprintf (buf, bufsize, "%02d:%02d:%02d:%02d", timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
532                 } else {
533                         snprintf (buf, bufsize, "(%02d:%02d:%02d:%02d)", timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
534                 }
535                 break;
536         }
537 }
538
539 void
540 EditorSources::show_context_menu (int button, int time)
541 {
542
543 }
544
545 bool
546 EditorSources::key_press (GdkEventKey* ev)
547 {
548         return false;
549 }
550
551 bool
552 EditorSources::button_press (GdkEventButton *ev)
553 {
554         boost::shared_ptr<ARDOUR::Source> source;
555         TreeIter iter;
556         TreeModel::Path path;
557         TreeViewColumn* column;
558         int cellx;
559         int celly;
560
561         if (_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
562                 if ((iter = _model->get_iter (path))) {
563                         source = (*iter)[_columns.source];
564                 }
565         }
566
567         if (Keyboard::is_context_menu_event (ev)) {
568                 show_context_menu (ev->button, ev->time);
569                 return false;
570         }
571
572         return false;
573 }
574
575 void
576 EditorSources::selection_mapover (sigc::slot<void,boost::shared_ptr<Region> > sl)
577 {
578
579 }
580
581
582 void
583 EditorSources::drag_data_received (const RefPtr<Gdk::DragContext>& context,
584                                    int x, int y,
585                                    const SelectionData& data,
586                                    guint info, guint time)
587 {
588
589 }
590
591 bool
592 EditorSources::selection_filter (const RefPtr<TreeModel>& model, const TreeModel::Path& path, bool already_selected)
593 {
594         return true;
595 }
596
597 /** @return Region that has been dragged out of the list, or 0 */
598 boost::shared_ptr<ARDOUR::Source>
599 EditorSources::get_dragged_source ()
600 {
601         list<boost::shared_ptr<ARDOUR::Source> > sources;
602         TreeView* source;
603         _display.get_object_drag_data (sources, &source);
604
605         if (sources.empty()) {
606                 return boost::shared_ptr<ARDOUR::Source> ();
607         }
608
609         assert (sources.size() == 1);
610         return sources.front ();
611 }
612
613 void
614 EditorSources::clear ()
615 {
616         _display.set_model (Glib::RefPtr<Gtk::TreeStore> (0));
617         _model->clear ();
618         _display.set_model (_model);
619 }
620
621 boost::shared_ptr<ARDOUR::Source>
622 EditorSources::get_single_selection ()
623 {
624         Glib::RefPtr<TreeSelection> selected = _display.get_selection();
625
626         if (selected->count_selected_rows() != 1) {
627                 return boost::shared_ptr<ARDOUR::Source> ();
628         }
629
630         TreeView::Selection::ListHandle_Path rows = selected->get_selected_rows ();
631
632         /* only one row selected, so rows.begin() is it */
633
634         TreeIter iter = _model->get_iter (*rows.begin());
635
636         if (!iter) {
637                 return boost::shared_ptr<ARDOUR::Source> ();
638         }
639
640         return (*iter)[_columns.source];
641 }
642
643 void
644 EditorSources::freeze_tree_model ()
645 {
646         _display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
647         _model->set_sort_column (-2, SORT_ASCENDING); //Disable sorting to gain performance
648 }
649
650 void
651 EditorSources::thaw_tree_model (){
652
653         _model->set_sort_column (0, SORT_ASCENDING); // renabale sorting
654         _display.set_model (_model);
655 }
656
657 XMLNode &
658 EditorSources::get_state () const
659 {
660         XMLNode* node = new XMLNode (X_("SourcesList"));
661
662         //TODO:  save sort state?
663
664         return *node;
665 }
666
667 void
668 EditorSources::set_state (const XMLNode & node)
669 {
670         bool changed = false;
671
672 }
673
674 RefPtr<Action>
675 EditorSources::remove_unused_regions_action () const
676 {
677         return ActionManager::get_action (X_("SourcesList"), X_("removeUnusedRegions"));
678 }