CommandBarControl.xba 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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="CommandBarControl" script:language="StarBasic">
  4. REM =======================================================================================================================
  5. REM === The Access2Base library is a part of the LibreOffice project. ===
  6. REM === Full documentation is available on http://www.access2base.com ===
  7. REM =======================================================================================================================
  8. Option Compatible
  9. Option ClassModule
  10. Option Explicit
  11. REM -----------------------------------------------------------------------------------------------------------------------
  12. REM --- CLASS ROOT FIELDS ---
  13. REM -----------------------------------------------------------------------------------------------------------------------
  14. Private _Type As String &apos; Must be COMMANDBARCONTROL
  15. Private _This As Object &apos; Workaround for absence of This builtin function
  16. Private _Parent As Object
  17. Private _InternalIndex As Integer &apos; Index in toolbar including separators
  18. Private _Index As Integer &apos; Index in collection, starting at 1 !!
  19. Private _ControlType As Integer &apos; 1 of the msoControl* constants
  20. Private _ParentCommandBarName As String
  21. Private _ParentCommandBar As Object &apos; com.sun.star.ui.XUIElement
  22. Private _ParentBuiltin As Boolean
  23. Private _Element As Variant
  24. Private _BeginGroup As Boolean
  25. REM -----------------------------------------------------------------------------------------------------------------------
  26. REM --- CONSTRUCTORS / DESTRUCTORS ---
  27. REM -----------------------------------------------------------------------------------------------------------------------
  28. Private Sub Class_Initialize()
  29. _Type = OBJCOMMANDBARCONTROL
  30. Set _This = Nothing
  31. Set _Parent = Nothing
  32. _Index = -1
  33. _ParentCommandBarName = &quot;&quot;
  34. Set _ParentCommandBar = Nothing
  35. _ParentBuiltin = False
  36. _Element = Array()
  37. _BeginGroup = False
  38. End Sub &apos; Constructor
  39. REM -----------------------------------------------------------------------------------------------------------------------
  40. Private Sub Class_Terminate()
  41. On Local Error Resume Next
  42. Call Class_Initialize()
  43. End Sub &apos; Destructor
  44. REM -----------------------------------------------------------------------------------------------------------------------
  45. Public Sub Dispose()
  46. Call Class_Terminate()
  47. End Sub &apos; Explicit destructor
  48. REM -----------------------------------------------------------------------------------------------------------------------
  49. REM --- CLASS GET/LET/SET PROPERTIES ---
  50. REM -----------------------------------------------------------------------------------------------------------------------
  51. REM -----------------------------------------------------------------------------------------------------------------------
  52. Property Get BeginGroup() As Boolean
  53. BeginGroup = _PropertyGet(&quot;BeginGroup&quot;)
  54. End Property &apos; BeginGroup (get)
  55. REM -----------------------------------------------------------------------------------------------------------------------
  56. Property Get BuiltIn() As Boolean
  57. BuiltIn = _PropertyGet(&quot;BuiltIn&quot;)
  58. End Property &apos; BuiltIn (get)
  59. REM -----------------------------------------------------------------------------------------------------------------------
  60. Property Get Caption() As Variant
  61. Caption = _PropertyGet(&quot;Caption&quot;)
  62. End Property &apos; Caption (get)
  63. Property Let Caption(ByVal pvValue As Variant)
  64. Call _PropertySet(&quot;Caption&quot;, pvValue)
  65. End Property &apos; Caption (set)
  66. REM -----------------------------------------------------------------------------------------------------------------------
  67. Property Get Index() As Integer
  68. Index = _PropertyGet(&quot;Index&quot;)
  69. End Property &apos; Index (get)
  70. REM -----------------------------------------------------------------------------------------------------------------------
  71. Property Get ObjectType() As String
  72. ObjectType = _PropertyGet(&quot;ObjectType&quot;)
  73. End Property &apos; ObjectType (get)
  74. REM -----------------------------------------------------------------------------------------------------------------------
  75. Property Get OnAction() As Variant
  76. OnAction = _PropertyGet(&quot;OnAction&quot;)
  77. End Property &apos; OnAction (get)
  78. Property Let OnAction(ByVal pvValue As Variant)
  79. Call _PropertySet(&quot;OnAction&quot;, pvValue)
  80. End Property &apos; OnAction (set)
  81. REM -----------------------------------------------------------------------------------------------------------------------
  82. Property Get Parent() As Object
  83. Parent = _PropertyGet(&quot;Parent&quot;)
  84. End Property &apos; Parent (get)
  85. REM -----------------------------------------------------------------------------------------------------------------------
  86. Public Function Properties(ByVal Optional pvIndex As Variant) As Variant
  87. &apos; Return
  88. &apos; a Collection object if pvIndex absent
  89. &apos; a Property object otherwise
  90. Dim vProperty As Variant, vPropertiesList() As Variant, sObject As String
  91. vPropertiesList = _PropertiesList()
  92. sObject = Utils._PCase(_Type)
  93. If IsMissing(pvIndex) Then
  94. vProperty = PropertiesGet._Properties(sObject, _This, vPropertiesList)
  95. Else
  96. vProperty = PropertiesGet._Properties(sObject, _This, vPropertiesList, pvIndex)
  97. vProperty._Value = _PropertyGet(vPropertiesList(pvIndex))
  98. End If
  99. Exit_Function:
  100. Set Properties = vProperty
  101. Exit Function
  102. End Function &apos; Properties
  103. REM -----------------------------------------------------------------------------------------------------------------------
  104. Property Get TooltipText() As Variant
  105. TooltipText = _PropertyGet(&quot;TooltipText&quot;)
  106. End Property &apos; TooltipText (get)
  107. Property Let TooltipText(ByVal pvValue As Variant)
  108. Call _PropertySet(&quot;TooltipText&quot;, pvValue)
  109. End Property &apos; TooltipText (set)
  110. REM -----------------------------------------------------------------------------------------------------------------------
  111. Public Function pType() As Integer
  112. pType = _PropertyGet(&quot;Type&quot;)
  113. End Function &apos; Type (get)
  114. REM -----------------------------------------------------------------------------------------------------------------------
  115. Property Get Visible() As Variant
  116. Visible = _PropertyGet(&quot;Visible&quot;)
  117. End Property &apos; Visible (get)
  118. Property Let Visible(ByVal pvValue As Variant)
  119. Call _PropertySet(&quot;Visible&quot;, pvValue)
  120. End Property &apos; Visible (set)
  121. REM -----------------------------------------------------------------------------------------------------------------------
  122. REM --- CLASS METHODS ---
  123. REM -----------------------------------------------------------------------------------------------------------------------
  124. REM -----------------------------------------------------------------------------------------------------------------------
  125. Public Function Execute()
  126. &apos; Execute the command stored in a toolbar button
  127. If _ErrorHandler() Then On Local Error Goto Error_Function
  128. Const cstThisSub = &quot;CommandBarControl.Execute&quot;
  129. Utils._SetCalledSub(cstThisSub)
  130. Dim sExecute As String
  131. Execute = True
  132. sExecute = _GetPropertyValue(_Element, &quot;CommandURL&quot;, &quot;&quot;)
  133. Select Case True
  134. Case sExecute = &quot;&quot; : Execute = False
  135. Case _IsLeft(sExecute, &quot;.uno:&quot;)
  136. Execute = DoCmd.RunCommand(sExecute)
  137. Case _IsLeft(sExecute, &quot;vnd.sun.star.script:&quot;)
  138. Execute = Utils._RunScript(sExecute, Array(Nothing))
  139. Case Else
  140. End Select
  141. Exit_Function:
  142. Utils._ResetCalledSub(cstThisSub)
  143. Exit Function
  144. Error_Function:
  145. TraceError(TRACEABORT, Err, cstThisSub, Erl)
  146. Execute = False
  147. GoTo Exit_Function
  148. End Function &apos; Execute V1.3.0
  149. REM -----------------------------------------------------------------------------------------------------------------------
  150. Public Function getProperty(Optional ByVal pvProperty As Variant) As Variant
  151. &apos; Return property value of psProperty property name
  152. Utils._SetCalledSub(&quot;CommandBarControl.getProperty&quot;)
  153. If IsMissing(pvProperty) Then Call _TraceArguments()
  154. getProperty = _PropertyGet(pvProperty)
  155. Utils._ResetCalledSub(&quot;CommandBar.getProperty&quot;)
  156. End Function &apos; getProperty
  157. REM -----------------------------------------------------------------------------------------------------------------------
  158. Public Function hasProperty(ByVal Optional pvProperty As Variant) As Boolean
  159. &apos; Return True if object has a valid property called pvProperty (case-insensitive comparison !)
  160. If IsMissing(pvProperty) Then hasProperty = PropertiesGet._hasProperty(_Type, _PropertiesList()) Else hasProperty = PropertiesGet._hasProperty(_Type, _PropertiesList(), pvProperty)
  161. Exit Function
  162. End Function &apos; hasProperty
  163. REM -----------------------------------------------------------------------------------------------------------------------
  164. REM --- PRIVATE FUNCTIONS ---
  165. REM -----------------------------------------------------------------------------------------------------------------------
  166. REM -----------------------------------------------------------------------------------------------------------------------
  167. Private Function _PropertiesList() As Variant
  168. _PropertiesList = Array(&quot;BeginGroup&quot;, &quot;BuiltIn&quot;, &quot;Caption&quot;, &quot;Index&quot; _
  169. , &quot;ObjectType&quot;, &quot;OnAction&quot;, &quot;Parent&quot; _
  170. , &quot;TooltipText&quot;, &quot;Type&quot;, &quot;Visible&quot; _
  171. )
  172. End Function &apos; _PropertiesList
  173. REM -----------------------------------------------------------------------------------------------------------------------
  174. Private Function _PropertyGet(ByVal psProperty As String) As Variant
  175. &apos; Return property value of the psProperty property name
  176. If _ErrorHandler() Then On Local Error Goto Error_Function
  177. Dim cstThisSub As String
  178. cstThisSub = &quot;CommandBarControl.get&quot; &amp; psProperty
  179. Utils._SetCalledSub(cstThisSub)
  180. _PropertyGet = Null
  181. Dim oLayout As Object, iElementIndex As Integer
  182. Dim sValue As String
  183. Const cstUnoPrefix = &quot;.uno:&quot;
  184. Select Case UCase(psProperty)
  185. Case UCase(&quot;BeginGroup&quot;)
  186. _PropertyGet = _BeginGroup
  187. Case UCase(&quot;BuiltIn&quot;)
  188. sValue = _GetPropertyValue(_Element, &quot;CommandURL&quot;, &quot;&quot;)
  189. _PropertyGet = ( _IsLeft(sValue, cstUnoPrefix) )
  190. Case UCase(&quot;Caption&quot;)
  191. _PropertyGet = _GetPropertyValue(_Element, &quot;Label&quot;, &quot;&quot;)
  192. Case UCase(&quot;Index&quot;)
  193. _PropertyGet = _Index
  194. Case UCase(&quot;ObjectType&quot;)
  195. _PropertyGet = _Type
  196. Case UCase(&quot;OnAction&quot;)
  197. _PropertyGet = _GetPropertyValue(_Element, &quot;CommandURL&quot;, &quot;&quot;)
  198. Case UCase(&quot;Parent&quot;)
  199. Set _PropertyGet = _Parent
  200. Case UCase(&quot;TooltipText&quot;)
  201. sValue = _GetPropertyValue(_Element, &quot;Tooltip&quot;, &quot;&quot;)
  202. If sValue &lt;&gt; &quot;&quot; Then _PropertyGet = sValue Else _PropertyGet = _GetPropertyValue(_Element, &quot;Label&quot;, &quot;&quot;)
  203. Case UCase(&quot;Type&quot;)
  204. _PropertyGet = msoControlButton
  205. Case UCase(&quot;Visible&quot;)
  206. _PropertyGet = _GetPropertyValue(_Element, &quot;IsVisible&quot;, &quot;&quot;)
  207. Case Else
  208. Goto Trace_Error
  209. End Select
  210. Exit_Function:
  211. Utils._ResetCalledSub(cstThisSub)
  212. Exit Function
  213. Trace_Error:
  214. TraceError(TRACEFATAL, ERRPROPERTY, Utils._CalledSub(), 0, 1, psProperty)
  215. _PropertyGet = Nothing
  216. Goto Exit_Function
  217. Error_Function:
  218. TraceError(TRACEABORT, Err, Utils._CalledSub(), Erl)
  219. _PropertyGet = Nothing
  220. GoTo Exit_Function
  221. End Function &apos; _PropertyGet
  222. REM -----------------------------------------------------------------------------------------------------------------------
  223. Private Function _PropertySet(ByVal psProperty As String, ByVal pvValue As Variant) As Boolean
  224. &apos; Return True if property setting OK
  225. If _ErrorHandler() Then On Local Error Goto Error_Function
  226. Dim cstThisSub As String
  227. cstThisSub = &quot;CommandBarControl.set&quot; &amp; psProperty
  228. Utils._SetCalledSub(cstThisSub)
  229. _PropertySet = True
  230. Dim iArgNr As Integer
  231. Dim oSettings As Object, sValue As String
  232. Select Case UCase(_A2B_.CalledSub)
  233. Case UCase(&quot;setProperty&quot;) : iArgNr = 3
  234. Case UCase(&quot;CommandBar.setProperty&quot;) : iArgNr = 2
  235. Case UCase(cstThisSub) : iArgNr = 1
  236. End Select
  237. If Not hasProperty(psProperty) Then Goto Trace_Error
  238. If _ParentBuiltin Then Goto Trace_Error &apos; Modifications of individual controls forbidden for builtin toolbars (design choice)
  239. Const cstUnoPrefix = &quot;.uno:&quot;
  240. Const cstScript = &quot;vnd.sun.star.script:&quot;
  241. Set oSettings = _ParentCommandBar.getSettings(True)
  242. Select Case UCase(psProperty)
  243. Case UCase(&quot;OnAction&quot;)
  244. If Not Utils._CheckArgument(pvValue, iArgNr, _AddNumeric(vbString), , False) Then Goto Trace_Error_Value
  245. Select Case VarType(pvValue)
  246. Case vbString
  247. If _IsLeft(pvValue, cstUnoPrefix) Then
  248. sValue = pvValue
  249. ElseIf _IsLeft(pvValue, cstScript) Then
  250. sValue = pvValue
  251. Else
  252. sValue = DoCmd.RunCommand(pvValue, True)
  253. End If
  254. Case Else &apos; Numeric
  255. sValue = DoCmd.RunCommand(pvValue, True)
  256. End Select
  257. _SetPropertyValue(_Element, &quot;CommandURL&quot;, sValue)
  258. Case UCase(&quot;TooltipText&quot;)
  259. If Not Utils._CheckArgument(pvValue, iArgNr, vbString, , False) Then Goto Trace_Error_Value
  260. _SetPropertyValue(_Element, &quot;Tooltip&quot;, pvValue)
  261. Case UCase(&quot;Visible&quot;)
  262. If Not Utils._CheckArgument(pvValue, iArgNr, vbBoolean, , False) Then Goto Trace_Error_Value
  263. _SetPropertyValue(_Element, &quot;IsVisible&quot;, pvValue)
  264. Case Else
  265. Goto Trace_Error
  266. End Select
  267. oSettings.replaceByIndex(_InternalIndex, _Element)
  268. _ParentCommandBar.setSettings(oSettings)
  269. Exit_Function:
  270. Utils._ResetCalledSub(cstThisSub)
  271. Exit Function
  272. Trace_Error:
  273. TraceError(TRACEFATAL, ERRPROPERTY, Utils._CalledSub(), 0, , psProperty)
  274. _PropertySet = False
  275. Goto Exit_Function
  276. Trace_Error_Value:
  277. TraceError(TRACEFATAL, ERRPROPERTYVALUE, Utils._CalledSub(), 0, 1, Array(pvValue, psProperty))
  278. _PropertySet = False
  279. Goto Exit_Function
  280. Error_Function:
  281. TraceError(TRACEABORT, Err, cstThisSub, Erl)
  282. _PropertySet = False
  283. GoTo Exit_Function
  284. End Function &apos; _PropertySet
  285. </script:module>