Writer/API/View cursor

From Apache OpenOffice Wiki
Jump to: navigation, search

Writer Icon.png

Writer Project

Please view the guidelines
before contributing.

Popular Subcategories:

Internal Documentation:

API Documentation:

Ongoing Efforts:

Sw.OpenOffice.org

Accessing the view cursor

A view cursor in text document is referenced as follows:

oVC = thisComponent.getCurrentController.getViewCursor

It is the only way to move within the page layout. For example to find the start of a line, the top of a page.

Moving the view cursor

bExtend = false
oVC.goLeft(5, bExtend)   'go left 5 characters
oVC.goRight(10, bExtend) 'go right 10 characters
oVC.gotoStart(bExtend)   'go to the start of the text range
oVC.gotoEnd(bExtend)     'go to the end of the text range
oVC.gotoRange(oCursor, false) 'view cursor can go anywhere
 
oVC.goDown(2, bExtend)
oVC.goLeft(3, bExtend)
oVC.goUp(1, bExtend) 
oVC.gotoEndOfLine(bExtend)
oVC.gotoStartOfLine(bExtend)
 
oVC.jumpToEndOfPage
oVC.jumpToFirstPage
oVC.jumpToLastPage
oVC.jumpToNextPage
oVC.jumpToPage(2, false)
oVC.jumpToPreviousPage
oVC.jumpToStartOfPage

Using view cursor to find location on page

The following routine returns the current insertion points location on a page in terms of lines and characters. The number of characters is meaningless if the insertion point is in a table.

function fnPosn(oDoc)
set mCurSelection = oDoc.currentSelection ' Record current selection
'Locking controller prevents flicker. But under ALL circumstances must be unlocked
on local error goto finished:
oDoc.lockControllers
oVC = oDoc.getCurrentController.getViewCursor
oVC.collapseToEnd  'Move view Cursor
oVC.gotoStartofLine(true)
nX = len(oVC.string)  'How many characters from the start of the line
nY = 0
'How many lines from top of page
nPage = oVC.getPage
while oVC.goUp(1,false) and oVC.getPage = nPage
   nY = nY + 1
wend
thisComponent.currentController.select(mCurSelection) 'Restore current selection
finished:
on error goto 0
oDoc.unlockControllers
fnPosn = "(" & nx & ", " & nY & ")"
end function

To call this code:

msgbox fnPosn(thisComponent)


Updating the status bar with the insertion points current location

This code could be called whenever a key is pressed or the mouse clicked so that the status bar is updated with the current insertion point location. As follows:

global Iannz_TSB_Controller, Iannz_TSB_MouseClickHandler, Iannz_TSB_KeyHandler
 
sub subSetupCustomStatusBar
Iannz_TSB_Controller = thisComponent.currentController
Iannz_TSB_MouseClickHandler = CreateUnoListener("MouseClickHandler_","com.sun.star.awt.XMouseClickHandler")
Iannz_TSB_Controller.addMouseClickHandler(Iannz_TSB_MouseClickHandler)
Iannz_TSB_KeyHandler = CreateUnoListener("KeyHandler_","com.sun.star.awt.XKeyHandler")
Iannz_TSB_Controller.addKeyHandler(Iannz_TSB_KeyHandler)
end sub
 
 
sub subRemoveCustomStatusBar
if not isNull(Iannz_TSB_Controller) and not isEmpty(Iannz_TSB_Controller) then
        oStatus = Iannz_TSB_Controller.Frame.createStatusIndicator
        oStatus.end
        Iannz_TSB_Controller.removeMouseClickHandler(Iannz_TSB_MouseClickHandler)
        Iannz_TSB_Controller.removeKeyHandler(Iannz_TSB_KeyHandler)
end if
end sub
 
 
Sub MouseClickHandler_MousePressed(Event As Object)
Iannz_TSB_Controller.addMouseClickHandler(Iannz_TSB_MouseClickHandler)
end sub
 
 
sub MouseClickHandler_mouseReleased
subChangeStatusBar
Iannz_TSB_Controller.addMouseClickHandler(Iannz_TSB_MouseClickHandler)
end sub
 
 
sub MouseClickHandler_disposing
end sub
 
 
function KeyHandler_KeyPressed
KeyHandler_KeyPressed = false
end function
 
 
function KeyHandler_KeyReleased
subChangeStatusBar
KeyHandler_KeyReleased = false
end function
 
 
sub KeyHandler_disposing
end sub
 
 
sub subChangeStatusBar
oDoc = thisComponent
oVC = Iannz_TSB_Controller.viewCursor
oStatus = Iannz_TSB_Controller.Frame.createStatusIndicator
oStatus.end
if oVC.isCollapsed and isEmpty(oVC.getPropertyValue("Cell")) then
        oStatus.start(fnPosn(thisComponent),100)
end if
end sub

See also

Personal tools