The code is below. This is may be not the most efficient method, but the simplest one. It uses editing methods.
Code: Select all
procedure DetectCustomLinks(RVData: TCustomRVFormattedData; rve: TCustomRichViewEdit);
var i,r,c,p1,p2,p3: Integer;
s, Target, Text: String;
Table: TRVTableItemInfo;
const OPENKEYWORD = '[open/';
CLOSEKEYWORD = '[/open]';
begin
i := RVData.ItemCount-1;
while i>=0 do begin
if (RVData.GetItemStyle(i)>=0) and (RVData.GetItemTag(i)=0) then begin
s := RVData.GetItemText(i);
p1 := Pos(OPENKEYWORD, s);
if p1>0 then begin
s := Copy(s, p1+Length(OPENKEYWORD), Length(s));
p2 := Pos(']', s);
if p2>0 then begin
Target := Copy(s, 1, p2-1);
p3 := Pos(CLOSEKEYWORD, s);
if p3>0 then begin
Text := Copy(s, p2+1, p3-p2-1);
RVData := TCustomRVFormattedData(RVData.Edit);
RVData.SetSelectionBounds(i, p1, i, p1+Length(OPENKEYWORD)+p3+Length(CLOSEKEYWORD)-1);
rve.ApplyStyleConversion(CONVERT_TO_HYPERTEXT);
rve.InsertStringTag(Text, Target);
// for older versions of TRichView: rve.InsertStringTag(Text, Integer(StrNew(PChar(Target))));
continue;
end;
end;
end;
end
else if RVData.GetItemStyle(i)=rvsTable then begin
Table := TRVTableItemInfo(RVData.GetItem(i));
for r := 0 to Table.RowCount-1 do
for c := 0 to Table.ColCount-1 do
if Table.Cells[r,c]<>nil then
DetectCustomLinks(TCustomRVFormattedData(Table.Cells[r,c].GetRVData), rve);
end;
dec(i);
end;
end;
Call:
Code: Select all
DetectCustomLinks(RichViewEdit1.RVData, RichViewEdit1);
I created this code as an addition to Assorted\Hypertext\CreateHyperlink\ demo, so it uses a style conversion CONVERT_TO_HYPERTEXT.
If you use procedures from Assorted\Hypertext\URLs\, then instead of rve.ApplyStyleConversion(CONVERT_TO_HYPERTEXT) you can write:
Code: Select all
URLScanEvent(RVData.GetItemStyle(i), r);
rve.ApplyTextStyle(r);
Update 2011-Oct-22: changed for compatibility with TRichView 13.3+