FastAddTable method

Applies to
TWPCustomRichText

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

Declaration
procedure FastAddTable(col: Integer; const Pprp: array of TParProps);

Description
This is the .NET version of FastAddTable. Instead of a pointer to the properties a dynamic array has to be passed to describe the cells. The value of 'col' may be smaller or larger than the length of the array. If it is larger, the last entry in the array will be used for the rest of te columns.

Category
V4_FastProcedures


FastAddTable Example
We are using procedure TWPCustomRichText.FastAddTable(col: Integer; const Pprp : array of TParProps) from unit WPCTRRich.PAS

// This example is using the new dynamic
// array based implementation. 
// It requires WPTools Version 5
// 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: array of TParProps;
  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
    SetLength(p_props,colcount);
    FFirst := TRUE;
    for i := 0 to colcount - 1 do
      SetLength( p_props[i].parr, maxlen);
    Attr := WPRichText1.Attr;
    // ---------------------------------------------------------------------------
    try
      // Table1.First;
      // if not Table1.EOF do
      repeat
        for i := 0 to colcount - 1 do
        begin
          // The text for the cell
          p_props[i].Text := Copy(Column(i), 1, maxlen);
          j := Length(p_props[i].Text);
          // The attribute of the cell - the first char is bold
          p_props[i].parr[0] := Attr;
          p_props[i].parr[0].Style := [afsBold];
          dec(j);
          while j > 0 do
          begin
            p_props[i].parr[j] := Attr;
            dec(j);
          end;
          // Some border properties
          p_props[i].BorderType.LineType := [blEnabled, blBottom];
          include(p_props[i].BorderType.LineType, blRight);
          if i=0 then include(p_props[i].BorderType.LineType, blLeft)
          else exclude(p_props[i].BorderType.LineType, blLeft);

          if FFirst then include(p_props[i].BorderType.LineType, blTop)
          else exclude(p_props[i].BorderType.LineType, blTop);
        end;
        FFirst := FALSE;
        FLast := not NextRow;
        // do something for the last ?
        WPRichText1.FastAddTable(colcount, p_props);
      until FLast;
      // ---------------------------------------------------------------------------
    finally
      for i := 0 to colcount - 1 do
      begin
        p_props[i].Text := '';
        p_props[i].parr := nil;
      end;
      p_props := nil;
    end;
    WPRichText1.EndTable;
    WPRichText1.Refresh;
  end;
end;

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 5!

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 5
// 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;


Copyright (C) by WPCUBED GmbH - Munich
http://www.wpcubed.com