[Solved] [Writer] Scroll to have cursor in middle of screen

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
ptbento
Posts: 45
Joined: Fri Jan 18, 2013 9:33 pm

[Solved] [Writer] Scroll to have cursor in middle of screen

Post by ptbento »

I would like to build a macro code to scroll up or down so that cursor stays approximately in the middle of the screen in vertical terms. I am not an expert and I do not see how this could be done by recording an action, as some variable (screen height or similar) must somehow be halved.

Thanks in advance! :o
Last edited by Hagar Delest on Mon Jun 29, 2020 8:44 pm, edited 1 time in total.
Reason: tagged solved.
OpenOffice 3.4.1 on Windowx XP
JeJe
Volunteer
Posts: 3119
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro to scroll to have cursor in the middle of screen

Post by JeJe »

Do you mean:

a) you want to press a shortcut key or menu item or otherwise run a macro that scrolls the window so the cursor is in the middle of the screen?

Or

b) you want a macro that continually keeps the cursor in the middle of the screen all the time as you work?
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
ptbento
Posts: 45
Joined: Fri Jan 18, 2013 9:33 pm

Re: Macro to scroll to have cursor in the middle of screen

Post by ptbento »

Thank you for your reply! I was thinking about a), but it seems that that b) could be useful too, including for non advanced users. I would be happy with just a) or b), though.
OpenOffice 3.4.1 on Windowx XP
JeJe
Volunteer
Posts: 3119
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro to scroll to have cursor in the middle of screen

Post by JeJe »

Here are some macros I wrote a while ago to scroll up or down a line or screen which you can assign to shortcut keys. You'd have to keep the line up or line down shortcut key pressed till you have the scroll position where you want it.

Code: Select all

sub doLineUp() 'LINE UP
	doscrollEvent 0
end sub

sub doLineDown() 'LINE DOWN
	doscrollEvent 1
end sub

sub doScreenUp() 'SCREEN UP
	doscrollEvent 2
end sub

sub doScreenDown() 'SCREEN DOWN
	doscrollEvent 3
end sub

sub doscrollEvent(i)
	dim found as boolean
	found = false
	comp =thiscomponent.currentcontroller.frame.getcomponentwindow
	compchild=comp.getAccessibleContext.getAccessibleChild(0)
	on error resume next
	for j = 0 to compchild.getAccessibleContext.getAccessibleChildcount -1
		with compchild.getAccessibleContext.getAccessibleChild(j)
			if .getAccessibleContext.getAccessibleName = "Vertical scroll bar" then found = true
			if found then
				.getAccessibleContext.doAccessibleAction(i)
				exit for
			end if
		end with
	next
end sub

IF the view cursor isn't on screen and you want it to be that is easy

Code: Select all

sub selectViewCursor
vc = thiscomponent.currentcontroller.viewcursor
thiscomponent.currentcontroller.select(vc)
end sub

A macro that scrolls so the viewcursor is in the middle in one go is much more complicated, but if you explain why you want that someone may be able to help you find another way to achieve whatever that goal is.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
ptbento
Posts: 45
Joined: Fri Jan 18, 2013 9:33 pm

Re: [Writer] Scroll to have cursor in the middle of screen

Post by ptbento »

Thank you very much for your reply, Jeje. I believe your first set of macros will be useful when I find a way to "measure" distance in lines between the cursor position line and the middle of screen. It would be useful to go into middle of screen because I work on screen with a sequence of text as follows:
example text [type A text]
example text [type B text]
example text [type C text]
----
example text1 [type A text]
example text1 [type B text]
etc
After applying a macro to process a line, the macro moves cursor from line 1 in the example (type A) to line 5 (type A again) so that it may be at the bottom of screen.
Sometimes it gets out of the screen, so your last macro will also be useful.
OpenOffice 3.4.1 on Windowx XP
JeJe
Volunteer
Posts: 3119
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Writer] Scroll to have cursor in the middle of screen

Post by JeJe »

Would having two windows instead of one and making the cursor in the second window go to the second type A text be a solution?
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
ptbento
Posts: 45
Joined: Fri Jan 18, 2013 9:33 pm

Re: [Writer] Scroll to have cursor in the middle of screen

Post by ptbento »

Thanks again!
Yes, if the second window would show up automatically and fully opened. Then the original need would come up again - perhaps on the 2nd iteration the cursor would move back to (the centre of) the first window? Otherwise too much windows would need to be opened and it would become impossible memory wise, I guess.
OpenOffice 3.4.1 on Windowx XP
JeJe
Volunteer
Posts: 3119
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Writer] Scroll to have cursor in the middle of screen

Post by JeJe »

I'm not following what you're trying to do. If you want two parts of the document visible at once they that's what the New Window feature is for. I wasn't suggesting keeping opening and closing windows but keeping two open - but like I say, I'm not really following you.

As for keeping the view cursor center screen - 'I don't have the exact answer
'this roughly works on mine with the zoom at 100%. You can play around with it to try and improve it.

Code: Select all

'I don't have the exact answer 
'this roughly works on mine with the zoom at 100% 
'you can play around with one or other someothervalue to try and get 
'something better/more general in these two lines
'	svalue = (y+someothervalue)/ (10/(2.83465 * 2)) 
'	svalue = svalue - someothervalue
	
sub VCToCentreScreen
	doc = thiscomponent
	vc =doc.currentcontroller.viewcursor
	y= vc.position.y 'position is in page/metric coordinates
	'svalue = (y+someothervalue )/ (10/(2.83465 * 2)) 'converts value to scrollbar uses half points 
	svalue = (y+0)/ (10/(2.83465 * 2)) 'converts value to scrollbar uses half points 
	comp =doc.currentcontroller.frame.getcomponentwindow
	compchild=comp.getAccessibleContext.getAccessibleChild(0)
	for j = 0 to compchild.getAccessibleContext.getAccessibleChildcount -1 'find the scrollbar
		with compchild.getAccessibleContext.getAccessibleChild(j)
			if .getAccessibleContext.getAccessibleName = "Vertical scroll bar" then
			'.getblockincrement is usually the amount scrolled when screen down
			'subtract a bit for that /2 doesn't work on my machine /5 roughly does
				'svalue = svalue - someothervalue
				svalue = svalue - .getblockincrement/5 
				.getAccessibleContext.setcurrentvalue clng(svalue)
				exit for
			end if
		end with
	next
end sub

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
ptbento
Posts: 45
Joined: Fri Jan 18, 2013 9:33 pm

Re: [Writer] Scroll to have cursor in the middle of screen

Post by ptbento »

Thank you very much again, JeJe (and apologies for the late reply). I have used your code and it was of great help, it works wonderfully in my pc.
OpenOffice 3.4.1 on Windowx XP
Post Reply