summaryrefslogtreecommitdiff
path: root/prev/spinbox_darwin.m
blob: fddfbfd0cf75b9173cea36d0318a53818b6082ac (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
// 29 october 2014

#include "objc_darwin.h"
#include "_cgo_export.h"
#import <Cocoa/Cocoa.h>

#define togoSpinbox(x) ((goSpinbox *) (x))

@interface goSpinbox : NSObject {
@public
	void *gospinbox;
	NSTextField *textfield;
	NSNumberFormatter *formatter;
	NSStepper *stepper;

	NSInteger value;
	NSInteger minimum;
	NSInteger maximum;
}
@end

@implementation goSpinbox

- (id)initWithMinimum:(NSInteger)minimum maximum:(NSInteger)maximum
{
	self = [super init];
	if (self == nil)
		return nil;

	self->textfield = (NSTextField *) newTextField();

	self->formatter = [NSNumberFormatter new];
	[self->formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
	[self->formatter setLocalizesFormat:NO];
	[self->formatter setUsesGroupingSeparator:NO];
	[self->formatter setHasThousandSeparators:NO];
	[self->formatter setAllowsFloats:NO];
	// TODO partial string validation?
	[self->textfield setFormatter:self->formatter];

	self->stepper = [[NSStepper alloc] initWithFrame:NSZeroRect];
	[self->stepper setIncrement:1];
	[self->stepper setValueWraps:NO];
	[self->stepper setAutorepeat:YES];		// hold mouse button to step repeatedly

	// TODO how SHOULD the formatter treat invald input?

	[self setMinimum:minimum];
	[self setMaximum:maximum];
	[self setValue:self->minimum];

	[self->textfield setDelegate:self];
	[self->stepper setTarget:self];
	[self->stepper setAction:@selector(stepperClicked:)];

	return self;
}

- (void)setValue:(NSInteger)value
{
	self->value = value;
	if (self->value < self->minimum)
		self->value = self->minimum;
	if (self->value > self->maximum)
		self->value = self->maximum;
	[self->textfield setIntegerValue:self->value];
	[self->stepper setIntegerValue:self->value];
}

- (void)setMinimum:(NSInteger)min
{
	self->minimum = min;
	[self->formatter setMinimum:[NSNumber numberWithInteger:self->minimum]];
	[self->stepper setMinValue:((double) (self->minimum))];
}

- (void)setMaximum:(NSInteger)max
{
	self->maximum = max;
	[self->formatter setMaximum:[NSNumber numberWithInteger:self->maximum]];
	[self->stepper setMaxValue:((double) (self->maximum))];
}

- (IBAction)stepperClicked:(id)sender
{
	[self setValue:[self->stepper integerValue]];
	spinboxChanged(self->gospinbox);
}

- (void)controlTextDidChange:(NSNotification *)note
{
	[self setValue:[self->textfield integerValue]];
	spinboxChanged(self->gospinbox);
}

@end

id newSpinbox(void *gospinbox, intmax_t minimum, intmax_t maximum)
{
	goSpinbox *s;

	s = [[goSpinbox new] initWithMinimum:((NSInteger) minimum) maximum:((NSInteger) maximum)];
	s->gospinbox = gospinbox;
	return s;
}

id spinboxTextField(id spinbox)
{
	return (id) (togoSpinbox(spinbox)->textfield);
}

id spinboxStepper(id spinbox)
{
	return (id) (togoSpinbox(spinbox)->stepper);
}

intmax_t spinboxValue(id spinbox)
{
	return (intmax_t) (togoSpinbox(spinbox)->value);
}

void spinboxSetValue(id spinbox, intmax_t value)
{
	[togoSpinbox(spinbox) setValue:((NSInteger) value)];
}