Page 1 of 1

Lost HyperLink in RichViewEdit

Posted: Fri Nov 15, 2013 7:42 am
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;


Posted: Fri Nov 15, 2013 12:07 pm
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); 

Posted: Fri Nov 15, 2013 12:16 pm
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.

Posted: Fri Nov 15, 2013 12:21 pm
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; 

Posted: Fri Nov 15, 2013 2:52 pm
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);

Posted: Fri Nov 15, 2013 4:56 pm
by Sergey Tkachenko
Bpl files must be rebuild on all TRichView updates.
If you rebuild them, does it fix the problem?

Posted: Mon Nov 18, 2013 11:00 pm
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/

Posted: Thu Nov 21, 2013 2:06 pm
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.