Potential fix for confusion for Input/Output buttons on tracks with audio+midi ports
[ardour.git] / gtk2_ardour / editor_ops.cc
index 2327a318d4393a339abf07cebe1c21a8224fa5a2..2683020cbf0653f69df888bd77eaa39320ec9679 100644 (file)
@@ -97,6 +97,7 @@
 #include "strip_silence_dialog.h"
 #include "time_axis_view.h"
 #include "transpose_dialog.h"
+#include "transform_dialog.h"
 
 #include "i18n.h"
 
@@ -121,6 +122,11 @@ Editor::undo (uint32_t n)
        
        if (_session) {
                _session->undo (n);
+               if (_session->undo_depth() == 0) {
+                       undo_action->set_sensitive(false);
+               }
+               redo_action->set_sensitive(true);
+               begin_selection_op_history ();
        }
 }
 
@@ -133,6 +139,11 @@ Editor::redo (uint32_t n)
        
        if (_session) {
                _session->redo (n);
+               if (_session->redo_depth() == 0) {
+                       redo_action->set_sensitive(false);
+               }
+               undo_action->set_sensitive(true);
+               begin_selection_op_history ();
        }
 }
 
@@ -1701,29 +1712,9 @@ Editor::temporal_zoom_region (bool both_axes)
        framepos_t end = 0;
        set<TimeAxisView*> tracks;
 
-       RegionSelection rs = get_regions_from_selection_and_entered ();
-
-       if (rs.empty()) {
+       if ( !get_selection_extents(start, end) )
                return;
-       }
-
-       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
-
-               if ((*i)->region()->position() < start) {
-                       start = (*i)->region()->position();
-               }
-
-               if ((*i)->region()->last_frame() + 1 > end) {
-                       end = (*i)->region()->last_frame() + 1;
-               }
-
-               tracks.insert (&((*i)->get_time_axis_view()));
-       }
-
-       if ((start == 0 && end == 0) || end < start) {
-               return;
-       }
-
+       
        calc_extra_zoom_edges (start, end);
 
        /* if we're zooming on both axes we need to save track heights etc.
@@ -1761,6 +1752,46 @@ Editor::temporal_zoom_region (bool both_axes)
 }
 
 
+bool
+Editor::get_selection_extents ( framepos_t &start, framepos_t &end )
+{
+       start = max_framepos;
+       end = 0;
+       bool ret = true;
+       
+       //ToDo:  if notes are selected, set extents to that selection
+
+       //ToDo:  if control points are selected, set extents to that selection
+
+       if ( !selection->regions.empty() ) {
+               RegionSelection rs = get_regions_from_selection_and_entered ();
+
+               for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+
+                       if ((*i)->region()->position() < start) {
+                               start = (*i)->region()->position();
+                       }
+
+                       if ((*i)->region()->last_frame() + 1 > end) {
+                               end = (*i)->region()->last_frame() + 1;
+                       }
+               }
+
+       } else if (!selection->time.empty()) {
+               start = selection->time.start();
+               end = selection->time.end_frame();
+       } else 
+               ret = false;  //no selection found
+
+       //range check
+       if ((start == 0 && end == 0) || end < start) {
+               ret = false;
+       }
+       
+       return ret;     
+}
+
+
 void
 Editor::temporal_zoom_selection (bool both_axes)
 {
@@ -1777,16 +1808,14 @@ Editor::temporal_zoom_selection (bool both_axes)
        //if a range is selected, zoom to that
        if (!selection->time.empty()) {
 
-               framepos_t start = selection->time.start();
-               framepos_t end = selection->time.end_frame();
-
-               calc_extra_zoom_edges(start, end);
-
-               temporal_zoom_by_frame (start, end);
-
+               framepos_t start,  end;
+               if (get_selection_extents (start, end)) {
+                       calc_extra_zoom_edges(start, end);
+                       temporal_zoom_by_frame (start, end);
+               }
+               
                if (both_axes)
-                       fit_selected_tracks();
-
+                       fit_selection();
        }
 
 }
@@ -2331,11 +2360,15 @@ Editor::play_from_edit_point_and_return ()
 void
 Editor::play_selection ()
 {
-       if (selection->time.empty()) {
+       framepos_t start, end;
+       if (!get_selection_extents ( start, end))
                return;
-       }
 
-       _session->request_play_range (&selection->time, true);
+       AudioRange ar (start, end, 0);
+       list<AudioRange> lar;
+       lar.push_back (ar);
+
+       _session->request_play_range (&lar, true);
 }
 
 framepos_t
@@ -2368,16 +2401,17 @@ Editor::maybe_locate_with_edit_preroll ( framepos_t location )
 void
 Editor::play_with_preroll ()
 {
-       if (selection->time.empty()) {
-               return;
-       } else {
+       {
                framepos_t preroll = get_preroll();
                
-               framepos_t start = 0;
-               if (selection->time[clicked_selection].start > preroll)
-                       start = selection->time[clicked_selection].start - preroll;
+               framepos_t start, end;
+               if (!get_selection_extents ( start, end))
+                       return;
+
+               if (start > preroll)
+                       start = start - preroll;
                
-               framepos_t end = selection->time[clicked_selection].end + preroll;
+               end = end + preroll;  //"post-roll"
                
                AudioRange ar (start, end, 0);
                list<AudioRange> lar;
@@ -2690,8 +2724,14 @@ Editor::create_region_from_selection (vector<boost::shared_ptr<Region> >& new_re
                return;
        }
 
-       framepos_t start = selection->time[clicked_selection].start;
-       framepos_t end = selection->time[clicked_selection].end;
+       framepos_t start, end;
+       if (clicked_selection) {
+               start = selection->time[clicked_selection].start;
+               end = selection->time[clicked_selection].end;
+       } else {
+               start = selection->time.start();
+               end = selection->time.end_frame();
+       }
 
        TrackViewList ts = selection->tracks.filter_to_unique_playlists ();
        sort_track_selection (ts);
@@ -3951,7 +3991,7 @@ struct AutomationRecord {
  *  @param op Operation (Cut, Copy or Clear)
  */
 void
-Editor::cut_copy_points (Editing::CutCopyOp op, Evoral::MusicalTime earliest, bool midi)
+Editor::cut_copy_points (Editing::CutCopyOp op, Evoral::Beats earliest, bool midi)
 {
        if (selection->points.empty ()) {
                return;
@@ -3993,7 +4033,7 @@ Editor::cut_copy_points (Editing::CutCopyOp op, Evoral::MusicalTime earliest, bo
                        lists[al].copy->fast_simple_add ((*j)->when, (*j)->value);
                        if (midi) {
                                /* Update earliest MIDI start time in beats */
-                               earliest = std::min(earliest,  Evoral::MusicalTime((*j)->when));
+                               earliest = std::min(earliest, Evoral::Beats((*j)->when));
                        } else {
                                /* Update earliest session start time in frames */
                                start = std::min(start, (*i)->line().session_position(j));
@@ -4002,8 +4042,8 @@ Editor::cut_copy_points (Editing::CutCopyOp op, Evoral::MusicalTime earliest, bo
 
                /* Snap start time backwards, so copy/paste is snap aligned. */
                if (midi) {
-                       if (earliest == Evoral::MusicalTime::max()) {
-                               earliest = Evoral::MusicalTime();  // Weird... don't offset
+                       if (earliest == Evoral::Beats::max()) {
+                               earliest = Evoral::Beats();  // Weird... don't offset
                        }
                        earliest.round_down_to_beat();
                } else {
@@ -4056,7 +4096,7 @@ Editor::cut_copy_points (Editing::CutCopyOp op, Evoral::MusicalTime earliest, bo
 void
 Editor::cut_copy_midi (CutCopyOp op)
 {
-       Evoral::MusicalTime earliest = Evoral::MusicalTime::max();
+       Evoral::Beats earliest = Evoral::Beats::max();
        for (MidiRegionSelection::iterator i = selection->midi_regions.begin(); i != selection->midi_regions.end(); ++i) {
                MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
                if (mrv) {
@@ -4591,7 +4631,13 @@ Editor::duplicate_selection (float times)
                        continue;
                }
                playlist->clear_changes ();
-               playlist->duplicate (*ri, selection->time[clicked_selection].end, times);
+               framepos_t end;
+               if (clicked_selection) {
+                       end = selection->time[clicked_selection].end;
+               } else {
+                       end = selection->time.end_frame();
+               }
+               playlist->duplicate (*ri, end, times);
                _session->add_command (new StatefulDiffCommand (playlist));
 
                ++ri;
@@ -4896,14 +4942,14 @@ Editor::strip_region_silence ()
 Command*
 Editor::apply_midi_note_edit_op_to_region (MidiOperator& op, MidiRegionView& mrv)
 {
-       Evoral::Sequence<Evoral::MusicalTime>::Notes selected;
+       Evoral::Sequence<Evoral::Beats>::Notes selected;
        mrv.selection_as_notelist (selected, true);
 
-       vector<Evoral::Sequence<Evoral::MusicalTime>::Notes> v;
+       vector<Evoral::Sequence<Evoral::Beats>::Notes> v;
        v.push_back (selected);
 
-       framepos_t          pos_frames = mrv.midi_region()->position() - mrv.midi_region()->start();
-       Evoral::MusicalTime pos_beats  = _session->tempo_map().framewalk_to_beats(0, pos_frames);
+       framepos_t    pos_frames = mrv.midi_region()->position() - mrv.midi_region()->start();
+       Evoral::Beats pos_beats  = _session->tempo_map().framewalk_to_beats(0, pos_frames);
 
        return op (mrv.midi_region()->model(), pos_beats, v);
 }
@@ -5026,6 +5072,33 @@ Editor::legatize_regions (const RegionSelection& rs, bool shrink_only)
        apply_midi_note_edit_op (legatize, rs);
 }
 
+void
+Editor::transform_region ()
+{
+       if (_session) {
+               transform_regions(get_regions_from_selection_and_entered ());
+       }
+}
+
+void
+Editor::transform_regions (const RegionSelection& rs)
+{
+       if (rs.n_midi_regions() == 0) {
+               return;
+       }
+
+       TransformDialog* td = new TransformDialog();
+
+       td->present();
+       const int r = td->run();
+       td->hide();
+
+       if (r == Gtk::RESPONSE_OK) {
+               Transform transform(td->get());
+               apply_midi_note_edit_op(transform, rs);
+       }
+}
+
 void
 Editor::insert_patch_change (bool from_context)
 {
@@ -5043,7 +5116,7 @@ Editor::insert_patch_change (bool from_context)
        */
        MidiRegionView* first = dynamic_cast<MidiRegionView*> (rs.front ());
 
-       Evoral::PatchChange<Evoral::MusicalTime> empty (Evoral::MusicalTime(), 0, 0, 0);
+       Evoral::PatchChange<Evoral::Beats> empty (Evoral::Beats(), 0, 0, 0);
         PatchChangeDialog d (0, _session, empty, first->instrument_info(), Gtk::Stock::ADD);
 
        if (d.run() == RESPONSE_CANCEL) {
@@ -5721,20 +5794,25 @@ Editor::set_playhead_cursor ()
 void
 Editor::split_region ()
 {
+       //if a range is selected, separate it
        if ( !selection->time.empty()) {
                separate_regions_between (selection->time);
                return;
        }
 
-       RegionSelection rs = get_regions_from_selection_and_edit_point ();
+       //if no range was selected, try to find some regions to split
+       if (current_mouse_mode() == MouseObject) {  //don't try this for Internal Edit, Stretch, Draw, etc.
+       
+               RegionSelection rs = get_regions_from_selection_and_edit_point ();
 
-       framepos_t where = get_preferred_edit_position ();
+               framepos_t where = get_preferred_edit_position ();
 
-       if (rs.empty()) {
-               return;
-       }
+               if (rs.empty()) {
+                       return;
+               }
 
-       split_regions_at (where, rs);
+               split_regions_at (where, rs);
+       }
 }
 
 struct EditorOrderRouteSorter {
@@ -5808,64 +5886,28 @@ Editor::select_prev_route()
 
 void
 Editor::set_loop_from_selection (bool play)
-{
-       if (_session == 0 || selection->time.empty()) {
-               return;
-       }
-
-       framepos_t start = selection->time[clicked_selection].start;
-       framepos_t end = selection->time[clicked_selection].end;
-
-       set_loop_range (start, end,  _("set loop range from selection"));
-
-       if (play) {
-               _session->request_locate (start, true);
-               _session->request_play_loop (true);
-       }
-}
-
-void
-Editor::set_loop_from_edit_range (bool play)
 {
        if (_session == 0) {
                return;
        }
 
-       framepos_t start;
-       framepos_t end;
-
-       if (!get_edit_op_range (start, end)) {
+       framepos_t start, end;
+       if (!get_selection_extents ( start, end))
                return;
-       }
 
-       set_loop_range (start, end,  _("set loop range from edit range"));
+       set_loop_range (start, end,  _("set loop range from selection"));
 
        if (play) {
-               _session->request_locate (start, true);
-               _session->request_play_loop (true);
+               _session->request_play_loop (true, true);
        }
 }
 
 void
 Editor::set_loop_from_region (bool play)
 {
-       framepos_t start = max_framepos;
-       framepos_t end = 0;
-
-       RegionSelection rs = get_regions_from_selection_and_entered ();
-
-       if (rs.empty()) {
+       framepos_t start, end;
+       if (!get_selection_extents ( start, end))
                return;
-       }
-
-       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
-               if ((*i)->region()->position() < start) {
-                       start = (*i)->region()->position();
-               }
-               if ((*i)->region()->last_frame() + 1 > end) {
-                       end = (*i)->region()->last_frame() + 1;
-               }
-       }
 
        set_loop_range (start, end, _("set loop range from region"));
 
@@ -5878,12 +5920,13 @@ Editor::set_loop_from_region (bool play)
 void
 Editor::set_punch_from_selection ()
 {
-       if (_session == 0 || selection->time.empty()) {
+       if (_session == 0) {
                return;
        }
 
-       framepos_t start = selection->time[clicked_selection].start;
-       framepos_t end = selection->time[clicked_selection].end;
+       framepos_t start, end;
+       if (!get_selection_extents ( start, end))
+               return;
 
        set_punch_range (start, end,  _("set punch range from selection"));
 }
@@ -5891,15 +5934,16 @@ Editor::set_punch_from_selection ()
 void
 Editor::set_session_extents_from_selection ()
 {
-       if (_session == 0 || selection->time.empty()) {
+       if (_session == 0) {
                return;
        }
+       
+       framepos_t start, end;
+       if (!get_selection_extents ( start, end))
+               return;
 
        begin_reversible_command (_("set session start/stop from selection"));
 
-       framepos_t start = selection->time[clicked_selection].start;
-       framepos_t end = selection->time[clicked_selection].end;
-
        Location* loc;
        if ((loc = _session->locations()->session_range_location()) == 0) {
                _session->set_session_extents ( start, end );  // this will create a new session range;  no need for UNDO
@@ -5916,43 +5960,12 @@ Editor::set_session_extents_from_selection ()
        }
 }
 
-void
-Editor::set_punch_from_edit_range ()
-{
-       if (_session == 0) {
-               return;
-       }
-
-       framepos_t start;
-       framepos_t end;
-
-       if (!get_edit_op_range (start, end)) {
-               return;
-       }
-
-       set_punch_range (start, end,  _("set punch range from edit range"));
-}
-
 void
 Editor::set_punch_from_region ()
 {
-       framepos_t start = max_framepos;
-       framepos_t end = 0;
-
-       RegionSelection rs = get_regions_from_selection_and_entered ();
-
-       if (rs.empty()) {
+       framepos_t start, end;
+       if (!get_selection_extents ( start, end))
                return;
-       }
-
-       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
-               if ((*i)->region()->position() < start) {
-                       start = (*i)->region()->position();
-               }
-               if ((*i)->region()->last_frame() + 1 > end) {
-                       end = (*i)->region()->last_frame() + 1;
-               }
-       }
 
        set_punch_range (start, end, _("set punch range from region"));
 }
@@ -6904,7 +6917,7 @@ Editor::insert_time (
 }
 
 void
-Editor::fit_selected_tracks ()
+Editor::fit_selection ()
 {
         if (!selection->tracks.empty()) {
                 fit_tracks (selection->tracks);