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