Difference between revisions of "Writer/API/TextRange"
Linda octa (talk | contribs) |
|||
Line 12: | Line 12: | ||
The following routine inserts text at the view cursor. | The following routine inserts text at the view cursor. | ||
− | < | + | <source lang="oobas"> |
sub subInsertString(oDoc, sString, bReplace) | sub subInsertString(oDoc, sString, bReplace) | ||
oVC = oDoc.getCurrentController.getViewCursor | oVC = oDoc.getCurrentController.getViewCursor | ||
Line 18: | Line 18: | ||
oText.insertString(oVC, sString, bReplace) | oText.insertString(oVC, sString, bReplace) | ||
end sub | end sub | ||
− | </ | + | </source> |
The following example inserts the text "Some sample text" to the left of the current selection, leaving the orginal selection still selected. | The following example inserts the text "Some sample text" to the left of the current selection, leaving the orginal selection still selected. | ||
− | < | + | <source lang="oobas"> |
subInsertString(thisComponent, "Some sample text", false) | subInsertString(thisComponent, "Some sample text", false) | ||
− | </ | + | </source> |
=Inserting control codes= | =Inserting control codes= | ||
− | < | + | <source lang="oobas"> |
sub subInsertParagraphMarker(oDoc) | sub subInsertParagraphMarker(oDoc) | ||
oVC = oDoc.getCurrentController.getViewCursor | oVC = oDoc.getCurrentController.getViewCursor | ||
Line 32: | Line 32: | ||
oText.insertControlCharacter(oVC, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False) | oText.insertControlCharacter(oVC, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False) | ||
end sub | end sub | ||
− | </ | + | </source> |
The control codes are: | The control codes are: | ||
Line 73: | Line 73: | ||
It is possible to create an enumeration of paragraphs (and text tables) from an object with service [[ TextRange]]. | It is possible to create an enumeration of paragraphs (and text tables) from an object with service [[ TextRange]]. | ||
− | < | + | <source lang="oobas"> |
' Create enumeration object | ' Create enumeration object | ||
oTextElementEnum = thisComponent.getText.createEnumeration | oTextElementEnum = thisComponent.getText.createEnumeration | ||
Line 88: | Line 88: | ||
end if | end if | ||
wend | wend | ||
− | </ | + | </source> |
=Text Portions= | =Text Portions= | ||
Line 95: | Line 95: | ||
The following function counts the number of portions in a paragraph. | The following function counts the number of portions in a paragraph. | ||
− | < | + | <source lang="oobas"> |
function fnNoPortions(oPara) | function fnNoPortions(oPara) | ||
Line 106: | Line 106: | ||
fnNoPortions = i | fnNoPortions = i | ||
end function | end function | ||
− | </ | + | </source> |
An example of calling this function is: | An example of calling this function is: | ||
− | < | + | <source lang="oobas"> |
thisComponent.getCurrentSelection.getByIndex(i).createEnumeration | thisComponent.getCurrentSelection.getByIndex(i).createEnumeration | ||
while oTextElementEnum.hasMoreElements() | while oTextElementEnum.hasMoreElements() | ||
Line 120: | Line 120: | ||
end if | end if | ||
wend | wend | ||
− | </ | + | </source> |
For more routines see [[ Working with properties]]. | For more routines see [[ Working with properties]]. | ||
[[Category:Writer]] | [[Category:Writer]] | ||
[[Category:Basic:Tutorials]] | [[Category:Basic:Tutorials]] |
Revision as of 17:45, 10 July 2008
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.
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.
subInsertString(thisComponent, "Some sample text", false)
Inserting control codes
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.
' 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.
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:
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.