remove all lines to avoid recompiles after commits
[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, 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 void
288 Editor::mapover_audio_tracks (slot<void,AudioTimeAxisView&,uint32_t> sl)
289 {
290         set<AudioTimeAxisView*> relevant_tracks;
291
292         get_relevant_audio_tracks (relevant_tracks);
293
294         uint32_t sz = relevant_tracks.size();
295
296         for (set<AudioTimeAxisView*>::iterator ati = relevant_tracks.begin(); ati != relevant_tracks.end(); ++ati) {
297                 sl (**ati, sz);
298         }
299 }
300
301 void
302 Editor::mapped_get_equivalent_regions (RouteTimeAxisView& tv, uint32_t ignored, RegionView* basis, vector<RegionView*>* all_equivs)
303 {
304         boost::shared_ptr<Playlist> pl;
305         vector<boost::shared_ptr<Region> > results;
306         RegionView* marv;
307         boost::shared_ptr<Diskstream> ds;
308
309         if ((ds = tv.get_diskstream()) == 0) {
310                 /* bus */
311                 return;
312         }
313
314         if (&tv == &basis->get_time_axis_view()) {
315                 /* looking in same track as the original */
316                 return;
317         }
318
319         if ((pl = ds->playlist()) != 0) {
320                 pl->get_equivalent_regions (basis->region(), results);
321         }
322
323         for (vector<boost::shared_ptr<Region> >::iterator ir = results.begin(); ir != results.end(); ++ir) {
324                 if ((marv = tv.view()->find_view (*ir)) != 0) {
325                         all_equivs->push_back (marv);
326                 }
327         }
328 }
329
330 void
331 Editor::get_equivalent_regions (RegionView* basis, vector<RegionView*>& equivalent_regions)
332 {
333         mapover_audio_tracks (bind (mem_fun (*this, &Editor::mapped_get_equivalent_regions), basis, &equivalent_regions));
334         
335         /* add clicked regionview since we skipped all other regions in the same track as the one it was in */
336         
337         equivalent_regions.push_back (basis);
338 }
339
340 bool
341 Editor::set_selected_regionview_from_click (bool press, Selection::Operation op, bool no_track_remove)
342 {
343         vector<RegionView*> all_equivalent_regions;
344         bool commit = false;
345
346         if (!clicked_regionview || !clicked_audio_trackview) {
347                 return false;
348         }
349
350         if (press) {
351                 button_release_can_deselect = false;
352         } 
353
354         if (op == Selection::Toggle || op == Selection::Set) {
355
356
357                 switch (op) {
358                 case Selection::Toggle:
359                         
360                         if (clicked_regionview->get_selected()) {
361                                 if (press) {
362
363                                         /* whatever was clicked was selected already; do nothing here but allow
364                                            the button release to deselect it
365                                         */
366
367                                         button_release_can_deselect = true;
368
369                                 } else {
370
371                                         if (button_release_can_deselect) {
372
373                                                 /* just remove this one region, but only on a permitted button release */
374
375                                                 selection->remove (clicked_regionview);
376                                                 commit = true;
377
378                                                 /* no more deselect action on button release till a new press
379                                                    finds an already selected object.
380                                                 */
381
382                                                 button_release_can_deselect = false;
383                                         }
384                                 } 
385
386                         } else {
387
388                                 if (press) {
389
390                                         if (selection->selected (clicked_audio_trackview)) {
391                                                 get_equivalent_regions (clicked_regionview, all_equivalent_regions);
392                                         } else {
393                                                 all_equivalent_regions.push_back (clicked_regionview);
394                                         }
395
396                                         /* add all the equivalent regions, but only on button press */
397                                         
398
399
400                                         if (!all_equivalent_regions.empty()) {
401                                                 commit = true;
402                                         }
403
404                                         selection->add (all_equivalent_regions);
405                                 } 
406                         }
407                         break;
408                         
409                 case Selection::Set:
410                         if (!clicked_regionview->get_selected()) {
411
412                                 if (selection->selected (clicked_audio_trackview)) {
413                                         get_equivalent_regions (clicked_regionview, all_equivalent_regions);
414                                 } else {
415                                         all_equivalent_regions.push_back (clicked_regionview);
416                                 }
417
418                                 selection->set (all_equivalent_regions);
419                                 commit = true;
420                         } else {
421                                 /* no commit necessary: clicked on an already selected region */
422                                 goto out;
423                         }
424                         break;
425
426                 default:
427                         /* silly compiler */
428                         break;
429                 }
430
431         } else if (op == Selection::Extend) {
432
433                 list<Selectable*> results;
434                 nframes_t last_frame;
435                 nframes_t first_frame;
436
437                 /* 1. find the last selected regionview in the track that was clicked in */
438
439                 last_frame = 0;
440                 first_frame = max_frames;
441
442                 for (RegionSelection::iterator x = selection->regions.begin(); x != selection->regions.end(); ++x) {
443                         if (&(*x)->get_time_axis_view() == &clicked_regionview->get_time_axis_view()) {
444
445                                 if ((*x)->region()->last_frame() > last_frame) {
446                                         last_frame = (*x)->region()->last_frame();
447                                 }
448
449                                 if ((*x)->region()->first_frame() < first_frame) {
450                                         first_frame = (*x)->region()->first_frame();
451                                 }
452                         }
453                 }
454
455                 /* 2. figure out the boundaries for our search for new objects */
456
457                 switch (clicked_regionview->region()->coverage (first_frame, last_frame)) {
458                 case OverlapNone:
459                         if (last_frame < clicked_regionview->region()->first_frame()) {
460                                 first_frame = last_frame;
461                                 last_frame = clicked_regionview->region()->last_frame();
462                         } else {
463                                 last_frame = first_frame;
464                                 first_frame = clicked_regionview->region()->first_frame();
465                         }
466                         break;
467
468                 case OverlapExternal:
469                         if (last_frame < clicked_regionview->region()->first_frame()) {
470                                 first_frame = last_frame;
471                                 last_frame = clicked_regionview->region()->last_frame();
472                         } else {
473                                 last_frame = first_frame;
474                                 first_frame = clicked_regionview->region()->first_frame();
475                         }
476                         break;
477
478                 case OverlapInternal:
479                         if (last_frame < clicked_regionview->region()->first_frame()) {
480                                 first_frame = last_frame;
481                                 last_frame = clicked_regionview->region()->last_frame();
482                         } else {
483                                 last_frame = first_frame;
484                                 first_frame = clicked_regionview->region()->first_frame();
485                         }
486                         break;
487
488                 case OverlapStart:
489                 case OverlapEnd:
490                         /* nothing to do except add clicked region to selection, since it
491                            overlaps with the existing selection in this track.
492                         */
493                         break;
494                 }
495
496                 /* 2. find all selectable objects (regionviews in this case) between that one and the end of the
497                       one that was clicked.
498                 */
499
500                 set<AudioTimeAxisView*> relevant_tracks;
501                 
502                 get_relevant_audio_tracks (relevant_tracks);
503
504                 for (set<AudioTimeAxisView*>::iterator t = relevant_tracks.begin(); t != relevant_tracks.end(); ++t) {
505                         (*t)->get_selectables (first_frame, last_frame, -1.0, -1.0, results);
506                 }
507                 
508                 /* 3. convert to a vector of audio regions */
509
510                 vector<RegionView*> regions;
511                 
512                 for (list<Selectable*>::iterator x = results.begin(); x != results.end(); ++x) {
513                         RegionView* arv;
514
515                         if ((arv = dynamic_cast<RegionView*>(*x)) != 0) {
516                                 regions.push_back (arv);
517                         }
518                 }
519
520                 if (!regions.empty()) {
521                         selection->add (regions);
522                         commit = true;
523                 }
524         }
525
526   out:
527         return commit;
528 }
529
530 void
531 Editor::set_selected_regionview_from_region_list (boost::shared_ptr<Region> region, Selection::Operation op)
532 {
533         vector<RegionView*> all_equivalent_regions;
534
535         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
536                 
537                 RouteTimeAxisView* tatv;
538                 
539                 if ((tatv = dynamic_cast<RouteTimeAxisView*> (*i)) != 0) {
540                         
541                         boost::shared_ptr<Playlist> pl;
542                         vector<boost::shared_ptr<Region> > results;
543                         RegionView* marv;
544                         boost::shared_ptr<Diskstream> ds;
545                         
546                         if ((ds = tatv->get_diskstream()) == 0) {
547                                 /* bus */
548                                 continue;
549                         }
550                         
551                         if ((pl = (ds->playlist())) != 0) {
552                                 pl->get_region_list_equivalent_regions (region, results);
553                         }
554                         
555                         for (vector<boost::shared_ptr<Region> >::iterator ir = results.begin(); ir != results.end(); ++ir) {
556                                 if ((marv = tatv->view()->find_view (*ir)) != 0) {
557                                         all_equivalent_regions.push_back (marv);
558                                 }
559                         }
560                         
561                 }
562         }
563         
564         begin_reversible_command (_("set selected regions"));
565         
566         switch (op) {
567         case Selection::Toggle:
568                 /* XXX this is not correct */
569                 selection->toggle (all_equivalent_regions);
570                 break;
571         case Selection::Set:
572                 selection->set (all_equivalent_regions);
573                 break;
574         case Selection::Extend:
575                 selection->add (all_equivalent_regions);
576                 break;
577         case Selection::Add:
578                 selection->add (all_equivalent_regions);
579                 break;
580         }
581
582         commit_reversible_command () ;
583 }
584
585 bool
586 Editor::set_selected_regionview_from_map_event (GdkEventAny* ev, StreamView* sv, boost::weak_ptr<Region> weak_r)
587 {
588         RegionView* rv;
589         boost::shared_ptr<Region> r (weak_r.lock());
590
591         if (!r) {
592                 return true;
593         }
594
595         boost::shared_ptr<AudioRegion> ar;
596
597         if ((ar = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
598                 return true;
599         }
600
601         if ((rv = sv->find_view (ar)) == 0) {
602                 return true;
603         }
604
605         /* don't reset the selection if its something other than 
606            a single other region.
607         */
608
609         if (selection->regions.size() > 1) {
610                 return true;
611         }
612         
613         begin_reversible_command (_("set selected regions"));
614         
615         selection->set (rv);
616
617         commit_reversible_command () ;
618
619         return true;
620 }
621
622 void
623 Editor::track_selection_changed ()
624 {
625         switch (selection->tracks.size()){
626         case 0:
627                 break;
628         default:
629                 set_selected_mixer_strip (*(selection->tracks.front()));
630                 break;
631         }
632
633         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
634                 (*i)->set_selected (false);
635                 if (mouse_mode == MouseRange) {
636                         (*i)->hide_selection ();
637                 }
638         }
639
640         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
641                 (*i)->set_selected (true);
642                 if (mouse_mode == MouseRange) {
643                         (*i)->show_selection (selection->time);
644                 }
645         }
646 }
647
648 void
649 Editor::time_selection_changed ()
650 {
651         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
652                 (*i)->hide_selection ();
653         }
654
655         if (selection->tracks.empty()) {
656                 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
657                         (*i)->show_selection (selection->time);
658                 }
659         } else {
660                 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
661                         (*i)->show_selection (selection->time);
662                 }
663         }
664
665         if (selection->time.empty()) {
666                 ActionManager::set_sensitive (ActionManager::time_selection_sensitive_actions, false);
667         } else {
668                 ActionManager::set_sensitive (ActionManager::time_selection_sensitive_actions, true);
669         }
670 }
671
672 void
673 Editor::region_selection_changed ()
674 {
675         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
676                 (*i)->set_selected_regionviews (selection->regions);
677         }
678 }
679
680 void
681 Editor::point_selection_changed ()
682 {
683         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
684                 (*i)->set_selected_points (selection->points);
685         }
686 }
687
688 void
689 Editor::select_all_in_track (Selection::Operation op)
690 {
691         list<Selectable *> touched;
692
693         if (!clicked_trackview) {
694                 return;
695         }
696         
697         clicked_trackview->get_selectables (0, max_frames, 0, DBL_MAX, touched);
698
699         switch (op) {
700         case Selection::Toggle:
701                 selection->add (touched);
702                 break;
703         case Selection::Set:
704                 selection->set (touched);
705                 break;
706         case Selection::Extend:
707                 /* meaningless, because we're selecting everything */
708                 break;
709         case Selection::Add:
710                 selection->add (touched);
711                 break;
712         }
713 }
714
715 void
716 Editor::select_all (Selection::Operation op)
717 {
718         list<Selectable *> touched;
719         
720         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
721                 if ((*iter)->hidden()) {
722                         continue;
723                 }
724                 (*iter)->get_selectables (0, max_frames, 0, DBL_MAX, touched);
725         }
726         begin_reversible_command (_("select all"));
727         switch (op) {
728         case Selection::Add:
729                 selection->add (touched);
730                 break;
731         case Selection::Toggle:
732                 selection->add (touched);
733                 break;
734         case Selection::Set:
735                 selection->set (touched);
736                 break;
737         case Selection::Extend:
738                 /* meaningless, because we're selecting everything */
739                 break;
740         }
741         commit_reversible_command ();
742 }
743
744 void
745 Editor::invert_selection_in_track ()
746 {
747         list<Selectable *> touched;
748
749         if (!clicked_trackview) {
750                 return;
751         }
752         
753         clicked_trackview->get_inverted_selectables (*selection, touched);
754         selection->set (touched);
755 }
756
757 void
758 Editor::invert_selection ()
759 {
760         list<Selectable *> touched;
761         
762         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
763                 if ((*iter)->hidden()) {
764                         continue;
765                 }
766                 (*iter)->get_inverted_selectables (*selection, touched);
767         }
768
769         selection->set (touched);
770 }
771
772 bool
773 Editor::select_all_within (nframes_t start, nframes_t end, double top, double bot, Selection::Operation op)
774 {
775         list<Selectable*> touched;
776         list<Selectable*>::size_type n = 0;
777         TrackViewList touched_tracks;
778
779         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
780                 if ((*iter)->hidden()) {
781                         continue;
782                 }
783
784                 n = touched.size();
785
786                 (*iter)->get_selectables (start, end, top, bot, touched);
787
788                 if (n != touched.size()) {
789                         touched_tracks.push_back (*iter);
790                 }
791         }
792
793         if (!touched_tracks.empty()) {
794                 switch (op) {
795                 case Selection::Add:
796                         selection->add (touched_tracks);
797                         break;
798                 case Selection::Toggle:
799                         selection->toggle (touched_tracks);
800                         break;
801                 case Selection::Set:
802                         selection->set (touched_tracks);
803                         break;
804                 case Selection::Extend:
805                         /* not defined yet */
806                         break;
807                 }
808         }
809                 
810         begin_reversible_command (_("select all within"));
811         switch (op) {
812         case Selection::Add:
813                 selection->add (touched);
814                 break;
815         case Selection::Toggle:
816                 selection->toggle (touched);
817                 break;
818         case Selection::Set:
819                 selection->set (touched);
820                 break;
821         case Selection::Extend:
822                 /* not defined yet */
823                 break;
824         }
825
826         commit_reversible_command ();
827         return !touched.empty();
828 }
829
830 void
831 Editor::set_selection_from_audio_region ()
832 {
833         if (selection->regions.empty()) {
834                 return;
835         }
836
837         RegionView* rv = *(selection->regions.begin());
838         boost::shared_ptr<Region> region = rv->region();
839         
840         begin_reversible_command (_("set selection from region"));
841         selection->set (0, region->position(), region->last_frame());
842         commit_reversible_command ();
843
844         set_mouse_mode (Editing::MouseRange, false);
845 }
846
847 void
848 Editor::set_selection_from_punch()
849 {
850         Location* location;
851
852         if ((location = session->locations()->auto_punch_location()) == 0)  {
853                 return;
854         }
855
856         set_selection_from_range (*location);
857 }
858
859 void
860 Editor::set_selection_from_loop()
861 {
862         Location* location;
863
864         if ((location = session->locations()->auto_loop_location()) == 0)  {
865                 return;
866         }
867         set_selection_from_range (*location);
868 }
869
870 void
871 Editor::set_selection_from_range (Location& loc)
872 {
873         begin_reversible_command (_("set selection from range"));
874         selection->set (0, loc.start(), loc.end());
875         commit_reversible_command ();
876
877         set_mouse_mode (Editing::MouseRange, false);
878 }
879
880 void
881 Editor::select_all_selectables_using_time_selection ()
882 {
883         list<Selectable *> touched;
884
885         if (selection->time.empty()) {
886                 return;
887         }
888
889         nframes_t start = selection->time[clicked_selection].start;
890         nframes_t end = selection->time[clicked_selection].end;
891
892         if (end - start < 1)  {
893                 return;
894         }
895
896         for (TrackViewList::iterator iter = selection->tracks.begin(); iter != selection->tracks.end(); ++iter) {
897                 if ((*iter)->hidden()) {
898                         continue;
899                 }
900                 (*iter)->get_selectables (start, end - 1, 0, DBL_MAX, touched);
901         }
902
903         begin_reversible_command (_("select all from range"));
904         selection->set (touched);
905         commit_reversible_command ();
906 }
907
908
909 void
910 Editor::select_all_selectables_using_punch()
911 {
912         Location* location = session->locations()->auto_punch_location();
913         list<Selectable *> touched;
914
915         if (location == 0 || (location->end() - location->start() <= 1))  {
916                 return;
917         }
918
919         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
920                 if ((*iter)->hidden()) {
921                         continue;
922                 }
923                 (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
924         }
925         begin_reversible_command (_("select all from punch"));
926         selection->set (touched);
927         commit_reversible_command ();
928
929 }
930
931 void
932 Editor::select_all_selectables_using_loop()
933 {
934         Location* location = session->locations()->auto_loop_location();
935         list<Selectable *> touched;
936
937         if (location == 0 || (location->end() - location->start() <= 1))  {
938                 return;
939         }
940
941         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
942                 if ((*iter)->hidden()) {
943                         continue;
944                 }
945                 (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
946         }
947         begin_reversible_command (_("select all from loop"));
948         selection->set (touched);
949         commit_reversible_command ();
950
951 }
952
953 void
954 Editor::select_all_selectables_using_cursor (Cursor *cursor, bool after)
955 {
956         nframes_t start;
957         nframes_t end;
958         list<Selectable *> touched;
959
960         if (after) {
961                 begin_reversible_command (_("select all after cursor"));
962                 start = cursor->current_frame ;
963                 end = session->current_end_frame();
964         } else {
965                 if (cursor->current_frame > 0) {
966                         begin_reversible_command (_("select all before cursor"));
967                         start = 0;
968                         end = cursor->current_frame - 1;
969                 } else {
970                         return;
971                 }
972         }
973
974         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
975                 if ((*iter)->hidden()) {
976                         continue;
977                 }
978                 (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
979         }
980         selection->set (touched);
981         commit_reversible_command ();
982 }
983
984 void
985 Editor::select_all_selectables_between_cursors (Cursor *cursor, Cursor *other_cursor)
986 {
987         nframes_t start;
988         nframes_t end;
989         list<Selectable *> touched;
990         bool  other_cursor_is_first = cursor->current_frame > other_cursor->current_frame;
991
992         if (cursor->current_frame == other_cursor->current_frame) {
993                 return;
994         }
995
996         begin_reversible_command (_("select all between cursors"));
997         if (other_cursor_is_first) {
998                 start = other_cursor->current_frame;
999                 end = cursor->current_frame - 1;
1000                 
1001         } else {
1002                 start = cursor->current_frame;
1003                 end = other_cursor->current_frame - 1;
1004         }
1005         
1006         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
1007                 if ((*iter)->hidden()) {
1008                         continue;
1009                 }
1010                 (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
1011         }
1012         selection->set (touched);
1013         commit_reversible_command ();
1014 }
1015