+// - as setAllowedFileTypes is only functional for NSOpenPanel on 10.6+, on earlier systems, the file
+// type choice will not be shown, but all possible file items will be shown, if a popup must be working
+// then the delegate method - (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename will have to
+// be implemented
+
+@interface wxOpenPanelDelegate : NSObject wxOSX_10_6_AND_LATER(<NSOpenSavePanelDelegate>)
+{
+ wxFileDialog* _dialog;
+}
+
+- (wxFileDialog*) fileDialog;
+- (void) setFileDialog:(wxFileDialog*) dialog;
+
+- (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename;
+
+@end
+
+@implementation wxOpenPanelDelegate
+
+- (id) init
+{
+ self = [super init];
+ _dialog = NULL;
+ return self;
+}
+
+- (wxFileDialog*) fileDialog
+{
+ return _dialog;
+}
+
+- (void) setFileDialog:(wxFileDialog*) dialog
+{
+ _dialog = dialog;
+}
+
+- (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename
+{
+ BOOL showObject = YES;
+
+ NSString* resolvedLink = [[NSFileManager defaultManager] pathContentOfSymbolicLinkAtPath:filename];
+ if ( resolvedLink != nil )
+ filename = resolvedLink;
+
+ NSDictionary* fileAttribs = [[NSFileManager defaultManager]
+ fileAttributesAtPath:filename traverseLink:YES];
+ if (fileAttribs)
+ {
+ // check for packages
+ if ([NSFileTypeDirectory isEqualTo:[fileAttribs objectForKey:NSFileType]])
+ {
+ if ([[NSWorkspace sharedWorkspace] isFilePackageAtPath:filename] == NO)
+ showObject = YES; // it's a folder, OK to show
+ else
+ {
+ // it's a packaged directory, apply check
+ wxCFStringRef filecf([filename retain]);
+ showObject = _dialog->CheckFile(filecf.AsString());
+ }
+ }
+ else
+ {
+ // the code above only solves links, not aliases, do this here:
+
+ NSString* resolvedAlias = nil;
+
+ CFURLRef url = CFURLCreateWithFileSystemPath (kCFAllocatorDefault,
+ (CFStringRef)filename,
+ kCFURLPOSIXPathStyle,
+ NO);
+ if (url != NULL)
+ {
+ FSRef fsRef;
+ if (CFURLGetFSRef(url, &fsRef))
+ {
+ Boolean targetIsFolder, wasAliased;
+ OSErr err = FSResolveAliasFile (&fsRef, true, &targetIsFolder, &wasAliased);
+
+ if ((err == noErr) && wasAliased)
+ {
+ CFURLRef resolvedUrl = CFURLCreateFromFSRef(kCFAllocatorDefault, &fsRef);
+ if (resolvedUrl != NULL)
+ {
+ resolvedAlias = (NSString*) CFURLCopyFileSystemPath(resolvedUrl,
+ kCFURLPOSIXPathStyle);
+ CFRelease(resolvedUrl);
+ }
+ }
+ }
+ CFRelease(url);
+ }
+
+ if (resolvedAlias != nil)
+ {
+ // recursive call
+ [resolvedAlias autorelease];
+ showObject = [self panel:sender shouldShowFilename:resolvedAlias];
+ }
+ else
+ {
+ wxCFStringRef filecf([filename retain]);
+ showObject = _dialog->CheckFile(filecf.AsString());
+ }
+ }
+ }
+
+ return showObject;
+}
+
+@end