]> git.mxchange.org Git - flightgear.git/blob - src/GUI/MessageBox.cxx
Fix a clang unused constant warning
[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 using namespace simgear::strutils;
35
36 namespace {
37
38 bool isCanvasImplementationRegistered()
39 {
40         if (!globals) {
41                 return false;
42         }
43
44     SGCommandMgr* cmd = globals->get_commands();
45     return (cmd->getCommand("canvas-message-box") != NULL);
46 }
47
48 #if defined(SG_WINDOWS)
49
50 HWND getMainViewerHWND()
51 {
52         osgViewer::Viewer::Windows windows;
53         if (!globals || !globals->get_renderer() || !globals->get_renderer()->getViewer()) {
54                 return 0;
55         }
56
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")) {
61             continue;
62         }
63         
64         osgViewer::GraphicsWindowWin32* platformWin = 
65             static_cast<osgViewer::GraphicsWindowWin32*>(*it);
66         return platformWin->getHWND();
67     }
68     
69     return 0;
70 }
71     
72 flightgear::MessageBoxResult
73 win32MessageBox(const std::string& caption,
74                     const std::string& msg,
75                     const std::string& moreText)
76 {
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;
83     }
84     
85     UINT mbType = MB_OK;
86         WCharVec wMsg(convertUtf8ToWString(fullMsg)),
87                 wCap(convertUtf8ToWString(caption));
88         wMsg.push_back(0);
89         wCap.push_back(0);
90
91         ::MessageBoxExW(ownerWindow, wMsg.data(), wCap.data(),
92                     mbType, 0 /* system lang */);
93
94         return flightgear::MSG_BOX_OK;
95 }
96     
97 #endif
98     
99 } // anonymous namespace
100
101 namespace flightgear
102 {
103     
104 MessageBoxResult modalMessageBox(const std::string& caption,
105     const std::string& msg,
106     const std::string& moreText)
107 {
108     // prefer canvas
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);
115    
116         // how to make it modal?
117         
118         return MSG_BOX_OK;
119     }
120
121 #if defined(SG_WINDOWS)
122     return win32MessageBox(caption, msg, moreText);
123 #elif defined(SG_MAC)
124     return cocoaMessageBox(msg, moreText);
125 #else
126     SG_LOG(SG_GENERAL, SG_ALERT, caption << ":" << msg);
127     if (!moreText.empty()) {
128         SG_LOG(SG_GENERAL, SG_ALERT, "(" << moreText << ")");
129     }
130     return MSG_BOX_OK;
131 #endif
132 }
133
134 MessageBoxResult fatalMessageBox(const std::string& caption,
135     const std::string& msg,
136     const std::string& moreText)
137 {
138 #if defined(SG_WINDOWS)
139     return win32MessageBox(caption, msg, moreText);
140 #elif defined(SG_MAC)
141     return cocoaFatalMessage(msg, moreText);
142 #else
143     std::cerr << "FATAL:" << msg << "\n";
144     if (!moreText.empty()) {
145         std::cerr << "(" << moreText << ")";
146     }
147     std::cerr << std::endl;
148     return MSG_BOX_OK;
149 #endif
150 }
151
152 } // of namespace flightgear