/
_CPPopUpList.j
858 lines (703 loc) · 23.6 KB
/
_CPPopUpList.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
/*
* _CPPopUpList.j
* AppKit
*
* Created by Aparajita Fishman.
* Copyright (c) 2012, The Cappuccino Foundation
*
* 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 "CPTableView.j"
@import "_CPPopUpListDataSource.j"
/*!
Notification sent when the list is about to pop up. \c object is the _CPPopUpList.
*/
_CPPopUpListWillPopUpNotification = @"_CPPopUpListWillPopUpNotification";
/*!
Notification sent when the list is about to be dismissed. \c object is the _CPPopUpList.
*/
_CPPopUpListWillDismissNotification = @"_CPPopUpListWillDismissNotification";
/*!
Notification sent when the list is dismissed. \c object is the _CPPopUpList.
*/
_CPPopUpListDidDismissNotification = @"_CPPopUpListDidDismissNotification";
/*!
Notification sent by when an item is selected. \c object is the _CPPopUpList.
When this is received the list has already been dismissed and the dismiss notification has been sent.
*/
_CPPopUpListItemWasClickedNotification = @"_CPPopUpListItemWasClickedNotification";
/*!
@ignore
The minimum number of items that must be visible below the related field.
If less than this number would be completely visible, and there is room for this many complete items
above the field, the list is displayed above.
*/
var ListMinimumItems = 3;
/*! @ignore */
var ListColumnIdentifier = @"1";
/*!
This class is a controller for a panel that can pop up and display a scrollable list of items in a CPTableView.
It is used by CPComboBox to display the list of choices.
This class requires a data source which must conform to the interface of _CPPopUpListDataSource.
Objects of this class send the following notifications:
_CPPopUpListWillPopUpNotification
_CPPopUpListWillDismissNotification
_CPPopUpListDidDismissNotification
_CPPopUpListItemWasClickedNotification
*/
@implementation _CPPopUpList : CPObject
{
_CPPopUpListDataSource _dataSource;
BOOL _itemWasClicked;
BOOL _listWasClicked;
int _listWidth;
_CPPopUpPanel _panel;
CPScrollView _scrollView;
_CPPopUpTableView _tableView;
CPTableColumn _tableColumn;
}
#pragma mark Creating and Displaying a List
/*!
Creates a pop up list of choices that will display in a scrollable CPTableView.
@param aDataSource A subclass of _CPPopUpListDataSource
*/
- (id)initWithDataSource:(_CPPopUpListDataSource)aDataSource
{
self = [super init];
if (self)
{
[self setDataSource:aDataSource];
_itemWasClicked = NO;
_listWasClicked = NO;
_listWidth = 0;
_tableView = [self makeTableView];
// Start with a default size, we will resize it later
frame = CGRectMake(0, 0, 200, 200);
_tableColumn = [[CPTableColumn alloc] initWithIdentifier:ListColumnIdentifier];
[_tableColumn setWidth:CGRectGetWidth(frame) - [CPScroller scrollerWidth]];
[_tableColumn setResizingMask:CPTableColumnAutoresizingMask];
[_tableView addTableColumn:_tableColumn];
_scrollView = [self makeScrollViewWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame), CGRectGetHeight(frame))];
[_scrollView setDocumentView:_tableView];
// This has to be done after setDocumentView so that the table knows which scroll view to update
[_tableView setHeaderView:nil];
_panel = [self makeListPanelWithFrame:frame];
[[_panel contentView] addSubview:_scrollView];
[_panel setInitialFirstResponder:_tableView];
if ([_dataSource numberOfItemsInList:self] > 0)
[_tableView selectRowIndexes:[CPIndexSet indexSetWithIndex:0] byExtendingSelection:NO];
else
[_tableView setEnabled:NO];
[_scrollView scrollToBeginningOfDocument:nil];
}
return self;
}
/*! @ignore */
- (CPPanel)makeListPanelWithFrame:(CGRect)aFrame
{
var panel = [[_CPPopUpPanel alloc] initWithContentRect:aFrame styleMask:CPBorderlessWindowMask];
[panel setTitle:@""];
[panel setFloatingPanel:YES];
[panel setBecomesKeyOnlyIfNeeded:YES];
[panel setLevel:CPPopUpMenuWindowLevel];
[panel setHasShadow:YES];
[panel setShadowStyle:CPMenuWindowShadowStyle];
[panel setDelegate:self];
return panel;
}
/*! @ignore */
- (_CPPopUpTableView)makeTableView
{
[self removeTableViewObservers];
var table = [[_CPPopUpTableView alloc] initWithFrame:CGRectMakeZero()];
[table setDelegate:self];
[table setDataSource:self];
[table setColumnAutoresizingStyle:CPTableViewLastColumnOnlyAutoresizingStyle];
[table setUsesAlternatingRowBackgroundColors:NO];
[table setAllowsMultipleSelection:NO];
[table setIntercellSpacing:CGSizeMake(3, 2)];
[table setTarget:self];
[table setDoubleAction:@selector(tableViewClickAction:)];
[table setAction:@selector(tableViewClickAction:)];
[table setRowHeight:[self rowHeightForTableView:table]];
return table;
}
/*! @ignore */
- (void)removeTableViewObservers
{
if (_tableView)
{
var defaultCenter = [CPNotificationCenter defaultCenter];
[defaultCenter removeObserver:self name:CPTableViewSelectionIsChangingNotification object:_tableView];
[defaultCenter removeObserver:self name:CPTableViewSelectionDidChangeNotification object:_tableView];
}
}
/*! @ignore */
- (CPScrollView)makeScrollViewWithFrame:(CGRect)aFrame
{
var scroll = [[CPScrollView alloc] initWithFrame:aFrame];
[scroll setBorderType:CPLineBorder];
[scroll setAutohidesScrollers:NO];
[scroll setHasVerticalScroller:YES];
[scroll setHasHorizontalScroller:NO];
[scroll setLineScroll:[_tableView rowHeight]];
[scroll setVerticalPageScroll:0.0];
return scroll;
}
/*!
Pop up the list if it is not already visible.
If it is not visible, a _CPPopUpListWillPopUpNotification will be sent.
@param aRect A rect (in \c aView coordinates) to display relative to
@param aView The view whose coordinate system \c aRect is in
@param offset How far to offset the list from \c aRect
*/
- (void)popUpRelativeToRect:(CGRect)aRect view:(CPView)aView offset:(int)offset
{
if ([_panel isVisible])
return;
var rowRect = [_tableView rectOfRow:[self numberOfRowsInTableView:_tableView] - 1],
frame = CGRectMake(0, 0, MAX(_listWidth, CGRectGetWidth(aRect)), CGRectGetMaxY(rowRect));
// Place the frame relative to aRect and constrain it to the screen bounds
frame = [self constrain:frame relativeToRect:aRect view:aView offset:offset];
[_panel setFrame:frame];
[_scrollView setFrameSize:CGSizeMakeCopy(frame.size)];
[_tableView setEnabled:[_dataSource numberOfItemsInList:self] > 0];
[self scrollItemAtIndexToTop:[_tableView selectedRow]];
[self listWillPopUp];
[_panel orderFront:nil];
}
#pragma mark Setting Display Attributes
/*!
Returns the desired width of the list.
*/
- (int)listWidth
{
return _listWidth;
}
/*!
Sets the desired width of the list for the next call to \ref showListForfield:relativeTo:.
Note that the actual display width may be larger if the given width is less than the width of the associated
field.
*/
- (void)setListWidth:(int)width
{
_listWidth = width;
}
- (void)setFont:(CPFont)aFont
{
var oldDataView = [_tableColumn dataView],
newDataView = [CPTextField new];
[newDataView setFont:aFont];
[newDataView setAlignment:[oldDataView alignment]];
[_tableColumn setDataView:newDataView];
// Force the data view cache to flush
[_tableView reloadData];
}
- (void)setAlignment:(CPTextAlignment)alignment
{
var oldDataView = [_tableColumn dataView],
newDataView = [CPTextField new];
[newDataView setAlignment:alignment];
[newDataView setFont:[oldDataView font]];
[_tableColumn setDataView:newDataView];
// Force the data view cache to flush
[_tableView reloadData];
}
/*!
Returns whether the list is currently visible.
*/
- (BOOL)isVisible
{
return [_panel isVisible];
}
/*!
Returns the desired row height for the table view.
Subclasses should override this if they want something other than the default.
*/
- (int)rowHeightForTableView:(CPTableView)aTableView
{
return [aTableView rowHeight];
}
/*!
Returns the table view used by the list.
*/
- (CPTableView)tableView
{
return _tableView;
}
/*!
Returns the single table column used by the list.
*/
- (CPTableColumn)tableColumn
{
return _tableColumn;
}
/*!
Returns the scroll view used by the list.
*/
- (CPScrollView)scrollView
{
return _scrollView;
}
/*!
Returns the panel in which the list appears.
*/
- (CPPanel)panel
{
return _panel;
}
#pragma mark Setting a Data Source
- (void)setDataSource:(_CPPopUpListDataSource)aDataSource
{
if (_dataSource === aDataSource)
return;
if (![_CPPopUpListDataSource protocolIsImplementedByObject:aDataSource])
{
CPLog.warn("Illegal %s data source (%s). Must implement the methods in _CPPopUpListDataSource.", [self className], [aDataSource description]);
}
else
_dataSource = aDataSource;
}
- (_CPPopUpListDataSource)dataSource
{
return _dataSource;
}
#pragma mark Manipulating the Selection
/*!
Select the next item in the list if there one. If there is currently no selected item,
the first item is selected. Returns YES if the selection changed.
*/
- (BOOL)selectNextItem
{
if (![_tableView isEnabled])
return NO;
var row = [_tableView selectedRow];
if (row < ([_dataSource numberOfItemsInList:self] - 1))
return [self selectRow:++row];
else
return NO;
}
/*!
Select the previous item in the list. If there is currently no selected item,
nothing happens. Returns YES if the selection changed.
*/
- (BOOL)selectPreviousItem
{
if (![_tableView isEnabled])
return NO;
var row = [_tableView selectedRow];
if (row > 0)
return [self selectRow:--row];
else
return NO;
}
/*!
Returns the selected object value. If no value is selected,
returns nil.
*/
- (id)selectedObjectValue
{
var row = [_tableView selectedRow];
return (row >= 0) ? [_dataSource list:self objectValueForItemAtIndex:row] : nil;
}
/*!
Returns the selected value as a single-line string. If no value is selected,
returns nil.
*/
- (CPString)selectedStringValue
{
var value = [self selectedObjectValue];
return value !== nil ? [_dataSource list:self stringValueForObjectValue:value] : nil;
}
/*!
Returns the last selected row in the list. If no row has been selected, returns -1.
*/
- (int)selectedRow
{
return [_tableView selectedRow];
}
/*!
Selects a row and scrolls it to be visible. Returns YES if the selection actually changed.
*/
- (BOOL)selectRow:(int)row
{
if (row === [_tableView selectedRow])
return NO;
var validRow = (row >= 0 && row < [self numberOfRowsInTableView:_tableView]),
indexes = validRow ? [CPIndexSet indexSetWithIndex:row] : [CPIndexSet indexSet];
[_tableView selectRowIndexes:indexes byExtendingSelection:NO];
if (validRow)
{
[_tableView scrollRowToVisible:row];
return YES;
}
else
return NO;
}
#pragma mark Manipulating the Displayed List
/*!
Scroll the list down one page.
*/
- (void)scrollPageDown
{
[_scrollView scrollPageDown:nil];
}
/*!
Scroll the list up one page.
*/
- (void)scrollPageUp
{
[_scrollView scrollPageUp:nil];
}
/*!
Scroll to the top of the list.
*/
- (void)scrollToTop
{
[_scrollView scrollToBeginningOfDocument:nil];
}
/*!
Scroll to the bottom of the list.
*/
- (void)scrollToBottom
{
[_scrollView scrollToEndOfDocument:nil];
}
- (void)scrollItemAtIndexToTop:(int)row
{
var rect = [_tableView rectOfRow:row];
[[_tableView superview] scrollToPoint:rect.origin];
}
/*!
Close the list if it is currently visible. If it is visible,
a CPComboBoxWillDismissNotification will be sent. If the
list is being closed after an item was clicked, the close
is delayed slightly so the user can briefly see the clicked row
get highlighted.
*/
- (void)close
{
if (![_panel isVisible])
return;
if ([self listWasClicked])
{
[self setListWasClicked:NO];
// Wait until we get through the run loop and delay a little
// so the user can briefly see the clicked row get highlighted.
if ([self itemWasClicked])
{
[self setItemWasClicked:NO];
[CPTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(closeListAfterItemClick) userInfo:nil repeats:NO];
return;
}
}
[[CPNotificationCenter defaultCenter] postNotificationName:_CPPopUpListWillDismissNotification object:self];
[_panel close];
[[CPNotificationCenter defaultCenter] postNotificationName:_CPPopUpListDidDismissNotification object:self];
}
/*!
Close the list after an item was clicked.
*/
- (void)closeListAfterItemClick
{
[self close];
[[CPNotificationCenter defaultCenter] postNotificationName:_CPPopUpListItemWasClickedNotification object:self];
}
#pragma mark Handling Events
/*!
Handles standard key equivalents for moving the selection
and selecting an item. This method should be called by
the -performKeyEquivalent method of the field that is
controlling the list.
*/
- (BOOL)performKeyEquivalent:(CPEvent)anEvent
{
var key = [anEvent charactersIgnoringModifiers];
switch (key)
{
case CPDownArrowFunctionKey:
if ([self isVisible])
{
[self selectNextItem];
return YES;
}
break;
case CPUpArrowFunctionKey:
if ([self isVisible])
{
[self selectPreviousItem];
return YES;
}
break;
case CPEscapeFunctionKey:
if ([self isVisible])
{
[self close];
return YES;
}
break;
case CPPageUpFunctionKey:
if ([self isVisible])
{
[self scrollPageUp];
return YES;
}
break;
case CPPageDownFunctionKey:
if ([self isVisible])
{
[self scrollPageDown];
return YES;
}
break;
case CPHomeFunctionKey:
if ([self isVisible])
{
[self scrollToTop];
return YES;
}
break;
case CPEndFunctionKey:
if ([self isVisible])
{
[self scrollToBottom];
return YES;
}
break;
case CPCarriageReturnCharacter:
if ([self isVisible])
{
[self closeListAfterItemClick];
return YES;
}
break;
}
return NO;
}
/*!
Returns whether an item in the list was clicked since it was opened.
If there are no items, \ref itemWasClicked will always return NO.
*/
- (BOOL)itemWasClicked
{
return _itemWasClicked && ([_dataSource numberOfItemsInList:self] > 0);
}
/*!
Sets whether an item in the list was clicked since it was opened.
If there are no items, \ref itemWasClicked will always return NO.
Subclasses will usually want to set this in the mouseDown:
of the control.
*/
- (void)setItemWasClicked:(BOOL)flag
{
_itemWasClicked = ([_dataSource numberOfItemsInList:self] > 0) && flag;
}
/*!
Returns whether any view in the list was clicked since it was opened.
If there are no items, \ref listWasClicked will always return NO.
*/
- (BOOL)listWasClicked
{
return _listWasClicked && ([_dataSource numberOfItemsInList:self] > 0);
}
/*!
Sets whether any view in the list was clicked since it was opened.
If there are no items, \ref listWasClicked will always return NO.
Subclasses will usually want to use a subclass of CPPanel and override
sendEvent: to set this flag when the event type is CPLeftMouseDown
or CPRightMouseDown. This is distinct from \ref itemWasClicked because,
for example, a scroller in the list may be clicked without clicking an
item in the list.
*/
- (void)setListWasClicked:(BOOL)flag
{
_listWasClicked = ([_dataSource numberOfItemsInList:self] > 0) && flag;
}
/*!
Returns whether a controlling view should resign. This should be called
from the controlling view's resignFirstResponder method.
*/
- (BOOL)controllingViewShouldResign
{
if ([self listWasClicked])
{
/*
If an item was not clicked (probably the scrollbar), clear the click flag so that future
clicks outside the list will allow it to close.
*/
if ([self listWasClicked] && ![self itemWasClicked])
[self setListWasClicked:NO];
return NO;
}
else
return YES;
}
#pragma mark Internal Helpers
/*! @ignore */
- (void)listWillPopUp
{
[[CPNotificationCenter defaultCenter] postNotificationName:_CPPopUpListWillPopUpNotification object:self];
}
/*!
Return a frame in platform window base coordinates such that the list, when displayed, will show at least ListMinimumItems
items completely on screen. Normally the list should be displayed below \c aRect, but if there is not room
for at least ListMinimumItems items, an attempt should be made to display that many
items above \c aRect. If the minimum cannot be displayed on top, whichever direction can display more items
is chosen.
@ignore
*/
- (CGRect)constrain:(CGRect)aFrame relativeToRect:(CGRect)aRect view:(CPView)aView offset:(int)offset
{
// Convert from the view's coordinate system to the coordinate system of the primary platform window
var baseOrigin = [aView convertPointToBase:aRect.origin],
windowOrigin = [[aView window] convertBaseToPlatformWindow:baseOrigin],
rowHeight = [self rowHeightForTableView:_tableView] + [_tableView intercellSpacing].height,
// Be sure to clip the number of displayed rows to what the field wants
numberOfRows = MIN([self numberOfRowsInTableView:_tableView], [_dataSource numberOfVisibleItemsInList:self]),
// Add 2 to height for border
frame = CGRectMake(windowOrigin.x, windowOrigin.y + CGRectGetHeight(aRect) + offset, MAX(_listWidth, CGRectGetWidth(aFrame)), (rowHeight * numberOfRows) + 2),
// Get the bottom coordinate of the frame and the platform window
bottomFrame = CGRectMakeCopy(frame),
bottom = CGRectGetMaxY(bottomFrame),
viewRect = [[CPPlatformWindow primaryPlatformWindow] visibleFrame],
visibleBottom = CGRectGetMaxY(viewRect),
bottomVisibleRows = numberOfRows;
// Make sure it will fit in the screen. If not, reduce the number of items till we reach the minimum.
while (bottom > visibleBottom && bottomVisibleRows >= ListMinimumItems)
{
bottom -= rowHeight;
bottomFrame.size.height -= rowHeight;
--bottomVisibleRows;
}
if (bottom >= visibleBottom || bottomVisibleRows < ListMinimumItems)
{
// The minimum number of items will not fit, try above
var topFrame = CGRectMakeCopy(frame);
topFrame.origin.y = windowOrigin.y - offset - CGRectGetHeight(topFrame);
var visibleTop = CGRectGetMinY(viewRect),
topVisibleRows = numberOfRows;
while (topFrame.origin.y <= visibleTop && topVisibleRows >= ListMinimumItems)
{
topFrame.origin.y += rowHeight;
topFrame.size.height -= rowHeight;
--topVisibleRows;
}
// If there is room on the top or it can display more than at the bottom, show it there
if ((topFrame.origin.y > visibleTop && topVisibleRows >= ListMinimumItems) || topVisibleRows > bottomVisibleRows)
frame = topFrame;
else
frame = bottomFrame;
}
else
frame = bottomFrame;
return frame;
}
- (void)tableViewClickAction:(id)sender
{
[self close];
}
@end
var _CPPopUpListDataSourceKey = @"_CPPopUpListDataSourceKey",
_CPPopUpListListWidthKey = @"_CPPopUpListListWidthKey",
_CPPopUpListListPanelKey = @"_CPPopUpListListPanelKey",
_CPPopUpListScrollViewKey = @"_CPPopUpListScrollViewKey",
_CPPopUpListTableViewKey = @"_CPPopUpListTableViewKey";
@implementation _CPPopUpList (CPCoding)
- (id)initWithCoder:(CPCoder)aCoder
{
self = [super initWithCoder:aCoder];
if (self)
{
_listWasClicked = NO;
_itemWasClicked = NO;
_dataSource = [aCoder decodeObjectForKey:_CPPopUpListDataSourceKey];
_listWidth = [aCoder decodeIntForKey:_CPPopUpListListWidthKey];
_panel = [aCoder decodeObjectForKey:_CPPopUpListListPanelKey];
_scrollView = [aCoder decodeObjectForKey:_CPPopUpListScrollViewKey];
_tableView = [aCoder decodeObjectForKey:_CPPopUpListTableViewKey];
_tableColumn = [_tableView tableColumnWithIdentifier:ListColumnIdentifier];
[_scrollView setDocumentView:_tableView];
}
return self;
}
- (void)encodeWithCoder:(CPCoder)aCoder
{
[super encodeWithCoder:aCoder];
[aCoder encodeObject:_dataSource forKey:_CPPopUpListDataSourceKey];
[aCoder encodeObject:_listWidth forKey:_CPPopUpListListWidthKey];
[aCoder encodeObject:_panel forKey:_CPPopUpListListPanelKey];
[aCoder encodeObject:_scrollView forKey:_CPPopUpListScrollViewKey];
[aCoder encodeObject:_tableView forKey:_CPPopUpListTableViewKey];
}
@end
@implementation _CPPopUpList (CPTableViewDataSource)
- (int)numberOfRowsInTableView:(id)aTableView
{
return MAX([_dataSource numberOfItemsInList:self], 1);
}
- (id)tableView:(id)aTableView objectValueForTableColumn:(CPTableColumn)aColumn row:(int)aRow
{
return [_dataSource list:self displayValueForObjectValue:[_dataSource list:self objectValueForItemAtIndex:aRow]];
}
@end
@implementation _CPPopUpTableView : CPTableView
{
BOOL _acceptFirstResponder;
}
- (id)initWithFrame:(CGRect)aFrame
{
if (self = [super initWithFrame:aFrame])
{
// We want the autocomplete to remain first responder until we are clicked.
_acceptFirstResponder = NO;
}
return self;
}
- (void)trackMouse:(CPEvent)anEvent
{
if (![self isEnabled])
return;
[[self delegate] setItemWasClicked:YES];
// CPTableView will not track the click if it is not first responder
_acceptFirstResponder = YES;
[[self window] makeFirstResponder:self];
[super trackMouse:anEvent];
}
- (void)stopTracking:(CGPoint)lastPoint at:(CGPoint)aPoint mouseIsUp:(BOOL)mouseIsUp
{
_acceptFirstResponder = NO;
[super stopTracking:lastPoint at:aPoint mouseIsUp:mouseIsUp];
}
- (BOOL)acceptsFirstResponder
{
return _acceptFirstResponder;
}
/*!
Return the column used for the list.
*/
- (CPTableColumn)listColumn
{
return _tableColumn;
}
@end
@implementation _CPPopUpPanel : CPPanel
- (void)sendEvent:(CPEvent)anEvent
{
var type = [anEvent type];
if (type === CPLeftMouseDown || type === CPRightMouseDown)
[[self delegate] setListWasClicked:YES];
return [super sendEvent:anEvent];
}
@end