Listbox.xba 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
  3. <!--
  4. * This file is part of the LibreOffice project.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. *
  10. * This file incorporates work covered by the following license notice:
  11. *
  12. * Licensed to the Apache Software Foundation (ASF) under one or more
  13. * contributor license agreements. See the NOTICE file distributed
  14. * with this work for additional information regarding copyright
  15. * ownership. The ASF licenses this file to you under the Apache
  16. * License, Version 2.0 (the "License"); you may not use this file
  17. * except in compliance with the License. You may obtain a copy of
  18. * the License at http://www.apache.org/licenses/LICENSE-2.0 .
  19. -->
  20. <script:module xmlns:script="http://openoffice.org/2000/script" script:name="Listbox" script:language="StarBasic">Option Explicit
  21. Dim OriginalList()
  22. Dim oDialogModel as Object
  23. Sub MergeList(SourceListBox() as Object, SecondList() as String)
  24. Dim i as Integer
  25. Dim MaxIndex as Integer
  26. MaxIndex = Ubound(SecondList())
  27. OriginalList() = AddListToList(OriginalList(), SecondList())
  28. For i = 0 To MaxIndex
  29. SourceListbox = AddSingleItemToListbox(SourceListbox, SecondList(i))
  30. Next i
  31. Call FormSetMoveRights()
  32. End Sub
  33. Sub RemoveListItems(SourceListbox as Object, TargetListbox as Object, RemoveList() as String)
  34. Dim i as Integer
  35. Dim s as Integer
  36. Dim MaxIndex as Integer
  37. Dim CopyList()
  38. MaxIndex = Ubound(RemoveList())
  39. For i = 0 To MaxIndex
  40. RemoveListboxItemByName(SourceListbox, RemoveList(i))
  41. RemoveListboxItemByName(TargetListbox, RemoveList(i))
  42. Next i
  43. CopyList() = OriginalList()
  44. s = 0
  45. MaxIndex = Ubound(CopyList())
  46. For i = 0 To MaxIndex
  47. If IndexInArray(CopyList(i),RemoveList())= -1 Then
  48. OriginalList(s) = CopyList(i)
  49. s = s + 1
  50. End If
  51. Next i
  52. ReDim Preserve OriginalList(s-1)
  53. Call FormSetMoveRights()
  54. End Sub
  55. &apos; Note Boolean Parameter
  56. Sub InitializeListboxProcedures(oModel as Object, SourceListbox as Object, TargetListbox as Object)
  57. Dim EmptyList()
  58. Set oDialogModel = oModel
  59. OriginalList()= SourceListbox.StringItemList()
  60. TargetListbox.StringItemList() = EmptyList()
  61. End Sub
  62. Sub CopyListboxItems(SourceListbox as Object, TargetListbox As Object)
  63. Dim NullArray()
  64. TargetListbox.StringItemList() = OriginalList()
  65. SourceListbox.StringItemList() = NullArray()
  66. End Sub
  67. Sub FormMoveSelected()
  68. Call MoveSelectedListBox(oDialogModel.lstFields, oDialogModel.lstSelFields)
  69. Call FormSetMoveRights()
  70. oDialogModel.lstSelFields.Tag = True
  71. End Sub
  72. Sub FormMoveAll()
  73. Call CopyListboxItems(oDialogModel.lstFields, oDialogModel.lstSelFields)
  74. Call FormSetMoveRights()
  75. oDialogModel.lstSelFields.Tag = True
  76. End Sub
  77. Sub FormRemoveSelected()
  78. Call MoveOrderedSelectedListbox(oDialogModel.lstFields, oDialogModel.lstSelFields, False)
  79. Call FormSetMoveRights()
  80. oDialogModel.lstSelFields.Tag = True
  81. End Sub
  82. Sub FormRemoveAll()
  83. Call MoveOrderedSelectedListbox(oDialogModel.lstFields, oDialogModel.lstSelFields, True)
  84. Call FormSetMoveRights()
  85. oDialogModel.lstSelFields.Tag = 1
  86. End Sub
  87. Sub MoveSelectedListBox(SourceListbox as Object, TargetListbox as Object)
  88. Dim MaxCurTarget as Integer
  89. Dim MaxSourceSelected as Integer
  90. Dim n as Integer
  91. Dim m as Integer
  92. Dim CurIndex
  93. Dim iOldTargetSelect as Integer
  94. Dim iOldSourceSelect as Integer
  95. MaxCurTarget = Ubound(TargetListbox.StringItemList())
  96. MaxSourceSelected = Ubound(SourceListbox.SelectedItems())
  97. Dim TargetList(MaxCurTarget+MaxSourceSelected+1)
  98. If MaxSourceSelected &gt; -1 Then
  99. iOldSourceSelect = SourceListbox.SelectedItems(0)
  100. If Ubound(TargetListbox.SelectedItems()) &gt; -1 Then
  101. iOldTargetSelect = TargetListbox.SelectedItems(0)
  102. Else
  103. iOldTargetSelect = -1
  104. End If
  105. For n = 0 To MaxCurTarget
  106. TargetList(n) = TargetListbox.StringItemList(n)
  107. Next n
  108. For m = 0 To MaxSourceSelected
  109. CurIndex = SourceListbox.SelectedItems(m)
  110. TargetList(n) = SourceListbox.StringItemList(CurIndex)
  111. n = n + 1
  112. Next m
  113. TargetListBox.StringItemList() = TargetList()
  114. SourceListbox.StringItemList() = RemoveSelected (SourceListbox)
  115. SetNewSelection(SourceListbox, iOldSourceSelect)
  116. SetNewSelection(TargetListbox, iOldTargetSelect)
  117. End If
  118. End Sub
  119. Sub MoveOrderedSelectedListbox(lstSource as Object, lstTarget as Object, bMoveAll as Boolean)
  120. Dim NullArray()
  121. Dim MaxSelected as Integer
  122. Dim MaxSourceIndex as Integer
  123. Dim MaxOriginalIndex as Integer
  124. Dim MaxNewIndex as Integer
  125. Dim n as Integer
  126. Dim m as Integer
  127. Dim CurIndex as Integer
  128. Dim SearchString as String
  129. Dim SourceList() as String
  130. Dim iOldTargetSelect as Integer
  131. Dim iOldSourceSelect as Integer
  132. If bMoveAll Then
  133. lstSource.StringItemList() = OriginalList()
  134. lstTarget.StringItemList() = NullArray()
  135. Else
  136. MaxOriginalIndex = Ubound(OriginalList())
  137. MaxSelected = Ubound(lstTarget.SelectedItems())
  138. iOldTargetSelect = lstTarget.SelectedItems(0)
  139. If Ubound(lstSource.SelectedItems()) &gt; -1 Then
  140. iOldSourceSelect = lstSource.SelectedItems(0)
  141. End If
  142. Dim SelList(MaxSelected)
  143. For n = 0 To MaxSelected
  144. CurIndex = lstTarget.SelectedItems(n)
  145. SelList(n) = lstTarget.StringItemList(CurIndex)
  146. Next n
  147. SourceList() = lstSource.StringItemList()
  148. MaxSourceIndex = Ubound(lstSource.StringItemList())
  149. MaxNewIndex = MaxSelected + MaxSourceIndex + 1
  150. Dim NewSourceList(MaxNewIndex)
  151. m = 0
  152. For n = 0 To MaxOriginalIndex
  153. SearchString = OriginalList(n)
  154. If IndexInArray(SearchString, SelList()) &lt;&gt; -1 Then
  155. NewSourceList(m) = SearchString
  156. m = m + 1
  157. ElseIf IndexInArray(SearchString, SourceList()) &lt;&gt; -1 Then
  158. NewSourceList(m) = SearchString
  159. m = m + 1
  160. End If
  161. Next n
  162. lstSource.StringItemList() = NewSourceList()
  163. lstTarget.StringItemList() = RemoveSelected(lstTarget)
  164. End If
  165. SetNewSelection(lstSource, iOldSourceSelect)
  166. SetNewSelection(lstTarget, iOldTargetSelect)
  167. End Sub
  168. Function RemoveSelected(oListbox as Object)
  169. Dim MaxIndex as Integer
  170. Dim MaxSelected as Integer
  171. Dim n as Integer
  172. Dim m as Integer
  173. Dim CurIndex as Integer
  174. Dim CurItem as String
  175. Dim ResultArray()
  176. MaxIndex = Ubound(oListbox.StringItemList())
  177. MaxSelected = Ubound(oListbox.SelectedItems())
  178. Dim LocItemList(MaxIndex)
  179. LocItemList() = oListbox.StringItemList()
  180. If MaxSelected &gt; -1 Then
  181. For n = 0 To MaxSelected
  182. CurIndex = oListbox.SelectedItems(n)
  183. LocItemList(CurIndex) = &quot;&quot;
  184. Next n
  185. If MaxIndex &gt; 0 Then
  186. ReDim ResultArray(MaxIndex - MaxSelected - 1)
  187. m = 0
  188. For n = 0 To MaxIndex
  189. CurItem = LocItemList(n)
  190. If CurItem &lt;&gt; &quot;&quot; Then
  191. ResultArray(m) = CurItem
  192. m = m + 1
  193. End If
  194. Next n
  195. End If
  196. RemoveSelected = ResultArray()
  197. Else
  198. RemoveSelected = oListbox.StringItemList()
  199. End If
  200. End Function
  201. Sub SetNewSelection(oListBox as Object, iLastSelection as Integer)
  202. Dim MaxIndex as Integer
  203. Dim SelIndex as Integer
  204. Dim SelList(0) as Integer
  205. MaxIndex = Ubound(oListBox.StringItemList())
  206. If MaxIndex &gt; -1 AND iLastSelection &gt; -1 Then
  207. If iLastSelection &gt; MaxIndex Then
  208. Selindex = MaxIndex
  209. Else
  210. SelIndex = iLastSelection
  211. End If
  212. Sellist(0) = SelIndex
  213. oListBox.SelectedItems() = SelList()
  214. End If
  215. End Sub
  216. Sub ToggleListboxControls(oDialogModel as Object, bDoEnable as Boolean)
  217. With oDialogModel
  218. .lblFields.Enabled = bDoEnable
  219. .lblSelFields.Enabled = bDoEnable
  220. &apos; .lstTables.Enabled = bDoEnable
  221. .lstFields.Enabled = bDoEnable
  222. .lstSelFields.Enabled = bDoEnable
  223. .cmdRemoveAll.Enabled = bDoEnable
  224. .cmdRemoveSelected.Enabled = bDoEnable
  225. .cmdMoveAll.Enabled = bDoEnable
  226. .cmdMoveSelected.Enabled = bDoEnable
  227. End With
  228. If bDoEnable Then
  229. FormSetMoveRights()
  230. End If
  231. End Sub
  232. &apos; Enable or disable the buttons used for moving the available
  233. &apos; fields between the two list boxes.
  234. Sub FormSetMoveRights()
  235. Dim bIsFieldSelected as Boolean
  236. Dim bSelectSelected as Boolean
  237. Dim FieldCount as Integer
  238. Dim SelectCount as Integer
  239. bIsFieldSelected = Ubound(oDialogModel.lstFields.SelectedItems()) &lt;&gt; -1
  240. FieldCount = Ubound(oDialogModel.lstFields.StringItemList()) + 1
  241. bSelectSelected = Ubound(oDialogModel.lstSelFields.SelectedItems()) &gt; -1
  242. SelectCount = Ubound(oDialogModel.lstSelFields.StringItemList()) + 1
  243. oDialogModel.cmdRemoveAll.Enabled = SelectCount&gt;=1
  244. oDialogModel.cmdRemoveSelected.Enabled = bSelectSelected
  245. oDialogModel.cmdMoveAll.Enabled = FieldCount &gt;=1
  246. oDialogModel.cmdMoveSelected.Enabled = bIsFieldSelected
  247. oDialogModel.cmdGoOn.Enabled = SelectCount&gt;=1
  248. &apos; This flag is set to &apos;1&apos; when the lstSelFields has been modified
  249. End Sub
  250. Function AddSingleItemToListbox(ByVal oListbox as Object, ListItem as String, Optional iSelIndex) as Object
  251. Dim MaxIndex as Integer
  252. Dim i as Integer
  253. MaxIndex = Ubound(oListbox.StringItemList())
  254. Dim LocList(MaxIndex + 1)
  255. &apos; Todo: This goes faster with the Redim LocList(MaxIndex + 1) Preserve function
  256. For i = 0 To MaxIndex
  257. LocList(i) = oListbox.StringItemList(i)
  258. Next i
  259. LocList(MaxIndex + 1) = ListItem
  260. oListbox.StringItemList() = LocList()
  261. If Not IsMissing(iSelIndex) Then
  262. SelectListboxItem(oListbox, iSelIndex)
  263. End If
  264. AddSingleItemToListbox() = oListbox
  265. End Function
  266. Sub EmptyListbox(oListbox as Object)
  267. Dim NullList() as String
  268. oListbox.StringItemList() = NullList()
  269. End Sub
  270. Sub SelectListboxItem(oListbox as Object, iSelIndex as Integer)
  271. Dim LocSelList(0) as Integer
  272. If iSelIndex &lt;&gt; -1 Then
  273. LocSelList(0) = iSelIndex
  274. oListbox.SelectedItems() = LocSelList()
  275. End If
  276. End Sub
  277. Function GetSelectedListboxItems(oListbox as Object)
  278. Dim SelList(Ubound(oListBox.SelectedItems())) as String
  279. Dim i as Integer
  280. Dim CurIndex as Integer
  281. For i = 0 To Ubound(oListbox.SelectedItems())
  282. CurIndex = oListbox.SelectedItems(i)
  283. SelList(i) = oListbox.StringItemList(CurIndex)
  284. Next i
  285. GetSelectedListboxItems() = SelList()
  286. End Function
  287. &apos; Note: When using this Sub it must be ensured that the
  288. &apos; &apos;RemoveItem&apos; appears only once in the Listbox
  289. Sub RemoveListboxItemByName(oListbox as Object, RemoveItem as String)
  290. Dim OldList() as String
  291. Dim NullList() as String
  292. Dim i as Integer
  293. Dim a as Integer
  294. Dim MaxIndex as Integer
  295. OldList = oListbox.StringItemList()
  296. MaxIndex = Ubound(OldList())
  297. If IndexInArray(RemoveItem, OldList()) &lt;&gt; -1 Then
  298. If MaxIndex &gt; 0 Then
  299. a = 0
  300. Dim NewList(MaxIndex -1)
  301. For i = 0 To MaxIndex
  302. If RemoveItem &lt;&gt; OldList(i) Then
  303. NewList(a) = OldList(i)
  304. a = a + 1
  305. End If
  306. Next i
  307. oListbox.StringItemList() = NewList()
  308. Else
  309. oListBox.StringItemList() = NullList()
  310. End If
  311. End If
  312. End Sub
  313. Function GetItemPos(oListBox as Object, sItem as String)
  314. Dim ItemList()
  315. Dim MaxIndex as Integer
  316. Dim i as Integer
  317. ItemList() = oListBox.StringItemList()
  318. MaxIndex = Ubound(ItemList())
  319. For i = 0 To MaxIndex
  320. If sItem = ItemList(i) Then
  321. GetItemPos() = i
  322. Exit Function
  323. End If
  324. Next i
  325. GetItemPos() = -1
  326. End Function
  327. </script:module>