-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Ruler.cpp
1835 lines (1563 loc) · 49.1 KB
/
Ruler.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
/**********************************************************************
Audacity: A Digital Audio Editor
Ruler.cpp
Dominic Mazzoni
*******************************************************************//**
\class Ruler
\brief Used to display a Ruler.
This is a generic class which can be used to display just about
any kind of ruler.
At a minimum, the user must specify the dimensions of the
ruler, its orientation (horizontal or vertical), and the
values displayed at the two ends of the ruler (min and max).
By default, this class will display tick marks at reasonable
round numbers and fractions, for example, 100, 50, 10, 5, 1,
0.5, 0.1, etc.
The class is designed to display a small handful of
labeled Major ticks, and a few Minor ticks between each of
these. Minor ticks are labeled if there is enough space.
Labels will never run into each other.
In addition to Real numbers, the Ruler currently supports
two other formats for its display:
Integer - never shows tick marks for fractions of an integer
Time - Assumes values represent seconds, and labels the tick
marks in "HH:MM:SS" format, e.g. 4000 seconds becomes
"1:06:40", for example. Will display fractions of
a second, and tick marks are all reasonable round
numbers for time (i.e. 15 seconds, 30 seconds, etc.)
*//***************************************************************//**
\class RulerPanel
\brief RulerPanel class allows you to work with a Ruler like
any other wxWindow.
*//***************************************************************//**
\class Ruler::Label
\brief An array of these created by the Ruler is used to determine
what and where text annotations to the numbers on the Ruler get drawn.
\todo Check whether Ruler is costing too much time in allocation/free of
array of Ruler::Label.
*//******************************************************************/
#include "Ruler.h"
#include <wx/dcclient.h>
#include <wx/dcscreen.h>
#include "AColor.h"
#include "AllThemeResources.h"
#include "Envelope.h"
#include "NumberScale.h"
#include "Theme.h"
#include "ViewInfo.h"
#include "graphics/Painter.h"
#include "graphics/WXPainterUtils.h"
#include "graphics/WXColor.h"
#include "graphics/WXPainterFactory.h"
#include "CodeConversions.h"
using std::min;
using std::max;
//wxColour Ruler::mTickColour{ 153, 153, 153 };
//
// Ruler
//
Ruler::Ruler()
{
mMin = mHiddenMin = 0.0;
mMax = mHiddenMax = 100.0;
mOrientation = wxHORIZONTAL;
mSpacing = 6;
mHasSetSpacing = false;
mFormat = RealFormat;
mFlip = false;
mLog = false;
mLabelEdges = false;
mLeft = -1;
mTop = -1;
mRight = -1;
mBottom = -1;
mbTicksOnly = true;
mbTicksAtExtremes = false;
mTickColour = wxColour( theTheme.Colour( clrTrackPanelText ));
mPen.SetColour(mTickColour);
mDbMirrorValue = 0.0;
// Note: the font size is now adjusted automatically whenever
// Invalidate is called on a horizontal Ruler, unless the user
// calls SetFonts manually. So the defaults here are not used
// often.
int fontSize = 10;
#ifdef __WXMSW__
fontSize = 8;
#endif
mLength = 0;
mCustom = false;
mbMinor = true;
mTwoTone = false;
mUseZoomInfo = NULL;
}
Ruler::~Ruler()
{
Invalidate(); // frees up our arrays
}
void Ruler::SetTwoTone(bool twoTone)
{
mTwoTone = twoTone;
}
void Ruler::SetFormat(RulerFormat format)
{
// IntFormat, RealFormat, RealLogFormat, TimeFormat, or LinearDBFormat
if (mFormat != format) {
mFormat = format;
Invalidate();
}
}
void Ruler::SetLog(bool log)
{
// Logarithmic
if (mLog != log) {
mLog = log;
Invalidate();
}
}
void Ruler::SetUnits(const TranslatableString &units)
{
// Specify the name of the units (like "dB") if you
// want numbers like "1.6" formatted as "1.6 dB".
if (mUnits != units) {
mUnits = units;
Invalidate();
}
}
void Ruler::SetDbMirrorValue( const double d )
{
if (mDbMirrorValue != d) {
mDbMirrorValue = d;
Invalidate();
}
}
void Ruler::SetOrientation(int orient)
{
// wxHORIZONTAL || wxVERTICAL
if (mOrientation != orient) {
mOrientation = orient;
if (mOrientation == wxVERTICAL && !mHasSetSpacing)
mSpacing = 2;
Invalidate();
}
}
void Ruler::SetRange(double min, double max)
{
SetRange(min, max, min, max);
}
void Ruler::SetRange
(double min, double max, double hiddenMin, double hiddenMax)
{
// For a horizontal ruler,
// min is the value in the center of pixel "left",
// max is the value in the center of pixel "right".
// In the special case of a time ruler,
// hiddenMin and hiddenMax are values that would be shown with the fisheye
// turned off. In other cases they equal min and max respectively.
if (mMin != min || mMax != max ||
mHiddenMin != hiddenMin || mHiddenMax != hiddenMax) {
mMin = min;
mMax = max;
mHiddenMin = hiddenMin;
mHiddenMax = hiddenMax;
Invalidate();
}
}
void Ruler::SetSpacing(int spacing)
{
mHasSetSpacing = true;
if (mSpacing != spacing) {
mSpacing = spacing;
Invalidate();
}
}
void Ruler::SetLabelEdges(bool labelEdges)
{
// If this is true, the edges of the ruler will always
// receive a label. If not, the nearest round number is
// labeled (which may or may not be the edge).
if (mLabelEdges != labelEdges) {
mLabelEdges = labelEdges;
Invalidate();
}
}
void Ruler::SetFlip(bool flip)
{
// If this is true, the orientation of the tick marks
// is reversed from the default; eg. above the line
// instead of below
if (mFlip != flip) {
mFlip = flip;
Invalidate();
}
}
void Ruler::SetMinor(bool value)
{
mbMinor = value;
}
namespace {
void FindFontHeights(
wxCoord& height, wxCoord& lead, Painter& painter, const wxFont& font)
{
static const char* exampleText = "0.9"; //ignored for height calcs on all platforms
auto painterFont = FontFromWXFont(painter, font);
const auto size = painter.GetTextSize(*painterFont, exampleText);
const auto metrics = painterFont->GetFontMetrics();
height = size.height;
lead = metrics.Linegap;
}
void FindFontHeights(
wxCoord &height, wxCoord &lead,
Painter &painter, int fontSize, wxFontWeight weight = wxFONTWEIGHT_NORMAL )
{
const wxFont font{ fontSize, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, weight };
FindFontHeights( height, lead, painter, font );
}
}
void Ruler::SetFonts(const wxFont &minorFont, const wxFont &majorFont, const wxFont &minorMinorFont)
{
// Won't override these fonts
mpUserFonts = std::make_unique<Fonts>(
Fonts{ majorFont, minorFont, minorMinorFont, 0 } );
wxScreenDC dc;
wxCoord height;
FindFontHeights( height, mpUserFonts->lead, GetMeasuringPainter(), majorFont );
mpFonts.reset();
Invalidate();
}
void Ruler::SetNumberScale(const NumberScale &scale)
{
if ( mNumberScale != scale ) {
mNumberScale = scale;
Invalidate();
}
}
void Ruler::OfflimitsPixels(int start, int end)
{
int length = mLength;
if (mOrientation == wxHORIZONTAL)
length = mRight - mLeft;
else
length = mBottom - mTop;
if( length < 0 )
return;
auto size = static_cast<size_t>( length + 1 );
if ( mUserBits.size() < size ) {
mLength = length;
mUserBits.resize( size, false );
}
if (end < start)
std::swap( start, end );
if (start < 0)
start = 0;
if (end > mLength)
end = mLength;
for(int i = start; i <= end; i++)
mUserBits[i] = true;
Invalidate();
}
void Ruler::SetBounds(int left, int top, int right, int bottom)
{
if (mLeft != left || mTop != top ||
mRight != right || mBottom != bottom) {
mLeft = left;
mTop = top;
mRight = right;
mBottom = bottom;
Invalidate();
}
}
void Ruler::Invalidate()
{
if (mOrientation == wxHORIZONTAL)
mLength = mRight-mLeft;
else
mLength = mBottom-mTop;
mpCache.reset();
// Bug 2316 we must preserve off-limit pixels.
// mUserBits.clear();
}
struct Ruler::TickSizes
{
bool useMajor = true;
double mMajor;
double mMinor;
int mDigits;
TickSizes( double UPP, int orientation, RulerFormat format, bool log )
{
//TODO: better dynamic digit computation for the log case
(void)log;
// Given the dimensions of the ruler, the range of values it
// has to display, and the format (i.e. Int, Real, Time),
// figure out how many units are in one Minor tick, and
// in one Major tick.
//
// The goal is to always put tick marks on nice round numbers
// that are easy for humans to grok. This is the most tricky
// with time.
double d;
// As a heuristic, we want at least 22 pixels between each
// minor tick. We want to show numbers like "-48"
// in that space.
// If vertical, we don't need as much space.
double units = ((orientation == wxHORIZONTAL) ? 22 : 16) * fabs(UPP);
mDigits = 0;
switch(format) {
case LinearDBFormat:
if (units < 0.001) {
mMinor = 0.001;
mMajor = 0.005;
return;
}
if (units < 0.01) {
mMinor = 0.01;
mMajor = 0.05;
return;
}
if (units < 0.1) {
mMinor = 0.1;
mMajor = 0.5;
return;
}
if (units < 1.0) {
mMinor = 1.0;
mMajor = 6.0;
return;
}
if (units < 3.0) {
mMinor = 3.0;
mMajor = 12.0;
return;
}
if (units < 6.0) {
mMinor = 6.0;
mMajor = 24.0;
return;
}
if (units < 12.0) {
mMinor = 12.0;
mMajor = 48.0;
return;
}
if (units < 24.0) {
mMinor = 24.0;
mMajor = 96.0;
return;
}
d = 20.0;
for(;;) {
if (units < d) {
mMinor = d;
mMajor = d*5.0;
return;
}
d *= 5.0;
if (units < d) {
mMinor = d;
mMajor = d*5.0;
return;
}
d *= 2.0;
}
break;
case IntFormat:
d = 1.0;
for(;;) {
if (units < d) {
mMinor = d;
mMajor = d*5.0;
return;
}
d *= 5.0;
if (units < d) {
mMinor = d;
mMajor = d*2.0;
return;
}
d *= 2.0;
}
break;
case TimeFormat:
if (units > 0.5) {
if (units < 1.0) { // 1 sec
mMinor = 1.0;
mMajor = 5.0;
return;
}
if (units < 5.0) { // 5 sec
mMinor = 5.0;
mMajor = 15.0;
return;
}
if (units < 10.0) {
mMinor = 10.0;
mMajor = 30.0;
return;
}
if (units < 15.0) {
mMinor = 15.0;
mMajor = 60.0;
return;
}
if (units < 30.0) {
mMinor = 30.0;
mMajor = 60.0;
return;
}
if (units < 60.0) { // 1 min
mMinor = 60.0;
mMajor = 300.0;
return;
}
if (units < 300.0) { // 5 min
mMinor = 300.0;
mMajor = 900.0;
return;
}
if (units < 600.0) { // 10 min
mMinor = 600.0;
mMajor = 1800.0;
return;
}
if (units < 900.0) { // 15 min
mMinor = 900.0;
mMajor = 3600.0;
return;
}
if (units < 1800.0) { // 30 min
mMinor = 1800.0;
mMajor = 3600.0;
return;
}
if (units < 3600.0) { // 1 hr
mMinor = 3600.0;
mMajor = 6*3600.0;
return;
}
if (units < 6*3600.0) { // 6 hrs
mMinor = 6*3600.0;
mMajor = 24*3600.0;
return;
}
if (units < 24*3600.0) { // 1 day
mMinor = 24*3600.0;
mMajor = 7*24*3600.0;
return;
}
mMinor = 24.0 * 7.0 * 3600.0; // 1 week
mMajor = 24.0 * 7.0 * 3600.0;
}
// Otherwise fall through to RealFormat
// (fractions of a second should be dealt with
// the same way as for RealFormat)
case RealFormat:
d = 0.000001;
// mDigits is number of digits after the decimal point.
mDigits = 6;
for(;;) {
if (units < d) {
mMinor = d;
mMajor = d*5.0;
return;
}
d *= 5.0;
if (units < d) {
mMinor = d;
mMajor = d*2.0;
return;
}
d *= 2.0;
mDigits--;
// More than 10 digit numbers? Something is badly wrong.
// Probably units is coming in with too high a value.
wxASSERT( mDigits >= -10 );
if( mDigits < -10 )
break;
}
mMinor = d;
mMajor = d * 2.0;
break;
case RealLogFormat:
d = 0.000001;
// mDigits is number of digits after the decimal point.
mDigits = 6;
for(;;) {
if (units < d) {
mMinor = d;
mMajor = d*5.0;
return;
}
d *= 5.0;
if (units < d) {
mMinor = d;
mMajor = d*2.0;
return;
}
d *= 2.0;
mDigits--;
// More than 10 digit numbers? Something is badly wrong.
// Probably units is coming in with too high a value.
wxASSERT( mDigits >= -10 );
if( mDigits < -10 )
break;
}
mDigits++;
mMinor = d;
mMajor = d * 2.0;
break;
}
}
TranslatableString LabelString(
double d, RulerFormat format, const TranslatableString &units )
const
{
// Given a value, turn it into a string according
// to the current ruler format. The number of digits of
// accuracy depends on the resolution of the ruler,
// i.e. how far zoomed in or out you are.
wxString s;
// PRL Todo: are all these cases properly localized? (Decimal points,
// hour-minute-second, etc.?)
// Replace -0 with 0
if (d < 0.0 && (d+mMinor > 0.0) && ( format != RealLogFormat ))
d = 0.0;
switch( format ) {
case IntFormat:
s.Printf(wxT("%d"), (int)floor(d+0.5));
break;
case LinearDBFormat:
if (mMinor >= 1.0)
s.Printf(wxT("%d"), (int)floor(d+0.5));
else {
int precision = -log10(mMinor);
s.Printf(wxT("%.*f"), precision, d);
}
break;
case RealFormat:
if (mMinor >= 1.0)
s.Printf(wxT("%d"), (int)floor(d+0.5));
else {
s.Printf(wxString::Format(wxT("%%.%df"), mDigits), d);
}
break;
case RealLogFormat:
if (mMinor >= 1.0)
s.Printf(wxT("%d"), (int)floor(d+0.5));
else {
s.Printf(wxString::Format(wxT("%%.%df"), mDigits), d);
}
break;
case TimeFormat:
if (useMajor) {
if (d < 0) {
s = wxT("-");
d = -d;
}
#if ALWAYS_HH_MM_SS
int secs = (int)(d + 0.5);
if (mMinor >= 1.0) {
s.Printf(wxT("%d:%02d:%02d"), secs/3600, (secs/60)%60, secs%60);
}
else {
wxString t1, t2, format;
t1.Printf(wxT("%d:%02d:"), secs/3600, (secs/60)%60);
format.Printf(wxT("%%0%d.%dlf"), mDigits+3, mDigits);
t2.Printf(format, fmod(d, 60.0));
s += t1 + t2;
}
break;
#endif
if (mMinor >= 3600.0) {
int hrs = (int)(d / 3600.0 + 0.5);
wxString h;
h.Printf(wxT("%d:00:00"), hrs);
s += h;
}
else if (mMinor >= 60.0) {
int minutes = (int)(d / 60.0 + 0.5);
wxString m;
if (minutes >= 60)
m.Printf(wxT("%d:%02d:00"), minutes/60, minutes%60);
else
m.Printf(wxT("%d:00"), minutes);
s += m;
}
else if (mMinor >= 1.0) {
int secs = (int)(d + 0.5);
wxString t;
if (secs >= 3600)
t.Printf(wxT("%d:%02d:%02d"), secs/3600, (secs/60)%60, secs%60);
else if (secs >= 60)
t.Printf(wxT("%d:%02d"), secs/60, secs%60);
else
t.Printf(wxT("%d"), secs);
s += t;
}
else {
// Commented out old and incorrect code for avoiding the 40mins and 60 seconds problem
// It was causing Bug 463 - Incorrect Timeline numbering (where at high zoom and long tracks,
// numbers did not change.
#if 0
// The casting to float is working around an issue where 59 seconds
// would show up as 60 when using g++ (Ubuntu 4.3.3-5ubuntu4) 4.3.3.
int secs = (int)(float)(d);
wxString t1, t2, format;
if (secs >= 3600)
t1.Printf(wxT("%d:%02d:"), secs/3600, (secs/60)%60);
else if (secs >= 60)
t1.Printf(wxT("%d:"), secs/60);
if (secs >= 60)
format.Printf(wxT("%%0%d.%dlf"), mDigits+3, mDigits);
else
format.Printf(wxT("%%%d.%dlf"), mDigits+3, mDigits);
// The casting to float is working around an issue where 59 seconds
// would show up as 60 when using g++ (Ubuntu 4.3.3-5ubuntu4) 4.3.3.
t2.Printf(format, fmod((float)d, (float)60.0));
#else
// For d in the range of hours, d is just very slightly below the value it should
// have, because of using a double, which in turn yields values like 59:59:999999
// mins:secs:nanosecs when we want 1:00:00:000000
// so adjust by less than a nano second per hour to get nicer number formatting.
double dd = d * 1.000000000000001;
int secs = (int)(dd);
wxString t1, t2, format;
if (secs >= 3600)
t1.Printf(wxT("%d:%02d:"), secs/3600, (secs/60)%60);
else if (secs >= 60)
t1.Printf(wxT("%d:"), secs/60);
if (secs >= 60)
format.Printf(wxT("%%0%d.%dlf"), mDigits+3, mDigits);
else
format.Printf(wxT("%%%d.%dlf"), mDigits+3, mDigits);
// dd will be reduced to just the seconds and fractional part.
dd = dd - secs + (secs%60);
// truncate to appropriate number of digits, so that the print formatting
// doesn't round up 59.9999999 to 60.
double multiplier = pow( 10, mDigits);
dd = ((int)(dd * multiplier))/multiplier;
t2.Printf(format, dd);
#endif
s += t1 + t2;
}
}
else {
}
}
auto result = Verbatim( s );
if (!units.empty())
result += units;
return result;
}
}; // struct Ruler::TickSizes
auto Ruler::MakeTick(
Label lab,
Painter &painter, const std::shared_ptr<PainterFont>& font,
std::vector<bool> &bits,
int left, int top, int spacing, int lead,
bool flip, int orientation )
-> std::pair< wxRect, Label >
{
lab.lx = left - 1000; // don't display
lab.ly = top - 1000; // don't display
auto length = bits.size() - 1;
auto pos = lab.pos;
auto str = lab.text;
// Do not put the text into results until we are sure it does not overlap
lab.text = {};
const auto strSize = painter.GetTextSize(*font, audacity::ToUTF8(str.Translation()));
int strPos, strLen, strLeft, strTop;
if ( orientation == wxHORIZONTAL ) {
strLen = strSize.width;
strPos = pos - strSize.width / 2;
if (strPos < 0)
strPos = 0;
if (strPos + strSize.width >= length)
strPos = length - strSize.width;
strLeft = left + strPos;
if ( flip )
strTop = top + 4;
else
strTop = -strSize.height - lead;
// strTop = top - lead + 4;// More space was needed...
}
else {
strLen = strSize.height;
strPos = pos - strSize.height / 2;
if (strPos < 0)
strPos = 0;
if (strPos + strSize.height >= length)
strPos = length - strSize.height;
strTop = top + strPos;
if ( flip )
strLeft = left + 5;
else
strLeft = -strSize.width - 6;
}
// FIXME: we shouldn't even get here if strPos < 0.
// Ruler code currently does not handle very small or
// negative sized windows (i.e. don't draw) properly.
if( strPos < 0 )
return { {}, lab };
// See if any of the pixels we need to draw this
// label is already covered
int i;
for(i=0; i<strLen; i++)
if ( bits[strPos+i] )
return { {}, lab };
// If not, position the label
lab.lx = strLeft;
lab.ly = strTop;
// And mark these pixels, plus some surrounding
// ones (the spacing between labels), as covered
int leftMargin = spacing;
if (strPos < leftMargin)
leftMargin = strPos;
strPos -= leftMargin;
strLen += leftMargin;
int rightMargin = spacing;
if (strPos + strLen > length - spacing)
rightMargin = length - strPos - strLen;
strLen += rightMargin;
for(i=0; i<strLen; i++)
bits[strPos+i] = true;
// Good to display the text
lab.text = str;
return { { strLeft, strTop, static_cast<int>(strSize.width), static_cast<int>(strSize.height) }, lab };
}
struct Ruler::Updater {
const Ruler &mRuler;
const ZoomInfo *zoomInfo;
explicit Updater( const Ruler &ruler, const ZoomInfo *z )
: mRuler{ ruler }
, zoomInfo{ z }
{}
const double mDbMirrorValue = mRuler.mDbMirrorValue;
const int mLength = mRuler.mLength;
const RulerFormat mFormat = mRuler.mFormat;
const TranslatableString mUnits = mRuler.mUnits;
const int mLeft = mRuler.mLeft;
const int mTop = mRuler.mTop;
const int mBottom = mRuler.mBottom;
const int mRight = mRuler.mRight;
const int mSpacing = mRuler.mSpacing;
const int mOrientation = mRuler.mOrientation;
const bool mFlip = mRuler.mFlip;
const bool mCustom = mRuler.mCustom;
const Fonts& mFonts = *mRuler.mpFonts;
const PainterFonts& mPainterFonts = *mRuler.mpPainterFonts;
const bool mLog = mRuler.mLog;
const double mHiddenMin = mRuler.mHiddenMin;
const double mHiddenMax = mRuler.mHiddenMax;
const bool mLabelEdges = mRuler.mLabelEdges;
const double mMin = mRuler.mMin;
const double mMax = mRuler.mMax;
const int mLeftOffset = mRuler.mLeftOffset;
const NumberScale mNumberScale = mRuler.mNumberScale;
struct TickOutputs;
bool Tick( Painter &painter,
int pos, double d, const TickSizes &tickSizes, const std::shared_ptr<PainterFont>& font,
TickOutputs outputs
) const;
// Another tick generator for custom ruler case (noauto) .
bool TickCustom(
Painter& painter, int labelIdx, const std::shared_ptr<PainterFont>& font,
TickOutputs outputs
) const;
static void CreatePainterFonts(
Fonts& fonts, std::unique_ptr<PainterFonts>& pPainterFonts,
Painter& painter);
static void ChooseFonts(
std::unique_ptr<Fonts>& pFonts,
std::unique_ptr<PainterFonts>& pPainterFonts,
const Fonts* pUserFonts, Painter& painter,
int desiredPixelHeight);
struct UpdateOutputs;
void Update(
Painter& painter, const Envelope* envelope,
UpdateOutputs &allOutputs
)// Envelope *speedEnv, long minSpeed, long maxSpeed )
const;
void UpdateCustom(Painter& painter, UpdateOutputs& allOutputs) const;
void UpdateLinear(
Painter& painter, const Envelope* envelope,
UpdateOutputs& allOutputs) const;
void UpdateNonlinear(Painter& painter, UpdateOutputs& allOutputs) const;
};
struct Ruler::Cache {
Bits mBits;
Labels mMajorLabels, mMinorLabels, mMinorMinorLabels;
wxRect mRect;
};
struct Ruler::Updater::TickOutputs{ Labels &labels; Bits &bits; wxRect &box; };
struct Ruler::Updater::UpdateOutputs {
Labels &majorLabels, &minorLabels, &minorMinorLabels;
Bits &bits;
wxRect &box;
};
bool Ruler::Updater::Tick(
Painter& painter,
int pos, double d, const TickSizes &tickSizes, const std::shared_ptr<PainterFont>& font,
// in/out:
TickOutputs outputs ) const
{
// Bug 521. dB view for waveforms needs a 2-sided scale.
if(( mDbMirrorValue > 1.0 ) && ( -d > mDbMirrorValue ))
d = -2*mDbMirrorValue - d;
// FIXME: We don't draw a tick if off end of our label arrays
// But we shouldn't have an array of labels.
if( outputs.labels.size() >= mLength )
return false;
Label lab;
lab.value = d;
lab.pos = pos;
lab.text = tickSizes.LabelString( d, mFormat, mUnits );
const auto result = MakeTick(
lab,
painter, font,
outputs.bits,
mLeft, mTop, mSpacing, mFonts.lead,
mFlip,
mOrientation );
auto &rect = result.first;
outputs.box.Union( rect );
outputs.labels.emplace_back( result.second );
return !rect.IsEmpty();
}
bool Ruler::Updater::TickCustom(
Painter& painter, int labelIdx, const std::shared_ptr<PainterFont>& font,
// in/out:
TickOutputs outputs ) const
{
// FIXME: We don't draw a tick if of end of our label arrays
// But we shouldn't have an array of labels.
if( labelIdx >= outputs.labels.size() )
return false;
//This should only used in the mCustom case
Label lab;
lab.value = 0.0;
const auto result = MakeTick(
lab,
painter, font,
outputs.bits,
mLeft, mTop, mSpacing, mFonts.lead,
mFlip,
mOrientation );
auto &rect = result.first;
outputs.box.Union( rect );
outputs.labels[labelIdx] = ( result.second );
return !rect.IsEmpty();