Writer/API/TextRange

From Apache OpenOffice Wiki
< Writer
Revision as of 13:26, 16 February 2007 by Fpe (Talk | contribs)

Jump to: navigation, search

Working with Text Ranges

The Current selection, the View cursor and not visible Text cursors all provide the TextRange service.

As does thisComponent.text, and to make life more incestous text portion enumerations.

It is this service which provides the methods and properties for changing the text.

Inserting text

Inserting text is accomplished with the method insertString. The parameters to this function are the location in the text range to insert the string at, the string itself, and whether the text at the specified location gets replaced.

The following routine inserts text at the view cursor.

[oobas] sub subInsertString(oDoc, sString, bReplace) oVC = oDoc.getCurrentController.getViewCursor oText = oVC.text oText.insertString(oVC, sString, bReplace) end sub The following example inserts the text "Some sample text" to the left of the current selection, leaving the orginal selection still selected.

[oobas] subInsertString(thisComponent, "Some sample text", false)

Inserting control codes

[oobas] sub subInsertParagraphMarker(oDoc) oVC = oDoc.getCurrentController.getViewCursor oText = oVC.text oText.insertControlCharacter(oVC, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False) end sub The control codes are:

0 com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK This control character starts a new paragraph.
1 com.sun.star.text.ControlCharacter.LINE_BREAK This control character starts a new line in a paragraph.
2 com.sun.star.text.ControlCharacter.HARD_HYPHEN This control character equals a dash but prevents this position from being hyphenated.
3 com.sun.star.text.ControlCharacter.SOFT_HYPHEN This control character defines a special position as a hyphenation point. If a word containing a soft hyphen must be split at the end of a line, then this position is preferred.
4 com.sun.star.text.ControlCharacter.HARD_SPACE This control character is used to link two words and prevents this concatenation from being hyphenated. It is printed as a space.
5 com.sun.star.text.ControlCharacter.APPEND_PARAGRAPH This control character appends a new paragraph at the end of the current paragraph.


Although you can insert control codes by inserting the equivalent string (e.g. chr(10) for LINE_BREAK), it is safer to use insertControlCharacter as the screen gets updated properly.

Changing properties

To change the properties of a piece of text, select the text e.g. with a cursor, and then use the setPropertyValue method.

See working with styles for an example.

Paragraphs

It is possible to create an enumeration of paragraphs (and text tables) from an object with service TextRange.

[oobas] ' Create enumeration object oTextElementEnum = thisComponent.getText.createEnumeration 'or thisComponent.getCurrentSelection.getByIndex(i).createEnumeration

' loop over all text elements while oTextElementEnum.hasMoreElements()

       oTextElement = oTextElementEnum.nextElement
       if oTextElement.supportsService("com.sun.star.text.TextTable") then
               MsgBox "The current block contains a table."
       end if
       if oTextElement.supportsService("com.sun.star.text.Paragraph") then
               MsgBox "The current block contains a paragraph."
       end if

wend

Text Portions

A paragraph consists of text portions. A text portions is a block of text within a paragraph with identical formatting.

The following function counts the number of portions in a paragraph.

[oobas] function fnNoPortions(oPara)

oPortionEnum = oPara.createEnumeration i = 0 while oPortionEnum.hasMoreElements

       i = i + 1
       oPortionEnum.nextElement

wend fnNoPortions = i end function An example of calling this function is:

[oobas] thisComponent.getCurrentSelection.getByIndex(i).createEnumeration while oTextElementEnum.hasMoreElements()

       oTextElement = oTextElementEnum.nextElement
       if oTextElement.supportsService("com.sun.star.text.TextTable") then
               MsgBox "The current block contains a table."
       end if
       if oTextElement.supportsService("com.sun.star.text.Paragraph") then
               MsgBox "The paragraph contains " & fnNoPortions(oTextElement) & "text portions"
       end if

wend For more routines see Working with properties.

Personal tools