| 1 | // FileDialog.hxx - abstract interface for a file open/save dialog |
| 2 | |
| 3 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | // SPDX-FileCopyrightText: Copyright (C) 2012 James Turner - [email protected] |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include <memory> // for std::unique_ptr |
| 9 | |
| 10 | #include <simgear/misc/sg_path.hxx> |
| 11 | #include <simgear/misc/strutils.hxx> // for string_list |
| 12 | |
| 13 | #include <simgear/nasal/cppbind/NasalCallContext.hxx> |
| 14 | |
| 15 | // forward decls |
| 16 | class SGPropertyNode; |
| 17 | |
| 18 | class FGFileDialog |
| 19 | { |
| 20 | public: |
| 21 | typedef enum { |
| 22 | USE_OPEN_FILE = 0, |
| 23 | USE_SAVE_FILE, |
| 24 | USE_CHOOSE_DIR |
| 25 | } Usage; |
| 26 | |
| 27 | std::string getTitle() const |
| 28 | { |
| 29 | return _title; |
| 30 | } |
| 31 | |
| 32 | void setTitle(const std::string& aTitle); |
| 33 | |
| 34 | std::string getButton() const |
| 35 | { |
| 36 | return _buttonText; |
| 37 | } |
| 38 | |
| 39 | void setButton(const std::string& aText); |
| 40 | |
| 41 | SGPath getDirectory() const |
| 42 | { |
| 43 | return _initialPath; |
| 44 | } |
| 45 | |
| 46 | void setDirectory(const SGPath& aPath); |
| 47 | |
| 48 | string_list filterPatterns() const |
| 49 | { |
| 50 | return _filterPatterns; |
| 51 | } |
| 52 | |
| 53 | void setFilterPatterns(const string_list& patterns); |
| 54 | |
| 55 | /// for saving |
| 56 | std::string getPlaceholder() const |
| 57 | { |
| 58 | return _placeholder; |
| 59 | } |
| 60 | |
| 61 | void setPlaceholderName(const std::string& aName); |
| 62 | |
| 63 | bool showHidden() const |
| 64 | { |
| 65 | return _showHidden; |
| 66 | } |
| 67 | void setShowHidden(bool show); |
| 68 | |
| 69 | /** |
| 70 | * @brief Set the current directory of the file dialog based on a standard-location string. |
| 71 | * E.g., `DESKTOP` or `DOCUMENTS`. This allows Nasal to request such a location without Nasal |
| 72 | * itself having read/write access until the user picks a path (and hence it's added as allowed) |
| 73 | * |
| 74 | * @param name |
| 75 | */ |
| 76 | void setStandardLocation(const std::string& name); |
| 77 | |
| 78 | |
| 79 | /** |
| 80 | * Destructor. |
| 81 | */ |
| 82 | virtual ~FGFileDialog(); |
| 83 | |
| 84 | virtual void exec() = 0; |
| 85 | virtual void close() = 0; |
| 86 | |
| 87 | class Callback |
| 88 | { |
| 89 | public: |
| 90 | virtual ~Callback() {} |
| 91 | virtual void onFileDialogDone(FGFileDialog* ins, const SGPath& result) = 0; |
| 92 | }; |
| 93 | |
| 94 | virtual void setCallback(Callback* aCB); |
| 95 | |
| 96 | void setCallbackFromNasal(const nasal::CallContext& ctx); |
| 97 | |
| 98 | protected: |
| 99 | FGFileDialog(Usage use); |
| 100 | |
| 101 | /** |
| 102 | * @brief helper for derived classes, when a path is selected |
| 103 | * |
| 104 | * @param p - the SGPath which was selected |
| 105 | * @return true if the path was handled ok, false if path was disallowed |
| 106 | */ |
| 107 | bool handleSelectedPath(const SGPath& p); |
| 108 | |
| 109 | const Usage _usage; |
| 110 | std::string _title, _buttonText; |
| 111 | SGPath _initialPath; |
| 112 | string_list _filterPatterns; |
| 113 | std::string _placeholder; |
| 114 | bool _showHidden; |
| 115 | std::unique_ptr<Callback> _callback; |
| 116 | }; |