aboutsummaryrefslogtreecommitdiff
path: root/src/libui_sdl/libui/darwin/slider.m
blob: f00da50fdd19274b4775f509685e8b5be90aaf46 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// 14 august 2015
#import "uipriv_darwin.h"

// 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

struct uiSlider {
	uiDarwinControl c;
	NSSlider *slider;
	void (*onChanged)(uiSlider *, void *);
	void *onChangedData;
};

@interface sliderDelegateClass : NSObject {
	struct mapTable *sliders;
}
- (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
{
	mapDestroy(self->sliders);
	[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];
	mapDelete(self->sliders, s->slider);
}

@end

static sliderDelegateClass *sliderDelegate = nil;

uiDarwinControlAllDefaultsExceptDestroy(uiSlider, slider)

static void uiSliderDestroy(uiControl *c)
{
	uiSlider *s = uiSlider(c);

	[sliderDelegate unregisterSlider:s];
	[s->slider release];
	uiFreeControl(uiControl(s));
}

int uiSliderValue(uiSlider *s)
{
	return [s->slider integerValue];
}

void uiSliderSetValue(uiSlider *s, int value)
{
	[s->slider setIntegerValue:value];
}

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
}

uiSlider *uiNewSlider(int min, int max)
{
	uiSlider *s;
	NSSliderCell *cell;
	int temp;

	if (min >= max) {
		temp = min;
		min = max;
		max = temp;
	}

	uiDarwinNewControl(uiSlider, s);

	// 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)];
	[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];

	if (sliderDelegate == nil) {
		sliderDelegate = [[sliderDelegateClass new] autorelease];
		[delegates addObject:sliderDelegate];
	}
	[sliderDelegate registerSlider:s];
	uiSliderOnChanged(s, defaultOnChanged, NULL);

	return s;
}