2013-11-06 23:49:58 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <simgear/simgear_config.h>
|
|
|
|
|
2017-04-08 11:49:55 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2013-11-06 23:49:58 +00:00
|
|
|
#include "MessageBox.hxx"
|
|
|
|
|
2016-07-21 11:42:09 +00:00
|
|
|
#include <Main/fg_props.hxx>
|
2013-11-06 23:49:58 +00:00
|
|
|
#include <Main/globals.hxx>
|
|
|
|
#include <Viewer/renderer.hxx>
|
2016-07-21 06:56:05 +00:00
|
|
|
#include <GUI/new_gui.hxx>
|
2013-11-06 23:49:58 +00:00
|
|
|
|
|
|
|
#include <osgViewer/Viewer>
|
|
|
|
|
|
|
|
#include <simgear/structure/commands.hxx>
|
|
|
|
|
|
|
|
#ifdef SG_WINDOWS
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#include <osgViewer/GraphicsWindow>
|
|
|
|
#include <osgViewer/api/Win32/GraphicsWindowWin32>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(SG_MAC)
|
|
|
|
|
|
|
|
// externs from CocoaMessageBox.mm
|
|
|
|
flightgear::MessageBoxResult
|
|
|
|
cocoaFatalMessage(const std::string& msg, const std::string& text);
|
|
|
|
|
|
|
|
flightgear::MessageBoxResult
|
|
|
|
cocoaMessageBox(const std::string& msg, const std::string& text);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-11-24 07:24:32 +00:00
|
|
|
#ifdef HAVE_QT
|
|
|
|
#include "QtMessageBox.hxx"
|
|
|
|
#endif
|
|
|
|
|
2013-11-06 23:49:58 +00:00
|
|
|
using namespace simgear::strutils;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool isCanvasImplementationRegistered()
|
|
|
|
{
|
2014-01-22 22:33:30 +00:00
|
|
|
if (!globals) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-06 23:49:58 +00:00
|
|
|
SGCommandMgr* cmd = globals->get_commands();
|
|
|
|
return (cmd->getCommand("canvas-message-box") != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(SG_WINDOWS)
|
|
|
|
|
|
|
|
HWND getMainViewerHWND()
|
|
|
|
{
|
|
|
|
osgViewer::Viewer::Windows windows;
|
2014-01-22 22:33:30 +00:00
|
|
|
if (!globals || !globals->get_renderer() || !globals->get_renderer()->getViewer()) {
|
2013-11-06 23:49:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
globals->get_renderer()->getViewer()->getWindows(windows);
|
|
|
|
osgViewer::Viewer::Windows::const_iterator it = windows.begin();
|
|
|
|
for(; it != windows.end(); ++it) {
|
|
|
|
if (strcmp((*it)->className(), "GraphicsWindowWin32")) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-10-20 18:52:21 +00:00
|
|
|
|
|
|
|
osgViewer::GraphicsWindowWin32* platformWin =
|
2013-11-06 23:49:58 +00:00
|
|
|
static_cast<osgViewer::GraphicsWindowWin32*>(*it);
|
|
|
|
return platformWin->getHWND();
|
|
|
|
}
|
2016-10-20 18:52:21 +00:00
|
|
|
|
2013-11-06 23:49:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-10-20 18:52:21 +00:00
|
|
|
|
2013-11-06 23:49:58 +00:00
|
|
|
flightgear::MessageBoxResult
|
|
|
|
win32MessageBox(const std::string& caption,
|
|
|
|
const std::string& msg,
|
|
|
|
const std::string& moreText)
|
|
|
|
{
|
|
|
|
// during early startup (aircraft / fg-data validation) there is no
|
|
|
|
// osgViewer so no HWND.
|
|
|
|
HWND ownerWindow = getMainViewerHWND();
|
|
|
|
std::string fullMsg(msg);
|
|
|
|
if (!moreText.empty()) {
|
|
|
|
fullMsg += "\n\n" + moreText;
|
|
|
|
}
|
2016-10-20 18:52:21 +00:00
|
|
|
|
2013-11-06 23:49:58 +00:00
|
|
|
UINT mbType = MB_OK;
|
2016-07-15 09:15:55 +00:00
|
|
|
std::wstring wMsg(convertUtf8ToWString(fullMsg)),
|
2013-11-06 23:49:58 +00:00
|
|
|
wCap(convertUtf8ToWString(caption));
|
|
|
|
|
2016-07-15 09:15:55 +00:00
|
|
|
::MessageBoxExW(ownerWindow, wMsg.c_str(), wCap.c_str(),
|
2013-11-06 23:49:58 +00:00
|
|
|
mbType, 0 /* system lang */);
|
|
|
|
|
|
|
|
return flightgear::MSG_BOX_OK;
|
|
|
|
}
|
2016-10-20 18:52:21 +00:00
|
|
|
|
2013-11-06 23:49:58 +00:00
|
|
|
#endif
|
2016-10-20 18:52:21 +00:00
|
|
|
|
2013-11-06 23:49:58 +00:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
namespace flightgear
|
|
|
|
{
|
2016-10-20 18:52:21 +00:00
|
|
|
|
2013-11-06 23:49:58 +00:00
|
|
|
MessageBoxResult modalMessageBox(const std::string& caption,
|
|
|
|
const std::string& msg,
|
|
|
|
const std::string& moreText)
|
|
|
|
{
|
2018-06-11 06:52:34 +00:00
|
|
|
// Headless mode.
|
|
|
|
if (globals->is_headless()) {
|
|
|
|
SG_LOG(SG_HEADLESS, SG_ALERT, "ModalMessageBox Caption: \"" << caption << "\"");
|
|
|
|
SG_LOG(SG_HEADLESS, SG_ALERT, "ModalMessageBox Message: \"" << msg << "\"");
|
|
|
|
if (!moreText.empty())
|
|
|
|
SG_LOG(SG_HEADLESS, SG_ALERT, "ModalMessageBox More text: \"" << moreText << "\"");
|
|
|
|
return MSG_BOX_OK;
|
|
|
|
}
|
|
|
|
|
2013-11-06 23:49:58 +00:00
|
|
|
// prefer canvas
|
|
|
|
if (isCanvasImplementationRegistered()) {
|
|
|
|
SGPropertyNode_ptr args(new SGPropertyNode);
|
|
|
|
args->setStringValue("caption", caption);
|
|
|
|
args->setStringValue("message", msg);
|
|
|
|
args->setStringValue("more", moreText);
|
2017-07-04 23:23:24 +00:00
|
|
|
globals->get_commands()->execute("canvas-message-box", args, nullptr);
|
2016-10-20 18:52:21 +00:00
|
|
|
|
2013-11-06 23:49:58 +00:00
|
|
|
// how to make it modal?
|
2016-10-20 18:52:21 +00:00
|
|
|
|
2013-11-06 23:49:58 +00:00
|
|
|
return MSG_BOX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(SG_WINDOWS)
|
|
|
|
return win32MessageBox(caption, msg, moreText);
|
|
|
|
#elif defined(SG_MAC)
|
2014-01-27 17:27:12 +00:00
|
|
|
return cocoaMessageBox(msg, moreText);
|
2015-11-24 07:24:32 +00:00
|
|
|
#elif defined(HAVE_QT)
|
|
|
|
return QtMessageBox(caption, msg, moreText, false);
|
2013-11-06 23:49:58 +00:00
|
|
|
#else
|
2016-07-21 06:56:05 +00:00
|
|
|
std::string s = caption + ": "+ msg;
|
2013-11-06 23:49:58 +00:00
|
|
|
if (!moreText.empty()) {
|
2016-07-21 06:56:05 +00:00
|
|
|
s += "\n( " + moreText + ")";
|
2013-11-06 23:49:58 +00:00
|
|
|
}
|
2016-07-21 06:56:05 +00:00
|
|
|
|
2016-10-20 18:52:21 +00:00
|
|
|
NewGUI* gui = globals->get_subsystem<NewGUI>();
|
|
|
|
if (!gui || (fgGetBool("/sim/rendering/initialized", false) == false)) {
|
2016-07-21 11:42:09 +00:00
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, s);
|
|
|
|
} else {
|
2016-10-20 18:52:21 +00:00
|
|
|
SGPropertyNode_ptr dlg = gui->getDialogProperties("popup");
|
2016-07-21 11:42:09 +00:00
|
|
|
dlg->setStringValue("text/label", s );
|
2016-10-20 18:52:21 +00:00
|
|
|
gui->showDialog("popup");
|
2016-07-21 11:42:09 +00:00
|
|
|
}
|
2013-11-06 23:49:58 +00:00
|
|
|
return MSG_BOX_OK;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-04-09 16:38:38 +00:00
|
|
|
MessageBoxResult fatalMessageBoxWithoutExit(const std::string& caption,
|
2017-04-09 15:49:06 +00:00
|
|
|
const std::string& msg,
|
|
|
|
const std::string& moreText)
|
2013-11-06 23:49:58 +00:00
|
|
|
{
|
|
|
|
#if defined(SG_WINDOWS)
|
2017-04-09 15:49:06 +00:00
|
|
|
return win32MessageBox(caption, msg, moreText);
|
2013-11-06 23:49:58 +00:00
|
|
|
#elif defined(SG_MAC)
|
2017-04-09 15:49:06 +00:00
|
|
|
return cocoaFatalMessage(msg, moreText);
|
2015-11-24 07:24:32 +00:00
|
|
|
#elif defined(HAVE_QT)
|
2017-04-09 15:49:06 +00:00
|
|
|
return QtMessageBox(caption, msg, moreText, true);
|
2013-11-06 23:49:58 +00:00
|
|
|
#else
|
2016-07-21 11:42:09 +00:00
|
|
|
std::string s = "FATAL: "+ msg;
|
2013-11-06 23:49:58 +00:00
|
|
|
if (!moreText.empty()) {
|
2016-07-21 11:42:09 +00:00
|
|
|
s += "\n( " + moreText + ")";
|
|
|
|
}
|
2016-07-21 11:55:31 +00:00
|
|
|
if (fgGetBool("/sim/rendering/initialized", false) == false) {
|
2016-07-21 11:42:09 +00:00
|
|
|
std::cerr << s << std::endl;
|
|
|
|
} else {
|
|
|
|
NewGUI* _gui = (NewGUI *)globals->get_subsystem("gui");
|
|
|
|
SGPropertyNode_ptr dlg = _gui->getDialogProperties("popup");
|
|
|
|
dlg->setStringValue("text/label", s );
|
|
|
|
_gui->showDialog("popup");
|
2013-11-06 23:49:58 +00:00
|
|
|
}
|
2017-04-09 15:49:06 +00:00
|
|
|
return MSG_BOX_OK;
|
2013-11-06 23:49:58 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-04-09 16:38:38 +00:00
|
|
|
[[noreturn]] void fatalMessageBoxThenExit(
|
|
|
|
const std::string& caption,
|
|
|
|
const std::string& msg,
|
|
|
|
const std::string& moreText,
|
|
|
|
int exitStatus)
|
|
|
|
{
|
|
|
|
fatalMessageBoxWithoutExit(caption, msg, moreText);
|
|
|
|
exit(exitStatus);
|
|
|
|
}
|
|
|
|
|
2013-11-06 23:49:58 +00:00
|
|
|
} // of namespace flightgear
|