]> git.mxchange.org Git - flightgear.git/blob - src/GUI/MessageBox.cxx
Launcher: Maintain aircraft selection better
[flightgear.git] / src / GUI / MessageBox.cxx
1 #ifdef HAVE_CONFIG_H
2     #include "config.h"
3 #endif
4
5 #include <simgear/simgear_config.h>
6
7 #include "MessageBox.hxx"
8
9 #include <Main/globals.hxx>
10 #include <Viewer/renderer.hxx>
11
12 #include <osgViewer/Viewer>
13
14 #include <simgear/structure/commands.hxx>
15
16 #ifdef SG_WINDOWS
17     #include <windows.h>
18
19 #include <osgViewer/GraphicsWindow>
20 #include <osgViewer/api/Win32/GraphicsWindowWin32>
21 #endif
22
23 #if defined(SG_MAC)
24
25 // externs from CocoaMessageBox.mm
26 flightgear::MessageBoxResult
27 cocoaFatalMessage(const std::string& msg, const std::string& text);
28
29 flightgear::MessageBoxResult
30 cocoaMessageBox(const std::string& msg, const std::string& text);
31
32 #endif
33
34 #ifdef HAVE_QT
35     #include "QtMessageBox.hxx"
36 #endif
37
38 using namespace simgear::strutils;
39
40 namespace {
41
42 bool isCanvasImplementationRegistered()
43 {
44         if (!globals) {
45                 return false;
46         }
47
48     SGCommandMgr* cmd = globals->get_commands();
49     return (cmd->getCommand("canvas-message-box") != NULL);
50 }
51
52 #if defined(SG_WINDOWS)
53
54 HWND getMainViewerHWND()
55 {
56         osgViewer::Viewer::Windows windows;
57         if (!globals || !globals->get_renderer() || !globals->get_renderer()->getViewer()) {
58                 return 0;
59         }
60
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")) {
65             continue;
66         }
67         
68         osgViewer::GraphicsWindowWin32* platformWin = 
69             static_cast<osgViewer::GraphicsWindowWin32*>(*it);
70         return platformWin->getHWND();
71     }
72     
73     return 0;
74 }
75     
76 flightgear::MessageBoxResult
77 win32MessageBox(const std::string& caption,
78                     const std::string& msg,
79                     const std::string& moreText)
80 {
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;
87     }
88     
89     UINT mbType = MB_OK;
90         WCharVec wMsg(convertUtf8ToWString(fullMsg)),
91                 wCap(convertUtf8ToWString(caption));
92         wMsg.push_back(0);
93         wCap.push_back(0);
94
95         ::MessageBoxExW(ownerWindow, wMsg.data(), wCap.data(),
96                     mbType, 0 /* system lang */);
97
98         return flightgear::MSG_BOX_OK;
99 }
100     
101 #endif
102     
103 } // anonymous namespace
104
105 namespace flightgear
106 {
107     
108 MessageBoxResult modalMessageBox(const std::string& caption,
109     const std::string& msg,
110     const std::string& moreText)
111 {
112     // prefer canvas
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);
119    
120         // how to make it modal?
121         
122         return MSG_BOX_OK;
123     }
124
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);
131 #else
132     SG_LOG(SG_GENERAL, SG_ALERT, caption << ":" << msg);
133     if (!moreText.empty()) {
134         SG_LOG(SG_GENERAL, SG_ALERT, "(" << moreText << ")");
135     }
136     return MSG_BOX_OK;
137 #endif
138 }
139
140 MessageBoxResult fatalMessageBox(const std::string& caption,
141     const std::string& msg,
142     const std::string& moreText)
143 {
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);
150 #else
151     std::cerr << "FATAL:" << msg << "\n";
152     if (!moreText.empty()) {
153         std::cerr << "(" << moreText << ")";
154     }
155     std::cerr << std::endl;
156     return MSG_BOX_OK;
157 #endif
158 }
159
160 } // of namespace flightgear