From ced527d0e5510333ecff6568b01b3d9aa11c12c1 Mon Sep 17 00:00:00 2001 From: fredkiefer Date: Sun, 9 Sep 2007 15:30:06 +0000 Subject: [PATCH] Add new MacOSX 10.3 methods and constants. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@25470 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 10 +- Headers/AppKit/NSTextField.h | 8 ++ Headers/AppKit/NSTextFieldCell.h | 30 +++++ Source/NSTextField.m | 182 ++++++++++++++++--------------- Source/NSTextFieldCell.m | 172 ++++++++++++++++++++++------- 5 files changed, 274 insertions(+), 128 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2e292cf6c..4ce57bc6a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,12 @@ -2007-09-08 Fred Kiefer +2007-09-09 Fred Kiefer + + * Headers/AppKit/NSTextField.h: + * Headers/AppKit/NSTextFieldCell.h: + * Source/NSTextField.m, + * Source/NSTextFieldCell.m: Add new MacOSX 10.3 methods and + constants. + +2007-09-09 Fred Kiefer * Headers/AppKit/NSBitmapImageRep.h, * Headers/AppKit/NSResponder.h: Correct conditional compilation. diff --git a/Headers/AppKit/NSTextField.h b/Headers/AppKit/NSTextField.h index e41b56921..b85dedb87 100644 --- a/Headers/AppKit/NSTextField.h +++ b/Headers/AppKit/NSTextField.h @@ -29,8 +29,11 @@ #ifndef _GNUstep_H_NSTextField #define _GNUstep_H_NSTextField +#import #include +// For NSTextFieldBezelStyle +#include @class NSNotification; @class NSColor; @@ -114,6 +117,11 @@ - (void)setTitleWithMnemonic:(NSString *)aString; #endif +#if OS_API_VERSION(MAC_OS_X_VERSION_10_2, GS_API_LATEST) +- (void)setBezelStyle:(NSTextFieldBezelStyle)style; +- (NSTextFieldBezelStyle)bezelStyle; +#endif + @end #endif // _GNUstep_H_NSTextField diff --git a/Headers/AppKit/NSTextFieldCell.h b/Headers/AppKit/NSTextFieldCell.h index 24ad1f3c0..85fada496 100644 --- a/Headers/AppKit/NSTextFieldCell.h +++ b/Headers/AppKit/NSTextFieldCell.h @@ -29,21 +29,39 @@ #ifndef _GNUstep_H_NSTextFieldCell #define _GNUstep_H_NSTextFieldCell +#import #include @class NSColor; +#if OS_API_VERSION(MAC_OS_X_VERSION_10_2, GS_API_LATEST) +typedef enum _NSTextFieldBezelStyle +{ + NSTextFieldSquareBezel = 0, + NSTextFieldRoundedBezel +} NSTextFieldBezelStyle; +#endif + @interface NSTextFieldCell : NSActionCell { // Attributes NSColor *_background_color; NSColor *_text_color; + +#if OS_API_VERSION(MAC_OS_X_VERSION_10_2, GS_API_LATEST) + NSTextFieldBezelStyle _bezelStyle; +#else + unsigned int _bezelStyle; +#endif + // Think of the following ones as of two BOOL ivars #define _textfieldcell_draws_background _cell.subclass_bool_one // The following is different from _draws_background // if we are using a semi-transparent color. #define _textfieldcell_is_opaque _cell.subclass_bool_two +#define _textfieldcell_placeholder_is_attributed_string _cell.subclass_bool_three + id _placeholder; } // @@ -58,6 +76,18 @@ - (void)setBackgroundColor:(NSColor *)aColor; - (NSColor *)backgroundColor; +#if OS_API_VERSION(MAC_OS_X_VERSION_10_2, GS_API_LATEST) +- (void)setBezelStyle:(NSTextFieldBezelStyle)style; +- (NSTextFieldBezelStyle)bezelStyle; +#endif + +#if OS_API_VERSION(MAC_OS_X_VERSION_10_3, GS_API_LATEST) +- (void)setPlaceholderString:(NSString *)string; +- (NSString *)placeholderString; +- (void)setPlaceholderAttributedString:(NSAttributedString *)string; +- (NSAttributedString *)placeholderAttributedString; +#endif + @end #endif // _GNUstep_H_NSTextFieldCell diff --git a/Source/NSTextField.m b/Source/NSTextField.m index 910981fd3..6e91473c1 100644 --- a/Source/NSTextField.m +++ b/Source/NSTextField.m @@ -83,11 +83,12 @@ static Class textFieldCellClass; // - (id) initWithFrame: (NSRect)frameRect { - [super initWithFrame: frameRect]; - [_cell setState: 1]; + self = [super initWithFrame: frameRect]; + if (self == nil) + return self; + [_cell setBezeled: YES]; [_cell setSelectable: YES]; - [_cell setEnabled: YES]; [_cell setEditable: YES]; [_cell setDrawsBackground: YES]; _text_object = nil; @@ -99,8 +100,7 @@ static Class textFieldCellClass; { if (_delegate != nil) { - [nc removeObserver: _delegate name: nil object: self]; - _delegate = nil; + [self setDelegate: nil]; } [super dealloc]; @@ -157,28 +157,28 @@ static Class textFieldCellClass; if ([self isSelectable] && (_super_view != nil)) { if (_text_object) - [_text_object selectAll: self]; + [_text_object selectAll: self]; else - { - NSText *text = [_window fieldEditor: YES forObject: self]; - int length; + { + NSText *text = [_window fieldEditor: YES forObject: self]; + int length; - if ([text superview] != nil) - if ([text resignFirstResponder] == NO) - return; - - // [NSCursor hide]; - /* [self stringValue] generates a call to validateEditing - so we need to call it before setting up the _text_object */ - length = [[self stringValue] length]; - _text_object = [_cell setUpFieldEditorAttributes: text]; - [_cell selectWithFrame: _bounds - inView: self - editor: _text_object - delegate: self - start: 0 - length: length]; - } + if ([text superview] != nil) + if ([text resignFirstResponder] == NO) + return; + + // [NSCursor hide]; + /* [self stringValue] generates a call to validateEditing + so we need to call it before setting up the _text_object */ + length = [[self stringValue] length]; + _text_object = [_cell setUpFieldEditorAttributes: text]; + [_cell selectWithFrame: _bounds + inView: self + editor: _text_object + delegate: self + start: 0 + length: length]; + } } } @@ -379,7 +379,7 @@ static Class textFieldCellClass; if ([_window makeFirstResponder: self]) { NSText *t = [_window fieldEditor: YES forObject: self]; - + if ([t superview] != nil) { /* Can't take the field editor ... give up. */ @@ -526,35 +526,35 @@ static Class textFieldCellClass; partialString = [_text_object string]; wasAccepted = [formatter isPartialStringValid: partialString - newEditingString: &newString - errorDescription: &error]; + newEditingString: &newString + errorDescription: &error]; if (wasAccepted == NO) - { - SEL sel = @selector(control:didFailToValidatePartialString:errorDescription:); - - if ([_delegate respondsToSelector: sel]) - { - [_delegate control: self - didFailToValidatePartialString: partialString - errorDescription: error]; - } - } + { + SEL sel = @selector(control:didFailToValidatePartialString:errorDescription:); + + if ([_delegate respondsToSelector: sel]) + { + [_delegate control: self + didFailToValidatePartialString: partialString + errorDescription: error]; + } + } if (newString != nil) - { - NSLog (@"Unimplemented: should set string to %@", newString); - // FIXME ! This would reset editing ! - //[_text_object setString: newString]; - } + { + NSLog (@"Unimplemented: should set string to %@", newString); + // FIXME ! This would reset editing ! + //[_text_object setString: newString]; + } else - { - if (wasAccepted == NO) - { - // FIXME: Need to delete last typed character (?!) - NSLog (@"Unimplemented: should delete last typed character"); - } - } + { + if (wasAccepted == NO) + { + // FIXME: Need to delete last typed character (?!) + NSLog (@"Unimplemented: should delete last typed character"); + } + } } } @@ -568,27 +568,27 @@ static Class textFieldCellClass; if (textMovement) { switch ([(NSNumber *)textMovement intValue]) - { - case NSReturnTextMovement: - if ([self sendAction: [self action] to: [self target]] == NO) - { - if ([self performKeyEquivalent: [_window currentEvent]] == NO) - [self selectText: self]; - } - break; - case NSTabTextMovement: - [_window selectKeyViewFollowingView: self]; + { + case NSReturnTextMovement: + if ([self sendAction: [self action] to: [self target]] == NO) + { + if ([self performKeyEquivalent: [_window currentEvent]] == NO) + [self selectText: self]; + } + break; + case NSTabTextMovement: + [_window selectKeyViewFollowingView: self]; - if ([_window firstResponder] == _window) - [self selectText: self]; - break; - case NSBacktabTextMovement: - [_window selectKeyViewPrecedingView: self]; + if ([_window firstResponder] == _window) + [self selectText: self]; + break; + case NSBacktabTextMovement: + [_window selectKeyViewPrecedingView: self]; - if ([_window firstResponder] == _window) - [self selectText: self]; - break; - } + if ([_window firstResponder] == _window) + [self selectText: self]; + break; + } } } @@ -598,9 +598,9 @@ static Class textFieldCellClass; return NO; if (_delegate && [_delegate respondsToSelector: - @selector(control:textShouldBeginEditing:)]) + @selector(control:textShouldBeginEditing:)]) return [_delegate control: self - textShouldBeginEditing: textObject]; + textShouldBeginEditing: textObject]; else return YES; } @@ -614,13 +614,13 @@ static Class textFieldCellClass; } if ([_delegate respondsToSelector: - @selector(control:textShouldEndEditing:)]) + @selector(control:textShouldEndEditing:)]) { if ([_delegate control: self textShouldEndEditing: textObject] == NO) - { - NSBeep (); - return NO; - } + { + NSBeep (); + return NO; + } } if ([_delegate respondsToSelector: @selector(control:isValidObject:)] == YES) @@ -631,14 +631,14 @@ static Class textFieldCellClass; formatter = [_cell formatter]; if ([formatter getObjectValue: &newObjectValue - forString: [_text_object text] - errorDescription: NULL] == YES) - { - if ([_delegate control: self isValidObject: newObjectValue] == NO) - { - return NO; - } - } + forString: [_text_object text] + errorDescription: NULL] == YES) + { + if ([_delegate control: self isValidObject: newObjectValue] == NO) + { + return NO; + } + } } // In all other cases @@ -651,8 +651,8 @@ static Class textFieldCellClass; [_delegate respondsToSelector: @selector(control:textView:doCommandBySelector:)]) { return [_delegate control: self - textView: textView - doCommandBySelector: command]; + textView: textView + doCommandBySelector: command]; } return NO; @@ -687,6 +687,16 @@ static Class textFieldCellClass; [_cell setTitleWithMnemonic: aString]; } +- (void)setBezelStyle:(NSTextFieldBezelStyle)style +{ + [_cell setBezelStyle: style]; +} + +- (NSTextFieldBezelStyle)bezelStyle +{ + return [_cell bezelStyle]; +} + // // NSCoding protocol // diff --git a/Source/NSTextFieldCell.m b/Source/NSTextFieldCell.m index e7a69cf5d..0933a6239 100644 --- a/Source/NSTextFieldCell.m +++ b/Source/NSTextFieldCell.m @@ -29,16 +29,17 @@ #include "config.h" #include +#include "AppKit/NSAttributedString.h" #include "AppKit/NSColor.h" #include "AppKit/NSControl.h" +#include "AppKit/NSEvent.h" #include "AppKit/NSFont.h" #include "AppKit/NSGraphics.h" #include "AppKit/NSTextFieldCell.h" #include "AppKit/NSText.h" -#include "AppKit/NSEvent.h" -static NSColor *bgCol; -static NSColor *txtCol; +static NSColor *bgCol; +static NSColor *txtCol; @interface NSTextFieldCell (PrivateColor) + (void) _systemColorsChanged: (NSNotification*)n; @@ -46,12 +47,13 @@ static NSColor *txtCol; - (BOOL) _isOpaque; @end -@implementation NSTextFieldCell (PrivateColor) +@implementation NSTextFieldCell (PrivateColor) + (void) _systemColorsChanged: (NSNotification*)n { ASSIGN(bgCol, [NSColor textBackgroundColor]); ASSIGN(txtCol, [NSColor textColor]); } + - (BOOL) _isOpaque { if (_textfieldcell_draws_background == NO @@ -70,10 +72,10 @@ static NSColor *txtCol; { [self setVersion: 2]; [[NSNotificationCenter defaultCenter] - addObserver: self - selector: @selector(_systemColorsChanged:) - name: NSSystemColorsDidChangeNotification - object: nil]; + addObserver: self + selector: @selector(_systemColorsChanged:) + name: NSSystemColorsDidChangeNotification + object: nil]; [self _systemColorsChanged: nil]; } } @@ -81,20 +83,16 @@ static NSColor *txtCol; // // Initialization // -- (id) init -{ - [self initTextCell: @""]; - return self; -} - - (id) initTextCell: (NSString *)aString { - [super initTextCell: aString]; + self = [super initTextCell: aString]; + if (self == nil) + return self; ASSIGN(_text_color, txtCol); ASSIGN(_background_color, bgCol); - _textfieldcell_draws_background = NO; - _textfieldcell_is_opaque = NO; +// _textfieldcell_draws_background = NO; +// _textfieldcell_is_opaque = NO; _action_mask = NSKeyUpMask | NSKeyDownMask; return self; } @@ -103,6 +101,7 @@ static NSColor *txtCol; { RELEASE(_background_color); RELEASE(_text_color); + RELEASE(_placeholder); [super dealloc]; } @@ -110,8 +109,9 @@ static NSColor *txtCol; { NSTextFieldCell *c = [super copyWithZone: zone]; - RETAIN (_background_color); - RETAIN (_text_color); + RETAIN(_background_color); + RETAIN(_text_color); + c->_placeholder = [_placeholder copyWithZone: zone]; return c; } @@ -176,6 +176,52 @@ static NSColor *txtCol; return _text_color; } +- (void) setBezelStyle: (NSTextFieldBezelStyle)style +{ + _bezelStyle = style; +} + +- (NSTextFieldBezelStyle) bezelStyle +{ + return _bezelStyle; +} + +- (NSAttributedString*) placeholderAttributedString +{ + if (_textfieldcell_placeholder_is_attributed_string == YES) + { + return (NSAttributedString*)_placeholder; + } + else + { + return nil; + } +} + +- (NSString*) placeholderString +{ + if (_textfieldcell_placeholder_is_attributed_string == YES) + { + return nil; + } + else + { + return (NSString*)_placeholder; + } +} + +- (void) setPlaceholderAttributedString: (NSAttributedString*)string +{ + ASSIGN(_placeholder, string); + _textfieldcell_placeholder_is_attributed_string = YES; +} + +- (void) setPlaceholderString: (NSString*)string +{ + ASSIGN(_placeholder, string); + _textfieldcell_placeholder_is_attributed_string = NO; +} + - (NSText *) setUpFieldEditorAttributes: (NSText *)textObject { textObject = [super setUpFieldEditorAttributes: textObject]; @@ -190,19 +236,63 @@ static NSColor *txtCol; if (_textfieldcell_draws_background) { if ([self isEnabled]) - { + { [_background_color set]; - } + } else - { - [[NSColor controlBackgroundColor] set]; - } - NSRectFill ([self drawingRectForBounds: cellFrame]); + { + [[NSColor controlBackgroundColor] set]; + } + NSRectFill([self drawingRectForBounds: cellFrame]); } [super drawInteriorWithFrame: cellFrame inView: controlView]; } +/* + Attributed string that will be displayed. + */ +- (NSAttributedString*)_drawAttributedString +{ + NSAttributedString *attrStr; + + attrStr = [super _drawAttributedString]; + if (attrStr == nil) + { + attrStr = [self placeholderAttributedString]; + if (attrStr == nil) + { + NSString *string; + NSDictionary *attributes; + NSMutableDictionary *newAttribs; + + string = [self placeholderString]; + if (string == nil) + { + return nil; + } + + attributes = [self _nonAutoreleasedTypingAttributes]; + newAttribs = [NSMutableDictionary + dictionaryWithDictionary: attributes]; + [newAttribs setObject: [NSColor disabledControlTextColor] + forKey: NSForegroundColorAttributeName]; + + return AUTORELEASE([[NSAttributedString alloc] + initWithString: string + attributes: newAttribs]); + } + else + { + return attrStr; + } + } + else + { + return attrStr; + } +} + - (BOOL) isOpaque { return _textfieldcell_is_opaque; @@ -244,9 +334,9 @@ static NSColor *txtCol; [self setTextColor: textColor]; if ([aDecoder containsValueForKey: @"NSDrawsBackground"]) { - [self setDrawsBackground: [aDecoder decodeBoolForKey: - @"NSDrawsBackground"]]; - } + [self setDrawsBackground: [aDecoder decodeBoolForKey: + @"NSDrawsBackground"]]; + } } else { @@ -254,19 +344,19 @@ static NSColor *txtCol; if ([aDecoder versionForClassName:@"NSTextFieldCell"] < 2) { - /* Replace the old default _action_mask with the new default one - if it's set. There isn't really a way to modify this value - on an NSTextFieldCell encoded in a .gorm file. The old default value - causes problems with newer NSTableViews which uses this to discern - whether it should trackMouse:inRect:ofView:untilMouseUp: or not. - This also disables the action from being sent on an uneditable and - unselectable text fields. - */ - if (_action_mask == NSLeftMouseUpMask) - { - _action_mask = NSKeyUpMask | NSKeyDownMask; - } - } + /* Replace the old default _action_mask with the new default one + if it's set. There isn't really a way to modify this value + on an NSTextFieldCell encoded in a .gorm file. The old default value + causes problems with newer NSTableViews which uses this to discern + whether it should trackMouse:inRect:ofView:untilMouseUp: or not. + This also disables the action from being sent on an uneditable and + unselectable text fields. + */ + if (_action_mask == NSLeftMouseUpMask) + { + _action_mask = NSKeyUpMask | NSKeyDownMask; + } + } [aDecoder decodeValueOfObjCType: @encode(id) at: &_background_color]; [aDecoder decodeValueOfObjCType: @encode(id) at: &_text_color];