1 // FileDialog -- generic FileDialog interface and Nasal wrapper
3 // Written by James Turner, started 2012.
5 // Copyright (C) 2012 James Turner <zakalawe@mac.com>
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include "FileDialog.hxx"
28 #include <boost/shared_ptr.hpp>
30 #include <simgear/nasal/cppbind/from_nasal.hxx>
31 #include <simgear/nasal/cppbind/to_nasal.hxx>
32 #include <simgear/nasal/cppbind/NasalHash.hxx>
34 #include <Main/globals.hxx>
35 #include <Scripting/NasalSys.hxx>
36 #include "PUIFileDialog.hxx"
39 #include "CocoaFileDialog.hxx"
42 FGFileDialog::FGFileDialog(Usage use) :
49 FGFileDialog::~FGFileDialog()
51 // ensure this is concrete so callback gets cleaned up.
54 void FGFileDialog::setTitle(const std::string& aText)
59 void FGFileDialog::setButton(const std::string& aText)
64 void FGFileDialog::setDirectory(const SGPath& aPath)
69 void FGFileDialog::setFilterPatterns(const string_list& patterns)
71 _filterPatterns = patterns;
74 void FGFileDialog::setPlaceholderName(const std::string& aName)
79 void FGFileDialog::setCallback(Callback* aCB)
84 void FGFileDialog::setShowHidden(bool show)
89 class NasalCallback : public FGFileDialog::Callback
92 NasalCallback(naRef f, naRef obj) :
96 FGNasalSys* sys = static_cast<FGNasalSys*>(globals->get_subsystem("nasal"));
97 _gcKeys[0] = sys->gcSave(f);
98 _gcKeys[1] = sys->gcSave(obj);
101 virtual void onFileDialogDone(FGFileDialog* instance, const SGPath& aPath)
103 FGNasalSys* sys = static_cast<FGNasalSys*>(globals->get_subsystem("nasal"));
104 naContext ctx = sys->context();
107 args[0] = nasal::to_nasal(ctx, aPath);
109 sys->callMethod(func, object, 1, args, naNil() /* locals */);
114 FGNasalSys* sys = static_cast<FGNasalSys*>(globals->get_subsystem("nasal"));
115 sys->gcRelease(_gcKeys[0]);
116 sys->gcRelease(_gcKeys[1]);
124 void FGFileDialog::setCallbackFromNasal(const nasal::CallContext& ctx)
126 // wrap up the naFunc in our callback type
127 naRef func = ctx.requireArg<naRef>(0);
128 naRef object = ctx.getArg<naRef>(1, naNil());
130 setCallback(new NasalCallback(func, object));
133 typedef boost::shared_ptr<FGFileDialog> FileDialogPtr;
134 typedef nasal::Ghost<FileDialogPtr> NasalFileDialog;
137 * Create new FGFileDialog and get ghost for it.
139 static naRef f_createFileDialog(naContext c, naRef me, int argc, naRef* args)
141 nasal::CallContext ctx(c, argc, args);
142 FGFileDialog::Usage usage = (FGFileDialog::Usage) ctx.requireArg<int>(0);
145 FileDialogPtr fd(new CocoaFileDialog(usage));
147 FileDialogPtr fd(new PUIFileDialog(usage));
150 return NasalFileDialog::create(c, fd);
153 void postinitNasalGUI(naRef globals, naContext c)
155 NasalFileDialog::init("gui._FileDialog")
156 .member("title", &FGFileDialog::getTitle, &FGFileDialog::setTitle)
157 .member("button", &FGFileDialog::getButton, &FGFileDialog::setButton)
158 .member("directory", &FGFileDialog::getDirectory, &FGFileDialog::setDirectory)
159 .member("show_hidden", &FGFileDialog::showHidden, &FGFileDialog::setShowHidden)
160 .member("placeholder", &FGFileDialog::getPlaceholder, &FGFileDialog::setPlaceholderName)
161 .member("pattern", &FGFileDialog::filterPatterns, &FGFileDialog::setFilterPatterns)
162 .method("open", &FGFileDialog::exec)
163 .method("close", &FGFileDialog::close)
164 .method("setCallback", &FGFileDialog::setCallbackFromNasal);
166 nasal::Hash guiModule = nasal::Hash(globals, c).get<nasal::Hash>("gui");
168 guiModule.set("FILE_DIALOG_OPEN_FILE", (int) FGFileDialog::USE_OPEN_FILE);
169 guiModule.set("FILE_DIALOG_SAVE_FILE", (int) FGFileDialog::USE_SAVE_FILE);
170 guiModule.set("FILE_DIALOG_CHOOSE_DIR", (int) FGFileDialog::USE_CHOOSE_DIR);
171 guiModule.set("_createFileDialog", f_createFileDialog);