libui/darwin/combobox.m

191 lines
4.4 KiB
Mathematica
Raw Normal View History

2015-08-14 20:46:35 -05:00
// 14 august 2015
#include "uipriv_darwin.h"
struct uiCombobox {
uiDarwinControl c;
BOOL editable;
NSPopUpButton *pb;
2015-08-22 11:48:48 -05:00
NSArrayController *pbac;
2015-08-14 20:46:35 -05:00
NSComboBox *cb;
NSObject *handle; // for uiControlHandle()
void (*onSelected)(uiCombobox *, void *);
void *onSelectedData;
2015-08-14 20:46:35 -05:00
};
@interface comboboxDelegateClass : NSObject<NSComboBoxDelegate> {
NSMapTable *comboboxes;
}
- (void)comboBoxSelectionDidChange:(NSNotification *)note;
- (IBAction)onSelected:(id)sender;
- (void)registerCombobox:(uiCombobox *)c;
- (void)unregisterCombobox:(uiCombobox *)c;
@end
@implementation comboboxDelegateClass
- (id)init
{
self = [super init];
if (self)
self->comboboxes = newMap();
return self;
}
- (void)dealloc
{
if ([self->comboboxes count] != 0)
complain("attempt to destroy shared combobox delegate but comboboxes are still registered to it");
[self->comboboxes release];
[super dealloc];
}
// note: does not trigger when text changed
// TODO not perfect either:
// - triggered when keyboard navigating the open menu
// - does not trigger when the text is changed to a menu item (which normally selects that item; IDK how to inhibit that behavior - TODO)
- (void)comboBoxSelectionDidChange:(NSNotification *)note
{
[self onSelected:[note object]];
}
- (IBAction)onSelected:(id)sender
{
uiCombobox *c;
c = (uiCombobox *) mapGet(self->comboboxes, sender);
(*(c->onSelected))(c, c->onSelectedData);
}
- (void)registerCombobox:(uiCombobox *)c
{
mapSet(self->comboboxes, c->handle, c);
if (c->editable)
[c->cb setDelegate:self];
else {
[c->pb setTarget:self];
[c->pb setAction:@selector(onSelected:)];
}
}
- (void)unregisterCombobox:(uiCombobox *)c
{
if (c->editable)
[c->cb setDelegate:nil];
else
[c->pb setTarget:nil];
[self->comboboxes removeObjectForKey:c->handle];
}
@end
static comboboxDelegateClass *comboboxDelegate = nil;
2015-08-22 11:48:48 -05:00
static void onDestroy(uiCombobox *);
uiDarwinDefineControlWithOnDestroy(
2015-08-14 20:46:35 -05:00
uiCombobox, // type name
uiComboboxType, // type function
2015-08-22 11:48:48 -05:00
handle, // handle
onDestroy(this); // on destroy
2015-08-14 20:46:35 -05:00
)
2015-08-22 11:48:48 -05:00
static void onDestroy(uiCombobox *c)
{
[comboboxDelegate unregisterCombobox:c];
2015-08-22 11:48:48 -05:00
if (!c->editable) {
[c->pb unbind:@"contentObjects"];
[c->pb unbind:@"selectedIndex"];
[c->pbac release];
}
}
2015-08-14 20:46:35 -05:00
void uiComboboxAppend(uiCombobox *c, const char *text)
{
if (c->editable)
[c->cb addItemWithObjectValue:toNSString(text)];
else
2015-08-22 11:48:48 -05:00
[c->pbac addObject:toNSString(text)];
2015-08-14 20:46:35 -05:00
}
intmax_t uiComboboxSelected(uiCombobox *c)
{
if (c->editable)
return [c->cb indexOfSelectedItem];
return [c->pb indexOfSelectedItem];
}
void uiComboboxOnSelected(uiCombobox *c, void (*f)(uiCombobox *c, void *data), void *data)
{
c->onSelected = f;
c->onSelectedData = data;
}
static void defaultOnSelected(uiCombobox *c, void *data)
{
// do nothing
}
2015-08-14 20:46:35 -05:00
static uiCombobox *finishNewCombobox(BOOL editable)
{
uiCombobox *c;
c = (uiCombobox *) uiNewControl(uiComboboxType());
c->editable = editable;
if (c->editable) {
c->cb = [[NSComboBox alloc] initWithFrame:NSZeroRect];
[c->cb setUsesDataSource:NO];
[c->cb setButtonBordered:YES];
[c->cb setCompletes:NO];
2015-08-14 21:50:20 -05:00
uiDarwinSetControlFont(c->cb, NSRegularControlSize);
c->handle = c->cb;
2015-08-14 20:46:35 -05:00
} else {
NSPopUpButtonCell *pbcell;
2015-08-14 20:46:35 -05:00
c->pb = [[NSPopUpButton alloc] initWithFrame:NSZeroRect pullsDown:NO];
[c->pb setPreferredEdge:NSMinYEdge];
pbcell = (NSPopUpButtonCell *) [c->pb cell];
[pbcell setArrowPosition:NSPopUpArrowAtBottom];
2015-08-14 20:46:35 -05:00
// TODO font
c->handle = c->pb;
2015-08-22 11:48:48 -05:00
// NSPopUpButton doesn't work like a combobox
// - it automatically selects the first item
// - it doesn't support duplicates
// but we can use a NSArrayController and Cocoa bindings to bypass these restrictions
c->pbac = [NSArrayController new];
[c->pbac setAvoidsEmptySelection:NO];
[c->pbac setSelectsInsertedObjects:NO];
[c->pbac setAutomaticallyRearrangesObjects:NO];
[c->pb bind:@"contentValues"
toObject:c->pbac
withKeyPath:@"arrangedObjects"
options:nil];
[c->pb bind:@"selectedIndex"
toObject:c->pbac
withKeyPath:@"selectionIndex"
options:nil];
2015-08-14 20:46:35 -05:00
}
if (comboboxDelegate == nil) {
comboboxDelegate = [comboboxDelegateClass new];
[delegates addObject:comboboxDelegate];
}
[comboboxDelegate registerCombobox:c];
uiComboboxOnSelected(c, defaultOnSelected, NULL);
2015-08-14 20:46:35 -05:00
uiDarwinFinishNewControl(c, uiCombobox);
return c;
}
uiCombobox *uiNewCombobox(void)
{
return finishNewCombobox(NO);
}
uiCombobox *uiNewEditableCombobox(void)
{
return finishNewCombobox(YES);
}