forked from mattball/mbtablegrid
-
Notifications
You must be signed in to change notification settings - Fork 16
/
MBTableGridHeaderView.m
356 lines (275 loc) · 12.6 KB
/
MBTableGridHeaderView.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
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 "MBTableGridHeaderView.h"
#import "MBTableGrid.h"
#import "MBTableGridContentView.h"
@interface MBTableGrid (Private)
- (NSString *)_headerStringForColumn:(NSUInteger)columnIndex;
- (NSString *)_headerStringForRow:(NSUInteger)rowIndex;
- (MBTableGridContentView *)_contentView;
- (void)_dragColumnsWithEvent:(NSEvent *)theEvent;
- (void)_dragRowsWithEvent:(NSEvent *)theEvent;
@end
@implementation MBTableGridHeaderView
@synthesize orientation;
@synthesize headerCell;
- (id)initWithFrame:(NSRect)frameRect
{
if(self = [super initWithFrame:frameRect]) {
// Setup the header cell
headerCell = [[MBTableGridHeaderCell alloc] init];
// We haven't clicked any item
mouseDownItem = -1;
// Initially, we're not dragging anything
shouldDragItems = NO;
isInDrag = NO;
// No resize at start
canResize = NO;
isResizing = NO;
}
return self;
}
- (void)drawRect:(NSRect)rect
{
if (self.orientation == MBTableHeaderHorizontalOrientation) {
// Remove all tracking areas
if (trackingAreas) {
for (NSTrackingArea *trackingArea in trackingAreas) {
[self removeTrackingArea:trackingArea];
}
}
// reset tracking array
trackingAreas = [NSMutableArray array];
// Draw the column headers
NSUInteger numberOfColumns = [self tableGrid].numberOfColumns;
[headerCell setOrientation:self.orientation];
NSUInteger column = 0;
while (column < numberOfColumns) {
NSRect headerRect = [self headerRectOfColumn:column];
// Only draw the header if we need to
if ([self needsToDrawRect:headerRect]) {
// Check if any part of the selection is in this column
NSIndexSet *selectedColumns = [[self tableGrid] selectedColumnIndexes];
if ([selectedColumns containsIndex:column]) {
[headerCell setState:NSOnState];
} else {
[headerCell setState:NSOffState];
}
[headerCell setStringValue:[[self tableGrid] _headerStringForColumn:column]];
[headerCell drawWithFrame:headerRect inView:self];
// Create new tracking area for resizing columns
NSRect resizeRect = NSMakeRect(NSMinX(headerRect) + NSWidth(headerRect) - 2, NSMinY(headerRect), 5, NSHeight(headerRect));
NSTrackingArea *resizeTrackingArea = [[NSTrackingArea alloc] initWithRect:resizeRect options:(NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways) owner:self userInfo:nil];
// keep track of tracking areas and add tracking to view
[trackingAreas addObject:resizeTrackingArea];
[self addTrackingArea:resizeTrackingArea];
}
column++;
}
} else if (self.orientation == MBTableHeaderVerticalOrientation) {
// Draw the row headers
NSUInteger numberOfRows = [self tableGrid].numberOfRows;
[headerCell setOrientation:self.orientation];
NSUInteger row = 0;
while(row < numberOfRows) {
NSRect headerRect = [self headerRectOfRow:row];
// Only draw the header if we need to
if ([self needsToDrawRect:headerRect]) {
// Check if any part of the selection is in this column
NSIndexSet *selectedRows = [[self tableGrid] selectedRowIndexes];
if ([selectedRows containsIndex:row]) {
[headerCell setState:NSOnState];
} else {
[headerCell setState:NSOffState];
}
[headerCell setStringValue:[[self tableGrid] _headerStringForRow:row]];
[headerCell drawWithFrame:headerRect inView:self];
}
row++;
}
}
}
- (BOOL)isFlipped
{
return YES;
}
- (void)mouseDown:(NSEvent *)theEvent
{
// Get the location of the click
NSPoint loc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
mouseDownLocation = loc;
NSInteger column = [[self tableGrid] columnAtPoint:[self convertPoint:loc toView:[self tableGrid]]];
NSInteger row = [[self tableGrid] rowAtPoint:[self convertPoint:loc toView:[self tableGrid]]];
if (canResize) {
// Set resize column index
draggingColumnIndex = [[self tableGrid] columnAtPoint:[self convertPoint:NSMakePoint(loc.x - 3, loc.y) toView:[self tableGrid]]];
lastMouseDraggingLocation = loc;
isResizing = YES;
} else {
// For single clicks,
if([theEvent clickCount] == 1) {
if(([theEvent modifierFlags] & NSShiftKeyMask) && [self tableGrid].allowsMultipleSelection) {
// If the shift key was held down, extend the selection
} else {
// No modifier keys, so change the selection
if(self.orientation == MBTableHeaderHorizontalOrientation) {
mouseDownItem = column;
if([[self tableGrid].selectedColumnIndexes containsIndex:column] && [[self tableGrid].selectedRowIndexes count] == [self tableGrid].numberOfRows) {
// Allow the user to drag the column
shouldDragItems = YES;
} else {
[self tableGrid].selectedColumnIndexes = [NSIndexSet indexSetWithIndex:column];
// Select every row
[self tableGrid].selectedRowIndexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,[self tableGrid].numberOfRows)];
}
} else if(self.orientation == MBTableHeaderVerticalOrientation) {
mouseDownItem = row;
if([[self tableGrid].selectedRowIndexes containsIndex:row] && [[self tableGrid].selectedColumnIndexes count] == [self tableGrid].numberOfColumns) {
// Allow the user to drag the row
shouldDragItems = YES;
} else {
[self tableGrid].selectedRowIndexes = [NSIndexSet indexSetWithIndex:row];
// Select every column
[self tableGrid].selectedColumnIndexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,[self tableGrid].numberOfColumns)];
}
}
}
}
// Pass the event back to the MBTableGrid (Used to give First Responder status)
[[self tableGrid] mouseDown:theEvent];
}
}
- (void)mouseDragged:(NSEvent *)theEvent
{
// Get the location of the mouse
NSPoint loc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
float deltaX = abs(loc.x - mouseDownLocation.x);
float deltaY = abs(loc.y - mouseDownLocation.y);
if (canResize) {
// Set drag distance
float dragDistance = loc.x - lastMouseDraggingLocation.x;
lastMouseDraggingLocation = loc;
// Resize column and resize views
[self.tableGrid resizeColumnWithIndex:draggingColumnIndex withDistance:dragDistance];
} else {
// Drag operation doesn't start until the mouse has moved more than 5 points
float dragThreshold = 5.0;
// If we've met the conditions for a drag operation,
if (shouldDragItems && mouseDownItem >= 0 && (deltaX >= dragThreshold || deltaY >= dragThreshold)) {
if (self.orientation == MBTableHeaderHorizontalOrientation) {
[[self tableGrid] _dragColumnsWithEvent:theEvent];
} else if (self.orientation == MBTableHeaderVerticalOrientation) {
[[self tableGrid] _dragRowsWithEvent:theEvent];
}
// We've responded to the drag, so don't respond again during this drag session
shouldDragItems = NO;
// Flag that we are currently dragging items
isInDrag = YES;
}
// Otherwise, extend the selection (if possible)
else if (mouseDownItem >= 0 && !isInDrag && !shouldDragItems) {
// Determine which item is under the mouse
NSInteger itemUnderMouse = -1;
if (self.orientation == MBTableHeaderHorizontalOrientation) {
itemUnderMouse = [[self tableGrid] columnAtPoint:[self convertPoint:loc toView:[self tableGrid]]];
} else if(self.orientation == MBTableHeaderVerticalOrientation) {
itemUnderMouse = [[self tableGrid] rowAtPoint:[self convertPoint:loc toView:[self tableGrid]]];
}
// If there's nothing under the mouse, bail out (something went wrong)
if (itemUnderMouse < 0)
return;
// Calculate the range of items to select
NSInteger firstItemToSelect = mouseDownItem;
NSInteger numberOfItemsToSelect = itemUnderMouse - mouseDownItem + 1;
if(itemUnderMouse < mouseDownItem) {
firstItemToSelect = itemUnderMouse;
numberOfItemsToSelect = mouseDownItem - itemUnderMouse + 1;
}
// Set the selected items
if (self.orientation == MBTableHeaderHorizontalOrientation) {
[self tableGrid].selectedColumnIndexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(firstItemToSelect, numberOfItemsToSelect)];
} else if (self.orientation == MBTableHeaderVerticalOrientation) {
[self tableGrid].selectedRowIndexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(firstItemToSelect, numberOfItemsToSelect)];
}
}
}
}
- (void)mouseUp:(NSEvent *)theEvent
{
if (canResize) {
isResizing = NO;
} else {
// If we only clicked on a header that was part of a bigger selection, select it
if(shouldDragItems && !isInDrag) {
if (self.orientation == MBTableHeaderHorizontalOrientation) {
[self tableGrid].selectedColumnIndexes = [NSIndexSet indexSetWithIndex:mouseDownItem];
// Select every row
[self tableGrid].selectedRowIndexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,[self tableGrid].numberOfRows)];
} else if (self.orientation == MBTableHeaderVerticalOrientation) {
[self tableGrid].selectedRowIndexes = [NSIndexSet indexSetWithIndex:mouseDownItem];
// Select every column
[self tableGrid].selectedColumnIndexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,[self tableGrid].numberOfColumns)];
}
}
// Reset the pressed item
mouseDownItem = -1;
// In case it didn't already happen, reset the drag flags
shouldDragItems = NO;
isInDrag = NO;
// Reset the location
mouseDownLocation = NSZeroPoint;
}
}
- (void)mouseEntered:(NSEvent *)theEvent
{
// Change to resize cursor
[[NSCursor resizeLeftRightCursor] set];
canResize = YES;
}
- (void)mouseExited:(NSEvent *)theEvent
{
if (!isResizing) {
// Revert to normal cursor
[[NSCursor arrowCursor] set];
canResize = NO;
}
}
#pragma mark -
#pragma mark Subclass Methods
- (MBTableGrid *)tableGrid
{
return (MBTableGrid *)[[self enclosingScrollView] superview];
}
#pragma mark Layout Support
- (NSRect)headerRectOfColumn:(NSUInteger)columnIndex
{
NSRect rect = [[[self tableGrid] _contentView] rectOfColumn:columnIndex];
rect.size.height = MBTableGridColumnHeaderHeight;
return rect;
}
- (NSRect)headerRectOfRow:(NSUInteger)rowIndex
{
NSRect rect = [[[self tableGrid] _contentView] rectOfRow:rowIndex];
rect.size.width = MBTableGridRowHeaderWidth;
return rect;
}
@end