SF_Register.xba 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 SFWidgets library is one of the associated libraries. ===
  6. REM === Full documentation is available on https://help.libreoffice.org/ ===
  7. REM =======================================================================================================================
  8. Option Compatible
  9. Option Explicit
  10. &apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
  11. &apos;&apos;&apos; SF_Register
  12. &apos;&apos;&apos; ===========
  13. &apos;&apos;&apos; The ScriptForge framework includes
  14. &apos;&apos;&apos; the master ScriptForge library
  15. &apos;&apos;&apos; a number of &quot;associated&quot; libraries SF*
  16. &apos;&apos;&apos; any user/contributor extension wanting to fit into the framework
  17. &apos;&apos;&apos;
  18. &apos;&apos;&apos; The main methods in this module allow the current library to cling to ScriptForge
  19. &apos;&apos;&apos; - RegisterScriptServices
  20. &apos;&apos;&apos; Register the list of services implemented by the current library
  21. &apos;&apos;&apos; - _NewMenu
  22. &apos;&apos;&apos; Create a new menu service instance.
  23. &apos;&apos;&apos; Called from SFDocuments services with CreateMenu()
  24. &apos;&apos;&apos; - _NewPopupMenu
  25. &apos;&apos;&apos; Create a new popup menu service instance.
  26. &apos;&apos;&apos; Called from CreateScriptService()
  27. &apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
  28. REM ================================================================== EXCEPTIONS
  29. REM ================================================================= DEFINITIONS
  30. REM ============================================================== PUBLIC METHODS
  31. REM -----------------------------------------------------------------------------
  32. Public Sub RegisterScriptServices() As Variant
  33. &apos;&apos;&apos; Register into ScriptForge the list of the services implemented by the current library
  34. &apos;&apos;&apos; Each library pertaining to the framework must implement its own version of this method
  35. &apos;&apos;&apos;
  36. &apos;&apos;&apos; It consists in successive calls to the RegisterService() and RegisterEventManager() methods
  37. &apos;&apos;&apos; with 2 arguments:
  38. &apos;&apos;&apos; ServiceName: the name of the service as a case-insensitive string
  39. &apos;&apos;&apos; ServiceReference: the reference as an object
  40. &apos;&apos;&apos; If the reference refers to a module, then return the module as an object:
  41. &apos;&apos;&apos; GlobalScope.Library.Module
  42. &apos;&apos;&apos; If the reference is a class instance, then return a string referring to the method
  43. &apos;&apos;&apos; containing the New statement creating the instance
  44. &apos;&apos;&apos; &quot;libraryname.modulename.function&quot;
  45. With GlobalScope.ScriptForge.SF_Services
  46. .RegisterService(&quot;Menu&quot;, &quot;SFWidgets.SF_Register._NewMenu&quot;) &apos; Reference to the function initializing the service
  47. .RegisterService(&quot;PopupMenu&quot;, &quot;SFWidgets.SF_Register._NewPopupMenu&quot;) &apos; id.
  48. End With
  49. End Sub &apos; SFWidgets.SF_Register.RegisterScriptServices
  50. REM =========================================================== PRIVATE FUNCTIONS
  51. REM -----------------------------------------------------------------------------
  52. Public Function _NewMenu(Optional ByVal pvArgs As Variant) As Object
  53. &apos;&apos;&apos; Create a new instance of the SF_Menu class
  54. &apos;&apos;&apos; [called internally from SFDocuments.Document.CreateMenu() ONLY]
  55. &apos;&apos;&apos; Args:
  56. &apos;&apos;&apos; Component: the com.sun.star.lang.XComponent where to find the menubar to plug the new menu in
  57. &apos;&apos;&apos; Header: the name/header of the menu
  58. &apos;&apos;&apos; Before: the place where to put the new menu on the menubar (string or number &gt;= 1)
  59. &apos;&apos;&apos; When not found =&gt; last position
  60. &apos;&apos;&apos; SubmenuChar: the delimiter used in menu trees. Default = &quot;&gt;&quot;
  61. &apos;&apos;&apos; Returns: the instance or Nothing
  62. Dim oMenu As Object &apos; Return value
  63. Dim oComponent As Object &apos; The document or formdocument&apos;s component - com.sun.star.lang.XComponent
  64. Dim sHeader As String &apos; Menu header
  65. Dim sBefore As String &apos; Position of menu as a string
  66. Dim iBefore As Integer &apos; as a number
  67. Dim sSubmenuChar As String &apos; Delimiter in menu trees
  68. If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
  69. Set oMenu = Nothing
  70. Check:
  71. &apos; Types and number of arguments are not checked because internal call only
  72. Set oComponent = pvArgs(0)
  73. sHeader = pvArgs(1)
  74. Select Case VarType(pvArgs(2))
  75. Case V_STRING : sBefore = pvArgs(2)
  76. iBefore = 0
  77. Case Else : sBefore = &quot;&quot;
  78. iBefore = pvArgs(2)
  79. End Select
  80. sSubmenuChar = pvArgs(3)
  81. Try:
  82. If Not IsNull(oComponent) Then
  83. Set oMenu = New SF_Menu
  84. With oMenu
  85. Set .[Me] = oMenu
  86. ._Initialize(oComponent, sHeader, sBefore, iBefore, sSubmenuChar)
  87. End With
  88. End If
  89. Finally:
  90. Set _NewMenu = oMenu
  91. Exit Function
  92. Catch:
  93. GoTo Finally
  94. End Function &apos; SFWidgets.SF_Register._NewMenu
  95. REM -----------------------------------------------------------------------------
  96. Public Function _NewPopupMenu(Optional ByVal pvArgs As Variant) As Object
  97. &apos;&apos;&apos; Create a new instance of the SF_PopupMenu class
  98. &apos;&apos;&apos; Args:
  99. &apos;&apos;&apos; Event: a mouse event
  100. &apos;&apos;&apos; If the event has no source or is not a mouse event, the menu is displayed above the actual window
  101. &apos;&apos;&apos; X, Y: forced coordinates
  102. &apos;&apos;&apos; SubmenuChar: Delimiter used in menu trees
  103. &apos;&apos;&apos; Returns: the instance or Nothing
  104. Dim oMenu As Object &apos; Return value
  105. Dim Event As Variant &apos; Mouse event
  106. Dim X As Long &apos; Mouse click coordinates
  107. Dim Y As Long
  108. Dim SubmenuChar As String &apos; Delimiter in menu trees
  109. Dim vUno As Variant &apos; UNO type split into an array
  110. Dim sEventType As String &apos; Event type, must be &quot;MouseEvent&quot;
  111. Dim oControl As Object &apos; The dialog or form control view which triggered the event
  112. Dim oWindow As Object &apos; ui.Window type
  113. Dim oSession As Object : Set oSession = ScriptForge.SF_Services.CreateScriptService(&quot;ScriptForge.Session&quot;)
  114. Dim oUi As Object : Set oUi = ScriptForge.SF_Services.CreateScriptService(&quot;ScriptForge.UI&quot;)
  115. If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
  116. Check:
  117. &apos; Check and get arguments, their number may vary
  118. If IsMissing(pvArgs) Or IsEmpty(pvArgs) Then pvArgs = Array()
  119. If Not IsArray(pvArgs) Then pvArgs = Array(pvArgs)
  120. If UBound(pvArgs) &gt;= 0 Then Event = pvArgs(0) Else Event = Nothing
  121. If IsEmpty(Event) Then Event = Nothing
  122. If UBound(pvArgs) &gt;= 1 Then X = pvArgs(1) Else X = 0
  123. If UBound(pvArgs) &gt;= 2 Then Y = pvArgs(2) Else Y = 0
  124. If UBound(pvArgs) &gt;= 3 Then SubmenuChar = pvArgs(3) Else SubmenuChar = &quot;&quot;
  125. If Not ScriptForge.SF_Utils._Validate(Event, &quot;Event&quot;, ScriptForge.V_OBJECT) Then GoTo Finally
  126. If Not ScriptForge.SF_Utils._Validate(X, &quot;X&quot;, ScriptForge.V_NUMERIC) Then GoTo Finally
  127. If Not ScriptForge.SF_Utils._Validate(Y, &quot;Y&quot;, ScriptForge.V_NUMERIC) Then GoTo Finally
  128. If Not ScriptForge.SF_Utils._Validate(SubmenuChar, &quot;SubmenuChar&quot;, V_STRING) Then GoTo Finally
  129. Set oMenu = Nothing
  130. Try:
  131. &apos; Find and identify the control that triggered the popup menu
  132. Set oControl = Nothing
  133. If Not IsNull(Event) Then
  134. &apos; Determine the X, Y coordinates
  135. vUno = Split(oSession.UnoObjectType(Event), &quot;.&quot;)
  136. sEventType = vUno(UBound(vUno))
  137. If UCase(sEventType) = &quot;MOUSEEVENT&quot; Then
  138. X = Event.X
  139. Y = Event.Y
  140. &apos; Determine the window peer target
  141. If oSession.HasUnoProperty(Event, &quot;Source&quot;) Then Set oControl = Event.Source.Peer
  142. End If
  143. End If
  144. &apos; If not a mouse event, if no control, find what can be decent alternatives: (a menu header in) the actual window
  145. If IsNull(oControl) Then
  146. Set oWindow = oUi._IdentifyWindow(StarDesktop.getCurrentComponent()) &apos; A menu has been clicked necessarily in the current window
  147. With oWindow
  148. If Not IsNull(.Frame) Then Set oControl = .Frame.getContainerWindow()
  149. End With
  150. End If
  151. If Not IsNull(oControl) Then
  152. Set oMenu = New SF_PopupMenu
  153. With oMenu
  154. Set .[Me] = oMenu
  155. ._Initialize(oControl, X, Y, SubmenuChar)
  156. End With
  157. Else
  158. Set oMenu = Nothing
  159. End If
  160. Finally:
  161. Set _NewPopupMenu = oMenu
  162. Exit Function
  163. Catch:
  164. GoTo Finally
  165. End Function &apos; SFWidgets.SF_Register._NewPopupMenu
  166. REM ============================================== END OF SFWidgets.SF_REGISTER
  167. </script:module>