luaBridge support const references to class-instance pointers
[ardour.git] / gtk2_ardour / selection.cc
index 2e5b8b84e0bfbbb7143eaa2d7e71678e624db1fd..6ed7a104347695974dc08247700752a00c60790a 100644 (file)
@@ -26,6 +26,8 @@
 #include "ardour/playlist.h"
 #include "ardour/rc_configuration.h"
 
+#include "control_protocol/control_protocol.h"
+
 #include "audio_region_view.h"
 #include "debug.h"
 #include "gui_thread.h"
@@ -38,8 +40,9 @@
 #include "automation_time_axis.h"
 #include "public_editor.h"
 #include "control_point.h"
+#include "vca_time_axis.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace std;
 using namespace ARDOUR;
@@ -55,7 +58,6 @@ Selection::Selection (const PublicEditor* e)
        : tracks (e)
        , editor (e)
        , next_time_id (0)
-       , _no_tracks_changed (false)
 {
        clear ();
 
@@ -130,13 +132,13 @@ void
 Selection::clear_tracks (bool with_signal)
 {
        if (!tracks.empty()) {
+               PresentationInfo::ChangeSuspender cs;
+
                for (TrackViewList::iterator x = tracks.begin(); x != tracks.end(); ++x) {
                        (*x)->set_selected (false);
                }
+
                tracks.clear ();
-               if (!_no_tracks_changed && with_signal) {
-                       TracksChanged();
-               }
        }
 }
 
@@ -270,7 +272,12 @@ Selection::toggle (boost::shared_ptr<Playlist> pl)
 void
 Selection::toggle (const TrackViewList& track_list)
 {
+       PresentationInfo::ChangeSuspender cs;
+
        for (TrackViewList::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
+               if (dynamic_cast<VCATimeAxisView*> (*i)) {
+                       continue;
+               }
                toggle ((*i));
        }
 }
@@ -278,19 +285,20 @@ Selection::toggle (const TrackViewList& track_list)
 void
 Selection::toggle (TimeAxisView* track)
 {
+       if (dynamic_cast<VCATimeAxisView*> (track)) {
+               return;
+       }
+
        TrackSelection::iterator i;
 
        if ((i = find (tracks.begin(), tracks.end(), track)) == tracks.end()) {
-               track->set_selected (true);
                tracks.push_back (track);
+               track->set_selected (true);
        } else {
-               track->set_selected (false);
                tracks.erase (i);
+               track->set_selected (false);
        }
 
-       if (!_no_tracks_changed) {
-               TracksChanged();
-       }
 }
 
 void
@@ -427,25 +435,30 @@ Selection::add (const list<boost::shared_ptr<Playlist> >& pllist)
 }
 
 void
-Selection::add (const TrackViewList& track_list)
+Selection::add (TrackViewList const & track_list)
 {
        clear_objects();  //enforce object/range exclusivity
 
+       PresentationInfo::ChangeSuspender cs;
+
        TrackViewList added = tracks.add (track_list);
 
        if (!added.empty()) {
                for (TrackViewList::iterator x = added.begin(); x != added.end(); ++x) {
+                       if (dynamic_cast<VCATimeAxisView*> (*x)) {
+                               continue;
+                       }
                        (*x)->set_selected (true);
                }
-               if (!_no_tracks_changed) {
-                       TracksChanged ();
-               }
        }
 }
 
 void
 Selection::add (TimeAxisView* track)
 {
+       if (dynamic_cast<VCATimeAxisView*> (track)) {
+               return;
+       }
        clear_objects();  //enforce object/range exclusivity
 
        TrackViewList tr;
@@ -635,32 +648,29 @@ Selection::remove (TimeAxisView* track)
 {
        list<TimeAxisView*>::iterator i;
        if ((i = find (tracks.begin(), tracks.end(), track)) != tracks.end()) {
-               track->set_selected (false);
+               /* erase first, because set_selected() will remove the track
+                  from the selection, invalidating the iterator.
+
+                  In fact, we don't really even need to do the erase, but this is
+                  a hangover of axis view selection being in the GUI.
+               */
                tracks.erase (i);
-               if (!_no_tracks_changed) {
-                       TracksChanged();
-               }
+               track->set_selected (false);
        }
 }
 
 void
 Selection::remove (const TrackViewList& track_list)
 {
-       bool changed = false;
+       PresentationInfo::ChangeSuspender cs;
 
        for (TrackViewList::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
 
                TrackViewList::iterator x = find (tracks.begin(), tracks.end(), *i);
+
                if (x != tracks.end()) {
-                       (*i)->set_selected (false);
                        tracks.erase (x);
-                       changed = true;
-               }
-       }
-
-       if (changed) {
-               if (!_no_tracks_changed) {
-                       TracksChanged();
+                       (*i)->set_selected (false);
                }
        }
 }
@@ -793,8 +803,27 @@ Selection::remove (boost::shared_ptr<ARDOUR::AutomationList> ac)
 void
 Selection::set (TimeAxisView* track)
 {
+       if (dynamic_cast<VCATimeAxisView*> (track)) {
+               return;
+       }
        clear_objects ();  //enforce object/range exclusivity
-       clear_tracks (false);
+
+       PresentationInfo::ChangeSuspender cs;
+
+       if (!tracks.empty()) {
+
+               if (tracks.size() == 1 && tracks.front() == track) {
+                       /* already single selection: nothing to do */
+                       return;
+               }
+
+               for (TrackViewList::iterator x = tracks.begin(); x != tracks.end(); ++x) {
+                       (*x)->set_selected (false);
+               }
+
+               tracks.clear ();
+       }
+
        add (track);
 }
 
@@ -802,7 +831,46 @@ void
 Selection::set (const TrackViewList& track_list)
 {
        clear_objects();  //enforce object/range exclusivity
-       clear_tracks (false);
+
+       PresentationInfo::ChangeSuspender cs;
+
+       if (!tracks.empty()) {
+
+               /* cannot use set<T>::operator== (set<T> const &) here, because
+                * apparently the ordering used within 2 sets is not
+                * necessarily the same.
+                */
+
+               if (tracks.size() == track_list.size()) {
+                       bool missing = false;
+
+                       for (TrackViewList::const_iterator x = track_list.begin(); x != track_list.end(); ++x) {
+                               if (dynamic_cast<VCATimeAxisView*> (*x)) {
+                                       continue;
+                               }
+                               if (find (tracks.begin(), tracks.end(), *x) == tracks.end()) {
+                                       missing = true;
+                               }
+                       }
+
+                       if (!missing) {
+                               /* already same selection: nothing to do */
+                               return;
+                       }
+               }
+
+               /* argument is different from existing selection */
+
+               for (TrackViewList::iterator x = tracks.begin(); x != tracks.end(); ++x) {
+                       if (dynamic_cast<VCATimeAxisView*> (*x)) {
+                               continue;
+                       }
+                       (*x)->set_selected (false);
+               }
+
+               tracks.clear ();
+       }
+
        add (track_list);
 }
 
@@ -951,7 +1019,7 @@ Selection::selected (ArdourMarker* m)
 bool
 Selection::selected (TimeAxisView* tv)
 {
-       return tv->get_selected ();
+       return tv->selected ();
 }
 
 bool
@@ -997,7 +1065,7 @@ Selection::toggle (ControlPoint* cp)
        clear_time();  //enforce region/object exclusivity
        clear_tracks();  //enforce object/track exclusivity
 
-       cp->set_selected (!cp->get_selected ());
+       cp->set_selected (!cp->selected ());
        PointSelection::iterator i = find (points.begin(), points.end(), cp);
        if (i == points.end()) {
                points.push_back (cp);
@@ -1147,7 +1215,7 @@ Selection::set (ControlPoint* cp)
        clear_time ();  //enforce region/object exclusivity
        clear_tracks();  //enforce object/track exclusivity
 
-       if (cp->get_selected () && points.size () == 1) {
+       if (cp->selected () && points.size () == 1) {
                return;
        }
 
@@ -1267,36 +1335,19 @@ Selection::get_state () const
        }
 
        /* midi region views have thir own internal selection. */
-       XMLNode* n = NULL;
        list<pair<PBD::ID, std::set<boost::shared_ptr<Evoral::Note<Evoral::Beats> > > > > rid_notes;
        editor->get_per_region_note_selection (rid_notes);
-       if (!rid_notes.empty()) {
-               n = node->add_child (X_("MIDINote"));
-       }
+
        list<pair<PBD::ID, std::set<boost::shared_ptr<Evoral::Note<Evoral::Beats> > > > >::iterator rn_it;
        for (rn_it = rid_notes.begin(); rn_it != rid_notes.end(); ++rn_it) {
-               assert(n); // hint for clang static analysis
-               n->add_property (X_("region_id"), atoi((*rn_it).first.to_s().c_str()));
+               XMLNode* n = node->add_child (X_("MIDINotes"));
+               n->add_property (X_("region-id"), atoi((*rn_it).first.to_s().c_str()));
 
                for (std::set<boost::shared_ptr<Evoral::Note<Evoral::Beats> > >::iterator i = (*rn_it).second.begin(); i != (*rn_it).second.end(); ++i) {
                        XMLNode* nc = n->add_child(X_("note"));
-                       snprintf(buf, sizeof(buf), "%d", (*i)->channel());
-                       nc->add_property(X_("channel"), string(buf));
-
-                       snprintf(buf, sizeof(buf), "%f", (*i)->time().to_double());
-                       nc->add_property(X_("time"), string(buf));
-
-                       snprintf(buf, sizeof(buf), "%d", (*i)->note());
-                       nc->add_property(X_("note"), string(buf));
-
-                       snprintf(buf, sizeof(buf), "%f", (*i)->length().to_double());
-                       nc->add_property(X_("length"), string(buf));
-
-                       snprintf(buf, sizeof(buf), "%d", (*i)->velocity());
-                       nc->add_property(X_("velocity"), string(buf));
 
-                       snprintf(buf, sizeof(buf), "%d", (*i)->off_velocity());
-                       nc->add_property(X_("off-velocity"), string(buf));
+                       snprintf(buf, sizeof(buf), "%d", (*i)->id());
+                       nc->add_property (X_("note-id"), string(buf));
                }
        }
 
@@ -1391,7 +1442,7 @@ Selection::set_state (XMLNode const & node, int)
                                regions.pending.push_back (id);
                        }
 
-               } else if ((*i)->name() == X_("MIDINote")) {
+               } else if ((*i)->name() == X_("MIDINotes")) {
                        XMLProperty const * prop_region_id = (*i)->property (X_("region-id"));
 
                        assert (prop_region_id);
@@ -1401,35 +1452,15 @@ Selection::set_state (XMLNode const & node, int)
 
                        editor->get_regionviews_by_id (id, rs); // there could be more than one
 
-                       std::list<boost::shared_ptr<Evoral::Note<Evoral::Beats> > > notes;
+                       std::list<Evoral::event_id_t> notes;
                        XMLNodeList children = (*i)->children ();
 
                        for (XMLNodeList::const_iterator ci = children.begin(); ci != children.end(); ++ci) {
-                               XMLProperty const * prop_channel = (*ci)->property (X_("channel"));
-                               XMLProperty const * prop_time = (*ci)->property (X_("time"));
-                               XMLProperty const * prop_note = (*ci)->property (X_("note"));
-                               XMLProperty const * prop_length = (*ci)->property (X_("length"));
-                               XMLProperty const * prop_velocity = (*ci)->property (X_("velocity"));
-                               XMLProperty const * prop_off_velocity = (*ci)->property (X_("off-velocity"));
-
-                               assert (prop_channel);
-                               assert (prop_time);
-                               assert (prop_note);
-                               assert (prop_length);
-                               assert (prop_velocity);
-                               assert (prop_off_velocity);
-
-                               uint8_t channel = atoi(prop_channel->value());
-                               Evoral::Beats time (atof(prop_time->value()));
-                               Evoral::Beats length (atof(prop_length->value()));
-                               uint8_t note = atoi(prop_note->value());
-                               uint8_t velocity = atoi(prop_velocity->value());
-                               uint8_t off_velocity = atoi(prop_off_velocity->value());
-                               boost::shared_ptr<Evoral::Note<Evoral::Beats> > the_note
-                                       (new Evoral::Note<Evoral::Beats>  (channel, time, length, note, velocity));
-                               the_note->set_off_velocity (off_velocity);
-
-                               notes.push_back (the_note);
+                               XMLProperty const * prop_id = (*ci)->property (X_("note-id"));
+                               if (prop_id) {
+                                       Evoral::event_id_t id = atoi(prop_id->value());
+                                       notes.push_back (id);
+                               }
                        }
 
                        for (RegionSelection::iterator rsi = rs.begin(); rsi != rs.end(); ++rsi) {
@@ -1585,9 +1616,3 @@ Selection::remove_regions (TimeAxisView* t)
                i = tmp;
        }
 }
-
-void
-Selection::block_tracks_changed (bool yn)
-{
-       _no_tracks_changed = yn;
-}