2014-10-29 11:12:00 -05:00
|
|
|
// 29 october 2014
|
|
|
|
|
|
|
|
#include "objc_darwin.h"
|
|
|
|
#include "_cgo_export.h"
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
2014-10-29 19:34:41 -05:00
|
|
|
#define togoSpinbox(x) ((goSpinbox *) (x))
|
|
|
|
|
2014-10-29 11:12:00 -05:00
|
|
|
@interface goSpinbox : NSObject {
|
|
|
|
@public
|
|
|
|
void *gospinbox;
|
2014-10-29 19:34:41 -05:00
|
|
|
NSTextField *textfield;
|
|
|
|
NSNumberFormatter *formatter;
|
|
|
|
NSStepper *stepper;
|
2014-10-29 21:38:34 -05:00
|
|
|
|
|
|
|
NSInteger value;
|
|
|
|
NSInteger minimum;
|
|
|
|
NSInteger maximum;
|
2014-10-29 11:12:00 -05:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation goSpinbox
|
2014-10-29 21:38:34 -05:00
|
|
|
|
2014-10-30 13:35:31 -05:00
|
|
|
- (id)initWithMinimum:(NSInteger)minimum maximum:(NSInteger)maximum
|
2014-10-29 21:38:34 -05:00
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self == nil)
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
self->textfield = (NSTextField *) newTextField();
|
2014-10-30 10:52:27 -05:00
|
|
|
|
2014-10-29 21:38:34 -05:00
|
|
|
self->formatter = [NSNumberFormatter new];
|
2014-10-30 10:52:27 -05:00
|
|
|
[self->formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
|
|
|
|
[self->formatter setLocalizesFormat:NO];
|
|
|
|
[self->formatter setUsesGroupingSeparator:NO];
|
|
|
|
[self->formatter setHasThousandSeparators:NO];
|
2014-10-29 21:38:34 -05:00
|
|
|
[self->formatter setAllowsFloats:NO];
|
2014-10-30 10:52:27 -05:00
|
|
|
// TODO partial string validation?
|
2014-10-29 21:38:34 -05:00
|
|
|
[self->textfield setFormatter:self->formatter];
|
2014-10-30 10:52:27 -05:00
|
|
|
|
2014-10-29 21:38:34 -05:00
|
|
|
self->stepper = [[NSStepper alloc] initWithFrame:NSZeroRect];
|
|
|
|
[self->stepper setIncrement:1];
|
2014-10-30 13:35:31 -05:00
|
|
|
[self->stepper setValueWraps:NO];
|
2014-10-29 21:38:34 -05:00
|
|
|
[self->stepper setAutorepeat:YES]; // hold mouse button to step repeatedly
|
|
|
|
|
|
|
|
// TODO how SHOULD the formatter treat invald input?
|
|
|
|
|
2014-10-30 13:35:31 -05:00
|
|
|
[self setMinimum:minimum];
|
|
|
|
[self setMaximum:maximum];
|
|
|
|
[self setValue:self->minimum];
|
2014-10-29 21:38:34 -05:00
|
|
|
|
|
|
|
[self->textfield setDelegate:self];
|
|
|
|
[self->stepper setTarget:self];
|
|
|
|
[self->stepper setAction:@selector(stepperClicked:)];
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setValue:(NSInteger)value
|
|
|
|
{
|
2014-10-30 13:35:31 -05:00
|
|
|
if (self->value < self->minimum)
|
|
|
|
self->value = self->minimum;
|
|
|
|
if (self->value > self->maximum)
|
|
|
|
self->value = self->maximum;
|
2014-10-29 21:38:34 -05:00
|
|
|
self->value = value;
|
2014-10-30 13:35:31 -05:00
|
|
|
// TODO does not work?
|
|
|
|
[self->textfield setIntegerValue:self->value];
|
|
|
|
[self->stepper setIntegerValue:self->value];
|
2014-10-29 21:38:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setMinimum:(NSInteger)min
|
|
|
|
{
|
|
|
|
self->minimum = min;
|
2014-10-30 13:35:31 -05:00
|
|
|
[self->formatter setMinimum:[NSNumber numberWithInteger:self->minimum]];
|
|
|
|
[self->stepper setMinValue:((double) (self->minimum))];
|
2014-10-29 21:38:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setMaximum:(NSInteger)max
|
|
|
|
{
|
|
|
|
self->maximum = max;
|
2014-10-30 13:35:31 -05:00
|
|
|
[self->formatter setMaximum:[NSNumber numberWithInteger:self->maximum]];
|
|
|
|
[self->stepper setMaxValue:((double) (self->maximum))];
|
2014-10-29 21:38:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)stepperClicked:(id)sender
|
|
|
|
{
|
2014-10-30 13:35:31 -05:00
|
|
|
NSLog(@"stepperClicked %d\n", [self->stepper integerValue]);
|
2014-10-29 21:38:34 -05:00
|
|
|
[self setValue:[self->stepper integerValue]];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)controlTextDidChange:(NSNotification *)note
|
|
|
|
{
|
2014-10-30 13:35:31 -05:00
|
|
|
NSLog(@"controlTextDidChange %d\n", [self->textfield integerValue]);
|
2014-10-29 21:38:34 -05:00
|
|
|
[self setValue:[self->textfield integerValue]];
|
|
|
|
}
|
|
|
|
|
2014-10-29 11:12:00 -05:00
|
|
|
@end
|
|
|
|
|
2014-10-30 13:35:31 -05:00
|
|
|
id newSpinbox(void *gospinbox, intmax_t minimum, intmax_t maximum)
|
2014-10-29 11:12:00 -05:00
|
|
|
{
|
2014-10-29 19:34:41 -05:00
|
|
|
goSpinbox *s;
|
|
|
|
|
2014-10-30 13:35:31 -05:00
|
|
|
s = [[goSpinbox new] initWithMinimum:((NSInteger) minimum) maximum:((NSInteger) maximum)];
|
2014-10-29 19:34:41 -05:00
|
|
|
s->gospinbox = gospinbox;
|
2014-10-29 21:38:34 -05:00
|
|
|
return s;
|
2014-10-29 11:12:00 -05:00
|
|
|
}
|
|
|
|
|
2014-10-29 19:34:41 -05:00
|
|
|
id spinboxTextField(id spinbox)
|
|
|
|
{
|
|
|
|
return (id) (togoSpinbox(spinbox)->textfield);
|
|
|
|
}
|
|
|
|
|
|
|
|
id spinboxStepper(id spinbox)
|
2014-10-29 11:12:00 -05:00
|
|
|
{
|
2014-10-29 19:34:41 -05:00
|
|
|
return (id) (togoSpinbox(spinbox)->stepper);
|
2014-10-29 11:12:00 -05:00
|
|
|
}
|
2014-10-30 13:35:31 -05:00
|
|
|
|
|
|
|
intmax_t spinboxValue(id spinbox)
|
|
|
|
{
|
|
|
|
return (intmax_t) (togoSpinbox(spinbox)->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void spinboxSetValue(id spinbox, intmax_t value)
|
|
|
|
{
|
|
|
|
[togoSpinbox(spinbox) setValue:((NSInteger) value)];
|
|
|
|
}
|