2012-12-28 14:48:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include "CocoaFileDialog.hxx"
|
|
|
|
|
|
|
|
// bring it all in!
|
|
|
|
#include <Cocoa/Cocoa.h>
|
|
|
|
|
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
2012-12-28 17:27:38 +00:00
|
|
|
#include <osgViewer/Viewer>
|
|
|
|
#include <osgViewer/api/Cocoa/GraphicsWindowCocoa>
|
|
|
|
|
2012-12-28 14:48:19 +00:00
|
|
|
#include <simgear/debug/logstream.hxx>
|
|
|
|
#include <simgear/misc/strutils.hxx>
|
|
|
|
|
|
|
|
#include <Main/globals.hxx>
|
|
|
|
#include <Main/fg_props.hxx>
|
2012-12-28 17:27:38 +00:00
|
|
|
#include <Viewer/renderer.hxx>
|
2012-12-28 14:48:19 +00:00
|
|
|
|
|
|
|
static NSString* stdStringToCocoa(const std::string& s)
|
|
|
|
{
|
|
|
|
return [NSString stringWithUTF8String:s.c_str()];
|
|
|
|
}
|
|
|
|
|
|
|
|
static NSURL* pathToNSURL(const SGPath& aPath)
|
|
|
|
{
|
|
|
|
return [NSURL fileURLWithPath:stdStringToCocoa(aPath.str())];
|
|
|
|
}
|
|
|
|
|
|
|
|
class CocoaFileDialog::CocoaFileDialogPrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CocoaFileDialogPrivate() :
|
|
|
|
panel(nil)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
~CocoaFileDialogPrivate()
|
|
|
|
{
|
|
|
|
[panel release];
|
|
|
|
}
|
|
|
|
|
|
|
|
NSSavePanel* panel;
|
|
|
|
};
|
|
|
|
|
2013-01-19 16:23:05 +00:00
|
|
|
CocoaFileDialog::CocoaFileDialog(FGFileDialog::Usage use) :
|
|
|
|
FGFileDialog(use)
|
2012-12-28 14:48:19 +00:00
|
|
|
{
|
|
|
|
d.reset(new CocoaFileDialogPrivate);
|
|
|
|
if (use == USE_SAVE_FILE) {
|
|
|
|
d->panel = [NSSavePanel savePanel];
|
|
|
|
} else {
|
2012-12-28 17:27:38 +00:00
|
|
|
NSOpenPanel* openPanel = [NSOpenPanel openPanel];
|
|
|
|
d->panel = openPanel;
|
|
|
|
|
|
|
|
if (use == USE_CHOOSE_DIR) {
|
|
|
|
[openPanel setCanChooseDirectories:YES];
|
|
|
|
}
|
|
|
|
} // of USE_OPEN_FILE or USE_CHOOSE_DIR -> building NSOpenPanel
|
2013-01-19 16:23:05 +00:00
|
|
|
|
|
|
|
[d->panel retain];
|
2012-12-28 14:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CocoaFileDialog::~CocoaFileDialog()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void CocoaFileDialog::exec()
|
|
|
|
{
|
2012-12-28 17:27:38 +00:00
|
|
|
// find the native Cocoa NSWindow handle so we can parent the dialog and show
|
|
|
|
// it window-modal.
|
|
|
|
NSWindow* cocoaWindow = nil;
|
|
|
|
std::vector<osgViewer::GraphicsWindow*> windows;
|
|
|
|
globals->get_renderer()->getViewer()->getWindows(windows);
|
|
|
|
BOOST_FOREACH(osgViewer::GraphicsWindow* gw, windows) {
|
|
|
|
// OSG doesn't use RTTI, so no dynamic cast. Let's check the class type
|
|
|
|
// using OSG's own system, before we blindly static_cast<> and break
|
|
|
|
// everything.
|
|
|
|
if (strcmp(gw->className(), "GraphicsWindowCocoa")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
osgViewer::GraphicsWindowCocoa* gwCocoa = static_cast<osgViewer::GraphicsWindowCocoa*>(gw);
|
|
|
|
cocoaWindow = (NSWindow*) gwCocoa->getWindow();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup the panel fields now we have collected all the data
|
2012-12-28 14:48:19 +00:00
|
|
|
if (_usage == USE_SAVE_FILE) {
|
|
|
|
[d->panel setNameFieldStringValue:stdStringToCocoa(_placeholder)];
|
|
|
|
}
|
|
|
|
|
2013-01-19 16:23:05 +00:00
|
|
|
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))];
|
2012-12-28 14:48:19 +00:00
|
|
|
}
|
|
|
|
|
2013-01-19 16:23:05 +00:00
|
|
|
[d->panel setAllowedFileTypes:extensions];
|
|
|
|
}
|
|
|
|
|
2012-12-28 14:48:19 +00:00
|
|
|
[d->panel setTitle:stdStringToCocoa(_title)];
|
|
|
|
if (_showHidden) {
|
|
|
|
[d->panel setShowsHiddenFiles:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
[d->panel setDirectoryURL: pathToNSURL(_initialPath)];
|
|
|
|
|
2012-12-28 17:27:38 +00:00
|
|
|
[d->panel beginSheetModalForWindow:cocoaWindow completionHandler:^(NSInteger result)
|
2012-12-28 14:48:19 +00:00
|
|
|
{
|
|
|
|
if (result == NSFileHandlingPanelOKButton) {
|
2012-12-28 17:27:38 +00:00
|
|
|
NSString* path = [[d->panel URL] path];
|
|
|
|
//NSLog(@"the URL is: %@", d->panel URL]);
|
|
|
|
SGPath sgpath([path UTF8String]);
|
|
|
|
_callback->onFileDialogDone(this, sgpath);
|
2012-12-28 14:48:19 +00:00
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
2013-01-19 16:23:05 +00:00
|
|
|
|
|
|
|
void CocoaFileDialog::close()
|
|
|
|
{
|
|
|
|
[d->panel close];
|
|
|
|
}
|
|
|
|
|