home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Utilities / FileSpy-1.1-MIHS / Finder.m < prev    next >
Encoding:
Text File  |  1996-09-22  |  3.9 KB  |  179 lines

  1. /*
  2.  *   This sourcecode is part of FileSpy, a logfile observing utility.
  3.  *   Copyright (C) 1996  Felix Rauch
  4.  *
  5.  *   This program is free software; you can redistribute it and/or modify
  6.  *   it under the terms of the GNU General Public License as published by
  7.  *   the Free Software Foundation; either version 2 of the License, or
  8.  *   (at your option) any later version.
  9.  *   
  10.  *   Notice that this program may not be possessed, used, copied,
  11.  *   distributed or modified by people having to do with nuclear
  12.  *   weapons. See the file CONDITIONS for details.
  13.  *
  14.  *   This program is distributed in the hope that it will be useful,
  15.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  *   GNU General Public License for more details.
  18.  *
  19.  *   You should have received a copy of the GNU General Public License
  20.  *   along with this program; if not, write to the Free Software
  21.  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  *
  23.  *   To contact the original author, try:
  24.  *   e-mail: Felix.Rauch@nice.ch
  25.  *   Traditional mail:    Felix Rauch
  26.  *            Sempacherstrasse 33
  27.  *            8032 Zurich
  28.  *            Switzerland
  29.  */
  30.  
  31. #import "Finder.h"
  32.  
  33. @implementation Finder
  34.  
  35. - init
  36. {
  37.     [super init];
  38.  
  39.     ignoreCase = YES;
  40.     scopeAll = NO;
  41.     if(!findPanel) {
  42.     if(![NXApp loadNibSection:"Findpanel.nib" owner:self withNames:NO fromZone:[self zone]]) {
  43.         NXLogError("Can't open Preferences.nib!");
  44.     }
  45.     }
  46.     return self;
  47. }
  48.  
  49. - setDelegate:sender
  50. {
  51.     delegate = sender;
  52.     return self;
  53. }
  54.  
  55. - caseClick:sender
  56. {
  57.     ignoreCase = [sender state] ? YES : NO;
  58.     return self;
  59. }
  60.  
  61. - findNext:sender
  62. {
  63.     [self findText:NO];
  64.     return self;
  65. }
  66.  
  67. - findPrevious:sender
  68. {
  69.     [self findText:YES];
  70.     return self;
  71. }
  72.  
  73. - findText:(BOOL)backwardsflag
  74. {
  75.     NXSelPt start, end;
  76.     unsigned int i, max;
  77.     const id textList = [delegate findTexts:scopeAll];
  78.     BOOL succesful = NO;
  79.  
  80.     if(textList == nil) {
  81.     NXBeep();
  82.     return self;
  83.     }
  84.     max = [textList count];
  85.     for(i = 0; i < max; i++) {
  86.     [[textList objectAt:i] getSel:&start :&end];
  87.     if(!backwardsflag)
  88.         [[textList objectAt:i] setSel:end.cp :end.cp];    // search from end of last selection
  89.     else
  90.         [[textList objectAt:i] setSel:start.cp :start.cp];    // search from start of last sel. backwards
  91.     if([[textList objectAt:i] findText:[myFormCell stringValue]
  92.                 ignoreCase:ignoreCase
  93.                 backwards: backwardsflag
  94.                 wrap: YES] == YES) {
  95.         [[[[textList objectAt:i] delegate] window] orderFront:self];
  96.         succesful = YES;
  97.         break;
  98.     }
  99.     }
  100.     [textList free];
  101.     if(!succesful) {
  102.     NXBeep();
  103.     [findStatusText setStringValue:"Not found"];
  104.     [findForm selectTextAt:0];
  105.     } else {
  106.     [findStatusText setStringValue:""];
  107.     }
  108.     return self;
  109. }
  110.  
  111. - scopeClick:sender
  112. {
  113.     scopeAll = ([[sender selectedCell] tag] == 0) ? NO : YES;
  114.     return self;
  115. }
  116.  
  117. - showFindPanel:sender
  118. {
  119.     [findStatusText setStringValue:""];
  120.     [findPanel makeKeyAndOrderFront:self];
  121.     return self;
  122. }
  123.  
  124. - window
  125. {
  126.     return myWindow;
  127. }
  128.  
  129. - formCell
  130. {
  131.     return myFormCell;
  132. }
  133.  
  134. - windowDidBecomeKey:sender
  135. {
  136.     [findForm selectTextAt:0];
  137.     return self;
  138. }
  139.  
  140. /* taken from Yapp's FindPanel.m */
  141.  
  142. // In case the selection is shorter than this amount, we use a local buffer 
  143. // (on the stack) rather than bothering with allocating it.
  144.  
  145. #define LOCALBUFFERLEN 100
  146.  
  147. - enterSelection:sender
  148. {
  149.     id obj = [self firstResponderText];
  150.  
  151.     if (!obj) {
  152.     NXBeep();
  153.     } else {
  154.     char localBuffer[LOCALBUFFERLEN+1], *str;
  155.     NXSelPt from, to;
  156.     int bufLen; 
  157.     
  158.     [obj getSel:&from :&to];
  159.     bufLen = to.cp - from.cp;
  160.     str = (bufLen <= LOCALBUFFERLEN) ? localBuffer : NXZoneMalloc(NXDefaultMallocZone(), bufLen+1);
  161.         
  162.     [obj getSubstring:str start:from.cp length:bufLen];
  163.     str[bufLen] = '\0';
  164.     [myFormCell setStringValue:str];
  165.     if (str != localBuffer)
  166.         free(str);
  167.     }
  168.  
  169.     return self;
  170. }
  171.  
  172. - firstResponderText
  173. {
  174.     id obj = [[NXApp mainWindow] firstResponder];
  175.     return (obj && [obj isKindOf:[Text class]]) ? obj : nil;
  176. }
  177.  
  178. @end
  179.