forked from sensboston/fictionbookeditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Speller.h
296 lines (264 loc) · 15.8 KB
/
Speller.h
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
#ifndef SPELLER_H
#define SPELLER_H
#include <map>
#include <set>
#include <atlutil.h>
#include "utils.h"
#include "ModelessDialog.h"
#include "Splitter.h"
// Hunspell API function prototypes
#ifdef __cplusplus
extern "C" {
#endif
#include "hunspell.h"
#ifdef __cplusplus
}
#endif
enum SPELL_RESULT
{
SPELL_MISSPELL = 0,
SPELL_OK = 1,
SPELL_IGNORE,
SPELL_IGNOREALL,
SPELL_CHANGE,
SPELL_CHANGEALL,
SPELL_ADD,
SPELL_UNDO,
SPELL_CANCEL
};
// TODO: hardcoded set of dictionaries should be changed
enum SPELL_LANG
{
LANG_EN = 0,
LANG_RU,
LANG_DE,
LANG_FR,
LANG_ES,
LANG_UA,
LANG_CZ,
LANG_BY,
LANG_BG,
LANG_PL,
LANG_IT,
LANG_NONE
};
struct DICT
{
Hunhandle* handle;
SPELL_LANG lang;
CString name;
int codepage;
};
// currently supported dictionaries
const DICT dicts[] = {
{0, LANG_EN, CString("en_US"), CP_UTF8}, // UTF-8
{0, LANG_RU, CString("ru_RU"), 20866}, // KOI8-R
{0, LANG_DE, CString("de_DE"), 28591}, // ISO 8859-1 Latin 1
{0, LANG_FR, CString("fr_FR"), CP_UTF8}, // UTF-8
{0, LANG_ES, CString("es_ES"), 28591}, // ISO 8859-1 Latin 1
{0, LANG_UA, CString("uk_UA"), CP_UTF8}, // UTF-8
{0, LANG_CZ, CString("cs_CZ"), 28592}, // ISO 8859-1 Latin 2
{0, LANG_BY, CString("be_BY"), 1251}, // Win-1251
{0, LANG_BG, CString("bg_BG"), 1251}, // Win-1251
{0, LANG_PL, CString("pl_PL"), 28592}, // ISO 8859-1 Latin 2
{0, LANG_IT, CString("it_IT"), 28605}, // ISO 8859-15 Latin 9
{0, LANG_NONE, CString(""), 0} // unsupported language
};
typedef CSimpleArray<CString> CStrings;
class CSpeller;
class CSpellDialog: public CModelessDialogImpl<CSpellDialog>
{
public:
enum { IDD = IDD_SPELL_CHECK };
CString m_sBadWord;
CString m_sReplacement;
CStrings* m_strSuggestions;
CSpellDialog(CSpeller* parent): m_Speller(parent), m_strSuggestions(NULL) {}
BEGIN_MSG_MAP(CSpellDlg)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
MESSAGE_HANDLER(WM_ACTIVATE, OnActivate)
COMMAND_ID_HANDLER(IDC_SPELL_IGNORE, OnIgnore)
COMMAND_ID_HANDLER(IDC_SPELL_IGNOREALL, OnIgnoreAll)
COMMAND_ID_HANDLER(IDC_SPELL_CHANGE, OnChange)
COMMAND_ID_HANDLER(IDC_SPELL_CHANGEALL, OnChangeAll)
COMMAND_ID_HANDLER(IDC_SPELL_ADD, OnAdd)
COMMAND_ID_HANDLER(IDC_SPELL_UNDO, OnUndo)
COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
COMMAND_HANDLER(IDC_SPELL_REPLACEMENT, EN_CHANGE, OnEditChange)
COMMAND_HANDLER(IDC_SPELL_SUGG_LIST, LBN_SELCHANGE, OnSelChange)
COMMAND_HANDLER(IDC_SPELL_SUGG_LIST, LBN_DBLCLK, OnSelDblClick)
END_MSG_MAP()
LRESULT UpdateData();
private:
CSpeller* m_Speller;
int m_RetCode;
bool m_WasSuspended;
CEdit m_BadWord;
CEdit m_Replacement;
CListBox m_Suggestions;
// buttons
CButton m_IgnoreContinue;
LRESULT OnInitDialog(UINT, WPARAM, LPARAM, BOOL&);
LRESULT OnActivate(UINT, WPARAM, LPARAM, BOOL&);
LRESULT OnIgnore(WORD, WORD wID, HWND, BOOL&);
LRESULT OnIgnoreAll(WORD, WORD wID, HWND, BOOL&);
LRESULT OnChange(WORD, WORD wID, HWND, BOOL&);
LRESULT OnChangeAll(WORD, WORD wID, HWND, BOOL&);
LRESULT OnAdd(WORD, WORD wID, HWND, BOOL&);
LRESULT OnUndo(WORD, WORD wID, HWND, BOOL&);
LRESULT OnSelChange(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
LRESULT OnSelDblClick(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
LRESULT OnEditChange(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
LRESULT OnCancel(WORD, WORD wID, HWND, BOOL&);
};
typedef std::pair<long, MSHTML::IHighlightSegmentPtr> HIGHLIGHT;
typedef std::multimap<long, MSHTML::IHighlightSegmentPtr> HIGHLIGHTS;
// tokens to tokenize string to separate words
const CString Tokens(L" .,?–!—…\r\n\t\"«»“”‘’:;<>(){}[]\u00A0\u2003\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u200B\u202F\u205F\u2060\u3000\u2012\u2013\u2014\u00BA\u25A1\u25AB\u25E6\u201e\u201c");
class CSpeller
{
public:
CSpeller(CString dictPath = L"..\\dict");
~CSpeller();
void AttachDocument(MSHTML::IHTMLDocumentPtr doc);
void SetFrame(HWND frame) { m_frame = frame; }
inline bool Available() { return (m_Dictionaries[m_Lang].handle != NULL); }
inline bool Enabled() { return m_Enabled; }
void SetDocumentLanguage();
void SetEnabled (bool Enabled)
{
if (m_Enabled != Enabled)
{
m_Enabled = Enabled;
if (!m_Enabled)
ClearAllMarks();
else
HighlightMisspells();
}
}
void SetCustomDictionary(CString pathToDictionary, UINT codePage)
{
m_CustomDictPath = pathToDictionary;
m_CustomDictCodepage = codePage;
LoadCustomDict();
}
void StartDocumentCheck(MSHTML::IMarkupServices2Ptr undoSrv = NULL);
void ContinueDocumentCheck();
void EndDocumentCheck(bool bCancel = true);
// undo support
void Undo()
{
if (m_browser)
{
IOleCommandTargetPtr ct(m_browser);
if (ct)
ct->Exec(&CGID_MSHTML, IDM_UNDO, 0, NULL, NULL);
}
}
bool GetUndoState()
{
bool stat = false;
if (m_browser)
{
static OLECMD cmd[] = {IDM_UNDO};
IOleCommandTargetPtr ct(m_browser);
if (ct)
{
ct->QueryStatus(&CGID_MSHTML, 1, cmd, NULL);
stat = (cmd[0].cmdf != 1);
}
}
return stat;
}
void BeginUndoUnit(const wchar_t *name)
{
ATLASSERT(m_undoSrv);
m_undoSrv->BeginUndoUnit((wchar_t *)name);
}
void EndUndoUnit()
{
ATLASSERT(m_undoSrv);
m_undoSrv->EndUndoUnit();
}
void CheckScroll();
void CheckElement(MSHTML::IHTMLElementPtr elem, long uniqID, bool HTMLChanged);
void CheckCurrentPage();
// main function
SPELL_RESULT SpellCheck(CString word);
CStrings* GetSuggestions(CString word);
void MarkElement(MSHTML::IHTMLElementPtr elem, long uniqID, CString word, int pos);
void ClearMarks(int elemID);
void ClearAllMarks();
// SeNS
void SetHighlightMisspells(bool Enabled)
{
if (Enabled != m_HighlightMisspells)
{
m_HighlightMisspells = Enabled;
if (!m_HighlightMisspells)
ClearAllMarks();
else
HighlightMisspells();
}
}
void HighlightMisspells();
void AdvanceVersionNumber(int delta)
{
::PostMessage(m_frame, WM_COMMAND,MAKELONG(ID_VER_ADVANCE,delta), 0);
}
// popup menu service
void AppendSpellMenu (HMENU menu);
void Replace(int nIndex);
void Replace(CString word);
void IgnoreAll(CString word = CString(""));
void AddToDictionary();
void AddToDictionary(CString word);
void AddReplacement(CString badWord, CString goodWord)
{
m_ChangeWords.Add(badWord);
m_ChangeWordsTo.Add(goodWord);
}
protected:
SHD::IWebBrowser2Ptr m_browser;
MSHTML::IHTMLDocument2Ptr m_doc2;
MSHTML::IHTMLDocument3Ptr m_doc3;
MSHTML::IHTMLDocument4Ptr m_doc4;
MSHTML::IHTMLElementPtr m_fbw_body;
MSHTML::IHTMLElement2Ptr m_scrollElement;
MSHTML::IHTMLRenderStylePtr m_irs;
MSHTML::IMarkupContainer2Ptr m_mkc;
MSHTML::IMarkupServicesPtr m_ims;
MSHTML::IDisplayServicesPtr m_ids;
MSHTML::IHighlightRenderingServicesPtr m_ihrs;
// for document checking
MSHTML::IHTMLTxtRangePtr m_prevSelRange;
MSHTML::IHTMLTxtRangePtr m_selRange;
MSHTML::IMarkupPointerPtr m_impStart;
MSHTML::IMarkupPointerPtr m_impEnd;
MSHTML::IMarkupServices2Ptr m_undoSrv;
CSpellDialog* m_spell_dlg;
bool m_Enabled;
bool m_HighlightMisspells;
int m_prevY, m_codePage, m_numAphChanged;
HWND m_frame;
SPELL_LANG m_Lang;
CSimpleArray<DICT> m_Dictionaries;
CStrings m_IgnoreWords;
CStrings m_ChangeWords;
CStrings m_ChangeWordsTo;
CStrings m_CustomDict;
CStrings* m_menuSuggestions;
std::set<long> m_uniqIDs;
HIGHLIGHTS m_ElementHighlights;
CString m_DictPath;
CString m_CustomDictPath;
DWORD m_CustomDictCodepage;
Hunhandle* LoadDictionary(CString dictPath, CString dictName);
Hunhandle* GetDictionary(CString word);
void LoadCustomDict();
void SaveCustomDict();
MSHTML::IHTMLTxtRangePtr GetSelWordRange();
CString GetSelWord();
CSplitter* splitter;
};
#endif