mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-11-30 10:22:03 +00:00
First pass at CPComboBox. All functionality is there except for the drop-down button.
This commit is contained in:
1 parent
1918528710
commit
6eee5543f9
3 files changed
+1652
No files matched your search
@@ -0,0 +1,953 @@
|
||||
/*
|
||||
* CPComboBox.j
|
||||
* AppKit
|
||||
*
|
||||
* Created by Aparajita Fishman.
|
||||
* Copyright (c) 2011, Intalio, 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 <AppKit/CPTextField.j>
|
||||
@import <AppKit/_CPPopUpList.j>
|
||||
|
||||
|
||||
CPComboBoxSelectionDidChangeNotification = @"CPComboBoxSelectionDidChangeNotification";
|
||||
CPComboBoxSelectionIsChangingNotification = @"CPComboBoxSelectionIsChangingNotification";
|
||||
CPComboBoxWillDismissNotification = @"CPComboBoxWillDismissNotification";
|
||||
CPComboBoxWillPopUpNotification = @"CPComboBoxWillPopUpNotification";
|
||||
|
||||
var CPComboBoxTextSubview = @"text",
|
||||
CPComboBoxButtonSubview = @"button",
|
||||
CPComboBoxDefaultNumberOfVisibleItems = 5;
|
||||
|
||||
|
||||
@implementation CPComboBox : CPTextField
|
||||
{
|
||||
CPArray _items;
|
||||
_CPPopUpList _list;
|
||||
Class _listClass;
|
||||
CPComboBoxDataSource _dataSource;
|
||||
BOOL _usesDataSource;
|
||||
BOOL _completes;
|
||||
BOOL _canComplete;
|
||||
int _numberOfVisibleItems;
|
||||
BOOL _forceSelection;
|
||||
BOOL _hasVerticalScroller;
|
||||
CPString _selectedStringValue;
|
||||
}
|
||||
|
||||
- (id)initWithFrame:(CGRect)aFrame
|
||||
{
|
||||
self = [super initWithFrame:aFrame];
|
||||
|
||||
if (self)
|
||||
[self _init];
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)_init
|
||||
{
|
||||
_items = []
|
||||
_listClass = [_CPPopUpList class];
|
||||
_usesDataSource = NO;
|
||||
_completes = NO;
|
||||
_canComplete = NO;
|
||||
_numberOfVisibleItems = CPComboBoxDefaultNumberOfVisibleItems;
|
||||
_forceSelection = NO;
|
||||
_hasVerticalScroller = YES;
|
||||
_selectedStringValue = @"";
|
||||
|
||||
[self setTarget:self];
|
||||
[self setAction:@selector(textFieldAction:)];
|
||||
|
||||
[[CPNotificationCenter defaultCenter] addObserver:self selector:@selector(controlTextDidChange:) name:CPControlTextDidChangeNotification object:self];
|
||||
}
|
||||
|
||||
//#pragma mark Setting Display Attributes
|
||||
|
||||
- (BOOL)hasVerticalScroller
|
||||
{
|
||||
return _hasVerticalScroller;
|
||||
}
|
||||
|
||||
- (void)setHasVerticalScroller:(BOOL)flag
|
||||
{
|
||||
flag = !!flag;
|
||||
|
||||
if (_hasVerticalScroller === flag)
|
||||
return;
|
||||
|
||||
_hasVerticalScroller = flag;
|
||||
[[_list scrollView] setHasVerticalScroller:flag];
|
||||
}
|
||||
|
||||
- (CGSize)intercellSpacing
|
||||
{
|
||||
return [[_list tableView] intercellSpacing];
|
||||
}
|
||||
|
||||
- (void)setIntercellSpacing:(CGSize)aSize
|
||||
{
|
||||
[[_list tableView] setIntercellSpacing:aSize];
|
||||
}
|
||||
|
||||
- (BOOL)isButtonBordered
|
||||
{
|
||||
return YES;
|
||||
//return [[self ephemeralSubviewNamed:CPComboBoxButtonSubview] isBordered];
|
||||
}
|
||||
|
||||
- (void)setButtonBordered:(BOOL)flag
|
||||
{
|
||||
//[[self ephemeralSubviewNamed:CPComboBoxButtonSubview] setBordered:flag];
|
||||
}
|
||||
|
||||
- (float)itemHeight
|
||||
{
|
||||
return [[_list tableView] rowHeight];
|
||||
}
|
||||
|
||||
- (void)setItemHeight:(float)itemHeight
|
||||
{
|
||||
[[_list tableView] setRowHeight:itemHeight];
|
||||
}
|
||||
|
||||
- (int)numberOfVisibleItems
|
||||
{
|
||||
return _numberOfVisibleItems;
|
||||
}
|
||||
|
||||
- (void)setNumberOfVisibleItems:(int)visibleItems
|
||||
{
|
||||
// There should always be at least 1 visible item!
|
||||
_numberOfVisibleItems = MAX(visibleItems, 1);
|
||||
}
|
||||
|
||||
//#pragma mark Setting a Delegate
|
||||
|
||||
- (id < CPComboBoxDelegate >)delegate
|
||||
{
|
||||
return [super delegate];
|
||||
}
|
||||
|
||||
/*!
|
||||
Sets the CPComboBox delegate. Note that although the Cocoa
|
||||
docs say that the delegate must conform to the NSComboBoxDelegate
|
||||
protocol, in actual fact it doesn't. Also note that the same
|
||||
delegate may conform to the NSTextFieldDelegate protocol.
|
||||
*/
|
||||
- (void)setDelegate:(id < CPComboBoxDelegate >)aDelegate
|
||||
{
|
||||
var delegate = [self delegate];
|
||||
|
||||
if (aDelegate === delegate)
|
||||
return;
|
||||
|
||||
var defaultCenter = [CPNotificationCenter defaultCenter];
|
||||
|
||||
if (delegate)
|
||||
{
|
||||
[defaultCenter removeObserver:delegate name:CPComboBoxSelectionIsChangingNotification object:self];
|
||||
[defaultCenter removeObserver:delegate name:CPComboBoxSelectionDidChangeNotification object:self];
|
||||
[defaultCenter removeObserver:delegate name:CPComboBoxWillDismissNotification object:self];
|
||||
[defaultCenter removeObserver:delegate name:CPComboBoxWillPopUpNotification object:self];
|
||||
}
|
||||
|
||||
if (aDelegate)
|
||||
{
|
||||
if ([aDelegate respondsToSelector:@selector(comboBoxSelectionIsChanging:)])
|
||||
[defaultCenter addObserver:delegate
|
||||
selector:@selector(comboBoxSelectionIsChanging:)
|
||||
name:CPComboBoxSelectionIsChangingNotification
|
||||
object:self];
|
||||
|
||||
if ([aDelegate respondsToSelector:@selector(comboBoxSelectionDidChange:)])
|
||||
[defaultCenter addObserver:delegate
|
||||
selector:@selector(comboBoxSelectionDidChange:)
|
||||
name:CPComboBoxSelectionDidChangeNotification
|
||||
object:self];
|
||||
|
||||
if ([aDelegate respondsToSelector:@selector(comboBoxWillPopUp:)])
|
||||
[defaultCenter addObserver:delegate
|
||||
selector:@selector(comboBoxWillPopUp:)
|
||||
name:CPComboBoxWillPopUpNotification
|
||||
object:self];
|
||||
|
||||
if ([aDelegate respondsToSelector:@selector(comboBoxWillDismiss:)])
|
||||
[defaultCenter addObserver:delegate
|
||||
selector:@selector(comboBoxWillDissmis:)
|
||||
name:CPComboBoxWillDismissNotification
|
||||
object:self];
|
||||
}
|
||||
|
||||
[super setDelegate:aDelegate];
|
||||
}
|
||||
|
||||
//#pragma mark Setting a Data Source
|
||||
|
||||
- (id < CPComboBoxDataSource >)dataSource
|
||||
{
|
||||
if (_usesDataSource === NO)
|
||||
[self dataSourceWarningForMethod:_cmd condition:NO];
|
||||
|
||||
return _dataSource;
|
||||
}
|
||||
|
||||
- (void)setDataSource:(id < CPComboBoxDataSource >)aSource
|
||||
{
|
||||
if (_usesDataSource === NO)
|
||||
[self dataSourceWarningForMethod:_cmd condition:NO];
|
||||
else if (_dataSource !== aSource)
|
||||
{
|
||||
if (![aSource respondsToSelector:@selector(numberOfItemsInComboBox:)] ||
|
||||
![aSource respondsToSelector:@selector(comboBox:objectValueForItemAtIndex:)])
|
||||
{
|
||||
CPLog.warn("Illegal %s data source (%s). Must implement numberOfItemsInComboBox: and comboBox:objectValueForItemAtIndex:", [self className], [aSource description]);
|
||||
}
|
||||
else
|
||||
_dataSource = aSource;
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)usesDataSource
|
||||
{
|
||||
return _usesDataSource;
|
||||
}
|
||||
|
||||
- (void)setUsesDataSource:(BOOL)flag
|
||||
{
|
||||
flag = !!flag;
|
||||
|
||||
if (_usesDataSource === flag)
|
||||
return;
|
||||
|
||||
_usesDataSource = flag;
|
||||
|
||||
// Cocoa empties the internal item list if usesDataSource is YES
|
||||
if (_usesDataSource)
|
||||
[_items removeAllObjects];
|
||||
|
||||
[self reloadData];
|
||||
}
|
||||
|
||||
//#pragma mark Working with an Internal List
|
||||
|
||||
- (void)addItemsWithObjectValues:(CPArray)objects
|
||||
{
|
||||
[_items addObjectsFromArray:objects];
|
||||
|
||||
[self reloadDataSourceForSelector:_cmd];
|
||||
}
|
||||
|
||||
- (void)addItemWithObjectValue:(id)anObject
|
||||
{
|
||||
[_items addObject:anObject];
|
||||
|
||||
[self reloadDataSourceForSelector:_cmd];
|
||||
}
|
||||
|
||||
- (void)insertItemWithObjectValue:(id)anObject atIndex:(int)anIndex
|
||||
{
|
||||
// Issue the warning first, because removeObjectAtIndex may raise
|
||||
if (_usesDataSource)
|
||||
[self dataSourceWarningForMethod:_cmd condition:YES];
|
||||
|
||||
[_items insertObject:anObject atIndex:anIndex];
|
||||
[self reloadData];
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns the internal array of items. Note that this does a deep copy of the array,
|
||||
effectively returning an immutable array to be consistent with Cocoa.
|
||||
|
||||
If usesDataSource is YES, a warning is logged and an empty array is returned.
|
||||
*/
|
||||
- (CPArray)objectValues
|
||||
{
|
||||
if (_usesDataSource)
|
||||
[self dataSourceWarningForMethod:_cmd condition:YES];
|
||||
|
||||
return [[CPArray alloc] initWithArray:_items copyItems:YES];
|
||||
}
|
||||
|
||||
- (void)removeAllItems
|
||||
{
|
||||
[_items removeAllObjects];
|
||||
|
||||
[self reloadDataSourceForSelector:_cmd];
|
||||
}
|
||||
|
||||
- (void)removeItemAtIndex:(int)index
|
||||
{
|
||||
// Issue the warning first, because removeObjectAtIndex may raise
|
||||
if (_usesDataSource)
|
||||
[self dataSourceWarningForMethod:_cmd condition:YES];
|
||||
|
||||
[_items removeObjectAtIndex:index];
|
||||
[self reloadData];
|
||||
}
|
||||
|
||||
- (void)removeItemWithObjectValue:(id)anObject
|
||||
{
|
||||
[_items removeObject:anObject];
|
||||
|
||||
[self reloadDataSourceForSelector:_cmd];
|
||||
}
|
||||
|
||||
- (int)numberOfItems
|
||||
{
|
||||
if (_usesDataSource)
|
||||
return [_dataSource numberOfItemsInComboBox:self];
|
||||
else
|
||||
return _items.length;
|
||||
}
|
||||
|
||||
//#pragma mark Manipulating the Displayed List
|
||||
|
||||
/*!
|
||||
Returns the helper class to be used when creating the pop up list.
|
||||
*/
|
||||
- (Class)listClass
|
||||
{
|
||||
return _listClass;
|
||||
}
|
||||
|
||||
/*!
|
||||
Sets the helper class to be used when creating the pop up list.
|
||||
By default this is _CPPopUpList. If you are using a subclass
|
||||
of _CPPopUpList, call this method with your subclass.
|
||||
*/
|
||||
- (void)setListClass:(Class)aClass
|
||||
{
|
||||
_listClass = aClass;
|
||||
}
|
||||
|
||||
- (int)indexOfItemWithObjectValue:(id)anObject
|
||||
{
|
||||
if (_usesDataSource)
|
||||
[self dataSourceWarningForMethod:_cmd condition:YES];
|
||||
|
||||
return [_items indexOfObject:anObject];
|
||||
}
|
||||
|
||||
- (id)itemObjectValueAtIndex:(int)index
|
||||
{
|
||||
if (_usesDataSource)
|
||||
[self dataSourceWarningForMethod:_cmd condition:YES];
|
||||
|
||||
return [_items objectAtIndex:index];
|
||||
}
|
||||
|
||||
- (void)noteNumberOfItemsChanged
|
||||
{
|
||||
[[_list tableView] noteNumberOfRowsChanged];
|
||||
}
|
||||
|
||||
- (void)scrollItemAtIndexToTop:(int)index
|
||||
{
|
||||
[_list scrollItemAtIndexToTop:index];
|
||||
}
|
||||
|
||||
- (void)scrollItemAtIndexToVisible:(int)index
|
||||
{
|
||||
[[_list tableView] scrollRowToVisible:index];
|
||||
}
|
||||
|
||||
- (void)reloadData
|
||||
{
|
||||
[[_list tableView] reloadData];
|
||||
}
|
||||
|
||||
/*! @ignore */
|
||||
- (void)popUpList
|
||||
{
|
||||
if (!_list)
|
||||
[self makeList];
|
||||
|
||||
[self selectMatchingItem];
|
||||
[_list popUpRelativeTo:[self borderFrame]];
|
||||
}
|
||||
|
||||
/*! @ignore */
|
||||
- (_CPPopUpList)makeList
|
||||
{
|
||||
var defaultCenter = [CPNotificationCenter defaultCenter];
|
||||
|
||||
if (_list)
|
||||
{
|
||||
[defaultCenter removeObserver:self name:_CPPopUpListWillPopUpNotification object:_list];
|
||||
[defaultCenter removeObserver:self name:_CPPopUpListWillDismissNotification object:_list];
|
||||
[defaultCenter removeObserver:self name:_CPPopUpListDidDismissNotification object:_list];
|
||||
[defaultCenter removeObserver:self name:_CPPopUpListItemWasClickedNotification object:_list];
|
||||
}
|
||||
|
||||
_list = [[_listClass alloc] initWithDelegate:self];
|
||||
|
||||
[defaultCenter addObserver:self
|
||||
selector:@selector(listWillPopUp:)
|
||||
name:_CPPopUpListWillPopUpNotification
|
||||
object:_list];
|
||||
|
||||
[defaultCenter addObserver:self
|
||||
selector:@selector(listWillDismiss:)
|
||||
name:_CPPopUpListWillDismissNotification
|
||||
object:_list];
|
||||
|
||||
[defaultCenter addObserver:self
|
||||
selector:@selector(listDidDismiss:)
|
||||
name:_CPPopUpListDidDismissNotification
|
||||
object:_list];
|
||||
|
||||
[defaultCenter addObserver:self
|
||||
selector:@selector(itemWasClicked:)
|
||||
name:_CPPopUpListItemWasClickedNotification
|
||||
object:_list];
|
||||
|
||||
[[_list scrollView] setHasVerticalScroller:_hasVerticalScroller];
|
||||
|
||||
var tableView = [_list tableView];
|
||||
|
||||
[defaultCenter addObserver:self
|
||||
selector:@selector(comboBoxSelectionIsChanging:)
|
||||
name:CPTableViewSelectionIsChangingNotification
|
||||
object:tableView];
|
||||
|
||||
[defaultCenter addObserver:self
|
||||
selector:@selector(comboBoxSelectionDidChange:)
|
||||
name:CPTableViewSelectionDidChangeNotification
|
||||
object:tableView];
|
||||
}
|
||||
|
||||
/*! @ignore */
|
||||
- (BOOL)listIsVisible
|
||||
{
|
||||
return _list ? [_list isVisible] : NO;
|
||||
}
|
||||
|
||||
/*! @ignore */
|
||||
- (void)reloadDataSourceForSelector:(SEL)cmd
|
||||
{
|
||||
if (_usesDataSource)
|
||||
[self dataSourceWarningForMethod:cmd condition:YES]
|
||||
else
|
||||
[self reloadData];
|
||||
}
|
||||
|
||||
/*!
|
||||
If the list is non-empty, sets the value of the field from the currently selected value of the list
|
||||
and returns YES. IF the list is empty, immediately returns NO.
|
||||
*/
|
||||
- (BOOL)takeStringValueFromList
|
||||
{
|
||||
if ([_dataSource numberOfItemsInComboBox:self] === 0)
|
||||
return NO;
|
||||
|
||||
_selectedStringValue = [_list selectedStringValue] || @"";
|
||||
|
||||
[self setStringValue:_selectedStringValue];
|
||||
return YES;
|
||||
}
|
||||
|
||||
/*!
|
||||
The field receives this notification when the list is about to be pop up.
|
||||
*/
|
||||
- (void)listWillPopUp:(CPNotification)aNotification
|
||||
{
|
||||
[self comboBoxWillPopUp];
|
||||
}
|
||||
|
||||
/*!
|
||||
The field receives this notification when the list is about to be dismissed.
|
||||
*/
|
||||
- (void)listWillDismiss:(CPNotification)aNotification
|
||||
{
|
||||
[self comboBoxWillDismiss];
|
||||
}
|
||||
|
||||
/*!
|
||||
The field receives this notification when the list is closed.
|
||||
*/
|
||||
- (void)listDidDismiss:(CPNotification)aNotification
|
||||
{
|
||||
[[self window] makeFirstResponder:self];
|
||||
}
|
||||
|
||||
/*!
|
||||
The field receives this notification when an item in the list is clicked.
|
||||
*/
|
||||
- (void)itemWasClicked:(CPNotification)aNotification
|
||||
{
|
||||
[self takeStringValueFromList];
|
||||
}
|
||||
|
||||
//#pragma mark Manipulating the Selection
|
||||
|
||||
- (void)deselectItemAtIndex:(int)index
|
||||
{
|
||||
var table = [_list tableView],
|
||||
row = [table selectedRow];
|
||||
|
||||
if (row !== index)
|
||||
return;
|
||||
|
||||
[table deselectRow:index];
|
||||
}
|
||||
|
||||
- (int)indexOfSelectedItem
|
||||
{
|
||||
return [[_list tableView] selectedRow];
|
||||
}
|
||||
|
||||
- (id)objectValueOfSelectedItem
|
||||
{
|
||||
var row = [[_list tableView] selectedRow];
|
||||
|
||||
if (row >= 0)
|
||||
{
|
||||
if (_usesDataSource)
|
||||
[self dataSourceWarningForMethod:_cmd condition:YES];
|
||||
|
||||
return _items[row];
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (void)selectItemAtIndex:(int)index
|
||||
{
|
||||
var table = [_list tableView],
|
||||
row = [table selectedRow];
|
||||
|
||||
if (row === index)
|
||||
return;
|
||||
|
||||
[table selectRowIndexes:[CPIndexSet indexSetWithIndex:index] byExtendingSelection:NO];
|
||||
}
|
||||
|
||||
- (void)selectItemWithObjectValue:(id)anObject
|
||||
{
|
||||
var index = [self indexOfItemWithObjectValue:anObject];
|
||||
|
||||
if (index !== CPNotFound)
|
||||
[self selectItemAtIndex:index];
|
||||
}
|
||||
|
||||
//#pragma mark Completing the Text Field
|
||||
|
||||
- (BOOL)completes
|
||||
{
|
||||
return _completes;
|
||||
}
|
||||
|
||||
- (void)setCompletes:(BOOL)flag
|
||||
{
|
||||
_completes = !!flag;
|
||||
}
|
||||
|
||||
- (CPString)completedString:(CPString)substring
|
||||
{
|
||||
if (_usesDataSource)
|
||||
return [self comboBoxCompletedString:substring];
|
||||
else
|
||||
{
|
||||
var index = [_items indexOfObjectPassingTest:CPComboBoxCompletionTest context:substring];
|
||||
|
||||
return index !== CPNotFound ? _items[index] : nil;
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns whether the combo box forces the user to enter or select
|
||||
an item that is in the item list.
|
||||
*/
|
||||
- (BOOL)forceSelection
|
||||
{
|
||||
return _forceSelection;
|
||||
}
|
||||
|
||||
/*!
|
||||
Sets whether the combo box forces the user to enter or select
|
||||
an item that is in the item list. If \c flag is \c YES and the user enters a value
|
||||
that is not in the list, when the field loses focus it will revert
|
||||
to the previous value. If \c flag is \c NO, the user can enter any value they wish.
|
||||
*/
|
||||
- (void)setForceSelection:(BOOL)flag
|
||||
{
|
||||
_forceSelection = !!flag;
|
||||
}
|
||||
|
||||
//#pragma mark CPTextField Delegate Methods and Overrides
|
||||
|
||||
- (void)keyDown:(CPEvent)anEvent
|
||||
{
|
||||
// Only if characters are added at the end of the value can completion occur
|
||||
_canComplete = NO;
|
||||
|
||||
if (_completes)
|
||||
{
|
||||
if (![anEvent _couldBeKeyEquivalent] && [anEvent characters].charAt(0) !== CPDeleteCharacter)
|
||||
{
|
||||
var value = [self _inputElement].value,
|
||||
selectedRange = [self selectedRange];
|
||||
|
||||
_canComplete = CPMaxRange(selectedRange) === value.length;
|
||||
}
|
||||
}
|
||||
|
||||
[super keyDown:anEvent];
|
||||
}
|
||||
|
||||
- (void)paste:(id)sender
|
||||
{
|
||||
if (_completes)
|
||||
{
|
||||
// Completion can occur only if pasting at the end of the value
|
||||
var value = [self _inputElement].value,
|
||||
selectedRange = [self selectedRange];
|
||||
|
||||
_canComplete = CPMaxRange(selectedRange) === value.length;
|
||||
}
|
||||
else
|
||||
_canComplete = NO;
|
||||
|
||||
[super paste:sender];
|
||||
}
|
||||
|
||||
- (void)controlTextDidChange:(CPNotification)anNotification
|
||||
{
|
||||
/*
|
||||
Completion is attempted iff:
|
||||
- _completes is YES
|
||||
- Characters were added at the end of the value
|
||||
*/
|
||||
var uncompletedString = [self stringValue],
|
||||
newString = uncompletedString;
|
||||
|
||||
if (_completes && _canComplete)
|
||||
{
|
||||
newString = [self completedString:uncompletedString];
|
||||
|
||||
if (newString && newString.length > uncompletedString.length)
|
||||
{
|
||||
[self setStringValue:newString];
|
||||
[self setSelectedRange:CPMakeRange(uncompletedString.length, newString.length - uncompletedString.length)];
|
||||
}
|
||||
}
|
||||
|
||||
[self selectMatchingItem];
|
||||
_canComplete = NO;
|
||||
}
|
||||
|
||||
- (BOOL)performKeyEquivalent:(CPEvent)anEvent
|
||||
{
|
||||
if ([[self window] firstResponder] === self)
|
||||
{
|
||||
var key = [anEvent charactersIgnoringModifiers];
|
||||
|
||||
switch (key)
|
||||
{
|
||||
case CPDownArrowFunctionKey:
|
||||
if ([self listIsVisible])
|
||||
[_list selectNextItem];
|
||||
else
|
||||
[self popUpList];
|
||||
|
||||
return YES;
|
||||
|
||||
case CPUpArrowFunctionKey:
|
||||
if ([self listIsVisible])
|
||||
{
|
||||
[_list selectPreviousItem];
|
||||
return YES;
|
||||
}
|
||||
break;
|
||||
|
||||
case CPEscapeFunctionKey:
|
||||
if ([self listIsVisible])
|
||||
{
|
||||
if (_forceSelection)
|
||||
[self setStringValue:_selectedStringValue];
|
||||
|
||||
[_list close];
|
||||
return YES;
|
||||
}
|
||||
break;
|
||||
|
||||
case CPPageUpFunctionKey:
|
||||
if ([self listIsVisible])
|
||||
{
|
||||
[_list scrollPageUp];
|
||||
return YES;
|
||||
}
|
||||
break;
|
||||
|
||||
case CPPageDownFunctionKey:
|
||||
if ([self listIsVisible])
|
||||
{
|
||||
[_list scrollPageDown];
|
||||
return YES;
|
||||
}
|
||||
break;
|
||||
|
||||
case CPHomeFunctionKey:
|
||||
if ([self listIsVisible])
|
||||
{
|
||||
[_list scrollToTop];
|
||||
return YES;
|
||||
}
|
||||
break;
|
||||
|
||||
case CPEndFunctionKey:
|
||||
if ([self listIsVisible])
|
||||
{
|
||||
[_list scrollToBottom];
|
||||
return YES;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return [super performKeyEquivalent:anEvent];
|
||||
}
|
||||
|
||||
- (BOOL)resignFirstResponder
|
||||
{
|
||||
/*
|
||||
If the list is clicked, we lose focus. In that case the list will refuse first responder,
|
||||
and we refuse to resign. But we still have to manually restore the focus to the input element.
|
||||
*/
|
||||
if ([_list 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. It isn't so great doing that here, but
|
||||
the sequence of events is such that it has to be done here.
|
||||
*/
|
||||
if (![_list itemWasClicked])
|
||||
[_list setListWasClicked:NO];
|
||||
|
||||
//#if PLATFORM(DOM)
|
||||
[self _inputElement].focus();
|
||||
//#endif
|
||||
|
||||
return NO;
|
||||
}
|
||||
|
||||
// The list was not clicked, we need to close it now
|
||||
[_list close];
|
||||
|
||||
// If the field is empty, allow it to remain empty.
|
||||
// Otherwise restore the most recently selected value if forcing selection.
|
||||
var value = [self stringValue];
|
||||
|
||||
if (value)
|
||||
{
|
||||
if (_forceSelection && ![value isEqual:_selectedStringValue])
|
||||
[self setStringValue:_selectedStringValue];
|
||||
}
|
||||
else
|
||||
_selectedStringValue = @"";
|
||||
|
||||
return [super resignFirstResponder];
|
||||
}
|
||||
|
||||
/*!
|
||||
We override this so that we can know when the field is about to lose focus
|
||||
because the user pressed tab.
|
||||
*/
|
||||
- (CPView)nextValidKeyView
|
||||
{
|
||||
if ([self listIsVisible])
|
||||
[self takeStringValueFromList];
|
||||
|
||||
return [super nextValidKeyView];
|
||||
}
|
||||
|
||||
/*!
|
||||
We override this so that we can know when the field is about to lose focus
|
||||
because the user pressed shift tab.
|
||||
*/
|
||||
- (CPView)previousValidKeyView
|
||||
{
|
||||
if ([self listIsVisible])
|
||||
[self takeStringValueFromList];
|
||||
|
||||
return [super previousValidKeyView];
|
||||
}
|
||||
|
||||
//#pragma mark Internal Helpers
|
||||
|
||||
- (void)dataSourceWarningForMethod:(SEL)cmd condition:(CPString)flag
|
||||
{
|
||||
CPLog.warn("-[%s %s] should not be called when usesDataSource is set to %s", [self className], cmd, flag ? "YES" : "NO");
|
||||
}
|
||||
|
||||
- (id)objectValueForItemAtIndex:(int)index
|
||||
{
|
||||
return [_dataSource comboBox:self objectValueForItemAtIndex:index];
|
||||
}
|
||||
|
||||
/*!
|
||||
Select the item that matches the current value of the combobox.
|
||||
*/
|
||||
- (void)selectMatchingItem
|
||||
{
|
||||
var index = CPNotFound,
|
||||
stringValue = [self stringValue];
|
||||
|
||||
if (_usesDataSource)
|
||||
{
|
||||
if (_dataSource && [_dataSource respondsToSelector:@selector(comboBox:indexOfItemWithStringValue:)])
|
||||
index = [_dataSource comboBox:self indexOfItemWithStringValue:stringValue]
|
||||
}
|
||||
else
|
||||
index = [self indexOfItemWithObjectValue:stringValue];
|
||||
|
||||
[_list selectRow:index];
|
||||
|
||||
// selectRow scrolls the row to visible, if a row is selected scroll it to the top
|
||||
if (index !== CPNotFound)
|
||||
{
|
||||
[_list scrollRowToTop:index];
|
||||
_selectedStringValue = stringValue;
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)textFieldAction:(id)sender
|
||||
{
|
||||
if ([self listIsVisible])
|
||||
{
|
||||
[self takeStringValueFromList];
|
||||
[_list close];
|
||||
}
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
/*!
|
||||
@ignore
|
||||
|
||||
Calculate the frame in base coordinates that will nestle just below the visible border of the text field.
|
||||
*/
|
||||
- (CGRect)borderFrame
|
||||
{
|
||||
// Calculate the focus inset by subtracting the focused bezel inset
|
||||
// from the unfocused bezel inset
|
||||
var unfocusedInset = [self valueForThemeAttribute:@"bezel-inset" inState:CPThemeStateBezeled],
|
||||
focusedInset = [self valueForThemeAttribute:@"bezel-inset" inState:CPThemeStateBezeled | CPThemeStateEditing],
|
||||
focusInset = CGInsetDifference(unfocusedInset, focusedInset),
|
||||
bounds = [self convertRectToBase:[self bounds]];
|
||||
|
||||
bounds.origin.x += focusInset.left;
|
||||
bounds.origin.y += focusInset.top;
|
||||
bounds.size.width -= focusInset.left + focusInset.right;
|
||||
bounds.size.height -= focusInset.top + focusInset.bottom;
|
||||
|
||||
return bounds;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation CPComboBox (CPComboBoxDelegate)
|
||||
|
||||
- (void)comboBoxSelectionIsChanging:(CPNotification)aNotification
|
||||
{
|
||||
[[CPNotificationCenter defaultCenter] postNotificationName:CPComboBoxSelectionIsChangingNotification object:self];
|
||||
}
|
||||
|
||||
- (void)comboBoxSelectionDidChange:(CPNotification)aNotification
|
||||
{
|
||||
[[CPNotificationCenter defaultCenter] postNotificationName:CPComboBoxSelectionDidChangeNotification object:self];
|
||||
}
|
||||
|
||||
- (void)comboBoxWillPopUp
|
||||
{
|
||||
[[CPNotificationCenter defaultCenter] postNotificationName:CPComboBoxWillPopUpNotification object:self];
|
||||
}
|
||||
|
||||
- (void)comboBoxWillDismiss
|
||||
{
|
||||
[[CPNotificationCenter defaultCenter] postNotificationName:CPComboBoxWillDismissNotification object:self];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation CPComboBox (CPComboBoxDataSource)
|
||||
|
||||
- (CPString)comboBoxCompletedString:(CPString)uncompletedString
|
||||
{
|
||||
if ([_dataSource respondsToSelector:@selector(comboBox:completedString:)])
|
||||
return [_dataSource comboBox:self completedString:uncompletedString];
|
||||
else
|
||||
return nil;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
var CPComboBoxItemsKey = @"CPComboBoxItemsKey",
|
||||
CPComboBoxListKey = @"CPComboBoxListKey",
|
||||
CPComboBoxDelegateKey = @"CPComboBoxDelegateKey",
|
||||
CPComboBoxDataSourceKey = @"CPComboBoxDataSourceKey",
|
||||
CPComboBoxUsesDataSourceKey = @"CPComboBoxUsesDataSourceKey",
|
||||
CPComboBoxCompletesKey = @"CPComboBoxCompletesKey",
|
||||
CPComboBoxNumberOfVisibleItemsKey = @"CPComboBoxNumberOfVisibleItemsKey",
|
||||
CPComboBoxHasVerticalScrollerKey = @"CPComboBoxHasVerticalScrollerKey";
|
||||
|
||||
@implementation CPComboBox (CPCoding)
|
||||
|
||||
- (id)initWithCoder:(CPCoder)aCoder
|
||||
{
|
||||
self = [super initWithCoder:aCoder];
|
||||
|
||||
if (self)
|
||||
{
|
||||
[self _init];
|
||||
|
||||
_items = [aCoder decodeObjectForKey:CPComboBoxItemsKey];
|
||||
_list = [aCoder decodeObjectForKey:CPComboBoxListKey];
|
||||
_delegate = [aCoder decodeObjectForKey:CPComboBoxDelegateKey];
|
||||
_dataSource = [aCoder decodeObjectForKey:CPComboBoxDataSourceKey];
|
||||
_usesDataSource = [aCoder decodeBoolForKey:CPComboBoxUsesDataSourceKey];
|
||||
_completes = [aCoder decodeBoolForKey:CPComboBoxCompletesKey];
|
||||
_numberOfVisibleItems = [aCoder decodeIntForKey:CPComboBoxNumberOfVisibleItemsKey];
|
||||
_hasVerticalScroller = [aCoder decodeBoolForKey:CPComboBoxHasVerticalScrollerKey];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)encodeWithCoder:(CPCoder)aCoder
|
||||
{
|
||||
[super encodeWithCoder:aCoder];
|
||||
|
||||
[aCoder encodeObject:_items forKey:CPComboBoxItemsKey];
|
||||
[aCoder encodeObject:_list forKey:CPComboBoxListKey];
|
||||
[aCoder encodeObject:_delegate forKey:CPComboBoxDelegateKey];
|
||||
[aCoder encodeObject:_dataSource forKey:CPComboBoxDataSourceKey];
|
||||
[aCoder encodeBool:_usesDataSource forKey:CPComboBoxUsesDataSourceKey];
|
||||
[aCoder encodeBool:_completes forKey:CPComboBoxCompletesKey];
|
||||
[aCoder encodeInt:_numberOfVisibleItems forKey:CPComboBoxNumberOfVisibleItemsKey];
|
||||
[aCoder encodeBool:_hasVerticalScroller forKey:CPComboBoxHasVerticalScrollerKey];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
var CPComboBoxCompletionTest = function(object, index, context)
|
||||
{
|
||||
return object.toString().indexOf(context) === 0;
|
||||
};
|
||||
@@ -267,6 +267,32 @@ function CGPointFromEvent(anEvent)
|
||||
return _CGPointMake(anEvent.clientX, anEvent.clientY);
|
||||
}
|
||||
|
||||
/*!
|
||||
Combines two insets by adding their individual elements and returns the result.
|
||||
|
||||
@group CGInset
|
||||
*/
|
||||
function CGInsetUnion(lhsInset, rhsInset)
|
||||
{
|
||||
return _CGInsetMake(lhsInset.top + rhsInset.top,
|
||||
lhsInset.right + rhsInset.right,
|
||||
lhsInset.bottom + rhsInset.bottom,
|
||||
lhsInset.left + rhsInset.left);
|
||||
}
|
||||
|
||||
/*!
|
||||
Subtract one inset from another by subtracting their individual elements and returns the result.
|
||||
|
||||
@group CGInset
|
||||
*/
|
||||
function CGInsetDifference(lhsInset, rhsInset)
|
||||
{
|
||||
return _CGInsetMake(lhsInset.top - rhsInset.top,
|
||||
lhsInset.right - rhsInset.right,
|
||||
lhsInset.bottom - rhsInset.bottom,
|
||||
lhsInset.left - rhsInset.left);
|
||||
}
|
||||
|
||||
function CGInsetFromString(aString)
|
||||
{
|
||||
var numbers = aString.substr(1, aString.length - 2).split(',');
|
||||
|
||||
@@ -0,0 +1,673 @@
|
||||
/*
|
||||
* _CPPopUpList.j
|
||||
* AppKit
|
||||
*
|
||||
* Created by Aparajita Fishman.
|
||||
* Copyright (c) 2011, Intalio, 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 <AppKit/CPTableView.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 when the selection of the list changes. \c object is the _CPPopUpList.
|
||||
*/
|
||||
_CPPopUpListDidDismissNotification = @"_CPPopUpListDidDismissNotification";
|
||||
|
||||
/*!
|
||||
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";
|
||||
|
||||
/*!
|
||||
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.
|
||||
For tables that display single-line rows of scalar data, no subclassing should be necessary.
|
||||
|
||||
The delegate of this class MUST implement the following methods:
|
||||
|
||||
numberOfItems The number of items the list should display
|
||||
numberOfVisibleItems The maximum number of items to display at once
|
||||
objectValueForItemAtIndex: Retrieves an object value for an item in the range 0..(numberOfItems - 1)
|
||||
|
||||
Objects of this class send the following notifications:
|
||||
|
||||
_CPPopUpListWillPopUpNotification
|
||||
_CPPopUpListWillDismissNotification
|
||||
_CPPopUpListDidDismissNotification
|
||||
_CPPopUpListItemWasClickedNotification
|
||||
*/
|
||||
@implementation _CPPopUpList : CPObject
|
||||
{
|
||||
_CPPopUpListDelegate _delegate;
|
||||
BOOL _itemWasClicked;
|
||||
BOOL _listWasClicked;
|
||||
int _listWidth;
|
||||
_CPPopUpPanel _panel @accessors(readonly, property=panel);
|
||||
CPScrollView _scrollView @accessors(readonly, property=scrollView);
|
||||
_CPPopUpTableView _tableView @accessors(readonly, property=tableView);
|
||||
CPTableColumn _tableColumn;
|
||||
}
|
||||
|
||||
/*!
|
||||
Creates a pop up list of choices which will display relative to the given rect in base coordinates.
|
||||
|
||||
@param aDelegate The object that usually is controlling this list.
|
||||
@param baseRect A rect in base coordinates relative to which the list should be displayed
|
||||
*/
|
||||
- (id)initWithDelegate:(_CPPopUpListDelegate)aDelegate
|
||||
{
|
||||
self = [super init];
|
||||
|
||||
if (self)
|
||||
{
|
||||
_delegate = aDelegate;
|
||||
_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 ([_delegate numberOfItems] > 0)
|
||||
[_tableView selectRowIndexes:[CPIndexSet indexSetWithIndex:0] byExtendingSelection:NO];
|
||||
else
|
||||
[_tableView setEnabled:NO];
|
||||
|
||||
[_scrollView scrollToBeginningOfDocument:nil];
|
||||
|
||||
[self finalizeTableView:_tableView];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
/*!
|
||||
Create and configure the panel containing the table view.
|
||||
Subclasses should override this (and usually call super first)
|
||||
if they want to customize the list panel.
|
||||
*/
|
||||
- (CPPanel)makeListPanelWithFrame:(CGRect)aFrame
|
||||
{
|
||||
var panel = [[_CPPopUpPanel alloc] initWithContentRect:aFrame styleMask:CPBorderlessWindowMask];
|
||||
|
||||
[panel setTitle:@""];
|
||||
[panel setFloatingPanel:YES];
|
||||
[panel setBecomesKeyOnlyIfNeeded:YES];
|
||||
[panel setHasShadow:NO];
|
||||
[panel setDelegate:self];
|
||||
|
||||
return panel;
|
||||
}
|
||||
|
||||
/*!
|
||||
Create and configure the table view. Subclasses should use \ref finalizeTableView:.
|
||||
*/
|
||||
- (_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(0, 0)];
|
||||
[table setTarget:self];
|
||||
[table setDoubleAction:@selector(tableViewClickAction:)];
|
||||
[table setAction:@selector(tableViewClickAction:)];
|
||||
[table setRowHeight:[self rowHeightForTableView:table]];
|
||||
|
||||
return table;
|
||||
}
|
||||
|
||||
- (void)removeTableViewObservers
|
||||
{
|
||||
if (_tableView)
|
||||
{
|
||||
var defaultCenter = [CPNotificationCenter defaultCenter];
|
||||
|
||||
[defaultCenter removeObserver:self name:CPTableViewSelectionIsChangingNotification object:_tableView];
|
||||
[defaultCenter removeObserver:self name:CPTableViewSelectionDidChangeNotification object:_tableView];
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
If subclasses want to configure the table view after it has been
|
||||
completely configured, for example to set a custom data view,
|
||||
this is the place. Such customization can also be done by an
|
||||
observer of _CPPopUpListListWillDisplayNotification.
|
||||
*/
|
||||
- (void)finalizeTableView:(CPTableView)aTableView
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
Creates and configures the scroll view containing the table view.
|
||||
Subclasses should override this (and usually call super first)
|
||||
if they want to customize the scroll view.
|
||||
*/
|
||||
- (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;
|
||||
}
|
||||
/*!
|
||||
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;
|
||||
}
|
||||
|
||||
/!*
|
||||
Pop up the list if it is not already visible.
|
||||
If it is not visible, a _CPPopUpListWillPopUpNotification will be sent.
|
||||
*/
|
||||
- (void)popUpRelativeTo:(CGRect)baseRect
|
||||
{
|
||||
if ([_panel isVisible])
|
||||
return;
|
||||
|
||||
var frame = CGRectMake(0, 0, MAX(_listWidth, CGRectGetWidth(baseRect)), [self rowHeightForTableView:_tableView] * [self numberOfRowsInTableView:_tableView]);
|
||||
|
||||
// Place the frame relative to the baseRect and constrain it to the screen bounds
|
||||
frame = [self constrain:frame relativeTo:baseRect];
|
||||
|
||||
[_panel setFrame:frame];
|
||||
[_scrollView setFrameSize:CGSizeMakeCopy(frame.size)];
|
||||
|
||||
[self listWillPopUp];
|
||||
|
||||
[_panel orderFront:nil];
|
||||
}
|
||||
|
||||
/*!
|
||||
Return a frame in 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 baseRect, but if there is not room
|
||||
for at least ListMinimumItems items, an attempt should be made to display that many
|
||||
items above \c baseRect. If the minimum cannot be displayed on top, whichever direction can display more items
|
||||
is chosen.
|
||||
*/
|
||||
- (CGRect)constrain:(CGRect)aFrame relativeTo:(CGRect)baseRect
|
||||
{
|
||||
var baseOrigin = CGPointMake(CGRectGetMinX(baseRect), CGRectGetMaxY(baseRect)),
|
||||
rowHeight = [self rowHeightForTableView:_tableView],
|
||||
// Be sure to clip the number of displayed rows to what the field wants
|
||||
numberOfRows = MIN([self numberOfRowsInTableView:_tableView], [_delegate numberOfVisibleItems]),
|
||||
// Add 2 to height for border
|
||||
frame = CGRectMake(baseOrigin.x, baseOrigin.y, MAX(_listWidth, CGRectGetWidth(aFrame)), (rowHeight * numberOfRows) + 2),
|
||||
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 = CGRectGetMinY(baseRect) - 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;
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns whether the list is currently visible.
|
||||
*/
|
||||
- (BOOL)isVisible
|
||||
{
|
||||
return [_panel isVisible];
|
||||
}
|
||||
|
||||
/*!
|
||||
Subclasses MUST call this method just before the list is about to display.
|
||||
*/
|
||||
- (void)listWillPopUp
|
||||
{
|
||||
[[CPNotificationCenter defaultCenter] postNotificationName:_CPPopUpListWillPopUpNotification object:self];
|
||||
}
|
||||
|
||||
/*!
|
||||
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 < ([_delegate numberOfItems] - 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;
|
||||
}
|
||||
|
||||
/*!
|
||||
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];
|
||||
}
|
||||
|
||||
- (BOOL)selectRow:(int)row
|
||||
{
|
||||
if (row === [_tableView selectedRow])
|
||||
return NO;
|
||||
|
||||
var indexes = row >= 0 ? [CPIndexSet indexSetWithIndex:row] : [CPIndexSet indexSet];
|
||||
|
||||
[_tableView selectRowIndexes:indexes byExtendingSelection:NO];
|
||||
|
||||
if (row >= 0)
|
||||
{
|
||||
[_tableView scrollRowToVisible:row];
|
||||
return YES;
|
||||
}
|
||||
else
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (void)scrollRowToTop:(int)row
|
||||
{
|
||||
var rect = [_tableView rectOfRow:row];
|
||||
|
||||
[[_tableView superview] scrollToPoint:rect.origin];
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns the selected value as a single-line string. If no value is selected,
|
||||
returns nil.
|
||||
*/
|
||||
- (CPString)selectedStringValue
|
||||
{
|
||||
var row = [_tableView selectedRow];
|
||||
|
||||
return (row >= 0) ? [self stringValueForObjectValue:[_delegate objectValueForItemAtIndex:row] row:row] : nil;
|
||||
}
|
||||
|
||||
/*!
|
||||
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 && ([_delegate numberOfItems] > 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 = ([_delegate numberOfItems] > 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 && ([_delegate numberOfItems] > 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 = ([_delegate numberOfItems] > 0) && flag;
|
||||
}
|
||||
|
||||
/*!
|
||||
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];
|
||||
}
|
||||
|
||||
/*!
|
||||
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 a value to display for a single row in the list. Subclasses should override
|
||||
this if the table data needs to be converted or formatted in some way to be displayed.
|
||||
If there are no search results, an empty string is returned.
|
||||
If subclasses use a data representation other than CPStrings, they must override
|
||||
this method and return the appropriate data when there are no search results.
|
||||
|
||||
If the table uses a custom data view, this method should return a value suitable
|
||||
for sending to the setObjectValue: method of the data view.
|
||||
|
||||
@param aValue Table data for the given row
|
||||
@param aRow The row being displayed
|
||||
@return A value to be displayed in the list
|
||||
*/
|
||||
- (id)displayValueForObjectValue:(id)aValue row:(int)aRow
|
||||
{
|
||||
return aValue || @"";
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns a single-line string for use in an autocomplete field. Subclasses should override
|
||||
this if the row data is not convertable to a simple string, and return the data as a single-line string.
|
||||
|
||||
@param aValue Table data to be converted to a string
|
||||
@param aRow The row whose data is being converted
|
||||
@return A value to be displayed in the autocomplete field
|
||||
*/
|
||||
- (CPString)stringValueForObjectValue:(id)aValue row:(int)aRow
|
||||
{
|
||||
return String(aValue);
|
||||
}
|
||||
|
||||
- (void)tableViewClickAction:(id)sender
|
||||
{
|
||||
[self close];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
var _CPPopUpListFieldKey = @"_CPPopUpListDelegateKey",
|
||||
_CPPopUpListListWidthKey = @"_CPPopUpListListWidthKey",
|
||||
_CPPopUpListListPanelKey = @"_CPPopUpListListPanelKey",
|
||||
_CPPopUpListScrollViewKey = @"_CPPopUpListScrollViewKey",
|
||||
_CPPopUpListTableViewKey = @"_CPPopUpListTableViewKey";
|
||||
|
||||
@implementation _CPPopUpList (CPCoding)
|
||||
|
||||
- (id)initWithCoder:(CPCoder)aCoder
|
||||
{
|
||||
self = [super initWithCoder:aCoder];
|
||||
|
||||
if (self)
|
||||
{
|
||||
_delegate = [aCoder decodeObjectForKey:_CPPopUpListDelegateKey];
|
||||
_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:_delegate forKey:_CPPopUpListDelegateKey];
|
||||
[aCoder encodeObject:_listWidth forKey:_CPPopUpListListWidthKey];
|
||||
[aCoder encodeObject:_panel forKey:_CPPopUpListListPanelKey];
|
||||
[aCoder encodeObject:_scrollView forKey:_CPPopUpListScrollViewKey];
|
||||
[aCoder encodeObject:_tableView forKey:_CPPopUpListTableViewKey];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation _CPPopUpList (CPTableViewDelegate)
|
||||
|
||||
|
||||
|
||||
@end
|
||||
|
||||
@implementation _CPPopUpList (CPTableViewDataSource)
|
||||
|
||||
- (int)numberOfRowsInTableView:(id)aTableView
|
||||
{
|
||||
return MAX([_delegate numberOfItems], 1);
|
||||
}
|
||||
|
||||
- (id)tableView:(id)aTableView objectValueForTableColumn:(CPTableColumn)aColumn row:(int)aRow
|
||||
{
|
||||
return [self displayValueForObjectValue:[_delegate objectValueForItemAtIndex:aRow] row:aRow];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation _CPPopUpTableView : CPTableView
|
||||
|
||||
- (void)mouseDown:(CPEvent)anEvent
|
||||
{
|
||||
if (![self isEnabled])
|
||||
return;
|
||||
|
||||
[[self delegate] setItemWasClicked:YES];
|
||||
[super mouseDown:anEvent];
|
||||
}
|
||||
|
||||
/*
|
||||
We always want the autocomplete to have focus
|
||||
*/
|
||||
- (BOOL)acceptsFirstResponder
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
|
||||
/*!
|
||||
Return the column used for the list.
|
||||
*/
|
||||
- (CPTableColumn)listColumn
|
||||
{
|
||||
return [self tableColumnWithIdentifier:ListColumnIdentifier];
|
||||
}
|
||||
|
||||
@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
|
||||
Reference in new issue
Block a user