WPCubed Product Support

Problem with bookmark delete and input

I use bookmarks for storing information which may appear in a popup menu. Some text is selected and the bookmark name inserted.

Sometimes the user will select text which already contains a bookmark somewhere. The old bookmark must then be deleted before a new one is inserted.

Essentially I use the following code:

Select text and click an Insert button.

DeleteBookmarkInSelection(WPRichText1);WPRichText1.BookmarkInput('TEST');

The procedureDeleteBookmarkInSelection is:

procedure DeleteBookmarkInSelection( WPRichText : TWPRichText);
var
  TxtObj : TWPTextObj;
  const S = 'TOBEDELETED';
begin
  with WPRichText do
  begin
    CPPosition := SelStart;
    repeat
      TxtObj := BookmarkAtCP;
      CPMoveNext;
    until (TxtObj <> nil) or (CPPosition = SelStart + SelLength);
    if TxtObj <> nil then
    begin
      TxtObj.Name := S;  //Rename so other bookmarks with same name are not deleted!
      BookmarkDelete(S,false,false);
    end;
  end;
end;

There is a slight problem: After the bookmark deletion the selection is shifted one character to the right so the bookmark starts at the second character of the first selected word.

Is there an obscure bug in my code or is there a problem with BookmarkDelete?

Julian: I have sent you a demo which shows the effect.

#1 ♥ 0
2.8k Views 2 users read 3 min
support

Hi,

thanks for the example project.

I see the following problems in your code:
a) using CPPosition = SelStart + SelLength in a condition
This properties are all calculated. It would be better to use a
l := SelLength; first and use it in a loop
until (l>0) and CPMobeNext;
b) A bookmark is stored as a TWPTExtObj - this means it is handles as a character. If it is deleted the selection moves because the position inside a paragraph changes.
You could insert your new bookmark BEFORE you use DeleteBookmarks.

The is a callback function: CallForSelectedText - that is ideal for this task:

procedure TForm1.SelObjCallback(Sender: TObject;
          TxtObj: TWPTextObj;  par: TParagraph;
          posinpar: Integer; var Abort: Boolean);
begin
  if TxtObj.ObjType=wpobjBookmark then
     (Sender as TWPTextObjList).Add(TxtObj);
end;

procedure TForm1.DeleteBookmarkInSelection( WPRichText : TWPCustomRtfEdit);
var
  TxtObjLst : TWPTextObjList;
  i : Integer;
  par : TParagraph;
begin
  TxtObjLst := TWPTextObjList.Create;
  try
    WPRichText1.TextCursor.CallForSelectedText( TxtObjLst,  // --> Sender
     [],          // options
     nil,nil,nil, SelObjCallback,  // different callbacks
     nil, nil, 0, 0); // Optionally par/posinpar

    //---------------------------------
    // NOW INSERT NEW BOOKMARK
    // --------------------------------

    // NOW DELETE ALL BOOKMARKS IN SELECTION
    for i:=0 to TxtObjLst.Count-1 do
    begin
      par := TxtObjLst[i].ParentPar;
      TxtObjLst[i].Free;
      par.DeleteMarkedChar;
    end;
    WPRichText1.DelayedReformat;
  finally
    TxtObjLst.Free;
  end;
end;

(BTW: To highlight this code shippit I inserted it, selected it and than clicked on the |code| button in the toolbar of this editor.)

#2 ♥ 0
This category is archived. No new posts are possible.