Remove redundant view <-> model value mapping stuff from AudioRegionGainLine.
[ardour.git] / gtk2_ardour / editor_selection.cc
index 80f8ca347c185eaa5b9ae3bf228ce31a9265c682..41b6d32a0bb0a2cbd6de8b6ea85817012e0fbd84 100644 (file)
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id: editor.cc 1353 2007-01-18 03:06:15Z paul $
 */
 
+#include <pbd/stacktrace.h>
+
 #include <ardour/diskstream.h>
 #include <ardour/playlist.h>
 #include <ardour/route_group.h>
@@ -28,6 +29,7 @@
 #include "audio_region_view.h"
 #include "audio_streamview.h"
 #include "automation_line.h"
+#include "control_point.h"
 
 #include "i18n.h"
 
@@ -152,6 +154,11 @@ Editor::extend_selection_to_track (TimeAxisView& view)
        return false;
 }
 
+void
+Editor::select_all_tracks ()
+{
+       selection->set (track_views);
+}
 
 bool
 Editor::set_selected_track (TimeAxisView& view, Selection::Operation op, bool no_remove)
@@ -211,7 +218,7 @@ Editor::set_selected_track (TimeAxisView& view, Selection::Operation op, bool no
 bool
 Editor::set_selected_track_from_click (bool press, Selection::Operation op, bool no_remove)
 {
-       if (!clicked_trackview) {
+       if (!clicked_routeview) {
                return false;
        }
        
@@ -219,7 +226,7 @@ Editor::set_selected_track_from_click (bool press, Selection::Operation op, bool
                return false;
        }
 
-       return set_selected_track (*clicked_trackview, op, no_remove);
+       return set_selected_track (*clicked_routeview, op, no_remove);
 }
 
 bool
@@ -239,23 +246,23 @@ Editor::set_selected_control_point_from_click (Selection::Operation op, bool no_
        y1 = clicked_control_point->get_x() - 10;
        y2 = clicked_control_point->get_y() + 10;
 
-       return select_all_within (x1, x2, y1, y2, op);
+       return select_all_within (x1, x2, y1, y2, selection->tracks, op);
 }
 
 void
-Editor::get_relevant_audio_tracks (set<AudioTimeAxisView*>& relevant_tracks)
+Editor::get_relevant_tracks (set<RouteTimeAxisView*>& relevant_tracks)
 {
        /* step one: get all selected tracks and all tracks in the relevant edit groups */
 
        for (TrackSelection::iterator ti = selection->tracks.begin(); ti != selection->tracks.end(); ++ti) {
 
-               AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(*ti);
+               RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(*ti);
 
-               if (!atv) {
+               if (!rtv) {
                        continue;
                }
 
-               RouteGroup* group = atv->route()->edit_group();
+               RouteGroup* group = rtv->route()->edit_group();
 
                if (group && group->is_active()) {
                        
@@ -263,32 +270,32 @@ Editor::get_relevant_audio_tracks (set<AudioTimeAxisView*>& relevant_tracks)
 
                        for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
                                
-                               AudioTimeAxisView* tatv;
+                               RouteTimeAxisView* trtv;
                                
-                               if ((tatv = dynamic_cast<AudioTimeAxisView*> (*i)) != 0) {
+                               if ((trtv = dynamic_cast<RouteTimeAxisView*> (*i)) != 0) {
                                        
-                                       if (tatv->route()->edit_group() == group) {
-                                               relevant_tracks.insert (tatv);
+                                       if (trtv->route()->edit_group() == group) {
+                                               relevant_tracks.insert (trtv);
                                        }
                                }
                        }
                } else {
-                       relevant_tracks.insert (atv);
+                       relevant_tracks.insert (rtv);
                }
        }
 }
 
 void
-Editor::mapover_audio_tracks (slot<void,AudioTimeAxisView&,uint32_t> sl)
+Editor::mapover_tracks (slot<void,RouteTimeAxisView&,uint32_t> sl)
 {
-       set<AudioTimeAxisView*> relevant_tracks;
+       set<RouteTimeAxisView*> relevant_tracks;
 
-       get_relevant_audio_tracks (relevant_tracks);
+       get_relevant_tracks (relevant_tracks);
 
        uint32_t sz = relevant_tracks.size();
 
-       for (set<AudioTimeAxisView*>::iterator ati = relevant_tracks.begin(); ati != relevant_tracks.end(); ++ati) {
-               sl (**ati, sz);
+       for (set<RouteTimeAxisView*>::iterator rti = relevant_tracks.begin(); rti != relevant_tracks.end(); ++rti) {
+               sl (**rti, sz);
        }
 }
 
@@ -313,7 +320,7 @@ Editor::mapped_get_equivalent_regions (RouteTimeAxisView& tv, uint32_t ignored,
        if ((pl = ds->playlist()) != 0) {
                pl->get_equivalent_regions (basis->region(), results);
        }
-       
+
        for (vector<boost::shared_ptr<Region> >::iterator ir = results.begin(); ir != results.end(); ++ir) {
                if ((marv = tv.view()->find_view (*ir)) != 0) {
                        all_equivs->push_back (marv);
@@ -324,7 +331,7 @@ Editor::mapped_get_equivalent_regions (RouteTimeAxisView& tv, uint32_t ignored,
 void
 Editor::get_equivalent_regions (RegionView* basis, vector<RegionView*>& equivalent_regions)
 {
-       mapover_audio_tracks (bind (mem_fun (*this, &Editor::mapped_get_equivalent_regions), basis, &equivalent_regions));
+       mapover_tracks (bind (mem_fun (*this, &Editor::mapped_get_equivalent_regions), basis, &equivalent_regions));
        
        /* add clicked regionview since we skipped all other regions in the same track as the one it was in */
        
@@ -337,18 +344,17 @@ Editor::set_selected_regionview_from_click (bool press, Selection::Operation op,
        vector<RegionView*> all_equivalent_regions;
        bool commit = false;
 
-       if (!clicked_regionview || !clicked_audio_trackview) {
+       if (!clicked_regionview || !clicked_routeview) {
                return false;
        }
 
        if (press) {
                button_release_can_deselect = false;
-       }
+       } 
 
        if (op == Selection::Toggle || op == Selection::Set) {
-               
-               get_equivalent_regions (clicked_regionview, all_equivalent_regions);
-               
+
+
                switch (op) {
                case Selection::Toggle:
                        
@@ -381,8 +387,17 @@ Editor::set_selected_regionview_from_click (bool press, Selection::Operation op,
                        } else {
 
                                if (press) {
+
+                                       if (selection->selected (clicked_routeview)) {
+                                               get_equivalent_regions (clicked_regionview, all_equivalent_regions);
+                                       } else {
+                                               all_equivalent_regions.push_back (clicked_regionview);
+                                       }
+
                                        /* add all the equivalent regions, but only on button press */
                                        
+
+
                                        if (!all_equivalent_regions.empty()) {
                                                commit = true;
                                        }
@@ -394,6 +409,13 @@ Editor::set_selected_regionview_from_click (bool press, Selection::Operation op,
                        
                case Selection::Set:
                        if (!clicked_regionview->get_selected()) {
+
+                               if (selection->selected (clicked_routeview)) {
+                                       get_equivalent_regions (clicked_regionview, all_equivalent_regions);
+                               } else {
+                                       all_equivalent_regions.push_back (clicked_regionview);
+                               }
+
                                selection->set (all_equivalent_regions);
                                commit = true;
                        } else {
@@ -476,11 +498,11 @@ Editor::set_selected_regionview_from_click (bool press, Selection::Operation op,
                      one that was clicked.
                */
 
-               set<AudioTimeAxisView*> relevant_tracks;
+               set<RouteTimeAxisView*> relevant_tracks;
                
-               get_relevant_audio_tracks (relevant_tracks);
-               
-               for (set<AudioTimeAxisView*>::iterator t = relevant_tracks.begin(); t != relevant_tracks.end(); ++t) {
+               get_relevant_tracks (relevant_tracks);
+
+               for (set<RouteTimeAxisView*>::iterator t = relevant_tracks.begin(); t != relevant_tracks.end(); ++t) {
                        (*t)->get_selectables (first_frame, last_frame, -1.0, -1.0, results);
                }
                
@@ -561,43 +583,6 @@ Editor::set_selected_regionview_from_region_list (boost::shared_ptr<Region> regi
        commit_reversible_command () ;
 }
 
-bool
-Editor::set_selected_regionview_from_map_event (GdkEventAny* ev, StreamView* sv, boost::weak_ptr<Region> weak_r)
-{
-       RegionView* rv;
-       boost::shared_ptr<Region> r (weak_r.lock());
-
-       if (!r) {
-               return true;
-       }
-
-       boost::shared_ptr<AudioRegion> ar;
-
-       if ((ar = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
-               return true;
-       }
-
-       if ((rv = sv->find_view (ar)) == 0) {
-               return true;
-       }
-
-       /* don't reset the selection if its something other than 
-          a single other region.
-       */
-
-       if (selection->regions.size() > 1) {
-               return true;
-       }
-       
-       begin_reversible_command (_("set selected regions"));
-       
-       selection->set (rv);
-
-       commit_reversible_command () ;
-
-       return true;
-}
-
 void
 Editor::track_selection_changed ()
 {
@@ -664,3 +649,338 @@ Editor::point_selection_changed ()
        }
 }
 
+/** Select everything in the selected tracks
+ * @param Selection operation to apply.
+ */
+void
+Editor::select_all_in_selected_tracks (Selection::Operation op)
+{
+       list<Selectable *> touched;
+
+       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
+               (*i)->get_selectables (0, max_frames, 0, DBL_MAX, touched);
+       }
+
+       switch (op) {
+       case Selection::Toggle:
+               selection->add (touched);
+               break;
+       case Selection::Set:
+               selection->set (touched);
+               break;
+       case Selection::Extend:
+               /* meaningless, because we're selecting everything */
+               break;
+       case Selection::Add:
+               selection->add (touched);
+               break;
+       }
+}
+
+void
+Editor::select_all (Selection::Operation op)
+{
+       list<Selectable *> touched;
+       
+       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
+               if ((*iter)->hidden()) {
+                       continue;
+               }
+               (*iter)->get_selectables (0, max_frames, 0, DBL_MAX, touched);
+       }
+       begin_reversible_command (_("select all"));
+       switch (op) {
+       case Selection::Add:
+               selection->add (touched);
+               break;
+       case Selection::Toggle:
+               selection->add (touched);
+               break;
+       case Selection::Set:
+               selection->set (touched);
+               break;
+       case Selection::Extend:
+               /* meaningless, because we're selecting everything */
+               break;
+       }
+       commit_reversible_command ();
+}
+
+/** Invert the selection in the selected tracks */
+void
+Editor::invert_selection_in_selected_tracks ()
+{
+       list<Selectable *> touched;
+
+       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
+               (*i)->get_inverted_selectables (*selection, touched);
+       }
+       
+       selection->set (touched);
+}
+
+void
+Editor::invert_selection ()
+{
+       list<Selectable *> touched;
+       
+       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
+               if ((*iter)->hidden()) {
+                       continue;
+               }
+               (*iter)->get_inverted_selectables (*selection, touched);
+       }
+
+       selection->set (touched);
+}
+
+bool
+Editor::select_all_within (nframes_t start, nframes_t end, double top, double bot, const TrackViewList& tracklist, Selection::Operation op)
+{
+       list<Selectable*> touched;
+       list<Selectable*>::size_type n = 0;
+       TrackViewList touched_tracks;
+
+       for (TrackViewList::const_iterator iter = tracklist.begin(); iter != tracklist.end(); ++iter) {
+               if ((*iter)->hidden()) {
+                       continue;
+               }
+
+               n = touched.size();
+
+               (*iter)->get_selectables (start, end, top, bot, touched);
+               
+               if (n != touched.size()) {
+                       touched_tracks.push_back (*iter);
+               }
+       }
+
+       if (touched.empty()) {
+               return false;
+       }
+
+       if (!touched_tracks.empty()) {
+
+               switch (op) {
+               case Selection::Add:
+                       selection->add (touched_tracks);
+                       break;
+               case Selection::Toggle:
+                       selection->toggle (touched_tracks);
+                       break;
+               case Selection::Set:
+                       selection->set (touched_tracks);
+                       break;
+               case Selection::Extend:
+                       /* not defined yet */
+                       break;
+               }
+       }
+
+       begin_reversible_command (_("select all within"));
+       switch (op) {
+       case Selection::Add:
+               selection->add (touched);
+               break;
+       case Selection::Toggle:
+               selection->toggle (touched);
+               break;
+       case Selection::Set:
+               selection->set (touched);
+               break;
+       case Selection::Extend:
+               /* not defined yet */
+               break;
+       }
+       
+       commit_reversible_command ();
+
+       return !touched.empty();
+}
+
+void
+Editor::set_selection_from_audio_region ()
+{
+       if (selection->regions.empty()) {
+               return;
+       }
+
+       RegionView* rv = *(selection->regions.begin());
+       boost::shared_ptr<Region> region = rv->region();
+       
+       begin_reversible_command (_("set selection from region"));
+       selection->set (0, region->position(), region->last_frame());
+       commit_reversible_command ();
+
+       set_mouse_mode (Editing::MouseRange, false);
+}
+
+void
+Editor::set_selection_from_punch()
+{
+       Location* location;
+
+       if ((location = session->locations()->auto_punch_location()) == 0)  {
+               return;
+       }
+
+       set_selection_from_range (*location);
+}
+
+void
+Editor::set_selection_from_loop()
+{
+       Location* location;
+
+       if ((location = session->locations()->auto_loop_location()) == 0)  {
+               return;
+       }
+       set_selection_from_range (*location);
+}
+
+void
+Editor::set_selection_from_range (Location& loc)
+{
+       begin_reversible_command (_("set selection from range"));
+       selection->set (0, loc.start(), loc.end());
+       commit_reversible_command ();
+
+       set_mouse_mode (Editing::MouseRange, false);
+}
+
+void
+Editor::select_all_selectables_using_time_selection ()
+{
+       list<Selectable *> touched;
+
+       if (selection->time.empty()) {
+               return;
+       }
+
+       nframes_t start = selection->time[clicked_selection].start;
+       nframes_t end = selection->time[clicked_selection].end;
+
+       if (end - start < 1)  {
+               return;
+       }
+
+       for (TrackViewList::iterator iter = selection->tracks.begin(); iter != selection->tracks.end(); ++iter) {
+               if ((*iter)->hidden()) {
+                       continue;
+               }
+               (*iter)->get_selectables (start, end - 1, 0, DBL_MAX, touched);
+       }
+
+       begin_reversible_command (_("select all from range"));
+       selection->set (touched);
+       commit_reversible_command ();
+}
+
+
+void
+Editor::select_all_selectables_using_punch()
+{
+       Location* location = session->locations()->auto_punch_location();
+       list<Selectable *> touched;
+
+       if (location == 0 || (location->end() - location->start() <= 1))  {
+               return;
+       }
+
+       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
+               if ((*iter)->hidden()) {
+                       continue;
+               }
+               (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
+       }
+       begin_reversible_command (_("select all from punch"));
+       selection->set (touched);
+       commit_reversible_command ();
+
+}
+
+void
+Editor::select_all_selectables_using_loop()
+{
+       Location* location = session->locations()->auto_loop_location();
+       list<Selectable *> touched;
+
+       if (location == 0 || (location->end() - location->start() <= 1))  {
+               return;
+       }
+
+       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
+               if ((*iter)->hidden()) {
+                       continue;
+               }
+               (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
+       }
+       begin_reversible_command (_("select all from loop"));
+       selection->set (touched);
+       commit_reversible_command ();
+
+}
+
+void
+Editor::select_all_selectables_using_cursor (Cursor *cursor, bool after)
+{
+        nframes_t start;
+       nframes_t end;
+       list<Selectable *> touched;
+
+       if (after) {
+               begin_reversible_command (_("select all after cursor"));
+               start = cursor->current_frame ;
+               end = session->current_end_frame();
+       } else {
+               if (cursor->current_frame > 0) {
+                       begin_reversible_command (_("select all before cursor"));
+                       start = 0;
+                       end = cursor->current_frame - 1;
+               } else {
+                       return;
+               }
+       }
+
+       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
+               if ((*iter)->hidden()) {
+                       continue;
+               }
+               (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
+       }
+       selection->set (touched);
+       commit_reversible_command ();
+}
+
+void
+Editor::select_all_selectables_between_cursors (Cursor *cursor, Cursor *other_cursor)
+{
+        nframes_t start;
+       nframes_t end;
+       list<Selectable *> touched;
+       bool  other_cursor_is_first = cursor->current_frame > other_cursor->current_frame;
+
+       if (cursor->current_frame == other_cursor->current_frame) {
+               return;
+       }
+
+       begin_reversible_command (_("select all between cursors"));
+       if (other_cursor_is_first) {
+               start = other_cursor->current_frame;
+               end = cursor->current_frame - 1;
+               
+       } else {
+               start = cursor->current_frame;
+               end = other_cursor->current_frame - 1;
+       }
+       
+       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
+               if ((*iter)->hidden()) {
+                       continue;
+               }
+               (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
+       }
+       selection->set (touched);
+       commit_reversible_command ();
+}
+