Difference between revisions of "Writer/API/Text cursor"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (Start and End of a text cursor: sp, l.c.e)
(demote headings to Level 2)
Line 1: Line 1:
 
A text cursor can only move within the text range of the text object in which it was created. Thus a text cursor that is created for the main document's text range can not move inside a table.  
 
A text cursor can only move within the text range of the text object in which it was created. Thus a text cursor that is created for the main document's text range can not move inside a table.  
  
=Creating a text cursor=
+
==Creating a text cursor==
 
A common thing to do is to create a text cursor which has the same location as the view cursor, as follows:  
 
A common thing to do is to create a text cursor which has the same location as the view cursor, as follows:  
  
Line 17: Line 17:
 
</source>
 
</source>
  
=Start and End of a text cursor=
+
==Start and End of a text cursor==
 
A cursor has the methods <tt>getStart</tt>, and <tt>getEnd</tt>. Which is the <tt>Start</tt> end and which is the <tt>End</tt> end of the selection is determined by the direction in which the selection was made.  
 
A cursor has the methods <tt>getStart</tt>, and <tt>getEnd</tt>. Which is the <tt>Start</tt> end and which is the <tt>End</tt> end of the selection is determined by the direction in which the selection was made.  
  
 
However, the methods <tt>collapseToStart</tt> and <tt>collapseToEnd</tt> are relative to the start and end of the document.
 
However, the methods <tt>collapseToStart</tt> and <tt>collapseToEnd</tt> are relative to the start and end of the document.
  
=Moving a cursor=
+
==Moving a cursor==
 
A cursor has a number of <tt>gotoXXX</tt> where XXX is some relative location. In each case there is always a boolean parameter for "extending the selection". When this parameter is set to <tt>true</tt> the selection is extended from the current selection to the new location.  
 
A cursor has a number of <tt>gotoXXX</tt> where XXX is some relative location. In each case there is always a boolean parameter for "extending the selection". When this parameter is set to <tt>true</tt> the selection is extended from the current selection to the new location.  
  
Line 35: Line 35:
 
oCursor.gotoStart(bExtend)  'go to the start of the text range
 
oCursor.gotoStart(bExtend)  'go to the start of the text range
 
oCursor.gotoEnd(bExtend)    'go to the end of the text range
 
oCursor.gotoEnd(bExtend)    'go to the end of the text range
oCursor.gotoRange(oVC, false)'go to the same range as the view cursor (error if oVC not in same text range
+
oCursor.gotoRange(oVC, false)'go to the same range as the view cursor (error if oVC not in same text range)
 
   
 
   
 
oCursor.gotoNextWord(bExtend)
 
oCursor.gotoNextWord(bExtend)
Line 53: Line 53:
 
</source>
 
</source>
  
= See also =
+
== See also ==
 
* [[Programming_OooWriter |Programming OOoWriter in C++]]
 
* [[Programming_OooWriter |Programming OOoWriter in C++]]
 
* [[Extensions_development_basic| Using OOoBasic]]
 
* [[Extensions_development_basic| Using OOoBasic]]

Revision as of 09:15, 14 March 2009

A text cursor can only move within the text range of the text object in which it was created. Thus a text cursor that is created for the main document's text range can not move inside a table.

Creating a text cursor

A common thing to do is to create a text cursor which has the same location as the view cursor, as follows:

oVC = thisComponent.getCurrentController.getViewCursor
oCursor = oVC.getText.createTextCursorByRange(oVC)

If you want a cursor for the text in the main document:

oCursor = thisComponent.getText.createTextCursor
'or
oText = thisComponent.getText
oCursor = oText.createTextCursorByRange(oText.getStart)

Start and End of a text cursor

A cursor has the methods getStart, and getEnd. Which is the Start end and which is the End end of the selection is determined by the direction in which the selection was made.

However, the methods collapseToStart and collapseToEnd are relative to the start and end of the document.

Moving a cursor

A cursor has a number of gotoXXX where XXX is some relative location. In each case there is always a boolean parameter for "extending the selection". When this parameter is set to true the selection is extended from the current selection to the new location.

There is no method for adding to the selection. While the view cursor can have more than one selection, text cursors only have one selection.

The gotoXXX methods return true if they were able to execute the movement, false otherwise.

bExtend = false
oCursor.goLeft(5, bExtend)   'go left 5 characters
oCursor.goRight(10, bExtend) 'go right 10 characters
oCursor.gotoStart(bExtend)   'go to the start of the text range
oCursor.gotoEnd(bExtend)     'go to the end of the text range
oCursor.gotoRange(oVC, false)'go to the same range as the view cursor (error if oVC not in same text range)
 
oCursor.gotoNextWord(bExtend)
oCursor.gotoPreviousWord(bExtend)
oCursor.gotoEndOfWord(bExtend)
oCursor.gotoStartOfWord(bExtend)
 
oCursor.gotoNextSentence(bExtend)
oCursor.gotoPreviousSentence(bExtend)
oCursor.gotoStartOfSentence(bExtend)
oCursor.gotoEndOfSentence(bExtend)
 
oCursor.gotoStartOfParagraph(bExtend)
oCursor.gotoEndOfParagraph(bExtend)
oCursor.gotoNextParagraph(bExtend)
oCursor.gotoPreviousParagraph(bExtend)

See also

Personal tools