]> git.lyx.org Git - lyx.git/blob - src/AppleSpellChecker.cpp
2beadb2eae723aa0be8569fe36f7068403f477a4
[lyx.git] / src / AppleSpellChecker.cpp
1 /**
2  * \file AppleSpellChecker.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Stephan Witt
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "AppleSpellChecker.h"
14 #include "WordLangTuple.h"
15
16 #include "support/lassert.h"
17 #include "support/debug.h"
18 #include "support/docstring_list.h"
19 #include "support/AppleSpeller.h"
20
21 using namespace std;
22 using namespace lyx::support;
23
24 namespace lyx {
25
26 struct AppleSpellChecker::Private
27 {
28         Private();
29
30         ~Private();
31
32         /// the speller
33         AppleSpeller speller;
34 };
35
36
37 AppleSpellChecker::Private::Private()
38 {
39         speller = newAppleSpeller();
40 }
41
42
43 AppleSpellChecker::Private::~Private()
44 {
45         freeAppleSpeller(speller);
46         speller = 0;
47 }
48
49
50 AppleSpellChecker::AppleSpellChecker(): d(new Private)
51 {
52 }
53
54
55 AppleSpellChecker::~AppleSpellChecker()
56 {
57         delete d;
58 }
59
60
61 SpellChecker::Result AppleSpellChecker::check(WordLangTuple const & word)
62 {
63         string const word_str = to_utf8(word.word());
64         int const word_ok = checkAppleSpeller(d->speller, word_str.c_str(), word.lang()->code().c_str());
65         return (word_ok) ? OK : UNKNOWN_WORD;
66 }
67
68
69 // add to personal dictionary
70 void AppleSpellChecker::insert(WordLangTuple const & word)
71 {
72         string const word_str = to_utf8(word.word());
73         learnAppleSpeller(d->speller, word_str.c_str());
74 }
75
76
77 // ignore for session
78 void AppleSpellChecker::accept(WordLangTuple const & word)
79 {
80         string const word_str = to_utf8(word.word());
81         ignoreAppleSpeller(d->speller, word_str.c_str());
82 }
83
84
85 void AppleSpellChecker::suggest(WordLangTuple const & wl,
86         docstring_list & suggestions)
87 {
88         suggestions.clear();
89         string const word_str = to_utf8(wl.word());
90         size_t num = makeSuggestionAppleSpeller(d->speller, word_str.c_str(), wl.lang()->code().c_str());
91         for (size_t i = 0; i < num; i++) {
92                 char const * next = getSuggestionAppleSpeller(d->speller, i);
93                 if (!next) break;
94                 suggestions.push_back(from_utf8(next));
95         }
96 }
97
98
99 bool AppleSpellChecker::hasDictionary(Language const * lang) const
100 {
101         return hasLanguageAppleSpeller(d->speller,lang->code().c_str());
102 }
103
104
105 docstring const AppleSpellChecker::error()
106 {
107         return docstring();
108 }
109
110
111 } // namespace lyx