Step 1 and 2 only handle references to external resources, i.e. addresses of web pages or files. In this step, links to positions in the same document will be handled. These links are called internal links and the positions, to where they point, are called targets. Targets are also referred to as anchors (in the context of HTML editors) or bookmarks (in word processors). When using this example, first add some text and then some targets with the Insert / Target... menu item. Finally use the Insert / Hypertext Link... menu item to add links to these targets.
1. Inserting a Target:
Targets are realized again as a special type of a marked text field. The type and the target's name must be set with the FieldType and the FieldTypeData properties.
Unlike links, targets have no visible text, therefore an empty field must be inserted with the FieldInsert method to insert a target:
procedure TForm1.Target1Click(Sender: TObject); var TargetName : String; begin TargetName := InputBox('Target name:', 'Insert target', ''); If TargetName <> '' Then begin TXTextControl1.FieldInsert (''); TXTextControl1.FieldType[TXTextControl1.FieldCurrent] := txFieldLinkTarget; TXTextControl1.FieldTypeData[ TXTextControl1.FieldCurrent] := TargetName; End; end;
Only one text box is required to display the name of a target, so a simple InputBox statement can be used.
2. Inserting Links to Targets:
To insert links to the just inserted targets, the Hypertext Link dialog box is extended with a list box showing the names of all targets the document contains. The FieldNext method is used to fill this list box:
Procedure TForm2.FillListboxWithTargets(Sender : TObject); var FieldID : Integer; begin ListBox1.clear; FieldID := tx.FieldNext(0, $100); While FieldID <> 0 do begin ListBox1.Items.Add(tx.FieldTypeData[FieldID]); FieldID := tx.FieldNext(FieldID, $100); end; end;
When the user selects a target, the Link To field is filled with the target's name.
After typing the link's text and pressing the OK button, the link is inserted. An internal link is inserted in the same way as the external links from step 1, but the FieldType property now is set to txFieldInternalLink and the FieldTypeData property is set to the target's name.
3. Jumping to a Target:
After inserting internal links and targets, a jump must be realized. When the user clicks on a marked text field that represents a hypertext link, Text Control fires a FieldLinkClicked event. The information provided through this event can be used with the FieldGoto method to jump to the target:
procedure TForm1.TXTextControl1FieldLinkClicked(Sender: TObject; FieldId, FieldType: Smallint; var TypeData: WideString); begin If FieldType = txFieldInternalLink Then TXTextControl1.FieldGoto(txFieldLinkTarget, TypeData); end;
While the FieldGoto method is used for targets within the same file, links to external targets must be treated differently. When the Jumping to an External Target event occurs, and the FieldType parameter indicates that the link is external, then it depends on the type of the application, what to do. External links can point to, for instance, files on the local harddisk, or addresses in the internet.
Note that responding to the events is only required for making the hypertext links work while the text is edited in Text Control. If the text is saved to a file and displayed with a browser, then the hypertext links will work depending on the used browser.