Allow non-musical snap when dragging bbt ruler.
[ardour.git] / gtk2_ardour / editor_drag.cc
index 16c1862dd8fee3050f04df31135274ae0d2eabc2..f209aed59da1b32141c0536163aafd6d2ad66426 100644 (file)
@@ -3115,8 +3115,10 @@ TrimDrag::setup_pointer_frame_offset ()
 }
 
 MeterMarkerDrag::MeterMarkerDrag (Editor* e, ArdourCanvas::Item* i, bool c)
-       : Drag (e, i),
-         _copy (c)
+       : Drag (e, i)
+       , _copy (c)
+       , _old_snap_type (e->snap_type())
+       , _old_snap_mode (e->snap_mode())
        , before_state (0)
 {
        DEBUG_TRACE (DEBUG::Drags, "New MeterMarkerDrag\n");
@@ -3143,7 +3145,6 @@ void
 MeterMarkerDrag::motion (GdkEvent* event, bool first_move)
 {
        if (first_move) {
-
                // create a dummy marker to catch events, then hide it.
 
                char name[64];
@@ -3178,11 +3179,16 @@ MeterMarkerDrag::motion (GdkEvent* event, bool first_move)
                        } else {
                                --bbt.bars;
                        }
-                       const double beat = map.bbt_to_beats (bbt);
+                       const double beat = map.beat_at_bbt (bbt);
                        _real_section = map.add_meter (Meter (_marker->meter().divisions_per_bar(), _marker->meter().note_divisor())
-                                                      , beat, bbt, map.frame_time (bbt), _real_section->position_lock_style());
+                                                      , beat, bbt, map.frame_at_bbt (bbt), _real_section->position_lock_style());
 
                }
+               /* only snap to bars. leave snap mode alone for audio locked meters.*/
+               if (_real_section->position_lock_style() != AudioTime) {
+                       _editor->set_snap_to (SnapToBar);
+                       _editor->set_snap_mode (SnapNormal);
+               }
        }
 
        framepos_t pf = adjusted_current_frame (event);
@@ -3194,8 +3200,9 @@ MeterMarkerDrag::motion (GdkEvent* event, bool first_move)
 
        _editor->session()->tempo_map().gui_move_meter (_real_section, pf);
 
-       setup_pointer_frame_offset ();
-       _marker->set_position (pf);
+       /* fake marker meeds to stay under the mouse, unlike the real one. */
+       _marker->set_position (adjusted_current_frame (event, false));
+
        show_verbose_cursor_time (_real_section->frame());
 }
 
@@ -3209,6 +3216,10 @@ MeterMarkerDrag::finished (GdkEvent* event, bool movement_occurred)
                return;
        }
 
+       /* reinstate old snap setting */
+       _editor->set_snap_to (_old_snap_type);
+       _editor->set_snap_mode (_old_snap_mode);
+
        TempoMap& map (_editor->session()->tempo_map());
 
        XMLNode &after = map.get_state();
@@ -3225,6 +3236,10 @@ MeterMarkerDrag::aborted (bool moved)
 {
        _marker->set_position (_marker->meter().frame ());
        if (moved) {
+               /* reinstate old snap setting */
+               _editor->set_snap_to (_old_snap_type);
+               _editor->set_snap_mode (_old_snap_mode);
+
                _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.
@@ -3293,62 +3308,24 @@ TempoMarkerDrag::motion (GdkEvent* event, bool first_move)
                TempoMap& map (_editor->session()->tempo_map());
                /* get current state */
                before_state = &map.get_state();
+
                if (!_copy) {
                        _editor->begin_reversible_command (_("move tempo mark"));
-               } 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);
+               } else {
+                       const framepos_t frame = adjusted_current_frame (event) + 1;
 
-                       /* add new tempo section to map, ensuring we don't refer to existing tempos for snap */
+                       _editor->begin_reversible_command (_("copy tempo mark"));
 
                        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_position (_real_section, bbt).first;
-                               _real_section = map.add_tempo (_marker->tempo(), pulse, 0, _real_section->type(), MusicTime);
+                               _real_section = map.add_tempo (_marker->tempo(), map.pulse_at_frame (frame), 0, _real_section->type(), MusicTime);
                        } 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_position (_real_section, bbt).second;
-                               }
                                _real_section = map.add_tempo (_marker->tempo(), 0.0, frame, _real_section->type(), AudioTime);
                        }
                }
 
        }
 
-       framepos_t pf;
-
        if (Keyboard::modifier_state_contains (event->button.state, ArdourKeyboard::constraint_modifier ())) {
                /* use vertical movement to alter tempo .. should be log */
                double new_bpm = _real_section->beats_per_minute() + ((last_pointer_y() - current_pointer_y()) / 5.0);
@@ -3359,45 +3336,34 @@ TempoMarkerDrag::motion (GdkEvent* event, bool first_move)
                show_verbose_cursor_text (strs.str());
 
        } else if (_movable && !_real_section->locked_to_meter()) {
+               TempoMap& map (_editor->session()->tempo_map());
+               const bool was_music = _real_section->position_lock_style() == MusicTime;
 
-               if (!_editor->snap_musical()) {
-                       /* snap normally (this is not self-referential).*/
-                       pf = adjusted_current_frame (event);
+               const framepos_t 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.
-                       */
-                       TempoMap& map (_editor->session()->tempo_map());
-                       Timecode::BBT_Time when;
-                       bool use_snap;
+               if (!_editor->snap_musical()) {
 
-                       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;
-                               }
+                       if (was_music) {
+                               _real_section->set_position_lock_style (AudioTime);
                        }
 
-                       pf = adjusted_current_frame (event);
-                       map.bbt_time (pf, when);
+                       map.gui_move_tempo (_real_section, pf);
 
-                       if (use_snap && _editor->snap_type() == SnapToBar) {
-                               map.round_bbt (when, -1, (pf > _real_section->frame()) ? RoundUpMaybe : RoundDownMaybe);
+                       if (was_music) {
+                               _real_section->set_position_lock_style (MusicTime);
+                       }
 
+               } else {
+
+                       if (!was_music) {
+                               _real_section->set_position_lock_style (MusicTime);
                        }
-                       const pair<double, framepos_t> future_pos = map.predict_tempo_position (_real_section, when);
-                       map.gui_move_tempo (_real_section, future_pos);
 
+                       map.gui_move_tempo (_real_section, pf);
+
+                       if (!was_music) {
+                               _real_section->set_position_lock_style (AudioTime);
+                       }
                }
 
                show_verbose_cursor_time (_real_section->frame());
@@ -3406,7 +3372,7 @@ TempoMarkerDrag::motion (GdkEvent* event, bool first_move)
        /* this has moved the bar lines themselves, so recalibrate the offset */
        setup_pointer_frame_offset();
 
-       _marker->set_position (pf);
+       _marker->set_position (adjusted_current_frame (event, false));
 }
 
 void
@@ -3449,7 +3415,6 @@ TempoMarkerDrag::aborted (bool moved)
 BBTRulerDrag::BBTRulerDrag (Editor* e, ArdourCanvas::Item* i)
        : Drag (e, i)
        , _pulse (0.0)
-       , _beat (0.0)
        , _tempo (0)
        , before_state (0)
 {
@@ -3461,12 +3426,11 @@ void
 BBTRulerDrag::start_grab (GdkEvent* event, Gdk::Cursor* cursor)
 {
        Drag::start_grab (event, cursor);
-
        TempoMap& map (_editor->session()->tempo_map());
+       _tempo = const_cast<TempoSection*> (&map.tempo_section_at_frame (raw_grab_frame()));
        ostringstream sstr;
 
-       _tempo = const_cast<TempoSection*> (&map.tempo_section_at (raw_grab_frame()));
-       sstr << "^" << fixed << setprecision(3) << map.tempo_at (adjusted_current_frame (event)).beats_per_minute() << "\n";
+       sstr << "^" << fixed << setprecision(3) << map.tempo_at_frame (adjusted_current_frame (event)).beats_per_minute() << "\n";
        sstr << "<" << fixed << setprecision(3) << _tempo->beats_per_minute();
        show_verbose_cursor_text (sstr.str());
        finished (event, false);
@@ -3478,17 +3442,22 @@ BBTRulerDrag::setup_pointer_frame_offset ()
        TempoMap& map (_editor->session()->tempo_map());
        const double beat_at_frame = map.beat_at_frame (raw_grab_frame());
        const uint32_t divisions = _editor->get_grid_beat_divisions (0);
+       double beat = 0.0;
+
        if (divisions > 0) {
-               _beat = floor (beat_at_frame) + (floor (((beat_at_frame - floor (beat_at_frame)) * divisions)) / divisions);
+               beat = floor (beat_at_frame) + (floor (((beat_at_frame - floor (beat_at_frame)) * divisions)) / divisions);
        } else {
                /* while it makes some sense for the user to determine the division to 'grab',
                   grabbing a bar often leads to confusing results wrt the actual tempo section being altered
                   and the result over steep tempo curves. Use sixteenths.
                */
-               _beat = floor (beat_at_frame) + (floor (((beat_at_frame - floor (beat_at_frame)) * 4)) / 4);
+               beat = floor (beat_at_frame) + (floor (((beat_at_frame - floor (beat_at_frame)) * 4)) / 4);
        }
-       _pulse = map.pulse_at_beat (_beat);
-       _pointer_frame_offset = raw_grab_frame() - map.frame_at_beat (_beat);
+
+       _pulse = map.pulse_at_beat (beat);
+
+       _pointer_frame_offset = raw_grab_frame() - map.frame_at_pulse (_pulse);
+
 }
 
 void
@@ -3502,14 +3471,20 @@ BBTRulerDrag::motion (GdkEvent* event, bool first_move)
                _editor->begin_reversible_command (_("dilate tempo"));
        }
 
-       framepos_t const pf = adjusted_current_frame (event, false);
+       framepos_t pf;
+
+       if (_editor->snap_musical()) {
+               pf = adjusted_current_frame (event, false);
+       } else {
+               pf = adjusted_current_frame (event);
+       }
 
        if (Keyboard::modifier_state_contains (event->button.state, ArdourKeyboard::constraint_modifier())) {
                /* adjust previous tempo to match pointer frame */
                _editor->session()->tempo_map().gui_dilate_tempo (_tempo, map.frame_at_pulse (_pulse), pf, _pulse);
        }
        ostringstream sstr;
-       sstr << "^" << fixed << setprecision(3) << map.tempo_at (pf).beats_per_minute() << "\n";
+       sstr << "^" << fixed << setprecision(3) << map.tempo_at_frame (pf).beats_per_minute() << "\n";
        sstr << "<" << fixed << setprecision(3) << _tempo->beats_per_minute();
        show_verbose_cursor_text (sstr.str());
 }