calc: on editing invalidation of view with different zoom is wrong
[LibreOffice.git] / wizards / source / sfdialogs / SF_Register.xba
blobe81dbb069fa8c7c96617925fb3b14587f24b244b
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 SFDialogs 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 ================================================================= DEFINITIONS
27 &apos;&apos;&apos; Event management of dialogs requires to being able to rebuild a Dialog object
28 &apos;&apos;&apos; from its com.sun.star.awt.XControl - stardiv.Toolkit.UnoDialogControl UNO instance
29 &apos;&apos;&apos; For that purpose, the started dialogs are buffered in a global array of _DialogCache types
31 Type _DialogCache
32 Terminated As Boolean
33 XUnoDialog As Object
34 BasicDialog As Object
35 End Type
37 REM ================================================================== EXCEPTIONS
39 Private Const DIALOGNOTFOUNDERROR = &quot;DIALOGNOTFOUNDERROR&quot;
41 REM ============================================================== PUBLIC METHODS
43 REM -----------------------------------------------------------------------------
44 Public Sub RegisterScriptServices() As Variant
45 &apos;&apos;&apos; Register into ScriptForge the list of the services implemented by the current library
46 &apos;&apos;&apos; Each library pertaining to the framework must implement its own version of this method
47 &apos;&apos;&apos;
48 &apos;&apos;&apos; It consists in successive calls to the RegisterService() and RegisterEventManager() methods
49 &apos;&apos;&apos; with 2 arguments:
50 &apos;&apos;&apos; ServiceName: the name of the service as a case-insensitive string
51 &apos;&apos;&apos; ServiceReference: the reference as an object
52 &apos;&apos;&apos; If the reference refers to a module, then return the module as an object:
53 &apos;&apos;&apos; GlobalScope.Library.Module
54 &apos;&apos;&apos; If the reference is a class instance, then return a string referring to the method
55 &apos;&apos;&apos; containing the New statement creating the instance
56 &apos;&apos;&apos; &quot;libraryname.modulename.function&quot;
58 With GlobalScope.ScriptForge.SF_Services
59 .RegisterService(&quot;Dialog&quot;, &quot;SFDialogs.SF_Register._NewDialog&quot;) &apos; Reference to the function initializing the service
60 .RegisterEventManager(&quot;DialogEvent&quot;, &quot;SFDialogs.SF_Register._EventManager&quot;) &apos; Reference to the events manager
61 &apos;TODO
62 End With
64 End Sub &apos; SFDialogs.SF_Register.RegisterScriptServices
66 REM =========================================================== PRIVATE FUNCTIONS
68 REM -----------------------------------------------------------------------------
69 Private Function _AddDialogToCache(ByRef pvUnoDialog As Object _
70 , ByRef pvBasicDialog As Object _
71 ) As Long
72 &apos;&apos;&apos; Add a new entry in the cache array with the references of the actual dialog
73 &apos;&apos;&apos; If relevant, the last entry of the cache is reused.
74 &apos;&apos;&apos; The cache is located in the global _SF_ variable
75 &apos;&apos;&apos; Args:
76 &apos;&apos;&apos; pvUnoDialog: the com.sun.star.awt.XControl - stardiv.Toolkit.UnoDialogControl of the dialog box
77 &apos;&apos;&apos; pvBasicDialog: its corresponding Basic object
78 &apos;&apos;&apos; Returns:
79 &apos;&apos;&apos; The index of the new or modified entry
81 Dim vCache As New _DialogCache &apos; Entry to be added
82 Dim lIndex As Long &apos; UBound of _SF_.SFDialogs
83 Dim vCacheArray As Variant &apos; Alias of _SF_.SFDialogs
85 Try:
86 vCacheArray = _SF_.SFDialogs
88 If IsEmpty(vCacheArray) Then vCacheArray = Array()
89 lIndex = UBound(vCacheArray)
90 If lIndex &lt; LBound(vCacheArray) Then
91 ReDim vCacheArray(0 To 0)
92 lIndex = 0
93 ElseIf Not vCacheArray(lIndex).Terminated Then &apos; Often last entry can be reused
94 lIndex = lIndex + 1
95 ReDim Preserve vCacheArray(0 To lIndex)
96 End If
98 With vCache
99 .Terminated = False
100 Set .XUnoDialog = pvUnoDialog
101 Set .BasicDialog = pvBasicDialog
102 End With
103 vCacheArray(lIndex) = vCache
105 _SF_.SFDialogs = vCacheArray
107 Finally:
108 _AddDialogToCache = lIndex
109 Exit Function
110 End Function &apos; SFDialogs.SF_Register._AddDialogToCache
112 REM -----------------------------------------------------------------------------
113 Private Sub _CleanCacheEntry(ByVal plIndex As Long)
114 &apos;&apos;&apos; Clean the plIndex-th entry in the dialogs cache
115 &apos;&apos;&apos; Args:
116 &apos;&apos;&apos; plIndex: must fit within the actual boundaries of the cache, otherwise the request is ignored
118 Dim vCache As New _DialogCache &apos; Cleaned entry
120 With _SF_
121 If Not IsArray(.SFDialogs) Then Exit Sub
122 If plIndex &lt; LBound(.SFDialogs) Or plIndex &gt; UBound(.SFDialogs) Then Exit Sub
124 With vCache
125 .Terminated = True
126 Set .XUnoDialog = Nothing
127 Set .BasicDialog = Nothing
128 End With
129 .SFDialogs(plIndex) = vCache
130 End With
132 Finally:
133 Exit Sub
134 End Sub &apos; SFDialogs.SF_Register._CleanCacheEntry
136 REM -----------------------------------------------------------------------------
137 Public Function _EventManager(Optional ByRef pvArgs As Variant) As Object
138 &apos;&apos;&apos; Returns a Dialog or DialogControl object corresponding with the Basic dialog
139 &apos;&apos;&apos; which triggered the event in argument
140 &apos;&apos;&apos; This method should be triggered only thru the invocation of CreateScriptService
141 &apos;&apos;&apos; Args:
142 &apos;&apos;&apos; pvEvent: com.sun.star.xxx
143 &apos;&apos;&apos; Returns:
144 &apos;&apos;&apos; the output of a Dialog or DialogControl service or Nothing
145 &apos;&apos;&apos; Example:
146 &apos;&apos;&apos; Sub TriggeredByEvent(ByRef poEvent As Object)
147 &apos;&apos;&apos; Dim oDlg As Object
148 &apos;&apos;&apos; Set oDlg = CreateScriptService(&quot;SFDialogs.DialogEvent&quot;, poEvent)
149 &apos;&apos;&apos; If Not IsNull(oDlg) Then
150 &apos;&apos;&apos; &apos; ... (a valid dialog or one of its controls has been identified)
151 &apos;&apos;&apos; End Sub
153 Dim oSource As Object &apos; Return value
154 Dim oEventSource As Object &apos; Event UNO source
155 Dim vEvent As Variant &apos; Alias of pvArgs(0)
156 Dim sSourceType As String &apos; Implementation name of event source
157 Dim oDialog As Object &apos; com.sun.star.awt.XControl - stardiv.Toolkit.UnoDialogControl
158 Dim bControl As Boolean &apos; True when control event
160 &apos; Never abort while an event is processed
161 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Finally
162 Set oSource = Nothing
164 Check:
165 If IsMissing(pvArgs) Or IsEmpty(pvArgs) Then pvArgs = Array()
166 If UBound(pvArgs) &gt;= 0 Then vEvent = pvArgs(0) Else vEvent = Empty
167 If VarType(vEvent) &lt;&gt; ScriptForge.V_OBJECT Then GoTo Finally
168 If Not ScriptForge.SF_Session.HasUnoProperty(vEvent, &quot;Source&quot;) Then GoTo Finally
170 Try:
171 Set oEventSource = vEvent.Source
172 sSourceType = ScriptForge.SF_Session.UnoObjectType(oEventSource)
174 Set oDialog = Nothing
175 Select Case True
176 Case sSourceType = &quot;stardiv.Toolkit.UnoDialogControl&quot; &apos; A dialog
177 &apos; Search the dialog in the cache
178 Set oDialog = _FindDialogInCache(oEventSource)
179 bControl = False
180 Case Left(sSourceType, 16) = &quot;stardiv.Toolkit.&quot; &apos; A dialog control
181 Set oDialog = _FindDialogInCache(oEventSource.Context)
182 bControl = True
183 Case Else
184 End Select
186 If Not IsNull(oDialog) Then
187 If bControl Then Set oSource = oDialog.Controls(oEventSource.Model.Name) Else Set oSource = oDialog
188 End If
190 Finally:
191 Set _EventManager = oSource
192 Exit Function
193 End Function &apos; SFDialogs.SF_Register._EventManager
195 REM -----------------------------------------------------------------------------
196 Private Function _FindDialogInCache(ByRef poDialog As Object) As Object
197 &apos;&apos;&apos; Find the dialog based on its XUnoDialog
198 &apos;&apos;&apos; The dialog must not be terminated
199 &apos;&apos;&apos; Returns:
200 &apos;&apos;&apos; The corresponding Basic dialog part or Nothing
202 Dim oBasicDialog As Object &apos; Return value
203 Dim oCache As _DialogCache &apos; Entry in the cache
205 Set oBasicDialog = Nothing
207 Try:
208 For Each oCache In _SF_.SFDialogs
209 If EqualUnoObjects(poDialog, oCache.XUnoDialog) And Not oCache.Terminated Then
210 Set oBasicDialog = oCache.BasicDialog
211 Exit For
212 End If
213 Next oCache
215 Finally:
216 Set _FindDialogInCache = oBasicDialog
217 Exit Function
218 End Function &apos; SFDialogs.SF_Register._FindDialogInCache
220 REM -----------------------------------------------------------------------------
221 Public Function _NewDialog(Optional ByVal pvArgs As Variant) As Object
222 &apos;&apos;&apos; Create a new instance of the SF_Dialog class
223 &apos;&apos;&apos; Args:
224 &apos;&apos;&apos; Container: either &quot;GlobalScope&quot; or a WindowName. Default = the active window
225 &apos;&apos;&apos; see the definition of WindowName in the description of the UI service
226 &apos;&apos;&apos; Library: the name of the library hosting the dialog. Default = &quot;Standard&quot;
227 &apos;&apos;&apos; DialogName: The name of the dialog
228 &apos;&apos;&apos; Library and dialog names are case-sensitive
229 &apos;&apos;&apos; Context: When called from Python, the context must be provided : XSCRIPTCONTEXT
230 &apos;&apos;&apos; Returns: the instance or Nothing
232 Dim oDialog As Object &apos; Return value
233 Dim vContainer As Variant &apos; Alias of pvArgs(0)
234 Dim vLibrary As Variant &apos; Alias of pvArgs(1)
235 Dim vDialogName As Variant &apos; Alias of pvArgs(2)
236 Dim oLibraries As Object &apos; com.sun.star.comp.sfx2.DialogLibraryContainer
237 Dim vContext As Variant &apos; com.sun.star.uno.XComponentContext
238 Dim oDialogProvider As Object &apos; com.sun.star.io.XInputStreamProvider
239 Dim oEnum As Object &apos; com.sun.star.container.XEnumeration
240 Dim oComp As Object &apos; com.sun.star.lang.XComponent
241 Dim oDialogControl As Object &apos; com.sun.star.awt.XControl - stardiv.Toolkit.UnoDialogControl
242 Dim vWindow As Window &apos; A single component
243 Dim sScope As String &apos; &quot;application&quot; or &quot;document&quot;
244 Dim sURI As String &apos; URI of the targeted dialog
245 Dim oUi As Object &apos; &quot;UI&quot; service
246 Dim bFound As Boolean &apos; True if WindowName is found on the desktop
247 Const cstService = &quot;SFDialogs.Dialog&quot;
248 Const cstGlobal = &quot;GlobalScope&quot;
250 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
252 Check:
253 If IsMissing(pvArgs) Or IsEmpty(pvArgs) Then pvArgs = Array()
254 If Not IsArray(pvArgs) Then pvArgs = Array(pvArgs) &apos; Needed when _NewDialog called from _EventManager
255 If UBound(pvArgs) &gt;= 0 Then vContainer = pvArgs(0) Else vContainer = &quot;&quot;
256 If UBound(pvArgs) &gt;= 1 Then vLibrary = pvArgs(1)
257 If IsEmpty(vLibrary) Then vLibrary = &quot;Standard&quot;
258 If UBound(pvArgs) &gt;= 2 Then vDialogName = pvArgs(2) Else vDialogName = Empty &apos; Use Empty to force mandatory status
259 If Not ScriptForge.SF_Utils._Validate(vContainer, &quot;Container&quot;, Array(V_STRING, ScriptForge.V_OBJECT)) Then GoTo Finally
260 If Not ScriptForge.SF_Utils._Validate(vLibrary, &quot;Library&quot;, V_STRING) Then GoTo Finally
261 If Not ScriptForge.SF_Utils._Validate(vDialogName, &quot;DialogName&quot;, V_STRING) Then GoTo Finally
262 If UBound(pvArgs) &gt;= 3 Then vContext = pvArgs(3) Else vContext = Nothing
263 If Not ScriptForge.SF_Utils._Validate(vContext, &quot;DialogName&quot;, V_OBJECT) Then GoTo Finally
264 Set oDialog = Nothing
266 Try:
267 &apos; Determine the library container hosting the dialog
268 Set oUi = ScriptForge.SF_Register.CreateScriptService(&quot;UI&quot;)
269 Set oComp = Nothing
270 If VarType(vContainer) = V_STRING Then
271 bFound = ( UCase(vContainer) = UCase(cstGlobal) )
272 End If
273 If Not bFound Then
274 Select Case VarType(vContainer)
275 Case V_STRING
276 If Len(vContainer) &gt; 0 Then
277 bFound = False
278 Set oEnum = StarDesktop.Components().createEnumeration
279 Do While oEnum.hasMoreElements
280 Set oComp = oEnum.nextElement
281 vWindow = oUi._IdentifyWindow(oComp)
282 With vWindow
283 &apos; Does the current window match the argument ?
284 If (Len(.WindowFileName) &gt; 0 And .WindowFileName = ScriptForge.SF_FileSystem._ConvertToUrl(vContainer)) _
285 Or (Len(.WindowName) &gt; 0 And .WindowName = vContainer) _
286 Or (Len(.WindowTitle) &gt; 0 And .WindowTitle = vContainer) Then
287 bFound = True
288 Exit Do
289 End If
290 End With
291 Loop
292 Else
293 bFound = True
294 Set oComp = StarDesktop.CurrentComponent
295 vWindow = oUi._IdentifyWindow(oComp)
296 End If
297 Case V_OBJECT &apos; com.sun.star.lang.XComponent
298 bFound = True
299 vWindow = oUi._IdentifyWindow(vContainer)
300 Set oComp = vContainer
301 End Select
302 If Not bFound Then GoTo CatchNotFound
303 If Len(vWindow.DocumentType) = 0 Then GoTo CatchNotFound
304 End If
306 &apos; Determine the dialog provider
307 Select Case True
308 Case IsNull(vContext) And IsNull(oComp) &apos; Basic and GlobalScope
309 Set oDialogProvider = GetProcessServiceManager.createInstance(&quot;com.sun.star.awt.DialogProvider&quot;)
310 Case IsNull(vContext) And Not IsNull(oComp) &apos; Basic and Document
311 Set oDialogProvider = GetProcessServiceManager.createInstanceWithArguments(&quot;com.sun.star.awt.DialogProvider&quot;, Array(oComp))
312 Case Not IsNull(vContext) And IsNull(oComp) &apos; Python and GlobalScope
313 Set oDialogProvider = vContext.getServiceManager().createInstanceWithContext(&quot;com.sun.star.awt.DialogProvider&quot;, vContext)
314 Case Not IsNull(vContext) And Not IsNull(oComp) &apos; Python and Document
315 Set oDialogProvider = vContext.getServiceManager().createInstanceWithArguments(&quot;com.sun.star.awt.DialogProvider&quot;, Array(oComp))
316 End Select
318 &apos; Create the graphical interface
319 sScope = Iif(IsNull(oComp), &quot;application&quot;, &quot;document&quot;)
320 sURI = &quot;vnd.sun.star.script:&quot; &amp; vLibrary &amp; &quot;.&quot; &amp; vDialogName &amp; &quot;?location=&quot; &amp; sScope
321 On Local Error GoTo CatchNotFound
322 Set oDialogControl = oDialogProvider.createDialog(sURI)
324 &apos; Initialize the basic SF_Dialog instance to return to the user script
325 Set oDialog = New SF_Dialog
326 With oDialog
327 Set .[Me] = oDialog
328 If VarType(vContainer) = V_STRING Then ._Container = vContainer Else ._Container = vWindow.WindowName
329 ._Library = vLibrary
330 ._Name = vDialogName
331 Set ._DialogProvider = oDialogProvider
332 Set ._DialogControl = oDialogControl
333 ._Initialize()
334 End With
336 Finally:
337 Set _NewDialog = oDialog
338 Exit Function
339 Catch:
340 GoTo Finally
341 CatchNotFound:
342 ScriptForge.SF_Exception.RaiseFatal(DIALOGNOTFOUNDERROR, &quot;Service&quot;, cstService _
343 , &quot;Container&quot;, vContainer, &quot;Library&quot;, vLibrary, &quot;DialogName&quot;, vDialogName)
344 GoTo Finally
345 End Function &apos; SFDialogs.SF_Register._NewDialog
347 REM ============================================== END OF SFDIALOGS.SF_REGISTER
348 </script:module>