Added per-textfield character filtering, and enabled it for CheatCodeDialog.

git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@3146 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba
This commit is contained in:
stephena 2015-02-09 17:14:28 +00:00
parent 26fcabc23c
commit 5dece736b1
6 changed files with 52 additions and 9 deletions

View File

@ -26,6 +26,10 @@
* Fixed bug when running ROMs using AtariVox controllers; the app would
crash upon exiting the ROM.
* Certain textfields in the UI now have filtering enabled, preventing
insertion of illegal characters. This will be extended throughout
the code in future releases.
-Have fun!

View File

@ -83,11 +83,22 @@ CheatCodeDialog::CheatCodeDialog(OSystem& osystem, DialogContainer& parent,
// Inputbox which will pop up when adding/editing a cheat
StringList labels;
labels.push_back("Name: ");
labels.push_back("Code: ");
labels.push_back("Name: ");
labels.push_back("Code (hex): ");
myCheatInput = make_ptr<InputTextDialog>(this, font, labels);
myCheatInput->setTarget(this);
// Add filtering for each textfield
EditableWidget::TextFilter f0 = [](char c) {
return isprint(c) && c != '\"' && c != ':';
};
myCheatInput->setTextFilter(f0, 0);
EditableWidget::TextFilter f1 = [](char c) {
return (c >= 'a' && c <= 'f') || (c >= '0' && c <= '9');
};
myCheatInput->setTextFilter(f1, 1);
addToFocusList(wid);
// Add OK and Cancel buttons

View File

@ -40,6 +40,9 @@ EditableWidget::EditableWidget(GuiObject* boss, const GUI::Font& font,
_bgcolorhi = kWidColor;
_textcolor = kTextColor;
_textcolorhi = kTextColor;
// By default, include all printable chars except quotes
_filter = [](char c) { return isprint(c) && c != '\"'; };
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -50,9 +53,12 @@ EditableWidget::~EditableWidget()
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void EditableWidget::setText(const string& str, bool)
{
// TODO: We probably should filter the input string here,
// e.g. using tryInsertChar.
_editString = str;
// Filter input string
_editString = "";
for(int i = 0; i < str.size(); ++i)
if(_filter(tolower(str[i])))
_editString.push_back(str[i]);
_caretPos = (int)_editString.size();
_editScrollOffset = (_font.getStringWidth(_editString) - (getEditRect().width()));
@ -70,15 +76,15 @@ void EditableWidget::setEditable(bool editable)
{
_editable = editable;
if(_editable)
setFlags(WIDGET_WANTS_RAWDATA|WIDGET_RETAIN_FOCUS);
setFlags(WIDGET_WANTS_RAWDATA | WIDGET_RETAIN_FOCUS);
else
clearFlags(WIDGET_WANTS_RAWDATA|WIDGET_RETAIN_FOCUS);
clearFlags(WIDGET_WANTS_RAWDATA | WIDGET_RETAIN_FOCUS);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool EditableWidget::tryInsertChar(char c, int pos)
{
if(isprint(c) && c != '\"')
if(_filter(tolower(c)))
{
_editString.insert(pos, 1, c);
return true;

View File

@ -30,6 +30,9 @@
class EditableWidget : public Widget, public CommandSender
{
public:
/** Function used by 'tryInsertChar' to test validity of a character */
using TextFilter = std::function<bool(char)>;
enum {
kAcceptCmd = 'EDac',
kCancelCmd = 'EDcl',
@ -53,6 +56,9 @@ class EditableWidget : public Widget, public CommandSender
// We only want to focus this widget when we can edit its contents
virtual bool wantsFocus() { return _editable; }
// Set filter used by 'tryInsertChar'
void setTextFilter(TextFilter& filter) { _filter = filter; }
protected:
virtual void startEditMode() { setFlags(WIDGET_WANTS_RAWDATA); }
virtual void endEditMode() { clearFlags(WIDGET_WANTS_RAWDATA); }
@ -64,6 +70,9 @@ class EditableWidget : public Widget, public CommandSender
bool setCaretPos(int newPos);
bool adjustOffset();
// This method will use the current TextFilter to insert a character
// Note that classes which override this method will no longer use the
// current TextFilter, and will assume all responsibility for filtering
virtual bool tryInsertChar(char c, int pos);
private:
@ -91,6 +100,9 @@ class EditableWidget : public Widget, public CommandSender
int _editScrollOffset;
static string _clippedText;
private:
TextFilter _filter;
};
#endif

View File

@ -71,7 +71,7 @@ void InputTextDialog::initialize(const GUI::Font& lfont, const GUI::Font& nfont,
WidgetArray wid;
// Calculate real dimensions
_w = fontWidth * 30;
_w = fontWidth * 35;
_h = lineHeight * 4 + (int)labels.size() * (lineHeight + 5);
// Determine longest label
@ -176,6 +176,13 @@ void InputTextDialog::setText(const string& str, int idx)
myInput[idx]->setText(str);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void InputTextDialog::setTextFilter(EditableWidget::TextFilter& f, int idx)
{
if((uInt32)idx < myInput.size())
myInput[idx]->setTextFilter(f);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void InputTextDialog::setFocus(int idx)
{

View File

@ -26,6 +26,7 @@ class EditTextWidget;
#include "Dialog.hxx"
#include "Command.hxx"
#include "EditableWidget.hxx"
class InputTextDialog : public Dialog, public CommandSender
{
@ -45,6 +46,8 @@ class InputTextDialog : public Dialog, public CommandSender
const string& getResult(int idx = 0);
void setText(const string& str, int idx = 0);
void setTextFilter(EditableWidget::TextFilter& f, int idx = 0);
void setEmitSignal(int cmd) { myCmd = cmd; }
void setTitle(const string& title);