FastAddTable method

Applies to
TWPCustomRichText

Overloaded Variants
Procedure FastAddTable(col: Integer; const Pprp: array of TParProps; ReplaceCurrentPar: Boolean = false);
Procedure FastAddTable(col: Integer; Pprp: PTParProps);

Declaration
procedure FastAddTable(col: Integer; const Pprp: array of TParProps; ReplaceCurrentPar: Boolean = false);

Description
If the optional parameter ReplaceCurrentPar = true it will create or work (extend) a table at current position, otherwise a table is appended at the end. The function has been added for compatibility to WPTools 4, better use TableAdd.

Declaration
procedure FastAddTable(col: Integer; Pprp: PTParProps);

Description
This procedure can be used to create a table in code. It has been provided for backward compatibility only. Please don't use this procedure in new projects.
Please note, although FastAddTable is still there it is not supported in Delphi 8 since it uses a parameter which has to be passed as pointer, Pprp. Especially for .NET the method FastAddTable2 has been added.


FastAddTable expects two parameters. The count of columns to be created and an array of TParProps record to describe each cell. The TParProps elements are uses the same way as in FastAddText. The property CWidth or Width_PC or Width_TW is used to select the width for the cell which is created.
CWidth is the width in 1/255 of the total width, Width_PC is the width in %*100 and Width_TW is the width in twips. The last 2 properties are available since version 6!

FastAddTable is available in two versions, one using pointers and the other using dynamic arrays.

Note
Please don't forget to initialize the TParProps records using FillRect(prp, SizeOf(TParProps), 0); At the end of your procedure you need to assign 'nil' to all the strings and dynamic arrays you have used to avoid a memory leak. This all of course only affects you if you are not using .NET.

Category
V4_FastProcedures


FastAddTable Example
We are using procedure TWPCustomRichText.FastAddTable(col: Integer; Pprp : PTParProps) from unit WPCTRRich.PAS.

// This example is using the old pointer
// based implementation. It works with
// WPTools Version 4 and WPTools version 6
// We create a 6 column	table filled
// with	data using some	generic	code
var
  AllCount: Integer;
  // You can change that to read from a	table
  function NextRow: Boolean;
  begin
    dec(AllCount);
    Result := (AllCount	> 0);
    // Table1.Next;
    // Result := not Table1.EOF;
  end;
  function Column(colnr: Integer): string;
  begin
    Result := IntToStr(colnr) +	'.CELL';
    // Result := Table1.Fields[colnr].AsString;
  end;
  // We	need this variables for	FastAddTable
var
  colcount, i, j, maxlen: Integer;
  p_props, pp1:	PTParProps;
  pa: PTattr;
  Attr:	TAttr;
  FFirst, FLast	: Boolean;
  WPRichText1 :	TWPRichText;
begin
  WPRichText1 :=Source;
  if WPRichText1 <> nil	then
  begin
    WPRichText1.BeginTable('', 0, 0, 0);
    AllCount :=	200; //	Demo: Count of Rows
    colcount :=	5; // How many columns
    maxlen := 100; // Maximum length for one cell
    GetMem(p_props, SizeOf(TParProps) *	colcount);
    FillChar(p_props^, SizeOf(TParProps) * colcount, 0);
    pp1	:= p_props;
    FFirst := TRUE;
    for	i := 0 to colcount - 1 do
    begin
      GetMem(pp1^.pa, SizeOf(TAttr) * maxlen);
      FillChar(pp1^.pa^, SizeOf(TAttr) * maxlen, 0);
      inc(pp1);
    end;
    Attr := WPRichText1.Attr;
    // ---------------------
    try
      // Table1.First;
      // if not	Table1.EOF do
      repeat
	pp1 := p_props;
	for i := 0 to colcount - 1 do
	begin
	  // The text for the cell
	  pp1^.Text := Copy(Column(i), 1, maxlen);
	  j := Length(pp1^.Text);
	  // The attribute of the cell - the first char	is bold
	  pa :=	pp1^.pa;
	  pa^ := Attr;
	  pa^.Style := [afsBold];
	  inc(pa);
	  dec(j);
	  while	j > 0 do
	  begin
	    pa^	:= Attr;
	    inc(pa);
	    dec(j);
	  end;
	  // Some border properties
	  pp1^.BorderType.LineType := [blEnabled, blBottom];
	  include(pp1^.BorderType.LineType, blRight);
	  if i=0 then include(pp1^.BorderType.LineType,	blLeft)
	  else exclude(pp1^.BorderType.LineType, blLeft);

	  if FFirst then include(pp1^.BorderType.LineType, blTop)
	  else exclude(pp1^.BorderType.LineType, blTop);

	  inc(pp1);
	end;
	FFirst := FALSE;
	FLast := not NextRow;
	// do something	for the	last ?
	WPRichText1.FastAddTable(colcount, p_props);
      until FLast;
      // ----------------------
    finally
      pp1 := p_props;
      for i := 0 to colcount - 1 do
      begin
	pp1^.Text := '';
	FreeMem(pp1^.pa);
	inc(pp1);
      end;
      FreeMem(p_props);
    end;
    WPRichText1.EndTable;
    WPRichText1.Refresh;
  end;
end;


WPCubed