source: cprs/branches/HealthSevak-CPRS/CPRS-Lib/Hans SpellCheck/Demo/fHunSpell.pas@ 1727

Last change on this file since 1727 was 1725, checked in by healthsevak, 9 years ago

Cleaned little bit of glitches and added the About dialog and also synced the demo project in line with the updated component properties

File size: 13.6 KB
RevLine 
[1715]1unit fHunSpell;
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
33interface
34
35uses
36 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
37 StdCtrls, ComCtrls, RichEdit, Buttons, ExtCtrls, ShellAPI,
[1725]38 skaSpellCheck, jpeg;
[1715]39
40type
41 TfrmHunSpell = class(TForm)
42 btnClose: TButton;
43 OpenDialog1: TOpenDialog;
44 SpellCheck1: TskaHunSpellChecker;
45 edtDictionary: TEdit;
46 btnSelectDict: TBitBtn;
47 RichEdit1: TRichEdit;
48 pnlMisSpelled: TPanel;
49 Label1: TLabel;
[1725]50 lstSuggestions: TListBox;
[1715]51 Label4: TLabel;
[1725]52 edtMisSpelt: TEdit;
[1715]53 btnReplaceWith: TButton;
54 btnChangeAll: TButton;
55 btnChange: TButton;
56 btnAddToDictionary: TButton;
57 btnIgnoreAll: TButton;
58 btnIgnoreOnce: TButton;
59 btnAbort: TButton;
[1725]60 btnAbout: TButton;
61 pnlAbout: TPanel;
62 btnHideAbout: TButton;
[1715]63 btnUndo: TButton;
[1718]64 cbIgnoreAllCaps: TCheckBox;
65 cbIgnoreDigit: TCheckBox;
[1725]66 btnRestat: TButton;
67 cbHyphenatedWord: TCheckBox;
68 StaticText1: TStaticText;
69 StaticText2: TStaticText;
70 StaticText3: TStaticText;
71 Label2: TLabel;
72 StaticText4: TStaticText;
73 Label3: TLabel;
74 StaticText5: TStaticText;
75 StaticText6: TStaticText;
76 Image1: TImage;
77 lblDictionariesURL: TLabel;
78 lblDictionary: TLabel;
79 lblOpenMedURL: TLabel;
80 Label5: TLabel;
81 Label6: TLabel;
[1715]82 procedure FormCreate(Sender: TObject);
83 procedure btnIgnoreOnceClick(Sender: TObject);
84 procedure btnIgnoreAllClick(Sender: TObject);
85 procedure btnChangeClick(Sender: TObject);
86 procedure btnChangeAllClick(Sender: TObject);
87 procedure btnCloseClick(Sender: TObject);
88 procedure FormActivate(Sender: TObject);
89 procedure btnSelectDictClick(Sender: TObject);
90 procedure edtDictionaryEnter(Sender: TObject);
91 procedure btnReplaceWithClick(Sender: TObject);
92 procedure btnAddToDictionaryClick(Sender: TObject);
[1725]93 procedure FormShow(Sender: TObject);
[1715]94 procedure FormClose(Sender: TObject; var Action: TCloseAction);
95 procedure btnAbortClick(Sender: TObject);
[1725]96 procedure lblOpenMedURLClick(Sender: TObject);
97 procedure btnAboutClick(Sender: TObject);
98 procedure btnHideAboutClick(Sender: TObject);
[1715]99 procedure btnUndoClick(Sender: TObject);
100 procedure RichEdit1KeyUp(Sender: TObject; var Key: Word;
101 Shift: TShiftState);
102 procedure RichEdit1KeyDown(Sender: TObject; var Key: Word;
103 Shift: TShiftState);
[1725]104 procedure lstSuggestionsDblClick(Sender: TObject);
105 procedure SpellCheck1StateChange(const Sender: TObject;
106 const State: TSpellState);
[1715]107 procedure SpellCheck1Abort(Sender: TObject);
108 procedure SpellCheck1Start(Sender: TObject);
[1718]109 procedure cbIgnoreAllCapsClick(Sender: TObject);
110 procedure cbIgnoreDigitClick(Sender: TObject);
111 procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
[1725]112 procedure btnRestatClick(Sender: TObject);
113 procedure cbHyphenatedWordClick(Sender: TObject);
114 procedure Image1Click(Sender: TObject);
115 procedure Label6Click(Sender: TObject);
116 procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
[1715]117 private
118 { Private declarations }
119 NoEngineOpted: Boolean;
120 FSourceControl: TCustomMemo;
121 procedure GoToURL(const aURL: String);
[1725]122 procedure UpdateGUI;
[1715]123 public
124 { Public declarations }
125 Showhighlight:boolean;
126 highlightcolor:TColor;
127 HighLightList:TStringlist;
128 OldRichEditWndProc: {integer}pointer;
129 PRichEditWndProc:pointer;
[1725]130 class function DoHunSpellCheck(AnEditControl: TCustomMemo): TModalResult; static;
[1715]131 end;
132
133 Resourcestring
134 TX_AFF_NOT_FOUND = 'Correspong AFF file named not found!'
135 + #13
136 + ' Specify dictionary file whose *.aff is also '
137 + 'present in same directory.' ;
138 TX_DIC_FILE_NOT_FOUND = 'Dictionary File for SpellCheck Engine not found!';
139 TX_SPELL_COMPLETE = 'Spell Check Complete';
140 TX_SPELL_CANCELLED = 'Spell Check Aborted'
141 + #13
142 + 'No Changes applied to the original text!';
143
144 Const
[1725]145 DefaultDicFile = '\dict\en_US-large.dic';
146
[1715]147 var
148 frmHunSpell: TfrmHunSpell;
149
150implementation
151{$R *.DFM}
152
153
[1725]154class function TfrmHunSpell.DoHunSpellCheck(AnEditControl: TCustomMemo): TModalResult;
[1715]155var
156 frm: TfrmHunSpell;
157begin
158 Result := mrCancel;
159 frm:= TfrmHunSpell.create(Application);
160 try
161 frm.RichEdit1.Text:= AnEditControl.Text;
162 frm.FSourceControl := AnEditControl;
163 Result := frm.ShowModal;
164 finally
165 frm.Free;
166 end;
167end;
168
169 {************ HighLight ***********888}
[1725]170 procedure TfrmHunSpell.FormShow(Sender: TObject);
[1715]171begin
[1725]172 if SpellCheck1.SpellCheckState = ssReady then
173 SpellCheck1.CheckSpelling;
174 try
175 if lstSuggestions.Count > 0 then
176 btnChange.SetFocus;
177 except
178 end;
179end;
180
181procedure TfrmHunSpell.GoToURL(const aURL: String);
182begin
[1715]183if length(trim(aURL)) > 4 then
184 ShellExecute(Handle, 'open', PChar(aURL), '', '', SW_NORMAL);
185end;
186
[1725]187procedure TfrmHunSpell.Image1Click(Sender: TObject);
[1715]188begin
[1725]189 inherited;
190 GoToURL('http://www.healthsevak.com');
191end;
192
193procedure TfrmHunSpell.Label6Click(Sender: TObject);
194begin
195 inherited;
196 GoToURL('mailto: digitiger@gmail.com');
197end;
198
199procedure TfrmHunSpell.lblOpenMedURLClick(Sender: TObject);
200begin
201 inherited;
[1715]202 GoToURL(TLabel(sender).Caption);
203end;
204
205procedure TfrmHunSpell.lstSuggestionsDblClick(Sender: TObject);
206begin
[1725]207 inherited;
208 btnChangeClick(sender);
[1715]209end;
210
211procedure TfrmHunSpell.RichEdit1KeyDown(Sender: TObject; var Key: Word;
212 Shift: TShiftState);
213begin
[1725]214 inherited;
[1715]215 if SpellCheck1.SpellCheckState = ssChecking then
216 SpellCheck1.ManualChangeStart;
217end;
218
219procedure TfrmHunSpell.RichEdit1KeyUp(Sender: TObject; var Key: Word;
220 Shift: TShiftState);
221begin
[1725]222 inherited;
223 if SpellCheck1.SpellCheckState in [ssCancelled, ssCompleted] then
[1715]224 SpellCheck1.Reopen;
[1725]225
[1715]226 if SpellCheck1.SpellCheckState = ssChecking then
227 SpellCheck1.ManualChangeDone;
228end;
229
230procedure TfrmHunSpell.SpellCheck1Abort(Sender: TObject);
231begin
[1725]232 inherited;
[1715]233 UpdateGUI;
234end;
235
236procedure TfrmHunSpell.SpellCheck1Start(Sender: TObject);
237begin
[1725]238 inherited;
[1715]239 UpdateGUI;
240end;
241
242procedure TfrmHunSpell.SpellCheck1StateChange(const Sender: TObject;
243 const State: TSpellState);
244begin
[1725]245 inherited;
[1715]246 if State = ssCompleted then
247 ShowMessage(TX_SPELL_COMPLETE);
248 UpdateGUI;
249end;
250
251procedure TfrmHunSpell.UpdateGUI;
252var
253 Checking: Boolean;
254begin
[1725]255 if csDestroying in componentstate then
256 exit;
[1715]257 Checking := SpellCheck1.SpellCheckState = ssChecking;
258 pnlMisSpelled.Visible := Checking;
259 pnlMisSpelled.Enabled := Checking;
260 btnClose.Visible := not Checking;
261end;
262
263{************* FormCreate **********}
[1725]264procedure TfrmHunSpell.btnAboutClick(Sender: TObject);
[1715]265begin
[1725]266 inherited;
267 pnlAbout.Show;
268 pnlAbout.BringToFront;
[1715]269end;
270
271procedure TfrmHunSpell.btnAddToDictionaryClick(Sender: TObject);
272begin
273 SpellCheck1.AddCustomWord;
274end;
275
276procedure TfrmHunSpell.btnReplaceWithClick(Sender: TObject);
277begin
278 SpellCheck1.CorrectWithMyWord;
279end;
280
[1725]281procedure TfrmHunSpell.btnRestatClick(Sender: TObject);
282begin
283 inherited;
284 SpellCheck1.ReStart
285end;
286
[1715]287procedure TfrmHunSpell.btnAbortClick(Sender: TObject);
288begin
[1725]289 if SpellCheck1.AbortSpellCheck(True) then
[1715]290end;
291
292procedure TfrmHunSpell.btnChangeAllClick(Sender: TObject);
293begin
294 SpellCheck1.ChangeAll;
295end;
296
297procedure TfrmHunSpell.btnChangeClick(Sender: TObject);
298begin
299 SpellCheck1.Change;
300end;
301
302procedure TfrmHunSpell.btnCloseClick(Sender: TObject);
303begin
[1725]304 close;
[1715]305end;
306
307procedure TfrmHunSpell.btnIgnoreAllClick(Sender: TObject);
308begin
309 SpellCheck1.IgnoreAll;
310end;
311
312procedure TfrmHunSpell.btnIgnoreOnceClick(Sender: TObject);
313begin
314 SpellCheck1.IgnoreOnce;
315end;
316
317procedure TfrmHunSpell.btnSelectDictClick(Sender: TObject);
318var
319 aff: String;
320begin
321 if OpenDialog1.Execute then
322 begin
323 if SpellCheck1.DictionaryFileName = OpenDialog1.FileName then
324 exit;
325
326 aff := ChangeFileExt(OpenDialog1.FileName, '.aff');
327 if not FileExists(aff) then
328 begin
329 ShowMessage(TX_AFF_NOT_FOUND);
330 OpenDialog1.FileName := '';
331 btnSelectDictClick(self);
332 end
333 else
334 begin
335 if SpellCheck1.SpellCheckState = ssChecking then
336 SpellCheck1.AbortSpellCheck(False);
337 edtDictionary.Text := OpenDialog1.FileName;
338 SpellCheck1.DictionaryFileName := edtDictionary.Text;
339 SpellCheck1.AffixFileName := aff;
340 SpellCheck1.Open;
341 end;
342 end;
343end;
344
345procedure TfrmHunSpell.btnUndoClick(Sender: TObject);
346begin
347 inherited;
348 SpellCheck1.Undo;
349end;
350
[1725]351procedure TfrmHunSpell.btnHideAboutClick(Sender: TObject);
352begin
353 inherited;
354 pnlAbout.hide;
355end;
356
357procedure TfrmHunSpell.cbHyphenatedWordClick(Sender: TObject);
358begin
359 inherited;
360 if SpellCheck1.HyphenatedWords <> cbHyphenatedWord.Checked then
361 SpellCheck1.HyphenatedWords := cbHyphenatedWord.Checked;
362
363 if SpellCheck1.SpellCheckState = ssChecking then
364 SpellCheck1.ReStart;
365end;
366
[1718]367procedure TfrmHunSpell.cbIgnoreAllCapsClick(Sender: TObject);
368begin
[1725]369 inherited;
[1718]370 if SpellCheck1.IgnoreAllCaps <> cbIgnoreAllCaps.Checked then
371 SpellCheck1.IgnoreAllCaps := cbIgnoreAllCaps.Checked;
[1725]372
373 if SpellCheck1.SpellCheckState = ssChecking then
374 SpellCheck1.ReStart;
[1718]375end;
376
377procedure TfrmHunSpell.cbIgnoreDigitClick(Sender: TObject);
378begin
[1725]379 inherited;
[1718]380 if SpellCheck1.IgnoreWordWithDigits <> cbIgnoreDigit.Checked then
381 SpellCheck1.IgnoreWordWithDigits := cbIgnoreDigit.Checked;
[1725]382
383 if SpellCheck1.SpellCheckState = ssChecking then
384 SpellCheck1.ReStart;
[1718]385end;
386
[1715]387procedure TfrmHunSpell.edtDictionaryEnter(Sender: TObject);
388begin
389 btnSelectDict.SetFocus;
390end;
391
392procedure TfrmHunSpell.FormActivate(Sender: TObject);
393begin
[1725]394 if ( not btnRestat.Enabled) and (not NoEngineOpted) then
[1715]395 begin
396 btnSelectDictClick(self);
397 NoEngineOpted := True;
398 end;
[1718]399 if cbIgnoreAllCaps.Checked <> SpellCheck1.IgnoreAllCaps then
400 cbIgnoreAllCaps.Checked := SpellCheck1.IgnoreAllCaps;
401
402 if cbIgnoreDigit.Checked <> SpellCheck1.IgnoreWordWithDigits then
403 cbIgnoreDigit.Checked := SpellCheck1.IgnoreWordWithDigits;
[1725]404
405 if cbHyphenatedWord.Checked <> SpellCheck1.HyphenatedWords then
406 cbHyphenatedWord.Checked := SpellCheck1.HyphenatedWords;
[1715]407end;
408
409procedure TfrmHunSpell.FormClose(Sender: TObject; var Action: TCloseAction);
410begin
[1725]411 if SpellCheck1.SpellCheckState = ssCompleted then
[1715]412 begin
[1725]413 // ShowMsg(TX_SPELL_COMPLETE, TShow508MessageIcon(1), smbOK) ;
[1715]414 if Assigned(FSourceControl) then
415 FSourceControl.Text := RichEdit1.Text;
[1725]416 end;
417 {else
418 ShowMsg(TX_SPELL_CANCELLED + CRLF + TX_NO_CORRECTIONS,
419 TShow508MessageIcon(1), smbOK) ;}
[1715]420end;
421
[1718]422procedure TfrmHunSpell.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
423begin
[1725]424 inherited;
425
[1718]426 CanClose := (SpellCheck1.SpellCheckState <> ssChecking)
427 or (SpellCheck1.AbortSpellCheck(True));
428 if CanClose then
429 ModalResult := mrCancel;
430end;
431
[1715]432procedure TfrmHunSpell.FormCreate(Sender: TObject);
433var
434 dicFile: String;
435 function affFile: String;
436 begin
437 Result := ChangeFileExt(dicFile, '.aff');
438 end;
439begin
[1725]440 dicFile := ExtractFilePath(Application.ExeName)+ DefaultDicFile;
[1715]441
442 if (FileExists(dicFile)) and (FileExists(affFile)) then
443 begin
444 SpellCheck1.AffixFileName := affFile;
[1725]445 SpellCheck1.DictionaryFileName := dicFile;
446 edtDictionary.Text := dicFile;
[1715]447 end
448 else
449 edtDictionary.Text := TX_Dic_File_Not_Found;
450
451 if edtDictionary.Text = TX_Dic_File_Not_Found then
452 btnSelectDictClick(self);
[1725]453
454 SpellCheck1.SourceTextControl := RichEdit1;
455 SpellCheck1.SuggestionList := lstSuggestions;
456 SpellCheck1.MisSpeltWord := edtMisSpelt;
457 btnRestat.Enabled := (SpellCheck1.DictionaryFileName <> '')
[1715]458 and FileExists(dicFile);
459end;
460
461
462
[1725]463procedure TfrmHunSpell.FormKeyUp(Sender: TObject; var Key: Word;
464 Shift: TShiftState);
465begin
466 if (key = 27) and (pnlAbout.Showing) then
467 btnHideAboutClick(self);
468end;
469
[1715]470end.
Note: See TracBrowser for help on using the repository browser.