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);
35 #include "QtMessageBox.hxx"
38 using namespace simgear::strutils;
42 bool isCanvasImplementationRegistered()
48 SGCommandMgr* cmd = globals->get_commands();
49 return (cmd->getCommand("canvas-message-box") != NULL);
52 #if defined(SG_WINDOWS)
54 HWND getMainViewerHWND()
56 osgViewer::Viewer::Windows windows;
57 if (!globals || !globals->get_renderer() || !globals->get_renderer()->getViewer()) {
61 globals->get_renderer()->getViewer()->getWindows(windows);
62 osgViewer::Viewer::Windows::const_iterator it = windows.begin();
63 for(; it != windows.end(); ++it) {
64 if (strcmp((*it)->className(), "GraphicsWindowWin32")) {
68 osgViewer::GraphicsWindowWin32* platformWin =
69 static_cast<osgViewer::GraphicsWindowWin32*>(*it);
70 return platformWin->getHWND();
76 flightgear::MessageBoxResult
77 win32MessageBox(const std::string& caption,
78 const std::string& msg,
79 const std::string& moreText)
81 // during early startup (aircraft / fg-data validation) there is no
82 // osgViewer so no HWND.
83 HWND ownerWindow = getMainViewerHWND();
84 std::string fullMsg(msg);
85 if (!moreText.empty()) {
86 fullMsg += "\n\n" + moreText;
90 WCharVec wMsg(convertUtf8ToWString(fullMsg)),
91 wCap(convertUtf8ToWString(caption));
95 ::MessageBoxExW(ownerWindow, wMsg.data(), wCap.data(),
96 mbType, 0 /* system lang */);
98 return flightgear::MSG_BOX_OK;
103 } // anonymous namespace
108 MessageBoxResult modalMessageBox(const std::string& caption,
109 const std::string& msg,
110 const std::string& moreText)
113 if (isCanvasImplementationRegistered()) {
114 SGPropertyNode_ptr args(new SGPropertyNode);
115 args->setStringValue("caption", caption);
116 args->setStringValue("message", msg);
117 args->setStringValue("more", moreText);
118 globals->get_commands()->execute("canvas-message-box", args);
120 // how to make it modal?
125 #if defined(SG_WINDOWS)
126 return win32MessageBox(caption, msg, moreText);
127 #elif defined(SG_MAC)
128 return cocoaMessageBox(msg, moreText);
129 #elif defined(HAVE_QT)
130 return QtMessageBox(caption, msg, moreText, false);
132 SG_LOG(SG_GENERAL, SG_ALERT, caption << ":" << msg);
133 if (!moreText.empty()) {
134 SG_LOG(SG_GENERAL, SG_ALERT, "(" << moreText << ")");
140 MessageBoxResult fatalMessageBox(const std::string& caption,
141 const std::string& msg,
142 const std::string& moreText)
144 #if defined(SG_WINDOWS)
145 return win32MessageBox(caption, msg, moreText);
146 #elif defined(SG_MAC)
147 return cocoaFatalMessage(msg, moreText);
148 #elif defined(HAVE_QT)
149 return QtMessageBox(caption, msg, moreText, true);
151 std::cerr << "FATAL:" << msg << "\n";
152 if (!moreText.empty()) {
153 std::cerr << "(" << moreText << ")";
155 std::cerr << std::endl;
160 } // of namespace flightgear