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/
|
2006-01-16 22:02:54 +01:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @file validators.cpp
|
|
|
|
/// @brief Various validators for wx
|
|
|
|
/// @ingroup custom_control utility
|
|
|
|
///
|
2006-01-16 22:02:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
///////////
|
|
|
|
// Headers
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#ifndef AGI_PRE
|
2007-09-12 01:22:26 +02:00
|
|
|
#include <wx/textctrl.h>
|
2009-09-10 15:06:40 +02:00
|
|
|
#endif
|
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
|
|
|
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
|
|
|
/// @brief Constructor
|
|
|
|
/// @param _valPtr
|
|
|
|
/// @param isfloat
|
|
|
|
/// @param issigned
|
|
|
|
///
|
2007-06-17 23:12:28 +02:00
|
|
|
NumValidator::NumValidator(wxString* _valPtr,bool isfloat,bool issigned) {
|
|
|
|
isFloat = isfloat;
|
|
|
|
isSigned = issigned;
|
|
|
|
valPtr = _valPtr;
|
|
|
|
|
|
|
|
// Get value
|
|
|
|
if (valPtr) {
|
|
|
|
if (isFloat) {
|
|
|
|
valPtr->ToDouble(&fValue);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
long tLong;
|
|
|
|
valPtr->ToLong(&tLong);
|
|
|
|
iValue = tLong;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
|
|
|
/// @brief Copy constructor
|
|
|
|
/// @param from
|
|
|
|
///
|
2008-03-13 19:55:09 +01:00
|
|
|
NumValidator::NumValidator(const NumValidator &from)
|
|
|
|
: wxValidator()
|
|
|
|
{
|
2007-06-18 20:20:45 +02:00
|
|
|
valPtr = from.valPtr;
|
|
|
|
isFloat = from.isFloat;
|
|
|
|
isSigned = from.isSigned;
|
|
|
|
SetWindow(from.GetWindow());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-17 23:12:28 +02:00
|
|
|
///////////////
|
|
|
|
// Event table
|
|
|
|
BEGIN_EVENT_TABLE(NumValidator, wxValidator)
|
|
|
|
EVT_CHAR(NumValidator::OnChar)
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
|
|
|
/// @brief Clone
|
|
|
|
/// @return
|
|
|
|
///
|
2007-06-17 23:12:28 +02:00
|
|
|
wxObject* NumValidator::Clone() const {
|
|
|
|
NumValidator *clone = new NumValidator(valPtr,isFloat,isSigned);
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
|
|
|
/// @brief Validate
|
|
|
|
/// @param parent
|
|
|
|
/// @return
|
|
|
|
///
|
2007-06-17 23:12:28 +02:00
|
|
|
bool NumValidator::Validate(wxWindow* parent) {
|
|
|
|
wxTextCtrl *ctrl = (wxTextCtrl*) GetWindow();
|
|
|
|
wxString value = ctrl->GetValue();
|
|
|
|
|
|
|
|
// Is the control enabled?
|
|
|
|
if (!ctrl->IsEnabled()) return true;
|
|
|
|
|
|
|
|
// Check length
|
|
|
|
if (value.Length() < 1) return false;
|
|
|
|
|
|
|
|
// Check each character
|
|
|
|
bool gotDecimal = false;
|
|
|
|
for (size_t i=0;i<value.Length();i++) {
|
2007-06-18 20:20:45 +02:00
|
|
|
if (!CheckCharacter(value[i],i==0,true,gotDecimal)) return false;
|
2007-06-17 23:12:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// All clear
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
|
|
|
/// @brief Check if a given character is valid
|
|
|
|
/// @param chr
|
|
|
|
/// @param isFirst
|
|
|
|
/// @param canSign
|
|
|
|
/// @param gotDecimal
|
|
|
|
/// @return
|
|
|
|
///
|
2007-06-18 20:20:45 +02:00
|
|
|
bool NumValidator::CheckCharacter(int chr,bool isFirst,bool canSign,bool &gotDecimal) {
|
2007-06-17 23:12:28 +02:00
|
|
|
// Check sign
|
|
|
|
if (chr == _T('-') || chr == _T('+')) {
|
2007-06-18 20:20:45 +02:00
|
|
|
if (!isFirst || !canSign || !isSigned) return false;
|
2007-06-17 23:12:28 +02:00
|
|
|
else return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
if (chr == _T('.') || chr == _T(',')) {
|
|
|
|
if (!isFloat || gotDecimal) return false;
|
|
|
|
else {
|
|
|
|
gotDecimal = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check digit
|
|
|
|
if (chr < _T('0') || chr > _T('9')) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
|
|
|
/// @brief Filter keypresses
|
|
|
|
/// @param event
|
|
|
|
/// @return
|
|
|
|
///
|
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];
|
|
|
|
if (curchr == _T('.') || curchr == _T(',')) decimals++;
|
|
|
|
if (curchr == _T('+') || curchr == _T('-')) signs++;
|
|
|
|
}
|
|
|
|
bool gotDecimal = decimals > 0;
|
2007-06-18 20:20:45 +02:00
|
|
|
bool isFirst = from == 0;
|
|
|
|
bool canSign = signs == 0;
|
2007-06-17 23:12:28 +02:00
|
|
|
|
|
|
|
// Check character
|
2007-06-18 20:20:45 +02:00
|
|
|
if (!CheckCharacter(chr,isFirst,canSign,gotDecimal)) {
|
2007-06-17 23:12:28 +02:00
|
|
|
if (!wxValidator::IsSilent()) wxBell();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// OK
|
|
|
|
event.Skip();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
|
|
|
/// @brief Transfer to window
|
|
|
|
/// @return
|
|
|
|
///
|
2007-06-17 23:12:28 +02:00
|
|
|
bool NumValidator::TransferToWindow() {
|
|
|
|
wxTextCtrl *ctrl = (wxTextCtrl*) GetWindow();
|
|
|
|
if (isFloat) ctrl->SetValue(PrettyFloatD(fValue));
|
|
|
|
else ctrl->SetValue(wxString::Format(_T("%d"),iValue));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
|
|
|
/// @brief Receive from window
|
|
|
|
///
|
2007-06-17 23:12:28 +02:00
|
|
|
bool NumValidator::TransferFromWindow() {
|
|
|
|
wxTextCtrl *ctrl = (wxTextCtrl*) GetWindow();
|
|
|
|
wxString value = ctrl->GetValue();
|
|
|
|
|
|
|
|
// Validate
|
|
|
|
bool ok = Validate(ctrl);
|
|
|
|
if (!ok) return false;
|
|
|
|
|
|
|
|
// Transfer
|
|
|
|
if (isFloat) {
|
|
|
|
value.ToDouble(&fValue);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
long tLong;
|
|
|
|
value.ToLong(&tLong);
|
|
|
|
iValue = tLong;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2009-07-29 07:43:02 +02:00
|
|
|
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|