Page 1 of 1

[Solved] How do you change Writer's scroll position by code?

Posted: Sat Sep 03, 2016 8:23 pm
by irocoombes
Hey everyone.

A long while ago I wrote a macro in Basic to automatically adjust the zoom and scrollbars in Writer so that the current page (or pair of pages) is displayed filling the screen exactly, and when you move the cursor to the next page, say, it snaps the scroll position to display that page instead of half and half. I remember it took a while to get it right and the code was a bit messy, but for the last few years it worked exactly as I wanted.

Recently though I lost the macro during an upgrade, and now for the life of me I can't get a replacement working. I can extract the current position from

Code: Select all

thisComponent.currentController.ViewData
and I can calculate the new position easily, but plugging that value back in has no effect. I'm almost sure that the first time round I just altered the ViewData object directly, but it's now telling me it's read-only so either it's changed or I'm misremembering. The only other way I've found is a method called RestoreViewData, but that seems to have no effect, even when I test it separately.

Am I missing something obvious? I was hoping there'd be a more explicit option to adjust the scroll without using ViewData at all, because it seemed like a bit of a hack the first time round too, but I'll take it if it's the only way :)

Re: How do you change Writer's scroll position by code?

Posted: Tue Sep 06, 2016 5:11 am
by UnklDonald418
As you noted ViewData is read only. ViewSettings does have writable properties such as

Code: Select all

oCont =  thisComponent.currentController
oCont.ViewSettings.setPropertyValue("ZoomType",com.sun.star.view.DocumentZoomType.ENTIRE_PAGE)
http://www.openoffice.org/api/docs/comm ... #Constants
http://www.openoffice.org/api/docs/comm ... tings.html

or scrolling can be done with something like:

Code: Select all

oCont.getViewCursor().screenDown()
https://wiki.openoffice.org/wiki/Docume ... ViewCursor

Re: How do you change Writer's scroll position by code?

Posted: Tue Sep 06, 2016 2:08 pm
by irocoombes
Thanks for the reply.

I'm already using ViewSettings to control the zoom level, but that wasn't really a problem anyway because I only need to set that once and doing it manually isn't a big issue :)

The scrolling is another matter because obviously that happens many many times in a session. Unfortunately as far as I can tell ScreenDown does the same as PageDown, with the view scrolling to keep the cursor roughly in the middle of the screen, which is not what I want.

To give an example, if you start at the top of page 1 (in book view) and press Page Down, the cursor moves to the top of page 3, and what you see on screen is the bottom half of page 1, and the top halves of pages 2 and 3. ScreenDown gives the exact same result. What makes it especially frustrating is that if you then move the cursor to the bottom of page 2, you get half of 2 and 3, and half of 4 and 5, only to jump back up when you move onto page 3. What I want it to do is scroll an extra half-page down to show each pair of pages in their entirety.

Is there no way of setting the scroll position to a specific value, independent of the cursor? It would be easy then. I know because I did it in 3.x with ViewData. I'd rather not have to downgrade to get this feature back.

Re: How do you change Writer's scroll position by code?

Posted: Tue Sep 06, 2016 3:27 pm
by Villeroy
irocoombes wrote:I know because I did it in 3.x with ViewData. I'd rather not have to downgrade to get this feature back.
Just run that macro that you used to run. I bet it still works with any version of AOO and LO.

Re: How do you change Writer's scroll position by code?

Posted: Tue Sep 06, 2016 4:21 pm
by irocoombes
I would try that but sadly I lost the code during the update. I've been trying to rewrite it from memory but it was 5 years ago...

However, I was looking at the ViewSettings call UnklDonald418 suggested and it turns out that method resets the view as well as the zoom, so I can just call that whenever the page changes. It's not quite as precise as the ViewData way so I can't adjust for margins, but the code certainly looks much neater now :)

In case anyone's wondering:

Code: Select all

Global KeyPressHandler
Global CurrentPage as Integer

Sub SetupKeyHandler

	oCont = thisComponent.currentController
	KeyPressHandler = CreateUnoListener("KeyHandler_","com.sun.star.awt.XKeyHandler")
	oCont.addKeyHandler(KeyPressHandler)

End Sub

Function KeyHandler_keyReleased(oKeyEvent as new com.sun.star.awt.KeyHandler) as Boolean

	oVC = ThisComponent.CurrentController.ViewCursor
	
	If oVC.Page <> CurrentPage Then
		
		oCont =  thisComponent.currentController
		oCont.ViewSettings.setPropertyValue("ZoomType",com.sun.star.view.DocumentZoomType.ENTIRE_PAGE)

		CurrentPage = oVC.page

	End If
		
	KeyHandler_keyReleased = False

End Function
Just call SetupKeyHandler on first load.

Thanks for the help, guys.

Re: [Solved] How do you change Writer's scroll position by c

Posted: Wed Feb 01, 2017 3:16 pm
by JeJe
This is an old thread but I've been looking at this. Looking at the getViewdata numbers
incrementing two of them works. On my system (Windows 8, latest OO) it only does anything once there's been an edit and save. A negative increment before that will cause a crash. Problems when a new window is opened too. But here is the viewdata way. 192 seems to work as a number for a line increment - may be specific to my system/document, I don't know.

Code: Select all

sub scrollincrement(increment as long) 'TOO BUGGY NEEDS A SAVE TO WORK. Doesn't work or crash before that
	dim vdata
	dim vdParts,a
	vdata = thisComponent.getCurrentController.getViewdata
	a = 0
	vdParts = Split(vdata(a), ";")
	vdParts(4)= clng(vdParts(4))+increment
	vdParts(6)= clng(vdParts(6))+increment
	vdata(a) = join(vdParts, ";")
	thisComponent.getCurrentController.restoreViewdata(vdata)
end sub

Re: [Solved] How do you change Writer's scroll position by c

Posted: Wed Feb 01, 2017 3:52 pm
by JeJe
Looking some more. There's the thisComponent.getCurrentController.getViewdata/restoreViewdata here (used above)

https://www.openoffice.org/api/docs/com ... oller.html

But also this thisComponent.getViewdata / setViewdata too.

https://www.openoffice.org/api/docs/com ... plier.html