2007-01-01 21:11:21 +00:00
|
|
|
/* Copyright (c) 2006-2007 Christopher J. W. Lloyd
|
2006-12-22 04:41:44 +00:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
#import <AppKit/NSTextField.h>
|
|
|
|
#import <AppKit/NSTextFieldCell.h>
|
2013-03-27 19:19:14 +00:00
|
|
|
#import <AppKit/NSTextView.h>
|
|
|
|
#import <AppKit/NSTextStorage.h>
|
2006-12-22 04:41:44 +00:00
|
|
|
#import <AppKit/NSApplication.h>
|
|
|
|
#import <AppKit/NSWindow.h>
|
|
|
|
#import <AppKit/NSCursor.h>
|
|
|
|
#import <AppKit/NSPasteboard.h>
|
|
|
|
#import <AppKit/NSDragging.h>
|
2009-08-05 20:14:25 +00:00
|
|
|
#import "NSKeyValueBinding/NSTextFieldBinder.h"
|
2009-07-19 16:06:35 +00:00
|
|
|
#import "NSKeyValueBinding/NSObject+BindingSupport.h"
|
2006-12-22 04:41:44 +00:00
|
|
|
|
2010-12-08 10:02:58 +00:00
|
|
|
|
|
|
|
@interface NSTextFieldCell (Private)
|
|
|
|
|
|
|
|
- (CGFloat) _fontSize;
|
|
|
|
- (void) _setFontSize:(CGFloat)fontSize;
|
|
|
|
- (NSString*) _fontFamilyName;
|
|
|
|
- (void) _setFontFamilyName:(NSString*)familyName;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-12-22 04:41:44 +00:00
|
|
|
@implementation NSTextField
|
|
|
|
|
|
|
|
+(Class)cellClass {
|
|
|
|
return [NSTextFieldCell class];
|
|
|
|
}
|
|
|
|
|
2009-07-19 16:06:35 +00:00
|
|
|
+(Class)_binderClassForBinding:(id)binding
|
|
|
|
{
|
|
|
|
if ([binding isEqual:@"value"])
|
2009-08-05 20:14:25 +00:00
|
|
|
return [_NSTextFieldBinder class];
|
2009-07-19 16:06:35 +00:00
|
|
|
else
|
|
|
|
return [super _binderClassForBinding:binding];
|
|
|
|
}
|
|
|
|
|
2006-12-22 04:41:44 +00:00
|
|
|
-initWithCoder:(NSCoder *)coder {
|
|
|
|
[super initWithCoder:coder];
|
|
|
|
[self registerForDraggedTypes:[NSArray arrayWithObject:NSStringPboardType]];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
-initWithFrame:(NSRect)frame {
|
|
|
|
[super initWithFrame:frame];
|
2011-01-20 21:16:48 +00:00
|
|
|
// Default for a NSTextFieldCell is NOT the same as NSTextField
|
|
|
|
[_cell setEditable:YES];
|
|
|
|
[_cell setSelectable:YES];
|
|
|
|
[_cell setBezeled:YES];
|
2006-12-22 04:41:44 +00:00
|
|
|
[self registerForDraggedTypes:[NSArray arrayWithObject:NSStringPboardType]];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(BOOL)isFlipped { return YES; }
|
|
|
|
|
|
|
|
-(BOOL)isOpaque {
|
|
|
|
return [_cell drawsBackground] || [_cell isBezeled];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)resetCursorRects {
|
|
|
|
[_cell resetCursorRect:[self bounds] inView:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
-delegate {
|
|
|
|
return _delegate;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)setDelegate:delegate {
|
|
|
|
NSNotificationCenter *center=[NSNotificationCenter defaultCenter];
|
|
|
|
struct {
|
|
|
|
SEL selector;
|
|
|
|
NSString *name;
|
|
|
|
} notes[]={
|
|
|
|
{ @selector(controlTextDidBeginEditing:), NSControlTextDidBeginEditingNotification },
|
|
|
|
{ @selector(controlTextDidChange:), NSControlTextDidChangeNotification },
|
|
|
|
{ @selector(controlTextDidEndEditing:), NSControlTextDidEndEditingNotification },
|
|
|
|
{ NULL, nil }
|
|
|
|
};
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if(_delegate!=nil)
|
|
|
|
for(i=0;notes[i].selector!=NULL;i++)
|
|
|
|
[center removeObserver:_delegate name:notes[i].name object:self];
|
|
|
|
|
|
|
|
_delegate=delegate;
|
|
|
|
|
|
|
|
for(i=0;notes[i].selector!=NULL;i++)
|
|
|
|
if([_delegate respondsToSelector:notes[i].selector])
|
|
|
|
[center addObserver:_delegate selector:notes[i].selector name:notes[i].name object:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(NSColor *)backgroundColor {
|
|
|
|
return [_cell backgroundColor];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(NSColor *)textColor {
|
|
|
|
return [_cell textColor];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(BOOL)drawsBackground {
|
|
|
|
return [_cell drawsBackground];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)setBackgroundColor:(NSColor *)color {
|
|
|
|
[_cell setBackgroundColor:color];
|
|
|
|
[self setNeedsDisplay:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)setTextColor:(NSColor *)color {
|
|
|
|
[_cell setTextColor:color];
|
|
|
|
[self setNeedsDisplay:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)setDrawsBackground:(BOOL)flag {
|
|
|
|
[_cell setDrawsBackground:flag];
|
|
|
|
[self setNeedsDisplay:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(BOOL)acceptsFirstResponder {
|
2012-03-29 22:50:15 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(BOOL)canBecomeKeyView {
|
|
|
|
if(![self isEditable])
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
return [super canBecomeKeyView];
|
2006-12-22 04:41:44 +00:00
|
|
|
}
|
|
|
|
|
2011-01-29 21:26:21 +00:00
|
|
|
-(BOOL)needsPanelToBecomeKey {
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2006-12-22 04:41:44 +00:00
|
|
|
-(BOOL)becomeFirstResponder {
|
|
|
|
[self selectText:nil];
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)_selectTextWithRange:(NSRange)range {
|
|
|
|
NSTextFieldCell *cell=[self selectedCell];
|
|
|
|
|
|
|
|
if(![cell isEnabled])
|
|
|
|
return;
|
|
|
|
|
|
|
|
if([cell isEditable] || [cell isSelectable]){
|
|
|
|
if(_currentEditor==nil){
|
2013-03-27 19:19:14 +00:00
|
|
|
NSText* editor =[[self window] fieldEditor:YES forObject:self];
|
|
|
|
_currentEditor = [[cell setUpFieldEditorAttributes: editor] retain];
|
2011-02-21 00:44:09 +00:00
|
|
|
[_currentEditor setDelegate:self];
|
|
|
|
[_currentEditor registerForDraggedTypes:[self _draggedTypes]];
|
|
|
|
[_currentEditor becomeFirstResponder];
|
2006-12-22 04:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[cell selectWithFrame:[self bounds] inView:self editor:_currentEditor delegate:self start:range.location length:range.length];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)selectText:sender {
|
2013-03-27 19:19:14 +00:00
|
|
|
[self _selectTextWithRange:NSMakeRange(0,[[self stringValue] length])];
|
2006-12-22 04:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
-(void)setPreviousText:text {
|
|
|
|
// FIX, not sure how to implement this
|
|
|
|
// [self setPreviousKeyView:text];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)setNextText:text {
|
|
|
|
[self setNextKeyView:text];
|
|
|
|
}
|
|
|
|
|
|
|
|
-previousText {
|
|
|
|
return [self previousKeyView];
|
|
|
|
}
|
|
|
|
|
|
|
|
-nextText {
|
|
|
|
return [self nextKeyView];
|
|
|
|
}
|
|
|
|
|
2013-03-27 19:19:14 +00:00
|
|
|
-(void)validateEditing
|
|
|
|
{
|
|
|
|
if (_currentEditor) {
|
|
|
|
NSString *string = [_currentEditor string];
|
|
|
|
BOOL acceptsString = YES;
|
|
|
|
NSFormatter *formatter = [self formatter];
|
|
|
|
if (formatter) {
|
|
|
|
acceptsString = NO;
|
|
|
|
|
|
|
|
id objectValue = nil;
|
|
|
|
NSString *error = nil;
|
|
|
|
if ([formatter getObjectValue: &objectValue
|
|
|
|
forString: string
|
|
|
|
errorDescription: &error] == YES) {
|
|
|
|
[[self selectedCell] setObjectValue:objectValue];
|
|
|
|
} else {
|
|
|
|
// Ask the delegate what to do
|
|
|
|
SEL sel = @selector(control:didFailToFormatString:errorDescription:);
|
|
|
|
if ([_delegate respondsToSelector: sel]) {
|
|
|
|
acceptsString = [_delegate control: self
|
|
|
|
didFailToFormatString: string
|
|
|
|
errorDescription: error];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (acceptsString) {
|
|
|
|
if ([_currentEditor isRichText]) {
|
|
|
|
if ([_currentEditor isKindOfClass:[NSTextView class]]) {
|
|
|
|
NSTextView *textview = (NSTextView *)_currentEditor;
|
|
|
|
NSAttributedString *text = [textview textStorage];
|
|
|
|
NSAttributedString *string = [[[NSAttributedString alloc] initWithAttributedString:text] autorelease];
|
|
|
|
[[self selectedCell] setAttributedStringValue:string];
|
|
|
|
} else {
|
|
|
|
[[self selectedCell] setStringValue:string];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
[[self selectedCell] setStringValue:string];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-22 04:41:44 +00:00
|
|
|
-(void)textDidEndEditing:(NSNotification *)note {
|
2013-03-27 19:19:14 +00:00
|
|
|
|
2006-12-22 04:41:44 +00:00
|
|
|
int movement=[[[note userInfo] objectForKey:@"NSTextMovement"] intValue];
|
|
|
|
|
|
|
|
[super textDidEndEditing:note];
|
|
|
|
|
2007-11-19 14:49:00 +00:00
|
|
|
if(movement==NSReturnTextMovement || [[self selectedCell] sendsActionOnEndEditing])
|
2006-12-22 04:41:44 +00:00
|
|
|
[self sendAction:[self action] to:[self target]];
|
|
|
|
|
|
|
|
if(movement==NSTabTextMovement)
|
|
|
|
[[self window] selectKeyViewFollowingView:self];
|
2009-07-19 02:32:35 +00:00
|
|
|
|
|
|
|
if (movement == NSBacktabTextMovement)
|
|
|
|
[[self window] selectKeyViewPrecedingView:self];
|
2006-12-22 04:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void)mouseDown:(NSEvent *)event {
|
|
|
|
NSTextFieldCell *cell=[self selectedCell];
|
|
|
|
|
|
|
|
if(![cell isEnabled])
|
|
|
|
return;
|
|
|
|
|
2013-04-16 12:52:03 +00:00
|
|
|
if ([[self window] firstResponder] != self) {
|
|
|
|
// This will create our editor if needed
|
|
|
|
[[self window] makeFirstResponder:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
NSPoint point=[self convertPoint:[event locationInWindow] fromView:nil];
|
|
|
|
NSRect editingFrame=[cell titleRectForBounds:[self bounds]];
|
|
|
|
|
|
|
|
// In case we get the mouse down because we hadn't an editor yet, propagate the mouse event to the editor
|
|
|
|
if(_currentEditor && NSMouseInRect(point,editingFrame,[self isFlipped])) {
|
|
|
|
[_currentEditor mouseDown:event];
|
|
|
|
} else {
|
2013-03-27 19:19:14 +00:00
|
|
|
[super mouseDown:event];
|
2006-12-22 04:41:44 +00:00
|
|
|
}
|
2010-10-22 18:49:57 +00:00
|
|
|
}
|
2006-12-22 04:41:44 +00:00
|
|
|
|
2008-10-03 13:31:45 +00:00
|
|
|
-(BOOL)textShouldBeginEditing:(NSText *)text {
|
|
|
|
if ([_delegate respondsToSelector:@selector(control:textShouldBeginEditing:)])
|
|
|
|
if ([_delegate control:self textShouldBeginEditing:text] == NO) {
|
|
|
|
NSBeep();
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(BOOL)textShouldEndEditing:(NSText *)text {
|
|
|
|
if ([_delegate respondsToSelector:@selector(control:textShouldEndEditing:)])
|
|
|
|
if ([_delegate control:self textShouldEndEditing:text] == NO) {
|
|
|
|
NSBeep();
|
|
|
|
return NO;
|
|
|
|
}
|
2013-03-27 19:19:14 +00:00
|
|
|
|
|
|
|
NSFormatter *formatter = [[self selectedCell] formatter];
|
|
|
|
BOOL acceptsString = YES;
|
|
|
|
if (formatter) {
|
|
|
|
// Ask the formatter if the string is valid
|
|
|
|
// If it's not, then we'll present an error and refuse the end of the editing
|
|
|
|
|
|
|
|
NSString *string = [text string];
|
|
|
|
id objectValue;
|
|
|
|
NSError *error = nil;
|
|
|
|
if ([formatter isKindOfClass:[NSNumberFormatter class]]) {
|
|
|
|
NSNumberFormatter *numberFormatter = (NSNumberFormatter *)formatter;
|
|
|
|
acceptsString = [numberFormatter getObjectValue:&objectValue forString:string range:NULL error:&error];
|
|
|
|
} else {
|
|
|
|
NSString *errorString;
|
|
|
|
if ([formatter getObjectValue: &objectValue
|
|
|
|
forString: string
|
|
|
|
errorDescription: &errorString] == NO) {
|
|
|
|
|
|
|
|
acceptsString = NO;
|
|
|
|
NSDictionary *info = nil;
|
|
|
|
if (errorString == nil) {
|
|
|
|
// Just in case we get no error from the formatter
|
|
|
|
errorString = NSLocalizedStringFromTableInBundle(@"Invalid number", nil, [NSBundle bundleForClass: [NSTextField class]], @"");
|
|
|
|
}
|
|
|
|
info = [NSDictionary dictionaryWithObject:errorString forKey:NSLocalizedDescriptionKey];
|
|
|
|
error = [NSError errorWithDomain:NSCocoaErrorDomain code:2048 userInfo:info];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (error) {
|
|
|
|
[self presentError:error];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return acceptsString;
|
2008-10-03 13:31:45 +00:00
|
|
|
}
|
|
|
|
|
2009-06-26 02:18:45 +00:00
|
|
|
-(void)setEditable:(BOOL)flag {
|
|
|
|
[super setEditable:flag];
|
|
|
|
[_currentEditor setEditable:[self isEditable]];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)setSelectable:(BOOL)flag {
|
|
|
|
[super setSelectable:flag];
|
|
|
|
[_currentEditor setSelectable:[self isSelectable]];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)setStringValue:(NSString *)value {
|
2015-08-10 02:48:05 +00:00
|
|
|
if (!_currentEditor)
|
|
|
|
[super setStringValue:value];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSRange selectedRange=[_currentEditor selectedRange];
|
|
|
|
BOOL isEntireString=NSEqualRanges(selectedRange, NSMakeRange(0, [[_currentEditor string] length]));
|
2012-04-22 04:45:20 +00:00
|
|
|
|
2015-08-10 02:48:05 +00:00
|
|
|
[super setStringValue:value];
|
|
|
|
[_currentEditor setString:[self stringValue]];
|
|
|
|
NSRange entireString=NSMakeRange(0, [[_currentEditor string] length]);
|
2012-04-22 04:45:20 +00:00
|
|
|
|
2015-08-10 02:48:05 +00:00
|
|
|
if (isEntireString) {
|
|
|
|
// NSTextField will re-select entire string on a setString: if the previous value is completely selected
|
|
|
|
[_currentEditor setSelectedRange:entireString];
|
|
|
|
}
|
|
|
|
else {
|
2012-04-22 04:45:20 +00:00
|
|
|
// otherwise it will re-select what it can
|
|
|
|
selectedRange=NSIntersectionRange(selectedRange, entireString);
|
|
|
|
|
2015-08-10 02:48:05 +00:00
|
|
|
if (selectedRange.length==0) // 0 on intersection is undefined location, so we have to set it
|
2012-04-22 04:45:20 +00:00
|
|
|
selectedRange.location=0;
|
2015-08-10 02:48:05 +00:00
|
|
|
[_currentEditor setSelectedRange:entireString];
|
|
|
|
}
|
|
|
|
}
|
2009-06-26 02:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void)setFont:(NSFont *)font {
|
2012-02-29 15:48:50 +00:00
|
|
|
// Don't do extra work if we don't need to...
|
|
|
|
if (font != [self font]) {
|
|
|
|
[super setFont:font];
|
|
|
|
[_currentEditor setFont:[self font]];
|
|
|
|
}
|
2009-06-26 02:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void)setAlignment:(NSTextAlignment)alignment {
|
|
|
|
[super setAlignment:alignment];
|
|
|
|
[_currentEditor setAlignment:[self alignment]];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)setRefusesFirstResponder:(BOOL)flag {
|
|
|
|
[_cell setRefusesFirstResponder:flag];
|
|
|
|
}
|
|
|
|
|
2009-11-08 19:49:12 +00:00
|
|
|
-(void)doCommandBySelector:(SEL)selector {
|
|
|
|
BOOL didSelector=NO;
|
|
|
|
|
|
|
|
if([_delegate respondsToSelector:@selector(control:textView:doCommandBySelector:)])
|
|
|
|
didSelector=[_delegate control:self textView:(NSTextView *)_currentEditor doCommandBySelector:selector];
|
|
|
|
|
|
|
|
if(!didSelector)
|
|
|
|
[super doCommandBySelector:selector];
|
|
|
|
}
|
|
|
|
|
2010-10-22 18:49:57 +00:00
|
|
|
-(void)setTitleWithMnemonic:(NSString *)value {
|
|
|
|
[self setStringValue:value];
|
|
|
|
}
|
2009-11-08 19:49:12 +00:00
|
|
|
|
2006-12-22 04:41:44 +00:00
|
|
|
@end
|
2010-12-08 16:52:48 +00:00
|
|
|
|
|
|
|
@implementation NSTextField (Bindings)
|
2010-12-08 10:02:58 +00:00
|
|
|
|
|
|
|
- (CGFloat) _fontSize {
|
|
|
|
return [(NSTextFieldCell*)_cell _fontSize];
|
|
|
|
}
|
|
|
|
- (void) _setFontSize:(CGFloat)fontSize {
|
|
|
|
[(NSTextFieldCell*)_cell _setFontSize:fontSize];
|
|
|
|
}
|
|
|
|
- (NSString*) _fontFamilyName {
|
|
|
|
return [(NSTextFieldCell*)_cell _fontFamilyName];
|
|
|
|
}
|
|
|
|
- (void) _setFontFamilyName:(NSString*)familyName {
|
|
|
|
[(NSTextFieldCell*)_cell _setFontFamilyName:familyName];
|
|
|
|
}
|
|
|
|
|
2010-12-08 16:52:48 +00:00
|
|
|
@end
|