mp4chaps Lua script: don't clutter global environment
[ardour.git] / gtk2_ardour / selection.cc
index 7639d47dbc58b4b278d8b1009d3e51bfb28ae4dd..a75574bb196bd932d809dd23ed569cbfd203b0b1 100644 (file)
@@ -1138,21 +1138,24 @@ Selection::get_state () const
        XMLNode* node = new XMLNode (X_("Selection"));
 
        for (TrackSelection::const_iterator i = tracks.begin(); i != tracks.end(); ++i) {
-               RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*i);
+               StripableTimeAxisView* stv = dynamic_cast<StripableTimeAxisView*> (*i);
                AutomationTimeAxisView* atv = dynamic_cast<AutomationTimeAxisView*> (*i);
-               if (rtv) {
-                       XMLNode* t = node->add_child (X_("RouteView"));
-                       t->set_property (X_("id"), rtv->route()->id ());
+               if (stv) {
+                       XMLNode* t = node->add_child (X_("StripableView"));
+                       t->set_property (X_("id"), stv->stripable()->id ());
                } else if (atv) {
                        XMLNode* t = node->add_child (X_("AutomationView"));
-                       t->set_property (X_("id"), atv->parent_route()->id ());
+                       t->set_property (X_("id"), atv->parent_stripable()->id ());
                        t->set_property (X_("parameter"), EventTypeMap::instance().to_symbol (atv->parameter ()));
                }
        }
 
-       for (RegionSelection::const_iterator i = regions.begin(); i != regions.end(); ++i) {
-               XMLNode* r = node->add_child (X_("Region"));
-               r->set_property (X_("id"), (*i)->region ()->id ());
+       if (!regions.empty()) {
+               XMLNode* parent = node->add_child (X_("Regions"));
+               for (RegionSelection::const_iterator i = regions.begin(); i != regions.end(); ++i) {
+                       XMLNode* r = parent->add_child (X_("Region"));
+                       r->set_property (X_("id"), (*i)->region ()->id ());
+               }
        }
 
        /* midi region views have thir own internal selection. */
@@ -1176,7 +1179,7 @@ Selection::get_state () const
 
                        XMLNode* r = node->add_child (X_("ControlPoint"));
                        r->set_property (X_("type"), "track");
-                       r->set_property (X_("route-id"), atv->parent_route()->id ());
+                       r->set_property (X_("route-id"), atv->parent_stripable()->id ());
                        r->set_property (X_("automation-list-id"), (*i)->line().the_list()->id ());
                        r->set_property (X_("parameter"), EventTypeMap::instance().to_symbol ((*i)->line().the_list()->parameter ()));
                        r->set_property (X_("view-index"), (*i)->view_index());
@@ -1225,8 +1228,6 @@ Selection::set_state (XMLNode const & node, int)
        clear_time ();
        clear_markers ();
 
-       RegionSelection selected_regions;
-
        /* NOTE: stripable/time-axis-view selection is saved/restored by
         * ARDOUR::CoreSelection, not this Selection object
         */
@@ -1235,23 +1236,34 @@ Selection::set_state (XMLNode const & node, int)
        XMLNodeList children = node.children ();
 
        for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
-               if ((*i)->name() == X_("Region")) {
+               if ((*i)->name() == X_("Regions")) {
+                       RegionSelection selected_regions;
+                       XMLNodeList children = (*i)->children ();
+                       for (XMLNodeList::const_iterator ci = children.begin(); ci != children.end(); ++ci) {
+                               PBD::ID id;
 
-                       if (!(*i)->get_property (X_("id"), id)) {
-                               assert(false);
+                               if (!(*ci)->get_property (X_("id"), id)) {
+                                       continue;
+                               }
+
+                               RegionSelection rs;
+                               editor->get_regionviews_by_id (id, rs);
+
+                               if (!rs.empty ()) {
+                                       for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
+                                               selected_regions.push_back (*i);
+                                       }
+                               } else {
+                                       /*
+                                         regionviews haven't been constructed - stash the region IDs
+                                         so we can identify them in Editor::region_view_added ()
+                                       */
+                                       regions.pending.push_back (id);
+                               }
                        }
 
-                       RegionSelection rs;
-                       editor->get_regionviews_by_id (id, rs);
-
-                       if (!rs.empty ()) {
-                               selected_regions.insert (selected_regions.end(), rs.begin(), rs.end());
-                       } else {
-                               /*
-                                 regionviews haven't been constructed - stash the region IDs
-                                 so we can identify them in Editor::region_view_added ()
-                               */
-                               regions.pending.push_back (id);
+                       if (!selected_regions.empty()) {
+                               add (selected_regions);
                        }
 
                } else if ((*i)->name() == X_("MIDINotes")) {
@@ -1305,11 +1317,11 @@ Selection::set_state (XMLNode const & node, int)
                                        assert(false);
                                }
 
-                               RouteTimeAxisView* rtv = editor->get_route_view_by_route_id (route_id);
+                               StripableTimeAxisView* stv = editor->get_stripable_time_axis_by_id (route_id);
                                vector <ControlPoint *> cps;
 
-                               if (rtv) {
-                                       boost::shared_ptr<AutomationTimeAxisView> atv = rtv->automation_child (EventTypeMap::instance().from_symbol (param));
+                               if (stv) {
+                                       boost::shared_ptr<AutomationTimeAxisView> atv = stv->automation_child (EventTypeMap::instance().from_symbol (param));
                                        if (atv) {
                                                list<boost::shared_ptr<AutomationLine> > lines = atv->lines();
                                                for (list<boost::shared_ptr<AutomationLine> > ::iterator li = lines.begin(); li != lines.end(); ++li) {
@@ -1364,7 +1376,8 @@ Selection::set_state (XMLNode const & node, int)
                        if (!(*i)->get_property (X_("start"), start) || !(*i)->get_property (X_("end"), end)) {
                                assert(false);
                        }
-                       set_preserving_all_ranges (start, end);
+
+                       add (start, end);
 
                } else if ((*i)->name() == X_("AutomationView")) {
 
@@ -1374,10 +1387,10 @@ Selection::set_state (XMLNode const & node, int)
                                assert (false);
                        }
 
-                       RouteTimeAxisView* rtv = editor->get_route_view_by_route_id (id);
+                       StripableTimeAxisView* stv = editor->get_stripable_time_axis_by_id (id);
 
-                       if (rtv) {
-                               boost::shared_ptr<AutomationTimeAxisView> atv = rtv->automation_child (EventTypeMap::instance().from_symbol (param));
+                       if (stv) {
+                               boost::shared_ptr<AutomationTimeAxisView> atv = stv->automation_child (EventTypeMap::instance().from_symbol (param));
 
                                /* the automation could be for an entity that was never saved
                                   in the session file. Don't freak out if we can't find
@@ -1405,9 +1418,6 @@ Selection::set_state (XMLNode const & node, int)
 
        }
 
-       // now add regions to selection at once
-       add (selected_regions);
-
        return 0;
 }
 
@@ -1452,10 +1462,6 @@ Selection::toggle (const TrackViewList& track_list)
 void
 Selection::toggle (TimeAxisView* track)
 {
-       if (dynamic_cast<VCATimeAxisView*> (track)) {
-               return;
-       }
-
        TrackViewList tr;
        tr.push_back (track);
        toggle (tr);
@@ -1479,10 +1485,6 @@ Selection::add (TrackViewList const & track_list)
 void
 Selection::add (TimeAxisView* track)
 {
-       if (dynamic_cast<VCATimeAxisView*> (track)) {
-               return;
-       }
-
        TrackViewList tr;
        tr.push_back (track);
        add (tr);
@@ -1491,10 +1493,6 @@ Selection::add (TimeAxisView* track)
 void
 Selection::remove (TimeAxisView* track)
 {
-       if (dynamic_cast<VCATimeAxisView*> (track)) {
-               return;
-       }
-
        TrackViewList tvl;
        tvl.push_back (track);
        remove (tvl);
@@ -1516,10 +1514,6 @@ Selection::remove (const TrackViewList& t)
 void
 Selection::set (TimeAxisView* track)
 {
-       if (dynamic_cast<VCATimeAxisView*> (track)) {
-               return;
-       }
-
        TrackViewList tvl;
        tvl.push_back (track);
        set (tvl);
@@ -1531,6 +1525,43 @@ Selection::set (const TrackViewList& track_list)
        TrackViewList t = add_grouped_tracks (track_list);
 
        CoreSelection& selection (editor->session()->selection());
+
+#if 1 // crazy optmization hack
+       /* check is the selection actually changed, ignore NO-OPs
+        *
+        * There are excessive calls from EditorRoutes::selection_changed():
+        * Every click calls selection_changed() even if it doesn't change.
+        * Also re-ordering tracks calls into this due to gtk's odd DnD signal
+        * messaging (row removed, re-added).
+        *
+        * Re-ordering a row results in at least 2 calls to selection_changed()
+        * without actual change. Calling selection.clear_stripables()
+        * and re-adding the same tracks every time in turn emits changed signals.
+        */
+       bool changed = false;
+       CoreSelection::StripableAutomationControls sac;
+       selection.get_stripables (sac);
+       for (TrackSelection::const_iterator i = t.begin(); i != t.end(); ++i) {
+               boost::shared_ptr<Stripable> s = (*i)->stripable ();
+               boost::shared_ptr<AutomationControl> c = (*i)->control ();
+               bool found = false;
+               for (CoreSelection::StripableAutomationControls::iterator j = sac.begin (); j != sac.end (); ++j) {
+                       if (j->stripable == s && j->controllable == c) {
+                               found = true;
+                               sac.erase (j);
+                               break;
+                       }
+               }
+               if (!found) {
+                       changed = true;
+                       break;
+               }
+       }
+       if (!changed && sac.size() == 0) {
+               return;
+       }
+#endif
+
        PresentationInfo::ChangeSuspender cs;
 
        selection.clear_stripables ();