[Scummvm-git-logs] scummvm master -> aba347ed9e166ca3e39e9afce69d6118e3762aaf

criezy noreply at scummvm.org
Wed Nov 2 22:15:20 UTC 2022


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
aba347ed9e GUI: Make character validation less ugly


Commit: aba347ed9e166ca3e39e9afce69d6118e3762aaf
    https://github.com/scummvm/scummvm/commit/aba347ed9e166ca3e39e9afce69d6118e3762aaf
Author: Orgad Shaneh (orgads at gmail.com)
Date: 2022-11-02T22:15:16Z

Commit Message:
GUI: Make character validation less ugly

Changed paths:
    gui/editgamedialog.cpp
    gui/widgets/editable.cpp
    gui/widgets/editable.h


diff --git a/gui/editgamedialog.cpp b/gui/editgamedialog.cpp
index 1976910047e..556483eb343 100644
--- a/gui/editgamedialog.cpp
+++ b/gui/editgamedialog.cpp
@@ -80,23 +80,14 @@ enum {
 	kGraphicsTabContainerReflowCmd = 'gtcr'
 };
 
-/*
-* TODO: Clean up this ugly design: we subclass EditTextWidget to perform
-* input validation. It would be much more elegant to use a decorator pattern,
-* or a validation callback, or something like that.
-*/
 class DomainEditTextWidget : public EditTextWidget {
 public:
-	DomainEditTextWidget(GuiObject *boss, const Common::String &name, const Common::U32String &text, const Common::U32String &tooltip = Common::U32String())
+	DomainEditTextWidget(GuiObject *boss, const Common::String &name, const Common::U32String &text, const Common::U32String &tooltip)
 		: EditTextWidget(boss, name, text, tooltip) {}
 
 protected:
-	bool tryInsertChar(Common::u32char_type_t c, int pos) override {
-		if (Common::isAlnum(c) || c == '-' || c == '_') {
-			_editString.insertChar(c, pos);
-			return true;
-		}
-		return false;
+	bool isCharAllowed(Common::u32char_type_t c) const override {
+		return Common::isAlnum(c) || c == '-' || c == '_';
 	}
 };
 
diff --git a/gui/widgets/editable.cpp b/gui/widgets/editable.cpp
index 82ae73040bd..61b831216c1 100644
--- a/gui/widgets/editable.cpp
+++ b/gui/widgets/editable.cpp
@@ -77,12 +77,15 @@ void EditableWidget::setEditString(const Common::U32String &str) {
 	markAsDirty();
 }
 
+bool EditableWidget::isCharAllowed(Common::u32char_type_t c) const {
+	return (c >= 32 && c <= 127) || c >= 160;
+}
+
 bool EditableWidget::tryInsertChar(Common::u32char_type_t c, int pos) {
-	if ((c >= 32 && c <= 127) || c >= 160) {
-		_editString.insertChar(c, pos);
-		return true;
-	}
-	return false;
+	if (!isCharAllowed(c))
+		return false;
+	_editString.insertChar(c, pos);
+	return true;
 }
 
 int EditableWidget::caretVisualPos(int logicalPos) {
diff --git a/gui/widgets/editable.h b/gui/widgets/editable.h
index d64eb317214..bdc9213d553 100644
--- a/gui/widgets/editable.h
+++ b/gui/widgets/editable.h
@@ -94,7 +94,8 @@ protected:
 
 	void setFontStyle(ThemeEngine::FontStyle font) { _font = font; }
 
-	virtual bool tryInsertChar(Common::u32char_type_t c, int pos);
+	virtual bool isCharAllowed(Common::u32char_type_t c) const;
+	bool tryInsertChar(Common::u32char_type_t c, int pos);
 
 	int caretVisualPos(int logicalPos);
 	int caretLogicalPos() const;




More information about the Scummvm-git-logs mailing list