FlightGear next
dialog.cxx
Go to the documentation of this file.
1// dialog.cxx: implementation of an XML-configurable dialog box.
2
3#include "config.h"
4
5#include <utility>
6
7#include "dialog.hxx"
8
9#include <simgear/props/props.hxx>
10
11namespace {
12
13using WS = FGDialog::WindowStyle;
14using WF = FGDialog::WindowFlags;
15
16FGDialog::WindowStyle styleFromProps(const std::string& s)
17{
18 if (s == "modal-dialag") {
19 return WS::ModalDialog;
20 }
21
22 if (s == "message-box") {
23 return WS::MessageBox;
24 }
25
26 return WS::Window;
27}
28
29int defaultFlagsForStyle(FGDialog::WindowStyle ws)
30{
31 switch (ws) {
32 case WS::ModalDialog:
33 return WF::ButtonBox;
34
35 case WS::MessageBox:
36 return WF::ButtonBox;
37
38 default:
39 return WF::Resizable | WF::Closeable;
40 }
41}
42
43} // namespace
44
45FGDialog::FGDialog(SGPropertyNode* props, std::string translationDomain)
46 : _translationDomain(std::move(translationDomain)),
47 _windowStyle(styleFromProps(props->getStringValue("window-style")))
48{
49 _flags = defaultFlagsForStyle(_windowStyle);
50 updateFlagFromProperty(WF::Closeable, props, "closeable");
51 updateFlagFromProperty(WF::Resizable, props, "resizeable");
52 updateFlagFromProperty(WF::ButtonBox, props, "has-buttons");
53
54 const auto translationDomainNode = props->getChild("translation-domain");
55 if (translationDomainNode) {
56 // Override what was set by the constructor member initializer list
57 setTranslationDomain(translationDomainNode->getStringValue());
58 }
59}
60
61std::string FGDialog::translationDomain() const noexcept
62{
63 return _translationDomain;
64}
65
66void FGDialog::setTranslationDomain(std::string domain) noexcept
67{
68 _translationDomain = std::move(domain);
69}
70
71void FGDialog::updateFlagFromProperty(WindowFlags f, SGPropertyNode* props, const std::string& name)
72{
73 auto c = props->getChild(name);
74 if (!c) {
75 return;
76 }
77
78 const auto invF = ~f;
79 _flags &= invF; // clear to zero
80 if (c->getBoolValue()) {
81 _flags |= f;
82 }
83}
84
85
86FGDialog::~FGDialog() = default;
87
89{
90 return _windowStyle;
91}
92
94{
95 return _flags & f;
96}
FGDialog(SGPropertyNode *props, std::string translationDomain="core")
Construct a new GUI widget configured by a property tree.
Definition dialog.cxx:45
std::string translationDomain() const noexcept
Return the translation domain of the dialog.
Definition dialog.cxx:61
void setTranslationDomain(std::string domain) noexcept
Set the translation domain of the dialog.
Definition dialog.cxx:66
bool isFlagSet(WindowFlags f) const
Definition dialog.cxx:93
WindowStyle windowStyle() const
Definition dialog.cxx:88
virtual ~FGDialog()
Destructor.
const char * name