Cleanup of region drag code to be a bit more efficient and shorter. Fixes crashes...
[ardour.git] / gtk2_ardour / editor_selection.cc
index 3e5b412c85909f6f266cb0304dbccc2068ce5f20..806efaa66ac5e555c891c18bbc4a1070d72ecd8f 100644 (file)
@@ -23,7 +23,6 @@
 #include "pbd/stacktrace.h"
 
 #include "ardour/session.h"
-#include "ardour/diskstream.h"
 #include "ardour/playlist.h"
 #include "ardour/route_group.h"
 #include "ardour/profile.h"
@@ -171,24 +170,110 @@ Editor::select_all_tracks ()
        selection->set (visible_views);
 }
 
-/** Select clicked_routeview, unless there are no currently selected
+/** Select clicked_axisview, unless there are no currently selected
  *  tracks, in which case nothing will happen unless `force' is true.
  */
 void
-Editor::set_selected_track_as_side_effect (bool force)
+Editor::set_selected_track_as_side_effect (Selection::Operation op, bool /*force*/)
 {
-       if (!clicked_routeview) {
+       if (!clicked_axisview) {
                return;
        }
 
+#if 1
+        if (!clicked_routeview) {
+                return;
+        }
+
+        bool had_tracks = !selection->tracks.empty();
+        RouteGroup* group = clicked_routeview->route()->route_group();
+        RouteGroup& arg (_session->all_route_group());
+        
+       switch (op) {
+       case Selection::Toggle: 
+               if (selection->selected (clicked_axisview)) {
+                       if (arg.is_select() && arg.is_active()) {
+                               for (TrackViewList::iterator i = track_views.begin(); i != track_views.end (); ++i) {
+                                        selection->remove(*i);
+                               }
+                       } else if (group && group->is_active()) {
+                               for (TrackViewList::iterator i = track_views.begin(); i != track_views.end (); ++i) {
+                                       if ((*i)->route_group() == group)
+                                               selection->remove(*i);
+                               }
+                       } else {
+                               selection->remove (clicked_axisview);
+                        }
+               } else {
+                       if (arg.is_select() && arg.is_active()) {
+                               for (TrackViewList::iterator i = track_views.begin(); i != track_views.end (); ++i) {
+                                               selection->add(*i);
+                               }
+                       } else if (group && group->is_active()) {
+                               for (TrackViewList::iterator i = track_views.begin(); i != track_views.end (); ++i) {
+                                       if ( (*i)->route_group() == group)
+                                               selection->add(*i);
+                               }
+                        } else {
+                                selection->add (clicked_axisview);
+                        }
+               }
+                break;
+       
+       case Selection::Add: 
+               if (!had_tracks && arg.is_select() && arg.is_active()) {
+                        /* nothing was selected already, and all group is active etc. so use
+                           all tracks.
+                        */
+                       for (TrackViewList::iterator i = track_views.begin(); i != track_views.end (); ++i) {
+                                       selection->add(*i);
+                       }
+               } else if (group && group->is_active()) {
+                       for (TrackViewList::iterator i  = track_views.begin(); i != track_views.end (); ++i) {
+                               if ((*i)->route_group() == group)
+                                       selection->add(*i);
+                       }
+                } else {
+                       selection->add (clicked_axisview);
+                }
+                break;
+               
+       case Selection::Set:
+                selection->clear();
+               if (!had_tracks && arg.is_select() && arg.is_active()) {
+                        /* nothing was selected already, and all group is active etc. so use
+                           all tracks.
+                        */
+                       for (TrackViewList::iterator i = track_views.begin(); i != track_views.end (); ++i) {
+                                       selection->add(*i);
+                       }
+               } else if (group && group->is_active()) {
+                       for (TrackViewList::iterator i  = track_views.begin(); i != track_views.end (); ++i) {
+                               if ((*i)->route_group() == group)
+                                       selection->add(*i);
+                       }
+                } else {
+                       selection->set (clicked_axisview);
+                }
+                break;
+       
+       case Selection::Extend: 
+               selection->clear();
+               cerr << ("Editor::set_selected_track_as_side_effect  case  Selection::Add  not yet implemented\n");
+                break;
+       }
+
+#else // the older version
+
        if (!selection->tracks.empty()) {
-               if (!selection->selected (clicked_routeview)) {
-                       selection->add (clicked_routeview);
+               if (!selection->selected (clicked_axisview)) {
+                       selection->add (clicked_axisview);
                }
 
        } else if (force) {
-               selection->set (clicked_routeview);
+               selection->set (clicked_axisview);
        }
+#endif
 }
 
 void
@@ -241,36 +326,23 @@ Editor::set_selected_control_point_from_click (Selection::Operation op, bool /*n
        if (!clicked_control_point) {
                return false;
        }
-
-       if (clicked_control_point->selected()) {
-               /* the clicked control point is already selected; others may be as well, so
-                  don't change the selection.
-               */
-               return true;
+       
+       switch (op) {
+       case Selection::Set:
+               selection->set (clicked_control_point);
+               break;
+       case Selection::Add:
+               selection->add (clicked_control_point);
+               break;
+       case Selection::Toggle:
+               selection->toggle (clicked_control_point);
+               break;
+       case Selection::Extend:
+               /* XXX */
+               break;
        }
 
-       /* We know the ControlPoint that was clicked, but (as discussed in automation_selectable.h)
-        * selected automation data are described by areas on the AutomationLine.  A ControlPoint
-        * represents any model points in the space that it takes up, so the AutomationSelectable
-        * needs to be the size of the ControlPoint.
-        */
-
-       double const size = clicked_control_point->size ();
-       
-       nframes64_t const x1 = pixel_to_frame (clicked_control_point->get_x() - size / 2);
-       nframes64_t const x2 = pixel_to_frame (clicked_control_point->get_x() + size / 2);
-       double y1 = clicked_control_point->get_y() - size / 2;
-       double y2 = clicked_control_point->get_y() + size / 2;
-
-       /* convert the y values to trackview space */
-       double dummy = 0;
-       clicked_control_point->line().parent_group().i2w (dummy, y1);
-       clicked_control_point->line().parent_group().i2w (dummy, y2);
-       _trackview_group->w2i (dummy, y1);
-       _trackview_group->w2i (dummy, y2);
-
-       /* and set up the selection */
-       return select_all_within (x1, x2, y1, y2, selection->tracks, Selection::Set);
+       return true;
 }
 
 void
@@ -295,6 +367,7 @@ void
 Editor::mapover_tracks (sigc::slot<void, RouteTimeAxisView&, uint32_t> sl, TimeAxisView* basis, PBD::PropertyID prop) const
 {
        RouteTimeAxisView* route_basis = dynamic_cast<RouteTimeAxisView*> (basis);
+
        if (route_basis == 0) {
                return;
        }
@@ -303,7 +376,8 @@ Editor::mapover_tracks (sigc::slot<void, RouteTimeAxisView&, uint32_t> sl, TimeA
        tracks.insert (route_basis);
 
        RouteGroup* group = route_basis->route()->route_group();
-       if (group && group->enabled_property (prop)) {
+
+       if (group && group->enabled_property(prop) && group->enabled_property (Properties::active.property_id) ) {
 
                /* the basis is a member of an active route group, with the appropriate
                   properties; find other members */
@@ -318,6 +392,7 @@ Editor::mapover_tracks (sigc::slot<void, RouteTimeAxisView&, uint32_t> sl, TimeA
 
        /* call the slots */
        uint32_t const sz = tracks.size ();
+       
        for (set<RouteTimeAxisView*>::iterator i = tracks.begin(); i != tracks.end(); ++i) {
                sl (**i, sz);
        }
@@ -329,9 +404,9 @@ Editor::mapped_get_equivalent_regions (RouteTimeAxisView& tv, uint32_t, RegionVi
        boost::shared_ptr<Playlist> pl;
        vector<boost::shared_ptr<Region> > results;
        RegionView* marv;
-       boost::shared_ptr<Diskstream> ds;
+       boost::shared_ptr<Track> tr;
 
-       if ((ds = tv.get_diskstream()) == 0) {
+       if ((tr = tv.track()) == 0) {
                /* bus */
                return;
        }
@@ -341,7 +416,7 @@ Editor::mapped_get_equivalent_regions (RouteTimeAxisView& tv, uint32_t, RegionVi
                return;
        }
 
-       if ((pl = ds->playlist()) != 0) {
+       if ((pl = tr->playlist()) != 0) {
                pl->get_equivalent_regions (basis->region(), results);
        }
 
@@ -355,7 +430,7 @@ Editor::mapped_get_equivalent_regions (RouteTimeAxisView& tv, uint32_t, RegionVi
 void
 Editor::get_equivalent_regions (RegionView* basis, vector<RegionView*>& equivalent_regions, PBD::PropertyID property) const
 {
-       mapover_tracks (sigc::bind (sigc::mem_fun (*this, &Editor::mapped_get_equivalent_regions), basis, &equivalent_regions), &basis->get_trackview(), property);
+       mapover_tracks (sigc::bind (sigc::mem_fun (*this, &Editor::mapped_get_equivalent_regions), basis, &equivalent_regions), &basis->get_time_axis_view(), property);
 
        /* add clicked regionview since we skipped all other regions in the same track as the one it was in */
 
@@ -373,7 +448,7 @@ Editor::get_equivalent_regions (RegionSelection & basis, PBD::PropertyID prop) c
 
                mapover_tracks (
                        sigc::bind (sigc::mem_fun (*this, &Editor::mapped_get_equivalent_regions), *i, &eq),
-                       &(*i)->get_trackview(), prop
+                       &(*i)->get_time_axis_view(), prop
                        );
 
                for (vector<RegionView*>::iterator j = eq.begin(); j != eq.end(); ++j) {
@@ -401,14 +476,14 @@ Editor::get_regionview_count_from_region_list (boost::shared_ptr<Region> region)
                        boost::shared_ptr<Playlist> pl;
                        vector<boost::shared_ptr<Region> > results;
                        RegionView* marv;
-                       boost::shared_ptr<Diskstream> ds;
+                       boost::shared_ptr<Track> tr;
 
-                       if ((ds = tatv->get_diskstream()) == 0) {
+                       if ((tr = tatv->track()) == 0) {
                                /* bus */
                                continue;
                        }
 
-                       if ((pl = (ds->playlist())) != 0) {
+                       if ((pl = (tr->playlist())) != 0) {
                                pl->get_region_list_equivalent_regions (region, results);
                        }
 
@@ -476,7 +551,7 @@ Editor::set_selected_regionview_from_click (bool press, Selection::Operation op,
                                if (press) {
 
                                        if (selection->selected (clicked_routeview)) {
-                                               get_equivalent_regions (clicked_regionview, all_equivalent_regions, ARDOUR::Properties::select.id);
+                                               get_equivalent_regions (clicked_regionview, all_equivalent_regions, ARDOUR::Properties::select.property_id);
                                        } else {
                                                all_equivalent_regions.push_back (clicked_regionview);
                                        }
@@ -494,7 +569,7 @@ Editor::set_selected_regionview_from_click (bool press, Selection::Operation op,
 
                case Selection::Set:
                        if (!selection->selected (clicked_regionview)) {
-                               get_equivalent_regions (clicked_regionview, all_equivalent_regions, ARDOUR::Properties::select.id);
+                               get_equivalent_regions (clicked_regionview, all_equivalent_regions, ARDOUR::Properties::select.property_id);
                                selection->set (all_equivalent_regions);
                                commit = true;
                        } else {
@@ -799,7 +874,7 @@ Editor::set_selected_regionview_from_map_event (GdkEventAny* /*ev*/, StreamView*
 void
 Editor::track_selection_changed ()
 {
-       switch (selection->tracks.size()){
+       switch (selection->tracks.size()) {
        case 0:
                break;
        default:
@@ -808,12 +883,24 @@ Editor::track_selection_changed ()
        }
 
        for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
-               if (find (selection->tracks.begin(), selection->tracks.end(), *i) != selection->tracks.end()) {
-                       (*i)->set_selected (true);
-               } else {
-                       (*i)->set_selected (false);
-               }
-       }
+
+                bool yn = (find (selection->tracks.begin(), selection->tracks.end(), *i) != selection->tracks.end());
+                
+                (*i)->set_selected (yn);
+                
+                TimeAxisView::Children c = (*i)->get_child_list ();
+                for (TimeAxisView::Children::iterator j = c.begin(); j != c.end(); ++j) {
+                        (*j)->set_selected (find (selection->tracks.begin(), selection->tracks.end(), j->get()) != selection->tracks.end());
+                }
+
+                if (yn && 
+                    ((mouse_mode == MouseRange) || 
+                     ((mouse_mode == MouseObject) && (_join_object_range_state == JOIN_OBJECT_RANGE_OBJECT)))) {
+                        (*i)->reshow_selection (selection->time);
+                } else {
+                        (*i)->hide_selection ();
+                }
+        }
 
        ActionManager::set_sensitive (ActionManager::track_selection_sensitive_actions, !selection->tracks.empty());
 }
@@ -975,16 +1062,19 @@ Editor::invert_selection ()
        selection->set (touched);
 }
 
-/** @param top Top (lower) y limit in trackview coordinates.
- *  @param bottom Bottom (higher) y limit in trackview coordinates.
+/** @param start Start time in session frames.
+ *  @param end End time in session frames.
+ *  @param top Top (lower) y limit in trackview coordinates (ie 0 at the top of the track view)
+ *  @param bottom Bottom (higher) y limit in trackview coordinates (ie 0 at the top of the track view)
+ *  @param preserve_if_selected true to leave the current selection alone if we're adding to the selection and all of the selectables
+ *  within the region are already selected.
  */
 bool
 Editor::select_all_within (
-       nframes64_t start, nframes64_t end, double top, double bot, const TrackViewList& tracklist, Selection::Operation op
+       framepos_t start, framepos_t end, double top, double bot, const TrackViewList& tracklist, Selection::Operation op, bool preserve_if_selected
        )
 {
        list<Selectable*> found;
-       TrackViewList tracks;
 
        for (TrackViewList::const_iterator iter = tracklist.begin(); iter != tracklist.end(); ++iter) {
                
@@ -992,34 +1082,21 @@ Editor::select_all_within (
                        continue;
                }
 
-               list<Selectable*>::size_type const n = found.size ();
-
                (*iter)->get_selectables (start, end, top, bot, found);
-
-               if (n != found.size()) {
-                       tracks.push_back (*iter);
-               }
        }
-       
+
        if (found.empty()) {
                return false;
        }
 
-       if (!tracks.empty()) {
+       if (preserve_if_selected && op != Selection::Toggle) {
+               list<Selectable*>::iterator i = found.begin();
+               while (i != found.end() && (*i)->get_selected()) {
+                       ++i;
+               }
 
-               switch (op) {
-               case Selection::Add:
-                       selection->add (tracks);
-                       break;
-               case Selection::Toggle:
-                       selection->toggle (tracks);
-                       break;
-               case Selection::Set:
-                       selection->set (tracks);
-                       break;
-               case Selection::Extend:
-                       /* not defined yet */
-                       break;
+               if (i == found.end()) {
+                       return false;
                }
        }
 
@@ -1306,6 +1383,10 @@ Editor::select_range_between ()
         nframes64_t start;
        nframes64_t end;
 
+        if (mouse_mode == MouseRange && !selection->time.empty()) {
+                selection->clear_time ();
+        }
+
        if (!get_edit_op_range (start, end)) {
                return;
        }