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

From Apache OpenOffice Wiki
Jump to: navigation, search
m
m
 
Line 5: Line 5:
 
A view cursor in text document is referenced as follows:  
 
A view cursor in text document is referenced as follows:  
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
oVC = thisComponent.getCurrentController.getViewCursor
 
oVC = thisComponent.getCurrentController.getViewCursor
</source>
+
</syntaxhighlight>
 
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.  
 
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 ==
 
== Moving the view cursor ==
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
bExtend = false
 
bExtend = false
 
oVC.goLeft(5, bExtend)  'go left 5 characters
 
oVC.goLeft(5, bExtend)  'go left 5 characters
Line 32: Line 32:
 
oVC.jumpToPreviousPage
 
oVC.jumpToPreviousPage
 
oVC.jumpToStartOfPage
 
oVC.jumpToStartOfPage
</source>
+
</syntaxhighlight>
  
 
== Using view cursor to find location on page ==
 
== 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.  
 
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.  
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
function fnPosn(oDoc)
 
function fnPosn(oDoc)
 
set mCurSelection = oDoc.currentSelection ' Record current selection
 
set mCurSelection = oDoc.currentSelection ' Record current selection
Line 59: Line 59:
 
fnPosn = "(" & nx & ", " & nY & ")"
 
fnPosn = "(" & nx & ", " & nY & ")"
 
end function
 
end function
</source>
+
</syntaxhighlight>
 
To call this code:  
 
To call this code:  
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
msgbox fnPosn(thisComponent)
 
msgbox fnPosn(thisComponent)
</source>
+
</syntaxhighlight>
  
  
Line 71: Line 71:
 
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:  
 
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:  
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
global Iannz_TSB_Controller, Iannz_TSB_MouseClickHandler, Iannz_TSB_KeyHandler
 
global Iannz_TSB_Controller, Iannz_TSB_MouseClickHandler, Iannz_TSB_KeyHandler
 
   
 
   
Line 132: Line 132:
 
end if
 
end if
 
end sub
 
end sub
</source>
+
</syntaxhighlight>
  
 
== See also ==
 
== See also ==

Latest revision as of 13:49, 5 March 2021

Writer Icon.png

Writer Project

Please view the guidelines
before contributing.

Popular Subcategories:

Extension:DynamicPageList (DPL), version 2.3.0 : Warning: No results.

Internal Documentation:

Extension:DynamicPageList (DPL), version 2.3.0 : Warning: No results.

API Documentation:

Ongoing Efforts:

Extension:DynamicPageList (DPL), version 2.3.0 : Warning: No results.

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