From 4ff8d8742c1822d9f73ee73457eb6adfa8b25c30 Mon Sep 17 00:00:00 2001 From: Ykkrosh Date: Sun, 30 May 2010 13:11:32 +0000 Subject: [PATCH] Partially support Unicode strings in GUI XML files This was SVN commit r7594. --- source/gui/CGUI.cpp | 2 +- source/gui/GUITooltip.cpp | 2 +- source/gui/GUItext.cpp | 4 +- source/gui/GUIutil.cpp | 64 +++++++++---------- source/gui/GUIutil.h | 6 +- source/gui/IGUIObject.cpp | 2 +- source/gui/IGUIObject.h | 2 +- .../gui/scripting/JSInterface_IGUIObject.cpp | 10 ++- 8 files changed, 48 insertions(+), 44 deletions(-) diff --git a/source/gui/CGUI.cpp b/source/gui/CGUI.cpp index 8e9a766946..6288abf678 100644 --- a/source/gui/CGUI.cpp +++ b/source/gui/CGUI.cpp @@ -1237,7 +1237,7 @@ void CGUI::Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObjec ManuallySetZ = true; // Try setting the value - if (object->SetSetting(pFile->GetAttributeString(attr.Name), CStr(attr.Value), true) != PSRETURN_OK) + if (object->SetSetting(pFile->GetAttributeString(attr.Name), CStrW(attr.Value), true) != PSRETURN_OK) { LOGERROR(L"GUI: (object: %hs) Can't set \"%hs\" to \"%ls\"", object->GetPresentableName().c_str(), pFile->GetAttributeString(attr.Name).c_str(), CStrW(attr.Value).c_str()); diff --git a/source/gui/GUITooltip.cpp b/source/gui/GUITooltip.cpp index f4a428ee1d..edd70ef156 100644 --- a/source/gui/GUITooltip.cpp +++ b/source/gui/GUITooltip.cpp @@ -193,7 +193,7 @@ void GUITooltip::HideTooltip(const CStr& style, CGUI* gui) } // Clear the caption - usedobj->SetSetting("caption", ""); + usedobj->SetSetting("caption", L""); usedobj->HandleMessage(SGUIMessage(GUIM_SETTINGS_UPDATED, "caption")); diff --git a/source/gui/GUItext.cpp b/source/gui/GUItext.cpp index 56a5d8d2e4..35e678882b 100644 --- a/source/gui/GUItext.cpp +++ b/source/gui/GUItext.cpp @@ -161,7 +161,7 @@ void CGUIString::GenerateTextCall(SFeedback &Feedback, { CSize displacement; // Parse the value - if (!GUI::ParseString(itTextChunk->m_Tags[0].m_TagAdditionalValue, displacement)) + if (!GUI::ParseString(CStrW(itTextChunk->m_Tags[0].m_TagAdditionalValue), displacement)) LOG(CLogger::Error, LOG_CATEGORY, L"Error parsing 'displace' value for tag [ICON]"); else SpriteCall.m_Area += displacement; @@ -207,7 +207,7 @@ void CGUIString::GenerateTextCall(SFeedback &Feedback, TextCall.m_UseCustomColor = true; // Try parsing the color std::string - if (!GUI::ParseString(it2->m_TagValue, TextCall.m_Color)) + if (!GUI::ParseString(CStrW(it2->m_TagValue), TextCall.m_Color)) { if (pObject) LOG(CLogger::Error, LOG_CATEGORY, L"Error parsing the value of a [color]-tag in GUI text when reading object \"%hs\".", pObject->GetPresentableName().c_str()); diff --git a/source/gui/GUIutil.cpp b/source/gui/GUIutil.cpp index b07ea90078..b0a4d11681 100644 --- a/source/gui/GUIutil.cpp +++ b/source/gui/GUIutil.cpp @@ -32,12 +32,12 @@ extern int g_yres; template <> -bool __ParseString(const CStr& Value, bool &Output) +bool __ParseString(const CStrW& Value, bool &Output) { - if (Value == "true") + if (Value == L"true") Output = true; else - if (Value == "false") + if (Value == L"false") Output = false; else return false; @@ -46,26 +46,26 @@ bool __ParseString(const CStr& Value, bool &Output) } template <> -bool __ParseString(const CStr& Value, int &Output) +bool __ParseString(const CStrW& Value, int &Output) { Output = Value.ToInt(); return true; } template <> -bool __ParseString(const CStr& Value, float &Output) +bool __ParseString(const CStrW& Value, float &Output) { Output = Value.ToFloat(); return true; } template <> -bool __ParseString(const CStr& Value, CRect &Output) +bool __ParseString(const CStrW& Value, CRect &Output) { // Use the parser to parse the values CParser& parser (CParserCache::Get("_$value_$value_$value_$value_")); - std::string str = Value; + std::string str = CStr(Value); CParserLine line; line.ParseString(parser, str); @@ -90,13 +90,13 @@ bool __ParseString(const CStr& Value, CRect &Output) } template <> -bool __ParseString(const CStr& Value, CClientArea &Output) +bool __ParseString(const CStrW& Value, CClientArea &Output) { return Output.SetClientArea(Value); } template <> -bool GUI::ParseColor(const CStr& Value, CColor &Output, float DefaultAlpha) +bool GUI::ParseColor(const CStrW& Value, CColor &Output, float DefaultAlpha) { // First, check our database in g_GUI for pre-defined colors // If we find anything, we'll ignore DefaultAlpha @@ -109,7 +109,7 @@ bool GUI::ParseColor(const CStr& Value, CColor &Output, float DefaultAlpha) template <> -bool __ParseString(const CStr& Value, CColor &Output) +bool __ParseString(const CStrW& Value, CColor &Output) { // First, check our database in g_GUI for pre-defined colors // If it fails, it won't do anything with Output @@ -120,12 +120,12 @@ bool __ParseString(const CStr& Value, CColor &Output) } template <> -bool __ParseString(const CStr& Value, CSize &Output) +bool __ParseString(const CStrW& Value, CSize &Output) { // Use the parser to parse the values CParser& parser (CParserCache::Get("_$value_$value_")); - std::string str = Value; + std::string str = CStr(Value); CParserLine line; line.ParseString(parser, str); @@ -158,12 +158,12 @@ bool __ParseString(const CStr& Value, CSize &Output) } template <> -bool __ParseString(const CStr& Value, CPos &Output) +bool __ParseString(const CStrW& Value, CPos &Output) { CParser& parser (CParserCache::Get("_[-$arg(_minus)]$value_[-$arg(_minus)]$value_")); CParserLine line; - line.ParseString(parser, Value); + line.ParseString(parser, CStr(Value)); if (!line.m_ParseOK) return false; @@ -180,15 +180,15 @@ bool __ParseString(const CStr& Value, CPos &Output) } template <> -bool __ParseString(const CStr& Value, EAlign &Output) +bool __ParseString(const CStrW& Value, EAlign &Output) { - if (Value == "left") + if (Value == L"left") Output = EAlign_Left; else - if (Value == "center") + if (Value == L"center") Output = EAlign_Center; else - if (Value == "right") + if (Value == L"right") Output = EAlign_Right; else return false; @@ -197,15 +197,15 @@ bool __ParseString(const CStr& Value, EAlign &Output) } template <> -bool __ParseString(const CStr& Value, EVAlign &Output) +bool __ParseString(const CStrW& Value, EVAlign &Output) { - if (Value == "top") + if (Value == L"top") Output = EVAlign_Top; else - if (Value == "center") + if (Value == L"center") Output = EVAlign_Center; else - if (Value == "bottom") + if (Value == L"bottom") Output = EVAlign_Bottom; else return false; @@ -214,17 +214,16 @@ bool __ParseString(const CStr& Value, EVAlign &Output) } template <> -bool __ParseString(const CStr& Value, CGUIString &Output) +bool __ParseString(const CStrW& Value, CGUIString &Output) { - // Translate the Value and retrieve the localised string in - // Unicode. + // TODO: i18n: Might want to translate the Value perhaps - Output.SetValue(I18n::translate((CStrW)Value)); + Output.SetValue(Value); return true; } template <> -bool __ParseString(const CStr& Value, CStr&Output) +bool __ParseString(const CStrW& Value, CStr& Output) { // Do very little. Output = Value; @@ -232,24 +231,23 @@ bool __ParseString(const CStr& Value, CStr&Output) } template <> -bool __ParseString(const CStr& Value, CStrW& Output) +bool __ParseString(const CStrW& Value, CStrW& Output) { - // Translate the Value and retrieve the localised string in - // Unicode. + // TODO: i18n: Might want to translate the Value perhaps - Output = I18n::translate((CStrW)Value); + Output = Value; return true; } template <> -bool __ParseString(const CStr& Value, CGUISpriteInstance &Output) +bool __ParseString(const CStrW& Value, CGUISpriteInstance &Output) { Output = Value; return true; } template <> -bool __ParseString(const CStr& UNUSED(Value), CGUIList& UNUSED(Output)) +bool __ParseString(const CStrW& UNUSED(Value), CGUIList& UNUSED(Output)) { return false; } diff --git a/source/gui/GUIutil.h b/source/gui/GUIutil.h index 6cbe0e24c7..ac1b2ca5c5 100644 --- a/source/gui/GUIutil.h +++ b/source/gui/GUIutil.h @@ -52,7 +52,7 @@ class CClientArea; class CGUIString; template -bool __ParseString(const CStr& Value, T &tOutput); +bool __ParseString(const CStrW& Value, T &tOutput); // Load Identity matrix and // adapt (origio) to being in top left corner and down @@ -231,12 +231,12 @@ public: * * @see __ParseString() */ - static bool ParseString(const CStr& Value, T &tOutput) + static bool ParseString(const CStrW& Value, T &tOutput) { return __ParseString(Value, tOutput); } - static bool ParseColor(const CStr& Value, CColor &tOutput, float DefaultAlpha); + static bool ParseColor(const CStrW& Value, CColor &tOutput, float DefaultAlpha); private: diff --git a/source/gui/IGUIObject.cpp b/source/gui/IGUIObject.cpp index 4c3445830e..be77437752 100644 --- a/source/gui/IGUIObject.cpp +++ b/source/gui/IGUIObject.cpp @@ -254,7 +254,7 @@ bool IGUIObject::SettingExists(const CStr& Setting) const GUI::SetSetting(this, Setting, _Value, SkipMessage); \ } -PSRETURN IGUIObject::SetSetting(const CStr& Setting, const CStr& Value, const bool& SkipMessage) +PSRETURN IGUIObject::SetSetting(const CStr& Setting, const CStrW& Value, const bool& SkipMessage) { if (!SettingExists(Setting)) { diff --git a/source/gui/IGUIObject.h b/source/gui/IGUIObject.h index 90b4080ecc..24dac2442e 100644 --- a/source/gui/IGUIObject.h +++ b/source/gui/IGUIObject.h @@ -272,7 +272,7 @@ public: * * @return PSERROR (PSRETURN_OK if successful) */ - PSRETURN SetSetting(const CStr& Setting, const CStr& Value, const bool& SkipMessage=false); + PSRETURN SetSetting(const CStr& Setting, const CStrW& Value, const bool& SkipMessage=false); /** * Retrieves the type of a named setting. diff --git a/source/gui/scripting/JSInterface_IGUIObject.cpp b/source/gui/scripting/JSInterface_IGUIObject.cpp index 64857fd2ca..5e2e4c9770 100644 --- a/source/gui/scripting/JSInterface_IGUIObject.cpp +++ b/source/gui/scripting/JSInterface_IGUIObject.cpp @@ -427,7 +427,10 @@ JSBool JSI_IGUIObject::setProperty(JSContext* cx, JSObject* obj, jsval id, jsval { if (JSVAL_IS_STRING(*vp)) { - if (e->SetSetting(propName, JS_GetStringBytes(JS_ValueToString(cx, *vp))) != PSRETURN_OK) + std::wstring value; + StringConvert::jsstring_to_wstring(JS_ValueToString(cx, *vp), value); + + if (e->SetSetting(propName, value) != PSRETURN_OK) { JS_ReportError(cx, "Invalid value for setting '%s'", propName.c_str()); return JS_FALSE; @@ -464,7 +467,10 @@ JSBool JSI_IGUIObject::setProperty(JSContext* cx, JSObject* obj, jsval id, jsval { if (JSVAL_IS_STRING(*vp)) { - if (e->SetSetting(propName, JS_GetStringBytes(JS_ValueToString(cx, *vp))) != PSRETURN_OK) + std::wstring value; + StringConvert::jsstring_to_wstring(JS_ValueToString(cx, *vp), value); + + if (e->SetSetting(propName, value) != PSRETURN_OK) { JS_ReportError(cx, "Invalid value for setting '%s'", propName.c_str()); return JS_FALSE;