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()
44 SGCommandMgr* cmd = globals->get_commands();
45 return (cmd->getCommand("canvas-message-box") != NULL);
48 #if defined(SG_WINDOWS)
50 HWND getMainViewerHWND()
52 osgViewer::Viewer::Windows windows;
53 if (!globals || !globals->get_renderer() || !globals->get_renderer()->getViewer()) {
57 globals->get_renderer()->getViewer()->getWindows(windows);
58 osgViewer::Viewer::Windows::const_iterator it = windows.begin();
59 for(; it != windows.end(); ++it) {
60 if (strcmp((*it)->className(), "GraphicsWindowWin32")) {
64 osgViewer::GraphicsWindowWin32* platformWin =
65 static_cast<osgViewer::GraphicsWindowWin32*>(*it);
66 return platformWin->getHWND();
72 flightgear::MessageBoxResult
73 win32MessageBox(const std::string& caption,
74 const std::string& msg,
75 const std::string& moreText)
77 // during early startup (aircraft / fg-data validation) there is no
78 // osgViewer so no HWND.
79 HWND ownerWindow = getMainViewerHWND();
80 std::string fullMsg(msg);
81 if (!moreText.empty()) {
82 fullMsg += "\n\n" + moreText;
86 WCharVec wMsg(convertUtf8ToWString(fullMsg)),
87 wCap(convertUtf8ToWString(caption));
91 ::MessageBoxExW(ownerWindow, wMsg.data(), wCap.data(),
92 mbType, 0 /* system lang */);
94 return flightgear::MSG_BOX_OK;
99 } // anonymous namespace
104 MessageBoxResult modalMessageBox(const std::string& caption,
105 const std::string& msg,
106 const std::string& moreText)
109 if (isCanvasImplementationRegistered()) {
110 SGPropertyNode_ptr args(new SGPropertyNode);
111 args->setStringValue("caption", caption);
112 args->setStringValue("message", msg);
113 args->setStringValue("more", moreText);
114 globals->get_commands()->execute("canvas-message-box", args);
116 // how to make it modal?
121 #if defined(SG_WINDOWS)
122 return win32MessageBox(caption, msg, moreText);
123 #elif defined(SG_MAC)
124 return cocoaMessageBox(msg, moreText);
126 SG_LOG(SG_GENERAL, SG_ALERT, caption << ":" << msg);
127 if (!moreText.empty()) {
128 SG_LOG(SG_GENERAL, SG_ALERT, "(" << moreText << ")");
134 MessageBoxResult fatalMessageBox(const std::string& caption,
135 const std::string& msg,
136 const std::string& moreText)
138 #if defined(SG_WINDOWS)
139 return win32MessageBox(caption, msg, moreText);
140 #elif defined(SG_MAC)
141 return cocoaFatalMessage(msg, moreText);
143 std::cerr << "FATAL:" << msg << "\n";
144 if (!moreText.empty()) {
145 std::cerr << "(" << moreText << ")";
147 std::cerr << std::endl;
152 } // of namespace flightgear