]> git.mxchange.org Git - flightgear.git/blob - src/GUI/FileDialog.cxx
CrashRpt tweaks, checking.
[flightgear.git] / src / GUI / FileDialog.cxx
1 // FileDialog -- generic FileDialog interface and Nasal wrapper
2 //
3 // Written by James Turner, started 2012.
4 //
5 // Copyright (C) 2012 James Turner  <zakalawe@mac.com>
6 //
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.
11 //
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.
16 //
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.
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25
26 #include "FileDialog.hxx"
27
28 #include <boost/shared_ptr.hpp>
29
30 #include <simgear/nasal/cppbind/from_nasal.hxx>
31 #include <simgear/nasal/cppbind/to_nasal.hxx>
32 #include <simgear/nasal/cppbind/NasalHash.hxx>
33
34 #include <Main/globals.hxx>
35 #include <Scripting/NasalSys.hxx>
36 #include "PUIFileDialog.hxx"
37
38 #if defined(SG_MAC)
39     #include "CocoaFileDialog.hxx"
40 #endif
41
42 #if defined(SG_WINDOWS)
43     #include "WindowsFileDialog.hxx"
44 #endif
45
46 FGFileDialog::FGFileDialog(Usage use) :
47     _usage(use),
48     _showHidden(false)
49 {
50     
51 }
52
53 FGFileDialog::~FGFileDialog()
54 {
55     // ensure this is concrete so callback gets cleaned up.
56 }
57
58 void FGFileDialog::setTitle(const std::string& aText)
59 {
60     _title = aText;
61 }
62
63 void FGFileDialog::setButton(const std::string& aText)
64 {
65     _buttonText = aText;
66 }
67
68 void FGFileDialog::setDirectory(const SGPath& aPath)
69 {
70     _initialPath = aPath;
71 }
72
73 void FGFileDialog::setFilterPatterns(const string_list& patterns)
74 {
75     _filterPatterns = patterns;
76 }
77
78 void FGFileDialog::setPlaceholderName(const std::string& aName)
79 {
80     _placeholder = aName;
81 }
82
83 void FGFileDialog::setCallback(Callback* aCB)
84 {
85     _callback.reset(aCB);
86 }
87
88 void FGFileDialog::setShowHidden(bool show)
89 {
90     _showHidden = show;
91 }
92
93 class NasalCallback : public FGFileDialog::Callback
94 {
95 public:
96     NasalCallback(naRef f, naRef obj) :
97         func(f),
98         object(obj)
99     {
100         FGNasalSys* sys = static_cast<FGNasalSys*>(globals->get_subsystem("nasal"));
101         _gcKeys[0] = sys->gcSave(f);
102         _gcKeys[1] = sys->gcSave(obj);
103     }
104     
105     virtual void onFileDialogDone(FGFileDialog* instance, const SGPath& aPath)
106     {
107         FGNasalSys* sys = static_cast<FGNasalSys*>(globals->get_subsystem("nasal"));
108         
109         naContext ctx = naNewContext();
110         naRef args[1];
111         args[0] = nasal::to_nasal(ctx, aPath);
112         
113         sys->callMethod(func, object, 1, args, naNil() /* locals */);
114         naFreeContext(ctx);
115     }
116     
117     ~NasalCallback()
118     {
119         FGNasalSys* sys = static_cast<FGNasalSys*>(globals->get_subsystem("nasal"));
120         sys->gcRelease(_gcKeys[0]);
121         sys->gcRelease(_gcKeys[1]);
122     }
123 private:
124     naRef func;
125     naRef object;
126     int _gcKeys[2];
127 };
128
129 void FGFileDialog::setCallbackFromNasal(const nasal::CallContext& ctx)
130 {
131     // wrap up the naFunc in our callback type
132     naRef func = ctx.requireArg<naRef>(0);
133     naRef object = ctx.getArg<naRef>(1, naNil());
134     
135     setCallback(new NasalCallback(func, object));
136 }
137
138 typedef boost::shared_ptr<FGFileDialog> FileDialogPtr;
139 typedef nasal::Ghost<FileDialogPtr> NasalFileDialog;
140
141 /**
142  * Create new FGFileDialog and get ghost for it.
143  */
144 static naRef f_createFileDialog(naContext c, naRef me, int argc, naRef* args)
145 {
146     nasal::CallContext ctx(c, argc, args);
147     FGFileDialog::Usage usage = (FGFileDialog::Usage) ctx.requireArg<int>(0);
148   
149 #if defined(SG_MAC)
150     FileDialogPtr fd(new CocoaFileDialog(usage));
151 #elif defined(SG_WINDOWS)
152         FileDialogPtr fd(new WindowsFileDialog(usage));
153 #else
154     FileDialogPtr fd(new PUIFileDialog(usage));
155 #endif
156     
157     return NasalFileDialog::create(c, fd);
158 }
159
160 void postinitNasalGUI(naRef globals, naContext c)
161 {
162     NasalFileDialog::init("gui._FileDialog")
163     .member("title", &FGFileDialog::getTitle,  &FGFileDialog::setTitle)
164     .member("button", &FGFileDialog::getButton,  &FGFileDialog::setButton)
165     .member("directory", &FGFileDialog::getDirectory, &FGFileDialog::setDirectory)
166     .member("show_hidden", &FGFileDialog::showHidden, &FGFileDialog::setShowHidden)
167     .member("placeholder", &FGFileDialog::getPlaceholder, &FGFileDialog::setPlaceholderName)
168     .member("pattern", &FGFileDialog::filterPatterns, &FGFileDialog::setFilterPatterns)
169     .method("open", &FGFileDialog::exec)
170     .method("close", &FGFileDialog::close)
171     .method("setCallback", &FGFileDialog::setCallbackFromNasal);
172     
173     nasal::Hash guiModule = nasal::Hash(globals, c).get<nasal::Hash>("gui");
174     
175     guiModule.set("FILE_DIALOG_OPEN_FILE", (int) FGFileDialog::USE_OPEN_FILE);
176     guiModule.set("FILE_DIALOG_SAVE_FILE", (int) FGFileDialog::USE_SAVE_FILE);
177     guiModule.set("FILE_DIALOG_CHOOSE_DIR", (int) FGFileDialog::USE_CHOOSE_DIR);
178     guiModule.set("_createFileDialog", f_createFileDialog);
179 }