trichview.com

trichview.support.examples




Example 1: making table from the selection in editor


Return to index


Author

Message

Sergey Tkachenko

Posted: 11/07/2004 19:36:10


This procedure creates a table 1x2 and inserts it instead of the selected

paragraphs.

If N paragraphs are selected, the first (N+1) div 2 paragraphs are copied in

the first column of this table, the rest of these paragraphs are copied to

the second column.


procedure CreateTableFromSelection(rve: TCustomRichViewEdit);

var StartNo, StartOffs, EndNo, EndOffs, MidNo, i, ParaCount,

    ParaCount1: Integer;

    table: TRVTableItemInfo;

    Stream: TMemoryStream;

    Clr: TColor;

begin

  rve := rve.TopLevelEditor;

  // StartNo..EndNo := beginning and end of the selected paragraphs

  rve.GetSelectionBounds(StartNo, StartOffs, EndNo, EndOffs, True);

  if StartNo<0 then

    exit;

  SendMessage(rve.Handle, WM_SETREDRAW, 0, 0);

  try

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

      dec(StartNo);

    while (EndNo+1<rve.ItemCount) and not rve.IsParaStart(EndNo+1) do

      inc(EndNo);

    // ParaCount := number of selected paragraphs

    ParaCount := 0;

    for i := StartNo to EndNo do

      if rve.IsParaStart(i) then

        inc(ParaCount);

    // ParaCount1 := number of paragraphs in the first column

    ParaCount1 := (ParaCount+1) div 2;

    // MidNo := index of the last item to copy in the first column

    MidNo := EndNo;

    if ParaCount>1 then begin

      ParaCount := 0;

      for i := StartNo to EndNo do

        if rve.IsParaStart(i) then begin

          inc(ParaCount);

          if ParaCount=ParaCount1+1 then begin

            MidNo := i-1;

            break;

          end;

        end;

    end;

    // creating table 1 x 2

    table := TRVTableItemInfo.CreateEx(1, 2, rve.RVData);

    table.CellBorderWidth := 1;

    table.CellBorderStyle := rvtbColor;

    table.CellHSpacing := -1;

    table.CellVSpacing := -1;

    table.BorderHSpacing := 0;

    table.BorderVSpacing := 0;

    // copying the first column

    Stream := TMemoryStream.Create;

    rve.SetSelectionBounds(StartNo, rve.GetOffsBeforeItem(StartNo),

      MidNo, rve.GetOffsAfterItem(MidNo));

    rve.SaveRVFToStream(Stream, True);

    Stream.Position := 0;

    table.Cells[0,0].LoadRVFFromStream(Stream, Clr, nil, nil);

    Stream.Free;

    // copying the second column

    if MidNo<>EndNo then begin

      Stream := TMemoryStream.Create;

      rve.SetSelectionBounds(MidNo+1, rve.GetOffsBeforeItem(MidNo+1),

        EndNo, rve.GetOffsAfterItem(EndNo));

      rve.SaveRVFToStream(Stream, True);

      Stream.Position := 0;

      table.Cells[0,1].LoadRVFFromStream(Stream, Clr, nil, nil);

      Stream.Free;

    end;

    rve.SetSelectionBounds(StartNo, rve.GetOffsBeforeItem(StartNo),

      EndNo, rve.GetOffsAfterItem(EndNo));

    // inserting table

    rve.InsertItem('', table);

  finally

    SendMessage(rve.Handle, WM_SETREDRAW, 1, 0);

    rve.Invalidate;

  end;

end;





Powered by ABC Amber Outlook Express Converter