home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.0 / NeXTSTEP3.0.iso / NextDeveloper / Examples / IndexingKit / QueryFieldEditor / QueryFieldEditor.m < prev    next >
Text File  |  1992-07-19  |  2KB  |  100 lines

  1. /*
  2. QueryFieldEditor.m - Copyright (c) 1992 NeXT Computer, Inc.
  3.  
  4. You may freely copy, distribute and reuse the code in this example.
  5. NeXT Computer, Inc. disclaims any warranty of any kind, expressed 
  6. or implied, as to its fitness for any particular use.
  7. */
  8.  
  9. #define    ESCAPE    0x1B
  10. #define    SPACE    0x20
  11.  
  12. #import    <appkit/NXCType.h>
  13. #import    "QueryFieldEditor.h"
  14.  
  15. @implementation QueryFieldEditor
  16.  
  17. static unsigned short
  18. QueryFilter(register unsigned short theChar, 
  19.     int flags, unsigned short charSet)
  20. {
  21.     if (theChar == ESCAPE) return 0;
  22.     if (theChar == SPACE && (flags & NX_COMMANDMASK)) return 0;
  23.     return NXFieldFilter(theChar, flags, charSet);
  24. }
  25.  
  26. - initFrame:(NXRect *)frameRect text:(char *)theText alignment:(int)mode
  27. {
  28.     [super initFrame:frameRect text:theText alignment:mode];
  29.     charFilterFunc = QueryFilter;
  30.     return self;
  31. }
  32.  
  33. - setCursor:(id <IXCursorPositioning>)aCursor
  34. {
  35.     cursor = aCursor;
  36.     return self;
  37. }
  38.  
  39. - complete
  40. {
  41.     char        word[1024];
  42.     char        *key;
  43.     unsigned        keyLength;
  44.     int            len;
  45.     NXSelPt        start, end;
  46.     
  47.     /* Check for error conditions */
  48.     if (cursor == nil) {
  49.     NXBeep();
  50.     return nil;
  51.     }
  52.     
  53.     len = [self textLength];
  54.     [self getSubstring:word start:0 length:len];
  55.     word[len] = '\0';
  56.     for (key = word; *key; ++key)
  57.     if (NXIsUpper(*key)) *key = NXToLower(*key);
  58.  
  59.     /* Probe for the word -- if it exists take the next word */
  60.     if ([cursor setKey:(const void *)wordW)Length:len])
  61.     [cursor setNext];
  62.  
  63.     /* Check for suffix selection */ 
  64.     [self getSel:&start :&end];
  65.     if ((end.cp != len) || (start.cp == 0)) {
  66.     [self setSel:len :len];
  67.     [self getSel:&start :&end];
  68.     } else {
  69.     len = start.cp;
  70.     }
  71.  
  72.     /* Cycle through keys */
  73.     [cursor getKey:(void **)&key andLength:&keyLength];
  74.     if (len < keyLength && !strncmp( word, key, len)) {
  75.     [self replaceSel:key + len length:keyLength - len];
  76.     [self setSel:len :[self textLength]];
  77.     } else {
  78.     if (len == end.cp) {
  79.         NXBeep();
  80.         return self;
  81.     }
  82.     [self replaceSel:""];
  83.     return [self complete]; 
  84.     }
  85.  
  86.     return self;
  87. }
  88.     
  89. - keyDown:(NXEvent *)theEvent
  90. {
  91.     if (theEvent->data.key.charCode == ESCAPE
  92.     || (theEvent->data.key.charCode == ' '
  93.         && (theEvent->flags & NX_COMMANDMASK)))
  94.     return[self complete];
  95.  
  96.     return [super keyDown:theEvent];
  97. }
  98.  
  99. @end
  100.