]> git.mxchange.org Git - flightgear.git/blob - src/GUI/MessageBox.cxx
Reset work, fix time-slew on OSG event handling.
[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     SGCommandMgr* cmd = globals->get_commands();
41     return (cmd->getCommand("canvas-message-box") != NULL);
42 }
43
44 #if defined(SG_WINDOWS)
45
46 HWND getMainViewerHWND()
47 {
48         osgViewer::Viewer::Windows windows;
49         if (!globals->get_renderer() || !globals->get_renderer()->getViewer()) {
50                 return 0;
51         }
52
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")) {
57             continue;
58         }
59         
60         osgViewer::GraphicsWindowWin32* platformWin = 
61             static_cast<osgViewer::GraphicsWindowWin32*>(*it);
62         return platformWin->getHWND();
63     }
64     
65     return 0;
66 }
67     
68 flightgear::MessageBoxResult
69 win32MessageBox(const std::string& caption,
70                     const std::string& msg,
71                     const std::string& moreText)
72 {
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;
79     }
80     
81     UINT mbType = MB_OK;
82         WCharVec wMsg(convertUtf8ToWString(fullMsg)),
83                 wCap(convertUtf8ToWString(caption));
84         wMsg.push_back(0);
85         wCap.push_back(0);
86
87         ::MessageBoxExW(ownerWindow, wMsg.data(), wCap.data(),
88                     mbType, 0 /* system lang */);
89
90         return flightgear::MSG_BOX_OK;
91 }
92     
93 #endif
94     
95 } // anonymous namespace
96
97 namespace flightgear
98 {
99     
100 MessageBoxResult modalMessageBox(const std::string& caption,
101     const std::string& msg,
102     const std::string& moreText)
103 {
104     // prefer canvas
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);
111    
112         // how to make it modal?
113         
114         return MSG_BOX_OK;
115     }
116
117 #if defined(SG_WINDOWS)
118     return win32MessageBox(caption, msg, moreText);
119 #elif defined(SG_MAC)
120     return cocoaFatalMessage(msg, moreText);
121 #else
122     SG_LOG(SG_GENERAL, SG_ALERT, caption << ":" << msg);
123     if (!moreText.empty()) {
124         SG_LOG(SG_GENERAL, SG_ALERT, "(" << moreText << ")");
125     }
126     return MSG_BOX_OK;
127 #endif
128 }
129
130 MessageBoxResult fatalMessageBox(const std::string& caption,
131     const std::string& msg,
132     const std::string& moreText)
133 {
134 #if defined(SG_WINDOWS)
135     return win32MessageBox(caption, msg, moreText);
136 #elif defined(SG_MAC)
137     return cocoaFatalMessage(msg, moreText);
138 #else
139     std::cerr << "FATAL:" << msg << "\n";
140     if (!moreText.empty()) {
141         std::cerr << "(" << moreText << ")";
142     }
143     std::cerr << std::endl;
144     return MSG_BOX_OK;
145 #endif
146 }
147
148 } // of namespace flightgear