Tempo ramps - reinstate cross-dragging of music-locked meters, various bug fixes.
[ardour.git] / gtk2_ardour / editor_drag.cc
index b8339cc557f4e302ad1a841ac0cef6e1776126d4..a6867892537ac7ebc43bcac86a5045a12dd8323a 100644 (file)
@@ -3122,6 +3122,8 @@ MeterMarkerDrag::MeterMarkerDrag (Editor* e, ArdourCanvas::Item* i, bool c)
        DEBUG_TRACE (DEBUG::Drags, "New MeterMarkerDrag\n");
        _marker = reinterpret_cast<MeterMarker*> (_item->get_data ("marker"));
        assert (_marker);
+       _real_section = &_marker->meter();
+
 }
 
 void
@@ -3141,21 +3143,12 @@ void
 MeterMarkerDrag::motion (GdkEvent* event, bool first_move)
 {
        if (!_marker->meter().movable()) {
-               return;
+               //return;
        }
 
        if (first_move) {
 
-               // create a dummy marker for visual representation of moving the
-               // section, because whether its a copy or not, we're going to
-               // leave or lose the original marker (leave if its a copy; lose if its
-               // not, because we'll remove it from the map).
-
-               MeterSection section (_marker->meter());
-
-               if (!section.movable()) {
-                       return;
-               }
+               // create a dummy marker to catch events, then hide it.
 
                char name[64];
                snprintf (name, sizeof(name), "%g/%g", _marker->meter().divisions_per_bar(), _marker->meter().note_divisor ());
@@ -3170,20 +3163,65 @@ MeterMarkerDrag::motion (GdkEvent* event, bool first_move)
 
                /* use the new marker for the grab */
                swap_grab (&_marker->the_item(), 0, GDK_CURRENT_TIME);
+               _marker->hide();
+
+               TempoMap& map (_editor->session()->tempo_map());
+               /* get current state */
+               before_state = &map.get_state();
 
                if (!_copy) {
-                       TempoMap& map (_editor->session()->tempo_map());
-                       /* get current state */
-                       before_state = &map.get_state();
-                       /* remove the section while we drag it */
-                       map.remove_meter (section, true);
+                       _editor->begin_reversible_command (_("move meter mark"));
+               } else {
+                       _editor->begin_reversible_command (_("copy meter mark"));
+
+                       Timecode::BBT_Time bbt = _real_section->bbt();
+
+                       /* we can't add a meter where one currently exists */
+                       if (_real_section->frame() < adjusted_current_frame (event, false)) {
+                               ++bbt.bars;
+                       } else {
+                               --bbt.bars;
+                       }
+                       const double beat = map.bbt_to_beats (bbt);
+
+                       if (_real_section->position_lock_style() == AudioTime) {
+                               _real_section = map.add_meter (Meter (_marker->meter().divisions_per_bar(), _marker->meter().note_divisor())
+                                                              , map.frame_time (bbt), beat, bbt);
+                       } else {
+                               _real_section = map.add_meter (Meter (_marker->meter().divisions_per_bar(), _marker->meter().note_divisor()), beat, bbt);
+                       }
                }
        }
 
-       framepos_t const pf = adjusted_current_frame (event);
-
+       framepos_t const pf = adjusted_current_frame (event, false);
+       if (_marker->meter().position_lock_style() == MusicTime) {
+               TempoMap& map (_editor->session()->tempo_map());
+               Timecode::BBT_Time bbt;
+               map.bbt_time (pf, bbt);
+               /* round bbt to bars */
+               map.round_bbt (bbt, -1, RoundNearest);
+
+               if (Keyboard::modifier_state_contains (event->button.state, ArdourKeyboard::constraint_modifier ())) {
+                       /* adjust previous tempo to match meter frame */
+                       _editor->session()->tempo_map().gui_dilate_tempo (_real_section, pf);
+               } else if ((bbt.bars != _real_section->bbt().bars && pf > last_pointer_frame())
+                          || (bbt.bars < _real_section->bbt().bars && pf < last_pointer_frame())) {
+                       /* move meter beat-based */
+                       _editor->session()->tempo_map().gui_move_meter (_real_section, bbt);
+               }
+       } else {
+               /* AudioTime */
+               if (Keyboard::modifier_state_contains (event->button.state, ArdourKeyboard::constraint_modifier ())) {
+                       /* currently disabled in the lib for AudioTime */
+                       if (_real_section->movable()) {
+                               _editor->session()->tempo_map().gui_dilate_tempo (_real_section, pf);
+                       }
+               } else {
+                       _editor->session()->tempo_map().gui_move_meter (_real_section, pf);
+               }
+       }
        _marker->set_position (pf);
-       show_verbose_cursor_time (pf);
+       show_verbose_cursor_time (_real_section->frame());
 }
 
 void
@@ -3196,45 +3234,11 @@ MeterMarkerDrag::finished (GdkEvent* event, bool movement_occurred)
                return;
        }
 
-       if (!_marker->meter().movable()) {
-               return;
-       }
-
-       motion (event, false);
-
-       Timecode::BBT_Time when;
-
        TempoMap& map (_editor->session()->tempo_map());
-       map.bbt_time (_marker->position(), when);
-
-       if (_copy == true) {
-               _editor->begin_reversible_command (_("copy meter mark"));
-               XMLNode &before = map.get_state();
-
-               if (_marker->meter().position_lock_style() == AudioTime) {
-                       map.add_meter (_marker->meter(), _marker->position());
-               } else {
-                       map.add_meter (_marker->meter(), map.bbt_to_beats (when), when);
-               }
 
-               XMLNode &after = map.get_state();
-               _editor->session()->add_command(new MementoCommand<TempoMap>(map, &before, &after));
-               _editor->commit_reversible_command ();
-
-       } else {
-               _editor->begin_reversible_command (_("move meter mark"));
-
-               /* we removed it before, so add it back now */
-               if (_marker->meter().position_lock_style() == AudioTime) {
-                       map.add_meter (_marker->meter(), _marker->position());
-               } else {
-                       map.add_meter (_marker->meter(), map.beat_at_frame (_marker->position()), when);
-               }
-
-               XMLNode &after = map.get_state();
-               _editor->session()->add_command(new MementoCommand<TempoMap>(map, before_state, &after));
-               _editor->commit_reversible_command ();
-       }
+       XMLNode &after = map.get_state();
+       _editor->session()->add_command(new MementoCommand<TempoMap>(map, before_state, &after));
+       _editor->commit_reversible_command ();
 
        // delete the dummy marker we used for visual representation while moving.
        // a new visual marker will show up automatically.
@@ -3245,11 +3249,8 @@ void
 MeterMarkerDrag::aborted (bool moved)
 {
        _marker->set_position (_marker->meter().frame ());
-
        if (moved) {
-               TempoMap& map (_editor->session()->tempo_map());
-               /* we removed it before, so add it back now */
-               map.add_meter (_marker->meter(), map.beat_at_frame (_marker->meter().frame()), _marker->meter().bbt());
+               _editor->session()->tempo_map().set_state (*before_state, Stateful::current_state_version);
                // delete the dummy marker we used for visual representation while moving.
                // a new visual marker will show up automatically.
                delete _marker;
@@ -3265,6 +3266,7 @@ TempoMarkerDrag::TempoMarkerDrag (Editor* e, ArdourCanvas::Item* i, bool c)
 
        _marker = reinterpret_cast<TempoMarker*> (_item->get_data ("marker"));
        _real_section = &_marker->tempo();
+       _movable = _real_section->movable();
        assert (_marker);
 }
 
@@ -3272,7 +3274,11 @@ void
 TempoMarkerDrag::start_grab (GdkEvent* event, Gdk::Cursor* cursor)
 {
        Drag::start_grab (event, cursor);
-       show_verbose_cursor_time (adjusted_current_frame (event));
+       if (!_real_section->active()) {
+               show_verbose_cursor_text (_("inactive"));
+       } else {
+               show_verbose_cursor_time (adjusted_current_frame (event));
+       }
 }
 
 void
@@ -3284,19 +3290,12 @@ TempoMarkerDrag::setup_pointer_frame_offset ()
 void
 TempoMarkerDrag::motion (GdkEvent* event, bool first_move)
 {
-       if (!_marker->tempo().movable()) {
+       if (!_real_section->active()) {
                return;
        }
-
        if (first_move) {
 
-               // create a dummy marker for visual representation of moving the
-               // section, because whether its a copy or not, we're going to
-               // leave or lose the original marker (leave if its a copy; lose if its
-               // not, because we'll remove it from the map).
-
-               // create a dummy marker for visual representation of moving the copy.
-               // The actual copying is not done before we reach the finish callback.
+               // mvc drag - create a dummy marker to catch events, hide it.
 
                char name[64];
                snprintf (name, sizeof (name), "%.2f", _marker->tempo().beats_per_minute());
@@ -3313,28 +3312,139 @@ TempoMarkerDrag::motion (GdkEvent* event, bool first_move)
 
                /* use the new marker for the grab */
                swap_grab (&_marker->the_item(), 0, GDK_CURRENT_TIME);
+               _marker->hide();
 
+               TempoMap& map (_editor->session()->tempo_map());
+               /* get current state */
+               before_state = &map.get_state();
                if (!_copy) {
                        _editor->begin_reversible_command (_("move tempo mark"));
-                       TempoMap& map (_editor->session()->tempo_map());
-                       /* get current state */
-                       before_state = &map.get_state();
+               } else {
+                       _editor->begin_reversible_command (_("copy tempo mark"));
+                       framepos_t frame;
+                       bool use_snap = false;
+
+                       if (!_editor->snap_musical()) {
+                               frame = adjusted_current_frame (event);
+                       } else {
+                               frame = adjusted_current_frame (event, false);
+                               if (ArdourKeyboard::indicates_snap (event->button.state)) {
+                                       if (_editor->snap_mode() == Editing::SnapOff) {
+                                               use_snap = true;
+                                       } else {
+                                               use_snap = false;
+                                       }
+                               } else {
+                                       if (_editor->snap_mode() == Editing::SnapOff) {
+                                               use_snap = false;
+                                       } else {
+                                               use_snap = true;
+                                       }
+                               }
+                       }
+
+                       Timecode::BBT_Time bbt;
+                       map.bbt_time (frame, bbt);
+
+                       /* add new tempo section to map, ensuring we don't refer to existing tempos for snap */
+
+                       if (_real_section->position_lock_style() == MusicTime) {
+                               if (use_snap && _editor->snap_type() == SnapToBar) {
+                                       map.round_bbt (bbt, -1, (frame > _real_section->frame()) ? RoundUpMaybe : RoundDownMaybe);
+                               } else if (use_snap) {
+                                       map.round_bbt (bbt, _editor->get_grid_beat_divisions (0), RoundNearest);
+                               }
+                               double const pulse = map.predict_tempo_pulse (_real_section, map.frame_time (bbt));
+                               _real_section = map.add_tempo (_marker->tempo(), pulse, _real_section->type());
+                       } else {
+                               if (use_snap && _editor->snap_type() == SnapToBar) {
+                                       map.round_bbt (bbt, -1, (frame > _real_section->frame()) ? RoundUpMaybe : RoundDownMaybe);
+                               } else if (use_snap) {
+                                       map.round_bbt (bbt, _editor->get_grid_beat_divisions (0), RoundNearest);
+                               }
+                               if (use_snap) {
+                                       frame = map.predict_tempo_frame (_real_section, bbt);
+                               }
+                               _real_section = map.add_tempo (_marker->tempo(), frame, _real_section->type());
+                       }
                }
-               _marker->hide();
+
        }
 
-       framepos_t const pf = adjusted_current_frame (event, false);
-       double const baf = _editor->session()->tempo_map().beat_at_frame (pf);
+       framepos_t pf;
 
-       _marker->set_position (adjusted_current_frame (event, false));
-       _editor->session()->tempo_map().gui_set_tempo_frame (*_real_section, pf, baf);
+       if (Keyboard::modifier_state_contains (event->button.state, ArdourKeyboard::constraint_modifier ())) {
+               double new_bpm = _real_section->beats_per_minute() + ((last_pointer_y() - current_pointer_y()) / 5.0);
+               _editor->session()->tempo_map().gui_change_tempo (_real_section, Tempo (new_bpm, _real_section->note_type()));
+               stringstream strs;
+               strs << new_bpm;
+               show_verbose_cursor_text (strs.str());
+       } else if (_movable && !_real_section->locked_to_meter()) {
+               if (!_editor->snap_musical()) {
+                       /* snap normally (this is not self-referential).*/
+                       pf = adjusted_current_frame (event);
+               } else {
+                       /* but this is.
+                          we can't use the map for anything related to tempo,
+                          so we round bbt using meters, which have no dependency
+                          on pulse for this kind of thing.
+                       */
+                       bool use_snap;
+                       TempoMap& map (_editor->session()->tempo_map());
 
-       show_verbose_cursor_time (pf);
+                       pf = adjusted_current_frame (event, false);
+                       if (ArdourKeyboard::indicates_snap (event->button.state)) {
+                               if (_editor->snap_mode() == Editing::SnapOff) {
+                                       use_snap = true;
+                               } else {
+                                       use_snap = false;
+                               }
+                       } else {
+                               if (_editor->snap_mode() == Editing::SnapOff) {
+                                       use_snap = false;
+                               } else {
+                                       use_snap = true;
+                               }
+                       }
+
+                       Timecode::BBT_Time when;
+                       map.bbt_time (pf, when);
+
+                       if (_real_section->position_lock_style() == MusicTime) {
+
+                               const double pulse = map.predict_tempo_pulse (_real_section, pf);
+                               when = map.pulse_to_bbt (pulse);
+                               if (use_snap && _editor->snap_type() == SnapToBar) {
+                                       map.round_bbt (when, -1, (pf > _real_section->frame()) ? RoundUpMaybe : RoundDownMaybe);
+                               } else if (use_snap) {
+                                       map.round_bbt (when, _editor->get_grid_beat_divisions (0), RoundNearest);
+                               }
+                               const double beat = map.bbt_to_beats (when);
+                               map.gui_move_tempo_beat (_real_section, beat);
+                       } else {
+                               if (use_snap && _editor->snap_type() == SnapToBar) {
+                                       map.round_bbt (when, -1, (pf > _real_section->frame()) ? RoundUpMaybe : RoundDownMaybe);
+                               } else if (use_snap) {
+                                       map.round_bbt (when, _editor->get_grid_beat_divisions (0), RoundNearest);
+                               }
+                               if (use_snap) {
+                                       pf = map.predict_tempo_frame (_real_section, when);
+                               }
+                               map.gui_move_tempo_frame (_real_section, pf);
+                       }
+               }
+
+               show_verbose_cursor_time (_real_section->frame());
+       }
+       _marker->set_position (pf);
 }
 
 void
 TempoMarkerDrag::finished (GdkEvent* event, bool movement_occurred)
 {
+       if (!_real_section->active()) {
+               return;
+       }
        if (!movement_occurred) {
                if (was_double_click()) {
                        _editor->edit_tempo_marker (*_marker);
@@ -3342,40 +3452,11 @@ TempoMarkerDrag::finished (GdkEvent* event, bool movement_occurred)
                return;
        }
 
-       if (!_marker->tempo().movable()) {
-               return;
-       }
-
-       //motion (event, false);
-
        TempoMap& map (_editor->session()->tempo_map());
 
-       if (_copy == true) {
-               _editor->begin_reversible_command (_("copy tempo mark"));
-               XMLNode &before = map.get_state();
-
-               if (_marker->tempo().position_lock_style() == MusicTime) {
-                       map.add_tempo (_marker->tempo(), _real_section->beat(), _marker->tempo().type());
-               } else {
-                       map.add_tempo (_marker->tempo(), _real_section->frame(), _marker->tempo().type());
-               }
-
-               XMLNode &after = map.get_state();
-               _editor->session()->add_command (new MementoCommand<TempoMap>(map, &before, &after));
-               _editor->commit_reversible_command ();
-
-       } else {
-               /* we removed it before, so add it back now */
-               if (_marker->tempo().position_lock_style() == MusicTime) {
-                       map.replace_tempo (*_real_section, _marker->tempo().beats_per_minute() , _real_section->beat(), _marker->tempo().type());
-               } else {
-                       map.replace_tempo (*_real_section, _marker->tempo().beats_per_minute() , _real_section->frame(), _marker->tempo().type());
-               }
-
-               XMLNode &after = map.get_state();
-               _editor->session()->add_command (new MementoCommand<TempoMap>(map, before_state, &after));
-               _editor->commit_reversible_command ();
-       }
+       XMLNode &after = map.get_state();
+       _editor->session()->add_command (new MementoCommand<TempoMap>(map, before_state, &after));
+       _editor->commit_reversible_command ();
 
        // delete the dummy marker we used for visual representation while moving.
        // a new visual marker will show up automatically.
@@ -3388,12 +3469,7 @@ TempoMarkerDrag::aborted (bool moved)
        _marker->set_position (_marker->tempo().frame());
        if (moved) {
                TempoMap& map (_editor->session()->tempo_map());
-               /* we removed it before, so add it back now */
-               if (_marker->tempo().position_lock_style() == MusicTime) {
-                       map.add_tempo (_marker->tempo(), _marker->tempo().beat(), _marker->tempo().type());
-               } else {
-                       map.add_tempo (_marker->tempo(), _marker->tempo().frame(), _marker->tempo().type());
-               }
+               map.set_state (*before_state, Stateful::current_state_version);
                // delete the dummy marker we used for visual representation while moving.
                // a new visual marker will show up automatically.
                delete _marker;