Use TextObjects (i.e. page numbers, sum fields, dynamic chapter headlines)

<< Click to Display Table of Contents >>

Navigation:  Programming > Objects >

Use TextObjects (i.e. page numbers, sum fields, dynamic chapter headlines)

WPTools uses instances of type TWPTextObj for several tasks. They all use the same class definition but not all properties are used for each functionality. The functionality of a TWPTextObj is selected by the property ObjType. There are singualr object, such as anchors for images or simple text objects and paired objects. The latter are used for fields, hyperlinks and bookmarks.

 

Note: We are using one class for all different object types since we wanted to avoid the overhead of class checking here. To converted one object into another it is sufficient to just set the value in ObjType - to check its type we just need to check this integer value and avoid the slow "is" operator. Further more the TWPTextObj have a random lifetime. TWPTextObj will be deleted and duplicated completely under the control of the editor. For example if the user types RETURN in front of page number object, the object will be destroyed and recreated in a new paragraph. In the undo buffer a copy of this object will be created.

 

Using the component TWPTextObjectClasses it is possible to some kind of virtual subclassing to TWPTextObj. Using few selecting properties, certain events can be executed only for certain text objects. This feature has been added in WPTools 8.20.

 

Here we write about standard text objects, the use the ObjType wpobjTextObj.

 

The text objects can be create with InputTextField(type) and InputTextFieldName(name).

 

 

All text objects can show custom text which is defined by event OnTextObjectGetTextEx. Custom text objects will usually display the text assigned to their property Params.

 

It is possible to paint the object yourself if you assign

 PaintObject := TXTObject;

PaintObject.OnPaint := OnPaintSum;

 

with an event handler like this:

 

procedure TForm1.OnPaintSum(Sender: TWPTextObj; OutCanvas: TCanvas; xres, yres: Integer; x, Y, w, h, BASE: Integer);

begin

if Sender.NameIs('anyname') then

begin

    // paint right aligned

    OutCanvas.TextOut(X+w-OutCanvas.TextWidth(Sender.Params),Y+base,Sender.Params);

end;

end;

 

This code evaluates the page the object is printed on. It sums up the values of all paragraphs or cells with the wpat_name "VAL".

 

(The property wpat_name is used by CurrAttr.CellName)

 

 

procedure TForm1.WPRichText1TextObjGetTextEx(RefCanvas: TCanvas;

 TXTObject: TWPTextObj; var PrintString: WideString; var WidthInPix,

 HeightInPix: Integer; var PaintObject: TWPTextObj; Xres, YRes: Integer);

var i : Integer;

   aPage : TWPVirtPage;

   aLineData : TWPVirtPageImageLineRef;

   sum : Double;

begin

if TXTObject.NameIs('anyname') then

begin

    PrintString := '                ';

    PaintObject := TXTObject;

    TXTObject.Params := '--.--';

    if WPRichText1.Memo._MeasureObjectCurrPage<>nil then

    try

       aPage := WPRichText1.Memo._MeasureObjectCurrPage;

       sum := 0;

      for i := 0 to aPage.LineCount-1 do

      begin

 

        if aPage.GetLine(i,aLineData) and (aLineData.Par.CharCount>0) then

        begin

          if (aLineData.LineNr=0) and (aLineData.Par.WPATName='VAL') then

               sum := sum + StrToFloat( aLineData.Par.GetAllText(false,false) );

        end;

      end;

       TXTObject.Params := FloatToStr(sum);

    except

    end;

    PaintObject.OnPaint := OnPaintSum;

end

end;