likely fix for crash in MidiBuffer::merge_in_place() due to unwarranted assumption...
[ardour.git] / gtk2_ardour / editor_selection.cc
index c2338347d2d4b85235bca3392c1bc7f5cc3d4c5d..142204f9b4864992404e73146cd8a6db68cd4d85 100644 (file)
@@ -51,9 +51,9 @@ using namespace Editing;
 
 struct TrackViewByPositionSorter
 {
-    bool operator() (const TimeAxisView* a, const TimeAxisView *b) {
-           return a->y_position() < b->y_position();
-    }
+       bool operator() (const TimeAxisView* a, const TimeAxisView *b) {
+               return a->y_position() < b->y_position();
+       }
 };
 
 bool
@@ -184,20 +184,20 @@ Editor::set_selected_track_as_side_effect (Selection::Operation op, bool /*force
        }
 
 #if 1
-        if (!clicked_routeview) {
-                return;
-        }
+       if (!clicked_routeview) {
+               return;
+       }
 
-        bool had_tracks = !selection->tracks.empty();
-        RouteGroup* group = clicked_routeview->route()->route_group();
-        RouteGroup& arg (_session->all_route_group());
+       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);
+                                       selection->remove(*i);
                                }
                        } else if (group && group->is_active()) {
                                for (TrackViewList::iterator i = track_views.begin(); i != track_views.end (); ++i) {
@@ -206,64 +206,64 @@ Editor::set_selected_track_as_side_effect (Selection::Operation op, bool /*force
                                }
                        } 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);
+                                       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);
-                        }
+                       } else {
+                               selection->add (clicked_axisview);
+                       }
                }
-                break;
+               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.
-                        */
+                       /* 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);
+                               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 {
+               } else {
                        selection->add (clicked_axisview);
-                }
-                break;
+               }
+               break;
 
        case Selection::Set:
-                selection->clear();
+               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.
-                        */
+                       /* 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);
+                               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 {
+               } else {
                        selection->set (clicked_axisview);
-                }
-                break;
+               }
+               break;
 
        case Selection::Extend:
                selection->clear();
                cerr << ("Editor::set_selected_track_as_side_effect  case  Selection::Add  not yet implemented\n");
-                break;
+               break;
        }
 
 #else // the older version
@@ -351,11 +351,11 @@ Editor::set_selected_control_point_from_click (Selection::Operation op, bool /*n
 void
 Editor::get_onscreen_tracks (TrackViewList& tvl)
 {
-       for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
-               if ((*i)->y_position() < _canvas_height) {
-                       tvl.push_back (*i);
-               }
-       }
+       for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
+               if ((*i)->y_position() < _canvas_height) {
+                       tvl.push_back (*i);
+               }
+       }
 }
 
 /** Call a slot for a given `basis' track and also for any track that is in the same
@@ -401,6 +401,63 @@ Editor::mapover_tracks (sigc::slot<void, RouteTimeAxisView&, uint32_t> sl, TimeA
        }
 }
 
+/** Call a slot for a given `basis' track and also for any track that is in the same
+ *  active route group with a particular set of properties.
+ *
+ *  @param sl Slot to call.
+ *  @param basis Basis track.
+ *  @param prop Properties that active edit groups must share to be included in the map.
+ */
+
+void
+Editor::mapover_tracks_with_unique_playlists (sigc::slot<void, RouteTimeAxisView&, uint32_t> sl, TimeAxisView* basis, PBD::PropertyID prop) const
+{
+       RouteTimeAxisView* route_basis = dynamic_cast<RouteTimeAxisView*> (basis);
+       set<boost::shared_ptr<Playlist> > playlists;
+
+       if (route_basis == 0) {
+               return;
+       }
+
+       set<RouteTimeAxisView*> tracks;
+       tracks.insert (route_basis);
+
+       RouteGroup* group = route_basis->route()->route_group(); // could be null, not a problem
+
+       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 */
+
+               for (TrackViewList::const_iterator i = track_views.begin(); i != track_views.end(); ++i) {
+                       RouteTimeAxisView* v = dynamic_cast<RouteTimeAxisView*> (*i);
+
+                       if (v && v->route()->route_group() == group) {
+                               
+                               boost::shared_ptr<Track> t = v->track();
+                               if (t) {
+                                       if (playlists.insert (t->playlist()).second) {
+                                               /* haven't seen this playlist yet */
+                                               tracks.insert (v);
+                                       }
+                               } else {
+                                       /* not actually a "Track", but a timeaxis view that
+                                          we should mapover anyway.
+                                       */
+                                       tracks.insert (v);
+                               }
+                       }
+               }
+       }
+
+       /* call the slots */
+       uint32_t const sz = tracks.size ();
+
+       for (set<RouteTimeAxisView*>::iterator i = tracks.begin(); i != tracks.end(); ++i) {
+               sl (**i, sz);
+       }
+}
+
 void
 Editor::mapped_get_equivalent_regions (RouteTimeAxisView& tv, uint32_t, RegionView * basis, vector<RegionView*>* all_equivs) const
 {
@@ -433,7 +490,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_time_axis_view(), property);
+       mapover_tracks_with_unique_playlists (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 */
 
@@ -449,10 +506,9 @@ Editor::get_equivalent_regions (RegionSelection & basis, PBD::PropertyID prop) c
 
                vector<RegionView*> eq;
 
-               mapover_tracks (
+               mapover_tracks_with_unique_playlists (
                        sigc::bind (sigc::mem_fun (*this, &Editor::mapped_get_equivalent_regions), *i, &eq),
-                       &(*i)->get_time_axis_view(), prop
-                       );
+                       &(*i)->get_time_axis_view(), prop);
 
                for (vector<RegionView*>::iterator j = eq.begin(); j != eq.end(); ++j) {
                        equivalent.add (*j);
@@ -765,8 +821,8 @@ Editor::set_selected_regionview_from_click (bool press, Selection::Operation op,
                                                                        */
 
                                                                        if (find (already_in_selection.begin(),
-                                                                                 already_in_selection.end(),
-                                                                                 artv) == already_in_selection.end()) {
+                                                                                 already_in_selection.end(),
+                                                                                 artv) == already_in_selection.end()) {
 
                                                                                relevant_tracks.insert (artv);
                                                                        }
@@ -779,7 +835,7 @@ Editor::set_selected_regionview_from_click (bool press, Selection::Operation op,
                }
 
                /* 3. find all selectable objects (regionviews in this case) between that one and the end of the
-                          one that was clicked.
+                  one that was clicked.
                */
 
                for (set<RouteTimeAxisView*>::iterator t = relevant_tracks.begin(); t != relevant_tracks.end(); ++t) {
@@ -804,7 +860,7 @@ Editor::set_selected_regionview_from_click (bool press, Selection::Operation op,
                }
        }
 
-  out:
+out:
        return commit;
 }
 
@@ -885,23 +941,23 @@ Editor::track_selection_changed ()
 
        for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
 
-                bool yn = (find (selection->tracks.begin(), selection->tracks.end(), *i) != selection->tracks.end());
+               bool yn = (find (selection->tracks.begin(), selection->tracks.end(), *i) != selection->tracks.end());
 
-                (*i)->set_selected (yn);
+               (*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());
-                }
+               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 ();
-                }
-        }
+               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());
 }
@@ -974,6 +1030,7 @@ Editor::sensitize_the_right_region_actions ()
        /* Look through the regions that are selected and make notes about what we have got */
 
        bool have_audio = false;
+       bool have_multichannel_audio = false;
        bool have_midi = false;
        bool have_locked = false;
        bool have_unlocked = false;
@@ -998,6 +1055,9 @@ Editor::sensitize_the_right_region_actions ()
 
                if (ar) {
                        have_audio = true;
+                       if (ar->n_channels() > 1) {
+                               have_multichannel_audio = true;
+                       }
                }
 
                if (boost::dynamic_pointer_cast<MidiRegion> (r)) {
@@ -1037,18 +1097,22 @@ Editor::sensitize_the_right_region_actions ()
                }
 
                if (ar) {
-                        /* its a bit unfortunate that "envelope visible" is a view-only
-                           property. we have to find the regionview to able to check
-                           its current setting.
-                        */
-
-                        have_envelope_invisible = true;
-
-                        if (*i) {
-                                AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*i);
-                                if (arv && arv->envelope_visible()) {
-                                        have_envelope_visible = true;
-                                }
+                       /* its a bit unfortunate that "envelope visible" is a view-only
+                          property. we have to find the regionview to able to check
+                          its current setting.
+                       */
+
+                       have_envelope_invisible = false;
+
+                       if (*i) {
+                               AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*i);
+                               if (arv) {
+                                       if (arv->envelope_visible()) {
+                                               have_envelope_visible = true;
+                                       } else {
+                                               have_envelope_invisible = true;
+                                       }
+                               }
                        }
 
                        if (ar->envelope_active()) {
@@ -1078,6 +1142,10 @@ Editor::sensitize_the_right_region_actions ()
                _region_actions->get_action("combine-regions")->set_sensitive (false);
        }
 
+       if (!have_multichannel_audio) {
+               _region_actions->get_action("split-multichannel-region")->set_sensitive (false);
+       }
+
        if (!have_midi) {
                _region_actions->get_action("show-region-list-editor")->set_sensitive (false);
                _region_actions->get_action("quantize-region")->set_sensitive (false);
@@ -1104,13 +1172,13 @@ Editor::sensitize_the_right_region_actions ()
                if (have_envelope_visible && !have_envelope_invisible) {
                        Glib::RefPtr<ToggleAction>::cast_dynamic (_region_actions->get_action("toggle-region-gain-envelope-visible"))->set_active ();
                } else if (have_envelope_visible && have_envelope_invisible) {
-//                     _region_actions->get_action("toggle-region-gain-envelope-visible")->set_inconsistent ();
+                       // _region_actions->get_action("toggle-region-gain-envelope-visible")->set_inconsistent ();
                }
 
                if (have_envelope_active && !have_envelope_inactive) {
                        Glib::RefPtr<ToggleAction>::cast_dynamic (_region_actions->get_action("toggle-region-gain-envelope-active"))->set_active ();
                } else if (have_envelope_active && have_envelope_inactive) {
-//                     _region_actions->get_action("toggle-region-gain-envelope-active")->set_inconsistent ();
+                       // _region_actions->get_action("toggle-region-gain-envelope-active")->set_inconsistent ();
                }
 
        } else {
@@ -1129,23 +1197,23 @@ Editor::sensitize_the_right_region_actions ()
 
        Glib::RefPtr<ToggleAction>::cast_dynamic (_region_actions->get_action("toggle-region-lock"))->set_active (have_locked && !have_unlocked);
        if (have_locked && have_unlocked) {
-//             _region_actions->get_action("toggle-region-lock")->set_inconsistent ();
+               // _region_actions->get_action("toggle-region-lock")->set_inconsistent ();
        }
 
        Glib::RefPtr<ToggleAction>::cast_dynamic (_region_actions->get_action("toggle-region-lock-style"))->set_active (have_position_lock_style_music && !have_position_lock_style_audio);
 
        if (have_position_lock_style_music && have_position_lock_style_audio) {
-//             _region_actions->get_action("toggle-region-lock-style")->set_inconsistent ();
+               // _region_actions->get_action("toggle-region-lock-style")->set_inconsistent ();
        }
 
        Glib::RefPtr<ToggleAction>::cast_dynamic (_region_actions->get_action("toggle-region-mute"))->set_active (have_muted && !have_unmuted);
        if (have_muted && have_unmuted) {
-//             _region_actions->get_action("toggle-region-mute")->set_inconsistent ();
+               // _region_actions->get_action("toggle-region-mute")->set_inconsistent ();
        }
 
        Glib::RefPtr<ToggleAction>::cast_dynamic (_region_actions->get_action("toggle-opaque-region"))->set_active (have_opaque && !have_non_opaque);
        if (have_opaque && have_non_opaque) {
-//                     _region_actions->get_action("toggle-opaque-region")->set_inconsistent ();
+               // _region_actions->get_action("toggle-opaque-region")->set_inconsistent ();
        }
 
        if (!have_not_at_natural_position) {
@@ -1179,9 +1247,9 @@ Editor::region_selection_changed ()
                (*i)->set_selected_regionviews (selection->regions);
        }
 
-        if (_region_selection_change_updates_region_list) {
-                _regions->set_selected (selection->regions);
-        }
+       if (_region_selection_change_updates_region_list) {
+               _regions->set_selected (selection->regions);
+       }
 
        _regions->block_change_connection (false);
        editor_regions_selection_changed_connection.block(false);
@@ -1230,15 +1298,13 @@ Editor::select_all_in_track (Selection::Operation op)
 }
 
 void
-Editor::select_all_internal_edit (Selection::Operation op)
+Editor::select_all_internal_edit (Selection::Operation)
 {
-        /* currently limited to MIDI only */
-
        for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
                MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
-                if (mrv) {
-                        mrv->select_all_notes ();
-                }
+               if (mrv) {
+                       mrv->select_all_notes ();
+               }
        }
 }
 
@@ -1247,10 +1313,10 @@ Editor::select_all (Selection::Operation op)
 {
        list<Selectable *> touched;
 
-        if (_internal_editing) {
-                select_all_internal_edit (op);
-                return;
-        }
+       if (_internal_editing) {
+               select_all_internal_edit (op);
+               return;
+       }
 
        for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
                if ((*iter)->hidden()) {
@@ -1275,6 +1341,7 @@ Editor::select_all (Selection::Operation op)
        }
        commit_reversible_command ();
 }
+
 void
 Editor::invert_selection_in_track ()
 {
@@ -1293,6 +1360,16 @@ Editor::invert_selection ()
 {
        list<Selectable *> touched;
 
+       if (_internal_editing) {
+               for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+                       MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
+                       if (mrv) {
+                               mrv->invert_selection ();
+                       }
+               }
+               return;
+       }
+
        for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
                if ((*iter)->hidden()) {
                        continue;
@@ -1311,9 +1388,7 @@ Editor::invert_selection ()
  *  within the region are already selected.
  */
 void
-Editor::select_all_within (
-       framepos_t start, framepos_t end, double top, double bot, const TrackViewList& tracklist, Selection::Operation op, bool preserve_if_selected
-       )
+Editor::select_all_within (framepos_t start, framepos_t end, double top, double bot, const TrackViewList& tracklist, Selection::Operation op, bool preserve_if_selected)
 {
        list<Selectable*> found;
 
@@ -1510,17 +1585,15 @@ Editor::select_all_selectables_using_loop()
 void
 Editor::select_all_selectables_using_cursor (EditorCursor *cursor, bool after)
 {
-        framepos_t start;
+       framepos_t start;
        framepos_t end;
        list<Selectable *> touched;
 
        if (after) {
-               begin_reversible_command (_("select all after cursor"));
                start = cursor->current_frame;
                end = _session->current_end_frame();
        } else {
                if (cursor->current_frame > 0) {
-                       begin_reversible_command (_("select all before cursor"));
                        start = 0;
                        end = cursor->current_frame - 1;
                } else {
@@ -1528,6 +1601,21 @@ Editor::select_all_selectables_using_cursor (EditorCursor *cursor, bool after)
                }
        }
 
+       if (_internal_editing) {
+               for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+                       MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
+                       if (mrv) {
+                               mrv->select_range (start, end);
+                       }
+               }
+               return;
+       }
+
+       if (after) {
+               begin_reversible_command (_("select all after cursor"));
+       } else {
+               begin_reversible_command (_("select all before cursor"));
+       }
 
        TrackViewList* ts;
 
@@ -1550,17 +1638,15 @@ Editor::select_all_selectables_using_cursor (EditorCursor *cursor, bool after)
 void
 Editor::select_all_selectables_using_edit (bool after)
 {
-        framepos_t start;
+       framepos_t start;
        framepos_t end;
        list<Selectable *> touched;
 
        if (after) {
-               begin_reversible_command (_("select all after edit"));
                start = get_preferred_edit_position();
                end = _session->current_end_frame();
        } else {
                if ((end = get_preferred_edit_position()) > 1) {
-                       begin_reversible_command (_("select all before edit"));
                        start = 0;
                        end -= 1;
                } else {
@@ -1568,6 +1654,19 @@ Editor::select_all_selectables_using_edit (bool after)
                }
        }
 
+       if (_internal_editing) {
+               for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+                       MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
+                       mrv->select_range (start, end);
+               }
+               return;
+       }
+
+       if (after) {
+               begin_reversible_command (_("select all after edit"));
+       } else {
+               begin_reversible_command (_("select all before edit"));
+       }
 
        TrackViewList* ts;
 
@@ -1590,7 +1689,7 @@ Editor::select_all_selectables_using_edit (bool after)
 void
 Editor::select_all_selectables_between (bool /*within*/)
 {
-        framepos_t start;
+       framepos_t start;
        framepos_t end;
        list<Selectable *> touched;
 
@@ -1598,6 +1697,14 @@ Editor::select_all_selectables_between (bool /*within*/)
                return;
        }
 
+       if (_internal_editing) {
+               for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+                       MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
+                       mrv->select_range (start, end);
+               }
+               return;
+       }
+
        TrackViewList* ts;
 
        if (selection->tracks.empty()) {
@@ -1619,12 +1726,12 @@ Editor::select_all_selectables_between (bool /*within*/)
 void
 Editor::select_range_between ()
 {
-        framepos_t start;
+       framepos_t start;
        framepos_t end;
 
-        if (mouse_mode == MouseRange && !selection->time.empty()) {
-                selection->clear_time ();
-        }
+       if (mouse_mode == MouseRange && !selection->time.empty()) {
+               selection->clear_time ();
+       }
 
        if (!get_edit_op_range (start, end)) {
                return;
@@ -1652,7 +1759,7 @@ Editor::get_edit_op_range (framepos_t& start, framepos_t& end) const
        if (!mouse_frame (m, ignored)) {
                /* mouse is not in a canvas, try playhead+selected marker.
                   this is probably most true when using menus.
-                */
+               */
 
                if (selection->markers.empty()) {
                        return false;
@@ -1692,9 +1799,9 @@ Editor::get_edit_op_range (framepos_t& start, framepos_t& end) const
                        if (selection->markers.empty()) {
 
                                MessageDialog win (_("No edit range defined"),
-                                                  false,
-                                                  MESSAGE_INFO,
-                                                  BUTTONS_OK);
+                                                  false,
+                                                  MESSAGE_INFO,
+                                                  BUTTONS_OK);
 
                                win.set_secondary_text (
                                        _("the edit point is Selected Marker\nbut there is no selected marker."));