Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add checkbox to NSOpenPanel

I would like to add a checkbox to NSOpenPanel, and then query its state when receiving the selected files. How can I do this?

Additionally, it would be desirable to be able to enable or disable the checkbox based on the current file selection.

like image 305
hpique Avatar asked Dec 09 '25 16:12

hpique


2 Answers

The complete solution based on the answers of Joshua Nozzi and Mark Alldritt:

NSOpenPanel* openDlg = [NSOpenPanel openPanel];
NSButton *button = [[NSButton alloc] init];
[button setButtonType:NSSwitchButton];
button.title = NSLocalizedString(@"I am a checkbox", @"");
[button sizeToFit];
[openDlg setAccessoryView:button];
openDlg.delegate = self;
[openDlg beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) 
{
    openDlg.delegate = nil; // TODO: Check if this is necessary
    if (result != NSFileHandlingPanelOKButton) return;
    BOOL checkboxOn = (((NSButton*)openDlg.accessoryView).state == NSOnState); 
    // Do something
}];

The NSOpenSavePanelDelegate:

- (void)panelSelectionDidChange:(id)sender {
    NSOpenPanel *panel = sender;
    NSButton *button = (NSButton*)panel.accessoryView;
    // Update button based on panel selection
}
like image 133
hpique Avatar answered Dec 12 '25 13:12

hpique


NSOpenPanel is a subclass of NSSavePanel, which has -setAccessoryView:.

like image 20
Joshua Nozzi Avatar answered Dec 12 '25 15:12

Joshua Nozzi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!