libui/darwin/combobox.m

95 lines
2.2 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()
2015-08-14 20:46:35 -05:00
};
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)
{
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
}
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
}
uiDarwinFinishNewControl(c, uiCombobox);
return c;
}
uiCombobox *uiNewCombobox(void)
{
return finishNewCombobox(NO);
}
uiCombobox *uiNewEditableCombobox(void)
{
return finishNewCombobox(YES);
}