2 Copyright (C) 2000-2006 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.
20 #include <pbd/stacktrace.h>
22 #include <ardour/diskstream.h>
23 #include <ardour/playlist.h>
24 #include <ardour/route_group.h>
28 #include "audio_time_axis.h"
29 #include "audio_region_view.h"
30 #include "audio_streamview.h"
31 #include "automation_line.h"
37 using namespace ARDOUR;
41 using namespace Gtkmm2ext;
42 using namespace Editing;
44 struct TrackViewByPositionSorter
46 bool operator() (const TimeAxisView* a, const TimeAxisView *b) {
47 return a->y_position < b->y_position;
52 Editor::extend_selection_to_track (TimeAxisView& view)
54 if (selection->selected (&view)) {
55 /* already selected, do nothing */
59 if (selection->tracks.empty()) {
61 if (!selection->selected (&view)) {
62 selection->set (&view);
69 /* something is already selected, so figure out which range of things to add */
71 TrackViewList to_be_added;
72 TrackViewList sorted = track_views;
73 TrackViewByPositionSorter cmp;
74 bool passed_clicked = false;
79 if (!selection->selected (&view)) {
80 to_be_added.push_back (&view);
83 /* figure out if we should go forward or backwards */
85 for (TrackViewList::iterator i = sorted.begin(); i != sorted.end(); ++i) {
88 passed_clicked = true;
91 if (selection->selected (*i)) {
101 passed_clicked = false;
105 for (TrackViewList::iterator i = sorted.begin(); i != sorted.end(); ++i) {
108 passed_clicked = true;
112 if (passed_clicked) {
113 if ((*i)->hidden()) {
116 if (selection->selected (*i)) {
118 } else if (!(*i)->hidden()) {
119 to_be_added.push_back (*i);
126 for (TrackViewList::reverse_iterator r = sorted.rbegin(); r != sorted.rend(); ++r) {
129 passed_clicked = true;
133 if (passed_clicked) {
135 if ((*r)->hidden()) {
139 if (selection->selected (*r)) {
141 } else if (!(*r)->hidden()) {
142 to_be_added.push_back (*r);
148 if (!to_be_added.empty()) {
149 selection->add (to_be_added);
157 Editor::select_all_tracks ()
159 selection->set (track_views);
163 Editor::set_selected_track (TimeAxisView& view, Selection::Operation op, bool no_remove)
168 case Selection::Toggle:
169 if (selection->selected (&view)) {
171 selection->remove (&view);
175 selection->add (&view);
181 if (!selection->selected (&view)) {
182 selection->add (&view);
188 if (selection->selected (&view) && selection->tracks.size() == 1) {
189 /* no commit necessary */
192 /* reset track selection if there is only 1 other track
193 selected OR if no_remove is not set (its there to
194 prevent deselecting a multi-track selection
195 when clicking on an already selected track
199 if (selection->tracks.empty()) {
200 selection->set (&view);
202 } else if (selection->tracks.size() == 1 || !no_remove) {
203 selection->set (&view);
209 case Selection::Extend:
210 commit = extend_selection_to_track (view);
218 Editor::set_selected_track_from_click (bool press, Selection::Operation op, bool no_remove)
220 if (!clicked_trackview) {
228 return set_selected_track (*clicked_trackview, op, no_remove);
232 Editor::set_selected_control_point_from_click (Selection::Operation op, bool no_remove)
234 if (!clicked_control_point) {
238 /* select this point and any others that it represents */
243 x1 = pixel_to_frame (clicked_control_point->get_x() - 10);
244 x2 = pixel_to_frame (clicked_control_point->get_x() + 10);
245 y1 = clicked_control_point->get_x() - 10;
246 y2 = clicked_control_point->get_y() + 10;
248 return select_all_within (x1, x2, y1, y2, selection->tracks, op);
252 Editor::get_relevant_audio_tracks (set<AudioTimeAxisView*>& relevant_tracks)
254 /* step one: get all selected tracks and all tracks in the relevant edit groups */
256 for (TrackSelection::iterator ti = selection->tracks.begin(); ti != selection->tracks.end(); ++ti) {
258 AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(*ti);
264 RouteGroup* group = atv->route()->edit_group();
266 if (group && group->is_active()) {
268 /* active group for this track, loop over all tracks and get every member of the group */
270 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
272 AudioTimeAxisView* tatv;
274 if ((tatv = dynamic_cast<AudioTimeAxisView*> (*i)) != 0) {
276 if (tatv->route()->edit_group() == group) {
277 relevant_tracks.insert (tatv);
282 relevant_tracks.insert (atv);
289 * Call a slot for a given `basis' track and also for any track that is in the same
291 * @param sl Slot to call.
292 * @param basis Basis track.
296 Editor::mapover_audio_tracks (slot<void,AudioTimeAxisView&,uint32_t> sl, TimeAxisView* basis)
298 AudioTimeAxisView* audio_basis = dynamic_cast<AudioTimeAxisView*> (basis);
299 if (audio_basis == 0) {
303 /* work out the tracks that we will call the slot for; use
304 a set here as it will disallow possible duplicates of the
306 set<AudioTimeAxisView*> tracks;
308 /* always call for the basis */
309 tracks.insert (audio_basis);
311 RouteGroup* group = audio_basis->route()->edit_group();
312 if (group && group->is_active()) {
314 /* the basis is a member of an active edit group; find other members */
315 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
316 AudioTimeAxisView* v = dynamic_cast<AudioTimeAxisView*> (*i);
317 if (v && v->route()->edit_group() == group) {
324 uint32_t const sz = tracks.size ();
325 for (set<AudioTimeAxisView*>::iterator i = tracks.begin(); i != tracks.end(); ++i) {
331 Editor::mapped_get_equivalent_regions (RouteTimeAxisView& tv, uint32_t ignored, RegionView* basis, vector<RegionView*>* all_equivs)
333 boost::shared_ptr<Playlist> pl;
334 vector<boost::shared_ptr<Region> > results;
336 boost::shared_ptr<Diskstream> ds;
338 if ((ds = tv.get_diskstream()) == 0) {
343 if (&tv == &basis->get_time_axis_view()) {
344 /* looking in same track as the original */
348 if ((pl = ds->playlist()) != 0) {
349 pl->get_equivalent_regions (basis->region(), results);
352 for (vector<boost::shared_ptr<Region> >::iterator ir = results.begin(); ir != results.end(); ++ir) {
353 if ((marv = tv.view()->find_view (*ir)) != 0) {
354 all_equivs->push_back (marv);
360 Editor::get_equivalent_regions (RegionView* basis, vector<RegionView*>& equivalent_regions)
362 mapover_audio_tracks (bind (mem_fun (*this, &Editor::mapped_get_equivalent_regions), basis, &equivalent_regions), &basis->get_trackview());
364 /* add clicked regionview since we skipped all other regions in the same track as the one it was in */
366 equivalent_regions.push_back (basis);
370 Editor::set_selected_regionview_from_click (bool press, Selection::Operation op, bool no_track_remove)
372 vector<RegionView*> all_equivalent_regions;
375 if (!clicked_regionview || !clicked_audio_trackview) {
380 button_release_can_deselect = false;
383 if (op == Selection::Toggle || op == Selection::Set) {
387 case Selection::Toggle:
389 if (clicked_regionview->get_selected()) {
392 /* whatever was clicked was selected already; do nothing here but allow
393 the button release to deselect it
396 button_release_can_deselect = true;
400 if (button_release_can_deselect) {
402 /* just remove this one region, but only on a permitted button release */
404 selection->remove (clicked_regionview);
407 /* no more deselect action on button release till a new press
408 finds an already selected object.
411 button_release_can_deselect = false;
419 if (selection->selected (clicked_audio_trackview)) {
420 get_equivalent_regions (clicked_regionview, all_equivalent_regions);
422 all_equivalent_regions.push_back (clicked_regionview);
425 /* add all the equivalent regions, but only on button press */
429 if (!all_equivalent_regions.empty()) {
433 selection->add (all_equivalent_regions);
439 if (!clicked_regionview->get_selected()) {
441 get_equivalent_regions (clicked_regionview, all_equivalent_regions);
442 selection->set (all_equivalent_regions);
445 /* no commit necessary: clicked on an already selected region */
455 } else if (op == Selection::Extend) {
457 list<Selectable*> results;
458 nframes_t last_frame;
459 nframes_t first_frame;
461 /* 1. find the last selected regionview in the track that was clicked in */
464 first_frame = max_frames;
466 for (RegionSelection::iterator x = selection->regions.begin(); x != selection->regions.end(); ++x) {
467 if (&(*x)->get_time_axis_view() == &clicked_regionview->get_time_axis_view()) {
469 if ((*x)->region()->last_frame() > last_frame) {
470 last_frame = (*x)->region()->last_frame();
473 if ((*x)->region()->first_frame() < first_frame) {
474 first_frame = (*x)->region()->first_frame();
479 /* 2. figure out the boundaries for our search for new objects */
481 switch (clicked_regionview->region()->coverage (first_frame, last_frame)) {
483 if (last_frame < clicked_regionview->region()->first_frame()) {
484 first_frame = last_frame;
485 last_frame = clicked_regionview->region()->last_frame();
487 last_frame = first_frame;
488 first_frame = clicked_regionview->region()->first_frame();
492 case OverlapExternal:
493 if (last_frame < clicked_regionview->region()->first_frame()) {
494 first_frame = last_frame;
495 last_frame = clicked_regionview->region()->last_frame();
497 last_frame = first_frame;
498 first_frame = clicked_regionview->region()->first_frame();
502 case OverlapInternal:
503 if (last_frame < clicked_regionview->region()->first_frame()) {
504 first_frame = last_frame;
505 last_frame = clicked_regionview->region()->last_frame();
507 last_frame = first_frame;
508 first_frame = clicked_regionview->region()->first_frame();
514 /* nothing to do except add clicked region to selection, since it
515 overlaps with the existing selection in this track.
520 /* 2. find all selectable objects (regionviews in this case) between that one and the end of the
521 one that was clicked.
524 set<AudioTimeAxisView*> relevant_tracks;
526 get_relevant_audio_tracks (relevant_tracks);
528 for (set<AudioTimeAxisView*>::iterator t = relevant_tracks.begin(); t != relevant_tracks.end(); ++t) {
529 (*t)->get_selectables (first_frame, last_frame, -1.0, -1.0, results);
532 /* 3. convert to a vector of audio regions */
534 vector<RegionView*> regions;
536 for (list<Selectable*>::iterator x = results.begin(); x != results.end(); ++x) {
539 if ((arv = dynamic_cast<RegionView*>(*x)) != 0) {
540 regions.push_back (arv);
544 if (!regions.empty()) {
545 selection->add (regions);
555 Editor::set_selected_regionview_from_region_list (boost::shared_ptr<Region> region, Selection::Operation op)
557 vector<RegionView*> all_equivalent_regions;
559 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
561 RouteTimeAxisView* tatv;
563 if ((tatv = dynamic_cast<RouteTimeAxisView*> (*i)) != 0) {
565 boost::shared_ptr<Playlist> pl;
566 vector<boost::shared_ptr<Region> > results;
568 boost::shared_ptr<Diskstream> ds;
570 if ((ds = tatv->get_diskstream()) == 0) {
575 if ((pl = (ds->playlist())) != 0) {
576 pl->get_region_list_equivalent_regions (region, results);
579 for (vector<boost::shared_ptr<Region> >::iterator ir = results.begin(); ir != results.end(); ++ir) {
580 if ((marv = tatv->view()->find_view (*ir)) != 0) {
581 all_equivalent_regions.push_back (marv);
589 case Selection::Toggle:
590 /* XXX this is not correct */
591 selection->toggle (all_equivalent_regions);
594 selection->set (all_equivalent_regions);
596 case Selection::Extend:
597 selection->add (all_equivalent_regions);
600 selection->add (all_equivalent_regions);
606 Editor::set_selected_regionview_from_map_event (GdkEventAny* ev, StreamView* sv, boost::weak_ptr<Region> weak_r)
609 boost::shared_ptr<Region> r (weak_r.lock());
615 boost::shared_ptr<AudioRegion> ar;
617 if ((ar = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
621 if ((rv = sv->find_view (ar)) == 0) {
625 /* don't reset the selection if its something other than
626 a single other region.
629 if (selection->regions.size() > 1) {
633 begin_reversible_command (_("set selected regions"));
637 commit_reversible_command () ;
643 Editor::track_selection_changed ()
645 switch (selection->tracks.size()){
649 set_selected_mixer_strip (*(selection->tracks.front()));
653 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
654 if (find (selection->tracks.begin(), selection->tracks.end(), *i) != selection->tracks.end()) {
655 (*i)->set_selected (true);
657 (*i)->set_selected (false);
663 Editor::time_selection_changed ()
665 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
666 (*i)->hide_selection ();
669 if (selection->tracks.empty()) {
670 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
671 (*i)->show_selection (selection->time);
674 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
675 (*i)->show_selection (selection->time);
679 if (selection->time.empty()) {
680 ActionManager::set_sensitive (ActionManager::time_selection_sensitive_actions, false);
682 ActionManager::set_sensitive (ActionManager::time_selection_sensitive_actions, true);
688 Editor::region_selection_changed ()
690 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
691 (*i)->set_selected_regionviews (selection->regions);
696 Editor::point_selection_changed ()
698 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
699 (*i)->set_selected_points (selection->points);
704 Editor::select_all_in_track (Selection::Operation op)
706 list<Selectable *> touched;
708 if (!clicked_trackview) {
712 clicked_trackview->get_selectables (0, max_frames, 0, DBL_MAX, touched);
715 case Selection::Toggle:
716 selection->add (touched);
719 selection->set (touched);
721 case Selection::Extend:
722 /* meaningless, because we're selecting everything */
725 selection->add (touched);
731 Editor::select_all (Selection::Operation op)
733 list<Selectable *> touched;
735 for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
736 if ((*iter)->hidden()) {
739 (*iter)->get_selectables (0, max_frames, 0, DBL_MAX, touched);
741 begin_reversible_command (_("select all"));
744 selection->add (touched);
746 case Selection::Toggle:
747 selection->add (touched);
750 selection->set (touched);
752 case Selection::Extend:
753 /* meaningless, because we're selecting everything */
756 commit_reversible_command ();
760 Editor::invert_selection_in_track ()
762 list<Selectable *> touched;
764 if (!clicked_trackview) {
768 clicked_trackview->get_inverted_selectables (*selection, touched);
769 selection->set (touched);
773 Editor::invert_selection ()
775 list<Selectable *> touched;
777 for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
778 if ((*iter)->hidden()) {
781 (*iter)->get_inverted_selectables (*selection, touched);
784 selection->set (touched);
788 Editor::select_all_within (nframes_t start, nframes_t end, double top, double bot, const TrackViewList& tracklist, Selection::Operation op)
790 list<Selectable*> touched;
791 list<Selectable*>::size_type n = 0;
792 TrackViewList touched_tracks;
794 for (TrackViewList::const_iterator iter = tracklist.begin(); iter != tracklist.end(); ++iter) {
795 if ((*iter)->hidden()) {
801 (*iter)->get_selectables (start, end, top, bot, touched);
803 if (n != touched.size()) {
804 touched_tracks.push_back (*iter);
808 if (touched.empty()) {
812 if (!touched_tracks.empty()) {
816 selection->add (touched_tracks);
818 case Selection::Toggle:
819 selection->toggle (touched_tracks);
822 selection->set (touched_tracks);
824 case Selection::Extend:
825 /* not defined yet */
830 begin_reversible_command (_("select all within"));
833 selection->add (touched);
835 case Selection::Toggle:
836 selection->toggle (touched);
839 selection->set (touched);
841 case Selection::Extend:
842 /* not defined yet */
846 commit_reversible_command ();
848 return !touched.empty();
852 Editor::set_selection_from_audio_region ()
854 if (selection->regions.empty()) {
858 RegionView* rv = *(selection->regions.begin());
859 boost::shared_ptr<Region> region = rv->region();
861 begin_reversible_command (_("set selection from region"));
862 selection->set (0, region->position(), region->last_frame());
863 commit_reversible_command ();
865 set_mouse_mode (Editing::MouseRange, false);
869 Editor::set_selection_from_punch()
873 if ((location = session->locations()->auto_punch_location()) == 0) {
877 set_selection_from_range (*location);
881 Editor::set_selection_from_loop()
885 if ((location = session->locations()->auto_loop_location()) == 0) {
888 set_selection_from_range (*location);
892 Editor::set_selection_from_range (Location& loc)
894 begin_reversible_command (_("set selection from range"));
895 selection->set (0, loc.start(), loc.end());
896 commit_reversible_command ();
898 set_mouse_mode (Editing::MouseRange, false);
902 Editor::select_all_selectables_using_time_selection ()
904 list<Selectable *> touched;
906 if (selection->time.empty()) {
910 nframes_t start = selection->time[clicked_selection].start;
911 nframes_t end = selection->time[clicked_selection].end;
913 if (end - start < 1) {
917 for (TrackViewList::iterator iter = selection->tracks.begin(); iter != selection->tracks.end(); ++iter) {
918 if ((*iter)->hidden()) {
921 (*iter)->get_selectables (start, end - 1, 0, DBL_MAX, touched);
924 begin_reversible_command (_("select all from range"));
925 selection->set (touched);
926 commit_reversible_command ();
931 Editor::select_all_selectables_using_punch()
933 Location* location = session->locations()->auto_punch_location();
934 list<Selectable *> touched;
936 if (location == 0 || (location->end() - location->start() <= 1)) {
940 for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
941 if ((*iter)->hidden()) {
944 (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
946 begin_reversible_command (_("select all from punch"));
947 selection->set (touched);
948 commit_reversible_command ();
953 Editor::select_all_selectables_using_loop()
955 Location* location = session->locations()->auto_loop_location();
956 list<Selectable *> touched;
958 if (location == 0 || (location->end() - location->start() <= 1)) {
962 for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
963 if ((*iter)->hidden()) {
966 (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
968 begin_reversible_command (_("select all from loop"));
969 selection->set (touched);
970 commit_reversible_command ();
975 Editor::select_all_selectables_using_cursor (Cursor *cursor, bool after)
979 list<Selectable *> touched;
982 begin_reversible_command (_("select all after cursor"));
983 start = cursor->current_frame ;
984 end = session->current_end_frame();
986 if (cursor->current_frame > 0) {
987 begin_reversible_command (_("select all before cursor"));
989 end = cursor->current_frame - 1;
995 for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
996 if ((*iter)->hidden()) {
999 (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
1001 selection->set (touched);
1002 commit_reversible_command ();
1006 Editor::select_all_selectables_between_cursors (Cursor *cursor, Cursor *other_cursor)
1010 list<Selectable *> touched;
1011 bool other_cursor_is_first = cursor->current_frame > other_cursor->current_frame;
1013 if (cursor->current_frame == other_cursor->current_frame) {
1017 begin_reversible_command (_("select all between cursors"));
1018 if (other_cursor_is_first) {
1019 start = other_cursor->current_frame;
1020 end = cursor->current_frame - 1;
1023 start = cursor->current_frame;
1024 end = other_cursor->current_frame - 1;
1027 for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
1028 if ((*iter)->hidden()) {
1031 (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
1033 selection->set (touched);
1034 commit_reversible_command ();