Lazarus:3.4
Window 10@64
Experience Level: NEWBIE
Project: change the font parameters
of searched selected text
Note: Use Search to highlight SetStart to SelLength then set the font.paranters
All works great except for the the '//' search text because it doesn't have a second search to find the next text giving me a begin highlight to and end highlight
successful test
'String' is clBlue
successful test
Procedure is [fsBold]
successful test
'.TObject' is [fsUnderline] and clBrown
fail test '//' (need the length of the line row and subtract the position of the memo.carret with in that row length)
I can hack my way through this if
1. get the row length
2. find the position in that row where the memo.carret is
3. subtract the Carret position of the row from the total row length
4. finally add that length to the SelLength:=SelStart+ //what ever the rest of the row length is
So all I need is to add from SelStart to the end of that row
//......
Procedure SetFontPacage(aMemo:TRichMemo;aSearchStyle:TSearchOptions;SStr:String;fnt:String;fSyl:TFontStyles;fc:Tcolor;fSiz:Integer;Tags:String='');
var
StartPos:LongInt;
SStrLen:LongInt;
s:string;
begin
StartPos:=0;
SStrLen:= Length(SStr);
While aMemo.Search(SStr,StartPos,Length(aMemo.rtf),aSearchStyle,StartPos,SStrLen) do
begin
if Tags>''
then
begin
StartPos:=aMemo.Search(SStr,StartPos,Length(aMemo.rtf),aSearchStyle);
SStrLen:=aMemo.Search(Tags,StartPos+1,Length(aMemo.rtf),aSearchStyle);
SStrLen:=(SStrLen-StartPos);
end;
if SStr='//'
then
begin
//aMemo.CharAtPos(Mouse.CursorPos.x,Mouse.CursorPos.y);
SStrPos:=aMemo.Lines.IndexOf(SStr,StartPos);
// sstrPos:=aMemo.Lines.
SStrLen:= aMemo.CaretPos.y;
s:=aMemo.Lines[SStrLen];
SStrLen:= aMemo.CaretPos.x;
SStrLen:=Length(s);
SStrLen:=(Length(s)-SStrLen);
end;
aMemo.GetTextAttributes(StartPos,aFont);
if fnt<>'' then aFont.Name:=fnt;
if fSyl<>[] then aFont.Style:=fSyl;
if fc<>clNone then aFont.Color:=fc;
if fSiz<>0 then aFont.Size:=fSiz;
aMemo.SetTextAttributes(StartPos,SStrLen,aFont);
StartPos:=StartPos+ SStrLen;
end
end;
------------------------------------------
UPDATE
OK, it was a matter of figuring out what data is available at when event happens.
My udated code
Procedure SetFontPacage(aMemo:TRichMemo;aSearchStyle:TSearchOptions;SStr:String;fnt:String;fSyl:TFontStyles;fc:Tcolor;fSiz:Integer;Tags:String='');
var
StartPos:LongInt;
SStrLen,I:LongInt;
s:string;
begin
if TaskProgressBar.Position<100 Then TaskProgressBar.Position:=TaskProgressBar.Position+1;
TaskProgText.Caption:='Task '+SStr;
TaskProgressBar.Caption:=sstr;
Panel2.Invalidate;
StartPos:=0;
SStrLen:= Length(SStr);
While aMemo.Search(SStr,StartPos,Length(aMemo.rtf),aSearchStyle,StartPos,SStrLen) do
begin
aMemo.GetTextAttributes(StartPos,aFont);
if Tags>''
then
begin
//ShowMSG('Next search '+SStr);
if SStr='//'
then
begin
aMemo.SelStart:=StartPos;
I:=aMemo.CaretPos.y;
s:= aMemo.Lines[I];
SStrLen:=Length(s);
SStrLen:=(SStrLen-aMemo.CaretPos.x)+Length(SStr);
end
else
begin
SStrLen:=aMemo.Search(Tags,StartPos+1,Length(aMemo.rtf),aSearchStyle);
if SStrlen=-1 then break;
SStrLen:=(SStrLen-StartPos)+Length(SStr);
end;
end;
if fnt<>'' then aFont.Name:=fnt;
if fSyl<>[] then aFont.Style:=fSyl;
if fc<>clNone then aFont.Color:=fc;
if fSiz<>0 then aFont.Size:=fSiz;
aMemo.SetTextAttributes(StartPos,SStrLen,aFont);
DocProgressBar.Position:=Round(StartPos/DocProgressBar.Step);
StartPos:=StartPos+ SStrLen;
end
end;
YEAH it works