<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="SF_DialogUtils" script:language="StarBasic" script:moduleType="normal">REM =======================================================================================================================
REM ===			The ScriptForge library and its associated libraries are part of the LibreOffice project.				===
REM	===						The SFDialogs library is one of the associated libraries.									===
REM ===					Full documentation is available on https://help.libreoffice.org/								===
REM =======================================================================================================================

Option Explicit
Option Private Module

&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;
&apos;&apos;&apos;	SF_DialogUtils
&apos;&apos;&apos;	========
&apos;&apos;&apos;		FOR INTERNAL USE ONLY
&apos;&apos;&apos;		Groups private functions that are common to the SF_Dialog and SF_DialogControl class modules
&apos;&apos;&apos;
&apos;&apos;&apos;		Topics where SF_DialogUtils matters:
&apos;&apos;&apos;			- resizing dialog and dialog controls
&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;

REM ================================================================== EXCEPTIONS

REM ============================================================ MODULE CONSTANTS

Public Const MINPOSITION			= -99999		&apos;	Conventionally indicates &quot;do not change position&quot;

REM =========================================pvA================= PRIVATE METHODS

REM -----------------------------------------------------------------------------
Public Function _ConvertPointToAppFont(ByRef poView As Object _
										, ByVal plX As Long _
										, ByVal plY As Long _
										) As Object
&apos;&apos;&apos;	Convert the X, Y position expressed in pixels to a Point expressed in &quot;Map APPFONT&quot;
&apos;&apos;&apos;	Args:
&apos;&apos;&apos;		poView: a com.sun.star.awt.XControl - stardiv.Toolkit.UnoDialogControl
&apos;&apos;&apos;		plX, plY : the horizontal and vertical coordinates of the top-left corner of the control
&apos;&apos;&apos;	Returns:
&apos;&apos;&apos;		a com.sun.star.awt.Point object

Dim oPoint As New com.sun.star.awt.Point	&apos;	The input Point
Dim oReturn As Object						&apos;	Return value

Try:
	oPoint.X = plX
	oPoint.Y = plY
	Set oReturn = poView.convertPointToLogic(oPoint, com.sun.star.util.MeasureUnit.APPFONT)

Finally:
	Set _ConvertPointToAppFont = oReturn
	Exit Function
End Function	&apos;	SFDialogs.SF_DialogUtils._ConvertPointToAppFont

REM -----------------------------------------------------------------------------
Public Function _ConvertPointToPixel(ByRef poView As Object _
										, ByVal plX As Long _
										, ByVal plY As Long _
										) As Object
&apos;&apos;&apos;	Convert the X, Y coordinates expressed in &quot;Map APPFONT&quot; units to a point expressed in pixels
&apos;&apos;&apos;	Args:
&apos;&apos;&apos;		poView: a com.sun.star.awt.XControl - stardiv.Toolkit.UnoDialogControl
&apos;&apos;&apos;		plX, plY : the horizontal and vertical coordinates of the top-left corner of the control
&apos;&apos;&apos;	Returns:
&apos;&apos;&apos;		a com.sun.star.awt.Point object

Dim oPoint As New com.sun.star.awt.Point	&apos;	The input point
Dim oReturn As Object						&apos;	Return value

Try:
	oPoint.X = plX
	oPoint.Y = plY
	Set oReturn = poView.convertPointToPixel(oPoint, com.sun.star.util.MeasureUnit.APPFONT)

Finally:
	Set _ConvertPointToPixel = oReturn
	Exit Function
End Function	&apos;	SFDialogs.SF_DialogUtils._ConvertPointToPixel

REM -----------------------------------------------------------------------------
Public Function _ConvertSizeToAppFont(ByRef poView As Object _
										, ByVal plWidth As Long _
										, ByVal plHeight As Long _
										) As Object
&apos;&apos;&apos;	Convert the Width, Height dimensions expressed in pixels to a Size expressed in &quot;Map APPFONT&quot;
&apos;&apos;&apos;	Args:
&apos;&apos;&apos;		poView: a com.sun.star.awt.XControl - stardiv.Toolkit.UnoDialogControl
&apos;&apos;&apos;		plWidth, plHeight : the horizontal and vertical dimensions of the control
&apos;&apos;&apos;	Returns:
&apos;&apos;&apos;		a com.sun.star.awt.Size object

Dim oSize As New com.sun.star.awt.Size		&apos;	The input size
Dim oReturn As Object						&apos;	Return value

Try:
	oSize.Width = plWidth
	oSize.Height = plHeight
	Set oReturn = poView.convertSizeToLogic(oSize, com.sun.star.util.MeasureUnit.APPFONT)

Finally:
	Set _ConvertSizeToAppFont = oReturn
	Exit Function
End Function	&apos;	SFDialogs.SF_DialogUtils._ConvertSizeToAppFont

REM -----------------------------------------------------------------------------
Public Function _ConvertSizeToPixel(ByRef poView As Object _
										, ByVal plWidth As Long _
										, ByVal plHeight As Long _
										) As Object
&apos;&apos;&apos;	Convert the Width, Height dimensions expressed in &quot;Map APPFONT&quot; units to a Size expressed in pixels
&apos;&apos;&apos;	Args:
&apos;&apos;&apos;		poView: a com.sun.star.awt.XControl - stardiv.Toolkit.UnoDialogControl
&apos;&apos;&apos;		plWidth, plHeight : the horizontal and vertical dimensions of the control
&apos;&apos;&apos;	Returns:
&apos;&apos;&apos;		a com.sun.star.awt.Size object

Dim oSize As New com.sun.star.awt.Size		&apos;	The input size
Dim oReturn As Object						&apos;	Return value

Try:
	oSize.Width = plWidth
	oSize.Height = plHeight
	Set oReturn = poView.convertSizeToPixel(oSize, com.sun.star.util.MeasureUnit.APPFONT)

Finally:
	Set _ConvertSizeToPixel = oReturn
	Exit Function
End Function	&apos;	SFDialogs.SF_DialogUtils._ConvertSizeToPixel

REM -----------------------------------------------------------------------------
Public Function _ConvertToAppFont(ByRef poView As Object _
									, ByVal pbPoint As Boolean _
									) As Object
&apos;&apos;&apos;	Switch between the _ConvertPointToAppFont and the _ConvertSizeToAppFont routines
&apos;&apos;&apos;	Args:
&apos;&apos;&apos;		poView: a com.sun.star.awt.XControl - stardiv.Toolkit.UnoDialogControl
&apos;&apos;&apos;		pbPoint: when True return a Point, otherwise return a Size
&apos;&apos;&apos;	Returns:
&apos;&apos;&apos;		a com.sun.star.awt.Point or a com.sun.star.awt.Size object

Static oSession As Object					&apos;	Alias of SF_Session
Dim oPosSize As Object						&apos;	com.sun.star.awt.Rectangle

Try:
	If IsNull(oSession) Then Set oSession = ScriptForge.SF_Services.CreateScriptService(&quot;Session&quot;)
	If oSession.HasUNOMethod(poView, &quot;getPosSize&quot;) Then
		Set oPosSize =poView.getPosSize()
	Else	&apos;	Should not happen
		Set oPosSize = New com.sun.star.awt.Rectangle
	End If

	If pbPoint Then
		_ConvertToAppFont = _ConvertPointToAppFont(poView, oPosSize.X, oPosSize.Y)			&apos;	com.sun.star.awt.Point
	Else
		_ConvertToAppFont = _ConvertSizeToAppFont(poView, oPosSize.Width, oPosSize.Height)	&apos;	com.sun.star.awt.Size
	End If

End Function	&apos;	SFDialogs.SF_DialogUtils._ConvertToAppFont

REM -----------------------------------------------------------------------------
Private Function _FormatsList(psControlType) As Variant
&apos;&apos;&apos;	Return the list of the allowed formats for Date and Time control types
&apos;&apos;&apos;	Args:
&apos;&apos;&apos;		DateField or TimeField control
&apos;&apos;&apos;	Returns:
&apos;&apos;&apos;		The allowed format entries as a zero-based array

Dim vFormats() As Variant		&apos;	Return value
Const CTLDATEFIELD		= &quot;DateField&quot;
Const CTLTIMEFIELD		= &quot;TimeField&quot;

	Select Case psControlType
		Case CTLDATEFIELD
			vFormats = Array( _
				&quot;Standard (short)&quot; _
				, &quot;Standard (short YY)&quot; _
				, &quot;Standard (short YYYY)&quot; _
				, &quot;Standard (long)&quot; _
				, &quot;DD/MM/YY&quot; _
				, &quot;MM/DD/YY&quot; _
				, &quot;YY/MM/DD&quot; _
				, &quot;DD/MM/YYYY&quot; _
				, &quot;MM/DD/YYYY&quot; _
				, &quot;YYYY/MM/DD&quot; _
				, &quot;YY-MM-DD&quot; _
				, &quot;YYYY-MM-DD&quot; _
				)
		Case CTLTIMEFIELD
			vFormats = Array( _
				&quot;24h short&quot; _
				, &quot;24h long&quot; _
				, &quot;12h short&quot; _
				, &quot;12h long&quot; _
				)
		Case Else
			vFormats = Array()
	End Select
	
	_FormatsList = vFormats

End Function	&apos;	SFDialogs.SF_DialogUtils._FormatsList

REM -----------------------------------------------------------------------------
Public Function _Resize(ByRef Control As Object _
								, Optional ByVal Left As Variant _
								, Optional ByVal Top As Variant _
								, Optional ByVal Width As Variant _
								, Optional ByVal Height As Variant _
								) As Boolean
&apos;&apos;&apos;	Move the top-left corner of a dialog or a dialog control to new coordinates and/or modify its dimensions
&apos;&apos;&apos;	Without arguments, the method either:
&apos;&apos;&apos;				leaves the position unchanged and computes best fit dimensions
&apos;&apos;&apos;				resets the initial position and dimensions (Scrollbar, ProgressBar, FixedLine, GroupBox, TreeControl&quot;, TableControl)
&apos;&apos;&apos;	Attributes denoting the position and size of a dialog are expressed in &quot;Map AppFont&quot; units.
&apos;&apos;&apos;	Map AppFont units are device and resolution independent.
&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.
&apos;&apos;&apos;	The dialog editor (= the Basic IDE) also uses Map AppFont units.
&apos;&apos;&apos;	Args:
&apos;&apos;&apos;		Control: a SF_Dialog or SF_DialogControl class instance
&apos;&apos;&apos;		Left : the horizontal distance from the top-left corner
&apos;&apos;&apos;		Top : the vertical distance from the top-left corner
&apos;&apos;&apos;		Width : the horizontal width of the rectangle containing the Dialog[Control]
&apos;&apos;&apos;		Height : the vertical height of the rectangle containing the Dialog[Control]
&apos;&apos;&apos;		Negative or missing arguments are left unchanged.
&apos;&apos;&apos;	Returns:
&apos;&apos;&apos;		True when successful

Dim bResize As Boolean				&apos;	Return value
Dim oModel As Object				&apos;	Model of Control object
Dim oView As Object					&apos;	View of Control object
Dim Displayed As Boolean			&apos;	When Trs, the dialog is currently active
Dim oSize As Object					&apos;	com.sun.star.awt.Size
Dim oPoint As Object				&apos;	com.sun.star.awt.Point
Dim oPreferredSize As Object		&apos;	com.sun.star.awt.Size
Dim iFlags As Integer				&apos;	com.sun.star.awt.PosSize constants
Static oSession As Object			&apos;	SF_Session alias
Dim cstThisSub As String
Const cstSubArgs = &quot;[Left], [Top], [Width], [Height]&quot;

	If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
	bResize = False

Check:
	If IsNull(Control) Then GoTo Finally
	If IsMissing(Left) Or IsEmpty(Left) Then Left = MINPOSITION
	If IsMissing(Top) Or IsEmpty(Top) Then Top = MINPOSITION
	If IsMissing(Height) Or IsEmpty(Height) Then Height = -1
	If IsMissing(Width) Or IsEmpty(Width) Then Width = -1
	If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
		If Not ScriptForge.SF_Utils._Validate(Left, &quot;Left&quot;, ScriptForge.V_NUMERIC) Then GoTo Finally
		If Not ScriptForge.SF_Utils._Validate(Top, &quot;Top&quot;, ScriptForge.V_NUMERIC) Then GoTo Finally
		If Not ScriptForge.SF_Utils._Validate(Width, &quot;Width&quot;, ScriptForge.V_NUMERIC) Then GoTo Finally
		If Not ScriptForge.SF_Utils._Validate(Height, &quot;Height&quot;, ScriptForge.V_NUMERIC) Then GoTo Finally
	End If

Try:
	With Control
		&apos;	Initialize local variables depending on caller
		Select Case .ObjectType
			Case &quot;DIALOG&quot;
				cstThisSub = &quot;SFDialogs.Dialog.Resize&quot;
				Set oModel = ._DialogModel
				Set oView = ._DialogControl
				Displayed = ._Displayed
			Case &quot;DIALOGCONTROL&quot;
				cstThisSub = &quot;SFDialogs.DialogControl.Resize&quot;
				Set oModel = ._ControlModel
				Set oView = ._ControlView
				Displayed = .[Parent]._Displayed
			Case Else
		End Select
		&apos;	Manage absence of arguments: best fit or reset
		If Left = MINPOSITION And Top = MINPOSITION And Width = -1 And Height = -1 Then
			If IsNull(oSession) Then Set oSession = ScriptForge.SF_Services.CreateScriptService(&quot;ScriptForge.Session&quot;)
			If oSession.HasUnoMethod(oView, &quot;getPreferredSize&quot;) Then
				&apos;	Compute a best fit size when relevant
				Set oPreferredSize = oView.getPreferredSize()
				Set oSize = SF_DialogUtils._ConvertSizeToAppFont(oView, oPreferredSize.Width, oPreferredSize.Height)
				Width = oSize.Width
				Height = oSize.Height
			Else
				&apos;	Reset factory settings otherwise
				Left = ._Left
				Top = ._Top
				Width = ._Width
				Height = ._Height
			End If
		End If
	End With

	&apos;	Model sizes are in APPFONTs, View sizes are in pixels. Use view.convertSizeToPixel() to convert
	&apos;	For dynamic dialogs: convertSizeToPixel() is available only as from the dialog is made visible
	&apos;	=&gt;	When the dialog is visible, positions and sizes are updated in view
	&apos;		When the dialog is not visible, positions and sizes adapted on model
	If Displayed Then
		With oView
			&apos;	Trace the elements to change
			iFlags = 0
			With com.sun.star.awt.PosSize
				If Left &gt; MINPOSITION	Then iFlags = iFlags + .X		Else Left = 0
				If Top &gt; MINPOSITION	Then iFlags = iFlags + .Y		Else Top = 0
				If Width &gt; 0			Then iFlags = iFlags + .WIDTH	Else Width = 0
				If Height &gt; 0			Then iFlags = iFlags + .HEIGHT	Else Height = 0
			End With
			&apos;	Convert APPFONT units to pixels
			Set oPoint = SF_DialogUtils._ConvertPointToPixel(oView, CLng(Left), CLng(Top))
			Set oSize = SF_DialogUtils._ConvertSizeToPixel(oView, CLng(Width), CLng(Height))
			&apos;	Rewrite
			If iFlags &gt; 0 Then .setPosSize(oPoint.X, oPoint.Y, oSize.Width, oSize.Height, iFlags)
		End With
	Else
		With oModel
			&apos;	Store position and dimensions in APPFONT units
			If Left &gt; MINPOSITION	Then .PositionX = CLng(Left)
			If Top &gt; MINPOSITION	Then .PositionY = CLng(Top)
			If Width &gt; 0			Then .Width = CLng(Width)
			If Height &gt; 0			Then .Height = CLng(Height)
		End With
	End If
	bResize = True

Finally:
	_Resize = bResize
	ScriptForge.SF_Utils._ExitFunction(cstThisSub)
	Exit Function
Catch:
	GoTo Finally
End Function	&apos;	SFDialogss.SF_DialogUtils._Resize

REM ============================================= END OF SFDIALOGS.SF_DIALOGUTILS

</script:module>