libui/zOLD_darwin/combobox.m

146 lines
3.3 KiB
Mathematica
Raw Normal View History

2015-08-14 20:46:35 -05:00
// 14 august 2015
#import "uipriv_darwin.h"
2015-08-14 20:46:35 -05:00
// NSComboBoxes have no intrinsic width; we'll use the default Interface Builder width for them.
// NSPopUpButton is fine.
#define comboboxWidth 96
2015-08-14 20:46:35 -05:00
struct uiCombobox {
uiDarwinControl c;
NSPopUpButton *pb;
2015-08-22 11:48:48 -05:00
NSArrayController *pbac;
void (*onSelected)(uiCombobox *, void *);
void *onSelectedData;
2015-08-14 20:46:35 -05:00
};
2016-05-23 17:50:02 -05:00
@interface comboboxDelegateClass : NSObject {
uiprivMap *comboboxes;
}
- (IBAction)onSelected:(id)sender;
- (void)registerCombobox:(uiCombobox *)c;
- (void)unregisterCombobox:(uiCombobox *)c;
@end
@implementation comboboxDelegateClass
- (id)init
{
self = [super init];
if (self)
self->comboboxes = uiprivNewMap();
return self;
}
- (void)dealloc
{
uiprivMapDestroy(self->comboboxes);
[super dealloc];
}
- (IBAction)onSelected:(id)sender
{
uiCombobox *c;
c = uiCombobox(uiprivMapGet(self->comboboxes, sender));
(*(c->onSelected))(c, c->onSelectedData);
}
- (void)registerCombobox:(uiCombobox *)c
{
uiprivMapSet(self->comboboxes, c->pb, c);
2016-05-23 17:50:02 -05:00
[c->pb setTarget:self];
[c->pb setAction:@selector(onSelected:)];
}
- (void)unregisterCombobox:(uiCombobox *)c
{
2016-05-23 17:50:02 -05:00
[c->pb setTarget:nil];
uiprivMapDelete(self->comboboxes, c->pb);
}
@end
static comboboxDelegateClass *comboboxDelegate = nil;
2016-05-23 17:50:02 -05:00
uiDarwinControlAllDefaultsExceptDestroy(uiCombobox, pb)
2015-08-22 11:48:48 -05:00
static void uiComboboxDestroy(uiControl *cc)
2015-08-22 11:48:48 -05:00
{
uiCombobox *c = uiCombobox(cc);
[comboboxDelegate unregisterCombobox:c];
2016-05-23 17:50:02 -05:00
[c->pb unbind:@"contentObjects"];
[c->pb unbind:@"selectedIndex"];
[c->pbac release];
[c->pb release];
uiFreeControl(uiControl(c));
2015-08-22 11:48:48 -05:00
}
2015-08-14 20:46:35 -05:00
void uiComboboxAppend(uiCombobox *c, const char *text)
{
[c->pbac addObject:uiprivToNSString(text)];
2015-08-14 20:46:35 -05:00
}
2016-06-13 20:05:40 -05:00
int uiComboboxSelected(uiCombobox *c)
{
return [c->pb indexOfSelectedItem];
}
2016-06-13 20:05:40 -05:00
void uiComboboxSetSelected(uiCombobox *c, int n)
{
[c->pb selectItemAtIndex:n];
}
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
}
2016-05-23 17:50:02 -05:00
uiCombobox *uiNewCombobox(void)
2015-08-14 20:46:35 -05:00
{
uiCombobox *c;
2016-05-23 17:50:02 -05:00
NSPopUpButtonCell *pbcell;
2015-08-14 20:46:35 -05:00
uiDarwinNewControl(uiCombobox, c);
2015-08-14 20:46:35 -05:00
2016-05-23 17:50:02 -05:00
c->pb = [[NSPopUpButton alloc] initWithFrame:NSZeroRect pullsDown:NO];
[c->pb setPreferredEdge:NSMinYEdge];
pbcell = (NSPopUpButtonCell *) [c->pb cell];
[pbcell setArrowPosition:NSPopUpArrowAtBottom];
// the font defined by Interface Builder is Menu 13, which is lol
// just use the regular control size for consistency
uiDarwinSetControlFont(c->pb, NSRegularControlSize);
2016-05-23 17:50:02 -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] autorelease];
2018-05-05 18:46:57 -05:00
[uiprivDelegates addObject:comboboxDelegate];
}
[comboboxDelegate registerCombobox:c];
uiComboboxOnSelected(c, defaultOnSelected, NULL);
2015-08-14 20:46:35 -05:00
return c;
}