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