forked from audacity/audacity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalnum.cpp
More file actions
566 lines (479 loc) · 15.7 KB
/
Copy pathvalnum.cpp
File metadata and controls
566 lines (479 loc) · 15.7 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
/////////////////////////////////////////////////////////////////////////////
//
// Backport from wxWidgets-3.0-rc1
//
/////////////////////////////////////////////////////////////////////////////
// Name: src/common/valnum.cpp
// Purpose: Numeric validator classes implementation
// Author: Vadim Zeitlin based on the submission of Fulvio Senore
// Created: 2010-11-06
// Copyright: (c) 2010 wxWidgets team
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
// ============================================================================
// Declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include "../Audacity.h"
#include "valnum.h"
// For compilers that support precompilation, includes "wx.h".
#include <wx/wxprec.h>
#include <wx/setup.h> // for wxUSE_* macros
#include "AudacityMessageBox.h"
#include "../Internat.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#if wxUSE_VALIDATORS && wxUSE_TEXTCTRL
#ifndef WX_PRECOMP
#include <wx/textctrl.h>
#include <wx/combobox.h>
#endif
#include <wx/clipbrd.h>
#include <wx/dataobj.h>
#include "numformatter.h"
// ============================================================================
// NumValidatorBase implementation
// ============================================================================
BEGIN_EVENT_TABLE(NumValidatorBase, wxValidator)
EVT_CHAR(NumValidatorBase::OnChar)
EVT_TEXT_PASTE(wxID_ANY, NumValidatorBase::OnPaste)
EVT_KILL_FOCUS(NumValidatorBase::OnKillFocus)
END_EVENT_TABLE()
int NumValidatorBase::GetFormatFlags() const
{
int flags = NumberFormatter::Style_None;
if ( m_style & NumValidatorStyle::THOUSANDS_SEPARATOR )
flags |= NumberFormatter::Style_WithThousandsSep;
if ( m_style & NumValidatorStyle::NO_TRAILING_ZEROES )
flags |= NumberFormatter::Style_NoTrailingZeroes;
if ( m_style & NumValidatorStyle::ONE_TRAILING_ZERO )
flags |= NumberFormatter::Style_OneTrailingZero;
if ( m_style & NumValidatorStyle::TWO_TRAILING_ZEROES )
flags |= NumberFormatter::Style_TwoTrailingZeroes;
if ( m_style & NumValidatorStyle::THREE_TRAILING_ZEROES )
flags |= NumberFormatter::Style_ThreeTrailingZeroes;
return flags;
}
wxTextEntry *NumValidatorBase::GetTextEntry() const
{
#if wxUSE_TEXTCTRL
if ( wxTextCtrl *text = wxDynamicCast(m_validatorWindow, wxTextCtrl) )
return text;
#endif // wxUSE_TEXTCTRL
#if wxUSE_COMBOBOX
if ( wxComboBox *combo = wxDynamicCast(m_validatorWindow, wxComboBox) )
return combo;
#endif // wxUSE_COMBOBOX
wxFAIL_MSG(wxT("Can only be used with wxTextCtrl or wxComboBox"));
return NULL;
}
bool NumValidatorBase::Validate(wxWindow *parent)
{
// If window is disabled, simply return
if ( !m_validatorWindow->IsEnabled() )
return true;
TranslatableString errmsg;
bool res = DoValidateNumber(&errmsg);
if ( !res )
{
AudacityMessageBox(
errmsg,
XO("Validation error"),
wxOK | wxICON_ERROR,
parent);
wxTextEntry *te = GetTextEntry();
if ( te )
{
te->SelectAll();
m_validatorWindow->SetFocus();
}
return false;
}
return true;
}
void
NumValidatorBase::GetCurrentValueAndInsertionPoint(wxString& val,
int& pos) const
{
wxTextEntry * const control = GetTextEntry();
if ( !control )
return;
val = control->GetValue();
pos = control->GetInsertionPoint();
long selFrom, selTo;
control->GetSelection(&selFrom, &selTo);
const long selLen = selTo - selFrom;
if ( selLen )
{
// Remove selected text because pressing a key would make it disappear.
val.erase(selFrom, selLen);
// And adjust the insertion point to have correct position in the NEW
// string.
if ( pos > selFrom )
{
if ( pos >= selTo )
pos -= selLen;
else
pos = selFrom;
}
}
}
bool NumValidatorBase::IsMinusOk(const wxString& val, int pos) const
{
// Minus is only ever accepted in the beginning of the string.
if ( pos != 0 )
return false;
// And then only if there is no existing minus sign there.
if ( !val.empty() && val[0] == '-' )
return false;
return true;
}
void NumValidatorBase::OnChar(wxKeyEvent& event)
{
// By default we just validate this key so don't prevent the normal
// handling from taking place.
event.Skip();
if ( !m_validatorWindow )
return;
#if wxUSE_UNICODE
const int ch = event.GetUnicodeKey();
const int c = event.GetKeyCode();
if ( c > WXK_START )
{
// It's a character without any Unicode equivalent at all, e.g. cursor
// arrow or function key, we never filter those.
return;
}
#else // !wxUSE_UNICODE
const int ch = event.GetKeyCode();
const int c = ch;
if ( ch > WXK_DELETE )
{
// Not a character neither.
return;
}
#endif // wxUSE_UNICODE/!wxUSE_UNICODE
// Space is an allowed thousands separator. But we don't allow user to type
// it. We will add it at formatting time in OnKillFocus().
if ( c < WXK_SPACE || c == WXK_DELETE )
{
// Allow ASCII control characters and Delete.
return;
}
// Check if this character is allowed in the current state.
wxString val;
int pos;
GetCurrentValueAndInsertionPoint(val, pos);
if ( !IsCharOk(val, pos, ch) )
{
if ( !wxValidator::IsSilent() )
wxBell();
// Do not skip the event in this case, stop handling it here.
event.Skip(false);
}
}
void NumValidatorBase::OnPaste(wxClipboardTextEvent& event)
{
event.Skip(false);
wxTextEntry * const control = GetTextEntry();
if ( !control )
{
return;
}
wxClipboardLocker cb;
// if (!wxClipboard::Get()->IsSupported(wxDataFormat(wxDF_UNICODETEXT)))
if (!wxClipboard::Get()->IsSupported(wxDF_UNICODETEXT))
{
return;
}
wxTextDataObject data;
if (!wxClipboard::Get()->GetData( data ))
{
return;
}
wxString toPaste = data.GetText();
wxString val;
int pos;
GetCurrentValueAndInsertionPoint(val, pos);
for (size_t i = 0, cnt = toPaste.length(); i < cnt; i++)
{
const wxChar ch = toPaste[i];
// Check if this character is allowed in the current state.
if ( IsCharOk(val, pos, ch) )
{
val = GetValueAfterInsertingChar(val, pos++, ch);
}
else if ( !wxValidator::IsSilent() )
{
wxBell();
}
}
// When we change the control value below, its "modified" status is reset
// so we need to explicitly keep it marked as modified if it was so in the
// first place.
//
// Notice that only wxTextCtrl (and not wxTextEntry) has
// IsModified()/MarkDirty() methods hence the need for dynamic cast.
wxTextCtrl * const text = wxDynamicCast(m_validatorWindow, wxTextCtrl);
const bool wasModified = text ? text->IsModified() : false;
// Use SetValue because effect still needs EVT_TEXT (bug 1357)
control->SetValue(NormalizeString(val));
if ( wasModified )
{
text->MarkDirty();
}
}
void NumValidatorBase::OnKillFocus(wxFocusEvent& event)
{
wxTextEntry * const control = GetTextEntry();
if ( !control )
return;
// When we change the control value below, its "modified" status is reset
// so we need to explicitly keep it marked as modified if it was so in the
// first place.
//
// Notice that only wxTextCtrl (and not wxTextEntry) has
// IsModified()/MarkDirty() methods hence the need for dynamic cast.
wxTextCtrl * const text = wxDynamicCast(m_validatorWindow, wxTextCtrl);
const bool wasModified = text ? text->IsModified() : false;
control->ChangeValue(NormalizeString(control->GetValue()));
if ( wasModified )
text->MarkDirty();
event.Skip();
// Validate(text);
}
// ============================================================================
// IntegerValidatorBase implementation
// ============================================================================
wxString IntegerValidatorBase::ToString(LongestValueType value) const
{
return NumberFormatter::ToString(value, GetFormatFlags());
}
bool
IntegerValidatorBase::FromString(const wxString& s, LongestValueType *value)
{
return NumberFormatter::FromString(s, value);
}
bool
IntegerValidatorBase::IsCharOk(const wxString& val, int pos, wxChar ch) const
{
// We may accept minus sign if we can represent negative numbers at all.
if ( ch == '-' )
{
// Notice that entering '-' can make our value invalid, for example if
// we're limited to -5..15 range and the current value is 12, then the
// NEW value would be (invalid) -12. We consider it better to let the
// user do this because perhaps he is going to press Delete key next to
// make it -2 and forcing him to DELETE 1 first would be unnatural.
//
// TODO: It would be nice to indicate that the current control contents
// is invalid (if it's indeed going to be the case) once
// wxValidator supports doing this non-intrusively.
return m_min < 0 && IsMinusOk(val, pos);
}
// A separator is accepted if the locale allow it, the other chars must be digits
if ( ch < '0' || ch > '9' )
{
wxChar thousands;
if ( NumberFormatter::GetThousandsSeparatorIfUsed(&thousands) )
{
// if (ch != thousands)
return false;
}
else
{
return false;
}
}
return true;
}
bool IntegerValidatorBase::DoValidateNumber(TranslatableString * errMsg) const
{
wxTextEntry * const control = GetTextEntry();
if ( !control )
return false;
wxString s(control->GetValue());
wxChar thousandsSep;
if ( NumberFormatter::GetThousandsSeparatorIfUsed(&thousandsSep) )
s.Replace(wxString(thousandsSep), wxString());
if ( s.empty() )
{
// Is blank, but allowed. Stop here
if ( HasFlag(NumValidatorStyle::ZERO_AS_BLANK) )
{
return true;
}
// We can't do any check with an empty string
else
{
*errMsg = XO("Empty value");
return false;
}
}
// Can it be converted to a value?
LongestValueType value = 0;
bool res = FromString(s, &value);
if ( !res )
*errMsg = XO("Malformed number");
else
{
res = IsInRange(value);
if ( !res )
*errMsg = XO("Not in range %d to %d")
.Format( (int) m_min, (int) m_max );
}
return res;
}
// ============================================================================
// FloatingPointValidatorBase implementation
// ============================================================================
wxString FloatingPointValidatorBase::ToString(LongestValueType value) const
{
return NumberFormatter::ToString(value, m_precision, GetFormatFlags());
}
bool
FloatingPointValidatorBase::FromString(const wxString& s,
LongestValueType *value)
{
return NumberFormatter::FromString(s, value);
}
bool
FloatingPointValidatorBase::IsCharOk(const wxString& val,
int pos,
wxChar ch) const
{
if ( ch == '-' )
{
// We may accept minus sign if we can represent negative numbers at all.
if ( pos == 0 )
return m_min < 0 && IsMinusOk(val, pos);
// or for the exponent definition
else if ( val[pos-1] != 'e' && val[pos-1] != 'E' )
return false;
return true;
}
else if ( ch == '+' )
{
if ( pos == 0 )
return m_max >= 0;
else if ( val[pos-1] != 'e' && val[pos-1] != 'E' )
return false;
return true;
}
const wxChar separator = NumberFormatter::GetDecimalSeparator();
if ( ch == separator )
{
if ( val.find(separator) != wxString::npos )
{
// There is already a decimal separator, can't insert another one.
return false;
}
// Prepending a separator before the sign isn't allowed.
if ( pos == 0 && !val.empty() && ( val[0] == '-' || val[0] == '+' ) )
return false;
// Otherwise always accept it, adding a decimal separator doesn't
// change the number value and, in particular, can't make it invalid.
// OTOH the checks below might not pass because strings like "." or
// "-." are not valid numbers so parsing them would fail, hence we need
// to treat it specially here.
return true;
}
// Must be a digit, an exponent or a thousands separator.
if( ( ch < '0' || ch > '9' ) && ch != 'E' && ch != 'e' )
{
wxChar thousands;
if ( NumberFormatter::GetThousandsSeparatorIfUsed(&thousands) )
{
// if (ch != thousands)
return false;
}
else
{
return false;
}
}
// Check the number of decimal digits in the final string
wxString str(val);
str.insert(pos, ch);
return ValidatePrecision(str);
}
bool FloatingPointValidatorBase::DoValidateNumber(TranslatableString * errMsg) const
{
wxTextEntry * const control = GetTextEntry();
if ( !control )
return false;
wxString s(control->GetValue());
wxChar thousandsSep;
if ( NumberFormatter::GetThousandsSeparatorIfUsed(&thousandsSep) )
s.Replace(wxString(thousandsSep), wxString());
if ( s.empty() )
{
if ( HasFlag(NumValidatorStyle::ZERO_AS_BLANK) )
return true; //Is blank, but allowed. Stop here
else
{
*errMsg = XO("Empty value");
return false; //We can't do any checks with an empty string
}
}
LongestValueType value = 0;
bool res = FromString(s, &value); // Can it be converted to a value?
if ( !res )
*errMsg = XO("Value overflow");
else
{
res = ValidatePrecision(s);
if ( !res )
*errMsg = XO("Too many decimal digits");
else
{
res = IsInRange(value);
if ( !res )
{
wxString strMin = wxString::Format(wxT("%f"), m_min);
wxString strMax = wxString::Format(wxT("%f"), m_max);
NumberFormatter::RemoveTrailingZeroes(strMin);
NumberFormatter::RemoveTrailingZeroes(strMax);
if (m_minSet && m_maxSet)
{
*errMsg = XO("Value not in range: %s to %s")
.Format( strMin, strMax );
}
else if (m_minSet)
{
*errMsg = XO("Value must not be less than %s").Format( strMin );
}
else if (m_maxSet)
{
*errMsg = XO("Value must not be greater than %s")
.Format( strMax );
}
}
}
}
return res;
}
bool FloatingPointValidatorBase::ValidatePrecision(const wxString& s) const
{
size_t posSep = s.find(NumberFormatter::GetDecimalSeparator());
if ( posSep == wxString::npos )
posSep = s.length();
// If user typed exponent 'e' the number of decimal digits is not
// important at all. But we must know that 'e' position.
// PRL: I can't find anything in lconv or std::numpunct that describes
// alternatives to e. So just use a plain string literal. Don't trouble
// with i18n.
size_t posExp = s.Lower().Find("e");
if ( posExp == wxString::npos )
posExp = s.length();
// Return true if number has no more decimal digits than allowed
return ( (int)(posExp - posSep) - 1 <= (int)m_precision );
}
double RoundValue(int precision, double value)
{
return Internat::CompatibleToDouble( Internat::ToString(value, precision) );
}
#endif // wxUSE_VALIDATORS && wxUSE_TEXTCTRL