2006-01-16 22:02:54 +01:00
|
|
|
// Copyright (c) 2005, Rodrigo Braz Monteiro
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file validators.cpp
|
|
|
|
/// @brief Various validators for wx
|
|
|
|
/// @ingroup custom_control utility
|
|
|
|
///
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2007-09-12 01:22:26 +02:00
|
|
|
#include <wx/textctrl.h>
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "utils.h"
|
|
|
|
#include "validators.h"
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-01-20 06:57:30 +01:00
|
|
|
NumValidator::NumValidator(wxString val, bool isfloat, bool issigned)
|
2012-10-15 06:37:14 +02:00
|
|
|
: fValue(0)
|
|
|
|
, iValue(0)
|
|
|
|
, isFloat(isfloat)
|
2011-01-20 06:57:30 +01:00
|
|
|
, isSigned(issigned)
|
|
|
|
{
|
|
|
|
if (isFloat) {
|
2012-10-15 06:37:14 +02:00
|
|
|
val.ToDouble(&fValue);
|
2011-01-20 06:57:30 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-11-28 23:16:33 +01:00
|
|
|
long tLong = 0;
|
2011-01-20 06:57:30 +01:00
|
|
|
val.ToLong(&tLong);
|
|
|
|
iValue = tLong;
|
2007-06-17 23:12:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-22 22:23:07 +01:00
|
|
|
NumValidator::NumValidator(int val, bool issigned)
|
2012-10-15 06:37:14 +02:00
|
|
|
: fValue(0)
|
|
|
|
, iValue(val)
|
2011-12-22 22:23:07 +01:00
|
|
|
, isFloat(false)
|
|
|
|
, isSigned(issigned)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-01-18 23:51:17 +01:00
|
|
|
NumValidator::NumValidator(int64_t val, bool issigned)
|
2012-10-15 06:37:14 +02:00
|
|
|
: fValue(0)
|
|
|
|
, iValue((int)val)
|
2012-01-18 23:51:17 +01:00
|
|
|
, isFloat(false)
|
|
|
|
, isSigned(issigned)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-12-22 22:23:07 +01:00
|
|
|
NumValidator::NumValidator(double val, bool issigned)
|
|
|
|
: fValue(val)
|
2012-10-15 06:37:14 +02:00
|
|
|
, iValue(0)
|
2011-12-22 22:23:07 +01:00
|
|
|
, isFloat(true)
|
|
|
|
, isSigned(issigned)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-03-13 19:55:09 +01:00
|
|
|
NumValidator::NumValidator(const NumValidator &from)
|
|
|
|
: wxValidator()
|
2011-01-20 06:57:30 +01:00
|
|
|
, fValue(from.fValue)
|
|
|
|
, iValue(from.iValue)
|
|
|
|
, isFloat(from.isFloat)
|
|
|
|
, isSigned(from.isSigned)
|
2008-03-13 19:55:09 +01:00
|
|
|
{
|
2007-06-18 20:20:45 +02:00
|
|
|
SetWindow(from.GetWindow());
|
|
|
|
}
|
|
|
|
|
2007-06-17 23:12:28 +02:00
|
|
|
BEGIN_EVENT_TABLE(NumValidator, wxValidator)
|
2011-01-20 06:57:30 +01:00
|
|
|
EVT_CHAR(NumValidator::OnChar)
|
2007-06-17 23:12:28 +02:00
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
wxObject* NumValidator::Clone() const {
|
2011-01-20 06:57:30 +01:00
|
|
|
return new NumValidator(*this);
|
2007-06-17 23:12:28 +02:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:09:31 +01:00
|
|
|
bool NumValidator::Validate(wxWindow*) {
|
2007-06-17 23:12:28 +02:00
|
|
|
wxTextCtrl *ctrl = (wxTextCtrl*) GetWindow();
|
|
|
|
wxString value = ctrl->GetValue();
|
|
|
|
|
2011-01-20 06:57:30 +01:00
|
|
|
if (!ctrl->IsEnabled()) return true;
|
2007-06-17 23:12:28 +02:00
|
|
|
|
|
|
|
if (value.Length() < 1) return false;
|
|
|
|
|
|
|
|
bool gotDecimal = false;
|
2011-01-20 06:57:30 +01:00
|
|
|
for (size_t i = 0; i < value.Length(); i++) {
|
|
|
|
if (!CheckCharacter(value[i], !i, true, gotDecimal))
|
|
|
|
return false;
|
2007-06-17 23:12:28 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-20 06:57:30 +01:00
|
|
|
bool NumValidator::CheckCharacter(int chr, bool isFirst, bool canSign, bool &gotDecimal) {
|
2007-06-17 23:12:28 +02:00
|
|
|
// Check sign
|
2011-01-20 06:57:30 +01:00
|
|
|
if (chr == '-' || chr == '+') {
|
|
|
|
return isFirst && canSign && isSigned;
|
2007-06-17 23:12:28 +02:00
|
|
|
}
|
|
|
|
|
2007-06-18 20:20:45 +02:00
|
|
|
// Don't allow anything before a sign
|
|
|
|
if (isFirst && !canSign) return false;
|
|
|
|
|
2007-06-17 23:12:28 +02:00
|
|
|
// Check decimal point
|
2011-01-20 06:57:30 +01:00
|
|
|
if (chr == '.' || chr == ',') {
|
|
|
|
if (!isFloat || gotDecimal)
|
|
|
|
return false;
|
2007-06-17 23:12:28 +02:00
|
|
|
else {
|
|
|
|
gotDecimal = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check digit
|
2011-01-20 06:57:30 +01:00
|
|
|
return chr >= '0' && chr <= '9';
|
2007-06-17 23:12:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void NumValidator::OnChar(wxKeyEvent& event) {
|
|
|
|
wxTextCtrl *ctrl = (wxTextCtrl*) GetWindow();
|
|
|
|
wxString value = ctrl->GetValue();
|
|
|
|
int chr = event.GetKeyCode();
|
|
|
|
|
|
|
|
// Special keys
|
|
|
|
if (chr < WXK_SPACE || chr == WXK_DELETE || chr > WXK_START) {
|
|
|
|
event.Skip();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get selection
|
|
|
|
long from,to;
|
|
|
|
ctrl->GetSelection(&from,&to);
|
|
|
|
|
|
|
|
// Count decimal points and signs outside selection
|
|
|
|
int decimals = 0;
|
|
|
|
int signs = 0;
|
|
|
|
wxChar curchr;
|
|
|
|
for (size_t i=0;i<value.Length();i++) {
|
|
|
|
if (i >= (unsigned)from && i < (unsigned)to) continue;
|
|
|
|
curchr = value[i];
|
2011-01-20 06:57:30 +01:00
|
|
|
if (curchr == '.' || curchr == ',') decimals++;
|
|
|
|
if (curchr == '+' || curchr == '-') signs++;
|
2007-06-17 23:12:28 +02:00
|
|
|
}
|
|
|
|
bool gotDecimal = decimals > 0;
|
|
|
|
|
|
|
|
// Check character
|
2011-01-20 06:57:30 +01:00
|
|
|
if (!CheckCharacter(chr,!from,!signs,gotDecimal)) {
|
2007-06-17 23:12:28 +02:00
|
|
|
if (!wxValidator::IsSilent()) wxBell();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// OK
|
|
|
|
event.Skip();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NumValidator::TransferToWindow() {
|
|
|
|
wxTextCtrl *ctrl = (wxTextCtrl*) GetWindow();
|
2011-01-20 06:57:30 +01:00
|
|
|
if (isFloat)
|
|
|
|
ctrl->SetValue(wxString::Format("%g",fValue));
|
|
|
|
else
|
|
|
|
ctrl->SetValue(wxString::Format("%d",iValue));
|
2007-06-17 23:12:28 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NumValidator::TransferFromWindow() {
|
|
|
|
wxTextCtrl *ctrl = (wxTextCtrl*) GetWindow();
|
|
|
|
wxString value = ctrl->GetValue();
|
|
|
|
|
2011-01-20 06:57:30 +01:00
|
|
|
if (!Validate(ctrl)) return false;
|
2007-06-17 23:12:28 +02:00
|
|
|
|
|
|
|
// Transfer
|
|
|
|
if (isFloat) {
|
|
|
|
value.ToDouble(&fValue);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
long tLong;
|
|
|
|
value.ToLong(&tLong);
|
|
|
|
iValue = tLong;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|