Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / wizards / source / sfdialogs / SF_DialogUtils.xba
blobe364acac46d36b2c480d4672c6f2dff1356911d5
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3 <script:module xmlns:script="http://openoffice.org/2000/script" script:name="SF_DialogUtils" script:language="StarBasic" script:moduleType="normal">REM =======================================================================================================================
4 REM === The ScriptForge library and its associated libraries are part of the LibreOffice project. ===
5 REM === The SFDialogs library is one of the associated libraries. ===
6 REM === Full documentation is available on https://help.libreoffice.org/ ===
7 REM =======================================================================================================================
9 Option Explicit
10 Option Private Module
12 &apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
13 &apos;&apos;&apos; SF_DialogUtils
14 &apos;&apos;&apos; ========
15 &apos;&apos;&apos; FOR INTERNAL USE ONLY
16 &apos;&apos;&apos; Groups private functions that are common to the SF_Dialog and SF_DialogControl class modules
17 &apos;&apos;&apos;
18 &apos;&apos;&apos; Topics where SF_DialogUtils matters:
19 &apos;&apos;&apos; - resizing dialog and dialog controls
20 &apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
22 REM ================================================================== EXCEPTIONS
24 REM ============================================================ MODULE CONSTANTS
26 Public Const MINPOSITION = -99999 &apos; Conventionally indicates &quot;do not change position&quot;
28 REM =========================================pvA================= PRIVATE METHODS
30 REM -----------------------------------------------------------------------------
31 Public Function _ConvertPointToAppFont(ByRef poView As Object _
32 , ByVal plX As Long _
33 , ByVal plY As Long _
34 ) As Object
35 &apos;&apos;&apos; Convert the X, Y position expressed in pixels to a Point expressed in &quot;Map APPFONT&quot;
36 &apos;&apos;&apos; Args:
37 &apos;&apos;&apos; poView: a com.sun.star.awt.XControl - stardiv.Toolkit.UnoDialogControl
38 &apos;&apos;&apos; plX, plY : the horizontal and vertical coordinates of the top-left corner of the control
39 &apos;&apos;&apos; Returns:
40 &apos;&apos;&apos; a com.sun.star.awt.Point object
42 Dim oPoint As New com.sun.star.awt.Point &apos; The input Point
43 Dim oReturn As Object &apos; Return value
45 Try:
46 oPoint.X = plX
47 oPoint.Y = plY
48 Set oReturn = poView.convertPointToLogic(oPoint, com.sun.star.util.MeasureUnit.APPFONT)
50 Finally:
51 Set _ConvertPointToAppFont = oReturn
52 Exit Function
53 End Function &apos; SFDialogs.SF_DialogUtils._ConvertPointToAppFont
55 REM -----------------------------------------------------------------------------
56 Public Function _ConvertPointToPixel(ByRef poView As Object _
57 , ByVal plX As Long _
58 , ByVal plY As Long _
59 ) As Object
60 &apos;&apos;&apos; Convert the X, Y coordinates expressed in &quot;Map APPFONT&quot; units to a point expressed in pixels
61 &apos;&apos;&apos; Args:
62 &apos;&apos;&apos; poView: a com.sun.star.awt.XControl - stardiv.Toolkit.UnoDialogControl
63 &apos;&apos;&apos; plX, plY : the horizontal and vertical coordinates of the top-left corner of the control
64 &apos;&apos;&apos; Returns:
65 &apos;&apos;&apos; a com.sun.star.awt.Point object
67 Dim oPoint As New com.sun.star.awt.Point &apos; The input point
68 Dim oReturn As Object &apos; Return value
70 Try:
71 oPoint.X = plX
72 oPoint.Y = plY
73 Set oReturn = poView.convertPointToPixel(oPoint, com.sun.star.util.MeasureUnit.APPFONT)
75 Finally:
76 Set _ConvertPointToPixel = oReturn
77 Exit Function
78 End Function &apos; SFDialogs.SF_DialogUtils._ConvertPointToPixel
80 REM -----------------------------------------------------------------------------
81 Public Function _ConvertSizeToAppFont(ByRef poView As Object _
82 , ByVal plWidth As Long _
83 , ByVal plHeight As Long _
84 ) As Object
85 &apos;&apos;&apos; Convert the Width, Height dimensions expressed in pixels to a Size expressed in &quot;Map APPFONT&quot;
86 &apos;&apos;&apos; Args:
87 &apos;&apos;&apos; poView: a com.sun.star.awt.XControl - stardiv.Toolkit.UnoDialogControl
88 &apos;&apos;&apos; plWidth, plHeight : the horizontal and vertical dimensions of the control
89 &apos;&apos;&apos; Returns:
90 &apos;&apos;&apos; a com.sun.star.awt.Size object
92 Dim oSize As New com.sun.star.awt.Size &apos; The input size
93 Dim oReturn As Object &apos; Return value
95 Try:
96 oSize.Width = plWidth
97 oSize.Height = plHeight
98 Set oReturn = poView.convertSizeToLogic(oSize, com.sun.star.util.MeasureUnit.APPFONT)
100 Finally:
101 Set _ConvertSizeToAppFont = oReturn
102 Exit Function
103 End Function &apos; SFDialogs.SF_DialogUtils._ConvertSizeToAppFont
105 REM -----------------------------------------------------------------------------
106 Public Function _ConvertSizeToPixel(ByRef poView As Object _
107 , ByVal plWidth As Long _
108 , ByVal plHeight As Long _
109 ) As Object
110 &apos;&apos;&apos; Convert the Width, Height dimensions expressed in &quot;Map APPFONT&quot; units to a Size expressed in pixels
111 &apos;&apos;&apos; Args:
112 &apos;&apos;&apos; poView: a com.sun.star.awt.XControl - stardiv.Toolkit.UnoDialogControl
113 &apos;&apos;&apos; plWidth, plHeight : the horizontal and vertical dimensions of the control
114 &apos;&apos;&apos; Returns:
115 &apos;&apos;&apos; a com.sun.star.awt.Size object
117 Dim oSize As New com.sun.star.awt.Size &apos; The input size
118 Dim oReturn As Object &apos; Return value
120 Try:
121 oSize.Width = plWidth
122 oSize.Height = plHeight
123 Set oReturn = poView.convertSizeToPixel(oSize, com.sun.star.util.MeasureUnit.APPFONT)
125 Finally:
126 Set _ConvertSizeToPixel = oReturn
127 Exit Function
128 End Function &apos; SFDialogs.SF_DialogUtils._ConvertSizeToPixel
130 REM -----------------------------------------------------------------------------
131 Public Function _ConvertToAppFont(ByRef poView As Object _
132 , ByVal pbPoint As Boolean _
133 ) As Object
134 &apos;&apos;&apos; Switch between the _ConvertPointToAppFont and the _ConvertSizeToAppFont routines
135 &apos;&apos;&apos; Args:
136 &apos;&apos;&apos; poView: a com.sun.star.awt.XControl - stardiv.Toolkit.UnoDialogControl
137 &apos;&apos;&apos; pbPoint: when True return a Point, otherwise return a Size
138 &apos;&apos;&apos; Returns:
139 &apos;&apos;&apos; a com.sun.star.awt.Point or a com.sun.star.awt.Size object
141 Static oSession As Object &apos; Alias of SF_Session
142 Dim oPosSize As Object &apos; com.sun.star.awt.Rectangle
144 Try:
145 If IsNull(oSession) Then Set oSession = ScriptForge.SF_Services.CreateScriptService(&quot;Session&quot;)
146 If oSession.HasUNOMethod(poView, &quot;getPosSize&quot;) Then
147 Set oPosSize =poView.getPosSize()
148 Else &apos; Should not happen
149 Set oPosSize = New com.sun.star.awt.Rectangle
150 End If
152 If pbPoint Then
153 _ConvertToAppFont = _ConvertPointToAppFont(poView, oPosSize.X, oPosSize.Y) &apos; com.sun.star.awt.Point
154 Else
155 _ConvertToAppFont = _ConvertSizeToAppFont(poView, oPosSize.Width, oPosSize.Height) &apos; com.sun.star.awt.Size
156 End If
158 End Function &apos; SFDialogs.SF_DialogUtils._ConvertToAppFont
160 REM -----------------------------------------------------------------------------
161 Private Function _FormatsList(psControlType) As Variant
162 &apos;&apos;&apos; Return the list of the allowed formats for Date and Time control types
163 &apos;&apos;&apos; Args:
164 &apos;&apos;&apos; DateField or TimeField control
165 &apos;&apos;&apos; Returns:
166 &apos;&apos;&apos; The allowed format entries as a zero-based array
168 Dim vFormats() As Variant &apos; Return value
169 Const CTLDATEFIELD = &quot;DateField&quot;
170 Const CTLTIMEFIELD = &quot;TimeField&quot;
172 Select Case psControlType
173 Case CTLDATEFIELD
174 vFormats = Array( _
175 &quot;Standard (short)&quot; _
176 , &quot;Standard (short YY)&quot; _
177 , &quot;Standard (short YYYY)&quot; _
178 , &quot;Standard (long)&quot; _
179 , &quot;DD/MM/YY&quot; _
180 , &quot;MM/DD/YY&quot; _
181 , &quot;YY/MM/DD&quot; _
182 , &quot;DD/MM/YYYY&quot; _
183 , &quot;MM/DD/YYYY&quot; _
184 , &quot;YYYY/MM/DD&quot; _
185 , &quot;YY-MM-DD&quot; _
186 , &quot;YYYY-MM-DD&quot; _
188 Case CTLTIMEFIELD
189 vFormats = Array( _
190 &quot;24h short&quot; _
191 , &quot;24h long&quot; _
192 , &quot;12h short&quot; _
193 , &quot;12h long&quot; _
195 Case Else
196 vFormats = Array()
197 End Select
199 _FormatsList = vFormats
201 End Function &apos; SFDialogs.SF_DialogUtils._FormatsList
203 REM -----------------------------------------------------------------------------
204 Public Function _Resize(ByRef Control As Object _
205 , Optional ByVal Left As Variant _
206 , Optional ByVal Top As Variant _
207 , Optional ByVal Width As Variant _
208 , Optional ByVal Height As Variant _
209 ) As Boolean
210 &apos;&apos;&apos; Move the top-left corner of a dialog or a dialog control to new coordinates and/or modify its dimensions
211 &apos;&apos;&apos; Without arguments, the method either:
212 &apos;&apos;&apos; leaves the position unchanged and computes best fit dimensions
213 &apos;&apos;&apos; resets the initial position and dimensions (Scrollbar, ProgressBar, FixedLine, GroupBox, TreeControl&quot;, TableControl)
214 &apos;&apos;&apos; Attributes denoting the position and size of a dialog are expressed in &quot;Map AppFont&quot; units.
215 &apos;&apos;&apos; Map AppFont units are device and resolution independent.
216 &apos;&apos;&apos; One Map AppFont unit is equal to one eighth of the average character (Systemfont) height and one quarter of the average character width.
217 &apos;&apos;&apos; The dialog editor (= the Basic IDE) also uses Map AppFont units.
218 &apos;&apos;&apos; Args:
219 &apos;&apos;&apos; Control: a SF_Dialog or SF_DialogControl class instance
220 &apos;&apos;&apos; Left : the horizontal distance from the top-left corner
221 &apos;&apos;&apos; Top : the vertical distance from the top-left corner
222 &apos;&apos;&apos; Width : the horizontal width of the rectangle containing the Dialog[Control]
223 &apos;&apos;&apos; Height : the vertical height of the rectangle containing the Dialog[Control]
224 &apos;&apos;&apos; Negative or missing arguments are left unchanged.
225 &apos;&apos;&apos; Returns:
226 &apos;&apos;&apos; True when successful
228 Dim bResize As Boolean &apos; Return value
229 Dim oModel As Object &apos; Model of Control object
230 Dim oView As Object &apos; View of Control object
231 Dim Displayed As Boolean &apos; When Trs, the dialog is currently active
232 Dim oSize As Object &apos; com.sun.star.awt.Size
233 Dim oPoint As Object &apos; com.sun.star.awt.Point
234 Dim oPreferredSize As Object &apos; com.sun.star.awt.Size
235 Dim iFlags As Integer &apos; com.sun.star.awt.PosSize constants
236 Static oSession As Object &apos; SF_Session alias
237 Dim cstThisSub As String
238 Const cstSubArgs = &quot;[Left], [Top], [Width], [Height]&quot;
240 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
241 bResize = False
243 Check:
244 If IsNull(Control) Then GoTo Finally
245 If IsMissing(Left) Or IsEmpty(Left) Then Left = MINPOSITION
246 If IsMissing(Top) Or IsEmpty(Top) Then Top = MINPOSITION
247 If IsMissing(Height) Or IsEmpty(Height) Then Height = -1
248 If IsMissing(Width) Or IsEmpty(Width) Then Width = -1
249 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
250 If Not ScriptForge.SF_Utils._Validate(Left, &quot;Left&quot;, ScriptForge.V_NUMERIC) Then GoTo Finally
251 If Not ScriptForge.SF_Utils._Validate(Top, &quot;Top&quot;, ScriptForge.V_NUMERIC) Then GoTo Finally
252 If Not ScriptForge.SF_Utils._Validate(Width, &quot;Width&quot;, ScriptForge.V_NUMERIC) Then GoTo Finally
253 If Not ScriptForge.SF_Utils._Validate(Height, &quot;Height&quot;, ScriptForge.V_NUMERIC) Then GoTo Finally
254 End If
256 Try:
257 With Control
258 &apos; Initialize local variables depending on caller
259 Select Case .ObjectType
260 Case &quot;DIALOG&quot;
261 cstThisSub = &quot;SFDialogs.Dialog.Resize&quot;
262 Set oModel = ._DialogModel
263 Set oView = ._DialogControl
264 Displayed = ._Displayed
265 Case &quot;DIALOGCONTROL&quot;
266 cstThisSub = &quot;SFDialogs.DialogControl.Resize&quot;
267 Set oModel = ._ControlModel
268 Set oView = ._ControlView
269 Displayed = .[Parent]._Displayed
270 Case Else
271 End Select
272 &apos; Manage absence of arguments: best fit or reset
273 If Left = MINPOSITION And Top = MINPOSITION And Width = -1 And Height = -1 Then
274 If IsNull(oSession) Then Set oSession = ScriptForge.SF_Services.CreateScriptService(&quot;ScriptForge.Session&quot;)
275 If oSession.HasUnoMethod(oView, &quot;getPreferredSize&quot;) Then
276 &apos; Compute a best fit size when relevant
277 Set oPreferredSize = oView.getPreferredSize()
278 Set oSize = SF_DialogUtils._ConvertSizeToAppFont(oView, oPreferredSize.Width, oPreferredSize.Height)
279 Width = oSize.Width
280 Height = oSize.Height
281 Else
282 &apos; Reset factory settings otherwise
283 Left = ._Left
284 Top = ._Top
285 Width = ._Width
286 Height = ._Height
287 End If
288 End If
289 End With
291 &apos; Model sizes are in APPFONTs, View sizes are in pixels. Use view.convertSizeToPixel() to convert
292 &apos; For dynamic dialogs: convertSizeToPixel() is available only as from the dialog is made visible
293 &apos; =&gt; When the dialog is visible, positions and sizes are updated in view
294 &apos; When the dialog is not visible, positions and sizes adapted on model
295 If Displayed Then
296 With oView
297 &apos; Trace the elements to change
298 iFlags = 0
299 With com.sun.star.awt.PosSize
300 If Left &gt; MINPOSITION Then iFlags = iFlags + .X Else Left = 0
301 If Top &gt; MINPOSITION Then iFlags = iFlags + .Y Else Top = 0
302 If Width &gt; 0 Then iFlags = iFlags + .WIDTH Else Width = 0
303 If Height &gt; 0 Then iFlags = iFlags + .HEIGHT Else Height = 0
304 End With
305 &apos; Convert APPFONT units to pixels
306 Set oPoint = SF_DialogUtils._ConvertPointToPixel(oView, CLng(Left), CLng(Top))
307 Set oSize = SF_DialogUtils._ConvertSizeToPixel(oView, CLng(Width), CLng(Height))
308 &apos; Rewrite
309 If iFlags &gt; 0 Then .setPosSize(oPoint.X, oPoint.Y, oSize.Width, oSize.Height, iFlags)
310 End With
311 Else
312 With oModel
313 &apos; Store position and dimensions in APPFONT units
314 If Left &gt; MINPOSITION Then .PositionX = CLng(Left)
315 If Top &gt; MINPOSITION Then .PositionY = CLng(Top)
316 If Width &gt; 0 Then .Width = CLng(Width)
317 If Height &gt; 0 Then .Height = CLng(Height)
318 End With
319 End If
320 bResize = True
322 Finally:
323 _Resize = bResize
324 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
325 Exit Function
326 Catch:
327 GoTo Finally
328 End Function &apos; SFDialogss.SF_DialogUtils._Resize
330 REM ============================================= END OF SFDIALOGS.SF_DIALOGUTILS
332 </script:module>