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