3 #include "CocoaFileDialog.hxx"
6 #include <Cocoa/Cocoa.h>
8 #include <boost/foreach.hpp>
10 #include <simgear/debug/logstream.hxx>
11 #include <simgear/misc/strutils.hxx>
13 #include <Main/globals.hxx>
14 #include <Main/fg_props.hxx>
16 static NSString* stdStringToCocoa(const std::string& s)
18 return [NSString stringWithUTF8String:s.c_str()];
21 static NSURL* pathToNSURL(const SGPath& aPath)
23 return [NSURL fileURLWithPath:stdStringToCocoa(aPath.str())];
26 class CocoaFileDialog::CocoaFileDialogPrivate
29 CocoaFileDialogPrivate() :
35 ~CocoaFileDialogPrivate()
43 CocoaFileDialog::CocoaFileDialog(const std::string& aTitle, FGFileDialog::Usage use) :
44 FGFileDialog(aTitle, use)
46 d.reset(new CocoaFileDialogPrivate);
47 if (use == USE_SAVE_FILE) {
48 d->panel = [NSSavePanel savePanel];
50 d->panel = [NSOpenPanel openPanel];
53 if (use == USE_CHOOSE_DIR) {
54 [d->panel setCanChooseDirectories:YES];
58 CocoaFileDialog::~CocoaFileDialog()
63 void CocoaFileDialog::exec()
65 if (_usage == USE_SAVE_FILE) {
66 [d->panel setNameFieldStringValue:stdStringToCocoa(_placeholder)];
69 NSMutableArray* extensions = [NSMutableArray arrayWithCapacity:0];
70 BOOST_FOREACH(std::string ext, _filterPatterns) {
71 if (!simgear::strutils::starts_with(ext, "*.")) {
72 SG_LOG(SG_GENERAL, SG_INFO, "can't use pattern on Cococa:" << ext);
75 [extensions addObject:stdStringToCocoa(ext.substr(2))];
78 [d->panel setAllowedFileTypes:extensions];
79 [d->panel setTitle:stdStringToCocoa(_title)];
81 [d->panel setShowsHiddenFiles:YES];
84 [d->panel setDirectoryURL: pathToNSURL(_initialPath)];
86 [d->panel beginWithCompletionHandler:^(NSInteger result)
88 if (result == NSFileHandlingPanelOKButton) {
89 NSURL* theDoc = [d->panel URL];
90 NSLog(@"the URL is: %@", theDoc);