forked from mikecsh/mbtablegrid
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMBTableGridFooterView.m
More file actions
160 lines (128 loc) · 5.48 KB
/
MBTableGridFooterView.m
File metadata and controls
160 lines (128 loc) · 5.48 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
/*
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"
@interface MBTableGrid ()
- (NSCell *)_footerCellForColumn:(NSUInteger)columnIndex;
- (NSCell *)_footerCellForRow:(NSUInteger)rowIndex;
- (void)_willDisplayFooterMenu:(NSMenu *)menu forColumn:(NSUInteger)columnIndex;
- (void)_willDisplayFooterMenu:(NSMenu *)menu forRow:(NSUInteger)rowIndex;
- (NSRange)_rangeOfColumnsIntersectingRect:(NSRect)rect;
- (NSRange)_rangeOfRowsIntersectingRect:(NSRect)rect;
@end
@implementation MBTableGridFooterView
- (instancetype)initWithFrame:(NSRect)frameRect andTableGrid:(MBTableGrid *)tableGrid
{
if(self = [super initWithFrame:frameRect]) {
self.tableGrid = tableGrid;
}
return self;
}
- (void)drawRect:(NSRect)rect {
// Draw the column footers
NSRange columnRange = (self.isVertical ?
[self.tableGrid _rangeOfRowsIntersectingRect:[self convertRect:rect toView:self.tableGrid]] :
[self.tableGrid _rangeOfColumnsIntersectingRect:[self convertRect:rect toView:self.tableGrid]]);
// Find the columns to draw
NSUInteger column = columnRange.location;
while (column != NSNotFound && column < NSMaxRange(columnRange)) {
NSRect cellFrame = self.isVertical ? [self footerRectOfRow:column] : [self footerRectOfColumn:column];
// Only draw the header if we need to
if ([self needsToDrawRect:cellFrame]) {
NSCell *_cell = self.isVertical ? [self.tableGrid _footerCellForRow:column] : [self.tableGrid _footerCellForColumn:column];
[_cell drawWithFrame:cellFrame inView:self];
}
column++;
}
}
- (BOOL)isFlipped
{
return YES;
}
- (NSMenu *)menuForEvent:(NSEvent *)theEvent {
NSPoint local_point = [self convertPoint:theEvent.locationInWindow fromView:nil];
if (self.isVertical) {
NSInteger column = [self footerRowAtPoint:local_point];
[self.tableGrid _willDisplayFooterMenu:self.menu forRow:column];
} else {
NSInteger column = [self footerColumnAtPoint:local_point];
[self.tableGrid _willDisplayFooterMenu:self.menu forColumn:column];
}
return self.menu;
}
- (void)mouseDown:(NSEvent *)theEvent
{
NSPoint mouseLocationInContentView = [self convertPoint:theEvent.locationInWindow fromView:nil];
NSInteger mouseDownColumn = [self footerColumnAtPoint:mouseLocationInContentView];
if (theEvent.clickCount == 1) {
// Pass the event back to the MBTableGrid (Used to give First Responder status)
[self.tableGrid mouseDown:theEvent];
editedColumn = (self.isVertical ?
[self footerRowAtPoint:mouseLocationInContentView] :
[self footerColumnAtPoint:mouseLocationInContentView]);
if (self.isVertical) {
if ([self.tableGrid.delegate respondsToSelector:@selector(tableGrid:footerCellClicked:forRow:withEvent:)]) {
NSCell *cell = [self.tableGrid _footerCellForRow:mouseDownColumn];
[self.tableGrid.delegate tableGrid:self.tableGrid footerCellClicked:cell forRow:editedColumn withEvent:theEvent];
}
} else {
if ([self.tableGrid.delegate respondsToSelector:@selector(tableGrid:footerCellClicked:forColumn:withEvent:)]) {
NSCell *cell = [self.tableGrid _footerCellForColumn:mouseDownColumn];
[self.tableGrid.delegate tableGrid:self.tableGrid footerCellClicked:cell forColumn:editedColumn withEvent:theEvent];
}
}
}
self.needsDisplay = YES;
}
- (NSRect)adjustScroll:(NSRect)proposedVisibleRect
{
NSRect modifiedRect = proposedVisibleRect;
if (self.isVertical) {
modifiedRect.origin.x = 0.0;
} else {
modifiedRect.origin.y = 0.0;
}
return modifiedRect;
}
#pragma mark Layout Support
- (NSRect)footerRectOfColumn:(NSUInteger)columnIndex
{
NSRect rect = [self.tableGrid.contentView rectOfColumn:columnIndex];
rect.size.height = NSHeight(self.bounds);
return rect;
}
- (NSRect)footerRectOfRow:(NSUInteger)rowIndex
{
NSRect rect = [self.tableGrid.contentView rectOfRow:rowIndex];
rect.size.width = NSWidth(self.bounds);
return rect;
}
- (NSInteger)footerColumnAtPoint:(NSPoint)aPoint
{
return [self.tableGrid.contentView columnAtPoint:aPoint];
}
- (NSInteger)footerRowAtPoint:(NSPoint)aPoint
{
return [self.tableGrid.contentView rowAtPoint:aPoint];
}
@end