2015-08-14 22:50:38 -05:00
|
|
|
// 14 august 2015
|
|
|
|
#import "uipriv_darwin.h"
|
|
|
|
|
2015-08-21 15:31:17 -05:00
|
|
|
// Horizontal sliders have no intrinsic width; we'll use the default Interface Builder width for them.
|
|
|
|
// This will also be used for the initial frame size, to ensure the slider is always horizontal (see below).
|
|
|
|
#define sliderWidth 92
|
|
|
|
|
|
|
|
@interface libui_intrinsicWidthNSSlider : NSSlider
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation libui_intrinsicWidthNSSlider
|
|
|
|
|
|
|
|
- (NSSize)intrinsicContentSize
|
|
|
|
{
|
|
|
|
NSSize s;
|
|
|
|
|
|
|
|
s = [super intrinsicContentSize];
|
|
|
|
s.width = sliderWidth;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-08-16 22:44:23 -05:00
|
|
|
struct uiSlider {
|
2015-08-14 22:50:38 -05:00
|
|
|
uiDarwinControl c;
|
|
|
|
NSSlider *slider;
|
|
|
|
void (*onChanged)(uiSlider *, void *);
|
|
|
|
void *onChangedData;
|
|
|
|
};
|
|
|
|
|
2015-08-17 14:16:54 -05:00
|
|
|
@interface sliderDelegateClass : NSObject {
|
2016-01-07 13:41:20 -06:00
|
|
|
struct mapTable *sliders;
|
2015-08-17 14:16:54 -05:00
|
|
|
}
|
|
|
|
- (IBAction)onChanged:(id)sender;
|
|
|
|
- (void)registerSlider:(uiSlider *)b;
|
|
|
|
- (void)unregisterSlider:(uiSlider *)b;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation sliderDelegateClass
|
|
|
|
|
|
|
|
- (id)init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self)
|
|
|
|
self->sliders = newMap();
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
2016-01-07 13:41:20 -06:00
|
|
|
mapDestroy(self->sliders);
|
2015-08-17 14:16:54 -05:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)onChanged:(id)sender
|
|
|
|
{
|
|
|
|
uiSlider *s;
|
|
|
|
|
|
|
|
s = (uiSlider *) mapGet(self->sliders, sender);
|
|
|
|
(*(s->onChanged))(s, s->onChangedData);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)registerSlider:(uiSlider *)s
|
|
|
|
{
|
|
|
|
mapSet(self->sliders, s->slider, s);
|
|
|
|
[s->slider setTarget:self];
|
|
|
|
[s->slider setAction:@selector(onChanged:)];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)unregisterSlider:(uiSlider *)s
|
|
|
|
{
|
|
|
|
[s->slider setTarget:nil];
|
2016-01-07 13:41:20 -06:00
|
|
|
mapDelete(self->sliders, s->slider);
|
2015-08-17 14:16:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
static sliderDelegateClass *sliderDelegate = nil;
|
|
|
|
|
2016-04-25 10:54:09 -05:00
|
|
|
uiDarwinControlAllDefaultsExceptDestroy(uiSlider, slider)
|
|
|
|
|
|
|
|
static void uiSliderDestroy(uiControl *c)
|
|
|
|
{
|
2016-04-25 11:52:54 -05:00
|
|
|
uiSlider *s = uiSlider(c);
|
|
|
|
|
|
|
|
[sliderDelegate unregisterSlider:s];
|
|
|
|
[s->slider release];
|
|
|
|
uiFreeControl(uiControl(s));
|
2016-04-25 10:54:09 -05:00
|
|
|
}
|
2015-08-14 22:50:38 -05:00
|
|
|
|
2016-06-13 19:55:50 -05:00
|
|
|
int uiSliderValue(uiSlider *s)
|
2015-08-14 22:50:38 -05:00
|
|
|
{
|
2015-08-17 14:16:54 -05:00
|
|
|
return [s->slider integerValue];
|
2015-08-14 22:50:38 -05:00
|
|
|
}
|
|
|
|
|
2016-06-13 19:55:50 -05:00
|
|
|
void uiSliderSetValue(uiSlider *s, int value)
|
2015-08-14 22:50:38 -05:00
|
|
|
{
|
2015-08-17 14:16:54 -05:00
|
|
|
[s->slider setIntegerValue:value];
|
2015-08-14 22:50:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiSliderOnChanged(uiSlider *s, void (*f)(uiSlider *, void *), void *data)
|
|
|
|
{
|
|
|
|
s->onChanged = f;
|
|
|
|
s->onChangedData = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void defaultOnChanged(uiSlider *s, void *data)
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2016-06-13 19:55:50 -05:00
|
|
|
uiSlider *uiNewSlider(int min, int max)
|
2015-08-14 22:50:38 -05:00
|
|
|
{
|
|
|
|
uiSlider *s;
|
|
|
|
NSSliderCell *cell;
|
2016-06-13 19:55:50 -05:00
|
|
|
int temp;
|
2016-05-22 19:11:52 -05:00
|
|
|
|
|
|
|
if (min >= max) {
|
|
|
|
temp = min;
|
|
|
|
min = max;
|
|
|
|
max = temp;
|
|
|
|
}
|
2015-08-14 22:50:38 -05:00
|
|
|
|
2016-04-25 10:54:09 -05:00
|
|
|
uiDarwinNewControl(uiSlider, s);
|
2015-08-14 22:50:38 -05:00
|
|
|
|
2015-08-21 15:31:17 -05:00
|
|
|
// a horizontal slider is defined as one where the width > height, not by a flag
|
|
|
|
// to be safe, don't use NSZeroRect, but make it horizontal from the get-go
|
|
|
|
s->slider = [[libui_intrinsicWidthNSSlider alloc]
|
|
|
|
initWithFrame:NSMakeRect(0, 0, sliderWidth, 2)];
|
2015-08-14 22:50:38 -05:00
|
|
|
[s->slider setMinValue:min];
|
|
|
|
[s->slider setMaxValue:max];
|
|
|
|
[s->slider setAllowsTickMarkValuesOnly:NO];
|
|
|
|
[s->slider setNumberOfTickMarks:0];
|
|
|
|
[s->slider setTickMarkPosition:NSTickMarkAbove];
|
|
|
|
|
|
|
|
cell = (NSSliderCell *) [s->slider cell];
|
|
|
|
[cell setSliderType:NSLinearSlider];
|
|
|
|
|
2015-08-17 14:16:54 -05:00
|
|
|
if (sliderDelegate == nil) {
|
2016-05-24 22:17:08 -05:00
|
|
|
sliderDelegate = [[sliderDelegateClass new] autorelease];
|
2015-08-17 14:16:54 -05:00
|
|
|
[delegates addObject:sliderDelegate];
|
|
|
|
}
|
|
|
|
[sliderDelegate registerSlider:s];
|
2015-08-14 22:50:38 -05:00
|
|
|
uiSliderOnChanged(s, defaultOnChanged, NULL);
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|