Restore Writer document scroll position

Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This section is not for asking questions about writing your own macros.
Post Reply
JeJe
Volunteer
Posts: 2784
Joined: Wed Mar 09, 2016 2:40 pm

Restore Writer document scroll position

Post by JeJe »

Saves a Writer document's scroll position to a file in the user's configuration folder when closed.
Restores that position when the document is reopened.
No changes are made to the document and the scroll position is restored even if the document was previously opened just to read.

Only lighly tested on OO with one document's view created and going to be closed events set to the first two subs.
That could be set for those two application events instead.

- expect bugs, there usually are...

Code: Select all



Sub Doc_Events_ViewCreated(ev)
	on error goto hr
	with  ev.source
		if .ImplementationName = "SwXTextDocument" then
			LoadScrollPos
		end if
	end with
	hr:

end sub
sub Doc_Events_ViewGoingToBeClosed(ev)

	on error goto hr
	with  ev.source
		if .ImplementationName = "SwXTextDocument" then
			SaveScrollPos
		end if
	end with
	hr:


end sub


Sub SaveScrollPos

	dim st as string,pth as string

	st= getVScrollvaluecontroller(thiscomponent.currentcontroller,0)
	pth = GetUserConfigFilePath("DocPositions",thiscomponent.title & ".dat")
	setFileString pth,st

End Sub

sub LoadScrollPos()
	dim st as string,pth as string
	pth = GetUserConfigFilePath("DocPositions",thiscomponent.title & ".dat")
	st=GetFileString (pth)
	if st<>"" then
		setVScrollvaluecontroller(thiscomponent.currentcontroller,val(st))
	end if
end sub




Function GetUserConfigFilePath(foldername, filename) as string
	Dim oPathSettings , t as string
	oPathSettings = CreateUnoService( "com.sun.star.util.PathSettings" )
	t= oPathSettings.UserConfig
	t=t & "/"
	if foldername <>"" then t = t & foldername
	if filename <> "" then t = t & "/"
	t=t & filename
	GetUserConfigFilePath= ConvertFromURL(t)
End function



function GetFileString(pth as string) as string
	dim i
	Dim sLine As String
	i = Freefile
	'msgbox pth
	if dir(pth) <> "" then
		'if filelen(pth) <>0 then
		'msgbox filelen(pth)
		Open pth For Input As #i
		while not eof(i)
			Line Input #i, sLine
			wend
			close #i
			GetFileString=sline
		end if
	end  function

sub SetFileString(pth as string,st as string)
	dim i

	i = Freefile
	Open pth For Output As #i
	Print #i, st
	close #i
end sub



function setVScrollvaluecontroller(cc,svalue) 'set scrollbar value which sets the scroll position
	dim found as boolean
	found = false
	comp =cc.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.setcurrentvalue ( svalue)
				exit for
			end if
		end with
	next
end function



function getVScrollvaluecontroller(cc, vtype) 'gets the scrollbar position or scrollbar maximum value
	dim found as boolean
	found = false
	comp =cc.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
				select case vtype
				case 0
					getVScrollvaluecontroller= .getAccessibleContext.currentvalue
				case 1
					getVScrollvaluecontroller= .getAccessibleContext.maximumvalue
				case 2
					getVScrollvaluecontroller= .getAccessibleContext.minimumvalue
				end select
				exit for
			end if
		end with
	next
end function




Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply