]> git.mxchange.org Git - flightgear.git/blob - src/GUI/PUIFileDialog.cxx
Support for multiple data dirs.
[flightgear.git] / src / GUI / PUIFileDialog.cxx
1
2
3 #include "PUIFileDialog.hxx"
4
5 #include <boost/foreach.hpp>
6
7 #include <simgear/debug/logstream.hxx>
8 #include <simgear/props/props_io.hxx>
9
10 #include <Main/globals.hxx>
11 #include <Main/fg_props.hxx>
12 #include <GUI/new_gui.hxx>
13
14 class PUIFileDialog::PathListener : public SGPropertyChangeListener
15 {
16 public:
17     PathListener(PUIFileDialog* dlg) :
18         _dialog(dlg)
19     { ; }
20     
21     virtual void valueChanged(SGPropertyNode* node)
22     {
23         _dialog->pathChanged(SGPath(node->getStringValue()));
24     }
25     
26 private:
27     PUIFileDialog* _dialog;
28 };
29
30 PUIFileDialog::PUIFileDialog(Usage use) :
31     FGFileDialog(use),
32     _listener(NULL)
33 {
34     SG_LOG(SG_GENERAL, SG_INFO, "created PUIFileDialog");
35 }
36
37 PUIFileDialog::~PUIFileDialog()
38 {
39     if (_listener) {
40         SGPropertyNode_ptr path = _dialogRoot->getNode("path");
41         path->removeChangeListener(_listener);
42         delete _listener;
43     }
44 }
45
46 void PUIFileDialog::exec()
47 {
48     NewGUI* gui = static_cast<NewGUI*>(globals->get_subsystem("gui"));
49     std::string name("native-file-0");
50     _dialogRoot = fgGetNode("/sim/gui/dialogs/" + name, true);
51     
52     SGPropertyNode_ptr dlg = _dialogRoot->getChild("dialog", 0, true);
53     SGPath dlgXML = globals->resolve_resource_path("gui/dialogs/file-select.xml");
54     readProperties(dlgXML.str(), dlg);
55     
56     dlg->setStringValue("name", name);
57     gui->newDialog(dlg);
58     
59     _dialogRoot->setStringValue("title", _title);
60     _dialogRoot->setStringValue("button", _buttonText);
61     _dialogRoot->setStringValue("directory", _initialPath.str());
62     _dialogRoot->setStringValue("selection", _placeholder);
63     
64 // convert patterns vector into pattern nodes
65     _dialogRoot->removeChildren("pattern");
66     int index=0;
67     BOOST_FOREACH(std::string pat, _filterPatterns) {
68         _dialogRoot->getNode("pattern", index++, true)->setStringValue(pat);
69     }
70     
71     _dialogRoot->setBoolValue("show-files", _usage != USE_CHOOSE_DIR);
72     _dialogRoot->setBoolValue("dotfiles", _showHidden);
73     
74     if (!_listener) {
75         _listener = new PathListener(this);
76     }
77     SGPropertyNode_ptr path = _dialogRoot->getNode("path", 0, true);
78     path->addChangeListener(_listener);
79     
80     gui->showDialog(name);
81 }
82
83 void PUIFileDialog::close()
84 {
85     NewGUI* gui = static_cast<NewGUI*>(globals->get_subsystem("gui"));
86     std::string name("native-file-0");
87     gui->closeDialog(name);
88 }
89
90 void PUIFileDialog::pathChanged(const SGPath& aPath)
91 {
92     _callback->onFileDialogDone(this, aPath);
93 }