3 #include "PUIFileDialog.hxx"
5 #include <boost/foreach.hpp>
7 #include <simgear/debug/logstream.hxx>
8 #include <simgear/props/props_io.hxx>
10 #include <Main/globals.hxx>
11 #include <Main/fg_props.hxx>
12 #include <GUI/new_gui.hxx>
14 class PUIFileDialog::PathListener : public SGPropertyChangeListener
17 PathListener(PUIFileDialog* dlg) :
21 virtual void valueChanged(SGPropertyNode* node)
23 _dialog->pathChanged(SGPath(node->getStringValue()));
27 PUIFileDialog* _dialog;
30 PUIFileDialog::PUIFileDialog(Usage use) :
34 SG_LOG(SG_GENERAL, SG_INFO, "created PUIFileDialog");
37 PUIFileDialog::~PUIFileDialog()
40 SGPropertyNode_ptr path = _dialogRoot->getNode("path");
41 path->removeChangeListener(_listener);
46 void PUIFileDialog::exec()
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);
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);
56 dlg->setStringValue("name", name);
59 _dialogRoot->setStringValue("title", _title);
60 _dialogRoot->setStringValue("button", _buttonText);
61 _dialogRoot->setStringValue("directory", _initialPath.str());
62 _dialogRoot->setStringValue("selection", _placeholder);
64 // convert patterns vector into pattern nodes
65 _dialogRoot->removeChildren("pattern");
67 BOOST_FOREACH(std::string pat, _filterPatterns) {
68 _dialogRoot->getNode("pattern", index++, true)->setStringValue(pat);
71 _dialogRoot->setBoolValue("show-files", _usage != USE_CHOOSE_DIR);
72 _dialogRoot->setBoolValue("dotfiles", _showHidden);
75 _listener = new PathListener(this);
77 SGPropertyNode_ptr path = _dialogRoot->getNode("path", 0, true);
78 path->addChangeListener(_listener);
80 gui->showDialog(name);
83 void PUIFileDialog::close()
85 NewGUI* gui = static_cast<NewGUI*>(globals->get_subsystem("gui"));
86 std::string name("native-file-0");
87 gui->closeDialog(name);
90 void PUIFileDialog::pathChanged(const SGPath& aPath)
92 _callback->onFileDialogDone(this, aPath);