summaryrefslogtreecommitdiff
path: root/experiments/combobox_darwin.m
blob: da9b658f5f262aa596fde0c5e02b73475dc6dff9 (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
146
147
148
149
150
151
152
153
154
155
156
// 17 may 2014

#include "objc_darwin.h"
#include <AppKit/NSPopUpButton.h>
#include <AppKit/NSComboBox.h>
#include <AppKit/NSArrayController.h>

/*
Cocoa doesn't have combo boxes in the sense that other systems do. NSPopUpButton is not editable and technically behaves like a menu on a menubar. NSComboBox is editable and is the more traditional combo box, but the edit field and list are even more separated than they are on other platforms.

Unfortunately, their default internal storage mechanisms exhibit the automatic selection behavior I DON'T want, so we're going to have to do that ourselves.

You will notice that a bunch of functions here call functions in listbox_darwin.m. How convenient =P
*/

@interface combobox : NSObject {
@public
	NSPopUpButton *pb;
	NSComboBox *cb;
	NSArrayController *ac;
}
// and these are so we can use a combobox like a reegular control
- (void)setHidden:(BOOL)hidden;
- (void)setFont:(NSFont *)font;
- (void)setFrame:(NSRect)r;
- (NSRect)frame;
- (void)sizeToFit;
@end

@implementation combobox

#define OVERRIDE(sig, msg) \
	sig \
	{ \
		if (pb != nil) { \
			[pb msg]; \
			return; \
		} \
		[cb msg]; \
	}

OVERRIDE(- (void)setHidden:(BOOL)hidden, setHidden:hidden)
OVERRIDE(- (void)setFont:(NSFont *)font, setFont:font)
OVERRIDE(- (void)setFrame:(NSRect)r, setFrame:r)

- (NSRect)frame
{
	if (pb != nil)
		return [pb frame];
	return [cb frame];
}

OVERRIDE(- (void)sizeToFit, sizeToFit)

@end

extern NSRect dummyRect;

#define to(T, x) ((T *) (x))
#define tocombobox(x) to(combobox, (x))

#define toNSInteger(x) ((NSInteger) (x))
#define fromNSInteger(x) ((intptr_t) (x))

#define COMBOBOXKEY @"cbitem"
static NSString *comboboxKey = COMBOBOXKEY;
static NSString *comboboxKeyPath = @"arrangedObjects." COMBOBOXKEY;

id makeCombobox(BOOL editable)
{
	combobox *c;

	c = [combobox new];
	c->pb = nil;
	c->cb = nil;
	c->ac = makeListboxArray();
#define BIND bind:@"contentValues" toObject:c->ac withKeyPath:comboboxKeyPath options:nil
	if (!editable) {
		c->pb = [[NSPopUpButton alloc]
			initWithFrame:dummyRect
			pullsDown:NO];
		[c->pb BIND];
	} else {
		c->cb = [[NSComboBox alloc]
			initWithFrame:dummyRect];
		[c->cb setUsesDataSource:NO];
		[c->cb BIND];
	}
	return c;
}

void addCombobox(id parentWindow, id cbox)
{
	combobox *c;

	c = tocombobox(cbox);
	if (c->pb != nil) {
		addControl(parentWindow, c->pb);
		return;
	}
	addControl(parentWindow, c->cb);
}

id comboboxText(id cbox, BOOL editable)
{
	combobox *c;

	c = tocombobox(cbox);
	if (!editable)
		return [c->pb titleOfSelectedItem];
	return [c->cb stringValue];
}

void comboboxAppend(id cbox, BOOL editable, id str)
{
	combobox *c;

	c = tocombobox(cbox);
	listboxArrayAppend(c->ac, toListboxItem(comboboxKey, str));
}

void comboboxInsertBefore(id cbox, BOOL editable, id str, intptr_t before)
{
	combobox *c;

	c = tocombobox(cbox);
	listboxArrayInsertBefore(c->ac, toListboxItem(comboboxKey, str), before);
}

intptr_t comboboxSelectedIndex(id cbox)
{
	combobox *c;

	c = tocombobox(cbox);
	if (c->pb != nil)
		return fromNSInteger([c->pb indexOfSelectedItem]);
	return fromNSInteger([c->cb indexOfSelectedItem]);
}

void comboboxDelete(id cbox, intptr_t index)
{
	combobox *c;

	c = tocombobox(cbox);
	listboxArrayDelete(c->ac, index);
}

intptr_t comboboxLen(id cbox)
{
	combobox *c;

	c = tocombobox(cbox);
	if (c->pb != nil)
		return fromNSInteger([c->pb numberOfItems]);
	return fromNSInteger([c->cb numberOfItems]);
}