tdf#164434 Correct SvgSymbolNode SVG import
[LibreOffice.git] / wizards / source / sfdocuments / SF_Register.xba
blobc2a58bc61a8db78762dabf021098d3d7f239df1f
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_Register" 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 SFDocuments library is one of the associated libraries. ===
6 REM === Full documentation is available on https://help.libreoffice.org/ ===
7 REM =======================================================================================================================
9 Option Compatible
10 Option Explicit
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_Register
14 &apos;&apos;&apos; ===========
15 &apos;&apos;&apos; The ScriptForge framework includes
16 &apos;&apos;&apos; the master ScriptForge library
17 &apos;&apos;&apos; a number of &quot;associated&quot; libraries SF*
18 &apos;&apos;&apos; any user/contributor extension wanting to fit into the framework
19 &apos;&apos;&apos;
20 &apos;&apos;&apos; The main methods in this module allow the current library to cling to ScriptForge
21 &apos;&apos;&apos; - RegisterScriptServices
22 &apos;&apos;&apos; Register the list of services implemented by the current library
23 &apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
25 REM ================================================================== EXCEPTIONS
27 REM ================================================================= DEFINITIONS
29 &apos;&apos;&apos; Strategy for management of Form and FormControl events:
30 &apos;&apos;&apos; ------------------------------------------------------
31 &apos;&apos;&apos; At the contrary of Dialogs and DialogControls, which are always started from some code,
32 &apos;&apos;&apos; Forms and FormControls will be initiated most often by the user, even if the SFDocuments library
33 &apos;&apos;&apos; allows to start forms programmatically
34 &apos;&apos;&apos;
35 &apos;&apos;&apos; For Forms started programmatically, the corresponding objects are built top-down
36 &apos;&apos;&apos; Event management of forms and their controls requires to being able to rebuild Form
37 &apos;&apos;&apos; and FormControl objects bottom-up
38 &apos;&apos;&apos;
39 &apos;&apos;&apos; To avoid multiple rebuilds requested by multiple events,
40 &apos;&apos;&apos; 1. The active form objects are cached in a global array of _FormCache types
41 &apos;&apos;&apos; 2. FormControl objects are cached in Form objects
42 &apos;&apos;&apos; 3. The bottom-up rebuild is executed only once, at instance creation
44 Type _FormCache
45 Terminated As Boolean
46 XUnoForm As Object
47 BasicForm As Object
48 End Type
50 REM ============================================================== PUBLIC METHODS
52 REM -----------------------------------------------------------------------------
53 Public Sub RegisterScriptServices() As Variant
54 &apos;&apos;&apos; Register into ScriptForge the list of the services implemented by the current library
55 &apos;&apos;&apos; Each library pertaining to the framework must implement its own version of this method
56 &apos;&apos;&apos;
57 &apos;&apos;&apos; It consists in successive calls to the RegisterService() and RegisterEventManager() methods
58 &apos;&apos;&apos; with 2 arguments:
59 &apos;&apos;&apos; ServiceName: the name of the service as a case-insensitive string
60 &apos;&apos;&apos; ServiceReference: the reference as an object
61 &apos;&apos;&apos; If the reference refers to a module, then return the module as an object:
62 &apos;&apos;&apos; GlobalScope.Library.Module
63 &apos;&apos;&apos; If the reference is a class instance, then return a string referring to the method
64 &apos;&apos;&apos; containing the New statement creating the instance
65 &apos;&apos;&apos; &quot;libraryname.modulename.function&quot;
67 With GlobalScope.ScriptForge.SF_Services
68 .RegisterService(&quot;Document&quot;, &quot;SFDocuments.SF_Register._NewDocument&quot;) &apos; Reference to the function initializing the service
69 .RegisterService(&quot;Base&quot;, &quot;SFDocuments.SF_Register._NewDocument&quot;) &apos; Same reference, distinction is made inside the function
70 .RegisterService(&quot;Calc&quot;, &quot;SFDocuments.SF_Register._NewDocument&quot;) &apos; Same reference, distinction is made inside the function
71 .RegisterService(&quot;Writer&quot;, &quot;SFDocuments.SF_Register._NewDocument&quot;) &apos; Same reference, distinction is made inside the function
72 .RegisterService(&quot;FormDocument&quot;, &quot;SFDocuments.SF_Register._NewDocument&quot;) &apos; Same reference, distinction is made inside the function
73 .RegisterEventManager(&quot;DocumentEvent&quot;, &quot;SFDocuments.SF_Register._EventManager&quot;) &apos; Reference to the events manager
74 .RegisterEventManager(&quot;FormEvent&quot;, &quot;SFDocuments.SF_Register._FormEventManager&quot;)&apos; Reference to the form and controls events manager
75 End With
77 End Sub &apos; SFDocuments.SF_Register.RegisterScriptServices
79 REM =========================================================== PRIVATE FUNCTIONS
81 REM -----------------------------------------------------------------------------
82 Private Function _AddFormToCache(ByRef pvUnoForm As Object _
83 , ByRef pvBasicForm As Object _
84 ) As Long
85 &apos;&apos;&apos; Add a new entry in the cache array with the references of the actual Form
86 &apos;&apos;&apos; If relevant, the last entry of the cache is reused.
87 &apos;&apos;&apos; The cache is located in the global _SF_ variable
88 &apos;&apos;&apos; Args:
89 &apos;&apos;&apos; pvUnoForm: com.sun.star.form.XForm or com.sun.star.comp.forms.ODatabaseForm
90 &apos;&apos;&apos; pvBasicForm: its corresponding Basic object
91 &apos;&apos;&apos; Returns:
92 &apos;&apos;&apos; The index of the new or modified entry
94 Dim vCache As New _FormCache &apos; Entry to be added
95 Dim lIndex As Long &apos; UBound of _SF_.SFForms
96 Dim vCacheArray As Variant &apos; Alias of _SF_.SFForms
98 Try:
99 vCacheArray = _SF_.SFForms
101 If IsEmpty(vCacheArray) Then vCacheArray = Array()
102 lIndex = UBound(vCacheArray)
103 If lIndex &lt; LBound(vCacheArray) Then
104 ReDim vCacheArray(0 To 0)
105 lIndex = 0
106 ElseIf Not vCacheArray(lIndex).Terminated Then &apos; Often last entry can be reused
107 lIndex = lIndex + 1
108 ReDim Preserve vCacheArray(0 To lIndex)
109 End If
111 With vCache
112 .Terminated = False
113 Set .XUnoForm = pvUnoForm
114 Set .BasicForm = pvBasicForm
115 End With
116 Set vCacheArray(lIndex) = vCache
118 _SF_.SFForms = vCacheArray
120 Finally:
121 _AddFormToCache = lIndex
122 Exit Function
123 End Function &apos; SFDocuments.SF_Register._AddFormToCache
125 REM -----------------------------------------------------------------------------
126 Private Sub _CleanCacheEntry(ByVal plIndex As Long)
127 &apos;&apos;&apos; Clean the plIndex-th entry in the Forms cache
128 &apos;&apos;&apos; Args:
129 &apos;&apos;&apos; plIndex: must fit within the actual boundaries of the cache, otherwise the request is ignored
131 Dim vCache As New _FormCache &apos; Cleaned entry
133 With _SF_
134 If Not IsArray(.SFForms) Then Exit Sub
135 If plIndex &lt; LBound(.SFForms) Or plIndex &gt; UBound(.SFForms) Then Exit Sub
137 With vCache
138 .Terminated = True
139 Set .XUnoForm = Nothing
140 Set .BasicForm = Nothing
141 End With
142 .SFForms(plIndex) = vCache
143 End With
145 Finally:
146 Exit Sub
147 End Sub &apos; SFDocuments.SF_Register._CleanCacheEntry
149 REM -----------------------------------------------------------------------------
150 Public Function _EventManager(Optional ByRef pvArgs As Variant) As Object
151 &apos;&apos;&apos; Returns a Document, Calc or Base object corresponding with the active component
152 &apos;&apos;&apos; which triggered the event in argument
153 &apos;&apos;&apos; This method should be triggered only thru the invocation of CreateScriptService
154 &apos;&apos;&apos; Args:
155 &apos;&apos;&apos; pvEvent: com.sun.star.document.DocumentEvent
156 &apos;&apos;&apos; Returns:
157 &apos;&apos;&apos; the output of a Document, Calc, ... service or Nothing
158 &apos;&apos;&apos; Example:
159 &apos;&apos;&apos; Sub TriggeredByEvent(ByRef poEvent As Object)
160 &apos;&apos;&apos; Dim oDoc As Object
161 &apos;&apos;&apos; Set oDoc = CreateScriptService(&quot;SFDocuments.DocumentEvent&quot;, poEvent)
162 &apos;&apos;&apos; If Not IsNull(oDoc) Then
163 &apos;&apos;&apos; &apos; ... (a valid document has been identified)
164 &apos;&apos;&apos; End Sub
166 Dim oSource As Object &apos; Return value
167 Dim vEvent As Variant &apos; Alias of pvArgs(0)
169 &apos; Never abort while an event is processed
170 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Finally
171 Set oSource = Nothing
173 Check:
174 If IsMissing(pvArgs) Or IsEmpty(pvArgs) Then pvArgs = Array()
175 If UBound(pvArgs) &gt;= 0 Then vEvent = pvArgs(0) Else Set vEvent = Empty
176 If VarType(vEvent) &lt;&gt; ScriptForge.V_OBJECT Then GoTo Finally
178 Try:
179 If ScriptForge.SF_Session.UnoObjectType(vEvent) = &quot;com.sun.star.document.DocumentEvent&quot; Then
180 Set oSource = SF_Register._NewDocument(vEvent.Source)
181 End If
183 Finally:
184 Set _EventManager = oSource
185 Exit Function
186 End Function &apos; SFDocuments.SF_Register._EventManager
188 REM -----------------------------------------------------------------------------
189 Private Function _FindFormInCache(ByRef poForm As Object) As Object
190 &apos;&apos;&apos; Find the Form based on its XUnoForm
191 &apos;&apos;&apos; The Form must not be terminated
192 &apos;&apos;&apos; Returns:
193 &apos;&apos;&apos; The corresponding Basic Form part or Nothing
195 Dim oBasicForm As Object &apos; Return value
196 Dim oCache As _FormCache &apos; Entry in the cache
198 Set oBasicForm = Nothing
200 Try:
201 With _SF_
202 If Not IsEmpty(.SFForms) Then
203 For Each oCache In .SFForms
204 If EqualUnoObjects(poForm, oCache.XUnoForm) And Not oCache.Terminated Then
205 Set oBasicForm = oCache.BasicForm
206 Exit For
207 End If
208 Next oCache
209 End If
210 End With
212 Finally:
213 Set _FindFormInCache = oBasicForm
214 Exit Function
215 End Function &apos; SFDocuments.SF_Register._FindFormInCache
217 REM -----------------------------------------------------------------------------
218 Public Function _FormEventManager(Optional ByRef pvArgs As Variant) As Object
219 &apos;&apos;&apos; Returns a Form or FormControl object corresponding with the form or control
220 &apos;&apos;&apos; which triggered the event in argument
221 &apos;&apos;&apos; This method should be triggered only thru the invocation of CreateScriptService
222 &apos;&apos;&apos; Args:
223 &apos;&apos;&apos; pvEvent: com.sun.star.lang.EventObject
224 &apos;&apos;&apos; Returns:
225 &apos;&apos;&apos; the output of a Form, FormControl service or Nothing
226 &apos;&apos;&apos; Example:
227 &apos;&apos;&apos; Sub TriggeredByEvent(ByRef poEvent As Object)
228 &apos;&apos;&apos; Dim oForm As Object
229 &apos;&apos;&apos; Set oForm = CreateScriptService(&quot;SFDocuments.FormEvent&quot;, poEvent)
230 &apos;&apos;&apos; If Not IsNull(oForm) Then
231 &apos;&apos;&apos; &apos; ... (a valid form or subform has been identified)
232 &apos;&apos;&apos; End Sub
234 Dim oSource As Object &apos; Return value
235 Dim vEvent As Variant &apos; Alias of pvArgs(0)
236 Dim oControlModel As Object &apos; com.sun.star.awt.XControlModel
237 Dim oParent As Object &apos; com.sun.star.form.OGridControlModel or com.sun.star.comp.forms.ODatabaseForm
238 Dim sParentType As String &apos; &quot;com.sun.star.form.OGridControlModel&quot; or &quot;com.sun.star.comp.forms.ODatabaseForm&quot;
239 Dim oSFParent As Object &apos; The parent as a ScriptForge instance: SF_Form or SF_FormControl
240 Dim oSFForm As Object &apos; The grand-parent SF_Form instance
241 Dim oSession As Object : Set oSession = ScriptForge.SF_Session
243 &apos; Never abort while an event is processed
244 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Finally
245 Set oSource = Nothing
247 Check:
248 If IsMissing(pvArgs) Or IsEmpty(pvArgs) Then pvArgs = Array()
249 If UBound(pvArgs) &gt;= 0 Then vEvent = pvArgs(0) Else Set vEvent = Empty
250 If VarType(vEvent) &lt;&gt; ScriptForge.V_OBJECT Then GoTo Finally
252 Try:
253 If oSession.HasUnoProperty(vEvent, &quot;Source&quot;) Then
255 &apos; FORM EVENT
256 If oSession.UnoObjectType(vEvent.Source) = &quot;com.sun.star.comp.forms.ODatabaseForm&quot; Then
257 Set oSource = SF_Register._NewForm(vEvent.Source, pbForceInit := True)
259 &apos; CONTROL EVENT
260 Else
261 &apos; A SF_FormControl instance is always created from its parent, either a form, a subform or a table control
262 Set oControlModel = vEvent.Source.Model &apos; The event source is a control view com.sun.star.awt.XControl
263 Set oParent = oControlModel.Parent
264 sParentType = oSession.UnoObjectType(oParent)
265 Select Case sParentType
266 Case &quot;com.sun.star.form.OGridControlModel&quot;
267 Set oSFForm = SF_Register._NewForm(oParent.Parent, pbForceInit := True)
268 Set oSFParent = oSFForm.Controls(oParent.Name)
269 Case &quot;com.sun.star.comp.forms.ODatabaseForm&quot;
270 Set oSFParent = SF_Register._NewForm(oParent, pbForceInit := True)
271 End Select
272 &apos; The final instance is derived from its parent instance
273 Set oSource = oSFParent.Controls(oControlModel.Name)
275 End If
277 End If
279 Finally:
280 Set _FormEventManager = oSource
281 Exit Function
282 End Function &apos; SFDocuments.SF_Register._FormEventManager
284 REM -----------------------------------------------------------------------------
285 Public Function _GetEventScriptCode(poObject As Object _
286 , ByVal psEvent As String _
287 , ByVal psName As String _
288 ) As String
289 &apos;&apos;&apos; Extract from the parent of poObject the Basic script linked to psEvent.
290 &apos;&apos;&apos; Helper function common to forms and form controls
291 &apos;&apos;&apos; Args:
292 &apos;&apos;&apos; poObject: a com.sun.star.form.XForm or XControl object
293 &apos;&apos;&apos; psEvent: the &quot;On...&quot; name of the event
294 &apos;&apos;&apos; psName: the name of the object to be identified from the parent object
295 &apos;&apos;&apos; Returns:
296 &apos;&apos;&apos; The script to trigger when psEvent occurs
297 &apos;&apos;&apos; See Scripting Framework URI Specification : https://wiki.documentfoundation.org/Documentation/DevGuide/Scripting_Framework#Scripting_Framework_URI_Specification
299 Dim vEvents As Variant &apos; List of available events in the parent object
300 &apos; Array of com.sun.star.script.ScriptEventDescriptor
301 Dim sEvent As String &apos; The targeted event name
302 Dim oParent As Object &apos; The parent object
303 Dim lIndex As Long &apos; The index of the targeted event in the events list of the parent object
304 Dim sName As String &apos; The corrected UNO event name
305 Dim i As Long
307 _GetEventScriptCode = &quot;&quot;
308 On Local Error GoTo Catch
309 If Not ScriptForge.SF_Session.HasUnoMethod(poObject, &quot;getParent&quot;) Then GoTo Finally
311 Try:
312 &apos; Find form index i.e. find control via getByIndex()
313 &apos; The name is known (= psName) but getByIndex() is not in the same sequence as getElementNames()
314 Set oParent = poObject.getParent()
315 lIndex = -1
316 For i = 0 To oParent.getCount() - 1
317 sName = oParent.getByIndex(i).Name
318 If (sName = psName) Then
319 lIndex = i
320 Exit For
321 End If
322 Next i
323 If lIndex &lt; 0 Then GoTo Finally &apos; Not found, should not happen
325 &apos; Find script triggered by event
326 vEvents = oParent.getScriptEvents(lIndex) &apos; Returns an array
327 &apos; Fix historical typo error
328 sEvent = Replace(LCase(Mid(psEvent, 3, 1)) &amp; Mid(psEvent, 4), &quot;errorOccurred&quot;, &quot;errorOccured&quot;)
329 For i = 0 To UBound(vEvents)
330 If vEvents(i).EventMethod = sEvent Then
331 _GetEventScriptCode = vEvents(i).ScriptCode
332 Exit For
333 End If
334 Next i
336 Finally:
337 Exit Function
338 Catch:
339 GoTo Finally
340 End Function &apos; SFDocuments.SF_Register._GetEventScriptCode
342 REM -----------------------------------------------------------------------------
343 Public Function _NewDocument(Optional ByVal pvArgs As Variant) As Object
344 &apos;&apos;&apos; Create a new instance of the (super) SF_Document class or of one of its subclasses (SF_Calc, ...)
345 &apos; Args:
346 &apos;&apos;&apos; WindowName: see the definition of WindowName in the description of the UI service
347 &apos;&apos;&apos; If absent, the document is presumed to be in the active window
348 &apos;&apos;&apos; If WindowName is an object, it must be a component
349 &apos;&apos;&apos; (com.sun.star.lang.XComponent or com.sun.star.comp.dba.ODatabaseDocument)
350 &apos;&apos;&apos; Returns: the instance or Nothing
352 Dim oDocument As Object &apos; Return value
353 Dim oSuperDocument As Object &apos; Companion superclass document
354 Dim vWindowName As Variant &apos; Alias of pvArgs(0)
355 Dim oEnum As Object &apos; com.sun.star.container.XEnumeration
356 Dim oComp As Object &apos; com.sun.star.lang.XComponent
357 Dim vWindow As Window &apos; A single component
358 Dim oUi As Object &apos; &quot;UI&quot; service
359 Dim bFound As Boolean &apos; True if the document is found on the desktop
361 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
363 Check:
364 If IsMissing(pvArgs) Or IsEmpty(pvArgs) Then pvArgs = Array()
365 If Not IsArray(pvArgs) Then pvArgs = Array(pvArgs) &apos; Needed when _NewDocument called from _EventManager
366 If UBound(pvArgs) &gt;= 0 Then vWindowName = pvArgs(0) Else vWindowName = &quot;&quot;
367 If Not ScriptForge.SF_Utils._Validate(vWindowName, &quot;WindowName&quot;, Array(V_STRING, ScriptForge.V_OBJECT)) Then GoTo Finally
368 Set oDocument = Nothing
370 Try:
371 Set oUi = ScriptForge.SF_Services.CreateScriptService(&quot;UI&quot;)
372 Select Case VarType(vWindowName)
373 Case V_STRING
374 If Len(vWindowName) &gt; 0 Then
375 bFound = False
376 Set oEnum = StarDesktop.Components().createEnumeration
377 Do While oEnum.hasMoreElements
378 Set oComp = oEnum.nextElement
379 vWindow = oUi._IdentifyWindow(oComp)
380 With vWindow
381 &apos; Does the current window match the argument ?
382 If (Len(.WindowFileName) &gt; 0 And .WindowFileName = ScriptForge.SF_FileSystem._ConvertToUrl(vWindowName)) _
383 Or (Len(.WindowName) &gt; 0 And .WindowName = vWindowName) _
384 Or (Len(.WindowTitle) &gt; 0 And .WindowTitle = vWindowName) Then
385 bFound = True
386 Exit Do
387 End If
388 End With
389 Loop
390 Else
391 bFound = True
392 vWindow = oUi._IdentifyWindow(StarDesktop.CurrentComponent)
393 End If
394 Case ScriptForge.V_OBJECT &apos; com.sun.star.lang.XComponent
395 bFound = True
396 vWindow = oUi._IdentifyWindow(vWindowName)
397 End Select
399 If bFound And Not IsNull(vWindow.Frame) And Len(vWindow.DocumentType) &gt; 0 Then
400 &apos; Create the right subclass and associate to it a new instance of the superclass
401 Select Case vWindow.DocumentType
402 Case &quot;Base&quot;
403 Set oDocument = New SF_Base
404 Set oSuperDocument = New SF_Document
405 Set oDocument.[_Super] = oSuperDocument &apos; Now both super and subclass are twinned
406 Set oSuperDocument.[_SubClass] = oDocument
407 Case &quot;Calc&quot;
408 Set oDocument = New SF_Calc
409 Set oSuperDocument = New SF_Document
410 Set oDocument.[_Super] = oSuperDocument &apos; Now both super and subclass are twinned
411 Set oSuperDocument.[_SubClass] = oDocument
412 Case &quot;FormDocument&quot;
413 Set oDocument = New SF_FormDocument
414 Set oSuperDocument = New SF_Document
415 Set oDocument.[_Super] = oSuperDocument &apos; Now both super and subclass are twinned
416 Set oSuperDocument.[_SubClass] = oDocument
417 Case &quot;Writer&quot;
418 Set oDocument = New SF_Writer
419 Set oSuperDocument = New SF_Document
420 Set oDocument.[_Super] = oSuperDocument &apos; Now both super and subclass are twinned
421 Set oSuperDocument.[_SubClass] = oDocument
422 Case Else &apos; Only superclass
423 Set oDocument = New SF_Document
424 Set oSuperDocument = oDocument
425 End Select
426 With oDocument &apos; Initialize attributes of subclass
427 Set .[Me] = oDocument
428 Set ._Component = vWindow.Component
429 &apos; Initialize specific attributes
430 Select Case vWindow.DocumentType
431 Case &quot;Base&quot;
432 Set ._DataSource = ._Component.DataSource
433 Case &quot;FormDocument&quot;
434 ._Initialize()
435 Case Else
436 End Select
437 End With
438 With oSuperDocument &apos; Initialize attributes of superclass
439 Set .[Me] = oSuperDocument
440 Set ._Component = vWindow.Component
441 Set ._Frame = vWindow.Frame
442 ._WindowName = vWindow.WindowName
443 ._WindowTitle = vWindow.WindowTitle
444 ._WindowFileName = vWindow.WindowFileName
445 ._DocumentType = vWindow.DocumentType
446 End With
447 End If
449 Finally:
450 Set _NewDocument = oDocument
451 Exit Function
452 Catch:
453 GoTo Finally
454 End Function &apos; SFDocuments.SF_Register._NewDocument
456 REM -----------------------------------------------------------------------------
457 Public Function _NewForm(ByRef poForm As Object _
458 , Optional pbForceInit As Boolean _
459 ) As Object
460 &apos;&apos;&apos; Returns an existing or a new SF_Form instance based on the argument
461 &apos;&apos;&apos; If the instance is new (not found in cache), the minimal members are initialized
462 &apos;&apos;&apos; Args:
463 &apos;&apos;&apos; poForm: com.sun.star.form.XForm or com.sun.star.comp.forms.ODatabaseForm
464 &apos;&apos;&apos; pbForceInit: when True, initialize the form instance. Default = False
465 &apos;&apos;&apos; Returns:
466 &apos;&apos;&apos; A SF_Form instance
468 Dim oForm As Object &apos; Return value
470 Try:
471 Set oForm = SF_Register._FindFormInCache(poForm)
472 If IsNull(oForm) Then &apos; Not found
473 If IsMissing(pbForceInit) Or IsEmpty(pbForceInit) Then pbForceInit = False
474 Set oForm = New SF_Form
475 With oForm
476 ._Name = poForm.Name
477 Set .[Me] = oForm
478 Set ._Form = poForm
479 If pbForceInit Then ._Initialize()
480 End With
481 End If
483 Finally:
484 Set _NewForm = oForm
485 Exit Function
486 End Function &apos; SFDocuments.SF_Register._NewForm
488 REM -----------------------------------------------------------------------------
489 Public Function _RegisterEventScript(poObject As Object _
490 , ByVal psEvent As String _
491 , ByVal psListener As String _
492 , ByVal psScriptCode As String _
493 , ByVal psName As String _
494 ) As Boolean
495 &apos;&apos;&apos; Register a script event (psEvent) to poObject (Form, SubForm or Control)
496 &apos;&apos;&apos; Args:
497 &apos;&apos;&apos; poObject: a com.sun.star.form.XForm or XControl object
498 &apos;&apos;&apos; psEvent: the &quot;On...&quot; name of the event
499 &apos;&apos;&apos; psListener: the listener name corresponding with the event
500 &apos;&apos;&apos; psScriptCode: The script to trigger when psEvent occurs
501 &apos;&apos;&apos; See Scripting Framework URI Specification : https://wiki.documentfoundation.org/Documentation/DevGuide/Scripting_Framework#Scripting_Framework_URI_Specification
502 &apos;&apos;&apos; psName: the name of the object to associate with the event
503 &apos;&apos;&apos; Returns:
504 &apos;&apos;&apos; True when successful
506 Dim oEvent As Object &apos; com.sun.star.script.ScriptEventDescriptor
507 Dim sEvent As String &apos; The targeted event name
508 Dim oParent As Object &apos; The parent object
509 Dim lIndex As Long &apos; The index of the targeted event in the events list of the parent object
510 Dim sName As String &apos; The corrected UNO event name
511 Dim i As Long
513 _RegisterEventScript = False
514 On Local Error GoTo Catch
515 If Not ScriptForge.SF_Session.HasUnoMethod(poObject, &quot;getParent&quot;) Then GoTo Finally
517 Try:
518 &apos; Find object&apos;s internal index i.e. how to reach it via getByIndex()
519 Set oParent = poObject.getParent()
520 lIndex = -1
521 For i = 0 To oParent.getCount() - 1
522 sName = oParent.getByIndex(i).Name
523 If (sName = psName) Then
524 lIndex = i
525 Exit For
526 End If
527 Next i
528 If lIndex &lt; 0 Then GoTo Finally &apos; Not found, should not happen
530 &apos; Fix historical typo error
531 sEvent = Replace(LCase(Mid(psEvent, 3, 1)) &amp; Mid(psEvent, 4), &quot;errorOccurred&quot;, &quot;errorOccured&quot;)
532 &apos; Apply new script code. Erasing it is done with a specific UNO method
533 If psScriptCode = &quot;&quot; Then
534 oParent.revokeScriptEvent(lIndex, psListener, sEvent, &quot;&quot;)
535 Else
536 Set oEvent = CreateUnoStruct(&quot;com.sun.star.script.ScriptEventDescriptor&quot;)
537 With oEvent
538 .ListenerType = psListener
539 .EventMethod = sEvent
540 .ScriptType = &quot;Script&quot; &apos; Better than &quot;Basic&quot;
541 .ScriptCode = psScriptCode
542 End With
543 oParent.registerScriptEvent(lIndex, oEvent)
544 End If
545 _RegisterEventScript = True
547 Finally:
548 Exit Function
549 Catch:
550 GoTo Finally
551 End Function &apos; SFDocuments.SF_Register._RegisterEventScript
553 REM ============================================== END OF SFDOCUMENTS.SF_REGISTER
554 </script:module>