yaze 0.2.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
file_dialog.mm
Go to the documentation of this file.
2
3#include <iostream>
4#include <string>
5#include <vector>
6
7#if defined(__APPLE__) && defined(__MACH__)
8/* Apple OSX and iOS (Darwin). */
9#include <Foundation/Foundation.h>
10#include <TargetConditionals.h>
11
12#import <CoreText/CoreText.h>
13
14#if TARGET_IPHONE_SIMULATOR == 1 || TARGET_OS_IPHONE == 1
15/* iOS in Xcode simulator */
16#import <UIKit/UIKit.h>
17#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
18
20
21namespace {
22static std::string selectedFile;
23
24void ShowOpenFileDialogImpl(void (^completionHandler)(std::string)) {
25 AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
26 [appDelegate PresentDocumentPickerWithCompletionHandler:^(NSString *filePath) {
27 selectedFile = std::string([filePath UTF8String]);
28 completionHandler(selectedFile);
29 }];
30}
31
32std::string ShowOpenFileDialogSync() {
33 __block std::string result;
34
35 ShowOpenFileDialogImpl(^(std::string filePath) {
36 result = filePath;
37 });
38
39 return result;
40}
41} // namespace
42
43std::string yaze::core::FileDialogWrapper::ShowOpenFileDialog() { return ShowOpenFileDialogSync(); }
44
46
48 const std::string &folder) {
49 return {};
50}
51
53 const std::string &folder) {
54 return {};
55}
56
58 NSBundle* bundle = [NSBundle mainBundle];
59 NSString* resourceDirectoryPath = [bundle bundlePath];
60 NSString* path = [resourceDirectoryPath stringByAppendingString:@"/"];
61 return [path UTF8String];
62}
63
64#elif TARGET_OS_MAC == 1
65/* macOS */
66
67#import <Cocoa/Cocoa.h>
68
70 NSOpenPanel* openPanel = [NSOpenPanel openPanel];
71 [openPanel setCanChooseFiles:YES];
72 [openPanel setCanChooseDirectories:NO];
73 [openPanel setAllowsMultipleSelection:NO];
74
75 if ([openPanel runModal] == NSModalResponseOK) {
76 NSURL* url = [[openPanel URLs] objectAtIndex:0];
77 NSString* path = [url path];
78 return std::string([path UTF8String]);
79 }
80
81 return "";
82}
83
85 NSOpenPanel* openPanel = [NSOpenPanel openPanel];
86 [openPanel setCanChooseFiles:NO];
87 [openPanel setCanChooseDirectories:YES];
88 [openPanel setAllowsMultipleSelection:NO];
89
90 if ([openPanel runModal] == NSModalResponseOK) {
91 NSURL* url = [[openPanel URLs] objectAtIndex:0];
92 NSString* path = [url path];
93 return std::string([path UTF8String]);
94 }
95
96 return "";
97}
98
100 const std::string& folder) {
101 std::vector<std::string> filenames;
102 NSFileManager* fileManager = [NSFileManager defaultManager];
103 NSDirectoryEnumerator* enumerator =
104 [fileManager enumeratorAtPath:[NSString stringWithUTF8String:folder.c_str()]];
105 NSString* file;
106 while (file = [enumerator nextObject]) {
107 if ([file hasPrefix:@"."]) {
108 continue;
109 }
110 filenames.push_back(std::string([file UTF8String]));
111 }
112 return filenames;
113}
114
116 const std::string& folder) {
117 std::vector<std::string> subdirectories;
118 NSFileManager* fileManager = [NSFileManager defaultManager];
119 NSDirectoryEnumerator* enumerator =
120 [fileManager enumeratorAtPath:[NSString stringWithUTF8String:folder.c_str()]];
121 NSString* file;
122 while (file = [enumerator nextObject]) {
123 if ([file hasPrefix:@"."]) {
124 continue;
125 }
126 BOOL isDirectory;
127 NSString* path =
128 [NSString stringWithFormat:@"%@/%@", [NSString stringWithUTF8String:folder.c_str()], file];
129 [fileManager fileExistsAtPath:path isDirectory:&isDirectory];
130 if (isDirectory) {
131 subdirectories.push_back(std::string([file UTF8String]));
132 }
133 }
134 return subdirectories;
135}
136
138 NSBundle* bundle = [NSBundle mainBundle];
139 NSString* resourceDirectoryPath = [bundle bundlePath];
140 NSString* path = [resourceDirectoryPath stringByAppendingString:@"/"];
141 return [path UTF8String];
142}
143
144#else
145// Unsupported platform
146#endif // TARGET_OS_MAC
147
148#endif // __APPLE__ && __MACH__
static std::vector< std::string > GetFilesInFolder(const std::string &folder_path)
static std::string ShowOpenFileDialog()
ShowOpenFileDialog opens a file dialog and returns the selected filepath.
static std::string ShowOpenFolderDialog()
ShowOpenFolderDialog opens a file dialog and returns the selected folder path.
static std::vector< std::string > GetSubdirectoriesInFolder(const std::string &folder_path)
std::string GetBundleResourcePath()
GetBundleResourcePath returns the path to the bundle resource directory. Specific to MacOS.