forked from audacity/audacity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumericTextCtrl.cpp
More file actions
2384 lines (2035 loc) · 72.4 KB
/
Copy pathNumericTextCtrl.cpp
File metadata and controls
2384 lines (2035 loc) · 72.4 KB
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
NumericTextCtrl.cpp
Dominic Mazzoni
********************************************************************//**
NumericConverter
\class NumericConverter
\brief NumericConverter provides the advanced formatting control used
in the selection bar of Audacity.
Any negative value given to the converter is considered invalid and
all digit positions of the resulting string will be filled with hyphens.
Otherwise:
The NumericConverter makes use of a format string to specify the
exact way that a single value is split into several fields,
such as the hh:mm:ss format. The advantage of this format string
is that it is very small and compact, but human-readable and
somewhat intuitive, so that it's easy to add NEW layouts
in the future. It's also designed to make it easier to add
i18n support, since the way that numbers are displayed in different
languages could conceivably vary a lot.
The number to be formatted may be expressed in seconds, so the format
string can specify the relationship of each field to the number of
seconds.
The class is also reused to format some non-time values such as
frequency and log of frequency.
Let's start by considering an example: here's the format string
that prints an integer number of seconds in the hour minute
second h:m:s format:
*:60:60
The "*" is a wildcard, saying that the leftmost field can contain
numbers of arbitrary magnitude. The next character, ':', since it
is not a digit or a wildcard, is interpreted as a delimiter, and
will be displayed between those fields. The next number, 60,
indicates that the range of the next field (minutes) is 60.
Then there's another ':' delimiter, and finally the last field
(seconds) is 60. So, if you give it a number like 3758
it is formatted as:
3758 seconds, "*:60:60" -> "1:2:38"
Note that 3758 = 1*60*60 + 2*60 + 38.
When NumericConverter formats an integer, you can think of its process
as working from right to left. Given the value "3758", it fills
in the seconds by dividing by 60, sticking the remainder in the
seconds field and then passing the quotient to the next field to
the left.
In order to format a field with leading zeros, simply add a leading
zero to that field, like this:
3758 seconds, "*:060:060" -> "1:02:38"
In order to format fractions, simply include a field delimiter
ending with a decimal point. If the delimiter is simply '.' with
nothing else, then the '.' is actually displayed. Otherwise the
'.' is dropped, and the other characters in the delimiter are
displayed instead.
Here's how we'd display hours, minutes, and seconds with three
decimal places after the seconds:
3758.5 seconds, "*:060:060.01000" -> "1:02:38.500"
Similarly, here's how we'd display the fractional part of
seconds as film frames (24 per second) instead of milliseconds:
3758.5 seconds, "*:060:060 and .24 frames" -> "1:02:38 and 12 frames"
Note that the decimal '.' is associated with the delimiter, not
with the 24.
Additionally, the special character '#' can be used in place of a number
to represent the current sample rate. Use '0#' to add leading
zeros to that field. For example:
3758.5 seconds, "*:060:060+.#samples" -> "1:02:38+22050samples"
(Almost) Finally, there is a rule that allows you to change the units into
something other than seconds. To do this, put a "|" character on
the far right, followed by a number specifying the scaling factor.
As an exception to previous rules, decimal points are allowed
in the final scaling factor - the period is not interpreted as it
would be before the "|" character. (This is fine, because all
previous fields must be integers to make sense.) Anyway, if you
include a scaling factor after a "|", the number will be
multiplied by this factor before it is formatted. For example, to
express the current time in NTSC frames (~29.97 fps), you could
use the following formatting:
3758.5 seconds, "*.01000 frames|29.97002997" -> "112642.358 frames"
Finally there is a further special character that can be used after a "|"
and that is "N". This applies special rule for NTSC drop-frame timecode.
Summary of format string rules:
- The characters '0-9', '*', and '#' are numeric. Any sequence of
these characters is treated as defining a NEW field by specifying
its range. All other characters become delimiters between fields.
(The one exception is that '.' is treated as numeric after the
optional '|'.)
- A field with a range of '*', which only makes sense as the
leftmost field, means the field should display as large a number
as necessary. (Note: this no longer makes sense here and applies to a
previous version).
- The character '#' represents the current sample rate.
- If a field specifier beings with a leading zero, it will be formatted
with leading zeros, too - enough to display the maximum value
that field can display. So the number 7 in a field specified
as '01000' would be formatted as '007'. Bond. James Bond.
- Any non-numeric characters before the first field are treated
as a prefix, and will be displayed to the left of the first field.
- A delimiter ending in '.' is treated specially. All fields after
this delimiter are fractional fields, after the decimal point.
- The '|' character is treated as a special delimiter. The number
to the right of this character (which is allowed to contain a
decimal point) is treated as a scaling factor. The number is
multiplied by this factor before converting.
- The special character 'N' after '|' is only used for NTSC drop-frame.
*******************************************************************//**
\class NumericTextCtrlAx
\brief NumericTextCtrlAx gives the NumericTextCtrl Accessibility.
*******************************************************************//**
\class NumericConverter
\brief NumericConverter has all the time conversion and snapping
functionality that used to live in NumericTextCtrl. The idea is to have
a GUI-less class which can do the conversions, so that we can use it
in sanpping without having a window created each time.
*//****************************************************************//**
\class BuiltinFormatString
\brief BuiltinFormatString is a structure used in the NumericTextCtrl
and holds both a descriptive name for the string format and a
wxPrintf inspired style format string, optimised for displaying time in
different formats.
*//****************************************************************//**
\class NumericField
\brief NumericField is a class used in NumericTextCtrl
*//****************************************************************//**
\class DigitInfo
\brief DigitInfo is a class used in NumericTextCtrl
**********************************************************************/
#include "../Audacity.h"
#include "NumericTextCtrl.h"
#include "audacity/Types.h"
#include "../AllThemeResources.h"
#include "../AColor.h"
#include "../KeyboardCapture.h"
#include "../Theme.h"
#include <algorithm>
#include <math.h>
#include <limits>
#include <wx/setup.h> // for wxUSE_* macros
#include <wx/wx.h>
#include <wx/dcbuffer.h>
#include <wx/font.h>
#include <wx/intl.h>
#include <wx/menu.h>
#include <wx/sizer.h>
#include <wx/stattext.h>
#include <wx/tooltip.h>
#include <wx/toplevel.h>
#if wxUSE_ACCESSIBILITY
#include "WindowAccessible.h"
class NumericTextCtrlAx final : public WindowAccessible
{
public:
NumericTextCtrlAx(NumericTextCtrl * ctrl);
virtual ~ NumericTextCtrlAx();
// Performs the default action. childId is 0 (the action for this object)
// or > 0 (the action for a child).
// Return wxACC_NOT_SUPPORTED if there is no default action for this
// window (e.g. an edit control).
wxAccStatus DoDefaultAction(int childId) override;
// Retrieves the address of an IDispatch interface for the specified child.
// All objects must support this property.
wxAccStatus GetChild(int childId, wxAccessible **child) override;
// Gets the number of children.
wxAccStatus GetChildCount(int *childCount) override;
// Gets the default action for this object (0) or > 0 (the action for a child).
// Return wxACC_OK even if there is no action. actionName is the action, or the empty
// string if there is no action.
// The retrieved string describes the action that is performed on an object,
// not what the object does as a result. For example, a toolbar button that prints
// a document has a default action of "Press" rather than "Prints the current document."
wxAccStatus GetDefaultAction(int childId, wxString *actionName) override;
// Returns the description for this object or a child.
wxAccStatus GetDescription(int childId, wxString *description) override;
// Gets the window with the keyboard focus.
// If childId is 0 and child is NULL, no object in
// this subhierarchy has the focus.
// If this object has the focus, child should be 'this'.
wxAccStatus GetFocus(int *childId, wxAccessible **child) override;
// Returns help text for this object or a child, similar to tooltip text.
wxAccStatus GetHelpText(int childId, wxString *helpText) override;
// Returns the keyboard shortcut for this object or child.
// Return e.g. ALT+K
wxAccStatus GetKeyboardShortcut(int childId, wxString *shortcut) override;
// Returns the rectangle for this object (id = 0) or a child element (id > 0).
// rect is in screen coordinates.
wxAccStatus GetLocation(wxRect & rect, int elementId) override;
// Gets the name of the specified object.
wxAccStatus GetName(int childId, wxString *name) override;
// Returns a role constant.
wxAccStatus GetRole(int childId, wxAccRole *role) override;
// Gets a variant representing the selected children
// of this object.
// Acceptable values:
// - a null variant (IsNull() returns TRUE)
// - a list variant (GetType() == wxT("list"))
// - an integer representing the selected child element,
// or 0 if this object is selected (GetType() == wxT("long"))
// - a "void*" pointer to a wxAccessible child object
wxAccStatus GetSelections(wxVariant *selections) override;
// Returns a state constant.
wxAccStatus GetState(int childId, long *state) override;
// Returns a localized string representing the value for the object
// or child.
wxAccStatus GetValue(int childId, wxString *strValue) override;
private:
NumericTextCtrl *mCtrl;
int mLastField;
int mLastDigit;
wxString mCachedName;
wxString mLastCtrlString;
};
#endif // wxUSE_ACCESSIBILITY
//
// ----------------------------------------------------------------------------
// BuiltinFormatString Struct
// ----------------------------------------------------------------------------
//
/** \brief struct to hold a formatting control string and its user facing name
* Used in an array to hold the built-in time formats that are always available
* to the user */
struct BuiltinFormatString
{
NumericFormatSymbol name;
NumericConverter::FormatStrings formatStrings;
friend inline bool operator ==
(const BuiltinFormatString &a, const BuiltinFormatString &b)
{ return a.name == b.name; }
};
//
// ----------------------------------------------------------------------------
// NumericField Class
// ----------------------------------------------------------------------------
//
class NumericField
{
public:
NumericField(bool _frac, int _base, int _range, bool _zeropad)
{
frac = _frac;
base = _base;
range = _range;
zeropad = _zeropad;
digits = 0;
}
NumericField( const NumericField & ) = default;
NumericField &operator = ( const NumericField & ) = default;
//NumericField( NumericField && ) = default;
//NumericField &operator = ( NumericField && ) = default;
void CreateDigitFormatStr()
{
if (range > 1)
digits = (int)ceil(log10(range-1.0));
else
digits = 5; // hack: default
if (zeropad && range>1)
formatStr.Printf(wxT("%%0%dd"), digits); // ex. "%03d" if digits is 3
else {
formatStr.Printf(wxT("%%0%dd"), digits);
}
}
bool frac; // is it a fractional field
int base; // divide by this (multiply, after decimal point)
int range; // then take modulo this
int digits;
int pos; // Index of this field in the ValueString
int fieldX; // x-position of the field on-screen
int fieldW; // width of the field on-screen
int labelX; // x-position of the label on-screen
bool zeropad;
wxString label;
wxString formatStr;
wxString str;
};
//
// ----------------------------------------------------------------------------
// DigitInfo Class
// ----------------------------------------------------------------------------
//
class DigitInfo
{
public:
DigitInfo(int _field, int _index, int _pos, wxRect _box)
{
field = _field;
index = _index;
pos = _pos;
digitBox = _box;
}
int field; // Which field
int index; // Index of this digit within the field
int pos; // Position in the ValueString
wxRect digitBox;
};
namespace {
/** \brief array of formats the control knows about internally
* array of string pairs for name of the format and the format string
* needed to create that format output. This is used for the pop-up
* list of formats to choose from in the control. */
static const BuiltinFormatString TimeConverterFormats_[] = {
{
/* i18n-hint: Name of time display format that shows time in seconds */
{ XO("seconds") },
/* i18n-hint: Format string for displaying time in seconds. Change the comma
* in the middle to the 1000s separator for your locale, and the 'seconds'
* on the end to the word for seconds. Don't change the numbers. */
XO("01000,01000 seconds")
},
{
/* i18n-hint: Name of time display format that shows time in hours, minutes
* and seconds */
{ XO("hh:mm:ss") },
/* i18n-hint: Format string for displaying time in hours, minutes and
* seconds. Change the 'h' to the abbreviation for hours, 'm' to the
* abbreviation for minutes and 's' to the abbreviation for seconds. Don't
* change the numbers unless there aren't 60 seconds in a minute in your
* locale */
XO("0100 h 060 m 060 s")
},
{
/* i18n-hint: Name of time display format that shows time in days, hours,
* minutes and seconds */
{ XO("dd:hh:mm:ss") },
/* i18n-hint: Format string for displaying time in days, hours, minutes and
* seconds. Change the 'days' to the word for days, 'h' to the abbreviation
* for hours, 'm' to the abbreviation for minutes and 's' to the
* abbreviation for seconds. Don't change the numbers unless there aren't
* 24 hours in a day in your locale */
XO("0100 days 024 h 060 m 060 s")
},
{
/* i18n-hint: Name of time display format that shows time in hours,
* minutes, seconds and hundredths of a second (1/100 second) */
{ XO("hh:mm:ss + hundredths") },
/* i18n-hint: Format string for displaying time in hours, minutes, seconds
* and hundredths of a second. Change the 'h' to the abbreviation for hours,
* 'm' to the abbreviation for minutes and 's' to the abbreviation for seconds
* (the hundredths are shown as decimal seconds). Don't change the numbers
* unless there aren't 60 minutes in an hour in your locale.
* The decimal separator is specified using '<' if your language uses a ',' or
* to '>' if your language uses a '.'. */
XO("0100 h 060 m 060>0100 s")
},
{
/* i18n-hint: Name of time display format that shows time in hours,
* minutes, seconds and milliseconds (1/1000 second) */
{ XO("hh:mm:ss + milliseconds") },
/* i18n-hint: Format string for displaying time in hours, minutes, seconds
* and milliseconds. Change the 'h' to the abbreviation for hours, 'm' to the
* abbreviation for minutes and 's' to the abbreviation for seconds (the
* milliseconds are shown as decimal seconds) . Don't change the numbers
* unless there aren't 60 minutes in an hour in your locale.
* The decimal separator is specified using '<' if your language uses a ',' or
* to '>' if your language uses a '.'. */
XO("0100 h 060 m 060>01000 s")
},
{
/* i18n-hint: Name of time display format that shows time in hours,
* minutes, seconds and samples (at the current project sample rate) */
{ XO("hh:mm:ss + samples") },
/* i18n-hint: Format string for displaying time in hours, minutes, seconds
* and samples. Change the 'h' to the abbreviation for hours, 'm' to the
* abbreviation for minutes, 's' to the abbreviation for seconds and
* translate samples . Don't change the numbers
* unless there aren't 60 seconds in a minute in your locale.
* The decimal separator is specified using '<' if your language uses a ',' or
* to '>' if your language uses a '.'. */
XO("0100 h 060 m 060 s+># samples")
},
{
/* i18n-hint: Name of time display format that shows time in samples (at the
* current project sample rate). For example the number of a sample at 1
* second into a recording at 44.1KHz would be 44,100.
*/
{ XO("samples") },
/* i18n-hint: Format string for displaying time in samples (lots of samples).
* Change the ',' to the 1000s separator for your locale, and translate
* samples. If 1000s aren't a base multiple for your number system, then you
* can change the numbers to an appropriate one, and put a 0 on the front */
XO("01000,01000,01000 samples|#")
},
{
/* i18n-hint: Name of time display format that shows time in hours, minutes,
* seconds and frames at 24 frames per second (commonly used for films) */
{ XO("hh:mm:ss + film frames (24 fps)") },
/* i18n-hint: Format string for displaying time in hours, minutes, seconds
* and frames at 24 frames per second. Change the 'h' to the abbreviation
* for hours, 'm' to the abbreviation for minutes, 's' to the abbreviation
* for seconds and translate 'frames' . Don't change the numbers
* unless there aren't 60 seconds in a minute in your locale.
* The decimal separator is specified using '<' if your language uses a ',' or
* to '>' if your language uses a '.'. */
XO("0100 h 060 m 060 s+>24 frames")
},
{
/* i18n-hint: Name of time display format that shows time in frames (lots of
* frames) at 24 frames per second (commonly used for films) */
{ XO("film frames (24 fps)") },
/* i18n-hint: Format string for displaying time in frames at 24 frames per
* second. Change the comma
* in the middle to the 1000s separator for your locale,
* translate 'frames' and leave the rest alone */
XO("01000,01000 frames|24")
},
{
/* i18n-hint: Name of time display format that shows time in hours, minutes,
* seconds and frames at NTSC TV drop-frame rate (used for American /
* Japanese TV, and very odd) */
{ XO("hh:mm:ss + NTSC drop frames") },
/* i18n-hint: Format string for displaying time in hours, minutes, seconds
* and frames with NTSC drop frames. Change the 'h' to the abbreviation
* for hours, 'm' to the abbreviation for minutes, 's' to the abbreviation
* for seconds and translate 'frames'. Leave the |N alone, it's important!
* The decimal separator is specified using '<' if your language uses a ',' or
* to '>' if your language uses a '.'. */
XO("0100 h 060 m 060 s+>30 frames|N")
},
{
/* i18n-hint: Name of time display format that shows time in hours, minutes,
* seconds and frames at NTSC TV non-drop-frame rate (used for American /
* Japanese TV, and doesn't quite match wall time */
{ XO("hh:mm:ss + NTSC non-drop frames") },
/* i18n-hint: Format string for displaying time in hours, minutes, seconds
* and frames with NTSC drop frames. Change the 'h' to the abbreviation
* for hours, 'm' to the abbreviation for minutes, 's' to the abbreviation
* for seconds and translate 'frames'. Leave the | .999000999 alone,
* the whole things really is slightly off-speed!
* The decimal separator is specified using '<' if your language uses a ',' or
* to '>' if your language uses a '.'. */
XO("0100 h 060 m 060 s+>030 frames| .999000999")
},
{
/* i18n-hint: Name of time display format that shows time in frames at NTSC
* TV frame rate (used for American / Japanese TV */
{ XO("NTSC frames") },
/* i18n-hint: Format string for displaying time in frames with NTSC frames.
* Change the comma
* in the middle to the 1000s separator for your locale,
* translate 'frames' and leave the rest alone. That really is the frame
* rate! */
XO("01000,01000 frames|29.97002997")
},
{
/* i18n-hint: Name of time display format that shows time in hours, minutes,
* seconds and frames at PAL TV frame rate (used for European TV) */
{ XO("hh:mm:ss + PAL frames (25 fps)") },
/* i18n-hint: Format string for displaying time in hours, minutes, seconds
* and frames with PAL TV frames. Change the 'h' to the abbreviation
* for hours, 'm' to the abbreviation for minutes, 's' to the abbreviation
* for seconds and translate 'frames'. Nice simple time code!
* The decimal separator is specified using '<' if your language uses a ',' or
* to '>' if your language uses a '.'. */
XO("0100 h 060 m 060 s+>25 frames")
},
{
/* i18n-hint: Name of time display format that shows time in frames at PAL
* TV frame rate (used for European TV) */
{ XO("PAL frames (25 fps)") },
/* i18n-hint: Format string for displaying time in frames with NTSC frames.
* Change the comma
* in the middle to the 1000s separator for your locale,
* translate 'frames' and leave the rest alone. */
XO("01000,01000 frames|25")
},
{
/* i18n-hint: Name of time display format that shows time in hours, minutes,
* seconds and frames at CD Audio frame rate (75 frames per second) */
{ XO("hh:mm:ss + CDDA frames (75 fps)") },
/* i18n-hint: Format string for displaying time in hours, minutes, seconds
* and frames with CD Audio frames. Change the 'h' to the abbreviation
* for hours, 'm' to the abbreviation for minutes, 's' to the abbreviation
* for seconds and translate 'frames'.
* The decimal separator is specified using '<' if your language uses a ',' or
* to '>' if your language uses a '.'. */
XO("0100 h 060 m 060 s+>75 frames")
},
{
/* i18n-hint: Name of time display format that shows time in frames at CD
* Audio frame rate (75 frames per second) */
{ XO("CDDA frames (75 fps)") },
/* i18n-hint: Format string for displaying time in frames with CD Audio
* frames. Change the comma
* in the middle to the 1000s separator for your locale,
* translate 'frames' and leave the rest alone */
XO("01000,01000 frames|75")
},
};
/** \brief array of formats the control knows about internally
* array of string pairs for name of the format and the format string
* needed to create that format output. This is used for the pop-up
* list of formats to choose from in the control. */
static const BuiltinFormatString FrequencyConverterFormats_[] = {
{
/* i18n-hint: Name of display format that shows frequency in hertz */
{ XO("Hz") },
{
/* i18n-hint: Format string for displaying frequency in hertz. Change
* the decimal point for your locale. Don't change the numbers.
* The decimal separator is specified using '<' if your language uses a ',' or
* to '>' if your language uses a '.'. */
XO("010,01000>0100 Hz")
, XO("centihertz")
}
},
{
/* i18n-hint: Name of display format that shows frequency in kilohertz */
{ XO("kHz") },
{
/* i18n-hint: Format string for displaying frequency in kilohertz. Change
* the decimal point for your locale. Don't change the numbers.
* The decimal separator is specified using '<' if your language uses a ',' or
* to '>' if your language uses a '.'. */
XO("01000>01000 kHz|0.001")
, XO("hertz")
}
},
};
/** \brief array of formats the control knows about internally
* array of string pairs for name of the format and the format string
* needed to create that format output. This is used for the pop-up
* list of formats to choose from in the control. */
static const BuiltinFormatString BandwidthConverterFormats_[] = {
{
/* i18n-hint: Name of display format that shows log of frequency
* in octaves */
{ XO("octaves") },
{
/* i18n-hint: Format string for displaying log of frequency in octaves.
* Change the decimal points for your locale. Don't change the numbers.
* The decimal separator is specified using '<' if your language uses a ',' or
* to '>' if your language uses a '.'. */
XO("100>01000 octaves|1.442695041"), // Scale factor is 1 / ln (2)
/* i18n-hint: an octave is a doubling of frequency */
XO("thousandths of octaves")
}
},
{
/* i18n-hint: Name of display format that shows log of frequency
* in semitones and cents */
{ XO("semitones + cents") },
{
/* i18n-hint: Format string for displaying log of frequency in semitones
* and cents.
* Change the decimal points for your locale. Don't change the numbers.
* The decimal separator is specified using '<' if your language uses a ',' or
* to '>' if your language uses a '.'. */
XO("1000 semitones >0100 cents|17.312340491"), // Scale factor is 12 / ln (2)
/* i18n-hint: a cent is a hundredth of a semitone (which is 1/12 octave) */
XO("hundredths of cents")
}
},
{
/* i18n-hint: Name of display format that shows log of frequency
* in decades */
{ XO("decades") },
{
/* i18n-hint: Format string for displaying log of frequency in decades.
* Change the decimal points for your locale. Don't change the numbers. */
XO("10>01000 decades|0.434294482"), // Scale factor is 1 / ln (10)
/* i18n-hint: a decade is a tenfold increase of frequency */
XO("thousandths of decades")
}
},
};
const BuiltinFormatString *ChooseBuiltinFormatStrings
(NumericConverter::Type type)
{
switch (type) {
default:
case NumericConverter::TIME:
return TimeConverterFormats_;
case NumericConverter::FREQUENCY:
return FrequencyConverterFormats_;
case NumericConverter::BANDWIDTH:
return BandwidthConverterFormats_;
}
}
size_t ChooseNBuiltinFormatStrings
(NumericConverter::Type type)
{
switch (type) {
default:
case NumericConverter::TIME:
return WXSIZEOF(TimeConverterFormats_);
case NumericConverter::FREQUENCY:
return WXSIZEOF(FrequencyConverterFormats_);
case NumericConverter::BANDWIDTH:
return WXSIZEOF(BandwidthConverterFormats_);
}
}
}
//
// ----------------------------------------------------------------------------
// NumericConverter Class
// ----------------------------------------------------------------------------
//
NumericFormatSymbol NumericConverter::DefaultSelectionFormat()
{ return TimeConverterFormats_[4].name; }
NumericFormatSymbol NumericConverter::TimeAndSampleFormat()
{ return TimeConverterFormats_[5].name; }
NumericFormatSymbol NumericConverter::SecondsFormat()
{ return TimeConverterFormats_[0].name; }
NumericFormatSymbol NumericConverter::HoursMinsSecondsFormat()
{ return TimeConverterFormats_[1].name; }
NumericFormatSymbol NumericConverter::HundredthsFormat()
{ return TimeConverterFormats_[3].name; }
NumericFormatSymbol NumericConverter::HertzFormat()
{ return FrequencyConverterFormats_[0].name; }
NumericFormatSymbol NumericConverter::LookupFormat( Type type, const wxString& id)
{
if (id.empty()) {
if (type == TIME)
return DefaultSelectionFormat();
else
return ChooseBuiltinFormatStrings(type)[0].name;
}
else {
auto begin = ChooseBuiltinFormatStrings(type);
auto end = begin + ChooseNBuiltinFormatStrings(type);
auto iter = std::find( begin, end, BuiltinFormatString{ id, {} } );
if (iter == end)
iter = begin;
return iter->name;
}
}
NumericConverter::NumericConverter(Type type,
const NumericFormatSymbol & formatName,
double value,
double sampleRate)
: mBuiltinFormatStrings( ChooseBuiltinFormatStrings( type ) )
, mNBuiltins( ChooseNBuiltinFormatStrings( type ) )
{
ResetMinValue();
ResetMaxValue();
mInvalidValue = -1.0;
mDefaultNdx = 0;
mType = type;
if (type == NumericConverter::TIME )
mDefaultNdx = 4; // Default to "hh:mm:ss + milliseconds".
mScalingFactor = 1.0f;
mSampleRate = 1.0f;
mNtscDrop = false;
mFocusedDigit = 0;
mValue = value; // used in SetSampleRate, reassigned later
SetSampleRate(sampleRate);
SetFormatName(formatName);
SetValue(value); // mValue got overridden to -1 in ControlsToValue(), reassign
}
void NumericConverter::ParseFormatString(
const TranslatableString & untranslatedFormat)
{
auto format = untranslatedFormat.Translation();
mPrefix = wxT("");
mFields.clear();
mDigits.clear();
mScalingFactor = 1.0;
// We will change inFrac to true when we hit our first decimal point.
bool inFrac = false;
int fracMult = 1;
int numWholeFields = 0;
int numFracFields = 0;
wxString numStr;
wxString delimStr;
unsigned int i;
mNtscDrop = false;
for(i=0; i<format.length(); i++) {
bool handleDelim = false;
bool handleNum = false;
if (format[i] == '|') {
wxString remainder = format.Right(format.length() - i - 1);
// For languages which use , as a separator.
remainder.Replace(wxT(","), wxT("."));
if (remainder == wxT("#"))
mScalingFactor = mSampleRate;
else if (remainder == wxT("N")) {
mNtscDrop = true;
}
else
// Use the C locale here for string to number.
// Translations are often incomplete.
// We can't rely on the correct ',' or '.' in the
// translation, so we work based on '.' for decimal point.
remainder.ToCDouble(&mScalingFactor);
i = format.length()-1; // force break out of loop
if (!delimStr.empty())
handleDelim = true;
if (!numStr.empty())
handleNum = true;
}
else if ((format[i] >= '0' && format[i] <='9') ||
format[i] == wxT('*') || format[i] == wxT('#')) {
numStr += format[i];
if (!delimStr.empty())
handleDelim = true;
}
else {
delimStr += format[i];
if (!numStr.empty())
handleNum = true;
}
if (i == format.length() - 1) {
if (!numStr.empty())
handleNum = true;
if (!delimStr.empty())
handleDelim = true;
}
if (handleNum) {
bool zeropad = false;
long range = 0;
if (numStr.Right(1) == wxT("#"))
range = (long int)mSampleRate;
else if (numStr.Right(1) != wxT("*")) {
numStr.ToLong(&range);
}
if (numStr.GetChar(0)=='0' && numStr.length()>1)
zeropad = true;
// Hack: always zeropad
zeropad = true;
if (inFrac) {
int base = fracMult * range;
mFields.push_back(NumericField(inFrac, base, range, zeropad));
fracMult *= range;
numFracFields++;
}
else {
unsigned int j;
for(j=0; j<mFields.size(); j++)
mFields[j].base *= range;
mFields.push_back(NumericField(inFrac, 1, range, zeropad));
numWholeFields++;
}
numStr = wxT("");
}
if (handleDelim) {
bool goToFrac = false;
if (!inFrac) {
wxChar delim = delimStr[delimStr.length()-1];
if (delim=='<' || delim=='>') {
goToFrac = true;
if (delimStr.length() > 1)
delimStr = delimStr.BeforeLast(delim);
}
}
if (inFrac) {
if (numFracFields == 0) {
// Should never happen
return;
}
if (handleNum && numFracFields > 1)
mFields[mFields.size()-2].label = delimStr;
else
mFields[mFields.size()-1].label = delimStr;
}
else {
if (numWholeFields == 0)
mPrefix = delimStr;
else {
delimStr.Replace(wxT("<"), wxT(","));
delimStr.Replace(wxT(">"), wxT("."));
mFields[numWholeFields-1].label = delimStr;
}
}
if (goToFrac)
inFrac = true;
delimStr = wxT("");
}
}
for(i = 0; i < mFields.size(); i++) {
mFields[i].CreateDigitFormatStr();
}
int pos = 0;
int j;
mValueMask = wxT("");
mValueTemplate = wxT("");
mValueTemplate += mPrefix;
for(j=0; j<(int)mPrefix.length(); j++)
mValueMask += wxT(".");
pos += mPrefix.length();
for(i = 0; i < mFields.size(); i++) {
mFields[i].pos = pos;
for(j=0; j<mFields[i].digits; j++) {
mDigits.push_back(DigitInfo(i, j, pos, wxRect()));
mValueTemplate += wxT("0");
mValueMask += wxT("0");
pos++;
}
pos += mFields[i].label.length();
mValueTemplate += mFields[i].label;
for(j=0; j<(int)mFields[i].label.length(); j++)
mValueMask += wxT(".");
}
}
void NumericConverter::PrintDebugInfo()
{
unsigned int i;
wxPrintf("%s", (const char *)mPrefix.mb_str());
for(i = 0; i < mFields.size(); i++) {
if (mFields[i].frac) {
wxPrintf("(t * %d) %% %d '%s' ",
mFields[i].base,
mFields[i].range,
(const char *)mFields[i].label.mb_str());
}
else {
wxPrintf("(t / %d) %% %d '%s' ",
mFields[i].base,
mFields[i].range,
(const char *)mFields[i].label.mb_str());
}
}
wxPrintf("\n");
}
NumericConverter::~NumericConverter()
{
}
void NumericConverter::ValueToControls()
{
ValueToControls(mValue);
}
void NumericConverter::ValueToControls(double rawValue, bool nearest /* = true */)
{
//rawValue = 4.9995f; Only for testing!
if (mType == TIME)
rawValue =
floor(rawValue * mSampleRate + (nearest ? 0.5f : 0.0f))
/ mSampleRate; // put on a sample
double theValue =
rawValue * mScalingFactor
// PRL: what WAS this .000001 for? Nobody could explain.
// + .000001
;
sampleCount t_int;
bool round = true;
// We round on the last field. If we have a fractional field we round using it.
// Otherwise we round to nearest integer.
for(unsigned int i = 0; i < mFields.size(); i++) {
if (mFields[i].frac)
round = false;
}
if (theValue < 0)
t_int = -1;
else if(round)
t_int = sampleCount(theValue + (nearest ? 0.5f : 0.0f));
else
{
wxASSERT( mFields.back().frac );
theValue += (nearest ? 0.5f : 0.0f) / mFields.back().base;
t_int = sampleCount(theValue);
}
double t_frac;
if (theValue < 0)
t_frac = -1;
else
t_frac = (theValue - t_int.as_double() );
unsigned int i;
int tenMins;
int mins;
int addMins;
int secs;
int frames;
mValueString = mPrefix;
if(mNtscDrop && theValue >= 0) {
frames = (int)(theValue*30./1.001 + (nearest ? 0.5f : 0.0f));
tenMins = frames/17982;
frames -= tenMins*17982;
mins = tenMins * 10;
if(frames >= 1800) {
frames -= 1800;