Debian Codegraph @

android/app/jni/extern/nfd-modified/src/nfd_cocoa.mm

raw
1 /*
2 Native File Dialog
3
4 http://www.frogtoss.com/labs
5 */
6
7 #include <AppKit/AppKit.h>
8 #include "nfd.h"
9 #include "nfd_common.h"
10
11 // this language is a mess!
12 // who thought it was a good idea to combine Objective-C and C++ together
13 // when you could just have used C++ and call it a day!!!
14 //
15 // might as well make Objective-Ruswift++...
16
17 static NSArray *BuildAllowedFileTypes( const std::vector<std::string>& filterList )
18 {
19 // Commas and semicolons are the same thing on this platform
20
21 // like what about THIS INSTEAD!
22 // NSMutableArray *buildFilterList = NSMutableArray::alloc()->init();
23 NSMutableArray *buildFilterList = [[NSMutableArray alloc] init];
24
25 std::string typebuf;
26 int index=-1;
27 for (const std::string& i: filterList) {
28 index++;
29 if (!(index&1)) {
30 continue;
31 }
32 typebuf="";
33 for (const char& j: i) {
34 if (j==' ' || j==',' || j ==';') {
35 // or this: NSString::stringWithUTF8String(typebuf);
36 // buildFilterList->addObject(thisType);
37 // really? did you have to make this mess?!
38 const char* typebufC=typebuf.c_str();
39 NSString *thisType = [NSString stringWithUTF8String:typebufC];
40 [buildFilterList addObject:thisType];
41 typebuf="";
42 } else if (j!='.' && j!='*') {
43 typebuf+=j;
44 }
45 }
46 if (!typebuf.empty()) {
47 // I don't think this will work, but come on...
48 const char* typebufC=typebuf.c_str();
49 NSString *thisType = [NSString stringWithUTF8String:typebufC];
50 [buildFilterList addObject:thisType];
51 }
52 }
53
54 NSArray *returnArray = [NSArray arrayWithArray:buildFilterList];
55
56 [buildFilterList release];
57 return returnArray;
58 }
59
60 static void AddFilterListToDialog( NSSavePanel *dialog, const std::vector<std::string>& filterList )
61 {
62 if ( filterList.size()&1 )
63 return;
64
65 NSArray *allowedFileTypes = BuildAllowedFileTypes( filterList );
66 if ( [allowedFileTypes count] != 0 )
67 {
68 [dialog setAllowedFileTypes:allowedFileTypes];
69 }
70 }
71
72 static void SetDefaultPath( NSSavePanel *dialog, const nfdchar_t *defaultPath )
73 {
74 if ( !defaultPath || strlen(defaultPath) == 0 )
75 return;
76
77 NSString *defaultPathString = [NSString stringWithUTF8String: defaultPath];
78 NSURL *url = [NSURL fileURLWithPath:defaultPathString isDirectory:YES];
79 [dialog setDirectoryURL:url];
80 }
81
82
83 /* fixme: pathset should be pathSet */
84 static nfdresult_t AllocPathSet( NSArray *urls, nfdpathset_t *pathset )
85 {
86 assert(pathset);
87 assert([urls count]);
88
89 pathset->count = (size_t)[urls count];
90 pathset->indices = (size_t*)NFDi_Malloc( sizeof(size_t)*pathset->count );
91 if ( !pathset->indices )
92 {
93 return NFD_ERROR;
94 }
95
96 // count the total space needed for buf
97 size_t bufsize = 0;
98 for ( NSURL *url in urls )
99 {
100 NSString *path = [url path];
101 bufsize += [path lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;
102 }
103
104 pathset->buf = (nfdchar_t*)NFDi_Malloc( sizeof(nfdchar_t) * bufsize );
105 if ( !pathset->buf )
106 {
107 return NFD_ERROR;
108 }
109
110 // fill buf
111 nfdchar_t *p_buf = pathset->buf;
112 size_t count = 0;
113 for ( NSURL *url in urls )
114 {
115 NSString *path = [url path];
116 const nfdchar_t *utf8Path = [path UTF8String];
117 size_t byteLen = [path lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;
118 memcpy( p_buf, utf8Path, byteLen );
119
120 ptrdiff_t index = p_buf - pathset->buf;
121 assert( index >= 0 );
122 pathset->indices[count] = (size_t)index;
123
124 p_buf += byteLen;
125 ++count;
126 }
127
128 return NFD_OKAY;
129 }
130
131 /* public */
132
133
134 nfdresult_t NFD_OpenDialog( const std::vector<std::string>& filterList,
135 const nfdchar_t *defaultPath,
136 nfdchar_t **outPath,
137 nfdselcallback_t selCallback,
138 const nfdchar_t *defaultFileName )
139 {
140 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
141
142 NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
143 NSOpenPanel *dialog = [NSOpenPanel openPanel];
144 [dialog setAllowsMultipleSelection:NO];
145
146 // Build the filter list
147 AddFilterListToDialog(dialog, filterList);
148
149 // Set the starting directory
150 SetDefaultPath(dialog, defaultPath);
151
152 // Set the file name
153 if (defaultFileName!=NULL) {
154 NSString* defFileName=[NSString stringWithUTF8String:defaultFileName];
155 [dialog setNameFieldStringValue:defFileName];
156 }
157
158 nfdresult_t nfdResult = NFD_CANCEL;
159 if ( [dialog runModal] == NSModalResponseOK )
160 {
161 NSURL *url = [dialog URL];
162 const char *utf8Path = [[url path] UTF8String];
163
164 // byte count, not char count
165 size_t len = strlen(utf8Path);//NFDi_UTF8_Strlen(utf8Path);
166
167 *outPath = (nfdchar_t*)NFDi_Malloc( len+1 );
168 if ( !*outPath )
169 {
170 [pool release];
171 [keyWindow makeKeyAndOrderFront:nil];
172 return NFD_ERROR;
173 }
174 memcpy( *outPath, utf8Path, len+1 ); /* copy null term */
175 nfdResult = NFD_OKAY;
176 }
177 [pool release];
178
179 [keyWindow makeKeyAndOrderFront:nil];
180 return nfdResult;
181 }
182
183
184 nfdresult_t NFD_OpenDialogMultiple( const std::vector<std::string>& filterList,
185 const nfdchar_t *defaultPath,
186 nfdpathset_t *outPaths,
187 nfdselcallback_t selCallback,
188 const nfdchar_t *defaultFileName )
189 {
190 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
191 NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
192
193 NSOpenPanel *dialog = [NSOpenPanel openPanel];
194 [dialog setAllowsMultipleSelection:YES];
195
196 // Build the fiter list.
197 AddFilterListToDialog(dialog, filterList);
198
199 // Set the starting directory
200 SetDefaultPath(dialog, defaultPath);
201
202 // Set the file name
203 if (defaultFileName!=NULL) {
204 NSString* defFileName=[NSString stringWithUTF8String:defaultFileName];
205 [dialog setNameFieldStringValue:defFileName];
206 }
207
208 nfdresult_t nfdResult = NFD_CANCEL;
209 if ( [dialog runModal] == NSModalResponseOK )
210 {
211 NSArray *urls = [dialog URLs];
212
213 if ( [urls count] == 0 )
214 {
215 [pool release];
216 [keyWindow makeKeyAndOrderFront:nil];
217 return NFD_CANCEL;
218 }
219
220 if ( AllocPathSet( urls, outPaths ) == NFD_ERROR )
221 {
222 [pool release];
223 [keyWindow makeKeyAndOrderFront:nil];
224 return NFD_ERROR;
225 }
226
227 nfdResult = NFD_OKAY;
228 }
229 [pool release];
230
231 [keyWindow makeKeyAndOrderFront:nil];
232 return nfdResult;
233 }
234
235
236 nfdresult_t NFD_SaveDialog( const std::vector<std::string>& filterList,
237 const nfdchar_t *defaultPath,
238 nfdchar_t **outPath,
239 nfdselcallback_t selCallback,
240 const nfdchar_t *defaultFileName )
241 {
242 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
243 NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
244
245 NSSavePanel *dialog = [NSSavePanel savePanel];
246 [dialog setExtensionHidden:NO];
247
248 // Build the filter list.
249 AddFilterListToDialog(dialog, filterList);
250
251 // Set the starting directory
252 SetDefaultPath(dialog, defaultPath);
253
254 // Set the file name
255 if (defaultFileName!=NULL) {
256 NSString* defFileName=[NSString stringWithUTF8String:defaultFileName];
257 [dialog setNameFieldStringValue:defFileName];
258 }
259
260 nfdresult_t nfdResult = NFD_CANCEL;
261 if ( [dialog runModal] == NSModalResponseOK )
262 {
263 NSURL *url = [dialog URL];
264 const char *utf8Path = [[url path] UTF8String];
265
266 size_t byteLen = [url.path lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;
267
268 *outPath = (nfdchar_t*)NFDi_Malloc( byteLen );
269 if ( !*outPath )
270 {
271 [pool release];
272 [keyWindow makeKeyAndOrderFront:nil];
273 return NFD_ERROR;
274 }
275 memcpy( *outPath, utf8Path, byteLen );
276 nfdResult = NFD_OKAY;
277 }
278
279 [pool release];
280 [keyWindow makeKeyAndOrderFront:nil];
281 return nfdResult;
282 }
283
284 nfdresult_t NFD_PickFolder(const nfdchar_t *defaultPath,
285 nfdchar_t **outPath)
286 {
287 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
288
289 NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
290 NSOpenPanel *dialog = [NSOpenPanel openPanel];
291 [dialog setAllowsMultipleSelection:NO];
292 [dialog setCanChooseDirectories:YES];
293 [dialog setCanCreateDirectories:YES];
294 [dialog setCanChooseFiles:NO];
295
296 // Set the starting directory
297 SetDefaultPath(dialog, defaultPath);
298
299 nfdresult_t nfdResult = NFD_CANCEL;
300 if ( [dialog runModal] == NSModalResponseOK )
301 {
302 NSURL *url = [dialog URL];
303 const char *utf8Path = [[url path] UTF8String];
304
305 // byte count, not char count
306 size_t len = strlen(utf8Path);//NFDi_UTF8_Strlen(utf8Path);
307
308 *outPath = (nfdchar_t*)NFDi_Malloc( len+1 );
309 if ( !*outPath )
310 {
311 [pool release];
312 [keyWindow makeKeyAndOrderFront:nil];
313 return NFD_ERROR;
314 }
315 memcpy( *outPath, utf8Path, len+1 ); /* copy null term */
316 nfdResult = NFD_OKAY;
317 }
318 [pool release];
319
320 [keyWindow makeKeyAndOrderFront:nil];
321 return nfdResult;
322 }