Lost HyperLink in RichViewEdit

General TRichView support forum. Please post your questions here
Post Reply
Wizard
Posts: 3
Joined: Thu Nov 14, 2013 3:31 pm

Lost HyperLink in RichViewEdit

Post by Wizard »

I need edit HTML document. I Try RichViewEdit component. I LoadHTML document using TRvHtmlImporter and save it using RichViewEdit function “SaveToHTML”. It’s Work but if I insert hyperLink in Document I lost url when save it.

I find what I must use “RichViewEdit1WriteHyperlink” and “RichViewEdit1ReadHyperlink” properties. I insert code in it. It’s fix may problem, but when I paste formatted Text from clip boar program rise Error “List Out of bound”. How can I fix all my problems?

Code: Select all

procedure TfrmRichEditForm.RichViewEdit1ReadHyperlink(Sender: TCustomRichView;
  const Target, Extras: string; DocFormat: TRVLoadFormat; var StyleNo,
  ItemTag: Integer; var ItemName: TRVRawByteString);
var
  lStr: String;
  lPWideChar: PWideChar;
  lPointer: Pointer;
begin
  if DocFormat=rvlfURL then
    StyleNo :=
      rvActionInsertHyperlink1.GetHyperlinkStyleNo(RichViewEdit1);
  lStr:= (rvActionInsertHyperlink1.EncodeTarget(Target));
  New(lPWideChar);
  GetMem(lPWideChar, Length(lStr)*SizeOf(Char));
  Move(lPWideChar, lStr, Length(lStr));
  lPointer:= lPWideChar;
  ItemTag:= integer(lPointer);
end;
procedure TfrmRichEditForm.RichViewEdit1WriteHyperlink(Sender: TCustomRichView;
  id: Integer; RVData: TCustomRVData; ItemNo: Integer;
  SaveFormat: TRVSaveFormat; var Target, Extras: string);
var
  lRVTag: TRVTag;
begin
  if ItemNo >= 0 then begin
     lRVTag := RichViewEdit1.GetItemTag(ItemNo);
     if lRVTag > 0  then
       Target:= PWideChar(lRVTag);
  end;
end;

Sergey Tkachenko
Site Admin
Posts: 17569
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

What version of TRichView do you use?

I can see you already use TRVTag type (= String). This type was introduced when tags became strings.
But I can see that your RichViewEdit1ReadHyperlink has incorrect (old) parameters - ItemTag is Integer instead of TRVTag. It may lead to all kinds of errors (and actually, Delphi should warn you about incompatible parameter types).
It must be:

Code: Select all

procedure TfrmRichEditForm.RichViewEdit1ReadHyperlink(Sender: TCustomRichView; 
  const Target, Extras: string; DocFormat: TRVLoadFormat; 
  [color=red]var StyleNo: Integer;  var ItemTag: TRVTag;[/color]
  var ItemName: TRVRawByteString); 
Sergey Tkachenko
Site Admin
Posts: 17569
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Next, if you use new version of TRichView v13.13 or newer, it is not necessary to assign OnReadHyperlink and OnWriteHyperlink.
Starting from this version, if they are not assigned, TRichView itself read and writes hyperlink targets to item tags.
You said targets are lost, so I assume you use an older version.

The best solution is upgrading to a new version.
In the new version, OnWriteHyperlink is not necessary.
OnReadHyperlink is useful only when pasting URLs (DocFormat=rvlfURL), to switch to a hypertext style.

Additionally, by default TRvHtmlImporter uses OnReadHyperlink only if you exclude rvhtmloAutoHyperlinks from its Options property. If this option is set, it saves hyperlink targets to item tags directly.
Sergey Tkachenko
Site Admin
Posts: 17569
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

If you still want to write handlers for these events.

In OnReadHyperlink, you must not allocate any memory dynamically.
In OnWriteHyperlink, you must call RVData.GetItemTag instead of RichViewEdit1.GetItemTag.

Code: Select all

procedure TfrmRichEditForm.RichViewEdit1ReadHyperlink(Sender: TCustomRichView;
  const Target, Extras: String; DocFormat: TRVLoadFormat; var StyleNo: Integer;
  var ItemTag: TRVTag; var ItemName: TRVRawByteString);
begin
  if DocFormat=rvlfURL then
    StyleNo :=
      rvActionsResource.rvActionInsertHyperlink1.GetHyperlinkStyleNo(RichViewEdit1);
  ItemTag := Target;
end;

Code: Select all

procedure TfrmRichEditForm.RichViewEdit1WriteHyperlink(Sender: TCustomRichView; 
  id: Integer; RVData: TCustomRVData; ItemNo: Integer; 
  SaveFormat: TRVSaveFormat; var Target, Extras: string); 
begin 
   Target := RVData.GetItemTag(ItemNo); 
end; 
Wizard
Posts: 3
Joined: Thu Nov 14, 2013 3:31 pm

Post by Wizard »

I use RichViewEdit v14.5 and RvHtmlImporter v0.0048. I try your solutions but hyperlink still lost url.

When I use code for RichViewEdit1ReadHyperlink and RichViewEdit1WriteHyperlink as you propose i obtain errors when compile project

[dcc32 Error] E2010 Incompatible types: 'string' and 'Integer'
in RichViewEdit1WriteHyperlink
and
[dcc32 Error] E2010 Incompatible types: 'Integer' and 'string'
in RichViewEdit1ReadHyperlink

In this case you mean what I need fix source code of RichViewEdit and re build all *.bpl files ?

Code: Select all

	
procedure TfrmRichEditForm.RichViewEdit1ReadHyperlink(Sender: TCustomRichView;
  const Target, Extras: string; DocFormat: TRVLoadFormat;
  var StyleNo: Integer;  var ItemTag: TRVTag;
  var ItemName: TRVRawByteString);
Sergey Tkachenko
Site Admin
Posts: 17569
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Bpl files must be rebuild on all TRichView updates.
If you rebuild them, does it fix the problem?
Wizard
Posts: 3
Joined: Thu Nov 14, 2013 3:31 pm

Post by Wizard »

I have some problems with rebuilding compoents. I must deactivate directive {.$DEFINE RVOLDTAGS}. Full instraction about converting RTF into HTML I public here.
http://fk-uran.com.ua/trichviewedit-ter ... limporter/
Sergey Tkachenko
Site Admin
Posts: 17569
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

This directive is not activated by default. It was added for simplifying support of old projects, but its using is highly not recommended.
Post Reply