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

From Apache OpenOffice Wiki
Jump to: navigation, search
m (Updating the status bar with the insertion points current location)
m (Moving the view cursor: code> -> source> (works much better))
Line 10: Line 10:
  
 
=Moving the view cursor=
 
=Moving the view cursor=
<code>[oobas]
+
<source lang="oobas">
 
bExtend = false
 
bExtend = false
 
oVC.goLeft(5, bExtend)  'go left 5 characters
 
oVC.goLeft(5, bExtend)  'go left 5 characters
Line 31: Line 31:
 
oVC.jumpToPreviousPage
 
oVC.jumpToPreviousPage
 
oVC.jumpToStartOfPage
 
oVC.jumpToStartOfPage
</code>
+
</source>
  
 
=Using view cursor to find location on page=
 
=Using view cursor to find location on page=

Revision as of 23:29, 23 September 2009

View Cursor in a Text document

Accessing the view cursor

A view cursor in text document is referenced as follows:

[oobas] 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.

[oobas] 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:

[oobas] 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:

[oobas] 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