forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_selector.cpp
81 lines (66 loc) · 2.17 KB
/
file_selector.cpp
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
// Aseprite
// Copyright (C) 2020-2021 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/file_selector.h"
#include "app/app.h"
#include "app/pref/preferences.h"
#include "app/ui/file_selector.h"
#include "os/native_dialogs.h"
#include "os/system.h"
#include "os/window.h"
namespace app {
bool show_file_selector(
const std::string& title,
const std::string& initialPath,
const base::paths& extensions,
FileSelectorType type,
base::paths& output)
{
const std::string defExtension =
Preferences::instance().saveFile.defaultExtension();
if (Preferences::instance().experimental.useNativeFileDialog() &&
os::instance()->nativeDialogs()) {
os::FileDialogRef dlg =
os::instance()->nativeDialogs()->makeFileDialog();
if (dlg) {
dlg->setTitle(title);
dlg->setFileName(initialPath);
os::FileDialog::Type nativeType = os::FileDialog::Type::OpenFile;
switch (type) {
case FileSelectorType::Open:
nativeType = os::FileDialog::Type::OpenFile;
break;
case FileSelectorType::OpenMultiple:
nativeType = os::FileDialog::Type::OpenFiles;
break;
case FileSelectorType::Save:
nativeType = os::FileDialog::Type::SaveFile;
break;
}
dlg->setType(nativeType);
for (const auto& ext : extensions)
dlg->addFilter(ext, ext + " files (*." + ext + ")");
if (!defExtension.empty())
dlg->setDefaultExtension(defExtension);
bool res = dlg->show(os::instance()->defaultWindow());
if (res) {
if (type == FileSelectorType::OpenMultiple)
dlg->getMultipleFileNames(output);
else
output.push_back(dlg->fileName());
}
return res;
}
}
FileSelector fileSelector(type);
if (!defExtension.empty())
fileSelector.setDefaultExtension(defExtension);
return fileSelector.show(title, initialPath, extensions, output);
}
} // namespace app