[Solved] Move scrollbar to show text using code

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Jimmy2768
Posts: 19
Joined: Sat Jan 22, 2022 2:15 pm

[Solved] Move scrollbar to show text using code

Post by Jimmy2768 »

Hi to all.

I have made one dialog. In one page of dialog i want to show some long text.

I want to scroll down or up to read the text using a ScrollBar and with Basic code.

I tried to use TextFiled to do that but when the control gets focus a cursor appears. Also i don't see any way to move the vertical scroll bar with code.

I tried to use Label but there is no scrollbar on that control.

Is there a better way to do that?

Thanks.
Last edited by Jimmy2768 on Sat Jan 22, 2022 8:16 pm, edited 1 time in total.
Open Office 4.1.9
MX Linux 19.4 - Windows 7
JeJe
Volunteer
Posts: 2780
Joined: Wed Mar 09, 2016 2:40 pm

Re: Move scrollbar to show text using code

Post by JeJe »

If you set the textfield to read only then the cursor will appear but the contents can't be edited.

You can use a listbox and set that to read only.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2780
Joined: Wed Mar 09, 2016 2:40 pm

Re: Move scrollbar to show text using code

Post by JeJe »

If you use a listbox you can scroll by using makevisible

Code: Select all

dlg.getcontrol("ListBox1").makevisible 8 'ensures item 8 is visible
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
F3K Total
Volunteer
Posts: 1038
Joined: Fri Dec 16, 2011 8:20 pm

Re: Move scrollbar to show text using code

Post by F3K Total »

Find attached a sample file using this code:

Code: Select all

global oDialog1 as Object
global otxtVerticalScrollbar as Object

Sub Start_Textfield

    DialogLibraries.loadLibrary("Standard")
    oDialog1 = CreateUnoDialog(DialogLibraries.Standard.Dialog1)
    otxtVerticalScrollbar = oDialog1.getcontrol("txtVerticalScrollbar")
    otxtVerticalScrollbar.Text = "Hi to all. I have made one dialog. In one page of dialog i want to show some long text."_
    & "I want to scroll down or up to read the text using a ScrollBar and with Basic code.I tried to use TextFiled to do that but"_
    & "when the control gets focus a cursor appears. Also i don't see any way to move the vertical scroll bar with code."_
    & "I tried to use Label but there is no scrollbar on that control. Is there a better way to do that?"_
    & "Hi to all. I have made one dialog. In one page of dialog i want to show some long text."_
    & "I want to scroll down or up to read the text using a ScrollBar and with Basic code.I tried to use TextFiled to do that but"_
    & "when the control gets focus a cursor appears. Also i don't see any way to move the vertical scroll bar with code."_
    & "I tried to use Label but there is no scrollbar on that control. Is there a better way to do that?"_
    & "I want to scroll down or up to read the text using a ScrollBar and with Basic code.I tried to use TextFiled to do that but"_
    & "when the control gets focus a cursor appears. Also i don't see any way to move the vertical scroll bar with code."_
    & "I tried to use Label but there is no scrollbar on that control. Is there a better way to do that?"_
    & "Hi to all. I have made one dialog. In one page of dialog i want to show some long text."_
    & "I want to scroll down or up to read the text using a ScrollBar and with Basic code.I tried to use TextFiled to do that but"_
    & "when the control gets focus a cursor appears. Also i don't see any way to move the vertical scroll bar with code."_
    & "I tried to use Label but there is no scrollbar on that control. Is there a better way to do that? Thanks."_
    & "I want to scroll down or up to read the text using a ScrollBar and with Basic code.I tried to use TextFiled to do that but"_
    & "when the control gets focus a cursor appears. Also i don't see any way to move the vertical scroll bar with code."_
    & "I tried to use Label but there is no scrollbar on that control. Is there a better way to do that?"_
    & "Hi to all. I have made one dialog. In one page of dialog i want to show some long text."_
    & "I want to scroll down or up to read the text using a ScrollBar and with Basic code.I tried to use TextFiled to do that but"_
    & "when the control gets focus a cursor appears. Also i don't see any way to move the vertical scroll bar with code."_
    & "I tried to use Label but there is no scrollbar on that control. Is there a better way to do that?"_
    & "I want to scroll down or up to read the text using a ScrollBar and with Basic code.I tried to use TextFiled to do that but"_
    & "when the control gets focus a cursor appears. Also i don't see any way to move the vertical scroll bar with code."_
    & "I tried to use Label but there is no scrollbar on that control. Is there a better way to do that?"_
    & "Hi to all. I have made one dialog. In one page of dialog i want to show some long text."_
    & "I want to scroll down or up to read the text using a ScrollBar and with Basic code.I tried to use TextFiled to do that but"_
    & "when the control gets focus a cursor appears. Also i don't see any way to move the vertical scroll bar with code."_
    & "I tried to use Label but there is no scrollbar on that control. Is there a better way to do that? Thanks."_
    & "I want to scroll down or up to read the text using a ScrollBar and with Basic code.I tried to use TextFiled to do that but"_
    & "when the control gets focus a cursor appears. Also i don't see any way to move the vertical scroll bar with code."_
    & "I tried to use Label but there is no scrollbar on that control. Is there a better way to do that?"_
    & "Hi to all. I have made one dialog. In one page of dialog i want to show some long text."_
    & "I want to scroll down or up to read the text using a ScrollBar and with Basic code.I tried to use TextFiled to do that but"_
    & "when the control gets focus a cursor appears. Also i don't see any way to move the vertical scroll bar with code."_
    & "I tried to use Label but there is no scrollbar on that control. Is there a better way to do that?"_
    & "I want to scroll down or up to read the text using a ScrollBar and with Basic code.I tried to use TextFiled to do that but"_
    & "when the control gets focus a cursor appears. Also i don't see any way to move the vertical scroll bar with code."_
    & "I tried to use Label but there is no scrollbar on that control. Is there a better way to do that?"_
    & "Hi to all. I have made one dialog. In one page of dialog i want to show some long text."_
    & "I want to scroll down or up to read the text using a ScrollBar and with Basic code.I tried to use TextFiled to do that but"_
    & "when the control gets focus a cursor appears. Also i don't see any way to move the vertical scroll bar with code."_
    & "I tried to use Label but there is no scrollbar on that control. Is there a better way to do that? Thanks."
    
    oDialog1.execute

End Sub


Sub Scroll (event)
    dim nAction as integer
    nAction = event.Source.Model.tag
    oACC =  otxtVerticalScrollbar.AccessibleContext.getAccessibleChild(1)
    oScrollbar =  oACC.AccessibleContext
    oScrollbar.doAccessibleAction(nAction)
    
    '0 decrementLine
    '1 incrementLine
    '2 decrementBlock
    '3 incrementBlock
    'doAccessibleAction        ( nIndex as long ) 
    'for i =  0 to oScrollbar.getAccessibleActionCount - 1
    'oAAction = oScrollbar.getAccessibleActionDescription(i)
    'xray oAAction
    'next i 
end sub

Attachments
VSCB.ods
(11.92 KiB) Downloaded 163 times
  • MMove 1.0.6
  • Extension for easy, exact positioning of shapes, pictures, controls, frames ...
  • my current system
  • Windows 10 AOO, LOLinux Mint AOO, LO
Jimmy2768
Posts: 19
Joined: Sat Jan 22, 2022 2:15 pm

Re: Move scrollbar to show text using code

Post by Jimmy2768 »

Thanks for suggestions guys.

Especially thank you “F3K Total”. Excellent work with sample attached file to.

Perfect. :D
Open Office 4.1.9
MX Linux 19.4 - Windows 7
JeJe
Volunteer
Posts: 2780
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Move scrollbar to show text using code

Post by JeJe »

Attached is the Listbox version if anyone visiting the thread in future wants it...

I borrowed F3K Total's command buttons. There may be a more efficient way to code the next and previous scroll items.
Attachments
listbox.odt
(13.05 KiB) Downloaded 146 times
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Jimmy2768
Posts: 19
Joined: Sat Jan 22, 2022 2:15 pm

Re: [Solved] Move scrollbar to show text using code

Post by Jimmy2768 »

Thanks JeJe.

Useful and without cursor if every line has predefined short length, else an unwanted horizontal scroll bar appears. :D
Open Office 4.1.9
MX Linux 19.4 - Windows 7
JeJe
Volunteer
Posts: 2780
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Move scrollbar to show text using code

Post by JeJe »

Yeah, you can get round that by reading the lines off a hidden textbox. Borrowing F3K Total's string...

Edit: I didn't do this, but if the textbox is made the same width as the listbox and both have or don't have a vertical scrollbar then they match exactly for me.
Attachments
listbox2.odt
(13.05 KiB) Downloaded 135 times
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Jimmy2768
Posts: 19
Joined: Sat Jan 22, 2022 2:15 pm

Re: [Solved] Move scrollbar to show text using code

Post by Jimmy2768 »

Excellent JeJe :D

Works very well. On version Apache 4.1.9

I tried on different versions of OpenOffice for compatibility issues and found that i doesn't work in versions prior to 4.1.0.4. The Line lba.getaccessiblechild(i).getlocation.y gives wrong big values so in every step it jumps some lines and moving scrollbar upwards doesn't work at all.
Open Office 4.1.9
MX Linux 19.4 - Windows 7
JeJe
Volunteer
Posts: 2780
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Move scrollbar to show text using code

Post by JeJe »

That's interesting - I don't check previous versions for compatibility, just LibreOffice sometimes.

A more direct way to get the first displayed item would be this (but it uses .getlocation.y too so I presume it won't work in earlier versions either)

Code: Select all

case "1","2" 'up,down
lba= lb.accessiblecontext.getaccessiblechild(0).accessiblecontext
cc=lba.getaccessiblechildcount 
with lba.getaccessiblechild(0)
i =cint (-.getlocation.y/.getsize.height)
end with
if nm= "2" then
if i <cc-1 then lb.makevisible i+1
else
if i >0 then lb.makevisible i-1
end if
Do you need a macro that goes to the next or previous item - its on the scrollbar and the up/down arrow keys for the user already? And you can go to whichever chosen line you want using .makevisible (it could be a heading line perhaps).

If so, I'd suggest a label version reading the lines of a textbox into a label, or a label for each line perhaps, together with a scrollbar control... the scrollbar controls of OO and LO aren't compatible with each other though.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
RoryOF
Moderator
Posts: 34613
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [Solved] Move scrollbar to show text using code

Post by RoryOF »

Note that OpenOffice (and I presume LibreOffice) has a very full set of listbox macros under OpenOffice Macros /Tools /Listbox.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
JeJe
Volunteer
Posts: 2780
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Move scrollbar to show text using code

Post by JeJe »

Yet another method of getting the first visible item is this (don't know if will work on earlier versions)

Code: Select all

sub scroll(ev)
dim pt as new  com.sun.star.awt.Point 

dlg = ev.source.getcontext
lb = dlg.getcontrol("ListBox1")
nm =right(ev.source.model.name,1)

select case nm
case "1","2" 'up,down
cc= lb.itemcount
pt.x= 5
pt.y = 5
ac= lb.accessiblecontext.getaccessiblechild(0).accessiblecontext
j = ac.getaccessibleatpoint(pt)
if isnull(j)=false then 
i= j.accessiblecontext.accessibleindexinparent
if nm= "2" then
if i <cc-1 then lb.makevisible i+1
else
if i >0 then lb.makevisible i-1
end if
end if

case "3" 'start
lb.makevisible 0
case "4" 'end
lb.makevisible lb.itemcount -1
end select
end sub

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Jimmy2768
Posts: 19
Joined: Sat Jan 22, 2022 2:15 pm

Re: [Solved] Move scrollbar to show text using code

Post by Jimmy2768 »

RoryOF wrote:Note that OpenOffice (and I presume LibreOffice) has a very full set of listbox macros under OpenOffice Macros /Tools /Listbox.
Thank you RoryOF.
I din't knew that. :)
Open Office 4.1.9
MX Linux 19.4 - Windows 7
Jimmy2768
Posts: 19
Joined: Sat Jan 22, 2022 2:15 pm

Re: [Solved] Move scrollbar to show text using code

Post by Jimmy2768 »

Thank you for your persistance JeJe. :super:

I tried on both portable and installed versions.

Again, doesn't work on earlier editions.

I found that if i scroll the list using the scrollbar, the .getlocation.y gives correct results.
But if i use .makeVisible() to scroll the list, then .getlocation.y gives incorrect results.
Maybe it was a bug on those versions.
Open Office 4.1.9
MX Linux 19.4 - Windows 7
JeJe
Volunteer
Posts: 2780
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Move scrollbar to show text using code

Post by JeJe »

In that circumstance I look for something that can indirectly reset it to the correct value... redrawing the control or something like (that probably won't work, but something like that, that's otherwise harmless).

(Edit: though of course there may not be anything)

Anyone with the earlier version might be wise to update anyway.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply