2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2013-04-17 21:09:55 -06:00
|
|
|
// Refer to the license.txt file included.
|
2009-02-22 23:15:48 -07:00
|
|
|
|
2014-02-19 20:11:52 -07:00
|
|
|
#include <cstdarg>
|
2014-02-17 03:18:15 -07:00
|
|
|
#include <cstdio>
|
2014-02-19 20:11:52 -07:00
|
|
|
#include <string>
|
2009-02-22 23:15:48 -07:00
|
|
|
|
2015-09-28 09:57:16 -06:00
|
|
|
#include "Common/Common.h"
|
2014-09-07 19:06:58 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2016-06-24 02:43:46 -06:00
|
|
|
#include "Common/Logging/Log.h"
|
2015-09-26 15:13:07 -06:00
|
|
|
#include "Common/MsgHandler.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/StringUtil.h"
|
2009-02-22 23:15:48 -07:00
|
|
|
|
2017-01-23 00:08:45 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2017-07-25 22:11:17 -06:00
|
|
|
bool DefaultMsgHandler(const char* caption, const char* text, bool yes_no, MsgType style);
|
2009-02-22 23:15:48 -07:00
|
|
|
static MsgAlertHandler msg_handler = DefaultMsgHandler;
|
2009-03-18 16:05:31 -06:00
|
|
|
static bool AlertEnabled = true;
|
2009-02-22 23:15:48 -07:00
|
|
|
|
2011-01-13 17:15:08 -07:00
|
|
|
std::string DefaultStringTranslator(const char* text);
|
2011-01-12 19:05:58 -07:00
|
|
|
static StringTranslator str_translator = DefaultStringTranslator;
|
|
|
|
|
2011-02-23 22:05:25 -07:00
|
|
|
// Select which of these functions that are used for message boxes. If
|
2018-08-07 07:48:17 -06:00
|
|
|
// Qt is enabled we will use QtMsgAlertHandler() that is defined in Main.cpp
|
2009-02-22 23:15:48 -07:00
|
|
|
void RegisterMsgAlertHandler(MsgAlertHandler handler)
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
msg_handler = handler;
|
2009-02-22 23:15:48 -07:00
|
|
|
}
|
|
|
|
|
2018-08-07 07:48:17 -06:00
|
|
|
// Select translation function.
|
2011-01-12 19:05:58 -07:00
|
|
|
void RegisterStringTranslator(StringTranslator translator)
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
str_translator = translator;
|
2011-01-12 19:05:58 -07:00
|
|
|
}
|
|
|
|
|
2009-03-18 16:05:31 -06:00
|
|
|
// enable/disable the alert handler
|
2011-01-12 19:05:58 -07:00
|
|
|
void SetEnableAlert(bool enable)
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
AlertEnabled = enable;
|
2009-03-18 16:05:31 -06:00
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:17 -06:00
|
|
|
std::string GetStringT(const char* string)
|
2015-11-20 03:33:47 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
return str_translator(string);
|
2015-11-20 03:33:47 -07:00
|
|
|
}
|
|
|
|
|
2011-02-23 22:05:25 -07:00
|
|
|
// This is the first stop for gui alerts where the log is updated and the
|
|
|
|
// correct window is shown
|
2017-07-25 22:11:17 -06:00
|
|
|
bool MsgAlert(bool yes_no, MsgType style, const char* format, ...)
|
2009-02-22 23:15:48 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
// Read message and write it to the log
|
|
|
|
std::string caption;
|
|
|
|
char buffer[2048];
|
|
|
|
|
|
|
|
static std::string info_caption;
|
|
|
|
static std::string warn_caption;
|
|
|
|
static std::string ques_caption;
|
|
|
|
static std::string crit_caption;
|
|
|
|
|
|
|
|
if (!info_caption.length())
|
|
|
|
{
|
|
|
|
info_caption = str_translator(_trans("Information"));
|
|
|
|
ques_caption = str_translator(_trans("Question"));
|
|
|
|
warn_caption = str_translator(_trans("Warning"));
|
|
|
|
crit_caption = str_translator(_trans("Critical"));
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:17 -06:00
|
|
|
switch (style)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2017-07-25 22:11:17 -06:00
|
|
|
case MsgType::Information:
|
2016-06-24 02:43:46 -06:00
|
|
|
caption = info_caption;
|
|
|
|
break;
|
2017-07-25 22:11:17 -06:00
|
|
|
case MsgType::Question:
|
2016-06-24 02:43:46 -06:00
|
|
|
caption = ques_caption;
|
|
|
|
break;
|
2017-07-25 22:11:17 -06:00
|
|
|
case MsgType::Warning:
|
2016-06-24 02:43:46 -06:00
|
|
|
caption = warn_caption;
|
|
|
|
break;
|
2017-07-25 22:11:17 -06:00
|
|
|
case MsgType::Critical:
|
2016-06-24 02:43:46 -06:00
|
|
|
caption = crit_caption;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
CharArrayFromFormatV(buffer, sizeof(buffer) - 1, str_translator(format).c_str(), args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
ERROR_LOG(MASTER_LOG, "%s: %s", caption.c_str(), buffer);
|
|
|
|
|
|
|
|
// Don't ignore questions, especially AskYesNo, PanicYesNo could be ignored
|
2017-07-25 22:11:17 -06:00
|
|
|
if (msg_handler && (AlertEnabled || style == MsgType::Question || style == MsgType::Critical))
|
|
|
|
return msg_handler(caption.c_str(), buffer, yes_no, style);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
return true;
|
2009-02-22 23:15:48 -07:00
|
|
|
}
|
|
|
|
|
2011-02-23 22:05:25 -07:00
|
|
|
// Default non library dependent panic alert
|
2017-07-25 22:11:17 -06:00
|
|
|
bool DefaultMsgHandler(const char* caption, const char* text, bool yes_no, MsgType style)
|
2009-02-22 23:15:48 -07:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2017-07-25 22:11:17 -06:00
|
|
|
int window_style = MB_ICONINFORMATION;
|
|
|
|
if (style == MsgType::Question)
|
|
|
|
window_style = MB_ICONQUESTION;
|
|
|
|
if (style == MsgType::Warning)
|
|
|
|
window_style = MB_ICONWARNING;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
return IDYES == MessageBox(0, UTF8ToTStr(text).c_str(), UTF8ToTStr(caption).c_str(),
|
2017-07-25 22:11:17 -06:00
|
|
|
window_style | (yes_no ? MB_YESNO : MB_OK));
|
2009-02-22 23:15:48 -07:00
|
|
|
#else
|
2016-06-24 02:43:46 -06:00
|
|
|
fprintf(stderr, "%s\n", text);
|
2015-11-07 17:58:25 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
// Return no to any question (which will in general crash the emulator)
|
|
|
|
return false;
|
2009-02-22 23:15:48 -07:00
|
|
|
#endif
|
|
|
|
}
|
2011-01-12 19:05:58 -07:00
|
|
|
|
|
|
|
// Default (non) translator
|
2011-01-13 17:15:08 -07:00
|
|
|
std::string DefaultStringTranslator(const char* text)
|
2011-01-12 19:05:58 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
return text;
|
2011-01-12 19:05:58 -07:00
|
|
|
}
|