Changing hyperlink tag issue

General TRichView support forum. Please post your questions here
Post Reply
whisper1980
Posts: 20
Joined: Sun May 25, 2025 6:41 pm

Changing hyperlink tag issue

Post by whisper1980 »

Using the eval version under Delphi 12.3

I have this RTF hyperlink in a TRichViewEdit after loading an RTF file:

Code: Select all

{\field{\*\fldinst{HYPERLINK "$noprint:\{1\}"}}{\*\fldtitle{$noprint:\{1\}}}
{\fldrslt{\f0\cf1 pre-cloaked\f0\cf0  \f0\cf1\b red\f0\cf1\b0  line\f0\cf0}}}
Within the hyperlink there is a bold word "red", which seems to divide the hyperlink into several text items within the field. If I click on either "red" or "line" and attempt to update the tag, it actually inserts a new hyperlink field. For example, I use the onclick event and do this:

Code: Select all

  var vTag := String(RichViewEdit1.GetCurrentTag);
  if vTag.StartsWith('$noprint:{0}', true) then
    RichViewEdit1.SetCurrentTag(ReplaceStr(vTag, '{0}', '{1}'))
  else if vTag.StartsWith('$noprint:{1}', true) then
    RichViewEdit1.SetCurrentTag(ReplaceStr(vTag, '{1}', '{0}'));
I end up with this RTF after clicking on the word "line":

Code: Select all

{\field{\*\fldinst HYPERLINK "$noprint:\{1\}"}
{\fldrslt \plain \f0\fs20\cf11 pre-cloaked\plain \f0\fs20  \plain \f0\b\fs20\cf11 red}}
{\field{\*\fldinst HYPERLINK "$noprint:\{0\}"}{\fldrslt \plain \f0\fs20\cf11  line}}
I wanted to just update the original Hyperlink field tag, and not insert a new hyperlink field. I assume this is because GetCurrentTag is operating on the item's outer field, and SetCurrentTag is operating on the current text item "line", so it makes it a hyperlink for some reason.

I'm not sure how to get around this. I've also tried using GetCurrentItem and updating the tag that way, but the same thing occurs. Help?

Eric
whisper1980
Posts: 20
Joined: Sun May 25, 2025 6:41 pm

Re: Changing hyperlink tag issue

Post by whisper1980 »

I've attached a modified version of the Editor 1 demo which also includes foundations.rtf you need to load with it. The referenced RTF in my previous post that can be clicked on is [ pre-cloaked red line ].

Eric
Attachments
Editor 1 modified.zip
(83.31 KiB) Downloaded 12 times
Sergey Tkachenko
Site Admin
Posts: 17852
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Changing hyperlink tag issue

Post by Sergey Tkachenko »

Yes, if a hyperlink has text having different fonts, it is represented as several text items.
But they are highlighted as a whole, and they are saved back to RTF as a single hyperlink.

However, if you want to update the link target, you need to update Tags of all adjacent items that has the same tag.
Your code modifies Tag of one of items. Other items still have the old Tag value. So, after modification, there are several links: with the new Tag and with the old Tag.
I'll post a code sample tomorrow.
whisper1980
Posts: 20
Joined: Sun May 25, 2025 6:41 pm

Re: Changing hyperlink tag issue

Post by whisper1980 »

This is what I came up with which seems to work to toggle a tag value. See any issues or even a better way?

Code: Select all

  var vTag := String(RichViewEdit1.GetCurrentTag);
  if not vTag.StartsWith('$noprint', true) then exit;

  var vItem := RichViewEdit1.GetCurrentItem;
  var vItemNo := RichViewEdit1.GetItemNo(vItem);

  // Find the first item
  var vFirstItemNo := vItemNo-1;
  while RichViewEdit1.GetItemTag(vFirstItemNo) = vTag do
    Dec(vFirstItemNo);

  // Find the last item
  var vLastItemNo := vItemNo+1;
  while RichViewEdit1.GetItemTag(vLastItemNo) = vTag do
    Inc(vLastItemNo);

  for var i := vFirstItemNo + 1 to VLastItemNo-1 do
  begin
    vItem := RichViewEdit1.GetItem(i);

    if vTag.StartsWith('$noprint:{0}', true) then
      vItem.Tag := ReplaceStr(vTag, '{0}', '{1}');

    else if vTag.StartsWith('$noprint:{1}', true) then
      vItem.Tag := ReplaceStr(vTag, '{1}', '{0}');
  end;
Post Reply