-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHunSpellDialog.pas
316 lines (291 loc) · 8.6 KB
/
HunSpellDialog.pas
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
{
THunSpellDialog
Copyright (C) 2010, Stefan Ascher
@abstract(Component to implement spell checking with a Dialog.)
@author(Stefan Ascher <[email protected]>)
@created(25-11-2010)
@cvs($Date: 2010/12/13 03:21:41 $)
$Id: HunSpellDialog.pas,v 1.3 2010/12/13 03:21:41 Stefan Ascher Exp $
}
unit HunSpellDialog;
{$i HunSpell.inc}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
HunSpell, StdCtrls, dlgCheckSpelling;
type
{ Result of @link(CorrectWord) }
TCorrectResult = (
crChange, {< Change the word }
crNoChange, {< Don't change the word }
crCancel {< Stop checking }
);
{
Fired before correcting a word.
@param Sender The sending object.
@param AWord The word to correct.
@param ASkip If @true it skips this word.
}
TCorrectEvent = procedure(Sender: TObject; AWord: string; var ASkip: boolean) of object;
{
Finished with checking the Memo.
@param Sender The sending object.
@param ACanceled Is @true when checking was canceled.
}
TFinishedEvent = procedure(Sender: TObject; const ACanceled: boolean) of object;
{
Component for check spelling a TMemo with a dialog.
}
THunSpellDialog = class(TComponent)
private
{ Private declarations }
fHunSpell: THunSpell;
fMemo: TCustomMemo;
fOnCorrect: TCorrectEvent;
fOnCheckStart: TNotifyEvent;
fOnCheckFinished: TFinishedEvent;
function InternalExecute: boolean;
{
Get the word at a certain position.
@param APos The line and col of the word.
@param AStart The start position of the returned word.
@param AEnd The end position of the word.
@param The word.
}
function GetWordAt(const APos: TPoint; out AStart, AEnd: integer): string;
{
Check a single word.
@param AWord The word to check.
@return @true when the word is correctly spelled.
}
function SpellCheck(const AWord: string): boolean;
{
Correct a wrong word. Shows the dialog.
@param ADlg The spell checker dialog.
@param AWord The wrong word.
@param AChange The new word.
@return crChange to change the word; crNoChange to not change the word; crCancel to close the dialog.
}
function CorrectWord(ADlg: TCheckSpellingDialog; const AWord: string; out AChange: string): TCorrectResult;
function GetWordPos(const APos: TPoint; out AStart, AEnd: integer): boolean;
function GetLineOffset(const ALn: integer): integer;
function GetVisibleLines: integer;
protected
{ Protected declarations }
procedure DoOnCorrect(AWord: string; var ASkip: boolean); dynamic;
public
{ Public declarations }
{
Check spelling.
@return @true when spell checking was not canceled.
}
function Execute: boolean;
published
{ Published declarations }
{ @link(THunSpell) object to use for spell checking. }
property HunSpell: THunSpell read fHunSpell write fHunSpell;
{ Memo to check for spelling errors. }
property Memo: TCustomMemo read fMemo write fMemo;
{ Correct a word. }
property OnCorrect: TCorrectEvent read fOnCorrect write fOnCorrect;
{ Start checking }
property OnCheckStart: TNotifyEvent read fOnCheckStart write fOnCheckStart;
{ Finished with checking }
property OnCheckFinished: TFinishedEvent read fOnCheckFinished write fOnCheckFinished;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Additional', [THunSpellDialog]);
end;
{ THunSpellDialog }
function THunSpellDialog.CorrectWord(ADlg: TCheckSpellingDialog; const AWord: string; out AChange: string): TCorrectResult;
begin
ADlg.Word := AWord;
fHunSpell.Suggest(AWord, ADlg.Suggestions);
case ADlg.ShowModal of
mrOK:
begin
// Change
AChange := ADlg.ChangeTo;
Result := crChange;
end;
mrIgnore:
begin
// Ignore all
fHunSpell.Ignore(AWord);
Result := crNoChange;
end;
mrYes:
begin
// Add
fHunSpell.Add(AWord);
Result := crNoChange;
end;
mrNo:
begin
// Ignore this
Result := crNoChange;
end;
else
// Close
Result := crCancel;
end;
end;
function THunSpellDialog.Execute: boolean;
begin
if Assigned(fHunSpell) and fHunSpell.Initialized and Assigned(fMemo) then begin
Result := InternalExecute;
end else
Result := false;
end;
procedure THunSpellDialog.DoOnCorrect(AWord: string; var ASkip: boolean);
begin
if Assigned(fOnCorrect) then
fOnCorrect(Self, AWord, ASkip);
end;
function THunSpellDialog.InternalExecute: boolean;
procedure ScollIntoView(const ALine: integer);
var
fl, ll: integer;
begin
fl := SendMessage(fMemo.Handle, EM_GETFIRSTVISIBLELINE, 0, 0);
ll := fl + GetVisibleLines;
if (Aline < fl) or (ALine >= ll) then
SendMessage(fMemo.Handle, EM_SCROLLCARET, 0, 0);
end;
var
i, c: integer;
ln, Word, WordChange: string;
dlg: TCheckSpellingDialog;
p: TPoint;
ws, we, ss, se, off: integer;
cancel, skip: boolean;
begin
if Assigned(fOnCheckStart) then
fOnCheckStart(Self);
dlg := TCheckSpellingDialog.Create(Owner);
try
dlg.btnAdd.Visible := fHunSpell.CustomDic;
cancel := false;
c := fMemo.Lines.Count;
for i := 0 to c - 1 do begin
ln := fMemo.Lines[i];
p.x := 0;
if Trim(ln) <> '' then begin
p.y := i;
Word := GetWordAt(p, ws, we);
while (we < Length(fMemo.Lines[i])) or (Word <> '') do begin
p.x := we + 1;
if Length(Word) > 1 then begin
if not SpellCheck(Word) then begin
skip := false;
DoOnCorrect(Word, skip);
if not skip then begin
off := GetLineOffset(i);
ss := ws + off;
se := we + off;
SendMessage(fMemo.Handle, EM_SETSEL, ss, se-1);
ScollIntoView(i);
case CorrectWord(dlg, Word, WordChange) of
crChange:
begin
fMemo.SelText := WordChange;
p.x := fMemo.SelStart + 1;
end;
crCancel:
begin
cancel := true;
Break;
end;
end; { case }
end; { if not skip }
end; { if not SpellCheck }
end; { if Word <> '' }
Word := GetWordAt(p, ws, we);
end; { while }
end; { if Trim(ln) <> '' }
if cancel then
Break;
end; { for }
finally
dlg.Free;
end;
if Assigned(fOnCheckFinished) then
fOnCheckFinished(Self, cancel);
Result := not cancel;
end;
function THunSpellDialog.SpellCheck(const AWord: string): boolean;
begin
if Assigned(fHunSpell) then
Result := fHunSpell.Spell(AWord)
else
Result := true;
end;
function THunSpellDialog.GetWordAt(const APos: TPoint; out AStart, AEnd: integer): string;
var
sln: string;
begin
if GetWordPos(APos, AStart, AEnd) then begin
sln := fMemo.Lines[APos.y];
Result := Copy(sln, AStart + 1, AEnd - AStart - 1);
end else
Result := '';
end;
function THunSpellDialog.GetWordPos(const APos: TPoint; out AStart, AEnd: integer): boolean;
var
ln: integer;
sln: string;
begin
ln := APos.y;
if ln < fMemo.Lines.Count then begin
sln := fMemo.Lines[ln];
if (Length(sln) > 0) and (APos.x <= Length(sln)) then begin
AStart := APos.x;
while (AStart > 0) and not IsSeparator(sln[AStart]) do
Dec(AStart);
AEnd := APos.x;
if AEnd = 0 then
AEnd := 1;
while (AEnd <= Length(sln)) and not IsSeparator(sln[AEnd]) do
Inc(AEnd);
if (AStart < AEnd) and (AStart >= 0) and (AEnd <= Length(sln)) then begin
if (sln[AStart+1] = '''') and (sln[AEnd-1] = '''') then begin
// Delete single quotes
Inc(AStart);
Dec(AEnd);
end;
end;
Result := true;
end else
Result := false;
end else
Result := false;
end;
function THunSpellDialog.GetLineOffset(const ALn: integer): integer;
begin
Result := SendMessage(fMemo.Handle, EM_LINEINDEX, ALn, 0);
end;
function THunSpellDialog.GetVisibleLines: integer;
var
OldFont: HFont;
DC: THandle;
TextMetric: TTextMetric;
cr: TRect;
begin
DC := GetDC(fMemo.Handle);
try
OldFont := SelectObject(DC, TMemo(fMemo).Font.Handle);
try
GetTextMetrics(DC, TextMetric);
cr := fMemo.ClientRect;
Result := (cr.Bottom - cr.Top) div (TextMetric.tmHeight + TextMetric.tmExternalLeading);
finally
SelectObject(DC, OldFont);
end;
finally
ReleaseDC(fMemo.Handle, DC);
end;
end;
end.