forked from Sneeds-Feed-and-Seed/sneedacity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrackPanel.cpp
1548 lines (1300 loc) · 47.6 KB
/
TrackPanel.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
TrackPanel.cpp
Dominic Mazzoni
and lots of other contributors
Implements TrackPanel.
********************************************************************//*!
\file TrackPanel.cpp
\brief
Implements TrackPanel.
*//***************************************************************//**
\class TrackPanel
\brief
The TrackPanel class coordinates updates and operations on the
main part of the screen which contains multiple tracks.
It uses many other classes, but in particular it uses the
TrackInfo class to draw the controls area on the left of a track,
and the TrackArtist class to draw the actual waveforms.
Note that in some of the older code here, e.g., GetLabelWidth(),
"Label" means the TrackInfo plus the vertical ruler.
Confusing relative to LabelTrack labels.
The TrackPanel manages multiple tracks and their TrackInfos.
Note that with stereo tracks there will be one TrackInfo
being used by two wavetracks.
*//*****************************************************************//**
\class TrackPanel::SneedacityTimer
\brief Timer class dedicated to informing the TrackPanel that it
is time to refresh some aspect of the screen.
*//*****************************************************************/
#include "TrackPanel.h"
#include <wx/setup.h> // for wxUSE_* macros
#include "AdornedRulerPanel.h"
#include "KeyboardCapture.h"
#include "Project.h"
#include "ProjectAudioIO.h"
#include "ProjectAudioManager.h"
#include "ProjectHistory.h"
#include "ProjectSettings.h"
#include "ProjectStatus.h"
#include "ProjectWindow.h"
#include "Theme.h"
#include "TrackPanelMouseEvent.h"
#include "TrackPanelResizeHandle.h"
//#define DEBUG_DRAW_TIMING 1
#include "UndoManager.h"
#include "AColor.h"
#include "AllThemeResources.h"
#include "AudioIO.h"
#include "float_cast.h"
#include "Prefs.h"
#include "RefreshCode.h"
#include "TrackArtist.h"
#include "TrackPanelAx.h"
#include "TrackPanelResizerCell.h"
#include "WaveTrack.h"
#include "tracks/ui/TrackControls.h"
#include "tracks/ui/TrackView.h"
#include "tracks/ui/TrackVRulerControls.h"
//This loads the appropriate set of cursors, depending on platform.
#include "../images/Cursors.h"
#include <algorithm>
#include <wx/dc.h>
#include <wx/dcclient.h>
#include <wx/graphics.h>
/**
\class TrackPanel
This is a diagram of TrackPanel's division of one (non-stereo) track rectangle.
Total height equals TrackView::GetHeight()'s value. Total width is the wxWindow's
width. Each character that is not . represents one pixel.
Inset space of this track, and top inset of the next track, are used to draw the
focus highlight.
Top inset of the right channel of a stereo track, and bottom shadow line of the
left channel, are used for the channel separator.
"Margin" is a term used for inset plus border (top and left) or inset plus
shadow plus border (right and bottom).
GetVRulerOffset() counts columns from the left edge up to and including
controls, and is a constant.
GetVRulerWidth() is variable -- all tracks have the same ruler width at any
time, but that width may be adjusted when tracks change their vertical scales.
GetLabelWidth() counts columns up to and including the VRuler.
GetLeftOffset() is yet one more -- it counts the "one pixel" column.
Cell for label has a rectangle that OMITS left, top, and bottom
margins
Cell for vruler has a rectangle right of the label,
up to and including the One Pixel column, and OMITS top and bottom margins
Cell() for track returns a rectangle with x == GetLeftOffset(), and OMITS
right, top, and bottom margins
+--------------- ... ------ ... --------------------- ... ... -------------+
| Top Inset |
| |
| +------------ ... ------ ... --------------------- ... ... ----------+ |
| L|+-Border---- ... ------ ... --------------------- ... ... -Border-+ |R |
| e||+---------- ... -++--- ... -+++----------------- ... ... -------+| |i |
| f|B| || ||| |BS|g |
| t|o| Controls || V |O| The good stuff |oh|h |
| |r| || R |n| |ra|t |
| I|d| || u |e| |dd| |
| n|e| || l | | |eo|I |
| s|r| || e |P| |rw|n |
| e||| || r |i| ||||s |
| t||| || |x| ||||e |
| ||| || |e| ||||t |
| ||| || |l| |||| |
| ||| || ||| |||| |
. ... .. ... .... .
. ... .. ... .... .
. ... .. ... .... .
| ||| || ||| |||| |
| ||+---------- -++-- ... -+++----------------- ... ... -------+||| |
| |+-Border---- ... ----- ... --------------------- ... ... -Border-+|| |
| | Shadow---- ... ----- ... --------------------- ... ... --Shadow-+| |
*/
// Is the distance between A and B less than D?
template < class A, class B, class DIST > bool within(A a, B b, DIST d)
{
return (a > b - d) && (a < b + d);
}
BEGIN_EVENT_TABLE(TrackPanel, CellularPanel)
EVT_MOUSE_EVENTS(TrackPanel::OnMouseEvent)
EVT_KEY_DOWN(TrackPanel::OnKeyDown)
EVT_PAINT(TrackPanel::OnPaint)
EVT_TIMER(wxID_ANY, TrackPanel::OnTimer)
EVT_SIZE(TrackPanel::OnSize)
END_EVENT_TABLE()
/// Makes a cursor from an XPM, uses CursorId as a fallback.
/// TODO: Move this function to some other source file for reuse elsewhere.
std::unique_ptr<wxCursor> MakeCursor( int WXUNUSED(CursorId), const char * const pXpm[36], int HotX, int HotY )
{
#define CURSORS_SIZE32
#ifdef CURSORS_SIZE32
const int HotAdjust =0;
#else
const int HotAdjust =8;
#endif
wxImage Image = wxImage(wxBitmap(pXpm).ConvertToImage());
Image.SetMaskColour(255,0,0);
Image.SetMask();// Enable mask.
Image.SetOption( wxIMAGE_OPTION_CUR_HOTSPOT_X, HotX-HotAdjust );
Image.SetOption( wxIMAGE_OPTION_CUR_HOTSPOT_Y, HotY-HotAdjust );
return std::make_unique<wxCursor>( Image );
}
namespace{
SneedacityProject::AttachedWindows::RegisteredFactory sKey{
[]( SneedacityProject &project ) -> wxWeakRef< wxWindow > {
auto &ruler = AdornedRulerPanel::Get( project );
auto &viewInfo = ViewInfo::Get( project );
auto &window = ProjectWindow::Get( project );
auto mainPage = window.GetMainPage();
wxASSERT( mainPage ); // to justify safenew
auto &tracks = TrackList::Get( project );
auto result = safenew TrackPanel(mainPage,
window.NextWindowID(),
wxDefaultPosition,
wxDefaultSize,
tracks.shared_from_this(),
&viewInfo,
&project,
&ruler);
project.SetPanel( result );
return result;
}
};
}
TrackPanel &TrackPanel::Get( SneedacityProject &project )
{
return project.AttachedWindows::Get< TrackPanel >( sKey );
}
const TrackPanel &TrackPanel::Get( const SneedacityProject &project )
{
return Get( const_cast< SneedacityProject & >( project ) );
}
void TrackPanel::Destroy( SneedacityProject &project )
{
auto *pPanel = project.AttachedWindows::Find( sKey );
if (pPanel) {
pPanel->wxWindow::Destroy();
project.AttachedWindows::Assign( sKey, nullptr );
}
}
// Don't warn us about using 'this' in the base member initializer list.
#ifndef __WXGTK__ //Get rid if this pragma for gtk
#pragma warning( disable: 4355 )
#endif
TrackPanel::TrackPanel(wxWindow * parent, wxWindowID id,
const wxPoint & pos,
const wxSize & size,
const std::shared_ptr<TrackList> &tracks,
ViewInfo * viewInfo,
SneedacityProject * project,
AdornedRulerPanel * ruler)
: CellularPanel(parent, id, pos, size, viewInfo,
wxWANTS_CHARS | wxNO_BORDER),
mListener( &ProjectWindow::Get( *project ) ),
mTracks(tracks),
mRuler(ruler),
mTrackArtist(nullptr),
mRefreshBacking(false)
#ifndef __WXGTK__ //Get rid if this pragma for gtk
#pragma warning( default: 4355 )
#endif
{
SetLayoutDirection(wxLayout_LeftToRight);
SetLabel(XO("Track Panel"));
SetName(XO("Track Panel"));
SetBackgroundStyle(wxBG_STYLE_PAINT);
{
auto pAx = std::make_unique <TrackPanelAx>( *project );
pAx->SetWindow( this );
wxWeakRef< TrackPanel > weakThis{ this };
pAx->SetFinder(
[weakThis]( const Track &track ) -> wxRect {
if (weakThis)
return weakThis->FindTrackRect( &track );
return {};
}
);
TrackFocus::Get( *GetProject() ).SetAccessible(
*this, std::move( pAx ) );
}
mTrackArtist = std::make_unique<TrackArtist>( this );
mTimeCount = 0;
mTimer.parent = this;
// Timer is started after the window is visible
ProjectWindow::Get( *GetProject() ).Bind(wxEVT_IDLE,
&TrackPanel::OnIdle, this);
// Register for tracklist updates
mTracks->Bind(EVT_TRACKLIST_RESIZING,
&TrackPanel::OnTrackListResizing,
this);
mTracks->Bind(EVT_TRACKLIST_ADDITION,
&TrackPanel::OnTrackListResizing,
this);
mTracks->Bind(EVT_TRACKLIST_DELETION,
&TrackPanel::OnTrackListDeletion,
this);
mTracks->Bind(EVT_TRACKLIST_TRACK_REQUEST_VISIBLE,
&TrackPanel::OnEnsureVisible,
this);
auto theProject = GetProject();
theProject->Bind(
EVT_PROJECT_SETTINGS_CHANGE, &TrackPanel::OnProjectSettingsChange, this);
theProject->Bind(
EVT_TRACK_FOCUS_CHANGE, &TrackPanel::OnTrackFocusChange, this );
theProject->Bind(EVT_UNDO_RESET, &TrackPanel::OnUndoReset, this);
wxTheApp->Bind(EVT_AUDIOIO_PLAYBACK,
&TrackPanel::OnAudioIO,
this);
wxTheApp->Bind(EVT_AUDIOIO_CAPTURE,
&TrackPanel::OnAudioIO,
this);
UpdatePrefs();
}
TrackPanel::~TrackPanel()
{
mTimer.Stop();
// This can happen if a label is being edited and the user presses
// ALT+F4 or Command+Q
if (HasCapture())
ReleaseMouse();
}
void TrackPanel::UpdatePrefs()
{
// All vertical rulers must be recalculated since the minimum and maximum
// frequencies may have been changed.
UpdateVRulers();
Refresh();
}
/// Gets the pointer to the SneedacityProject that
/// goes with this track panel.
SneedacityProject * TrackPanel::GetProject() const
{
//JKC casting away constness here.
//Do it in two stages in case 'this' is not a wxWindow.
//when the compiler will flag the error.
wxWindow const * const pConstWind = this;
wxWindow * pWind=(wxWindow*)pConstWind;
#ifdef EXPERIMENTAL_NOTEBOOK
pWind = pWind->GetParent(); //Page
wxASSERT( pWind );
pWind = pWind->GetParent(); //Notebook
wxASSERT( pWind );
#endif
pWind = pWind->GetParent(); //MainPanel
wxASSERT( pWind );
pWind = pWind->GetParent(); //ProjectWindow
wxASSERT( pWind );
return &static_cast<ProjectWindow*>( pWind )->GetProject();
}
void TrackPanel::OnSize( wxSizeEvent &evt )
{
evt.Skip();
const auto &size = evt.GetSize();
mViewInfo->SetWidth( size.GetWidth() );
mViewInfo->SetHeight( size.GetHeight() );
}
void TrackPanel::OnIdle(wxIdleEvent& event)
{
event.Skip();
// The window must be ready when the timer fires (#1401)
if (IsShownOnScreen())
{
mTimer.Start(kTimerInterval, FALSE);
// Timer is started, we don't need the event anymore
GetProjectFrame( *GetProject() ).Unbind(wxEVT_IDLE,
&TrackPanel::OnIdle, this);
}
else
{
// Get another idle event, wx only guarantees we get one
// event after "some other normal events occur"
event.RequestMore();
}
}
/// AS: This gets called on our wx timer events.
void TrackPanel::OnTimer(wxTimerEvent& )
{
mTimeCount++;
SneedacityProject *const p = GetProject();
auto &window = ProjectWindow::Get( *p );
auto &projectAudioIO = ProjectAudioIO::Get( *p );
auto gAudioIO = AudioIO::Get();
// Check whether we were playing or recording, but the stream has stopped.
if (projectAudioIO.GetAudioIOToken()>0 && !IsAudioActive())
{
//the stream may have been started up after this one finished (by some other project)
//in that case reset the buttons don't stop the stream
auto &projectAudioManager = ProjectAudioManager::Get( *p );
projectAudioManager.Stop(!gAudioIO->IsStreamActive());
}
// Next, check to see if we were playing or recording
// audio, but now Audio I/O is completely finished.
if (projectAudioIO.GetAudioIOToken()>0 &&
!gAudioIO->IsAudioTokenActive(projectAudioIO.GetAudioIOToken()))
{
projectAudioIO.SetAudioIOToken(0);
window.RedrawProject();
}
if (mLastDrawnSelectedRegion != mViewInfo->selectedRegion) {
UpdateSelectionDisplay();
}
// Notify listeners for timer ticks
{
wxCommandEvent e(EVT_TRACK_PANEL_TIMER);
p->ProcessEvent(e);
}
DrawOverlays(false);
mRuler->DrawOverlays(false);
if(IsAudioActive() && gAudioIO->GetNumCaptureChannels()) {
// Periodically update the display while recording
if ((mTimeCount % 5) == 0) {
// Must tell OnPaint() to recreate the backing bitmap
// since we've not done a full refresh.
mRefreshBacking = true;
Refresh( false );
}
}
if(mTimeCount > 1000)
mTimeCount = 0;
}
void TrackPanel::OnProjectSettingsChange( wxCommandEvent &event )
{
event.Skip();
switch ( static_cast<ProjectSettings::EventCode>( event.GetInt() ) ) {
case ProjectSettings::ChangedSyncLock:
Refresh(false);
break;
default:
break;
}
}
void TrackPanel::OnUndoReset( wxCommandEvent &event )
{
event.Skip();
TrackFocus::Get( *GetProject() ).Set( nullptr );
Refresh( false );
}
/// AS: OnPaint( ) is called during the normal course of
/// completing a repaint operation.
void TrackPanel::OnPaint(wxPaintEvent & /* event */)
{
mLastDrawnSelectedRegion = mViewInfo->selectedRegion;
#if DEBUG_DRAW_TIMING
wxStopWatch sw;
#endif
{
wxPaintDC dc(this);
// Retrieve the damage rectangle
wxRect box = GetUpdateRegion().GetBox();
// Recreate the backing bitmap if we have a full refresh
// (See TrackPanel::Refresh())
if (mRefreshBacking || (box == GetRect()))
{
// Reset (should a mutex be used???)
mRefreshBacking = false;
// Redraw the backing bitmap
DrawTracks(&GetBackingDCForRepaint());
// Copy it to the display
DisplayBitmap(dc);
}
else
{
// Copy full, possibly clipped, damage rectangle
RepairBitmap(dc, box.x, box.y, box.width, box.height);
}
// Done with the clipped DC
// Drawing now goes directly to the client area.
// DrawOverlays() may need to draw outside the clipped region.
// (Used to make a NEW, separate wxClientDC, but that risks flashing
// problems on Mac.)
dc.DestroyClippingRegion();
DrawOverlays(true, &dc);
}
#if DEBUG_DRAW_TIMING
sw.Pause();
wxLogDebug(wxT("Total: %ld milliseconds"), sw.Time());
wxPrintf(wxT("Total: %ld milliseconds\n"), sw.Time());
#endif
}
void TrackPanel::MakeParentRedrawScrollbars()
{
mListener->TP_RedrawScrollbars();
}
namespace {
std::shared_ptr<Track> FindTrack(TrackPanelCell *pCell )
{
if (pCell)
return static_cast<CommonTrackPanelCell*>( pCell )->FindTrack();
return {};
}
}
void TrackPanel::ProcessUIHandleResult
(TrackPanelCell *pClickedCell, TrackPanelCell *pLatestCell,
UIHandle::Result refreshResult)
{
const auto panel = this;
auto pLatestTrack = FindTrack( pLatestCell ).get();
// This precaution checks that the track is not only nonnull, but also
// really owned by the track list
auto pClickedTrack = GetTracks()->Lock(
std::weak_ptr<Track>{ FindTrack( pClickedCell ) }
).get();
// TODO: make a finer distinction between refreshing the track control area,
// and the waveform area. As it is, redraw both whenever you must redraw either.
// Copy data from the underlying tracks to the pending tracks that are
// really displayed
TrackList::Get( *panel->GetProject() ).UpdatePendingTracks();
using namespace RefreshCode;
if (refreshResult & DestroyedCell) {
panel->UpdateViewIfNoTracks();
// Beware stale pointer!
if (pLatestTrack == pClickedTrack)
pLatestTrack = NULL;
pClickedTrack = NULL;
}
if (pClickedTrack && (refreshResult & RefreshCode::UpdateVRuler))
panel->UpdateVRuler(pClickedTrack);
if (refreshResult & RefreshCode::DrawOverlays) {
panel->DrawOverlays(false);
mRuler->DrawOverlays(false);
}
// Refresh all if told to do so, or if told to refresh a track that
// is not known.
const bool refreshAll =
( (refreshResult & RefreshAll)
|| ((refreshResult & RefreshCell) && !pClickedTrack)
|| ((refreshResult & RefreshLatestCell) && !pLatestTrack));
if (refreshAll)
panel->Refresh(false);
else {
if (refreshResult & RefreshCell)
panel->RefreshTrack(pClickedTrack);
if (refreshResult & RefreshLatestCell)
panel->RefreshTrack(pLatestTrack);
}
if (refreshResult & FixScrollbars)
panel->MakeParentRedrawScrollbars();
if (refreshResult & Resize)
panel->GetListener()->TP_HandleResize();
if ((refreshResult & RefreshCode::EnsureVisible) && pClickedTrack) {
TrackFocus::Get(*GetProject()).Set(pClickedTrack);
pClickedTrack->EnsureVisible();
}
}
void TrackPanel::HandlePageUpKey()
{
mListener->TP_ScrollWindow(2 * mViewInfo->h - mViewInfo->GetScreenEndTime());
}
void TrackPanel::HandlePageDownKey()
{
mListener->TP_ScrollWindow(mViewInfo->GetScreenEndTime());
}
bool TrackPanel::IsAudioActive()
{
SneedacityProject *p = GetProject();
return ProjectAudioIO::Get( *p ).IsAudioActive();
}
void TrackPanel::UpdateStatusMessage( const TranslatableString &st )
{
auto status = st;
if (HasEscape())
/* i18n-hint Esc is a key on the keyboard */
status.Join( XO("(Esc to cancel)"), " " );
ProjectStatus::Get( *GetProject() ).Set( status );
}
void TrackPanel::UpdateSelectionDisplay()
{
// Full refresh since the label area may need to indicate
// newly selected tracks.
Refresh(false);
// Make sure the ruler follows suit.
mRuler->DrawSelection();
}
// Counts selected tracks, counting stereo tracks as one track.
size_t TrackPanel::GetSelectedTrackCount() const
{
return GetTracks()->SelectedLeaders().size();
}
void TrackPanel::UpdateViewIfNoTracks()
{
if (mTracks->empty())
{
// BG: There are no more tracks on screen
//BG: Set zoom to normal
mViewInfo->SetZoom(ZoomInfo::GetDefaultZoom());
//STM: Set selection to 0,0
//PRL: and default the rest of the selection information
mViewInfo->selectedRegion = SelectedRegion();
// PRL: Following causes the time ruler to align 0 with left edge.
// Bug 972
mViewInfo->h = 0;
mListener->TP_HandleResize();
//STM: Clear message if all tracks are removed
ProjectStatus::Get( *GetProject() ).Set({});
}
}
// The tracks positions within the list have changed, so update the vertical
// ruler size for the track that triggered the event.
void TrackPanel::OnTrackListResizing(TrackListEvent & e)
{
auto t = e.mpTrack.lock();
// A deleted track can trigger the event. In which case do nothing here.
// A deleted track can have a valid pointer but no owner, bug 2060
if( t && t->HasOwner() )
UpdateVRuler(t.get());
e.Skip();
// fix for bug 2477
mListener->TP_RedrawScrollbars();
}
// Tracks have been removed from the list.
void TrackPanel::OnTrackListDeletion(wxEvent & e)
{
// copy shared_ptr for safety, as in HandleClick
auto handle = Target();
if (handle) {
handle->OnProjectChange(GetProject());
}
// If the focused track disappeared but there are still other tracks,
// this reassigns focus.
TrackFocus( *GetProject() ).Get();
UpdateVRulerSize();
e.Skip();
}
void TrackPanel::OnKeyDown(wxKeyEvent & event)
{
switch (event.GetKeyCode())
{
// Allow PageUp and PageDown keys to
//scroll the Track Panel left and right
case WXK_PAGEUP:
HandlePageUpKey();
return;
case WXK_PAGEDOWN:
HandlePageDownKey();
return;
default:
// fall through to base class handler
event.Skip();
}
}
void TrackPanel::OnMouseEvent(wxMouseEvent & event)
{
if (event.LeftDown()) {
// wxTimers seem to be a little unreliable, so this
// "primes" it to make sure it keeps going for a while...
// When this timer fires, we call TrackPanel::OnTimer and
// possibly update the screen for offscreen scrolling.
mTimer.Stop();
mTimer.Start(kTimerInterval, FALSE);
}
if (event.ButtonUp()) {
//EnsureVisible should be called after processing the up-click.
this->CallAfter( [this, event]{
const auto foundCell = FindCell(event.m_x, event.m_y);
const auto t = FindTrack( foundCell.pCell.get() );
if ( t ) {
TrackFocus::Get(*GetProject()).Set(t.get());
t->EnsureVisible();
}
} );
}
// Must also fall through to base class handler
event.Skip();
}
double TrackPanel::GetMostRecentXPos()
{
return mViewInfo->PositionToTime(
MostRecentXCoord(), mViewInfo->GetLabelWidth());
}
void TrackPanel::RefreshTrack(Track *trk, bool refreshbacking)
{
if (!trk)
return;
trk = *GetTracks()->FindLeader(trk);
auto &view = TrackView::Get( *trk );
auto height =
TrackList::Channels(trk).sum( TrackView::GetTrackHeight )
- kTopInset - kShadowThickness;
// subtract insets and shadows from the rectangle, but not border
// This matters because some separators do paint over the border
wxRect rect(kLeftInset,
-mViewInfo->vpos + view.GetY() + kTopInset,
GetRect().GetWidth() - kLeftInset - kRightInset - kShadowThickness,
height);
if( refreshbacking )
{
mRefreshBacking = true;
}
Refresh( false, &rect );
}
/// This method overrides Refresh() of wxWindow so that the
/// boolean play indicator can be set to false, so that an old play indicator that is
/// no longer there won't get XORed (to erase it), thus redrawing it on the
/// TrackPanel
void TrackPanel::Refresh(bool eraseBackground /* = TRUE */,
const wxRect *rect /* = NULL */)
{
// Tell OnPaint() to refresh the backing bitmap.
//
// Originally I had the check within the OnPaint() routine and it
// was working fine. That was until I found that, even though a full
// refresh was requested, Windows only set the onscreen portion of a
// window as damaged.
//
// So, if any part of the trackpanel was off the screen, full refreshes
// didn't work and the display got corrupted.
if( !rect || ( *rect == GetRect() ) )
{
mRefreshBacking = true;
}
wxWindow::Refresh(eraseBackground, rect);
CallAfter([this]{ CellularPanel::HandleCursorForPresentMouseState(); } );
}
void TrackPanel::OnAudioIO(wxCommandEvent & evt)
{
evt.Skip();
// Some hit tests want to change their cursor to and from the ban symbol
CallAfter( [this]{ CellularPanel::HandleCursorForPresentMouseState(); } );
}
#include "TrackPanelDrawingContext.h"
/// Draw the actual track areas. We only draw the borders
/// and the little buttons and menues and whatnot here, the
/// actual contents of each track are drawn by the TrackArtist.
void TrackPanel::DrawTracks(wxDC * dc)
{
wxRegion region = GetUpdateRegion();
const wxRect clip = GetRect();
const SelectedRegion &sr = mViewInfo->selectedRegion;
mTrackArtist->pSelectedRegion = &sr;
mTrackArtist->pZoomInfo = mViewInfo;
TrackPanelDrawingContext context {
*dc, Target(), mLastMouseState, mTrackArtist.get()
};
// Don't draw a bottom margin here.
const auto &settings = ProjectSettings::Get( *GetProject() );
bool bMultiToolDown =
(ToolCodes::multiTool == settings.GetTool());
bool envelopeFlag =
bMultiToolDown || (ToolCodes::envelopeTool == settings.GetTool());
bool bigPointsFlag =
bMultiToolDown || (ToolCodes::drawTool == settings.GetTool());
bool sliderFlag = bMultiToolDown;
const bool hasSolo = GetTracks()->Any< PlayableTrack >()
.any_of( []( const PlayableTrack *pt ) {
pt = static_cast< const PlayableTrack * >(
pt->SubstitutePendingChangedTrack().get() );
return (pt && pt->GetSolo());
} );
mTrackArtist->drawEnvelope = envelopeFlag;
mTrackArtist->bigPoints = bigPointsFlag;
mTrackArtist->drawSliders = sliderFlag;
mTrackArtist->hasSolo = hasSolo;
this->CellularPanel::Draw( context, TrackArtist::NPasses );
}
void TrackPanel::SetBackgroundCell
(const std::shared_ptr< TrackPanelCell > &pCell)
{
mpBackground = pCell;
}
std::shared_ptr< TrackPanelCell > TrackPanel::GetBackgroundCell()
{
return mpBackground;
}
void TrackPanel::UpdateVRulers()
{
for (auto t : GetTracks()->Any< WaveTrack >())
UpdateTrackVRuler(t);
UpdateVRulerSize();
}
void TrackPanel::UpdateVRuler(Track *t)
{
if (t)
UpdateTrackVRuler(t);
UpdateVRulerSize();
}
void TrackPanel::UpdateTrackVRuler(Track *t)
{
wxASSERT(t);
if (!t)
return;
wxRect rect(mViewInfo->GetVRulerOffset(),
0,
mViewInfo->GetVRulerWidth(),
0);
for (auto channel : TrackList::Channels(t)) {
auto &view = TrackView::Get( *channel );
const auto height = view.GetHeight() - (kTopMargin + kBottomMargin);
rect.SetHeight( height );
const auto subViews = view.GetSubViews( rect );
if (subViews.empty())
continue;
auto iter = subViews.begin(), end = subViews.end(), next = iter;
auto yy = iter->first;
wxSize vRulerSize{ 0, 0 };
for ( ; iter != end; iter = next ) {
++next;
auto nextY = ( next == end )
? height
: next->first;
rect.SetHeight( nextY - yy );
// This causes ruler size in the track to be reassigned:
TrackVRulerControls::Get( *iter->second ).UpdateRuler( rect );
// But we want to know the maximum width and height over all sub-views:
vRulerSize.IncTo( t->vrulerSize );
yy = nextY;
}
t->vrulerSize = vRulerSize;
}
}
void TrackPanel::UpdateVRulerSize()
{
auto trackRange = GetTracks()->Any();
if (trackRange) {
wxSize s { 0, 0 };
for (auto t : trackRange)
s.IncTo(t->vrulerSize);
if (mViewInfo->GetVRulerWidth() != s.GetWidth()) {
mViewInfo->SetVRulerWidth( s.GetWidth() );
mRuler->SetLeftOffset(
mViewInfo->GetLeftOffset()); // bevel on AdornedRuler
mRuler->Refresh();
}
}
Refresh(false);
}
void TrackPanel::OnTrackMenu(Track *t)
{
CellularPanel::DoContextMenu( t ? &TrackView::Get( *t ) : nullptr );
}
// Tracks have been removed from the list.
void TrackPanel::OnEnsureVisible(TrackListEvent & e)
{
e.Skip();
bool modifyState = e.GetInt();
auto pTrack = e.mpTrack.lock();
auto t = pTrack.get();
int trackTop = 0;
int trackHeight =0;
for (auto it : GetTracks()->Leaders()) {
trackTop += trackHeight;
auto channels = TrackList::Channels(it);
trackHeight = channels.sum( TrackView::GetTrackHeight );
//We have found the track we want to ensure is visible.
if (channels.contains(t)) {
//Get the size of the trackpanel.
int width, height;
GetSize(&width, &height);
if (trackTop < mViewInfo->vpos) {
height = mViewInfo->vpos - trackTop + mViewInfo->scrollStep;
height /= mViewInfo->scrollStep;
mListener->TP_ScrollUpDown(-height);
}
else if (trackTop + trackHeight > mViewInfo->vpos + height) {
height = (trackTop + trackHeight) - (mViewInfo->vpos + height);
height = (height + mViewInfo->scrollStep + 1) / mViewInfo->scrollStep;
mListener->TP_ScrollUpDown(height);
}
break;
}
}
Refresh(false);
if ( modifyState )
ProjectHistory::Get( *GetProject() ).ModifyState( false );
}
// 0.0 scrolls to top
// 1.0 scrolls to bottom.
void TrackPanel::VerticalScroll( float fracPosition){
int trackTop = 0;
int trackHeight = 0;
auto tracks = GetTracks();
auto range = tracks->Leaders();
if (!range.empty()) {
trackHeight = TrackView::GetChannelGroupHeight( *range.rbegin() );
--range.second;
}