Skip to content

Commit be4670f

Browse files
BlockMenPilzAdam
authored andcommittedAug 4, 2013
Fix formspec label issues in win builds (MSVC)
1 parent 2af5864 commit be4670f

File tree

2 files changed

+39
-61
lines changed

2 files changed

+39
-61
lines changed
 

‎src/gettext.h

+7
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,12 @@ inline void changeCtype(const char *l)
8383
else
8484
infostream<<"locale has been set to:"<<ret<<std::endl;*/
8585
}
86+
87+
inline std::wstring wstrgettext(std::string text) {
88+
wchar_t* wlabel = wgettext(text.c_str());
89+
std::wstring out = (std::wstring)wlabel;
90+
delete[] wlabel;
91+
return out;
92+
}
8693
#define GETTEXT_HEADER
8794
#endif

‎src/guiFormSpecMenu.cpp

+32-61
Original file line numberDiff line numberDiff line change
@@ -411,29 +411,24 @@ void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element) {
411411
if (selected == "true")
412412
fselected = true;
413413

414-
wchar_t* wlabel = 0;
414+
std::wstring wlabel = narrow_to_wide(label.c_str());
415415

416416
if (m_use_gettext)
417-
wlabel = wgettext(label.c_str());
418-
else
419-
wlabel = (wchar_t*) narrow_to_wide(label.c_str()).c_str();
417+
wlabel = wstrgettext(label);
420418

421419
FieldSpec spec = FieldSpec(
422420
narrow_to_wide(name.c_str()),
423-
narrow_to_wide(""),
421+
L"",
424422
wlabel,
425423
258+m_fields.size()
426424
);
427425

428426
spec.ftype = f_CheckBox;
429-
427+
spec.flabel = wlabel; //Needed for displaying text on MSVC
430428
gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
431-
spec.fid, wlabel);
432-
429+
spec.fid, spec.flabel.c_str());
433430
m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e));
434431
m_fields.push_back(spec);
435-
if (m_use_gettext)
436-
delete[] wlabel;
437432
return;
438433
}
439434
errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
@@ -547,17 +542,15 @@ void GUIFormSpecMenu::parseButton(parserData* data,std::string element,std::stri
547542

548543
label = unescape_string(label);
549544

550-
wchar_t* wlabel = 0;
545+
std::wstring wlabel = narrow_to_wide(label.c_str());
551546

552547
if (m_use_gettext)
553-
wlabel = wgettext(label.c_str());
554-
else
555-
wlabel = (wchar_t*) narrow_to_wide(label.c_str()).c_str();
548+
wlabel = wstrgettext(label);
556549

557550
FieldSpec spec = FieldSpec(
558551
narrow_to_wide(name.c_str()),
559552
wlabel,
560-
narrow_to_wide(""),
553+
L"",
561554
258+m_fields.size()
562555
);
563556
spec.ftype = f_Button;
@@ -566,8 +559,6 @@ void GUIFormSpecMenu::parseButton(parserData* data,std::string element,std::stri
566559

567560
Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
568561
m_fields.push_back(spec);
569-
if (m_use_gettext)
570-
delete[] wlabel;
571562
return;
572563
}
573564
errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
@@ -639,8 +630,8 @@ void GUIFormSpecMenu::parseTextList(parserData* data,std::string element) {
639630

640631
FieldSpec spec = FieldSpec(
641632
fname_w,
642-
narrow_to_wide(""),
643-
narrow_to_wide(""),
633+
L"",
634+
L"",
644635
258+m_fields.size()
645636
);
646637

@@ -720,8 +711,8 @@ void GUIFormSpecMenu::parseDropDown(parserData* data,std::string element) {
720711

721712
FieldSpec spec = FieldSpec(
722713
fname_w,
723-
narrow_to_wide(""),
724-
narrow_to_wide(""),
714+
L"",
715+
L"",
725716
258+m_fields.size()
726717
);
727718

@@ -782,21 +773,19 @@ void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element) {
782773

783774
label = unescape_string(label);
784775

785-
wchar_t* wlabel = 0;
776+
std::wstring wlabel = narrow_to_wide(label.c_str());
786777

787778
if (m_use_gettext) {
788779
if (label.length() > 1)
789-
wlabel = wgettext(label.c_str());
780+
wlabel = wstrgettext(label);
790781
else
791-
wlabel = (wchar_t*) narrow_to_wide("").c_str();
782+
wlabel = L"";
792783
}
793-
else
794-
wlabel = (wchar_t*) narrow_to_wide(label.c_str()).c_str();
795784

796785
FieldSpec spec = FieldSpec(
797786
narrow_to_wide(name.c_str()),
798787
wlabel,
799-
narrow_to_wide(""),
788+
L"",
800789
258+m_fields.size()
801790
);
802791

@@ -819,8 +808,6 @@ void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element) {
819808
evt.KeyInput.PressedDown = true;
820809
e->OnEvent(evt);
821810
m_fields.push_back(spec);
822-
if ((m_use_gettext) && (label.length() >1))
823-
delete[] wlabel;
824811
return;
825812
}
826813
errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
@@ -862,16 +849,14 @@ void GUIFormSpecMenu::parseSimpleField(parserData* data,std::vector<std::string>
862849
default_val = unescape_string(default_val);
863850
label = unescape_string(label);
864851

865-
wchar_t* wlabel = 0;
852+
std::wstring wlabel = narrow_to_wide(label.c_str());
866853

867854
if (m_use_gettext) {
868855
if (label.length() > 1)
869-
wlabel = wgettext(label.c_str());
856+
wlabel = wstrgettext(label);
870857
else
871-
wlabel = (wchar_t*) narrow_to_wide("").c_str();
858+
wlabel = L"";
872859
}
873-
else
874-
wlabel = (wchar_t*) narrow_to_wide(label.c_str()).c_str();
875860

876861
FieldSpec spec = FieldSpec(
877862
narrow_to_wide(name.c_str()),
@@ -904,8 +889,6 @@ void GUIFormSpecMenu::parseSimpleField(parserData* data,std::vector<std::string>
904889
Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
905890
}
906891
}
907-
if (m_use_gettext && (label.length() > 1))
908-
delete[] wlabel;
909892

910893
m_fields.push_back(spec);
911894
}
@@ -953,16 +936,14 @@ void GUIFormSpecMenu::parseTextArea(parserData* data,std::vector<std::string>& p
953936
default_val = unescape_string(default_val);
954937
label = unescape_string(label);
955938

956-
wchar_t* wlabel = 0;
939+
std::wstring wlabel = narrow_to_wide(label.c_str());
957940

958941
if (m_use_gettext) {
959942
if (label.length() > 1)
960-
wlabel = wgettext(label.c_str());
943+
wlabel = wstrgettext(label);
961944
else
962-
wlabel = (wchar_t*) narrow_to_wide("").c_str();
945+
wlabel = L"";
963946
}
964-
else
965-
wlabel = (wchar_t*) narrow_to_wide(label.c_str()).c_str();
966947

967948
FieldSpec spec = FieldSpec(
968949
narrow_to_wide(name.c_str()),
@@ -1004,8 +985,6 @@ void GUIFormSpecMenu::parseTextArea(parserData* data,std::vector<std::string>& p
1004985
Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0);
1005986
}
1006987
}
1007-
if (m_use_gettext && (label.length() > 1))
1008-
delete[] wlabel;
1009988
m_fields.push_back(spec);
1010989
}
1011990

@@ -1044,23 +1023,19 @@ void GUIFormSpecMenu::parseLabel(parserData* data,std::string element) {
10441023

10451024
text = unescape_string(text);
10461025

1047-
wchar_t* wlabel = 0;
1026+
std::wstring wlabel = narrow_to_wide(text.c_str());
10481027

10491028
if (m_use_gettext)
1050-
wlabel = wgettext(text.c_str());
1051-
else
1052-
wlabel = (wchar_t*) narrow_to_wide(text.c_str()).c_str();
1029+
wlabel = wstrgettext(text);
10531030

10541031
FieldSpec spec = FieldSpec(
1055-
narrow_to_wide(""),
1032+
L"",
10561033
wlabel,
1057-
narrow_to_wide(""),
1034+
L"",
10581035
258+m_fields.size()
10591036
);
10601037
Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid);
10611038
m_fields.push_back(spec);
1062-
if (m_use_gettext)
1063-
delete[] wlabel;
10641039
return;
10651040
}
10661041
errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'" << std::endl;
@@ -1099,9 +1074,9 @@ void GUIFormSpecMenu::parseVertLabel(parserData* data,std::string element) {
10991074
}
11001075

11011076
FieldSpec spec = FieldSpec(
1102-
narrow_to_wide(""),
1077+
L"",
11031078
narrow_to_wide(label.c_str()),
1104-
narrow_to_wide(""),
1079+
L"",
11051080
258+m_fields.size()
11061081
);
11071082
gui::IGUIStaticText *t =
@@ -1157,12 +1132,10 @@ void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,std:
11571132

11581133
label = unescape_string(label);
11591134

1160-
wchar_t* wlabel = 0;
1135+
std::wstring wlabel = narrow_to_wide(label.c_str());
11611136

11621137
if (m_use_gettext)
1163-
wlabel = wgettext(label.c_str());
1164-
else
1165-
wlabel = (wchar_t*) narrow_to_wide(label.c_str()).c_str();
1138+
wlabel = wstrgettext(label);
11661139

11671140
FieldSpec spec = FieldSpec(
11681141
narrow_to_wide(name.c_str()),
@@ -1205,8 +1178,6 @@ void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,std:
12051178
e->setDrawBorder(drawborder);
12061179

12071180
m_fields.push_back(spec);
1208-
if (m_use_gettext)
1209-
delete[] wlabel;
12101181
return;
12111182
}
12121183

@@ -1236,8 +1207,8 @@ void GUIFormSpecMenu::parseTabHeader(parserData* data,std::string element) {
12361207

12371208
FieldSpec spec = FieldSpec(
12381209
narrow_to_wide(name.c_str()),
1239-
narrow_to_wide(""),
1240-
narrow_to_wide(""),
1210+
L"",
1211+
L"",
12411212
258+m_fields.size()
12421213
);
12431214

0 commit comments

Comments
 (0)
Please sign in to comment.