-
Notifications
You must be signed in to change notification settings - Fork 716
/
Copy patheditor_drag.cc
7513 lines (6127 loc) · 214 KB
/
editor_drag.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2009-2012 Carl Hetherington <[email protected]>
* Copyright (C) 2009-2015 David Robillard <[email protected]>
* Copyright (C) 2009-2017 Paul Davis <[email protected]>
* Copyright (C) 2012-2019 Robin Gareus <[email protected]>
* Copyright (C) 2013-2017 Nick Mainsbridge <[email protected]>
* Copyright (C) 2013 Michael Fisher <[email protected]>
* Copyright (C) 2014-2019 Ben Loftis <[email protected]>
* Copyright (C) 2014 Colin Fletcher <[email protected]>
* Copyright (C) 2015-2017 Tim Mayberry <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef WAF_BUILD
#include "gtk2ardour-config.h"
#endif
#include <algorithm>
#include <stdint.h>
#include "pbd/basename.h"
#include "pbd/memento_command.h"
#include "pbd/stateful_diff_command.h"
#include <gtkmm/stock.h>
#include "gtkmm2ext/utils.h"
#include "ardour/audio_track.h"
#include "ardour/audioengine.h"
#include "ardour/audioregion.h"
#include "ardour/dB.h"
#include "ardour/midi_region.h"
#include "ardour/midi_track.h"
#include "ardour/operations.h"
#include "ardour/profile.h"
#include "ardour/region_factory.h"
#include "ardour/session.h"
#include "ardour/session_playlists.h"
#include "canvas/canvas.h"
#include "canvas/lollipop.h"
#include "canvas/scroll_group.h"
#include "ardour_ui.h"
#include "audio_region_view.h"
#include "audio_time_axis.h"
#include "automation_region_view.h"
#include "automation_time_axis.h"
#include "bbt_marker_dialog.h"
#include "control_point.h"
#include "debug.h"
#include "editor.h"
#include "editor_cursors.h"
#include "editor_drag.h"
#include "gui_thread.h"
#include "keyboard.h"
#include "midi_region_view.h"
#include "midi_selection.h"
#include "midi_time_axis.h"
#include "mouse_cursors.h"
#include "note_base.h"
#include "patch_change.h"
#include "pbd/i18n.h"
#include "region_gain_line.h"
#include "selection.h"
#include "ui_config.h"
#include "velocity_ghost_region.h"
#include "verbose_cursor.h"
#include "video_timeline.h"
using namespace std;
using namespace ARDOUR;
using namespace PBD;
using namespace Gtk;
using namespace Gtkmm2ext;
using namespace Editing;
using namespace ArdourCanvas;
using namespace Temporal;
using Gtkmm2ext::Keyboard;
double ControlPointDrag::_zero_gain_fraction = -1.0;
DragManager::DragManager (Editor* e)
: _editor (e)
, _ending (false)
, _current_pointer_x (0.0)
, _current_pointer_y (0.0)
, _current_pointer_time (timepos_t::from_superclock (0)) /* avoid early use of superclock_ticks_per_second */
, _old_follow_playhead (false)
{
}
DragManager::~DragManager ()
{
abort ();
}
/** Call abort for each active drag */
void
DragManager::abort ()
{
_ending = true;
for (list<Drag*>::const_iterator i = _drags.begin (); i != _drags.end (); ++i) {
(*i)->abort ();
delete *i;
}
if (!_drags.empty ()) {
_editor->set_follow_playhead (_old_follow_playhead, false);
}
_drags.clear ();
_editor->abort_reversible_command ();
_ending = false;
}
void
DragManager::add (Drag* d)
{
d->set_manager (this);
_drags.push_back (d);
}
void
DragManager::set (Drag* d, GdkEvent* e, Gdk::Cursor* c)
{
d->set_manager (this);
_drags.push_back (d);
start_grab (e, c);
}
bool
DragManager::preview_video () const
{
for (list<Drag*>::const_iterator i = _drags.begin (); i != _drags.end (); ++i) {
if ((*i)->preview_video ()) {
return true;
}
}
return false;
}
bool
DragManager::mid_drag_key_event (GdkEventKey* ev)
{
bool handled = false;
for (auto & drag : _drags) {
if (drag->mid_drag_key_event (ev)) {
handled = true;
break;
}
}
return handled;
}
void
DragManager::start_grab (GdkEvent* e, Gdk::Cursor* c)
{
/* Prevent follow playhead during the drag to be nice to the user */
_old_follow_playhead = _editor->follow_playhead ();
_editor->set_follow_playhead (false);
_current_pointer_time = timepos_t (_editor->canvas_event_sample (e, &_current_pointer_x, &_current_pointer_y));
for (list<Drag*>::const_iterator i = _drags.begin (); i != _drags.end (); ++i) {
if ((*i)->grab_button () < 0) {
(*i)->start_grab (e, c);
}
}
}
/** Call end_grab for each active drag.
* @return true if any drag reported movement having occurred.
*/
bool
DragManager::end_grab (GdkEvent* e)
{
_ending = true;
bool r = false;
for (list<Drag*>::iterator i = _drags.begin (); i != _drags.end ();) {
list<Drag*>::iterator tmp = i;
if ((*i)->grab_button () == (int)e->button.button) {
bool const t = (*i)->end_grab (e);
if (t) {
r = true;
}
delete *i;
tmp = _drags.erase (i);
} else {
++tmp;
}
i = tmp;
}
_ending = false;
if (_drags.empty ()) {
_editor->set_follow_playhead (_old_follow_playhead, false);
}
return r;
}
void
DragManager::mark_double_click ()
{
for (list<Drag*>::const_iterator i = _drags.begin (); i != _drags.end (); ++i) {
(*i)->set_double_click (true);
}
}
bool
DragManager::motion_handler (GdkEvent* e, bool from_autoscroll)
{
bool r = false;
/* calling this implies that we expect the event to have canvas
* coordinates
*
* Can we guarantee that this is true?
*/
_current_pointer_time = timepos_t (_editor->canvas_event_sample (e, &_current_pointer_x, &_current_pointer_y));
for (list<Drag*>::iterator i = _drags.begin (); i != _drags.end (); ++i) {
bool const t = (*i)->motion_handler (e, from_autoscroll);
/* run all handlers; return true if at least one of them
returns true (indicating that the event has been handled).
*/
if (t) {
r = true;
}
}
return r;
}
bool
DragManager::have_item (ArdourCanvas::Item* i) const
{
list<Drag*>::const_iterator j = _drags.begin ();
while (j != _drags.end () && (*j)->item () != i) {
++j;
}
return j != _drags.end ();
}
Drag::Drag (Editor* e, ArdourCanvas::Item* i, Temporal::TimeDomain td, bool trackview_only, bool hide_snapped_cursor)
: _editor (e)
, _drags (0)
, _item (i)
, _pointer_offset (0)
, _video_offset (0)
, _preview_video (false)
, _x_constrained (false)
, _y_constrained (false)
, _was_rolling (false)
, _earliest_time_limit (0)
, _trackview_only (trackview_only)
, _hide_snapped_cursor (hide_snapped_cursor)
, _move_threshold_passed (false)
, _starting_point_passed (false)
, _initially_vertical (false)
, _was_double_click (false)
, _grab_x (0.0)
, _grab_y (0.0)
, _last_pointer_x (0.0)
, _last_pointer_y (0.0)
, _time_domain (td)
, _snap_delta (0)
, _constraint_pressed (false)
, _grab_button (-1)
{
}
void
Drag::set_time_domain (Temporal::TimeDomain td)
{
/* must be called early in life of a Drag */
_time_domain = td;
}
timepos_t
Drag::pixel_to_time (double x) const
{
samplepos_t p = _editor->pixel_to_sample (x);
if (_time_domain == Temporal::AudioTime) {
return timepos_t (p);
}
return timepos_t (timepos_t (p).beats ());
}
void
Drag::swap_grab (ArdourCanvas::Item* new_item, Gdk::Cursor* cursor, uint32_t /*time*/)
{
_item->ungrab ();
_item = new_item;
if (!_cursor_ctx) {
_cursor_ctx = CursorContext::create (*_editor, cursor);
} else {
_cursor_ctx->change (cursor);
}
_item->grab ();
}
void
Drag::set_grab_button_anyway (GdkEvent* ev)
{
_grab_button = ev->button.button;
}
void
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);
const samplepos_t pos = _editor->canvas_event_sample (event, &_grab_x, &_grab_y);
if (_time_domain == Temporal::AudioTime) {
_raw_grab_time = timepos_t (pos);
} else {
_raw_grab_time = timepos_t (timepos_t (pos).beats ());
}
_grab_button = event->button.button;
setup_pointer_offset ();
setup_video_offset ();
if (!UIConfiguration::instance ().get_preview_video_frame_on_drag ()) {
_preview_video = false;
}
if (_hide_snapped_cursor) {
_editor->snapped_cursor ()->hide ();
}
_grab_time = adjusted_time (_raw_grab_time, event);
_last_pointer_time = _grab_time;
_last_pointer_x = _grab_x;
if (_trackview_only) {
_grab_y = _grab_y - _editor->get_trackview_group ()->canvas_origin ().y;
}
_last_pointer_y = _grab_y;
_item->grab ();
if (!_editor->cursors ()->is_invalid (cursor)) {
/* CAIROCANVAS need a variant here that passes *cursor */
_cursor_ctx = CursorContext::create (*_editor, cursor);
}
if (_editor->session () && _editor->session ()->transport_rolling ()) {
_was_rolling = true;
} else {
_was_rolling = false;
}
#if 0
if ( UIConfiguration::instance().get_snap_to_region_start() || UIConfiguration::instance().get_snap_to_region_end() || UIConfiguration::instance().get_snap_to_region_sync() ) {
_editor->build_region_boundary_cache ();
}
#endif
}
/** Call to end a drag `successfully'. Ungrabs item and calls
* subclass' finished() method.
*
* @param event GDK event, or 0.
* @return true if some movement occurred, otherwise false.
*/
bool
Drag::end_grab (GdkEvent* event)
{
_editor->stop_canvas_autoscroll ();
_item->ungrab ();
finished (event, _starting_point_passed);
_editor->verbose_cursor ()->hide ();
_cursor_ctx.reset ();
return _starting_point_passed;
}
timepos_t
Drag::adjusted_time (timepos_t const& f, GdkEvent const* event, bool snap) const
{
timepos_t pos (f.time_domain ()); /* zero */
if (f > _pointer_offset) {
pos = f;
pos.shift_earlier (_pointer_offset);
}
if (snap) {
_editor->snap_to_with_modifier (pos, event);
}
return pos;
}
timepos_t
Drag::adjusted_current_time (GdkEvent const* event, bool snap) const
{
return adjusted_time (_drags->current_pointer_time (), event, snap);
}
timecnt_t
Drag::snap_delta (guint state) const
{
if (ArdourKeyboard::indicates_snap_delta (state)) {
return _snap_delta;
}
return timecnt_t (_editor->default_time_domain ());
}
double
Drag::current_pointer_x () const
{
return _drags->current_pointer_x ();
}
double
Drag::current_pointer_y () const
{
if (!_trackview_only) {
return _drags->current_pointer_y ();
}
return _drags->current_pointer_y () - _editor->get_trackview_group ()->canvas_origin ().y;
}
void
Drag::setup_snap_delta (timepos_t const& pos)
{
timepos_t snap (pos);
_editor->snap_to (snap, Temporal::RoundNearest, ARDOUR::SnapToAny_Visual, true);
_snap_delta = pos.distance (snap);
}
bool
Drag::motion_handler (GdkEvent* event, bool from_autoscroll)
{
/* check to see if we have moved in any way that matters since the last motion event */
if (_move_threshold_passed &&
(!x_movement_matters () || _last_pointer_x == current_pointer_x ()) &&
(!y_movement_matters () || _last_pointer_y == current_pointer_y ())) {
return false;
}
pair<timecnt_t, int> const threshold = move_threshold ();
bool const old_move_threshold_passed = _move_threshold_passed;
if (!_move_threshold_passed) {
bool const xp = (_raw_grab_time.distance (_drags->current_pointer_time ()).abs () >= threshold.first);
bool const yp = (::fabs ((current_pointer_y () - _grab_y)) >= threshold.second);
_move_threshold_passed = ((xp && x_movement_matters ()) || (yp && y_movement_matters ()));
}
if (active (_editor->mouse_mode) && _move_threshold_passed) {
if (event->motion.state & Gdk::BUTTON1_MASK || event->motion.state & Gdk::BUTTON2_MASK) {
if (old_move_threshold_passed != _move_threshold_passed) {
/* just changed */
if (fabs (current_pointer_y () - _grab_y) > fabs (current_pointer_x () - _grab_x)) {
_initially_vertical = true;
} else {
_initially_vertical = false;
}
/** check constraints for this drag.
* Note that the current convention is to use "contains" for
* key modifiers during motion and "equals" when initiating a drag.
* In this case we haven't moved yet, so "equals" applies here.
*/
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 (_constraint_pressed) {
_x_constrained = false;
_y_constrained = true;
} else {
_x_constrained = true;
_y_constrained = false;
}
} else if (_constraint_pressed) {
// if dragging normally, the motion is constrained to the first direction of movement.
if (_initially_vertical) {
_x_constrained = true;
_y_constrained = false;
} else {
_x_constrained = false;
_y_constrained = true;
}
}
} else {
if (event->button.state & Gdk::BUTTON2_MASK) {
_x_constrained = false;
} else {
_x_constrained = true;
}
_y_constrained = false;
}
}
if (!from_autoscroll) {
_editor->maybe_autoscroll (allow_horizontal_autoscroll (), allow_vertical_autoscroll (), false);
}
if (!_editor->autoscroll_active () || from_autoscroll) {
bool first_move = (_move_threshold_passed != old_move_threshold_passed) || from_autoscroll;
motion (event, first_move && !_starting_point_passed);
if (first_move && !_starting_point_passed) {
_starting_point_passed = true;
}
_last_pointer_x = _drags->current_pointer_x ();
_last_pointer_y = current_pointer_y ();
_last_pointer_time = adjusted_current_time (event, false);
}
return true;
}
}
return false;
}
/** Call to abort a drag. Ungrabs item and calls subclass's aborted () */
void
Drag::abort ()
{
if (_item) {
_item->ungrab ();
}
aborted (_move_threshold_passed);
_editor->stop_canvas_autoscroll ();
_editor->verbose_cursor ()->hide ();
}
void
Drag::show_verbose_cursor_time (timepos_t const& pos)
{
_editor->verbose_cursor ()->set_time (pos.samples ());
_editor->verbose_cursor ()->show ();
}
void
Drag::show_verbose_cursor_duration (timepos_t const& start, timepos_t const& end, double /*xoffset*/)
{
_editor->verbose_cursor ()->set_duration (start.samples (), end.samples ());
_editor->verbose_cursor ()->show ();
}
void
Drag::show_verbose_cursor_text (string const& text)
{
_editor->verbose_cursor ()->set (text);
_editor->verbose_cursor ()->show ();
}
void
Drag::show_view_preview (timepos_t const& pos)
{
if (_preview_video) {
ARDOUR_UI::instance ()->video_timeline->manual_seek_video_monitor (pos.samples ());
}
}
std::shared_ptr<Region>
Drag::add_midi_region (MidiTimeAxisView* view, bool commit)
{
if (_editor->session ()) {
const timepos_t pos (grab_time ().beats ());
const timecnt_t len = pos.distance (max (timepos_t::zero (Temporal::BeatTime), timepos_t (pos.beats () + Beats (1, 0))));
return view->add_region (pos, len, commit);
}
return std::shared_ptr<Region> ();
}
struct TimeAxisViewStripableSorter {
bool operator() (TimeAxisView* tav_a, TimeAxisView* tav_b)
{
std::shared_ptr<ARDOUR::Stripable> const& a = tav_a->stripable ();
std::shared_ptr<ARDOUR::Stripable> const& b = tav_b->stripable ();
return ARDOUR::Stripable::Sorter () (a, b);
}
};
RegionDrag::RegionDrag (Editor* e, ArdourCanvas::Item* i, RegionView* p, list<RegionView*> const& v, Temporal::TimeDomain td, bool hide_snapped_cursor)
: Drag (e, i, td, true, hide_snapped_cursor)
, _primary (p)
, _ntracks (0)
{
_editor->visible_order_range (&_visible_y_low, &_visible_y_high);
/* Make a list of tracks to refer to during the drag; we include hidden tracks,
as some of the regions we are dragging may be on such tracks.
*/
TrackViewList track_views = _editor->track_views;
track_views.sort (TimeAxisViewStripableSorter ());
for (TrackViewList::iterator i = track_views.begin (); i != track_views.end (); ++i) {
_time_axis_views.push_back (*i);
TimeAxisView::Children children_list = (*i)->get_child_list ();
for (TimeAxisView::Children::iterator j = children_list.begin (); j != children_list.end (); ++j) {
_time_axis_views.push_back (j->get ());
}
}
/* the list of views can be empty at this point if this is a region list-insert drag
*/
for (list<RegionView*>::const_iterator i = v.begin (); i != v.end (); ++i) {
_views.push_back (DraggingView (*i, this, &(*i)->get_time_axis_view ()));
}
RegionView::RegionViewGoingAway.connect (death_connection, invalidator (*this), boost::bind (&RegionDrag::region_going_away, this, _1), gui_context ());
}
void
RegionDrag::region_going_away (RegionView* v)
{
list<DraggingView>::iterator i = _views.begin ();
while (i != _views.end () && i->view != v) {
++i;
}
if (i != _views.end ()) {
_views.erase (i);
}
}
/** Given a TimeAxisView, return the index of it into the _time_axis_views vector,
* or -1 if it is not found.
*/
int
RegionDrag::find_time_axis_view (TimeAxisView* t) const
{
int i = 0;
int const N = _time_axis_views.size ();
while (i < N && _time_axis_views[i] != t) {
++i;
}
if (i == N) {
return -1;
}
return i;
}
void
RegionDrag::setup_video_offset ()
{
if (_views.empty ()) {
_preview_video = true;
return;
}
timepos_t first_sync = _views.begin ()->view->region ()->sync_position ();
for (list<DraggingView>::const_iterator i = _views.begin (); i != _views.end (); ++i) {
first_sync = std::min (first_sync, i->view->region ()->sync_position ());
}
_video_offset = raw_grab_time ().distance (first_sync + _pointer_offset);
_preview_video = true;
}
void
RegionDrag::add_stateful_diff_commands_for_playlists (PlaylistSet const& playlists)
{
for (PlaylistSet::const_iterator i = playlists.begin (); i != playlists.end (); ++i) {
StatefulDiffCommand* c = new StatefulDiffCommand (*i);
if (!c->empty ()) {
_editor->session ()->add_command (c);
} else {
delete c;
}
}
}
RegionSlipContentsDrag::RegionSlipContentsDrag (Editor* e, ArdourCanvas::Item* i, RegionView* p, list<RegionView*> const& v, TimeDomain td)
: RegionDrag (e, i, p, v, td)
{
DEBUG_TRACE (DEBUG::Drags, "New RegionSlipContentsDrag\n");
}
void
RegionSlipContentsDrag::start_grab (GdkEvent* event, Gdk::Cursor* cursor)
{
Drag::start_grab (event, _editor->cursors ()->trimmer);
}
void
RegionSlipContentsDrag::motion (GdkEvent* event, bool first_move)
{
if (first_move) {
/*prepare reversible cmd*/
_editor->begin_reversible_command (_("Slip Contents"));
for (list<DraggingView>::iterator i = _views.begin (); i != _views.end (); ++i) {
RegionView* rv = i->view;
rv->region ()->clear_changes ();
rv->drag_start (); // this allows the region to draw itself 'transparently' while we drag it
}
} else {
for (list<DraggingView>::iterator i = _views.begin (); i != _views.end (); ++i) {
RegionView* rv = i->view;
timecnt_t slippage = adjusted_current_time (event, false).distance (last_pointer_time ());
rv->move_contents (slippage);
}
show_verbose_cursor_time (_primary->region ()->start ());
}
}
void
RegionSlipContentsDrag::finished (GdkEvent*, bool movement_occurred)
{
if (movement_occurred) {
/*finish reversible cmd*/
for (list<DraggingView>::iterator i = _views.begin (); i != _views.end (); ++i) {
RegionView* rv = i->view;
_editor->session ()->add_command (new StatefulDiffCommand (rv->region ()));
rv->drag_end ();
}
_editor->commit_reversible_command ();
}
}
void
RegionSlipContentsDrag::aborted (bool movement_occurred)
{
/* ToDo: revert to the original region properties */
_editor->abort_reversible_command ();
}
RegionBrushDrag::RegionBrushDrag (Editor* e, ArdourCanvas::Item* i, RegionView* p, list<RegionView*> const& v, TimeDomain td)
: RegionDrag (e, i, p, v, td)
{
DEBUG_TRACE (DEBUG::Drags, "New RegionBrushDrag\n");
_y_constrained = true;
}
void
RegionBrushDrag::start_grab (GdkEvent* event, Gdk::Cursor* cursor)
{
Drag::start_grab (event, _editor->cursors ()->trimmer);
}
void
RegionBrushDrag::motion (GdkEvent* event, bool first_move)
{
if (first_move) {
_editor->begin_reversible_command (_("Region brush drag"));
_already_pasted.insert (_primary->region ()->position ());
} else {
timepos_t snapped (adjusted_current_time (event, false));
_editor->snap_to (snapped, RoundDownAlways, SnapToGrid_Scaled, false);
if (_already_pasted.find (snapped) == _already_pasted.end ()) {
_editor->mouse_brush_insert_region (_primary, snapped);
_already_pasted.insert (snapped);
}
}
}
void
RegionBrushDrag::finished (GdkEvent*, bool movement_occurred)
{
if (!movement_occurred) {
return;
}
PlaylistSet modified_playlists;
modified_playlists.insert (_primary->region ()->playlist ());
add_stateful_diff_commands_for_playlists (modified_playlists);
_editor->commit_reversible_command ();
_already_pasted.clear ();
}
void
RegionBrushDrag::aborted (bool movement_occurred)
{
_already_pasted.clear ();
/* ToDo: revert to the original playlist properties */
_editor->abort_reversible_command ();
}
RegionMotionDrag::RegionMotionDrag (Editor* e, ArdourCanvas::Item* i, RegionView* p, list<RegionView*> const& v, TimeDomain td)
: RegionDrag (e, i, p, v, td, false)
, _ignore_video_lock (false)
, _total_x_delta (0)
, _last_pointer_time_axis_view (0)
, _last_pointer_layer (0)
, _ndropzone (0)
, _pdropzone (0)
, _ddropzone (0)
{
DEBUG_TRACE (DEBUG::Drags, "New RegionMotionDrag\n");
}
void
RegionMotionDrag::start_grab (GdkEvent* event, Gdk::Cursor* cursor)
{
Drag::start_grab (event, cursor);
setup_snap_delta (_last_position);
show_verbose_cursor_time (_last_position);
show_view_preview (_last_position + _video_offset);
/* this conditional is required because drag-n-drop'ed regions end up
* here, and at this point they are not attached to a playlist.
*/
if (_editor->should_ripple () && _primary && _primary->region () && _primary->region ()->playlist ()) {
_earliest_time_limit = _primary->region ()->playlist ()->find_prev_region_start (_primary->region ()->position ());
}
pair<TimeAxisView*, double> const tv = _editor->trackview_by_y_position (current_pointer_y ());
if (tv.first) {
_last_pointer_time_axis_view = find_time_axis_view (tv.first);
assert (_last_pointer_time_axis_view >= 0);
_last_pointer_layer = tv.first->layer_display () == Overlaid ? 0 : tv.second;
}
if (Keyboard::modifier_state_equals (event->button.state, Keyboard::ModifierMask (Keyboard::TertiaryModifier))) {
_ignore_video_lock = true;
}
if (_editor->should_ripple ()) {
/* we do not drag across tracks when rippling or brushing */
_y_constrained = true;
}
}
double
RegionMotionDrag::compute_x_delta (GdkEvent const* event, Temporal::timepos_t& pending_region_position)
{
/* compute the amount of pointer motion in samples, and where
the region would be if we moved it by that much.
*/
if (_x_constrained) {
pending_region_position = _last_position;
return 0.0;
}
pending_region_position = adjusted_time (_drags->current_pointer_time (), event, false);
timecnt_t sync_offset;
int32_t sync_dir;
sync_offset = _primary->region ()->sync_offset (sync_dir);
/* we don't handle a sync point that lies before zero.
*/
if (sync_dir >= 0 || (sync_dir < 0 && pending_region_position >= sync_offset)) {
timecnt_t const sd = snap_delta (event->button.state);
timepos_t sync_snap;
if (sync_dir > 0) {
sync_snap = pending_region_position + sync_offset + sd;
} else {
sync_snap = pending_region_position.earlier (sync_offset) + sd;
}
_editor->snap_to_with_modifier (sync_snap, event);
if (sync_offset.is_zero () && sd.is_zero ()) {
pending_region_position = sync_snap;
} else {
pending_region_position = _primary->region ()->adjust_to_sync (sync_snap).earlier (sd);
}
} else {
pending_region_position = _last_position;
}
if (pending_region_position > timepos_t::max (time_domain ()).earlier (_primary->region ()->length ())) {
pending_region_position = _last_position;
}
if (!_earliest_time_limit.is_zero () && pending_region_position <= _earliest_time_limit) {
pending_region_position = _earliest_time_limit;
return 0.0;
}
double dx = 0;
bool const x_move_allowed = !_x_constrained;
if ((pending_region_position != _last_position) && x_move_allowed) {
/* x movement since last time (in pixels) */
dx = _editor->duration_to_pixels_unrounded (_last_position.distance (pending_region_position));
/* total x movement */
timecnt_t total_dx = timecnt_t (pixel_to_time (_total_x_delta + dx), grab_time ());
for (list<DraggingView>::const_iterator i = _views.begin (); i != _views.end (); ++i) {
const timepos_t off = i->view->region ()->position () + total_dx;
if (off.is_negative ()) {
dx -= _editor->time_to_pixel_unrounded (off);
pending_region_position = pending_region_position.earlier (timecnt_t (off, timepos_t (time_domain ())));
break;
}
}
}
_editor->set_snapped_cursor_position (pending_region_position);
return dx;
}
int
RegionDrag::apply_track_delta (const int start, const int delta, const int skip, const bool distance_only) const
{
if (delta == 0) {
return start;
}
const int tavsize = _time_axis_views.size ();
const int dt = delta > 0 ? +1 : -1;
int current = start;
int target = start + delta - skip;
assert (current < 0 || current >= tavsize || !_time_axis_views[current]->hidden ());
assert (skip == 0 || (skip < 0 && delta < 0) || (skip > 0 && delta > 0));
while (current >= 0 && current != target) {
current += dt;
if (current < 0 && dt < 0) {
break;
}
if (current >= tavsize && dt > 0) {
break;
}
if (current < 0 || current >= tavsize) {
continue;
}
RouteTimeAxisView const* rtav = dynamic_cast<RouteTimeAxisView const*> (_time_axis_views[current]);
if (_time_axis_views[current]->hidden () || !rtav || !rtav->is_track ()) {
target += dt;
}
if (distance_only && current == start + delta) {
break;
}
}
return target;
}
bool
RegionMotionDrag::y_movement_allowed (int delta_track, double delta_layer, int skip_invisible) const
{
if (_y_constrained) {
return false;
}
const int tavsize = _time_axis_views.size ();
for (list<DraggingView>::const_iterator i = _views.begin (); i != _views.end (); ++i) {
int n = apply_track_delta (i->time_axis_view, delta_track, skip_invisible);
assert (n < 0 || n >= tavsize || !_time_axis_views[n]->hidden ());