<< Click to Display Table of Contents >> Navigation: Programming > Move cursor / select text > TWPRTFDataCursor > Drop-Markers |
The 'Drop Markers' are special markers which can be placed in the text. They are not really inserted in the text (such as bookmarks). They are simple flags which can be used to quickly return to a certain position in the text. They are not saved with the text. Many of the input routines automatically move the drop marker, this makes them much more powerful than simply storing the text position (CPPosition).
function DropMarker: Integer;
function DropMarkerAt(par: TParagraph; PosInPar: Integer): Integer;
This functions drop a marker which can be collected later with CollectMarker.Text insertions and deletions - as long as no paragraphs are inserted or deleted - will will automatically modify the markers of the affacted paragraphs to make sure their logical position is not changed. This makes markers much more powerful than cursor character offsets, such as 'CPPosition'.
We suggest to delete all markers with CollectAllMarker when they are not required anymore.
Note:
•a) that markers are not saved with the text.
•b) If the paragraph has been deleted which was used for a marker, the marker will be moved to the start of the following paragraph.
Drops a marker at the start/end of the selected text:
function DropMarkerAtSelStart: Integer;
function DropMarkerAtSelEnd: Integer;
Returns the text position of a certain marker. The result value will be -1 if the marker is not defined or if it is located in a RTFDataBlock which is currently not edited. (ActiveText) Please use GotoMarker to move the cursor to a certain position.
function DropMarkerPosition(DroppMarkerID: Integer): Integer;
Moves to a marker which was dropped by 'DropMarker'. If the parameter 'Collect' = TRUE this invalidates all markers which were dropped after the specified marker. You can use (true,-1) to collect the last marker or (true,X) to delete all markers including the marker X. (true,1) will collect all markers! If it is not possible to move to that marker the result value is FALSE.
function GotoMarker(Collect: Boolean = TRUE;
DroppMarkerID: Integer = -1): Boolean;
function GotoMarker(DroppMarkerID: Integer): Boolean;
Select the text between 2 markers.
function SelectMarker(FromMarker, ToMarker: Integer): Boolean;
Select text with a give length starting with a given marker.
function SelectMarkerStartLen(FromMarker, Length: Integer): Boolean;
Removes all markers from the text:
procedure CollectAllMarker;
Collects all markers including and after DroppMarkerID:
procedure CollectMarker(DroppMarkerID: Integer);
Example: This is the implementation of the ReplaceTokens procedure. It converts text which is wrapped by special characters such as "<<" and ">>" into mail merge fileds, It uses the DropMarkers and the Finder. It also uses some 'Code' methods to create the objects which are used to mark mail merge fields.
function TWPCustomRtfEdit.ReplaceTokens(const opening, closing: string): Integer;
var
s, r: string;
StartID, SelStartID, SelEndID, rl: Integer;
RestoreSel: Boolean;
startf, endf: TWPTextObj;
begin
StartID := 0;
RestoreSel := FALSE;
SelEndID := 0;
SelStartID := 0;
Result := 0;
with TextCursor do
try
s := opening + '*' + closing;
StartID := DropMarker;
SelStartID := DropMarkerAtSelStart;
SelEndID := DropMarkerAtSelEnd;
RestoreSel := HideSelection;
with Finder do
begin
ToStart;
while Next(s) do
begin
r := FoundText;
rl := FoundLength;
CPPosition := FoundPosition + rl - Length(closing);
DeleteChar(Length(closing));
r := Copy(r, Length(opening) + 1,
Length(r) - Length(opening) - Length(closing));
endf := InputSingleCode(wpobjMergeField, r);
endf.Mode := [wpobjUsedPairwise, wpobjIsClosing];
// Move the cursor to the found position
CPPosition := FoundPosition;
DeleteChar(Length(opening));
startf := InputSingleCode(wpobjMergeField, r);
startf.Mode := [wpobjUsedPairwise, wpobjIsOpening];
endf.SetTag(startf.NewTag);
MoveNext(Length(r));
inc(Result);
end;
end;
finally
HideSelection;
if RestoreSel then
begin
TextCursor.SelectMarker(SelEndID, SelStartID);
end;
TextCursor.GotoMarker(StartID);
Refresh;
end;
end;