REM ======================================================================================================================= REM === The ScriptForge library and its associated libraries are part of the LibreOffice project. === REM === The SFDocuments library is one of the associated libraries. === REM === Full documentation is available on https://help.libreoffice.org/ === REM ======================================================================================================================= Option Compatible Option Explicit ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''' SF_Register ''' =========== ''' The ScriptForge framework includes ''' the master ScriptForge library ''' a number of "associated" libraries SF* ''' any user/contributor extension wanting to fit into the framework ''' ''' The main methods in this module allow the current library to cling to ScriptForge ''' - RegisterScriptServices ''' Register the list of services implemented by the current library ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' REM ================================================================== EXCEPTIONS REM ============================================================== PUBLIC METHODS REM ----------------------------------------------------------------------------- Public Sub RegisterScriptServices() As Variant ''' Register into ScriptForge the list of the services implemented by the current library ''' Each library pertaining to the framework must implement its own version of this method ''' ''' It consists in successive calls to the RegisterService() and RegisterEventManager() methods ''' with 2 arguments: ''' ServiceName: the name of the service as a case-insensitive string ''' ServiceReference: the reference as an object ''' If the reference refers to a module, then return the module as an object: ''' GlobalScope.Library.Module ''' If the reference is a class instance, then return a string referring to the method ''' containing the New statement creating the instance ''' "libraryname.modulename.function" With GlobalScope.ScriptForge.SF_Services .RegisterService("Document", "SFDocuments.SF_Register._NewDocument") ' Reference to the function initializing the service .RegisterService("Calc", "SFDocuments.SF_Register._NewDocument") ' Same references, distinction is made inside the function .RegisterService("Base", "SFDocuments.SF_Register._NewDocument") ' Same references, distinction is made inside the function .RegisterEventManager("DocumentEvent", "SFDocuments.SF_Register._EventManager") ' Reference to the events manager 'TODO End With End Sub ' SFDocuments.SF_Register.RegisterScriptServices REM =========================================================== PRIVATE FUNCTIONS REM ----------------------------------------------------------------------------- Public Function _EventManager(Optional ByRef pvArgs As Variant) As Object ''' Returns a Document or Calc object corresponding with the active component ''' which triggered the event in argument ''' This method should be triggered only thru the invocation of CreateScriptService ''' Args: ''' pvEvent: com.sun.star.document.DocumentEvent ''' Returns: ''' the output of a Document, Calc, ... service or Nothing ''' Example: ''' Sub TriggeredByEvent(ByRef poEvent As Object) ''' Dim oDoc As Object ''' Set oDoc = CreateScriptService("SFDocuments.DocumentEvent", poEvent) ''' If Not IsNull(oDoc) Then ''' ' ... (a valid document has been identified) ''' End Sub Dim oSource As Object ' Return value Dim vEvent As Variant ' Alias of pvArgs(0) ' Never abort while an event is processed If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Finally Set oSource = Nothing Check: If IsMissing(pvArgs) Or IsEmpty(pvArgs) Then pvArgs = Array() If UBound(pvArgs) >= 0 Then vEvent = pvArgs(0) Else Set vEvent = Empty If VarType(vEvent) <> ScriptForge.V_OBJECT Then GoTo Finally Try: If ScriptForge.SF_Session.UnoObjectType(vEvent) = "com.sun.star.document.DocumentEvent" Then If ScriptForge.SF_Session.UnoObjectType(vEvent.Source) = "SwXTextDocument" Then Set oSource = SF_Register._NewDocument(vEvent.Source) End If End If Finally: Set _EventManager = oSource Exit Function End Function ' SFDocuments.SF_Documents._EventManager REM ----------------------------------------------------------------------------- Public Function _NewDocument(Optional ByVal pvArgs As Variant) As Object ''' Create a new instance of the (super) SF_Document class or of one of its subclasses (SF_Calc, ...) ' Args: ''' WindowName: see the definition of WindowName in the description of the UI service ''' If absent, the document is presumed to be in the active window ''' If WindowName is an object, it must be a component ''' (com.sun.star.lang.XComponent or com.sun.star.comp.dba.ODatabaseDocument) ''' Returns: the instance or Nothing Dim oDocument As Object ' Return value Dim oSuperDocument As Object ' Companion superclass document Dim vWindowName As Variant ' Alias of pvArgs(0) Dim oEnum As Object ' com.sun.star.container.XEnumeration Dim oComp As Object ' com.sun.star.lang.XComponent Dim vWindow As Window ' A single component Dim oUi As Object ' "UI" service Dim bFound As Boolean ' True if the document is found on the desktop If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch Check: If IsMissing(pvArgs) Or IsEmpty(pvArgs) Then pvArgs = Array() If Not IsArray(pvArgs) Then pvArgs = Array(pvArgs) ' Needed when _NewDocument called from _EventManager If UBound(pvArgs) >= 0 Then vWindowName = pvArgs(0) Else vWindowName = "" If Not ScriptForge.SF_Utils._Validate(vWindowName, "WindowName", Array(V_STRING, ScriptForge.V_OBJECT)) Then GoTo Finally Set oDocument = Nothing Try: Set oUi = ScriptForge.SF_Register.CreateScriptService("UI") Select Case VarType(vWindowName) Case V_STRING If Len(vWindowName) > 0 Then bFound = False Set oEnum = StarDesktop.Components().createEnumeration Do While oEnum.hasMoreElements Set oComp = oEnum.nextElement vWindow = oUi._IdentifyWindow(oComp) With vWindow ' Does the current window match the argument ? If (Len(.WindowFileName) > 0 And .WindowFileName = ScriptForge.SF_FileSystem._ConvertToUrl(vWindowName)) _ Or (Len(.WindowName) > 0 And .WindowName = vWindowName) _ Or (Len(.WindowTitle) > 0 And .WindowTitle = vWindowName) Then bFound = True Exit Do End If End With Loop Else bFound = True vWindow = oUi._IdentifyWindow(StarDesktop.CurrentComponent) End If Case ScriptForge.V_OBJECT ' com.sun.star.lang.XComponent bFound = True vWindow = oUi._IdentifyWindow(vWindowName) End Select If bFound And Not IsNull(vWindow.Frame) And Len(vWindow.DocumentType) > 0 Then ' Create the right subclass and associate to it a new instance of the superclass Select Case vWindow.DocumentType Case "Base" Set oDocument = New SF_Base Set oSuperDocument = New SF_Document Set oDocument.[_Super] = oSuperDocument ' Now both super and subclass are twinned Case "Calc" Set oDocument = New SF_Calc Set oSuperDocument = New SF_Document Set oDocument.[_Super] = oSuperDocument ' Now both super and subclass are twinned Case Else ' Only superclass Set oDocument = New SF_Document Set oSuperDocument = oDocument End Select With oDocument ' Initialize attributes of subclass Set .[Me] = oDocument Set ._Component = vWindow.Component ' Initialize specific attributes Select Case vWindow.DocumentType Case "Base" Set ._DataSource = ._Component.DataSource Case Else End Select End With With oSuperDocument ' Initialize attributes of superclass Set .[Me] = oSuperDocument Set ._Component = vWindow.Component Set ._Frame = vWindow.Frame ._WindowName = vWindow.WindowName ._WindowTitle = vWindow.WindowTitle ._WindowFileName = vWindow.WindowFileName ._DocumentType = vWindow.DocumentType End With End If Finally: Set _NewDocument = oDocument Exit Function Catch: GoTo Finally End Function ' SFDocuments.SF_Register._NewDocument REM ============================================== END OF SFDOCUMENTS.SF_REGISTER