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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
// 16 july 2014
#import "objc_darwin.h"
#import "_cgo_export.h"
#import <Cocoa/Cocoa.h>
#define toNSButton(x) ((NSButton *) (x))
#define toNSTextField(x) ((NSTextField *) (x))
#define toNSView(x) ((NSView *) (x))
#define toNSWindow(x) ((NSWindow *) (x))
#define toNSBox(x) ((NSBox *) (x))
#define toNSTextView(x) ((NSTextView *) (x))
#define toNSProgressIndicator(x) ((NSProgressIndicator *) (x))
@interface goControlDelegate : NSObject <NSTextFieldDelegate> {
@public
void *gocontrol;
}
@end
@implementation goControlDelegate
- (IBAction)buttonClicked:(id)sender
{
buttonClicked(self->gocontrol);
}
- (IBAction)checkboxToggled:(id)sender
{
checkboxToggled(self->gocontrol);
}
- (void)controlTextDidChange:(NSNotification *)note
{
textfieldChanged(self->gocontrol);
}
@end
id newButton(void)
{
NSButton *b;
b = [[NSButton alloc] initWithFrame:NSZeroRect];
[b setButtonType:NSMomentaryPushInButton];
[b setBordered:YES];
[b setBezelStyle:NSRoundedBezelStyle];
setStandardControlFont((id) b);
return (id) b;
}
void buttonSetDelegate(id button, void *b)
{
goControlDelegate *d;
d = [goControlDelegate new];
d->gocontrol = b;
[toNSButton(button) setTarget:d];
[toNSButton(button) setAction:@selector(buttonClicked:)];
}
const char *buttonText(id button)
{
return [[toNSButton(button) title] UTF8String];
}
void buttonSetText(id button, char *text)
{
[toNSButton(button) setTitle:[NSString stringWithUTF8String:text]];
}
id newCheckbox(void)
{
NSButton *c;
c = [[NSButton alloc] initWithFrame:NSZeroRect];
[c setButtonType:NSSwitchButton];
[c setBordered:NO];
setStandardControlFont((id) c);
return (id) c;
}
void checkboxSetDelegate(id checkbox, void *b)
{
goControlDelegate *d;
d = [goControlDelegate new];
d->gocontrol = b;
[toNSButton(checkbox) setTarget:d];
[toNSButton(checkbox) setAction:@selector(checkboxToggled:)];
}
BOOL checkboxChecked(id c)
{
if ([toNSButton(c) state] == NSOnState)
return YES;
return NO;
}
void checkboxSetChecked(id c, BOOL checked)
{
NSInteger state;
state = NSOnState;
if (checked == NO)
state = NSOffState;
[toNSButton(c) setState:state];
}
// also good for labels
// not static because area_darwin.m uses it
id finishNewTextField(id _t, BOOL bordered)
{
NSTextField *t = toNSTextField(_t);
// same for text fields, password fields, and labels
setStandardControlFont((id) t);
// these three are the same across text fields, password fields, and labels; the only difference is the setBezeled: value, and it's only different on labels
// THE ORDER OF THESE CALLS IS IMPORTANT; CHANGE IT AND THE BORDERS WILL DISAPPEAR
[t setBordered:NO];
[t setBezelStyle:NSTextFieldSquareBezel];
[t setBezeled:bordered];
// smart quotes and other autocorrect features are handled by the window; see newWindow() in window_darwin.m for details
// Interface Builder does this to make the text box behave properly
// this disables both word wrap AND ellipsizing in one fell swoop
// however, we need to send it to the control's cell, not to the control directly
[[t cell] setLineBreakMode:NSLineBreakByClipping];
// Interface Builder also sets this to allow horizontal scrolling
// it also sets this for labels, despite those not being scrollable
[[t cell] setScrollable:YES];
return (id) t;
}
id newTextField(void)
{
NSTextField *t;
t = [[NSTextField alloc] initWithFrame:NSZeroRect];
[t setSelectable:YES]; // otherwise the setting is masked by the editable default of YES
return finishNewTextField((id) t, YES);
}
id newPasswordField(void)
{
NSSecureTextField *t;
t = [[NSSecureTextField alloc] initWithFrame:NSZeroRect];
[t setSelectable:YES]; // otherwise the setting is masked by the editable default of YES
return finishNewTextField((id) t, YES);
}
void textfieldSetDelegate(id textfield, void *t)
{
goControlDelegate *d;
d = [goControlDelegate new];
d->gocontrol = t;
[toNSTextField(textfield) setDelegate:d];
}
// also good for labels
const char *textfieldText(id t)
{
return [[toNSTextField(t) stringValue] UTF8String];
}
// also good for labels
void textfieldSetText(id t, char *text)
{
[toNSTextField(t) setStringValue:[NSString stringWithUTF8String:text]];
}
id textfieldOpenInvalidPopover(id textfield, char *reason)
{
id popover;
popover = newWarningPopover(reason);
warningPopoverShow(popover, textfield);
NSBeep();
return (id) popover;
}
void textfieldCloseInvalidPopover(id popover)
{
[toNSWindow(popover) close];
// don't release; close does that already
}
BOOL textfieldEditable(id textfield)
{
return [toNSTextField(textfield) isEditable];
}
void textfieldSetEditable(id textfield, BOOL editable)
{
[toNSTextField(textfield) setEditable:editable];
}
id newLabel(void)
{
NSTextField *l;
l = [[NSTextField alloc] initWithFrame:NSZeroRect];
[l setEditable:NO];
[l setSelectable:NO];
[l setDrawsBackground:NO];
return finishNewTextField((id) l, NO);
}
id newGroup(id container)
{
NSBox *group;
group = [[NSBox alloc] initWithFrame:NSZeroRect];
[group setBorderType:NSLineBorder];
[group setBoxType:NSBoxPrimary];
[group setTransparent:NO];
// can't use setSmallControlFont() here because the selector is different
[group setTitleFont:[NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]];
[group setTitlePosition:NSAtTop];
[group setContentView:toNSView(container)];
return (id) group;
}
const char *groupText(id group)
{
return [[toNSBox(group) title] UTF8String];
}
void groupSetText(id group, char *text)
{
[toNSBox(group) setTitle:[NSString stringWithUTF8String:text]];
}
id newTextbox(void)
{
NSTextView *tv;
tv = [[NSTextView alloc] initWithFrame:NSZeroRect];
// verified against Interface Builder, except for rich text options
[tv setAllowsDocumentBackgroundColorChange:NO];
[tv setBackgroundColor:[NSColor textBackgroundColor]];
[tv setTextColor:[NSColor textColor]];
[tv setAllowsUndo:YES];
[tv setEditable:YES];
[tv setSelectable:YES];
[tv setRichText:NO];
[tv setImportsGraphics:NO];
[tv setBaseWritingDirection:NSWritingDirectionNatural];
// TODO default paragraph format
[tv setAllowsImageEditing:NO];
[tv setAutomaticQuoteSubstitutionEnabled:NO];
[tv setAutomaticLinkDetectionEnabled:NO];
[tv setUsesRuler:NO];
[tv setRulerVisible:NO];
[tv setUsesInspectorBar:NO];
[tv setSelectionGranularity:NSSelectByCharacter];
//TODO [tv setInsertionPointColor:[NSColor insertionColor]];
[tv setContinuousSpellCheckingEnabled:NO];
[tv setGrammarCheckingEnabled:NO];
[tv setUsesFontPanel:NO];
[tv setEnabledTextCheckingTypes:0];
[tv setAutomaticDashSubstitutionEnabled:NO];
[tv setAutomaticSpellingCorrectionEnabled:NO];
[tv setAutomaticTextReplacementEnabled:NO];
[tv setSmartInsertDeleteEnabled:NO];
[tv setLayoutOrientation:NSTextLayoutOrientationHorizontal];
// TODO default find panel behavior
// now just to be safe; this will do some of the above but whatever
disableAutocorrect((id) tv);
// this option is complex; just set it to the Interface Builder default
[[tv layoutManager] setAllowsNonContiguousLayout:YES];
// this will work because it's the same selector
setStandardControlFont((id) tv);
return (id) tv;
}
char *textboxText(id tv)
{
return [[toNSTextView(tv) string] UTF8String];
}
void textboxSetText(id tv, char *text)
{
[toNSTextView(tv) setString:[NSString stringWithUTF8String:text]];
}
id newProgressBar(void)
{
NSProgressIndicator *pi;
pi = [[NSProgressIndicator alloc] initWithFrame:NSZeroRect];
[pi setStyle:NSProgressIndicatorBarStyle];
[pi setControlSize:NSRegularControlSize];
[pi setControlTint:NSDefaultControlTint];
[pi setBezeled:YES];
[pi setDisplayedWhenStopped:YES];
[pi setUsesThreadedAnimation:YES];
[pi setIndeterminate:NO];
[pi setMinValue:0];
[pi setMaxValue:100];
[pi setDoubleValue:0];
return (id) pi;
}
intmax_t progressbarPercent(id pbar)
{
return (intmax_t) [toNSProgressIndicator(pbar) doubleValue];
}
void progressbarSetPercent(id pbar, intmax_t percent)
{
[toNSProgressIndicator(pbar) setDoubleValue:((double) percent)];
}
|