Debian Codegraph @

platform/macos/godot_open_save_delegate.mm

raw
1 /**************************************************************************/
2 /* godot_open_save_delegate.mm */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #import "godot_open_save_delegate.h"
32
33 @implementation GodotOpenSaveDelegate
34
35 - (instancetype)init {
36 self = [super init];
37 if ((self = [super init])) {
38 dialog = nullptr;
39 cur_index = 0;
40 ctr_id = 1;
41 allowed_types = nullptr;
42 root = String();
43 }
44 return self;
45 }
46
47 - (void)makeAccessoryView:(NSSavePanel *)p_panel filters:(const Vector<String> &)p_filters options:(const TypedArray<Dictionary> &)p_options {
48 dialog = p_panel;
49
50 NSMutableArray *constraints = [NSMutableArray array];
51
52 NSView *base_view = [[NSView alloc] initWithFrame:NSZeroRect];
53 base_view.translatesAutoresizingMaskIntoConstraints = NO;
54
55 NSGridView *view = [NSGridView gridViewWithNumberOfColumns:2 rows:0];
56 view.translatesAutoresizingMaskIntoConstraints = NO;
57 view.columnSpacing = 10;
58 view.rowSpacing = 10;
59 view.rowAlignment = NSGridRowAlignmentLastBaseline;
60
61 int option_count = 0;
62
63 for (int i = 0; i < p_options.size(); i++) {
64 const Dictionary &item = p_options[i];
65 if (!item.has("name") || !item.has("values") || !item.has("default")) {
66 continue;
67 }
68 const String &name = item["name"];
69 const Vector<String> &values = item["values"];
70 int default_idx = item["default"];
71
72 NSTextField *label = [NSTextField labelWithString:[NSString stringWithUTF8String:name.utf8().get_data()]];
73 if (@available(macOS 10.14, *)) {
74 label.textColor = NSColor.secondaryLabelColor;
75 }
76 if (@available(macOS 11.10, *)) {
77 label.font = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
78 }
79
80 NSView *popup = nullptr;
81 if (values.is_empty()) {
82 NSButton *popup_check = [NSButton checkboxWithTitle:@"" target:self action:@selector(popupCheckAction:)];
83 int tag = [self setDefaultBool:name value:(bool)default_idx];
84 popup_check.state = (default_idx) ? NSControlStateValueOn : NSControlStateValueOff;
85 popup_check.tag = tag;
86 popup = popup_check;
87 } else {
88 NSPopUpButton *popup_list = [[NSPopUpButton alloc] initWithFrame:NSZeroRect pullsDown:NO];
89 for (int i = 0; i < values.size(); i++) {
90 [popup_list addItemWithTitle:[NSString stringWithUTF8String:values[i].utf8().get_data()]];
91 }
92 int tag = [self setDefaultInt:name value:default_idx];
93 [popup_list selectItemAtIndex:default_idx];
94 popup_list.tag = tag;
95 popup_list.target = self;
96 popup_list.action = @selector(popupOptionAction:);
97 popup = popup_list;
98 }
99
100 [view addRowWithViews:[NSArray arrayWithObjects:label, popup, nil]];
101
102 option_count++;
103 }
104
105 NSMutableArray *new_allowed_types = [[NSMutableArray alloc] init];
106 Vector<Vector<String>> pref_types;
107 bool has_type_popup = false;
108 {
109 NSTextField *label = [NSTextField labelWithString:[NSString stringWithUTF8String:RTR("Format").utf8().get_data()]];
110 if (@available(macOS 10.14, *)) {
111 label.textColor = NSColor.secondaryLabelColor;
112 }
113 if (@available(macOS 11.10, *)) {
114 label.font = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
115 }
116
117 if (p_filters.size() > 1) {
118 NSPopUpButton *popup = [[NSPopUpButton alloc] initWithFrame:NSZeroRect pullsDown:NO];
119 for (int i = 0; i < p_filters.size(); i++) {
120 Vector<String> tokens = p_filters[i].split(";");
121 Vector<String> pref_type;
122 if (tokens.size() >= 1) {
123 String flt = tokens[0].strip_edges();
124 String mime = (tokens.size() >= 3) ? tokens[2].strip_edges() : String();
125 int filter_slice_count = flt.get_slice_count(",");
126
127 NSMutableArray *type_filters = [[NSMutableArray alloc] init];
128 for (int j = 0; j < filter_slice_count; j++) {
129 String str = (flt.get_slicec(',', j).strip_edges());
130 if (!str.is_empty()) {
131 if (@available(macOS 11, *)) {
132 UTType *ut = nullptr;
133 if (str == "*.*") {
134 ut = UTTypeData;
135 } else {
136 ut = [UTType typeWithFilenameExtension:[NSString stringWithUTF8String:str.replace("*.", "").strip_edges().utf8().get_data()]];
137 }
138 if (ut) {
139 [type_filters addObject:ut];
140 pref_type.push_back(str.replace("*.", "").strip_edges());
141 }
142 } else {
143 [type_filters addObject:[NSString stringWithUTF8String:str.replace("*.", "").strip_edges().utf8().get_data()]];
144 pref_type.push_back(str.replace("*.", "").strip_edges());
145 }
146 }
147 }
148
149 if (@available(macOS 11, *)) {
150 filter_slice_count = mime.get_slice_count(",");
151 for (int j = 0; j < filter_slice_count; j++) {
152 String str = mime.get_slicec(',', j).strip_edges();
153 if (!str.is_empty()) {
154 UTType *ut = [UTType typeWithMIMEType:[NSString stringWithUTF8String:str.strip_edges().utf8().get_data()]];
155 if (ut) {
156 [type_filters addObject:ut];
157 }
158 }
159 }
160 }
161
162 if ([type_filters count] > 0) {
163 NSString *name_str = [NSString stringWithUTF8String:((tokens.size() == 1) ? tokens[0] : tokens[1].strip_edges()).utf8().get_data()];
164 [new_allowed_types addObject:type_filters];
165 pref_types.push_back(pref_type);
166 [popup addItemWithTitle:name_str];
167 }
168 }
169 }
170 if (popup.numberOfItems > 1) {
171 has_type_popup = true;
172 popup.target = self;
173 popup.action = @selector(popupFileAction:);
174
175 [view addRowWithViews:[NSArray arrayWithObjects:label, popup, nil]];
176 }
177 } else if (p_filters.size() == 1) {
178 Vector<String> tokens = p_filters[0].split(";");
179 Vector<String> pref_type;
180 if (tokens.size() >= 1) {
181 String flt = tokens[0].strip_edges();
182 String mime = (tokens.size() >= 3) ? tokens[2] : String();
183 int filter_slice_count = flt.get_slice_count(",");
184
185 NSMutableArray *type_filters = [[NSMutableArray alloc] init];
186 for (int j = 0; j < filter_slice_count; j++) {
187 String str = (flt.get_slicec(',', j).strip_edges());
188 if (!str.is_empty()) {
189 if (@available(macOS 11, *)) {
190 UTType *ut = nullptr;
191 if (str == "*.*") {
192 ut = UTTypeData;
193 } else {
194 ut = [UTType typeWithFilenameExtension:[NSString stringWithUTF8String:str.replace("*.", "").strip_edges().utf8().get_data()]];
195 }
196 if (ut) {
197 [type_filters addObject:ut];
198 pref_type.push_back(str.replace("*.", "").strip_edges());
199 }
200 } else {
201 [type_filters addObject:[NSString stringWithUTF8String:str.replace("*.", "").strip_edges().utf8().get_data()]];
202 pref_type.push_back(str.replace("*.", "").strip_edges());
203 }
204 }
205 }
206 if (@available(macOS 11, *)) {
207 filter_slice_count = mime.get_slice_count(",");
208 for (int j = 0; j < filter_slice_count; j++) {
209 String str = mime.get_slicec(',', j).strip_edges();
210 if (!str.is_empty()) {
211 UTType *ut = [UTType typeWithMIMEType:[NSString stringWithUTF8String:str.strip_edges().utf8().get_data()]];
212 if (ut) {
213 [type_filters addObject:ut];
214 }
215 }
216 }
217 }
218
219 if ([type_filters count] > 0) {
220 [new_allowed_types addObject:type_filters];
221 pref_types.push_back(pref_type);
222 }
223 }
224 }
225 [self setFileTypes:new_allowed_types pref:pref_types];
226 }
227
228 [base_view addSubview:view];
229 [constraints addObject:[view.topAnchor constraintEqualToAnchor:base_view.topAnchor constant:10]];
230 [constraints addObject:[base_view.bottomAnchor constraintEqualToAnchor:view.bottomAnchor constant:10]];
231 [constraints addObject:[base_view.centerXAnchor constraintEqualToAnchor:view.centerXAnchor constant:10]];
232 [NSLayoutConstraint activateConstraints:constraints];
233
234 if (option_count > 0 || has_type_popup) {
235 [p_panel setAccessoryView:base_view];
236 }
237 if ([new_allowed_types count] > 0) {
238 NSMutableArray *type_filters = [new_allowed_types objectAtIndex:0];
239 if (@available(macOS 11, *)) {
240 if (type_filters && [type_filters count] == 1 && [type_filters objectAtIndex:0] == UTTypeData) {
241 [p_panel setAllowedContentTypes:@[ UTTypeData ]];
242 [p_panel setAllowsOtherFileTypes:true];
243 } else {
244 [p_panel setAllowsOtherFileTypes:false];
245 [p_panel setAllowedContentTypes:type_filters];
246 }
247 } else {
248 if (type_filters && [type_filters count] == 1 && [[type_filters objectAtIndex:0] isEqualToString:@"*"]) {
249 [p_panel setAllowedFileTypes:nil];
250 [p_panel setAllowsOtherFileTypes:true];
251 } else {
252 [p_panel setAllowsOtherFileTypes:false];
253 [p_panel setAllowedFileTypes:type_filters];
254 }
255 }
256 } else {
257 if (@available(macOS 11, *)) {
258 [p_panel setAllowedContentTypes:@[ UTTypeData ]];
259 } else {
260 [p_panel setAllowedFileTypes:nil];
261 }
262 [p_panel setAllowsOtherFileTypes:true];
263 }
264 }
265
266 - (int)getIndex {
267 return cur_index;
268 }
269
270 - (Dictionary)getSelection {
271 return options;
272 }
273
274 - (int)setDefaultInt:(const String &)p_name value:(int)p_value {
275 int cid = ctr_id++;
276 options[p_name] = p_value;
277 ctr_ids[cid] = p_name;
278
279 return cid;
280 }
281
282 - (int)setDefaultBool:(const String &)p_name value:(bool)p_value {
283 int cid = ctr_id++;
284 options[p_name] = p_value;
285 ctr_ids[cid] = p_name;
286
287 return cid;
288 }
289
290 - (void)setFileTypes:(NSMutableArray *)p_allowed_types pref:(const Vector<Vector<String>> &)p_preftypes {
291 allowed_types = p_allowed_types;
292 preferred_types = p_preftypes;
293 }
294
295 - (instancetype)initWithDialog:(NSSavePanel *)p_dialog {
296 if ((self = [super init])) {
297 dialog = p_dialog;
298 cur_index = 0;
299 ctr_id = 1;
300 allowed_types = nullptr;
301 }
302 return self;
303 }
304
305 - (void)popupCheckAction:(id)p_sender {
306 NSButton *btn = p_sender;
307 if (btn && ctr_ids.has(btn.tag)) {
308 options[ctr_ids[btn.tag]] = ([btn state] == NSControlStateValueOn);
309 }
310 }
311
312 - (void)popupOptionAction:(id)p_sender {
313 NSPopUpButton *btn = p_sender;
314 if (btn && ctr_ids.has(btn.tag)) {
315 options[ctr_ids[btn.tag]] = (int)[btn indexOfSelectedItem];
316 }
317 }
318
319 - (void)popupFileAction:(id)p_sender {
320 NSPopUpButton *btn = p_sender;
321 if (btn) {
322 NSUInteger index = [btn indexOfSelectedItem];
323 if (allowed_types && index < [allowed_types count]) {
324 NSMutableArray *type_filters = [allowed_types objectAtIndex:index];
325 if (@available(macOS 11, *)) {
326 if (type_filters && [type_filters count] == 1 && [type_filters objectAtIndex:0] == UTTypeData) {
327 [dialog setAllowedContentTypes:@[ UTTypeData ]];
328 [dialog setAllowsOtherFileTypes:true];
329 } else {
330 [dialog setAllowsOtherFileTypes:false];
331 [dialog setAllowedContentTypes:type_filters];
332 }
333 } else {
334 if (type_filters && [type_filters count] == 1 && [[type_filters objectAtIndex:0] isEqualToString:@"*"]) {
335 [dialog setAllowedFileTypes:nil];
336 [dialog setAllowsOtherFileTypes:true];
337 } else {
338 [dialog setAllowsOtherFileTypes:false];
339 [dialog setAllowedFileTypes:type_filters];
340 }
341 }
342 cur_index = index;
343 } else {
344 if (@available(macOS 11, *)) {
345 [dialog setAllowedContentTypes:@[ UTTypeData ]];
346 } else {
347 [dialog setAllowedFileTypes:nil];
348 }
349 [dialog setAllowsOtherFileTypes:true];
350 cur_index = -1;
351 }
352 }
353 }
354
355 - (void)setRootPath:(const String &)p_root_path {
356 root = p_root_path;
357 }
358
359 - (String)validateFilename:(const String &)p_path {
360 if (@available(macOS 11, *)) {
361 if (allowed_types) {
362 NSMutableArray *type_filters = [allowed_types objectAtIndex:cur_index];
363 UTType *ut = [type_filters objectAtIndex:0];
364 String ext = String::utf8([[ut preferredFilenameExtension] UTF8String]);
365 Vector<String> pref_ext = preferred_types[cur_index];
366 if (!pref_ext.is_empty() && !pref_ext.has(ext) && p_path.has_extension(ext) && pref_ext[0] != "*") {
367 return p_path.get_basename() + "." + pref_ext[0];
368 }
369 }
370 }
371 return p_path;
372 }
373
374 - (BOOL)panel:(id)sender validateURL:(NSURL *)url error:(NSError *_Nullable *)outError {
375 if (root.is_empty()) {
376 return YES;
377 }
378
379 NSString *ns_path = url.URLByStandardizingPath.URLByResolvingSymlinksInPath.path;
380 String path = String::utf8([ns_path UTF8String]).simplify_path();
381 if (!path.begins_with(root.simplify_path())) {
382 return NO;
383 }
384
385 return YES;
386 }
387
388 @end