WebKitTextChecker.cpp [plain text]
#include "config.h"
#include "WebKitTextChecker.h"
#if ENABLE(SPELLCHECK)
#include "WebKitPrivate.h"
using namespace WebKit;
static inline WebKitTextChecker* toTextChecker(const void* clientInfo)
{
return static_cast<WebKitTextChecker*>(const_cast<void*>(clientInfo));
}
static bool continuousSpellCheckingEnabledCallback(const void* clientInfo)
{
return toTextChecker(clientInfo)->isSpellCheckingEnabled();
}
static void setContinuousSpellCheckingEnabledCallback(bool enabled, const void* clientInfo)
{
toTextChecker(clientInfo)->setSpellCheckingEnabled(enabled);
}
static void checkSpellingOfStringCallback(uint64_t , WKStringRef text, int32_t* misspellingLocation, int32_t* misspellingLength, const void* clientInfo)
{
toTextChecker(clientInfo)->checkSpellingOfString(toImpl(text)->string(), *misspellingLocation, *misspellingLength);
}
static WKArrayRef guessesForWordCallback(uint64_t , WKStringRef word, const void* clientInfo)
{
Vector<String> guesses = toTextChecker(clientInfo)->getGuessesForWord(toImpl(word)->string());
if (guesses.isEmpty())
return 0;
WKMutableArrayRef wkSuggestions = WKMutableArrayCreate();
for (Vector<String>::const_iterator iter = guesses.begin(); iter != guesses.end(); ++iter) {
WKRetainPtr<WKStringRef> wkSuggestion(AdoptWK, WKStringCreateWithUTF8CString(iter->utf8().data()));
WKArrayAppendItem(wkSuggestions, wkSuggestion.get());
}
return wkSuggestions;
}
static void learnWordCallback(uint64_t , WKStringRef word, const void* clientInfo)
{
toTextChecker(clientInfo)->learnWord(toImpl(word)->string());
}
static void ignoreWordCallback(uint64_t , WKStringRef word, const void* clientInfo)
{
toTextChecker(clientInfo)->ignoreWord(toImpl(word)->string());
}
WebKitTextChecker::~WebKitTextChecker()
{
}
WebKitTextChecker::WebKitTextChecker()
: m_textChecker(WebCore::TextCheckerEnchant::create())
, m_spellCheckingEnabled(false)
{
WKTextCheckerClientV0 wkTextCheckerClient = {
{
0, this, },
0, continuousSpellCheckingEnabledCallback,
setContinuousSpellCheckingEnabledCallback,
0, 0, 0, 0, checkSpellingOfStringCallback,
0, 0, 0, 0, 0, guessesForWordCallback,
learnWordCallback,
ignoreWordCallback,
};
WKTextCheckerSetClient(&wkTextCheckerClient.base);
}
void WebKitTextChecker::checkSpellingOfString(const String& string, int& misspellingLocation, int& misspellingLength)
{
m_textChecker->checkSpellingOfString(string, misspellingLocation, misspellingLength);
}
Vector<String> WebKitTextChecker::getGuessesForWord(const String& word)
{
return m_textChecker->getGuessesForWord(word);
}
void WebKitTextChecker::learnWord(const String& word)
{
m_textChecker->learnWord(word);
}
void WebKitTextChecker::ignoreWord(const String& word)
{
m_textChecker->ignoreWord(word);
}
void WebKitTextChecker::setSpellCheckingEnabled(bool enabled)
{
if (m_spellCheckingEnabled == enabled)
return;
m_spellCheckingEnabled = enabled;
WKTextCheckerContinuousSpellCheckingEnabledStateChanged(enabled);
}
const char* const* WebKitTextChecker::getSpellCheckingLanguages()
{
Vector<String> spellCheckingLanguages = m_textChecker->loadedSpellCheckingLanguages();
if (spellCheckingLanguages.isEmpty())
return 0;
m_spellCheckingLanguages = adoptGRef(g_ptr_array_new_with_free_func(g_free));
for (size_t i = 0; i < spellCheckingLanguages.size(); ++i)
g_ptr_array_add(m_spellCheckingLanguages.get(), g_strdup(spellCheckingLanguages[i].utf8().data()));
g_ptr_array_add(m_spellCheckingLanguages.get(), 0);
return reinterpret_cast<char**>(m_spellCheckingLanguages->pdata);
}
void WebKitTextChecker::setSpellCheckingLanguages(const char* const* languages)
{
Vector<String> spellCheckingLanguages;
for (size_t i = 0; languages[i]; ++i)
spellCheckingLanguages.append(String::fromUTF8(languages[i]));
m_textChecker->updateSpellCheckingLanguages(spellCheckingLanguages);
}
#endif // ENABLE(SPELLCHECK)