Merge commit '5461f6700dac79e9e71e3966f8a1270706c385ba'
This commit is contained in:
80
deps/nfde/src/nfd_cocoa.m
vendored
80
deps/nfde/src/nfd_cocoa.m
vendored
@ -6,8 +6,28 @@
|
||||
*/
|
||||
|
||||
#include <AppKit/AppKit.h>
|
||||
#include <Availability.h>
|
||||
#include "nfd.h"
|
||||
|
||||
// MacOS is deprecating the allowedFileTypes property in favour of allowedContentTypes, so we have
|
||||
// to introduce this breaking change. Define NFD_MACOS_ALLOWEDCONTENTTYPES to 1 to have it set the
|
||||
// allowedContentTypes property of the SavePanel or OpenPanel. Define
|
||||
// NFD_MACOS_ALLOWEDCONTENTTYPES to 0 to have it set the allowedFileTypes property of the SavePanel
|
||||
// or OpenPanel. If NFD_MACOS_ALLOWEDCONTENTTYPES is undefined, then it will set it to 1 if
|
||||
// __MAC_OS_X_VERSION_MIN_REQUIRED >= 11.0, and 0 otherwise.
|
||||
#if !defined(NFD_MACOS_ALLOWEDCONTENTTYPES)
|
||||
#if !defined(__MAC_OS_X_VERSION_MIN_REQUIRED) || !defined(__MAC_11_0) || \
|
||||
__MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_11_0
|
||||
#define NFD_MACOS_ALLOWEDCONTENTTYPES 0
|
||||
#else
|
||||
#define NFD_MACOS_ALLOWEDCONTENTTYPES 1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if NFD_MACOS_ALLOWEDCONTENTTYPES == 1
|
||||
#include <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
|
||||
#endif
|
||||
|
||||
static const char* g_errorstr = NULL;
|
||||
|
||||
static void NFDi_SetError(const char* msg) {
|
||||
@ -26,10 +46,51 @@ static void NFDi_Free(void* ptr) {
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
#if NFD_MACOS_ALLOWEDCONTENTTYPES == 1
|
||||
// Returns an NSArray of UTType representing the content types.
|
||||
static NSArray* BuildAllowedContentTypes(const nfdnfilteritem_t* filterList,
|
||||
nfdfiltersize_t filterCount) {
|
||||
NSMutableArray* buildFilterList = [[NSMutableArray alloc] init];
|
||||
|
||||
for (nfdfiltersize_t filterIndex = 0; filterIndex != filterCount; ++filterIndex) {
|
||||
// this is the spec to parse (we don't use the friendly name on OS X)
|
||||
const nfdnchar_t* filterSpec = filterList[filterIndex].spec;
|
||||
|
||||
const nfdnchar_t* p_currentFilterBegin = filterSpec;
|
||||
for (const nfdnchar_t* p_filterSpec = filterSpec; *p_filterSpec; ++p_filterSpec) {
|
||||
if (*p_filterSpec == ',') {
|
||||
// add the extension to the array
|
||||
NSString* filterStr = [[NSString alloc]
|
||||
initWithBytes:(const void*)p_currentFilterBegin
|
||||
length:(sizeof(nfdnchar_t) * (p_filterSpec - p_currentFilterBegin))
|
||||
encoding:NSUTF8StringEncoding];
|
||||
UTType* filterType = [UTType typeWithFilenameExtension:filterStr
|
||||
conformingToType:UTTypeData];
|
||||
[filterStr release];
|
||||
if (filterType) [buildFilterList addObject:filterType];
|
||||
p_currentFilterBegin = p_filterSpec + 1;
|
||||
}
|
||||
}
|
||||
// add the extension to the array
|
||||
NSString* filterStr = [[NSString alloc] initWithUTF8String:p_currentFilterBegin];
|
||||
UTType* filterType = [UTType typeWithFilenameExtension:filterStr
|
||||
conformingToType:UTTypeData];
|
||||
[filterStr release];
|
||||
if (filterType) [buildFilterList addObject:filterType];
|
||||
}
|
||||
|
||||
NSArray* returnArray = [NSArray arrayWithArray:buildFilterList];
|
||||
|
||||
[buildFilterList release];
|
||||
|
||||
assert([returnArray count] != 0);
|
||||
|
||||
return returnArray;
|
||||
}
|
||||
#else
|
||||
// Returns an NSArray of NSString representing the file types.
|
||||
static NSArray* BuildAllowedFileTypes(const nfdnfilteritem_t* filterList,
|
||||
nfdfiltersize_t filterCount) {
|
||||
// Commas and semicolons are the same thing on this platform
|
||||
|
||||
NSMutableArray* buildFilterList = [[NSMutableArray alloc] init];
|
||||
|
||||
for (nfdfiltersize_t filterIndex = 0; filterIndex != filterCount; ++filterIndex) {
|
||||
@ -61,6 +122,7 @@ static NSArray* BuildAllowedFileTypes(const nfdnfilteritem_t* filterList,
|
||||
|
||||
return returnArray;
|
||||
}
|
||||
#endif
|
||||
|
||||
static void AddFilterListToDialog(NSSavePanel* dialog,
|
||||
const nfdnfilteritem_t* filterList,
|
||||
@ -71,11 +133,15 @@ static void AddFilterListToDialog(NSSavePanel* dialog,
|
||||
|
||||
assert(filterList);
|
||||
|
||||
// make NSArray of file types
|
||||
// Make NSArray of file types and set it on the dialog
|
||||
// We use setAllowedFileTypes or setAllowedContentTypes depending on the deployment target
|
||||
#if NFD_MACOS_ALLOWEDCONTENTTYPES == 1
|
||||
NSArray* allowedContentTypes = BuildAllowedContentTypes(filterList, filterCount);
|
||||
[dialog setAllowedContentTypes:allowedContentTypes];
|
||||
#else
|
||||
NSArray* allowedFileTypes = BuildAllowedFileTypes(filterList, filterCount);
|
||||
|
||||
// set it on the dialog
|
||||
[dialog setAllowedFileTypes:allowedFileTypes];
|
||||
#endif
|
||||
}
|
||||
|
||||
static void SetDefaultPath(NSSavePanel* dialog, const nfdnchar_t* defaultPath) {
|
||||
@ -114,6 +180,10 @@ const char* NFD_GetError(void) {
|
||||
return g_errorstr;
|
||||
}
|
||||
|
||||
void NFD_ClearError(void) {
|
||||
NFDi_SetError(NULL);
|
||||
}
|
||||
|
||||
void NFD_FreePathN(nfdnchar_t* filePath) {
|
||||
NFDi_Free((void*)filePath);
|
||||
}
|
||||
|
Reference in New Issue
Block a user