filedialog 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/gempush.yml +31 -0
  3. data/.gitignore +13 -0
  4. data/.gitmodules +3 -0
  5. data/.rubocop.yml +19 -0
  6. data/Gemfile +10 -0
  7. data/Gemfile.lock +30 -0
  8. data/LICENSE +674 -0
  9. data/README.md +22 -0
  10. data/Rakefile +35 -0
  11. data/bin/console +25 -0
  12. data/deps/filedialogbuilddeps.rb +49 -0
  13. data/deps/nativefiledialog/.github/ISSUE_TEMPLATE/bug_report.md +32 -0
  14. data/deps/nativefiledialog/.gitignore +181 -0
  15. data/deps/nativefiledialog/LICENSE +16 -0
  16. data/deps/nativefiledialog/README.md +180 -0
  17. data/deps/nativefiledialog/build/dont_run_premake.txt +1 -0
  18. data/deps/nativefiledialog/build/gmake_linux/Makefile +101 -0
  19. data/deps/nativefiledialog/build/gmake_linux/nfd.make +192 -0
  20. data/deps/nativefiledialog/build/gmake_linux/test_opendialog.make +188 -0
  21. data/deps/nativefiledialog/build/gmake_linux/test_opendialogmultiple.make +188 -0
  22. data/deps/nativefiledialog/build/gmake_linux/test_pickfolder.make +188 -0
  23. data/deps/nativefiledialog/build/gmake_linux/test_savedialog.make +188 -0
  24. data/deps/nativefiledialog/build/gmake_linux_zenity/Makefile +101 -0
  25. data/deps/nativefiledialog/build/gmake_linux_zenity/nfd.make +192 -0
  26. data/deps/nativefiledialog/build/gmake_linux_zenity/test_opendialog.make +188 -0
  27. data/deps/nativefiledialog/build/gmake_linux_zenity/test_opendialogmultiple.make +188 -0
  28. data/deps/nativefiledialog/build/gmake_linux_zenity/test_pickfolder.make +188 -0
  29. data/deps/nativefiledialog/build/gmake_linux_zenity/test_savedialog.make +188 -0
  30. data/deps/nativefiledialog/build/gmake_macosx/Makefile +85 -0
  31. data/deps/nativefiledialog/build/gmake_macosx/nfd.make +154 -0
  32. data/deps/nativefiledialog/build/gmake_macosx/test_opendialog.make +150 -0
  33. data/deps/nativefiledialog/build/gmake_macosx/test_opendialogmultiple.make +150 -0
  34. data/deps/nativefiledialog/build/gmake_macosx/test_pickfolder.make +150 -0
  35. data/deps/nativefiledialog/build/gmake_macosx/test_savedialog.make +150 -0
  36. data/deps/nativefiledialog/build/gmake_windows/Makefile +101 -0
  37. data/deps/nativefiledialog/build/gmake_windows/nfd.make +192 -0
  38. data/deps/nativefiledialog/build/gmake_windows/test_opendialog.make +188 -0
  39. data/deps/nativefiledialog/build/gmake_windows/test_opendialogmultiple.make +188 -0
  40. data/deps/nativefiledialog/build/gmake_windows/test_pickfolder.make +188 -0
  41. data/deps/nativefiledialog/build/gmake_windows/test_savedialog.make +188 -0
  42. data/deps/nativefiledialog/build/premake5.lua +265 -0
  43. data/deps/nativefiledialog/build/vs2010/NativeFileDialog.sln +78 -0
  44. data/deps/nativefiledialog/build/vs2010/nfd.vcxproj +168 -0
  45. data/deps/nativefiledialog/build/vs2010/nfd.vcxproj.filters +20 -0
  46. data/deps/nativefiledialog/build/vs2010/test_opendialog.vcxproj +182 -0
  47. data/deps/nativefiledialog/build/vs2010/test_opendialogmultiple.vcxproj +182 -0
  48. data/deps/nativefiledialog/build/vs2010/test_pickfolder.vcxproj +182 -0
  49. data/deps/nativefiledialog/build/vs2010/test_savedialog.vcxproj +182 -0
  50. data/deps/nativefiledialog/build/xcode4/NativeFileDialog.xcworkspace/contents.xcworkspacedata +19 -0
  51. data/deps/nativefiledialog/build/xcode4/nfd.xcodeproj/project.pbxproj +228 -0
  52. data/deps/nativefiledialog/build/xcode4/test_opendialog.xcodeproj/project.pbxproj +294 -0
  53. data/deps/nativefiledialog/build/xcode4/test_opendialogmultiple.xcodeproj/project.pbxproj +294 -0
  54. data/deps/nativefiledialog/build/xcode4/test_pickfolder.xcodeproj/project.pbxproj +294 -0
  55. data/deps/nativefiledialog/build/xcode4/test_savedialog.xcodeproj/project.pbxproj +294 -0
  56. data/deps/nativefiledialog/docs/build.md +39 -0
  57. data/deps/nativefiledialog/docs/contributing.md +25 -0
  58. data/deps/nativefiledialog/screens/open_cocoa.png +0 -0
  59. data/deps/nativefiledialog/screens/open_gtk3.png +0 -0
  60. data/deps/nativefiledialog/screens/open_win.png +0 -0
  61. data/deps/nativefiledialog/src/common.h +21 -0
  62. data/deps/nativefiledialog/src/include/nfd.h +74 -0
  63. data/deps/nativefiledialog/src/nfd_cocoa.m +286 -0
  64. data/deps/nativefiledialog/src/nfd_common.c +142 -0
  65. data/deps/nativefiledialog/src/nfd_common.h +39 -0
  66. data/deps/nativefiledialog/src/nfd_gtk.c +379 -0
  67. data/deps/nativefiledialog/src/nfd_win.cpp +762 -0
  68. data/deps/nativefiledialog/src/nfd_zenity.c +307 -0
  69. data/deps/nativefiledialog/src/simple_exec.h +218 -0
  70. data/deps/nativefiledialog/test/test_opendialog.c +29 -0
  71. data/deps/nativefiledialog/test/test_opendialogmultiple.c +32 -0
  72. data/deps/nativefiledialog/test/test_pickfolder.c +29 -0
  73. data/deps/nativefiledialog/test/test_savedialog.c +28 -0
  74. data/ext/filedialog/extconf.rb +58 -0
  75. data/ext/filedialog/filedialog.c +118 -0
  76. data/filedialog.gemspec +48 -0
  77. data/lib/filedialog.rb +50 -0
  78. data/lib/filedialog/version.rb +5 -0
  79. metadata +137 -0
@@ -0,0 +1,286 @@
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
+ static NSArray *BuildAllowedFileTypes( const char *filterList )
12
+ {
13
+ // Commas and semicolons are the same thing on this platform
14
+
15
+ NSMutableArray *buildFilterList = [[NSMutableArray alloc] init];
16
+
17
+ char typebuf[NFD_MAX_STRLEN] = {0};
18
+
19
+ size_t filterListLen = strlen(filterList);
20
+ char *p_typebuf = typebuf;
21
+ for ( size_t i = 0; i < filterListLen+1; ++i )
22
+ {
23
+ if ( filterList[i] == ',' || filterList[i] == ';' || filterList[i] == '\0' )
24
+ {
25
+ if (filterList[i] != '\0')
26
+ ++p_typebuf;
27
+ *p_typebuf = '\0';
28
+
29
+ NSString *thisType = [NSString stringWithUTF8String: typebuf];
30
+ [buildFilterList addObject:thisType];
31
+ p_typebuf = typebuf;
32
+ *p_typebuf = '\0';
33
+ }
34
+ else
35
+ {
36
+ *p_typebuf = filterList[i];
37
+ ++p_typebuf;
38
+
39
+ }
40
+ }
41
+
42
+ NSArray *returnArray = [NSArray arrayWithArray:buildFilterList];
43
+
44
+ [buildFilterList release];
45
+ return returnArray;
46
+ }
47
+
48
+ static void AddFilterListToDialog( NSSavePanel *dialog, const char *filterList )
49
+ {
50
+ if ( !filterList || strlen(filterList) == 0 )
51
+ return;
52
+
53
+ NSArray *allowedFileTypes = BuildAllowedFileTypes( filterList );
54
+ if ( [allowedFileTypes count] != 0 )
55
+ {
56
+ [dialog setAllowedFileTypes:allowedFileTypes];
57
+ }
58
+ }
59
+
60
+ static void SetDefaultPath( NSSavePanel *dialog, const nfdchar_t *defaultPath )
61
+ {
62
+ if ( !defaultPath || strlen(defaultPath) == 0 )
63
+ return;
64
+
65
+ NSString *defaultPathString = [NSString stringWithUTF8String: defaultPath];
66
+ NSURL *url = [NSURL fileURLWithPath:defaultPathString isDirectory:YES];
67
+ [dialog setDirectoryURL:url];
68
+ }
69
+
70
+
71
+ /* fixme: pathset should be pathSet */
72
+ static nfdresult_t AllocPathSet( NSArray *urls, nfdpathset_t *pathset )
73
+ {
74
+ assert(pathset);
75
+ assert([urls count]);
76
+
77
+ pathset->count = (size_t)[urls count];
78
+ pathset->indices = NFDi_Malloc( sizeof(size_t)*pathset->count );
79
+ if ( !pathset->indices )
80
+ {
81
+ return NFD_ERROR;
82
+ }
83
+
84
+ // count the total space needed for buf
85
+ size_t bufsize = 0;
86
+ for ( NSURL *url in urls )
87
+ {
88
+ NSString *path = [url path];
89
+ bufsize += [path lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;
90
+ }
91
+
92
+ pathset->buf = NFDi_Malloc( sizeof(nfdchar_t) * bufsize );
93
+ if ( !pathset->buf )
94
+ {
95
+ return NFD_ERROR;
96
+ }
97
+
98
+ // fill buf
99
+ nfdchar_t *p_buf = pathset->buf;
100
+ size_t count = 0;
101
+ for ( NSURL *url in urls )
102
+ {
103
+ NSString *path = [url path];
104
+ const nfdchar_t *utf8Path = [path UTF8String];
105
+ size_t byteLen = [path lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;
106
+ memcpy( p_buf, utf8Path, byteLen );
107
+
108
+ ptrdiff_t index = p_buf - pathset->buf;
109
+ assert( index >= 0 );
110
+ pathset->indices[count] = (size_t)index;
111
+
112
+ p_buf += byteLen;
113
+ ++count;
114
+ }
115
+
116
+ return NFD_OKAY;
117
+ }
118
+
119
+ /* public */
120
+
121
+
122
+ nfdresult_t NFD_OpenDialog( const nfdchar_t *filterList,
123
+ const nfdchar_t *defaultPath,
124
+ nfdchar_t **outPath )
125
+ {
126
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
127
+
128
+ NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
129
+ NSOpenPanel *dialog = [NSOpenPanel openPanel];
130
+ [dialog setAllowsMultipleSelection:NO];
131
+
132
+ // Build the filter list
133
+ AddFilterListToDialog(dialog, filterList);
134
+
135
+ // Set the starting directory
136
+ SetDefaultPath(dialog, defaultPath);
137
+
138
+ nfdresult_t nfdResult = NFD_CANCEL;
139
+ if ( [dialog runModal] == NSModalResponseOK )
140
+ {
141
+ NSURL *url = [dialog URL];
142
+ const char *utf8Path = [[url path] UTF8String];
143
+
144
+ // byte count, not char count
145
+ size_t len = strlen(utf8Path);//NFDi_UTF8_Strlen(utf8Path);
146
+
147
+ *outPath = NFDi_Malloc( len+1 );
148
+ if ( !*outPath )
149
+ {
150
+ [pool release];
151
+ [keyWindow makeKeyAndOrderFront:nil];
152
+ return NFD_ERROR;
153
+ }
154
+ memcpy( *outPath, utf8Path, len+1 ); /* copy null term */
155
+ nfdResult = NFD_OKAY;
156
+ }
157
+ [pool release];
158
+
159
+ [keyWindow makeKeyAndOrderFront:nil];
160
+ return nfdResult;
161
+ }
162
+
163
+
164
+ nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList,
165
+ const nfdchar_t *defaultPath,
166
+ nfdpathset_t *outPaths )
167
+ {
168
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
169
+ NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
170
+
171
+ NSOpenPanel *dialog = [NSOpenPanel openPanel];
172
+ [dialog setAllowsMultipleSelection:YES];
173
+
174
+ // Build the fiter list.
175
+ AddFilterListToDialog(dialog, filterList);
176
+
177
+ // Set the starting directory
178
+ SetDefaultPath(dialog, defaultPath);
179
+
180
+ nfdresult_t nfdResult = NFD_CANCEL;
181
+ if ( [dialog runModal] == NSModalResponseOK )
182
+ {
183
+ NSArray *urls = [dialog URLs];
184
+
185
+ if ( [urls count] == 0 )
186
+ {
187
+ [pool release];
188
+ [keyWindow makeKeyAndOrderFront:nil];
189
+ return NFD_CANCEL;
190
+ }
191
+
192
+ if ( AllocPathSet( urls, outPaths ) == NFD_ERROR )
193
+ {
194
+ [pool release];
195
+ [keyWindow makeKeyAndOrderFront:nil];
196
+ return NFD_ERROR;
197
+ }
198
+
199
+ nfdResult = NFD_OKAY;
200
+ }
201
+ [pool release];
202
+
203
+ [keyWindow makeKeyAndOrderFront:nil];
204
+ return nfdResult;
205
+ }
206
+
207
+
208
+ nfdresult_t NFD_SaveDialog( const nfdchar_t *filterList,
209
+ const nfdchar_t *defaultPath,
210
+ nfdchar_t **outPath )
211
+ {
212
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
213
+ NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
214
+
215
+ NSSavePanel *dialog = [NSSavePanel savePanel];
216
+ [dialog setExtensionHidden:NO];
217
+
218
+ // Build the filter list.
219
+ AddFilterListToDialog(dialog, filterList);
220
+
221
+ // Set the starting directory
222
+ SetDefaultPath(dialog, defaultPath);
223
+
224
+ nfdresult_t nfdResult = NFD_CANCEL;
225
+ if ( [dialog runModal] == NSModalResponseOK )
226
+ {
227
+ NSURL *url = [dialog URL];
228
+ const char *utf8Path = [[url path] UTF8String];
229
+
230
+ size_t byteLen = [url.path lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;
231
+
232
+ *outPath = NFDi_Malloc( byteLen );
233
+ if ( !*outPath )
234
+ {
235
+ [pool release];
236
+ [keyWindow makeKeyAndOrderFront:nil];
237
+ return NFD_ERROR;
238
+ }
239
+ memcpy( *outPath, utf8Path, byteLen );
240
+ nfdResult = NFD_OKAY;
241
+ }
242
+
243
+ [pool release];
244
+ [keyWindow makeKeyAndOrderFront:nil];
245
+ return nfdResult;
246
+ }
247
+
248
+ nfdresult_t NFD_PickFolder(const nfdchar_t *defaultPath,
249
+ nfdchar_t **outPath)
250
+ {
251
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
252
+
253
+ NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
254
+ NSOpenPanel *dialog = [NSOpenPanel openPanel];
255
+ [dialog setAllowsMultipleSelection:NO];
256
+ [dialog setCanChooseDirectories:YES];
257
+ [dialog setCanCreateDirectories:YES];
258
+ [dialog setCanChooseFiles:NO];
259
+
260
+ // Set the starting directory
261
+ SetDefaultPath(dialog, defaultPath);
262
+
263
+ nfdresult_t nfdResult = NFD_CANCEL;
264
+ if ( [dialog runModal] == NSModalResponseOK )
265
+ {
266
+ NSURL *url = [dialog URL];
267
+ const char *utf8Path = [[url path] UTF8String];
268
+
269
+ // byte count, not char count
270
+ size_t len = strlen(utf8Path);//NFDi_UTF8_Strlen(utf8Path);
271
+
272
+ *outPath = NFDi_Malloc( len+1 );
273
+ if ( !*outPath )
274
+ {
275
+ [pool release];
276
+ [keyWindow makeKeyAndOrderFront:nil];
277
+ return NFD_ERROR;
278
+ }
279
+ memcpy( *outPath, utf8Path, len+1 ); /* copy null term */
280
+ nfdResult = NFD_OKAY;
281
+ }
282
+ [pool release];
283
+
284
+ [keyWindow makeKeyAndOrderFront:nil];
285
+ return nfdResult;
286
+ }
@@ -0,0 +1,142 @@
1
+ /*
2
+ Native File Dialog
3
+
4
+ http://www.frogtoss.com/labs
5
+ */
6
+
7
+ #include <stdlib.h>
8
+ #include <assert.h>
9
+ #include <string.h>
10
+ #include "nfd_common.h"
11
+
12
+ static char g_errorstr[NFD_MAX_STRLEN] = {0};
13
+
14
+ /* public routines */
15
+
16
+ const char *NFD_GetError( void )
17
+ {
18
+ return g_errorstr;
19
+ }
20
+
21
+ size_t NFD_PathSet_GetCount( const nfdpathset_t *pathset )
22
+ {
23
+ assert(pathset);
24
+ return pathset->count;
25
+ }
26
+
27
+ nfdchar_t *NFD_PathSet_GetPath( const nfdpathset_t *pathset, size_t num )
28
+ {
29
+ assert(pathset);
30
+ assert(num < pathset->count);
31
+
32
+ return pathset->buf + pathset->indices[num];
33
+ }
34
+
35
+ void NFD_PathSet_Free( nfdpathset_t *pathset )
36
+ {
37
+ assert(pathset);
38
+ NFDi_Free( pathset->indices );
39
+ NFDi_Free( pathset->buf );
40
+ }
41
+
42
+ /* internal routines */
43
+
44
+ void *NFDi_Malloc( size_t bytes )
45
+ {
46
+ void *ptr = malloc(bytes);
47
+ if ( !ptr )
48
+ NFDi_SetError("NFDi_Malloc failed.");
49
+
50
+ return ptr;
51
+ }
52
+
53
+ void NFDi_Free( void *ptr )
54
+ {
55
+ assert(ptr);
56
+ free(ptr);
57
+ }
58
+
59
+ void NFDi_SetError( const char *msg )
60
+ {
61
+ int bTruncate = NFDi_SafeStrncpy( g_errorstr, msg, NFD_MAX_STRLEN );
62
+ assert( !bTruncate ); _NFD_UNUSED(bTruncate);
63
+ }
64
+
65
+
66
+ int NFDi_SafeStrncpy( char *dst, const char *src, size_t maxCopy )
67
+ {
68
+ size_t n = maxCopy;
69
+ char *d = dst;
70
+
71
+ assert( src );
72
+ assert( dst );
73
+
74
+ while ( n > 0 && *src != '\0' )
75
+ {
76
+ *d++ = *src++;
77
+ --n;
78
+ }
79
+
80
+ /* Truncation case -
81
+ terminate string and return true */
82
+ if ( n == 0 )
83
+ {
84
+ dst[maxCopy-1] = '\0';
85
+ return 1;
86
+ }
87
+
88
+ /* No truncation. Append a single NULL and return. */
89
+ *d = '\0';
90
+ return 0;
91
+ }
92
+
93
+
94
+ /* adapted from microutf8 */
95
+ int32_t NFDi_UTF8_Strlen( const nfdchar_t *str )
96
+ {
97
+ /* This function doesn't properly check validity of UTF-8 character
98
+ sequence, it is supposed to use only with valid UTF-8 strings. */
99
+
100
+ int32_t character_count = 0;
101
+ int32_t i = 0; /* Counter used to iterate over string. */
102
+ nfdchar_t maybe_bom[4];
103
+
104
+ /* If there is UTF-8 BOM ignore it. */
105
+ if (strlen(str) > 2)
106
+ {
107
+ strncpy(maybe_bom, str, 3);
108
+ maybe_bom[3] = 0;
109
+ if (strcmp(maybe_bom, (nfdchar_t*)NFD_UTF8_BOM) == 0)
110
+ i += 3;
111
+ }
112
+
113
+ while(str[i])
114
+ {
115
+ if (str[i] >> 7 == 0)
116
+ {
117
+ /* If bit pattern begins with 0 we have ascii character. */
118
+ ++character_count;
119
+ }
120
+ else if (str[i] >> 6 == 3)
121
+ {
122
+ /* If bit pattern begins with 11 it is beginning of UTF-8 byte sequence. */
123
+ ++character_count;
124
+ }
125
+ else if (str[i] >> 6 == 2)
126
+ ; /* If bit pattern begins with 10 it is middle of utf-8 byte sequence. */
127
+ else
128
+ {
129
+ /* In any other case this is not valid UTF-8. */
130
+ return -1;
131
+ }
132
+ ++i;
133
+ }
134
+
135
+ return character_count;
136
+ }
137
+
138
+ int NFDi_IsFilterSegmentChar( char ch )
139
+ {
140
+ return (ch==','||ch==';'||ch=='\0');
141
+ }
142
+
@@ -0,0 +1,39 @@
1
+ /*
2
+ Native File Dialog
3
+
4
+ Internal, common across platforms
5
+
6
+ http://www.frogtoss.com/labs
7
+ */
8
+
9
+
10
+ #ifndef _NFD_COMMON_H
11
+ #define _NFD_COMMON_H
12
+
13
+ #include "nfd.h"
14
+
15
+ #include <stdint.h>
16
+
17
+ #ifdef __cplusplus
18
+ extern "C" {
19
+ #endif
20
+
21
+ #define NFD_MAX_STRLEN 256
22
+ #define _NFD_UNUSED(x) ((void)x)
23
+
24
+ #define NFD_UTF8_BOM "\xEF\xBB\xBF"
25
+
26
+
27
+ void *NFDi_Malloc( size_t bytes );
28
+ void NFDi_Free( void *ptr );
29
+ void NFDi_SetError( const char *msg );
30
+ int NFDi_SafeStrncpy( char *dst, const char *src, size_t maxCopy );
31
+ int32_t NFDi_UTF8_Strlen( const nfdchar_t *str );
32
+ int NFDi_IsFilterSegmentChar( char ch );
33
+
34
+ #ifdef __cplusplus
35
+ }
36
+ #endif
37
+
38
+
39
+ #endif