554f5c341d4372fadc1d7cff8a9587726fb27ad7
[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 /**
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_tracks (slot<void, RouteTimeAxisView&, uint32_t> sl, TimeAxisView* basis) const
297 {
298         RouteTimeAxisView* route_basis = dynamic_cast<RouteTimeAxisView*> (basis);
299         if (route_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<RouteTimeAxisView*> tracks;
307
308         /* always call for the basis */
309         tracks.insert (route_basis);
310
311         RouteGroup* group = route_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::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) {
318                                 tracks.insert (v);
319                         }
320                 }
321         }
322
323         /* call the slots */
324         uint32_t const sz = tracks.size ();
325         for (set<RouteTimeAxisView*>::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) const
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) const
361 {
362         mapover_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_routeview) {
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_routeview)) {
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                                 selection->set (clicked_regionview);
441                                 commit = true;
442                         } else {
443                                 /* no commit necessary: clicked on an already selected region */
444                                 goto out;
445                         }
446                         break;
447
448                 default:
449                         /* silly compiler */
450                         break;
451                 }
452
453         } else if (op == Selection::Extend) {
454
455                 list<Selectable*> results;
456                 nframes_t last_frame;
457                 nframes_t first_frame;
458
459                 /* 1. find the last selected regionview in the track that was clicked in */
460
461                 last_frame = 0;
462                 first_frame = max_frames;
463
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()) {
466
467                                 if ((*x)->region()->last_frame() > last_frame) {
468                                         last_frame = (*x)->region()->last_frame();
469                                 }
470
471                                 if ((*x)->region()->first_frame() < first_frame) {
472                                         first_frame = (*x)->region()->first_frame();
473                                 }
474                         }
475                 }
476
477                 /* 2. figure out the boundaries for our search for new objects */
478
479                 switch (clicked_regionview->region()->coverage (first_frame, last_frame)) {
480                 case OverlapNone:
481                         if (last_frame < clicked_regionview->region()->first_frame()) {
482                                 first_frame = last_frame;
483                                 last_frame = clicked_regionview->region()->last_frame();
484                         } else {
485                                 last_frame = first_frame;
486                                 first_frame = clicked_regionview->region()->first_frame();
487                         }
488                         break;
489
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();
494                         } else {
495                                 last_frame = first_frame;
496                                 first_frame = clicked_regionview->region()->first_frame();
497                         }
498                         break;
499
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();
504                         } else {
505                                 last_frame = first_frame;
506                                 first_frame = clicked_regionview->region()->first_frame();
507                         }
508                         break;
509
510                 case OverlapStart:
511                 case OverlapEnd:
512                         /* nothing to do except add clicked region to selection, since it
513                            overlaps with the existing selection in this track.
514                         */
515                         break;
516                 }
517
518                 /* 2. find all selectable objects (regionviews in this case) between that one and the end of the
519                       one that was clicked.
520                 */
521
522                 set<RouteTimeAxisView*> relevant_tracks;
523                 
524                 get_relevant_tracks (relevant_tracks);
525
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);
528                 }
529                 
530                 /* 3. convert to a vector of audio regions */
531
532                 vector<RegionView*> regions;
533                 
534                 for (list<Selectable*>::iterator x = results.begin(); x != results.end(); ++x) {
535                         RegionView* arv;
536
537                         if ((arv = dynamic_cast<RegionView*>(*x)) != 0) {
538                                 regions.push_back (arv);
539                         }
540                 }
541
542                 if (!regions.empty()) {
543                         selection->add (regions);
544                         commit = true;
545                 }
546         }
547
548   out:
549         return commit;
550 }
551
552 void
553 Editor::set_selected_regionview_from_region_list (boost::shared_ptr<Region> region, Selection::Operation op)
554 {
555         vector<RegionView*> all_equivalent_regions;
556
557         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
558                 
559                 RouteTimeAxisView* tatv;
560                 
561                 if ((tatv = dynamic_cast<RouteTimeAxisView*> (*i)) != 0) {
562                         
563                         boost::shared_ptr<Playlist> pl;
564                         vector<boost::shared_ptr<Region> > results;
565                         RegionView* marv;
566                         boost::shared_ptr<Diskstream> ds;
567                         
568                         if ((ds = tatv->get_diskstream()) == 0) {
569                                 /* bus */
570                                 continue;
571                         }
572                         
573                         if ((pl = (ds->playlist())) != 0) {
574                                 pl->get_region_list_equivalent_regions (region, results);
575                         }
576                         
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);
580                                 }
581                         }
582                         
583                 }
584         }
585         
586         begin_reversible_command (_("set selected regions"));
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         commit_reversible_command () ;
605 }
606
607 void
608 Editor::track_selection_changed ()
609 {
610         switch (selection->tracks.size()){
611         case 0:
612                 break;
613         default:
614                 set_selected_mixer_strip (*(selection->tracks.front()));
615                 break;
616         }
617
618         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
619                 if (find (selection->tracks.begin(), selection->tracks.end(), *i) != selection->tracks.end()) {
620                         (*i)->set_selected (true);
621                 } else {
622                         (*i)->set_selected (false);
623                 }
624         }
625 }
626
627 void
628 Editor::time_selection_changed ()
629 {
630         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
631                 (*i)->hide_selection ();
632         }
633
634         if (selection->tracks.empty()) {
635                 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
636                         (*i)->show_selection (selection->time);
637                 }
638         } else {
639                 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
640                         (*i)->show_selection (selection->time);
641                 }
642         }
643
644         if (selection->time.empty()) {
645                 ActionManager::set_sensitive (ActionManager::time_selection_sensitive_actions, false);
646         } else {
647                 ActionManager::set_sensitive (ActionManager::time_selection_sensitive_actions, true);
648         }
649
650 }
651
652 void
653 Editor::region_selection_changed ()
654 {
655         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
656                 (*i)->set_selected_regionviews (selection->regions);
657         }
658 }
659
660 void
661 Editor::point_selection_changed ()
662 {
663         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
664                 (*i)->set_selected_points (selection->points);
665         }
666 }
667
668 /** Select everything in the selected tracks
669  * @param Selection operation to apply.
670  */
671 void
672 Editor::select_all_in_selected_tracks (Selection::Operation op)
673 {
674         list<Selectable *> touched;
675
676         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
677                 (*i)->get_selectables (0, max_frames, 0, DBL_MAX, touched);
678         }
679
680         switch (op) {
681         case Selection::Toggle:
682                 selection->add (touched);
683                 break;
684         case Selection::Set:
685                 selection->set (touched);
686                 break;
687         case Selection::Extend:
688                 /* meaningless, because we're selecting everything */
689                 break;
690         case Selection::Add:
691                 selection->add (touched);
692                 break;
693         }
694 }
695
696 void
697 Editor::select_all (Selection::Operation op)
698 {
699         list<Selectable *> touched;
700         
701         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
702                 if ((*iter)->hidden()) {
703                         continue;
704                 }
705                 (*iter)->get_selectables (0, max_frames, 0, DBL_MAX, touched);
706         }
707         begin_reversible_command (_("select all"));
708         switch (op) {
709         case Selection::Add:
710                 selection->add (touched);
711                 break;
712         case Selection::Toggle:
713                 selection->add (touched);
714                 break;
715         case Selection::Set:
716                 selection->set (touched);
717                 break;
718         case Selection::Extend:
719                 /* meaningless, because we're selecting everything */
720                 break;
721         }
722         commit_reversible_command ();
723 }
724
725 /** Invert the selection in the selected tracks */
726 void
727 Editor::invert_selection_in_selected_tracks ()
728 {
729         list<Selectable *> touched;
730
731         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
732                 (*i)->get_inverted_selectables (*selection, touched);
733         }
734         
735         selection->set (touched);
736 }
737
738 void
739 Editor::invert_selection ()
740 {
741         list<Selectable *> touched;
742         
743         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
744                 if ((*iter)->hidden()) {
745                         continue;
746                 }
747                 (*iter)->get_inverted_selectables (*selection, touched);
748         }
749
750         selection->set (touched);
751 }
752
753 bool
754 Editor::select_all_within (nframes_t start, nframes_t end, double top, double bot, const TrackViewList& tracklist, Selection::Operation op)
755 {
756         list<Selectable*> touched;
757         list<Selectable*>::size_type n = 0;
758         TrackViewList touched_tracks;
759
760         for (TrackViewList::const_iterator iter = tracklist.begin(); iter != tracklist.end(); ++iter) {
761                 if ((*iter)->hidden()) {
762                         continue;
763                 }
764
765                 n = touched.size();
766
767                 (*iter)->get_selectables (start, end, top, bot, touched);
768                 
769                 if (n != touched.size()) {
770                         touched_tracks.push_back (*iter);
771                 }
772         }
773
774         if (touched.empty()) {
775                 return false;
776         }
777
778         if (!touched_tracks.empty()) {
779
780                 switch (op) {
781                 case Selection::Add:
782                         selection->add (touched_tracks);
783                         break;
784                 case Selection::Toggle:
785                         selection->toggle (touched_tracks);
786                         break;
787                 case Selection::Set:
788                         selection->set (touched_tracks);
789                         break;
790                 case Selection::Extend:
791                         /* not defined yet */
792                         break;
793                 }
794         }
795
796         begin_reversible_command (_("select all within"));
797         switch (op) {
798         case Selection::Add:
799                 selection->add (touched);
800                 break;
801         case Selection::Toggle:
802                 selection->toggle (touched);
803                 break;
804         case Selection::Set:
805                 selection->set (touched);
806                 break;
807         case Selection::Extend:
808                 /* not defined yet */
809                 break;
810         }
811         
812         commit_reversible_command ();
813
814         return !touched.empty();
815 }
816
817 void
818 Editor::set_selection_from_audio_region ()
819 {
820         if (selection->regions.empty()) {
821                 return;
822         }
823
824         RegionView* rv = *(selection->regions.begin());
825         boost::shared_ptr<Region> region = rv->region();
826         
827         begin_reversible_command (_("set selection from region"));
828         selection->set (0, region->position(), region->last_frame());
829         commit_reversible_command ();
830
831         set_mouse_mode (Editing::MouseRange, false);
832 }
833
834 void
835 Editor::set_selection_from_punch()
836 {
837         Location* location;
838
839         if ((location = session->locations()->auto_punch_location()) == 0)  {
840                 return;
841         }
842
843         set_selection_from_range (*location);
844 }
845
846 void
847 Editor::set_selection_from_loop()
848 {
849         Location* location;
850
851         if ((location = session->locations()->auto_loop_location()) == 0)  {
852                 return;
853         }
854         set_selection_from_range (*location);
855 }
856
857 void
858 Editor::set_selection_from_range (Location& loc)
859 {
860         begin_reversible_command (_("set selection from range"));
861         selection->set (0, loc.start(), loc.end());
862         commit_reversible_command ();
863
864         set_mouse_mode (Editing::MouseRange, false);
865 }
866
867 void
868 Editor::select_all_selectables_using_time_selection ()
869 {
870         list<Selectable *> touched;
871
872         if (selection->time.empty()) {
873                 return;
874         }
875
876         nframes_t start = selection->time[clicked_selection].start;
877         nframes_t end = selection->time[clicked_selection].end;
878
879         if (end - start < 1)  {
880                 return;
881         }
882
883         for (TrackViewList::iterator iter = selection->tracks.begin(); iter != selection->tracks.end(); ++iter) {
884                 if ((*iter)->hidden()) {
885                         continue;
886                 }
887                 (*iter)->get_selectables (start, end - 1, 0, DBL_MAX, touched);
888         }
889
890         begin_reversible_command (_("select all from range"));
891         selection->set (touched);
892         commit_reversible_command ();
893 }
894
895
896 void
897 Editor::select_all_selectables_using_punch()
898 {
899         Location* location = session->locations()->auto_punch_location();
900         list<Selectable *> touched;
901
902         if (location == 0 || (location->end() - location->start() <= 1))  {
903                 return;
904         }
905
906         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
907                 if ((*iter)->hidden()) {
908                         continue;
909                 }
910                 (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
911         }
912         begin_reversible_command (_("select all from punch"));
913         selection->set (touched);
914         commit_reversible_command ();
915
916 }
917
918 void
919 Editor::select_all_selectables_using_loop()
920 {
921         Location* location = session->locations()->auto_loop_location();
922         list<Selectable *> touched;
923
924         if (location == 0 || (location->end() - location->start() <= 1))  {
925                 return;
926         }
927
928         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
929                 if ((*iter)->hidden()) {
930                         continue;
931                 }
932                 (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
933         }
934         begin_reversible_command (_("select all from loop"));
935         selection->set (touched);
936         commit_reversible_command ();
937
938 }
939
940 void
941 Editor::select_all_selectables_using_cursor (Cursor *cursor, bool after)
942 {
943         nframes_t start;
944         nframes_t end;
945         list<Selectable *> touched;
946
947         if (after) {
948                 begin_reversible_command (_("select all after cursor"));
949                 start = cursor->current_frame ;
950                 end = session->current_end_frame();
951         } else {
952                 if (cursor->current_frame > 0) {
953                         begin_reversible_command (_("select all before cursor"));
954                         start = 0;
955                         end = cursor->current_frame - 1;
956                 } else {
957                         return;
958                 }
959         }
960
961         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
962                 if ((*iter)->hidden()) {
963                         continue;
964                 }
965                 (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
966         }
967         selection->set (touched);
968         commit_reversible_command ();
969 }
970
971 void
972 Editor::select_all_selectables_using_edit (bool after)
973 {
974         nframes_t start;
975         nframes_t end;
976         list<Selectable *> touched;
977
978         if (after) {
979                 begin_reversible_command (_("select all after edit"));
980                 start = get_preferred_edit_position();
981                 end = session->current_end_frame();
982         } else {
983                 if ((end = get_preferred_edit_position()) > 1) {
984                         begin_reversible_command (_("select all before edit"));
985                         start = 0;
986                         end -= 1;
987                 } else {
988                         return;
989                 }
990         }
991
992         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
993                 if ((*iter)->hidden()) {
994                         continue;
995                 }
996                 (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
997         }
998         selection->set (touched);
999         commit_reversible_command ();
1000 }
1001
1002 void
1003 Editor::select_all_selectables_between (bool within)
1004 {
1005         nframes64_t start;
1006         nframes64_t end;
1007         list<Selectable *> touched;
1008
1009         if (!get_edit_op_range (start, end)) {
1010                 return;
1011         }
1012         
1013         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
1014                 if ((*iter)->hidden()) {
1015                         continue;
1016                 }
1017                 (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
1018         }
1019
1020         selection->set (touched);
1021 }
1022
1023 void
1024 Editor::select_range_between ()
1025 {
1026         nframes64_t start;
1027         nframes64_t end;
1028         
1029         if (!get_edit_op_range (start, end)) {
1030                 return;
1031         }
1032
1033         set_mouse_mode (MouseRange);
1034         selection->set (0, start, end);
1035 }
1036
1037 bool
1038 Editor::get_edit_op_range (nframes64_t& start, nframes64_t& end) const
1039 {
1040         nframes64_t m;
1041         bool ignored;
1042
1043         /* in range mode, use any existing selection */
1044
1045         if (mouse_mode == MouseRange && !selection->time.empty()) {
1046                 /* we know that these are ordered */
1047                 start = selection->time.start();
1048                 end = selection->time.end_frame();
1049                 return true;
1050         }
1051
1052         if (!mouse_frame (m, ignored)) {
1053                 /* mouse is not in a canvas, try playhead+selected marker.
1054                    this is probably most true when using menus.
1055                  */
1056
1057                 if (selection->markers.empty()) {
1058                         return false;
1059                 }
1060
1061                 start = selection->markers.front()->position();
1062                 end = session->audible_frame();
1063
1064         } else {
1065
1066                 switch (_edit_point) {
1067                 case EditAtPlayhead:
1068                         if (selection->markers.empty()) {
1069                                 /* use mouse + playhead */
1070                                 start = m;
1071                                 end = session->audible_frame();
1072                         } else {
1073                                 /* use playhead + selected marker */
1074                                 start = session->audible_frame();
1075                                 end = selection->markers.front()->position();
1076                         }
1077                         break;
1078                         
1079                 case EditAtMouse:
1080                 case EditAtSelectedMarker:
1081                         /* use mouse + selected marker */
1082                         if (selection->markers.empty()) {
1083                                 return false;
1084                         }
1085                         start = selection->markers.front()->position();
1086                         end = m;
1087                         break;
1088                 }
1089         }
1090
1091         if (start == end) {
1092                 return false;
1093         }
1094
1095         if (start > end) {
1096                 swap (start, end);
1097         }
1098
1099         return true;
1100 }