1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
/*
TextFinder.m
Copyright (c) 1995-1996, NeXT Software, Inc.
All rights reserved.
Author: Ali Ozer
You may freely copy, distribute and reuse the code in this example.
NeXT disclaims any warranty of any kind, expressed or implied,
as to its fitness for any particular use.
Find and replace functionality with a minimal panel...
In addition to including this class and FindPanel.nib in your app, you probably need to
hook up the following action methods in your document object (or whatever object
is first responder) to call the appropriate methods in [TextFinder sharedInstance]:
orderFrontFindPanel:
findNext:
findPrevious:
enterSelection: (calls setFindString:)
*/
#import <AppKit/AppKit.h>
#import "TextFinder.h"
@implementation TextFinder
- (id)init {
if (!(self = [super init])) return nil;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidActivate:) name:NSApplicationDidBecomeActiveNotification object:[NSApplication sharedApplication]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addWillDeactivate:) name:NSApplicationWillResignActiveNotification object:[NSApplication sharedApplication]];
[self setFindString:@""];
[self loadFindStringFromPasteboard];
return self;
}
- (void)appDidActivate:(NSNotification *)notification {
[self loadFindStringFromPasteboard];
}
- (void)addWillDeactivate:(NSNotification *)notification {
[self loadFindStringToPasteboard];
}
- (void)loadFindStringFromPasteboard {
NSPasteboard *pasteboard = [NSPasteboard pasteboardWithName:NSFindPboard];
if ([[pasteboard types] containsObject:NSStringPboardType]) {
NSString *string = [pasteboard stringForType:NSStringPboardType];
if (string && [string length]) {
[self setFindString:string];
findStringChangedSinceLastPasteboardUpdate = NO;
}
}
}
- (void)loadFindStringToPasteboard {
NSPasteboard *pasteboard = [NSPasteboard pasteboardWithName:NSFindPboard];
if (findStringChangedSinceLastPasteboardUpdate) {
[pasteboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
[pasteboard setString:[self findString] forType:NSStringPboardType];
findStringChangedSinceLastPasteboardUpdate = NO;
}
}
static id sharedFindObject = nil;
+ (id)sharedInstance {
if (!sharedFindObject) {
sharedFindObject = [[self allocWithZone:[[NSApplication sharedApplication] zone]] init];
}
return sharedFindObject;
}
- (void)loadUI {
if (!findTextField) {
if (![NSBundle loadNibNamed:@"FindPanel" owner:self]) {
NSLog(@"Failed to load FindPanel.nib");
NSBeep();
}
if (self == sharedFindObject) [[findTextField window] setFrameAutosaveName:@"Find"];
}
[findTextField setStringValue:[self findString]];
}
- (void)dealloc {
if (self != sharedFindObject) {
[findString release];
[super dealloc];
}
}
- (NSString *)findString {
return findString;
}
- (void)setFindString:(NSString *)string {
if ([string isEqualToString:findString]) return;
[findString autorelease];
findString = [string copyWithZone:[self zone]];
if (findTextField) {
[findTextField setStringValue:string];
[findTextField selectText:nil];
}
findStringChangedSinceLastPasteboardUpdate = YES;
}
- (NSTextView *)textObjectToSearchIn {
id obj = [[NSApp mainWindow] firstResponder];
return (obj && [obj isKindOfClass:[NSTextView class]]) ? obj : nil;
}
- (NSPanel *)findPanel {
if (!findTextField) [self loadUI];
return (NSPanel *)[findTextField window];
}
/* The primitive for finding; this ends up setting the status field (and beeping if necessary)...
*/
- (BOOL)find:(BOOL)direction {
NSTextView *text = [self textObjectToSearchIn];
lastFindWasSuccessful = NO;
if (text) {
NSString *textContents = [text string];
unsigned textLength;
if (textContents && (textLength = [textContents length])) {
NSRange range;
unsigned options = 0;
if (direction == Backward) options |= NSBackwardsSearch;
if ([ignoreCaseButton state]) options |= NSCaseInsensitiveSearch;
range = [textContents findString:[self findString] selectedRange:[text selectedRange] options:options wrap:YES];
if (range.length) {
[text setSelectedRange:range];
[text scrollRangeToVisible:range];
lastFindWasSuccessful = YES;
}
}
}
if (!lastFindWasSuccessful) {
NSBeep();
[statusField setStringValue:NSLocalizedStringFromTable(@"Not found", @"FindPanel", @"Status displayed in find panel when the find string is not found.")];
} else {
[statusField setStringValue:@""];
}
return lastFindWasSuccessful;
}
- (void)orderFrontFindPanel:(id)sender {
NSPanel *panel = [self findPanel];
[findTextField selectText:nil];
[panel makeKeyAndOrderFront:nil];
}
/**** Action methods for gadgets in the find panel; these should all end up setting or clearing the status field ****/
- (void)findNextAndOrderFindPanelOut:(id)sender {
[findNextButton performClick:nil];
if (lastFindWasSuccessful) {
[[self findPanel] orderOut:sender];
} else {
[findTextField selectText:nil];
}
}
- (void)findNext:(id)sender {
if (findTextField) [self setFindString:[findTextField stringValue]]; /* findTextField should be set */
(void)[self find:Forward];
}
- (void)findPrevious:(id)sender {
if (findTextField) [self setFindString:[findTextField stringValue]]; /* findTextField should be set */
(void)[self find:Backward];
}
- (void)replace:(id)sender {
NSTextView *text = [self textObjectToSearchIn];
if (!text) {
NSBeep();
} else {
[[text textStorage] replaceCharactersInRange:[text selectedRange] withString:[replaceTextField stringValue]];
[text didChangeText];
}
[statusField setStringValue:@""];
}
- (void)replaceAndFind:(id)sender {
[self replace:sender];
[self findNext:sender];
}
#define ReplaceAllScopeEntireFile 42
#define ReplaceAllScopeSelection 43
- (void)replaceAll:(id)sender {
NSTextView *text = [self textObjectToSearchIn];
if (!text) {
NSBeep();
} else {
NSTextStorage *textStorage = [text textStorage];
NSString *textContents = [text string];
NSString *replaceString = [replaceTextField stringValue];
BOOL entireFile = replaceAllScopeMatrix ? ([replaceAllScopeMatrix selectedTag] == ReplaceAllScopeEntireFile) : YES;
NSRange replaceRange = entireFile ? NSMakeRange(0, [textStorage length]) : [text selectedRange];
unsigned options = NSBackwardsSearch | ([ignoreCaseButton state] ? NSCaseInsensitiveSearch : 0);
unsigned replaced = 0;
if (findTextField) [self setFindString:[findTextField stringValue]];
while (1) {
NSRange foundRange = [textContents rangeOfString:[self findString] options:options range:replaceRange];
if (foundRange.length == 0) break;
if ([text shouldChangeTextInRange:foundRange replacementString:replaceString]) {
if (replaced == 0) [textStorage beginEditing];
replaced++;
[textStorage replaceCharactersInRange:foundRange withString:replaceString];
replaceRange.length = foundRange.location - replaceRange.location;
}
}
if (replaced > 0) { /* There was at least one replacement */
[textStorage endEditing]; /* We need this to bracket the beginEditing */
[text didChangeText]; /* We need one of these to terminate the shouldChange... methods we sent */
[statusField setStringValue:[NSString localizedStringWithFormat:NSLocalizedStringFromTable(@"%d replaced", @"FindPanel", @"Status displayed in find panel when indicated number of matches are replaced."), replaced]];
} else { /* No replacements were done... */
NSBeep();
[statusField setStringValue:NSLocalizedStringFromTable(@"Not found", @"FindPanel", @"Status displayed in find panel when the find string is not found.")];
}
}
}
@end
@implementation NSString (NSStringTextFinding)
- (NSRange)findString:(NSString *)string selectedRange:(NSRange)selectedRange options:(unsigned)options wrap:(BOOL)wrap {
BOOL forwards = (options & NSBackwardsSearch) == 0;
unsigned length = [self length];
NSRange searchRange, range;
if (forwards) {
searchRange.location = NSMaxRange(selectedRange);
searchRange.length = length - searchRange.location;
range = [self rangeOfString:string options:options range:searchRange];
if ((range.length == 0) && wrap) { /* If not found look at the first part of the string */
searchRange.location = 0;
searchRange.length = selectedRange.location;
range = [self rangeOfString:string options:options range:searchRange];
}
} else {
searchRange.location = 0;
searchRange.length = selectedRange.location;
range = [self rangeOfString:string options:options range:searchRange];
if ((range.length == 0) && wrap) {
searchRange.location = NSMaxRange(selectedRange);
searchRange.length = length - searchRange.location;
range = [self rangeOfString:string options:options range:searchRange];
}
}
return range;
}
@end
/*
2/21/95 aozer Created for Edit II.
2/24/95 aozer Find pasteboard support
8/16/95 aozer Replace functionality
10/4/95 aozer Status field update
11/12/96 aozer Correctly send shouldChange... to the textview while doing a replace
*/
|