-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindPanelWindowController.m
287 lines (230 loc) · 6.35 KB
/
FindPanelWindowController.m
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
274
275
276
277
278
279
280
281
282
283
284
285
/*
** FindPanelWindowController.m
**
** Copyright (c) 2002, 2003
**
** Author: Fabian, Ujwal S. Setlur
**
** Project: iTerm
**
** Description: Implements the find functions.
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#import <iTerm/iTermController.h>
#import <iTerm/PTYTextView.h>
#import <iTerm/FindPanelWindowController.h>
#define DEBUG_ALLOC 0
static FindPanelWindowController *sharedInstance = nil;
@implementation FindPanelWindowController
//
// class methods
//
+ (id) sharedInstance
{
if ( !sharedInstance )
sharedInstance = [[self alloc] initWithWindowNibName: @"FindPanel"];
return sharedInstance;
}
- (id) initWithWindowNibName: (NSString *) windowNibName
{
#if DEBUG_ALLOC
NSLog(@"FindPanelWindowController: -initWithWindowNibName");
#endif
self = [super initWithWindowNibName: windowNibName];
// We finally set our autosave window frame name and restore the one from the user's defaults.
[[self window] setFrameAutosaveName: @"FindPanel"];
[[self window] setFrameUsingName: @"FindPanel"];
[[self window] setDelegate: self];
return (self);
}
- (void) dealloc
{
#if DEBUG_ALLOC
NSLog(@"FindPanelWindowController: -dealloc");
#endif
sharedInstance = nil;
[super dealloc];
}
// NSWindow delegate methods
- (void)windowWillClose:(NSNotification *)aNotification
{
[self autorelease];
}
- (void)windowDidLoad
{
NSPasteboard *board = [NSPasteboard pasteboardWithName:NSFindPboard];
NSString* findString = [board stringForType:NSStringPboardType];
if ([findString length])
{
[self setSearchString:findString];
[[FindCommandHandler sharedInstance] setSearchString:findString];
}
[caseCheckBox setIntValue:[[FindCommandHandler sharedInstance] ignoresCase]];
}
- (void)windowDidBecomeKey:(NSNotification *)aNotification
{
// Post a notification
[[NSNotificationCenter defaultCenter] postNotificationName: @"nonTerminalWindowBecameKey" object: nil userInfo: nil];
}
- (IBAction)ignoreCaseSwitchAction:(id)sender;
{
[[FindCommandHandler sharedInstance] setIgnoresCase:[sender intValue]];
}
// action methods
- (IBAction) findNext: (id) sender
{
NSString* searchString = [self searchString];
if([searchString length] <= 0)
{
NSBeep();
return;
}
[[FindCommandHandler sharedInstance] setSearchString:searchString];
[[FindCommandHandler sharedInstance] findNext];
[[self window] close];
}
- (IBAction)findPrevious: (id) sender
{
NSString* searchString = [self searchString];
if([searchString length] <= 0)
{
NSBeep();
return;
}
[[FindCommandHandler sharedInstance] setSearchString:searchString];
[[FindCommandHandler sharedInstance] findPrevious];
[[self window] close];
}
// get/set methods
- (id) delegate
{
return (delegate);
}
- (void) setDelegate: (id) theDelegate
{
delegate = theDelegate;
}
- (void)setSearchString: (NSString *) aString
{
if (aString && [aString length]>0) {
[searchStringField setStringValue: aString];
}
else
[searchStringField setStringValue: @""];
}
- (NSString*)searchString;
{
NSString *aString = [searchStringField stringValue];
if ([aString length]>0) {
NSPasteboard *pboard = [NSPasteboard pasteboardWithName: NSFindPboard];
[pboard declareTypes: [NSArray arrayWithObject: NSStringPboardType] owner: self];
[pboard setString: aString forType: NSStringPboardType];
}
return aString;
}
@end
// ==========================================================================================
@implementation FindCommandHandler : NSObject
- (id)init;
{
self = [super init];
_ignoresCase = [[NSUserDefaults standardUserDefaults] boolForKey:@"findIgnoreCase_iTerm"];
return self;
}
- (void)dealloc;
{
[_searchString release];
[super dealloc];
}
+ (id)sharedInstance;
{
static id shared = nil;
if (!shared)
shared = [[FindCommandHandler alloc] init];
return shared;
}
- (PTYTextView*)currentTextView;
{
id obj = [[NSApp mainWindow] firstResponder];
return (obj && [obj isKindOfClass:[PTYTextView class]]) ? obj : nil;
}
- (IBAction) findNext
{
[self findSubString: _searchString forwardDirection: YES ignoringCase: _ignoresCase];
}
- (IBAction) findPrevious
{
[self findSubString: _searchString forwardDirection: NO ignoringCase: _ignoresCase];
}
- (IBAction) findWithSelection
{
PTYTextView* textView = [self currentTextView];
if (textView)
{
// get the selected text
NSString *contentString = [textView selectedText];
if (!contentString) {
NSBeep();
return;
}
[self setSearchString: contentString];
[self findNext];
}
else
NSBeep();
}
- (IBAction)jumpToSelection
{
PTYTextView* textView = [self currentTextView];
if (textView)
{
[textView scrollToSelection];
}
else
NSBeep();
}
- (void) findSubString: (NSString *) subString forwardDirection: (BOOL) direction ignoringCase: (BOOL) caseCheck
{
PTYTextView* textView = [self currentTextView];
if (textView)
{
if ([subString length] <= 0)
{
NSBeep();
return;
}
[textView findString:subString forwardDirection: direction ignoringCase: caseCheck];
}
}
- (NSString*)searchString;
{
return _searchString;
}
- (void) setSearchString: (NSString *) aString
{
[_searchString release];
_searchString = [aString retain];
}
- (BOOL)ignoresCase;
{
return _ignoresCase;
}
- (void)setIgnoresCase:(BOOL)set;
{
_ignoresCase = set;
[[NSUserDefaults standardUserDefaults] setBool:set forKey:@"findIgnoreCase_iTerm"];
}
@end