Relent a bit and make adding a route to a group which already contains that group...
[ardour.git] / gtk2_ardour / selection.cc
index be7552b0bd6109d58318f3f813e897ac3414c504..a7658ed2b9ab358e08743789bd3ebdeb83305742 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2002 Paul Davis 
+    Copyright (C) 2002 Paul Davis
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -25,6 +25,8 @@
 #include "ardour/playlist.h"
 #include "ardour/rc_configuration.h"
 
+#include "gui_thread.h"
+#include "midi_cut_buffer.h"
 #include "region_view.h"
 #include "selection.h"
 #include "selection_templates.h"
@@ -37,7 +39,6 @@
 using namespace std;
 using namespace ARDOUR;
 using namespace PBD;
-using namespace sigc;
 
 struct AudioRangeComparator {
     bool operator()(AudioRange a, AudioRange b) {
@@ -45,6 +46,23 @@ struct AudioRangeComparator {
     }
 };
 
+Selection::Selection (const PublicEditor* e)
+       : tracks (e)
+       , editor (e)
+       , next_time_id (0) 
+{
+       clear ();
+
+       /* we have disambiguate which remove() for the compiler */
+
+       void (Selection::*track_remove)(TimeAxisView*) = &Selection::remove;
+       TimeAxisView::CatchDeletion.connect (*this, ui_bind (track_remove, this, _1), gui_context());
+
+       void (Selection::*marker_remove)(Marker*) = &Selection::remove;
+       Marker::CatchDeletion.connect (*this, ui_bind (marker_remove, this, _1), gui_context());
+}      
+
+#if 0
 Selection&
 Selection::operator= (const Selection& other)
 {
@@ -53,22 +71,23 @@ Selection::operator= (const Selection& other)
                tracks = other.tracks;
                time = other.time;
                lines = other.lines;
-               midi = other.midi;
+               midi_regions = other.midi_regions;
+               midi_notes = other.midi_notes;
        }
        return *this;
 }
+#endif
 
 bool
 operator== (const Selection& a, const Selection& b)
 {
        return a.regions == b.regions &&
                a.tracks == b.tracks &&
-               a.time.track == b.time.track &&
-               a.time.group == b.time.group && 
                a.time == b.time &&
                a.lines == b.lines &&
                a.playlists == b.playlists &&
-               a.midi == b.midi;
+               a.midi_notes == b.midi_notes &&
+               a.midi_regions == b.midi_regions;
 }
 
 /** Clear everything from the Selection */
@@ -81,7 +100,8 @@ Selection::clear ()
        clear_lines();
        clear_time ();
        clear_playlists ();
-       clear_midi ();
+       clear_midi_notes ();
+       clear_midi_regions ();
 }
 
 void
@@ -113,19 +133,29 @@ Selection::clear_tracks ()
 }
 
 void
-Selection::clear_midi ()
+Selection::clear_midi_notes ()
 {
-       if (!midi.empty()) {
-               midi.clear ();
-               MidiChanged ();
+       if (!midi_notes.empty()) {
+               for (MidiNoteSelection::iterator x = midi_notes.begin(); x != midi_notes.end(); ++x) {
+                       delete *x;
+               }
+               midi_notes.clear ();
+               MidiNotesChanged ();
+       }
+}
+
+void
+Selection::clear_midi_regions ()
+{
+       if (!midi_regions.empty()) {
+               midi_regions.clear ();
+               MidiRegionsChanged ();
        }
 }
 
 void
 Selection::clear_time ()
 {
-       time.track = 0;
-       time.group = 0;
        time.clear();
 
        TimeChanged ();
@@ -182,10 +212,10 @@ Selection::toggle (boost::shared_ptr<Playlist> pl)
 }
 
 void
-Selection::toggle (const list<TimeAxisView*>& track_list)
+Selection::toggle (const TrackViewList& track_list)
 {
-       for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
-               toggle ( (*i) );
+       for (TrackViewList::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
+               toggle ((*i));
        }
 }
 
@@ -193,10 +223,8 @@ void
 Selection::toggle (TimeAxisView* track)
 {
        TrackSelection::iterator i;
-       
+
        if ((i = find (tracks.begin(), tracks.end(), track)) == tracks.end()) {
-               void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
-               track->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), track));
                tracks.push_back (track);
        } else {
                tracks.erase (i);
@@ -205,6 +233,31 @@ Selection::toggle (TimeAxisView* track)
        TracksChanged();
 }
 
+void
+Selection::toggle (const MidiNoteSelection& midi_note_list)
+{
+       for (MidiNoteSelection::const_iterator i = midi_note_list.begin(); i != midi_note_list.end(); ++i) {
+               toggle ((*i));
+       }
+}
+
+void
+Selection::toggle (MidiCutBuffer* midi)
+{
+       MidiNoteSelection::iterator i;
+
+       if ((i = find (midi_notes.begin(), midi_notes.end(), midi)) == midi_notes.end()) {
+               midi_notes.push_back (midi);
+       } else {
+               /* remember that we own the MCB */
+               delete *i;
+               midi_notes.erase (i);
+       }
+
+       MidiNotesChanged();
+}
+
+
 void
 Selection::toggle (RegionView* r)
 {
@@ -222,15 +275,15 @@ Selection::toggle (RegionView* r)
 void
 Selection::toggle (MidiRegionView* mrv)
 {
-       MidiSelection::iterator i;
+       MidiRegionSelection::iterator i;
 
-       if ((i = find (midi.begin(), midi.end(), mrv)) == midi.end()) {
+       if ((i = find (midi_regions.begin(), midi_regions.end(), mrv)) == midi_regions.end()) {
                add (mrv);
        } else {
-               midi.erase (i);
+               midi_regions.erase (i);
        }
 
-       MidiChanged ();
+       MidiRegionsChanged ();
 }
 
 void
@@ -259,7 +312,7 @@ Selection::toggle (nframes_t start, nframes_t end)
        time.push_back (AudioRange (start, end, next_time_id++));
        time.consolidate ();
        time.sort (cmp);
-       
+
        TimeChanged ();
 
        return next_time_id - 1;
@@ -287,22 +340,17 @@ Selection::add (const list<boost::shared_ptr<Playlist> >& pllist)
                        changed = true;
                }
        }
-       
+
        if (changed) {
                PlaylistsChanged ();
        }
 }
 
 void
-Selection::add (const list<TimeAxisView*>& track_list)
+Selection::add (const TrackViewList& track_list)
 {
-       list<TimeAxisView*> added = tracks.add (track_list);
+       TrackViewList added = tracks.add (track_list);
 
-       for (list<TimeAxisView*>::const_iterator i = added.begin(); i != added.end(); ++i) {
-               void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
-               (*i)->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), (*i)));
-       }
-       
        if (!added.empty()) {
                TracksChanged ();
        }
@@ -311,11 +359,31 @@ Selection::add (const list<TimeAxisView*>& track_list)
 void
 Selection::add (TimeAxisView* track)
 {
-       if (find (tracks.begin(), tracks.end(), track) == tracks.end()) {
-               void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
-               track->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), track));
-               tracks.push_back (track);
-               TracksChanged();
+       TrackViewList tr;
+       tr.push_back (track);
+       add (tr);
+}
+
+void
+Selection::add (const MidiNoteSelection& midi_list)
+{
+       const MidiNoteSelection::const_iterator b = midi_list.begin();
+       const MidiNoteSelection::const_iterator e = midi_list.end();
+
+       if (!midi_list.empty()) {
+               midi_notes.insert (midi_notes.end(), b, e);
+               MidiNotesChanged ();
+       }
+}
+
+void
+Selection::add (MidiCutBuffer* midi)
+{
+       /* we take ownership of the MCB */
+
+       if (find (midi_notes.begin(), midi_notes.end(), midi) == midi_notes.end()) {
+               midi_notes.push_back (midi);
+               MidiNotesChanged ();
        }
 }
 
@@ -326,7 +394,7 @@ Selection::add (vector<RegionView*>& v)
         */
 
        bool changed = false;
-       
+
        for (vector<RegionView*>::iterator i = v.begin(); i != v.end(); ++i) {
                if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
                        changed = regions.add ((*i));
@@ -348,7 +416,7 @@ Selection::add (const RegionSelection& rs)
         */
 
        bool changed = false;
-       
+
        for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
                if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
                        changed = regions.add ((*i));
@@ -357,7 +425,7 @@ Selection::add (const RegionSelection& rs)
                        }
                }
        }
-       
+
        if (changed) {
                RegionsChanged ();
        }
@@ -378,15 +446,15 @@ Selection::add (RegionView* r)
 void
 Selection::add (MidiRegionView* mrv)
 {
-       if (find (midi.begin(), midi.end(), mrv) == midi.end()) {
-               midi.push_back (mrv);
+       if (find (midi_regions.begin(), midi_regions.end(), mrv) == midi_regions.end()) {
+               midi_regions.push_back (mrv);
                /* XXX should we do this? */
 #if 0
                if (Config->get_link_region_and_track_selection()) {
                        add (&mrv->get_trackview());
                }
 #endif
-               MidiChanged ();
+               MidiRegionsChanged ();
        }
 }
 
@@ -400,7 +468,7 @@ Selection::add (nframes_t start, nframes_t end)
        time.push_back (AudioRange (start, end, next_time_id++));
        time.consolidate ();
        time.sort (cmp);
-       
+
        TimeChanged ();
 
        return next_time_id - 1;
@@ -453,15 +521,14 @@ Selection::remove (TimeAxisView* track)
 }
 
 void
-Selection::remove (const list<TimeAxisView*>& track_list)
+Selection::remove (const TrackViewList& track_list)
 {
        bool changed = false;
 
-       for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
-
-               list<TimeAxisView*>::iterator x;
+       for (TrackViewList::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
 
-               if ((x = find (tracks.begin(), tracks.end(), (*i))) != tracks.end()) {
+               TrackViewList::iterator x = find (tracks.begin(), tracks.end(), *i);
+               if (x != tracks.end()) {
                        tracks.erase (x);
                        changed = true;
                }
@@ -472,6 +539,39 @@ Selection::remove (const list<TimeAxisView*>& track_list)
        }
 }
 
+void
+Selection::remove (const MidiNoteSelection& midi_list)
+{
+       bool changed = false;
+
+       for (MidiNoteSelection::const_iterator i = midi_list.begin(); i != midi_list.end(); ++i) {
+
+               MidiNoteSelection::iterator x;
+
+               if ((x = find (midi_notes.begin(), midi_notes.end(), (*i))) != midi_notes.end()) {
+                       midi_notes.erase (x);
+                       changed = true;
+               }
+       }
+
+       if (changed) {
+               MidiNotesChanged();
+       }
+}
+
+void
+Selection::remove (MidiCutBuffer* midi)
+{
+       MidiNoteSelection::iterator x;
+
+       if ((x = find (midi_notes.begin(), midi_notes.end(), midi)) != midi_notes.end()) {
+               /* remember that we own the MCB */
+               delete *x;
+               midi_notes.erase (x);
+               MidiNotesChanged ();
+       }
+}
+
 void
 Selection::remove (boost::shared_ptr<Playlist> track)
 {
@@ -517,11 +617,11 @@ Selection::remove (RegionView* r)
 void
 Selection::remove (MidiRegionView* mrv)
 {
-       MidiSelection::iterator x;
+       MidiRegionSelection::iterator x;
 
-       if ((x = find (midi.begin(), midi.end(), mrv)) != midi.end()) {
-               midi.erase (x);
-               MidiChanged ();
+       if ((x = find (midi_regions.begin(), midi_regions.end(), mrv)) != midi_regions.end()) {
+               midi_regions.erase (x);
+               MidiRegionsChanged ();
        }
 
 #if 0
@@ -543,7 +643,7 @@ Selection::remove (uint32_t selection_id)
        for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
                if ((*i).id == selection_id) {
                        time.erase (i);
-                                               
+
                        TimeChanged ();
                        break;
                }
@@ -573,12 +673,19 @@ Selection::set (TimeAxisView* track)
 }
 
 void
-Selection::set (const list<TimeAxisView*>& track_list)
+Selection::set (const TrackViewList& track_list)
 {
        clear_tracks ();
        add (track_list);
 }
 
+void
+Selection::set (const MidiNoteSelection& midi_list)
+{
+       clear_midi_notes ();
+       add (midi_list);
+}
+
 void
 Selection::set (boost::shared_ptr<Playlist> playlist)
 {
@@ -602,9 +709,9 @@ Selection::set (const RegionSelection& rs)
 }
 
 void
-Selection::set (MidiRegionView* mrv) 
+Selection::set (MidiRegionView* mrv)
 {
-       clear_midi ();
+       clear_midi_regions ();
        add (mrv);
 }
 
@@ -630,8 +737,11 @@ Selection::set (vector<RegionView*>& v)
        add (v);
 }
 
+/** Set the start and end time of the time selection, without changing
+ *  the list of tracks it applies to.
+ */
 long
-Selection::set (TimeAxisView* track, nframes_t start, nframes_t end)
+Selection::set (nframes_t start, nframes_t end)
 {
        if ((start == 0 && end == 0) || end < start) {
                return 0;
@@ -649,14 +759,6 @@ Selection::set (TimeAxisView* track, nframes_t start, nframes_t end)
                time.front().end = end;
        }
 
-       if (track) {
-               time.track = track;
-               time.group = track->route_group();
-       } else {
-               time.track = 0;
-               time.group = 0;
-       }
-
        time.consolidate ();
 
        TimeChanged ();
@@ -690,17 +792,28 @@ Selection::selected (RegionView* rv)
 }
 
 bool
-Selection::empty ()
+Selection::empty (bool internal_selection)
 {
-       return regions.empty () &&
+       bool object_level_empty =  regions.empty () &&
                tracks.empty () &&
-               points.empty () && 
-               playlists.empty () && 
+               points.empty () &&
+               playlists.empty () &&
                lines.empty () &&
                time.empty () &&
                playlists.empty () &&
-               markers.empty()
+               markers.empty() &&
+               midi_regions.empty()
                ;
+
+       if (!internal_selection) {
+               return object_level_empty;
+       }
+
+       /* this is intended to really only apply when using a Selection
+          as a cut buffer.
+       */
+
+       return object_level_empty && midi_notes.empty();
 }
 
 void
@@ -720,14 +833,14 @@ Selection::toggle (const vector<AutomationSelectable*>& autos)
 }
 
 void
-Selection::toggle (list<Selectable*>& selectables)
+Selection::toggle (list<Selectable*> const & selectables)
 {
        RegionView* rv;
        AutomationSelectable* as;
        vector<RegionView*> rvs;
        vector<AutomationSelectable*> autos;
 
-       for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
+       for (std::list<Selectable*>::const_iterator i = selectables.begin(); i != selectables.end(); ++i) {
                if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
                        rvs.push_back (rv);
                } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
@@ -742,15 +855,15 @@ Selection::toggle (list<Selectable*>& selectables)
 
        if (!rvs.empty()) {
                toggle (rvs);
-       } 
+       }
 
        if (!autos.empty()) {
                toggle (autos);
-       } 
+       }
 }
 
 void
-Selection::set (list<Selectable*>& selectables)
+Selection::set (list<Selectable*> const & selectables)
 {
        clear_regions();
        clear_points ();
@@ -759,14 +872,14 @@ Selection::set (list<Selectable*>& selectables)
 
 
 void
-Selection::add (list<Selectable*>& selectables)
+Selection::add (list<Selectable*> const & selectables)
 {
        RegionView* rv;
        AutomationSelectable* as;
        vector<RegionView*> rvs;
        vector<AutomationSelectable*> autos;
 
-       for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
+       for (std::list<Selectable*>::const_iterator i = selectables.begin(); i != selectables.end(); ++i) {
                if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
                        rvs.push_back (rv);
                } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
@@ -781,11 +894,11 @@ Selection::add (list<Selectable*>& selectables)
 
        if (!rvs.empty()) {
                add (rvs);
-       } 
+       }
 
        if (!autos.empty()) {
                add (autos);
-       } 
+       }
 }
 
 void
@@ -818,7 +931,7 @@ void
 Selection::toggle (Marker* m)
 {
        MarkerSelection::iterator i;
-       
+
        if ((i = find (markers.begin(), markers.end(), m)) == markers.end()) {
                add (m);
        } else {
@@ -841,13 +954,6 @@ void
 Selection::add (Marker* m)
 {
        if (find (markers.begin(), markers.end(), m) == markers.end()) {
-
-               /* disambiguate which remove() for the compiler */
-               
-               void (Selection::*pmf)(Marker*) = &Selection::remove;
-
-               m->GoingAway.connect (bind (mem_fun (*this, pmf), m));
-
                markers.push_back (m);
                MarkersChanged();
        }
@@ -870,7 +976,7 @@ MarkerSelection::range (nframes64_t& s, nframes64_t& e)
 
                if ((*i)->position() < s) {
                        s = (*i)->position();
-               } 
+               }
 
                if ((*i)->position() > e) {
                        e = (*i)->position();