Equivalent questions:
At which end of a selection is the visible cursor?
At which end of a cursor will the next character be inserted?
Let's open a writer document with some text. Any document will do. Place the cursor somewhere in the middle of a line an consider two scenarios:
A cursor has the methods getStart(), and getEnd(). According to Andrew Pitonyak (https://www.pitonyak.org/book/: 14.6.2. Selected text: Which end is which?), the start position will always be, where the selection process starts, while the end position is where it ends and the (visible) cursor will therefore be positioned after selecting the text.
Also, the article https://wikinew.openoffice.org/wiki/Text_cursor states, that which is the start and which is the end depends on the direction, in which the selection is made, in contrast to collapseToStart and collapseToEnd, which are relative to the start and end of the document.
Hence, the following code, which has been derived from Pitonyak's text, should provide the answer:
However, it does not. I always get the answer 'cursor before selection', regardless in which direction the selection has been made (AOO 4.1.7 on Windows 10). It also doesn't matter, if I use compareRegionStarts(), getStart() or oViewCursor() in the comparison, I always get the same result for both directions of the selection.
Pitonyak also says, that the behaviour of getStart(), and getEnd() is undocumented and could change at any time, but that would mean, that also the Wiki article is wrong.
Does anybody know, why the code doesn't work, how it could be fixed or if there is an alternative way to solve the problem?
Thanks, and best regards,
ah8
At which end of a selection is the visible cursor?
At which end of a cursor will the next character be inserted?
Let's open a writer document with some text. Any document will do. Place the cursor somewhere in the middle of a line an consider two scenarios:
- Hold down the Shift key and move the cursor to the right using the Cursor Right key. Text is selected, and the cursor is at the end of the selection.
- Hold down the Shift key and move the cursor to the left using the Cursor Left key. Text is selected, and the cursor is at the start of the selection.
A cursor has the methods getStart(), and getEnd(). According to Andrew Pitonyak (https://www.pitonyak.org/book/: 14.6.2. Selected text: Which end is which?), the start position will always be, where the selection process starts, while the end position is where it ends and the (visible) cursor will therefore be positioned after selecting the text.
Also, the article https://wikinew.openoffice.org/wiki/Text_cursor states, that which is the start and which is the end depends on the direction, in which the selection is made, in contrast to collapseToStart and collapseToEnd, which are relative to the start and end of the document.
Hence, the following code, which has been derived from Pitonyak's text, should provide the answer:
Code:
Sub whichEndIsWhich()oViewCursor = ThisComponent.getCurrentController().getViewCursor()oText = oViewCursor.getText()oTextCursor = oText.createTextCursorByRange(oViewCursor)msgbox IIF(oText.compareRegionStarts(oTextCursor.getEnd(), oTextCursor) < 0, "cursor before selection", "cursor after selection")End Sub
Pitonyak also says, that the behaviour of getStart(), and getEnd() is undocumented and could change at any time, but that would mean, that also the Wiki article is wrong.
Does anybody know, why the code doesn't work, how it could be fixed or if there is an alternative way to solve the problem?
Thanks, and best regards,
ah8
Statistics: Posted by ah8 — Sat Jun 01, 2024 9:53 pm