home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 (1993) / nebula.bin / SourceCode / MiniExamples / FilterFunctions / NameTextField.m < prev    next >
Encoding:
Text File  |  1991-09-29  |  3.9 KB  |  130 lines

  1.  
  2. /*
  3.  *      You may freely copy, distribute and reuse the code
  4.  *      in this example.  NeXT disclaims any warranty of
  5.  *      any kind, expressed or implied, as to its fitness
  6.  *      for any particular use.
  7.  *
  8.  *      Written by: Susan Rayl
  9.  *
  10.  *      Created 21-Mar-91
  11.  */
  12.  
  13. #import "NameTextField.h"
  14. #include <appkit/Text.h>
  15.  
  16. @implementation NameTextField
  17.  
  18. /* WARNING!  This is not an elegant solution for setting the text filter
  19.  * function of the FieldEditor.  However, it works.  See the implementation for 
  20.  * the DateTextField for a preferred solution.
  21.  */
  22.  
  23. /* When the NameTextField becomes the firstResponder (i.e. The user has 
  24.  * clicked within the NameTextField or has TABbed into it due to a 
  25.  * nextText connection and is about to start typing characters into it.), 
  26.  * get the current FieldEditor and set its text filter to the nameFilter.  
  27.  * nameFilter limits the number of characters entered to 20 and also filters 
  28.  * out all non-alphabetic characters except "-", ".", " ", and ",".  
  29.  *
  30.  * Note that this is not an elegant solution because it is not "symmetrical".  
  31.  * That is, the fieldEditor's text filter is set when the TextFeild becomes 
  32.  * firstResponder and so it should be reset in resginsFirstResponder.  However, 
  33.  * since it is the FieldEditor itself which is the firstResponder
  34.  * while the user is typing, the resignFirstResponder method of 
  35.  * the NameTextField (which would reset the text filter function to the 
  36.  * original), will be called before the user ever gets to type!  So I found 
  37.  * textDidEnd:endChar: and put the stuff there.  Actually, I'm not sure if it 
  38.  * really works but I'll check that some other day.  How about that for a 
  39.  * comment?
  40.  */
  41. - becomeFirstResponder
  42. {
  43.     id theFieldEditor;
  44.     
  45.     /* do whatever the superclass would do */
  46.     [super becomeFirstResponder];
  47.     
  48.     /* get the current FieldEditor, maintained by the Window, or create on 
  49.      * if none exist
  50.      */
  51.     theFieldEditor = [window getFieldEditor:YES for:self];
  52.     
  53.     /* save the original text filter function so that it can be restored
  54.      * later
  55.      */
  56.     oldTextFilter = [theFieldEditor textFilter];
  57.     
  58.     /* set the text filter function of the FieldEditor to be nameFilter */
  59.     [theFieldEditor setTextFilter:(NXTextFilterFunc)nameFilter];
  60.     return self;
  61. }
  62.  
  63. - selectText:sender
  64. {
  65.     id theFieldEditor;
  66.     
  67.     /* do whatever the superclass would do */
  68.     [super selectText:sender];
  69.     
  70.     /* get the current FieldEditor, maintained by the Window, or create on 
  71.      * if none exist
  72.      */
  73.     theFieldEditor = [window getFieldEditor:YES for:self];
  74.     
  75.     /* save the original text filter function so that it can be restored
  76.      * later
  77.      */
  78.     oldTextFilter = [theFieldEditor textFilter];
  79.     
  80.     /* set the text filter function of the FieldEditor to be nameFilter */
  81.     [theFieldEditor setTextFilter:(NXTextFilterFunc)nameFilter];
  82.     return self;
  83. }
  84.  
  85. - textDidEnd:anObject endChar:(unsigned short)whyEnd
  86. {
  87.     /* restore original text filter function */
  88.     [anObject setTextFilter:(NXTextFilterFunc)oldTextFilter];
  89.     
  90.     /* invoke a method of the Cell to clean up Text object before it is 
  91.      * removed from the View hierarchy (since firstResponder is changing).
  92.      */
  93.     [[self cell] endEditing:anObject];
  94.     
  95.     /* do whatever the superclass would do */
  96.     [super textDidEnd:anObject endChar:whyEnd];
  97.     return self;
  98. }
  99.  
  100. char *nameFilter(id textObj, char *inputText, int *inputLength, int position)
  101. {
  102.     char temp[] = "";
  103.     
  104.     /* allow 20 or less characters only */
  105.     if (position >= 20) {
  106.         *inputLength = 0;
  107.         return (temp);
  108.     }
  109.     
  110.     /* only allow certain characters.  Replace any prohibited character 
  111.      * with a null-string, i.e. no character at all. 
  112.      */
  113.     if (((*inputText >= 'a') && (*inputText <= 'z')) ||
  114.         ((*inputText >= 'A') && (*inputText <= 'Z')) ||
  115.         (*inputText == '-') ||
  116.         (*inputText == ' ') ||
  117.         (*inputText == ',') ||
  118.         (*inputText == '.')) {
  119.            return (inputText);
  120.     } else {
  121.         /* must return the new length of the input.
  122.          * was 1 (one character), now 0 (a null-string)
  123.          */
  124.         *inputLength = 0;
  125.         return (temp);
  126.     }
  127. }
  128.  
  129. @end
  130.