Small cleanups to port matrix context menu.
[ardour.git] / gtk2_ardour / selection.cc
index 92546e5ef45186a4cc243b00f5f230d8fbac1404..c16c913658dd6984739da8d67b9f5a99dfa241dd 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
@@ -23,7 +23,9 @@
 #include "pbd/stacktrace.h"
 
 #include "ardour/playlist.h"
+#include "ardour/rc_configuration.h"
 
+#include "midi_cut_buffer.h"
 #include "region_view.h"
 #include "selection.h"
 #include "selection_templates.h"
@@ -52,6 +54,8 @@ Selection::operator= (const Selection& other)
                tracks = other.tracks;
                time = other.time;
                lines = other.lines;
+               midi_regions = other.midi_regions;
+               midi_notes = other.midi_notes;
        }
        return *this;
 }
@@ -62,10 +66,12 @@ 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.group == b.time.group &&
                a.time == b.time &&
                a.lines == b.lines &&
-               a.playlists == b.playlists;
+               a.playlists == b.playlists &&
+               a.midi_notes == b.midi_notes &&
+               a.midi_regions == b.midi_regions;
 }
 
 /** Clear everything from the Selection */
@@ -78,6 +84,8 @@ Selection::clear ()
        clear_lines();
        clear_time ();
        clear_playlists ();
+       clear_midi_notes ();
+       clear_midi_regions ();
 }
 
 void
@@ -108,6 +116,27 @@ Selection::clear_tracks ()
        }
 }
 
+void
+Selection::clear_midi_notes ()
+{
+       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 ()
 {
@@ -172,7 +201,7 @@ void
 Selection::toggle (const list<TimeAxisView*>& track_list)
 {
        for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
-               toggle ( (*i) );
+               toggle ((*i));
        }
 }
 
@@ -180,7 +209,7 @@ 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));
@@ -192,6 +221,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)
 {
@@ -206,6 +260,20 @@ Selection::toggle (RegionView* r)
        RegionsChanged ();
 }
 
+void
+Selection::toggle (MidiRegionView* mrv)
+{
+       MidiRegionSelection::iterator i;
+
+       if ((i = find (midi_regions.begin(), midi_regions.end(), mrv)) == midi_regions.end()) {
+               add (mrv);
+       } else {
+               midi_regions.erase (i);
+       }
+
+       MidiRegionsChanged ();
+}
+
 void
 Selection::toggle (vector<RegionView*>& r)
 {
@@ -232,7 +300,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;
@@ -260,7 +328,7 @@ Selection::add (const list<boost::shared_ptr<Playlist> >& pllist)
                        changed = true;
                }
        }
-       
+
        if (changed) {
                PlaylistsChanged ();
        }
@@ -269,18 +337,14 @@ Selection::add (const list<boost::shared_ptr<Playlist> >& pllist)
 void
 Selection::add (const list<TimeAxisView*>& track_list)
 {
-       bool changed = false;
+       list<TimeAxisView*> added = tracks.add (track_list);
 
-       for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
-               if (find (tracks.begin(), tracks.end(), (*i)) == tracks.end()) {
-                       void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
-                       (*i)->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), (*i)));
-                       tracks.push_back (*i);
-                       changed = true;
-               }
+       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 (changed) {
+
+       if (!added.empty()) {
                TracksChanged ();
        }
 }
@@ -296,6 +360,29 @@ Selection::add (TimeAxisView* track)
        }
 }
 
+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 ();
+       }
+}
+
 void
 Selection::add (vector<RegionView*>& v)
 {
@@ -303,7 +390,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));
@@ -325,7 +412,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));
@@ -334,9 +421,8 @@ Selection::add (const RegionSelection& rs)
                        }
                }
        }
-       
+
        if (changed) {
-               select_edit_group_regions ();
                RegionsChanged ();
        }
 }
@@ -353,6 +439,21 @@ Selection::add (RegionView* r)
        }
 }
 
+void
+Selection::add (MidiRegionView* 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
+               MidiRegionsChanged ();
+       }
+}
+
 long
 Selection::add (nframes_t start, nframes_t end)
 {
@@ -363,7 +464,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;
@@ -435,6 +536,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)
 {
@@ -477,6 +611,24 @@ Selection::remove (RegionView* r)
        }
 }
 
+void
+Selection::remove (MidiRegionView* mrv)
+{
+       MidiRegionSelection::iterator x;
+
+       if ((x = find (midi_regions.begin(), midi_regions.end(), mrv)) != midi_regions.end()) {
+               midi_regions.erase (x);
+               MidiRegionsChanged ();
+       }
+
+#if 0
+       /* XXX fix this up ? */
+       if (Config->get_link_region_and_track_selection() && !regions.involves (r->get_trackview())) {
+               remove (&r->get_trackview());
+       }
+#endif
+}
+
 
 void
 Selection::remove (uint32_t selection_id)
@@ -488,7 +640,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;
                }
@@ -496,7 +648,7 @@ Selection::remove (uint32_t selection_id)
 }
 
 void
-Selection::remove (nframes_t start, nframes_t end)
+Selection::remove (nframes_t /*start*/, nframes_t /*end*/)
 {
 }
 
@@ -524,6 +676,13 @@ Selection::set (const list<TimeAxisView*>& track_list)
        add (track_list);
 }
 
+void
+Selection::set (const MidiNoteSelection& midi_list)
+{
+       clear_midi_notes ();
+       add (midi_list);
+}
+
 void
 Selection::set (boost::shared_ptr<Playlist> playlist)
 {
@@ -546,6 +705,13 @@ Selection::set (const RegionSelection& rs)
        RegionsChanged(); /* EMIT SIGNAL */
 }
 
+void
+Selection::set (MidiRegionView* mrv)
+{
+       clear_midi_regions ();
+       add (mrv);
+}
+
 void
 Selection::set (RegionView* r, bool also_clear_tracks)
 {
@@ -589,7 +755,7 @@ Selection::set (TimeAxisView* track, nframes_t start, nframes_t end)
 
        if (track) {
                time.track = track;
-               time.group = track->edit_group();
+               time.group = track->route_group();
        } else {
                time.track = 0;
                time.group = 0;
@@ -628,17 +794,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
@@ -680,11 +857,11 @@ Selection::toggle (list<Selectable*>& selectables)
 
        if (!rvs.empty()) {
                toggle (rvs);
-       } 
+       }
 
        if (!autos.empty()) {
                toggle (autos);
-       } 
+       }
 }
 
 void
@@ -719,11 +896,11 @@ Selection::add (list<Selectable*>& selectables)
 
        if (!rvs.empty()) {
                add (rvs);
-       } 
+       }
 
        if (!autos.empty()) {
                add (autos);
-       } 
+       }
 }
 
 void
@@ -745,24 +922,6 @@ Selection::add (vector<AutomationSelectable*>& autos)
        PointsChanged ();
 }
 
-void
-Selection::select_edit_group_regions ()
-{
-       std::set<RegionView*> regions_to_add;
-       
-       for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
-               vector<RegionView*> e;
-               editor->get_equivalent_regions (*i, e);
-               for (vector<RegionView*>::iterator j = e.begin(); j != e.end(); ++j) {
-                       regions_to_add.insert(*j);
-               }
-       }
-
-       for (std::set<RegionView*>::iterator i = regions_to_add.begin(); i != regions_to_add.end(); ++i) {
-               add (*i);
-       }
-}
-
 void
 Selection::set (Marker* m)
 {
@@ -774,7 +933,7 @@ void
 Selection::toggle (Marker* m)
 {
        MarkerSelection::iterator i;
-       
+
        if ((i = find (markers.begin(), markers.end(), m)) == markers.end()) {
                add (m);
        } else {
@@ -799,7 +958,7 @@ 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));
@@ -826,7 +985,7 @@ MarkerSelection::range (nframes64_t& s, nframes64_t& e)
 
                if ((*i)->position() < s) {
                        s = (*i)->position();
-               } 
+               }
 
                if ((*i)->position() > e) {
                        e = (*i)->position();