<< Click to Display Table of Contents >> Navigation: Programming > Create text under program control > Low level text creation > Auto capitalisation |
You can use the event BeforeInitializePar to update the current paragraph:
procedure TForm1.WPRichText1BeforeInitializePar(Sender: TObject;
RTFEngine: TWPRTFEngineBasis; RTFDataBlock: TWPRTFDataBlock;
par: TParagraph);
var pos_wstart, i : Integer; s : string;
begin
// Current paragraph
// Current paragraph
if par = WPRichText1.ActivePar then
begin
i := WPRichText1.ActivePosInPar;
// 1. Overread spaces to the left
while (i>0) and par.IsSpace(i) do dec(i);
// 2. Overread a word
pos_wstart := -1;
if not par.IsWordDelimiter(i) and (par.IsSpace(i+1)) then
begin
while (i>=0) and not par.IsWordDelimiter(i) do begin pos_wstart := i; dec(i); end;
// 3. Overread spaces
while (i>0) and par.IsSpace(i) do dec(i);
// Now, if . ! ? or start of par uppercase it!
if (pos_wstart>=0) and
((i<=0) or (par.CharItem[i]='.') or
(par.CharItem[i]='!') or
(par.CharItem[i]='?'))
// Check for abbreviation (simplified - only check a..z
and ((par.IsSpace(i-1) or (par.CharItem[i]<>'.') or
((par.CharItem[i-1]>='a') and (par.CharItem[i-1]<='z'))))
then
begin
if Integer(par.CharItem[pos_wstart])<256 then
begin
s := AnsiUpperCase(Char(par.CharItem[pos_wstart]));
if s<>'' then par.CharItem[pos_wstart] := WideChar(s[1]);
end
else par.CharItem[pos_wstart] := WideUpperCase(par.CharItem[pos_wstart])[1];
end;
end;
end;
end;
new WPTools 8 Feature:
On popular demand the mode wpAutoCaptitalize has been added to property EditOptionsEx.
If this mode is active the editor will automatically capitalize Words at the beginning of a sentence.