2015-08-14 20:46:35 -05:00
|
|
|
// 14 august 2015
|
2015-10-09 17:49:41 -05:00
|
|
|
#import "uipriv_darwin.h"
|
2015-08-14 20:46:35 -05:00
|
|
|
|
2015-10-09 13:39:26 -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;
|
2015-10-08 15:32:12 -05:00
|
|
|
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 {
|
2016-01-07 13:41:20 -06:00
|
|
|
struct mapTable *comboboxes;
|
2015-10-08 15:32:12 -05:00
|
|
|
}
|
|
|
|
- (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
|
|
|
|
{
|
2016-01-07 13:41:20 -06:00
|
|
|
mapDestroy(self->comboboxes);
|
2015-10-08 15:32:12 -05:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)onSelected:(id)sender
|
|
|
|
{
|
|
|
|
uiCombobox *c;
|
|
|
|
|
2016-05-23 17:50:02 -05:00
|
|
|
c = uiCombobox(mapGet(self->comboboxes, sender));
|
2015-10-08 15:32:12 -05:00
|
|
|
(*(c->onSelected))(c, c->onSelectedData);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)registerCombobox:(uiCombobox *)c
|
|
|
|
{
|
2016-05-23 17:50:02 -05:00
|
|
|
mapSet(self->comboboxes, c->pb, c);
|
|
|
|
[c->pb setTarget:self];
|
|
|
|
[c->pb setAction:@selector(onSelected:)];
|
2015-10-08 15:32:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)unregisterCombobox:(uiCombobox *)c
|
|
|
|
{
|
2016-05-23 17:50:02 -05:00
|
|
|
[c->pb setTarget:nil];
|
|
|
|
mapDelete(self->comboboxes, c->pb);
|
2015-10-08 15:32:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
static comboboxDelegateClass *comboboxDelegate = nil;
|
|
|
|
|
2016-05-23 17:50:02 -05:00
|
|
|
uiDarwinControlAllDefaultsExceptDestroy(uiCombobox, pb)
|
2015-08-22 11:48:48 -05:00
|
|
|
|
2016-04-25 09:23:16 -05:00
|
|
|
static void uiComboboxDestroy(uiControl *cc)
|
2015-08-22 11:48:48 -05:00
|
|
|
{
|
2016-04-25 09:23:16 -05:00
|
|
|
uiCombobox *c = uiCombobox(cc);
|
|
|
|
|
2015-10-08 15:32:12 -05:00
|
|
|
[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];
|
2016-04-25 11:52:54 -05:00
|
|
|
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)
|
|
|
|
{
|
2016-05-23 17:50:02 -05:00
|
|
|
[c->pbac addObject:toNSString(text)];
|
2015-08-14 20:46:35 -05:00
|
|
|
}
|
|
|
|
|
2015-10-08 15:32:12 -05:00
|
|
|
intmax_t uiComboboxSelected(uiCombobox *c)
|
|
|
|
{
|
|
|
|
return [c->pb indexOfSelectedItem];
|
|
|
|
}
|
|
|
|
|
2015-10-09 13:39:26 -05:00
|
|
|
void uiComboboxSetSelected(uiCombobox *c, intmax_t n)
|
|
|
|
{
|
|
|
|
[c->pb selectItemAtIndex:n];
|
|
|
|
}
|
|
|
|
|
2015-10-08 15:32:12 -05:00
|
|
|
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
|
|
|
|
2016-04-25 09:23:16 -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];
|
|
|
|
// TODO font
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2015-10-08 15:32:12 -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
|
|
|
return c;
|
|
|
|
}
|