-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWNSearchWindowController.m
474 lines (393 loc) · 16 KB
/
WNSearchWindowController.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//---------------------------------------------------------------------------------------
// WNSearchWindowController.m created by erik on Fri 18-Sep-1998
// This code is part of the WordNet frontend by Erik Doernenburg. For copyright details
// see GNU public license version 2 or above. No warranties implied. Use at own risk.
// More information can be found at http://www.mulle-kybernetik.com/software/WordNet/.
//---------------------------------------------------------------------------------------
#import <AppKit/AppKit.h>
#import "WNController.h"
#import "WNResult.h"
#import "WNTextView.h"
#import "WNSearchWindowController.h"
@interface WNSearchWindowController(PrivateAPI)
- (void)_setOutputText:(NSString *)text;
- (void)findDirection:(int)direction;
- (NSRange)findString:(NSString *)string inString:(NSString *)supersetString selectedRange:(NSRange)selectedRange options:(unsigned)options wrap:(BOOL)wrap;
- (NSString *)pbSearchString;
- (void)setPbSearchString:(NSString *)aString;
@end
//=======================================================================================
@implementation WNSearchWindowController
//=======================================================================================
//---------------------------------------------------------------------------------------
// INIT & DEALLOC
//---------------------------------------------------------------------------------------
- (id)initWithAccessController:(WNController *)controller
{
if((self = [self initWithWindowNibName:@"SearchWindow"]) == nil)
return nil;
wnController = [controller retain];
return self;
}
- (void)windowDidLoad
{
NSArray *popUpButtonList;
NSMutableDictionary *titles;
NSEnumerator *popUpButtonEnum, *itemEnum;
NSPopUpButton *popUpButton;
id <NSMenuItem> item;
NSNumber *wordType;
[super windowDidLoad];
popUpButtonList = [NSArray arrayWithObjects:nounPopUp, verbPopUp, adjPopUp, advPopUp, nil];
popUpButtons = [[NSMutableDictionary alloc] init];
popUpTitles = [[NSMutableDictionary alloc] init];
popUpButtonEnum = [popUpButtonList objectEnumerator];
while((popUpButton = [popUpButtonEnum nextObject]) != nil)
{
[popUpButton setAutoenablesItems:NO];
wordType = [NSNumber numberWithInt:[popUpButton tag]];
[popUpButtons setObject:popUpButton forKey:wordType];
titles = [NSMutableDictionary dictionary];
itemEnum = [[popUpButton itemArray] objectEnumerator];
while((item = [itemEnum nextObject]) != nil)
if([item tag] > 0)
[titles setObject:[item title] forKey:[NSNumber numberWithInt:[item tag]]];
[popUpTitles setObject:titles forKey:wordType];
}
[(NSSearchFieldCell *)[inputField cell] setMaximumRecents:20];
[[self window] setFrameUsingName:@"SearchWindow"];
[[self window] setFrameAutosaveName:@"SearchWindow"];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDoubleClickNotification:) name:WNTextViewDoubleClickNotification object:textView];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[popUpButtons release];
[popUpTitles release];
[wnController release];
[super dealloc];
}
//---------------------------------------------------------------------------------------
// WINDOW HANDLING
//---------------------------------------------------------------------------------------
- (void)showWindow:(id)sender
{
[self window]; // will load NIB if necessary
[super showWindow:sender];
}
//---------------------------------------------------------------------------------------
// ACTIONS METHODS
//---------------------------------------------------------------------------------------
- (IBAction)runSearch:(id)sender
{
NSArray *resultSet = nil;
NSMutableDictionary *resultWords;
NSEnumerator *popUpButtonEnum;
NSPopUpButton *popUpButton;
NSDictionary *titles;
NSString *word;
NSMutableString *overviews;
NSEnumerator *resultEnum, *itemEnum;
WNResult *result;
NSMenuItem *item;
int itemIndex;
word = [[inputField stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if([word isEqualToString:@""] == NO)
resultSet = [wnController runSearchForWord:word];
if(resultSet == nil || [resultSet count] == 0)
{
[nounPopUp setEnabled:NO];
[verbPopUp setEnabled:NO];
[adjPopUp setEnabled:NO];
[advPopUp setEnabled:NO];
[textView setString:@""];
[inputField selectText:self];
[[self window] makeFirstResponder:inputField];
NSBeep();
return;
}
resultWords = [NSMutableDictionary dictionary];
overviews = [NSMutableString string];
resultEnum = [resultSet objectEnumerator];
while((result = [resultEnum nextObject]) != nil)
{
[resultWords setObject:[result word] forKey:[NSNumber numberWithInt:[result posCode]]];
if([result typeCode] == 29)
[overviews appendString:[wnController getDetailsForResult:result]];
}
// setup titles and disable all
popUpButtonEnum = [popUpButtons objectEnumerator];
while((popUpButton = [popUpButtonEnum nextObject]) != nil)
{
[popUpButton setEnabled:NO];
titles = [popUpTitles objectForKey:[NSNumber numberWithInt:[popUpButton tag]]];
itemEnum = [[popUpButton itemArray] objectEnumerator];
while((item = [itemEnum nextObject]) != nil)
{
if([item tag] < 0)
continue;
[item setTitle:[NSString stringWithFormat:[titles objectForKey:[NSNumber numberWithInt:[item tag]]], [resultWords objectForKey:[NSNumber numberWithInt:[popUpButton tag]]]]];
[item setEnabled:NO];
}
}
// enable valid searches
resultEnum = [resultSet objectEnumerator];
while((result = [resultEnum nextObject]) != nil)
{
popUpButton = [popUpButtons objectForKey:[NSNumber numberWithInt:[result posCode]]];
[popUpButton setEnabled:YES];
if((itemIndex = [popUpButton indexOfItemWithTag:[result typeCode]]) != -1)
{
item = [popUpButton itemAtIndex:itemIndex];
[item setEnabled:YES];
[item setRepresentedObject:result];
}
#if DEBUG
else
NSLog(@"warning. no popup item for '%@' %@/%@.", [result word], [result pos], [result type]);
#endif
}
[self _setOutputText:overviews];
[inputField selectText:self];
[[self window] makeFirstResponder:inputField];
}
- (IBAction)getDetails:(id)sender
{
NSString *details;
details = [wnController getDetailsForResult:[[sender selectedItem] representedObject]];
[self _setOutputText:details];
}
- (void)_setOutputText:(NSString *)text
{
static NSFont *font = nil;
NSMutableAttributedString *buffer;
NSMutableDictionary *fontAttr;
NSMutableParagraphStyle *lineStyle;
NSAttributedString *formattedLine, *newlineString;
NSEnumerator *lineEnum;
NSString *line;
NSScanner *scanner;
int prefixLength;
if(font == nil)
font = [[NSFont systemFontOfSize:[NSFont smallSystemFontSize]] retain];
buffer = [[[NSMutableAttributedString alloc] initWithString:@""] autorelease];
fontAttr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
newlineString = [[[NSAttributedString alloc] initWithString:@"\n"] autorelease];
lineEnum = [[text componentsSeparatedByString:@"\n"] objectEnumerator];
while((line = [lineEnum nextObject]) != nil)
{
scanner = [NSScanner scannerWithString:line];
if([scanner scanString:@"->" intoString:NULL] == YES)
prefixLength = [scanner scanLocation];
else if([scanner scanString:@"=>" intoString:NULL] == YES)
prefixLength = [scanner scanLocation];
else if([scanner scanString:@"MEMBER OF:" intoString:NULL] == YES)
prefixLength = [scanner scanLocation];
else if([scanner scanString:@"PART OF:" intoString:NULL] == YES)
prefixLength = [scanner scanLocation];
else if([scanner scanString:@"HAS PART:" intoString:NULL] == YES)
prefixLength = [scanner scanLocation];
else if([scanner scanString:@"EX:" intoString:NULL] == YES)
prefixLength = [scanner scanLocation];
else if([scanner scanInt:NULL] && [scanner scanString:@"." intoString:NULL])
prefixLength = [scanner scanLocation];
else
prefixLength = 0;
if(([line length] > prefixLength) && ([line characterAtIndex:prefixLength] == ' '))
prefixLength += 1;
lineStyle = [[[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
[lineStyle setHeadIndent:[[line substringToIndex:prefixLength] sizeWithAttributes:fontAttr].width];
formattedLine = [[[NSAttributedString alloc] initWithString:line attributes:[NSDictionary dictionaryWithObject:lineStyle forKey:NSParagraphStyleAttributeName]] autorelease];
[buffer appendAttributedString:formattedLine];
[buffer appendAttributedString:newlineString];
}
[[textView textStorage] beginEditing];
[[textView textStorage] setAttributedString:buffer];
[[textView textStorage] endEditing];
}
- (IBAction)printResult:(id)sender
{
NSColor *bgc = [textView backgroundColor];
[textView setBackgroundColor:[NSColor whiteColor]];
[textView print:sender];
[textView setBackgroundColor:bgc];
}
- (IBAction)enterSelection:(id)sender
{
NSRange range = [textView selectedRange];
if(range.length != 0)
{
NSString *searchString = [[[textView textStorage] string] substringWithRange:range];
[self setPbSearchString:searchString];
[findStringField setStringValue:searchString];
}
else
{
NSBeep();
}
}
- (void)jumpToSelection: sender
{
[textView scrollRangeToVisible:[textView selectedRange]];
}
//---------------------------------------------------------------------------------------
// FIND PANEL
//---------------------------------------------------------------------------------------
- (IBAction)showFindPanel:(id)sender
{
if([self pbSearchString])
[findStringField setStringValue:[self pbSearchString]];
else
[findStringField setStringValue:@""];
[findPanel makeKeyAndOrderFront:sender];
}
- (IBAction)findNext:(id)sender
{
[self setPbSearchString:[findStringField stringValue]];
[self findDirection:0];
}
- (IBAction)findPrevious:(id)sender
{
[self setPbSearchString:[findStringField stringValue]];
[self findDirection:NSBackwardsSearch];
}
/*
direction is one of
0 == forwards
NSBackwardsSearch == backwards
*/
- (void)findDirection:(int)direction
{
NSString *searchString;
BOOL found = NO;
searchString = [self pbSearchString];
if(searchString != nil)
{
NSString *textContents = [[textView textStorage] string];
unsigned textLength;
if(textContents && (textLength = [textContents length]))
{
NSRange range;
unsigned options = 0;
if(direction == NSBackwardsSearch)
options |= NSBackwardsSearch;
if([ignoreCaseButton state] == NSOnState)
options |= NSCaseInsensitiveSearch;
range = [self findString:searchString inString:textContents selectedRange:[textView selectedRange] options:options wrap:YES];
if(range.length)
{
[textView setSelectedRange:range];
[textView scrollRangeToVisible:range];
found = YES;
}
}
}
if(found == NO)
NSBeep();
}
- (NSRange)findString:(NSString *)string inString:(NSString *)supersetString selectedRange:(NSRange)selectedRange options:(unsigned)options wrap:(BOOL)wrap
{
BOOL forwards = (options & NSBackwardsSearch) == 0;
unsigned length = [supersetString length];
NSRange searchRange, range;
if(forwards)
{
searchRange.location = NSMaxRange(selectedRange);
searchRange.length = length - searchRange.location;
range = [supersetString 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 = [supersetString rangeOfString:string options:options range:searchRange];
}
}
else
{
searchRange.location = 0;
searchRange.length = selectedRange.location;
range = [supersetString rangeOfString:string options:options range:searchRange];
if((range.length == 0) && wrap)
{
searchRange.location = NSMaxRange(selectedRange);
searchRange.length = length - searchRange.location;
range = [supersetString rangeOfString:string options:options range:searchRange];
}
}
return range;
}
- (NSString *)pbSearchString
{
NSPasteboard *pasteboard = [NSPasteboard pasteboardWithName:NSFindPboard];
NSString *searchString = nil;
if([[pasteboard types] containsObject:NSStringPboardType])
searchString = [pasteboard stringForType:NSStringPboardType];
return searchString;
}
- (void)setPbSearchString:(NSString *)aString
{
NSPasteboard *pasteboard = [NSPasteboard pasteboardWithName:NSFindPboard];
[pasteboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
[pasteboard setString:aString forType:NSStringPboardType];
}
//---------------------------------------------------------------------------------------
// MENU ITEM VALIDATION
//---------------------------------------------------------------------------------------
- (BOOL)validateMenuItem:(NSMenuItem *)anItem
{
SEL action = [anItem action];
if(action == @selector(findPrevious:))
return ([self pbSearchString] != nil) && ([[[textView textStorage] string] length] != 0);
if(action == @selector(findNext:))
return ([self pbSearchString] != nil) && ([[[textView textStorage] string] length] != 0);
if(action == @selector(jumpToSelection:))
return [textView selectedRange].length != 0;
if(action == @selector(enterSelection:))
return [textView selectedRange].length != 0;
return YES;
}
//---------------------------------------------------------------------------------------
// OTHER INVOCATIONS FOR SEARCHES
//---------------------------------------------------------------------------------------
- (void)textViewDoubleClickNotification:(NSNotification *)notification
{
NSString *searchString;
searchString = [[[notification userInfo] objectForKey:@"selection"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[inputField setStringValue:searchString];
[inputField performClick:self];
}
- (void)provideLookupService:(NSPasteboard *)pasteboard userData:(NSString *)userData error:(NSString **)error
{
NSString *string;
if([[pasteboard types] containsObject:NSStringPboardType] == NO)
{
*error = NSLocalizedString(@"No text provided.", "");
return;
}
if((string = [pasteboard stringForType:NSStringPboardType]) == nil)
{
*error = NSLocalizedString(@"No text provided.", "");
return;
}
[NSApp activateIgnoringOtherApps:YES];
[self showWindow:self];
string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[inputField setStringValue:string];
[inputField performClick:self];
}
//---------------------------------------------------------------------------------------
// WINDOW DELEGATE METHODS
//---------------------------------------------------------------------------------------
- (void)windowDidBecomeMain:(NSNotification *)aNotification
{
// Update Findpanel with current NSFindPasteboard's content
// (which might have been changed by another app in the meantime)
if([self pbSearchString])
[findStringField setStringValue:[self pbSearchString]];
else
[findStringField setStringValue:@""];
}
//=======================================================================================
@end
//=======================================================================================