How to recognize delimited word and replace with a new word

General TRichView support forum. Please post your questions here
Post Reply
Sergey Tkachenko
Site Admin
Posts: 17281
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Would it be ok if keywords will be hyperlinks? If yes, can this document contain other types of hyperlinks, such as URLs?
Sergey Tkachenko
Site Admin
Posts: 17281
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Here is the example of what you asked originally.
When the user clicks any text inside [], it selects it (including []), displays some form (a message box in this test) and replace it to a new value.
This demo uses OnRVMouseUp event.

Limitation: this keyword, including [], must be written using the same font.

Code: Select all

function SelectFld(rve: TCustomRichViewEdit): Boolean;
var s: String;
    ItemNo, Offs, Offs1, Offs2: Integer;
begin
  Result := False;
  if rve.SelectionExists then
    exit;
  rve := rve.TopLevelEditor;
  ItemNo := rve.CurItemNo;
  Offs := rve.OffsetInCurItem;
  if (rve.GetItemStyle(ItemNo)<0) or
     (Offs<=rve.GetOffsBeforeItem(ItemNo)) or (Offs>=rve.GetOffsAfterItem(ItemNo)) then
     exit;
  s := rve.GetItemText(ItemNo);
  Offs1 := Offs;
  while (Offs1>=1) and  (s[Offs1]<>'[') do
    dec(Offs1);
  if Offs1<1 then
    exit;
  Offs2 := Offs;
  while (Offs2<=Length(s)) and (s[Offs2]<>']') do
    inc(Offs2);
  if Offs2>Length(s) then
    exit;
  rve.SetSelectionBounds(ItemNo, Offs1, ItemNo, Offs2+1);
  Result := True;
end;

procedure TForm3.RichViewEdit1RVMouseUp(Sender: TCustomRichView;
  Button: TMouseButton; Shift: TShiftState; ItemNo, X, Y: Integer);
begin
  if SelectFld(RichViewEdit1) then begin
    Application.MessageBox(PChar('Here you can display a form to edit '+RichViewEdit1.GetSelText), 'Test', 0);
    RichViewEdit1.InsertText('New value');
  end;
end;
Sergey Tkachenko
Site Admin
Posts: 17281
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

You can change text attributes of the selected text.
Do you use RichViewActions? They allow to do it in a very simple way.
Otherwise, you need to use OnStyleConversion event and ApplyStyleConversion method, see the demo in Demos\*\Editors\Editor 2\.
Post Reply