2015-08-14 22:50:38 -05:00
|
|
|
// 14 august 2015
|
|
|
|
#import "uipriv_darwin.h"
|
|
|
|
|
2015-08-21 20:03:50 -05:00
|
|
|
@interface libui_spinbox : NSView<NSTextFieldDelegate> {
|
|
|
|
NSTextField *tf;
|
|
|
|
NSNumberFormatter *formatter;
|
|
|
|
NSStepper *stepper;
|
|
|
|
|
|
|
|
NSInteger value;
|
|
|
|
NSInteger minimum;
|
|
|
|
NSInteger maximum;
|
|
|
|
|
|
|
|
uiSpinbox *spinbox;
|
|
|
|
}
|
|
|
|
- (id)initWithFrame:(NSRect)r spinbox:(uiSpinbox *)sb;
|
|
|
|
// see https://github.com/andlabs/ui/issues/82
|
|
|
|
- (NSInteger)libui_value;
|
|
|
|
- (void)libui_setValue:(NSInteger)val;
|
|
|
|
- (void)setMinimum:(NSInteger)min;
|
|
|
|
- (void)setMaximum:(NSInteger)max;
|
|
|
|
- (IBAction)stepperClicked:(id)sender;
|
|
|
|
- (void)controlTextDidChange:(NSNotification *)note;
|
|
|
|
@end
|
|
|
|
|
2015-08-14 22:50:38 -05:00
|
|
|
struct uiSpinbox {
|
|
|
|
uiDarwinControl c;
|
2015-08-21 20:03:50 -05:00
|
|
|
libui_spinbox *spinbox;
|
2015-08-14 22:50:38 -05:00
|
|
|
void (*onChanged)(uiSpinbox *, void *);
|
|
|
|
void *onChangedData;
|
|
|
|
};
|
|
|
|
|
2015-08-21 20:03:50 -05:00
|
|
|
@implementation libui_spinbox
|
|
|
|
|
|
|
|
- (id)initWithFrame:(NSRect)r spinbox:(uiSpinbox *)sb
|
|
|
|
{
|
|
|
|
self = [super initWithFrame:r];
|
|
|
|
if (self) {
|
|
|
|
self->tf = newEditableTextField();
|
|
|
|
[self->tf setTranslatesAutoresizingMaskIntoConstraints:NO];
|
|
|
|
|
|
|
|
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];
|
|
|
|
[self->tf 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
|
|
|
|
[self->stepper setTranslatesAutoresizingMaskIntoConstraints:NO];
|
|
|
|
|
|
|
|
[self->tf setDelegate:self];
|
|
|
|
[self->stepper setTarget:self];
|
|
|
|
[self->stepper setAction:@selector(stepperClicked:)];
|
|
|
|
|
|
|
|
[self addSubview:self->tf];
|
|
|
|
[self addSubview:self->stepper];
|
2016-04-30 16:14:14 -05:00
|
|
|
|
|
|
|
[self addConstraint:mkConstraint(self->tf, NSLayoutAttributeLeading,
|
|
|
|
NSLayoutRelationEqual,
|
|
|
|
self, NSLayoutAttributeLeading,
|
|
|
|
1, 0,
|
|
|
|
@"uiSpinbox left edge")];
|
|
|
|
[self addConstraint:mkConstraint(self->stepper, NSLayoutAttributeTrailing,
|
|
|
|
NSLayoutRelationEqual,
|
|
|
|
self, NSLayoutAttributeTrailing,
|
|
|
|
1, 0,
|
|
|
|
@"uiSpinbox right edge")];
|
|
|
|
[self addConstraint:mkConstraint(self->tf, NSLayoutAttributeTop,
|
|
|
|
NSLayoutRelationEqual,
|
|
|
|
self, NSLayoutAttributeTop,
|
|
|
|
1, 0,
|
|
|
|
@"uiSpinbox top edge text field")];
|
|
|
|
[self addConstraint:mkConstraint(self->tf, NSLayoutAttributeBottom,
|
|
|
|
NSLayoutRelationEqual,
|
|
|
|
self, NSLayoutAttributeBottom,
|
|
|
|
1, 0,
|
|
|
|
@"uiSpinbox bottom edge text field")];
|
|
|
|
[self addConstraint:mkConstraint(self->stepper, NSLayoutAttributeTop,
|
|
|
|
NSLayoutRelationEqual,
|
|
|
|
self, NSLayoutAttributeTop,
|
2016-05-01 15:25:05 -05:00
|
|
|
1, -1, // TODO make sure this is right
|
2016-04-30 16:14:14 -05:00
|
|
|
@"uiSpinbox top edge stepper")];
|
|
|
|
[self addConstraint:mkConstraint(self->stepper, NSLayoutAttributeBottom,
|
|
|
|
NSLayoutRelationEqual,
|
|
|
|
self, NSLayoutAttributeBottom,
|
2016-05-01 15:25:05 -05:00
|
|
|
1, -1, // TODO make sure this is right
|
2016-04-30 16:14:14 -05:00
|
|
|
@"uiSpinbox bottom edge stepper")];
|
2016-04-30 17:40:09 -05:00
|
|
|
[self addConstraint:mkConstraint(self->tf, NSLayoutAttributeTrailing,
|
2016-04-30 16:14:14 -05:00
|
|
|
NSLayoutRelationEqual,
|
2016-04-30 17:07:36 -05:00
|
|
|
self->stepper, NSLayoutAttributeLeading,
|
2016-04-30 16:14:14 -05:00
|
|
|
1, -3, // TODO
|
|
|
|
@"uiSpinbox space between text field and stepper")];
|
2015-08-21 20:03:50 -05:00
|
|
|
|
|
|
|
self->spinbox = sb;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[self->tf setDelegate:nil];
|
|
|
|
[self->tf removeFromSuperview];
|
|
|
|
[self->tf release];
|
|
|
|
[self->formatter release];
|
|
|
|
[self->stepper setTarget:nil];
|
|
|
|
[self->stepper removeFromSuperview];
|
|
|
|
[self->stepper release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSInteger)libui_value
|
|
|
|
{
|
|
|
|
return self->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)libui_setValue:(NSInteger)val
|
|
|
|
{
|
|
|
|
self->value = val;
|
|
|
|
if (self->value < self->minimum)
|
|
|
|
self->value = self->minimum;
|
|
|
|
if (self->value > self->maximum)
|
|
|
|
self->value = self->maximum;
|
|
|
|
[self->tf 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 libui_setValue:[self->stepper integerValue]];
|
|
|
|
(*(self->spinbox->onChanged))(self->spinbox, self->spinbox->onChangedData);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)controlTextDidChange:(NSNotification *)note
|
|
|
|
{
|
|
|
|
[self libui_setValue:[self->tf integerValue]];
|
|
|
|
(*(self->spinbox->onChanged))(self->spinbox, self->spinbox->onChangedData);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2016-04-25 10:54:09 -05:00
|
|
|
uiDarwinControlAllDefaults(uiSpinbox, spinbox)
|
2015-08-14 22:50:38 -05:00
|
|
|
|
|
|
|
intmax_t uiSpinboxValue(uiSpinbox *s)
|
|
|
|
{
|
2015-08-21 20:03:50 -05:00
|
|
|
return [s->spinbox libui_value];
|
2015-08-14 22:50:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiSpinboxSetValue(uiSpinbox *s, intmax_t value)
|
|
|
|
{
|
2015-08-21 20:03:50 -05:00
|
|
|
[s->spinbox libui_setValue:value];
|
2015-08-14 22:50:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiSpinboxOnChanged(uiSpinbox *s, void (*f)(uiSpinbox *, void *), void *data)
|
|
|
|
{
|
|
|
|
s->onChanged = f;
|
|
|
|
s->onChangedData = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void defaultOnChanged(uiSpinbox *s, void *data)
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2015-08-16 22:44:23 -05:00
|
|
|
uiSpinbox *uiNewSpinbox(intmax_t min, intmax_t max)
|
2015-08-14 22:50:38 -05:00
|
|
|
{
|
|
|
|
uiSpinbox *s;
|
2016-05-22 19:11:52 -05:00
|
|
|
intmax_t temp;
|
2015-08-14 22:50:38 -05:00
|
|
|
|
2016-05-22 19:11:52 -05:00
|
|
|
if (min >= max) {
|
|
|
|
temp = min;
|
|
|
|
min = max;
|
|
|
|
max = temp;
|
|
|
|
}
|
2015-08-21 20:03:50 -05:00
|
|
|
|
2016-04-25 10:54:09 -05:00
|
|
|
uiDarwinNewControl(uiSpinbox, s);
|
2015-08-14 22:50:38 -05:00
|
|
|
|
2015-08-21 20:03:50 -05:00
|
|
|
s->spinbox = [[libui_spinbox alloc] initWithFrame:NSZeroRect spinbox:s];
|
|
|
|
[s->spinbox setMinimum:min];
|
|
|
|
[s->spinbox setMaximum:max];
|
|
|
|
[s->spinbox libui_setValue:min];
|
2015-08-14 22:50:38 -05:00
|
|
|
|
2015-08-16 22:44:23 -05:00
|
|
|
uiSpinboxOnChanged(s, defaultOnChanged, NULL);
|
2015-08-14 22:50:38 -05:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|