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);
288 Editor::mapover_audio_tracks (slot<void,AudioTimeAxisView&,uint32_t> sl)
290 set<AudioTimeAxisView*> relevant_tracks;
292 get_relevant_audio_tracks (relevant_tracks);
294 uint32_t sz = relevant_tracks.size();
296 for (set<AudioTimeAxisView*>::iterator ati = relevant_tracks.begin(); ati != relevant_tracks.end(); ++ati) {
302 Editor::mapped_get_equivalent_regions (RouteTimeAxisView& tv, uint32_t ignored, RegionView* basis, vector<RegionView*>* all_equivs)
304 boost::shared_ptr<Playlist> pl;
305 vector<boost::shared_ptr<Region> > results;
307 boost::shared_ptr<Diskstream> ds;
309 if ((ds = tv.get_diskstream()) == 0) {
314 if (&tv == &basis->get_time_axis_view()) {
315 /* looking in same track as the original */
319 if ((pl = ds->playlist()) != 0) {
320 pl->get_equivalent_regions (basis->region(), results);
323 for (vector<boost::shared_ptr<Region> >::iterator ir = results.begin(); ir != results.end(); ++ir) {
324 if ((marv = tv.view()->find_view (*ir)) != 0) {
325 all_equivs->push_back (marv);
331 Editor::get_equivalent_regions (RegionView* basis, vector<RegionView*>& equivalent_regions)
333 mapover_audio_tracks (bind (mem_fun (*this, &Editor::mapped_get_equivalent_regions), basis, &equivalent_regions));
335 /* add clicked regionview since we skipped all other regions in the same track as the one it was in */
337 equivalent_regions.push_back (basis);
341 Editor::set_selected_regionview_from_click (bool press, Selection::Operation op, bool no_track_remove)
343 vector<RegionView*> all_equivalent_regions;
346 if (!clicked_regionview || !clicked_audio_trackview) {
351 button_release_can_deselect = false;
354 if (op == Selection::Toggle || op == Selection::Set) {
358 case Selection::Toggle:
360 if (clicked_regionview->get_selected()) {
363 /* whatever was clicked was selected already; do nothing here but allow
364 the button release to deselect it
367 button_release_can_deselect = true;
371 if (button_release_can_deselect) {
373 /* just remove this one region, but only on a permitted button release */
375 selection->remove (clicked_regionview);
378 /* no more deselect action on button release till a new press
379 finds an already selected object.
382 button_release_can_deselect = false;
390 if (selection->selected (clicked_audio_trackview)) {
391 get_equivalent_regions (clicked_regionview, all_equivalent_regions);
393 all_equivalent_regions.push_back (clicked_regionview);
396 /* add all the equivalent regions, but only on button press */
400 if (!all_equivalent_regions.empty()) {
404 selection->add (all_equivalent_regions);
410 if (!clicked_regionview->get_selected()) {
412 if (selection->selected (clicked_audio_trackview)) {
413 get_equivalent_regions (clicked_regionview, all_equivalent_regions);
415 all_equivalent_regions.push_back (clicked_regionview);
418 selection->set (all_equivalent_regions);
421 /* no commit necessary: clicked on an already selected region */
431 } else if (op == Selection::Extend) {
433 list<Selectable*> results;
434 nframes_t last_frame;
435 nframes_t first_frame;
437 /* 1. find the last selected regionview in the track that was clicked in */
440 first_frame = max_frames;
442 for (RegionSelection::iterator x = selection->regions.begin(); x != selection->regions.end(); ++x) {
443 if (&(*x)->get_time_axis_view() == &clicked_regionview->get_time_axis_view()) {
445 if ((*x)->region()->last_frame() > last_frame) {
446 last_frame = (*x)->region()->last_frame();
449 if ((*x)->region()->first_frame() < first_frame) {
450 first_frame = (*x)->region()->first_frame();
455 /* 2. figure out the boundaries for our search for new objects */
457 switch (clicked_regionview->region()->coverage (first_frame, last_frame)) {
459 if (last_frame < clicked_regionview->region()->first_frame()) {
460 first_frame = last_frame;
461 last_frame = clicked_regionview->region()->last_frame();
463 last_frame = first_frame;
464 first_frame = clicked_regionview->region()->first_frame();
468 case OverlapExternal:
469 if (last_frame < clicked_regionview->region()->first_frame()) {
470 first_frame = last_frame;
471 last_frame = clicked_regionview->region()->last_frame();
473 last_frame = first_frame;
474 first_frame = clicked_regionview->region()->first_frame();
478 case OverlapInternal:
479 if (last_frame < clicked_regionview->region()->first_frame()) {
480 first_frame = last_frame;
481 last_frame = clicked_regionview->region()->last_frame();
483 last_frame = first_frame;
484 first_frame = clicked_regionview->region()->first_frame();
490 /* nothing to do except add clicked region to selection, since it
491 overlaps with the existing selection in this track.
496 /* 2. find all selectable objects (regionviews in this case) between that one and the end of the
497 one that was clicked.
500 set<AudioTimeAxisView*> relevant_tracks;
502 get_relevant_audio_tracks (relevant_tracks);
504 for (set<AudioTimeAxisView*>::iterator t = relevant_tracks.begin(); t != relevant_tracks.end(); ++t) {
505 (*t)->get_selectables (first_frame, last_frame, -1.0, -1.0, results);
508 /* 3. convert to a vector of audio regions */
510 vector<RegionView*> regions;
512 for (list<Selectable*>::iterator x = results.begin(); x != results.end(); ++x) {
515 if ((arv = dynamic_cast<RegionView*>(*x)) != 0) {
516 regions.push_back (arv);
520 if (!regions.empty()) {
521 selection->add (regions);
531 Editor::set_selected_regionview_from_region_list (boost::shared_ptr<Region> region, Selection::Operation op)
533 vector<RegionView*> all_equivalent_regions;
535 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
537 RouteTimeAxisView* tatv;
539 if ((tatv = dynamic_cast<RouteTimeAxisView*> (*i)) != 0) {
541 boost::shared_ptr<Playlist> pl;
542 vector<boost::shared_ptr<Region> > results;
544 boost::shared_ptr<Diskstream> ds;
546 if ((ds = tatv->get_diskstream()) == 0) {
551 if ((pl = (ds->playlist())) != 0) {
552 pl->get_region_list_equivalent_regions (region, results);
555 for (vector<boost::shared_ptr<Region> >::iterator ir = results.begin(); ir != results.end(); ++ir) {
556 if ((marv = tatv->view()->find_view (*ir)) != 0) {
557 all_equivalent_regions.push_back (marv);
564 begin_reversible_command (_("set selected regions"));
567 case Selection::Toggle:
568 /* XXX this is not correct */
569 selection->toggle (all_equivalent_regions);
572 selection->set (all_equivalent_regions);
574 case Selection::Extend:
575 selection->add (all_equivalent_regions);
578 selection->add (all_equivalent_regions);
582 commit_reversible_command () ;
586 Editor::set_selected_regionview_from_map_event (GdkEventAny* ev, StreamView* sv, boost::weak_ptr<Region> weak_r)
589 boost::shared_ptr<Region> r (weak_r.lock());
595 boost::shared_ptr<AudioRegion> ar;
597 if ((ar = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
601 if ((rv = sv->find_view (ar)) == 0) {
605 /* don't reset the selection if its something other than
606 a single other region.
609 if (selection->regions.size() > 1) {
613 begin_reversible_command (_("set selected regions"));
617 commit_reversible_command () ;
623 Editor::track_selection_changed ()
625 switch (selection->tracks.size()){
629 set_selected_mixer_strip (*(selection->tracks.front()));
633 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
634 (*i)->set_selected (false);
635 if (mouse_mode == MouseRange) {
636 (*i)->hide_selection ();
640 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
641 (*i)->set_selected (true);
642 if (mouse_mode == MouseRange) {
643 (*i)->show_selection (selection->time);
649 Editor::time_selection_changed ()
651 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
652 (*i)->hide_selection ();
655 if (selection->tracks.empty()) {
656 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
657 (*i)->show_selection (selection->time);
660 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
661 (*i)->show_selection (selection->time);
665 if (selection->time.empty()) {
666 ActionManager::set_sensitive (ActionManager::time_selection_sensitive_actions, false);
668 ActionManager::set_sensitive (ActionManager::time_selection_sensitive_actions, true);
673 Editor::region_selection_changed ()
675 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
676 (*i)->set_selected_regionviews (selection->regions);
681 Editor::point_selection_changed ()
683 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
684 (*i)->set_selected_points (selection->points);
689 Editor::select_all_in_track (Selection::Operation op)
691 list<Selectable *> touched;
693 if (!clicked_trackview) {
697 clicked_trackview->get_selectables (0, max_frames, 0, DBL_MAX, touched);
700 case Selection::Toggle:
701 selection->add (touched);
704 selection->set (touched);
706 case Selection::Extend:
707 /* meaningless, because we're selecting everything */
710 selection->add (touched);
716 Editor::select_all (Selection::Operation op)
718 list<Selectable *> touched;
720 for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
721 if ((*iter)->hidden()) {
724 (*iter)->get_selectables (0, max_frames, 0, DBL_MAX, touched);
726 begin_reversible_command (_("select all"));
729 selection->add (touched);
731 case Selection::Toggle:
732 selection->add (touched);
735 selection->set (touched);
737 case Selection::Extend:
738 /* meaningless, because we're selecting everything */
741 commit_reversible_command ();
745 Editor::invert_selection_in_track ()
747 list<Selectable *> touched;
749 if (!clicked_trackview) {
753 clicked_trackview->get_inverted_selectables (*selection, touched);
754 selection->set (touched);
758 Editor::invert_selection ()
760 list<Selectable *> touched;
762 for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
763 if ((*iter)->hidden()) {
766 (*iter)->get_inverted_selectables (*selection, touched);
769 selection->set (touched);
773 Editor::select_all_within (nframes_t start, nframes_t end, double top, double bot, const TrackViewList& tracklist, Selection::Operation op)
775 list<Selectable*> touched;
776 list<Selectable*>::size_type n = 0;
777 TrackViewList touched_tracks;
779 for (TrackViewList::const_iterator iter = tracklist.begin(); iter != tracklist.end(); ++iter) {
780 if ((*iter)->hidden()) {
786 (*iter)->get_selectables (start, end, top, bot, touched);
788 if (n != touched.size()) {
789 touched_tracks.push_back (*iter);
793 if (touched.empty()) {
797 if (!touched_tracks.empty()) {
801 selection->add (touched_tracks);
803 case Selection::Toggle:
804 selection->toggle (touched_tracks);
807 selection->set (touched_tracks);
809 case Selection::Extend:
810 /* not defined yet */
815 begin_reversible_command (_("select all within"));
818 selection->add (touched);
820 case Selection::Toggle:
821 selection->toggle (touched);
824 selection->set (touched);
826 case Selection::Extend:
827 /* not defined yet */
831 commit_reversible_command ();
833 return !touched.empty();
837 Editor::set_selection_from_audio_region ()
839 if (selection->regions.empty()) {
843 RegionView* rv = *(selection->regions.begin());
844 boost::shared_ptr<Region> region = rv->region();
846 begin_reversible_command (_("set selection from region"));
847 selection->set (0, region->position(), region->last_frame());
848 commit_reversible_command ();
850 set_mouse_mode (Editing::MouseRange, false);
854 Editor::set_selection_from_punch()
858 if ((location = session->locations()->auto_punch_location()) == 0) {
862 set_selection_from_range (*location);
866 Editor::set_selection_from_loop()
870 if ((location = session->locations()->auto_loop_location()) == 0) {
873 set_selection_from_range (*location);
877 Editor::set_selection_from_range (Location& loc)
879 begin_reversible_command (_("set selection from range"));
880 selection->set (0, loc.start(), loc.end());
881 commit_reversible_command ();
883 set_mouse_mode (Editing::MouseRange, false);
887 Editor::select_all_selectables_using_time_selection ()
889 list<Selectable *> touched;
891 if (selection->time.empty()) {
895 nframes_t start = selection->time[clicked_selection].start;
896 nframes_t end = selection->time[clicked_selection].end;
898 if (end - start < 1) {
902 for (TrackViewList::iterator iter = selection->tracks.begin(); iter != selection->tracks.end(); ++iter) {
903 if ((*iter)->hidden()) {
906 (*iter)->get_selectables (start, end - 1, 0, DBL_MAX, touched);
909 begin_reversible_command (_("select all from range"));
910 selection->set (touched);
911 commit_reversible_command ();
916 Editor::select_all_selectables_using_punch()
918 Location* location = session->locations()->auto_punch_location();
919 list<Selectable *> touched;
921 if (location == 0 || (location->end() - location->start() <= 1)) {
925 for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
926 if ((*iter)->hidden()) {
929 (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
931 begin_reversible_command (_("select all from punch"));
932 selection->set (touched);
933 commit_reversible_command ();
938 Editor::select_all_selectables_using_loop()
940 Location* location = session->locations()->auto_loop_location();
941 list<Selectable *> touched;
943 if (location == 0 || (location->end() - location->start() <= 1)) {
947 for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
948 if ((*iter)->hidden()) {
951 (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
953 begin_reversible_command (_("select all from loop"));
954 selection->set (touched);
955 commit_reversible_command ();
960 Editor::select_all_selectables_using_cursor (Cursor *cursor, bool after)
964 list<Selectable *> touched;
967 begin_reversible_command (_("select all after cursor"));
968 start = cursor->current_frame ;
969 end = session->current_end_frame();
971 if (cursor->current_frame > 0) {
972 begin_reversible_command (_("select all before cursor"));
974 end = cursor->current_frame - 1;
980 for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
981 if ((*iter)->hidden()) {
984 (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
986 selection->set (touched);
987 commit_reversible_command ();
991 Editor::select_all_selectables_between_cursors (Cursor *cursor, Cursor *other_cursor)
995 list<Selectable *> touched;
996 bool other_cursor_is_first = cursor->current_frame > other_cursor->current_frame;
998 if (cursor->current_frame == other_cursor->current_frame) {
1002 begin_reversible_command (_("select all between cursors"));
1003 if (other_cursor_is_first) {
1004 start = other_cursor->current_frame;
1005 end = cursor->current_frame - 1;
1008 start = cursor->current_frame;
1009 end = other_cursor->current_frame - 1;
1012 for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
1013 if ((*iter)->hidden()) {
1016 (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
1018 selection->set (touched);
1019 commit_reversible_command ();