[1693] | 1 | unit skaSpellCheck;
|
---|
| 2 | (* ***************************** BEGIN LICENSE BLOCK **********************
|
---|
| 3 | *
|
---|
| 4 | * Copyright (C) 2015
|
---|
| 5 | * Sunil Kumar Arora (digitiger@gmail.com sunil@healthsevak.com)
|
---|
| 6 | * All Rights Reserved.
|
---|
| 7 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
| 8 | *
|
---|
| 9 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
| 10 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
| 11 | * the License. You may obtain a copy of the License at
|
---|
| 12 | * http://www.mozilla.org/MPL/
|
---|
| 13 | *
|
---|
| 14 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
| 15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
| 16 | * for the specific language governing rights and limitations under the
|
---|
| 17 | * License.
|
---|
| 18 | *
|
---|
| 19 | * Alternatively, the content of this file maybe used under the terms of either
|
---|
| 20 | * the GNU General Public License Version 2 or later (the "GPL"), or the GNU
|
---|
| 21 | * Lesser General Public License Version 2.1 or later (the "LGPL"), in which
|
---|
| 22 | * case the provisions of the GPL or the LGPL are applicable instead of those
|
---|
| 23 | * above. If you wish to allow use of your version of this file only under the
|
---|
| 24 | * terms of either the GPL or the LGPL, and not to allow others to use your
|
---|
| 25 | * version of this file under the terms of the MPL, indicate your division by
|
---|
| 26 | * deleting the provisions above and replace them with the notice and other
|
---|
| 27 | * provisions required by the GPL or LGPL. If you do not delete the provisions
|
---|
| 28 | * above, a recipient may use your version of this file under the terms of any
|
---|
| 29 | * one of the MPL, the GPL or the LGPL.
|
---|
| 30 | *
|
---|
| 31 | * *********************** END LICENSE BLOCK *********************************)
|
---|
| 32 |
|
---|
| 33 | interface
|
---|
| 34 |
|
---|
| 35 | uses
|
---|
| 36 | Windows, Classes, SysUtils, ComCtrls, StdCtrls, Graphics;
|
---|
| 37 |
|
---|
| 38 | const
|
---|
| 39 | AboutThis = 'A wrapper component developed by Sunil K Arora (digitiger@gmail.com) of HealthSevak using OpenSource HanSpell engine';
|
---|
| 40 | type
|
---|
| 41 | TSpellState = (ssNotStarted, ssChecking, ssCancelled, ssCompleted);
|
---|
| 42 |
|
---|
| 43 | TskaHunSpellChecker = class(TComponent)
|
---|
| 44 | private
|
---|
| 45 | FActiveOrLoaded: Boolean;
|
---|
| 46 | FpointerHunLib: Pointer;
|
---|
| 47 | FSourceEdit: TRichEdit;
|
---|
| 48 | FSuggestionList: TListbox;
|
---|
| 49 |
|
---|
| 50 | FAffixFileName: string;
|
---|
| 51 | FDictFileName: string;
|
---|
| 52 | CurrentWord: String;
|
---|
| 53 | CurrentText: String;
|
---|
| 54 | FoundAt: Integer;
|
---|
| 55 | PosOfFirstCharInCurrentLine: integer;
|
---|
| 56 | CurrentLine: Integer;
|
---|
| 57 | FIgnore: TStringList;
|
---|
| 58 | WaitForUser: Boolean;
|
---|
| 59 | WordLength:integer;
|
---|
| 60 | WordPos: Integer;
|
---|
| 61 | PREditorWndProc:pointer;
|
---|
| 62 | FHighlightColor: TColor;
|
---|
| 63 | FShowCompletion: Boolean;
|
---|
| 64 | FpointerSpellComplete: String;
|
---|
| 65 | FStatus: TSpellState;
|
---|
| 66 | FUndoList: TStringList;
|
---|
| 67 | FCustDict: TStringList;
|
---|
| 68 | FCustom: String;
|
---|
| 69 | FModified: Boolean;
|
---|
| 70 | FHighlightEdit: TEdit;
|
---|
| 71 | FbtnClose: TButton;
|
---|
[1694] | 72 | FTxtBeforeManualEdit: String;
|
---|
[1693] | 73 | function AddCustomWord(aWord: String; isInternal: Boolean = False): Boolean;
|
---|
| 74 | overload; virtual;
|
---|
| 75 | Function CurrentWordDetail(WithPosition: Boolean= True): String;
|
---|
| 76 | function GetActive: Boolean;
|
---|
| 77 | function GetStatus: TSpellState;
|
---|
| 78 | procedure Initialize;
|
---|
| 79 | procedure SetActive(const Value: Boolean);
|
---|
| 80 | procedure SetAffixFileName(const Value: string);
|
---|
| 81 | procedure SetbtnClose(const Value: TButton);
|
---|
| 82 | procedure SetCustomDict(const Value: String);
|
---|
| 83 | procedure SetDictFileName(const Value: string);
|
---|
| 84 | procedure SetHighLightEdit(const Value: TEdit);
|
---|
| 85 | procedure SetSourceEdit(const Value: TRichEdit);
|
---|
| 86 | Function ShowMisSpelledWord:boolean;
|
---|
| 87 | procedure Loaded; override;
|
---|
| 88 | procedure ReplaceCurrentWordWith(const aNewWord: String);
|
---|
| 89 | function GetAboutThis: String;
|
---|
[1694] | 90 | procedure SaveForUndo(const Ignoring: Boolean=False);
|
---|
[1693] | 91 | public
|
---|
| 92 | constructor Create(AOwner: TComponent); overload; override;
|
---|
| 93 | constructor Create(AOwner: TComponent; SourceTextRichEdit: TRichedit;
|
---|
| 94 | SuggestList: TListbox); ReIntroduce; overload;
|
---|
| 95 | destructor Destroy; override;
|
---|
| 96 |
|
---|
| 97 | function AbortSpellCheck(Verbose: Boolean = True):Boolean;
|
---|
| 98 | function AddCustomWord: Boolean; overload; virtual;
|
---|
| 99 | procedure Change;
|
---|
| 100 | procedure ChangeAll;
|
---|
| 101 | procedure CheckSpelling;
|
---|
| 102 | procedure Close; virtual;
|
---|
| 103 | procedure CorrectWithMyWord;
|
---|
| 104 | procedure GetSuggestions(const aMisSpeltWord: string;
|
---|
| 105 | const SuggestionList: TStrings); dynamic;
|
---|
| 106 | procedure IgnoreAll;
|
---|
| 107 | procedure IgnoreOnce;
|
---|
| 108 | function IsMisspelled(const AWord: string): Boolean; dynamic;
|
---|
[1694] | 109 | procedure ManualChangeStart;
|
---|
| 110 | procedure ManualChangeDone;
|
---|
[1693] | 111 | function Open:Boolean; virtual;
|
---|
| 112 | function ReStart: Boolean; virtual;
|
---|
[1694] | 113 |
|
---|
[1693] | 114 | function Undo: Boolean;
|
---|
| 115 |
|
---|
| 116 |
|
---|
| 117 | property SpellCheckState: TSpellState read GetStatus default ssNotStarted;
|
---|
| 118 | published
|
---|
| 119 | property About: String read GetAboutThis;
|
---|
| 120 | property Active: Boolean read GetActive write SetActive;
|
---|
| 121 | property AffixFileName: string read FAffixFileName write SetAffixFileName;
|
---|
| 122 | property btnClose: TButton read FbtnClose write SetbtnClose;
|
---|
| 123 | property CustDictionaryFile: String read FCustom write SetCustomDict;
|
---|
| 124 | property DictionaryFileName:string read FDictFileName write SetDictFileName;
|
---|
| 125 | property ColorForMisspelled: TColor read FHighlightColor write FHighlightColor default clRed;
|
---|
| 126 | property MisSpeltWord: TEdit read FHighlightEdit write SetHighLightEdit;
|
---|
| 127 | property IsModified: Boolean read FModified;
|
---|
| 128 | property ShowCompletionMessage: Boolean read FShowCompletion write FShowCompletion default True;
|
---|
| 129 | property SourceTextControl: TRichEdit read FSourceEdit write SetSourceEdit;
|
---|
| 130 | property SpellCheckCompletionMessage: String read FpointerSpellComplete write FpointerSpellComplete;
|
---|
| 131 | property SuggestionList: TListbox read FSuggestionList write FSuggestionList;
|
---|
| 132 |
|
---|
| 133 | end;
|
---|
| 134 |
|
---|
| 135 | procedure Register;
|
---|
| 136 |
|
---|
| 137 | Const
|
---|
| 138 | CompletionMessage = 'Spell Check Complete.';
|
---|
| 139 | CaptionForNewWord = 'New Word Suggestion';
|
---|
| 140 | ConfirmAbort = 'Really abort?';
|
---|
| 141 | PromptForNewWord = 'Specify the replacement for current mis-spelt word:';
|
---|
| 142 | DLLNotLoaded = 'Failed to load SpellCheck Engine DLL.';
|
---|
| 143 | MisSpeltReplacement = 'The new word specified by you "%s" looks mis-spelt!'
|
---|
| 144 | +' Would you want to still use it? Click NO button '
|
---|
| 145 | +'to specify better replacement word.';
|
---|
| 146 | var
|
---|
| 147 | OldRichEditWndProc: {integer}pointer;
|
---|
| 148 | CurrentMe: TskaHunSpellChecker;
|
---|
| 149 | implementation
|
---|
| 150 | uses messages, Dialogs, RichEdit, SHFolder, Forms, uHunSpellLib;
|
---|
| 151 |
|
---|
| 152 | procedure Register;
|
---|
| 153 | begin
|
---|
| 154 | RegisterComponentsProc('SkA Utility', [TskaHunSpellChecker]);
|
---|
| 155 | end;
|
---|
| 156 |
|
---|
| 157 | { TskaHunSpellChecker }
|
---|
| 158 |
|
---|
| 159 | function TskaHunSpellChecker.AbortSpellCheck(Verbose: Boolean = True): Boolean;
|
---|
| 160 | begin
|
---|
| 161 | Result := (not isModified) or
|
---|
| 162 | (not Verbose) or (MessageDlg(ConfirmAbort, mtConfirmation,
|
---|
| 163 | [mbYes, mbNo],0, mbNo) = 6);
|
---|
| 164 |
|
---|
| 165 | if Result then
|
---|
| 166 | FStatus := ssCancelled;
|
---|
| 167 | end;
|
---|
| 168 |
|
---|
| 169 | function TskaHunSpellChecker.AddCustomWord(aWord: String; isInternal: Boolean = False): Boolean;
|
---|
| 170 | begin
|
---|
| 171 | Result := False;
|
---|
| 172 | if (trim(aWord) = '') or (not assigned(SourceTextControl)) or (not assigned(SuggestionList)) then
|
---|
| 173 |
|
---|
| 174 |
|
---|
| 175 | Result := False;
|
---|
| 176 | if (not Active) then Exit;
|
---|
| 177 | uHunSpellLib.hunspell_put_word(FpointerHunLib, PAnsiChar(AnsiString(aWord)));
|
---|
| 178 | Result := True;
|
---|
| 179 | end;
|
---|
| 180 |
|
---|
| 181 | procedure TskaHunSpellChecker.ChangeAll;
|
---|
| 182 | begin
|
---|
| 183 | if (SpellCheckState <> ssChecking) or (not assigned(SourceTextControl)) or (not assigned(SuggestionList)) then
|
---|
| 184 | exit;
|
---|
| 185 | SaveForUndo;
|
---|
| 186 | SourceTextControl.Text := StringReplace(SourceTextControl.Text,
|
---|
| 187 | CurrentWord, SuggestionList.Items[SuggestionList.ItemIndex],
|
---|
| 188 | [rfReplaceAll,rfIgnoreCase]);
|
---|
| 189 | WaitForUser := False;
|
---|
| 190 | FModified := True;
|
---|
| 191 | SourceTextControl.Invalidate;
|
---|
| 192 |
|
---|
| 193 | end;
|
---|
| 194 |
|
---|
| 195 | function TskaHunSpellChecker.AddCustomWord: Boolean;
|
---|
| 196 | begin
|
---|
| 197 | Result := AddCustomWord(CurrentWord, False);
|
---|
| 198 | FCustdict.Add(CurrentWord);
|
---|
| 199 | WaitForUser := False;
|
---|
| 200 | AbortSpellCheck(False);
|
---|
| 201 | Initialize;
|
---|
| 202 | CheckSpelling;
|
---|
| 203 | ShowMisSpelledWord;
|
---|
| 204 | end;
|
---|
| 205 |
|
---|
| 206 | procedure TskaHunSpellChecker.ReplaceCurrentWordWith(const aNewWord: String);
|
---|
| 207 | var
|
---|
| 208 | full: String;
|
---|
| 209 | prefix: string;
|
---|
| 210 | suffix: string;
|
---|
| 211 | begin
|
---|
| 212 | full := SourceTextControl.Lines[CurrentLine];
|
---|
| 213 | prefix := copy(CurrentText, 1, WordPos-1);
|
---|
| 214 | Suffix := copy(CurrentText, WordPos+WordLength,
|
---|
| 215 | length(CurrentText));
|
---|
| 216 | SaveForUndo;
|
---|
| 217 | SourceTextControl.Lines[CurrentLine] :=prefix + aNewWord + suffix;
|
---|
| 218 | WaitForUser := False;
|
---|
| 219 | FStatus := ssChecking;
|
---|
| 220 | FModified := True;
|
---|
| 221 | SourceTextControl.Invalidate;
|
---|
| 222 | end;
|
---|
| 223 |
|
---|
| 224 | function TskaHunSpellChecker.ReStart: Boolean;
|
---|
| 225 | begin
|
---|
| 226 | Close;
|
---|
| 227 | Result := Open;
|
---|
| 228 | Initialize;
|
---|
| 229 | WaitForUser := False;
|
---|
| 230 | SourceTextControl.Invalidate;
|
---|
| 231 | Result := not WaitForUser;
|
---|
| 232 | end;
|
---|
| 233 |
|
---|
| 234 | procedure TskaHunSpellChecker.Change;
|
---|
| 235 |
|
---|
| 236 | begin
|
---|
| 237 | if (SpellCheckState <> ssChecking) or (not assigned(SourceTextControl)) or (not assigned(SuggestionList)) then
|
---|
| 238 | exit;
|
---|
| 239 | ReplaceCurrentWordWith(SuggestionList.Items[SuggestionList.ItemIndex]);
|
---|
| 240 | end;
|
---|
| 241 |
|
---|
| 242 | procedure TskaHunSpellChecker.CheckSpelling;
|
---|
| 243 | begin
|
---|
| 244 | if (SpellCheckState = ssChecking) or (not assigned(SourceTextControl)) or (not assigned(SuggestionList)) then
|
---|
| 245 | exit;
|
---|
| 246 |
|
---|
| 247 | Initialize;
|
---|
| 248 | FUndoList.Clear;
|
---|
| 249 | WaitForUser := False;
|
---|
| 250 | FStatus := ssChecking;
|
---|
| 251 | SourceTextControl.Invalidate;
|
---|
| 252 | //SourceTextControl.Invalidate;
|
---|
| 253 | end;
|
---|
| 254 |
|
---|
| 255 | procedure TskaHunSpellChecker.Close;
|
---|
| 256 | begin
|
---|
| 257 | if not Active then Exit;
|
---|
| 258 | uHunSpellLib.hunspell_uninitialize(FpointerHunLib);
|
---|
| 259 | FpointerHunLib := nil;
|
---|
| 260 | end;
|
---|
| 261 |
|
---|
| 262 |
|
---|
| 263 | procedure TskaHunSpellChecker.CorrectWithMyWord;
|
---|
| 264 | var
|
---|
| 265 | NewWord: String;
|
---|
| 266 | GotIt: Boolean;
|
---|
| 267 | begin
|
---|
| 268 | if (SpellCheckState <> ssChecking) or (not assigned(SourceTextControl)) or (not assigned(SuggestionList)) then
|
---|
| 269 | exit;
|
---|
| 270 |
|
---|
| 271 | if SuggestionList.Count > 0 then
|
---|
| 272 | NewWord := SuggestionList.Items[0]
|
---|
| 273 | else
|
---|
| 274 | NewWord := CurrentWord;
|
---|
| 275 |
|
---|
| 276 | GotIt := False;
|
---|
| 277 | while not GotIt do
|
---|
| 278 | begin
|
---|
| 279 | if not InputQuery(CaptionForNewWord, PromptForNewWord, NewWord) then
|
---|
| 280 | exit;
|
---|
| 281 |
|
---|
| 282 | GotIt := (not IsMisspelled(NewWord))
|
---|
| 283 | or (MessageDlg(Format(MisSpeltReplacement,[NewWord]),
|
---|
| 284 | mtWarning, [mbYes, mbNo],0, mbNo) =6) ;
|
---|
| 285 | end;
|
---|
| 286 |
|
---|
| 287 | if IsMisspelled(NewWord) then
|
---|
| 288 | AddCustomWord(NewWord, True);
|
---|
| 289 |
|
---|
| 290 | ReplaceCurrentWordWith(NewWord);
|
---|
| 291 | end;
|
---|
| 292 |
|
---|
| 293 | constructor TskaHunSpellChecker.Create(AOwner: TComponent);
|
---|
| 294 | function GetSpecialFolderPath(folder : integer) : string;
|
---|
| 295 | var
|
---|
| 296 | path: array [0..MAX_PATH] of char;
|
---|
| 297 | begin
|
---|
| 298 | if SUCCEEDED(SHGetFolderPath(0,folder,0,0,@path[0])) then
|
---|
| 299 | Result := path
|
---|
| 300 | else
|
---|
| 301 | Result := '';
|
---|
| 302 | end;
|
---|
| 303 | begin
|
---|
| 304 | inherited;
|
---|
| 305 |
|
---|
| 306 | ColorForMisspelled := clRed;
|
---|
| 307 | ShowCompletionMessage := True;
|
---|
| 308 | SpellCheckCompletionMessage := CompletionMessage;
|
---|
| 309 |
|
---|
| 310 |
|
---|
| 311 |
|
---|
| 312 | CurrentMe := Self;
|
---|
| 313 | FIgnore := TStringList.Create;
|
---|
| 314 | FCustDict := TStringList.Create;
|
---|
| 315 |
|
---|
| 316 | CustDictionaryFile := IncludeTrailingPathDelimiter(GetSpecialFolderPath(CSIDL_PERSONAL)) + 'CustomDictionary.txt';
|
---|
| 317 | if FileExists(CustDictionaryFile) then
|
---|
| 318 | try
|
---|
| 319 | FCustDict.LoadFromFile(CustDictionaryFile);
|
---|
| 320 | except
|
---|
| 321 | end;
|
---|
| 322 |
|
---|
| 323 | FUndoList := TStringList.Create;
|
---|
| 324 |
|
---|
| 325 | FStatus := ssNotStarted;
|
---|
| 326 | WaitForUser := False;
|
---|
| 327 | WordPos := 0;
|
---|
| 328 | end;
|
---|
| 329 |
|
---|
| 330 | constructor TskaHunSpellChecker.Create(AOwner: TComponent;
|
---|
| 331 | SourceTextRichEdit: TRichedit; SuggestList: TListbox);
|
---|
| 332 | begin
|
---|
| 333 | create(AOwner);
|
---|
| 334 | SourceTextControl := SourceTextRichEdit;
|
---|
| 335 | SuggestionList := SuggestList;
|
---|
| 336 | end;
|
---|
| 337 |
|
---|
| 338 | function TskaHunSpellChecker.CurrentWordDetail(WithPosition: Boolean): String;
|
---|
| 339 | begin
|
---|
| 340 | Result := '$$' + CurrentWord + '$$';
|
---|
| 341 | if WithPosition then
|
---|
| 342 | Result := '$$' + IntToStr(FoundAt+1) + Result;
|
---|
| 343 | end;
|
---|
| 344 |
|
---|
| 345 | destructor TskaHunSpellChecker.Destroy;
|
---|
| 346 | begin
|
---|
| 347 | Close;
|
---|
| 348 | FIgnore.clear;
|
---|
| 349 | FreeAndNil(FIgnore);
|
---|
| 350 | FreeAndNil(FUndoList);
|
---|
| 351 | if not (csDesigning in ComponentState) then
|
---|
| 352 | try
|
---|
| 353 | if FCustDict.Count > 0 then
|
---|
| 354 | try
|
---|
| 355 | FCustDict.SaveToFile(CustDictionaryFile);
|
---|
| 356 | except
|
---|
| 357 | end;
|
---|
| 358 | finally
|
---|
| 359 | FCustDict.Free;
|
---|
| 360 | end;
|
---|
| 361 | inherited;
|
---|
| 362 | end;
|
---|
| 363 |
|
---|
| 364 | function TskaHunSpellChecker.GetAboutThis: String;
|
---|
| 365 | begin
|
---|
| 366 | Result := AboutThis;
|
---|
| 367 | end;
|
---|
| 368 |
|
---|
| 369 | function TskaHunSpellChecker.GetActive: Boolean;
|
---|
| 370 | begin
|
---|
| 371 | Result := (FpointerHunLib <> nil);
|
---|
| 372 | end;
|
---|
| 373 |
|
---|
| 374 | function TskaHunSpellChecker.GetStatus: TSpellState;
|
---|
| 375 | begin
|
---|
| 376 | Result := FStatus;
|
---|
| 377 | end;
|
---|
| 378 |
|
---|
| 379 | procedure TskaHunSpellChecker.GetSuggestions(const aMisSpeltWord: string;
|
---|
| 380 | const SuggestionList: TStrings);
|
---|
| 381 | var
|
---|
| 382 | i: Integer;
|
---|
| 383 | pMisSpelt: PAnsiChar;
|
---|
| 384 | suggestions: PPAnsiChar;
|
---|
| 385 | Results: PPAnsiChar;
|
---|
| 386 | Count: Integer;
|
---|
| 387 | begin
|
---|
| 388 | if (not Active) or (not Assigned(SuggestionList)) then
|
---|
| 389 | exit;
|
---|
| 390 |
|
---|
| 391 | pMisSpelt := PAnsiChar(AnsiString(aMisSpeltWord));
|
---|
| 392 |
|
---|
| 393 | if not uHunSpellLib.hunspell_spell(FpointerHunLib, pMisSpelt) then
|
---|
| 394 | uHunSpellLib.hunspell_suggest_auto(FpointerHunLib, pMisSpelt, suggestions);
|
---|
| 395 | begin
|
---|
| 396 | Count := uHunSpellLib.hunspell_suggest(FpointerHunLib, pMisSpelt, suggestions);
|
---|
| 397 | Results := suggestions;
|
---|
| 398 | for i := 1 to Count do
|
---|
| 399 | begin
|
---|
| 400 | SuggestionList.Add(Results^);
|
---|
| 401 | Inc(Integer(Results), SizeOf(Pointer));
|
---|
| 402 | end;
|
---|
| 403 | uHunSpellLib.hunspell_suggest_free(FpointerHunLib, suggestions, Count);
|
---|
| 404 | end;
|
---|
| 405 | end;
|
---|
| 406 |
|
---|
| 407 | function TskaHunSpellChecker.ShowMisSpelledWord: boolean;
|
---|
| 408 | var
|
---|
| 409 | I , l :integer;
|
---|
| 410 | CharPosion:integer;
|
---|
| 411 | FirstVisibleLine, LastVisibleLine:integer;
|
---|
| 412 |
|
---|
| 413 | hndl: hwnd;
|
---|
| 414 | dcForHndl: THandle;
|
---|
| 415 | visrect:Trect;
|
---|
| 416 | vispoint:TPoint;
|
---|
| 417 | procedure ShowMisSpelletWord;
|
---|
| 418 | begin
|
---|
| 419 | if Assigned(FHighlightEdit) then
|
---|
| 420 | begin
|
---|
| 421 | FHighlightEdit.Font.Color := ColorForMisspelled;
|
---|
| 422 | FHighlightEdit.Text := CurrentWord;
|
---|
| 423 | FHighlightEdit.Show;
|
---|
| 424 | end ;
|
---|
| 425 |
|
---|
| 426 | if ((PosOfFirstCharInCurrentLine + FoundAt) < 1) then
|
---|
| 427 | exit;
|
---|
| 428 |
|
---|
| 429 | SendMessage (SourceTextControl.Handle, EM_POSFROMCHAR, integer(@VisPoint), PosOfFirstCharInCurrentLine + FoundAt-1);
|
---|
| 430 | SetTextColor(dcForHndl, ColorForMisspelled);
|
---|
| 431 | TextOut(dcForHndl, VisPoint.x, VisPoint.y, pchar(CurrentWord), WordLength);
|
---|
| 432 | end;
|
---|
| 433 | begin
|
---|
| 434 | Result := False;
|
---|
| 435 | if (SpellCheckState <> ssChecking) or (not assigned(SourceTextControl))
|
---|
| 436 | or (not assigned(SuggestionList)) then
|
---|
| 437 | exit;
|
---|
| 438 |
|
---|
| 439 | hndl:=SourceTextControl.Handle;
|
---|
| 440 |
|
---|
| 441 | result:= SendMessage (hndl, EM_GETRECT, 0, integer(@visrect))=0;
|
---|
| 442 |
|
---|
| 443 | dcForHndl := getdc(hndl);
|
---|
| 444 |
|
---|
| 445 | if result then
|
---|
| 446 | begin
|
---|
| 447 | // VisPoint := visrect.BottomRight;
|
---|
| 448 | vispoint.Y := visrect.Bottom;
|
---|
| 449 | vispoint.X := visrect.Right;
|
---|
| 450 | CharPosion := SendMessage (hndl, EM_CHARFROMPOS, 0, integer(@VisPoint));
|
---|
| 451 | LASTVISIBLELINE := SendMessage (hndl, EM_LINEFROMCHAR, CharPosion, 0);
|
---|
| 452 | FIRSTVISIBLELINE := SendMessage (hndl, EM_GETFIRSTVISIBLELINE, 0, 0);
|
---|
| 453 |
|
---|
| 454 | SetBkMode (dcForHndl, TRANSPARENT);
|
---|
| 455 | SelectObject(dcForHndl, SourceTextControl.font.Handle);
|
---|
| 456 | i := 0;
|
---|
| 457 |
|
---|
| 458 | if WaitForUser then
|
---|
| 459 | begin
|
---|
| 460 | ShowMisSpelletWord;
|
---|
| 461 | exit;
|
---|
| 462 | end;
|
---|
| 463 |
|
---|
| 464 | For l := 0 to SourceTextControl.Lines.Count -1 do
|
---|
| 465 | begin
|
---|
| 466 | {$R-}
|
---|
| 467 | CurrentLine := l;
|
---|
| 468 | if trim(SourceTextControl.Lines[CurrentLine]) = '' then
|
---|
| 469 | continue;
|
---|
| 470 |
|
---|
| 471 | CurrentText := ' ' + SourceTextControl.Lines[CurrentLine];
|
---|
| 472 | PosOfFirstCharInCurrentLine := SendMessage (SourceTextControl.Handle, EM_LINEINDEX, CurrentLine, 0);
|
---|
| 473 | i := 0;
|
---|
| 474 |
|
---|
| 475 | While i <= LENgth(CurrentText) do
|
---|
| 476 | begin
|
---|
| 477 | FoundAt := i -1;
|
---|
| 478 | if Assigned(FHighlightEdit) then
|
---|
| 479 | FHighlightEdit.Hide;
|
---|
| 480 |
|
---|
| 481 |
|
---|
| 482 | //SuggestionList.Clear;
|
---|
| 483 | {Any character except these will count as a word delimiter}
|
---|
| 484 | While CurrentText[i] in ['A'..'Z','a'..'z','0'..'9'] do inc(i);
|
---|
| 485 |
|
---|
| 486 | WordLength := i- FoundAt -1;
|
---|
| 487 | WordPos := i-WordLength;
|
---|
| 488 | CurrentWord := copy(CurrentText, WordPos, WordLength);
|
---|
| 489 | If ((FIgnore.IndexOf(CurrentWordDetail(True))< 0) //SingelIgnore
|
---|
| 490 | and (FIgnore.IndexOf(CurrentWordDetail(False))< 0) //IgnoreAll
|
---|
| 491 | and (IsMisspelled(CurrentWord))) Then
|
---|
| 492 | begin
|
---|
| 493 | GetSuggestions(CurrentWord, SuggestionList.Items);
|
---|
| 494 | if SuggestionList.Count > 0 then
|
---|
| 495 | SuggestionList.ItemIndex := 0;
|
---|
| 496 | ShowMisSpelletWord;
|
---|
| 497 | if CurrentLine > LastVisibleLine then
|
---|
| 498 | SendMessage(SourceTextControl.Handle, EM_LINESCROLL, 0, (CurrentLine - lastvisibleLine)+5);
|
---|
| 499 | WaitForUser := True;
|
---|
| 500 | exit;
|
---|
| 501 | End
|
---|
| 502 | else
|
---|
| 503 | SuggestionList.Clear;
|
---|
| 504 | inc(i);
|
---|
| 505 | end;
|
---|
| 506 | end;
|
---|
| 507 | if (CurrentLine >= SourceTextControl.Lines.Count-1) and (i >= length(CurrentText) +1) then
|
---|
| 508 | begin
|
---|
| 509 | FStatus := ssCompleted;
|
---|
| 510 | if Assigned(btnClose) then
|
---|
| 511 | btnClose.Click;
|
---|
| 512 | end;
|
---|
| 513 | {$R+}
|
---|
| 514 | end;
|
---|
| 515 | ReleaseDC(SourceTextControl.Handle, dcForHndl);
|
---|
| 516 |
|
---|
| 517 | End;
|
---|
| 518 |
|
---|
| 519 | function TskaHunSpellChecker.Undo: Boolean;
|
---|
| 520 | var
|
---|
| 521 | tmpStr: String;
|
---|
| 522 | tmpCount: Integer;
|
---|
| 523 | begin
|
---|
| 524 | if FUndoList.Count > 0 then
|
---|
| 525 | try
|
---|
| 526 | tmpStr := FUndoList.Strings[FUndoList.Count-1];
|
---|
| 527 | { showmessage(inttostr(AnsiPos('$$',tmpStr)) + #13 + inttostr(length(tmpstr)) + #13 +
|
---|
| 528 | copy(tmpStr,length(tmpStr)-2,2)); }
|
---|
| 529 | if (AnsiPos('$$',tmpStr)=1) and (copy(tmpStr,length(tmpStr)-1,2) = '$$')then
|
---|
| 530 | begin
|
---|
| 531 | tmpCount := strtoInt(StringReplace(tmpStr,'$$','',[rfReplaceAll]));
|
---|
| 532 | while FIgnore.Count > tmpCount do
|
---|
| 533 | FIgnore.Delete(FIgnore.Count -1);
|
---|
| 534 | end
|
---|
| 535 | else
|
---|
| 536 | SourceTextControl.Text := tmpStr;
|
---|
| 537 |
|
---|
| 538 | Result := True;
|
---|
| 539 | FUndoList.Delete(FUndoList.Count-1);
|
---|
| 540 | ReStart;
|
---|
| 541 | except
|
---|
| 542 | Result := False;
|
---|
| 543 | end;
|
---|
| 544 | end;
|
---|
| 545 |
|
---|
| 546 | procedure TskaHunSpellChecker.IgnoreAll;
|
---|
| 547 | begin
|
---|
| 548 | if (SpellCheckState <> ssChecking) or (not assigned(SourceTextControl)) or (not assigned(SuggestionList)) then
|
---|
| 549 | exit;
|
---|
| 550 | SaveForUndo(True);
|
---|
| 551 | FIgnore.Add(CurrentWordDetail(False)) ;
|
---|
| 552 | WaitForUser := False;
|
---|
| 553 | SourceTextControl.Invalidate;
|
---|
| 554 | end;
|
---|
| 555 |
|
---|
| 556 | procedure TskaHunSpellChecker.IgnoreOnce;
|
---|
| 557 | begin
|
---|
| 558 | if (SpellCheckState <> ssChecking) or (not assigned(SourceTextControl)) or (not assigned(SuggestionList)) then
|
---|
| 559 | exit;
|
---|
| 560 | if trim(CurrentWord) <> '' then
|
---|
| 561 | begin
|
---|
| 562 | SaveForUndo(True);
|
---|
| 563 | FIgnore.Add(CurrentWordDetail(True)) ;
|
---|
| 564 | end;
|
---|
| 565 | WaitForUser := False;
|
---|
| 566 | SourceTextControl.Invalidate;
|
---|
| 567 | end;
|
---|
| 568 |
|
---|
| 569 | procedure TskaHunSpellChecker.Initialize;
|
---|
| 570 | begin
|
---|
| 571 | CurrentWord := '';
|
---|
| 572 | WordLength := 0;
|
---|
| 573 | FoundAt := -1;
|
---|
| 574 | CurrentLine := 0;
|
---|
| 575 | WordPos := 0;
|
---|
| 576 | SuggestionList.Clear;
|
---|
| 577 | end;
|
---|
| 578 |
|
---|
| 579 | function TskaHunSpellChecker.IsMisspelled(const AWord: string): Boolean;
|
---|
| 580 | begin
|
---|
| 581 | if (not Active) then
|
---|
| 582 | Result := True
|
---|
| 583 | else
|
---|
| 584 | Result := not uHunSpellLib.hunspell_spell(FpointerHunLib, PAnsiChar(AnsiString(AWord)));
|
---|
| 585 | end;
|
---|
| 586 |
|
---|
| 587 | procedure TskaHunSpellChecker.Loaded;
|
---|
| 588 | begin
|
---|
| 589 | inherited;
|
---|
| 590 | SetActive(FActiveOrLoaded);
|
---|
| 591 | end;
|
---|
| 592 |
|
---|
[1694] | 593 | procedure TskaHunSpellChecker.ManualChangeDone;
|
---|
| 594 | begin
|
---|
| 595 | if trim(FTxtBeforeManualEdit) = '' then
|
---|
| 596 | exit;
|
---|
| 597 | FUndoList.Add(FTxtBeforeManualEdit);
|
---|
| 598 | ReStart;
|
---|
| 599 | end;
|
---|
| 600 |
|
---|
| 601 | procedure TskaHunSpellChecker.ManualChangeStart;
|
---|
| 602 | begin
|
---|
| 603 | FTxtBeforeManualEdit := FSourceEdit.Text;
|
---|
| 604 | end;
|
---|
| 605 |
|
---|
[1693] | 606 | function TskaHunSpellChecker.Open: Boolean;
|
---|
| 607 | var
|
---|
| 608 | CurrentLine: integer;
|
---|
| 609 | begin
|
---|
| 610 | Result := True;
|
---|
| 611 | if Active then Exit;
|
---|
| 612 | Result := False;
|
---|
| 613 | FpointerHunLib := Nil;
|
---|
| 614 | if not uHunSpellLib.LoadLibHunspell('') then
|
---|
| 615 | begin
|
---|
| 616 | MessageDlg(DLLNotLoaded, mtError, [mbOK],0);
|
---|
| 617 | Exit;
|
---|
| 618 | end;
|
---|
| 619 | FpointerHunLib := uHunSpellLib.hunspell_initialize(PAnsiChar(AnsiString(FAffixFileName)), PAnsiChar(AnsiString(FDictFileName)));
|
---|
| 620 | Result := Assigned(FpointerHunLib);
|
---|
| 621 |
|
---|
| 622 | if (Result) and (assigned(FCustDict)) then
|
---|
| 623 | for CurrentLine := 0 to FCustDict.Count - 1 do
|
---|
| 624 | AddCustomWord(FCustDict[CurrentLine], True);
|
---|
| 625 | end;
|
---|
| 626 |
|
---|
| 627 | procedure TskaHunSpellChecker.SaveForUndo(const Ignoring: Boolean = False);
|
---|
| 628 | begin
|
---|
| 629 | if Ignoring then
|
---|
| 630 | FUndoList.Add('$$'+ IntToStr(FIgnore.Count)+'$$')
|
---|
| 631 | else
|
---|
| 632 | FUndoList.Add(SourceTextControl.Text);
|
---|
| 633 | end;
|
---|
| 634 |
|
---|
| 635 | procedure TskaHunSpellChecker.SetActive(const Value: Boolean);
|
---|
| 636 | begin
|
---|
| 637 | if (csDesigning in ComponentState) or (csLoading in ComponentState) then
|
---|
| 638 | FActiveOrLoaded := Value
|
---|
| 639 | else
|
---|
| 640 | if Value then
|
---|
| 641 | FActiveOrLoaded := Open
|
---|
| 642 | else
|
---|
| 643 | Close;
|
---|
| 644 | end;
|
---|
| 645 |
|
---|
| 646 | procedure TskaHunSpellChecker.SetAffixFileName(const Value: string);
|
---|
| 647 | begin
|
---|
| 648 | Close;
|
---|
| 649 | FAffixFileName := Value;
|
---|
| 650 | end;
|
---|
| 651 |
|
---|
| 652 | procedure TskaHunSpellChecker.SetbtnClose(const Value: TButton);
|
---|
| 653 | begin
|
---|
| 654 | if btnClose = Value then
|
---|
| 655 | exit;
|
---|
| 656 | FbtnClose := Value;
|
---|
| 657 | FbtnClose.ModalResult := 1; //mrOK
|
---|
| 658 | end;
|
---|
| 659 |
|
---|
| 660 | procedure TskaHunSpellChecker.SetCustomDict(const Value: String);
|
---|
| 661 | begin
|
---|
| 662 | FCustom := Value;
|
---|
| 663 | if not (csDesigning in componentState) then
|
---|
| 664 | if Active and (FileExists(Value)) then
|
---|
| 665 | FCustDict.LoadFromFile(Value);
|
---|
| 666 | end;
|
---|
| 667 |
|
---|
| 668 | procedure TskaHunSpellChecker.SetDictFileName(const Value: string);
|
---|
| 669 | begin
|
---|
| 670 | Close;
|
---|
| 671 | FDictFileName := Value;
|
---|
| 672 | end;
|
---|
| 673 |
|
---|
| 674 | procedure TskaHunSpellChecker.SetHighLightEdit(const Value: TEdit);
|
---|
| 675 | begin
|
---|
| 676 | if FHighlightEdit = Value then
|
---|
| 677 | exit;
|
---|
| 678 |
|
---|
| 679 | FHighlightEdit := Value;
|
---|
| 680 |
|
---|
| 681 | if Active then
|
---|
| 682 | FHighlightEdit.Text := CurrentWord;
|
---|
| 683 |
|
---|
| 684 |
|
---|
| 685 | end;
|
---|
| 686 |
|
---|
| 687 | Function RichEditWndProc (handle:HWnd;uMsg,wParam,lParam:longint): longint stdcall;
|
---|
| 688 | begin
|
---|
| 689 | Result := CallWindowProc(OldRichEditWndProc, handle, uMsg, wParam, lParam);
|
---|
| 690 | if (uMsg=WM_PAINT) and assigned(CurrentMe) then CurrentMe.ShowMisSpelledWord;
|
---|
| 691 | End;
|
---|
| 692 |
|
---|
| 693 | procedure TskaHunSpellChecker.SetSourceEdit(const Value: TRichEdit);
|
---|
| 694 | begin
|
---|
| 695 | if FSourceEdit = Value then
|
---|
| 696 | exit;
|
---|
| 697 |
|
---|
| 698 | FSourceEdit := Value;
|
---|
| 699 |
|
---|
| 700 | if csDesigning in ComponentState then
|
---|
| 701 | exit;
|
---|
| 702 |
|
---|
| 703 | PREditorWndProc:=@RichEditWndProc;
|
---|
| 704 | Value.perform(EM_EXLIMITTEXT, 0, 65535*32); //raise the limit of text which could be inserted into this Richedit
|
---|
| 705 | OldRichEditWndProc := pointer(SetWindowLong(Value.handle, GWL_WNDPROC, longint(@RichEditWndProc)));
|
---|
| 706 |
|
---|
| 707 | end;
|
---|
| 708 |
|
---|
| 709 | end.
|
---|