forked from mikecsh/mbtablegrid
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMBTableGridHeaderCell.m
More file actions
211 lines (179 loc) · 8.1 KB
/
MBTableGridHeaderCell.m
File metadata and controls
211 lines (179 loc) · 8.1 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
Copyright (c) 2008 Matthew Ball - http://www.mattballdesign.com
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#import "MBTableGridHeaderCell.h"
#import "MBTableGridHeaderView.h"
#import "MBTableGrid.h"
extern CGFloat MBTableHeaderSortIndicatorWidth;
extern CGFloat MBTableHeaderSortIndicatorMargin;
#define kSortIndicatorXInset 4.0 /* Number of pixels to inset the drawing of the indicator from the right edge */
@interface MBTableGridHeaderCell ()
@end
@interface MBTableGrid (Private)
- (NSColor *)_selectionColor;
@end
#pragma mark -
@implementation MBTableGridHeaderCell
@synthesize orientation;
//@synthesize labelFont;
- (NSColor*)borderColor
{
if (_borderColor == nil)
_borderColor = NSColor.quaternaryLabelColor;
return _borderColor;
}
- (NSColor*)textColor
{
if (_textColor == nil)
_textColor = NSColor.headerTextColor;
return _textColor;
}
- (NSFont*)labelFont
{
if (_labelFont == nil)
_labelFont = [NSFont labelFontOfSize:NSFont.labelFontSize];
return _labelFont;
}
- (NSRect)sortIndicatorRectForBounds:(NSRect)rect {
NSRect indicatorRect = NSZeroRect;
NSSize sortImageSize = NSMakeSize(MBTableHeaderSortIndicatorWidth, MBTableHeaderSortIndicatorWidth);
indicatorRect.size = sortImageSize;
indicatorRect.origin.x = NSMaxX(rect) - (sortImageSize.width + MBTableHeaderSortIndicatorMargin);
indicatorRect.origin.y = NSMinY(rect) + roundf((NSHeight(rect) - sortImageSize.height) / 2.0);
return indicatorRect;
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(MBTableGridHeaderView *)controlView
{
NSRect cellFrameRect = cellFrame;
[NSColor.windowBackgroundColor set];
NSRectFill(cellFrame);
if(self.orientation == MBTableHeaderHorizontalOrientation) {
// Draw the right border
NSRect borderLine = NSMakeRect(NSMaxX(cellFrameRect)-1, NSMinY(cellFrameRect) + 4.0, 1.0, NSHeight(cellFrameRect) - 4.0);
[self.borderColor set];
NSRectFill(borderLine);
// Draw the bottom border
NSRect bottomLine = NSMakeRect(NSMinX(cellFrameRect), NSMaxY(cellFrameRect)-1.0, NSWidth(cellFrameRect), 1.0);
NSRectFill(bottomLine);
} else if(self.orientation == MBTableHeaderVerticalOrientation) {
// Draw the right border
[self.borderColor set];
NSRect borderLine = NSMakeRect(NSMaxX(cellFrameRect)-1, NSMinY(cellFrameRect), 1.0, NSHeight(cellFrameRect)-1);
NSRectFill(borderLine);
// Draw the bottom border
NSRect bottomLine = NSMakeRect(NSMinX(cellFrameRect) + 4.0, NSMaxY(cellFrameRect)-1.0, NSWidth(cellFrameRect) - 4.0, 1.0);
NSRectFill(bottomLine);
}
if(self.state == NSControlStateValueOn) {
NSRect fillRect = cellFrameRect;
if(self.orientation == MBTableHeaderVerticalOrientation) {
fillRect.origin.y -= 1.0;
fillRect.origin.x += 2.0;
fillRect.size.height += 2.0;
fillRect.size.width += 4.0;
}
else if(self.orientation == MBTableHeaderHorizontalOrientation) {
fillRect.size.height += 4.0;
fillRect.origin.x -= 1.0;
fillRect.size.width += 2.0;
}
NSBezierPath* path = [NSBezierPath bezierPathWithRoundedRect:fillRect xRadius:4.0 yRadius:4.0];
NSColor *overlayColor = [controlView.tableGrid._selectionColor colorWithAlphaComponent:0.26];
[overlayColor set];
[path fill];
}
// Draw the text
[self drawInteriorWithFrame:cellFrameRect inView:controlView];
}
- (NSAttributedString *)attributedStringValue {
NSMutableParagraphStyle *paragraphStyle = [NSParagraphStyle.defaultParagraphStyle mutableCopy];
if (self.orientation == MBTableHeaderVerticalOrientation || !self.sortIndicatorColor) {
paragraphStyle.alignment = NSTextAlignmentCenter;
}
NSDictionary<NSAttributedStringKey, id> *attributes = @{
NSFontAttributeName: self.labelFont,
NSForegroundColorAttributeName: self.textColor,
NSParagraphStyleAttributeName: paragraphStyle
};
return [[NSAttributedString alloc] initWithString:self.stringValue attributes:attributes];
}
- (void)drawSortIndicatorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView ascending:(BOOL)ascending priority:(NSInteger)priority {
if (!self.sortIndicatorColor)
return;
NSRect indicatorRect = [self sortIndicatorRectForBounds:cellFrame];
NSBezierPath *path = [NSBezierPath bezierPath];
path.lineCapStyle = NSLineCapStyleRound;
path.lineWidth = 1.5;
[self.sortIndicatorColor setStroke];
[path moveToPoint:NSMakePoint(NSMinX(indicatorRect) + path.lineWidth / 2,
indicatorRect.origin.y + 0.25 * indicatorRect.size.height)];
[path lineToPoint:NSMakePoint(NSMidX(indicatorRect),
indicatorRect.origin.y + 0.75 * indicatorRect.size.height)];
[path lineToPoint:NSMakePoint(NSMaxX(indicatorRect) - path.lineWidth / 2,
indicatorRect.origin.y + 0.25 * indicatorRect.size.height)];
if (ascending) {
NSAffineTransform *transform = [NSAffineTransform transform];
[transform translateXBy:0.0 yBy:2.5*indicatorRect.size.height];
[transform scaleXBy:1.0 yBy:-1.0];
[path transformUsingAffineTransform:transform];
}
[path stroke];
}
- (NSRect)titleRectForBounds:(NSRect)cellFrame {
NSRect cellFrameRect = cellFrame;
static CGFloat TEXT_PADDING = 6;
NSRect textFrame;
CGSize stringSize = self.attributedStringValue.size;
NSStringDrawingOptions options = (NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin);
if (self.orientation == MBTableHeaderHorizontalOrientation) {
textFrame = NSMakeRect(cellFrameRect.origin.x + TEXT_PADDING,
cellFrameRect.origin.y + (cellFrameRect.size.height - stringSize.height)/2,
cellFrameRect.size.width - 2 * TEXT_PADDING,
stringSize.height);
if (self.sortIndicatorColor)
textFrame.size.width -= MBTableHeaderSortIndicatorWidth;
} else {
NSRect boundingRect = [self.attributedStringValue boundingRectWithSize:cellFrame.size
options:options];
if (boundingRect.size.height < cellFrame.size.height) {
textFrame = NSMakeRect(cellFrameRect.origin.x,
cellFrameRect.origin.y + (cellFrameRect.size.height - boundingRect.size.height)/2,
cellFrame.size.width,
cellFrame.size.height - (cellFrameRect.size.height - boundingRect.size.height)/2);
} else {
textFrame = cellFrame;
}
}
return textFrame;
}
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
NSRect cellBounds = cellFrame;
cellBounds.origin.x = 0;
cellBounds.origin.y = 0;
NSRect titleFrame = [self titleRectForBounds:cellBounds];
titleFrame.origin.x += cellFrame.origin.x;
titleFrame.origin.y += cellFrame.origin.y;
NSStringDrawingOptions options = (NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin);
[self.attributedStringValue drawWithRect:titleFrame options:options];
[self drawSortIndicatorWithFrame:cellFrame inView:controlView ascending:self.sortIndicatorAscending priority:0];
}
@end