Bookmarks

Top  Previous  Next

The bookmarks of WPTools Version 9 are very powerful. They can be nested and several functions make it easy to work with the marks.

 

It is possible tio hide the bookmarks or to sow them. When they are displayed it is even possible to display themy through owner drawn code as in this example:

 

clip0022

This code creates a book mark

 

  WPRichText1.BookmarkInput('BM' + IntToStr(bm),true);

 

The following functions deal with bookmarks

 

//:: finds and selects a Bookmark

  function BookmarkSelect(const Name: string; OnlyText: Boolean): Boolean;

  //:: locates a bookmark and and moves Cursor

  function BookmarkMoveTo(const Name: string): Boolean;

  //:: locates a bookmark and and moves Cursor. Start at the current position

  function BookmarkMoveToNext(const Name: string): Boolean; // locates and moves Cursor to next ..

  //:: locates and returns position (or -1 iof not found)

  function BookmarkFind(const Name: string): Integer;

  {:: Creates a bookmark. If text is currently selected the selected text

      will be put iside of the new bookmark markers }

  function BookmarkInput(const Name: string; PlaceCursorBetweenTags: Boolean = FALSE):TWPTextObj;

  procedure BookmarkDeleteAllMarkers;

  {:: Deletes the bookmark markers with the given name. This procedure

   searches through all blocks of the current text }

  procedure BookmarkDeleteMarkers(const Name: string);

  procedure BookmarkDelete(const Name: string; Marks, Text: Boolean);

  function BookmarkDeleteInPar(const NameStart: string; par: TParagraph): Boolean;

  procedure BookmarkGetList(list: TStrings; FromAllBlock: Boolean = FALSE); overload;

  procedure BookmarkGetList(list: TWPTextObjList; FromAllBlock: Boolean = FALSE); overload;

 

  {:: This functions checks if the text at the given position is locate

   inside of a bookmark.<br>

   To check if there is a bookmark object under the mouse

   cursor use CodeAtXY(X,Y,Code)! }

  function BookmarkAtXY(x, y: Integer; var Bookmark: TWPTextObj): Boolean;

  {:: This functions checks if the cursor is inside of a bookmark.<br>

   To check for a text object, such a bookmark object at the cursor position

   use CPAttr.GetObject  }

  function BookmarkAtCP: TWPTextObj;

  function BookmarkFirstInPar(par: TParagraph): string;

  function BookmarkAtParLin(par: TParagraph; pos_in_par: Integer): TWPTextObj;

  function BookmarkForceInPar(par: TParagraph; const frmstr: string): string; // reserved

 

Example:

 

This code in the event handler for OnTextObjGetTextEx is used to paint the bookmarks in the image above:

 

procedure TForm1.WPRichText1TextObjGetTextEx(RefCanvas: TCanvas;

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

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

begin

if not ShowBookmarkCodes.Checked then exit;

if TXTObject.ObjType=wpobjBookmark then

begin

    // If you set PaintObject to anything <> nil PrintString will be ignored!

    // Here you can initialize an event for an owner draw object

    PaintObject := TXTObject;

    PaintObject.OnPaint := OnPaintObject;

    WidthInPix := RefCanvas.TextWidth(TXTObject.Name+'<'+#32);

end;

end;

 

procedure TForm1.OnPaintObject(Sender : TWPTextObj;

   OutCanvas  : TCanvas;

   XRes, YRes : Integer;

   X, Y, W, H, BASE: Integer );

var s : string;

begin

   OutCanvas.Rectangle(x,y,x+w,y+h);

   OutCanvas.MoveTo(x+w,y+1);

   OutCanvas.LineTo(x+w,y+h);

   OutCanvas.LineTo(x+1,y+h);

  if wpobjIsClosing in Sender.Mode then

      s := '>' + Sender.Name

  else s := Sender.Name + '<';

   OutCanvas.TextOut(x+2,y+BASE,s);

end;

 

Sometimes you will need to replace the text which is wrapped within bookmarks. This can be useful to process mailmerge forms which used to be processed using a word processor and OLE.

 

The following code is fast and effective:

 

procedure TForm1.ReplBookmarksClick(Sender: TObject);

var

 allbm : TWPTextObjList;

 i : Integer;

begin

  allbm := WPRichText1.CodeListTags(wpobjBookmark,'*ALL*',true);

  try

    for i:=0 to allbm.Count-1 do

    if allbm[i].Name='sum' then // check the name!

    begin

      // simply assign text. You can even create new paragraphs

      // with #13 or #13+#10 sequences.

      allbm[i].EmbeddedText :=

        '$134.39'+#13#10

        +'$180.00'+#13#10

        +'$200.00'+#13#10+'$272.00';

    end;

  finally

    allbm.Free;

  end;

  WPRichText1.ReformatAll(false,true);

end;