5 #include <simgear/simgear_config.h>
7 #include "MessageBox.hxx"
9 #include <Main/globals.hxx>
10 #include <Viewer/renderer.hxx>
12 #include <osgViewer/Viewer>
14 #include <simgear/structure/commands.hxx>
19 #include <osgViewer/GraphicsWindow>
20 #include <osgViewer/api/Win32/GraphicsWindowWin32>
25 // externs from CocoaMessageBox.mm
26 flightgear::MessageBoxResult
27 cocoaFatalMessage(const std::string& msg, const std::string& text);
29 flightgear::MessageBoxResult
30 cocoaMessageBox(const std::string& msg, const std::string& text);
34 using namespace simgear::strutils;
38 bool isCanvasImplementationRegistered()
40 SGCommandMgr* cmd = globals->get_commands();
41 return (cmd->getCommand("canvas-message-box") != NULL);
44 #if defined(SG_WINDOWS)
46 HWND getMainViewerHWND()
48 osgViewer::Viewer::Windows windows;
49 if (!globals->get_renderer() || !globals->get_renderer()->getViewer()) {
53 globals->get_renderer()->getViewer()->getWindows(windows);
54 osgViewer::Viewer::Windows::const_iterator it = windows.begin();
55 for(; it != windows.end(); ++it) {
56 if (strcmp((*it)->className(), "GraphicsWindowWin32")) {
60 osgViewer::GraphicsWindowWin32* platformWin =
61 static_cast<osgViewer::GraphicsWindowWin32*>(*it);
62 return platformWin->getHWND();
68 flightgear::MessageBoxResult
69 win32MessageBox(const std::string& caption,
70 const std::string& msg,
71 const std::string& moreText)
73 // during early startup (aircraft / fg-data validation) there is no
74 // osgViewer so no HWND.
75 HWND ownerWindow = getMainViewerHWND();
76 std::string fullMsg(msg);
77 if (!moreText.empty()) {
78 fullMsg += "\n\n" + moreText;
82 WCharVec wMsg(convertUtf8ToWString(fullMsg)),
83 wCap(convertUtf8ToWString(caption));
87 ::MessageBoxExW(ownerWindow, wMsg.data(), wCap.data(),
88 mbType, 0 /* system lang */);
90 return flightgear::MSG_BOX_OK;
95 } // anonymous namespace
100 MessageBoxResult modalMessageBox(const std::string& caption,
101 const std::string& msg,
102 const std::string& moreText)
105 if (isCanvasImplementationRegistered()) {
106 SGPropertyNode_ptr args(new SGPropertyNode);
107 args->setStringValue("caption", caption);
108 args->setStringValue("message", msg);
109 args->setStringValue("more", moreText);
110 globals->get_commands()->execute("canvas-message-box", args);
112 // how to make it modal?
117 #if defined(SG_WINDOWS)
118 return win32MessageBox(caption, msg, moreText);
119 #elif defined(SG_MAC)
120 return cocoaFatalMessage(msg, moreText);
122 SG_LOG(SG_GENERAL, SG_ALERT, caption << ":" << msg);
123 if (!moreText.empty()) {
124 SG_LOG(SG_GENERAL, SG_ALERT, "(" << moreText << ")");
130 MessageBoxResult fatalMessageBox(const std::string& caption,
131 const std::string& msg,
132 const std::string& moreText)
134 #if defined(SG_WINDOWS)
135 return win32MessageBox(caption, msg, moreText);
136 #elif defined(SG_MAC)
137 return cocoaFatalMessage(msg, moreText);
139 std::cerr << "FATAL:" << msg << "\n";
140 if (!moreText.empty()) {
141 std::cerr << "(" << moreText << ")";
143 std::cerr << std::endl;
148 } // of namespace flightgear