forked from mikecsh/mbtablegrid
-
Notifications
You must be signed in to change notification settings - Fork 3
/
MBTableGridFooterView.m
270 lines (202 loc) · 9.35 KB
/
MBTableGridFooterView.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
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 "MBTableGridFooterView.h"
#import "MBTableGrid.h"
#import "MBTableGridContentView.h"
#import "MBFooterTextCell.h"
#import "MBLevelIndicatorCell.h"
#import "MBTableGridFooterCell.h"
@interface MBTableGrid ()
- (MBTableGridContentView *)_contentView;
- (NSCell *)_footerCellForColumn:(NSUInteger)columnIndex;
- (id)_footerValueForColumn:(NSUInteger)columnIndex;
- (void)_setFooterValue:(id)value forColumn:(NSUInteger)columnIndex;
@end
@interface MBTableGridFooterView ()
@property (nonatomic, weak) MBTableGrid *cachedTableGrid;
@end
@implementation MBTableGridFooterView
- (id)initWithFrame:(NSRect)frameRect
{
if(self = [super initWithFrame:frameRect]) {
_defaultCell = [[MBFooterTextCell alloc] initTextCell:@""];
[_defaultCell setBordered:NO];
}
return self;
}
- (void)drawRect:(NSRect)rect {
// Draw the column footers
NSUInteger numberOfColumns = [self tableGrid].numberOfColumns;
NSUInteger column = 0;
NSColor *backgroundColor = [NSColor windowBackgroundColor];
while (column < numberOfColumns) {
NSRect cellFrame = [self footerRectOfColumn:column];
// Only draw the header if we need to
if ([self needsToDrawRect:cellFrame]) {
NSCell *_cell = [[self tableGrid] _footerCellForColumn:column];
if (!_cell) {
_cell = _defaultCell;
}
id objectValue = [[self tableGrid] _footerValueForColumn:column];
if ([_cell isKindOfClass:[MBFooterPopupButtonCell class]]) {
MBFooterPopupButtonCell *cell = (MBFooterPopupButtonCell *)_cell;
NSInteger index = [cell indexOfItemWithTitle:objectValue];
[_cell setObjectValue:@(index)];
} else {
[_cell setObjectValue:objectValue];
}
BOOL isFrozenColumn = self != [self tableGrid].frozenColumnFooterView && [[self tableGrid] isFrozenColumn:column];
if (isFrozenColumn) {
[backgroundColor set];
NSRectFill(cellFrame);
} else if ([_cell isKindOfClass:[MBFooterPopupButtonCell class]]) {
MBFooterPopupButtonCell *cell = (MBFooterPopupButtonCell *)_cell;
[cell drawWithFrame:cellFrame inView:self withBackgroundColor:backgroundColor];// Draw background color
} else if ([_cell isKindOfClass:[MBLevelIndicatorCell class]]) {
MBLevelIndicatorCell *cell = (MBLevelIndicatorCell *)_cell;
cell.target = self;
cell.action = @selector(updateLevelIndicator:);
[cell drawWithFrame:cellFrame inView:[self tableGrid] withBackgroundColor:backgroundColor];// Draw background color
} else {
MBTableGridFooterCell *cell = (MBTableGridFooterCell *)_cell;
[cell drawWithFrame:cellFrame inView:self withBackgroundColor:backgroundColor textColor:[NSColor labelColor]];// Draw background color
}
NSColor *sideColor = [NSColor windowBackgroundColor];
NSColor *borderColor = nil;
if (@available(macOS 10.13, *)) {
borderColor = [NSColor colorNamed:@"grid-line"];
} else {
borderColor = [NSColor gridColor];
}
if (!isFrozenColumn) {
// Draw the side bevels
NSRect sideLine = NSMakeRect(NSMinX(cellFrame), NSMinY(cellFrame), 1.0, NSHeight(cellFrame));
[sideColor set];
[[NSBezierPath bezierPathWithRect:sideLine] fill];
sideLine.origin.x = NSMaxX(cellFrame)-2.0;
[[NSBezierPath bezierPathWithRect:sideLine] fill];
// Draw the right border
NSRect borderLine = NSMakeRect(NSMaxX(cellFrame)-1, NSMinY(cellFrame), 1.0, NSHeight(cellFrame));
[borderColor set];
NSRectFill(borderLine);
}
// Draw the bottom border
// NSRect bottomLine = NSMakeRect(NSMinX(cellFrame), NSMaxY(cellFrame)-1.0, NSWidth(cellFrame), 1.0);
// NSRectFill(bottomLine);
}
column++;
}
// Draw the top border
NSRect topLine = NSMakeRect(NSMinX(rect), 0, NSWidth(rect), 1.0);
NSRectFill(topLine);
}
- (void)updateLevelIndicator:(NSNumber *)value {
NSInteger selectedColumn = [[self tableGrid].selectedColumnIndexes firstIndex];
// sanity check to make sure we have an NSNumber.
// I've observed that when the user lets go of the mouse,
// the value parameter becomes the MBTableGridContentView
// object for some reason.
if ([value isKindOfClass:[NSNumber class]]) {
[[self tableGrid] _setFooterValue:value forColumn:selectedColumn];
NSRect cellFrame = [self footerRectOfColumn:selectedColumn];
[[self tableGrid] setNeedsDisplayInRect:cellFrame];
}
}
- (BOOL)isFlipped
{
return YES;
}
- (void)mouseDown:(NSEvent *)theEvent
{
NSPoint mouseLocationInContentView = [self convertPoint:[theEvent locationInWindow] fromView:nil];
NSInteger mouseDownColumn = [self footerColumnAtPoint:mouseLocationInContentView];
if (theEvent.clickCount == 1 && !self.isHidden) {
// Pass the event back to the MBTableGrid (Used to give First Responder status)
[[self tableGrid] mouseDown:theEvent];
editedColumn = mouseDownColumn;
NSCell *cell = [[self tableGrid] _footerCellForColumn:mouseDownColumn];
id currentValue = [[self tableGrid] _footerValueForColumn:editedColumn];
if ([cell isKindOfClass:[MBFooterPopupButtonCell class]]) {
editedPopupCell = [cell copy];
NSRect cellFrame = [self footerRectOfColumn:editedColumn];
editedPopupCell.menu.font = cell.menu.font;
editedPopupCell.editable = YES;
editedPopupCell.selectable = YES;
NSMenu *menu = editedPopupCell.menu;
for (NSMenuItem *item in menu.itemArray) {
item.action = @selector(cellPopupMenuItemSelected:);
item.target = self;
if ([item.title isEqualToString:currentValue])
{
[editedPopupCell selectItem:item];
}
}
NSRect popupRect = cellFrame;
popupRect.origin.x += cellFrame.size.width - editedPopupCell.menu.size.width;
editedPopupCell.menu.font = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSControlSizeSmall]];
[editedPopupCell.menu popUpMenuPositioningItem:editedPopupCell.selectedItem atLocation:popupRect.origin inView:self];
}
}
[self setNeedsDisplay:YES];
}
- (NSRect)adjustScroll:(NSRect)proposedVisibleRect
{
NSRect modifiedRect = proposedVisibleRect;
modifiedRect.origin.y = 0.0;
return modifiedRect;
}
- (void)cellPopupMenuItemSelected:(NSMenuItem *)menuItem {
// MBFooterPopupButtonCell *cell = (MBFooterPopupButtonCell *)[[self tableGrid] _footerCellForColumn:editedColumn];
[editedPopupCell selectItemWithTitle:menuItem.title];
[editedPopupCell synchronizeTitleAndSelectedItem];
[[self tableGrid] _setFooterValue:menuItem.representedObject forColumn:editedColumn];
NSRect cellFrame = [self footerRectOfColumn:editedColumn];
[[self tableGrid] setNeedsDisplayInRect:cellFrame];
editedColumn = NSNotFound;
editedPopupCell = nil;
}
#pragma mark -
#pragma mark Subclass Methods
- (MBTableGrid *)tableGrid
{
if (!self.cachedTableGrid) {
NSScrollView *scrollView = self.enclosingScrollView;
// Will be nil for the floating view:
if (!scrollView) {
scrollView = (NSScrollView *)self.superview.superview;
}
self.cachedTableGrid = (MBTableGrid *)scrollView.superview;
}
return self.cachedTableGrid;
}
#pragma mark Layout Support
- (NSRect)footerRectOfColumn:(NSUInteger)columnIndex
{
NSRect rect = [[[self tableGrid] _contentView] rectOfColumn:columnIndex];
rect.size.height = MBTableGridColumnHeaderHeight;
return rect;
}
- (NSInteger)footerColumnAtPoint:(NSPoint)aPoint
{
return [[[self tableGrid] _contentView] columnAtPoint:aPoint];
}
@end