]> git.mxchange.org Git - flightgear.git/blob - src/GUI/PUIFileDialog.cxx
Initial work on native file dialog support.
[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(const std::string& aTitle, Usage use) :
31     FGFileDialog(aTitle, 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     }
43 }
44
45 void PUIFileDialog::exec()
46 {
47     NewGUI* gui = static_cast<NewGUI*>(globals->get_subsystem("gui"));
48     std::string name("native-file-0");
49     _dialogRoot = fgGetNode("/sim/gui/dialogs/" + name, true);
50     
51     SGPropertyNode_ptr dlg = _dialogRoot->getChild("dialog", 0, true);
52     SGPath dlgXML = globals->resolve_resource_path("gui/dialogs/file-select.xml");
53     readProperties(dlgXML.str(), dlg);
54     
55     dlg->setStringValue("name", name);
56     gui->newDialog(dlg);
57     
58     _dialogRoot->setStringValue("title", _title);
59     _dialogRoot->setStringValue("button", _buttonText);
60     _dialogRoot->setStringValue("directory", _initialPath.str());
61     _dialogRoot->setStringValue("selection", _placeholder);
62     
63 // convert patterns vector into pattern nodes
64     _dialogRoot->removeChildren("pattern");
65     int index=0;
66     BOOST_FOREACH(std::string pat, _filterPatterns) {
67         _dialogRoot->getNode("pattern", index++, true)->setStringValue(pat);
68     }
69     
70     _dialogRoot->setBoolValue("show-files", _usage != USE_CHOOSE_DIR);
71     _dialogRoot->setBoolValue("dotfiles", _showHidden);
72     
73     if (!_listener) {
74         _listener = new PathListener(this);
75     }
76     SGPropertyNode_ptr path = _dialogRoot->getNode("path", 0, true);
77     path->addChangeListener(_listener);
78     
79     gui->showDialog(name);
80 }
81
82 void PUIFileDialog::pathChanged(const SGPath& aPath)
83 {
84     _callback->onFileDialogDone(this, aPath);
85 }