Hyperlink Basic Functions

Demos, code samples. Only questions related to the existing topics are allowed here.
Post Reply
Aeperd
Posts: 20
Joined: Thu Dec 26, 2024 1:30 pm

Hyperlink Basic Functions

Post by Aeperd »

Is there any new and simple to implement for this basic functions for hyperlink? Add, Remove, Update and Detect if is is a hyperlink in RichViewEdit? Thanks in advance.
Sergey Tkachenko
Site Admin
Posts: 17805
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Hyperlink Basic Functions

Post by Sergey Tkachenko »

At their core, hyperlinks are items within a document. Possible options include:
  • If it is a text item, it must be associated with a text style that has the property Jump = True.
  • If it is a non-text item that is still associated with a text style (such as a label, footnote, etc.), then similarly, the text style must have the property Jump = True.
  • There are certain items that are hyperlinks by themselves, in particular the hot-picture item.
Although it’s not mandatory, it is convenient to store hyperlink targets in the Tag property, which is available for any item in a document. Almost all TRichView demos assume that Tag contains only the link target. However, you can store any custom text information there—in that case, you'll need to write a function to add and extract the link targets from the Tag. But for the purposes of this guide, we’ll assume that Tag contains only the link target.

The simplest examples of creating a document with hyperlinks and handling clicks on them (in the OnJump event) can be found in the second group of Tutorial projects for TRichView.
Sergey Tkachenko
Site Admin
Posts: 17805
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Hyperlinks in RichViewActions

Post by Sergey Tkachenko »

Hyperlinks in RichViewActions

The easiest way to work with hyperlinks is by using RichViewActions. The main commands for inserting and editing links are implemented in the TRVActionInsertHyperlink action. When this action is executed, a dialog window appears where you can set the properties of a new link or modify an existing one—including removing it by entering an empty target. It also supports links to positions within the current document; just like in HTML, such links start with the # character.

The appearance of a hyperlink depends on whether "real" (named) styles—StyleTemplates—are being used. If Editor.UseStyleTemplates = True, the appearance of hyperlinks should be defined in the style named "Hyperlink". Otherwise, the color and other formatting of the hyperlink text are set in the properties of TRVActionInsertHyperlink, and can also be changed by the user in a special dialog window, which can be opened from the insert/edit hyperlink dialog.

To follow a hyperlink, call the rvActionInsertHyperlink.GoToLink method in the OnJump event. It can open URLs and files, or scroll to a position within the document.

rvActionInsertHyperlink has the methods DetectURL and TerminateHyperlink, which should be called from text input events. DetectURL automatically creates hyperlinks from URLs on typing, while TerminateHyperlink switches the current style from hyperlink to regular text when the user types a character that ends the link (such as a space).

TRVActionInsertHyperlink has several other useful methods as well.

And finally, TRVActionRemoveHyperlinks removes all hyperlinks from the selected text.
Sergey Tkachenko
Site Admin
Posts: 17805
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Hyperlinks without RichViewActions

Post by Sergey Tkachenko »

Hyperlinks without RichViewActions

If you don't want to use RichViewActions (or can't, for example because you're working with FireMonkey, where RichViewActions are not yet implemented), take a look at the demo projects in the TRichView\Demos\*\Assorted\Hypertext\ folder.

CreateHyperlink demonstrates how to create a new hyperlink, change its target, or remove an existing one. It has a limitation—it works only with text items.

The URLs demo shows how to detect URLs while typing, find URLs in existing text, and modify link targets during editing. It includes a useful URLScan.pas unit that you can incorporate into your own application.
Sergey Tkachenko
Site Admin
Posts: 17805
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Plans

Post by Sergey Tkachenko »

Plans

I’m planning to create a new component to simplify document editing—providing an easy-to-use interface for working with text and paragraph attributes, hyperlinks, and tables. For VCL, it will serve as a lightweight alternative to RichViewActions; for FireMonkey, it will be the foundation for future RichViewActions FMX.
Post Reply