Skip to content
Permalink
Browse files

Added support for extra info panels in OSX native file choosers.

  • Loading branch information...
jules
jules committed Jan 29, 2015
1 parent 15a1548 commit 8b3935f12154767d37c65c1612d1bfeb93a5965f
@@ -99,7 +99,7 @@ bool FileChooser::showDialog (const int flags, FilePreviewComponent* const previ
#if JUCE_WINDOWS
if (useNativeDialogBox && ! (selectsFiles && selectsDirectories))
#elif JUCE_MAC || JUCE_LINUX
if (useNativeDialogBox && (previewComp == nullptr))
if (useNativeDialogBox)
#else
if (false)
#endif
@@ -29,9 +29,11 @@
FileChooserDelegateClass() : ObjCClass <NSObject> ("JUCEFileChooser_")
{
addIvar<StringArray*> ("filters");
addIvar<FilePreviewComponent*> ("filePreviewComponent");

addMethod (@selector (dealloc), dealloc, "v@:");
addMethod (@selector (panel:shouldShowFilename:), shouldShowFilename, "c@:@@");
addMethod (@selector (dealloc), dealloc, "v@:");
addMethod (@selector (panel:shouldShowFilename:), shouldShowFilename, "c@:@@");
addMethod (@selector (panelSelectionDidChange:), panelSelectionDidChange, "c@");

#if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
addProtocol (@protocol (NSOpenSavePanelDelegate));
@@ -40,21 +42,21 @@
registerClass();
}

static void setFilters (id self, StringArray* filters)
{
object_setInstanceVariable (self, "filters", filters);
}
static void setFilters (id self, StringArray* filters) { object_setInstanceVariable (self, "filters", filters); }
static void setFilePreviewComponent (id self, FilePreviewComponent* comp) { object_setInstanceVariable (self, "filePreviewComponent", comp); }
static StringArray* getFilters (id self) { return getIvar<StringArray*> (self, "filters"); }
static FilePreviewComponent* getFilePreviewComponent (id self) { return getIvar<FilePreviewComponent*> (self, "filePreviewComponent"); }

private:
static void dealloc (id self, SEL)
{
delete getIvar<StringArray*> (self, "filters");
delete getFilters (self);
sendSuperclassMessage (self, @selector (dealloc));
}

static BOOL shouldShowFilename (id self, SEL, id /*sender*/, NSString* filename)
{
StringArray* const filters = getIvar<StringArray*> (self, "filters");
StringArray* const filters = getFilters (self);

const File f (nsStringToJuce (filename));

@@ -81,6 +83,32 @@ static BOOL shouldShowFilename (id self, SEL, id /*sender*/, NSString* filename)
return f.isDirectory()
&& ! [[NSWorkspace sharedWorkspace] isFilePackageAtPath: filename];
}

static StringArray getSelectedPaths (id sender)
{
StringArray paths;

if ([sender isKindOfClass: [NSOpenPanel class]])
{
NSArray* urls = [(NSOpenPanel*) sender URLs];

for (NSUInteger i = 0; i < [urls count]; ++i)
paths.add (nsStringToJuce ([[urls objectAtIndex: i] path]));
}
else if ([sender isKindOfClass: [NSSavePanel class]])
{
paths.add (nsStringToJuce ([[(NSSavePanel*) sender URL] path]));
}

return paths;
}

static void panelSelectionDidChange (id self, SEL, id sender)
{
// NB: would need to extend FilePreviewComponent to handle the full list rather than just the first one
if (FilePreviewComponent* const previewComp = getFilePreviewComponent (self))
previewComp->selectedFileChanged (File (getSelectedPaths (sender)[0]));
}
};

static NSMutableArray* createAllowedTypesArray (const StringArray& filters)
@@ -113,7 +141,7 @@ static BOOL shouldShowFilename (id self, SEL, id /*sender*/, NSString* filename)
bool isSaveDialogue,
bool /*warnAboutOverwritingExistingFiles*/,
bool selectMultipleFiles,
FilePreviewComponent* /*extraInfoComponent*/)
FilePreviewComponent* extraInfoComponent)
{
JUCE_AUTORELEASEPOOL
{
@@ -151,6 +179,16 @@ static BOOL shouldShowFilename (id self, SEL, id /*sender*/, NSString* filename)
[openPanel setResolvesAliases: YES];
}

if (extraInfoComponent != nullptr)
{
NSView* view = [[[NSView alloc] initWithFrame: makeNSRect (extraInfoComponent->getLocalBounds())] autorelease];
extraInfoComponent->addToDesktop (0, (void*) view);
extraInfoComponent->setVisible (true);
FileChooserDelegateClass::setFilePreviewComponent (delegate, extraInfoComponent);

[panel setAccessoryView: view];
}

[panel setDelegate: delegate];

if (isSaveDialogue || selectsDirectory)

0 comments on commit 8b3935f

Please sign in to comment.
You can’t perform that action at this time.