[Base] Clean up simple message box utility.

This commit is contained in:
gibbed 2021-06-28 11:08:57 -05:00 committed by Rick Gibbed
parent bf5f700f9e
commit 80cafd9358
6 changed files with 44 additions and 25 deletions

View File

@ -16,6 +16,7 @@
#include "xenia/base/logging.h" #include "xenia/base/logging.h"
#include "xenia/base/main.h" #include "xenia/base/main.h"
#include "xenia/base/system.h"
namespace utfcpp = utf8; namespace utfcpp = utf8;
@ -67,7 +68,8 @@ void ParseLaunchArguments(int& argc, char**& argv,
if (xe::has_console_attached()) { if (xe::has_console_attached()) {
PrintHelpAndExit(); PrintHelpAndExit();
} else { } else {
xe::ShowInfoMessageBox(options.help({""})); xe::ShowSimpleMessageBox(xe::SimpleMessageBoxType::Help,
options.help({""}));
exit(0); exit(0);
} }
} }
@ -92,7 +94,7 @@ void ParseLaunchArguments(int& argc, char**& argv,
} else { } else {
std::string m = std::string m =
"Invalid launch options were given.\n" + options.help({""}); "Invalid launch options were given.\n" + options.help({""});
xe::ShowErrorMessageBox(m); xe::ShowSimpleMessageBox(xe::SimpleMessageBoxType::Error, m);
exit(0); exit(0);
} }
} }

View File

@ -26,6 +26,7 @@
#include "xenia/base/memory.h" #include "xenia/base/memory.h"
#include "xenia/base/ring_buffer.h" #include "xenia/base/ring_buffer.h"
#include "xenia/base/string.h" #include "xenia/base/string.h"
#include "xenia/base/system.h"
#include "xenia/base/threading.h" #include "xenia/base/threading.h"
// For MessageBox: // For MessageBox:
@ -353,28 +354,12 @@ void logging::AppendLogLine(LogLevel log_level, const char prefix_char,
void FatalError(const std::string_view str) { void FatalError(const std::string_view str) {
logging::AppendLogLine(LogLevel::Error, 'X', str); logging::AppendLogLine(LogLevel::Error, 'X', str);
#if XE_PLATFORM_WIN32
if (!xe::has_console_attached()) { if (!xe::has_console_attached()) {
MessageBoxW(NULL, (LPCWSTR)xe::to_utf16(str).c_str(), L"Xenia Error", ShowSimpleMessageBox(SimpleMessageBoxType::Error, str);
MB_OK | MB_ICONERROR | MB_APPLMODAL | MB_SETFOREGROUND);
} }
#endif // WIN32
ShutdownLogging(); ShutdownLogging();
std::exit(1); std::exit(1);
} }
void ShowInfoMessageBox(std::string m) {
#if XE_PLATFORM_WIN32
MessageBoxW(NULL, (LPCWSTR)xe::to_utf16(m).c_str(), L"Xenia Help",
MB_OK | MB_ICONINFORMATION | MB_APPLMODAL | MB_SETFOREGROUND);
#endif // WIN32
}
void ShowErrorMessageBox(std::string m) {
#if XE_PLATFORM_WIN32
MessageBoxW(NULL, (LPCWSTR)xe::path_to_utf16(m).c_str(), L"Xenia Error",
MB_OK | MB_ICONERROR | MB_APPLMODAL | MB_SETFOREGROUND);
#endif // WIN32
}
} // namespace xe } // namespace xe

View File

@ -95,11 +95,6 @@ void AppendLogLine(LogLevel log_level, const char prefix_char,
// Logs a fatal error and aborts the program. // Logs a fatal error and aborts the program.
void FatalError(const std::string_view str); void FatalError(const std::string_view str);
// Shows error box
void ShowErrorMessageBox(std::string m);
// Show info box
void ShowInfoMessageBox(std::string m);
} // namespace xe } // namespace xe
#if XE_OPTION_ENABLE_LOGGING #if XE_OPTION_ENABLE_LOGGING

View File

@ -20,6 +20,15 @@ namespace xe {
void LaunchWebBrowser(const std::string& url); void LaunchWebBrowser(const std::string& url);
void LaunchFileExplorer(const std::filesystem::path& path); void LaunchFileExplorer(const std::filesystem::path& path);
enum class SimpleMessageBoxType {
Help,
Warning,
Error,
};
// This is expected to block the caller until the message box is closed.
void ShowSimpleMessageBox(SimpleMessageBoxType type, std::string_view message);
} // namespace xe } // namespace xe
#endif // XENIA_BASE_SYSTEM_H_ #endif // XENIA_BASE_SYSTEM_H_

View File

@ -25,4 +25,8 @@ void LaunchWebBrowser(const std::string& url) {
void LaunchFileExplorer(const std::filesystem::path& path) { assert_always(); } void LaunchFileExplorer(const std::filesystem::path& path) { assert_always(); }
void ShowSimpleMessageBox(SimpleMessageBoxType type, std::string_view message) {
assert_always();
}
} // namespace xe } // namespace xe

View File

@ -24,4 +24,28 @@ void LaunchFileExplorer(const std::filesystem::path& url) {
SW_SHOWNORMAL); SW_SHOWNORMAL);
} }
void ShowSimpleMessageBox(SimpleMessageBoxType type,
const std::string_view message) {
const wchar_t* title;
std::u16string wide_message = xe::to_utf16(message);
DWORD type_flags = MB_OK | MB_APPLMODAL | MB_SETFOREGROUND;
switch (type) {
default:
case SimpleMessageBoxType::Help:
title = L"Xenia Help";
type_flags |= MB_ICONINFORMATION;
break;
case SimpleMessageBoxType::Warning:
title = L"Xenia Warning";
type_flags |= MB_ICONWARNING;
break;
case SimpleMessageBoxType::Error:
title = L"Xenia Error";
type_flags |= MB_ICONERROR;
break;
}
MessageBoxW(nullptr, reinterpret_cast<LPCWSTR>(wide_message.c_str()), title,
type_flags);
}
} // namespace xe } // namespace xe