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

Creating a macro - Writing a Script - Using the API

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

Postby ptbento » Wed Jun 10, 2020 8:35 pm

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
ptbento
 
Posts: 43
Joined: Fri Jan 18, 2013 9:33 pm

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

Postby JeJe » Wed Jun 10, 2020 8:55 pm

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

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

Postby ptbento » Wed Jun 10, 2020 9:05 pm

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
ptbento
 
Posts: 43
Joined: Fri Jan 18, 2013 9:33 pm

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

Postby JeJe » Thu Jun 11, 2020 12:56 am

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

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

Postby ptbento » Thu Jun 11, 2020 3:02 pm

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
ptbento
 
Posts: 43
Joined: Fri Jan 18, 2013 9:33 pm

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

Postby JeJe » Thu Jun 11, 2020 3:13 pm

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

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

Postby ptbento » Thu Jun 11, 2020 11:48 pm

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
ptbento
 
Posts: 43
Joined: Fri Jan 18, 2013 9:33 pm

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

Postby JeJe » Fri Jun 12, 2020 9:26 pm

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   Expand viewCollapse view
'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

Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 1089
Joined: Wed Mar 09, 2016 2:40 pm

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

Postby ptbento » Mon Jun 29, 2020 6:53 pm

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
ptbento
 
Posts: 43
Joined: Fri Jan 18, 2013 9:33 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 3 guests