forked from cappuccino/cappuccino
/
CPTableView.j
1807 lines (1366 loc) · 52.1 KB
/
CPTableView.j
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
/*
* CPTableView.j
* AppKit
*
* Created by Francisco Tolmasky.
* Copyright 2009, 280 North, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import <Foundation/CPArray.j>
@import "CPControl.j"
@import "CPTableColumn.j"
@import "_CPCornerView.j"
@import "CPScroller.j"
CPTableViewColumnDidMoveNotification = @"CPTableViewColumnDidMoveNotification";
CPTableViewColumnDidResizeNotification = @"CPTableViewColumnDidResizeNotification";
CPTableViewSelectionDidChangeNotification = @"CPTableViewSelectionDidChangeNotification";
CPTableViewSelectionIsChangingNotification = @"CPTableViewSelectionIsChangingNotification";
#include "CoreGraphics/CGGeometry.h"
var CPTableViewDataSource_tableView_setObjectValue_forTableColumn_row_ = 1 << 2,
CPTableViewDataSource_tableView_acceptDrop_row_dropOperation_ = 1 << 3,
CPTableViewDataSource_tableView_namesOfPromisedFilesDroppedAtDestination_forDraggedRowsWithIndexes_ = 1 << 4,
CPTableViewDataSource_tableView_validateDrop_proposedRow_proposedDropOperation_ = 1 << 5,
CPTableViewDataSource_tableView_writeRowsWithIndexes_toPasteboard_ = 1 << 6,
CPTableViewDataSource_tableView_sortDescriptorsDidChange_ = 1 << 7;
var CPTableViewDelegate_selectionShouldChangeInTableView_ = 1 << 0,
CPTableViewDelegate_tableView_dataViewForTableColumn_row_ = 1 << 1,
CPTableViewDelegate_tableView_didClickTableColumn_ = 1 << 2,
CPTableViewDelegate_tableView_didDragTableColumn_ = 1 << 3,
CPTableViewDelegate_tableView_heightOfRow_ = 1 << 4,
CPTableViewDelegate_tableView_isGroupRow_ = 1 << 5,
CPTableViewDelegate_tableView_mouseDownInHeaderOfTableColumn_ = 1 << 6,
CPTableViewDelegate_tableView_nextTypeSelectMatchFromRow_toRow_forString_ = 1 << 7,
CPTableViewDelegate_tableView_selectionIndexesForProposedSelection_ = 1 << 8,
CPTableViewDelegate_tableView_shouldEditTableColumn_row_ = 1 << 9,
CPTableViewDelegate_tableView_shouldSelectRow_ = 1 << 10,
CPTableViewDelegate_tableView_shouldSelectTableColumn_ = 1 << 11,
CPTableViewDelegate_tableView_shouldShowViewExpansionForTableColumn_row_ = 1 << 12,
CPTableViewDelegate_tableView_shouldTrackView_forTableColumn_row_ = 1 << 13,
CPTableViewDelegate_tableView_shouldTypeSelectForEvent_withCurrentSearchString_ = 1 << 14,
CPTableViewDelegate_tableView_toolTipForView_rect_tableColumn_row_mouseLocation_ = 1 << 15,
CPTableViewDelegate_tableView_typeSelectStringForTableColumn_row_ = 1 << 16,
CPTableViewDelegate_tableView_willDisplayView_forTableColumn_row_ = 1 << 17,
CPTableViewDelegate_tableViewSelectionDidChange_ = 1 << 18,
CPTableViewDelegate_tableViewSelectionIsChanging_ = 1 << 19;
// TODO: add docs
CPTableViewSelectionHighlightStyleRegular = 0;
CPTableViewSelectionHighlightStyleSourceList = 1;
CPTableViewGridNone = 0;
CPTableViewSolidVerticalGridLineMask = 1 << 0;
CPTableViewSolidHorizontalGridLineMask = 1 << 1;
#define NUMBER_OF_COLUMNS() (_tableColumns.length)
#define UPDATE_COLUMN_RANGES_IF_NECESSARY() if (_dirtyTableColumnRangeIndex !== CPNotFound) [self _recalculateTableColumnRanges];
@implementation _CPTableDrawView : CPView
{
CPTableView _tableView;
}
- (id)initWithTableView:(CPTableView)aTableView
{
self = [super init];
if (self)
_tableView = aTableView;
return self;
}
- (void)drawRect:(CGRect)aRect
{
var frame = [self frame],
context = [[CPGraphicsContext currentContext] graphicsPort];
CGContextTranslateCTM(context, -_CGRectGetMinX(frame), -_CGRectGetMinY(frame));
[_tableView _drawRect:aRect];
}
@end
@implementation CPTableView : CPControl
{
id _dataSource;
CPInteger _implementedDataSourceMethods;
id _delegate;
CPInteger _implementedDelegateMethods;
CPArray _tableColumns;
CPArray _tableColumnRanges;
CPInteger _dirtyTableColumnRangeIndex;
CPInteger _numberOfHiddenColumns;
BOOL _reloadAllRows;
Object _objectValues;
CPRange _exposedRows;
CPIndexSet _exposedColumns;
Object _dataViewsForTableColumns;
Object _cachedDataViews;
//Configuring Behavior
BOOL _allowsColumnReordering;
BOOL _allowsColumnResizing;
BOOL _allowsMultipleSelection;
BOOL _allowsEmptySelection;
//Setting Display Attributes
CGSize _intercellSpacing;
float _rowHeight;
BOOL _usesAlternatingRowBackgroundColors;
CPArray _alternatingRowBackgroundColors;
unsigned _selectionHighlightMask;
unsigned _currentHighlightedTableColumn;
unsigned _gridStyleMask;
CPColor _gridColor;
unsigned _numberOfRows;
CPTableHeaderView _headerView;
_CPCornerView _cornerView;
CPIndexSet _selectedColumnIndexes;
CPIndexSet _selectedRowIndexes;
CPInteger _selectionAnchorRow;
_CPTableDrawView _tableDrawView;
}
- (id)initWithFrame:(CGRect)aFrame
{
self = [super initWithFrame:aFrame];
if (self)
{
//Configuring Behavior
_allowsColumnReordering = YES;
_allowsColumnResizing = YES;
_allowsMultipleSelection = NO;
_allowsEmptySelection = YES;
_allowsColumnSelection = NO;
_tableViewFlags = 0;
//Setting Display Attributes
_selectionHighlightMask = CPTableViewSelectionHighlightStyleRegular;
[self setUsesAlternatingRowBackgroundColors:NO];
[self setAlternatingRowBackgroundColors:[[CPColor whiteColor], [CPColor colorWithHexString:@"e4e7ff"]]];
_tableColumns = [];
_tableColumnRanges = [];
_dirtyTableColumnRangeIndex = CPNotFound;
_numberOfHiddenColumns = 0;
_objectValues = { };
_dataViewsForTableColumns = { };
_dataViews= [];
_numberOfRows = 0;
_exposedRows = [CPIndexSet indexSet];
_exposedColumns = [CPIndexSet indexSet];
_cachedDataViews = { };
_intercellSpacing = _CGSizeMake(0.0, 0.0);
_rowHeight = 19.0;
[self setGridColor:[CPColor grayColor]];
[self setGridStyleMask:CPTableViewGridNone];
_headerView = [[CPTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, [self bounds].size.width, _rowHeight)];
[_headerView setTableView:self];
_cornerView = [[_CPCornerView alloc] initWithFrame:CGRectMake(0, 0, [CPScroller scrollerWidth], CGRectGetHeight([_headerView frame]))];
_selectedColumnIndexes = [CPIndexSet indexSet];
_selectedRowIndexes = [CPIndexSet indexSet];
_tableDrawView = [[_CPTableDrawView alloc] initWithTableView:self];
[_tableDrawView setBackgroundColor:[CPColor clearColor]];
[self addSubview:_tableDrawView];
}
return self;
}
- (void)setDataSource:(id)aDataSource
{
if (_dataSource === aDataSource)
return;
_dataSource = aDataSource;
_implementedDataSourceMethods = 0;
if (!_dataSource)
return;
if (![_dataSource respondsToSelector:@selector(numberOfRowsInTableView:)])
[CPException raise:CPInternalInconsistencyException
reason:[aDataSource description] + " does not implement numberOfRowsInTableView:."];
if (![_dataSource respondsToSelector:@selector(tableView:objectValueForTableColumn:row:)])
[CPException raise:CPInternalInconsistencyException
reason:[aDataSource description] + " does not implement tableView:objectValueForTableColumn:row:"];
if ([_dataSource respondsToSelector:@selector(tableView:setObjectValue:forTableColumn:row:)])
_implementedDataSourceMethods |= CPTableViewDataSource_tableView_setObjectValue_forTableColumn_row_;
if ([_dataSource respondsToSelector:@selector(tableView:setObjectValue:forTableColumn:row:)])
_implementedDataSourceMethods |= CPTableViewDataSource_tableView_acceptDrop_row_dropOperation_;
if ([_dataSource respondsToSelector:@selector(tableView:namesOfPromisedFilesDroppedAtDestination:forDraggedRowsWithIndexes:)])
_implementedDataSourceMethods |= CPTableViewDataSource_tableView_namesOfPromisedFilesDroppedAtDestination_forDraggedRowsWithIndexes_;
if ([_dataSource respondsToSelector:@selector(tableView:validateDrop:proposedRow:proposedDropOperation:)])
_implementedDataSourceMethods |= CPTableViewDataSource_tableView_validateDrop_proposedRow_proposedDropOperation_;
if ([_dataSource respondsToSelector:@selector(tableView:writeRowsWithIndexes:toPasteboard:)])
_implementedDataSourceMethods |= CPTableViewDataSource_tableView_writeRowsWithIndexes_toPasteboard_;
[self reloadData];
}
- (id)dataSource
{
return _dataSource;
}
//Loading Data
- (void)reloadDataForRowIndexes:(CPIndexSet)rowIndexes columnIndexes:(CPIndexSet)columnIndexes
{
[self reloadData];
// [_previouslyExposedRows removeIndexes:rowIndexes];
// [_previouslyExposedColumns removeIndexes:columnIndexes];
}
- (void)reloadData
{
if (!_dataSource)
return;
_reloadAllRows = YES;
_objectValues = { };
// This updates the size too.
[self noteNumberOfRowsChanged];
[self setNeedsLayout];
[self setNeedsDisplay:YES];
}
//Target-action Behavior
- (void)setDoubleAction:(SEL)anAction
{
_doubleAction = anAction;
}
- (SEL)doubleAction
{
return _doubleAction;
}
/*
* - clickedColumn
* - clickedRow
*/
//Configuring Behavior
- (void)setAllowsColumnReordering:(BOOL)shouldAllowColumnReordering
{
_allowsColumnReordering = !!shouldAllowColumnReordering;
}
- (BOOL)allowsColumnReordering
{
return _allowsColumnReordering;
}
- (void)setAllowsColumnResizing:(BOOL)shouldAllowColumnResizing
{
_allowsColumnResizing = !!shouldAllowColumnResizing;
}
- (BOOL)allowsColumnResizing
{
return _allowsColumnResizing;
}
- (void)setAllowsMultipleSelection:(BOOL)shouldAllowMultipleSelection
{
_allowsMultipleSelection = !!shouldAllowMultipleSelection;
}
- (BOOL)allowsMultipleSelection
{
return _allowsMultipleSelection;
}
- (void)setAllowsEmptySelection:(BOOL)shouldAllowEmptySelection
{
_allowsEmptySelection = !!shouldAllowEmptySelection;
}
- (BOOL)allowsEmptySelection
{
return _allowsEmptySelection;
}
- (void)setAllowsColumnSelection:(BOOL)shouldAllowColumnSelection
{
_allowsColumnSelection = !!shouldAllowColumnSelection;
}
- (BOOL)allowsColumnSelection
{
return _allowsColumnSelection;
}
//Setting Display Attributes
- (void)setIntercellSpacing:(CGSize)aSize
{
if (_CGSizeEqualToSize(_intercellSpacing, aSize))
return;
_intercellSpacing = _CGSizeMakeCopy(aSize);
[self setNeedsLayout];
}
- (void)setThemeState:(int)astae
{
}
- (CGSize)intercellSpacing
{
return _CGSizeMakeCopy(_intercellSpacing);
}
- (void)setRowHeight:(unsigned)aRowHeight
{
aRowHeight = +aRowHeight;
if (_rowHeight === aRowHeight)
return;
_rowHeight = MAX(0.0, aRowHeight);
[self setNeedsLayout];
}
- (unsigned)rowHeight
{
return _rowHeight;
}
- (void)setUsesAlternatingRowBackgroundColors:(BOOL)shouldUseAlternatingRowBackgroundColors
{
// TODO:need to look at how one actually sets the alternating row, a tip at:
// http://forums.macnn.com/79/developer-center/228347/nstableview-alternating-row-colors/
// otherwise this may not be feasible or may introduce an additional change req'd in CP
// we'd probably need to iterate through rowId % 2 == 0 and setBackgroundColor with
// whatever the alternating row color is.
_usesAlternatingRowBackgroundColors = shouldUseAlternatingRowBackgroundColors;
}
- (BOOL)usesAlternatingRowBackgroundColors
{
return _usesAlternatingRowBackgroundColors;
}
- (void)setAlternatingRowBackgroundColors:(CPArray)alternatingRowBackgroundColors
{
if ([_alternatingRowBackgroundColors isEqual:alternatingRowBackgroundColors])
return;
_alternatingRowBackgroundColors = alternatingRowBackgroundColors;
[self setNeedsDisplay:YES];
}
- (CPArray)alternatingRowBackgroundColors
{
return _alternatingRowBackgroundColors;
}
- (unsigned)selectionHighlightStyle
{
return _selectionHighlightMask;
}
- (void)setSelectionHighlightStyle:(unsigned)aSelectionHighlightStyle
{
_selectionHighlightMask = aSelectionHighlightStyle;
}
/*
* - indicatorImageInTableColumn:
* - setIndicatorImage:inTableColumn:
*/
- (void)setGridColor:(CPColor)aColor
{
if (_gridColor === aColor)
return;
_gridColor = aColor;
[self setNeedsDisplay:YES];
}
- (CPColor)gridColor
{
return _gridColor;
}
- (void)setGridStyleMask:(unsigned)aGrideStyleMask
{
if (_gridStyleMask === aGrideStyleMask)
return;
_gridStyleMask = aGrideStyleMask
[self setNeedsDisplay:YES];
}
- (unsigned)gridStyleMask
{
return _gridStyleMask;
}
//Column Management
- (void)addTableColumn:(CPTableColumn)aTableColumn
{
[_tableColumns addObject:aTableColumn];
[aTableColumn setTableView:self];
if (_dirtyTableColumnRangeIndex < 0)
_dirtyTableColumnRangeIndex = NUMBER_OF_COLUMNS() - 1;
else
_dirtyTableColumnRangeIndex = MIN(NUMBER_OF_COLUMNS() - 1, _dirtyTableColumnRangeIndex);
[self setNeedsLayout];
}
- (void)removeTableColumn:(CPTableColumn)aTableColumn
{
if ([aTableColumn tableView] !== self)
return;
var index = [_tableColumns indexOfObjectIdenticalTo:aTableColumn];
if (index === CPNotFound)
return;
[aTableColumn setTableView:nil];
[_tableColumns removeObjectAtIndex:index];
var tableColumnUID = [aTableColumn UID];
if (_objectValues[tableColumnUID])
_objectValues[tableColumnUID] = nil;
if (_dirtyTableColumnRangeIndex < 0)
_dirtyTableColumnRangeIndex = index;
else
_dirtyTableColumnRangeIndex = MIN(index, _dirtyTableColumnRangeIndex);
[self setNeedsLayout];
}
- (void)moveColumn:(unsigned)fromIndex toColumn:(unsigned)toIndex
{
fromIndex = +fromIndex;
toIndex = +toIndex;
if (fromIndex === toIndex)
return;
if (_dirtyTableColumnRangeIndex < 0)
_dirtyTableColumnRangeIndex = MIN(fromIndex, toIndex);
else
_dirtyTableColumnRangeIndex = MIN(fromIndex, toIndex, _dirtyTableColumnRangeIndex);
if (toIndex > fromIndex)
--toIndex;
var tableColumn = _tableColumns[fromIndex];
[_tableColumns removeObjectAtIndex:fromIndex];
[_tableColumns insertObject:tableColumn atIndex:toIndex];
[self setNeedsLayout];
}
- (CPArray)tableColumns
{
return _tableColumns;
}
- (CPInteger)columnWithIdentifier:(CPString)anIdentifier
{
var index = 0,
count = NUMBER_OF_COLUMNS();
for (; index < count; ++index)
if ([_tableColumns identifier] === anIdentifier)
return index;
return CPNotFound;
}
- (CPTableColumn)tableColumnWithIdentifier:(CPString)anIdentifier
{
var index = [self columnWithIdentifier:anIdentifier];
if (index === CPNotFound)
return nil;
return _tableColumns[index];
}
//Selecting Columns and Rows
- (void)selectColumnIndexes:(CPIndexSet)columns byExtendingSelection:(BOOL)shouldExtendSelection
{
// We deselect all columns when selecting rows.
_selectedRowIndexes = [CPIndexSet indexSet];
if (shouldExtendSelection)
[_selectedColumnIndexes addIndexes:columns];
else
_selectedColumnIndexes = [columns copy];
[self setNeedsLayout];
}
- (void)selectRowIndexes:(CPIndexSet)rows byExtendingSelection:(BOOL)shouldExtendSelection
{
// We deselect all rows when selecting columns.
_selectedColumnIndexes = [CPIndexSet indexSet];
if (shouldExtendSelection)
[_selectedRowIndexes addIndexes:rows];
else
_selectedRowIndexes = [rows copy];
[self setNeedsLayout];
}
- (CPIndexSet)selectedColumnIndexes
{
return _selectedColumnIndexes;
}
- (void)selectedRowIndexes
{
return _selectedRowIndexes;
}
- (void)deselectColumn:(CPInteger)aColumn
{
[_selectedColumnIndexes removeIndex:aColumn];
}
- (void)deselectRow:(CPInteger)aRow
{
[_selectedRowIndexes removeIndex:aRow];
}
- (CPInteger)numberOfSelectedColumns
{
return [_selectedColumnIndexes count];
}
- (CPInteger)numberOfSelectedRows
{
return [_selectedRowIndexes count];
}
/*
- (CPInteger)selectedColumn
* - selectedRow
*/
- (BOOL)isColumnSelected:(CPInteger)aColumn
{
return [_selectedColumnIndexes containsIndex:aColumn];
}
- (BOOL)isRowSelected:(CPInteger)aRow
{
return [_selectedRowIndexes containsIndex:aRow];
}
/*
- (void)selectAll:
* - deselectAll:
* - allowsTypeSelect
* - setAllowsTypeSelect:
*/
//Table Dimensions
- (int)numberOfColumns
{
return NUMBER_OF_COLUMNS();
}
/*
Returns the number of rows in the receiver.
*/
- (int)numberOfRows
{
if (!_dataSource)
return 0;
return [_dataSource numberOfRowsInTableView:self];
}
//Displaying Cell
/*
* - preparedCellAtColumn:row:
*/
//Editing Cells
/*
* - editColumn:row:withEvent:select:
* - editedColumn
* - editedRow
*/
//Setting Auxiliary Views
/*
* - setHeaderView:
* - headerView
* - setCornerView:
* - cornerView
*/
- (CPView)cornerView
{
return _cornerView;
}
- (void)setCornerView:(CPView)aView
{
if (_cornerView === aView)
return;
_cornerView = aView;
var scrollView = [[self superview] superview];
if ([scrollView isKindOfClass:[CPScrollView class]] && [scrollView documentView] === self)
[scrollView _updateCornerAndHeaderView];
}
- (CPView)headerView
{
return _headerView;
}
- (void)setHeaderView:(CPView)aHeaderView
{
if (_headerView === aHeaderView)
return;
[_headerView setTableView:nil];
_headerView = aHeaderView;
if (_headerView)
{
[_headerView setTableView:self];
[_headerView setFrameSize:_CGSizeMake(_CGRectGetWidth([self frame]), _CGRectGetHeight([_headerView frame]))];
}
var scrollView = [[self superview] superview];
if ([scrollView isKindOfClass:[CPScrollView class]] && [scrollView documentView] === self)
[scrollView _updateCornerAndHeaderView];
}
//Layout Support
// Complexity:
// O(Columns)
- (void)_recalculateTableColumnRanges
{
if (_dirtyTableColumnRangeIndex < 0)
return;
var index = _dirtyTableColumnRangeIndex,
count = NUMBER_OF_COLUMNS(),
x = index === 0 ? 0.0 : CPMaxRange(_tableColumnRanges[index - 1]);
for (; index < count; ++index)
{
var tableColumn = _tableColumns[index];
if ([tableColumn isHidden])
_tableColumnRanges[index] = CPMakeRange(x, 0.0);
else
{
var width = [_tableColumns[index] width];
_tableColumnRanges[index] = CPMakeRange(x, width);
x += width;
}
}
_tableColumnRanges.length = count;
_dirtyTableColumnRangeIndex = CPNotFound;
}
// Complexity:
// O(1)
- (CGRect)rectOfColumn:(CPInteger)aColumnIndex
{
aColumnIndex = +aColumnIndex;
if (aColumnIndex < 0 || aColumnIndex >= NUMBER_OF_COLUMNS())
return _CGRectMakeZero();
UPDATE_COLUMN_RANGES_IF_NECESSARY();
var range = _tableColumnRanges[aColumnIndex];
return _CGRectMake(range.location, 0.0, range.length, CGRectGetHeight([self bounds]));
}
- (CGRect)rectOfRow:(CPInteger)aRowIndex
{
if (NO)
return NULL;
// FIXME: WRONG: ASK TABLE COLUMN RANGE
return _CGRectMake(0.0, (aRowIndex * (_rowHeight + _intercellSpacing.height)), _CGRectGetWidth([self bounds]), _rowHeight);
}
// Complexity:
// O(1)
- (CPRange)rowsInRect:(CGRect)aRect
{
// If we have no rows, then we won't intersect anything.
if (_numberOfRows <= 0)
return CPMakeRange(0, 0);
var bounds = [self bounds];
// No rows if the rect doesn't even intersect us.
if (!CGRectIntersectsRect(aRect, bounds))
return CPMakeRange(0, 0);
var firstRow = [self rowAtPoint:aRect.origin];
// first row has to be undershot, because if not we wouldn't be intersecting.
if (firstRow < 0)
firstRow = 0;
var lastRow = [self rowAtPoint:_CGPointMake(0.0, _CGRectGetMaxY(aRect))];
// last row has to be overshot, because if not we wouldn't be intersecting.
if (lastRow < 0)
lastRow = _numberOfRows - 1;
return CPMakeRange(firstRow, lastRow - firstRow + 1);
}
// Complexity:
// O(lg Columns) if table view contains no hidden columns
// O(Columns) if table view contains hidden columns
- (CPIndexSet)columnIndexesInRect:(CGRect)aRect
{
var column = MAX(0, [self columnAtPoint:_CGPointMake(aRect.origin.x, 0.0)]),
lastColumn = [self columnAtPoint:_CGPointMake(_CGRectGetMaxX(aRect), 0.0)];
if (lastColumn === CPNotFound)
lastColumn = NUMBER_OF_COLUMNS() - 1;
// Don't bother doing the expensive removal of hidden indexes if we have no hidden columns.
if (_numberOfHiddenColumns <= 0)
return [CPIndexSet indexSetWithIndexesInRange:CPMakeRange(column, lastColumn - column + 1)];
//
var indexSet = [CPIndexSet indexSet];
for (; column <= lastColumn; ++column)
{
var tableColumn = _tableColumns[column];
if (![tableColumn isHidden])
[indexSet addIndex:column];
}
return indexSet;
}
// Complexity:
// O(lg Columns) if table view contains now hidden columns
// O(Columns) if table view contains hidden columns
- (CPInteger)columnAtPoint:(CGPoint)aPoint
{
var bounds = [self bounds];
if (!_CGRectContainsPoint(bounds, aPoint))
return CPNotFound;
UPDATE_COLUMN_RANGES_IF_NECESSARY();
var x = aPoint.x,
low = 0,
high = _tableColumnRanges.length - 1;
while (low <= high)
{
var middle = FLOOR(low + (high - low) / 2),
range = _tableColumnRanges[middle];
if (x < range.location)
high = middle - 1;
else if (x >= CPMaxRange(range))
low = middle + 1;
else
{
var numberOfColumns = _tableColumnRanges.length;
while (middle < numberOfColumns && [_tableColumns[middle] isHidden])
++middle;
if (middle < numberOfColumns)
return middle;
return CPNotFound;
}
}
return CPNotFound;
}
- (CPInteger)rowAtPoint:(CGPoint)aPoint
{
var y = aPoint.y;
if (NO)
{
}
var row = FLOOR(y / (_rowHeight + _intercellSpacing.height));
if (row >= _numberOfRows)
return -1;
return row;
}
- (CGRect)frameOfDataViewAtColumn:(CPInteger)aColumn row:(CPInteger)aRow
{
UPDATE_COLUMN_RANGES_IF_NECESSARY();
var tableColumnRange = _tableColumnRanges[aColumn],
rectOfRow = [self rectOfRow:aRow];
return _CGRectMake(tableColumnRange.location, _CGRectGetMinY(rectOfRow), tableColumnRange.length, _CGRectGetHeight(rectOfRow));
}
/*
* - columnAutoresizingStyle
* - setColumnAutoresizingStyle:
*/
- (void)sizeLastColumnToFit
{
var superview = [self superview];
if (!superview)
return;
var superviewSize = [superview bounds].size;
UPDATE_COLUMN_RANGES_IF_NECESSARY();
var count = NUMBER_OF_COLUMNS();
while (count-- && [_tableColumns[count] isHidden]) ;
if (count >= 0)
[_tableColumns[count] setWidth:MAX(0.0, superviewSize.width - _CGRectGetMinX([self rectOfColumn:count]))];
[self setNeedsLayout];
}
- (void)noteNumberOfRowsChanged
{
_numberOfRows = [_dataSource numberOfRowsInTableView:self];
[self tile];
}
- (void)tile
{
UPDATE_COLUMN_RANGES_IF_NECESSARY();
// FIXME: variable row heights.
var width = _tableColumnRanges.length > 0 ? CPMaxRange([_tableColumnRanges lastObject]) : 0.0,
height = (_rowHeight + _intercellSpacing.height) * _numberOfRows,
superview = [self superview];
if ([superview isKindOfClass:[CPClipView class]])
{
var superviewSize = [superview bounds].size;
width = MAX(superviewSize.width, width);
height = MAX(superviewSize.height, height);
}
[self setFrameSize:_CGSizeMake(width, height)];
[self setNeedsLayout];
[self setNeedsDisplay:YES];
}
/*
* - tile
* - sizeToFit
* - noteHeightOfRowsWithIndexesChanged:
*/
//Scrolling
/*
* - scrollRowToVisible:
* - scrollColumnToVisible:
*/
//Persistence
/*
* - autosaveName
* - autosaveTableColumns
* - setAutosaveName:
* - setAutosaveTableColumns:
*/
//Setting the Delegate:(id)aDelegate
- (void)setDelegate:(id)aDelegate
{
if (_delegate === aDelegate)
return;
var defaultCenter = [CPNotificationCenter defaultCenter];
if (_delegate)
{
if ([_delegate respondsToSelector:@selector(tableViewColumnDidMove:)])
[defaultCenter
removeObserver:_delegate
name:CPTableViewColumnDidMoveNotification
object:self];
if ([_delegate respondsToSelector:@selector(tableViewColumnDidResize:)])
[defaultCenter
removeObserver:_delegate
name:CPTableViewColumnDidResizeNotification
object:self];
if ([_delegate respondsToSelector:@selector(tableViewSelectionDidChange:)])
[defaultCenter
removeObserver:_delegate
name:CPTableViewSelectionDidChangeNotification
object:self];