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/audiofilesource.h"
31 #include "ardour/silentfilesource.h"
32 #include "ardour/region_factory.h"
33 #include "ardour/session.h"
34 #include "ardour/profile.h"
36 #include "gtkmm2ext/treeutils.h"
37 #include "gtkmm2ext/utils.h"
39 #include "widgets/choice.h"
40 #include "widgets/tooltips.h"
42 #include "audio_clock.h"
45 #include "editing_convert.h"
47 #include "ardour_ui.h"
48 #include "gui_thread.h"
50 #include "region_view.h"
52 #include "editor_regions.h"
53 #include "editor_drag.h"
54 #include "main_clock.h"
55 #include "ui_config.h"
60 using namespace ARDOUR;
61 using namespace ArdourWidgets;
62 using namespace ARDOUR_UI_UTILS;
66 using namespace Editing;
67 using Gtkmm2ext::Keyboard;
75 EditorRegions::EditorRegions (Editor* e)
80 , _show_automatic_regions (true)
81 , ignore_region_list_selection_change (false)
82 , ignore_selected_region_change (false)
83 , _no_redisplay (false)
84 , _sort_type ((Editing::RegionListSortType) 0)
87 _display.set_size_request (100, -1);
88 _display.set_rules_hint (true);
89 _display.set_name ("EditGroupList");
90 _display.set_fixed_height_mode (true);
92 /* Try to prevent single mouse presses from initiating edits.
93 This relies on a hack in gtktreeview.c:gtk_treeview_button_press()
95 _display.set_data ("mouse-edits-require-mod1", (gpointer) 0x1);
97 _model = TreeStore::create (_columns);
98 _model->set_sort_func (0, sigc::mem_fun (*this, &EditorRegions::sorter));
99 _model->set_sort_column (0, SORT_ASCENDING);
102 int bbt_width, check_width, height;
104 Glib::RefPtr<Pango::Layout> layout = _display.create_pango_layout (X_("000|000|000"));
105 Gtkmm2ext::get_pixel_size (layout, bbt_width, height);
109 TreeViewColumn* col_name = manage (new TreeViewColumn ("", _columns.name));
110 col_name->set_fixed_width (120);
111 col_name->set_sizing (TREE_VIEW_COLUMN_FIXED);
112 TreeViewColumn* col_position = manage (new TreeViewColumn ("", _columns.position));
113 col_position->set_fixed_width (bbt_width);
114 col_position->set_sizing (TREE_VIEW_COLUMN_FIXED);
115 TreeViewColumn* col_end = manage (new TreeViewColumn ("", _columns.end));
116 col_end->set_fixed_width (bbt_width);
117 col_end->set_sizing (TREE_VIEW_COLUMN_FIXED);
118 TreeViewColumn* col_length = manage (new TreeViewColumn ("", _columns.length));
119 col_length->set_fixed_width (bbt_width);
120 col_length->set_sizing (TREE_VIEW_COLUMN_FIXED);
121 TreeViewColumn* col_sync = manage (new TreeViewColumn ("", _columns.sync));
122 col_sync->set_fixed_width (bbt_width);
123 col_sync->set_sizing (TREE_VIEW_COLUMN_FIXED);
124 TreeViewColumn* col_fadein = manage (new TreeViewColumn ("", _columns.fadein));
125 col_fadein->set_fixed_width (bbt_width);
126 col_fadein->set_sizing (TREE_VIEW_COLUMN_FIXED);
127 TreeViewColumn* col_fadeout = manage (new TreeViewColumn ("", _columns.fadeout));
128 col_fadeout->set_fixed_width (bbt_width);
129 col_fadeout->set_sizing (TREE_VIEW_COLUMN_FIXED);
130 TreeViewColumn* col_locked = manage (new TreeViewColumn ("", _columns.locked));
131 col_locked->set_fixed_width (check_width);
132 col_locked->set_sizing (TREE_VIEW_COLUMN_FIXED);
133 TreeViewColumn* col_glued = manage (new TreeViewColumn ("", _columns.glued));
134 col_glued->set_fixed_width (check_width);
135 col_glued->set_sizing (TREE_VIEW_COLUMN_FIXED);
136 TreeViewColumn* col_muted = manage (new TreeViewColumn ("", _columns.muted));
137 col_muted->set_fixed_width (check_width);
138 col_muted->set_sizing (TREE_VIEW_COLUMN_FIXED);
139 TreeViewColumn* col_opaque = manage (new TreeViewColumn ("", _columns.opaque));
140 col_opaque->set_fixed_width (check_width);
141 col_opaque->set_sizing (TREE_VIEW_COLUMN_FIXED);
143 _display.append_column (*col_name);
144 _display.append_column (*col_position);
145 _display.append_column (*col_end);
146 _display.append_column (*col_length);
147 _display.append_column (*col_sync);
148 _display.append_column (*col_fadein);
149 _display.append_column (*col_fadeout);
150 _display.append_column (*col_locked);
151 _display.append_column (*col_glued);
152 _display.append_column (*col_muted);
153 _display.append_column (*col_opaque);
159 { 0, _("Region"), _("Region name, with number of channels in []'s") },
160 { 1, _("Position"), _("Position of start of region") },
161 { 2, _("End"), _("Position of end of region") },
162 { 3, _("Length"), _("Length of the region") },
163 { 4, _("Sync"), _("Position of region sync point, relative to start of the region") },
164 { 5, _("Fade In"), _("Length of region fade-in (units: secondary clock), () if disabled") },
165 { 6, _("Fade Out"), _("Length of region fade-out (units: secondary clock), () if disabled") },
166 { 7, S_("Lock|L"), _("Region position locked?") },
167 { 8, S_("Gain|G"), _("Region position glued to Bars|Beats time?") },
168 { 9, S_("Mute|M"), _("Region muted?") },
169 { 10, S_("Opaque|O"), _("Region opaque (blocks regions below it from being heard)?") },
173 for (int i = 0; ci[i].index >= 0; ++i) {
174 col = _display.get_column (ci[i].index);
175 l = manage (new Label (ci[i].label));
176 set_tooltip (*l, ci[i].tooltip);
177 col->set_widget (*l);
180 if (ci[i].index > 6) {
181 col->set_expand (false);
182 col->set_alignment (ALIGN_CENTER);
185 _display.set_model (_model);
187 _display.set_headers_visible (true);
188 _display.set_rules_hint ();
190 /* show path as the row tooltip */
191 _display.set_tooltip_column (14); /* path */
193 CellRendererText* region_name_cell = dynamic_cast<CellRendererText*>(_display.get_column_cell_renderer (0));
194 region_name_cell->property_editable() = true;
195 region_name_cell->signal_edited().connect (sigc::mem_fun (*this, &EditorRegions::name_edit));
196 region_name_cell->signal_editing_started().connect (sigc::mem_fun (*this, &EditorRegions::name_editing_started));
198 _display.get_selection()->set_select_function (sigc::mem_fun (*this, &EditorRegions::selection_filter));
200 TreeViewColumn* tv_col = _display.get_column(0);
201 CellRendererText* renderer = dynamic_cast<CellRendererText*>(_display.get_column_cell_renderer (0));
202 tv_col->add_attribute(renderer->property_text(), _columns.name);
203 tv_col->add_attribute(renderer->property_foreground_gdk(), _columns.color_);
204 tv_col->set_expand (true);
206 CellRendererToggle* locked_cell = dynamic_cast<CellRendererToggle*> (_display.get_column_cell_renderer (7));
207 locked_cell->property_activatable() = true;
208 locked_cell->signal_toggled().connect (sigc::mem_fun (*this, &EditorRegions::locked_changed));
210 TreeViewColumn* locked_col = _display.get_column (7);
211 locked_col->add_attribute (locked_cell->property_visible(), _columns.property_toggles_visible);
213 CellRendererToggle* glued_cell = dynamic_cast<CellRendererToggle*> (_display.get_column_cell_renderer (8));
214 glued_cell->property_activatable() = true;
215 glued_cell->signal_toggled().connect (sigc::mem_fun (*this, &EditorRegions::glued_changed));
217 TreeViewColumn* glued_col = _display.get_column (8);
218 glued_col->add_attribute (glued_cell->property_visible(), _columns.property_toggles_visible);
220 CellRendererToggle* muted_cell = dynamic_cast<CellRendererToggle*> (_display.get_column_cell_renderer (9));
221 muted_cell->property_activatable() = true;
222 muted_cell->signal_toggled().connect (sigc::mem_fun (*this, &EditorRegions::muted_changed));
224 TreeViewColumn* muted_col = _display.get_column (9);
225 muted_col->add_attribute (muted_cell->property_visible(), _columns.property_toggles_visible);
227 CellRendererToggle* opaque_cell = dynamic_cast<CellRendererToggle*> (_display.get_column_cell_renderer (10));
228 opaque_cell->property_activatable() = true;
229 opaque_cell->signal_toggled().connect (sigc::mem_fun (*this, &EditorRegions::opaque_changed));
231 TreeViewColumn* opaque_col = _display.get_column (10);
232 opaque_col->add_attribute (opaque_cell->property_visible(), _columns.property_toggles_visible);
234 _display.get_selection()->set_mode (SELECTION_MULTIPLE);
235 _display.add_object_drag (_columns.region.index(), "regions");
236 _display.set_drag_column (_columns.name.index());
238 /* setup DnD handling */
240 list<TargetEntry> region_list_target_table;
242 region_list_target_table.push_back (TargetEntry ("text/plain"));
243 region_list_target_table.push_back (TargetEntry ("text/uri-list"));
244 region_list_target_table.push_back (TargetEntry ("application/x-rootwin-drop"));
246 _display.add_drop_targets (region_list_target_table);
247 _display.signal_drag_data_received().connect (sigc::mem_fun(*this, &EditorRegions::drag_data_received));
249 _scroller.add (_display);
250 _scroller.set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC);
252 _display.signal_button_press_event().connect (sigc::mem_fun(*this, &EditorRegions::button_press), false);
253 _change_connection = _display.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &EditorRegions::selection_changed));
255 _scroller.signal_key_press_event().connect (sigc::mem_fun(*this, &EditorRegions::key_press), false);
256 _scroller.signal_focus_in_event().connect (sigc::mem_fun (*this, &EditorRegions::focus_in), false);
257 _scroller.signal_focus_out_event().connect (sigc::mem_fun (*this, &EditorRegions::focus_out));
259 _display.signal_enter_notify_event().connect (sigc::mem_fun (*this, &EditorRegions::enter_notify), false);
260 _display.signal_leave_notify_event().connect (sigc::mem_fun (*this, &EditorRegions::leave_notify), false);
262 // _display.signal_popup_menu().connect (sigc::bind (sigc::mem_fun (*this, &Editor::show__display_context_menu), 1, 0));
264 //ARDOUR_UI::instance()->secondary_clock.mode_changed.connect (sigc::mem_fun(*this, &Editor::redisplay_regions));
265 ARDOUR_UI::instance()->secondary_clock->mode_changed.connect (sigc::mem_fun(*this, &EditorRegions::update_all_rows));
266 ARDOUR::Region::RegionPropertyChanged.connect (region_property_connection, MISSING_INVALIDATOR, boost::bind (&EditorRegions::region_changed, this, _1, _2), gui_context());
267 ARDOUR::RegionFactory::CheckNewRegion.connect (check_new_region_connection, MISSING_INVALIDATOR, boost::bind (&EditorRegions::add_region, this, _1), gui_context());
269 e->EditorFreeze.connect (editor_freeze_connection, MISSING_INVALIDATOR, boost::bind (&EditorRegions::freeze_tree_model, this), gui_context());
270 e->EditorThaw.connect (editor_thaw_connection, MISSING_INVALIDATOR, boost::bind (&EditorRegions::thaw_tree_model, this), gui_context());
274 EditorRegions::focus_in (GdkEventFocus*)
276 Window* win = dynamic_cast<Window*> (_scroller.get_toplevel ());
279 old_focus = win->get_focus ();
286 /* try to do nothing on focus in (doesn't work, hence selection_count nonsense) */
291 EditorRegions::focus_out (GdkEventFocus*)
294 old_focus->grab_focus ();
304 EditorRegions::enter_notify (GdkEventCrossing*)
310 /* arm counter so that ::selection_filter() will deny selecting anything for the
311 next two attempts to change selection status.
313 _scroller.grab_focus ();
314 Keyboard::magic_widget_grab_focus ();
319 EditorRegions::leave_notify (GdkEventCrossing*)
322 old_focus->grab_focus ();
326 Keyboard::magic_widget_drop_focus ();
331 EditorRegions::set_session (ARDOUR::Session* s)
333 SessionHandlePtr::set_session (s);
338 EditorRegions::add_region (boost::shared_ptr<Region> region)
340 if (!region || !_session) {
347 bool missing_source = boost::dynamic_pointer_cast<SilentFileSource>(region->source()) != NULL;
349 if (!_show_automatic_regions && region->automatic()) {
353 if (region->hidden()) {
355 TreeModel::iterator iter = _model->get_iter ("0");
356 TreeModel::Row parent;
359 parent = *(_model->append());
360 parent[_columns.name] = _("Hidden");
361 boost::shared_ptr<Region> proxy = parent[_columns.region];
364 string s = (*iter)[_columns.name];
365 if (s != _("Hidden")) {
366 parent = *(_model->insert(iter));
367 parent[_columns.name] = _("Hidden");
368 boost::shared_ptr<Region> proxy = parent[_columns.region];
375 row = *(_model->append (parent.children()));
377 } else if (region->whole_file()) {
379 TreeModel::iterator i;
380 TreeModel::Children rows = _model->children();
382 for (i = rows.begin(); i != rows.end(); ++i) {
383 boost::shared_ptr<Region> rr = (*i)[_columns.region];
385 if (rr && region->region_list_equivalent (rr)) {
390 row = *(_model->append());
392 if (missing_source) {
393 // c.set_rgb(65535,0,0); // FIXME: error color from style
394 set_color_from_rgba (c, UIConfiguration::instance().color ("region list missing source"));
396 } else if (region->automatic()){
397 // c.set_rgb(0,65535,0); // FIXME: error color from style
398 set_color_from_rgba (c, UIConfiguration::instance().color ("region list automatic"));
401 set_color_from_rgba (c, UIConfiguration::instance().color ("region list whole file"));
404 row[_columns.color_] = c;
406 if (region->source()->name()[0] == '/') { // external file
408 if (region->whole_file()) {
410 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(region->source());
414 str = region_name_from_path (afs->path(), region->n_channels() > 1);
416 str += region->source()->name();
420 str = region->name();
424 str = region->name();
427 populate_row_name (region, row);
428 row[_columns.region] = region;
429 row[_columns.property_toggles_visible] = false;
431 if (missing_source) {
432 row[_columns.path] = _("(MISSING) ") + Gtkmm2ext::markup_escape_text (region->source()->name());
435 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource>(region->source());
437 row[_columns.path] = Gtkmm2ext::markup_escape_text (fs->path());
439 row[_columns.path] = Gtkmm2ext::markup_escape_text (region->source()->name());
443 region_row_map.insert(pair<boost::shared_ptr<ARDOUR::Region>, Gtk::TreeModel::RowReference>(region, TreeRowReference(_model, TreePath (row))) );
444 parent_regions_sources_map.insert(pair<string, Gtk::TreeModel::RowReference>(region->source_string(), TreeRowReference(_model, TreePath (row))) );
449 // find parent node, add as new child
450 TreeModel::iterator i;
452 boost::unordered_map<string, Gtk::TreeModel::RowReference>::iterator it;
454 it = parent_regions_sources_map.find (region->source_string());
456 if (it != parent_regions_sources_map.end()){
458 TreeModel::iterator j = _model->get_iter ((*it).second.get_path());
460 TreeModel::iterator ii;
461 TreeModel::Children subrows = (*j).children();
463 /* XXXX: should we be accounting for all regions? */
465 for (ii = subrows.begin(); ii != subrows.end(); ++ii) {
466 boost::shared_ptr<Region> rr = (*ii)[_columns.region];
468 if (region->region_list_equivalent (rr)) {
474 row = *(_model->insert (subrows.end()));
477 row = *(_model->append());
480 row[_columns.property_toggles_visible] = true;
483 row[_columns.region] = region;
485 region_row_map.insert(pair<boost::shared_ptr<ARDOUR::Region>, Gtk::TreeModel::RowReference>(region, TreeRowReference(_model, TreePath (row))) );
487 populate_row(region, (*row), pc);
491 EditorRegions::remove_unused_regions ()
493 vector<string> choices;
500 prompt = _("Do you really want to remove unused regions?"
501 "\n(This is destructive and cannot be undone)");
503 choices.push_back (_("No, do nothing."));
504 choices.push_back (_("Yes, remove."));
506 ArdourWidgets::Choice prompter (_("Remove unused regions"), prompt, choices);
508 if (prompter.run () == 1) {
509 _no_redisplay = true;
510 _session->cleanup_regions ();
511 _no_redisplay = false;
517 EditorRegions::region_changed (boost::shared_ptr<Region> r, const PropertyChange& what_changed)
519 PropertyChange our_interests;
521 our_interests.add (ARDOUR::Properties::name);
522 our_interests.add (ARDOUR::Properties::position);
523 our_interests.add (ARDOUR::Properties::length);
524 our_interests.add (ARDOUR::Properties::start);
525 our_interests.add (ARDOUR::Properties::sync_position);
526 our_interests.add (ARDOUR::Properties::locked);
527 our_interests.add (ARDOUR::Properties::position_lock_style);
528 our_interests.add (ARDOUR::Properties::muted);
529 our_interests.add (ARDOUR::Properties::opaque);
530 our_interests.add (ARDOUR::Properties::fade_in);
531 our_interests.add (ARDOUR::Properties::fade_out);
532 our_interests.add (ARDOUR::Properties::fade_in_active);
533 our_interests.add (ARDOUR::Properties::fade_out_active);
535 if (what_changed.contains (our_interests)) {
538 TreeModel::iterator j = _model->get_iter (last_row.get_path());
539 boost::shared_ptr<Region> c = (*j)[_columns.region];
542 populate_row (r, (*j), what_changed);
544 if (what_changed.contains (ARDOUR::Properties::hidden)) {
552 RegionRowMap::iterator it;
554 it = region_row_map.find (r);
556 if (it != region_row_map.end()){
558 TreeModel::iterator j = _model->get_iter ((*it).second.get_path());
559 boost::shared_ptr<Region> c = (*j)[_columns.region];
562 populate_row (r, (*j), what_changed);
564 if (what_changed.contains (ARDOUR::Properties::hidden)) {
573 if (what_changed.contains (ARDOUR::Properties::hidden)) {
579 EditorRegions::selection_changed ()
581 if (ignore_region_list_selection_change) {
585 _editor->_region_selection_change_updates_region_list = false;
587 if (_display.get_selection()->count_selected_rows() > 0) {
590 TreeView::Selection::ListHandle_Path rows = _display.get_selection()->get_selected_rows ();
592 _editor->get_selection().clear_regions ();
594 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
596 if ((iter = _model->get_iter (*i))) {
598 boost::shared_ptr<Region> region = (*iter)[_columns.region];
600 // they could have clicked on a row that is just a placeholder, like "Hidden"
601 // although that is not allowed by our selection filter. check it anyway
602 // since we need a region ptr.
606 _change_connection.block (true);
607 _editor->set_selected_regionview_from_region_list (region, Selection::Add);
608 _change_connection.block (false);
614 _editor->get_selection().clear_regions ();
617 _editor->_region_selection_change_updates_region_list = true;
621 EditorRegions::set_selected (RegionSelection& regions)
623 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
625 boost::shared_ptr<Region> r ((*i)->region());
627 RegionRowMap::iterator it;
629 it = region_row_map.find (r);
631 if (it != region_row_map.end()){
632 TreeModel::iterator j = _model->get_iter ((*it).second.get_path());
633 _display.get_selection()->select(*j);
639 EditorRegions::redisplay ()
641 if (_no_redisplay || !_session) {
645 bool tree_expanded = false;
647 /* If the list was expanded prior to rebuilding, expand it again afterwards */
648 if (toggle_full_action()->get_active()) {
649 tree_expanded = true;
652 _display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
654 _model->set_sort_column (-2, SORT_ASCENDING); //Disable sorting to gain performance
657 region_row_map.clear();
658 parent_regions_sources_map.clear();
660 /* now add everything we have, via a temporary list used to help with sorting */
662 const RegionFactory::RegionMap& regions (RegionFactory::regions());
664 for (RegionFactory::RegionMap::const_iterator i = regions.begin(); i != regions.end(); ++i) {
666 if ( i->second->whole_file()) {
667 /* add automatic regions first so that children can find their parents as we add them */
668 add_region (i->second);
672 tmp_region_list.push_front (i->second);
675 for (list<boost::shared_ptr<Region> >::iterator r = tmp_region_list.begin(); r != tmp_region_list.end(); ++r) {
679 _model->set_sort_column (0, SORT_ASCENDING); // renabale sorting
680 _display.set_model (_model);
682 tmp_region_list.clear();
685 _display.expand_all();
690 EditorRegions::update_row (boost::shared_ptr<Region> region)
692 if (!region || !_session) {
696 RegionRowMap::iterator it;
698 it = region_row_map.find (region);
700 if (it != region_row_map.end()){
702 TreeModel::iterator j = _model->get_iter ((*it).second.get_path());
703 populate_row(region, (*j), c);
708 EditorRegions::update_all_rows ()
714 RegionRowMap::iterator i;
716 for (i = region_row_map.begin(); i != region_row_map.end(); ++i) {
718 TreeModel::iterator j = _model->get_iter ((*i).second.get_path());
720 boost::shared_ptr<Region> region = (*j)[_columns.region];
722 if (!region->automatic()) {
724 populate_row(region, (*j), c);
730 EditorRegions::format_position (framepos_t pos, char* buf, size_t bufsize, bool onoff)
732 Timecode::BBT_Time bbt;
733 Timecode::Time timecode;
736 error << string_compose (_("EditorRegions::format_position: negative timecode position: %1"), pos) << endmsg;
737 snprintf (buf, bufsize, "invalid");
741 switch (ARDOUR_UI::instance()->secondary_clock->mode ()) {
742 case AudioClock::BBT:
743 bbt = _session->tempo_map().bbt_at_frame (pos);
745 snprintf (buf, bufsize, "%03d|%02d|%04d" , bbt.bars, bbt.beats, bbt.ticks);
747 snprintf (buf, bufsize, "(%03d|%02d|%04d)" , bbt.bars, bbt.beats, bbt.ticks);
751 case AudioClock::MinSec:
758 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
759 left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
760 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
761 left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
762 secs = left / (float) _session->frame_rate();
764 snprintf (buf, bufsize, "%02d:%02d:%06.3f", hrs, mins, secs);
766 snprintf (buf, bufsize, "(%02d:%02d:%06.3f)", hrs, mins, secs);
770 case AudioClock::Frames:
772 snprintf (buf, bufsize, "%" PRId64, pos);
774 snprintf (buf, bufsize, "(%" PRId64 ")", pos);
778 case AudioClock::Timecode:
780 _session->timecode_time (pos, timecode);
782 snprintf (buf, bufsize, "%02d:%02d:%02d:%02d", timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
784 snprintf (buf, bufsize, "(%02d:%02d:%02d:%02d)", timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
791 EditorRegions::populate_row (boost::shared_ptr<Region> region, TreeModel::Row const &row, PBD::PropertyChange const &what_changed)
793 boost::shared_ptr<AudioRegion> audioregion = boost::dynamic_pointer_cast<AudioRegion>(region);
794 //uint32_t used = _session->playlists->region_use_count (region);
795 /* Presently a region is only used once so let's save on the sequential scan to determine use count */
799 const bool all = what_changed == c;
801 if (all || what_changed.contains (Properties::position)) {
802 populate_row_position (region, row, used);
804 if (all || what_changed.contains (Properties::start) || what_changed.contains (Properties::sync_position)) {
805 populate_row_sync (region, row, used);
807 if (all || what_changed.contains (Properties::fade_in)) {
808 populate_row_fade_in (region, row, used, audioregion);
810 if (all || what_changed.contains (Properties::fade_out)) {
811 populate_row_fade_out (region, row, used, audioregion);
813 if (all || what_changed.contains (Properties::locked)) {
814 populate_row_locked (region, row, used);
816 if (all || what_changed.contains (Properties::position_lock_style)) {
817 populate_row_glued (region, row, used);
819 if (all || what_changed.contains (Properties::muted)) {
820 populate_row_muted (region, row, used);
822 if (all || what_changed.contains (Properties::opaque)) {
823 populate_row_opaque (region, row, used);
825 if (all || what_changed.contains (Properties::length)) {
826 populate_row_end (region, row, used);
827 populate_row_length (region, row);
830 populate_row_source (region, row);
832 if (all || what_changed.contains (Properties::name)) {
833 populate_row_name (region, row);
836 populate_row_used (region, row, used);
841 if (audioRegion && fades_in_seconds) {
847 left = audioRegion->fade_in()->back()->when;
848 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
849 left -= (framepos_t) floor (mins * _session->frame_rate() * 60.0f);
850 millisecs = (int) floor ((left * 1000.0f) / _session->frame_rate());
852 if (audioRegion->fade_in()->back()->when >= _session->frame_rate()) {
853 sprintf (fadein_str, "%01dM %01dmS", mins, millisecs);
855 sprintf (fadein_str, "%01dmS", millisecs);
858 left = audioRegion->fade_out()->back()->when;
859 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
860 left -= (framepos_t) floor (mins * _session->frame_rate() * 60.0f);
861 millisecs = (int) floor ((left * 1000.0f) / _session->frame_rate());
863 if (audioRegion->fade_out()->back()->when >= _session->frame_rate()) {
864 sprintf (fadeout_str, "%01dM %01dmS", mins, millisecs);
866 sprintf (fadeout_str, "%01dmS", millisecs);
872 EditorRegions::populate_row_used (boost::shared_ptr<Region>, TreeModel::Row const& row, uint32_t used)
875 snprintf (buf, sizeof (buf), "%4d" , used);
876 row[_columns.used] = buf;
880 EditorRegions::populate_row_length (boost::shared_ptr<Region> region, TreeModel::Row const &row)
884 if (ARDOUR_UI::instance()->secondary_clock->mode () == AudioClock::BBT) {
885 TempoMap& map (_session->tempo_map());
886 Timecode::BBT_Time bbt = map.bbt_at_beat (map.beat_at_frame (region->last_frame()) - map.beat_at_frame (region->first_frame()));
887 snprintf (buf, sizeof (buf), "%03d|%02d|%04d" , bbt.bars, bbt.beats, bbt.ticks);
889 format_position (region->length(), buf, sizeof (buf));
892 row[_columns.length] = buf;
896 EditorRegions::populate_row_end (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
898 if (region->whole_file()) {
899 row[_columns.end] = "";
900 } else if (used > 1) {
901 row[_columns.end] = _("Mult.");
902 } else if (region->last_frame() >= region->first_frame()) {
904 format_position (region->last_frame(), buf, sizeof (buf));
905 row[_columns.end] = buf;
907 row[_columns.end] = "empty";
912 EditorRegions::populate_row_position (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
914 if (region->whole_file()) {
915 row[_columns.position] = "";
916 } else if (used > 1) {
917 row[_columns.position] = _("Mult.");
920 format_position (region->position(), buf, sizeof (buf));
921 row[_columns.position] = buf;
926 EditorRegions::populate_row_sync (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
928 if (region->whole_file()) {
929 row[_columns.sync] = "";
930 } else if (used > 1) {
931 row[_columns.sync] = _("Mult."); /* translators: a short phrase for "multiple" as in "many" */
933 if (region->sync_position() == region->position()) {
934 row[_columns.sync] = _("Start");
935 } else if (region->sync_position() == (region->last_frame())) {
936 row[_columns.sync] = _("End");
939 format_position (region->sync_position(), buf, sizeof (buf));
940 row[_columns.sync] = buf;
946 EditorRegions::populate_row_fade_in (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used, boost::shared_ptr<AudioRegion> audioregion)
948 if (!audioregion || region->whole_file()) {
949 row[_columns.fadein] = "";
952 row[_columns.fadein] = _("Multiple");
955 format_position (audioregion->fade_in()->back()->when, buf, sizeof (buf), audioregion->fade_in_active());
956 row[_columns.fadein] = buf;
962 EditorRegions::populate_row_fade_out (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used, boost::shared_ptr<AudioRegion> audioregion)
964 if (!audioregion || region->whole_file()) {
965 row[_columns.fadeout] = "";
968 row[_columns.fadeout] = _("Multiple");
971 format_position (audioregion->fade_out()->back()->when, buf, sizeof (buf), audioregion->fade_out_active());
972 row[_columns.fadeout] = buf;
978 EditorRegions::populate_row_locked (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
980 if (region->whole_file()) {
981 row[_columns.locked] = false;
982 } else if (used > 1) {
983 row[_columns.locked] = false;
985 row[_columns.locked] = region->locked();
990 EditorRegions::populate_row_glued (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
992 if (region->whole_file() || used > 1) {
993 row[_columns.glued] = false;
995 if (region->position_lock_style() == MusicTime) {
996 row[_columns.glued] = true;
998 row[_columns.glued] = false;
1004 EditorRegions::populate_row_muted (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
1006 if (region->whole_file() || used > 1) {
1007 row[_columns.muted] = false;
1009 row[_columns.muted] = region->muted();
1014 EditorRegions::populate_row_opaque (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
1016 if (region->whole_file() || used > 1) {
1017 row[_columns.opaque] = false;
1019 row[_columns.opaque] = region->opaque();
1024 EditorRegions::populate_row_name (boost::shared_ptr<Region> region, TreeModel::Row const &row)
1026 if (region->n_channels() > 1) {
1027 row[_columns.name] = string_compose("%1 [%2]", Gtkmm2ext::markup_escape_text (region->name()), region->n_channels());
1029 row[_columns.name] = Gtkmm2ext::markup_escape_text (region->name());
1034 EditorRegions::populate_row_source (boost::shared_ptr<Region> region, TreeModel::Row const &row)
1036 if (boost::dynamic_pointer_cast<SilentFileSource>(region->source())) {
1037 row[_columns.path] = _("MISSING ") + Gtkmm2ext::markup_escape_text (region->source()->name());
1039 row[_columns.path] = Gtkmm2ext::markup_escape_text (region->source()->name());
1044 EditorRegions::toggle_show_auto_regions ()
1046 _show_automatic_regions = toggle_show_auto_regions_action()->get_active();
1051 EditorRegions::toggle_full ()
1053 set_full (toggle_full_action()->get_active ());
1057 EditorRegions::set_full (bool f)
1060 _display.expand_all ();
1063 _display.collapse_all ();
1069 EditorRegions::show_context_menu (int button, int time)
1072 _menu = dynamic_cast<Menu*> (ActionManager::get_widget ("/RegionListMenu"));
1075 if (_display.get_selection()->count_selected_rows() > 0) {
1076 ActionManager::set_sensitive (ActionManager::region_list_selection_sensitive_actions, true);
1078 ActionManager::set_sensitive (ActionManager::region_list_selection_sensitive_actions, false);
1081 /* Enable the "Show" option if any selected regions are hidden, and vice versa for "Hide" */
1083 bool have_shown = false;
1084 bool have_hidden = false;
1086 TreeView::Selection::ListHandle_Path rows = _display.get_selection()->get_selected_rows ();
1087 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
1088 TreeIter t = _model->get_iter (*i);
1089 boost::shared_ptr<Region> r = (*t)[_columns.region];
1099 hide_action()->set_sensitive (have_shown);
1100 show_action()->set_sensitive (have_hidden);
1102 _menu->popup (button, time);
1106 EditorRegions::key_press (GdkEventKey* ev)
1108 TreeViewColumn *col;
1110 switch (ev->keyval) {
1112 case GDK_ISO_Left_Tab:
1114 if (name_editable) {
1115 name_editable->editing_done ();
1119 col = _display.get_column (0); // select&focus on name column
1121 if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
1122 treeview_select_previous (_display, _model, col);
1124 treeview_select_next (_display, _model, col);
1138 EditorRegions::button_press (GdkEventButton *ev)
1140 boost::shared_ptr<Region> region;
1142 TreeModel::Path path;
1143 TreeViewColumn* column;
1147 if (_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
1148 if ((iter = _model->get_iter (path))) {
1149 region = (*iter)[_columns.region];
1153 if (Keyboard::is_context_menu_event (ev)) {
1154 show_context_menu (ev->button, ev->time);
1158 if (region != 0 && Keyboard::is_button2_event (ev)) {
1159 // start/stop audition
1160 if (!Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1161 _editor->consider_auditioning (region);
1170 EditorRegions::sorter (TreeModel::iterator a, TreeModel::iterator b)
1174 boost::shared_ptr<Region> r1 = (*a)[_columns.region];
1175 boost::shared_ptr<Region> r2 = (*b)[_columns.region];
1177 /* handle rows without regions, like "Hidden" */
1187 boost::shared_ptr<AudioRegion> region1 = boost::dynamic_pointer_cast<AudioRegion> (r1);
1188 boost::shared_ptr<AudioRegion> region2 = boost::dynamic_pointer_cast<AudioRegion> (r2);
1190 if (region1 == 0 || region2 == 0) {
1193 switch (_sort_type) {
1195 s1 = (*a)[_columns.name];
1196 s2 = (*b)[_columns.name];
1197 return (s1.compare (s2));
1203 switch (_sort_type) {
1205 cmp = region1->name().compare(region2->name());
1209 cmp = region1->length() - region2->length();
1213 cmp = region1->position() - region2->position();
1217 cmp = region1->source()->timestamp() - region2->source()->timestamp();
1221 cmp = region1->start() - region2->start();
1225 // cerr << "Compare " << (region1->start() + region1->length()) << " and " << (region2->start() + region2->length()) << endl;
1226 cmp = (region1->start() + region1->length()) - (region2->start() + region2->length());
1229 case BySourceFileName:
1230 cmp = region1->source()->name().compare(region2->source()->name());
1233 case BySourceFileLength:
1234 cmp = region1->source_length(0) - region2->source_length(0);
1237 case BySourceFileCreationDate:
1238 cmp = region1->source()->timestamp() - region2->source()->timestamp();
1241 case BySourceFileFS:
1242 if (region1->source()->name() == region2->source()->name()) {
1243 cmp = region1->name().compare(region2->name());
1245 cmp = region1->source()->name().compare(region2->source()->name());
1250 // cerr << "Comparison on " << enum_2_string (_sort_type) << " gives " << cmp << endl;
1254 } else if (cmp > 0) {
1262 EditorRegions::reset_sort_type (RegionListSortType type, bool force)
1264 if (type != _sort_type || force) {
1266 _model->set_sort_func (0, (sigc::mem_fun (*this, &EditorRegions::sorter)));
1271 EditorRegions::reset_sort_direction (bool up)
1273 _model->set_sort_column (0, up ? SORT_ASCENDING : SORT_DESCENDING);
1277 EditorRegions::selection_mapover (sigc::slot<void,boost::shared_ptr<Region> > sl)
1279 Glib::RefPtr<TreeSelection> selection = _display.get_selection();
1280 TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
1281 TreeView::Selection::ListHandle_Path::iterator i = rows.begin();
1283 if (selection->count_selected_rows() == 0 || _session == 0) {
1287 for (; i != rows.end(); ++i) {
1290 if ((iter = _model->get_iter (*i))) {
1292 /* some rows don't have a region associated with them, but can still be
1293 selected (XXX maybe prevent them from being selected)
1296 boost::shared_ptr<Region> r = (*iter)[_columns.region];
1307 EditorRegions::drag_data_received (const RefPtr<Gdk::DragContext>& context,
1309 const SelectionData& data,
1310 guint info, guint time)
1312 vector<string> paths;
1314 if (data.get_target() == "GTK_TREE_MODEL_ROW") {
1315 /* something is being dragged over the region list */
1316 _editor->_drags->abort ();
1317 _display.on_drag_data_received (context, x, y, data, info, time);
1321 if (_editor->convert_drop_to_paths (paths, context, x, y, data, info, time) == 0) {
1323 bool copy = ((context->get_actions() & (Gdk::ACTION_COPY | Gdk::ACTION_LINK | Gdk::ACTION_MOVE)) == Gdk::ACTION_COPY);
1325 if (UIConfiguration::instance().get_only_copy_imported_files() || copy) {
1326 _editor->do_import (paths, Editing::ImportDistinctFiles, Editing::ImportAsRegion,
1327 SrcBest, SMFTrackName, SMFTempoIgnore, pos);
1329 _editor->do_embed (paths, Editing::ImportDistinctFiles, ImportAsRegion, pos);
1331 context->drag_finish (true, false, time);
1336 EditorRegions::selection_filter (const RefPtr<TreeModel>& model, const TreeModel::Path& path, bool already_selected)
1338 /* not possible to select rows that do not represent regions, like "Hidden" */
1340 if (already_selected) {
1341 /* deselecting anything is OK with us */
1345 TreeModel::iterator iter = model->get_iter (path);
1348 boost::shared_ptr<Region> r =(*iter)[_columns.region];
1358 EditorRegions::name_editing_started (CellEditable* ce, const Glib::ustring& path)
1362 /* give it a special name */
1364 Gtk::Entry *e = dynamic_cast<Gtk::Entry*> (ce);
1367 e->set_name (X_("RegionNameEditorEntry"));
1370 if ((iter = _model->get_iter (path))) {
1371 boost::shared_ptr<Region> region = (*iter)[_columns.region];
1374 e->set_text(region->name());
1381 EditorRegions::name_edit (const std::string& path, const std::string& new_text)
1385 boost::shared_ptr<Region> region;
1388 if ((row_iter = _model->get_iter (path))) {
1389 region = (*row_iter)[_columns.region];
1390 (*row_iter)[_columns.name] = new_text;
1393 /* now mapover everything */
1396 vector<RegionView*> equivalents;
1397 _editor->get_regions_corresponding_to (region, equivalents, false);
1399 for (vector<RegionView*>::iterator i = equivalents.begin(); i != equivalents.end(); ++i) {
1400 if (new_text != (*i)->region()->name()) {
1401 (*i)->region()->set_name (new_text);
1405 populate_row_name (region, (*row_iter));
1409 /** @return Region that has been dragged out of the list, or 0 */
1410 boost::shared_ptr<Region>
1411 EditorRegions::get_dragged_region ()
1413 list<boost::shared_ptr<Region> > regions;
1415 _display.get_object_drag_data (regions, &source);
1417 if (regions.empty()) {
1418 return boost::shared_ptr<Region> ();
1421 assert (regions.size() == 1);
1422 return regions.front ();
1426 EditorRegions::clear ()
1428 _display.set_model (Glib::RefPtr<Gtk::TreeStore> (0));
1430 _display.set_model (_model);
1432 /* Clean up the maps */
1433 region_row_map.clear();
1434 parent_regions_sources_map.clear();
1437 boost::shared_ptr<Region>
1438 EditorRegions::get_single_selection ()
1440 Glib::RefPtr<TreeSelection> selected = _display.get_selection();
1442 if (selected->count_selected_rows() != 1) {
1443 return boost::shared_ptr<Region> ();
1446 TreeView::Selection::ListHandle_Path rows = selected->get_selected_rows ();
1448 /* only one row selected, so rows.begin() is it */
1450 TreeIter iter = _model->get_iter (*rows.begin());
1453 return boost::shared_ptr<Region> ();
1456 return (*iter)[_columns.region];
1460 EditorRegions::freeze_tree_model (){
1462 _display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
1463 _model->set_sort_column (-2, SORT_ASCENDING); //Disable sorting to gain performance
1468 EditorRegions::thaw_tree_model (){
1470 _model->set_sort_column (0, SORT_ASCENDING); // renabale sorting
1471 _display.set_model (_model);
1473 if (toggle_full_action()->get_active()) {
1474 _display.expand_all();
1479 EditorRegions::locked_changed (std::string const & path)
1481 TreeIter i = _model->get_iter (path);
1483 boost::shared_ptr<ARDOUR::Region> region = (*i)[_columns.region];
1485 region->set_locked (!(*i)[_columns.locked]);
1491 EditorRegions::glued_changed (std::string const & path)
1493 TreeIter i = _model->get_iter (path);
1495 boost::shared_ptr<ARDOUR::Region> region = (*i)[_columns.region];
1497 /* `glued' means MusicTime, and we're toggling here */
1498 region->set_position_lock_style ((*i)[_columns.glued] ? AudioTime : MusicTime);
1505 EditorRegions::muted_changed (std::string const & path)
1507 TreeIter i = _model->get_iter (path);
1509 boost::shared_ptr<ARDOUR::Region> region = (*i)[_columns.region];
1511 region->set_muted (!(*i)[_columns.muted]);
1518 EditorRegions::opaque_changed (std::string const & path)
1520 TreeIter i = _model->get_iter (path);
1522 boost::shared_ptr<ARDOUR::Region> region = (*i)[_columns.region];
1524 region->set_opaque (!(*i)[_columns.opaque]);
1531 EditorRegions::get_state () const
1533 XMLNode* node = new XMLNode (X_("RegionList"));
1535 node->set_property (X_("sort-type"), _sort_type);
1537 RefPtr<Action> act = ActionManager::get_action (X_("RegionList"), X_("SortAscending"));
1538 bool const ascending = RefPtr<RadioAction>::cast_dynamic(act)->get_active ();
1539 node->set_property (X_("sort-ascending"), ascending);
1540 node->set_property (X_("show-all"), toggle_full_action()->get_active());
1541 node->set_property (X_("show-automatic-regions"), _show_automatic_regions);
1547 EditorRegions::set_state (const XMLNode & node)
1549 bool changed = false;
1551 if (node.name() != X_("RegionList")) {
1555 Editing::RegionListSortType t;
1556 if (node.get_property (X_("sort-type"), t)) {
1558 if (_sort_type != t) {
1562 reset_sort_type (t, true);
1563 RefPtr<RadioAction> ract = sort_type_action (t);
1564 ract->set_active ();
1568 if (node.get_property (X_("sort-ascending"), yn)) {
1569 SortType old_sort_type;
1570 int old_sort_column;
1572 _model->get_sort_column_id (old_sort_column, old_sort_type);
1574 if (old_sort_type != (yn ? SORT_ASCENDING : SORT_DESCENDING)) {
1578 reset_sort_direction (yn);
1582 act = ActionManager::get_action (X_("RegionList"), X_("SortAscending"));
1584 act = ActionManager::get_action (X_("RegionList"), X_("SortDescending"));
1587 RefPtr<RadioAction>::cast_dynamic(act)->set_active ();
1590 if (node.get_property (X_("show-all"), yn)) {
1591 if (expanded != yn) {
1596 toggle_full_action()->set_active (yn);
1599 if (node.get_property (X_("show-automatic-regions"), yn)) {
1600 if (yn != _show_automatic_regions) {
1601 _show_automatic_regions = yn;
1602 toggle_show_auto_regions_action()->set_active (yn);
1613 EditorRegions::sort_type_action (Editing::RegionListSortType t) const
1615 const char* action = 0;
1618 case Editing::ByName:
1619 action = X_("SortByRegionName");
1621 case Editing::ByLength:
1622 action = X_("SortByRegionLength");
1624 case Editing::ByPosition:
1625 action = X_("SortByRegionPosition");
1627 case Editing::ByTimestamp:
1628 action = X_("SortByRegionTimestamp");
1630 case Editing::ByStartInFile:
1631 action = X_("SortByRegionStartinFile");
1633 case Editing::ByEndInFile:
1634 action = X_("SortByRegionEndinFile");
1636 case Editing::BySourceFileName:
1637 action = X_("SortBySourceFileName");
1639 case Editing::BySourceFileLength:
1640 action = X_("SortBySourceFileLength");
1642 case Editing::BySourceFileCreationDate:
1643 action = X_("SortBySourceFileCreationDate");
1645 case Editing::BySourceFileFS:
1646 action = X_("SortBySourceFilesystem");
1649 fatal << string_compose (_("programming error: %1: %2"), "EditorRegions: impossible sort type", (int) t) << endmsg;
1650 abort(); /*NOTREACHED*/
1653 RefPtr<Action> act = ActionManager::get_action (X_("RegionList"), action);
1656 return RefPtr<RadioAction>::cast_dynamic (act);
1660 EditorRegions::hide_action () const
1662 return ActionManager::get_action (X_("RegionList"), X_("rlHide"));
1667 EditorRegions::show_action () const
1669 return ActionManager::get_action (X_("RegionList"), X_("rlShow"));
1673 EditorRegions::remove_unused_regions_action () const
1675 return ActionManager::get_action (X_("RegionList"), X_("removeUnusedRegions"));
1678 RefPtr<ToggleAction>
1679 EditorRegions::toggle_full_action () const
1681 Glib::RefPtr<Action> act = ActionManager::get_action (X_("RegionList"), X_("rlShowAll"));
1683 return Glib::RefPtr<ToggleAction>::cast_dynamic (act);
1686 RefPtr<ToggleAction>
1687 EditorRegions::toggle_show_auto_regions_action () const
1689 Glib::RefPtr<Action> act = ActionManager::get_action (X_("RegionList"), X_("rlShowAuto"));
1691 return Glib::RefPtr<ToggleAction>::cast_dynamic (act);