Use ardour/session_state_utils.h in Editor::redisplay_snapshots
[ardour.git] / gtk2_ardour / editor_selection.cc
1 /*
2     Copyright (C) 2000-2006 Paul Davis 
3
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.
8
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.
13
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.
17
18 */
19
20 #include <pbd/stacktrace.h>
21
22 #include <ardour/diskstream.h>
23 #include <ardour/playlist.h>
24 #include <ardour/route_group.h>
25
26 #include "editor.h"
27 #include "actions.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"
33
34 #include "i18n.h"
35
36 using namespace std;
37 using namespace sigc;
38 using namespace ARDOUR;
39 using namespace PBD;
40 using namespace Gtk;
41 using namespace Glib;
42 using namespace Gtkmm2ext;
43 using namespace Editing;
44
45 struct TrackViewByPositionSorter
46 {
47     bool operator() (const TimeAxisView* a, const TimeAxisView *b) {
48             return a->y_position < b->y_position;
49     }
50 };
51
52 bool
53 Editor::extend_selection_to_track (TimeAxisView& view)
54 {
55         if (selection->selected (&view)) {
56                 /* already selected, do nothing */
57                 return false;
58         }
59
60         if (selection->tracks.empty()) {
61
62                 if (!selection->selected (&view)) {
63                         selection->set (&view);
64                         return true;
65                 } else {
66                         return false;
67                 }
68         } 
69
70         /* something is already selected, so figure out which range of things to add */
71         
72         TrackViewList to_be_added;
73         TrackViewList sorted = track_views;
74         TrackViewByPositionSorter cmp;
75         bool passed_clicked = false;
76         bool forwards = true;
77
78         sorted.sort (cmp);
79
80         if (!selection->selected (&view)) {
81                 to_be_added.push_back (&view);
82         }
83
84         /* figure out if we should go forward or backwards */
85
86         for (TrackViewList::iterator i = sorted.begin(); i != sorted.end(); ++i) {
87
88                 if ((*i) == &view) {
89                         passed_clicked = true;
90                 }
91
92                 if (selection->selected (*i)) {
93                         if (passed_clicked) {
94                                 forwards = true;
95                         } else {
96                                 forwards = false;
97                         }
98                         break;
99                 }
100         }
101                         
102         passed_clicked = false;
103
104         if (forwards) {
105
106                 for (TrackViewList::iterator i = sorted.begin(); i != sorted.end(); ++i) {
107                                         
108                         if ((*i) == &view) {
109                                 passed_clicked = true;
110                                 continue;
111                         }
112                                         
113                         if (passed_clicked) {
114                                 if ((*i)->hidden()) {
115                                         continue;
116                                 }
117                                 if (selection->selected (*i)) {
118                                         break;
119                                 } else if (!(*i)->hidden()) {
120                                         to_be_added.push_back (*i);
121                                 }
122                         }
123                 }
124
125         } else {
126
127                 for (TrackViewList::reverse_iterator r = sorted.rbegin(); r != sorted.rend(); ++r) {
128                                         
129                         if ((*r) == &view) {
130                                 passed_clicked = true;
131                                 continue;
132                         }
133                                         
134                         if (passed_clicked) {
135                                                 
136                                 if ((*r)->hidden()) {
137                                         continue;
138                                 }
139                                                 
140                                 if (selection->selected (*r)) {
141                                         break;
142                                 } else if (!(*r)->hidden()) {
143                                         to_be_added.push_back (*r);
144                                 }
145                         }
146                 }
147         }
148                         
149         if (!to_be_added.empty()) {
150                 selection->add (to_be_added);
151                 return true;
152         }
153         
154         return false;
155 }
156
157 void
158 Editor::select_all_tracks ()
159 {
160         selection->set (track_views);
161 }
162
163 bool
164 Editor::set_selected_track (TimeAxisView& view, Selection::Operation op, bool no_remove)
165 {
166         bool commit = false;
167
168         switch (op) {
169         case Selection::Toggle:
170                 if (selection->selected (&view)) {
171                         if (!no_remove) {
172                                 selection->remove (&view);
173                                 commit = true;
174                         }
175                 } else {
176                         selection->add (&view);
177                         commit = false;
178                 }
179                 break;
180
181         case Selection::Add:
182                 if (!selection->selected (&view)) {
183                         selection->add (&view);
184                         commit = true;
185                 }
186                 break;
187
188         case Selection::Set:
189                 if (selection->selected (&view) && selection->tracks.size() == 1) {
190                         /* no commit necessary */
191                 } else {
192                         
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
197                            for some reason.
198                         */
199
200                         if (selection->tracks.empty()) {
201                                 selection->set (&view);
202                                 commit = true;
203                         } else if (selection->tracks.size() == 1 || !no_remove) {
204                                 selection->set (&view);
205                                 commit = true;
206                         }
207                 }
208                 break;
209                 
210         case Selection::Extend:
211                 commit = extend_selection_to_track (view);
212                 break;
213         }
214
215         return commit;
216 }
217
218 bool
219 Editor::set_selected_track_from_click (bool press, Selection::Operation op, bool no_remove)
220 {
221         if (!clicked_routeview) {
222                 return false;
223         }
224         
225         if (!press) {
226                 return false;
227         }
228
229         return set_selected_track (*clicked_routeview, op, no_remove);
230 }
231
232 bool
233 Editor::set_selected_control_point_from_click (Selection::Operation op, bool no_remove)
234 {
235         if (!clicked_control_point) {
236                 return false;
237         }
238
239         /* select this point and any others that it represents */
240
241         double y1, y2;
242         nframes_t x1, x2;
243
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;
248
249         return select_all_within (x1, x2, y1, y2, selection->tracks, op);
250 }
251
252 void
253 Editor::get_relevant_tracks (set<RouteTimeAxisView*>& relevant_tracks)
254 {
255         /* step one: get all selected tracks and all tracks in the relevant edit groups */
256
257         for (TrackSelection::iterator ti = selection->tracks.begin(); ti != selection->tracks.end(); ++ti) {
258
259                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(*ti);
260
261                 if (!rtv) {
262                         continue;
263                 }
264
265                 RouteGroup* group = rtv->route()->edit_group();
266
267                 if (group && group->is_active()) {
268                         
269                         /* active group for this track, loop over all tracks and get every member of the group */
270
271                         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
272                                 
273                                 RouteTimeAxisView* trtv;
274                                 
275                                 if ((trtv = dynamic_cast<RouteTimeAxisView*> (*i)) != 0) {
276                                         
277                                         if (trtv->route()->edit_group() == group) {
278                                                 relevant_tracks.insert (trtv);
279                                         }
280                                 }
281                         }
282                 } else {
283                         relevant_tracks.insert (rtv);
284                 }
285         }
286 }
287
288 void
289 Editor::mapover_tracks (slot<void,RouteTimeAxisView&,uint32_t> sl)
290 {
291         set<RouteTimeAxisView*> relevant_tracks;
292
293         get_relevant_tracks (relevant_tracks);
294
295         uint32_t sz = relevant_tracks.size();
296
297         for (set<RouteTimeAxisView*>::iterator rti = relevant_tracks.begin(); rti != relevant_tracks.end(); ++rti) {
298                 sl (**rti, sz);
299         }
300 }
301
302 void
303 Editor::mapped_get_equivalent_regions (RouteTimeAxisView& tv, uint32_t ignored, RegionView* basis, vector<RegionView*>* all_equivs)
304 {
305         boost::shared_ptr<Playlist> pl;
306         vector<boost::shared_ptr<Region> > results;
307         RegionView* marv;
308         boost::shared_ptr<Diskstream> ds;
309
310         if ((ds = tv.get_diskstream()) == 0) {
311                 /* bus */
312                 return;
313         }
314
315         if (&tv == &basis->get_time_axis_view()) {
316                 /* looking in same track as the original */
317                 return;
318         }
319
320         if ((pl = ds->playlist()) != 0) {
321                 pl->get_equivalent_regions (basis->region(), results);
322         }
323
324         for (vector<boost::shared_ptr<Region> >::iterator ir = results.begin(); ir != results.end(); ++ir) {
325                 if ((marv = tv.view()->find_view (*ir)) != 0) {
326                         all_equivs->push_back (marv);
327                 }
328         }
329 }
330
331 void
332 Editor::get_equivalent_regions (RegionView* basis, vector<RegionView*>& equivalent_regions)
333 {
334         mapover_tracks (bind (mem_fun (*this, &Editor::mapped_get_equivalent_regions), basis, &equivalent_regions));
335         
336         /* add clicked regionview since we skipped all other regions in the same track as the one it was in */
337         
338         equivalent_regions.push_back (basis);
339 }
340
341 bool
342 Editor::set_selected_regionview_from_click (bool press, Selection::Operation op, bool no_track_remove)
343 {
344         vector<RegionView*> all_equivalent_regions;
345         bool commit = false;
346
347         if (!clicked_regionview || !clicked_routeview) {
348                 return false;
349         }
350
351         if (press) {
352                 button_release_can_deselect = false;
353         } 
354
355         if (op == Selection::Toggle || op == Selection::Set) {
356
357
358                 switch (op) {
359                 case Selection::Toggle:
360                         
361                         if (clicked_regionview->get_selected()) {
362                                 if (press) {
363
364                                         /* whatever was clicked was selected already; do nothing here but allow
365                                            the button release to deselect it
366                                         */
367
368                                         button_release_can_deselect = true;
369
370                                 } else {
371
372                                         if (button_release_can_deselect) {
373
374                                                 /* just remove this one region, but only on a permitted button release */
375
376                                                 selection->remove (clicked_regionview);
377                                                 commit = true;
378
379                                                 /* no more deselect action on button release till a new press
380                                                    finds an already selected object.
381                                                 */
382
383                                                 button_release_can_deselect = false;
384                                         }
385                                 } 
386
387                         } else {
388
389                                 if (press) {
390
391                                         if (selection->selected (clicked_routeview)) {
392                                                 get_equivalent_regions (clicked_regionview, all_equivalent_regions);
393                                         } else {
394                                                 all_equivalent_regions.push_back (clicked_regionview);
395                                         }
396
397                                         /* add all the equivalent regions, but only on button press */
398                                         
399
400
401                                         if (!all_equivalent_regions.empty()) {
402                                                 commit = true;
403                                         }
404
405                                         selection->add (all_equivalent_regions);
406                                 } 
407                         }
408                         break;
409                         
410                 case Selection::Set:
411                         if (!clicked_regionview->get_selected()) {
412
413                                 if (selection->selected (clicked_routeview)) {
414                                         get_equivalent_regions (clicked_regionview, all_equivalent_regions);
415                                 } else {
416                                         all_equivalent_regions.push_back (clicked_regionview);
417                                 }
418
419                                 selection->set (all_equivalent_regions);
420                                 commit = true;
421                         } else {
422                                 /* no commit necessary: clicked on an already selected region */
423                                 goto out;
424                         }
425                         break;
426
427                 default:
428                         /* silly compiler */
429                         break;
430                 }
431
432         } else if (op == Selection::Extend) {
433
434                 list<Selectable*> results;
435                 nframes_t last_frame;
436                 nframes_t first_frame;
437
438                 /* 1. find the last selected regionview in the track that was clicked in */
439
440                 last_frame = 0;
441                 first_frame = max_frames;
442
443                 for (RegionSelection::iterator x = selection->regions.begin(); x != selection->regions.end(); ++x) {
444                         if (&(*x)->get_time_axis_view() == &clicked_regionview->get_time_axis_view()) {
445
446                                 if ((*x)->region()->last_frame() > last_frame) {
447                                         last_frame = (*x)->region()->last_frame();
448                                 }
449
450                                 if ((*x)->region()->first_frame() < first_frame) {
451                                         first_frame = (*x)->region()->first_frame();
452                                 }
453                         }
454                 }
455
456                 /* 2. figure out the boundaries for our search for new objects */
457
458                 switch (clicked_regionview->region()->coverage (first_frame, last_frame)) {
459                 case OverlapNone:
460                         if (last_frame < clicked_regionview->region()->first_frame()) {
461                                 first_frame = last_frame;
462                                 last_frame = clicked_regionview->region()->last_frame();
463                         } else {
464                                 last_frame = first_frame;
465                                 first_frame = clicked_regionview->region()->first_frame();
466                         }
467                         break;
468
469                 case OverlapExternal:
470                         if (last_frame < clicked_regionview->region()->first_frame()) {
471                                 first_frame = last_frame;
472                                 last_frame = clicked_regionview->region()->last_frame();
473                         } else {
474                                 last_frame = first_frame;
475                                 first_frame = clicked_regionview->region()->first_frame();
476                         }
477                         break;
478
479                 case OverlapInternal:
480                         if (last_frame < clicked_regionview->region()->first_frame()) {
481                                 first_frame = last_frame;
482                                 last_frame = clicked_regionview->region()->last_frame();
483                         } else {
484                                 last_frame = first_frame;
485                                 first_frame = clicked_regionview->region()->first_frame();
486                         }
487                         break;
488
489                 case OverlapStart:
490                 case OverlapEnd:
491                         /* nothing to do except add clicked region to selection, since it
492                            overlaps with the existing selection in this track.
493                         */
494                         break;
495                 }
496
497                 /* 2. find all selectable objects (regionviews in this case) between that one and the end of the
498                       one that was clicked.
499                 */
500
501                 set<RouteTimeAxisView*> relevant_tracks;
502                 
503                 get_relevant_tracks (relevant_tracks);
504
505                 for (set<RouteTimeAxisView*>::iterator t = relevant_tracks.begin(); t != relevant_tracks.end(); ++t) {
506                         (*t)->get_selectables (first_frame, last_frame, -1.0, -1.0, results);
507                 }
508                 
509                 /* 3. convert to a vector of audio regions */
510
511                 vector<RegionView*> regions;
512                 
513                 for (list<Selectable*>::iterator x = results.begin(); x != results.end(); ++x) {
514                         RegionView* arv;
515
516                         if ((arv = dynamic_cast<RegionView*>(*x)) != 0) {
517                                 regions.push_back (arv);
518                         }
519                 }
520
521                 if (!regions.empty()) {
522                         selection->add (regions);
523                         commit = true;
524                 }
525         }
526
527   out:
528         return commit;
529 }
530
531 void
532 Editor::set_selected_regionview_from_region_list (boost::shared_ptr<Region> region, Selection::Operation op)
533 {
534         vector<RegionView*> all_equivalent_regions;
535
536         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
537                 
538                 RouteTimeAxisView* tatv;
539                 
540                 if ((tatv = dynamic_cast<RouteTimeAxisView*> (*i)) != 0) {
541                         
542                         boost::shared_ptr<Playlist> pl;
543                         vector<boost::shared_ptr<Region> > results;
544                         RegionView* marv;
545                         boost::shared_ptr<Diskstream> ds;
546                         
547                         if ((ds = tatv->get_diskstream()) == 0) {
548                                 /* bus */
549                                 continue;
550                         }
551                         
552                         if ((pl = (ds->playlist())) != 0) {
553                                 pl->get_region_list_equivalent_regions (region, results);
554                         }
555                         
556                         for (vector<boost::shared_ptr<Region> >::iterator ir = results.begin(); ir != results.end(); ++ir) {
557                                 if ((marv = tatv->view()->find_view (*ir)) != 0) {
558                                         all_equivalent_regions.push_back (marv);
559                                 }
560                         }
561                         
562                 }
563         }
564         
565         begin_reversible_command (_("set selected regions"));
566         
567         switch (op) {
568         case Selection::Toggle:
569                 /* XXX this is not correct */
570                 selection->toggle (all_equivalent_regions);
571                 break;
572         case Selection::Set:
573                 selection->set (all_equivalent_regions);
574                 break;
575         case Selection::Extend:
576                 selection->add (all_equivalent_regions);
577                 break;
578         case Selection::Add:
579                 selection->add (all_equivalent_regions);
580                 break;
581         }
582
583         commit_reversible_command () ;
584 }
585
586 void
587 Editor::track_selection_changed ()
588 {
589         switch (selection->tracks.size()){
590         case 0:
591                 break;
592         default:
593                 set_selected_mixer_strip (*(selection->tracks.front()));
594                 break;
595         }
596
597         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
598                 (*i)->set_selected (false);
599                 if (mouse_mode == MouseRange) {
600                         (*i)->hide_selection ();
601                 }
602         }
603
604         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
605                 (*i)->set_selected (true);
606                 if (mouse_mode == MouseRange) {
607                         (*i)->show_selection (selection->time);
608                 }
609         }
610 }
611
612 void
613 Editor::time_selection_changed ()
614 {
615         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
616                 (*i)->hide_selection ();
617         }
618
619         if (selection->tracks.empty()) {
620                 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
621                         (*i)->show_selection (selection->time);
622                 }
623         } else {
624                 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
625                         (*i)->show_selection (selection->time);
626                 }
627         }
628
629         if (selection->time.empty()) {
630                 ActionManager::set_sensitive (ActionManager::time_selection_sensitive_actions, false);
631         } else {
632                 ActionManager::set_sensitive (ActionManager::time_selection_sensitive_actions, true);
633         }
634 }
635
636 void
637 Editor::region_selection_changed ()
638 {
639         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
640                 (*i)->set_selected_regionviews (selection->regions);
641         }
642 }
643
644 void
645 Editor::point_selection_changed ()
646 {
647         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
648                 (*i)->set_selected_points (selection->points);
649         }
650 }
651
652 /** Select everything in the selected tracks
653  * @param Selection operation to apply.
654  */
655 void
656 Editor::select_all_in_selected_tracks (Selection::Operation op)
657 {
658         list<Selectable *> touched;
659
660         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
661                 (*i)->get_selectables (0, max_frames, 0, DBL_MAX, touched);
662         }
663
664         switch (op) {
665         case Selection::Toggle:
666                 selection->add (touched);
667                 break;
668         case Selection::Set:
669                 selection->set (touched);
670                 break;
671         case Selection::Extend:
672                 /* meaningless, because we're selecting everything */
673                 break;
674         case Selection::Add:
675                 selection->add (touched);
676                 break;
677         }
678 }
679
680 void
681 Editor::select_all (Selection::Operation op)
682 {
683         list<Selectable *> touched;
684         
685         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
686                 if ((*iter)->hidden()) {
687                         continue;
688                 }
689                 (*iter)->get_selectables (0, max_frames, 0, DBL_MAX, touched);
690         }
691         begin_reversible_command (_("select all"));
692         switch (op) {
693         case Selection::Add:
694                 selection->add (touched);
695                 break;
696         case Selection::Toggle:
697                 selection->add (touched);
698                 break;
699         case Selection::Set:
700                 selection->set (touched);
701                 break;
702         case Selection::Extend:
703                 /* meaningless, because we're selecting everything */
704                 break;
705         }
706         commit_reversible_command ();
707 }
708
709 /** Invert the selection in the selected tracks */
710 void
711 Editor::invert_selection_in_selected_tracks ()
712 {
713         list<Selectable *> touched;
714
715         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
716                 (*i)->get_inverted_selectables (*selection, touched);
717         }
718         
719         selection->set (touched);
720 }
721
722 void
723 Editor::invert_selection ()
724 {
725         list<Selectable *> touched;
726         
727         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
728                 if ((*iter)->hidden()) {
729                         continue;
730                 }
731                 (*iter)->get_inverted_selectables (*selection, touched);
732         }
733
734         selection->set (touched);
735 }
736
737 bool
738 Editor::select_all_within (nframes_t start, nframes_t end, double top, double bot, const TrackViewList& tracklist, Selection::Operation op)
739 {
740         list<Selectable*> touched;
741         list<Selectable*>::size_type n = 0;
742         TrackViewList touched_tracks;
743
744         for (TrackViewList::const_iterator iter = tracklist.begin(); iter != tracklist.end(); ++iter) {
745                 if ((*iter)->hidden()) {
746                         continue;
747                 }
748
749                 n = touched.size();
750
751                 (*iter)->get_selectables (start, end, top, bot, touched);
752                 
753                 if (n != touched.size()) {
754                         touched_tracks.push_back (*iter);
755                 }
756         }
757
758         if (touched.empty()) {
759                 return false;
760         }
761
762         if (!touched_tracks.empty()) {
763
764                 switch (op) {
765                 case Selection::Add:
766                         selection->add (touched_tracks);
767                         break;
768                 case Selection::Toggle:
769                         selection->toggle (touched_tracks);
770                         break;
771                 case Selection::Set:
772                         selection->set (touched_tracks);
773                         break;
774                 case Selection::Extend:
775                         /* not defined yet */
776                         break;
777                 }
778         }
779
780         begin_reversible_command (_("select all within"));
781         switch (op) {
782         case Selection::Add:
783                 selection->add (touched);
784                 break;
785         case Selection::Toggle:
786                 selection->toggle (touched);
787                 break;
788         case Selection::Set:
789                 selection->set (touched);
790                 break;
791         case Selection::Extend:
792                 /* not defined yet */
793                 break;
794         }
795         
796         commit_reversible_command ();
797
798         return !touched.empty();
799 }
800
801 void
802 Editor::set_selection_from_audio_region ()
803 {
804         if (selection->regions.empty()) {
805                 return;
806         }
807
808         RegionView* rv = *(selection->regions.begin());
809         boost::shared_ptr<Region> region = rv->region();
810         
811         begin_reversible_command (_("set selection from region"));
812         selection->set (0, region->position(), region->last_frame());
813         commit_reversible_command ();
814
815         set_mouse_mode (Editing::MouseRange, false);
816 }
817
818 void
819 Editor::set_selection_from_punch()
820 {
821         Location* location;
822
823         if ((location = session->locations()->auto_punch_location()) == 0)  {
824                 return;
825         }
826
827         set_selection_from_range (*location);
828 }
829
830 void
831 Editor::set_selection_from_loop()
832 {
833         Location* location;
834
835         if ((location = session->locations()->auto_loop_location()) == 0)  {
836                 return;
837         }
838         set_selection_from_range (*location);
839 }
840
841 void
842 Editor::set_selection_from_range (Location& loc)
843 {
844         begin_reversible_command (_("set selection from range"));
845         selection->set (0, loc.start(), loc.end());
846         commit_reversible_command ();
847
848         set_mouse_mode (Editing::MouseRange, false);
849 }
850
851 void
852 Editor::select_all_selectables_using_time_selection ()
853 {
854         list<Selectable *> touched;
855
856         if (selection->time.empty()) {
857                 return;
858         }
859
860         nframes_t start = selection->time[clicked_selection].start;
861         nframes_t end = selection->time[clicked_selection].end;
862
863         if (end - start < 1)  {
864                 return;
865         }
866
867         for (TrackViewList::iterator iter = selection->tracks.begin(); iter != selection->tracks.end(); ++iter) {
868                 if ((*iter)->hidden()) {
869                         continue;
870                 }
871                 (*iter)->get_selectables (start, end - 1, 0, DBL_MAX, touched);
872         }
873
874         begin_reversible_command (_("select all from range"));
875         selection->set (touched);
876         commit_reversible_command ();
877 }
878
879
880 void
881 Editor::select_all_selectables_using_punch()
882 {
883         Location* location = session->locations()->auto_punch_location();
884         list<Selectable *> touched;
885
886         if (location == 0 || (location->end() - location->start() <= 1))  {
887                 return;
888         }
889
890         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
891                 if ((*iter)->hidden()) {
892                         continue;
893                 }
894                 (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
895         }
896         begin_reversible_command (_("select all from punch"));
897         selection->set (touched);
898         commit_reversible_command ();
899
900 }
901
902 void
903 Editor::select_all_selectables_using_loop()
904 {
905         Location* location = session->locations()->auto_loop_location();
906         list<Selectable *> touched;
907
908         if (location == 0 || (location->end() - location->start() <= 1))  {
909                 return;
910         }
911
912         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
913                 if ((*iter)->hidden()) {
914                         continue;
915                 }
916                 (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
917         }
918         begin_reversible_command (_("select all from loop"));
919         selection->set (touched);
920         commit_reversible_command ();
921
922 }
923
924 void
925 Editor::select_all_selectables_using_cursor (Cursor *cursor, bool after)
926 {
927         nframes_t start;
928         nframes_t end;
929         list<Selectable *> touched;
930
931         if (after) {
932                 begin_reversible_command (_("select all after cursor"));
933                 start = cursor->current_frame ;
934                 end = session->current_end_frame();
935         } else {
936                 if (cursor->current_frame > 0) {
937                         begin_reversible_command (_("select all before cursor"));
938                         start = 0;
939                         end = cursor->current_frame - 1;
940                 } else {
941                         return;
942                 }
943         }
944
945         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
946                 if ((*iter)->hidden()) {
947                         continue;
948                 }
949                 (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
950         }
951         selection->set (touched);
952         commit_reversible_command ();
953 }
954
955 void
956 Editor::select_all_selectables_between_cursors (Cursor *cursor, Cursor *other_cursor)
957 {
958         nframes_t start;
959         nframes_t end;
960         list<Selectable *> touched;
961         bool  other_cursor_is_first = cursor->current_frame > other_cursor->current_frame;
962
963         if (cursor->current_frame == other_cursor->current_frame) {
964                 return;
965         }
966
967         begin_reversible_command (_("select all between cursors"));
968         if (other_cursor_is_first) {
969                 start = other_cursor->current_frame;
970                 end = cursor->current_frame - 1;
971                 
972         } else {
973                 start = cursor->current_frame;
974                 end = other_cursor->current_frame - 1;
975         }
976         
977         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
978                 if ((*iter)->hidden()) {
979                         continue;
980                 }
981                 (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
982         }
983         selection->set (touched);
984         commit_reversible_command ();
985 }
986