Revert "Revert "patch for waflib internals to allow tarball to be created even when...
[ardour.git] / gtk2_ardour / editor_drag.cc
index c95979514ffb85264fe791ffae2cb44aa0dd8baf..0c046e56ab1eb188144e6edf6882619417760672 100644 (file)
@@ -44,7 +44,7 @@
 #include "canvas/scroll_group.h"
 
 #include "editor.h"
-#include "i18n.h"
+#include "pbd/i18n.h"
 #include "keyboard.h"
 #include "audio_region_view.h"
 #include "automation_region_view.h"
@@ -237,6 +237,7 @@ Drag::Drag (Editor* e, ArdourCanvas::Item* i, bool trackview_only)
        , _grab_frame (0)
        , _last_pointer_frame (0)
        , _snap_delta (0)
+       , _constraint_pressed (false)
 {
 
 }
@@ -261,6 +262,7 @@ Drag::start_grab (GdkEvent* event, Gdk::Cursor *cursor)
 {
 
        /* we set up x/y dragging constraints on first move */
+       _constraint_pressed = ArdourKeyboard::indicates_constraint (event->button.state);
 
        _raw_grab_frame = _editor->canvas_event_sample (event, &_grab_x, &_grab_y);
 
@@ -420,14 +422,14 @@ Drag::motion_handler (GdkEvent* event, bool from_autoscroll)
                                if (Config->get_edit_mode() != Lock) {
                                        if (event->motion.state & Gdk::BUTTON2_MASK) {
                                                // if dragging with button2, the motion is x constrained, with constraint modifier it is y constrained
-                                               if (Keyboard::modifier_state_equals (event->button.state, ArdourKeyboard::constraint_modifier ())) {
+                                               if (_constraint_pressed) {
                                                        _x_constrained = false;
                                                        _y_constrained = true;
                                                } else {
                                                        _x_constrained = true;
                                                        _y_constrained = false;
                                                }
-                                       } else if (Keyboard::modifier_state_equals (event->button.state, ArdourKeyboard::constraint_modifier ())) {
+                                       } else if (_constraint_pressed) {
                                                // if dragging normally, the motion is constrained to the first direction of movement.
                                                if (_initially_vertical) {
                                                        _x_constrained = true;
@@ -510,7 +512,7 @@ Drag::show_verbose_cursor_text (string const & text)
 }
 
 boost::shared_ptr<Region>
-Drag::add_midi_region (MidiTimeAxisView* view, bool commit)
+Drag::add_midi_region (MidiTimeAxisView* view, bool commit, const int32_t sub_num)
 {
        if (_editor->session()) {
                const TempoMap& map (_editor->session()->tempo_map());
@@ -518,8 +520,8 @@ Drag::add_midi_region (MidiTimeAxisView* view, bool commit)
                /* not that the frame rate used here can be affected by pull up/down which
                   might be wrong.
                */
-               framecnt_t len = map.frame_at_beat (map.beat_at_frame (pos) + 1.0) - pos;
-               return view->add_region (grab_frame(), len, commit);
+               framecnt_t len = map.frame_at_beat (max (0.0, map.beat_at_frame (pos)) + 1.0) - pos;
+               return view->add_region (grab_frame(), len, commit, sub_num);
        }
 
        return boost::shared_ptr<Region>();
@@ -527,7 +529,7 @@ Drag::add_midi_region (MidiTimeAxisView* view, bool commit)
 
 struct PresentationInfoTimeAxisViewSorter {
        bool operator() (TimeAxisView* a, TimeAxisView* b) {
-               return a->presentation_info().order() < b->presentation_info().order();
+               return a->stripable()->presentation_info().order() < b->stripable()->presentation_info().order();
        }
 };
 
@@ -1256,8 +1258,8 @@ RegionMoveDrag::motion (GdkEvent* event, bool first_move)
                        MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(rv);
 
                        const boost::shared_ptr<const Region> original = rv->region();
-                       boost::shared_ptr<Region> region_copy = RegionFactory::create (original, true);
-                       region_copy->set_position (original->position());
+                       boost::shared_ptr<Region> region_copy = RegionFactory::create (original, true
+                                                                                      , _editor->get_grid_music_divisions (event->button.state));
                        /* need to set this so that the drop zone code can work. This doesn't
                           actually put the region into the playlist, but just sets a weak pointer
                           to it.
@@ -1364,7 +1366,8 @@ RegionMoveDrag::finished (GdkEvent* ev, bool movement_occurred)
                finished_copy (
                        changed_position,
                        changed_tracks,
-                       drag_delta
+                       drag_delta,
+                       ev->button.state
                        );
 
        } else {
@@ -1372,7 +1375,8 @@ RegionMoveDrag::finished (GdkEvent* ev, bool movement_occurred)
                finished_no_copy (
                        changed_position,
                        changed_tracks,
-                       drag_delta
+                       drag_delta,
+                       ev->button.state
                        );
 
        }
@@ -1395,22 +1399,22 @@ RegionMoveDrag::create_destination_time_axis (boost::shared_ptr<Region> region,
                                output_chan =  _editor->session()->master_out()->n_inputs().n_audio();
                        }
                        audio_tracks = _editor->session()->new_audio_track (region->n_channels(), output_chan, 0, 1, region->name(), PresentationInfo::max_order);
-                       RouteTimeAxisView* rtav = _editor->axis_view_from_route (audio_tracks.front());
-                       if (rtav) {
-                               rtav->set_height (original->current_height());
+                       TimeAxisView* tav =_editor->axis_view_from_stripable (audio_tracks.front());
+                       if (tav) {
+                               tav->set_height (original->current_height());
                        }
-                       return rtav;
+                       return dynamic_cast<RouteTimeAxisView*>(tav);
                } else {
                        ChanCount one_midi_port (DataType::MIDI, 1);
                        list<boost::shared_ptr<MidiTrack> > midi_tracks;
                        midi_tracks = _editor->session()->new_midi_track (one_midi_port, one_midi_port, boost::shared_ptr<ARDOUR::PluginInfo>(),
                                                                          (ARDOUR::Plugin::PresetRecord*) 0,
                                                                          (ARDOUR::RouteGroup*) 0, 1, region->name(), PresentationInfo::max_order);
-                       RouteTimeAxisView* rtav = _editor->axis_view_from_route (midi_tracks.front());
-                       if (rtav) {
-                               rtav->set_height (original->current_height());
+                       TimeAxisView* tav = _editor->axis_view_from_stripable (midi_tracks.front());
+                       if (tav) {
+                               tav->set_height (original->current_height());
                        }
-                       return rtav;
+                       return dynamic_cast<RouteTimeAxisView*> (tav);
                }
        } catch (...) {
                error << _("Could not create new track after region placed in the drop zone") << endmsg;
@@ -1419,7 +1423,7 @@ RegionMoveDrag::create_destination_time_axis (boost::shared_ptr<Region> region,
 }
 
 void
-RegionMoveDrag::finished_copy (bool const changed_position, bool const /*changed_tracks*/, framecnt_t const drag_delta)
+RegionMoveDrag::finished_copy (bool const changed_position, bool const /*changed_tracks*/, framecnt_t const drag_delta, int32_t const ev_state)
 {
        RegionSelection new_views;
        PlaylistSet modified_playlists;
@@ -1476,7 +1480,9 @@ RegionMoveDrag::finished_copy (bool const changed_position, bool const /*changed
                }
 
                if (dest_rtv != 0) {
-                       RegionView* new_view = insert_region_into_playlist (i->view->region(), dest_rtv, i->layer, where, modified_playlists);
+                       RegionView* new_view = insert_region_into_playlist (i->view->region(), dest_rtv, i->layer, where,
+                                                                           modified_playlists, _editor->get_grid_music_divisions (ev_state));
+
                        if (new_view != 0) {
                                new_views.push_back (new_view);
                        }
@@ -1510,7 +1516,8 @@ void
 RegionMoveDrag::finished_no_copy (
        bool const changed_position,
        bool const changed_tracks,
-       framecnt_t const drag_delta
+       framecnt_t const drag_delta,
+       int32_t const ev_state
        )
 {
        RegionSelection new_views;
@@ -1571,7 +1578,8 @@ RegionMoveDrag::finished_no_copy (
                        /* insert into new playlist */
 
                        RegionView* new_view = insert_region_into_playlist (
-                               RegionFactory::create (rv->region (), true), dest_rtv, dest_layer, where, modified_playlists
+                               RegionFactory::create (rv->region (), true), dest_rtv, dest_layer, where,
+                               modified_playlists, _editor->get_grid_music_divisions (ev_state)
                                );
 
                        if (new_view == 0) {
@@ -1631,7 +1639,7 @@ RegionMoveDrag::finished_no_copy (
                                playlist->freeze ();
                        }
 
-                       rv->region()->set_position (where);
+                       rv->region()->set_position (where, _editor->get_grid_music_divisions (ev_state));
                        _editor->session()->add_command (new StatefulDiffCommand (rv->region()));
                }
 
@@ -1734,7 +1742,8 @@ RegionMoveDrag::insert_region_into_playlist (
        RouteTimeAxisView* dest_rtv,
        layer_t dest_layer,
        framecnt_t where,
-       PlaylistSet& modified_playlists
+       PlaylistSet& modified_playlists,
+       const int32_t sub_num
        )
 {
        boost::shared_ptr<Playlist> dest_playlist = dest_rtv->playlist ();
@@ -1751,8 +1760,7 @@ RegionMoveDrag::insert_region_into_playlist (
        if (r.second) {
                dest_playlist->clear_changes ();
        }
-
-       dest_playlist->add_region (region, where);
+       dest_playlist->add_region (region, where, 1.0, false, sub_num);
 
        if (dest_rtv->view()->layer_display() == Stacked || dest_rtv->view()->layer_display() == Expanded) {
                dest_playlist->set_layer (region, dest_layer);
@@ -1876,7 +1884,7 @@ RegionInsertDrag::RegionInsertDrag (Editor* e, boost::shared_ptr<Region> r, Rout
 }
 
 void
-RegionInsertDrag::finished (GdkEvent *, bool)
+RegionInsertDrag::finished (GdkEvent * event, bool)
 {
        int pos = _views.front().time_axis_view;
        assert(pos >= 0 && pos < (int)_time_axis_views.size());
@@ -2303,7 +2311,7 @@ RegionCreateDrag::motion (GdkEvent* event, bool first_move)
 {
        if (first_move) {
                _editor->begin_reversible_command (_("create region"));
-               _region = add_midi_region (_view, false);
+               _region = add_midi_region (_view, false, _editor->get_grid_music_divisions (event->button.state));
                _view->playlist()->freeze ();
        } else {
                if (_region) {
@@ -2320,16 +2328,16 @@ RegionCreateDrag::motion (GdkEvent* event, bool first_move)
                        */
 
                        framecnt_t const len = (framecnt_t) fabs ((double)(f - grab_frame () - 1));
-                       _region->set_length (len < 1 ? 1 : len);
+                       _region->set_length (len < 1 ? 1 : len, _editor->get_grid_music_divisions (event->button.state));
                }
        }
 }
 
 void
-RegionCreateDrag::finished (GdkEvent*, bool movement_occurred)
+RegionCreateDrag::finished (GdkEvent* event, bool movement_occurred)
 {
        if (!movement_occurred) {
-               add_midi_region (_view, true);
+               add_midi_region (_view, true, _editor->get_grid_music_divisions (event->button.state));
        } else {
                _view->playlist()->thaw ();
                _editor->commit_reversible_command();
@@ -2850,7 +2858,6 @@ TrimDrag::motion (GdkEvent* event, bool first_move)
 
                for (list<DraggingView>::const_iterator i = _views.begin(); i != _views.end(); ++i) {
                        RegionView* rv = i->view;
-                       rv->enable_display (false);
                        rv->region()->playlist()->clear_owned_changes ();
 
                        if (_operation == StartTrim) {
@@ -2911,7 +2918,9 @@ TrimDrag::motion (GdkEvent* event, bool first_move)
        switch (_operation) {
        case StartTrim:
                for (list<DraggingView>::iterator i = _views.begin(); i != _views.end(); ++i) {
-                       bool changed = i->view->trim_front (i->initial_position + dt, non_overlap_trim);
+                       bool changed = i->view->trim_front (i->initial_position + dt, non_overlap_trim
+                                                           , _editor->get_grid_music_divisions (event->button.state));
+
                        if (changed && _preserve_fade_anchor) {
                                AudioRegionView* arv = dynamic_cast<AudioRegionView*> (i->view);
                                if (arv) {
@@ -2929,7 +2938,7 @@ TrimDrag::motion (GdkEvent* event, bool first_move)
 
        case EndTrim:
                for (list<DraggingView>::iterator i = _views.begin(); i != _views.end(); ++i) {
-                       bool changed = i->view->trim_end (i->initial_end + dt, non_overlap_trim);
+                       bool changed = i->view->trim_end (i->initial_end + dt, non_overlap_trim, _editor->get_grid_music_divisions (event->button.state));
                        if (changed && _preserve_fade_anchor) {
                                AudioRegionView* arv = dynamic_cast<AudioRegionView*> (i->view);
                                if (arv) {
@@ -3181,6 +3190,10 @@ MeterMarkerDrag::motion (GdkEvent* event, bool first_move)
                        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_at_bbt (bbt), _real_section->position_lock_style());
+                       if (!_real_section) {
+                               aborted (true);
+                               return;
+                       }
 
                }
                /* only snap to bars. leave snap mode alone for audio locked meters.*/
@@ -3312,20 +3325,27 @@ TempoMarkerDrag::motion (GdkEvent* event, bool first_move)
                        _editor->begin_reversible_command (_("move tempo mark"));
 
                } else {
+                       const Tempo tempo (_marker->tempo());
                        const framepos_t frame = adjusted_current_frame (event) + 1;
+                       const TempoSection::Type type = _real_section->type();
 
                        _editor->begin_reversible_command (_("copy tempo mark"));
 
                        if (_real_section->position_lock_style() == MusicTime) {
-                               _real_section = map.add_tempo (_marker->tempo(), map.pulse_at_frame (frame), 0, _real_section->type(), MusicTime);
+                               _real_section = map.add_tempo (tempo, map.pulse_at_frame (frame), 0, type, MusicTime);
                        } else {
-                               _real_section = map.add_tempo (_marker->tempo(), 0.0, frame, _real_section->type(), AudioTime);
+                               _real_section = map.add_tempo (tempo, 0.0, frame, type, AudioTime);
+                       }
+
+                       if (!_real_section) {
+                               aborted (true);
+                               return;
                        }
                }
 
        }
 
-       if (Keyboard::modifier_state_contains (event->button.state, ArdourKeyboard::constraint_modifier ())) {
+       if (ArdourKeyboard::indicates_constraint (event->button.state)) {
                /* 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);
                stringstream strs;
@@ -3335,29 +3355,26 @@ 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;
-
-               const framepos_t pf = adjusted_current_frame (event);
+               framepos_t pf;
 
-               /* cop-out : we really should set the musical position of music-locked tempo sections here, which generally works well.
-                  The problem is that when the mouse pointer's motion causes the location of the beat to change in a more-or-less
-                  chaotic way when cross-dragging tempo sections (the beat the pointer is now above could have changed without any pointer motion).
-                  Until there is a way to deal with this, just pretend the tempo section is audio-locked.
-               */
-               if (was_music) {
-                       _real_section->set_position_lock_style (AudioTime);
+               if (_editor->snap_musical()) {
+                       /* we can't snap to a grid that we are about to move.
+                        * gui_move_tempo() will sort out snap using the supplied beat divisions.
+                       */
+                       pf = adjusted_current_frame (event, false);
+               } else {
+                       pf = adjusted_current_frame (event);
                }
 
-               map.gui_move_tempo (_real_section, pf);
+               TempoMap& map (_editor->session()->tempo_map());
+
+               /* snap to beat is 1, snap to bar is -1 (sorry) */
+               const int sub_num = _editor->get_grid_music_divisions (event->button.state);
 
-               if (was_music) {
-                       _real_section->set_position_lock_style (MusicTime);
-               }
+               map.gui_move_tempo (_real_section, pf, sub_num);
 
                show_verbose_cursor_time (_real_section->frame());
        }
-
        _marker->set_position (adjusted_current_frame (event, false));
 }
 
@@ -3392,8 +3409,7 @@ TempoMarkerDrag::aborted (bool moved)
        if (moved) {
                TempoMap& map (_editor->session()->tempo_map());
                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 the dummy (hidden) marker we used for events while moving.
                delete _marker;
        }
 }
@@ -3426,7 +3442,7 @@ void
 BBTRulerDrag::setup_pointer_frame_offset ()
 {
        TempoMap& map (_editor->session()->tempo_map());
-       const double beat_at_frame = map.beat_at_frame (raw_grab_frame());
+       const double beat_at_frame = max (0.0, map.beat_at_frame (raw_grab_frame()));
        const uint32_t divisions = _editor->get_grid_beat_divisions (0);
        double beat = 0.0;
 
@@ -3465,7 +3481,7 @@ BBTRulerDrag::motion (GdkEvent* event, bool first_move)
                pf = adjusted_current_frame (event);
        }
 
-       if (Keyboard::modifier_state_contains (event->button.state, ArdourKeyboard::constraint_modifier())) {
+       if (ArdourKeyboard::indicates_constraint (event->button.state)) {
                /* adjust previous tempo to match pointer frame */
                _editor->session()->tempo_map().gui_dilate_tempo (_tempo, map.frame_at_pulse (_pulse), pf, _pulse);
        }
@@ -4239,6 +4255,10 @@ MarkerDrag::finished (GdkEvent* event, bool movement_occurred)
                        } else {
                                location->set (((*x).location)->start(), ((*x).location)->end());
                        }
+
+                       if (location->is_session_range()) {
+                               _editor->session()->set_end_is_free (false);
+                       }
                }
        }
 
@@ -4770,7 +4790,7 @@ RubberbandSelectDrag::finished (GdkEvent* event, bool movement_occurred)
                        /* MIDI track */
                        if (_editor->selection->empty() && _editor->mouse_mode == MouseDraw) {
                                /* nothing selected */
-                               add_midi_region (mtv, true);
+                               add_midi_region (mtv, true, _editor->get_grid_music_divisions(event->button.state));
                                do_deselect = false;
                        }
                }
@@ -5554,6 +5574,10 @@ NoteDrag::start_grab (GdkEvent* event, Gdk::Cursor *)
 frameoffset_t
 NoteDrag::total_dx (const guint state) const
 {
+       if (_x_constrained) {
+               return 0;
+       }
+
        /* dx in frames */
        frameoffset_t const dx = _editor->pixel_to_sample (_drags->current_pointer_x() - grab_x());
 
@@ -5600,6 +5624,10 @@ NoteDrag::total_dx (const guint state) const
 int8_t
 NoteDrag::total_dy () const
 {
+       if (_y_constrained) {
+               return 0;
+       }
+
        MidiStreamView* msv = _region->midi_stream_view ();
        double const y = _region->midi_view()->y_position ();
        /* new current note */
@@ -5619,8 +5647,8 @@ NoteDrag::motion (GdkEvent * event, bool)
        int8_t const dy = total_dy ();
 
        /* Now work out what we have to do to the note canvas items to set this new drag delta */
-       double const tdx = _editor->sample_to_pixel (dx) - _cumulative_dx;
-       double const tdy = -dy * _note_height - _cumulative_dy;
+       double const tdx = _x_constrained ? 0 : _editor->sample_to_pixel (dx) - _cumulative_dx;
+       double const tdy = _y_constrained ? 0 : -dy * _note_height - _cumulative_dy;
 
        if (tdx || tdy) {
                _cumulative_dx += tdx;
@@ -5628,17 +5656,19 @@ NoteDrag::motion (GdkEvent * event, bool)
 
                int8_t note_delta = total_dy();
 
-               _region->move_selection (tdx, tdy, note_delta);
+               if (tdx || tdy) {
+                       _region->move_selection (tdx, tdy, note_delta);
 
-               /* the new note value may be the same as the old one, but we
-                * don't know what that means because the selection may have
-                * involved more than one note and we might be doing something
-                * odd with them. so show the note value anyway, always.
-                */
+                       /* the new note value may be the same as the old one, but we
+                        * don't know what that means because the selection may have
+                        * involved more than one note and we might be doing something
+                        * odd with them. so show the note value anyway, always.
+                        */
 
-               uint8_t new_note = min (max (_primary->note()->note() + note_delta, 0), 127);
+                       uint8_t new_note = min (max (_primary->note()->note() + note_delta, 0), 127);
 
-               _region->show_verbose_cursor_for_new_note_value (_primary->note(), new_note);
+                       _region->show_verbose_cursor_for_new_note_value (_primary->note(), new_note);
+               }
        }
 }
 
@@ -6342,7 +6372,7 @@ RegionCutDrag::motion (GdkEvent*, bool)
 }
 
 void
-RegionCutDrag::finished (GdkEvent*, bool)
+RegionCutDrag::finished (GdkEvent* event, bool)
 {
        _editor->get_track_canvas()->canvas()->re_enter();
 
@@ -6356,7 +6386,7 @@ RegionCutDrag::finished (GdkEvent*, bool)
                return;
        }
 
-       _editor->split_regions_at (pos, rs);
+       _editor->split_regions_at (pos, rs, _editor->get_grid_music_divisions (event->button.state));
 }
 
 void