Annotation of Examples/AppKit/Yap/FindPanel.m, revision 1.1

1.1     ! root        1: // FindPanel.m
        !             2: //
        !             3: // Subclass of Object which implements find panel functionality.
        !             4: // Works on Text objects automatically; pretty easily can be
        !             5: // made to work on any text container which responds to a given protocol.
        !             6: //
        !             7: // You may freely copy, distribute, and reuse the code in this example.
        !             8: // NeXT disclaims any warranty of any kind, expressed or  implied, as to its
        !             9: // fitness for any particular use.
        !            10: 
        !            11: #import <appkit/appkit.h>
        !            12: #import <libc.h>
        !            13: #import <strings.h>
        !            14: 
        !            15: #import "FindPanel.h"
        !            16: 
        !            17: @implementation FindPanel
        !            18: 
        !            19: // Allocate and initialize the object. If we already created an instance, return that one...
        !            20: 
        !            21: 
        !            22: + sharedInstance
        !            23: {
        !            24:     static id sharedFindObject = nil;
        !            25:     NXZone *zone = NXApp ? [NXApp zone] : NXDefaultMallocZone();
        !            26: 
        !            27:     if (sharedFindObject) {
        !            28:         return sharedFindObject;
        !            29:     }
        !            30:     
        !            31:     self = [[super allocFromZone:zone] init];
        !            32:     if (![NXApp loadNibSection:"FindPanel.nib" owner:self withNames:NO fromZone:zone]) {
        !            33:        NXLogError ("Can't find FindPanel.nib!");
        !            34:        [self free];
        !            35:        return nil;
        !            36:     } else {
        !            37:        sharedFindObject = self;
        !            38:        [[findText window] setFrameAutosaveName:"Find"];
        !            39:        return self;
        !            40:     }
        !            41: }
        !            42: 
        !            43: + new
        !            44: {
        !            45:     return [self sharedInstance];
        !            46: }
        !            47: 
        !            48: // Because find panel is shared, only one instance is created, and it should be created 
        !            49: // with new rather than alloc/init. 
        !            50: 
        !            51: - allocFromZone:(NXZone *)zone
        !            52: {
        !            53:     return [self notImplemented:_cmd];
        !            54: }
        !            55: 
        !            56: - free
        !            57: {
        !            58:     return self;
        !            59: }
        !            60: 
        !            61: - (const char *)findString
        !            62: {
        !            63:     return [findText stringValue];
        !            64: }
        !            65: 
        !            66: - (void)setFindString:(const char *)string
        !            67: {
        !            68:     [findText setStringValue:string];
        !            69:     [findText selectText:nil];
        !            70: }
        !            71: 
        !            72: - (void)findIn:(Text *)textObject direction:(FindDirection)dir
        !            73: {
        !            74:     if (!textObject ||
        !            75:        ![textObject findText:[self findString]
        !            76:                   ignoreCase:[ignoreCaseButton state]
        !            77:                    backwards:(dir == FindBackward)
        !            78:                         wrap:YES]) {
        !            79:        NXBeep ();
        !            80:        return;
        !            81:     }    
        !            82: }
        !            83: 
        !            84: - firstResponderText
        !            85: {
        !            86:     id obj = [[NXApp mainWindow] firstResponder];
        !            87:     return (obj && [obj isKindOf:[Text class]]) ? obj : nil;
        !            88: }
        !            89: 
        !            90: - (void)findDirection:(FindDirection)dir
        !            91: {
        !            92:     [self findIn:[self firstResponderText] direction:dir];
        !            93: }
        !            94: 
        !            95: - findNextReturn:sender
        !            96: {
        !            97:     [findNextButton performClick:sender];
        !            98:     [self orderOut:sender];
        !            99:     return self;
        !           100: }
        !           101: 
        !           102: - makeKeyAndOrderFront:sender
        !           103: {
        !           104:     id retval = [[findText window] makeKeyAndOrderFront:sender];
        !           105:     [findText selectText:nil];
        !           106:     return retval;
        !           107: }
        !           108: 
        !           109: - orderOut:sender
        !           110: {
        !           111:     return [[findText window] orderOut:sender];
        !           112: }
        !           113: 
        !           114: // Called to find the next occurrence of the search string.
        !           115: 
        !           116: - findNext:sender
        !           117: {
        !           118:     [self findDirection:FindForward];
        !           119:     return self;
        !           120: }
        !           121: 
        !           122: // Called to find the previous occurrence of the search string.
        !           123: 
        !           124: - findPrevious:sender
        !           125: {
        !           126:     [self findDirection:FindBackward];
        !           127:     return self;
        !           128: }
        !           129: 
        !           130: // In case the selection is shorter than this amount, we use a local buffer 
        !           131: // (on the stack) rather than bothering with allocating it.
        !           132: 
        !           133: #define LOCALBUFFERLEN 100
        !           134: 
        !           135: - enterSelection:sender
        !           136: {
        !           137:     id obj = [self firstResponderText];
        !           138: 
        !           139:     if (!obj) {
        !           140:        NXBeep();
        !           141:     } else {
        !           142:        char localBuffer[LOCALBUFFERLEN+1], *str;
        !           143:        NXSelPt from, to;
        !           144:        int bufLen; 
        !           145:     
        !           146:        [obj getSel:&from :&to];
        !           147:        bufLen = to.cp - from.cp;
        !           148:        str = (bufLen <= LOCALBUFFERLEN) ? localBuffer : NXZoneMalloc(NXDefaultMallocZone(), bufLen+1);
        !           149:            
        !           150:        [obj getSubstring:str start:from.cp length:bufLen];
        !           151:        str[bufLen] = '\0';
        !           152:        [self setFindString:str];
        !           153:        if (str != localBuffer) free(str);
        !           154:     }
        !           155: 
        !           156:     return self;
        !           157: }
        !           158: 
        !           159: @end

unix.superglobalmegacorp.com

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