forked from Sneeds-Feed-and-Seed/sneedacity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdornedRulerPanel.cpp
2393 lines (2001 loc) · 68.9 KB
/
AdornedRulerPanel.cpp
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
/**********************************************************************
Sneedacity: A Digital Audio Editor
AdornedRulerPanel.cpp
Dominic Mazzoni
*******************************************************************//**
\class AdornedRulerPanel
\brief This is an Sneedacity Specific ruler panel which additionally
has border, selection markers, play marker.
Once TrackPanel uses wxSizers, we will derive it from some
wxWindow and the GetSize and SetSize functions
will then be wxWidgets functions instead.
*//******************************************************************/
#include "AdornedRulerPanel.h"
#include <wx/setup.h> // for wxUSE_* macros
#include <wx/tooltip.h>
#include "AColor.h"
#include "AllThemeResources.h"
#include "AudioIO.h"
#include "CellularPanel.h"
#include "HitTestResult.h"
#include "Menus.h"
#include "Prefs.h"
#include "Project.h"
#include "ProjectAudioIO.h"
#include "ProjectAudioManager.h"
#include "ProjectStatus.h"
#include "ProjectWindow.h"
#include "RefreshCode.h"
#include "Snap.h"
#include "Track.h"
#include "TrackPanelMouseEvent.h"
#include "UIHandle.h"
#include "ViewInfo.h"
#include "prefs/TracksBehaviorsPrefs.h"
#include "prefs/TracksPrefs.h"
#include "prefs/ThemePrefs.h"
#include "toolbars/ToolBar.h"
#include "tracks/ui/Scrubbing.h"
#include "tracks/ui/TrackView.h"
#include "widgets/AButton.h"
#include "widgets/SneedacityMessageBox.h"
#include "widgets/Grabber.h"
#include <wx/dcclient.h>
#include <wx/menu.h>
using std::min;
using std::max;
//#define SCRUB_ABOVE
#define SELECT_TOLERANCE_PIXEL 4
#define PLAY_REGION_TRIANGLE_SIZE 6
#define PLAY_REGION_RECT_WIDTH 1
#define PLAY_REGION_RECT_HEIGHT 3
#define PLAY_REGION_GLOBAL_OFFSET_Y 7
enum : int {
IndicatorSmallWidth = 9,
IndicatorMediumWidth = 13,
IndicatorOffset = 1,
TopMargin = 1,
BottomMargin = 2, // for bottom bevel and bottom line
LeftMargin = 1,
RightMargin = 1,
};
enum {
ScrubHeight = 14,
ProperRulerHeight = 29
};
inline int IndicatorHeightForWidth(int width)
{
return ((width / 2) * 3) / 2;
}
inline int IndicatorWidthForHeight(int height)
{
// Not an exact inverse of the above, with rounding, but good enough
return std::max(static_cast<int>(IndicatorSmallWidth),
(((height) * 2) / 3) * 2
);
}
inline int IndicatorBigHeight()
{
return std::max((int)(ScrubHeight - TopMargin),
(int)(IndicatorMediumWidth));
}
inline int IndicatorBigWidth()
{
return IndicatorWidthForHeight(IndicatorBigHeight());
}
/**********************************************************************
QuickPlayRulerOverlay.
Graphical helper for AdornedRulerPanel.
**********************************************************************/
class QuickPlayIndicatorOverlay;
// This is an overlay drawn on the ruler. It draws the little triangle or
// the double-headed arrow.
class AdornedRulerPanel::QuickPlayRulerOverlay final : public Overlay
{
public:
QuickPlayRulerOverlay(QuickPlayIndicatorOverlay &partner);
// Available to this and to partner
int mNewQPIndicatorPos { -1 };
bool mNewQPIndicatorSnapped {};
bool mNewPreviewingScrub {};
bool mNewScrub {};
bool mNewSeek {};
void Update();
private:
AdornedRulerPanel *GetRuler() const;
unsigned SequenceNumber() const override;
std::pair<wxRect, bool> DoGetRectangle(wxSize size) override;
void Draw(OverlayPanel &panel, wxDC &dc) override;
QuickPlayIndicatorOverlay &mPartner;
// Used by this only
int mOldQPIndicatorPos { -1 };
bool mOldScrub {};
bool mOldSeek {};
};
/**********************************************************************
QuickPlayIndicatorOverlay.
Graphical helper for AdornedRulerPanel.
**********************************************************************/
// This is an overlay drawn on a different window, the track panel.
// It draws the pale guide line that follows mouse movement.
class AdornedRulerPanel::QuickPlayIndicatorOverlay final : public Overlay
{
friend QuickPlayRulerOverlay;
friend AdornedRulerPanel;
public:
QuickPlayIndicatorOverlay(SneedacityProject *project);
private:
unsigned SequenceNumber() const override;
std::pair<wxRect, bool> DoGetRectangle(wxSize size) override;
void Draw(OverlayPanel &panel, wxDC &dc) override;
SneedacityProject *mProject;
std::shared_ptr<QuickPlayRulerOverlay> mPartner
{ std::make_shared<QuickPlayRulerOverlay>(*this) };
int mOldQPIndicatorPos { -1 };
bool mOldQPIndicatorSnapped {};
bool mOldPreviewingScrub {};
};
/**********************************************************************
Implementation of QuickPlayRulerOverlay.
**********************************************************************/
AdornedRulerPanel::QuickPlayRulerOverlay::QuickPlayRulerOverlay(
QuickPlayIndicatorOverlay &partner)
: mPartner(partner)
{
}
AdornedRulerPanel *AdornedRulerPanel::QuickPlayRulerOverlay::GetRuler() const
{
return &Get( *mPartner.mProject );
}
void AdornedRulerPanel::QuickPlayRulerOverlay::Update()
{
const auto project = mPartner.mProject;
auto &scrubber = Scrubber::Get( *project );
auto ruler = GetRuler();
// Hide during transport, or if mouse is not in the ruler, unless scrubbing
if ((!ruler->LastCell() || ProjectAudioIO::Get( *project ).IsAudioActive())
&& (!scrubber.IsScrubbing() || scrubber.IsSpeedPlaying()
|| scrubber.IsKeyboardScrubbing()))
mNewQPIndicatorPos = -1;
else {
const auto &selectedRegion = ViewInfo::Get( *project ).selectedRegion;
double latestEnd =
std::max(ruler->mTracks->GetEndTime(), selectedRegion.t1());
if (ruler->mQuickPlayPos >= latestEnd)
mNewQPIndicatorPos = -1;
else {
// This will determine the x coordinate of the line and of the
// ruler indicator
mNewQPIndicatorPos = ruler->Time2Pos(ruler->mQuickPlayPos);
// These determine which shape is drawn on the ruler, and whether
// in the scrub or the qp zone
mNewScrub =
ruler->mMouseEventState == AdornedRulerPanel::mesNone &&
(ruler->LastCell() == ruler->mScrubbingCell ||
(scrubber.HasMark()));
mNewSeek = mNewScrub &&
(scrubber.Seeks() || scrubber.TemporarilySeeks());
// These two will determine the color of the line stroked over
// the track panel, green for scrub or yellow for snapped or white
mNewPreviewingScrub =
ruler->LastCell() == ruler->mScrubbingCell &&
!scrubber.IsScrubbing();
mNewQPIndicatorSnapped = ruler->mIsSnapped;
}
}
}
unsigned
AdornedRulerPanel::QuickPlayRulerOverlay::SequenceNumber() const
{
return 30;
}
std::pair<wxRect, bool>
AdornedRulerPanel::QuickPlayRulerOverlay::DoGetRectangle(wxSize /*size*/)
{
Update();
const auto x = mOldQPIndicatorPos;
if (x >= 0) {
// These dimensions are always sufficient, even if a little
// excessive for the small triangle:
const int width = IndicatorBigWidth() * 3 / 2;
//const auto height = IndicatorHeightForWidth(width);
const int indsize = width / 2;
auto xx = x - indsize;
auto yy = 0;
return {
{ xx, yy,
indsize * 2 + 1,
GetRuler()->GetSize().GetHeight() },
(x != mNewQPIndicatorPos
|| (mOldScrub != mNewScrub)
|| (mOldSeek != mNewSeek) )
};
}
else
return { {}, mNewQPIndicatorPos >= 0 };
}
void AdornedRulerPanel::QuickPlayRulerOverlay::Draw(
OverlayPanel & /*panel*/, wxDC &dc)
{
mOldQPIndicatorPos = mNewQPIndicatorPos;
mOldScrub = mNewScrub;
mOldSeek = mNewSeek;
if (mOldQPIndicatorPos >= 0) {
auto ruler = GetRuler();
auto width = mOldScrub ? IndicatorBigWidth() : IndicatorSmallWidth;
ruler->DoDrawIndicator(
&dc, mOldQPIndicatorPos, true, width, mOldScrub, mOldSeek);
}
}
/**********************************************************************
Implementation of QuickPlayIndicatorOverlay.
**********************************************************************/
AdornedRulerPanel::QuickPlayIndicatorOverlay::QuickPlayIndicatorOverlay(
SneedacityProject *project)
: mProject(project)
{
}
unsigned
AdornedRulerPanel::QuickPlayIndicatorOverlay::SequenceNumber() const
{
return 30;
}
std::pair<wxRect, bool>
AdornedRulerPanel::QuickPlayIndicatorOverlay::DoGetRectangle(wxSize size)
{
mPartner->Update();
wxRect rect(mOldQPIndicatorPos, 0, 1, size.GetHeight());
return std::make_pair(
rect,
(mOldQPIndicatorPos != mPartner->mNewQPIndicatorPos ||
mOldQPIndicatorSnapped != mPartner->mNewQPIndicatorSnapped ||
mOldPreviewingScrub != mPartner->mNewPreviewingScrub)
);
}
void AdornedRulerPanel::QuickPlayIndicatorOverlay::Draw(
OverlayPanel &panel, wxDC &dc)
{
mOldQPIndicatorPos = mPartner->mNewQPIndicatorPos;
mOldQPIndicatorSnapped = mPartner->mNewQPIndicatorSnapped;
mOldPreviewingScrub = mPartner->mNewPreviewingScrub;
if (mOldQPIndicatorPos >= 0) {
mOldPreviewingScrub
? AColor::IndicatorColor(&dc, true) // Draw green line for preview.
: mOldQPIndicatorSnapped
? AColor::SnapGuidePen(&dc)
: AColor::Light(&dc, false)
;
// Draw indicator in all visible tracks
auto pCellularPanel = dynamic_cast<CellularPanel*>( &panel );
if ( !pCellularPanel ) {
wxASSERT( false );
return;
}
pCellularPanel
->VisitCells( [&]( const wxRect &rect, TrackPanelCell &cell ) {
const auto pTrackView = dynamic_cast<TrackView*>(&cell);
if (!pTrackView)
return;
// Draw the NEW indicator in its NEW location
AColor::Line(dc,
mOldQPIndicatorPos,
rect.GetTop(),
mOldQPIndicatorPos,
rect.GetBottom());
} );
}
}
/**********************************************************************
Implementation of AdornedRulerPanel.
Either we find a way to make this more generic, Or it will move
out of the widgets subdirectory into its own source file.
**********************************************************************/
enum {
OnToggleQuickPlayID = 7000,
OnSyncQuickPlaySelID,
OnAutoScrollID,
OnLockPlayRegionID,
OnTogglePinnedStateID,
};
BEGIN_EVENT_TABLE(AdornedRulerPanel, CellularPanel)
EVT_IDLE( AdornedRulerPanel::OnIdle )
EVT_PAINT(AdornedRulerPanel::OnPaint)
EVT_SIZE(AdornedRulerPanel::OnSize)
// Context menu commands
EVT_MENU(OnToggleQuickPlayID, AdornedRulerPanel::OnToggleQuickPlay)
EVT_MENU(OnSyncQuickPlaySelID, AdornedRulerPanel::OnSyncSelToQuickPlay)
EVT_MENU(OnAutoScrollID, AdornedRulerPanel::OnAutoScroll)
EVT_MENU(OnLockPlayRegionID, AdornedRulerPanel::OnLockPlayRegion)
EVT_MENU( OnTogglePinnedStateID, AdornedRulerPanel::OnTogglePinnedState )
EVT_COMMAND( OnTogglePinnedStateID,
wxEVT_COMMAND_BUTTON_CLICKED,
AdornedRulerPanel::OnPinnedButton )
END_EVENT_TABLE()
class AdornedRulerPanel::CommonCell : public TrackPanelCell
{
public:
explicit
CommonCell( AdornedRulerPanel *parent, MenuChoice menuChoice )
: mParent{ parent }
, mMenuChoice{ menuChoice }
{}
HitTestPreview DefaultPreview
(const TrackPanelMouseState &state, const SneedacityProject *pProject)
override
{
(void)pProject;// Compiler food
(void)state;// Compiler food
// May come here when recording is in progress, so hit tests are turned
// off.
TranslatableString tooltip;
if (mParent->mTimelineToolTip)
tooltip = XO("Timeline actions disabled during recording");
static wxCursor cursor{ wxCURSOR_DEFAULT };
return {
{},
&cursor,
tooltip,
};
}
unsigned DoContextMenu
(const wxRect &rect,
wxWindow *pParent, wxPoint *pPosition, SneedacityProject*) final override
{
(void)pParent;// Compiler food
(void)rect;// Compiler food
mParent->ShowContextMenu(mMenuChoice, pPosition);
return 0;
}
protected:
AdornedRulerPanel *mParent;
const MenuChoice mMenuChoice;
};
class AdornedRulerPanel::CommonRulerHandle : public UIHandle
{
public:
explicit
CommonRulerHandle(
AdornedRulerPanel *pParent, wxCoord xx, MenuChoice menuChoice )
: mParent(pParent)
, mX( xx )
, mChoice( menuChoice )
{}
bool Clicked() const { return mClicked != Button::None; }
static UIHandle::Result NeedChangeHighlight
(const CommonRulerHandle &oldState, const CommonRulerHandle &newState)
{
if (oldState.mX != newState.mX)
return RefreshCode::DrawOverlays;
return 0;
}
protected:
Result Click
(const TrackPanelMouseEvent &event, SneedacityProject *) override
{
mClicked = event.event.LeftIsDown() ? Button::Left : Button::Right;
return RefreshCode::DrawOverlays;
}
Result Drag
(const TrackPanelMouseEvent &, SneedacityProject *) override
{
return RefreshCode::DrawOverlays;
}
Result Release
(const TrackPanelMouseEvent &event, SneedacityProject *,
wxWindow *) override
{
if ( mParent && mClicked == Button::Right ) {
const auto pos = event.event.GetPosition();
mParent->ShowContextMenu( mChoice, &pos );
}
return RefreshCode::DrawOverlays;
}
Result Cancel(SneedacityProject *pProject) override
{
(void)pProject;// Compiler food
return RefreshCode::DrawOverlays;
}
void Enter(bool, SneedacityProject *) override
{
mChangeHighlight = RefreshCode::DrawOverlays;
}
wxWeakRef<AdornedRulerPanel> mParent;
wxCoord mX;
MenuChoice mChoice;
enum class Button { None, Left, Right };
Button mClicked{ Button::None };
};
class AdornedRulerPanel::QPHandle final : public CommonRulerHandle
{
public:
explicit
QPHandle( AdornedRulerPanel *pParent, wxCoord xx )
: CommonRulerHandle( pParent, xx, MenuChoice::QuickPlay )
{
}
std::unique_ptr<SnapManager> mSnapManager;
private:
Result Click
(const TrackPanelMouseEvent &event, SneedacityProject *pProject) override;
Result Drag
(const TrackPanelMouseEvent &event, SneedacityProject *pProject) override;
HitTestPreview Preview
(const TrackPanelMouseState &state, SneedacityProject *pProject)
override;
Result Release
(const TrackPanelMouseEvent &event, SneedacityProject *pProject,
wxWindow *pParent) override;
Result Cancel(SneedacityProject *pProject) override;
SelectedRegion mOldSelection;
};
namespace
{
wxCoord GetPlayHeadX( const SneedacityProject *pProject )
{
const auto &viewInfo = ViewInfo::Get( *pProject );
auto width = viewInfo.GetTracksUsableWidth();
return viewInfo.GetLeftOffset()
+ width * TracksPrefs::GetPinnedHeadPositionPreference();
}
double GetPlayHeadFraction( const SneedacityProject *pProject, wxCoord xx )
{
const auto &viewInfo = ViewInfo::Get( *pProject );
auto width = viewInfo.GetTracksUsableWidth();
auto fraction = (xx - viewInfo.GetLeftOffset()) / double(width);
return std::max(0.0, std::min(1.0, fraction));
}
// Handle for dragging the pinned play head, which so far does not need
// to be a friend of the AdornedRulerPanel class, so we don't make it nested.
class PlayheadHandle : public UIHandle
{
public:
explicit
PlayheadHandle( wxCoord xx )
: mX( xx )
{}
static UIHandle::Result NeedChangeHighlight
(const PlayheadHandle &oldState, const PlayheadHandle &newState)
{
if (oldState.mX != newState.mX)
return RefreshCode::DrawOverlays;
return 0;
}
static std::shared_ptr<PlayheadHandle>
HitTest( const SneedacityProject *pProject, wxCoord xx )
{
if( Scrubber::Get( *pProject )
.IsTransportingPinned() &&
ProjectAudioIO::Get( *pProject ).IsAudioActive() )
{
const auto targetX = GetPlayHeadX( pProject );
if ( abs( xx - targetX ) <= SELECT_TOLERANCE_PIXEL )
return std::make_shared<PlayheadHandle>( xx );
}
return {};
}
protected:
Result Click
(const TrackPanelMouseEvent &event, SneedacityProject *pProject) override
{
(void)pProject;// Compiler food
if (event.event.LeftDClick()) {
// Restore default position on double click
TracksPrefs::SetPinnedHeadPositionPreference( 0.5, true );
return RefreshCode::DrawOverlays |
// Do not start a drag
RefreshCode::Cancelled;
}
// Fix for Bug 2357
if (!event.event.LeftIsDown())
return RefreshCode::Cancelled;
mOrigPreference = TracksPrefs::GetPinnedHeadPositionPreference();
return 0;
}
Result Drag
(const TrackPanelMouseEvent &event, SneedacityProject *pProject) override
{
auto value = GetPlayHeadFraction(pProject, event.event.m_x );
TracksPrefs::SetPinnedHeadPositionPreference( value );
return RefreshCode::DrawOverlays;
}
HitTestPreview Preview
(const TrackPanelMouseState &state, SneedacityProject *pProject)
override
{
(void)pProject;// Compiler food
(void)state;// Compiler food
static wxCursor cursor{ wxCURSOR_SIZEWE };
return {
XO( "Click and drag to adjust, double-click to reset" ),
&cursor,
/* i18n-hint: This text is a tooltip on the icon (of a pin) representing
the temporal position in the audio. */
XO( "Record/Play head" )
};
}
Result Release
(const TrackPanelMouseEvent &event, SneedacityProject *pProject,
wxWindow *) override
{
auto value = GetPlayHeadFraction(pProject, event.event.m_x );
TracksPrefs::SetPinnedHeadPositionPreference( value, true );
return RefreshCode::DrawOverlays;
}
Result Cancel(SneedacityProject *pProject) override
{
(void)pProject;// Compiler food
TracksPrefs::SetPinnedHeadPositionPreference( mOrigPreference );
return RefreshCode::DrawOverlays;
}
void Enter(bool, SneedacityProject *) override
{
mChangeHighlight = RefreshCode::DrawOverlays;
}
wxCoord mX;
double mOrigPreference {};
};
}
class AdornedRulerPanel::QPCell final : public CommonCell
{
public:
explicit
QPCell( AdornedRulerPanel *parent )
: AdornedRulerPanel::CommonCell{ parent, MenuChoice::QuickPlay }
{}
std::vector<UIHandlePtr> HitTest
(const TrackPanelMouseState &state,
const SneedacityProject *pProject) override;
// Return shared_ptr to self, stored in parent
std::shared_ptr<TrackPanelCell> ContextMenuDelegate() override
{ return mParent->mQPCell; }
bool Hit() const { return !mHolder.expired(); }
bool Clicked() const {
if (auto ptr = mHolder.lock())
return ptr->Clicked();
return false;
}
std::weak_ptr<QPHandle> mHolder;
std::weak_ptr<PlayheadHandle> mPlayheadHolder;
};
std::vector<UIHandlePtr> AdornedRulerPanel::QPCell::HitTest
(const TrackPanelMouseState &state,
const SneedacityProject *pProject)
{
// Creation of overlays on demand here -- constructor of AdornedRulerPanel
// is too early to do it
mParent->CreateOverlays();
std::vector<UIHandlePtr> results;
auto xx = state.state.m_x;
#ifdef EXPERIMENTAL_DRAGGABLE_PLAY_HEAD
{
// Allow click and drag on the play head even while recording
// Make this handle more prominent then the quick play handle
auto result = PlayheadHandle::HitTest( pProject, xx );
if (result) {
result = AssignUIHandlePtr( mPlayheadHolder, result );
results.push_back( result );
}
}
#endif
// Disable mouse actions on Timeline while recording.
if (!mParent->mIsRecording) {
mParent->UpdateQuickPlayPos( xx, state.state.ShiftDown() );
auto result = std::make_shared<QPHandle>( mParent, xx );
result = AssignUIHandlePtr( mHolder, result );
results.push_back( result );
}
return results;
}
class AdornedRulerPanel::ScrubbingHandle final : public CommonRulerHandle
{
public:
explicit
ScrubbingHandle( AdornedRulerPanel *pParent, wxCoord xx )
: CommonRulerHandle( pParent, xx, MenuChoice::Scrub )
{
}
private:
Result Click
(const TrackPanelMouseEvent &event, SneedacityProject *pProject) override
{
auto result = CommonRulerHandle::Click(event, pProject);
if (!( result & RefreshCode::Cancelled )) {
if (mClicked == Button::Left) {
auto &scrubber = Scrubber::Get( *pProject );
// only if scrubbing is allowed now
bool canScrub =
scrubber.CanScrub() &&
mParent &&
mParent->ShowingScrubRuler();
if (!canScrub)
return RefreshCode::Cancelled;
if (!scrubber.HasMark()) {
// Asynchronous scrub poller gets activated here
scrubber.MarkScrubStart(
event.event.m_x, Scrubber::ShouldScrubPinned(), false);
}
}
}
return result;
}
Result Drag
(const TrackPanelMouseEvent &event, SneedacityProject *pProject) override
{
auto result = CommonRulerHandle::Drag(event, pProject);
if (!( result & RefreshCode::Cancelled )) {
// Nothing needed here. The scrubber works by polling mouse state
// after the start has been marked.
}
return result;
}
HitTestPreview Preview
(const TrackPanelMouseState &state, SneedacityProject *pProject)
override;
Result Release
(const TrackPanelMouseEvent &event, SneedacityProject *pProject,
wxWindow *pParent) override {
auto result = CommonRulerHandle::Release(event, pProject, pParent);
if (!( result & RefreshCode::Cancelled )) {
// Nothing needed here either. The scrub poller may have decided to
// seek because a drag happened before button up, or it may decide
// to start a scrub, as it watches mouse movement after the button up.
}
return result;
}
Result Cancel(SneedacityProject *pProject) override
{
auto result = CommonRulerHandle::Cancel(pProject);
if (mClicked == Button::Left) {
auto &scrubber = Scrubber::Get( *pProject );
scrubber.Cancel();
ProjectAudioManager::Get( *pProject ).Stop();
}
return result;
}
};
class AdornedRulerPanel::ScrubbingCell final : public CommonCell
{
public:
explicit
ScrubbingCell( AdornedRulerPanel *parent )
: AdornedRulerPanel::CommonCell{ parent, MenuChoice::Scrub }
{}
std::vector<UIHandlePtr> HitTest
(const TrackPanelMouseState &state,
const SneedacityProject *pProject) override;
// Return shared_ptr to self, stored in parent
std::shared_ptr<TrackPanelCell> ContextMenuDelegate() override
{ return mParent->mScrubbingCell; }
bool Hit() const { return !mHolder.expired(); }
bool Clicked() const {
if (auto ptr = mHolder.lock())
return ptr->Clicked();
return false;
}
private:
std::weak_ptr<ScrubbingHandle> mHolder;
};
std::vector<UIHandlePtr> AdornedRulerPanel::ScrubbingCell::HitTest
(const TrackPanelMouseState &state,
const SneedacityProject *pProject)
{
(void)pProject;// Compiler food
// Creation of overlays on demand here -- constructor of AdornedRulerPanel
// is too early to do it
mParent->CreateOverlays();
std::vector<UIHandlePtr> results;
// Disable mouse actions on Timeline while recording.
if (!mParent->mIsRecording) {
auto xx = state.state.m_x;
mParent->UpdateQuickPlayPos( xx, state.state.ShiftDown() );
auto result = std::make_shared<ScrubbingHandle>( mParent, xx );
result = AssignUIHandlePtr( mHolder, result );
results.push_back( result );
}
return results;
}
namespace{
SneedacityProject::AttachedWindows::RegisteredFactory sKey{
[]( SneedacityProject &project ) -> wxWeakRef< wxWindow > {
auto &viewInfo = ViewInfo::Get( project );
auto &window = ProjectWindow::Get( project );
return safenew AdornedRulerPanel( &project, window.GetTopPanel(),
wxID_ANY,
wxDefaultPosition,
wxSize( -1, AdornedRulerPanel::GetRulerHeight(false) ),
&viewInfo );
}
};
}
AdornedRulerPanel &AdornedRulerPanel::Get( SneedacityProject &project )
{
return project.AttachedWindows::Get< AdornedRulerPanel >( sKey );
}
const AdornedRulerPanel &AdornedRulerPanel::Get( const SneedacityProject &project )
{
return Get( const_cast< SneedacityProject & >( project ) );
}
void AdornedRulerPanel::Destroy( SneedacityProject &project )
{
auto *pPanel = project.AttachedWindows::Find( sKey );
if (pPanel) {
pPanel->wxWindow::Destroy();
project.AttachedWindows::Assign( sKey, nullptr );
}
}
AdornedRulerPanel::AdornedRulerPanel(SneedacityProject* project,
wxWindow *parent,
wxWindowID id,
const wxPoint& pos,
const wxSize& size,
ViewInfo *viewinfo)
: CellularPanel(parent, id, pos, size, viewinfo)
, mProject(project)
{
SetLayoutDirection(wxLayout_LeftToRight);
mQPCell = std::make_shared<QPCell>( this );
mScrubbingCell = std::make_shared<ScrubbingCell>( this );
for (auto &button : mButtons)
button = nullptr;
SetLabel( XO("Timeline") );
SetName();
SetBackgroundStyle(wxBG_STYLE_PAINT);
mLeftOffset = 0;
mIndTime = -1;
mLeftDownClick = -1;
mMouseEventState = mesNone;
mIsDragging = false;
mOuter = GetClientRect();
mRuler.SetUseZoomInfo(mLeftOffset, mViewInfo);
mRuler.SetLabelEdges( false );
mRuler.SetFormat( Ruler::TimeFormat );
mTracks = &TrackList::Get( *project );
mIsSnapped = false;
mIsRecording = false;
mTimelineToolTip = !!gPrefs->Read(wxT("/QuickPlay/ToolTips"), 1L);
mPlayRegionDragsSelection = (gPrefs->Read(wxT("/QuickPlay/DragSelection"), 0L) == 1)? true : false;
mQuickPlayEnabled = !!gPrefs->Read(wxT("/QuickPlay/QuickPlayEnabled"), 1L);
#if wxUSE_TOOLTIPS
wxToolTip::Enable(true);
#endif
wxTheApp->Bind(EVT_AUDIOIO_CAPTURE,
&AdornedRulerPanel::OnAudioStartStop,
this);
wxTheApp->Bind(EVT_AUDIOIO_PLAYBACK,
&AdornedRulerPanel::OnAudioStartStop,
this);
// Delay until after CommandManager has been populated:
this->CallAfter( &AdornedRulerPanel::UpdatePrefs );
wxTheApp->Bind(EVT_THEME_CHANGE, &AdornedRulerPanel::OnThemeChange, this);
// Bind event that updates the play region
mViewInfo->selectedRegion.Bind(EVT_SELECTED_REGION_CHANGE,
&AdornedRulerPanel::OnSelectionChange, this);
// And call it once to initialize it
DoSelectionChange( mViewInfo->selectedRegion );
}
AdornedRulerPanel::~AdornedRulerPanel()
{
}
void AdornedRulerPanel::Refresh( bool eraseBackground, const wxRect *rect )
{
CellularPanel::Refresh( eraseBackground, rect );
CallAfter([this]{ CellularPanel::HandleCursorForPresentMouseState(); } );
}
void AdornedRulerPanel::UpdatePrefs()
{
if (mNeedButtonUpdate) {
// Visit this block once only in the lifetime of this panel
mNeedButtonUpdate = false;
// Do this first time setting of button status texts
// when we are sure the CommandManager is initialized.
ReCreateButtons();
}
// Update button texts for language change
UpdateButtonStates();
mTimelineToolTip = !!gPrefs->Read(wxT("/QuickPlay/ToolTips"), 1L);
#ifdef EXPERIMENTAL_SCROLLING_LIMITS
#ifdef EXPERIMENTAL_TWO_TONE_TIME_RULER
{
bool scrollBeyondZero = false;
gPrefs->Read(TracksBehaviorsPrefs::ScrollingPreferenceKey(), &scrollBeyondZero,
TracksBehaviorsPrefs::ScrollingPreferenceDefault());
mRuler.SetTwoTone(scrollBeyondZero);
}
#endif
#endif
}
void AdornedRulerPanel::ReCreateButtons()
{
// TODO: Should we do this to destroy the grabber??
// Get rid of any children we may have