Adding modifications for NSSecureTextField. This is a partial

implementation which works at the same level as OPENSTEP 4.2.
The methods necessary to echo bullets to the screen will be
added later. (GJC)


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@8021 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Gregory John Casamento 2000-11-04 06:58:29 +00:00
parent 38098220e4
commit 6d89487694

View file

@ -5,9 +5,12 @@
Copyright (C) 1999 Free Software Foundation, Inc. Copyright (C) 1999 Free Software Foundation, Inc.
Author: Lyndon Tremblay <humasect@coolmail.com> Original Author: Lyndon Tremblay <humasect@coolmail.com>
Date: 1999 Date: 1999
Rewrite by: Gregory John Casamento <borgheron@yahoo.com>
Date: 2000
This file is part of the GNUstep GUI Library. This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
@ -34,10 +37,25 @@
// //
#include <gnustep/gui/config.h> #include <gnustep/gui/config.h>
#include <AppKit/NSSecureTextField.h> #include <AppKit/NSSecureTextField.h>
#include <AppKit/NSImage.h> #include <AppKit/NSImage.h>
#include <AppKit/NSFont.h> #include <AppKit/NSFont.h>
#include <AppKit/NSTextView.h>
#include <AppKit/NSLayoutManager.h>
#include <AppKit/NSWindow.h>
#include <AppKit/NSEvent.h>
#include "GSSimpleLayoutManager.h"
/* Other secure subclasses */
@interface NSSecureTextView : NSTextView
{
}
@end
@interface GSSimpleSecureLayoutManager : GSSimpleLayoutManager
{
}
@end
@implementation NSSecureTextField @implementation NSSecureTextField
@ -50,10 +68,32 @@
{ {
if (self == [NSSecureTextField class]) { if (self == [NSSecureTextField class]) {
[self setVersion:1]; [self setVersion:1];
[self setCellClass: [NSSecureTextFieldCell class]];
} }
} }
/*
============
+ cellClass
============
*/
+ (Class) cellClass
{
// Hard code here to make sure no other class can be used.
return [NSSecureTextFieldCell class];
}
/*
============
+ setCellClass
============
*/
+ (void) setCellClass: (Class)factoryId
{
// Ward off interlopers with a stern message.
[NSException raise: NSInvalidArgumentException
format: @"NSSecureTextField can only use NSSecureTextFieldCells in order to ensure security."];
}
/* /*
============== ==============
-initWithFrame: -initWithFrame:
@ -69,19 +109,28 @@
/* /*
============== ==============
-isSelectable: -initWithCoder:
============== ==============
*/ */
- (BOOL)isSelectable - (id) initWithCoder:(NSCoder *)decoder
{ {
return NO; BOOL echosBullets = YES;
[super initWithCoder: decoder];
[decoder decodeValueOfObjCType: @encode(BOOL) at: &echosBullets];
[_cell setEchosBullets: echosBullets];
return self;
} }
- (void) textDidEndEditing: (NSNotification *)aNotification
{
[_text_object setText: [[aNotification object] text]];
[super textDidEndEditing: aNotification];
}
@end /* NSSecureTextField */ @end /* NSSecureTextField */
@implementation NSSecureTextFieldCell @implementation NSSecureTextFieldCell
/* /*
============== ==============
+initialize +initialize
@ -94,17 +143,22 @@
} }
/* /*
============== ==================================
-copyWithZone: +_sharedSecureFieldEditorInstance
============== ==================================
*/ */
- (id)copyWithZone:(NSZone *)zone + (id)_sharedSecureFieldEditorInstance
{ {
//Prevent the cell's text value from being copied. static NSSecureTextView *secureView = nil;
NSSecureTextFieldCell *c = [super copyWithZone:zone];
[c setStringValue:@""]; if( secureView == nil )
[c setEchosBullets:i_echosBullets]; {
return c; secureView = [[NSSecureTextView alloc] init];
[secureView setFieldEditor: YES];
[secureView setText: @""];
}
return secureView;
} }
/* /*
@ -127,25 +181,6 @@
i_echosBullets = flag; i_echosBullets = flag;
} }
/*
===============
-setSelectable:
===============
*/
- (void)setSelectable:(BOOL)flag
{
_cell.is_selectable = NO;
}
/*
=============
-selectText:
=============
*/
- (void)selectText:(id)sender
{
}
/* /*
=============== ===============
-drawInteriorWithFrame: -drawInteriorWithFrame:
@ -153,45 +188,101 @@
*/ */
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{ {
NSString *string = [self stringValue]; // do nothing....
unsigned length = [string length];
[self setType:NSNullCellType];
[super drawInteriorWithFrame:cellFrame inView:controlView];
[self setType:NSTextCellType];
if (length && i_echosBullets) {
NSImage *image = [NSImage imageNamed:@"common_Diamond"];
NSSize size = [image size];
NSPoint position;
float stringWidth;
stringWidth = [[self font] widthOfString:string];
switch ([self alignment]) {
case NSRightTextAlignment:
position.x = (NSWidth(cellFrame) - (stringWidth)) - 1;
break;
case NSCenterTextAlignment:
position.x = NSMidX(cellFrame) - (stringWidth/2);
break;
default:
position.x = 1;
break;
} }
position.y = MAX(NSMidY(cellFrame) - (size.height/2), 0); /*
if ([controlView isFlipped]) * Editing Text
position.y += size.height; */
- (void) editWithFrame: (NSRect)aRect
inView: (NSView*)controlView
editor: (NSText*)textObject
delegate: (id)anObject
event: (NSEvent*)theEvent
{
id l_editor = [NSSecureTextFieldCell _sharedSecureFieldEditorInstance];
while (--length) { [l_editor setText: [textObject text]];
[image compositeToPoint:position operation:NSCompositeCopy]; [super editWithFrame: aRect
position.x += size.width+1; inView: controlView
} editor: l_editor
} delegate: anObject
//[self _drawText:stars inFrame:cellFrame]; event: theEvent];
} }
- (void) selectWithFrame: (NSRect)aRect
inView: (NSView*)controlView
editor: (NSText*)textObject
delegate: (id)anObject
start: (int)selStart
length: (int)selLength
{
id l_editor = [NSSecureTextFieldCell _sharedSecureFieldEditorInstance];
[l_editor setText: [textObject text]];
[super selectWithFrame: aRect
inView: controlView
editor: l_editor
delegate: anObject
start: selStart
length: selLength];
}
@end
@implementation GSSimpleSecureLayoutManager
- (void) drawGlyphsForGlyphRange: (NSRange)glyphRange
atPoint: (NSPoint)containerOrigin
{
// do nothing...
}
@end
@implementation NSSecureTextView
/* Class methods */
+ (void) initialize
{
if ([self class] == [NSSecureTextView class])
{
[self setVersion: 1];
[self registerForServices];
}
}
- (id) initWithFrame: (NSRect)frameRect
{
NSTextContainer *aTextContainer =
[[NSTextContainer alloc] initWithContainerSize: frameRect.size];
GSSimpleSecureLayoutManager *layoutManager = [[GSSimpleSecureLayoutManager alloc] init];
[super initWithFrame: frameRect];
[layoutManager addTextContainer: aTextContainer];
RELEASE(aTextContainer);
_textStorage = [[NSTextStorage alloc] init];
[_textStorage addLayoutManager: layoutManager];
RELEASE(layoutManager);
return [self initWithFrame: frameRect textContainer: aTextContainer];
}
- (void) copy: (id)sender
{
// Do nothing since copying from a NSSecureTextView is *not* permitted.
}
- (BOOL) writeSelectionToPasteboard: (NSPasteboard*)pboard
types: (NSArray*)types
{
/* Returns NO since the selection should never be
written to the pasteboard */
return NO;
}
- (id) validRequestorForSendType: (NSString*) sendType
returnType: (NSString*) returnType
{
/* return "nil" to indicate that no type can be sent to the pasteboard for
an object of this class */
return nil;
}
@end @end