Debian Codegraph @

src/GUI/CocoaFileDialog.mm

raw
1 // CocoaFileDialog.mm - Cocoa implementation of file-dialog interface
2 // SPDX-License-Identifier: GPL-2.0-or-later
3 // SPDX-FileCopyrightText: Copyright (C) 2013 James Turner - [email protected]
4
5 #include "CocoaFileDialog.hxx"
6 #include "GUI/FileDialog.hxx"
7 #include "simgear/debug/debug_types.h"
8
9 // Apple
10 #include <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
11
12 #include "GUI/FileDialog.hxx"
13 #include "simgear/debug/debug_types.h"
14 #include "simgear/math/sg_types.hxx"
15
16 #include <AppKit/NSSavePanel.h>
17 #include <AppKit/NSOpenPanel.h>
18
19 #include <osgViewer/Viewer>
20 #include <osgViewer/api/Cocoa/GraphicsWindowCocoa>
21
22 #include <simgear/debug/logstream.hxx>
23 #include <simgear/misc/strutils.hxx>
24
25 #include <GUI/CocoaHelpers_private.h>
26 #include <Main/globals.hxx>
27 #include <Main/fg_props.hxx>
28 #include <Viewer/renderer.hxx>
29
30 class CocoaFileDialog::CocoaFileDialogPrivate
31 {
32 public:
33 CocoaFileDialogPrivate() = default;
34
35 ~CocoaFileDialogPrivate() { [panel release]; }
36
37 NSSavePanel *panel = nil;
38
39 NSArray<UTType *> *buildContentTypes(const string_list &patterns) {
40 // important that we return an empty array (and not `nil`) if
41 // patterns is empty, since that corresponds to 'all types allowed'
42 auto *types = [[NSMutableArray<UTType *> new] autorelease];
43 for (auto p : patterns) {
44 if (!simgear::strutils::starts_with(p, "*.")) {
45 SG_LOG(SG_GUI, SG_ALERT, "can't use pattern on Cocoa:" << p);
46 continue;
47 }
48
49 auto cocoaFileExtension = stdStringToCocoa(p.substr(2));
50 auto *utType = [UTType typeWithFilenameExtension:cocoaFileExtension];
51 [types addObject:utType];
52 }
53
54 return types;
55 }
56 };
57
58 CocoaFileDialog::CocoaFileDialog(FGFileDialog::Usage use) :
59 FGFileDialog(use)
60 {
61 d.reset(new CocoaFileDialogPrivate);
62 if (use == USE_SAVE_FILE) {
63 d->panel = [NSSavePanel savePanel];
64 } else {
65 NSOpenPanel* openPanel = [NSOpenPanel openPanel];
66 d->panel = openPanel;
67
68 if (use == USE_CHOOSE_DIR) {
69 [openPanel setCanChooseDirectories:YES];
70 }
71 } // of USE_OPEN_FILE or USE_CHOOSE_DIR -> building NSOpenPanel
72
73 [d->panel retain];
74 }
75
76 CocoaFileDialog::~CocoaFileDialog()
77 {
78
79 }
80
81 void CocoaFileDialog::exec()
82 {
83 // find the native Cocoa NSWindow handle so we can parent the dialog and show
84 // it window-modal.
85 NSWindow* cocoaWindow = nil;
86 std::vector<osgViewer::GraphicsWindow*> windows;
87 globals->get_renderer()->getViewerBase()->getWindows(windows);
88
89 for (auto gw : windows) {
90 // OSG doesn't use RTTI, so no dynamic cast. Let's check the class type
91 // using OSG's own system, before we blindly static_cast<> and break
92 // everything.
93 if (strcmp(gw->className(), "GraphicsWindowCocoa")) {
94 continue;
95 }
96
97 osgViewer::GraphicsWindowCocoa* gwCocoa = static_cast<osgViewer::GraphicsWindowCocoa*>(gw);
98 cocoaWindow = (NSWindow*) gwCocoa->getWindow();
99 break;
100 }
101
102 // setup the panel fields now we have collected all the data
103 if (_usage == USE_SAVE_FILE) {
104 [d->panel setNameFieldStringValue:stdStringToCocoa(_placeholder)];
105 }
106
107 // if _filterPatterns is empty, we want an empty content types array to
108 // indicate 'any file type' as per the NSSavePanel docs
109 [d->panel setAllowedContentTypes:d->buildContentTypes(_filterPatterns)];
110 [d->panel setTitle:stdStringToCocoa(_title)];
111
112 if (_showHidden) {
113 [d->panel setShowsHiddenFiles:YES];
114 }
115
116 [d->panel setDirectoryURL: pathToNSURL(_initialPath)];
117
118 NSInteger result = [d->panel runModal];
119 if (result == NSModalResponseOK) {
120 NSString *nspath = [[d->panel URL] path];
121 // NSLog(@"the URL is: %@", d->panel URL]);
122 auto p = SGPath::fromUtf8([nspath UTF8String]);
123 handleSelectedPath(p);
124 }
125 }
126
127 void CocoaFileDialog::close()
128 {
129 [d->panel close];
130 }