Skip to content

Commit 473d81f

Browse files
SmallJokerrubenwardy
authored andcommitted
Formspecs: Unify textarea and field parsing functions, fix wrong fallback text
* textarea[], field[]: Unify function, fix wrong fallback text * Remove apparently superflous mainmenumanager.h incldue * intlGUIEditBox.cpp: make read-only boxes really read-only * Use elseif (trivial)
1 parent 12d1e4f commit 473d81f

File tree

3 files changed

+75
-103
lines changed

3 files changed

+75
-103
lines changed

src/gui/guiFormSpecMenu.cpp

+67-95
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
5555
#include "irrlicht_changes/static_text.h"
5656
#include "guiscalingfilter.h"
5757
#include "guiEditBoxWithScrollbar.h"
58-
59-
#if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
6058
#include "intlGUIEditBox.h"
61-
#include "mainmenumanager.h"
62-
63-
#endif
6459

6560
#define MY_CHECKPOS(a,b) \
6661
if (v_pos.size() != 2) { \
@@ -1007,6 +1002,71 @@ void GUIFormSpecMenu::parsePwdField(parserData* data, const std::string &element
10071002
errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
10081003
}
10091004

1005+
void GUIFormSpecMenu::createTextField(parserData *data, FieldSpec &spec,
1006+
core::rect<s32> &rect, bool is_multiline)
1007+
{
1008+
bool is_editable = !spec.fname.empty();
1009+
if (!is_editable && !is_multiline) {
1010+
// spec field id to 0, this stops submit searching for a value that isn't there
1011+
gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
1012+
this, spec.fid);
1013+
return;
1014+
}
1015+
1016+
if (is_editable) {
1017+
spec.send = true;
1018+
} else if (is_multiline &&
1019+
spec.fdefault.empty() && !spec.flabel.empty()) {
1020+
// Multiline textareas: swap default and label for backwards compat
1021+
spec.flabel.swap(spec.fdefault);
1022+
}
1023+
1024+
gui::IGUIEditBox *e = nullptr;
1025+
static constexpr bool use_intl_edit_box = USE_FREETYPE &&
1026+
IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9;
1027+
1028+
if (use_intl_edit_box && g_settings->getBool("freetype")) {
1029+
e = new gui::intlGUIEditBox(spec.fdefault.c_str(),
1030+
true, Environment, this, spec.fid, rect, is_editable, is_multiline);
1031+
e->drop();
1032+
} else {
1033+
if (is_multiline)
1034+
e = new GUIEditBoxWithScrollBar(spec.fdefault.c_str(), true,
1035+
Environment, this, spec.fid, rect, is_editable, true);
1036+
else if (is_editable)
1037+
e = Environment->addEditBox(spec.fdefault.c_str(), rect, true,
1038+
this, spec.fid);
1039+
}
1040+
1041+
if (e) {
1042+
if (is_editable && spec.fname == data->focused_fieldname)
1043+
Environment->setFocus(e);
1044+
1045+
if (is_multiline) {
1046+
e->setMultiLine(true);
1047+
e->setWordWrap(true);
1048+
e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1049+
} else {
1050+
irr::SEvent evt;
1051+
evt.EventType = EET_KEY_INPUT_EVENT;
1052+
evt.KeyInput.Key = KEY_END;
1053+
evt.KeyInput.Char = 0;
1054+
evt.KeyInput.Control = 0;
1055+
evt.KeyInput.Shift = 0;
1056+
evt.KeyInput.PressedDown = true;
1057+
e->OnEvent(evt);
1058+
}
1059+
}
1060+
1061+
if (!spec.flabel.empty()) {
1062+
int font_height = g_fontengine->getTextHeight();
1063+
rect.UpperLeftCorner.Y -= font_height;
1064+
rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1065+
gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
1066+
this, 0);
1067+
}
1068+
}
1069+
10101070
void GUIFormSpecMenu::parseSimpleField(parserData* data,
10111071
std::vector<std::string> &parts)
10121072
{
@@ -1040,46 +1100,7 @@ void GUIFormSpecMenu::parseSimpleField(parserData* data,
10401100
258+m_fields.size()
10411101
);
10421102

1043-
if (name.empty()) {
1044-
// spec field id to 0, this stops submit searching for a value that isn't there
1045-
gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true, this,
1046-
spec.fid);
1047-
} else {
1048-
spec.send = true;
1049-
gui::IGUIElement *e;
1050-
#if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
1051-
if (g_settings->getBool("freetype")) {
1052-
e = (gui::IGUIElement *) new gui::intlGUIEditBox(spec.fdefault.c_str(),
1053-
true, Environment, this, spec.fid, rect);
1054-
e->drop();
1055-
} else {
1056-
#else
1057-
{
1058-
#endif
1059-
e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
1060-
}
1061-
if (spec.fname == data->focused_fieldname) {
1062-
Environment->setFocus(e);
1063-
}
1064-
1065-
irr::SEvent evt;
1066-
evt.EventType = EET_KEY_INPUT_EVENT;
1067-
evt.KeyInput.Key = KEY_END;
1068-
evt.KeyInput.Char = 0;
1069-
evt.KeyInput.Control = 0;
1070-
evt.KeyInput.Shift = 0;
1071-
evt.KeyInput.PressedDown = true;
1072-
e->OnEvent(evt);
1073-
1074-
if (label.length() >= 1)
1075-
{
1076-
int font_height = g_fontengine->getTextHeight();
1077-
rect.UpperLeftCorner.Y -= font_height;
1078-
rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1079-
gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
1080-
this, 0);
1081-
}
1082-
}
1103+
createTextField(data, spec, rect, false);
10831104

10841105
if (parts.size() >= 4) {
10851106
// TODO: remove after 2016-11-03
@@ -1142,56 +1163,7 @@ void GUIFormSpecMenu::parseTextArea(parserData* data, std::vector<std::string>&
11421163
258+m_fields.size()
11431164
);
11441165

1145-
bool is_editable = !name.empty();
1146-
1147-
if (is_editable)
1148-
spec.send = true;
1149-
1150-
gui::IGUIEditBox *e = nullptr;
1151-
const wchar_t *text = spec.fdefault.empty() ?
1152-
wlabel.c_str() : spec.fdefault.c_str();
1153-
1154-
#if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
1155-
if (g_settings->getBool("freetype")) {
1156-
e = (gui::IGUIEditBox *) new gui::intlGUIEditBox(text,
1157-
true, Environment, this, spec.fid, rect, is_editable, true);
1158-
e->drop();
1159-
} else {
1160-
#else
1161-
{
1162-
#endif
1163-
e = new GUIEditBoxWithScrollBar(text, true,
1164-
Environment, this, spec.fid, rect, is_editable, true);
1165-
}
1166-
1167-
if (is_editable && spec.fname == data->focused_fieldname)
1168-
Environment->setFocus(e);
1169-
1170-
if (e) {
1171-
if (type == "textarea")
1172-
{
1173-
e->setMultiLine(true);
1174-
e->setWordWrap(true);
1175-
e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
1176-
} else {
1177-
irr::SEvent evt;
1178-
evt.EventType = EET_KEY_INPUT_EVENT;
1179-
evt.KeyInput.Key = KEY_END;
1180-
evt.KeyInput.Char = 0;
1181-
evt.KeyInput.Control = 0;
1182-
evt.KeyInput.Shift = 0;
1183-
evt.KeyInput.PressedDown = true;
1184-
e->OnEvent(evt);
1185-
}
1186-
}
1187-
1188-
if (is_editable && !label.empty()) {
1189-
int font_height = g_fontengine->getTextHeight();
1190-
rect.UpperLeftCorner.Y -= font_height;
1191-
rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1192-
gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
1193-
this, 0);
1194-
}
1166+
createTextField(data, spec, rect, type == "textarea");
11951167

11961168
if (parts.size() >= 6) {
11971169
// TODO: remove after 2016-11-03

src/gui/guiFormSpecMenu.h

+2
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,8 @@ class GUIFormSpecMenu : public GUIModalMenu
484484
void parseFieldCloseOnEnter(parserData *data, const std::string &element);
485485
void parsePwdField(parserData* data, const std::string &element);
486486
void parseField(parserData* data, const std::string &element, const std::string &type);
487+
void createTextField(parserData *data, FieldSpec &spec,
488+
core::rect<s32> &rect, bool is_multiline);
487489
void parseSimpleField(parserData* data,std::vector<std::string> &parts);
488490
void parseTextArea(parserData* data,std::vector<std::string>& parts,
489491
const std::string &type);

src/gui/intlGUIEditBox.cpp

+6-8
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,7 @@ bool intlGUIEditBox::processKey(const SEvent& event)
354354
break;
355355
case KEY_KEY_X:
356356
// cut to the clipboard
357-
if (!PasswordBox && Operator && MarkBegin != MarkEnd)
358-
{
357+
if (!PasswordBox && Operator && MarkBegin != MarkEnd) {
359358
const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
360359
const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
361360

@@ -364,8 +363,7 @@ bool intlGUIEditBox::processKey(const SEvent& event)
364363
sc = Text.subString(realmbgn, realmend - realmbgn).c_str();
365364
Operator->copyToClipboard(sc.c_str());
366365

367-
if (IsEnabled)
368-
{
366+
if (IsEnabled && m_writable) {
369367
// delete
370368
core::stringw s;
371369
s = Text.subString(0, realmbgn);
@@ -380,7 +378,7 @@ bool intlGUIEditBox::processKey(const SEvent& event)
380378
}
381379
break;
382380
case KEY_KEY_V:
383-
if ( !IsEnabled )
381+
if (!IsEnabled || !m_writable)
384382
break;
385383

386384
// paste from the clipboard
@@ -636,7 +634,7 @@ bool intlGUIEditBox::processKey(const SEvent& event)
636634
break;
637635

638636
case KEY_BACK:
639-
if ( !this->IsEnabled )
637+
if (!this->IsEnabled || !m_writable)
640638
break;
641639

642640
if (!Text.empty()) {
@@ -675,7 +673,7 @@ bool intlGUIEditBox::processKey(const SEvent& event)
675673
}
676674
break;
677675
case KEY_DELETE:
678-
if ( !this->IsEnabled )
676+
if (!this->IsEnabled || !m_writable)
679677
break;
680678

681679
if (!Text.empty()) {
@@ -1351,7 +1349,7 @@ s32 intlGUIEditBox::getLineFromPos(s32 pos)
13511349

13521350
void intlGUIEditBox::inputChar(wchar_t c)
13531351
{
1354-
if (!IsEnabled)
1352+
if (!IsEnabled || !m_writable)
13551353
return;
13561354

13571355
if (c != 0)

0 commit comments

Comments
 (0)