home *** CD-ROM | disk | FTP | other *** search
-
- /*
- * You may freely copy, distribute and reuse the code
- * in this example. NeXT disclaims any warranty of
- * any kind, expressed or implied, as to its fitness
- * for any particular use.
- *
- * Written by: Susan Rayl
- *
- * Created 21-Mar-91
- */
-
- #import "NameTextField.h"
- #include <appkit/Text.h>
-
- @implementation NameTextField
-
- /* WARNING! This is not an elegant solution for setting the text filter
- * function of the FieldEditor. However, it works. See the implementation for
- * the DateTextField for a preferred solution.
- */
-
- /* When the NameTextField becomes the firstResponder (i.e. The user has
- * clicked within the NameTextField or has TABbed into it due to a
- * nextText connection and is about to start typing characters into it.),
- * get the current FieldEditor and set its text filter to the nameFilter.
- * nameFilter limits the number of characters entered to 20 and also filters
- * out all non-alphabetic characters except "-", ".", " ", and ",".
- *
- * Note that this is not an elegant solution because it is not "symmetrical".
- * That is, the fieldEditor's text filter is set when the TextFeild becomes
- * firstResponder and so it should be reset in resginsFirstResponder. However,
- * since it is the FieldEditor itself which is the firstResponder
- * while the user is typing, the resignFirstResponder method of
- * the NameTextField (which would reset the text filter function to the
- * original), will be called before the user ever gets to type! So I found
- * textDidEnd:endChar: and put the stuff there. Actually, I'm not sure if it
- * really works but I'll check that some other day. How about that for a
- * comment?
- */
- - becomeFirstResponder
- {
- id theFieldEditor;
-
- /* do whatever the superclass would do */
- [super becomeFirstResponder];
-
- /* get the current FieldEditor, maintained by the Window, or create on
- * if none exist
- */
- theFieldEditor = [window getFieldEditor:YES for:self];
-
- /* save the original text filter function so that it can be restored
- * later
- */
- oldTextFilter = [theFieldEditor textFilter];
-
- /* set the text filter function of the FieldEditor to be nameFilter */
- [theFieldEditor setTextFilter:(NXTextFilterFunc)nameFilter];
- return self;
- }
-
- - selectText:sender
- {
- id theFieldEditor;
-
- /* do whatever the superclass would do */
- [super selectText:sender];
-
- /* get the current FieldEditor, maintained by the Window, or create on
- * if none exist
- */
- theFieldEditor = [window getFieldEditor:YES for:self];
-
- /* save the original text filter function so that it can be restored
- * later
- */
- oldTextFilter = [theFieldEditor textFilter];
-
- /* set the text filter function of the FieldEditor to be nameFilter */
- [theFieldEditor setTextFilter:(NXTextFilterFunc)nameFilter];
- return self;
- }
-
- - textDidEnd:anObject endChar:(unsigned short)whyEnd
- {
- /* restore original text filter function */
- [anObject setTextFilter:(NXTextFilterFunc)oldTextFilter];
-
- /* invoke a method of the Cell to clean up Text object before it is
- * removed from the View hierarchy (since firstResponder is changing).
- */
- [[self cell] endEditing:anObject];
-
- /* do whatever the superclass would do */
- [super textDidEnd:anObject endChar:whyEnd];
- return self;
- }
-
- char *nameFilter(id textObj, char *inputText, int *inputLength, int position)
- {
- char temp[] = "";
-
- /* allow 20 or less characters only */
- if (position >= 20) {
- *inputLength = 0;
- return (temp);
- }
-
- /* only allow certain characters. Replace any prohibited character
- * with a null-string, i.e. no character at all.
- */
- if (((*inputText >= 'a') && (*inputText <= 'z')) ||
- ((*inputText >= 'A') && (*inputText <= 'Z')) ||
- (*inputText == '-') ||
- (*inputText == ' ') ||
- (*inputText == ',') ||
- (*inputText == '.')) {
- return (inputText);
- } else {
- /* must return the new length of the input.
- * was 1 (one character), now 0 (a null-string)
- */
- *inputLength = 0;
- return (temp);
- }
- }
-
- @end
-