Annotation of Examples/IndexingKit/QueryFieldEditor/QueryFieldEditor.m, revision 1.1

1.1     ! root        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 or implied, 
        !             6: 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, int flags, unsigned short charSet)
        !            19: {
        !            20:     if (theChar == ESCAPE) return 0;
        !            21:     if (theChar == SPACE && (flags & NX_COMMANDMASK)) return 0;
        !            22:     return NXFieldFilter(theChar, flags, charSet);
        !            23: }
        !            24: 
        !            25: - initFrame:(NXRect *)frameRect text:(char *)theText alignment:(int)mode
        !            26: {
        !            27:     [super initFrame:frameRect text:theText alignment:mode];
        !            28:     charFilterFunc = QueryFilter;
        !            29:     return self;
        !            30: }
        !            31: 
        !            32: - setCursor:(id <IXCursorPositioning>)aCursor
        !            33: {
        !            34:     cursor = aCursor;
        !            35:     return self;
        !            36: }
        !            37: 
        !            38: - complete
        !            39: {
        !            40:     char               word[1024];
        !            41:     char               *key;
        !            42:     unsigned           keyLength;
        !            43:     int                        len;
        !            44:     NXSelPt            start, end;
        !            45: 
        !            46:     /* Check for error conditions */
        !            47:     if (cursor == nil)
        !            48:     {
        !            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 *)word andLength:len])
        !            61:        [cursor setNext];
        !            62: 
        !            63:     /* Check for suffix selection */ 
        !            64:     [self getSel:&start :&end];
        !            65:     if ((end.cp != len) || (start.cp == 0))
        !            66:     {
        !            67:        [self setSel:len :len];
        !            68:        [self getSel:&start :&end];
        !            69:     } else
        !            70:        len = start.cp;
        !            71: 
        !            72:     /* Cycle through keys */
        !            73:     [cursor getKey:(void **)&key andLength:&keyLength];
        !            74:     if (len < keyLength && !strncmp(word, key, len))
        !            75:     {
        !            76:        [self replaceSel:key + len length:keyLength - len];
        !            77:        [self setSel:len :[self textLength]];
        !            78:     } else
        !            79:     {
        !            80:        if (len == end.cp)
        !            81:        {
        !            82:            NXBeep();
        !            83:            return self;
        !            84:        }
        !            85: 
        !            86:        [self replaceSel:""];
        !            87:        return [self complete]; 
        !            88:     }
        !            89: 
        !            90:     return self;
        !            91: }
        !            92: 
        !            93: - keyDown:(NXEvent *)theEvent
        !            94: {
        !            95:     if (theEvent->data.key.charCode == ESCAPE
        !            96:        || (theEvent->data.key.charCode == ' ' && (theEvent->flags & NX_COMMANDMASK)))
        !            97:        return[self complete];
        !            98: 
        !            99:     return [super keyDown:theEvent];
        !           100: }
        !           101: 
        !           102: @end

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.