Tempo ramps - grid now follows dragging meter section.
[ardour.git] / gtk2_ardour / editor_drag.cc
index 21cd043304928851f78a593c6d72d7892acd16a0..c1ece46fd8cedc5b21fc90f6100174c5901de766 100644 (file)
@@ -515,11 +515,10 @@ Drag::add_midi_region (MidiTimeAxisView* view, bool commit)
        if (_editor->session()) {
                const TempoMap& map (_editor->session()->tempo_map());
                framecnt_t pos = grab_frame();
-               const Meter& m = map.meter_at (pos);
                /* not that the frame rate used here can be affected by pull up/down which
                   might be wrong.
                */
-               framecnt_t len = m.frames_per_bar (map.tempo_at (pos), _editor->session()->frame_rate());
+               framecnt_t len = map.frame_at_beat (map.beat_at_frame (pos) + 1.0) - pos;
                return view->add_region (grab_frame(), len, commit);
        }
 
@@ -3123,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
@@ -3152,15 +3153,15 @@ MeterMarkerDrag::motion (GdkEvent* event, bool first_move)
                // leave or lose the original marker (leave if its a copy; lose if its
                // not, because we'll remove it from the map).
 
+               char name[64];
+               snprintf (name, sizeof(name), "%g/%g", _marker->meter().divisions_per_bar(), _marker->meter().note_divisor ());
+
                MeterSection section (_marker->meter());
 
                if (!section.movable()) {
                        return;
                }
 
-               char name[64];
-               snprintf (name, sizeof(name), "%g/%g", _marker->meter().divisions_per_bar(), _marker->meter().note_divisor ());
-
                _marker = new MeterMarker (
                        *_editor,
                        *_editor->meter_group,
@@ -3173,17 +3174,21 @@ MeterMarkerDrag::motion (GdkEvent* event, bool first_move)
                swap_grab (&_marker->the_item(), 0, GDK_CURRENT_TIME);
 
                if (!_copy) {
+                       _editor->begin_reversible_command (_("move meter mark"));
                        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);
+                       //map.remove_meter (section, true);
                }
+               _marker->hide();
        }
 
        framepos_t const pf = adjusted_current_frame (event);
 
        _marker->set_position (pf);
+       _editor->session()->tempo_map().gui_move_meter (_real_section, _marker->meter(), pf);
+
        show_verbose_cursor_time (pf);
 }
 
@@ -3211,17 +3216,25 @@ MeterMarkerDrag::finished (GdkEvent* event, bool movement_occurred)
        if (_copy == true) {
                _editor->begin_reversible_command (_("copy meter mark"));
                XMLNode &before = map.get_state();
-               map.add_meter (_marker->meter(), when);
+
+               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);
+               }
 
-               map.add_meter (_marker->meter(), when);
                XMLNode &after = map.get_state();
                _editor->session()->add_command(new MementoCommand<TempoMap>(map, before_state, &after));
                _editor->commit_reversible_command ();
@@ -3240,7 +3253,7 @@ MeterMarkerDrag::aborted (bool moved)
        if (moved) {
                TempoMap& map (_editor->session()->tempo_map());
                /* we removed it before, so add it back now */
-               map.add_meter (_marker->meter(), _marker->meter().frame());
+               map.add_meter (_marker->meter(), map.beat_at_frame (_marker->meter().frame()), _marker->meter().bbt());
                // delete the dummy marker we used for visual representation while moving.
                // a new visual marker will show up automatically.
                delete _marker;
@@ -3255,6 +3268,7 @@ TempoMarkerDrag::TempoMarkerDrag (Editor* e, ArdourCanvas::Item* i, bool c)
        DEBUG_TRACE (DEBUG::Drags, "New TempoMarkerDrag\n");
 
        _marker = reinterpret_cast<TempoMarker*> (_item->get_data ("marker"));
+       _real_section = &_marker->tempo();
        assert (_marker);
 }
 
@@ -3309,13 +3323,15 @@ TempoMarkerDrag::motion (GdkEvent* event, bool first_move)
                        TempoMap& map (_editor->session()->tempo_map());
                        /* get current state */
                        before_state = &map.get_state();
-                       /* remove the section while we drag it */
-                       map.remove_tempo (section, true);
                }
+               _marker->hide();
        }
 
-       framepos_t const pf = adjusted_current_frame (event);
+       framepos_t const pf = adjusted_current_frame (event, false);
+       Tempo const tp = _marker->tempo();
        _marker->set_position (pf);
+       _editor->session()->tempo_map().gui_move_tempo (_real_section, tp, pf);
+
        show_verbose_cursor_time (pf);
 }
 
@@ -3333,25 +3349,32 @@ TempoMarkerDrag::finished (GdkEvent* event, bool movement_occurred)
                return;
        }
 
-       motion (event, false);
+       //motion (event, false);
 
        TempoMap& map (_editor->session()->tempo_map());
-       framepos_t beat_time = map.round_to_beat (_marker->position(), RoundNearest);
-       Timecode::BBT_Time when;
-
-       map.bbt_time (beat_time, when);
 
        if (_copy == true) {
                _editor->begin_reversible_command (_("copy tempo mark"));
                XMLNode &before = map.get_state();
-               map.add_tempo (_marker->tempo(), when);
+
+               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 */
-               map.add_tempo (_marker->tempo(), when);
+               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 ();
@@ -3369,7 +3392,11 @@ TempoMarkerDrag::aborted (bool moved)
        if (moved) {
                TempoMap& map (_editor->session()->tempo_map());
                /* we removed it before, so add it back now */
-               map.add_tempo (_marker->tempo(), _marker->tempo().start());
+               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());
+               }
                // delete the dummy marker we used for visual representation while moving.
                // a new visual marker will show up automatically.
                delete _marker;
@@ -3762,12 +3789,14 @@ MarkerDrag::MarkerDrag (Editor* e, ArdourCanvas::Item* i)
        , _selection_changed (false)
 {
        DEBUG_TRACE (DEBUG::Drags, "New MarkerDrag\n");
-
+       Gtk::Window* toplevel = _editor->current_toplevel();
        _marker = reinterpret_cast<ArdourMarker*> (_item->get_data ("marker"));
+
        assert (_marker);
 
        _points.push_back (ArdourCanvas::Duple (0, 0));
-       _points.push_back (ArdourCanvas::Duple (0, physical_screen_height (_editor->get_window())));
+
+       _points.push_back (ArdourCanvas::Duple (0, toplevel ? physical_screen_height (toplevel->get_window()) : 900));
 }
 
 MarkerDrag::~MarkerDrag ()
@@ -3998,7 +4027,7 @@ MarkerDrag::motion (GdkEvent* event, bool)
                                if (move_both || (*x).move_both) {
                                        copy_location->set_start (new_start);
                                        copy_location->set_end (new_end);
-                               } else  if (new_start < copy_location->end()) {
+                               } else  if (new_start < copy_location->end()) {
                                        copy_location->set_start (new_start);
                                } else if (newframe > 0) {
                                        //_editor->snap_to (next, RoundUpAlways, true);
@@ -4126,9 +4155,9 @@ MarkerDrag::finished (GdkEvent* event, bool movement_occurred)
 }
 
 void
-MarkerDrag::aborted (bool movement_occured)
+MarkerDrag::aborted (bool movement_occurred)
 {
-       if (!movement_occured) {
+       if (!movement_occurred) {
                return;
        }
 
@@ -4382,9 +4411,9 @@ LineDrag::motion (GdkEvent* event, bool first_move)
 }
 
 void
-LineDrag::finished (GdkEvent* event, bool movement_occured)
+LineDrag::finished (GdkEvent* event, bool movement_occurred)
 {
-       if (movement_occured) {
+       if (movement_occurred) {
                motion (event, false);
                _line->end_drag (false, 0);
                _editor->commit_reversible_command ();
@@ -4714,31 +4743,37 @@ TimeFXDrag::motion (GdkEvent* event, bool)
 void
 TimeFXDrag::finished (GdkEvent* event, bool movement_occurred)
 {
-       if (!movement_occurred) {
-               return;
-       }
+       /* this may have been a single click, no drag. We still want the dialog
+          to show up in that case, so that the user can manually edit the
+          parameters for the timestretch.
+       */
 
-       motion (event, false);
+       float fraction = 1.0;
 
-       _primary->get_time_axis_view().hide_timestretch ();
+       if (movement_occurred) {
 
-       framepos_t adjusted_frame_pos = adjusted_current_frame (event);
+               motion (event, false);
 
-       if (adjusted_frame_pos < _primary->region()->position()) {
-               /* backwards drag of the left edge - not usable */
-               return;
-       }
+               _primary->get_time_axis_view().hide_timestretch ();
 
-       framecnt_t newlen = adjusted_frame_pos - _primary->region()->position();
+               framepos_t adjusted_frame_pos = adjusted_current_frame (event);
 
-       float percentage = (double) newlen / (double) _primary->region()->length();
+               if (adjusted_frame_pos < _primary->region()->position()) {
+                       /* backwards drag of the left edge - not usable */
+                       return;
+               }
+
+               framecnt_t newlen = adjusted_frame_pos - _primary->region()->position();
+
+               fraction = (double) newlen / (double) _primary->region()->length();
 
 #ifndef USE_RUBBERBAND
-       // Soundtouch uses percentage / 100 instead of normal (/ 1)
-       if (_primary->region()->data_type() == DataType::AUDIO) {
-               percentage = (float) ((double) newlen - (double) _primary->region()->length()) / ((double) newlen) * 100.0f;
-       }
+               // Soundtouch uses fraction / 100 instead of normal (/ 1)
+               if (_primary->region()->data_type() == DataType::AUDIO) {
+                       fraction = (float) ((double) newlen - (double) _primary->region()->length()) / ((double) newlen) * 100.0f;
+               }
 #endif
+       }
 
        if (!_editor->get_selection().regions.empty()) {
                /* primary will already be included in the selection, and edit
@@ -4747,7 +4782,7 @@ TimeFXDrag::finished (GdkEvent* event, bool movement_occurred)
                   selection.
                */
 
-               if (_editor->time_stretch (_editor->get_selection().regions, percentage) == -1) {
+               if (_editor->time_stretch (_editor->get_selection().regions, fraction) == -1) {
                        error << _("An error occurred while executing time stretch operation") << endmsg;
                }
        }
@@ -5139,7 +5174,7 @@ RangeMarkerBarDrag::RangeMarkerBarDrag (Editor* e, ArdourCanvas::Item* i, Operat
 
        _drag_rect = new ArdourCanvas::Rectangle (_editor->time_line_group,
                                                  ArdourCanvas::Rect (0.0, 0.0, 0.0,
-                                                                     physical_screen_height (_editor->get_window())));
+                                                                     physical_screen_height (_editor->current_toplevel()->get_window())));
        _drag_rect->hide ();
 
        _drag_rect->set_fill_color (UIConfiguration::instance().color ("range drag rect"));
@@ -5361,9 +5396,9 @@ RangeMarkerBarDrag::finished (GdkEvent* event, bool movement_occurred)
 }
 
 void
-RangeMarkerBarDrag::aborted (bool movement_occured)
+RangeMarkerBarDrag::aborted (bool movement_occurred)
 {
-       if (movement_occured) {
+       if (movement_occurred) {
                _drag_rect->hide ();
        }
 }