use leave/enter from track canvas to be the primary driver of region action sensitivity
[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/midi_region.h"
26 #include "ardour/playlist.h"
27 #include "ardour/profile.h"
28 #include "ardour/route_group.h"
29 #include "ardour/session.h"
30
31 #include "control_protocol/control_protocol.h"
32
33 #include "editor_drag.h"
34 #include "editor.h"
35 #include "actions.h"
36 #include "audio_time_axis.h"
37 #include "audio_region_view.h"
38 #include "audio_streamview.h"
39 #include "automation_line.h"
40 #include "control_point.h"
41 #include "editor_regions.h"
42 #include "editor_cursors.h"
43 #include "midi_region_view.h"
44 #include "sfdb_ui.h"
45
46 #include "pbd/i18n.h"
47
48 using namespace std;
49 using namespace ARDOUR;
50 using namespace PBD;
51 using namespace Gtk;
52 using namespace Glib;
53 using namespace Gtkmm2ext;
54 using namespace Editing;
55
56 struct TrackViewByPositionSorter
57 {
58         bool operator() (const TimeAxisView* a, const TimeAxisView *b) {
59                 return a->y_position() < b->y_position();
60         }
61 };
62
63 bool
64 Editor::extend_selection_to_track (TimeAxisView& view)
65 {
66         if (selection->selected (&view)) {
67                 /* already selected, do nothing */
68                 return false;
69         }
70
71         if (selection->tracks.empty()) {
72
73                 if (!selection->selected (&view)) {
74                         selection->set (&view);
75                         return true;
76                 } else {
77                         return false;
78                 }
79         }
80
81         /* something is already selected, so figure out which range of things to add */
82
83         TrackViewList to_be_added;
84         TrackViewList sorted = track_views;
85         TrackViewByPositionSorter cmp;
86         bool passed_clicked = false;
87         bool forwards = true;
88
89         sorted.sort (cmp);
90
91         if (!selection->selected (&view)) {
92                 to_be_added.push_back (&view);
93         }
94
95         /* figure out if we should go forward or backwards */
96
97         for (TrackViewList::iterator i = sorted.begin(); i != sorted.end(); ++i) {
98
99                 if ((*i) == &view) {
100                         passed_clicked = true;
101                 }
102
103                 if (selection->selected (*i)) {
104                         if (passed_clicked) {
105                                 forwards = true;
106                         } else {
107                                 forwards = false;
108                         }
109                         break;
110                 }
111         }
112
113         passed_clicked = false;
114
115         if (forwards) {
116
117                 for (TrackViewList::iterator i = sorted.begin(); i != sorted.end(); ++i) {
118
119                         if ((*i) == &view) {
120                                 passed_clicked = true;
121                                 continue;
122                         }
123
124                         if (passed_clicked) {
125                                 if ((*i)->hidden()) {
126                                         continue;
127                                 }
128                                 if (selection->selected (*i)) {
129                                         break;
130                                 } else if (!(*i)->hidden()) {
131                                         to_be_added.push_back (*i);
132                                 }
133                         }
134                 }
135
136         } else {
137
138                 for (TrackViewList::reverse_iterator r = sorted.rbegin(); r != sorted.rend(); ++r) {
139
140                         if ((*r) == &view) {
141                                 passed_clicked = true;
142                                 continue;
143                         }
144
145                         if (passed_clicked) {
146
147                                 if ((*r)->hidden()) {
148                                         continue;
149                                 }
150
151                                 if (selection->selected (*r)) {
152                                         break;
153                                 } else if (!(*r)->hidden()) {
154                                         to_be_added.push_back (*r);
155                                 }
156                         }
157                 }
158         }
159
160         if (!to_be_added.empty()) {
161                 selection->add (to_be_added);
162                 return true;
163         }
164
165         return false;
166 }
167
168 void
169 Editor::select_all_tracks ()
170 {
171         TrackViewList visible_views;
172         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
173                 if ((*i)->marked_for_display()) {
174                         visible_views.push_back (*i);
175                 }
176         }
177         selection->set (visible_views);
178 }
179
180 /** Select clicked_axisview, unless there are no currently selected
181  *  tracks, in which case nothing will happen unless `force' is true.
182  */
183 void
184 Editor::set_selected_track_as_side_effect (Selection::Operation op)
185 {
186         if (!clicked_axisview) {
187                 return;
188         }
189
190         RouteGroup* group = NULL;
191         if (clicked_routeview) {
192                 group = clicked_routeview->route()->route_group();
193         }
194
195         switch (op) {
196         case Selection::Toggle:
197                 if (selection->selected (clicked_axisview)) {
198                         if (group && group->is_active()) {
199                                 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end (); ++i) {
200                                         if ((*i)->route_group() == group) {
201                                                 selection->remove(*i);
202                                         }
203                                 }
204                         } else {
205                                 selection->remove (clicked_axisview);
206                         }
207                 } else {
208                         if (group && group->is_active()) {
209                                 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end (); ++i) {
210                                         if ((*i)->route_group() == group) {
211                                                 selection->add(*i);
212                                         }
213                                 }
214                         } else {
215                                 selection->add (clicked_axisview);
216                         }
217                 }
218                 break;
219
220         case Selection::Add:
221                 if (group && group->is_active()) {
222                         for (TrackViewList::iterator i  = track_views.begin(); i != track_views.end (); ++i) {
223                                 if ((*i)->route_group() == group) {
224                                         selection->add(*i);
225                                 }
226                         }
227                 } else {
228                         selection->add (clicked_axisview);
229                 }
230                 break;
231
232         case Selection::Set:
233                 selection->clear();
234                 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                         }
240                 } else {
241                         selection->set (clicked_axisview);
242                 }
243                 break;
244
245         case Selection::Extend:
246                 selection->clear();
247                 break;
248         }
249 }
250
251 void
252 Editor::set_selected_track (TimeAxisView& view, Selection::Operation op, bool no_remove)
253 {
254         begin_reversible_selection_op (X_("Set Selected Track"));
255
256         switch (op) {
257         case Selection::Toggle:
258                 if (selection->selected (&view)) {
259                         if (!no_remove) {
260                                 selection->remove (&view);
261                         }
262                 } else {
263                         selection->add (&view);
264                 }
265                 break;
266
267         case Selection::Add:
268                 if (!selection->selected (&view)) {
269                         selection->add (&view);
270                 }
271                 break;
272
273         case Selection::Set:
274                 selection->set (&view);
275                 break;
276
277         case Selection::Extend:
278                 extend_selection_to_track (view);
279                 break;
280         }
281
282         commit_reversible_selection_op ();
283 }
284
285 void
286 Editor::set_selected_track_from_click (bool press, Selection::Operation op, bool no_remove)
287 {
288         if (!clicked_routeview) {
289                 return;
290         }
291
292         if (!press) {
293                 return;
294         }
295
296         set_selected_track (*clicked_routeview, op, no_remove);
297 }
298
299 bool
300 Editor::set_selected_control_point_from_click (bool press, Selection::Operation op)
301 {
302         if (!clicked_control_point) {
303                 return false;
304         }
305
306         bool ret = false;
307
308         switch (op) {
309         case Selection::Set:
310                 if (!selection->selected (clicked_control_point)) {
311                         selection->set (clicked_control_point);
312                         ret = true;
313                 } else {
314                         /* clicked on an already selected point */
315                         if (press) {
316                                 break;
317                         } else {
318                                 if (selection->points.size() > 1) {
319                                         selection->set (clicked_control_point);
320                                         ret = true;
321                                 }
322                         }
323                 }
324                 break;
325
326         case Selection::Add:
327                 if (press) {
328                         selection->add (clicked_control_point);
329                         ret = true;
330                 }
331                 break;
332         case Selection::Toggle:
333
334                 /* This is a bit of a hack; if we Primary-Click-Drag a control
335                    point (for push drag) we want the point we clicked on to be
336                    selected, otherwise we end up confusingly dragging an
337                    unselected point.  So here we ensure that the point is selected
338                    after the press, and if we subsequently get a release (meaning no
339                    drag occurred) we set things up so that the toggle has happened.
340                 */
341                 if (press && !selection->selected (clicked_control_point)) {
342                         /* This is the button press, and the control point is not selected; make it so,
343                            in case this press leads to a drag.  Also note that having done this, we don't
344                            need to toggle again on release.
345                         */
346                         selection->toggle (clicked_control_point);
347                         _control_point_toggled_on_press = true;
348                         ret = true;
349                 } else if (!press && !_control_point_toggled_on_press) {
350                         /* This is the release, and the point wasn't toggled on the press, so do it now */
351                         selection->toggle (clicked_control_point);
352                         ret = true;
353                 } else {
354                         /* Reset our flag */
355                         _control_point_toggled_on_press = false;
356                 }
357                 break;
358         case Selection::Extend:
359                 /* XXX */
360                 break;
361         }
362
363         return ret;
364 }
365
366 void
367 Editor::get_onscreen_tracks (TrackViewList& tvl)
368 {
369         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
370                 if ((*i)->y_position() < _visible_canvas_height) {
371                         tvl.push_back (*i);
372                 }
373         }
374 }
375
376 /** Call a slot for a given `basis' track and also for any track that is in the same
377  *  active route group with a particular set of properties.
378  *
379  *  @param sl Slot to call.
380  *  @param basis Basis track.
381  *  @param prop Properties that active edit groups must share to be included in the map.
382  */
383
384 void
385 Editor::mapover_tracks (sigc::slot<void, RouteTimeAxisView&, uint32_t> sl, TimeAxisView* basis, PBD::PropertyID prop) const
386 {
387         RouteTimeAxisView* route_basis = dynamic_cast<RouteTimeAxisView*> (basis);
388
389         if (route_basis == 0) {
390                 return;
391         }
392
393         set<RouteTimeAxisView*> tracks;
394         tracks.insert (route_basis);
395
396         RouteGroup* group = route_basis->route()->route_group();
397
398         if (group && group->enabled_property(prop) && group->enabled_property (Properties::active.property_id) ) {
399
400                 /* the basis is a member of an active route group, with the appropriate
401                    properties; find other members */
402
403                 for (TrackViewList::const_iterator i = track_views.begin(); i != track_views.end(); ++i) {
404                         RouteTimeAxisView* v = dynamic_cast<RouteTimeAxisView*> (*i);
405                         if (v && v->route()->route_group() == group) {
406                                 tracks.insert (v);
407                         }
408                 }
409         }
410
411         /* call the slots */
412         uint32_t const sz = tracks.size ();
413
414         for (set<RouteTimeAxisView*>::iterator i = tracks.begin(); i != tracks.end(); ++i) {
415                 sl (**i, sz);
416         }
417 }
418
419 /** Call a slot for a given `basis' track and also for any track that is in the same
420  *  active route group with a particular set of properties.
421  *
422  *  @param sl Slot to call.
423  *  @param basis Basis track.
424  *  @param prop Properties that active edit groups must share to be included in the map.
425  */
426
427 void
428 Editor::mapover_tracks_with_unique_playlists (sigc::slot<void, RouteTimeAxisView&, uint32_t> sl, TimeAxisView* basis, PBD::PropertyID prop) const
429 {
430         RouteTimeAxisView* route_basis = dynamic_cast<RouteTimeAxisView*> (basis);
431         set<boost::shared_ptr<Playlist> > playlists;
432
433         if (route_basis == 0) {
434                 return;
435         }
436
437         set<RouteTimeAxisView*> tracks;
438         tracks.insert (route_basis);
439
440         RouteGroup* group = route_basis->route()->route_group(); // could be null, not a problem
441
442         if (group && group->enabled_property(prop) && group->enabled_property (Properties::active.property_id) ) {
443
444                 /* the basis is a member of an active route group, with the appropriate
445                    properties; find other members */
446
447                 for (TrackViewList::const_iterator i = track_views.begin(); i != track_views.end(); ++i) {
448                         RouteTimeAxisView* v = dynamic_cast<RouteTimeAxisView*> (*i);
449
450                         if (v && v->route()->route_group() == group) {
451
452                                 boost::shared_ptr<Track> t = v->track();
453                                 if (t) {
454                                         if (playlists.insert (t->playlist()).second) {
455                                                 /* haven't seen this playlist yet */
456                                                 tracks.insert (v);
457                                         }
458                                 } else {
459                                         /* not actually a "Track", but a timeaxis view that
460                                            we should mapover anyway.
461                                         */
462                                         tracks.insert (v);
463                                 }
464                         }
465                 }
466         }
467
468         /* call the slots */
469         uint32_t const sz = tracks.size ();
470
471         for (set<RouteTimeAxisView*>::iterator i = tracks.begin(); i != tracks.end(); ++i) {
472                 sl (**i, sz);
473         }
474 }
475
476 void
477 Editor::mapped_get_equivalent_regions (RouteTimeAxisView& tv, uint32_t, RegionView * basis, vector<RegionView*>* all_equivs) const
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 = tv.track()) == 0) {
485                 /* bus */
486                 return;
487         }
488
489         if (&tv == &basis->get_time_axis_view()) {
490                 /* looking in same track as the original */
491                 return;
492         }
493
494         if ((pl = tr->playlist()) != 0) {
495                 pl->get_equivalent_regions (basis->region(), results);
496         }
497
498         for (vector<boost::shared_ptr<Region> >::iterator ir = results.begin(); ir != results.end(); ++ir) {
499                 if ((marv = tv.view()->find_view (*ir)) != 0) {
500                         all_equivs->push_back (marv);
501                 }
502         }
503 }
504
505 void
506 Editor::get_equivalent_regions (RegionView* basis, vector<RegionView*>& equivalent_regions, PBD::PropertyID property) const
507 {
508         mapover_tracks_with_unique_playlists (sigc::bind (sigc::mem_fun (*this, &Editor::mapped_get_equivalent_regions), basis, &equivalent_regions), &basis->get_time_axis_view(), property);
509
510         /* add clicked regionview since we skipped all other regions in the same track as the one it was in */
511
512         equivalent_regions.push_back (basis);
513 }
514
515 RegionSelection
516 Editor::get_equivalent_regions (RegionSelection & basis, PBD::PropertyID prop) const
517 {
518         RegionSelection equivalent;
519
520         for (RegionSelection::const_iterator i = basis.begin(); i != basis.end(); ++i) {
521
522                 vector<RegionView*> eq;
523
524                 mapover_tracks_with_unique_playlists (
525                         sigc::bind (sigc::mem_fun (*this, &Editor::mapped_get_equivalent_regions), *i, &eq),
526                         &(*i)->get_time_axis_view(), prop);
527
528                 for (vector<RegionView*>::iterator j = eq.begin(); j != eq.end(); ++j) {
529                         equivalent.add (*j);
530                 }
531
532                 equivalent.add (*i);
533         }
534
535         return equivalent;
536 }
537
538 int
539 Editor::get_regionview_count_from_region_list (boost::shared_ptr<Region> region)
540 {
541         int region_count = 0;
542
543         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
544
545                 RouteTimeAxisView* tatv;
546
547                 if ((tatv = dynamic_cast<RouteTimeAxisView*> (*i)) != 0) {
548
549                         boost::shared_ptr<Playlist> pl;
550                         vector<boost::shared_ptr<Region> > results;
551                         RegionView* marv;
552                         boost::shared_ptr<Track> tr;
553
554                         if ((tr = tatv->track()) == 0) {
555                                 /* bus */
556                                 continue;
557                         }
558
559                         if ((pl = (tr->playlist())) != 0) {
560                                 pl->get_region_list_equivalent_regions (region, results);
561                         }
562
563                         for (vector<boost::shared_ptr<Region> >::iterator ir = results.begin(); ir != results.end(); ++ir) {
564                                 if ((marv = tatv->view()->find_view (*ir)) != 0) {
565                                         region_count++;
566                                 }
567                         }
568
569                 }
570         }
571
572         return region_count;
573 }
574
575
576 bool
577 Editor::set_selected_regionview_from_click (bool press, Selection::Operation op)
578 {
579         vector<RegionView*> all_equivalent_regions;
580         bool commit = false;
581
582         if (!clicked_regionview || !clicked_routeview) {
583                 return false;
584         }
585
586         if (press) {
587                 button_release_can_deselect = false;
588         }
589
590         if (op == Selection::Toggle || op == Selection::Set) {
591
592                 switch (op) {
593                 case Selection::Toggle:
594                         if (selection->selected (clicked_regionview)) {
595                                 if (press) {
596
597                                         /* whatever was clicked was selected already; do nothing here but allow
598                                            the button release to deselect it
599                                         */
600
601                                         button_release_can_deselect = true;
602
603                                 } else {
604                                         if (button_release_can_deselect) {
605
606                                                 /* just remove this one region, but only on a permitted button release */
607
608                                                 selection->remove (clicked_regionview);
609                                                 commit = true;
610
611                                                 /* no more deselect action on button release till a new press
612                                                    finds an already selected object.
613                                                 */
614
615                                                 button_release_can_deselect = false;
616                                         }
617                                 }
618
619                         } else {
620
621                                 if (press) {
622
623                                         if (selection->selected (clicked_routeview)) {
624                                                 get_equivalent_regions (clicked_regionview, all_equivalent_regions, ARDOUR::Properties::group_select.property_id);
625                                         } else {
626                                                 all_equivalent_regions.push_back (clicked_regionview);
627                                         }
628
629                                         /* add all the equivalent regions, but only on button press */
630
631                                         if (!all_equivalent_regions.empty()) {
632                                                 commit = true;
633                                         }
634
635                                         selection->add (all_equivalent_regions);
636                                 }
637                         }
638                         break;
639
640                 case Selection::Set:
641                         if (!selection->selected (clicked_regionview)) {
642                                 get_equivalent_regions (clicked_regionview, all_equivalent_regions, ARDOUR::Properties::group_select.property_id);
643                                 selection->set (all_equivalent_regions);
644                                 commit = true;
645                         } else {
646                                 /* clicked on an already selected region */
647                                 if (press)
648                                         goto out;
649                                 else {
650                                         if (selection->regions.size() > 1) {
651                                                 /* collapse region selection down to just this one region (and its equivalents) */
652                                                 get_equivalent_regions(clicked_regionview, all_equivalent_regions, ARDOUR::Properties::group_select.property_id);
653                                                 selection->set(all_equivalent_regions);
654                                                 commit = true;
655                                         }
656                                 }
657                         }
658                         break;
659
660                 default:
661                         /* silly compiler */
662                         break;
663                 }
664
665         } else if (op == Selection::Extend) {
666
667                 list<Selectable*> results;
668                 framepos_t last_frame;
669                 framepos_t first_frame;
670                 bool same_track = false;
671
672                 /* 1. find the last selected regionview in the track that was clicked in */
673
674                 last_frame = 0;
675                 first_frame = max_framepos;
676
677                 for (RegionSelection::iterator x = selection->regions.begin(); x != selection->regions.end(); ++x) {
678                         if (&(*x)->get_time_axis_view() == &clicked_regionview->get_time_axis_view()) {
679
680                                 if ((*x)->region()->last_frame() > last_frame) {
681                                         last_frame = (*x)->region()->last_frame();
682                                 }
683
684                                 if ((*x)->region()->first_frame() < first_frame) {
685                                         first_frame = (*x)->region()->first_frame();
686                                 }
687
688                                 same_track = true;
689                         }
690                 }
691
692                 if (same_track) {
693
694                         /* 2. figure out the boundaries for our search for new objects */
695
696                         switch (clicked_regionview->region()->coverage (first_frame, last_frame)) {
697                         case Evoral::OverlapNone:
698                                 if (last_frame < clicked_regionview->region()->first_frame()) {
699                                         first_frame = last_frame;
700                                         last_frame = clicked_regionview->region()->last_frame();
701                                 } else {
702                                         last_frame = first_frame;
703                                         first_frame = clicked_regionview->region()->first_frame();
704                                 }
705                                 break;
706
707                         case Evoral::OverlapExternal:
708                                 if (last_frame < clicked_regionview->region()->first_frame()) {
709                                         first_frame = last_frame;
710                                         last_frame = clicked_regionview->region()->last_frame();
711                                 } else {
712                                         last_frame = first_frame;
713                                         first_frame = clicked_regionview->region()->first_frame();
714                                 }
715                                 break;
716
717                         case Evoral::OverlapInternal:
718                                 if (last_frame < clicked_regionview->region()->first_frame()) {
719                                         first_frame = last_frame;
720                                         last_frame = clicked_regionview->region()->last_frame();
721                                 } else {
722                                         last_frame = first_frame;
723                                         first_frame = clicked_regionview->region()->first_frame();
724                                 }
725                                 break;
726
727                         case Evoral::OverlapStart:
728                         case Evoral::OverlapEnd:
729                                 /* nothing to do except add clicked region to selection, since it
730                                    overlaps with the existing selection in this track.
731                                 */
732                                 break;
733                         }
734
735                 } else {
736
737                         /* click in a track that has no regions selected, so extend vertically
738                            to pick out all regions that are defined by the existing selection
739                            plus this one.
740                         */
741
742
743                         first_frame = clicked_regionview->region()->position();
744                         last_frame = clicked_regionview->region()->last_frame();
745
746                         for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
747                                 if ((*i)->region()->position() < first_frame) {
748                                         first_frame = (*i)->region()->position();
749                                 }
750                                 if ((*i)->region()->last_frame() + 1 > last_frame) {
751                                         last_frame = (*i)->region()->last_frame();
752                                 }
753                         }
754                 }
755
756                 /* 2. find all the tracks we should select in */
757
758                 set<RouteTimeAxisView*> relevant_tracks;
759
760                 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
761                         RouteTimeAxisView* r = dynamic_cast<RouteTimeAxisView*> (*i);
762                         if (r) {
763                                 relevant_tracks.insert (r);
764                         }
765                 }
766
767                 set<RouteTimeAxisView*> already_in_selection;
768
769                 if (relevant_tracks.empty()) {
770
771                         /* no tracks selected .. thus .. if the
772                            regionview we're in isn't selected
773                            (i.e. we're about to extend to it), then
774                            find all tracks between the this one and
775                            any selected ones.
776                         */
777
778                         if (!selection->selected (clicked_regionview)) {
779
780                                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (&clicked_regionview->get_time_axis_view());
781
782                                 if (rtv) {
783
784                                         /* add this track to the ones we will search */
785
786                                         relevant_tracks.insert (rtv);
787
788                                         /* find the track closest to this one that
789                                            already a selected region.
790                                         */
791
792                                         RouteTimeAxisView* closest = 0;
793                                         int distance = INT_MAX;
794                                         int key = rtv->route()->presentation_info().order ();
795
796                                         for (RegionSelection::iterator x = selection->regions.begin(); x != selection->regions.end(); ++x) {
797
798                                                 RouteTimeAxisView* artv = dynamic_cast<RouteTimeAxisView*>(&(*x)->get_time_axis_view());
799
800                                                 if (artv && artv != rtv) {
801
802                                                         pair<set<RouteTimeAxisView*>::iterator,bool> result;
803
804                                                         result = already_in_selection.insert (artv);
805
806                                                         if (result.second) {
807                                                                 /* newly added to already_in_selection */
808
809                                                                 int d = artv->route()->presentation_info().order ();
810
811                                                                 d -= key;
812
813                                                                 if (abs (d) < distance) {
814                                                                         distance = abs (d);
815                                                                         closest = artv;
816                                                                 }
817                                                         }
818                                                 }
819                                         }
820
821                                         if (closest) {
822
823                                                 /* now add all tracks between that one and this one */
824
825                                                 int okey = closest->route()->presentation_info().order ();
826
827                                                 if (okey > key) {
828                                                         swap (okey, key);
829                                                 }
830
831                                                 for (TrackViewList::iterator x = track_views.begin(); x != track_views.end(); ++x) {
832                                                         RouteTimeAxisView* artv = dynamic_cast<RouteTimeAxisView*>(*x);
833                                                         if (artv && artv != rtv) {
834
835                                                                 int k = artv->route()->presentation_info().order ();
836
837                                                                 if (k >= okey && k <= key) {
838
839                                                                         /* in range but don't add it if
840                                                                            it already has tracks selected.
841                                                                            this avoids odd selection
842                                                                            behaviour that feels wrong.
843                                                                         */
844
845                                                                         if (find (already_in_selection.begin(),
846                                                                                   already_in_selection.end(),
847                                                                                   artv) == already_in_selection.end()) {
848
849                                                                                 relevant_tracks.insert (artv);
850                                                                         }
851                                                                 }
852                                                         }
853                                                 }
854                                         }
855                                 }
856                         }
857                 }
858
859                 /* 3. find all selectable objects (regionviews in this case) between that one and the end of the
860                    one that was clicked.
861                 */
862
863                 for (set<RouteTimeAxisView*>::iterator t = relevant_tracks.begin(); t != relevant_tracks.end(); ++t) {
864                         (*t)->get_selectables (first_frame, last_frame, -1.0, -1.0, results);
865                 }
866
867                 /* 4. convert to a vector of regions */
868
869                 vector<RegionView*> regions;
870
871                 for (list<Selectable*>::iterator x = results.begin(); x != results.end(); ++x) {
872                         RegionView* arv;
873
874                         if ((arv = dynamic_cast<RegionView*>(*x)) != 0) {
875                                 regions.push_back (arv);
876                         }
877                 }
878
879                 if (!regions.empty()) {
880                         selection->add (regions);
881                         commit = true;
882                 } else if (selection->regions.empty() && !selection->selected (clicked_regionview)) {
883                         /* ensure that at least the clicked regionview is selected. */
884                         selection->set (clicked_regionview);
885                         commit = true;
886                 }
887
888         }
889
890 out:
891         return commit;
892 }
893
894
895 void
896 Editor::set_selected_regionview_from_region_list (boost::shared_ptr<Region> region, Selection::Operation op)
897 {
898         vector<RegionView*> all_equivalent_regions;
899
900         get_regions_corresponding_to (region, all_equivalent_regions, region->whole_file());
901
902         if (all_equivalent_regions.empty()) {
903                 return;
904         }
905
906         begin_reversible_selection_op (X_("set selected regions"));
907
908         switch (op) {
909         case Selection::Toggle:
910                 /* XXX this is not correct */
911                 selection->toggle (all_equivalent_regions);
912                 break;
913         case Selection::Set:
914                 selection->set (all_equivalent_regions);
915                 break;
916         case Selection::Extend:
917                 selection->add (all_equivalent_regions);
918                 break;
919         case Selection::Add:
920                 selection->add (all_equivalent_regions);
921                 break;
922         }
923
924         commit_reversible_selection_op () ;
925 }
926
927 bool
928 Editor::set_selected_regionview_from_map_event (GdkEventAny* /*ev*/, StreamView* sv, boost::weak_ptr<Region> weak_r)
929 {
930         RegionView* rv;
931         boost::shared_ptr<Region> r (weak_r.lock());
932
933         if (!r) {
934                 return true;
935         }
936
937         if ((rv = sv->find_view (r)) == 0) {
938                 return true;
939         }
940
941         /* don't reset the selection if its something other than
942            a single other region.
943         */
944
945         if (selection->regions.size() > 1) {
946                 return true;
947         }
948
949         begin_reversible_selection_op (X_("set selected regions"));
950
951         selection->set (rv);
952
953         commit_reversible_selection_op () ;
954
955         return true;
956 }
957
958 void
959 Editor::track_selection_changed ()
960 {
961         switch (selection->tracks.size()) {
962         case 0:
963                 break;
964         default:
965                 /* last element in selection list is the most recently
966                  * selected, because we always append to that list.
967                  */
968                 set_selected_mixer_strip (*(selection->tracks.back()));
969                 ensure_time_axis_view_is_visible (*(selection->tracks.back()), false);
970                 break;
971         }
972
973         RouteNotificationListPtr routes (new RouteNotificationList);
974         StripableNotificationListPtr stripables (new StripableNotificationList);
975
976         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
977
978                 bool yn = (find (selection->tracks.begin(), selection->tracks.end(), *i) != selection->tracks.end());
979
980                 (*i)->set_selected (yn);
981
982                 TimeAxisView::Children c = (*i)->get_child_list ();
983                 for (TimeAxisView::Children::iterator j = c.begin(); j != c.end(); ++j) {
984                         (*j)->set_selected (find (selection->tracks.begin(), selection->tracks.end(), j->get()) != selection->tracks.end());
985                 }
986
987                 if (yn) {
988                         (*i)->reshow_selection (selection->time);
989                 } else {
990                         (*i)->hide_selection ();
991                 }
992
993
994                 if (yn) {
995                         RouteTimeAxisView* rtav = dynamic_cast<RouteTimeAxisView*> (*i);
996                         if (rtav) {
997                                 routes->push_back (rtav->route());
998                                 stripables->push_back (rtav->route());
999                         }
1000                 }
1001         }
1002
1003         ActionManager::set_sensitive (ActionManager::track_selection_sensitive_actions, !selection->tracks.empty());
1004
1005         /* notify control protocols */
1006
1007         ControlProtocol::StripableSelectionChanged (stripables);
1008
1009         if (sfbrowser && _session && !_session->deletion_in_progress()) {
1010                 uint32_t audio_track_cnt = 0;
1011                 uint32_t midi_track_cnt = 0;
1012
1013                 for (TrackSelection::iterator x = selection->tracks.begin(); x != selection->tracks.end(); ++x) {
1014                         AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(*x);
1015
1016                         if (atv) {
1017                                 if (atv->is_audio_track()) {
1018                                         audio_track_cnt++;
1019                                 }
1020
1021                         } else {
1022                                 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(*x);
1023
1024                                 if (mtv) {
1025                                         if (mtv->is_midi_track()) {
1026                                                 midi_track_cnt++;
1027                                         }
1028                                 }
1029                         }
1030                 }
1031
1032                 sfbrowser->reset (audio_track_cnt, midi_track_cnt);
1033         }
1034 }
1035
1036 void
1037 Editor::time_selection_changed ()
1038 {
1039         /* XXX this is superficially inefficient. Hide the selection in all
1040          * tracks, then show it in all selected tracks.
1041          *
1042          * However, if you investigate what this actually does, it isn't
1043          * anywhere nearly as bad as it may appear. Remember: nothing is
1044          * redrawn or even recomputed during these two loops - that only
1045          * happens when we next render ...
1046          */
1047
1048         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
1049                 (*i)->hide_selection ();
1050         }
1051
1052         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
1053                 (*i)->show_selection (selection->time);
1054         }
1055
1056         if (selection->time.empty()) {
1057                 ActionManager::set_sensitive (ActionManager::time_selection_sensitive_actions, false);
1058         } else {
1059                 ActionManager::set_sensitive (ActionManager::time_selection_sensitive_actions, true);
1060         }
1061
1062         /* propagate into backend, but only when there is no drag or we are at
1063          * the end of a drag, otherwise this is too expensive (could case a
1064          * locate per mouse motion event.
1065          */
1066
1067         if (_session && !_drags->active()) {
1068                 if (selection->time.length() != 0) {
1069                         _session->set_range_selection (selection->time.start(), selection->time.end_frame());
1070                 } else {
1071                         _session->clear_range_selection ();
1072                 }
1073         }
1074 }
1075
1076 /** Set all region actions to have a given sensitivity */
1077 void
1078 Editor::sensitize_all_region_actions (bool s)
1079 {
1080         Glib::ListHandle<Glib::RefPtr<Action> > all = _region_actions->get_actions ();
1081
1082         for (Glib::ListHandle<Glib::RefPtr<Action> >::iterator i = all.begin(); i != all.end(); ++i) {
1083                 (*i)->set_sensitive (s);
1084         }
1085
1086         _all_region_actions_sensitized = s;
1087 }
1088
1089 /** Sensitize region-based actions.
1090  *
1091  *  This method is called from whenever we leave the canvas, either by moving
1092  *  the pointer out of it, or by popping up a context menu. See
1093  *  Editor::{entered,left}_track_canvas() for details there.
1094  */
1095 void
1096 Editor::sensitize_the_right_region_actions (bool from_context_menu, bool from_canvas_crossing)
1097 {
1098         bool have_selection = false;
1099         bool have_entered = false;
1100         bool have_edit_point = false;
1101         RegionSelection rs;
1102
1103         if (!selection->regions.empty()) {
1104                 have_selection = true;
1105                 rs = selection->regions;
1106         }
1107
1108         if (entered_regionview) {
1109                 have_entered = true;
1110                 rs.add (entered_regionview);
1111         }
1112
1113         if (rs.empty() && !selection->tracks.empty()) {
1114
1115                 /* no selected regions, but some selected tracks. what we do
1116                  * here depends on the context in which we are called
1117                  */
1118
1119                 if (from_canvas_crossing) {
1120                         if (!within_track_canvas && _edit_point == EditAtMouse) {
1121                                 have_edit_point = false;
1122                         } else {
1123                                 RegionSelection at_edit_point;
1124                                 framepos_t const where = get_preferred_edit_position (Editing::EDIT_IGNORE_NONE, false, !within_track_canvas);
1125                                 get_regions_at (at_edit_point, where, selection->tracks);
1126                                 if (!at_edit_point.empty()) {
1127                                         have_edit_point = true;
1128                                 }
1129                                 if (rs.empty()) {
1130                                         rs.insert (rs.end(), at_edit_point.begin(), at_edit_point.end());
1131                                 }
1132                         }
1133                 } else if (from_context_menu) {
1134                         /* we have a context click event */
1135                 }
1136         }
1137
1138         typedef std::map<std::string,RegionAction> RegionActionMap;
1139
1140         _ignore_region_action = true;
1141
1142         for (RegionActionMap::iterator x = region_action_map.begin(); x != region_action_map.end(); ++x) {
1143                 RegionActionTarget tgt = x->second.target;
1144                 bool sensitive = false;
1145
1146                 if ((tgt & SelectedRegions) && have_selection) {
1147                         sensitive = true;
1148                 } else if ((tgt & EnteredRegions) && have_entered) {
1149                         sensitive = true;
1150                 } else if ((tgt & EditPointRegions) && have_edit_point) {
1151                         sensitive = true;
1152                 }
1153
1154                 x->second.action->set_sensitive (sensitive);
1155         }
1156
1157         /* Look through the regions that are selected and make notes about what we have got */
1158
1159         bool have_audio = false;
1160         bool have_multichannel_audio = false;
1161         bool have_midi = false;
1162         bool have_locked = false;
1163         bool have_unlocked = false;
1164         bool have_video_locked = false;
1165         bool have_video_unlocked = false;
1166         bool have_position_lock_style_audio = false;
1167         bool have_position_lock_style_music = false;
1168         bool have_muted = false;
1169         bool have_unmuted = false;
1170         bool have_opaque = false;
1171         bool have_non_opaque = false;
1172         bool have_not_at_natural_position = false;
1173         bool have_envelope_active = false;
1174         bool have_envelope_inactive = false;
1175         bool have_non_unity_scale_amplitude = false;
1176         bool have_compound_regions = false;
1177         bool have_inactive_fade_in = false;
1178         bool have_inactive_fade_out = false;
1179         bool have_active_fade_in = false;
1180         bool have_active_fade_out = false;
1181         bool have_transients = false;
1182
1183         for (list<RegionView*>::const_iterator i = rs.begin(); i != rs.end(); ++i) {
1184
1185                 boost::shared_ptr<Region> r = (*i)->region ();
1186                 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (r);
1187
1188                 if (ar) {
1189                         have_audio = true;
1190                         if (ar->n_channels() > 1) {
1191                                 have_multichannel_audio = true;
1192                         }
1193                 }
1194
1195                 if (boost::dynamic_pointer_cast<MidiRegion> (r)) {
1196                         have_midi = true;
1197                 }
1198
1199                 if (r->is_compound()) {
1200                         have_compound_regions = true;
1201                 }
1202
1203                 if (r->locked()) {
1204                         have_locked = true;
1205                 } else {
1206                         have_unlocked = true;
1207                 }
1208
1209                 if (r->video_locked()) {
1210                         have_video_locked = true;
1211                 } else {
1212                         have_video_unlocked = true;
1213                 }
1214
1215                 if (r->position_lock_style() == MusicTime) {
1216                         have_position_lock_style_music = true;
1217                 } else {
1218                         have_position_lock_style_audio = true;
1219                 }
1220
1221                 if (r->muted()) {
1222                         have_muted = true;
1223                 } else {
1224                         have_unmuted = true;
1225                 }
1226
1227                 if (r->opaque()) {
1228                         have_opaque = true;
1229                 } else {
1230                         have_non_opaque = true;
1231                 }
1232
1233                 if (!r->at_natural_position()) {
1234                         have_not_at_natural_position = true;
1235                 }
1236
1237                 if (r->has_transients ()){
1238                         have_transients = true;
1239                 }
1240
1241                 if (ar) {
1242                         if (ar->envelope_active()) {
1243                                 have_envelope_active = true;
1244                         } else {
1245                                 have_envelope_inactive = true;
1246                         }
1247
1248                         if (ar->scale_amplitude() != 1) {
1249                                 have_non_unity_scale_amplitude = true;
1250                         }
1251
1252                         if (ar->fade_in_active ()) {
1253                                 have_active_fade_in = true;
1254                         } else {
1255                                 have_inactive_fade_in = true;
1256                         }
1257
1258                         if (ar->fade_out_active ()) {
1259                                 have_active_fade_out = true;
1260                         } else {
1261                                 have_inactive_fade_out = true;
1262                         }
1263                 }
1264         }
1265
1266         _region_actions->get_action("split-region-at-transients")->set_sensitive (have_transients);
1267
1268         if (rs.size() > 1) {
1269                 _region_actions->get_action("show-region-list-editor")->set_sensitive (false);
1270                 _region_actions->get_action("show-region-properties")->set_sensitive (false);
1271                 _region_actions->get_action("rename-region")->set_sensitive (false);
1272                 if (have_audio) {
1273                         /* XXX need to check whether there is than 1 per
1274                            playlist, because otherwise this makes no sense.
1275                         */
1276                         _region_actions->get_action("combine-regions")->set_sensitive (true);
1277                 } else {
1278                         _region_actions->get_action("combine-regions")->set_sensitive (false);
1279                 }
1280         } else if (rs.size() == 1) {
1281                 _region_actions->get_action("add-range-markers-from-region")->set_sensitive (false);
1282                 _region_actions->get_action("close-region-gaps")->set_sensitive (false);
1283                 _region_actions->get_action("combine-regions")->set_sensitive (false);
1284         }
1285
1286         if (!have_multichannel_audio) {
1287                 _region_actions->get_action("split-multichannel-region")->set_sensitive (false);
1288         }
1289
1290         if (!have_midi) {
1291                 editor_menu_actions->get_action("RegionMenuMIDI")->set_sensitive (false);
1292                 _region_actions->get_action("show-region-list-editor")->set_sensitive (false);
1293                 _region_actions->get_action("quantize-region")->set_sensitive (false);
1294                 _region_actions->get_action("legatize-region")->set_sensitive (false);
1295                 _region_actions->get_action("remove-overlap")->set_sensitive (false);
1296                 _region_actions->get_action("transform-region")->set_sensitive (false);
1297                 _region_actions->get_action("fork-region")->set_sensitive (false);
1298                 _region_actions->get_action("insert-patch-change-context")->set_sensitive (false);
1299                 _region_actions->get_action("insert-patch-change")->set_sensitive (false);
1300                 _region_actions->get_action("transpose-region")->set_sensitive (false);
1301         } else {
1302                 editor_menu_actions->get_action("RegionMenuMIDI")->set_sensitive (true);
1303                 /* others were already marked sensitive */
1304         }
1305
1306         /* ok, moving along... */
1307
1308         if (have_compound_regions) {
1309                 _region_actions->get_action("uncombine-regions")->set_sensitive (true);
1310         } else {
1311                 _region_actions->get_action("uncombine-regions")->set_sensitive (false);
1312         }
1313
1314         if (have_audio) {
1315
1316                 if (have_envelope_active && !have_envelope_inactive) {
1317                         Glib::RefPtr<ToggleAction>::cast_dynamic (_region_actions->get_action("toggle-region-gain-envelope-active"))->set_active ();
1318                 } else if (have_envelope_active && have_envelope_inactive) {
1319                         // Glib::RefPtr<ToggleAction>::cast_dynamic (_region_actions->get_action("toggle-region-gain-envelope-active"))->set_inconsistent ();
1320                 }
1321
1322         } else {
1323
1324                 _region_actions->get_action("loudness-analyze-region")->set_sensitive (false);
1325                 _region_actions->get_action("spectral-analyze-region")->set_sensitive (false);
1326                 _region_actions->get_action("reset-region-gain-envelopes")->set_sensitive (false);
1327                 _region_actions->get_action("toggle-region-gain-envelope-active")->set_sensitive (false);
1328                 _region_actions->get_action("pitch-shift-region")->set_sensitive (false);
1329                 _region_actions->get_action("strip-region-silence")->set_sensitive (false);
1330                 _region_actions->get_action("show-rhythm-ferret")->set_sensitive (false);
1331
1332         }
1333
1334         if (!have_non_unity_scale_amplitude || !have_audio) {
1335                 _region_actions->get_action("reset-region-scale-amplitude")->set_sensitive (false);
1336         }
1337
1338         Glib::RefPtr<ToggleAction> a = Glib::RefPtr<ToggleAction>::cast_dynamic (_region_actions->get_action("toggle-region-lock"));
1339         a->set_active (have_locked && !have_unlocked);
1340         if (have_locked && have_unlocked) {
1341                 // a->set_inconsistent ();
1342         }
1343
1344         a = Glib::RefPtr<ToggleAction>::cast_dynamic (_region_actions->get_action("toggle-region-video-lock"));
1345         a->set_active (have_video_locked && !have_video_unlocked);
1346         if (have_video_locked && have_video_unlocked) {
1347                 // a->set_inconsistent ();
1348         }
1349
1350         a = Glib::RefPtr<ToggleAction>::cast_dynamic (_region_actions->get_action("toggle-region-lock-style"));
1351         a->set_active (have_position_lock_style_music && !have_position_lock_style_audio);
1352
1353         vector<Widget*> proxies = a->get_proxies();
1354         for (vector<Widget*>::iterator p = proxies.begin(); p != proxies.end(); ++p) {
1355                 Gtk::CheckMenuItem* cmi = dynamic_cast<Gtk::CheckMenuItem*> (*p);
1356                 if (cmi) {
1357                         cmi->set_inconsistent (have_position_lock_style_music && have_position_lock_style_audio);
1358                 }
1359         }
1360
1361         a = Glib::RefPtr<ToggleAction>::cast_dynamic (_region_actions->get_action("toggle-region-mute"));
1362         a->set_active (have_muted && !have_unmuted);
1363         if (have_muted && have_unmuted) {
1364                 // a->set_inconsistent ();
1365         }
1366
1367         a = Glib::RefPtr<ToggleAction>::cast_dynamic (_region_actions->get_action("toggle-opaque-region"));
1368         a->set_active (have_opaque && !have_non_opaque);
1369         if (have_opaque && have_non_opaque) {
1370                 // a->set_inconsistent ();
1371         }
1372
1373         if (!have_not_at_natural_position) {
1374                 _region_actions->get_action("naturalize-region")->set_sensitive (false);
1375         }
1376
1377         /* XXX: should also check that there is a track of the appropriate type for the selected region */
1378         if (_edit_point == EditAtMouse || _regions->get_single_selection() == 0 || selection->tracks.empty()) {
1379                 _region_actions->get_action("insert-region-from-region-list")->set_sensitive (false);
1380         } else {
1381                 _region_actions->get_action("insert-region-from-region-list")->set_sensitive (true);
1382         }
1383
1384         a = Glib::RefPtr<ToggleAction>::cast_dynamic (_region_actions->get_action("toggle-region-fade-in"));
1385         a->set_active (have_active_fade_in && !have_inactive_fade_in);
1386         if (have_active_fade_in && have_inactive_fade_in) {
1387                 // a->set_inconsistent ();
1388         }
1389
1390         a = Glib::RefPtr<ToggleAction>::cast_dynamic (_region_actions->get_action("toggle-region-fade-out"));
1391         a->set_active (have_active_fade_out && !have_inactive_fade_out);
1392
1393         if (have_active_fade_out && have_inactive_fade_out) {
1394                 // a->set_inconsistent ();
1395         }
1396
1397         bool const have_active_fade = have_active_fade_in || have_active_fade_out;
1398         bool const have_inactive_fade = have_inactive_fade_in || have_inactive_fade_out;
1399
1400         a = Glib::RefPtr<ToggleAction>::cast_dynamic (_region_actions->get_action("toggle-region-fades"));
1401         a->set_active (have_active_fade && !have_inactive_fade);
1402
1403         if (have_active_fade && have_inactive_fade) {
1404                 // a->set_inconsistent ();
1405         }
1406
1407         _ignore_region_action = false;
1408
1409         _all_region_actions_sensitized = false;
1410 }
1411
1412 void
1413 Editor::region_selection_changed ()
1414 {
1415         _regions->block_change_connection (true);
1416         editor_regions_selection_changed_connection.block(true);
1417
1418         if (_region_selection_change_updates_region_list) {
1419                 _regions->unselect_all ();
1420         }
1421
1422         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
1423                 (*i)->set_selected_regionviews (selection->regions);
1424         }
1425
1426         if (_region_selection_change_updates_region_list) {
1427                 _regions->set_selected (selection->regions);
1428         }
1429
1430         _regions->block_change_connection (false);
1431         editor_regions_selection_changed_connection.block(false);
1432
1433         sensitize_the_right_region_actions (false, false);
1434
1435         /* propagate into backend */
1436
1437         if (_session) {
1438                 if (!selection->regions.empty()) {
1439                         _session->set_object_selection (selection->regions.start(), selection->regions.end_frame());
1440                 } else {
1441                         _session->clear_object_selection ();
1442                 }
1443         }
1444
1445 }
1446
1447 void
1448 Editor::point_selection_changed ()
1449 {
1450         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
1451                 (*i)->set_selected_points (selection->points);
1452         }
1453 }
1454
1455 void
1456 Editor::select_all_in_track (Selection::Operation op)
1457 {
1458         list<Selectable *> touched;
1459
1460         if (!clicked_routeview) {
1461                 return;
1462         }
1463
1464         begin_reversible_selection_op (X_("Select All in Track"));
1465
1466         clicked_routeview->get_selectables (0, max_framepos, 0, DBL_MAX, touched);
1467
1468         switch (op) {
1469         case Selection::Toggle:
1470                 selection->add (touched);
1471                 break;
1472         case Selection::Set:
1473                 selection->set (touched);
1474                 break;
1475         case Selection::Extend:
1476                 /* meaningless, because we're selecting everything */
1477                 break;
1478         case Selection::Add:
1479                 selection->add (touched);
1480                 break;
1481         }
1482
1483         commit_reversible_selection_op ();
1484 }
1485
1486 bool
1487 Editor::select_all_internal_edit (Selection::Operation)
1488 {
1489         bool selected = false;
1490
1491         for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
1492                 MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
1493                 if (mrv) {
1494                         mrv->select_all_notes ();
1495                         selected = true;
1496                 }
1497         }
1498
1499         MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(entered_regionview);
1500         if (mrv) {
1501                 mrv->select_all_notes ();
1502                 selected = true;
1503         }
1504
1505         return selected;
1506 }
1507
1508 void
1509 Editor::select_all_objects (Selection::Operation op)
1510 {
1511         list<Selectable *> touched;
1512
1513         TrackViewList ts  = track_views;
1514
1515         if (internal_editing() && select_all_internal_edit(op)) {
1516                 return;  // Selected notes
1517         }
1518
1519         for (TrackViewList::iterator iter = ts.begin(); iter != ts.end(); ++iter) {
1520                 if ((*iter)->hidden()) {
1521                         continue;
1522                 }
1523                 (*iter)->get_selectables (0, max_framepos, 0, DBL_MAX, touched);
1524                 selection->add (*iter);
1525         }
1526
1527
1528         begin_reversible_selection_op (X_("select all"));
1529         switch (op) {
1530         case Selection::Add:
1531                 selection->add (touched);
1532                 break;
1533         case Selection::Toggle:
1534                 selection->add (touched);
1535                 break;
1536         case Selection::Set:
1537                 selection->set (touched);
1538                 break;
1539         case Selection::Extend:
1540                 /* meaningless, because we're selecting everything */
1541                 break;
1542         }
1543         commit_reversible_selection_op ();
1544 }
1545
1546 void
1547 Editor::invert_selection_in_track ()
1548 {
1549         list<Selectable *> touched;
1550
1551         if (!clicked_routeview) {
1552                 return;
1553         }
1554
1555         begin_reversible_selection_op (X_("Invert Selection in Track"));
1556         clicked_routeview->get_inverted_selectables (*selection, touched);
1557         selection->set (touched);
1558         commit_reversible_selection_op ();
1559 }
1560
1561 void
1562 Editor::invert_selection ()
1563 {
1564         list<Selectable *> touched;
1565
1566         if (internal_editing()) {
1567                 for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
1568                         MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
1569                         if (mrv) {
1570                                 mrv->invert_selection ();
1571                         }
1572                 }
1573                 return;
1574         }
1575
1576         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
1577                 if ((*iter)->hidden()) {
1578                         continue;
1579                 }
1580                 (*iter)->get_inverted_selectables (*selection, touched);
1581         }
1582
1583         begin_reversible_selection_op (X_("Invert Selection"));
1584         selection->set (touched);
1585         commit_reversible_selection_op ();
1586 }
1587
1588 /** @param start Start time in session frames.
1589  *  @param end End time in session frames.
1590  *  @param top Top (lower) y limit in trackview coordinates (ie 0 at the top of the track view)
1591  *  @param bottom Bottom (higher) y limit in trackview coordinates (ie 0 at the top of the track view)
1592  *  @param preserve_if_selected true to leave the current selection alone if we're adding to the selection and all of the selectables
1593  *  within the region are already selected.
1594  */
1595 void
1596 Editor::select_all_within (framepos_t start, framepos_t end, double top, double bot, const TrackViewList& tracklist, Selection::Operation op, bool preserve_if_selected)
1597 {
1598         list<Selectable*> found;
1599
1600         for (TrackViewList::const_iterator iter = tracklist.begin(); iter != tracklist.end(); ++iter) {
1601
1602                 if ((*iter)->hidden()) {
1603                         continue;
1604                 }
1605
1606                 (*iter)->get_selectables (start, end, top, bot, found);
1607         }
1608
1609         if (found.empty()) {
1610                 selection->clear_objects();
1611                 selection->clear_time ();
1612                 return;
1613         }
1614
1615         if (preserve_if_selected && op != Selection::Toggle) {
1616                 list<Selectable*>::iterator i = found.begin();
1617                 while (i != found.end() && (*i)->selected()) {
1618                         ++i;
1619                 }
1620
1621                 if (i == found.end()) {
1622                         return;
1623                 }
1624         }
1625
1626         begin_reversible_selection_op (X_("select all within"));
1627         switch (op) {
1628         case Selection::Add:
1629                 selection->add (found);
1630                 break;
1631         case Selection::Toggle:
1632                 selection->toggle (found);
1633                 break;
1634         case Selection::Set:
1635                 selection->set (found);
1636                 break;
1637         case Selection::Extend:
1638                 /* not defined yet */
1639                 break;
1640         }
1641
1642         commit_reversible_selection_op ();
1643 }
1644
1645 void
1646 Editor::set_selection_from_region ()
1647 {
1648         if (selection->regions.empty()) {
1649                 return;
1650         }
1651
1652         /* find all the tracks that have selected regions */
1653
1654         set<TimeAxisView*> tracks;
1655
1656         for (RegionSelection::const_iterator r = selection->regions.begin(); r != selection->regions.end(); ++r) {
1657                 tracks.insert (&(*r)->get_time_axis_view());
1658         }
1659
1660         TrackViewList tvl;
1661         tvl.insert (tvl.end(), tracks.begin(), tracks.end());
1662
1663         /* select range (this will clear the region selection) */
1664
1665         selection->set (selection->regions.start(), selection->regions.end_frame());
1666
1667         /* and select the tracks */
1668
1669         selection->set (tvl);
1670
1671         set_mouse_mode (Editing::MouseRange, false);
1672 }
1673
1674 void
1675 Editor::set_selection_from_punch()
1676 {
1677         Location* location;
1678
1679         if ((location = _session->locations()->auto_punch_location()) == 0)  {
1680                 return;
1681         }
1682
1683         set_selection_from_range (*location);
1684 }
1685
1686 void
1687 Editor::set_selection_from_loop()
1688 {
1689         Location* location;
1690
1691         if ((location = _session->locations()->auto_loop_location()) == 0)  {
1692                 return;
1693         }
1694         set_selection_from_range (*location);
1695 }
1696
1697 void
1698 Editor::set_selection_from_range (Location& loc)
1699 {
1700         begin_reversible_selection_op (X_("set selection from range"));
1701         selection->set (loc.start(), loc.end());
1702         commit_reversible_selection_op ();
1703
1704         set_mouse_mode (Editing::MouseRange, false);
1705 }
1706
1707 void
1708 Editor::select_all_selectables_using_time_selection ()
1709 {
1710         list<Selectable *> touched;
1711
1712         if (selection->time.empty()) {
1713                 return;
1714         }
1715
1716         framepos_t start = selection->time[clicked_selection].start;
1717         framepos_t end = selection->time[clicked_selection].end;
1718
1719         if (end - start < 1)  {
1720                 return;
1721         }
1722
1723         TrackViewList* ts;
1724
1725         if (selection->tracks.empty()) {
1726                 ts = &track_views;
1727         } else {
1728                 ts = &selection->tracks;
1729         }
1730
1731         for (TrackViewList::iterator iter = ts->begin(); iter != ts->end(); ++iter) {
1732                 if ((*iter)->hidden()) {
1733                         continue;
1734                 }
1735                 (*iter)->get_selectables (start, end - 1, 0, DBL_MAX, touched);
1736         }
1737
1738         begin_reversible_selection_op (X_("select all from range"));
1739         selection->set (touched);
1740         commit_reversible_selection_op ();
1741 }
1742
1743
1744 void
1745 Editor::select_all_selectables_using_punch()
1746 {
1747         Location* location = _session->locations()->auto_punch_location();
1748         list<Selectable *> touched;
1749
1750         if (location == 0 || (location->end() - location->start() <= 1))  {
1751                 return;
1752         }
1753
1754
1755         TrackViewList* ts;
1756
1757         if (selection->tracks.empty()) {
1758                 ts = &track_views;
1759         } else {
1760                 ts = &selection->tracks;
1761         }
1762
1763         for (TrackViewList::iterator iter = ts->begin(); iter != ts->end(); ++iter) {
1764                 if ((*iter)->hidden()) {
1765                         continue;
1766                 }
1767                 (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
1768         }
1769         begin_reversible_selection_op (X_("select all from punch"));
1770         selection->set (touched);
1771         commit_reversible_selection_op ();
1772
1773 }
1774
1775 void
1776 Editor::select_all_selectables_using_loop()
1777 {
1778         Location* location = _session->locations()->auto_loop_location();
1779         list<Selectable *> touched;
1780
1781         if (location == 0 || (location->end() - location->start() <= 1))  {
1782                 return;
1783         }
1784
1785
1786         TrackViewList* ts;
1787
1788         if (selection->tracks.empty()) {
1789                 ts = &track_views;
1790         } else {
1791                 ts = &selection->tracks;
1792         }
1793
1794         for (TrackViewList::iterator iter = ts->begin(); iter != ts->end(); ++iter) {
1795                 if ((*iter)->hidden()) {
1796                         continue;
1797                 }
1798                 (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
1799         }
1800         begin_reversible_selection_op (X_("select all from loop"));
1801         selection->set (touched);
1802         commit_reversible_selection_op ();
1803
1804 }
1805
1806 void
1807 Editor::select_all_selectables_using_cursor (EditorCursor *cursor, bool after)
1808 {
1809         framepos_t start;
1810         framepos_t end;
1811         list<Selectable *> touched;
1812
1813         if (after) {
1814                 start = cursor->current_frame();
1815                 end = _session->current_end_frame();
1816         } else {
1817                 if (cursor->current_frame() > 0) {
1818                         start = 0;
1819                         end = cursor->current_frame() - 1;
1820                 } else {
1821                         return;
1822                 }
1823         }
1824
1825         if (internal_editing()) {
1826                 for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
1827                         MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
1828                         if (mrv) {
1829                                 mrv->select_range (start, end);
1830                         }
1831                 }
1832                 return;
1833         }
1834
1835         if (after) {
1836                 begin_reversible_selection_op (X_("select all after cursor"));
1837         } else {
1838                 begin_reversible_selection_op (X_("select all before cursor"));
1839         }
1840
1841         TrackViewList* ts;
1842
1843         if (selection->tracks.empty()) {
1844                 ts = &track_views;
1845         } else {
1846                 ts = &selection->tracks;
1847         }
1848
1849         for (TrackViewList::iterator iter = ts->begin(); iter != ts->end(); ++iter) {
1850                 if ((*iter)->hidden()) {
1851                         continue;
1852                 }
1853                 (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
1854         }
1855         selection->set (touched);
1856         commit_reversible_selection_op ();
1857 }
1858
1859 void
1860 Editor::select_all_selectables_using_edit (bool after, bool from_context_menu)
1861 {
1862         framepos_t start;
1863         framepos_t end;
1864         list<Selectable *> touched;
1865
1866         if (after) {
1867                 start = get_preferred_edit_position(EDIT_IGNORE_NONE, from_context_menu);
1868                 end = _session->current_end_frame();
1869         } else {
1870                 if ((end = get_preferred_edit_position(EDIT_IGNORE_NONE, from_context_menu)) > 1) {
1871                         start = 0;
1872                         end -= 1;
1873                 } else {
1874                         return;
1875                 }
1876         }
1877
1878         if (internal_editing()) {
1879                 for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
1880                         MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
1881                         mrv->select_range (start, end);
1882                 }
1883                 return;
1884         }
1885
1886         if (after) {
1887                 begin_reversible_selection_op (X_("select all after edit"));
1888         } else {
1889                 begin_reversible_selection_op (X_("select all before edit"));
1890         }
1891
1892         TrackViewList* ts;
1893
1894         if (selection->tracks.empty()) {
1895                 ts = &track_views;
1896         } else {
1897                 ts = &selection->tracks;
1898         }
1899
1900         for (TrackViewList::iterator iter = ts->begin(); iter != ts->end(); ++iter) {
1901                 if ((*iter)->hidden()) {
1902                         continue;
1903                 }
1904                 (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
1905         }
1906         selection->set (touched);
1907         commit_reversible_selection_op ();
1908 }
1909
1910 void
1911 Editor::select_all_selectables_between (bool within)
1912 {
1913         framepos_t start;
1914         framepos_t end;
1915         list<Selectable *> touched;
1916
1917         if (!get_edit_op_range (start, end)) {
1918                 return;
1919         }
1920
1921         if (internal_editing()) {
1922                 for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
1923                         MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
1924                         mrv->select_range (start, end);
1925                 }
1926                 return;
1927         }
1928
1929         TrackViewList* ts;
1930
1931         if (selection->tracks.empty()) {
1932                 ts = &track_views;
1933         } else {
1934                 ts = &selection->tracks;
1935         }
1936
1937         for (TrackViewList::iterator iter = ts->begin(); iter != ts->end(); ++iter) {
1938                 if ((*iter)->hidden()) {
1939                         continue;
1940                 }
1941                 (*iter)->get_selectables (start, end, 0, DBL_MAX, touched, within);
1942         }
1943
1944         begin_reversible_selection_op (X_("Select all Selectables Between"));
1945         selection->set (touched);
1946         commit_reversible_selection_op ();
1947 }
1948
1949 void
1950 Editor::select_range_between ()
1951 {
1952         framepos_t start;
1953         framepos_t end;
1954
1955         if ( !selection->time.empty() ) {
1956                 selection->clear_time ();
1957         }
1958
1959         if (!get_edit_op_range (start, end)) {
1960                 return;
1961         }
1962
1963         begin_reversible_selection_op (X_("Select Range Between"));
1964         set_mouse_mode (MouseRange);
1965         selection->set (start, end);
1966         commit_reversible_selection_op ();
1967 }
1968
1969 bool
1970 Editor::get_edit_op_range (framepos_t& start, framepos_t& end) const
1971 {
1972 //      framepos_t m;
1973 //      bool ignored;
1974
1975         /* if an explicit range exists, use it */
1976
1977         if ( (mouse_mode == MouseRange || get_smart_mode() ) &&  !selection->time.empty()) {
1978                 /* we know that these are ordered */
1979                 start = selection->time.start();
1980                 end = selection->time.end_frame();
1981                 return true;
1982         } else {
1983                 start = 0;
1984                 end = 0;
1985                 return false;
1986         }
1987
1988 //      if (!mouse_frame (m, ignored)) {
1989 //              /* mouse is not in a canvas, try playhead+selected marker.
1990 //                 this is probably most true when using menus.
1991 //              */
1992 //
1993 //              if (selection->markers.empty()) {
1994 //                      return false;
1995 //              }
1996
1997 //              start = selection->markers.front()->position();
1998 //              end = _session->audible_frame();
1999
2000 //      } else {
2001
2002 //              switch (_edit_point) {
2003 //              case EditAtPlayhead:
2004 //                      if (selection->markers.empty()) {
2005 //                              /* use mouse + playhead */
2006 //                              start = m;
2007 //                              end = _session->audible_frame();
2008 //                      } else {
2009 //                              /* use playhead + selected marker */
2010 //                              start = _session->audible_frame();
2011 //                              end = selection->markers.front()->position();
2012 //                      }
2013 //                      break;
2014
2015 //              case EditAtMouse:
2016 //                      /* use mouse + selected marker */
2017 //                      if (selection->markers.empty()) {
2018 //                              start = m;
2019 //                              end = _session->audible_frame();
2020 //                      } else {
2021 //                              start = selection->markers.front()->position();
2022 //                              end = m;
2023 //                      }
2024 //                      break;
2025
2026 //              case EditAtSelectedMarker:
2027 //                      /* use mouse + selected marker */
2028 //                      if (selection->markers.empty()) {
2029
2030 //                              MessageDialog win (_("No edit range defined"),
2031 //                                                 false,
2032 //                                                 MESSAGE_INFO,
2033 //                                                 BUTTONS_OK);
2034
2035 //                              win.set_secondary_text (
2036 //                                      _("the edit point is Selected Marker\nbut there is no selected marker."));
2037
2038
2039 //                              win.set_default_response (RESPONSE_CLOSE);
2040 //                              win.set_position (Gtk::WIN_POS_MOUSE);
2041 //                              win.show_all();
2042
2043 //                              win.run ();
2044
2045 //                              return false; // NO RANGE
2046 //                      }
2047 //                      start = selection->markers.front()->position();
2048 //                      end = m;
2049 //                      break;
2050 //              }
2051 //      }
2052
2053 //      if (start == end) {
2054 //              return false;
2055 //      }
2056
2057 //      if (start > end) {
2058 //              swap (start, end);
2059 //      }
2060
2061         /* turn range into one delimited by start...end,
2062            not start...end-1
2063         */
2064
2065 //      end++;
2066
2067 //      return true;
2068 }
2069
2070 void
2071 Editor::deselect_all ()
2072 {
2073         begin_reversible_selection_op (X_("Deselect All"));
2074         selection->clear ();
2075         commit_reversible_selection_op ();
2076 }
2077
2078 long
2079 Editor::select_range (framepos_t s, framepos_t e)
2080 {
2081         begin_reversible_selection_op (X_("Select Range"));
2082         selection->add (clicked_axisview);
2083         selection->time.clear ();
2084         long ret = selection->set (s, e);
2085         commit_reversible_selection_op ();
2086         return ret;
2087 }