1
0
Fork 0

Make it all work with existing FileSelector API.

This commit is contained in:
James Turner 2013-01-19 16:23:05 +00:00
parent 9f8c66fbf9
commit 008a2d65f1
6 changed files with 73 additions and 38 deletions

View file

@ -8,12 +8,12 @@
class CocoaFileDialog : public FGFileDialog
{
public:
CocoaFileDialog(const std::string& aTitle, FGFileDialog::Usage use);
CocoaFileDialog(FGFileDialog::Usage use);
virtual ~CocoaFileDialog();
virtual void exec();
virtual void close();
private:
class CocoaFileDialogPrivate;
std::auto_ptr<CocoaFileDialogPrivate> d;

View file

@ -44,8 +44,8 @@ public:
NSSavePanel* panel;
};
CocoaFileDialog::CocoaFileDialog(const std::string& aTitle, FGFileDialog::Usage use) :
FGFileDialog(aTitle, use)
CocoaFileDialog::CocoaFileDialog(FGFileDialog::Usage use) :
FGFileDialog(use)
{
d.reset(new CocoaFileDialogPrivate);
if (use == USE_SAVE_FILE) {
@ -58,6 +58,8 @@ CocoaFileDialog::CocoaFileDialog(const std::string& aTitle, FGFileDialog::Usage
[openPanel setCanChooseDirectories:YES];
}
} // of USE_OPEN_FILE or USE_CHOOSE_DIR -> building NSOpenPanel
[d->panel retain];
}
CocoaFileDialog::~CocoaFileDialog()
@ -90,16 +92,21 @@ void CocoaFileDialog::exec()
[d->panel setNameFieldStringValue:stdStringToCocoa(_placeholder)];
}
NSMutableArray* extensions = [NSMutableArray arrayWithCapacity:0];
BOOST_FOREACH(std::string ext, _filterPatterns) {
if (!simgear::strutils::starts_with(ext, "*.")) {
SG_LOG(SG_GENERAL, SG_INFO, "can't use pattern on Cococa:" << ext);
continue;
if (_filterPatterns.empty()) {
[d->panel setAllowedFileTypes:nil];
} else {
NSMutableArray* extensions = [NSMutableArray arrayWithCapacity:0];
BOOST_FOREACH(std::string ext, _filterPatterns) {
if (!simgear::strutils::starts_with(ext, "*.")) {
SG_LOG(SG_GENERAL, SG_INFO, "can't use pattern on Cococa:" << ext);
continue;
}
[extensions addObject:stdStringToCocoa(ext.substr(2))];
}
[extensions addObject:stdStringToCocoa(ext.substr(2))];
}
[d->panel setAllowedFileTypes:extensions];
[d->panel setAllowedFileTypes:extensions];
}
[d->panel setTitle:stdStringToCocoa(_title)];
if (_showHidden) {
[d->panel setShowsHiddenFiles:YES];
@ -117,3 +124,9 @@ void CocoaFileDialog::exec()
}
}];
}
void CocoaFileDialog::close()
{
[d->panel close];
}

View file

@ -39,9 +39,8 @@
#include "CocoaFileDialog.hxx"
#endif
FGFileDialog::FGFileDialog(const std::string& aTitle, Usage use) :
FGFileDialog::FGFileDialog(Usage use) :
_usage(use),
_title(aTitle),
_showHidden(false)
{
@ -52,6 +51,11 @@ FGFileDialog::~FGFileDialog()
// ensure this is concrete so callback gets cleaned up.
}
void FGFileDialog::setTitle(const std::string& aText)
{
_title = aText;
}
void FGFileDialog::setButton(const std::string& aText)
{
_buttonText = aText;
@ -88,6 +92,12 @@ naRef FGFileDialog::openFromNasal(const nasal::CallContext& ctx)
return naNil();
}
naRef FGFileDialog::closeFromNasal(const nasal::CallContext& ctx)
{
close();
return naNil();
}
class NasalCallback : public FGFileDialog::Callback
{
public:
@ -142,13 +152,12 @@ typedef nasal::Ghost<FileDialogPtr> NasalFileDialog;
static naRef f_createFileDialog(naContext c, naRef me, int argc, naRef* args)
{
nasal::CallContext ctx(c, argc, args);
std::string title = ctx.requireArg<std::string>(0);
FGFileDialog::Usage usage = (FGFileDialog::Usage) ctx.requireArg<int>(1);
FGFileDialog::Usage usage = (FGFileDialog::Usage) ctx.requireArg<int>(0);
#ifdef SG_MAC
FileDialogPtr fd(new CocoaFileDialog(title, usage));
FileDialogPtr fd(new CocoaFileDialog(usage));
#else
FileDialogPtr fd(new PUIFileDialog(title, usage));
FileDialogPtr fd(new PUIFileDialog(usage));
#endif
return NasalFileDialog::create(c, fd);
@ -156,23 +165,21 @@ static naRef f_createFileDialog(naContext c, naRef me, int argc, naRef* args)
void postinitNasalGUI(naRef globals, naContext c)
{
NasalFileDialog::init("gui.FileSelector")
NasalFileDialog::init("gui._FileDialog")
.member("title", &FGFileDialog::getTitle, &FGFileDialog::setTitle)
.member("button", &FGFileDialog::getButton, &FGFileDialog::setButton)
.member("directory", &FGFileDialog::getDirectory, &FGFileDialog::setDirectory)
.member("show-hidden", &FGFileDialog::showHidden, &FGFileDialog::setShowHidden)
.member("show_hidden", &FGFileDialog::showHidden, &FGFileDialog::setShowHidden)
.member("placeholder", &FGFileDialog::getPlaceholder, &FGFileDialog::setPlaceholderName)
.member("pattern", &FGFileDialog::filterPatterns, &FGFileDialog::setFilterPatterns)
.method<&FGFileDialog::openFromNasal>("open")
.method<&FGFileDialog::closeFromNasal>("close")
.method<&FGFileDialog::setCallbackFromNasal>("setCallback");
nasal::Hash guiModule = nasal::Hash(globals, c).get<nasal::Hash>("gui");
naRef guiModule = naHash_cget(globals, (char*) "gui");
if (naIsNil(guiModule)) {
SG_LOG(SG_GENERAL, SG_WARN, "postinitNasalGUI: gui.nas not loaded");
return;
}
nasal::Hash globals_module(globals, c),
gui_module = globals_module.get<nasal::Hash>("gui");
gui_module.set("_newFileDialog", f_createFileDialog);
guiModule.set("FILE_DIALOG_OPEN_FILE", (int) FGFileDialog::USE_OPEN_FILE);
guiModule.set("FILE_DIALOG_SAVE_FILE", (int) FGFileDialog::USE_SAVE_FILE);
guiModule.set("FILE_DIALOG_CHOOSE_DIR", (int) FGFileDialog::USE_CHOOSE_DIR);
guiModule.set("_createFileDialog", f_createFileDialog);
}

View file

@ -17,11 +17,16 @@ class FGFileDialog
{
public:
typedef enum {
USE_OPEN_FILE,
USE_OPEN_FILE = 0,
USE_SAVE_FILE,
USE_CHOOSE_DIR
} Usage;
std::string getTitle() const
{ return _title; }
void setTitle(const std::string& aTitle);
std::string getButton() const
{ return _buttonText; }
@ -53,7 +58,8 @@ public:
virtual ~FGFileDialog ();
virtual void exec() = 0;
virtual void close() = 0;
class Callback
{
public:
@ -64,9 +70,10 @@ public:
virtual void setCallback(Callback* aCB);
naRef openFromNasal(const nasal::CallContext& ctx);
naRef closeFromNasal(const nasal::CallContext& ctx);
naRef setCallbackFromNasal(const nasal::CallContext& ctx);
protected:
FGFileDialog(const std::string& aTitle, Usage use);
FGFileDialog(Usage use);
const Usage _usage;
std::string _title, _buttonText;

View file

@ -27,8 +27,8 @@ private:
PUIFileDialog* _dialog;
};
PUIFileDialog::PUIFileDialog(const std::string& aTitle, Usage use) :
FGFileDialog(aTitle, use),
PUIFileDialog::PUIFileDialog(Usage use) :
FGFileDialog(use),
_listener(NULL)
{
SG_LOG(SG_GENERAL, SG_INFO, "created PUIFileDialog");
@ -39,6 +39,7 @@ PUIFileDialog::~PUIFileDialog()
if (_listener) {
SGPropertyNode_ptr path = _dialogRoot->getNode("path");
path->removeChangeListener(_listener);
delete _listener;
}
}
@ -79,6 +80,13 @@ void PUIFileDialog::exec()
gui->showDialog(name);
}
void PUIFileDialog::close()
{
NewGUI* gui = static_cast<NewGUI*>(globals->get_subsystem("gui"));
std::string name("native-file-0");
gui->closeDialog(name);
}
void PUIFileDialog::pathChanged(const SGPath& aPath)
{
_callback->onFileDialogDone(this, aPath);

View file

@ -9,12 +9,12 @@
class PUIFileDialog : public FGFileDialog
{
public:
PUIFileDialog(const std::string& aTitle, FGFileDialog::Usage use);
PUIFileDialog(FGFileDialog::Usage use);
virtual ~PUIFileDialog();
virtual void exec();
virtual void close();
private:
class PathListener;
friend class PathListener;