Skip to content

Commit

Permalink
Added support for extra info panels in OSX native file choosers.
Browse files Browse the repository at this point in the history
  • Loading branch information
jules committed Jan 29, 2015
1 parent 15a1548 commit 8b3935f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
2 changes: 1 addition & 1 deletion modules/juce_gui_basics/filebrowser/juce_FileChooser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 47 additions & 9 deletions modules/juce_gui_basics/native/juce_mac_FileChooser.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));

Expand All @@ -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)
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8b3935f

Please sign in to comment.