/
filedialog_macx.mm
116 lines (99 loc) · 3.35 KB
/
filedialog_macx.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/** @file filedialog_macx.mm Native file chooser dialog.
*
* @authors Copyright (c) 2018 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de/FileDialog"
#import <Cocoa/Cocoa.h>
namespace de {
DE_PIMPL_NOREF(FileDialog)
{
String title = "Select File";
String prompt = "OK";
Behaviors behavior = AcceptFiles;
List<NativePath> selection;
NativePath initialLocation;
FileTypes fileTypes; // empty list: eveything allowed
};
FileDialog::FileDialog() : d(new Impl)
{}
void FileDialog::setTitle(const String &title)
{
d->title = title;
}
void FileDialog::setPrompt(const String &prompt)
{
d->prompt = prompt;
}
void FileDialog::setBehavior(Behaviors behaviors, FlagOp flagOp)
{
applyFlagOperation(d->behavior, behaviors, flagOp);
}
void FileDialog::setInitialLocation(const NativePath &initialLocation)
{
d->initialLocation = initialLocation;
}
void FileDialog::setFileTypes(const FileTypes &fileTypes)
{
d->fileTypes = fileTypes;
}
NativePath FileDialog::selectedPath() const
{
return d->selection ? d->selection.front() : NativePath();
}
List<NativePath> FileDialog::selectedPaths() const
{
return d->selection;
}
bool FileDialog::exec(GuiRootWidget &)
{
d->selection.clear();
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
[openDlg setCanChooseFiles:(d->behavior.testFlag(AcceptFiles) ? YES : NO)];
[openDlg setCanChooseDirectories:(d->behavior.testFlag(AcceptDirectories) ? YES : NO)];
[openDlg setAllowsMultipleSelection:(d->behavior.testFlag(MultipleSelection) ? YES : NO)];
[openDlg setDirectoryURL:
[NSURL fileURLWithPath:(NSString * _Nonnull)
[NSString stringWithUTF8String:d->initialLocation.c_str()]]];
[openDlg setMessage:[NSString stringWithUTF8String:d->title.c_str()]];
[openDlg setPrompt:[NSString stringWithUTF8String:d->prompt.c_str()]];
// The allowed file types.
{
NSMutableArray<NSString *> *types = nil;
if (d->fileTypes)
{
types = [NSMutableArray<NSString *> array];
for (const auto &type : d->fileTypes)
{
for (const auto &ext : type.extensions)
{
[types addObject:(NSString * _Nonnull)[NSString stringWithUTF8String:ext.c_str()]];
}
}
}
[openDlg setAllowedFileTypes:types];
}
if ([openDlg runModal] == NSModalResponseOK)
{
// Check the selected paths.
for (NSURL *url in [openDlg URLs])
{
d->selection << [url.path UTF8String];
}
return true;
}
return false;
}
} // namespace de