Implemented object value/formatter support for editing

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@7283 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Nicola Pero 2000-08-29 18:42:49 +00:00
parent 6768da6356
commit c96a2cd4db
2 changed files with 210 additions and 16 deletions

View file

@ -50,6 +50,7 @@
#include <AppKit/NSApplication.h>
#include <AppKit/NSMatrix.h>
static NSNotificationCenter *nc;
#define STRICT 0
@ -146,6 +147,8 @@ static SEL getSel = @selector(objectAtIndex:);
* MacOS-X docs say default cell class is NSActionCell
*/
defaultCellClass = [NSActionCell class];
//
nc = [NSNotificationCenter defaultCenter];
}
}
@ -1278,7 +1281,6 @@ static SEL getSel = @selector(objectAtIndex:);
- (void) textDidBeginEditing: (NSNotification *)aNotification
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSMutableDictionary *d;
d = [[NSMutableDictionary alloc] initWithDictionary:
@ -1291,9 +1293,8 @@ static SEL getSel = @selector(objectAtIndex:);
- (void) textDidChange: (NSNotification *)aNotification
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSMutableDictionary *d;
NSFormatter *formatter;
// MacOS-X asks us to inform the cell if possible.
if ((_selectedCell != nil) && [_selectedCell respondsToSelector:
@ -1307,11 +1308,52 @@ static SEL getSel = @selector(objectAtIndex:);
[nc postNotificationName: NSControlTextDidChangeNotification
object: self
userInfo: d];
formatter = [_cell formatter];
if (formatter != nil)
{
/*
* FIXME: This part needs heavy interaction with the yet to finish
* text system.
*
*/
NSString *partialString;
NSString *newString = nil;
NSString *error = nil;
BOOL wasAccepted;
partialString = [_textObject string];
wasAccepted = [formatter isPartialStringValid: partialString
newEditingString: &newString
errorDescription: &error];
if (wasAccepted == NO)
{
[_delegate control:self
didFailToValidatePartialString: partialString
errorDescription: error];
}
if (newString != nil)
{
NSLog (@"Unimplemented: should set string to %@", newString);
// FIXME ! This would reset editing !
//[_textObject setString: newString];
}
else
{
if (wasAccepted == NO)
{
// FIXME: Need to delete last typed character (?!)
NSLog (@"Unimplemented: should delete last typed character");
}
}
}
}
- (void) textDidEndEditing: (NSNotification *)aNotification
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSMutableDictionary *d;
id textMovement;
@ -1385,6 +1427,24 @@ static SEL getSel = @selector(objectAtIndex:);
}
}
if ([_delegate respondsToSelector:
@selector(control:isValidObject:)] == YES)
{
NSFormatter *formatter;
id newObjectValue;
formatter = [_cell formatter];
if ([formatter getObjectValue: &newObjectValue
forString: [_textObject text]
errorDescription: NULL] == YES)
{
if ([_delegate control: self
isValidObject: newObjectValue] == NO)
return NO;
}
}
// In all other cases
return YES;
}
@ -1411,7 +1471,7 @@ static SEL getSel = @selector(objectAtIndex:);
- (void) setValidateSize: (BOOL)flag
{
// TODO
// TODO
}
- (void) sizeToCells
@ -1508,6 +1568,16 @@ static SEL getSel = @selector(objectAtIndex:);
return drawsBackground;
}
- (void) drawCell: (NSCell *)aCell
{
int row, column;
if ([self getRow: &row column: &column ofCell: aCell] == YES)
{
[self drawCellAtRow: row column: column];
}
}
- (void) drawCellAtRow: (int)row column: (int)column
{
NSCell *aCell = [self cellAtRow: row column: column];
@ -2233,8 +2303,6 @@ static SEL getSel = @selector(objectAtIndex:);
- (void) setDelegate: (id)object
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
if (_delegate)
[nc removeObserver: _delegate name: nil object: self];
_delegate = object;
@ -2514,8 +2582,41 @@ static SEL getSel = @selector(objectAtIndex:);
- (void) validateEditing
{
if (_textObject)
[_selectedCell setStringValue: [_textObject text]];
if (_textObject)
{
NSFormatter *formatter;
NSString *string;
formatter = [_selectedCell formatter];
string = [_textObject text];
if (formatter == nil)
{
[_selectedCell setStringValue: string];
}
else
{
id newObjectValue;
NSString *error;
if ([formatter getObjectValue: &newObjectValue
forString: string
errorDescription: &error] == YES)
{
[_selectedCell setObjectValue: newObjectValue];
}
else
{
if ([_delegate control: self
didFailToFormatString: string
errorDescription: error] == YES)
{
[_selectedCell setStringValue: string];
}
}
}
}
}
@end

View file

@ -32,6 +32,7 @@
#include <gnustep/gui/config.h>
#include <Foundation/NSFormatter.h>
#include <Foundation/NSNotification.h>
#include <Foundation/NSString.h>
@ -41,6 +42,8 @@
#include <AppKit/NSTextFieldCell.h>
#include <AppKit/NSWindow.h>
static NSNotificationCenter *nc;
@implementation NSTextField
//
// Class methods
@ -51,6 +54,7 @@
{
[self setVersion: 1];
[self setCellClass: [NSTextFieldCell class]];
nc = [NSNotificationCenter defaultCenter];
}
}
@ -168,8 +172,6 @@
//
- (void) setDelegate: (id)anObject
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
if (_delegate)
[nc removeObserver: _delegate name: nil object: self];
_delegate = anObject;
@ -331,12 +333,44 @@
- (void) validateEditing
{
if (_text_object)
[_cell setStringValue: [_text_object text]];
{
NSFormatter *formatter;
NSString *string;
formatter = [_cell formatter];
string = [_text_object text];
if (formatter == nil)
{
[_cell setStringValue: string];
}
else
{
id newObjectValue;
NSString *error;
if ([formatter getObjectValue: &newObjectValue
forString: string
errorDescription: &error] == YES)
{
[_cell setObjectValue: newObjectValue];
}
else
{
if ([_delegate control: self
didFailToFormatString: string
errorDescription: error] == YES)
{
[_cell setStringValue: string];
}
}
}
}
}
- (void) textDidBeginEditing: (NSNotification *)aNotification
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSDictionary *d;
d = [NSDictionary dictionaryWithObject:[aNotification object]
@ -348,19 +382,60 @@
- (void) textDidChange: (NSNotification *)aNotification
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSDictionary *d;
NSFormatter *formatter;
d = [NSDictionary dictionaryWithObject: [aNotification object]
forKey: @"NSFieldEditor"];
[nc postNotificationName: NSControlTextDidChangeNotification
object: self
userInfo: d];
formatter = [_cell formatter];
if (formatter != nil)
{
/*
* FIXME: This part needs heavy interaction with the yet to finish
* text system.
*
*/
NSString *partialString;
NSString *newString = nil;
NSString *error = nil;
BOOL wasAccepted;
partialString = [_text_object string];
wasAccepted = [formatter isPartialStringValid: partialString
newEditingString: &newString
errorDescription: &error];
if (wasAccepted == NO)
{
[_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];
}
else
{
if (wasAccepted == NO)
{
// FIXME: Need to delete last typed character (?!)
NSLog (@"Unimplemented: should delete last typed character");
}
}
}
}
- (void) textDidEndEditing: (NSNotification *)aNotification
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSDictionary *d;
id textMovement;
@ -425,6 +500,24 @@
}
}
if ([_delegate respondsToSelector:
@selector(control:isValidObject:)] == YES)
{
NSFormatter *formatter;
id newObjectValue;
formatter = [_cell formatter];
if ([formatter getObjectValue: &newObjectValue
forString: [_text_object text]
errorDescription: NULL] == YES)
{
if ([_delegate control: self
isValidObject: newObjectValue] == NO)
return NO;
}
}
// In all other cases
return YES;
}