summaryrefslogtreecommitdiffstats
path: root/wizards/source/sfdialogs/SF_DialogUtils.xba
blob: e364acac46d36b2c480d4672c6f2dff1356911d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?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>