wizards/source/sfdocuments/SF_Calc.xba |   41 +++++++++++++++++++--------------
 wizards/source/sfdocuments/script.xlb  |   20 ++++++++--------
 2 files changed, 34 insertions(+), 27 deletions(-)

New commits:
commit 50ef4d525676b007c5805e015ff8da6f4768cf12
Author:     Jean-Pierre Ledure <[email protected]>
AuthorDate: Sun Oct 5 17:39:06 2025 +0200
Commit:     Jean-Pierre Ledure <[email protected]>
CommitDate: Mon Oct 6 10:57:54 2025 +0200

    ScriptForge (Calc) more accurate XRectangle
    
    The
      calc.XRectangle(range)
    property returns a com.sun.star.awt.Rectangle
    structure describing the position and size
    of the range given as a string.
    The function opens the opportunity to
    design popup menus in user scripts.
    
    The actual implementation has next improvements:
    
    1) When the zoom in the sheet was different from
       100%, there was a distorsion of the Height
       and Width values. Fixed now.
    
    2) When some toolbars were visible, the
       computation of the Y dimension was
       under-evaluated. Fixed now.
    
    3) The implementation does not use the
       deprecated XAccessibleXontext API
       anymore.
    
    The computations have been verified
    - the worksheet is not maximized
    - with varying row heights and column widths
    - with or without a formula bar
    - with or without toolbars
    - with different user interfaces
    
    The documentation does not need a revision.
    
    Change-Id: I06c9ec40d6cdaed74e5935950aaec9dec382b7fe
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/191906
    Tested-by: Jenkins
    Reviewed-by: Jean-Pierre Ledure <[email protected]>

diff --git a/wizards/source/sfdocuments/SF_Calc.xba 
b/wizards/source/sfdocuments/SF_Calc.xba
index 69f17353e2ed..b0eeabf80e4e 100644
--- a/wizards/source/sfdocuments/SF_Calc.xba
+++ b/wizards/source/sfdocuments/SF_Calc.xba
@@ -5355,17 +5355,22 @@ End Function    &apos;  
SFDocuments.SF_Calc._QuoteSheetName
 REM 
-----------------------------------------------------------------------------
 Private Function _RangePosition(ByVal psRange As String) As Object
 &apos;&apos;&apos;     Determine a best guess of the coordinates (in pixels) 
of the given range
+&apos;&apos;&apos;     The resulting values (X, Y, Width, Height) are computed 
relatively to the
+&apos;&apos;&apos;     borders of the ContainerWindow, which starts below the 
title and menu bars.
+&apos;&apos;&apos;     It includes the tabbed bar and the toolbars.
 &apos;&apos;&apos;     Inspired (and enhanced) from 
https://forum.openoffice.org/en/forum/viewtopic.php?p=308693#p308693
 &apos;&apos;&apos;     Args:
 &apos;&apos;&apos;             psRange: the range as a string
 &apos;&apos;&apos;     Returns:
 &apos;&apos;&apos;             a com.sun.star.awt.Rectangle UNO structure
 
-Dim oRectOnScreen As New com.sun.star.awt.Rectangle    &apos;  Range position 
versus top-left screen corner (return value)
-Dim oRect As New com.sun.star.awt.Rectangle                    &apos;  Range 
position versus the A1 cell
-Dim oLocation As Object                                                        
        &apos;  com.sun.star.awt.Rectangle
-Dim oController As Object                                                      
&apos;  Current controller
+Dim oRectOnScreen As New com.sun.star.awt.Rectangle    &apos;  Range position 
versus top-left corner of the ContainerWindow (return value)
+Dim oRect As New com.sun.star.awt.Rectangle                    &apos;  Range 
position versus the A1 cell (row/column headers excluded), in 1/100mm
+Dim dZoom As Double                                                            
        &apos;  The actual zoom value
+Dim oController As Object                                                      
&apos;  com.sun.star.frame.XController
+Dim oContainerWindow As Object                                         &apos;  
com.sun.star.awt.XWindow
 Dim oXRange As Object                                                          
&apos;  com.sun.star.Table.CellRange
+Dim oSize As New com.sun.star.awt.Size                         &apos;  Size of 
range in pixels
 
 Check:
        On Local Error GoTo Finally
@@ -5373,31 +5378,33 @@ Check:
 Try:
        Set oController = _Component.CurrentController
        Set oXRange = _ParseAddress(psRange).XCellRange
-       &apos;  Grab the window location on the screen
-       Select Case ScriptForge.SF_Platform._GetProductName(True)       &apos;  
Get a sortable release number
-               Case &lt;= 25.8
-                       Set oLocation = 
oController.ComponentWindow.AccessibleContext.LocationOnScreen
-               Case Else
-                       Set oLocation = 
oController.ComponentWindow.getProperty(&quot;XAccessible&quot;).LocationOnScreen
-       End Select
 
+       &apos;  Initialize the range area
        With oRect
                .X = oXRange.Position.X
                .Y = oXRange.Position.Y
-               .Width = oXRange.Size.Width
+               .Width = oXRange.Size.Width                     &apos;  Size is 
in 1/100mm
                .Height = oXRange.Size.Height
        End With
 
-       &apos;Compute the rectangle in pixels (empirical computation)
        With oController
+               &apos;  Convert dimensions in pixels
+               Set oSize = 
oController.ComponentWindow.convertSizeToPixel(oXRange.Size, 
com.sun.star.util.MeasureUnit.MM_100TH)
+               &apos;  Initialize useful data for next computations
+               dZoom = CDbl(.ZoomValue / 100.0)
+               Set oContainerWindow = .Frame.ContainerWindow
+       
+               &apos;  Compute the rectangle in pixels (empirical computations)
                oRectOnScreen.X = .VisibleAreaOnScreen.X _
                                + .VisibleAreaOnScreen.Width * (oRect.X - 
.VisibleArea.X) / .VisibleArea.Width _
-                               - oLocation.X
+                               - oContainerWindow.Info.LeftInset _
+                               - oContainerWindow.PosSize.X
                oRectOnScreen.Y = .VisibleAreaOnScreen.Y _
                                + .VisibleAreaOnScreen.Height * (oRect.Y - 
.VisibleArea.Y) / .VisibleArea.Height _
-                               - oLocation.Y
-               oRectOnScreen.Width = oRect.Width * .VisibleAreaOnScreen.Width 
/ .VisibleArea.Width
-               oRectOnScreen.Height = oRect.Height * 
.VisibleAreaOnScreen.Height / .VisibleArea.Height
+                               - oContainerWindow.Info.TopInset _
+                               - oContainerWindow.PosSize.Y
+               oRectOnScreen.Width = CLng(oSize.Width * dZoom + 0.5)
+               oRectOnScreen.Height = CLng(oSize.Height * dZoom + 0.5)
        End With
 
 Finally:
diff --git a/wizards/source/sfdocuments/script.xlb 
b/wizards/source/sfdocuments/script.xlb
index 8188fd53bb1f..3d2264bd3f6d 100644
--- a/wizards/source/sfdocuments/script.xlb
+++ b/wizards/source/sfdocuments/script.xlb
@@ -1,15 +1,15 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE library:library PUBLIC "-//OpenOffice.org//DTD OfficeDocument 
1.0//EN" "library.dtd">
 <library:library xmlns:library="http://openoffice.org/2000/library"; 
library:name="SFDocuments" library:readonly="false" 
library:passwordprotected="false">
- <library:element library:name="SF_Chart"/>
- <library:element library:name="SF_FormDocument"/>
- <library:element library:name="SF_FormControl"/>
- <library:element library:name="SF_Base"/>
- <library:element library:name="SF_Register"/>
- <library:element library:name="SF_Writer"/>
- <library:element library:name="SF_Calc"/>
- <library:element library:name="SF_Document"/>
- <library:element library:name="SF_DocumentListener"/>
- <library:element library:name="SF_Form"/>
  <library:element library:name="__License"/>
+ <library:element library:name="SF_Form"/>
+ <library:element library:name="SF_DocumentListener"/>
+ <library:element library:name="SF_Document"/>
+ <library:element library:name="SF_Calc"/>
+ <library:element library:name="SF_Writer"/>
+ <library:element library:name="SF_Register"/>
+ <library:element library:name="SF_Base"/>
+ <library:element library:name="SF_FormControl"/>
+ <library:element library:name="SF_FormDocument"/>
+ <library:element library:name="SF_Chart"/>
 </library:library>
\ No newline at end of file

Reply via email to