aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gui/EditTextWidget.cpp13
-rw-r--r--gui/EditTextWidget.h3
-rw-r--r--gui/launcher.cpp25
3 files changed, 36 insertions, 5 deletions
diff --git a/gui/EditTextWidget.cpp b/gui/EditTextWidget.cpp
index ea24035b11..076a5da1e1 100644
--- a/gui/EditTextWidget.cpp
+++ b/gui/EditTextWidget.cpp
@@ -71,6 +71,14 @@ void EditTextWidget::handleMouseDown(int x, int y, int button, int clickCount){
draw();
}
+bool EditTextWidget::tryInsertChar(char c, int pos) {
+ if (isprint(c)) {
+ _label.insertChar(c, pos);
+ return true;
+ }
+ return false;
+}
+
bool EditTextWidget::handleKeyDown(uint16 ascii, int keycode, int modifiers) {
bool handled = true;
bool dirty = false;
@@ -126,9 +134,8 @@ bool EditTextWidget::handleKeyDown(uint16 ascii, int keycode, int modifiers) {
dirty = adjustOffset();
break;
default:
- if (isprint((char)ascii)) {
- _label.insertChar((char)ascii, _pos++);
- //_label += (char)ascii;
+ if (tryInsertChar((char)ascii, _pos)) {
+ _pos++;
dirty = true;
} else {
handled = false;
diff --git a/gui/EditTextWidget.h b/gui/EditTextWidget.h
index 505982743d..be94cc5322 100644
--- a/gui/EditTextWidget.h
+++ b/gui/EditTextWidget.h
@@ -28,6 +28,7 @@ namespace GUI {
/* EditTextWidget */
class EditTextWidget : public StaticTextWidget {
+public:
typedef Common::StringList StringList;
typedef Common::String String;
protected:
@@ -52,6 +53,8 @@ protected:
int getCaretPos() const;
bool adjustOffset();
+
+ virtual bool tryInsertChar(char c, int pos);
};
} // End of namespace GUI
diff --git a/gui/launcher.cpp b/gui/launcher.cpp
index 5e9f9b3e08..e029dc4e8d 100644
--- a/gui/launcher.cpp
+++ b/gui/launcher.cpp
@@ -69,6 +69,27 @@ enum {
};
/*
+ * 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, int x, int y, int w, int h, const String &text)
+ : EditTextWidget(boss, x, y, w, h, text) {
+ }
+
+protected:
+ bool tryInsertChar(char c, int pos) {
+ if (isalnum(c) || c == '-' || c == '_') {
+ _label.insertChar(c, pos);
+ return true;
+ }
+ return false;
+ }
+};
+
+/*
* A dialog that allows the user to edit a config game entry.
* TODO: add widgets for some/all of the following
* - Maybe scaler/graphics mode. But there are two problems:
@@ -96,7 +117,7 @@ public:
protected:
EditTextWidget *_descriptionWidget;
- EditTextWidget *_domainWidget;
+ DomainEditTextWidget *_domainWidget;
StaticTextWidget *_gamePathWidget;
StaticTextWidget *_extraPathWidget;
@@ -141,7 +162,7 @@ EditGameDialog::EditGameDialog(const String &domain, GameSettings target)
// GUI: Label & edit widget for the game ID
new StaticTextWidget(tab, x, yoffset + 2, labelWidth, kLineHeight, "ID: ", kTextAlignRight);
- _domainWidget = new EditTextWidget(tab, x + labelWidth, yoffset, _w - labelWidth - 10, kLineHeight, _domain);
+ _domainWidget = new DomainEditTextWidget(tab, x + labelWidth, yoffset, _w - labelWidth - 10, kLineHeight, _domain);
yoffset += 16;
// GUI: Label & edit widget for the description