forked from cocoadialog/cocoadialog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCDSliderView.m
171 lines (151 loc) · 6.3 KB
/
CDSliderView.m
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// CDSliderView.m
// cocoadialog
//
// Copyright (c) 2004-2017 Mark A. Stratman <[email protected]>, Mark Carver <[email protected]>.
// All rights reserved.
// Licensed under GPL-2.
#import "CDSliderView.h"
IB_DESIGNABLE
@implementation CDSliderView
- (void) initView {
[super initView];
// NSString *_sliderLabel = @"OPTION_SLIDER_DEFAULT_LABEL".localized;
// if (option[@"slider-label"].wasProvided && ![option[@"slider-label"].stringValue isBlank]) {
// _sliderLabel = option[@"slider-label"].stringValue;
// }
// sliderLabel = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, cmFrame.size.width, 14.0f)];
// [sliderLabel setBezeled:NO];
// [sliderLabel setDrawsBackground:NO];
// [sliderLabel setEditable:NO];
// [sliderLabel setSelectable:NO];
// sliderLabel.alignment = NSLeftTextAlignment;
// sliderLabel.stringValue = _sliderLabel;
// [sliderView addSubview:sliderLabel];
//
// valueLabel = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, cmFrame.size.width, 14.0f)];
// [valueLabel setBezeled:NO];
// [valueLabel setDrawsBackground:NO];
// [valueLabel setEditable:NO];
// [valueLabel setSelectable:NO];
// valueLabel.alignment = NSRightTextAlignment;
// valueLabel.font = [NSFont fontWithName:valueLabel.font.fontName size:10.0f];
// if (!option[@"always-show-value"].wasProvided) {
// [valueLabel setHidden:YES];
// }
// [sliderView addSubview:valueLabel];
//
// [self.panel.contentView addSubview:sliderView];
//
//
// self.slider.alwaysShowValue = option[@"always-show-value"].boolValue;
// self.slider.delegate = self;
// slider.valueLabel = valueLabel;
// slider.minValue = min;
// slider.maxValue = max;
// slider.doubleValue = value;
// slider.numberOfTickMarks = ticks;
// slider.sticky = option[@"sticky"].boolValue;
// [slider setContinuous:YES];
// slider.target = self;
// slider.action = @selector(sliderChanged);
//
// [self.controlView addSubview:slider];
//
// // Save self.controlView height
// CGFloat oldHeight = cmFrame.size.height;
// cmFrame = self.controlView.frame;
//
// if (ticks > 0) {
// NSView *tickView = [[NSView alloc] initWithFrame:NSMakeRect(0.0f, cmFrame.origin.y - (cmFrame.size.height - oldHeight) - 17.0f, self.panel.frame.size.width, 18.0f)];
// tickView.autoresizingMask = NSViewMinYMargin;
//
// NSUInteger count = slider.numberOfTickMarks;
// for (NSUInteger i = 0; i < count; i++) {
// CGFloat length=cmFrame.size.width-2*10;
// CGFloat position=floor((count==1)?length/2:i*(length/(count-1)));
// NSTextField *tickLabel = [[NSTextField alloc] initWithFrame:NSMakeRect(cmFrame.origin.x + 10.0f + position, 0, 0, 0)];
// [tickLabel setBezeled:NO];
// [tickLabel setDrawsBackground:NO];
// [tickLabel setEditable:NO];
// [tickLabel setSelectable:NO];
// tickLabel.stringValue = [NSString stringWithFormat:@"%i", (int)[slider tickMarkValueAtIndex:i]];
// tickLabel.font = [NSFont fontWithName:tickLabel.font.fontName size:10.0f];
// [tickLabel sizeToFit];
// // Center the label on the tick
// NSPoint labelOrigin = tickLabel.frame.origin;
// labelOrigin.x -= floor(tickLabel.frame.size.width / 2.0f);
// [tickLabel setFrameOrigin:labelOrigin];
// [tickView addSubview:tickLabel];
// }
// [self.panel.contentView addSubview:tickView];
//
// // Move matrix to make room for tickView
// cmOrigin = cmFrame.origin;
// cmOrigin.y += tickView.frame.size.height + 4.0f;
// [matrix setFrameOrigin:cmOrigin];
//
// // Add the tickView to the panel height
// panelSize = self.panel.contentView.frame.size;
// panelSize.height += tickView.frame.size.height + 4.0f;
// [self.panel setContentSize:panelSize];
// [self resize];
// }
[self sliderChanged];
}
- (void) sliderChanged {
// CDSliderCell *slider = [matrix cellAtRow:0 column:0];
// // Update the label
// NSString *label = @"";
// if (option[@"return-float"].wasProvided) {
// label = [NSString stringWithFormat:@"%.2f", slider.doubleValue];
// }
// else {
// label = [NSString stringWithFormat:@"%i", slider.intValue];
// }
// valueLabel.stringValue = label;
}
@end
@implementation CDSliderCell
- (BOOL) trackMouse:(NSEvent *)theEvent inRect:(NSRect)cellFrame ofView:(NSView *)controlView untilMouseUp:(BOOL)flag {
if (!self.alwaysShowValue)
[self.valueLabel setHidden:NO];
return [super trackMouse:theEvent inRect:cellFrame ofView:controlView untilMouseUp:flag];
}
- (BOOL) startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView {
if (self.numberOfTickMarks > 0)
self.tracking = YES;
return [super startTrackingAt:startPoint inView:controlView];
}
- (BOOL) continueTracking:(NSPoint)lastPoint at:(NSPoint)currentPoint
inView:(NSView *)controlView {
if (self.tracking && self.sticky) {
NSUInteger count = self.numberOfTickMarks;
CGFloat snapFlexibility = (100 / count) / 2;
for (NSUInteger i = 0; i < count; i++) {
NSRect tickMarkRect = [self rectOfTickMarkAtIndex:i];
if (ABS(tickMarkRect.origin.x - currentPoint.x) <= snapFlexibility) {
[self setAllowsTickMarkValuesOnly:YES];
} else if (ABS(tickMarkRect.origin.x - currentPoint.x) >= snapFlexibility &&
ABS(tickMarkRect.origin.x - currentPoint.x) <= snapFlexibility * 2) {
[self setAllowsTickMarkValuesOnly:NO];
}
}
}
else {
[self setAllowsTickMarkValuesOnly:NO];
}
// Fix "may cause leak" warning.
// @see http://stackoverflow.com/a/20058585/1226717
if (self.delegate) {
IMP imp = [self.delegate methodForSelector:self.action];
void (*func)(id, SEL) = (void *)imp;
func(self.delegate, self.action);
}
return [super continueTracking:lastPoint at:currentPoint inView:controlView];
}
- (void) stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:(NSView *)controlView mouseIsUp:(BOOL)flag {
if (!self.alwaysShowValue)
[self.valueLabel setHidden:YES];
[super stopTracking:lastPoint at:stopPoint inView:controlView mouseIsUp:flag];
}
@end