forked from mikecsh/mbtablegrid
-
Notifications
You must be signed in to change notification settings - Fork 3
/
MBLevelIndicatorCell.m
136 lines (107 loc) · 3.5 KB
/
MBLevelIndicatorCell.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
//
// MBLevelIndicatorCell.m
// MBTableGrid
//
// Created by Brendan Duddridge on 2014-11-17.
//
//
#import "MBLevelIndicatorCell.h"
@interface MBLevelIndicatorCell()
@property (nonatomic, weak) NSView *theControlView;
@property (nonatomic, strong) NSColor *borderColor;
@end
@implementation MBLevelIndicatorCell
- (instancetype)initWithLevelIndicatorStyle:(NSLevelIndicatorStyle)levelIndicatorStyle {
self = [super initWithLevelIndicatorStyle:levelIndicatorStyle];
if (self) {
self.selectable = YES;
self.editable = YES;
if (@available(macOS 10.13, *)) {
self.borderColor = [NSColor colorNamed:@"grid-line"];
} else {
self.borderColor = [NSColor gridColor];
}
}
return self;
}
//-(BOOL)isHighlighted {
// return YES;
//}
#pragma mark - MBTableGridEditable
- (BOOL)editOnFirstClick {
return NO;
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView withBackgroundColor:(NSColor *)backgroundColor {
[backgroundColor set];
NSRectFill(cellFrame);
[self drawWithFrame:cellFrame inView:controlView];
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
NSRect rect = cellFrame;
rect.origin.y -= 2;
rect.origin.x += 4;
[super drawWithFrame:rect inView:controlView];
[self.borderColor set];
NSRect rightLine = NSMakeRect(NSMaxX(cellFrame)-1.0, NSMinY(cellFrame), 1.0, NSHeight(cellFrame));
NSRectFill(rightLine);
// Draw the bottom border
NSRect bottomLine = NSMakeRect(NSMinX(cellFrame), NSMaxY(cellFrame)-1.0, NSWidth(cellFrame), 1.0);
NSRectFill(bottomLine);
}
- (NSView *)controlView {
return self.theControlView;
}
static NSRect staticTrackingCellFrame;
- (BOOL)trackMouse:(NSEvent *)theEvent inRect:(NSRect)cellFrame ofView:(NSView *)controlView untilMouseUp:(BOOL)flag {
self.theControlView = controlView;
staticTrackingCellFrame = cellFrame;
return [super trackMouse:theEvent inRect:cellFrame ofView:controlView untilMouseUp:flag];
}
- (void)updateValueForPoint:(NSPoint)point {
CGFloat value;
NSRect drawingRect = [self drawingRectForBounds:staticTrackingCellFrame];
if ([self baseWritingDirection] == NSWritingDirectionRightToLeft) {
value = (NSMaxX(drawingRect) - point.x) / NSWidth(drawingRect);
} else {
value = (point.x - NSMinX(drawingRect)) / NSWidth(drawingRect);
}
value = [self minValue] + value * ([self maxValue] - [self minValue]);
switch (self.levelIndicatorStyle) {
case NSDiscreteCapacityLevelIndicatorStyle:
case NSRatingLevelIndicatorStyle:
value = ceil(value);
case NSRelevancyLevelIndicatorStyle:
case NSContinuousCapacityLevelIndicatorStyle:
default:
break;
}
if (value < [self minValue]) {
value = [self minValue];
}
if (value > [self maxValue]) {
value = [self maxValue];
}
// call action on target
// this technique prevents a warning about possible leaks because self.action is not known at compile time
if (self.target && self.action) {
IMP imp = [self.target methodForSelector:self.action];
void (*func)(id, SEL, NSNumber *) = (void *)imp;
func(self.target, self.action, @(value));
}
}
- (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView {
if (![self isEditable]) {
return NO;
} else {
[self updateValueForPoint:startPoint];
return YES;
}
}
- (BOOL)continueTracking:(NSPoint)lastPoint at:(NSPoint)currentPoint inView:(NSView *)controlView {
[self updateValueForPoint:currentPoint];
return YES;
}
- (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:(NSView *)controlView mouseIsUp:(BOOL)flag {
[self updateValueForPoint:stopPoint];
}
@end