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

Creating a macro - Writing a Script - Using the API

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

Postby irocoombes » Sat Sep 03, 2016 8:23 pm

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   Expand viewCollapse view
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 :)
Last edited by irocoombes on Tue Sep 06, 2016 4:22 pm, edited 1 time in total.
OpenOffice 4.1.2 on Windows 10
irocoombes
 
Posts: 3
Joined: Sat Sep 03, 2016 7:36 pm

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

Postby UnklDonald418 » Tue Sep 06, 2016 5:11 am

As you noted ViewData is read only. ViewSettings does have writable properties such as

Code: Select all   Expand viewCollapse view
oCont =  thisComponent.currentController
oCont.ViewSettings.setPropertyValue("ZoomType",com.sun.star.view.DocumentZoomType.ENTIRE_PAGE)

http://www.openoffice.org/api/docs/common/ref/com/sun/star/view/DocumentZoomType.html#Constants
http://www.openoffice.org/api/docs/common/ref/com/sun/star/text/ViewSettings.html

or scrolling can be done with something like:
Code: Select all   Expand viewCollapse view
oCont.getViewCursor().screenDown()

https://wiki.openoffice.org/wiki/Documentation/DevGuide/Text/TextViewCursor
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.6 & LibreOffice 6.1.5.2 - Windows 10 Professional
UnklDonald418
Volunteer
 
Posts: 1240
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

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

Postby irocoombes » Tue Sep 06, 2016 2:08 pm

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.
OpenOffice 4.1.2 on Windows 10
irocoombes
 
Posts: 3
Joined: Sat Sep 03, 2016 7:36 pm

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

Postby Villeroy » Tue Sep 06, 2016 3:27 pm

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.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, no OpenOffice, LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 27301
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

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

Postby irocoombes » Tue Sep 06, 2016 4:21 pm

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   Expand viewCollapse view
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.
OpenOffice 4.1.2 on Windows 10
irocoombes
 
Posts: 3
Joined: Sat Sep 03, 2016 7:36 pm

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

Postby JeJe » Wed Feb 01, 2017 3:16 pm

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   Expand viewCollapse view
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
Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 613
Joined: Wed Mar 09, 2016 2:40 pm

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

Postby JeJe » Wed Feb 01, 2017 3:52 pm

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
Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 613
Joined: Wed Mar 09, 2016 2:40 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: DavidHMcCracken and 8 guests