Source View

Top  Previous  Next

It is possible to capture the HTML source code and display it in a second editor:


Here we have two tabs, the first shows the TWPRichText which displays the formatted text, the other shows the source code and uses the XML syntax highlighting.

The editors are individual, edits in one will not update the other editor.clip0172


The second editor is initialized like this:


procedure TWPWebBrowser.FormCreate(Sender: TObject);



// Update the Source view

 WPRichText1.RTFData.AfterLoadFromStream := WPRichText1LoadFromStream;

// uses WPSyntaxHighlight:

 SourceView.CustomSyntax := TWPXMLSyntax.Create(nil);




The text is assigned in the event TWPRTFDataCollection.AfterLoadFromStream. The handler uses this code:


procedure TWPWebBrowser.WPRichText1LoadFromStream(

   RTFData: TWPRTFDataCollection;

   Stream: TStream;

   Reader: TWPCustomTextReader;

   OnlyBodyText: Boolean;

  var LoadedText: TWPRTFDataBlock);


if not OnlyBodyText then


    Stream.Position := 0;

    if Reader.CopyOfBodyData<>nil then

        // HTML extracted from MIME data

         SourceView.LoadFromStream(Reader.CopyOfBodyData, 'ANSI', true)

        // Original Stream

    else SourceView.LoadFromStream(Stream, 'ANSI', true);

    Caption := WPRichText1.RTFVariables.Strings['Title'];




Here we use the property CopyOfBodyData. This memory stream is only filled by the MIME reader to capture the HTML body source. The normal stream would show the complete message text, including all header lines and attachments.