macOS: Add "Show hidden files" button to native file chooser

This commit is contained in:
ManoloFLTK 2022-12-15 12:10:41 +01:00
parent 64d8c913be
commit 2fd1866f49

View File

@ -551,40 +551,77 @@ static char *prepareMacFilter(int count, const char *filter, char **patterns) {
}
@end
@interface FLHiddenFilesAction : NSObject
{
@public
NSSavePanel *panel;
NSButton *button;
}
- (void)action;
@end
@implementation FLHiddenFilesAction
- (void)action {
[panel setShowsHiddenFiles:[button intValue]];
}
@end
static NSPopUpButton *createPopupAccessory(NSSavePanel *panel, const char *filter, const char *title, int rank)
{
NSPopUpButton *popup;
NSRect rectview = NSMakeRect(5, 5, 350, 30 );
NSRect rectview = NSMakeRect(5, 5, 350, filter ? 60 : 30);
NSView *view = [[[NSView alloc] initWithFrame:rectview] autorelease];
NSRect rectbox = NSMakeRect(0, 3, 140, 20 );
NSBox *box = [[[NSBox alloc] initWithFrame:rectbox] autorelease];
NSRect rectpop = NSMakeRect(105, 0, 246, 30 );
popup = [[[NSPopUpButton alloc ] initWithFrame:rectpop pullsDown:NO] autorelease];
[view addSubview:box];
[view addSubview:popup];
[box setBorderType:NSNoBorder];
NSString *nstitle = [[NSString alloc] initWithUTF8String:title];
[box setTitle:nstitle];
[nstitle release];
NSFont *font = [NSFont controlContentFontOfSize:NSRegularControlSize];
[box setTitleFont:font];
[box sizeToFit];
// horizontally move box to fit the locale-dependent width of its title
NSRect r=[box frame];
NSPoint o = r.origin;
o.x = rectpop.origin.x - r.size.width + 15;
[box setFrameOrigin:o];
CFStringRef tab = CFSTR("\n");
CFStringRef tmp_cfs;
tmp_cfs = CFStringCreateWithCString(NULL, filter, kCFStringEncodingUTF8);
CFArrayRef array = CFStringCreateArrayBySeparatingStrings(NULL, tmp_cfs, tab);
CFRelease(tmp_cfs);
CFRelease(tab);
[popup addItemsWithTitles:(NSArray*)array];
NSMenuItem *item = [popup itemWithTitle:@""];
if (item) [popup removeItemWithTitle:@""];
CFRelease(array);
[popup selectItemAtIndex:rank];
// the "Show hidden files" button
NSRect hidden_files_rect = {{150, (filter ? 35 : 0)}, {80, 30}};
NSButton *hidden_files = [[[NSButton alloc] initWithFrame:hidden_files_rect] autorelease];
[hidden_files setButtonType:
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12
NSButtonTypeSwitch
#else
NSSwitchButton
#endif
];
[hidden_files setTitle:[NSString stringWithUTF8String:Fl_File_Chooser::hidden_label]];
[hidden_files sizeToFit];
[hidden_files setIntValue:0];
[view addSubview:hidden_files];
static FLHiddenFilesAction *target = [[FLHiddenFilesAction alloc] init]; // never released
target->panel = panel;
target->button = hidden_files;
[hidden_files setAction:@selector(action)];
[hidden_files setTarget:target];
if (filter) {
NSBox *box = [[[NSBox alloc] initWithFrame:rectbox] autorelease];
NSRect rectpop = NSMakeRect(105, 0, 246, 30 );
popup = [[[NSPopUpButton alloc ] initWithFrame:rectpop pullsDown:NO] autorelease];
[view addSubview:box];
[view addSubview:popup];
[box setBorderType:NSNoBorder];
NSString *nstitle = [[NSString alloc] initWithUTF8String:title];
[box setTitle:nstitle];
[nstitle release];
NSFont *font = [NSFont controlContentFontOfSize:NSRegularControlSize];
[box setTitleFont:font];
[box sizeToFit];
// horizontally move box to fit the locale-dependent width of its title
NSRect r=[box frame];
NSPoint o = r.origin;
o.x = rectpop.origin.x - r.size.width + 15;
[box setFrameOrigin:o];
CFStringRef tab = CFSTR("\n");
CFStringRef tmp_cfs;
tmp_cfs = CFStringCreateWithCString(NULL, filter, kCFStringEncodingUTF8);
CFArrayRef array = CFStringCreateArrayBySeparatingStrings(NULL, tmp_cfs, tab);
CFRelease(tmp_cfs);
CFRelease(tab);
[popup addItemsWithTitles:(NSArray*)array];
NSMenuItem *item = [popup itemWithTitle:@""];
if (item) [popup removeItemWithTitle:@""];
CFRelease(array);
[popup selectItemAtIndex:rank];
}
[panel setAccessoryView:view];
return popup;
}
@ -710,7 +747,7 @@ int Fl_Quartz_Native_File_Chooser_Driver::post() {
FLopenDelegate *openDelegate = [[[FLopenDelegate alloc] init] autorelease];
[openDelegate setPopup:popup filter_pattern:_filt_patt];
[_panel setDelegate:openDelegate];
}
} else createPopupAccessory(_panel, NULL, Fl_File_Chooser::show_label, 0);
}
else {
FLsaveDelegate *saveDelegate = [[[FLsaveDelegate alloc] init] autorelease];
@ -738,7 +775,7 @@ int Fl_Quartz_Native_File_Chooser_Driver::post() {
}
[_panel setCanSelectHiddenExtension:YES];
[_panel setExtensionHidden:NO];
}
} else createPopupAccessory(_panel, NULL, Fl_File_Chooser::show_label, 0);
}
int retval = runmodal();
if (_filt_total) {