GUI: U32: Improve U32 code

- Revert accidentally put translations in mt32.cpp
- Use U32::format in some places earlier missed
- Add %u and %i for u32::format
- Add support for GUIErrorMsgFormat to use u32::format internally
- Use the above whereever needed
- Improve linux tts by removing redundant code
- Some places I had changed nullptr -> "". Revert this
This commit is contained in:
aryanrawlani28 2020-06-30 03:40:13 +05:30 committed by Eugene Sandulenko
parent 22ef76638c
commit 185fb72783
30 changed files with 91 additions and 77 deletions

View File

@ -67,12 +67,12 @@ public:
// Callbacks for reporting various errors and information
void onErrorControlROM() {
GUI::MessageDialog dialog(_("MT32Emu: Init Error - Missing or invalid Control ROM image"), _("OK"));
GUI::MessageDialog dialog("MT32Emu: Init Error - Missing or invalid Control ROM image");
dialog.runModal();
error("MT32emu: Init Error - Missing or invalid Control ROM image");
}
void onErrorPCMROM() {
GUI::MessageDialog dialog(_("MT32Emu: Init Error - Missing PCM ROM image"), _("OK"));
GUI::MessageDialog dialog("MT32Emu: Init Error - Missing PCM ROM image");
dialog.runModal();
error("MT32emu: Init Error - Missing PCM ROM image");
}

View File

@ -345,8 +345,8 @@ void Storage::directoryDownloadedCallback(FileArrayResponse response) {
Common::U32String message;
if (response.value.size()) {
message = Common::String::format(
_("Download complete.\nFailed to download %u files.").encode().c_str(),
message = Common::U32String::format(
_("Download complete.\nFailed to download %u files."),
response.value.size());
} else {
message = _("Download complete.");

View File

@ -2500,14 +2500,12 @@ bool SurfaceSdlGraphicsManager::notifyEvent(const Common::Event &event) {
#ifdef USE_OSD
Common::U32String message;
if (_videoMode.aspectRatioCorrection)
message = Common::String::format("%s\n%d x %d -> %d x %d",
_("Enabled aspect ratio correction").encode().c_str(),
message = Common::U32String::format(_("Enabled aspect ratio correction") + Common::U32String("\n%d x %d -> %d x %d"),
_videoMode.screenWidth, _videoMode.screenHeight,
_hwScreen->w, _hwScreen->h
);
else
message = Common::String::format("%s\n%d x %d -> %d x %d",
_("Disabled aspect ratio correction").encode().c_str(),
message = Common::U32String::format(_("Disabled aspect ratio correction") + Common::U32String("%s\n%d x %d -> %d x %d"),
_videoMode.screenWidth, _videoMode.screenHeight,
_hwScreen->w, _hwScreen->h
);

View File

@ -194,24 +194,7 @@ bool SpeechDispatcherManager::say(const Common::U32String &str, Action action, C
return true;
}
if (charset.empty()) {
#ifdef USE_TRANSLATION
charset = TransMan.getCurrentCharset();
#else
charset = "ASCII";
#endif
}
Common::String strToSpeak = str.encode();
char *tmpStr = Common::Encoding::convert("UTF-8", charset, strToSpeak.c_str(), strToSpeak.size());
if (tmpStr == nullptr) {
warning("Cannot convert from %s encoding for text to speech", charset.c_str());
pthread_mutex_unlock(&_speechMutex);
return true;
}
Common::String strUtf8 = tmpStr;
free(tmpStr);
Common::String strUtf8 = str.encode();;
if (!_speechQueue.empty() && action == INTERRUPT_NO_REPEAT &&
_speechQueue.front() == strUtf8 && isSpeaking()) {

View File

@ -89,7 +89,7 @@ bool AchievementsManager::setAchievement(const String &id, const String &display
if (!displayedMessage.empty() && g_system) {
U32String msg;
msg = Common::String::format("%s\n%s", _("Achievement unlocked!").encode().c_str(), displayedMessage.c_str());
msg = _("Achievement unlocked!") + Common::U32String("\n") + Common::U32String(displayedMessage);
g_system->displayMessageOnOSD(msg);
}

View File

@ -110,21 +110,21 @@ public:
/**
* Returns the translation into the current language of the parameter
* message. In case the message isn't found in the translation catalog,
* it returns the original untranslated message.
* it returns the original untranslated message, as a U32String.
*/
U32String getTranslation(const char *message) const;
/**
* Returns the translation into the current language of the parameter
* message. In case the message isn't found in the translation catalog,
* it returns the original untranslated message.
* it returns the original untranslated message, as a U32String.
*/
U32String getTranslation(const String &message) const;
/**
* Returns the translation into the current language of the parameter
* message. In case the message isn't found in the translation catalog,
* it returns the original untranslated message.
* it returns the original untranslated message, as a U32String.
*
* If a translation is found for the given context it will return that
* translation, otherwise it will look for a translation for the same
@ -135,7 +135,7 @@ public:
/**
* Returns the translation into the current language of the parameter
* message. In case the message isn't found in the translation catalog,
* it returns the original untranslated message.
* it returns the original untranslated message, as a U32String.
*
* If a translation is found for the given context it will return that
* translation, otherwise it will look for a translation for the same

View File

@ -571,6 +571,22 @@ int U32String::vformat(U32String::const_iterator fmt, const U32String::const_ite
output.insertString(buffer, pos);
pos += len - 1;
break;
case 'u':
int_temp = va_arg(args, uint);
itoa(int_temp, buffer, 10);
len = strlen(buffer);
length += len;
output.insertString(buffer, pos);
pos += len - 1;
case 'i':
int_temp = va_arg(args, uint16);
itoa(int_temp, buffer, 10);
len = strlen(buffer);
length += len;
output.insertString(buffer, pos);
pos += len - 1;
default:
warning("Unexpected formatting type for U32String::Format.");
break;

View File

@ -756,7 +756,7 @@ int AgiEngine::scummVMSaveLoadDialog(bool isSave) {
int slot;
if (isSave) {
dialog = new GUI::SaveLoadChooser((_("Save game:")), Common::convertToU32String(("Save")), true);
dialog = new GUI::SaveLoadChooser(_("Save game:"), _("Save"), true);
slot = dialog->runModalWithCurrentTarget();
desc = dialog->getResultString();
@ -767,7 +767,7 @@ int AgiEngine::scummVMSaveLoadDialog(bool isSave) {
}
if (desc.size() > 28)
desc = Common::U32String(desc.c_str(), 28);
desc = Common::U32String(desc.encode().c_str(), 28);
} else {
dialog = new GUI::SaveLoadChooser((_("Restore game:")), (_("Restore")), false);
slot = dialog->runModalWithCurrentTarget();

View File

@ -228,7 +228,7 @@ Common::Error CGEEngine::run() {
Common::U32String msg = Common::U32String(_text->getText(kSayTheEnd));
if (!msg.empty()) {
g_system->delayMillis(10);
GUI::MessageDialog dialog(msg, Common::U32String("OK"));
GUI::MessageDialog dialog(msg);
dialog.runModal();
}
}

View File

@ -952,11 +952,11 @@ bool DrasculaEngine::loadDrasculaDat() {
ver = in.readByte();
if (ver != DRASCULA_DAT_VER) {
Common::String errorMessage = Common::String::format(
_("Incorrect version of the '%s' engine data file found. Expected %d.%d but got %d.%d.").encode().c_str(),
Common::U32String errorMessage = Common::U32String::format(
_("Incorrect version of the '%s' engine data file found. Expected %d.%d but got %d.%d."),
filename.c_str(), DRASCULA_DAT_VER, 0, ver, 0);
GUIErrorMessage(errorMessage);
warning("%s", errorMessage.c_str());
warning("%s", errorMessage.encode().c_str());
return false;
}

View File

@ -38,6 +38,7 @@
#include "common/file.h"
#include "common/system.h"
#include "common/str.h"
#include "common/ustr.h"
#include "common/error.h"
#include "common/list.h"
#include "common/memstream.h"
@ -430,6 +431,17 @@ void GUIErrorMessageFormat(const char *fmt, ...) {
GUIErrorMessage(msg);
}
void GUIErrorMessageFormat(Common::U32String fmt, ...) {
Common::U32String msg("");
va_list va;
va_start(va, fmt);
Common::U32String::vformat(fmt.begin(), fmt.end(), msg, va);
va_end(va);
GUIErrorMessage(msg);
}
void Engine::checkCD() {
#if defined(WIN32) && !defined(__SYMBIAN32__)
// It is a known bug under Windows that games that play CD audio cause

View File

@ -55,6 +55,7 @@ class Dialog;
*/
void GUIErrorMessage(const Common::U32String &msg, const char *url = nullptr);
void GUIErrorMessageWithURL(const Common::U32String &msg, const char *url);
void GUIErrorMessageFormat(Common::U32String fmt, ...);
void GUIErrorMessageFormat(const char *fmt, ...) GCC_PRINTF(1, 2);
class Engine;

View File

@ -593,8 +593,8 @@ bool VM::getWord(Common::String &line) {
_words.push_back(iw);
return true;
} else {
Common::String msg = Common::String::format(_("I don't know the word \"%s\".\n").encode().c_str(), iw._text.c_str());
print(msg);
Common::U32String msg = Common::U32String::format(_("I don't know the word \"%s\".\n"), iw._text.c_str());
print(msg.encode());
return false;
}
}

View File

@ -61,7 +61,7 @@ Common::Error LureEngine::init() {
Common::File f;
VersionStructure version;
if (!f.open(SUPPORT_FILENAME)) {
GUIErrorMessageFormat(Common::convertFromU32String(_("Unable to locate the '%s' engine data file.")).c_str(), SUPPORT_FILENAME);
GUIErrorMessageFormat(_("Unable to locate the '%s' engine data file."), SUPPORT_FILENAME);
return Common::kUnknownError;
}
@ -70,10 +70,10 @@ Common::Error LureEngine::init() {
f.close();
if (READ_LE_UINT16(&version.id) != 0xffff) {
GUIErrorMessageFormat(Common::convertFromU32String(_("The '%s' engine data file is corrupt.")).c_str(), SUPPORT_FILENAME);
GUIErrorMessageFormat(_("The '%s' engine data file is corrupt."), SUPPORT_FILENAME);
return Common::kUnknownError;
} else if ((version.vMajor != LURE_DAT_MAJOR) || (version.vMinor != LURE_DAT_MINOR)) {
GUIErrorMessageFormat(Common::convertFromU32String(_("Incorrect version of the '%s' engine data file found. Expected %d.%d but got %d.%d.")).c_str(),
GUIErrorMessageFormat(_("Incorrect version of the '%s' engine data file found. Expected %d.%d but got %d.%d."),
SUPPORT_FILENAME, LURE_DAT_MAJOR, LURE_DAT_MINOR,
version.vMajor, version.vMinor);
return Common::kUnknownError;

View File

@ -352,7 +352,7 @@ void TextDialog::draw() {
// Draw the text lines
int lineYp = _position.y + 5;
#ifdef USE_TTS
Common::U32String text;
Common::String text;
#endif
for (int lineNum = 0; lineNum <= _numLines; ++lineNum) {
if (_lineXp[lineNum] == -1) {
@ -393,7 +393,7 @@ void TextDialog::draw() {
Common::TextToSpeechManager *ttsMan = g_system->getTextToSpeechManager();
if (ttsMan != nullptr) {
ttsMan->stop();
ttsMan->say(text);
ttsMan->say(Common::convertToU32String(text.c_str()));
}
}
#endif

View File

@ -498,8 +498,8 @@ bool MohawkEngine_Riven::checkDatafiles() {
return true;
}
Common::String message = _("You are missing the following required Riven data files:\n").encode() + missingFiles;
warning("%s", message.c_str());
Common::U32String message = _("You are missing the following required Riven data files:\n") + Common::convertToU32String(missingFiles.c_str());
warning("%s", message.encode().c_str());
GUIErrorMessage(message);
return false;

View File

@ -302,7 +302,7 @@ Common::ErrorCode MortevielleEngine::loadMortDat() {
// Open the mort.dat file
if (!f.open(MORT_DAT)) {
GUIErrorMessageFormat(_("Unable to locate the '%s' engine data file.").encode().c_str(), MORT_DAT);
GUIErrorMessageFormat(_("Unable to locate the '%s' engine data file."), MORT_DAT);
return Common::kReadingFailed;
}
@ -310,7 +310,7 @@ Common::ErrorCode MortevielleEngine::loadMortDat() {
char fileId[4];
f.read(fileId, 4);
if (strncmp(fileId, "MORT", 4) != 0) {
GUIErrorMessageFormat(_("The '%s' engine data file is corrupt.").encode().c_str(), MORT_DAT);
GUIErrorMessageFormat(_("The '%s' engine data file is corrupt."), MORT_DAT);
return Common::kReadingFailed;
}
@ -320,7 +320,7 @@ Common::ErrorCode MortevielleEngine::loadMortDat() {
if (majVer < MORT_DAT_REQUIRED_VERSION) {
GUIErrorMessageFormat(
_("Incorrect version of the '%s' engine data file found. Expected %d.%d but got %d.%d.").encode().c_str(),
_("Incorrect version of the '%s' engine data file found. Expected %d.%d but got %d.%d."),
MORT_DAT, MORT_DAT_REQUIRED_VERSION, 0, majVer, minVer);
return Common::kReadingFailed;
}

View File

@ -389,7 +389,7 @@ InfoDialog::InfoDialog(ScummEngine *scumm, int res)
_text = new GUI::StaticTextWidget(this, 0, 0, 10, 10, _message, kTextAlignCenter);
}
InfoDialog::InfoDialog(ScummEngine *scumm, const U32String& message)
InfoDialog::InfoDialog(ScummEngine *scumm, const U32String &message)
: ScummDialog(0, 0, 0, 0), _vm(scumm) { // dummy x and w
_message = message;
@ -398,7 +398,7 @@ InfoDialog::InfoDialog(ScummEngine *scumm, const U32String& message)
_text = new GUI::StaticTextWidget(this, 0, 0, 10, 10, _message, kTextAlignCenter);
}
void InfoDialog::setInfoText(const U32String& message) {
void InfoDialog::setInfoText(const U32String &message) {
_message = message;
_text->setLabel(_message);
//reflowLayout(); // FIXME: Should we call this here? Depends on the usage patterns, I guess...
@ -477,8 +477,11 @@ void PauseDialog::handleKeyDown(Common::KeyState state) {
ConfirmDialog::ConfirmDialog(ScummEngine *scumm, int res)
: InfoDialog(scumm, res), _yesKey('y'), _noKey('n') {
if (_message.encode().lastChar() != ')') {
_yesKey = _message.encode().lastChar();
if (_message.empty())
return;
if (_message[_message.size() - 1] != ')') {
_yesKey = _message[_message.size() - 1];
_message.deleteLastChar();
if (_yesKey >= 'A' && _yesKey <= 'Z')

View File

@ -74,11 +74,11 @@ protected:
public:
// arbitrary message
InfoDialog(ScummEngine *scumm, const U32String& message);
InfoDialog(ScummEngine *scumm, const U32String &message);
// from resources
InfoDialog(ScummEngine *scumm, int res);
void setInfoText(const U32String& message);
void setInfoText(const U32String &message);
void handleMouseDown(int x, int y, int button, int clickCount) override {
setResult(0);

View File

@ -1894,10 +1894,10 @@ void ScummEngine::setupMusic(int midi) {
if (missingFile) {
GUI::MessageDialog dialog(
Common::String::format(
Common::U32String::format(
_("Native MIDI support requires the Roland Upgrade from LucasArts,\n"
"but %s is missing. Using AdLib instead.").encode().c_str(), fileName.c_str()),
_("OK").encode());
"but %s is missing. Using AdLib instead."), fileName.c_str()),
_("OK"));
dialog.runModal();
_sound->_musicType = MDT_ADLIB;
}

View File

@ -184,7 +184,7 @@ Common::Error SupernovaEngine::loadGameStrings() {
if (stream == nullptr) {
Common::Language l = Common::parseLanguage(ConfMan.get("language"));
GUIErrorMessageFormat(_("Unable to locate the text for %s language in engine data file.").encode().c_str(), Common::getLanguageDescription(l));
GUIErrorMessageFormat(_("Unable to locate the text for %s language in engine data file."), Common::getLanguageDescription(l));
return Common::kReadingFailed;
}
@ -455,19 +455,19 @@ Common::SeekableReadStream *SupernovaEngine::getBlockFromDatFile(Common::String
char id[5], lang[5];
id[4] = lang[4] = '\0';
if (!f.open(SUPERNOVA_DAT)) {
GUIErrorMessageFormat(_("Unable to locate the '%s' engine data file.").encode().c_str(), SUPERNOVA_DAT);
GUIErrorMessageFormat(_("Unable to locate the '%s' engine data file."), SUPERNOVA_DAT);
return nullptr;
}
f.read(id, 3);
if (strncmp(id, "MSN", 3) != 0) {
GUIErrorMessageFormat(_("The '%s' engine data file is corrupt.").encode().c_str(), SUPERNOVA_DAT);
GUIErrorMessageFormat(_("The '%s' engine data file is corrupt."), SUPERNOVA_DAT);
return nullptr;
}
int version = f.readByte();
if (version != SUPERNOVA_DAT_VERSION) {
GUIErrorMessageFormat(
_("Incorrect version of the '%s' engine data file found. Expected %d but got %d.").encode().c_str(),
_("Incorrect version of the '%s' engine data file found. Expected %d but got %d."),
SUPERNOVA_DAT, SUPERNOVA_DAT_VERSION, version);
return nullptr;
}
@ -477,7 +477,7 @@ Common::SeekableReadStream *SupernovaEngine::getBlockFromDatFile(Common::String
int part = f.readByte();
gameBlockSize = f.readUint32LE();
if (f.eos()){
GUIErrorMessageFormat(_("Unable to find block for part %d").encode().c_str(), _MSPart);
GUIErrorMessageFormat(_("Unable to find block for part %d"), _MSPart);
return nullptr;
}
if (part == _MSPart) {
@ -520,7 +520,7 @@ Common::Error SupernovaEngine::showTextReader(const char *extension) {
filename = Common::String::format("ms2.%s", extension);
if (!file.open(filename)) {
GUIErrorMessageFormat(_("Unable to find '%s' in game folder or the engine data file.").encode().c_str(), filename.c_str());
GUIErrorMessageFormat(_("Unable to find '%s' in game folder or the engine data file."), filename.c_str());
return Common::kReadingFailed;
}
stream = file.readStream(file.size());

View File

@ -218,10 +218,10 @@ bool SaveManager::readSaveGameHeader(Common::InSaveFile *in, SaveGameHeader &hea
if (header.version > SAVE_VERSION) {
uint tempVersion = header.version;
GUI::MessageDialog dialog(
Common::String::format(
Common::U32String::format(
_("This saved game uses version %u, but this engine only "
"supports up to version %d. You will need an updated version "
"of the engine to use this saved game.").encode().c_str(), tempVersion, SAVE_VERSION
"of the engine to use this saved game."), tempVersion, SAVE_VERSION
),
_("OK"));
dialog.runModal();

View File

@ -99,8 +99,8 @@ AboutDialog::AboutDialog()
version += gScummVMVersion;
_lines.push_back(version);
Common::U32String date = Common::U32String::format(_("(built on %s)"), gScummVMBuildDate);
_lines.push_back(U32String("C2") + date);
Common::U32String date = Common::U32String::format(_("(built on %s)"), gScummVMBuildDate);
_lines.push_back(U32String("C2") + date);
for (i = 0; i < ARRAYSIZE(copyright_text); i++)
addLine(U32String(copyright_text[i]));
@ -235,7 +235,7 @@ void AboutDialog::drawDialog(DrawLayer layerToDraw) {
}
// Trim leading whitespaces if center mode is on
if (align == Graphics::kTextAlignCenter)
while (*strLineItrBegin && *strLineItrBegin == ' ')
while (strLineItrBegin != strLineItrEnd && *strLineItrBegin == ' ')
strLineItrBegin++;
U32String renderStr(strLineItrBegin, strLineItrEnd);

View File

@ -48,7 +48,7 @@ ChooserDialog::ChooserDialog(const U32String &title, String dialogId)
_chooseButton->setEnabled(false);
}
void ChooserDialog::setList(const U32StringArray& list) {
void ChooserDialog::setList(const U32StringArray &list) {
_list->setList(list);
}

View File

@ -50,7 +50,7 @@ protected:
public:
ChooserDialog(const U32String &title, String dialogId = "Browser");
void setList(const U32StringArray& list);
void setList(const U32StringArray &list);
void handleCommand(CommandSender *sender, uint32 cmd, uint32 data) override;
};

View File

@ -224,8 +224,8 @@ Common::U32String DownloadDialog::getSpeedLabelText() {
void DownloadDialog::refreshWidgets() {
_localDirectory = CloudMan.getDownloadLocalDirectory();
_remoteDirectoryLabel->setLabel(_("From: ").encode() + CloudMan.getDownloadRemoteDirectory());
_localDirectoryLabel->setLabel(_("To: ").encode() + _localDirectory);
_remoteDirectoryLabel->setLabel(_("From: ") + Common::U32String(CloudMan.getDownloadRemoteDirectory()));
_localDirectoryLabel->setLabel(_("To: ") + Common::U32String(_localDirectory));
uint32 progress = (uint32)(100 * CloudMan.getDownloadingProgress());
_percentLabel->setLabel(Common::String::format("%u %%", progress));
_downloadSizeLabel->setLabel(getSizeLabelText());

View File

@ -25,6 +25,7 @@
#include "gui/dialog.h"
#include "common/str.h"
#include "common/ustr.h"
namespace GUI {
class LauncherDialog;

View File

@ -129,11 +129,11 @@ enum {
kApplyCmd = 'appl'
};
static const char *savePeriodLabels[] = { _s("Never"), _s("Every 5 mins"), _s("Every 10 mins"), _s("Every 15 mins"), _s("Every 30 mins"), "" };
static const char *savePeriodLabels[] = { _s("Never"), _s("Every 5 mins"), _s("Every 10 mins"), _s("Every 15 mins"), _s("Every 30 mins"), nullptr };
static const int savePeriodValues[] = { 0, 5 * 60, 10 * 60, 15 * 60, 30 * 60, -1 };
// The keyboard mouse speed values range from 0 to 7 and correspond to speeds shown in the label
// "10" (value 3) is the default speed corresponding to the speed before introduction of this control
static const char *kbdMouseSpeedLabels[] = { "3", "5", "8", "10", "13", "15", "18", "20", "" };
static const char *kbdMouseSpeedLabels[] = { "3", "5", "8", "10", "13", "15", "18", "20", nullptr };
OptionsDialog::OptionsDialog(const Common::String &domain, int x, int y, int w, int h)
: Dialog(x, y, w, h), _domain(domain), _graphicsTabId(-1), _midiTabId(-1), _pathsTabId(-1), _tabWidget(nullptr) {

View File

@ -485,7 +485,7 @@ void PredictiveDialog::processButton(ButtonId button) {
"next", "add",
"<",
"Cancel", "OK",
"Pre", "(0) ", ""
"Pre", "(0) ", nullptr
};
if (_mode == kModeAbc) {

View File

@ -294,7 +294,7 @@ void Widget::read(const Common::U32String &str) {
#pragma mark -
StaticTextWidget::StaticTextWidget(GuiObject *boss, int x, int y, int w, int h, const Common::U32String &text, Graphics::TextAlign align, Common::U32String tooltip, ThemeEngine::FontStyle font)
: Widget(boss, x, y, w, h, tooltip), _align(align) {
: Widget(boss, x, y, w, h, tooltip) {
setFlags(WIDGET_ENABLED);
_type = kStaticTextWidget;
_label = text;