/* AbiSuite * Copyright (C) 2004 RŽmi Payette * * 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., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. */ /* TODO: 1- Each instances has it's own ignore list... Can be annoying 2- Implement addToCustomDict */ #include #include "appleSpell_checker.h" #include "ut_string.h" AppleSpellChecker::AppleSpellChecker() { m_checker=[NSSpellChecker sharedSpellChecker]; m_tag=[NSSpellChecker uniqueSpellDocumentTag]; m_lang=NULL; } AppleSpellChecker::~AppleSpellChecker() { [m_checker closeSpellDocumentWithTag: m_tag]; if (m_lang) [m_lang release]; } SpellChecker::SpellCheckResult AppleSpellChecker::_checkWord (const UT_UCSChar * ucszWord, size_t len) { NSString *str; NSRange result; if ( !m_lang ) return SpellChecker::LOOKUP_ERROR; str = [[NSString alloc] initWithUTF8String:UT_UTF8String(ucszWord,len).utf8_str ()]; //NSLog(@"AppleSpellChecker check : %@ in %@ (%d)",str,m_lang,this->m_tag); result=[m_checker checkSpellingOfString:str startingAt:0 language:m_lang wrap:NO inSpellDocumentWithTag:m_tag wordCount:nil]; [str release]; if ( result.length ) return SpellChecker::LOOKUP_FAILED; return SpellChecker::LOOKUP_SUCCEEDED; } UT_GenericVector * AppleSpellChecker::_suggestWord (const UT_UCSChar *ucszWord, size_t len) { const char *ptr; NSString *str; NSArray *result; unsigned int i; UT_GenericVector * pvSugg = new UT_GenericVector(); UT_UCS4Char *ucs4; str = [[NSString alloc] initWithUTF8String:UT_UTF8String(ucszWord,len).utf8_str ()]; result = [m_checker guessesForWord: str]; [str release]; for ( i = 0; i < [result count]; i++) { ptr=[[result objectAtIndex:i] UTF8String]; UT_UCS4_cloneString (&ucs4, UT_UCS4String (ptr).ucs4_str()); pvSugg->addItem (ucs4); } //NSLog(@"AppleSpellChecker::_suggest %d suggestions",pvSugg->getItemCount()); return pvSugg; } bool AppleSpellChecker::addToCustomDict (const UT_UCSChar *word, size_t len) { // TODO return false; } void AppleSpellChecker::ignoreWord (const UT_UCSChar *toCorrect, size_t toCorrectLen) { NSString *str; str = [[NSString alloc] initWithUTF8String:UT_UTF8String(toCorrect,toCorrectLen).utf8_str ()]; [m_checker ignoreWord: str inSpellDocumentWithTag:m_tag]; //NSLog(@"AppleSpellChecker ignoring %@",str); [str release]; } bool AppleSpellChecker::_requestDictionary (const char * szLang) { if ( m_lang ) [m_lang release]; /* Only the 2 char language code can be fed to set language : fr is ok, not fr-CA */ m_lang=[[NSString alloc] initWithCString:szLang length:2]; if (![m_checker setLanguage:m_lang]) { couldNotLoadDictionary ( szLang ); [m_lang release]; m_lang=NULL; return false; } //NSLog(@"AppleSpellChecker:language : %@",[m_checker language]); return (true); }