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"
32 #include "control_point.h"
38 using namespace ARDOUR;
42 using namespace Gtkmm2ext;
43 using namespace Editing;
45 struct TrackViewByPositionSorter
47 bool operator() (const TimeAxisView* a, const TimeAxisView *b) {
48 return a->y_position < b->y_position;
53 Editor::extend_selection_to_track (TimeAxisView& view)
55 if (selection->selected (&view)) {
56 /* already selected, do nothing */
60 if (selection->tracks.empty()) {
62 if (!selection->selected (&view)) {
63 selection->set (&view);
70 /* something is already selected, so figure out which range of things to add */
72 TrackViewList to_be_added;
73 TrackViewList sorted = track_views;
74 TrackViewByPositionSorter cmp;
75 bool passed_clicked = false;
80 if (!selection->selected (&view)) {
81 to_be_added.push_back (&view);
84 /* figure out if we should go forward or backwards */
86 for (TrackViewList::iterator i = sorted.begin(); i != sorted.end(); ++i) {
89 passed_clicked = true;
92 if (selection->selected (*i)) {
102 passed_clicked = false;
106 for (TrackViewList::iterator i = sorted.begin(); i != sorted.end(); ++i) {
109 passed_clicked = true;
113 if (passed_clicked) {
114 if ((*i)->hidden()) {
117 if (selection->selected (*i)) {
119 } else if (!(*i)->hidden()) {
120 to_be_added.push_back (*i);
127 for (TrackViewList::reverse_iterator r = sorted.rbegin(); r != sorted.rend(); ++r) {
130 passed_clicked = true;
134 if (passed_clicked) {
136 if ((*r)->hidden()) {
140 if (selection->selected (*r)) {
142 } else if (!(*r)->hidden()) {
143 to_be_added.push_back (*r);
149 if (!to_be_added.empty()) {
150 selection->add (to_be_added);
158 Editor::select_all_tracks ()
160 selection->set (track_views);
164 Editor::set_selected_track (TimeAxisView& view, Selection::Operation op, bool no_remove)
169 case Selection::Toggle:
170 if (selection->selected (&view)) {
172 selection->remove (&view);
176 selection->add (&view);
182 if (!selection->selected (&view)) {
183 selection->add (&view);
189 if (selection->selected (&view) && selection->tracks.size() == 1) {
190 /* no commit necessary */
193 /* reset track selection if there is only 1 other track
194 selected OR if no_remove is not set (its there to
195 prevent deselecting a multi-track selection
196 when clicking on an already selected track
200 if (selection->tracks.empty()) {
201 selection->set (&view);
203 } else if (selection->tracks.size() == 1 || !no_remove) {
204 selection->set (&view);
210 case Selection::Extend:
211 commit = extend_selection_to_track (view);
219 Editor::set_selected_track_from_click (bool press, Selection::Operation op, bool no_remove)
221 if (!clicked_routeview) {
229 return set_selected_track (*clicked_routeview, op, no_remove);
233 Editor::set_selected_control_point_from_click (Selection::Operation op, bool no_remove)
235 if (!clicked_control_point) {
239 /* select this point and any others that it represents */
244 x1 = pixel_to_frame (clicked_control_point->get_x() - 10);
245 x2 = pixel_to_frame (clicked_control_point->get_x() + 10);
246 y1 = clicked_control_point->get_x() - 10;
247 y2 = clicked_control_point->get_y() + 10;
249 return select_all_within (x1, x2, y1, y2, selection->tracks, op);
253 Editor::get_relevant_tracks (set<RouteTimeAxisView*>& relevant_tracks)
255 /* step one: get all selected tracks and all tracks in the relevant edit groups */
257 for (TrackSelection::iterator ti = selection->tracks.begin(); ti != selection->tracks.end(); ++ti) {
259 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(*ti);
265 RouteGroup* group = rtv->route()->edit_group();
267 if (group && group->is_active()) {
269 /* active group for this track, loop over all tracks and get every member of the group */
271 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
273 RouteTimeAxisView* trtv;
275 if ((trtv = dynamic_cast<RouteTimeAxisView*> (*i)) != 0) {
277 if (trtv->route()->edit_group() == group) {
278 relevant_tracks.insert (trtv);
283 relevant_tracks.insert (rtv);
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_tracks (slot<void, RouteTimeAxisView&, uint32_t> sl, TimeAxisView* basis) const
298 RouteTimeAxisView* route_basis = dynamic_cast<RouteTimeAxisView*> (basis);
299 if (route_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<RouteTimeAxisView*> tracks;
308 /* always call for the basis */
309 tracks.insert (route_basis);
311 RouteGroup* group = route_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::const_iterator i = track_views.begin(); i != track_views.end(); ++i) {
316 RouteTimeAxisView* v = dynamic_cast<RouteTimeAxisView*> (*i);
317 if (v && v->route()->edit_group() == group) {
324 uint32_t const sz = tracks.size ();
325 for (set<RouteTimeAxisView*>::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) const
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) const
362 mapover_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_routeview) {
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_routeview)) {
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()) {
440 selection->set (clicked_regionview);
443 /* no commit necessary: clicked on an already selected region */
453 } else if (op == Selection::Extend) {
455 list<Selectable*> results;
456 nframes_t last_frame;
457 nframes_t first_frame;
459 /* 1. find the last selected regionview in the track that was clicked in */
462 first_frame = max_frames;
464 for (RegionSelection::iterator x = selection->regions.begin(); x != selection->regions.end(); ++x) {
465 if (&(*x)->get_time_axis_view() == &clicked_regionview->get_time_axis_view()) {
467 if ((*x)->region()->last_frame() > last_frame) {
468 last_frame = (*x)->region()->last_frame();
471 if ((*x)->region()->first_frame() < first_frame) {
472 first_frame = (*x)->region()->first_frame();
477 /* 2. figure out the boundaries for our search for new objects */
479 switch (clicked_regionview->region()->coverage (first_frame, last_frame)) {
481 if (last_frame < clicked_regionview->region()->first_frame()) {
482 first_frame = last_frame;
483 last_frame = clicked_regionview->region()->last_frame();
485 last_frame = first_frame;
486 first_frame = clicked_regionview->region()->first_frame();
490 case OverlapExternal:
491 if (last_frame < clicked_regionview->region()->first_frame()) {
492 first_frame = last_frame;
493 last_frame = clicked_regionview->region()->last_frame();
495 last_frame = first_frame;
496 first_frame = clicked_regionview->region()->first_frame();
500 case OverlapInternal:
501 if (last_frame < clicked_regionview->region()->first_frame()) {
502 first_frame = last_frame;
503 last_frame = clicked_regionview->region()->last_frame();
505 last_frame = first_frame;
506 first_frame = clicked_regionview->region()->first_frame();
512 /* nothing to do except add clicked region to selection, since it
513 overlaps with the existing selection in this track.
518 /* 2. find all selectable objects (regionviews in this case) between that one and the end of the
519 one that was clicked.
522 set<RouteTimeAxisView*> relevant_tracks;
524 get_relevant_tracks (relevant_tracks);
526 for (set<RouteTimeAxisView*>::iterator t = relevant_tracks.begin(); t != relevant_tracks.end(); ++t) {
527 (*t)->get_selectables (first_frame, last_frame, -1.0, -1.0, results);
530 /* 3. convert to a vector of audio regions */
532 vector<RegionView*> regions;
534 for (list<Selectable*>::iterator x = results.begin(); x != results.end(); ++x) {
537 if ((arv = dynamic_cast<RegionView*>(*x)) != 0) {
538 regions.push_back (arv);
542 if (!regions.empty()) {
543 selection->add (regions);
553 Editor::set_selected_regionview_from_region_list (boost::shared_ptr<Region> region, Selection::Operation op)
555 vector<RegionView*> all_equivalent_regions;
557 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
559 RouteTimeAxisView* tatv;
561 if ((tatv = dynamic_cast<RouteTimeAxisView*> (*i)) != 0) {
563 boost::shared_ptr<Playlist> pl;
564 vector<boost::shared_ptr<Region> > results;
566 boost::shared_ptr<Diskstream> ds;
568 if ((ds = tatv->get_diskstream()) == 0) {
573 if ((pl = (ds->playlist())) != 0) {
574 pl->get_region_list_equivalent_regions (region, results);
577 for (vector<boost::shared_ptr<Region> >::iterator ir = results.begin(); ir != results.end(); ++ir) {
578 if ((marv = tatv->view()->find_view (*ir)) != 0) {
579 all_equivalent_regions.push_back (marv);
586 begin_reversible_command (_("set selected regions"));
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);
604 commit_reversible_command () ;
608 Editor::track_selection_changed ()
610 switch (selection->tracks.size()){
614 set_selected_mixer_strip (*(selection->tracks.front()));
618 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
619 (*i)->set_selected (false);
620 if (mouse_mode == MouseRange) {
621 (*i)->hide_selection ();
625 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
626 (*i)->set_selected (true);
627 if (mouse_mode == MouseRange) {
628 (*i)->show_selection (selection->time);
634 Editor::time_selection_changed ()
636 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
637 (*i)->hide_selection ();
640 if (selection->tracks.empty()) {
641 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
642 (*i)->show_selection (selection->time);
645 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
646 (*i)->show_selection (selection->time);
650 if (selection->time.empty()) {
651 ActionManager::set_sensitive (ActionManager::time_selection_sensitive_actions, false);
653 ActionManager::set_sensitive (ActionManager::time_selection_sensitive_actions, true);
658 Editor::region_selection_changed ()
660 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
661 (*i)->set_selected_regionviews (selection->regions);
666 Editor::point_selection_changed ()
668 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
669 (*i)->set_selected_points (selection->points);
673 /** Select everything in the selected tracks
674 * @param Selection operation to apply.
677 Editor::select_all_in_selected_tracks (Selection::Operation op)
679 list<Selectable *> touched;
681 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
682 (*i)->get_selectables (0, max_frames, 0, DBL_MAX, touched);
686 case Selection::Toggle:
687 selection->add (touched);
690 selection->set (touched);
692 case Selection::Extend:
693 /* meaningless, because we're selecting everything */
696 selection->add (touched);
702 Editor::select_all (Selection::Operation op)
704 list<Selectable *> touched;
706 for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
707 if ((*iter)->hidden()) {
710 (*iter)->get_selectables (0, max_frames, 0, DBL_MAX, touched);
712 begin_reversible_command (_("select all"));
715 selection->add (touched);
717 case Selection::Toggle:
718 selection->add (touched);
721 selection->set (touched);
723 case Selection::Extend:
724 /* meaningless, because we're selecting everything */
727 commit_reversible_command ();
730 /** Invert the selection in the selected tracks */
732 Editor::invert_selection_in_selected_tracks ()
734 list<Selectable *> touched;
736 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
737 (*i)->get_inverted_selectables (*selection, touched);
740 selection->set (touched);
744 Editor::invert_selection ()
746 list<Selectable *> touched;
748 for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
749 if ((*iter)->hidden()) {
752 (*iter)->get_inverted_selectables (*selection, touched);
755 selection->set (touched);
759 Editor::select_all_within (nframes_t start, nframes_t end, double top, double bot, const TrackViewList& tracklist, Selection::Operation op)
761 list<Selectable*> touched;
762 list<Selectable*>::size_type n = 0;
763 TrackViewList touched_tracks;
765 for (TrackViewList::const_iterator iter = tracklist.begin(); iter != tracklist.end(); ++iter) {
766 if ((*iter)->hidden()) {
772 (*iter)->get_selectables (start, end, top, bot, touched);
774 if (n != touched.size()) {
775 touched_tracks.push_back (*iter);
779 if (touched.empty()) {
783 if (!touched_tracks.empty()) {
787 selection->add (touched_tracks);
789 case Selection::Toggle:
790 selection->toggle (touched_tracks);
793 selection->set (touched_tracks);
795 case Selection::Extend:
796 /* not defined yet */
801 begin_reversible_command (_("select all within"));
804 selection->add (touched);
806 case Selection::Toggle:
807 selection->toggle (touched);
810 selection->set (touched);
812 case Selection::Extend:
813 /* not defined yet */
817 commit_reversible_command ();
819 return !touched.empty();
823 Editor::set_selection_from_audio_region ()
825 if (selection->regions.empty()) {
829 RegionView* rv = *(selection->regions.begin());
830 boost::shared_ptr<Region> region = rv->region();
832 begin_reversible_command (_("set selection from region"));
833 selection->set (0, region->position(), region->last_frame());
834 commit_reversible_command ();
836 set_mouse_mode (Editing::MouseRange, false);
840 Editor::set_selection_from_punch()
844 if ((location = session->locations()->auto_punch_location()) == 0) {
848 set_selection_from_range (*location);
852 Editor::set_selection_from_loop()
856 if ((location = session->locations()->auto_loop_location()) == 0) {
859 set_selection_from_range (*location);
863 Editor::set_selection_from_range (Location& loc)
865 begin_reversible_command (_("set selection from range"));
866 selection->set (0, loc.start(), loc.end());
867 commit_reversible_command ();
869 set_mouse_mode (Editing::MouseRange, false);
873 Editor::select_all_selectables_using_time_selection ()
875 list<Selectable *> touched;
877 if (selection->time.empty()) {
881 nframes_t start = selection->time[clicked_selection].start;
882 nframes_t end = selection->time[clicked_selection].end;
884 if (end - start < 1) {
888 for (TrackViewList::iterator iter = selection->tracks.begin(); iter != selection->tracks.end(); ++iter) {
889 if ((*iter)->hidden()) {
892 (*iter)->get_selectables (start, end - 1, 0, DBL_MAX, touched);
895 begin_reversible_command (_("select all from range"));
896 selection->set (touched);
897 commit_reversible_command ();
902 Editor::select_all_selectables_using_punch()
904 Location* location = session->locations()->auto_punch_location();
905 list<Selectable *> touched;
907 if (location == 0 || (location->end() - location->start() <= 1)) {
911 for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
912 if ((*iter)->hidden()) {
915 (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
917 begin_reversible_command (_("select all from punch"));
918 selection->set (touched);
919 commit_reversible_command ();
924 Editor::select_all_selectables_using_loop()
926 Location* location = session->locations()->auto_loop_location();
927 list<Selectable *> touched;
929 if (location == 0 || (location->end() - location->start() <= 1)) {
933 for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
934 if ((*iter)->hidden()) {
937 (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
939 begin_reversible_command (_("select all from loop"));
940 selection->set (touched);
941 commit_reversible_command ();
946 Editor::select_all_selectables_using_cursor (Cursor *cursor, bool after)
950 list<Selectable *> touched;
953 begin_reversible_command (_("select all after cursor"));
954 start = cursor->current_frame ;
955 end = session->current_end_frame();
957 if (cursor->current_frame > 0) {
958 begin_reversible_command (_("select all before cursor"));
960 end = cursor->current_frame - 1;
966 for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
967 if ((*iter)->hidden()) {
970 (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
972 selection->set (touched);
973 commit_reversible_command ();
977 Editor::select_all_selectables_between_cursors (Cursor *cursor, Cursor *other_cursor)
981 list<Selectable *> touched;
982 bool other_cursor_is_first = cursor->current_frame > other_cursor->current_frame;
984 if (cursor->current_frame == other_cursor->current_frame) {
988 begin_reversible_command (_("select all between cursors"));
989 if (other_cursor_is_first) {
990 start = other_cursor->current_frame;
991 end = cursor->current_frame - 1;
994 start = cursor->current_frame;
995 end = other_cursor->current_frame - 1;
998 for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
999 if ((*iter)->hidden()) {
1002 (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
1004 selection->set (touched);
1005 commit_reversible_command ();