]> git.mxchange.org Git - flightgear.git/blob - src/GUI/CocoaFileDialog.mm
Cocoa: use a window-sheet, and fire the callback.
[flightgear.git] / src / GUI / CocoaFileDialog.mm
1
2
3 #include "CocoaFileDialog.hxx"
4
5 // bring it all in!
6 #include <Cocoa/Cocoa.h>
7
8 #include <boost/foreach.hpp>
9
10 #include <osgViewer/Viewer>
11 #include <osgViewer/api/Cocoa/GraphicsWindowCocoa>
12
13 #include <simgear/debug/logstream.hxx>
14 #include <simgear/misc/strutils.hxx>
15
16 #include <Main/globals.hxx>
17 #include <Main/fg_props.hxx>
18 #include <Viewer/renderer.hxx>
19
20 static NSString* stdStringToCocoa(const std::string& s)
21 {
22     return [NSString stringWithUTF8String:s.c_str()];
23 }
24
25 static NSURL* pathToNSURL(const SGPath& aPath)
26 {
27     return [NSURL fileURLWithPath:stdStringToCocoa(aPath.str())];
28 }
29
30 class CocoaFileDialog::CocoaFileDialogPrivate
31 {
32 public:
33     CocoaFileDialogPrivate() :
34         panel(nil)
35     {
36         
37     }
38     
39     ~CocoaFileDialogPrivate()
40     {
41         [panel release];
42     }
43     
44     NSSavePanel* panel;
45 };
46
47 CocoaFileDialog::CocoaFileDialog(const std::string& aTitle, FGFileDialog::Usage use) :
48     FGFileDialog(aTitle, use)
49 {
50     d.reset(new CocoaFileDialogPrivate);
51     if (use == USE_SAVE_FILE) {
52         d->panel = [NSSavePanel savePanel];
53     } else {
54         NSOpenPanel* openPanel = [NSOpenPanel openPanel]; 
55         d->panel = openPanel;
56         
57         if (use == USE_CHOOSE_DIR) {
58             [openPanel setCanChooseDirectories:YES];
59         }
60     } // of USE_OPEN_FILE or USE_CHOOSE_DIR -> building NSOpenPanel
61 }
62
63 CocoaFileDialog::~CocoaFileDialog()
64 {
65     
66 }
67
68 void CocoaFileDialog::exec()
69 {
70 // find the native Cocoa NSWindow handle so we can parent the dialog and show
71 // it window-modal.
72     NSWindow* cocoaWindow = nil;
73     std::vector<osgViewer::GraphicsWindow*> windows;
74     globals->get_renderer()->getViewer()->getWindows(windows);
75     BOOST_FOREACH(osgViewer::GraphicsWindow* gw, windows) {
76         // OSG doesn't use RTTI, so no dynamic cast. Let's check the class type
77         // using OSG's own system, before we blindly static_cast<> and break
78         // everything.
79         if (strcmp(gw->className(), "GraphicsWindowCocoa")) {
80             continue; 
81         }
82             
83         osgViewer::GraphicsWindowCocoa* gwCocoa = static_cast<osgViewer::GraphicsWindowCocoa*>(gw);
84         cocoaWindow = (NSWindow*) gwCocoa->getWindow();
85         break;
86     }
87     
88 // setup the panel fields now we have collected all the data
89     if (_usage == USE_SAVE_FILE) {
90         [d->panel setNameFieldStringValue:stdStringToCocoa(_placeholder)];
91     }
92     
93     NSMutableArray* extensions = [NSMutableArray arrayWithCapacity:0];
94     BOOST_FOREACH(std::string ext, _filterPatterns) {
95         if (!simgear::strutils::starts_with(ext, "*.")) {
96             SG_LOG(SG_GENERAL, SG_INFO, "can't use pattern on Cococa:" << ext);
97             continue;
98         }
99         [extensions addObject:stdStringToCocoa(ext.substr(2))];
100     }
101
102     [d->panel setAllowedFileTypes:extensions];
103     [d->panel setTitle:stdStringToCocoa(_title)];
104     if (_showHidden) {
105         [d->panel setShowsHiddenFiles:YES];
106     }
107     
108     [d->panel setDirectoryURL: pathToNSURL(_initialPath)];
109     
110     [d->panel beginSheetModalForWindow:cocoaWindow completionHandler:^(NSInteger result)
111     {
112         if (result == NSFileHandlingPanelOKButton) {
113             NSString* path = [[d->panel URL] path];
114             //NSLog(@"the URL is: %@", d->panel URL]);
115             SGPath sgpath([path UTF8String]);
116             _callback->onFileDialogDone(this, sgpath);
117         }
118     }];
119 }