trichview.com

trichview.support




Re: Applying TextStyle to an whole paragraph


Return to index


Author

Message

Sergey Tkachenko

Posted: 04/04/2002 20:35:10


Complete code is below.

Quite large, but I hope not overcomplicated:


/ Returns a number of characters between the beginning of StartItemNo-th

item

// and position defined as (ItemNo, Offs)

// (images, tables, etc. are considered as one character)

function GetDistanceFrom(rv: TCustomRichView; StartItemNo, ItemNo, Offs:

Integer): Integer;

var i: Integer;

begin

  Result := 0;

  for i := StartItemNo to ItemNo-1 do

    inc(Result, rv.RVData.ItemLength(i));

  inc(Result, Offs);

  if rv.GetItemStyle(ItemNo)>=0 then

    dec(Result);

end;


// Reverse function

procedure GetPosition(rv: TCustomRichView; StartItemNo, Dist: Integer; var

ItemNo, Offs: Integer);

begin

  ItemNo := StartItemNo;

  while Dist>rv.RVData.ItemLength(ItemNo) do begin

    dec(Dist, rv.RVData.ItemLength(ItemNo));

    inc(ItemNo);

  end;

  Offs := Dist;

  if rv.GetItemStyle(ItemNo)>=0 then

    inc(Offs);

end;


// selecting paragraph(s)

procedure SelectParagraph(rve: TCustomRichViewEdit);

var ItemNo1,ItemNo2,Offs1,Offs2: Integer;

begin

  while rve.InplaceEditor<>nil do

    rve := TCustomRichViewEdit(rve.InplaceEditor);

  rve.RVData.GetSelectionBoundsEx(ItemNo1,Offs1,ItemNo2,Offs2, True);

  while (ItemNo1>0) and not rve.IsParaStart(ItemNo1) do

    dec(ItemNo1);

  inc(ItemNo2);

  while (ItemNo2<rve.ItemCount) and not rve.IsParaStart(ItemNo2) do

    inc(ItemNo2);

  dec(ItemNo2);

  rve.SetSelectionBounds(ItemNo1, rve.GetOffsBeforeItem(ItemNo1),

                         ItemNo2, rve.GetOffsAfterItem(ItemNo2));

end;


// Store selection in StartItemNo, Dist1, Dist2

procedure StoreSelectionRelativeToTheBeginningOfParagraph(

  rve: TCustomRichViewEdit;

  var StartItemNo, Dist1, Dist2: Integer);

var ItemNo1, ItemNo2,Offs1,Offs2: Integer;

begin

  while rve.InplaceEditor<>nil do

    rve := TCustomRichViewEdit(rve.InplaceEditor);

  rve.RVData.GetSelectionBoundsEx(StartItemNo,Offs1,ItemNo2,Offs2, True);

  while (StartItemNo>0) and not rve.IsParaStart(StartItemNo) do

    dec(StartItemNo);

  rve.RVData.GetSelectionBoundsEx(ItemNo1,Offs1,ItemNo2,Offs2, False);

  Dist1 := GetDistanceFrom(rve, StartItemNo, ItemNo1, Offs1);

  Dist2 := GetDistanceFrom(rve, StartItemNo, ItemNo2, Offs2);

end;


// Restore stored selection

procedure RestoreSelectionRelativeToTheBeginningOfParagraph(

  rve: TCustomRichViewEdit;

  StartItemNo, Dist1, Dist2: Integer);

var ItemNo1, ItemNo2,Offs1,Offs2: Integer;

begin

  while rve.InplaceEditor<>nil do

    rve := TCustomRichViewEdit(rve.InplaceEditor);

  GetPosition(rve, StartItemNo, Dist1, ItemNo1, Offs1);

  GetPosition(rve, StartItemNo, Dist2, ItemNo2, Offs2);

  rve.SetSelectionBounds(ItemNo1, Offs1, ItemNo2, Offs2);

  rve.Invalidate;

end;



procedure TForm1.Button1Click(Sender: TObject);

var StartItemNo, Dist1, Dist2: Integer;

begin

  StoreSelectionRelativeToTheBeginningOfParagraph(RichViewEdit1,

StartItemNo, Dist1, Dist2);

  SelectParagraph(RichViewEdit1);

  RichViewEdit1.ApplyTextStyle(1);

  RestoreSelectionRelativeToTheBeginningOfParagraph(RichViewEdit1,

StartItemNo, Dist1, Dist2);

end;





Powered by ABC Amber Outlook Express Converter