[Solved] From MouseEvent point to cell position

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Ove
Posts: 10
Joined: Sun Oct 27, 2019 6:46 pm

[Solved] From MouseEvent point to cell position

Post by Ove »

Dear Community

I'm new to OpenOffice macro programming, so:
How can I translate the coordinates given in a MouseEvent into a cell position on a spreadsheet?

Appreciate any help
/Ove
Last edited by Hagar Delest on Mon Dec 16, 2019 4:20 pm, edited 1 time in total.
Reason: tagged solved
OpenOffice 4.1.7 on Windows 7
User avatar
Zizi64
Volunteer
Posts: 11359
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: From MouseEvent point to cell position

Post by Zizi64 »

Please upload your .ODF type sample file and your embedded macro code here.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Ove
Posts: 10
Joined: Sun Oct 27, 2019 6:46 pm

Re: From MouseEvent point to cell position

Post by Ove »

Sorry.
Are these functions enough to explain the problem?

Global g_oMouseClickHandler 'As Object

Sub OnDocumentLoadingFinished 'attached to event 'Document Loading Finished'
REM Grab the mouse clicks in this sheet
g_oMouseClickHandler = createUnoListener("MouseClickHandler_","com.sun.star.awt.XMouseClickHandler")
ThisComponent.CurrentController.addMouseClickHandler(g_oMouseClickHandler)
End Sub

'***************************************************************************************

sub MouseClickHandler_mousePressed(oEvt) As Boolean ' As com.sun.star.awt.MouseEvent)
REM Grab mouse clicks
Dim oCell As Object

oCell = FindCell(oEvt.X, oEvt.Y)
End Sub

'***************************************************************************************

sub MouseClickHandler_mouseReleased(oEvt) As Boolean
REM Grab this one too
End Sub

'***************************************************************************************

Function FindCell(xPos, yPos) As Object
Dim oCell As Object
'How do i find the cell under the (X, y) coordinates?'

FindCell = oCell
End Function
OpenOffice 4.1.7 on Windows 7
JeJe
Volunteer
Posts: 2780
Joined: Wed Mar 09, 2016 2:40 pm

Re: From MouseEvent point to cell position

Post by JeJe »

Suggestion here:

"I listen to selection event changes and keep the column and row of last change."

viewtopic.php?t=37304

An alternative approach *might* be to loop through column widths and row heights starting with the first visible ones and working out which is under the x y co-ordinate.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Ove
Posts: 10
Joined: Sun Oct 27, 2019 6:46 pm

Re: From MouseEvent point to cell position

Post by Ove »

Thank you JeJe. I liked the SelectionChangeEvent approach, but unfortunately this event seems to occure *after* the MouseClickEvent. I guess that leaves me with the loop approach?
/Ove
OpenOffice 4.1.7 on Windows 7
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: From MouseEvent point to cell position

Post by Villeroy »

Some code recorded by the MRI object inspection tool:

Code: Select all

Sub Snippet
  Dim oCurrentController As Variant
  Dim aVisibleArea As New com.sun.star.awt.Rectangle
  Dim aVisibleAreaOnScreen As New com.sun.star.awt.Rectangle
  Dim aVisibleRange As New com.sun.star.table.CellRangeAddress
  Dim oActiveSheet As Variant
  Dim oCellByPosition As Variant
  Dim aPosition As New com.sun.star.awt.Point
  Dim aSize As New com.sun.star.awt.Size
  Dim oCellByPosition2 As Variant
  Dim aPosition2 As New com.sun.star.awt.Point
  Dim aSize2 As New com.sun.star.awt.Size

  oCurrentController = ThisComponent.getCurrentController()
  aVisibleArea = oCurrentController.VisibleArea
  aVisibleAreaOnScreen = oCurrentController.VisibleAreaOnScreen
  
  aVisibleRange = oCurrentController.getVisibleRange()
  oActiveSheet = oCurrentController.getActiveSheet()
  oCellByPosition = oActiveSheet.getCellByPosition(0, 0)
  
  aPosition = oCellByPosition.Position
  aSize = oCellByPosition.Size
  oCellByPosition2 = oActiveSheet.getCellByPosition(1, 0)
  
  aPosition2 = oCellByPosition2.Position
  aSize2 = oCellByPosition2.Size
End Sub
The current controller has a visible area and a visible cell range address. Each cell of the visible range has a position and size which may intersect with a given mouse cursor position. Loop through the columns until you find the matching X and loop through the rows until you get the matching Y. Then you have the cell. Oh, and the view may be split or frozen. Then you have to work through multiple panes.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Ove
Posts: 10
Joined: Sun Oct 27, 2019 6:46 pm

[Solved]: From MouseEvent point to cell position

Post by Ove »

Thank you all for helping me out :)

/Ove
OpenOffice 4.1.7 on Windows 7
Post Reply