forked from mikecsh/mbtablegrid
-
Notifications
You must be signed in to change notification settings - Fork 3
/
MBTableGrid.m
2778 lines (2184 loc) · 97.7 KB
/
MBTableGrid.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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 "MBTableGrid.h"
#import "MBTableGridHeaderView.h"
#import "MBTableGridFooterView.h"
#import "MBTableGridHeaderCell.h"
#import "MBTableGridShadowView.h"
#import "MBTableGridContentView.h"
#import "MBTableGridCell.h"
#import "MBImageCell.h"
#import "MBButtonCell.h"
#import "MBPopupButtonCell.h"
#pragma mark -
#pragma mark Constant Definitions
NSString *MBTableGridDidChangeSelectionNotification = @"MBTableGridDidChangeSelectionNotification";
NSString *MBTableGridDidChangeColumnSelectionNotification = @"MBTableGridDidChangeColumnSelectionNotification";
NSString *MBTableGridDidChangeRowSelectionNotification = @"MBTableGridDidChangeRowSelectionNotification";
NSString *MBTableGridDidMoveColumnsNotification = @"MBTableGridDidMoveColumnsNotification";
NSString *MBTableGridDidMoveRowsNotification = @"MBTableGridDidMoveRowsNotification";
NSString *MBTableGridDidResizeColumnNotification = @"MBTableGridDidResizeColumnNotification";
CGFloat MBTableHeaderMinimumColumnWidth = 30.0f;
CGFloat MBTableGridContentViewPadding = 40.0f;
#pragma mark -
#pragma mark Drag Types
NSString *MBTableGridColumnDataType = @"mbtablegrid.pasteboard.column";
NSString *MBTableGridRowDataType = @"mbtablegrid.pasteboard.row";
@interface MBTableGrid ()
@property (nonatomic, strong) NSUndoManager *cachedUndoManager;
@property (nonatomic) BOOL syncronizingScroll;
@property (nonatomic, strong) NSEvent *keyEvent;
@end
@interface MBTableGrid (Drawing)
- (void)_drawColumnHeaderBackgroundInRect:(NSRect)aRect;
- (void)_drawColumnFooterBackgroundInRect:(NSRect)aRect;
- (void)_drawRowHeaderBackgroundInRect:(NSRect)aRect;
- (void)_drawCornerHeaderBackgroundInRect:(NSRect)aRect;
- (void)_drawCornerFooterBackgroundInRect:(NSRect)aRect;
@end
@interface MBTableGrid (DataAccessors)
- (NSString *)_headerStringForColumn:(NSUInteger)columnIndex;
- (NSString *)_headerStringForRow:(NSUInteger)rowIndex;
- (id)_objectValueForColumn:(NSUInteger)columnIndex row:(NSUInteger)rowIndex;
- (NSFormatter *)_formatterForColumn:(NSUInteger)columnIndex;
- (NSCell *)_cellForColumn:(NSUInteger)columnIndex;
- (NSImage *)_accessoryButtonImageForColumn:(NSUInteger)columnIndex row:(NSUInteger)rowIndex;
- (void)_accessoryButtonClicked:(NSUInteger)columnIndex row:(NSUInteger)rowIndex;
- (NSArray *)_availableObjectValuesForColumn:(NSUInteger)columnIndex;
- (NSArray *)_autocompleteValuesForEditString:(NSString *)editString column:(NSUInteger)columnIndex row:(NSUInteger)rowIndex;
- (void)_setObjectValue:(id)value forColumn:(NSUInteger)columnIndex row:(NSUInteger)rowIndex undoTitle:(NSString *)undoTitle;
- (float)_widthForColumn:(NSUInteger)columnIndex;
- (float)_setWidthForColumn:(NSUInteger)columnIndex;
- (id)_backgroundColorForColumn:(NSUInteger)columnIndex row:(NSUInteger)rowIndex;
- (id)_frozenBackgroundColorForColumn:(NSUInteger)columnIndex row:(NSUInteger)rowIndex;
- (id)_groupSummaryBackgroundColorForColumn:(NSUInteger)columnIndex row:(NSUInteger)rowIndex;
- (id)_textColorForColumn:(NSUInteger)columnIndex row:(NSUInteger)rowIndex;
- (BOOL)_canEditCellAtColumn:(NSUInteger)columnIndex row:(NSUInteger)rowIndex;
- (BOOL)_canFillCellAtColumn:(NSUInteger)columnIndex row:(NSUInteger)rowIndex;
- (void)_userDidEnterInvalidStringInColumn:(NSUInteger)columnIndex row:(NSUInteger)rowIndex errorDescription:(NSString *)errorDescription;
- (NSCell *)_footerCellForColumn:(NSUInteger)columnIndex;
- (id)_footerValueForColumn:(NSUInteger)columnIndex;
- (void)_setFooterValue:(id)value forColumn:(NSUInteger)columnIndex;
- (BOOL)_isGroupHeadingRow:(NSUInteger)rowIndex;
- (BOOL)_isGroupSummaryRow:(NSUInteger)rowIndex;
- (BOOL)_isGroupRow:(NSUInteger)rowIndex;
- (MBSortDirection)_sortDirectionForColumn:(NSUInteger)columnIndex;
- (void)_fillInColumn:(NSUInteger)column fromRow:(NSUInteger)row numberOfRowsWhenStarting:(NSUInteger)numberOfRowsWhenStartingFilling;
@end
@interface MBTableGrid (DragAndDrop)
- (void)_dragColumnsWithEvent:(NSEvent *)theEvent;
- (void)_dragRowsWithEvent:(NSEvent *)theEvent;
- (NSImage *)_imageForSelectedColumns;
- (NSImage *)_imageForSelectedRows;
- (NSUInteger)_dropColumnForPoint:(NSPoint)aPoint;
- (NSUInteger)_dropRowForPoint:(NSPoint)aPoint;
@end
@interface MBTableGrid (PrivateAccessors)
- (MBTableGridContentView *)_contentView;
- (void)_setStickyColumn:(MBTableGridEdge)stickyColumn row:(MBTableGridEdge)stickyRow;
- (MBTableGridEdge)_stickyColumn;
- (MBTableGridEdge)_stickyRow;
- (NSUndoManager *)_undoManager;
@end
@interface MBTableGridContentView (Private)
- (void)_setDraggingColumnOrRow:(BOOL)flag;
- (void)_setDropColumn:(NSInteger)columnIndex;
- (void)_setDropRow:(NSInteger)rowIndex;
@end
@implementation MBTableGrid
@synthesize defaultCellFont = _defaultCellFont;
@synthesize isEditable = _isEditable;
#pragma mark -
#pragma mark Initialization & Superclass Overrides
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
[self commonInit:NSMakeRect(0, 0, 500, 200)];
}
return self;
}
- (void)commonInit:(NSRect)frameRect {
self.wantsLayer = YES;
columnWidths = [NSMutableDictionary dictionary];
columnIndexNames = [NSMutableArray array];
self.includeGroupSummaryRows = YES;
// Post frame changed notifications
[self setPostsFrameChangedNotifications:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewFrameDidChange:) name:NSViewFrameDidChangeNotification object:self];
// Set the default cell
MBTableGridCell *defaultCell = [[MBTableGridCell alloc] initTextCell:@""];
[defaultCell setBordered:YES];
[defaultCell setScrollable:YES];
[defaultCell setLineBreakMode:NSLineBreakByTruncatingTail];
[self setCell:defaultCell];
// Setup the column headers
NSRect columnHeaderFrame = NSMakeRect(MBTableGridRowHeaderWidth, 0, frameRect.size.width - MBTableGridRowHeaderWidth, MBTableGridColumnHeaderHeight);
columnHeaderScrollView = [[NSScrollView alloc] initWithFrame:columnHeaderFrame];
columnHeaderView = [[MBTableGridHeaderView alloc] initWithFrame:columnHeaderScrollView.contentView.bounds];
columnHeaderView.identifier = @"Column Header View";
columnHeaderScrollView.autoresizesSubviews = YES;
[columnHeaderView setAutoresizingMask:NSViewWidthSizable];
[columnHeaderView setOrientation:MBTableHeaderHorizontalOrientation];
frozenColumnHeaderView = [[MBTableGridHeaderView alloc] initWithFrame:NSMakeRect(0, 0, 0, columnHeaderFrame.size.height)];
[frozenColumnHeaderView setOrientation:MBTableHeaderHorizontalOrientation];
[columnHeaderScrollView setAutoresizingMask:NSViewWidthSizable];
[columnHeaderScrollView setDocumentView:columnHeaderView];
[columnHeaderScrollView setDrawsBackground:NO];
[self addSubview:columnHeaderScrollView];
[columnHeaderScrollView addFloatingSubview:frozenColumnHeaderView forAxis:NSEventGestureAxisHorizontal];
frozenColumnHeaderView.hidden = YES;
// Setup the row headers
NSRect rowHeaderFrame = NSMakeRect(0, MBTableGridColumnHeaderHeight, MBTableGridRowHeaderWidth, [self frame].size.height);
rowHeaderScrollView = [[NSScrollView alloc] initWithFrame:rowHeaderFrame];
rowHeaderView = [[MBTableGridHeaderView alloc] initWithFrame:NSMakeRect(0, 0, rowHeaderFrame.size.width, rowHeaderFrame.size.height)];
// [rowHeaderView setAutoresizingMask:NSViewHeightSizable];
[rowHeaderView setOrientation:MBTableHeaderVerticalOrientation];
[rowHeaderScrollView setDocumentView:rowHeaderView];
[rowHeaderScrollView setAutoresizingMask:NSViewHeightSizable];
[rowHeaderScrollView setDrawsBackground:NO];
[self addSubview:rowHeaderScrollView];
// Setup the footer view
NSRect columnFooterFrame = NSMakeRect(MBTableGridRowHeaderWidth, frameRect.size.height - MBTableGridColumnHeaderHeight, frameRect.size.width - MBTableGridRowHeaderWidth, MBTableGridColumnHeaderHeight);
columnFooterScrollView = [[NSScrollView alloc] initWithFrame:columnFooterFrame];
columnFooterView = [[MBTableGridFooterView alloc] initWithFrame:NSMakeRect(0, 0, columnFooterFrame.size.width, columnFooterFrame.size.height)];
frozenColumnFooterView = [[MBTableGridFooterView alloc] initWithFrame:NSMakeRect(0, 0, 0, columnFooterFrame.size.height)];
// [columnFooterView setAutoresizingMask:NSViewWidthSizable];
[columnFooterScrollView setDocumentView:columnFooterView];
[columnFooterScrollView setAutoresizingMask:(NSViewWidthSizable | NSViewMinYMargin)];
[columnFooterScrollView setDrawsBackground:NO];
[self addSubview:columnFooterScrollView];
[columnFooterScrollView addFloatingSubview:frozenColumnFooterView forAxis:NSEventGestureAxisHorizontal];
frozenColumnFooterView.hidden = YES;
// Setup the content view
NSRect contentFrame = NSMakeRect(MBTableGridRowHeaderWidth, MBTableGridColumnHeaderHeight, NSWidth(frameRect) - MBTableGridRowHeaderWidth, NSHeight(frameRect) - MBTableGridColumnHeaderHeight);
contentScrollView = [[NSScrollView alloc] initWithFrame:contentFrame];
contentView = [[MBTableGridContentView alloc] initWithFrame:NSMakeRect(0, 0, contentFrame.size.width, contentFrame.size.height)];
[contentScrollView setDocumentView:contentView];
NSClipView *contentClipView = contentScrollView.subviews.firstObject;
contentClipView.copiesOnScroll = YES;
[contentScrollView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
[contentScrollView setHasHorizontalScroller:YES];
[contentScrollView setHasVerticalScroller:YES];
[contentScrollView setAutohidesScrollers:YES];
[contentScrollView setDrawsBackground:YES];
contentScrollView.backgroundColor = [NSColor controlBackgroundColor];
[self addSubview:contentScrollView];
// Setup the frozen content view
NSRect frozenContentFrame = NSMakeRect(MBTableGridRowHeaderWidth, MBTableGridColumnHeaderHeight, 0, NSHeight(frameRect) - MBTableGridColumnHeaderHeight);
frozenContentScrollView = [[NSScrollView alloc] initWithFrame:frozenContentFrame];
frozenContentView = [[MBTableGridContentView alloc] initWithFrame:NSMakeRect(0, 0, 0, frozenContentFrame.size.height)];
[frozenContentScrollView setDocumentView:frozenContentView];
[frozenContentScrollView setAutoresizingMask:NSViewHeightSizable];
[frozenContentScrollView setHasHorizontalScroller:NO];
[frozenContentScrollView setHasVerticalScroller:YES];
NSEdgeInsets insets = frozenContentScrollView.scrollerInsets;
insets.right = -99999;
frozenContentScrollView.scrollerInsets = insets;
[frozenContentScrollView setAutohidesScrollers:YES];
[frozenContentScrollView setHorizontalScrollElasticity:NSScrollElasticityNone];
[frozenContentScrollView setDrawsBackground:YES];
frozenContentScrollView.backgroundColor = [NSColor windowBackgroundColor];
frozenContentScrollView.hidden = YES;
[self addSubview:frozenContentScrollView];
// Setup the column shadow
NSRect columnShadowFrame = NSMakeRect(MBTableGridRowHeaderWidth, MBTableGridColumnHeaderHeight, frameRect.size.width, MBTableGridShadowHeight);
columnShadowView = [[MBTableGridShadowView alloc] initWithFrame:columnShadowFrame];
columnShadowView.orientation = MBTableHeaderHorizontalOrientation;
columnShadowView.autoresizingMask = NSViewWidthSizable;
[self addSubview:columnShadowView];
// Setup the row shadow
NSRect rowShadowFrame = NSMakeRect(MBTableGridRowHeaderWidth, 0, MBTableGridShadowWidth, frameRect.size.height);
rowShadowView = [[MBTableGridShadowView alloc] initWithFrame:rowShadowFrame];
rowShadowView.orientation = MBTableHeaderVerticalOrientation;
rowShadowView.autoresizingMask = NSViewHeightSizable;
[self addSubview:rowShadowView];
// We want to synchronize the scroll views
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(columnHeaderViewDidScroll:) name:NSViewBoundsDidChangeNotification object:[columnHeaderScrollView contentView]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rowHeaderViewDidScroll:) name:NSViewBoundsDidChangeNotification object:[rowHeaderScrollView contentView]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(columnFooterViewDidScroll:) name:NSViewBoundsDidChangeNotification object:[columnFooterScrollView contentView]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contentViewDidScroll:) name:NSViewBoundsDidChangeNotification object:[contentScrollView contentView]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(frozenContentViewDidScroll:) name:NSViewBoundsDidChangeNotification object:[frozenContentScrollView contentView]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didUndoOrRedo:) name:NSUndoManagerDidUndoChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didUndoOrRedo:) name:NSUndoManagerDidRedoChangeNotification object:nil];
// Set the default selection
_selectedColumnIndexes = [NSIndexSet indexSet];
_selectedRowIndexes = [NSMutableIndexSet indexSet];
self.allowsMultipleSelection = YES;
// Set the default sticky edges
stickyColumnEdge = MBTableGridLeftEdge;
stickyRowEdge = MBTableGridTopEdge;
shouldOverrideModifiers = NO;
self.columnRects = [NSMutableDictionary dictionary];
self.footerHidden = NO;
self.isEditable = YES;
}
- (id)initWithFrame:(NSRect)frameRect {
if (self = [super initWithFrame:frameRect]) {
[self commonInit:frameRect];
}
return self;
}
- (void)setDefaultCellFont:(NSFont *)defaultCellFont {
_defaultCellFont = defaultCellFont;
[[self contentView] setDefaultCellFont:defaultCellFont];
[[self frozenContentView] setDefaultCellFont:defaultCellFont];
[[self frozenColumnHeaderView] setDefaultCellFont:defaultCellFont];
[[self columnHeaderView] setDefaultCellFont:defaultCellFont];
if (_numberOfColumns > 0) {
[self reloadData];
}
}
- (void)sortButtonClickedOnColumn:(NSUInteger)columnIndex {
if ([[self delegate] respondsToSelector:@selector(tableGrid:didSortColumn:)]) {
[[self delegate] tableGrid:self didSortColumn:columnIndex];
}
}
- (void)awakeFromNib {
// [self reloadData];
[self registerForDraggedTypes:@[]];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
// NSLog(@"%@ dealloc", self);
}
- (BOOL)isFlipped {
return YES;
}
- (BOOL)canBecomeKeyView {
return YES;
}
- (BOOL)acceptsFirstResponder {
return YES;
}
- (void)setFooterHidden:(BOOL)footerHidden {
_footerHidden = footerHidden;
NSSize footerSize = columnFooterScrollView.frame.size;
NSSize contentScrollSize = contentScrollView.frame.size;
NSSize frozenContentScrollSize = frozenContentScrollView.frame.size;
NSSize rowHeaderScrollSize = rowHeaderScrollView.frame.size;
columnFooterView.hidden = footerHidden;
frozenColumnFooterView.hidden = footerHidden;
if (footerHidden) {
contentScrollSize.height = self.frame.size.height - columnHeaderScrollView.frame.size.height - 1;
frozenContentScrollSize.height = self.frame.size.height - columnHeaderScrollView.frame.size.height - 1;
rowHeaderScrollSize.height = self.frame.size.height - columnHeaderScrollView.frame.size.height - 1;
} else {
contentScrollSize.height = self.frame.size.height - footerSize.height - columnHeaderScrollView.frame.size.height;
frozenContentScrollSize.height = self.frame.size.height - footerSize.height - columnHeaderScrollView.frame.size.height;
rowHeaderScrollSize.height = self.frame.size.height - footerSize.height - columnHeaderScrollView.frame.size.height;
}
[contentScrollView setFrameSize:contentScrollSize];
[frozenContentScrollView setFrameSize:frozenContentScrollSize];
[rowHeaderScrollView setFrameSize:rowHeaderScrollSize];
[rowHeaderScrollView setNeedsDisplay:YES];
}
- (void)setNumberOfFrozenColumns:(NSUInteger)numberOfFrozenColumns {
_numberOfFrozenColumns = numberOfFrozenColumns;
[self setNeedsDisplay:YES];
}
- (void)setFreezeColumns:(BOOL)freezeColumns {
_freezeColumns = freezeColumns;
[self setNeedsDisplay:YES];
}
/**
* @brief Sets the indicator image for the specified column.
* This is used for indicating which direction the
* column is being sorted by.
*
* @param anImage The sort indicator image.
* @param reverseImage The reversed sort indicator image.
*
* @return The header value for the row.
*/
- (void)setSortAscendingImage:(NSImage *)ascendingImage sortDescendingImage:(NSImage*)descendingImage sortUndeterminedImage:(NSImage *)undeterminedImage {
MBTableGridHeaderView *headerView = [self columnHeaderView];
headerView.sortAscendingImage = ascendingImage;
headerView.sortDescendingImage = descendingImage;
headerView.sortUndeterminedImage = undeterminedImage;
}
/**
* @brief Returns the sort indicator image
* for the specified column.
*
* @param columnIndex The index of the column.
*
* @return The sort indicator image for the column.
*/
- (NSImage *)indicatorImageInColumn:(NSUInteger)columnIndex {
NSImage *indicatorImage = nil;
return indicatorImage;
}
- (void)setAutosaveName:(NSString *)autosaveName {
_autosaveName = autosaveName;
self.columnHeaderView.autosaveName = autosaveName;
}
- (void)drawRect:(NSRect)aRect {
// [[NSColor windowBackgroundColor] set];
// NSRectFill(aRect);
// If the view is the first responder, draw the focus ring
NSResponder *firstResponder = [[self window] firstResponder];
if (([[firstResponder class] isSubclassOfClass:[NSView class]] && [(NSView *)firstResponder isDescendantOf : self]) && [[self window] isKeyWindow]) {
[[NSGraphicsContext currentContext] saveGraphicsState];
NSSetFocusRingStyle(NSFocusRingOnly);
[[NSBezierPath bezierPathWithRect:NSMakeRect(0, 0, [self frame].size.width, [self frame].size.height)] fill];
[[NSGraphicsContext currentContext] restoreGraphicsState];
[self setKeyboardFocusRingNeedsDisplayInRect:[self bounds]];
}
// Draw the corner header
NSRect cornerRect = [self headerRectOfCorner];
[self _drawCornerHeaderBackgroundInRect:cornerRect];
// Draw the column header background
NSRect columnHeaderRect = NSMakeRect(NSWidth(cornerRect), 0, [self frame].size.width - NSWidth(cornerRect), MBTableGridColumnHeaderHeight);
[self _drawColumnHeaderBackgroundInRect:columnHeaderRect];
CGFloat footerHeight = MBTableGridColumnHeaderHeight;
if (_footerHidden) {
footerHeight = 0.0;
}
// Draw the row header background
NSRect rowHeaderRect = NSMakeRect(0, NSMaxY(cornerRect), MBTableGridRowHeaderWidth, MAX(self.frame.size.height, self.contentView.frame.size.height));
[self _drawRowHeaderBackgroundInRect:rowHeaderRect];
// Draw the column footer background
NSRect columnFooterRect = NSMakeRect(NSWidth(cornerRect), NSMaxY(self.frame) - MBTableGridColumnHeaderHeight - 2, NSWidth(self.frame) - NSWidth(cornerRect), MBTableGridColumnHeaderHeight);
[self _drawColumnFooterBackgroundInRect:columnFooterRect];
// Draw the corner footer
NSRect footerRect = [self footerRectOfCorner];
[self _drawCornerFooterBackgroundInRect:footerRect];
}
- (void)setNeedsDisplay:(BOOL)needsDisplay {
[super setNeedsDisplay:needsDisplay];
[self.contentView setNeedsDisplay:needsDisplay];
[columnHeaderView setNeedsDisplay:needsDisplay];
[rowHeaderView setNeedsDisplay:needsDisplay];
[self.frozenContentView setNeedsDisplay:needsDisplay];
[frozenColumnHeaderView setNeedsDisplay:needsDisplay];
[frozenColumnFooterView setNeedsDisplay:needsDisplay];
[columnShadowView setNeedsDisplay:needsDisplay];
[rowShadowView setNeedsDisplay:needsDisplay];
if (!_footerHidden) {
[columnFooterView setNeedsDisplay:needsDisplay];
}
}
#pragma mark Resize scrollview content size
/** Make sure we catch mouse down events inside the scrollview, but outside the table content view. */
//- (NSView*) hitTest:(NSPoint)point {
// NSView* v = [super hitTest:point];
//
// if (v == contentScrollView.contentView) {
// NSEvent* event = self.window.currentEvent;
// if(event != nil && event.type == NSEventTypeLeftMouseDown) {
// // Clear selection
// NSMutableIndexSet* empty = [NSMutableIndexSet indexSet];
// [self setSelectedRowIndexes:empty];
// [self setSelectedColumnIndexes:empty];
// }
// }
//
// return v;
//}
- (CGFloat)resizeColumnWithIndex:(NSUInteger)columnIndex withDistance:(float)distance location:(NSPoint)location {
// Get column key
NSString *columnKey = nil;
if ([columnIndexNames count] > columnIndex) {
columnKey = columnIndexNames[columnIndex];
}
if (!columnKey) {
columnKey = [NSString stringWithFormat:@"column%lu", columnIndex];
}
// Note that we only need this rect for its origin, which won't be changing, otherwise we'd need to flush the column rect cache first
NSRect columnRect = [self rectOfColumn:columnIndex];
// Flush rect cache for this column because we're changing its size
// Note that we're doing this after calling rectOfColumn: because that would cache the rect before we change its width...
[self.columnRects removeAllObjects];
// Set new width of column
CGFloat currentWidth = [columnWidths[columnKey] floatValue];
CGFloat oldWidth = currentWidth;
CGFloat offset = 0.0;
BOOL isFrozen = [self isFrozenColumn:columnIndex];
BOOL rightToLeft = [[NSApplication sharedApplication] userInterfaceLayoutDirection] == NSUserInterfaceLayoutDirectionRightToLeft;
currentWidth += distance;
CGFloat minColumnWidth = MBTableHeaderMinimumColumnWidth;
minColumnWidth += columnHeaderView.sortAscendingImage.size.width + 2.0f;
if (currentWidth <= minColumnWidth) {
currentWidth = minColumnWidth;
offset = columnRect.origin.x - rowHeaderView.bounds.size.width - location.x + minColumnWidth + contentScrollView.contentView.bounds.origin.x;
distance = currentWidth - oldWidth;
}
columnWidths[columnKey] = @(currentWidth);
// Update views with new sizes
if (rightToLeft) {
} else {
[contentView setFrameSize:NSMakeSize(NSWidth(contentView.frame) + distance, NSHeight(contentView.frame))];
[columnHeaderView setFrameSize:NSMakeSize(NSWidth(columnHeaderView.frame) + distance, NSHeight(columnHeaderView.frame))];
[columnFooterView setFrameSize:NSMakeSize(NSWidth(columnFooterView.frame) + distance, NSHeight(columnFooterView.frame))];
}
if (isFrozen) {
[frozenContentScrollView setFrameSize:NSMakeSize(NSWidth(frozenContentView.frame) + distance, NSHeight(frozenContentScrollView.frame))];
[frozenContentView setFrameSize:NSMakeSize(NSWidth(frozenContentView.frame) + distance, NSHeight(frozenContentView.frame))];
[frozenColumnHeaderView setFrameSize:NSMakeSize(NSWidth(frozenColumnHeaderView.frame) + distance, NSHeight(frozenColumnHeaderView.frame))];
[frozenColumnFooterView setFrameSize:NSMakeSize(NSWidth(frozenColumnFooterView.frame) + distance, NSHeight(frozenColumnFooterView.frame))];
}
NSRect rectOfResizedAndVisibleRightwardColumns = NSMakeRect(columnRect.origin.x - rowHeaderView.bounds.size.width, 0, contentView.bounds.size.width - columnRect.origin.x, NSHeight(contentView.frame));
[contentView setNeedsDisplayInRect:rectOfResizedAndVisibleRightwardColumns];
NSRect rectOfResizedAndVisibleRightwardHeaders = NSMakeRect(columnRect.origin.x - rowHeaderView.bounds.size.width, 0, contentView.bounds.size.width - columnRect.origin.x, NSHeight(columnHeaderView.frame));
[columnHeaderView setNeedsDisplayInRect:rectOfResizedAndVisibleRightwardHeaders];
NSRect rectOfResizedAndVisibleRightwardFooters = NSMakeRect(columnRect.origin.x - rowHeaderView.bounds.size.width, 0, contentView.bounds.size.width - columnRect.origin.x, NSHeight(columnFooterView.frame));
[columnFooterView setNeedsDisplayInRect:rectOfResizedAndVisibleRightwardFooters];
if (isFrozen) {
NSRect rectOfResizedAndVisibleRightwardFrozenColumns = NSMakeRect(columnRect.origin.x - rowHeaderView.bounds.size.width, 0, [self frozenColumnsWidth] - columnRect.origin.x, NSHeight(frozenContentView.frame));
[frozenContentView setNeedsDisplayInRect:rectOfResizedAndVisibleRightwardFrozenColumns];
NSRect rectOfResizedAndVisibleRightwardFrozenHeaders = NSMakeRect(columnRect.origin.x - rowHeaderView.bounds.size.width, 0, [self frozenColumnsWidth] - columnRect.origin.x, NSHeight(frozenColumnHeaderView.frame));
[frozenColumnHeaderView setNeedsDisplayInRect:rectOfResizedAndVisibleRightwardFrozenHeaders];
NSRect rectOfResizedAndVisibleRightwardFrozenFooters = NSMakeRect(columnRect.origin.x - rowHeaderView.bounds.size.width, 0, [self frozenColumnsWidth] - columnRect.origin.x, NSHeight(frozenColumnFooterView.frame));
[frozenColumnFooterView setNeedsDisplayInRect:rectOfResizedAndVisibleRightwardFrozenFooters];
}
// Update the shadow views' sizes
NSRect columnShadowFrame = [columnShadowView frame];
columnShadowFrame.size.width = self.frame.size.width;
columnShadowView.frame = columnShadowFrame;
NSRect rowShadowFrame = [rowShadowView frame];
rowShadowFrame.origin.x = MBTableGridRowHeaderWidth + frozenContentView.bounds.size.width;
rowShadowFrame.size.height = self.frame.size.height;
rowShadowView.frame = rowShadowFrame;
[self updateShadows];
return offset;
}
- (void)registerForDraggedTypes:(NSArray *)pboardTypes {
// Add the column and row types to the array
NSMutableArray *types = [NSMutableArray arrayWithArray:pboardTypes];
if (!pboardTypes) {
types = [NSMutableArray array];
}
[types addObjectsFromArray:@[MBTableGridColumnDataType, MBTableGridRowDataType]];
[super registerForDraggedTypes:types];
// Register the content view for everything
[contentView registerForDraggedTypes:types];
}
#pragma mark Mouse Events
- (void)setIsEditable:(BOOL)isEditable {
_isEditable = isEditable;
self.contentView.isEditable = isEditable;
self.frozenContentView.isEditable = isEditable;
self.columnHeaderView.isEditable = isEditable;
self.rowHeaderView.isEditable = isEditable;
}
- (void)mouseDown:(NSEvent *)theEvent {
if (!self.isEditable) {
[self.nextResponder mouseDown:theEvent];
} else {
// End editing (if necessary)
[[self cell] endEditing:[[self window] fieldEditor:NO forObject:contentView]];
[[self cell] endEditing:[[self window] fieldEditor:NO forObject:frozenContentView]];
// If we're not the first responder, we need to be
if ([[self window] firstResponder] != self) {
[[self window] makeFirstResponder:self];
}
}
}
#pragma mark Keyboard Events
- (void)keyDown:(NSEvent *)theEvent {
// Special handling to detect the numeric keypad Enter key
if (theEvent.modifierFlags & NSEventModifierFlagNumericPad) {
NSString *characters = theEvent.charactersIgnoringModifiers;
if (characters.length == 1) {
unichar keyChar = [characters characterAtIndex:0];
if (keyChar == NSEnterCharacter) {
[self doCommandBySelector:@selector(insertNewlineIgnoringFieldEditor:)];
return;
}
}
}
self.keyEvent = theEvent;
[self interpretKeyEvents:@[theEvent]];
}
/*- (void)interpretKeyEvents:(NSArray *)eventArray
{
}*/
#pragma mark NSResponder Event Handlers
- (void)copy:(id)sender {
NSIndexSet *selectedColumns = [self selectedColumnIndexes];
NSIndexSet *selectedRows = [self selectedRowIndexes];
if ([[self delegate] respondsToSelector:@selector(tableGrid:copyCellsAtColumns:rows:)]) {
[[self delegate] tableGrid:self copyCellsAtColumns:selectedColumns rows:selectedRows];
}
}
- (void)paste:(id)sender {
NSIndexSet *selectedColumns = [self selectedColumnIndexes];
NSIndexSet *selectedRows = [self selectedRowIndexes];
if ([[self delegate] respondsToSelector:@selector(tableGrid:pasteCellsAtColumns:rows:)]) {
[[self delegate] tableGrid:self pasteCellsAtColumns:selectedColumns rows:selectedRows];
}
}
- (void)insertTab:(id)sender {
// Pressing "Tab" moves to the next column
[self moveRight:sender];
}
- (void)insertBacktab:(id)sender {
// We want to change the selection, not expand it
shouldOverrideModifiers = YES;
// Pressing Shift+Tab moves to the previous column
[self moveLeft:sender];
}
- (void)insertNewline:(id)sender {
if ([[NSApp currentEvent] modifierFlags] & NSEventModifierFlagShift) {
// Pressing Shift+Return moves to the previous row
shouldOverrideModifiers = YES;
[self moveUp:sender];
}
else {
// Pressing Return moves to the next row
[self moveDown:sender];
}
}
- (void)insertNewlineIgnoringFieldEditor:(id)sender {
[self insertText:@"\n"];
}
- (NSUInteger)previousNonGroupRowFromRow:(NSUInteger)row {
// moving up, but previous row is a group row, so go to the previous non-group row
BOOL isGroupRow = YES;
while (row > 0 && isGroupRow) {
isGroupRow = [self _isGroupRow:row - 1];
if (isGroupRow) {
if (row - 1 > 0) {
row--;
} else {
break;
}
} else if (row > 0){
row--;
}
}
return row;
}
- (NSUInteger)nextNonGroupRowFromRow:(NSUInteger)row {
// moving down, but next row is a group row, so go to the next non-group row
BOOL isGroupRow = YES;
while (row + 1 < self.numberOfRows && isGroupRow) {
isGroupRow = [self _isGroupRow:row + 1];
if (isGroupRow) {
if (row + 1 < self.numberOfRows) {
row++;
} else {
break;
}
} else {
if (row < self.numberOfRows) {
row++;
}
}
}
return row;
}
- (void)moveUp:(id)sender {
if (_numberOfRows == 0 || _numberOfColumns == 0) {
return;
}
NSUInteger column = [self.selectedColumnIndexes firstIndex];
NSUInteger row = [self.selectedRowIndexes firstIndex];
// Accomodate for the sticky edges
if (stickyColumnEdge == MBTableGridRightEdge) {
column = [self.selectedColumnIndexes lastIndex];
}
if (stickyRowEdge == MBTableGridBottomEdge) {
row = [self.selectedRowIndexes lastIndex];
}
// If we're already at the first row, do nothing
if (row <= 0 || (row == 1 && [self _isGroupRow:row - 1] && self.selectedRowIndexes.count == 1)) {
// we may have selections, so clear them if we do
if (self.selectedRowIndexes.count > 1) {
self.selectedRowIndexes = [NSMutableIndexSet indexSetWithIndex:row];
}
return;
}
if (row > 0) {
// If the Shift key was not held, move the selection
if (![self.selectedColumnIndexes containsIndex:column]) {
self.selectedColumnIndexes = [NSIndexSet indexSetWithIndex:column];
}
row = [self previousNonGroupRowFromRow:row];
self.selectedRowIndexes = [NSMutableIndexSet indexSetWithIndex:row];
NSRect cellRect = [self frameOfCellAtColumn:column row:row];
cellRect = [self convertRect:cellRect toView:self.contentView];
if (!NSContainsRect(self.contentView.visibleRect, cellRect)) {
cellRect.origin.x = self.contentView.visibleRect.origin.x;
[self scrollToArea:cellRect animate:NO];
}
else {
// [self setNeedsDisplayInRect:cellRect];
if (self.numberOfFrozenColumns > 0) {
[self.frozenContentView setNeedsDisplay:YES];
}
}
}
}
- (void)moveUpAndModifySelection:(id)sender {
if (_numberOfRows == 0 || _numberOfColumns == 0) {
return;
}
if (shouldOverrideModifiers) {
[self moveUp:sender];
shouldOverrideModifiers = NO;
return;
}
NSUInteger firstRow = [self.selectedRowIndexes firstIndex];
NSUInteger lastRow = [self.selectedRowIndexes lastIndex];
// If there is only one row selected, change the sticky edge to the bottom
if ([self.selectedRowIndexes count] == 1) {
stickyRowEdge = MBTableGridBottomEdge;
}
// We can't expand past the last row
if (stickyRowEdge == MBTableGridBottomEdge && firstRow <= 0)
return;
NSUInteger column = [self.selectedColumnIndexes firstIndex];
if (![self.selectedColumnIndexes containsIndex:column]) {
self.selectedColumnIndexes = [NSIndexSet indexSetWithIndex:column];
}
if (firstSelectedRow > firstRow || self.selectedRowIndexes.count == 1) {
firstRow = [self previousNonGroupRowFromRow:firstRow];
[self.selectedRowIndexes addIndex:firstRow];
} else {
if (lastRow > 0) {
[self.selectedRowIndexes removeIndex:lastRow];
}
}
NSRect cellRect = [self frameOfCellAtColumn:column row:firstRow];
cellRect = [self convertRect:cellRect toView:self.contentView];
if (!NSContainsRect(self.contentView.visibleRect, cellRect)) {
cellRect.origin.x = self.contentView.visibleRect.origin.x;
[self scrollToArea:cellRect animate:NO];
}
[self.contentView setNeedsDisplay:YES];
[self.frozenContentView setNeedsDisplay:YES];
}
- (void)moveDown:(id)sender {
if (_numberOfRows == 0 || _numberOfColumns == 0) {
return;
}
NSUInteger column = [self.selectedColumnIndexes firstIndex];
NSUInteger row = [self.selectedRowIndexes firstIndex];
// Accomodate for the sticky edges
if (stickyColumnEdge == MBTableGridRightEdge) {
column = [self.selectedColumnIndexes lastIndex];
}
if (stickyRowEdge == MBTableGridBottomEdge) {
row = [self.selectedRowIndexes lastIndex];
}
// If we're already at the last row, do nothing
if (row >= (_numberOfRows - 1) || (row == (_numberOfRows - 2) && [self _isGroupRow:row + 1])) {
NSString *characters = self.keyEvent.charactersIgnoringModifiers;
if (characters.length == 1) {
unichar keyChar = [characters characterAtIndex:0];
if (keyChar == NSEnterCharacter || keyChar == NSCarriageReturnCharacter) {
if ([self.dataSource respondsToSelector:@selector(tableGrid:addRows:)]) {
[self.dataSource tableGrid:self addRows:1];
}
}
}
[self setNeedsDisplay:YES];
return;
}
if (row + 1 < [self numberOfRows]) {
// moving down, but next row is a group row, so go to the next non-group row
row = [self nextNonGroupRowFromRow:row];
// If the Shift key was not held, move the selection
if (![self.selectedColumnIndexes containsIndex:column]) {
self.selectedColumnIndexes = [NSIndexSet indexSetWithIndex:column];
}
self.selectedRowIndexes = [NSMutableIndexSet indexSetWithIndex:row];
NSRect cellRect = [self frameOfCellAtColumn:column row:row];
cellRect = [self convertRect:cellRect toView:self.contentView];
if (!NSContainsRect(self.contentView.visibleRect, cellRect)) {
cellRect.origin.y = cellRect.origin.y - self.contentView.visibleRect.size.height + cellRect.size.height;
cellRect.origin.x = self.contentView.visibleRect.origin.x;
[self scrollToArea:cellRect animate:NO];
}
else {
// [self setNeedsDisplayInRect:cellRect];
if (self.numberOfFrozenColumns > 0) {
[self.frozenContentView setNeedsDisplay:YES];
}
}
} else {
[self setNeedsDisplay:YES];
}
}
- (void)moveDownAndModifySelection:(id)sender {
if (_numberOfRows == 0 || _numberOfColumns == 0) {
return;
}
if (shouldOverrideModifiers) {
[self moveDown:sender];
shouldOverrideModifiers = NO;
return;
}
NSUInteger firstRow = [self.selectedRowIndexes firstIndex];
NSUInteger lastRow = [self.selectedRowIndexes lastIndex];
// If there is only one row selected, change the sticky edge to the top
if ([self.selectedRowIndexes count] == 1) {
stickyRowEdge = MBTableGridTopEdge;
}
// We can't expand past the last row
if (stickyRowEdge == MBTableGridTopEdge && lastRow >= (_numberOfRows - 1))
return;
NSUInteger column = [self.selectedColumnIndexes lastIndex];
if (lastRow + 1 < [self numberOfRows]) {
// moving down, but next row is a group row, so go to the next non-group row
lastRow = [self nextNonGroupRowFromRow:lastRow];
if (firstSelectedRow == firstRow || self.selectedRowIndexes.count == 1) {
[self.selectedRowIndexes addIndex:lastRow];
} else {
if (firstRow < [self numberOfRows]) {
[self.selectedRowIndexes removeIndex:firstRow];
}
}
NSRect cellRect = [self frameOfCellAtColumn:column row:lastRow + 1];
cellRect = [self convertRect:cellRect toView:self.contentView];
if (!NSContainsRect(self.contentView.visibleRect, cellRect)) {
cellRect.origin.y = cellRect.origin.y - self.contentView.visibleRect.size.height + cellRect.size.height;
cellRect.origin.x = self.contentView.visibleRect.origin.x;
[self scrollToArea:cellRect animate:NO];
}
[self.contentView setNeedsDisplay:YES];
[self.frozenContentView setNeedsDisplay:YES];
}
}
- (void)moveLeft:(id)sender {
if (_numberOfRows == 0 || _numberOfColumns == 0) {
return;
}
NSUInteger column = [self.selectedColumnIndexes firstIndex];
NSUInteger row = [self.selectedRowIndexes firstIndex];
// Accomodate for the sticky edges
if (stickyColumnEdge == MBTableGridRightEdge) {
column = [self.selectedColumnIndexes lastIndex];
}
if (stickyRowEdge == MBTableGridBottomEdge) {
row = [self.selectedRowIndexes lastIndex];
}
if (column > 0) {
NSRect cellRect = [self frameOfCellAtColumn:column - 1 row:row];
cellRect = [self convertRect:cellRect toView:contentScrollView.contentView];
if (![self scrollForFrozenColumnsFromColumn:column right:NO]) {
if (!NSContainsRect(self.contentView.visibleRect, cellRect)) {
cellRect.origin.y = self.contentView.visibleRect.origin.y;
[self scrollToArea:cellRect animate:NO];
} else {
[self setNeedsDisplayInRect:cellRect];
if (self.numberOfFrozenColumns > 0) {
[self.frozenContentView setNeedsDisplay:YES];
[self.frozenColumnHeaderView setNeedsDisplay:YES];
}
}
}
}
// If we're already at the first column, do nothing
if (column == 0) {
return;
}
// If the Shift key was not held, move the selection