Hot to determine the cursor position

    • Offizieller Beitrag
    Zitat

    I'm not clear on which is the best way to determine the position, relative to the start of the paragraph, of the text cursor.

    Pre-V5, I'd use <TWPRichText>.CPColNr, but that seems to be returning unexpected values (such as negative ones). So I tried <TWPRichText>.CPPosInPar. That seems to always be zero.

    What's the best way to do this?

    Please note that in WPTools 5 the cursor position is only determined by the paragraph and the position in the paragraph, no 'PTline' any more.

    The paragraph is physcially stored in
    WPRichgText.TextCursor.active_paragraph
    the position
    WPRichText.TextCursor.active_posinpar

    This are indeed variables, no properties so you can be sure they refelct the current value.

    CPColNr is a property which reflects the position in the current line (on screen!) - so this property should not be used if the text has not been formatted yet. In this case the result can be wrong!

    CPPosInPar just reads out WPRichText.TextCursor.active_posinpar.

    Please note that it usually not ideal to change the active_paragraph variable directly. Please use TextCursor.MoveTo and check the result for true. If the cursor was not moved that is FALSE.

    Zitat

    Is <TWPRichText>.SetPosition and <TWPRichText>.GetPosition not as reliable in V5 as in earlier versions?

    I've seen a number of instances in my application where they don't return the expect results or movement, and I'm starting to get the feeling that <TWPRichText>.CPPosition is the preferred approach, both for setting and retrieving the cursor position. Does this sound right to you?

    CPPosition is the absolute text position relatively to the start of the text. Each paragraph end is counted as 1 character, too. So CPPosition is an exact value.

    SetPosition / GetPosition has been mainly included to improve compatibility with 'old' WPTools. Please note that the 'line' part in fact is just a logical value, in WPZTools 4 it was based on the phsiacl pline pointer, and was exact. In WPTools it is based on the line array which stores nothing but offsets in the paragraph text. In general I would recommend to not use SetPosition / GetPosition.

    Conclusion:
    To read and write the position in the current paragraph use WPRichText.CPPosInPar.

    For absolute position use WPRichText.CPPosition, this is the same as TextCursor.Position.

    If you are use a paragraph is not deleted you can also store a par reference anbd then use TextCursorMoveTo to go to it agaian. This also works beteen text blocks, such as header and footer.

    If questions are open please let me know.

    Julian

  • Thanks for explaining the differences between the various variables and properties.

    I do have a few follow-up questions. You mentioned, in reference to CPColNr, that

    Zitat

    CPColNr is a property which reflects the position in the current line (on screen!) - so this property should not be used if the text has not been formatted yet. In this case the result can be wrong!


    Which is the preferred method to format it? I'm getting unexpected (negative) results when checking out a document that's displayed in a TWPRichText component, and I'd have thought it's already formatted, so I'm not sure exactly what is meant by "formatted"

    Also, with apologies, I don't quite follow the third paragraph in the Conclusion:

    Zitat

    If you are use a paragraph is not deleted you can also store a par reference anbd then use TextCursorMoveTo to go to it agaian. This also works beteen text blocks, such as header and footer.


    Could you please restate that? It sounds intriguing...:) A short example would be great.

    Lastly, I'd like to make a general comment about Version 5. As I've been migrating my (rather large) application from WPTools 3 to WPTools 5, I've found in a number of instances that somewhat lengthy blocks of WPTools 3-level code were rewritten with just a few, and occasionally, just one line of WPTools 5-level code! Lovely.

    diamond

    • Offizieller Beitrag

    Thanks for your comments!

    I checked the CPColNr and you are right. There is a problem in the code. The fix did not make it into R5 but it will be in R5.1

    About the par, posinpar reference I simply thought of the following:

    Code
    var  oldpar : TParagraph; oldposinpar : Integer;begin with WPRichText1.TextCursor do  begin     oldpar := active_paragraph;     oldpoainpar := active_posinpar;     // do something - NO DELETIONS PLEASE!     // Moving the cursor is ok, even to a different RTFDataBlock     // goto old position     MoveTo( oldpar , oldpoainpar )  end;


    Please also don't forget about the drop-markers (mentioned in other post).

    They are very efficient if you need a stack of 'bookmarks' which live through simple text insertions and deletions (only on paragraph basis, no load operations).

    Example:

    Note: You can either use SelectMarker or GotoMarker - but not both.

    Julian Ziersch