CommandBar.xba 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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="CommandBar" 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 COMMANDBAR
  15. Private _This As Object &apos; Workaround for absence of This builtin function
  16. Private _Parent As Object
  17. Private _Name As String
  18. Private _ResourceURL As String
  19. Private _Window As Object &apos; com.sun.star.frame.XFrame
  20. Private _Module As String
  21. Private _Toolbar As Object
  22. Private _BarBuiltin As Integer &apos; 1 = builtin, 2 = custom stored in LO/AOO (Base), 3 = custom stored in document (Form)
  23. Private _BarType As Integer &apos; See msoBarTypeXxx constants
  24. REM -----------------------------------------------------------------------------------------------------------------------
  25. REM --- CONSTRUCTORS / DESTRUCTORS ---
  26. REM -----------------------------------------------------------------------------------------------------------------------
  27. Private Sub Class_Initialize()
  28. _Type = OBJCOMMANDBAR
  29. Set _This = Nothing
  30. Set _Parent = Nothing
  31. _Name = &quot;&quot;
  32. _ResourceURL = &quot;&quot;
  33. Set _Window = Nothing
  34. _Module = &quot;&quot;
  35. Set _Toolbar = Nothing
  36. _BarBuiltin = 0
  37. _BarType = -1
  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 BuiltIn() As Boolean
  53. BuiltIn = _PropertyGet(&quot;BuiltIn&quot;)
  54. End Property &apos; BuiltIn (get)
  55. REM -----------------------------------------------------------------------------------------------------------------------
  56. Property Get Name() As String
  57. Name = _PropertyGet(&quot;Name&quot;)
  58. End Property &apos; Name (get)
  59. Public Function pName() As String &apos; For compatibility with &lt; V0.9.0
  60. pName = _PropertyGet(&quot;Name&quot;)
  61. End Function &apos; pName (get)
  62. REM -----------------------------------------------------------------------------------------------------------------------
  63. Property Get ObjectType() As String
  64. ObjectType = _PropertyGet(&quot;ObjectType&quot;)
  65. End Property &apos; ObjectType (get)
  66. REM -----------------------------------------------------------------------------------------------------------------------
  67. Public Function Parent() As Object
  68. Parent = _Parent
  69. End Function &apos; Parent (get) V6.4.0
  70. REM -----------------------------------------------------------------------------------------------------------------------
  71. Public Function Properties(ByVal Optional pvIndex As Variant) As Variant
  72. &apos; Return
  73. &apos; a Collection object if pvIndex absent
  74. &apos; a Property object otherwise
  75. Dim vProperty As Variant, vPropertiesList() As Variant, sObject As String
  76. vPropertiesList = _PropertiesList()
  77. sObject = Utils._PCase(_Type)
  78. If IsMissing(pvIndex) Then
  79. vProperty = PropertiesGet._Properties(sObject, _This, vPropertiesList)
  80. Else
  81. vProperty = PropertiesGet._Properties(sObject, _This, vPropertiesList, pvIndex)
  82. vProperty._Value = _PropertyGet(vPropertiesList(pvIndex))
  83. End If
  84. Exit_Function:
  85. Set Properties = vProperty
  86. Exit Function
  87. End Function &apos; Properties
  88. REM -----------------------------------------------------------------------------------------------------------------------
  89. Property Get Visible() As Variant
  90. Visible = _PropertyGet(&quot;Visible&quot;)
  91. End Property &apos; Visible (get)
  92. Property Let Visible(ByVal pvValue As Variant)
  93. Call _PropertySet(&quot;Visible&quot;, pvValue)
  94. End Property &apos; Visible (set)
  95. REM -----------------------------------------------------------------------------------------------------------------------
  96. REM --- CLASS METHODS ---
  97. REM -----------------------------------------------------------------------------------------------------------------------
  98. REM -----------------------------------------------------------------------------------------------------------------------
  99. Public Function CommandBarControls(Optional ByVal pvIndex As Variant) As Variant
  100. &apos; Return an object of type CommandBarControl indicated by its index
  101. &apos; Index is different from UNO index: separators do not count
  102. &apos; If no pvIndex argument, return a Collection type
  103. If _ErrorHandler() Then On Local Error Goto Error_Function
  104. Const cstThisSub = &quot;CommandBar.CommandBarControls&quot;
  105. Utils._SetCalledSub(cstThisSub)
  106. Dim oLayout As Object, vElements() As Variant, iIndexToolbar As Integer, oToolbar As Object
  107. Dim i As Integer, iItemsCount As Integer, oSettings As Object, vItem() As Variant, bSeparator As Boolean
  108. Dim oObject As Object
  109. Set oObject = Nothing
  110. If Not IsMissing(pvIndex) Then
  111. If Not Utils._CheckArgument(pvIndex, 1, Utils._AddNumeric()) Then Goto Exit_Function
  112. If pvIndex &lt; 0 Then Goto Trace_IndexError
  113. End If
  114. Select Case _BarType
  115. Case msoBarTypeNormal, msoBarTypeMenuBar
  116. Case Else : Goto Error_NotApplicable &apos; Status bar not supported
  117. End Select
  118. Set oLayout = _Window.LayoutManager
  119. vElements = oLayout.getElements()
  120. iIndexToolbar = _FindElement(vElements())
  121. If iIndexToolbar &lt; 0 Then Goto Error_NotApplicable &apos; Toolbar not visible
  122. Set oToolbar = vElements(iIndexToolbar)
  123. iItemsCount = 0
  124. Set oSettings = oToolbar.getSettings(False)
  125. bSeparator = False
  126. For i = 0 To oSettings.getCount() - 1
  127. Set vItem() = oSettings.getByIndex(i)
  128. If _GetPropertyValue(vItem, &quot;Type&quot;, 1) &lt;&gt; 1 Then &apos; Type = 1 indicates separator
  129. iItemsCount = iItemsCount + 1
  130. If Not IsMissing(pvIndex) Then
  131. If pvIndex = iItemsCount - 1 Then
  132. Set oObject = New CommandBarControl
  133. With oObject
  134. Set ._This = oObject
  135. Set ._Parent = _This
  136. ._ParentCommandBarName = _Name
  137. ._ParentCommandBar = oToolbar
  138. ._ParentBuiltin = ( _BarBuiltin = 1 )
  139. ._Element = vItem()
  140. ._InternalIndex = i
  141. ._Index = iItemsCount &apos; Indexes start at 1
  142. ._BeginGroup = bSeparator
  143. End With
  144. End If
  145. bSeparator = False
  146. End If
  147. Else
  148. bSeparator = True
  149. End If
  150. Next i
  151. If IsNull(oObject) Then
  152. Select Case True
  153. Case IsMissing(pvIndex)
  154. Set oObject = New Collect
  155. Set oObject._This = oObject
  156. oObject._CollType = COLLCOMMANDBARCONTROLS
  157. Set oObject._Parent = _This
  158. oObject._Count = iItemsCount
  159. Case Else &apos; pvIndex is numeric
  160. Goto Trace_IndexError
  161. End Select
  162. End If
  163. Exit_Function:
  164. Set CommandBarControls = oObject
  165. Set oObject = Nothing
  166. Utils._ResetCalledSub(cstThisSub)
  167. Exit Function
  168. Error_Function:
  169. TraceError(TRACEABORT, Err, cstThisSub, Erl)
  170. GoTo Exit_Function
  171. Trace_IndexError:
  172. TraceError(TRACEFATAL, ERRCOLLECTION, Utils._CalledSub(), 0)
  173. Goto Exit_Function
  174. Error_NotApplicable:
  175. TraceError(TRACEFATAL, ERRMETHOD, Utils._CalledSub(), 0, 1, cstThisSub)
  176. Goto Exit_Function
  177. End Function &apos; CommandBarControls V1,3,0
  178. REM -----------------------------------------------------------------------------------------------------------------------
  179. Public Function Controls(Optional ByVal pvIndex As Variant) As Variant
  180. &apos; Alias for CommandBarControls (VBA)
  181. If _ErrorHandler() Then On Local Error Goto Error_Function
  182. Const cstThisSub = &quot;CommandBar.Controls&quot;
  183. Utils._SetCalledSub(cstThisSub)
  184. Dim oObject As Object
  185. If IsMissing(pvIndex) Then Set oObject = CommandBarControls() Else Set oObject = CommandBarControls(pvIndex)
  186. Exit_Function:
  187. Set Controls = oObject
  188. Set oObject = Nothing
  189. Utils._ResetCalledSub(cstThisSub)
  190. Exit Function
  191. Error_Function:
  192. TraceError(TRACEABORT, Err, cstThisSub, Erl)
  193. GoTo Exit_Function
  194. End Function &apos; Controls V1,3,0
  195. REM -----------------------------------------------------------------------------------------------------------------------
  196. Public Function getProperty(Optional ByVal pvProperty As Variant) As Variant
  197. &apos; Return property value of psProperty property name
  198. Utils._SetCalledSub(&quot;CommandBar.getProperty&quot;)
  199. If IsMissing(pvProperty) Then Call _TraceArguments()
  200. getProperty = _PropertyGet(pvProperty)
  201. Utils._ResetCalledSub(&quot;CommandBar.getProperty&quot;)
  202. End Function &apos; getProperty
  203. REM -----------------------------------------------------------------------------------------------------------------------
  204. Public Function hasProperty(ByVal Optional pvProperty As Variant) As Boolean
  205. &apos; Return True if object has a valid property called pvProperty (case-insensitive comparison !)
  206. If IsMissing(pvProperty) Then hasProperty = PropertiesGet._hasProperty(_Type, _PropertiesList()) Else hasProperty = PropertiesGet._hasProperty(_Type, _PropertiesList(), pvProperty)
  207. Exit Function
  208. End Function &apos; hasProperty
  209. REM -----------------------------------------------------------------------------------------------------------------------
  210. Public Function Reset() As Boolean
  211. &apos; Reset a whole command bar to its initial values
  212. If _ErrorHandler() Then On Local Error Goto Error_Function
  213. Const cstThisSub = &quot;CommandBar.Reset&quot;
  214. Utils._SetCalledSub(cstThisSub)
  215. _Toolbar.reload()
  216. Exit_Function:
  217. Reset = True
  218. Utils._ResetCalledSub(cstThisSub)
  219. Exit Function
  220. Error_Function:
  221. TraceError(TRACEABORT, Err, cstThisSub, Erl)
  222. Reset = False
  223. GoTo Exit_Function
  224. End Function &apos; Reset V1.3.0
  225. REM -----------------------------------------------------------------------------------------------------------------------
  226. REM --- PRIVATE FUNCTIONS ---
  227. REM -----------------------------------------------------------------------------------------------------------------------
  228. REM -----------------------------------------------------------------------------------------------------------------------
  229. Private Function _FindElement(pvElements As Variant) As Integer
  230. &apos; Return -1 if not found, otherwise return index in elements table of LayoutManager
  231. Dim i As Integer
  232. _FindElement = -1
  233. If Not IsArray(pvElements) Then Exit Function
  234. For i = 0 To UBound(pvElements)
  235. If _ResourceURL = pvElements(i).ResourceURL Then
  236. _FindElement = i
  237. Exit Function
  238. End If
  239. Next i
  240. End Function
  241. REM -----------------------------------------------------------------------------------------------------------------------
  242. Private Function _PropertiesList() As Variant
  243. _PropertiesList = Array(&quot;BuiltIn&quot;, &quot;Name&quot;, &quot;ObjectType&quot;, &quot;Visible&quot;)
  244. End Function &apos; _PropertiesList
  245. REM -----------------------------------------------------------------------------------------------------------------------
  246. Private Function _PropertyGet(ByVal psProperty As String) As Variant
  247. &apos; Return property value of the psProperty property name
  248. If _ErrorHandler() Then On Local Error Goto Error_Function
  249. Dim cstThisSub As String
  250. cstThisSub = &quot;CommandBar.get&quot; &amp; psProperty
  251. Utils._SetCalledSub(cstThisSub)
  252. _PropertyGet = Nothing
  253. Dim oLayout As Object, iElementIndex As Integer
  254. Select Case UCase(psProperty)
  255. Case UCase(&quot;BuiltIn&quot;)
  256. _PropertyGet = ( _BarBuiltin = 1 )
  257. Case UCase(&quot;Name&quot;)
  258. _PropertyGet = _Name
  259. Case UCase(&quot;ObjectType&quot;)
  260. _PropertyGet = _Type
  261. Case UCase(&quot;Visible&quot;)
  262. Set oLayout = _Window.LayoutManager
  263. iElementIndex = _FindElement(oLayout.getElements())
  264. If iElementIndex &lt; 0 Then _PropertyGet = False Else _PropertyGet = oLayout.isElementVisible(_ResourceURL)
  265. Case Else
  266. Goto Trace_Error
  267. End Select
  268. Exit_Function:
  269. Utils._ResetCalledSub(cstThisSub)
  270. Exit Function
  271. Trace_Error:
  272. TraceError(TRACEFATAL, ERRPROPERTY, Utils._CalledSub(), 0, 1, psProperty)
  273. _PropertyGet = Nothing
  274. Goto Exit_Function
  275. Error_Function:
  276. TraceError(TRACEABORT, Err, Utils._CalledSub(), Erl)
  277. _PropertyGet = Nothing
  278. GoTo Exit_Function
  279. End Function &apos; _PropertyGet
  280. REM -----------------------------------------------------------------------------------------------------------------------
  281. Private Function _PropertySet(ByVal psProperty As String, ByVal pvValue As Variant) As Boolean
  282. &apos; Return True if property setting OK
  283. If _ErrorHandler() Then On Local Error Goto Error_Function
  284. Dim cstThisSub As String
  285. cstThisSub = &quot;CommandBar.set&quot; &amp; psProperty
  286. Utils._SetCalledSub(cstThisSub)
  287. _PropertySet = True
  288. Dim iArgNr As Integer
  289. Dim oLayout As Object, iElementIndex As Integer
  290. Select Case UCase(_A2B_.CalledSub)
  291. Case UCase(&quot;setProperty&quot;) : iArgNr = 3
  292. Case UCase(&quot;CommandBar.setProperty&quot;) : iArgNr = 2
  293. Case UCase(cstThisSub) : iArgNr = 1
  294. End Select
  295. If Not hasProperty(psProperty) Then Goto Trace_Error
  296. Select Case UCase(psProperty)
  297. Case UCase(&quot;Visible&quot;)
  298. If Not Utils._CheckArgument(pvValue, iArgNr, vbBoolean, , False) Then Goto Trace_Error_Value
  299. Set oLayout = _Window.LayoutManager
  300. With oLayout
  301. iElementIndex = _FindElement(.getElements())
  302. If iElementIndex &lt; 0 Then
  303. If pvValue Then
  304. .createElement(_ResourceURL)
  305. .showElement(_ResourceURL)
  306. End If
  307. Else
  308. If pvValue &lt;&gt; .isElementVisible(_ResourceURL) Then
  309. If pvValue Then .showElement(_ResourceURL) Else .hideElement(_ResourceURL)
  310. End If
  311. End If
  312. End With
  313. Case Else
  314. Goto Trace_Error
  315. End Select
  316. Exit_Function:
  317. Utils._ResetCalledSub(cstThisSub)
  318. Exit Function
  319. Trace_Error:
  320. TraceError(TRACEFATAL, ERRPROPERTY, Utils._CalledSub(), 0, , psProperty)
  321. _PropertySet = False
  322. Goto Exit_Function
  323. Trace_Error_Value:
  324. TraceError(TRACEFATAL, ERRPROPERTYVALUE, Utils._CalledSub(), 0, 1, Array(pvValue, psProperty))
  325. _PropertySet = False
  326. Goto Exit_Function
  327. Error_Function:
  328. TraceError(TRACEABORT, Err, cstThisSub, Erl)
  329. _PropertySet = False
  330. GoTo Exit_Function
  331. End Function &apos; _PropertySet
  332. </script:module>