aboutsummaryrefslogtreecommitdiff
path: root/src/libui_sdl/libui/darwin/combobox.m
blob: 89a2e28cb89d3204917fb7f2e4e2ac27c823c369 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// 14 august 2015
#import "uipriv_darwin.h"

// NSComboBoxes have no intrinsic width; we'll use the default Interface Builder width for them.
// NSPopUpButton is fine.
#define comboboxWidth 96

struct uiCombobox {
	uiDarwinControl c;
	NSPopUpButton *pb;
	NSArrayController *pbac;
	void (*onSelected)(uiCombobox *, void *);
	void *onSelectedData;
};

@interface comboboxDelegateClass : NSObject {
	struct mapTable *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 = newMap();
	return self;
}

- (void)dealloc
{
	mapDestroy(self->comboboxes);
	[super dealloc];
}

- (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->pb, c);
	[c->pb setTarget:self];
	[c->pb setAction:@selector(onSelected:)];
}

- (void)unregisterCombobox:(uiCombobox *)c
{
	[c->pb setTarget:nil];
	mapDelete(self->comboboxes, c->pb);
}

@end

static comboboxDelegateClass *comboboxDelegate = nil;

uiDarwinControlAllDefaultsExceptDestroy(uiCombobox, pb)

static void uiComboboxDestroy(uiControl *cc)
{
	uiCombobox *c = uiCombobox(cc);

	[comboboxDelegate unregisterCombobox:c];
	[c->pb unbind:@"contentObjects"];
	[c->pb unbind:@"selectedIndex"];
	[c->pbac release];
	[c->pb release];
	uiFreeControl(uiControl(c));
}

void uiComboboxAppend(uiCombobox *c, const char *text)
{
	[c->pbac addObject:toNSString(text)];
}

int uiComboboxSelected(uiCombobox *c)
{
	return [c->pb indexOfSelectedItem];
}

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
}

uiCombobox *uiNewCombobox(void)
{
	uiCombobox *c;
	NSPopUpButtonCell *pbcell;

	uiDarwinNewControl(uiCombobox, c);

	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);

	// 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];

	if (comboboxDelegate == nil) {
		comboboxDelegate = [[comboboxDelegateClass new] autorelease];
		[delegates addObject:comboboxDelegate];
	}
	[comboboxDelegate registerCombobox:c];
	uiComboboxOnSelected(c, defaultOnSelected, NULL);

	return c;
}