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:
nico 2000-08-29 18:42:49 +00:00
parent db719fa1b3
commit b154c6cf05
2 changed files with 210 additions and 16 deletions

View file

@ -50,6 +50,7 @@
#include <AppKit/NSApplication.h> #include <AppKit/NSApplication.h>
#include <AppKit/NSMatrix.h> #include <AppKit/NSMatrix.h>
static NSNotificationCenter *nc;
#define STRICT 0 #define STRICT 0
@ -146,6 +147,8 @@ static SEL getSel = @selector(objectAtIndex:);
* MacOS-X docs say default cell class is NSActionCell * MacOS-X docs say default cell class is NSActionCell
*/ */
defaultCellClass = [NSActionCell class]; defaultCellClass = [NSActionCell class];
//
nc = [NSNotificationCenter defaultCenter];
} }
} }
@ -1278,7 +1281,6 @@ static SEL getSel = @selector(objectAtIndex:);
- (void) textDidBeginEditing: (NSNotification *)aNotification - (void) textDidBeginEditing: (NSNotification *)aNotification
{ {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSMutableDictionary *d; NSMutableDictionary *d;
d = [[NSMutableDictionary alloc] initWithDictionary: d = [[NSMutableDictionary alloc] initWithDictionary:
@ -1291,9 +1293,8 @@ static SEL getSel = @selector(objectAtIndex:);
- (void) textDidChange: (NSNotification *)aNotification - (void) textDidChange: (NSNotification *)aNotification
{ {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSMutableDictionary *d; NSMutableDictionary *d;
NSFormatter *formatter;
// MacOS-X asks us to inform the cell if possible. // MacOS-X asks us to inform the cell if possible.
if ((_selectedCell != nil) && [_selectedCell respondsToSelector: if ((_selectedCell != nil) && [_selectedCell respondsToSelector:
@ -1307,11 +1308,52 @@ static SEL getSel = @selector(objectAtIndex:);
[nc postNotificationName: NSControlTextDidChangeNotification [nc postNotificationName: NSControlTextDidChangeNotification
object: self object: self
userInfo: d]; 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 - (void) textDidEndEditing: (NSNotification *)aNotification
{ {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSMutableDictionary *d; NSMutableDictionary *d;
id textMovement; 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 // In all other cases
return YES; return YES;
} }
@ -1411,7 +1471,7 @@ static SEL getSel = @selector(objectAtIndex:);
- (void) setValidateSize: (BOOL)flag - (void) setValidateSize: (BOOL)flag
{ {
// TODO // TODO
} }
- (void) sizeToCells - (void) sizeToCells
@ -1508,6 +1568,16 @@ static SEL getSel = @selector(objectAtIndex:);
return drawsBackground; 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 - (void) drawCellAtRow: (int)row column: (int)column
{ {
NSCell *aCell = [self cellAtRow: row column: column]; NSCell *aCell = [self cellAtRow: row column: column];
@ -2233,8 +2303,6 @@ static SEL getSel = @selector(objectAtIndex:);
- (void) setDelegate: (id)object - (void) setDelegate: (id)object
{ {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
if (_delegate) if (_delegate)
[nc removeObserver: _delegate name: nil object: self]; [nc removeObserver: _delegate name: nil object: self];
_delegate = object; _delegate = object;
@ -2514,8 +2582,41 @@ static SEL getSel = @selector(objectAtIndex:);
- (void) validateEditing - (void) validateEditing
{ {
if (_textObject) if (_textObject)
[_selectedCell setStringValue: [_textObject text]]; {
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 @end

View file

@ -32,6 +32,7 @@
#include <gnustep/gui/config.h> #include <gnustep/gui/config.h>
#include <Foundation/NSFormatter.h>
#include <Foundation/NSNotification.h> #include <Foundation/NSNotification.h>
#include <Foundation/NSString.h> #include <Foundation/NSString.h>
@ -41,6 +42,8 @@
#include <AppKit/NSTextFieldCell.h> #include <AppKit/NSTextFieldCell.h>
#include <AppKit/NSWindow.h> #include <AppKit/NSWindow.h>
static NSNotificationCenter *nc;
@implementation NSTextField @implementation NSTextField
// //
// Class methods // Class methods
@ -51,6 +54,7 @@
{ {
[self setVersion: 1]; [self setVersion: 1];
[self setCellClass: [NSTextFieldCell class]]; [self setCellClass: [NSTextFieldCell class]];
nc = [NSNotificationCenter defaultCenter];
} }
} }
@ -168,8 +172,6 @@
// //
- (void) setDelegate: (id)anObject - (void) setDelegate: (id)anObject
{ {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
if (_delegate) if (_delegate)
[nc removeObserver: _delegate name: nil object: self]; [nc removeObserver: _delegate name: nil object: self];
_delegate = anObject; _delegate = anObject;
@ -331,12 +333,44 @@
- (void) validateEditing - (void) validateEditing
{ {
if (_text_object) 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 - (void) textDidBeginEditing: (NSNotification *)aNotification
{ {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSDictionary *d; NSDictionary *d;
d = [NSDictionary dictionaryWithObject:[aNotification object] d = [NSDictionary dictionaryWithObject:[aNotification object]
@ -348,19 +382,60 @@
- (void) textDidChange: (NSNotification *)aNotification - (void) textDidChange: (NSNotification *)aNotification
{ {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSDictionary *d; NSDictionary *d;
NSFormatter *formatter;
d = [NSDictionary dictionaryWithObject: [aNotification object] d = [NSDictionary dictionaryWithObject: [aNotification object]
forKey: @"NSFieldEditor"]; forKey: @"NSFieldEditor"];
[nc postNotificationName: NSControlTextDidChangeNotification [nc postNotificationName: NSControlTextDidChangeNotification
object: self object: self
userInfo: d]; 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 - (void) textDidEndEditing: (NSNotification *)aNotification
{ {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSDictionary *d; NSDictionary *d;
id textMovement; 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 // In all other cases
return YES; return YES;
} }