move session file storage of nested sources from playlist into region, and reload...
[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 <algorithm>
21 #include <cstdlib>
22
23 #include "pbd/stacktrace.h"
24
25 #include "ardour/session.h"
26 #include "ardour/playlist.h"
27 #include "ardour/route_group.h"
28 #include "ardour/profile.h"
29 #include "ardour/midi_region.h"
30
31 #include "editor.h"
32 #include "actions.h"
33 #include "audio_time_axis.h"
34 #include "audio_region_view.h"
35 #include "audio_streamview.h"
36 #include "automation_line.h"
37 #include "control_point.h"
38 #include "editor_regions.h"
39 #include "editor_cursors.h"
40 #include "midi_region_view.h"
41
42 #include "i18n.h"
43
44 using namespace std;
45 using namespace ARDOUR;
46 using namespace PBD;
47 using namespace Gtk;
48 using namespace Glib;
49 using namespace Gtkmm2ext;
50 using namespace Editing;
51
52 struct TrackViewByPositionSorter
53 {
54     bool operator() (const TimeAxisView* a, const TimeAxisView *b) {
55             return a->y_position() < b->y_position();
56     }
57 };
58
59 bool
60 Editor::extend_selection_to_track (TimeAxisView& view)
61 {
62         if (selection->selected (&view)) {
63                 /* already selected, do nothing */
64                 return false;
65         }
66
67         if (selection->tracks.empty()) {
68
69                 if (!selection->selected (&view)) {
70                         selection->set (&view);
71                         return true;
72                 } else {
73                         return false;
74                 }
75         }
76
77         /* something is already selected, so figure out which range of things to add */
78
79         TrackViewList to_be_added;
80         TrackViewList sorted = track_views;
81         TrackViewByPositionSorter cmp;
82         bool passed_clicked = false;
83         bool forwards = true;
84
85         sorted.sort (cmp);
86
87         if (!selection->selected (&view)) {
88                 to_be_added.push_back (&view);
89         }
90
91         /* figure out if we should go forward or backwards */
92
93         for (TrackViewList::iterator i = sorted.begin(); i != sorted.end(); ++i) {
94
95                 if ((*i) == &view) {
96                         passed_clicked = true;
97                 }
98
99                 if (selection->selected (*i)) {
100                         if (passed_clicked) {
101                                 forwards = true;
102                         } else {
103                                 forwards = false;
104                         }
105                         break;
106                 }
107         }
108
109         passed_clicked = false;
110
111         if (forwards) {
112
113                 for (TrackViewList::iterator i = sorted.begin(); i != sorted.end(); ++i) {
114
115                         if ((*i) == &view) {
116                                 passed_clicked = true;
117                                 continue;
118                         }
119
120                         if (passed_clicked) {
121                                 if ((*i)->hidden()) {
122                                         continue;
123                                 }
124                                 if (selection->selected (*i)) {
125                                         break;
126                                 } else if (!(*i)->hidden()) {
127                                         to_be_added.push_back (*i);
128                                 }
129                         }
130                 }
131
132         } else {
133
134                 for (TrackViewList::reverse_iterator r = sorted.rbegin(); r != sorted.rend(); ++r) {
135
136                         if ((*r) == &view) {
137                                 passed_clicked = true;
138                                 continue;
139                         }
140
141                         if (passed_clicked) {
142
143                                 if ((*r)->hidden()) {
144                                         continue;
145                                 }
146
147                                 if (selection->selected (*r)) {
148                                         break;
149                                 } else if (!(*r)->hidden()) {
150                                         to_be_added.push_back (*r);
151                                 }
152                         }
153                 }
154         }
155
156         if (!to_be_added.empty()) {
157                 selection->add (to_be_added);
158                 return true;
159         }
160
161         return false;
162 }
163
164 void
165 Editor::select_all_tracks ()
166 {
167         TrackViewList visible_views;
168         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
169                 if ((*i)->marked_for_display()) {
170                         visible_views.push_back (*i);
171                 }
172         }
173         selection->set (visible_views);
174 }
175
176 /** Select clicked_axisview, unless there are no currently selected
177  *  tracks, in which case nothing will happen unless `force' is true.
178  */
179 void
180 Editor::set_selected_track_as_side_effect (Selection::Operation op, bool /*force*/)
181 {
182         if (!clicked_axisview) {
183                 return;
184         }
185
186 #if 1
187         if (!clicked_routeview) {
188                 return;
189         }
190
191         bool had_tracks = !selection->tracks.empty();
192         RouteGroup* group = clicked_routeview->route()->route_group();
193         RouteGroup& arg (_session->all_route_group());
194         
195         switch (op) {
196         case Selection::Toggle: 
197                 if (selection->selected (clicked_axisview)) {
198                         if (arg.is_select() && arg.is_active()) {
199                                 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end (); ++i) {
200                                         selection->remove(*i);
201                                 }
202                         } else if (group && group->is_active()) {
203                                 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end (); ++i) {
204                                         if ((*i)->route_group() == group)
205                                                 selection->remove(*i);
206                                 }
207                         } else {
208                                 selection->remove (clicked_axisview);
209                         }
210                 } else {
211                         if (arg.is_select() && arg.is_active()) {
212                                 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end (); ++i) {
213                                                 selection->add(*i);
214                                 }
215                         } else if (group && group->is_active()) {
216                                 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end (); ++i) {
217                                         if ( (*i)->route_group() == group)
218                                                 selection->add(*i);
219                                 }
220                         } else {
221                                 selection->add (clicked_axisview);
222                         }
223                 }
224                 break;
225         
226         case Selection::Add: 
227                 if (!had_tracks && arg.is_select() && arg.is_active()) {
228                         /* nothing was selected already, and all group is active etc. so use
229                            all tracks.
230                         */
231                         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end (); ++i) {
232                                         selection->add(*i);
233                         }
234                 } else if (group && group->is_active()) {
235                         for (TrackViewList::iterator i  = track_views.begin(); i != track_views.end (); ++i) {
236                                 if ((*i)->route_group() == group)
237                                         selection->add(*i);
238                         }
239                 } else {
240                         selection->add (clicked_axisview);
241                 }
242                 break;
243                 
244         case Selection::Set:
245                 selection->clear();
246                 if (!had_tracks && arg.is_select() && arg.is_active()) {
247                         /* nothing was selected already, and all group is active etc. so use
248                            all tracks.
249                         */
250                         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end (); ++i) {
251                                         selection->add(*i);
252                         }
253                 } else if (group && group->is_active()) {
254                         for (TrackViewList::iterator i  = track_views.begin(); i != track_views.end (); ++i) {
255                                 if ((*i)->route_group() == group)
256                                         selection->add(*i);
257                         }
258                 } else {
259                         selection->set (clicked_axisview);
260                 }
261                 break;
262         
263         case Selection::Extend: 
264                 selection->clear();
265                 cerr << ("Editor::set_selected_track_as_side_effect  case  Selection::Add  not yet implemented\n");
266                 break;
267         }
268
269 #else // the older version
270
271         if (!selection->tracks.empty()) {
272                 if (!selection->selected (clicked_axisview)) {
273                         selection->add (clicked_axisview);
274                 }
275
276         } else if (force) {
277                 selection->set (clicked_axisview);
278         }
279 #endif
280 }
281
282 void
283 Editor::set_selected_track (TimeAxisView& view, Selection::Operation op, bool no_remove)
284 {
285         switch (op) {
286         case Selection::Toggle:
287                 if (selection->selected (&view)) {
288                         if (!no_remove) {
289                                 selection->remove (&view);
290                         }
291                 } else {
292                         selection->add (&view);
293                 }
294                 break;
295
296         case Selection::Add:
297                 if (!selection->selected (&view)) {
298                         selection->add (&view);
299                 }
300                 break;
301
302         case Selection::Set:
303                 selection->set (&view);
304                 break;
305
306         case Selection::Extend:
307                 extend_selection_to_track (view);
308                 break;
309         }
310 }
311
312 void
313 Editor::set_selected_track_from_click (bool press, Selection::Operation op, bool no_remove)
314 {
315         if (!clicked_routeview) {
316                 return;
317         }
318
319         if (!press) {
320                 return;
321         }
322
323         set_selected_track (*clicked_routeview, op, no_remove);
324 }
325
326 bool
327 Editor::set_selected_control_point_from_click (Selection::Operation op, bool /*no_remove*/)
328 {
329         if (!clicked_control_point) {
330                 return false;
331         }
332         
333         switch (op) {
334         case Selection::Set:
335                 selection->set (clicked_control_point);
336                 break;
337         case Selection::Add:
338                 selection->add (clicked_control_point);
339                 break;
340         case Selection::Toggle:
341                 selection->toggle (clicked_control_point);
342                 break;
343         case Selection::Extend:
344                 /* XXX */
345                 break;
346         }
347
348         return true;
349 }
350
351 void
352 Editor::get_onscreen_tracks (TrackViewList& tvl)
353 {
354         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
355                 if ((*i)->y_position() < _canvas_height) {
356                         tvl.push_back (*i);
357                 }
358         }
359 }
360
361 /** Call a slot for a given `basis' track and also for any track that is in the same
362  *  active route group with a particular set of properties.
363  *
364  *  @param sl Slot to call.
365  *  @param basis Basis track.
366  *  @param prop Properties that active edit groups must share to be included in the map.
367  */
368
369 void
370 Editor::mapover_tracks (sigc::slot<void, RouteTimeAxisView&, uint32_t> sl, TimeAxisView* basis, PBD::PropertyID prop) const
371 {
372         RouteTimeAxisView* route_basis = dynamic_cast<RouteTimeAxisView*> (basis);
373
374         if (route_basis == 0) {
375                 return;
376         }
377
378         set<RouteTimeAxisView*> tracks;
379         tracks.insert (route_basis);
380
381         RouteGroup* group = route_basis->route()->route_group();
382
383         if (group && group->enabled_property(prop) && group->enabled_property (Properties::active.property_id) ) {
384
385                 /* the basis is a member of an active route group, with the appropriate
386                    properties; find other members */
387
388                 for (TrackViewList::const_iterator i = track_views.begin(); i != track_views.end(); ++i) {
389                         RouteTimeAxisView* v = dynamic_cast<RouteTimeAxisView*> (*i);
390                         if (v && v->route()->route_group() == group) {
391                                 tracks.insert (v);
392                         }
393                 }
394         }
395
396         /* call the slots */
397         uint32_t const sz = tracks.size ();
398         
399         for (set<RouteTimeAxisView*>::iterator i = tracks.begin(); i != tracks.end(); ++i) {
400                 sl (**i, sz);
401         }
402 }
403
404 void
405 Editor::mapped_get_equivalent_regions (RouteTimeAxisView& tv, uint32_t, RegionView * basis, vector<RegionView*>* all_equivs) const
406 {
407         boost::shared_ptr<Playlist> pl;
408         vector<boost::shared_ptr<Region> > results;
409         RegionView* marv;
410         boost::shared_ptr<Track> tr;
411
412         if ((tr = tv.track()) == 0) {
413                 /* bus */
414                 return;
415         }
416
417         if (&tv == &basis->get_time_axis_view()) {
418                 /* looking in same track as the original */
419                 return;
420         }
421
422         if ((pl = tr->playlist()) != 0) {
423                 pl->get_equivalent_regions (basis->region(), results);
424         }
425
426         for (vector<boost::shared_ptr<Region> >::iterator ir = results.begin(); ir != results.end(); ++ir) {
427                 if ((marv = tv.view()->find_view (*ir)) != 0) {
428                         all_equivs->push_back (marv);
429                 }
430         }
431 }
432
433 void
434 Editor::get_equivalent_regions (RegionView* basis, vector<RegionView*>& equivalent_regions, PBD::PropertyID property) const
435 {
436         mapover_tracks (sigc::bind (sigc::mem_fun (*this, &Editor::mapped_get_equivalent_regions), basis, &equivalent_regions), &basis->get_time_axis_view(), property);
437
438         /* add clicked regionview since we skipped all other regions in the same track as the one it was in */
439
440         equivalent_regions.push_back (basis);
441 }
442
443 RegionSelection
444 Editor::get_equivalent_regions (RegionSelection & basis, PBD::PropertyID prop) const
445 {
446         RegionSelection equivalent;
447
448         for (RegionSelection::const_iterator i = basis.begin(); i != basis.end(); ++i) {
449
450                 vector<RegionView*> eq;
451
452                 mapover_tracks (
453                         sigc::bind (sigc::mem_fun (*this, &Editor::mapped_get_equivalent_regions), *i, &eq),
454                         &(*i)->get_time_axis_view(), prop
455                         );
456
457                 for (vector<RegionView*>::iterator j = eq.begin(); j != eq.end(); ++j) {
458                         equivalent.add (*j);
459                 }
460
461                 equivalent.add (*i);
462         }
463
464         return equivalent;
465 }
466
467
468 int
469 Editor::get_regionview_count_from_region_list (boost::shared_ptr<Region> region)
470 {
471         int region_count = 0;
472
473         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
474
475                 RouteTimeAxisView* tatv;
476
477                 if ((tatv = dynamic_cast<RouteTimeAxisView*> (*i)) != 0) {
478
479                         boost::shared_ptr<Playlist> pl;
480                         vector<boost::shared_ptr<Region> > results;
481                         RegionView* marv;
482                         boost::shared_ptr<Track> tr;
483
484                         if ((tr = tatv->track()) == 0) {
485                                 /* bus */
486                                 continue;
487                         }
488
489                         if ((pl = (tr->playlist())) != 0) {
490                                 pl->get_region_list_equivalent_regions (region, results);
491                         }
492
493                         for (vector<boost::shared_ptr<Region> >::iterator ir = results.begin(); ir != results.end(); ++ir) {
494                                 if ((marv = tatv->view()->find_view (*ir)) != 0) {
495                                         region_count++;
496                                 }
497                         }
498
499                 }
500         }
501
502         return region_count;
503 }
504
505
506 bool
507 Editor::set_selected_regionview_from_click (bool press, Selection::Operation op, bool /*no_track_remove*/)
508 {
509         vector<RegionView*> all_equivalent_regions;
510         bool commit = false;
511
512         if (!clicked_regionview || !clicked_routeview) {
513                 return false;
514         }
515
516         if (press) {
517                 button_release_can_deselect = false;
518         }
519
520         if (op == Selection::Toggle || op == Selection::Set) {
521
522
523                 switch (op) {
524                 case Selection::Toggle:
525                         if (selection->selected (clicked_regionview)) {
526                                 if (press) {
527
528                                         /* whatever was clicked was selected already; do nothing here but allow
529                                            the button release to deselect it
530                                         */
531
532                                         button_release_can_deselect = true;
533
534                                 } else {
535                                         if (button_release_can_deselect) {
536
537                                                 /* just remove this one region, but only on a permitted button release */
538                                                 
539                                                 selection->remove (clicked_regionview);
540                                                 commit = true;
541
542                                                 /* no more deselect action on button release till a new press
543                                                    finds an already selected object.
544                                                 */
545
546                                                 button_release_can_deselect = false;
547                                         }
548                                 }
549
550                         } else {
551
552                                 if (press) {
553
554                                         if (selection->selected (clicked_routeview)) {
555                                                 get_equivalent_regions (clicked_regionview, all_equivalent_regions, ARDOUR::Properties::select.property_id);
556                                         } else {
557                                                 all_equivalent_regions.push_back (clicked_regionview);
558                                         }
559
560                                         /* add all the equivalent regions, but only on button press */
561
562                                         if (!all_equivalent_regions.empty()) {
563                                                 commit = true;
564                                         }
565
566                                         selection->add (all_equivalent_regions);
567                                 }
568                         }
569                         break;
570
571                 case Selection::Set:
572                         if (!selection->selected (clicked_regionview)) {
573                                 get_equivalent_regions (clicked_regionview, all_equivalent_regions, ARDOUR::Properties::select.property_id);
574                                 selection->set (all_equivalent_regions);
575                                 commit = true;
576                         } else {
577                                 /* no commit necessary: clicked on an already selected region */
578                                 goto out;
579                         }
580                         break;
581
582                 default:
583                         /* silly compiler */
584                         break;
585                 }
586
587         } else if (op == Selection::Extend) {
588
589                 list<Selectable*> results;
590                 framepos_t last_frame;
591                 framepos_t first_frame;
592                 bool same_track = false;
593
594                 /* 1. find the last selected regionview in the track that was clicked in */
595
596                 last_frame = 0;
597                 first_frame = max_framepos;
598
599                 for (RegionSelection::iterator x = selection->regions.begin(); x != selection->regions.end(); ++x) {
600                         if (&(*x)->get_time_axis_view() == &clicked_regionview->get_time_axis_view()) {
601
602                                 if ((*x)->region()->last_frame() > last_frame) {
603                                         last_frame = (*x)->region()->last_frame();
604                                 }
605
606                                 if ((*x)->region()->first_frame() < first_frame) {
607                                         first_frame = (*x)->region()->first_frame();
608                                 }
609
610                                 same_track = true;
611                         }
612                 }
613
614                 if (same_track) {
615
616                         /* 2. figure out the boundaries for our search for new objects */
617
618                         switch (clicked_regionview->region()->coverage (first_frame, last_frame)) {
619                         case OverlapNone:
620                                 if (last_frame < clicked_regionview->region()->first_frame()) {
621                                         first_frame = last_frame;
622                                         last_frame = clicked_regionview->region()->last_frame();
623                                 } else {
624                                         last_frame = first_frame;
625                                         first_frame = clicked_regionview->region()->first_frame();
626                                 }
627                                 break;
628
629                         case OverlapExternal:
630                                 if (last_frame < clicked_regionview->region()->first_frame()) {
631                                         first_frame = last_frame;
632                                         last_frame = clicked_regionview->region()->last_frame();
633                                 } else {
634                                         last_frame = first_frame;
635                                         first_frame = clicked_regionview->region()->first_frame();
636                                 }
637                                 break;
638
639                         case OverlapInternal:
640                                 if (last_frame < clicked_regionview->region()->first_frame()) {
641                                         first_frame = last_frame;
642                                         last_frame = clicked_regionview->region()->last_frame();
643                                 } else {
644                                         last_frame = first_frame;
645                                         first_frame = clicked_regionview->region()->first_frame();
646                                 }
647                                 break;
648
649                         case OverlapStart:
650                         case OverlapEnd:
651                                 /* nothing to do except add clicked region to selection, since it
652                                    overlaps with the existing selection in this track.
653                                 */
654                                 break;
655                         }
656
657                 } else {
658
659                         /* click in a track that has no regions selected, so extend vertically
660                            to pick out all regions that are defined by the existing selection
661                            plus this one.
662                         */
663
664
665                         first_frame = clicked_regionview->region()->position();
666                         last_frame = clicked_regionview->region()->last_frame();
667
668                         for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
669                                 if ((*i)->region()->position() < first_frame) {
670                                         first_frame = (*i)->region()->position();
671                                 }
672                                 if ((*i)->region()->last_frame() + 1 > last_frame) {
673                                         last_frame = (*i)->region()->last_frame();
674                                 }
675                         }
676                 }
677
678                 /* 2. find all the tracks we should select in */
679
680                 set<RouteTimeAxisView*> relevant_tracks;
681
682                 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
683                         RouteTimeAxisView* r = dynamic_cast<RouteTimeAxisView*> (*i);
684                         if (r) {
685                                 relevant_tracks.insert (r);
686                         }
687                 }
688                 
689                 set<RouteTimeAxisView*> already_in_selection;
690
691                 if (relevant_tracks.empty()) {
692
693                         /* no tracks selected .. thus .. if the
694                            regionview we're in isn't selected
695                            (i.e. we're about to extend to it), then
696                            find all tracks between the this one and
697                            any selected ones.
698                         */
699
700                         if (!selection->selected (clicked_regionview)) {
701
702                                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (&clicked_regionview->get_time_axis_view());
703
704                                 if (rtv) {
705
706                                         /* add this track to the ones we will search */
707
708                                         relevant_tracks.insert (rtv);
709
710                                         /* find the track closest to this one that
711                                            already a selected region.
712                                         */
713
714                                         RouteTimeAxisView* closest = 0;
715                                         int distance = INT_MAX;
716                                         int key = rtv->route()->order_key ("editor");
717
718                                         for (RegionSelection::iterator x = selection->regions.begin(); x != selection->regions.end(); ++x) {
719
720                                                 RouteTimeAxisView* artv = dynamic_cast<RouteTimeAxisView*>(&(*x)->get_time_axis_view());
721
722                                                 if (artv && artv != rtv) {
723
724                                                         pair<set<RouteTimeAxisView*>::iterator,bool> result;
725
726                                                         result = already_in_selection.insert (artv);
727
728                                                         if (result.second) {
729                                                                 /* newly added to already_in_selection */
730
731                                                                 int d = artv->route()->order_key ("editor");
732
733                                                                 d -= key;
734
735                                                                 if (abs (d) < distance) {
736                                                                         distance = abs (d);
737                                                                         closest = artv;
738                                                                 }
739                                                         }
740                                                 }
741                                         }
742
743                                         if (closest) {
744
745                                                 /* now add all tracks between that one and this one */
746
747                                                 int okey = closest->route()->order_key ("editor");
748
749                                                 if (okey > key) {
750                                                         swap (okey, key);
751                                                 }
752
753                                                 for (TrackViewList::iterator x = track_views.begin(); x != track_views.end(); ++x) {
754                                                         RouteTimeAxisView* artv = dynamic_cast<RouteTimeAxisView*>(*x);
755                                                         if (artv && artv != rtv) {
756
757                                                                 int k = artv->route()->order_key ("editor");
758
759                                                                 if (k >= okey && k <= key) {
760
761                                                                         /* in range but don't add it if
762                                                                            it already has tracks selected.
763                                                                            this avoids odd selection
764                                                                            behaviour that feels wrong.
765                                                                         */
766
767                                                                         if (find (already_in_selection.begin(),
768                                                                                   already_in_selection.end(),
769                                                                                   artv) == already_in_selection.end()) {
770
771                                                                                 relevant_tracks.insert (artv);
772                                                                         }
773                                                                 }
774                                                         }
775                                                 }
776                                         }
777                                 }
778                         }
779                 }
780
781                 /* 3. find all selectable objects (regionviews in this case) between that one and the end of the
782                            one that was clicked.
783                 */
784
785                 for (set<RouteTimeAxisView*>::iterator t = relevant_tracks.begin(); t != relevant_tracks.end(); ++t) {
786                         (*t)->get_selectables (first_frame, last_frame, -1.0, -1.0, results);
787                 }
788
789                 /* 4. convert to a vector of regions */
790
791                 vector<RegionView*> regions;
792
793                 for (list<Selectable*>::iterator x = results.begin(); x != results.end(); ++x) {
794                         RegionView* arv;
795
796                         if ((arv = dynamic_cast<RegionView*>(*x)) != 0) {
797                                 regions.push_back (arv);
798                         }
799                 }
800
801                 if (!regions.empty()) {
802                         selection->add (regions);
803                         commit = true;
804                 }
805         }
806
807   out:
808         return commit;
809 }
810
811
812 void
813 Editor::set_selected_regionview_from_region_list (boost::shared_ptr<Region> region, Selection::Operation op)
814 {
815         vector<RegionView*> all_equivalent_regions;
816
817         get_regions_corresponding_to (region, all_equivalent_regions);
818
819         if (all_equivalent_regions.empty()) {
820                 return;
821         }
822
823         begin_reversible_command (_("set selected regions"));
824
825         switch (op) {
826         case Selection::Toggle:
827                 /* XXX this is not correct */
828                 selection->toggle (all_equivalent_regions);
829                 break;
830         case Selection::Set:
831                 selection->set (all_equivalent_regions);
832                 break;
833         case Selection::Extend:
834                 selection->add (all_equivalent_regions);
835                 break;
836         case Selection::Add:
837                 selection->add (all_equivalent_regions);
838                 break;
839         }
840
841         commit_reversible_command () ;
842 }
843
844 bool
845 Editor::set_selected_regionview_from_map_event (GdkEventAny* /*ev*/, StreamView* sv, boost::weak_ptr<Region> weak_r)
846 {
847         RegionView* rv;
848         boost::shared_ptr<Region> r (weak_r.lock());
849
850         if (!r) {
851                 return true;
852         }
853
854         if ((rv = sv->find_view (r)) == 0) {
855                 return true;
856         }
857
858         /* don't reset the selection if its something other than
859            a single other region.
860         */
861
862         if (selection->regions.size() > 1) {
863                 return true;
864         }
865
866         begin_reversible_command (_("set selected regions"));
867
868         selection->set (rv);
869
870         commit_reversible_command () ;
871
872         return true;
873 }
874
875 void
876 Editor::track_selection_changed ()
877 {
878         switch (selection->tracks.size()) {
879         case 0:
880                 break;
881         default:
882                 set_selected_mixer_strip (*(selection->tracks.front()));
883                 break;
884         }
885
886         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
887
888                 bool yn = (find (selection->tracks.begin(), selection->tracks.end(), *i) != selection->tracks.end());
889                 
890                 (*i)->set_selected (yn);
891                 
892                 TimeAxisView::Children c = (*i)->get_child_list ();
893                 for (TimeAxisView::Children::iterator j = c.begin(); j != c.end(); ++j) {
894                         (*j)->set_selected (find (selection->tracks.begin(), selection->tracks.end(), j->get()) != selection->tracks.end());
895                 }
896
897                 if (yn && 
898                     ((mouse_mode == MouseRange) || 
899                      ((mouse_mode == MouseObject) && (_join_object_range_state == JOIN_OBJECT_RANGE_OBJECT)))) {
900                         (*i)->reshow_selection (selection->time);
901                 } else {
902                         (*i)->hide_selection ();
903                 }
904         }
905
906         ActionManager::set_sensitive (ActionManager::track_selection_sensitive_actions, !selection->tracks.empty());
907 }
908
909 void
910 Editor::time_selection_changed ()
911 {
912         if (Profile->get_sae()) {
913                 return;
914         }
915
916         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
917                 (*i)->hide_selection ();
918         }
919
920         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
921                 (*i)->show_selection (selection->time);
922         }
923
924         if (selection->time.empty()) {
925                 ActionManager::set_sensitive (ActionManager::time_selection_sensitive_actions, false);
926         } else {
927                 ActionManager::set_sensitive (ActionManager::time_selection_sensitive_actions, true);
928         }
929 }
930
931 /** Set all region actions to have a given sensitivity */
932 void
933 Editor::sensitize_all_region_actions (bool s)
934 {
935         Glib::ListHandle<Glib::RefPtr<Action> > all = _region_actions->get_actions ();
936
937         for (Glib::ListHandle<Glib::RefPtr<Action> >::iterator i = all.begin(); i != all.end(); ++i) {
938                 (*i)->set_sensitive (s);
939         }
940
941         _all_region_actions_sensitized = s;
942 }
943
944 /** Sensitize region-based actions based on the selection ONLY, ignoring the entered_regionview.
945  *  This method should be called just before displaying a Region menu.  When a Region menu is not
946  *  currently being shown, all region actions are sensitized so that hotkey-triggered actions
947  *  on entered_regionviews work without having to check sensitivity every time the selection or
948  *  entered_regionview changes.
949  *
950  *  This method also sets up toggle action state as appropriate.
951  */
952 void
953 Editor::sensitize_the_right_region_actions ()
954 {
955         if ((mouse_mode == MouseRange) || (mouse_mode != MouseObject && _join_object_range_state == JOIN_OBJECT_RANGE_RANGE)) {
956                 sensitize_all_region_actions (false);
957                 if (!selection->time.empty()) {
958                         _region_actions->get_action("split-region")->set_sensitive (true);
959                 }
960                 return;
961
962         } else if (mouse_mode != MouseObject) {
963                 sensitize_all_region_actions (false);
964                 return;
965         }
966
967         /* We get here if we are in Object mode */
968                         
969         RegionSelection rs = get_regions_from_selection_and_entered ();
970         sensitize_all_region_actions (!rs.empty ());
971
972         _ignore_region_action = true;
973         
974         /* Look through the regions that are selected and make notes about what we have got */
975         
976         bool have_audio = false;
977         bool have_midi = false;
978         bool have_locked = false;
979         bool have_unlocked = false;
980         bool have_position_lock_style_audio = false;
981         bool have_position_lock_style_music = false;
982         bool have_muted = false;
983         bool have_unmuted = false;
984         bool have_opaque = false;
985         bool have_non_opaque = false;
986         bool have_not_at_natural_position = false;
987         bool have_envelope_visible = false;
988         bool have_envelope_invisible = false;
989         bool have_envelope_active = false;
990         bool have_envelope_inactive = false;
991         bool have_non_unity_scale_amplitude = false;
992         bool have_compound_regions = false;
993
994         for (list<RegionView*>::const_iterator i = rs.begin(); i != rs.end(); ++i) {
995
996                 boost::shared_ptr<Region> r = (*i)->region ();
997                 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (r);
998                 
999                 if (ar) {
1000                         have_audio = true;
1001                 }
1002                 
1003                 if (boost::dynamic_pointer_cast<MidiRegion> (r)) {
1004                         have_midi = true;
1005                 }
1006
1007                 if (r->is_compound()) {
1008                         have_compound_regions = true;
1009                 }
1010
1011                 if (r->locked()) {
1012                         have_locked = true;
1013                 } else {
1014                         have_unlocked = true;
1015                 }
1016
1017                 if (r->position_lock_style() == MusicTime) {
1018                         have_position_lock_style_music = true;
1019                 } else {
1020                         have_position_lock_style_audio = true;
1021                 }
1022
1023                 if (r->muted()) {
1024                         have_muted = true;
1025                 } else {
1026                         have_unmuted = true;
1027                 }
1028
1029                 if (r->opaque()) {
1030                         have_opaque = true;
1031                 } else {
1032                         have_non_opaque = true;
1033                 }
1034
1035                 if (!r->at_natural_position()) {
1036                         have_not_at_natural_position = true;
1037                 }
1038
1039                 if (ar) {
1040                         /* its a bit unfortunate that "envelope visible" is a view-only
1041                            property. we have to find the regionview to able to check
1042                            its current setting.
1043                         */
1044
1045                         have_envelope_invisible = true;
1046
1047                         if (*i) {
1048                                 AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*i);
1049                                 if (arv && arv->envelope_visible()) {
1050                                         have_envelope_visible = true;
1051                                 }
1052                         }
1053
1054                         if (ar->envelope_active()) {
1055                                 have_envelope_active = true;
1056                         } else {
1057                                 have_envelope_inactive = true;
1058                         }
1059
1060                         if (ar->scale_amplitude() != 1) {
1061                                 have_non_unity_scale_amplitude = true;
1062                         }
1063                 }
1064         }
1065
1066         if (rs.size() > 1) {
1067                 _region_actions->get_action("show-region-list-editor")->set_sensitive (false);
1068                 _region_actions->get_action("show-region-properties")->set_sensitive (false);
1069                 _region_actions->get_action("rename-region")->set_sensitive (false);
1070                 _region_actions->get_action("combine-regions")->set_sensitive (true);
1071         } else if (rs.size() == 1) {
1072                 _region_actions->get_action("add-range-markers-from-region")->set_sensitive (false);
1073                 _region_actions->get_action("close-region-gaps")->set_sensitive (false);
1074                 _region_actions->get_action("combine-regions")->set_sensitive (false);
1075         } 
1076
1077         if (!have_midi) {
1078                 _region_actions->get_action("show-region-list-editor")->set_sensitive (false);
1079                 _region_actions->get_action("quantize-region")->set_sensitive (false);
1080                 _region_actions->get_action("fork-region")->set_sensitive (false);
1081                 _region_actions->get_action("transpose-region")->set_sensitive (false);
1082         }
1083
1084         if (_edit_point == EditAtMouse) {
1085                 _region_actions->get_action("set-region-sync-position")->set_sensitive (false);
1086                 _region_actions->get_action("trim-front")->set_sensitive (false);
1087                 _region_actions->get_action("trim-back")->set_sensitive (false);
1088                 _region_actions->get_action("split-region")->set_sensitive (false);
1089                 _region_actions->get_action("place-transient")->set_sensitive (false);
1090         }
1091
1092         if (have_compound_regions) {
1093                 _region_actions->get_action("uncombine-regions")->set_sensitive (true);
1094         } else {
1095                 _region_actions->get_action("uncombine-regions")->set_sensitive (false);
1096         }
1097
1098         if (have_audio) {
1099                 
1100                 if (have_envelope_visible && !have_envelope_invisible) {
1101                         Glib::RefPtr<ToggleAction>::cast_dynamic (_region_actions->get_action("toggle-region-gain-envelope-visible"))->set_active ();
1102                 } else if (have_envelope_visible && have_envelope_invisible) {
1103 //                      _region_actions->get_action("toggle-region-gain-envelope-visible")->set_inconsistent ();
1104                 }
1105                 
1106                 if (have_envelope_active && !have_envelope_inactive) {
1107                         Glib::RefPtr<ToggleAction>::cast_dynamic (_region_actions->get_action("toggle-region-gain-envelope-active"))->set_active ();
1108                 } else if (have_envelope_active && have_envelope_inactive) {
1109 //                      _region_actions->get_action("toggle-region-gain-envelope-active")->set_inconsistent ();
1110                 }
1111         
1112         } else {
1113                 
1114                 _region_actions->get_action("analyze-region")->set_sensitive (false);
1115                 _region_actions->get_action("reset-region-gain-envelopes")->set_sensitive (false);
1116                 _region_actions->get_action("toggle-region-gain-envelope-visible")->set_sensitive (false);
1117                 _region_actions->get_action("toggle-region-gain-envelope-active")->set_sensitive (false);
1118                 _region_actions->get_action("pitch-shift-region")->set_sensitive (false);
1119                 
1120         }
1121
1122         if (!have_non_unity_scale_amplitude || !have_audio) {
1123                 _region_actions->get_action("reset-region-scale-amplitude")->set_sensitive (false);
1124         }
1125                 
1126         Glib::RefPtr<ToggleAction>::cast_dynamic (_region_actions->get_action("toggle-region-lock"))->set_active (have_locked && !have_unlocked);
1127         if (have_locked && have_unlocked) {
1128 //              _region_actions->get_action("toggle-region-lock")->set_inconsistent ();
1129         }
1130
1131         Glib::RefPtr<ToggleAction>::cast_dynamic (_region_actions->get_action("toggle-region-lock-style"))->set_active (have_position_lock_style_music && !have_position_lock_style_audio);
1132                 
1133         if (have_position_lock_style_music && have_position_lock_style_audio) {
1134 //              _region_actions->get_action("toggle-region-lock-style")->set_inconsistent ();
1135         }
1136
1137         Glib::RefPtr<ToggleAction>::cast_dynamic (_region_actions->get_action("toggle-region-mute"))->set_active (have_muted && !have_unmuted);
1138         if (have_muted && have_unmuted) {
1139 //              _region_actions->get_action("toggle-region-mute")->set_inconsistent ();
1140         }
1141         
1142         Glib::RefPtr<ToggleAction>::cast_dynamic (_region_actions->get_action("toggle-opaque-region"))->set_active (have_opaque && !have_non_opaque);
1143         if (have_opaque && have_non_opaque) {
1144 //                      _region_actions->get_action("toggle-opaque-region")->set_inconsistent ();
1145         }
1146
1147         if (!have_not_at_natural_position) {
1148                 _region_actions->get_action("naturalize-region")->set_sensitive (false);
1149         }
1150
1151         /* XXX: should also check that there is a track of the appropriate type for the selected region */
1152         if (_edit_point == EditAtMouse || _regions->get_single_selection() == 0 || selection->tracks.empty()) {
1153                 _region_actions->get_action("insert-region-from-region-list")->set_sensitive (false);
1154         } else {
1155                 _region_actions->get_action("insert-region-from-region-list")->set_sensitive (true);
1156         }
1157
1158         _ignore_region_action = false;
1159         
1160         _all_region_actions_sensitized = false;
1161 }
1162
1163
1164 void
1165 Editor::region_selection_changed ()
1166 {
1167         _regions->block_change_connection (true);
1168         editor_regions_selection_changed_connection.block(true);
1169
1170         if (_region_selection_change_updates_region_list) {
1171                 _regions->unselect_all ();
1172         }
1173
1174         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
1175                 (*i)->set_selected_regionviews (selection->regions);
1176         }
1177
1178         if (_region_selection_change_updates_region_list) {
1179                 _regions->set_selected (selection->regions);
1180         }
1181
1182         _regions->block_change_connection (false);
1183         editor_regions_selection_changed_connection.block(false);
1184
1185         if (!_all_region_actions_sensitized) {
1186                 /* This selection change might have changed what region actions
1187                    are allowed, so sensitize them all in case a key is pressed.
1188                 */
1189                 sensitize_all_region_actions (true);
1190         }
1191 }
1192
1193 void
1194 Editor::point_selection_changed ()
1195 {
1196         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
1197                 (*i)->set_selected_points (selection->points);
1198         }
1199 }
1200
1201 void
1202 Editor::select_all_in_track (Selection::Operation op)
1203 {
1204         list<Selectable *> touched;
1205
1206         if (!clicked_routeview) {
1207                 return;
1208         }
1209
1210         clicked_routeview->get_selectables (0, max_framepos, 0, DBL_MAX, touched);
1211
1212         switch (op) {
1213         case Selection::Toggle:
1214                 selection->add (touched);
1215                 break;
1216         case Selection::Set:
1217                 selection->set (touched);
1218                 break;
1219         case Selection::Extend:
1220                 /* meaningless, because we're selecting everything */
1221                 break;
1222         case Selection::Add:
1223                 selection->add (touched);
1224                 break;
1225         }
1226 }
1227
1228 void
1229 Editor::select_all_internal_edit (Selection::Operation op)
1230 {
1231         /* currently limited to MIDI only */
1232
1233         for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
1234                 MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
1235                 if (mrv) {
1236                         mrv->select_all_notes ();
1237                 }
1238         }
1239 }
1240
1241 void
1242 Editor::select_all (Selection::Operation op)
1243 {
1244         list<Selectable *> touched;
1245
1246         if (_internal_editing) {
1247                 select_all_internal_edit (op);
1248                 return;
1249         }
1250
1251         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
1252                 if ((*iter)->hidden()) {
1253                         continue;
1254                 }
1255                 (*iter)->get_selectables (0, max_framepos, 0, DBL_MAX, touched);
1256         }
1257         begin_reversible_command (_("select all"));
1258         switch (op) {
1259         case Selection::Add:
1260                 selection->add (touched);
1261                 break;
1262         case Selection::Toggle:
1263                 selection->add (touched);
1264                 break;
1265         case Selection::Set:
1266                 selection->set (touched);
1267                 break;
1268         case Selection::Extend:
1269                 /* meaningless, because we're selecting everything */
1270                 break;
1271         }
1272         commit_reversible_command ();
1273 }
1274 void
1275 Editor::invert_selection_in_track ()
1276 {
1277         list<Selectable *> touched;
1278
1279         if (!clicked_routeview) {
1280                 return;
1281         }
1282
1283         clicked_routeview->get_inverted_selectables (*selection, touched);
1284         selection->set (touched);
1285 }
1286
1287 void
1288 Editor::invert_selection ()
1289 {
1290         list<Selectable *> touched;
1291
1292         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
1293                 if ((*iter)->hidden()) {
1294                         continue;
1295                 }
1296                 (*iter)->get_inverted_selectables (*selection, touched);
1297         }
1298
1299         selection->set (touched);
1300 }
1301
1302 /** @param start Start time in session frames.
1303  *  @param end End time in session frames.
1304  *  @param top Top (lower) y limit in trackview coordinates (ie 0 at the top of the track view)
1305  *  @param bottom Bottom (higher) y limit in trackview coordinates (ie 0 at the top of the track view)
1306  *  @param preserve_if_selected true to leave the current selection alone if we're adding to the selection and all of the selectables
1307  *  within the region are already selected.
1308  */
1309 void
1310 Editor::select_all_within (
1311         framepos_t start, framepos_t end, double top, double bot, const TrackViewList& tracklist, Selection::Operation op, bool preserve_if_selected
1312         )
1313 {
1314         list<Selectable*> found;
1315
1316         for (TrackViewList::const_iterator iter = tracklist.begin(); iter != tracklist.end(); ++iter) {
1317                 
1318                 if ((*iter)->hidden()) {
1319                         continue;
1320                 }
1321
1322                 (*iter)->get_selectables (start, end, top, bot, found);
1323         }
1324
1325         if (found.empty()) {
1326                 return;
1327         }
1328
1329         if (preserve_if_selected && op != Selection::Toggle) {
1330                 list<Selectable*>::iterator i = found.begin();
1331                 while (i != found.end() && (*i)->get_selected()) {
1332                         ++i;
1333                 }
1334
1335                 if (i == found.end()) {
1336                         return;
1337                 }
1338         }
1339
1340         begin_reversible_command (_("select all within"));
1341         switch (op) {
1342         case Selection::Add:
1343                 selection->add (found);
1344                 break;
1345         case Selection::Toggle:
1346                 selection->toggle (found);
1347                 break;
1348         case Selection::Set:
1349                 selection->set (found);
1350                 break;
1351         case Selection::Extend:
1352                 /* not defined yet */
1353                 break;
1354         }
1355
1356         commit_reversible_command ();
1357 }
1358
1359 void
1360 Editor::set_selection_from_region ()
1361 {
1362         if (selection->regions.empty()) {
1363                 return;
1364         }
1365
1366         selection->set (selection->regions.start(), selection->regions.end_frame());
1367         if (!Profile->get_sae()) {
1368                 set_mouse_mode (Editing::MouseRange, false);
1369         }
1370 }
1371
1372 void
1373 Editor::set_selection_from_punch()
1374 {
1375         Location* location;
1376
1377         if ((location = _session->locations()->auto_punch_location()) == 0)  {
1378                 return;
1379         }
1380
1381         set_selection_from_range (*location);
1382 }
1383
1384 void
1385 Editor::set_selection_from_loop()
1386 {
1387         Location* location;
1388
1389         if ((location = _session->locations()->auto_loop_location()) == 0)  {
1390                 return;
1391         }
1392         set_selection_from_range (*location);
1393 }
1394
1395 void
1396 Editor::set_selection_from_range (Location& loc)
1397 {
1398         begin_reversible_command (_("set selection from range"));
1399         selection->set (loc.start(), loc.end());
1400         commit_reversible_command ();
1401
1402         if (!Profile->get_sae()) {
1403                 set_mouse_mode (Editing::MouseRange, false);
1404         }
1405 }
1406
1407 void
1408 Editor::select_all_selectables_using_time_selection ()
1409 {
1410         list<Selectable *> touched;
1411
1412         if (selection->time.empty()) {
1413                 return;
1414         }
1415
1416         framepos_t start = selection->time[clicked_selection].start;
1417         framepos_t end = selection->time[clicked_selection].end;
1418
1419         if (end - start < 1)  {
1420                 return;
1421         }
1422
1423         TrackViewList* ts;
1424
1425         if (selection->tracks.empty()) {
1426                 ts = &track_views;
1427         } else {
1428                 ts = &selection->tracks;
1429         }
1430
1431         for (TrackViewList::iterator iter = ts->begin(); iter != ts->end(); ++iter) {
1432                 if ((*iter)->hidden()) {
1433                         continue;
1434                 }
1435                 (*iter)->get_selectables (start, end - 1, 0, DBL_MAX, touched);
1436         }
1437
1438         begin_reversible_command (_("select all from range"));
1439         selection->set (touched);
1440         commit_reversible_command ();
1441 }
1442
1443
1444 void
1445 Editor::select_all_selectables_using_punch()
1446 {
1447         Location* location = _session->locations()->auto_punch_location();
1448         list<Selectable *> touched;
1449
1450         if (location == 0 || (location->end() - location->start() <= 1))  {
1451                 return;
1452         }
1453
1454
1455         TrackViewList* ts;
1456
1457         if (selection->tracks.empty()) {
1458                 ts = &track_views;
1459         } else {
1460                 ts = &selection->tracks;
1461         }
1462
1463         for (TrackViewList::iterator iter = ts->begin(); iter != ts->end(); ++iter) {
1464                 if ((*iter)->hidden()) {
1465                         continue;
1466                 }
1467                 (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
1468         }
1469         begin_reversible_command (_("select all from punch"));
1470         selection->set (touched);
1471         commit_reversible_command ();
1472
1473 }
1474
1475 void
1476 Editor::select_all_selectables_using_loop()
1477 {
1478         Location* location = _session->locations()->auto_loop_location();
1479         list<Selectable *> touched;
1480
1481         if (location == 0 || (location->end() - location->start() <= 1))  {
1482                 return;
1483         }
1484
1485
1486         TrackViewList* ts;
1487
1488         if (selection->tracks.empty()) {
1489                 ts = &track_views;
1490         } else {
1491                 ts = &selection->tracks;
1492         }
1493
1494         for (TrackViewList::iterator iter = ts->begin(); iter != ts->end(); ++iter) {
1495                 if ((*iter)->hidden()) {
1496                         continue;
1497                 }
1498                 (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
1499         }
1500         begin_reversible_command (_("select all from loop"));
1501         selection->set (touched);
1502         commit_reversible_command ();
1503
1504 }
1505
1506 void
1507 Editor::select_all_selectables_using_cursor (EditorCursor *cursor, bool after)
1508 {
1509         framepos_t start;
1510         framepos_t end;
1511         list<Selectable *> touched;
1512
1513         if (after) {
1514                 begin_reversible_command (_("select all after cursor"));
1515                 start = cursor->current_frame;
1516                 end = _session->current_end_frame();
1517         } else {
1518                 if (cursor->current_frame > 0) {
1519                         begin_reversible_command (_("select all before cursor"));
1520                         start = 0;
1521                         end = cursor->current_frame - 1;
1522                 } else {
1523                         return;
1524                 }
1525         }
1526
1527
1528         TrackViewList* ts;
1529
1530         if (selection->tracks.empty()) {
1531                 ts = &track_views;
1532         } else {
1533                 ts = &selection->tracks;
1534         }
1535
1536         for (TrackViewList::iterator iter = ts->begin(); iter != ts->end(); ++iter) {
1537                 if ((*iter)->hidden()) {
1538                         continue;
1539                 }
1540                 (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
1541         }
1542         selection->set (touched);
1543         commit_reversible_command ();
1544 }
1545
1546 void
1547 Editor::select_all_selectables_using_edit (bool after)
1548 {
1549         framepos_t start;
1550         framepos_t end;
1551         list<Selectable *> touched;
1552
1553         if (after) {
1554                 begin_reversible_command (_("select all after edit"));
1555                 start = get_preferred_edit_position();
1556                 end = _session->current_end_frame();
1557         } else {
1558                 if ((end = get_preferred_edit_position()) > 1) {
1559                         begin_reversible_command (_("select all before edit"));
1560                         start = 0;
1561                         end -= 1;
1562                 } else {
1563                         return;
1564                 }
1565         }
1566
1567
1568         TrackViewList* ts;
1569
1570         if (selection->tracks.empty()) {
1571                 ts = &track_views;
1572         } else {
1573                 ts = &selection->tracks;
1574         }
1575
1576         for (TrackViewList::iterator iter = ts->begin(); iter != ts->end(); ++iter) {
1577                 if ((*iter)->hidden()) {
1578                         continue;
1579                 }
1580                 (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
1581         }
1582         selection->set (touched);
1583         commit_reversible_command ();
1584 }
1585
1586 void
1587 Editor::select_all_selectables_between (bool /*within*/)
1588 {
1589         framepos_t start;
1590         framepos_t end;
1591         list<Selectable *> touched;
1592
1593         if (!get_edit_op_range (start, end)) {
1594                 return;
1595         }
1596
1597         TrackViewList* ts;
1598
1599         if (selection->tracks.empty()) {
1600                 ts = &track_views;
1601         } else {
1602                 ts = &selection->tracks;
1603         }
1604
1605         for (TrackViewList::iterator iter = ts->begin(); iter != ts->end(); ++iter) {
1606                 if ((*iter)->hidden()) {
1607                         continue;
1608                 }
1609                 (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
1610         }
1611
1612         selection->set (touched);
1613 }
1614
1615 void
1616 Editor::select_range_between ()
1617 {
1618         framepos_t start;
1619         framepos_t end;
1620
1621         if (mouse_mode == MouseRange && !selection->time.empty()) {
1622                 selection->clear_time ();
1623         }
1624
1625         if (!get_edit_op_range (start, end)) {
1626                 return;
1627         }
1628
1629         set_mouse_mode (MouseRange);
1630         selection->set (start, end);
1631 }
1632
1633 bool
1634 Editor::get_edit_op_range (framepos_t& start, framepos_t& end) const
1635 {
1636         framepos_t m;
1637         bool ignored;
1638
1639         /* in range mode, use any existing selection */
1640
1641         if (mouse_mode == MouseRange && !selection->time.empty()) {
1642                 /* we know that these are ordered */
1643                 start = selection->time.start();
1644                 end = selection->time.end_frame();
1645                 return true;
1646         }
1647
1648         if (!mouse_frame (m, ignored)) {
1649                 /* mouse is not in a canvas, try playhead+selected marker.
1650                    this is probably most true when using menus.
1651                  */
1652
1653                 if (selection->markers.empty()) {
1654                         return false;
1655                 }
1656
1657                 start = selection->markers.front()->position();
1658                 end = _session->audible_frame();
1659
1660         } else {
1661
1662                 switch (_edit_point) {
1663                 case EditAtPlayhead:
1664                         if (selection->markers.empty()) {
1665                                 /* use mouse + playhead */
1666                                 start = m;
1667                                 end = _session->audible_frame();
1668                         } else {
1669                                 /* use playhead + selected marker */
1670                                 start = _session->audible_frame();
1671                                 end = selection->markers.front()->position();
1672                         }
1673                         break;
1674
1675                 case EditAtMouse:
1676                         /* use mouse + selected marker */
1677                         if (selection->markers.empty()) {
1678                                 start = m;
1679                                 end = _session->audible_frame();
1680                         } else {
1681                                 start = selection->markers.front()->position();
1682                                 end = m;
1683                         }
1684                         break;
1685
1686                 case EditAtSelectedMarker:
1687                         /* use mouse + selected marker */
1688                         if (selection->markers.empty()) {
1689
1690                                 MessageDialog win (_("No edit range defined"),
1691                                                    false,
1692                                                    MESSAGE_INFO,
1693                                                    BUTTONS_OK);
1694
1695                                 win.set_secondary_text (
1696                                         _("the edit point is Selected Marker\nbut there is no selected marker."));
1697
1698
1699                                 win.set_default_response (RESPONSE_CLOSE);
1700                                 win.set_position (Gtk::WIN_POS_MOUSE);
1701                                 win.show_all();
1702
1703                                 win.run ();
1704
1705                                 return false; // NO RANGE
1706                         }
1707                         start = selection->markers.front()->position();
1708                         end = m;
1709                         break;
1710                 }
1711         }
1712
1713         if (start == end) {
1714                 return false;
1715         }
1716
1717         if (start > end) {
1718                 swap (start, end);
1719         }
1720
1721         /* turn range into one delimited by start...end,
1722            not start...end-1
1723         */
1724
1725         end++;
1726
1727         return true;
1728 }
1729
1730 void
1731 Editor::deselect_all ()
1732 {
1733         selection->clear ();
1734 }
1735
1736 long
1737 Editor::select_range_around_region (RegionView* rv)
1738 {
1739         assert (rv);
1740         
1741         selection->set (&rv->get_time_axis_view());
1742         
1743         selection->time.clear ();
1744         boost::shared_ptr<Region> r = rv->region ();
1745         return selection->set (r->position(), r->position() + r->length());
1746 }