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
[Solved] From MouseEvent point to cell position
[Solved] From MouseEvent point to cell position
Last edited by Hagar Delest on Mon Dec 16, 2019 4:20 pm, edited 1 time in total.
Reason: tagged solved
Reason: tagged solved
OpenOffice 4.1.7 on Windows 7
Re: From MouseEvent point to cell position
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.
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.
Re: From MouseEvent point to cell position
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
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
Re: From MouseEvent point to cell position
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.
"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)
Re: From MouseEvent point to cell position
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
/Ove
OpenOffice 4.1.7 on Windows 7
Re: From MouseEvent point to cell position
Some code recorded by the MRI object inspection tool:
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.
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
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
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
[Solved]: From MouseEvent point to cell position
Thank you all for helping me out
/Ove
/Ove
OpenOffice 4.1.7 on Windows 7