From: <ste...@us...> - 2015-02-09 17:14:36
|
Revision: 3146 http://sourceforge.net/p/stella/code/3146 Author: stephena Date: 2015-02-09 17:14:28 +0000 (Mon, 09 Feb 2015) Log Message: ----------- Added per-textfield character filtering, and enabled it for CheatCodeDialog. Modified Paths: -------------- trunk/Changes.txt trunk/src/cheat/CheatCodeDialog.cxx trunk/src/gui/EditableWidget.cxx trunk/src/gui/EditableWidget.hxx trunk/src/gui/InputTextDialog.cxx trunk/src/gui/InputTextDialog.hxx Modified: trunk/Changes.txt =================================================================== --- trunk/Changes.txt 2015-02-06 16:42:24 UTC (rev 3145) +++ trunk/Changes.txt 2015-02-09 17:14:28 UTC (rev 3146) @@ -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! Modified: trunk/src/cheat/CheatCodeDialog.cxx =================================================================== --- trunk/src/cheat/CheatCodeDialog.cxx 2015-02-06 16:42:24 UTC (rev 3145) +++ trunk/src/cheat/CheatCodeDialog.cxx 2015-02-09 17:14:28 UTC (rev 3146) @@ -83,11 +83,22 @@ // 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 Modified: trunk/src/gui/EditableWidget.cxx =================================================================== --- trunk/src/gui/EditableWidget.cxx 2015-02-06 16:42:24 UTC (rev 3145) +++ trunk/src/gui/EditableWidget.cxx 2015-02-09 17:14:28 UTC (rev 3146) @@ -40,6 +40,9 @@ _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 @@ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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 @@ { _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; Modified: trunk/src/gui/EditableWidget.hxx =================================================================== --- trunk/src/gui/EditableWidget.hxx 2015-02-06 16:42:24 UTC (rev 3145) +++ trunk/src/gui/EditableWidget.hxx 2015-02-09 17:14:28 UTC (rev 3146) @@ -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 @@ // 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 @@ 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 @@ int _editScrollOffset; static string _clippedText; + + private: + TextFilter _filter; }; #endif Modified: trunk/src/gui/InputTextDialog.cxx =================================================================== --- trunk/src/gui/InputTextDialog.cxx 2015-02-06 16:42:24 UTC (rev 3145) +++ trunk/src/gui/InputTextDialog.cxx 2015-02-09 17:14:28 UTC (rev 3146) @@ -71,7 +71,7 @@ WidgetArray wid; // Calculate real dimensions - _w = fontWidth * 30; + _w = fontWidth * 35; _h = lineHeight * 4 + (int)labels.size() * (lineHeight + 5); // Determine longest label @@ -177,6 +177,13 @@ } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void InputTextDialog::setTextFilter(EditableWidget::TextFilter& f, int idx) +{ + if((uInt32)idx < myInput.size()) + myInput[idx]->setTextFilter(f); +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void InputTextDialog::setFocus(int idx) { if((uInt32)idx < myInput.size()) Modified: trunk/src/gui/InputTextDialog.hxx =================================================================== --- trunk/src/gui/InputTextDialog.hxx 2015-02-06 16:42:24 UTC (rev 3145) +++ trunk/src/gui/InputTextDialog.hxx 2015-02-09 17:14:28 UTC (rev 3146) @@ -26,6 +26,7 @@ #include "Dialog.hxx" #include "Command.hxx" +#include "EditableWidget.hxx" class InputTextDialog : public Dialog, public CommandSender { @@ -45,6 +46,8 @@ 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); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |