80f8ca347c185eaa5b9ae3bf228ce31a9265c682
[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     $Id: editor.cc 1353 2007-01-18 03:06:15Z paul $
19 */
20
21 #include <ardour/diskstream.h>
22 #include <ardour/playlist.h>
23 #include <ardour/route_group.h>
24
25 #include "editor.h"
26 #include "actions.h"
27 #include "audio_time_axis.h"
28 #include "audio_region_view.h"
29 #include "audio_streamview.h"
30 #include "automation_line.h"
31
32 #include "i18n.h"
33
34 using namespace std;
35 using namespace sigc;
36 using namespace ARDOUR;
37 using namespace PBD;
38 using namespace Gtk;
39 using namespace Glib;
40 using namespace Gtkmm2ext;
41 using namespace Editing;
42
43 struct TrackViewByPositionSorter
44 {
45     bool operator() (const TimeAxisView* a, const TimeAxisView *b) {
46             return a->y_position < b->y_position;
47     }
48 };
49
50 bool
51 Editor::extend_selection_to_track (TimeAxisView& view)
52 {
53         if (selection->selected (&view)) {
54                 /* already selected, do nothing */
55                 return false;
56         }
57
58         if (selection->tracks.empty()) {
59
60                 if (!selection->selected (&view)) {
61                         selection->set (&view);
62                         return true;
63                 } else {
64                         return false;
65                 }
66         } 
67
68         /* something is already selected, so figure out which range of things to add */
69         
70         TrackViewList to_be_added;
71         TrackViewList sorted = track_views;
72         TrackViewByPositionSorter cmp;
73         bool passed_clicked = false;
74         bool forwards = true;
75
76         sorted.sort (cmp);
77
78         if (!selection->selected (&view)) {
79                 to_be_added.push_back (&view);
80         }
81
82         /* figure out if we should go forward or backwards */
83
84         for (TrackViewList::iterator i = sorted.begin(); i != sorted.end(); ++i) {
85
86                 if ((*i) == &view) {
87                         passed_clicked = true;
88                 }
89
90                 if (selection->selected (*i)) {
91                         if (passed_clicked) {
92                                 forwards = true;
93                         } else {
94                                 forwards = false;
95                         }
96                         break;
97                 }
98         }
99                         
100         passed_clicked = false;
101
102         if (forwards) {
103
104                 for (TrackViewList::iterator i = sorted.begin(); i != sorted.end(); ++i) {
105                                         
106                         if ((*i) == &view) {
107                                 passed_clicked = true;
108                                 continue;
109                         }
110                                         
111                         if (passed_clicked) {
112                                 if ((*i)->hidden()) {
113                                         continue;
114                                 }
115                                 if (selection->selected (*i)) {
116                                         break;
117                                 } else if (!(*i)->hidden()) {
118                                         to_be_added.push_back (*i);
119                                 }
120                         }
121                 }
122
123         } else {
124
125                 for (TrackViewList::reverse_iterator r = sorted.rbegin(); r != sorted.rend(); ++r) {
126                                         
127                         if ((*r) == &view) {
128                                 passed_clicked = true;
129                                 continue;
130                         }
131                                         
132                         if (passed_clicked) {
133                                                 
134                                 if ((*r)->hidden()) {
135                                         continue;
136                                 }
137                                                 
138                                 if (selection->selected (*r)) {
139                                         break;
140                                 } else if (!(*r)->hidden()) {
141                                         to_be_added.push_back (*r);
142                                 }
143                         }
144                 }
145         }
146                         
147         if (!to_be_added.empty()) {
148                 selection->add (to_be_added);
149                 return true;
150         }
151         
152         return false;
153 }
154
155
156 bool
157 Editor::set_selected_track (TimeAxisView& view, Selection::Operation op, bool no_remove)
158 {
159         bool commit = false;
160
161         switch (op) {
162         case Selection::Toggle:
163                 if (selection->selected (&view)) {
164                         if (!no_remove) {
165                                 selection->remove (&view);
166                                 commit = true;
167                         }
168                 } else {
169                         selection->add (&view);
170                         commit = false;
171                 }
172                 break;
173
174         case Selection::Add:
175                 if (!selection->selected (&view)) {
176                         selection->add (&view);
177                         commit = true;
178                 }
179                 break;
180
181         case Selection::Set:
182                 if (selection->selected (&view) && selection->tracks.size() == 1) {
183                         /* no commit necessary */
184                 } else {
185                         
186                         /* reset track selection if there is only 1 other track
187                            selected OR if no_remove is not set (its there to 
188                            prevent deselecting a multi-track selection
189                            when clicking on an already selected track
190                            for some reason.
191                         */
192
193                         if (selection->tracks.empty()) {
194                                 selection->set (&view);
195                                 commit = true;
196                         } else if (selection->tracks.size() == 1 || !no_remove) {
197                                 selection->set (&view);
198                                 commit = true;
199                         }
200                 }
201                 break;
202                 
203         case Selection::Extend:
204                 commit = extend_selection_to_track (view);
205                 break;
206         }
207
208         return commit;
209 }
210
211 bool
212 Editor::set_selected_track_from_click (bool press, Selection::Operation op, bool no_remove)
213 {
214         if (!clicked_trackview) {
215                 return false;
216         }
217         
218         if (!press) {
219                 return false;
220         }
221
222         return set_selected_track (*clicked_trackview, op, no_remove);
223 }
224
225 bool
226 Editor::set_selected_control_point_from_click (Selection::Operation op, bool no_remove)
227 {
228         if (!clicked_control_point) {
229                 return false;
230         }
231
232         /* select this point and any others that it represents */
233
234         double y1, y2;
235         nframes_t x1, x2;
236
237         x1 = pixel_to_frame (clicked_control_point->get_x() - 10);
238         x2 = pixel_to_frame (clicked_control_point->get_x() + 10);
239         y1 = clicked_control_point->get_x() - 10;
240         y2 = clicked_control_point->get_y() + 10;
241
242         return select_all_within (x1, x2, y1, y2, op);
243 }
244
245 void
246 Editor::get_relevant_audio_tracks (set<AudioTimeAxisView*>& relevant_tracks)
247 {
248         /* step one: get all selected tracks and all tracks in the relevant edit groups */
249
250         for (TrackSelection::iterator ti = selection->tracks.begin(); ti != selection->tracks.end(); ++ti) {
251
252                 AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(*ti);
253
254                 if (!atv) {
255                         continue;
256                 }
257
258                 RouteGroup* group = atv->route()->edit_group();
259
260                 if (group && group->is_active()) {
261                         
262                         /* active group for this track, loop over all tracks and get every member of the group */
263
264                         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
265                                 
266                                 AudioTimeAxisView* tatv;
267                                 
268                                 if ((tatv = dynamic_cast<AudioTimeAxisView*> (*i)) != 0) {
269                                         
270                                         if (tatv->route()->edit_group() == group) {
271                                                 relevant_tracks.insert (tatv);
272                                         }
273                                 }
274                         }
275                 } else {
276                         relevant_tracks.insert (atv);
277                 }
278         }
279 }
280
281 void
282 Editor::mapover_audio_tracks (slot<void,AudioTimeAxisView&,uint32_t> sl)
283 {
284         set<AudioTimeAxisView*> relevant_tracks;
285
286         get_relevant_audio_tracks (relevant_tracks);
287
288         uint32_t sz = relevant_tracks.size();
289
290         for (set<AudioTimeAxisView*>::iterator ati = relevant_tracks.begin(); ati != relevant_tracks.end(); ++ati) {
291                 sl (**ati, sz);
292         }
293 }
294
295 void
296 Editor::mapped_get_equivalent_regions (RouteTimeAxisView& tv, uint32_t ignored, RegionView* basis, vector<RegionView*>* all_equivs)
297 {
298         boost::shared_ptr<Playlist> pl;
299         vector<boost::shared_ptr<Region> > results;
300         RegionView* marv;
301         boost::shared_ptr<Diskstream> ds;
302
303         if ((ds = tv.get_diskstream()) == 0) {
304                 /* bus */
305                 return;
306         }
307
308         if (&tv == &basis->get_time_axis_view()) {
309                 /* looking in same track as the original */
310                 return;
311         }
312
313         if ((pl = ds->playlist()) != 0) {
314                 pl->get_equivalent_regions (basis->region(), results);
315         }
316         
317         for (vector<boost::shared_ptr<Region> >::iterator ir = results.begin(); ir != results.end(); ++ir) {
318                 if ((marv = tv.view()->find_view (*ir)) != 0) {
319                         all_equivs->push_back (marv);
320                 }
321         }
322 }
323
324 void
325 Editor::get_equivalent_regions (RegionView* basis, vector<RegionView*>& equivalent_regions)
326 {
327         mapover_audio_tracks (bind (mem_fun (*this, &Editor::mapped_get_equivalent_regions), basis, &equivalent_regions));
328         
329         /* add clicked regionview since we skipped all other regions in the same track as the one it was in */
330         
331         equivalent_regions.push_back (basis);
332 }
333
334 bool
335 Editor::set_selected_regionview_from_click (bool press, Selection::Operation op, bool no_track_remove)
336 {
337         vector<RegionView*> all_equivalent_regions;
338         bool commit = false;
339
340         if (!clicked_regionview || !clicked_audio_trackview) {
341                 return false;
342         }
343
344         if (press) {
345                 button_release_can_deselect = false;
346         }
347
348         if (op == Selection::Toggle || op == Selection::Set) {
349                 
350                 get_equivalent_regions (clicked_regionview, all_equivalent_regions);
351                 
352                 switch (op) {
353                 case Selection::Toggle:
354                         
355                         if (clicked_regionview->get_selected()) {
356                                 if (press) {
357
358                                         /* whatever was clicked was selected already; do nothing here but allow
359                                            the button release to deselect it
360                                         */
361
362                                         button_release_can_deselect = true;
363
364                                 } else {
365
366                                         if (button_release_can_deselect) {
367
368                                                 /* just remove this one region, but only on a permitted button release */
369
370                                                 selection->remove (clicked_regionview);
371                                                 commit = true;
372
373                                                 /* no more deselect action on button release till a new press
374                                                    finds an already selected object.
375                                                 */
376
377                                                 button_release_can_deselect = false;
378                                         }
379                                 } 
380
381                         } else {
382
383                                 if (press) {
384                                         /* add all the equivalent regions, but only on button press */
385                                         
386                                         if (!all_equivalent_regions.empty()) {
387                                                 commit = true;
388                                         }
389
390                                         selection->add (all_equivalent_regions);
391                                 } 
392                         }
393                         break;
394                         
395                 case Selection::Set:
396                         if (!clicked_regionview->get_selected()) {
397                                 selection->set (all_equivalent_regions);
398                                 commit = true;
399                         } else {
400                                 /* no commit necessary: clicked on an already selected region */
401                                 goto out;
402                         }
403                         break;
404
405                 default:
406                         /* silly compiler */
407                         break;
408                 }
409
410         } else if (op == Selection::Extend) {
411
412                 list<Selectable*> results;
413                 nframes_t last_frame;
414                 nframes_t first_frame;
415
416                 /* 1. find the last selected regionview in the track that was clicked in */
417
418                 last_frame = 0;
419                 first_frame = max_frames;
420
421                 for (RegionSelection::iterator x = selection->regions.begin(); x != selection->regions.end(); ++x) {
422                         if (&(*x)->get_time_axis_view() == &clicked_regionview->get_time_axis_view()) {
423
424                                 if ((*x)->region()->last_frame() > last_frame) {
425                                         last_frame = (*x)->region()->last_frame();
426                                 }
427
428                                 if ((*x)->region()->first_frame() < first_frame) {
429                                         first_frame = (*x)->region()->first_frame();
430                                 }
431                         }
432                 }
433
434                 /* 2. figure out the boundaries for our search for new objects */
435
436                 switch (clicked_regionview->region()->coverage (first_frame, last_frame)) {
437                 case OverlapNone:
438                         if (last_frame < clicked_regionview->region()->first_frame()) {
439                                 first_frame = last_frame;
440                                 last_frame = clicked_regionview->region()->last_frame();
441                         } else {
442                                 last_frame = first_frame;
443                                 first_frame = clicked_regionview->region()->first_frame();
444                         }
445                         break;
446
447                 case OverlapExternal:
448                         if (last_frame < clicked_regionview->region()->first_frame()) {
449                                 first_frame = last_frame;
450                                 last_frame = clicked_regionview->region()->last_frame();
451                         } else {
452                                 last_frame = first_frame;
453                                 first_frame = clicked_regionview->region()->first_frame();
454                         }
455                         break;
456
457                 case OverlapInternal:
458                         if (last_frame < clicked_regionview->region()->first_frame()) {
459                                 first_frame = last_frame;
460                                 last_frame = clicked_regionview->region()->last_frame();
461                         } else {
462                                 last_frame = first_frame;
463                                 first_frame = clicked_regionview->region()->first_frame();
464                         }
465                         break;
466
467                 case OverlapStart:
468                 case OverlapEnd:
469                         /* nothing to do except add clicked region to selection, since it
470                            overlaps with the existing selection in this track.
471                         */
472                         break;
473                 }
474
475                 /* 2. find all selectable objects (regionviews in this case) between that one and the end of the
476                       one that was clicked.
477                 */
478
479                 set<AudioTimeAxisView*> relevant_tracks;
480                 
481                 get_relevant_audio_tracks (relevant_tracks);
482                 
483                 for (set<AudioTimeAxisView*>::iterator t = relevant_tracks.begin(); t != relevant_tracks.end(); ++t) {
484                         (*t)->get_selectables (first_frame, last_frame, -1.0, -1.0, results);
485                 }
486                 
487                 /* 3. convert to a vector of audio regions */
488
489                 vector<RegionView*> regions;
490                 
491                 for (list<Selectable*>::iterator x = results.begin(); x != results.end(); ++x) {
492                         RegionView* arv;
493
494                         if ((arv = dynamic_cast<RegionView*>(*x)) != 0) {
495                                 regions.push_back (arv);
496                         }
497                 }
498
499                 if (!regions.empty()) {
500                         selection->add (regions);
501                         commit = true;
502                 }
503         }
504
505   out:
506         return commit;
507 }
508
509 void
510 Editor::set_selected_regionview_from_region_list (boost::shared_ptr<Region> region, Selection::Operation op)
511 {
512         vector<RegionView*> all_equivalent_regions;
513
514         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
515                 
516                 RouteTimeAxisView* tatv;
517                 
518                 if ((tatv = dynamic_cast<RouteTimeAxisView*> (*i)) != 0) {
519                         
520                         boost::shared_ptr<Playlist> pl;
521                         vector<boost::shared_ptr<Region> > results;
522                         RegionView* marv;
523                         boost::shared_ptr<Diskstream> ds;
524                         
525                         if ((ds = tatv->get_diskstream()) == 0) {
526                                 /* bus */
527                                 continue;
528                         }
529                         
530                         if ((pl = (ds->playlist())) != 0) {
531                                 pl->get_region_list_equivalent_regions (region, results);
532                         }
533                         
534                         for (vector<boost::shared_ptr<Region> >::iterator ir = results.begin(); ir != results.end(); ++ir) {
535                                 if ((marv = tatv->view()->find_view (*ir)) != 0) {
536                                         all_equivalent_regions.push_back (marv);
537                                 }
538                         }
539                         
540                 }
541         }
542         
543         begin_reversible_command (_("set selected regions"));
544         
545         switch (op) {
546         case Selection::Toggle:
547                 /* XXX this is not correct */
548                 selection->toggle (all_equivalent_regions);
549                 break;
550         case Selection::Set:
551                 selection->set (all_equivalent_regions);
552                 break;
553         case Selection::Extend:
554                 selection->add (all_equivalent_regions);
555                 break;
556         case Selection::Add:
557                 selection->add (all_equivalent_regions);
558                 break;
559         }
560
561         commit_reversible_command () ;
562 }
563
564 bool
565 Editor::set_selected_regionview_from_map_event (GdkEventAny* ev, StreamView* sv, boost::weak_ptr<Region> weak_r)
566 {
567         RegionView* rv;
568         boost::shared_ptr<Region> r (weak_r.lock());
569
570         if (!r) {
571                 return true;
572         }
573
574         boost::shared_ptr<AudioRegion> ar;
575
576         if ((ar = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
577                 return true;
578         }
579
580         if ((rv = sv->find_view (ar)) == 0) {
581                 return true;
582         }
583
584         /* don't reset the selection if its something other than 
585            a single other region.
586         */
587
588         if (selection->regions.size() > 1) {
589                 return true;
590         }
591         
592         begin_reversible_command (_("set selected regions"));
593         
594         selection->set (rv);
595
596         commit_reversible_command () ;
597
598         return true;
599 }
600
601 void
602 Editor::track_selection_changed ()
603 {
604         switch (selection->tracks.size()){
605         case 0:
606                 break;
607         default:
608                 set_selected_mixer_strip (*(selection->tracks.front()));
609                 break;
610         }
611
612         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
613                 (*i)->set_selected (false);
614                 if (mouse_mode == MouseRange) {
615                         (*i)->hide_selection ();
616                 }
617         }
618
619         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
620                 (*i)->set_selected (true);
621                 if (mouse_mode == MouseRange) {
622                         (*i)->show_selection (selection->time);
623                 }
624         }
625 }
626
627 void
628 Editor::time_selection_changed ()
629 {
630         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
631                 (*i)->hide_selection ();
632         }
633
634         if (selection->tracks.empty()) {
635                 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
636                         (*i)->show_selection (selection->time);
637                 }
638         } else {
639                 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
640                         (*i)->show_selection (selection->time);
641                 }
642         }
643
644         if (selection->time.empty()) {
645                 ActionManager::set_sensitive (ActionManager::time_selection_sensitive_actions, false);
646         } else {
647                 ActionManager::set_sensitive (ActionManager::time_selection_sensitive_actions, true);
648         }
649 }
650
651 void
652 Editor::region_selection_changed ()
653 {
654         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
655                 (*i)->set_selected_regionviews (selection->regions);
656         }
657 }
658
659 void
660 Editor::point_selection_changed ()
661 {
662         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
663                 (*i)->set_selected_points (selection->points);
664         }
665 }
666