2 Copyright (C) 2000-2005 Paul Davis
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.
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.
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.
26 #include "pbd/basename.h"
27 #include "pbd/enumwriter.h"
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"
37 #include "gtkmm2ext/treeutils.h"
38 #include "gtkmm2ext/utils.h"
40 #include "widgets/choice.h"
41 #include "widgets/tooltips.h"
43 #include "audio_clock.h"
46 #include "editing_convert.h"
48 #include "ardour_ui.h"
49 #include "gui_thread.h"
51 #include "region_view.h"
53 #include "editor_drag.h"
54 #include "main_clock.h"
55 #include "ui_config.h"
59 #include "editor_sources.h"
62 using namespace ARDOUR;
63 using namespace ArdourWidgets;
64 using namespace ARDOUR_UI_UTILS;
68 using namespace Editing;
69 using Gtkmm2ext::Keyboard;
77 EditorSources::EditorSources (Editor* e)
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);
88 /* Try to prevent single mouse presses from initiating edits.
89 This relies on a hack in gtktreeview.c:gtk_treeview_button_press()
91 _display.set_data ("mouse-edits-require-mod1", (gpointer) 0x1);
93 _model = TreeStore::create (_columns);
94 _model->set_sort_column (0, SORT_ASCENDING);
97 int bbt_width, date_width, height;
99 Glib::RefPtr<Pango::Layout> layout = _display.create_pango_layout (X_("000|000|000"));
100 Gtkmm2ext::get_pixel_size (layout, bbt_width, height);
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);
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);
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);
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);
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);
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);
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") },
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);
148 _display.set_model (_model);
150 _display.set_headers_visible (true);
151 _display.set_rules_hint ();
153 _display.get_selection()->set_select_function (sigc::mem_fun (*this, &EditorSources::selection_filter));
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_);
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));
166 renderer->property_xalign() = ( ALIGN_RIGHT );
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);
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());
179 /* setup DnD handling */
181 list<TargetEntry> region_list_target_table;
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"));
187 _display.add_drop_targets (region_list_target_table);
188 _display.signal_drag_data_received().connect (sigc::mem_fun(*this, &EditorSources::drag_data_received));
190 _scroller.add (_display);
191 _scroller.set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC);
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));
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));
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);
203 ARDOUR_UI::instance()->primary_clock->mode_changed.connect (sigc::mem_fun(*this, &EditorSources::clock_format_changed));
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());
210 EditorSources::focus_in (GdkEventFocus*)
212 Window* win = dynamic_cast<Window*> (_scroller.get_toplevel ());
215 old_focus = win->get_focus ();
220 /* try to do nothing on focus in (doesn't work, hence selection_count nonsense) */
225 EditorSources::focus_out (GdkEventFocus*)
228 old_focus->grab_focus ();
236 EditorSources::enter_notify (GdkEventCrossing*)
238 /* arm counter so that ::selection_filter() will deny selecting anything for the
239 next two attempts to change selection status.
241 _scroller.grab_focus ();
242 Keyboard::magic_widget_grab_focus ();
247 EditorSources::leave_notify (GdkEventCrossing*)
250 old_focus->grab_focus ();
254 Keyboard::magic_widget_drop_focus ();
259 EditorSources::set_session (ARDOUR::Session* s)
261 SessionHandlePtr::set_session (s);
264 //get all existing sources
265 s->foreach_source (sigc::mem_fun (*this, &EditorSources::add_source));
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());
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());
279 EditorSources::remove_source (boost::shared_ptr<ARDOUR::Source> source)
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];
293 EditorSources::populate_row (TreeModel::Row row, boost::shared_ptr<ARDOUR::Source> source)
295 ENSURE_GUI_THREAD (*this, &ARDOUR_UI::record_state_changed, row, source);
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"));
308 set_color_from_rgba (c, UIConfiguration::instance().color ("region list whole file"));
311 row[_columns.color_] = c;
313 if (source->name()[0] == '/') { // external file
317 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source);
319 str += source->name();
321 str += afs->n_channels(); //ToDo: num channels may be its own column?
324 str += source->name();
328 str = source->name();
331 row[_columns.name] = str;
332 row[_columns.source] = source;
334 if (missing_source) {
335 row[_columns.path] = _("(MISSING) ") + Gtkmm2ext::markup_escape_text (source->name());
338 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource>(source);
340 row[_columns.path] = Gtkmm2ext::markup_escape_text (fs->path());
342 row[_columns.path] = Gtkmm2ext::markup_escape_text (source->name());
346 row[_columns.take_id] = source->take_id();
349 //Natural Position (samples, an invisible column for sorting)
350 row[_columns.natural_s] = source->natural_position();
352 //Natural Position (text representation)
354 snprintf(buf, 16, "--" );
355 if (source->natural_position() > 0) {
356 format_position (source->natural_position(), buf, sizeof (buf));
358 row[_columns.natural_pos] = buf;
362 EditorSources::add_source (boost::shared_ptr<ARDOUR::Source> source)
364 if (!source || !_session ) {
368 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (source);
370 if (!fs || fs->empty()) {
374 TreeModel::Row row = *(_model->append());
375 populate_row (row, source);
380 EditorSources::remove_unused_regions ()
382 /* vector<string> choices;
389 prompt = _("Do you really want to remove unused regions?"
390 "\n(This is destructive and cannot be undone)");
392 choices.push_back (_("No, do nothing."));
393 choices.push_back (_("Yes, remove."));
395 ArdourWidgets::Choice prompter (_("Remove unused regions"), prompt, choices);
397 if (prompter.run () == 1) {
398 _no_redisplay = true;
399 _session->cleanup_regions ();
400 _no_redisplay = false;
407 EditorSources::source_changed (boost::shared_ptr<ARDOUR::Source> source)
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];
414 populate_row(*i, source);
421 EditorSources::selection_changed ()
423 // _editor->_region_selection_change_updates_region_list = false;
425 if (_display.get_selection()->count_selected_rows() > 0) {
428 TreeView::Selection::ListHandle_Path rows = _display.get_selection()->get_selected_rows ();
430 _editor->get_selection().clear_regions ();
432 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
434 if ((iter = _model->get_iter (*i))) {
436 boost::shared_ptr<ARDOUR::Source> source = (*iter)[_columns.source];
439 set<boost::shared_ptr<Region> > regions;
440 RegionFactory::get_regions_using_source ( source, regions );
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);
453 _editor->get_selection().clear_regions ();
456 // _editor->_region_selection_change_updates_region_list = true;
460 EditorSources::clock_format_changed ()
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);
471 EditorSources::format_position (samplepos_t pos, char* buf, size_t bufsize, bool onoff)
473 Timecode::BBT_Time bbt;
474 Timecode::Time timecode;
477 error << string_compose (_("EditorSources::format_position: negative timecode position: %1"), pos) << endmsg;
478 snprintf (buf, bufsize, "invalid");
482 switch (ARDOUR_UI::instance()->primary_clock->mode ()) {
483 case AudioClock::BBT:
484 bbt = _session->tempo_map().bbt_at_sample (pos);
486 snprintf (buf, bufsize, "%03d|%02d|%04d" , bbt.bars, bbt.beats, bbt.ticks);
488 snprintf (buf, bufsize, "(%03d|%02d|%04d)" , bbt.bars, bbt.beats, bbt.ticks);
492 case AudioClock::MinSec:
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();
505 snprintf (buf, bufsize, "%02d:%02d:%06.3f", hrs, mins, secs);
507 snprintf (buf, bufsize, "(%02d:%02d:%06.3f)", hrs, mins, secs);
511 case AudioClock::Seconds:
513 snprintf (buf, bufsize, "%.1f", pos / (float)_session->sample_rate());
515 snprintf (buf, bufsize, "(%.1f)", pos / (float)_session->sample_rate());
519 case AudioClock::Samples:
521 snprintf (buf, bufsize, "%" PRId64, pos);
523 snprintf (buf, bufsize, "(%" PRId64 ")", pos);
527 case AudioClock::Timecode:
529 _session->timecode_time (pos, timecode);
531 snprintf (buf, bufsize, "%02d:%02d:%02d:%02d", timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
533 snprintf (buf, bufsize, "(%02d:%02d:%02d:%02d)", timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
540 EditorSources::show_context_menu (int button, int time)
546 EditorSources::key_press (GdkEventKey* ev)
552 EditorSources::button_press (GdkEventButton *ev)
554 boost::shared_ptr<ARDOUR::Source> source;
556 TreeModel::Path path;
557 TreeViewColumn* column;
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];
567 if (Keyboard::is_context_menu_event (ev)) {
568 show_context_menu (ev->button, ev->time);
576 EditorSources::selection_mapover (sigc::slot<void,boost::shared_ptr<Region> > sl)
583 EditorSources::drag_data_received (const RefPtr<Gdk::DragContext>& context,
585 const SelectionData& data,
586 guint info, guint time)
592 EditorSources::selection_filter (const RefPtr<TreeModel>& model, const TreeModel::Path& path, bool already_selected)
597 /** @return Region that has been dragged out of the list, or 0 */
598 boost::shared_ptr<ARDOUR::Source>
599 EditorSources::get_dragged_source ()
601 list<boost::shared_ptr<ARDOUR::Source> > sources;
603 _display.get_object_drag_data (sources, &source);
605 if (sources.empty()) {
606 return boost::shared_ptr<ARDOUR::Source> ();
609 assert (sources.size() == 1);
610 return sources.front ();
614 EditorSources::clear ()
616 _display.set_model (Glib::RefPtr<Gtk::TreeStore> (0));
618 _display.set_model (_model);
621 boost::shared_ptr<ARDOUR::Source>
622 EditorSources::get_single_selection ()
624 Glib::RefPtr<TreeSelection> selected = _display.get_selection();
626 if (selected->count_selected_rows() != 1) {
627 return boost::shared_ptr<ARDOUR::Source> ();
630 TreeView::Selection::ListHandle_Path rows = selected->get_selected_rows ();
632 /* only one row selected, so rows.begin() is it */
634 TreeIter iter = _model->get_iter (*rows.begin());
637 return boost::shared_ptr<ARDOUR::Source> ();
640 return (*iter)[_columns.source];
644 EditorSources::freeze_tree_model ()
646 _display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
647 _model->set_sort_column (-2, SORT_ASCENDING); //Disable sorting to gain performance
651 EditorSources::thaw_tree_model (){
653 _model->set_sort_column (0, SORT_ASCENDING); // renabale sorting
654 _display.set_model (_model);
658 EditorSources::get_state () const
660 XMLNode* node = new XMLNode (X_("SourcesList"));
662 //TODO: save sort state?
668 EditorSources::set_state (const XMLNode & node)
670 bool changed = false;
675 EditorSources::remove_unused_regions_action () const
677 return ActionManager::get_action (X_("SourcesList"), X_("removeUnusedRegions"));