1 | // Aseprite |
---|---|
2 | // Copyright (C) 2020-2021 Igara Studio S.A. |
3 | // Copyright (C) 2001-2018 David Capello |
4 | // |
5 | // This program is distributed under the terms of |
6 | // the End-User License Agreement for Aseprite. |
7 | |
8 | #ifdef HAVE_CONFIG_H |
9 | #include "config.h" |
10 | #endif |
11 | |
12 | #include "app/file_selector.h" |
13 | |
14 | #include "app/app.h" |
15 | #include "app/pref/preferences.h" |
16 | #include "app/ui/file_selector.h" |
17 | #include "os/native_dialogs.h" |
18 | #include "os/system.h" |
19 | #include "os/window.h" |
20 | |
21 | namespace app { |
22 | |
23 | bool show_file_selector( |
24 | const std::string& title, |
25 | const std::string& initialPath, |
26 | const base::paths& extensions, |
27 | FileSelectorType type, |
28 | base::paths& output) |
29 | { |
30 | const std::string defExtension = |
31 | Preferences::instance().saveFile.defaultExtension(); |
32 | |
33 | if (Preferences::instance().experimental.useNativeFileDialog() && |
34 | os::instance()->nativeDialogs()) { |
35 | os::FileDialogRef dlg = |
36 | os::instance()->nativeDialogs()->makeFileDialog(); |
37 | |
38 | if (dlg) { |
39 | dlg->setTitle(title); |
40 | dlg->setFileName(initialPath); |
41 | |
42 | os::FileDialog::Type nativeType = os::FileDialog::Type::OpenFile; |
43 | switch (type) { |
44 | case FileSelectorType::Open: |
45 | nativeType = os::FileDialog::Type::OpenFile; |
46 | break; |
47 | case FileSelectorType::OpenMultiple: |
48 | nativeType = os::FileDialog::Type::OpenFiles; |
49 | break; |
50 | case FileSelectorType::Save: |
51 | nativeType = os::FileDialog::Type::SaveFile; |
52 | break; |
53 | } |
54 | dlg->setType(nativeType); |
55 | |
56 | for (const auto& ext : extensions) |
57 | dlg->addFilter(ext, ext + " files (*."+ ext + ")"); |
58 | |
59 | if (!defExtension.empty()) |
60 | dlg->setDefaultExtension(defExtension); |
61 | |
62 | bool res = dlg->show(os::instance()->defaultWindow()); |
63 | if (res) { |
64 | if (type == FileSelectorType::OpenMultiple) |
65 | dlg->getMultipleFileNames(output); |
66 | else |
67 | output.push_back(dlg->fileName()); |
68 | } |
69 | return res; |
70 | } |
71 | } |
72 | |
73 | FileSelector fileSelector(type); |
74 | |
75 | if (!defExtension.empty()) |
76 | fileSelector.setDefaultExtension(defExtension); |
77 | |
78 | return fileSelector.show(title, initialPath, extensions, output); |
79 | } |
80 | |
81 | } // namespace app |
82 |