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 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
|
/** <title>NSSpellChecker</title>
<abstract>Class to provide the graphical interface to the spell checking
service.</abstract>
Copyright (C) 2001, 1996 Free Software Foundation, Inc.
Author: Gregory John Casamento <greg_casamento@yahoo.com>
Date: 2001,2003
Author: Scott Christley <scottc@net-community.com>
Date: 1996
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; see the file COPYING.LIB.
If not, see <http://www.gnu.org/licenses/> or write to the
Free Software Foundation, 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#import "config.h"
#import <Foundation/NSArray.h>
#import <Foundation/NSBundle.h>
#import <Foundation/NSConnection.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSDistantObject.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSException.h>
#import <Foundation/NSNotification.h>
#import <Foundation/NSProxy.h>
#import <Foundation/NSSet.h>
#import <Foundation/NSSpellServer.h>
#import <Foundation/NSUserDefaults.h>
#import <Foundation/NSValue.h>
#import "AppKit/NSBrowser.h"
#import "AppKit/NSBrowserCell.h"
#import "AppKit/NSEvent.h"
#import "AppKit/NSGraphics.h"
#import "AppKit/NSImage.h"
#import "AppKit/NSMatrix.h"
#import "AppKit/NSNib.h"
#import "AppKit/NSNibLoading.h"
#import "AppKit/NSPanel.h"
#import "AppKit/NSPopUpButton.h"
#import "AppKit/NSSpellChecker.h"
#import "AppKit/NSApplication.h"
#import "AppKit/NSTextField.h"
#import "AppKit/NSWindow.h"
#import "GSGuiPrivate.h"
#import "GNUstepGUI/GSServicesManager.h"
// prototype for function to create name for server
extern NSString *GSSpellServerName(NSString *checkerDictionary, NSString *language);
// These are methods which we only want the NSSpellChecker to call.
// The protocol is defined here so that the outside world does not
// have access to these internal methods.
@protocol NSSpellServerPrivateProtocol
- (NSRange) _findMisspelledWordInString: (NSString *)stringToCheck
language: (NSString *)language
ignoredWords: (NSArray *)ignoredWords
wordCount: (int *)wordCount
countOnly: (BOOL)countOnly;
- (BOOL) _learnWord: (NSString *)word
inDictionary: (NSString *)language;
- (BOOL) _forgetWord: (NSString *)word
inDictionary: (NSString *)language;
- (NSArray *) _suggestGuessesForWord: (NSString *)word
inLanguage: (NSString *)language;
@end
// Methods needed to get the GSServicesManager
@interface NSApplication(NSSpellCheckerMethods)
- (GSServicesManager *)_listener;
@end
@implementation NSApplication(NSSpellCheckerMethods)
- (GSServicesManager *)_listener
{
return _listener;
}
@end
// Methods in the GSServicesManager to launch the spell server.
@interface GSServicesManager(NSSpellCheckerMethods)
- (id)_launchSpellCheckerForLanguage: (NSString *)language;
- (NSArray *)_languagesForPopUpButton;
@end
@implementation GSServicesManager(NSSpellCheckerMethods)
- (id)_launchSpellCheckerForLanguage: (NSString *)language
{
id<NSSpellServerPrivateProtocol> proxy = nil;
NSDictionary *spellCheckers = [_allServices objectForKey: @"BySpell"];
NSDictionary *checkerDictionary = [spellCheckers objectForKey: language];
NSString *spellServicePath = [checkerDictionary objectForKey: @"ServicePath"];
NSString *vendor = [checkerDictionary objectForKey: @"NSSpellChecker"];
NSDate *finishBy;
NSString *port = GSSpellServerName(vendor, language);
double seconds = 30.0;
NSLog(@"Language: %@", language);
NSLog(@"Service to start: %@", spellServicePath);
NSLog(@"Port: %@",port);
finishBy = [NSDate dateWithTimeIntervalSinceNow: seconds];
proxy = GSContactApplication(spellServicePath, port, finishBy);
if (proxy == nil)
{
NSLog(@"Failed to contact spell checker for language '%@'", language);
}
else
{
NSLog(@"Set proxy");
[(NSDistantObject *)proxy setProtocolForProxy:
@protocol(NSSpellServerPrivateProtocol)];
}
return proxy;
}
- (NSArray *)_languagesForPopUpButton
{
NSDictionary *spellCheckers = [_allServices objectForKey: @"BySpell"];
NSArray *allKeys = [spellCheckers allKeys];
return allKeys;
}
@end
// Shared spell checker instance....
static NSSpellChecker *__sharedSpellChecker = nil;
static int __documentTag = 0;
// Implementation of spell checker class
@implementation NSSpellChecker
//
// Class methods
//
+ (void)initialize
{
if (self == [NSSpellChecker class])
{
// Initial version
[self setVersion:1];
}
}
//
// Making a Checker available
//
+ (NSSpellChecker *)sharedSpellChecker
{
// Create the shared instance.
if (__sharedSpellChecker == nil)
{
__sharedSpellChecker = [[NSSpellChecker alloc] init];
}
return __sharedSpellChecker;
}
+ (BOOL)sharedSpellCheckerExists
{
// If the spell checker has been created, the
// variable will not be nil.
return (__sharedSpellChecker != nil);
}
//
// Managing the Spelling Process
//
+ (int)uniqueSpellDocumentTag
{
return ++__documentTag;
}
//
// Internal methods for use by the spellChecker GUI
//
// Support function to start the spell server
- (id)_startServerForLanguage: (NSString *)language
{
id<NSSpellServerPrivateProtocol> proxy = nil;
// Start the service for this language
proxy = [[NSApp _listener] _launchSpellCheckerForLanguage: language];
if (proxy == nil)
{
NSLog(@"Failed to get the spellserver");
}
else
{
// remove any previous notifications we are observing.
[[NSNotificationCenter defaultCenter] removeObserver: self];
// Make sure that we handle the death of the server correctly.
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(_handleServerDeath:)
name: NSConnectionDidDieNotification
object: [(NSDistantObject *)proxy connectionForProxy]];
}
return proxy;
}
- (id)_serverProxy
{
if (_serverProxy == nil)
{
// Start the server and retain the reference to the
// proxy.
id<NSSpellServerPrivateProtocol> proxy = [self _startServerForLanguage: _language];
if (proxy != nil)
{
_serverProxy = proxy;
RETAIN(_serverProxy);
}
}
return _serverProxy;
}
- (void)_populateDictionaryPulldown: (NSArray *)dictionaries
{
[_dictionaryPulldown removeAllItems];
[_dictionaryPulldown addItemsWithTitles:
[dictionaries sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]];
[_dictionaryPulldown selectItemWithTitle: _language];
}
- (void)_populateAccessoryView
{
// refresh the columns in the browser
[_accessoryView reloadColumn: 0];
}
- (void)_handleServerDeath: (NSNotification *)notification
{
NSLog(@"Spell server died");
DESTROY(_serverProxy);
}
//
// Instance methods
//
- init
{
NSArray *languages;
self = [super init];
if (self == nil)
return nil;
languages = [[NSUserDefaults standardUserDefaults]
stringArrayForKey: @"NSLanguages"];
// Set the language to the default for the user.
_language = RETAIN([languages objectAtIndex: 0]);
_wrapFlag = NO;
_position = 0;
_spellPanel = nil;
_serverProxy = nil;
_currentTag = 0;
_ignoredWords = [NSMutableDictionary new];
// Load the NIB file from the GUI bundle
if (![GSGuiBundle() loadNibFile: @"GSSpellPanel"
externalNameTable: [NSDictionary dictionaryWithObject: self forKey: NSNibOwner]
withZone: [self zone]])
{
NSLog(@"Model file load failed for GSSpellPanel");
return nil;
}
return self;
}
- (void)dealloc
{
RELEASE(_language);
RELEASE(_ignoredWords);
RELEASE(_serverProxy);
[super dealloc];
}
//
// Managing the Spelling Panel
//
- (NSView *)accessoryView
{
return _accessoryView;
}
- (void)setAccessoryView:(NSView *)aView
{
_accessoryView = aView;
}
- (NSPanel *)spellingPanel
{
return _spellPanel;
}
//
// Checking Spelling
//
- (int)countWordsInString:(NSString *)aString
language:(NSString *)language
{
int count = 0;
id<NSSpellServerPrivateProtocol> proxy = [self _serverProxy];
if (proxy != nil)
[proxy _findMisspelledWordInString: aString
language: _language
ignoredWords: nil
wordCount: &count
countOnly: YES];
return count;
}
- (NSRange)checkSpellingOfString:(NSString *)stringToCheck
startingAt:(int)startingOffset
{
int wordCount = 0;
NSRange r;
r = [self checkSpellingOfString: stringToCheck
startingAt: startingOffset
language: _language
wrap: NO
inSpellDocumentWithTag: _currentTag
wordCount: &wordCount];
return r;
}
- (NSRange)checkSpellingOfString:(NSString *)stringToCheck
startingAt:(int)startingOffset
language:(NSString *)language
wrap:(BOOL)wrapFlag
inSpellDocumentWithTag:(int)tag
wordCount:(int *)wordCount
{
NSRange r;
NSArray *dictForTag = [self ignoredWordsInSpellDocumentWithTag: tag];
_currentTag = tag;
// We have no string to work with
if (stringToCheck == nil)
{
return NSMakeRange(0,0);
}
else
// The string is zero length
if ([stringToCheck length] == 0)
{
return NSMakeRange(0,0);
}
// Do this in an exception handling block in ensure that a failure of the
// spellserver does not bring down the application.
NS_DURING
{
id<NSSpellServerPrivateProtocol> proxy = [self _serverProxy];
// Get the substring and check it.
NSString *substringToCheck = [stringToCheck substringFromIndex: startingOffset];
if (proxy == nil)
NS_VALUERETURN(NSMakeRange(0,0), NSRange);
r = [proxy _findMisspelledWordInString: substringToCheck
language: _language
ignoredWords: dictForTag
wordCount: wordCount
countOnly: NO];
if (r.length != 0)
{
// Adjust results relative to the original string
r.location += startingOffset;
}
else
{
if (wrapFlag)
{
// Check the second half of the string
NSString *firstHalfOfString = [stringToCheck
substringToIndex: startingOffset];
r = [proxy _findMisspelledWordInString: firstHalfOfString
language: _language
ignoredWords: dictForTag
wordCount: wordCount
countOnly: NO];
}
}
NS_VALUERETURN(r, NSRange);
}
NS_HANDLER
{
NSLog(@"%@",[localException reason]);
}
NS_ENDHANDLER
return NSMakeRange(0,0);
}
- (NSArray *)guessesForWord:(NSString *)word
{
NSArray *guesses;
// Make the call to the server to get the guesses.
NS_DURING
{
guesses = [[self _serverProxy] _suggestGuessesForWord: word
inLanguage: _language];
NS_VALUERETURN(guesses, id);
}
NS_HANDLER
{
NSLog(@"%@",[localException reason]);
}
NS_ENDHANDLER
return nil;
}
//
// Setting the Language
//
- (NSString *)language
{
return _language;
}
- (BOOL)setLanguage:(NSString *)aLanguage
{
int index = 0;
BOOL result = NO;
index = [_dictionaryPulldown indexOfItemWithTitle: aLanguage];
if (index != -1)
{
[_dictionaryPulldown selectItemAtIndex: index];
result = YES;
}
return result;
}
//
// Managing the Spelling Process
//
// Remove the ignored word list for this
// document from the dictionary
- (void)closeSpellDocumentWithTag:(int)tag
{
NSNumber *key = [NSNumber numberWithInt: tag];
[_ignoredWords removeObjectForKey: key];
}
// Add a word to the ignored list.
- (void) ignoreWord:(NSString *)wordToIgnore
inSpellDocumentWithTag:(int)tag
{
NSNumber *key = [NSNumber numberWithInt: tag];
NSMutableSet *words = [_ignoredWords objectForKey: key];
if (![wordToIgnore isEqualToString: @""])
{
// If there is a dictionary add to it, if not create one.
if (words == nil)
{
words = [NSMutableSet setWithObject: wordToIgnore];
[_ignoredWords setObject: words forKey: key];
}
else
{
[words addObject: wordToIgnore];
}
}
}
// get the list of ignored words.
- (NSArray *)ignoredWordsInSpellDocumentWithTag:(int)tag
{
NSNumber *key = [NSNumber numberWithInt: tag];
NSSet *words = [_ignoredWords objectForKey: key];
return [words allObjects];
}
// set the list of ignored words for a given document
- (void)setIgnoredWords:(NSArray *)someWords
inSpellDocumentWithTag:(int)tag
{
NSNumber *key = [NSNumber numberWithInt: tag];
NSSet *words = [NSSet setWithArray: someWords];
[_ignoredWords setObject: words forKey: key];
}
- (void)setWordFieldStringValue:(NSString *)aString
{
[_wordField setStringValue: aString];
}
- (void)updateSpellingPanelWithMisspelledWord:(NSString *)word
{
if ((word == nil) || ([word isEqualToString: @""]))
{
[_ignoreButton setEnabled: NO];
[_guessButton setEnabled: NO];
NSBeep();
return;
}
[_ignoreButton setEnabled: YES];
[_guessButton setEnabled: NO];
[self setWordFieldStringValue: word];
[self _populateAccessoryView];
}
- (void) _findNext: (id)sender
{
BOOL processed = [[[NSApp mainWindow] firstResponder]
tryToPerform: @selector(checkSpelling:)
with: _spellPanel];
if (!processed)
{
NSLog(@"No responder found");
}
}
- (void) _learn: (id)sender
{
NSString *word = [_wordField stringValue];
// Call server and record the learned word.
NS_DURING
{
[[self _serverProxy] _learnWord: word
inDictionary: _language];
}
NS_HANDLER
{
NSLog(@"%@",[localException reason]);
}
NS_ENDHANDLER
[self _findNext: sender];
}
- (void) _forget: (id)sender
{
NSString *word = [_wordField stringValue];
// Call the server and remove the word from the learned
// list.
NS_DURING
{
[[self _serverProxy] _forgetWord: word
inDictionary: _language];
}
NS_HANDLER
{
NSLog(@"%@",[localException reason]);
}
NS_ENDHANDLER
[self _findNext: sender];
}
- (void) _ignore: (id)sender
{
BOOL processed = [[[NSApp mainWindow] firstResponder]
tryToPerform: @selector(ignoreSpelling:)
with: _wordField];
if (!processed)
{
NSLog(@"_ignore: No responder found");
}
[self _findNext: sender];
}
- (void) _guess: (id)sender
{
// Fill in the view...
[self _populateAccessoryView];
}
- (void) _correct: (id)sender
{
BOOL processed = [[[NSApp mainWindow] firstResponder]
tryToPerform: @selector(changeSpelling:)
with: _wordField];
if (!processed)
{
NSLog(@"No responder found");
}
[self _findNext: sender];
}
- (void) _switchDictionary: (id)sender
{
id<NSSpellServerPrivateProtocol> proxy = nil;
NSString *language = nil;
// Start the service for this language
language = [_dictionaryPulldown stringValue];
if (![language isEqualToString: _language])
{
NSLog(@"Language = %@",language);
proxy = [self _startServerForLanguage: language];
if (proxy != nil)
{
ASSIGN(_language, language);
ASSIGN(_serverProxy, (id)proxy);
}
else
{
// Reset the pulldown to the proper language.
[_dictionaryPulldown selectItemWithTitle: _language];
}
}
}
- (void) _highlightGuess: (id)sender
{
NSString *selectedGuess = nil;
selectedGuess = [[_accessoryView selectedCell] stringValue];
[_ignoreButton setEnabled: NO];
[_guessButton setEnabled: YES];
[_wordField setStringValue: selectedGuess];
}
- (void) awakeFromNib
{
[self _populateDictionaryPulldown:
[[NSApp _listener] _languagesForPopUpButton]];
[_accessoryView setDelegate: self];
[_accessoryView setDoubleAction: @selector(_correct:)];
[_findNextButton setKeyEquivalent: @"n"];
[_findNextButton setKeyEquivalentModifierMask: NSCommandKeyMask];
[_ignoreButton setKeyEquivalent: @"i"];
[_ignoreButton setKeyEquivalentModifierMask: NSCommandKeyMask];
[_learnButton setKeyEquivalent: @"l"];
[_learnButton setKeyEquivalentModifierMask: NSCommandKeyMask];
[_forgetButton setKeyEquivalent: @"f"];
[_forgetButton setKeyEquivalentModifierMask: NSCommandKeyMask];
[_guessButton setKeyEquivalent: @"g"];
[_guessButton setKeyEquivalentModifierMask: NSCommandKeyMask];
[_correctButton setKeyEquivalent: @"c"];
[_correctButton setKeyEquivalentModifierMask: NSCommandKeyMask];
[_correctButton setImagePosition: NSImageRight];
[_correctButton setImage: [NSImage imageNamed: @"common_ret"]];
[_correctButton setAlternateImage: [NSImage imageNamed: @"common_retH"]];
[_spellPanel makeFirstResponder: _correctButton];
[_spellPanel setBecomesKeyOnlyIfNeeded: YES];
[_spellPanel setFloatingPanel: YES];
}
@end
@interface NSSpellChecker(SpellBrowserDelegate)
- (BOOL) browser: (NSBrowser*)sender selectRow: (NSInteger)row inColumn: (NSInteger)column;
- (void) browser: (NSBrowser *)sender createRowsForColumn: (NSInteger)column
inMatrix: (NSMatrix *)matrix;
- (NSString*) browser: (NSBrowser*)sender titleOfColumn: (NSInteger)column;
- (void) browser: (NSBrowser *)sender
willDisplayCell: (id)cell
atRow: (NSInteger)row
column: (NSInteger)column;
- (BOOL) browser: (NSBrowser *)sender isColumnValid: (NSInteger)column;
@end
@implementation NSSpellChecker(SpellBrowserDelegate)
- (BOOL) browser: (NSBrowser*)sender selectRow: (NSInteger)row inColumn: (NSInteger)column
{
return YES;
}
- (void) browser: (NSBrowser *)sender createRowsForColumn: (NSInteger)column
inMatrix: (NSMatrix *)matrix
{
NSArray *guesses = [self guessesForWord: [_wordField stringValue]];
NSEnumerator *e = [guesses objectEnumerator];
NSString *word = nil;
NSBrowserCell *cell= nil;
NSInteger i = 0;
while ((word = [e nextObject]) != nil)
{
[matrix insertRow: i withCells: nil];
cell = [matrix cellAtRow: i column: 0];
[cell setLeaf: YES];
i++;
[cell setStringValue: word];
}
}
- (NSString*) browser: (NSBrowser*)sender titleOfColumn: (NSInteger)column
{
return _(@"Guess");
}
- (void) browser: (NSBrowser *)sender
willDisplayCell: (id)cell
atRow: (NSInteger)row
column: (NSInteger)column
{
}
- (BOOL) browser: (NSBrowser *)sender isColumnValid: (NSInteger)column
{
return NO;
}
@end
|