Make edit groups effective with lassoo selections.
[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)
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::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)
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_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
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<RouteTimeAxisView*> relevant_tracks;
525                 
526                 get_relevant_tracks (relevant_tracks);
527
528                 for (set<RouteTimeAxisView*>::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         begin_reversible_command (_("set selected regions"));
589         
590         switch (op) {
591         case Selection::Toggle:
592                 /* XXX this is not correct */
593                 selection->toggle (all_equivalent_regions);
594                 break;
595         case Selection::Set:
596                 selection->set (all_equivalent_regions);
597                 break;
598         case Selection::Extend:
599                 selection->add (all_equivalent_regions);
600                 break;
601         case Selection::Add:
602                 selection->add (all_equivalent_regions);
603                 break;
604         }
605
606         commit_reversible_command () ;
607 }
608
609 void
610 Editor::track_selection_changed ()
611 {
612         switch (selection->tracks.size()){
613         case 0:
614                 break;
615         default:
616                 set_selected_mixer_strip (*(selection->tracks.front()));
617                 break;
618         }
619
620         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
621                 (*i)->set_selected (false);
622                 if (mouse_mode == MouseRange) {
623                         (*i)->hide_selection ();
624                 }
625         }
626
627         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
628                 (*i)->set_selected (true);
629                 if (mouse_mode == MouseRange) {
630                         (*i)->show_selection (selection->time);
631                 }
632         }
633 }
634
635 void
636 Editor::time_selection_changed ()
637 {
638         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
639                 (*i)->hide_selection ();
640         }
641
642         if (selection->tracks.empty()) {
643                 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
644                         (*i)->show_selection (selection->time);
645                 }
646         } else {
647                 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
648                         (*i)->show_selection (selection->time);
649                 }
650         }
651
652         if (selection->time.empty()) {
653                 ActionManager::set_sensitive (ActionManager::time_selection_sensitive_actions, false);
654         } else {
655                 ActionManager::set_sensitive (ActionManager::time_selection_sensitive_actions, true);
656         }
657 }
658
659 void
660 Editor::region_selection_changed ()
661 {
662         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
663                 (*i)->set_selected_regionviews (selection->regions);
664         }
665 }
666
667 void
668 Editor::point_selection_changed ()
669 {
670         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
671                 (*i)->set_selected_points (selection->points);
672         }
673 }
674
675 /** Select everything in the selected tracks
676  * @param Selection operation to apply.
677  */
678 void
679 Editor::select_all_in_selected_tracks (Selection::Operation op)
680 {
681         list<Selectable *> touched;
682
683         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
684                 (*i)->get_selectables (0, max_frames, 0, DBL_MAX, touched);
685         }
686
687         switch (op) {
688         case Selection::Toggle:
689                 selection->add (touched);
690                 break;
691         case Selection::Set:
692                 selection->set (touched);
693                 break;
694         case Selection::Extend:
695                 /* meaningless, because we're selecting everything */
696                 break;
697         case Selection::Add:
698                 selection->add (touched);
699                 break;
700         }
701 }
702
703 void
704 Editor::select_all (Selection::Operation op)
705 {
706         list<Selectable *> touched;
707         
708         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
709                 if ((*iter)->hidden()) {
710                         continue;
711                 }
712                 (*iter)->get_selectables (0, max_frames, 0, DBL_MAX, touched);
713         }
714         begin_reversible_command (_("select all"));
715         switch (op) {
716         case Selection::Add:
717                 selection->add (touched);
718                 break;
719         case Selection::Toggle:
720                 selection->add (touched);
721                 break;
722         case Selection::Set:
723                 selection->set (touched);
724                 break;
725         case Selection::Extend:
726                 /* meaningless, because we're selecting everything */
727                 break;
728         }
729         commit_reversible_command ();
730 }
731
732 /** Invert the selection in the selected tracks */
733 void
734 Editor::invert_selection_in_selected_tracks ()
735 {
736         list<Selectable *> touched;
737
738         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
739                 (*i)->get_inverted_selectables (*selection, touched);
740         }
741         
742         selection->set (touched);
743 }
744
745 void
746 Editor::invert_selection ()
747 {
748         list<Selectable *> touched;
749         
750         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
751                 if ((*iter)->hidden()) {
752                         continue;
753                 }
754                 (*iter)->get_inverted_selectables (*selection, touched);
755         }
756
757         selection->set (touched);
758 }
759
760 bool
761 Editor::select_all_within (nframes_t start, nframes_t end, double top, double bot, const TrackViewList& tracklist, Selection::Operation op)
762 {
763         list<Selectable*> touched;
764         list<Selectable*>::size_type n = 0;
765         TrackViewList touched_tracks;
766
767         for (TrackViewList::const_iterator iter = tracklist.begin(); iter != tracklist.end(); ++iter) {
768                 if ((*iter)->hidden()) {
769                         continue;
770                 }
771
772                 n = touched.size();
773
774                 (*iter)->get_selectables (start, end, top, bot, touched);
775                 
776                 if (n != touched.size()) {
777                         touched_tracks.push_back (*iter);
778                 }
779         }
780
781         if (touched.empty()) {
782                 return false;
783         }
784
785         /* `touched' may contain some regions; if so, we need to add equivalent
786            regions from any edit groups */
787
788         list<Selectable*> to_add;
789
790         for (list<Selectable*>::iterator i = touched.begin(); i != touched.end(); ++i) {
791                 RegionView* r = dynamic_cast<RegionView*> (*i);
792                 if (r) {
793                         vector<RegionView*> e;
794                         get_equivalent_regions (r, e);
795                         for (vector<RegionView*>::iterator j = e.begin(); j != e.end(); ++j) {
796                                 to_add.push_back (*j);
797                         }
798                 }
799         }
800
801         for (list<Selectable*>::iterator i = to_add.begin(); i != to_add.end(); ++i) {
802                 touched.push_back (*i);
803         }
804
805         if (!touched_tracks.empty()) {
806
807                 switch (op) {
808                 case Selection::Add:
809                         selection->add (touched_tracks);
810                         break;
811                 case Selection::Toggle:
812                         selection->toggle (touched_tracks);
813                         break;
814                 case Selection::Set:
815                         selection->set (touched_tracks);
816                         break;
817                 case Selection::Extend:
818                         /* not defined yet */
819                         break;
820                 }
821         }
822
823         begin_reversible_command (_("select all within"));
824         switch (op) {
825         case Selection::Add:
826                 selection->add (touched);
827                 break;
828         case Selection::Toggle:
829                 selection->toggle (touched);
830                 break;
831         case Selection::Set:
832                 selection->set (touched);
833                 break;
834         case Selection::Extend:
835                 /* not defined yet */
836                 break;
837         }
838         
839         commit_reversible_command ();
840
841         return !touched.empty();
842 }
843
844 void
845 Editor::set_selection_from_audio_region ()
846 {
847         if (selection->regions.empty()) {
848                 return;
849         }
850
851         RegionView* rv = *(selection->regions.begin());
852         boost::shared_ptr<Region> region = rv->region();
853         
854         begin_reversible_command (_("set selection from region"));
855         selection->set (0, region->position(), region->last_frame());
856         commit_reversible_command ();
857
858         set_mouse_mode (Editing::MouseRange, false);
859 }
860
861 void
862 Editor::set_selection_from_punch()
863 {
864         Location* location;
865
866         if ((location = session->locations()->auto_punch_location()) == 0)  {
867                 return;
868         }
869
870         set_selection_from_range (*location);
871 }
872
873 void
874 Editor::set_selection_from_loop()
875 {
876         Location* location;
877
878         if ((location = session->locations()->auto_loop_location()) == 0)  {
879                 return;
880         }
881         set_selection_from_range (*location);
882 }
883
884 void
885 Editor::set_selection_from_range (Location& loc)
886 {
887         begin_reversible_command (_("set selection from range"));
888         selection->set (0, loc.start(), loc.end());
889         commit_reversible_command ();
890
891         set_mouse_mode (Editing::MouseRange, false);
892 }
893
894 void
895 Editor::select_all_selectables_using_time_selection ()
896 {
897         list<Selectable *> touched;
898
899         if (selection->time.empty()) {
900                 return;
901         }
902
903         nframes_t start = selection->time[clicked_selection].start;
904         nframes_t end = selection->time[clicked_selection].end;
905
906         if (end - start < 1)  {
907                 return;
908         }
909
910         for (TrackViewList::iterator iter = selection->tracks.begin(); iter != selection->tracks.end(); ++iter) {
911                 if ((*iter)->hidden()) {
912                         continue;
913                 }
914                 (*iter)->get_selectables (start, end - 1, 0, DBL_MAX, touched);
915         }
916
917         begin_reversible_command (_("select all from range"));
918         selection->set (touched);
919         commit_reversible_command ();
920 }
921
922
923 void
924 Editor::select_all_selectables_using_punch()
925 {
926         Location* location = session->locations()->auto_punch_location();
927         list<Selectable *> touched;
928
929         if (location == 0 || (location->end() - location->start() <= 1))  {
930                 return;
931         }
932
933         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
934                 if ((*iter)->hidden()) {
935                         continue;
936                 }
937                 (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
938         }
939         begin_reversible_command (_("select all from punch"));
940         selection->set (touched);
941         commit_reversible_command ();
942
943 }
944
945 void
946 Editor::select_all_selectables_using_loop()
947 {
948         Location* location = session->locations()->auto_loop_location();
949         list<Selectable *> touched;
950
951         if (location == 0 || (location->end() - location->start() <= 1))  {
952                 return;
953         }
954
955         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
956                 if ((*iter)->hidden()) {
957                         continue;
958                 }
959                 (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
960         }
961         begin_reversible_command (_("select all from loop"));
962         selection->set (touched);
963         commit_reversible_command ();
964
965 }
966
967 void
968 Editor::select_all_selectables_using_cursor (Cursor *cursor, bool after)
969 {
970         nframes_t start;
971         nframes_t end;
972         list<Selectable *> touched;
973
974         if (after) {
975                 begin_reversible_command (_("select all after cursor"));
976                 start = cursor->current_frame ;
977                 end = session->current_end_frame();
978         } else {
979                 if (cursor->current_frame > 0) {
980                         begin_reversible_command (_("select all before cursor"));
981                         start = 0;
982                         end = cursor->current_frame - 1;
983                 } else {
984                         return;
985                 }
986         }
987
988         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
989                 if ((*iter)->hidden()) {
990                         continue;
991                 }
992                 (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
993         }
994         selection->set (touched);
995         commit_reversible_command ();
996 }
997
998 void
999 Editor::select_all_selectables_between_cursors (Cursor *cursor, Cursor *other_cursor)
1000 {
1001         nframes_t start;
1002         nframes_t end;
1003         list<Selectable *> touched;
1004         bool  other_cursor_is_first = cursor->current_frame > other_cursor->current_frame;
1005
1006         if (cursor->current_frame == other_cursor->current_frame) {
1007                 return;
1008         }
1009
1010         begin_reversible_command (_("select all between cursors"));
1011         if (other_cursor_is_first) {
1012                 start = other_cursor->current_frame;
1013                 end = cursor->current_frame - 1;
1014                 
1015         } else {
1016                 start = cursor->current_frame;
1017                 end = other_cursor->current_frame - 1;
1018         }
1019         
1020         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
1021                 if ((*iter)->hidden()) {
1022                         continue;
1023                 }
1024                 (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
1025         }
1026         selection->set (touched);
1027         commit_reversible_command ();
1028 }
1029