home *** CD-ROM | disk | FTP | other *** search
/ com!online 2001 December / COMCD1201.iso / openoffice / f_0179 / Listbox.xba < prev    next >
Encoding:
Extensible Markup Language  |  2001-06-25  |  9.2 KB  |  312 lines

  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="Listbox" script:language="StarBasic">Option Explicit
  4. Dim OriginalList()
  5. Dim oDialogModel as Object
  6.  
  7.  
  8. Sub InitializeListboxProcedures(oModel as Object, SourceListbox as Object, TargetListbox as Object)
  9. Dim EmptyList()
  10.     Set oDialogModel = oModel
  11.     OriginalList()= SourceListbox.StringItemList()
  12.     TargetListbox.StringItemList() = EmptyList()
  13. End Sub
  14.  
  15.  
  16. Sub CopyListboxItems(SourceListbox as Object, TargetListbox As Object)
  17. Dim NullArray()
  18.     TargetListbox.StringItemList() = OriginalList()
  19.     SourceListbox.StringItemList() = NullArray()
  20. End Sub
  21.  
  22.  
  23. Sub FormMoveSelected(aEvent as Object)
  24.     Call MoveSelectedListBox(oDialogModel.lstFields, oDialogModel.lstSelFields)
  25.     Call FormSetMoveRights()
  26.     oDialogModel.lstSelFields.Tag = True
  27. End Sub
  28.  
  29.  
  30. Sub FormMoveAll()
  31.     Call CopyListboxItems(oDialogModel.lstFields, oDialogModel.lstSelFields)
  32.     Call FormSetMoveRights()
  33.     oDialogModel.lstSelFields.Tag = True
  34. End Sub
  35.  
  36.  
  37. Sub FormRemoveSelected()
  38.     Call MoveOrderedSelectedListbox(oDialogModel.lstFields, oDialogModel.lstSelFields, False)
  39.     Call FormSetMoveRights()
  40.     oDialogModel.lstSelFields.Tag = True
  41. End Sub
  42.  
  43.  
  44. Sub FormRemoveAll()
  45.     Call MoveOrderedSelectedListbox(oDialogModel.lstFields, oDialogModel.lstSelFields, True)
  46.     Call FormSetMoveRights()
  47.     oDialogModel.lstSelFields.Tag = 1
  48. End Sub
  49.  
  50.  
  51. Sub MoveSelectedListBox(SourceListbox as Object, TargetListbox as Object)
  52. Dim MaxCurTarget as Integer
  53. Dim MaxSourceSelected as Integer
  54. Dim n as Integer
  55. Dim m as Integer
  56. Dim CurIndex
  57. Dim iOldTargetSelect as Integer
  58. Dim iOldSourceSelect as Integer
  59.     MaxCurTarget = Ubound(TargetListbox.StringItemList())
  60.     MaxSourceSelected = Ubound(SourceListbox.SelectedItems())    
  61.     Dim TargetList(MaxCurTarget+MaxSourceSelected+1) 
  62.     If MaxSourceSelected > -1 Then
  63.         iOldSourceSelect = SourceListbox.SelectedItems(0)
  64.         If Ubound(TargetListbox.SelectedItems()) > -1 Then
  65.             iOldTargetSelect = TargetListbox.SelectedItems(0)
  66.         Else 
  67.             iOldTargetSelect = -1
  68.         End If
  69.         For n = 0 To MaxCurTarget
  70.             TargetList(n) = TargetListbox.StringItemList(n)
  71.         Next n
  72.         For m = 0 To MaxSourceSelected
  73.             CurIndex = SourceListbox.SelectedItems(m)
  74.             TargetList(n) = SourceListbox.StringItemList(CurIndex)
  75.             n = n + 1
  76.         Next m
  77.         TargetListBox.StringItemList() = TargetList()
  78.         SourceListbox.StringItemList() = RemoveSelected (SourceListbox)
  79.         SetNewSelection(SourceListbox, iOldSourceSelect)
  80.         SetNewSelection(TargetListbox, iOldTargetSelect)
  81.     End If
  82. End Sub
  83.  
  84.  
  85.  
  86. Sub MoveOrderedSelectedListbox(lstSource as Object, lstTarget as Object, bMoveAll as Boolean)
  87. Dim NullArray()
  88. Dim MaxSelected as Integer
  89. Dim MaxSourceIndex as Integer
  90. Dim MaxOriginalIndex as Integer
  91. Dim MaxNewIndex as Integer
  92. Dim n as Integer
  93. Dim m as Integer
  94. Dim CurIndex as Integer
  95. Dim SearchString as String
  96. Dim SourceList() as String
  97. Dim iOldTargetSelect as Integer
  98. Dim iOldSourceSelect as Integer
  99.     If bMoveAll Then
  100.         lstSource.StringItemList() = OriginalList()
  101.         lstTarget.StringItemList() = NullArray()
  102.     Else
  103.         MaxOriginalIndex = Ubound(OriginalList())
  104.         MaxSelected = Ubound(lstTarget.SelectedItems())
  105.         iOldTargetSelect = lstTarget.SelectedItems(0)
  106.         If Ubound(lstSource.SelectedItems()) > -1 Then
  107.             iOldSourceSelect = lstSource.SelectedItems(0)
  108.         End If
  109.         Dim SelList(MaxSelected)
  110.         For n = 0 To MaxSelected
  111.             CurIndex = lstTarget.SelectedItems(n)
  112.             SelList(n) = lstTarget.StringItemList(CurIndex)
  113.         Next n
  114.         SourceList() = lstSource.StringItemList()
  115.         MaxSourceIndex = Ubound(lstSource.StringItemList())
  116.         MaxNewIndex = MaxSelected + MaxSourceIndex + 1
  117.         Dim NewSourceList(MaxNewIndex)
  118.         m = 0
  119.         For n = 0 To MaxOriginalIndex
  120.             SearchString = OriginalList(n)
  121.             If IndexinArray(SearchString, SelList()) <> -1 Then
  122.                 NewSourceList(m) =  SearchString
  123.                 m = m + 1
  124.             ElseIf IndexinArray(SearchString, SourceList()) <> -1 Then
  125.                 NewSourceList(m) =  SearchString
  126.                 m = m + 1
  127.             End If
  128.         Next n
  129.         lstSource.StringItemList() = NewSourceList()
  130.         lstTarget.StringItemList() = RemoveSelected(lstTarget)
  131.     End If
  132. ' Todo: Hier weitermachen:
  133.     SetNewSelection(lstSource, iOldSourceSelect)
  134.     SetNewSelection(lstTarget, iOldTargetSelect)
  135.  
  136. End Sub
  137.  
  138.  
  139. Function RemoveSelected(oListbox as Object)
  140. Dim MaxIndex as Integer
  141. Dim MaxSelected as Integer
  142. Dim n as Integer
  143. Dim m as Integer
  144. Dim CurIndex as Integer
  145. Dim CurItem as String
  146. Dim ResultArray()
  147.     MaxIndex = Ubound(oListbox.StringItemList())
  148.     MaxSelected = Ubound(oListbox.SelectedItems())
  149.     Dim LocItemList(MaxIndex)
  150.     LocItemList() = oListbox.StringItemList()
  151.     If MaxSelected > -1 Then
  152.         For n = 0 To MaxSelected
  153.             CurIndex = oListbox.SelectedItems(n)
  154.             LocItemList(CurIndex) = ""
  155.         Next n
  156.         If MaxIndex > 0 Then
  157.             ReDim ResultArray(MaxIndex - MaxSelected - 1)
  158.             m = 0
  159.             For n = 0 To MaxIndex
  160.                 CurItem = LocItemList(n)
  161.                 If CurItem <> "" Then
  162.                     ResultArray(m) = CurItem
  163.                     m = m + 1
  164.                 End If
  165.             Next n    
  166.         End If
  167.         RemoveSelected = ResultArray()    
  168.     Else
  169.         RemoveSelected = oListbox.StringItemList()    
  170.     End If
  171. End Function
  172.  
  173.  
  174. Sub SetNewSelection(oListBox as Object, iLastSelection as Integer)
  175. Dim MaxIndex as Integer
  176. Dim SelIndex as Integer
  177. Dim SelList(0) as Integer
  178.     MaxIndex = Ubound(oListBox.StringItemList())
  179.     If MaxIndex > -1  AND iLastSelection > -1 Then
  180.         If iLastSelection > MaxIndex Then
  181.             Selindex = MaxIndex
  182.         Else
  183.             SelIndex = iLastSelection
  184.         End If
  185.         Sellist(0) = SelIndex
  186.         oListBox.SelectedItems() = SelList()
  187.     End If
  188. End Sub
  189.  
  190.  
  191. Sub ToggleListboxControls(oDialogModel as Object, bDoEnable as Boolean)
  192.     With oDialogModel
  193.         .lblFields.Enabled = bDoEnable
  194.         .lblSelFields.Enabled = bDoEnable
  195.         .lstTables.Enabled = bDoEnable
  196.         .lstFields.Enabled = bDoEnable
  197.         .lstSelFields.Enabled = bDoEnable
  198.         .cmdRemoveAll.Enabled = bDoEnable
  199.         .cmdRemoveSelected.Enabled = bDoEnable
  200.         .cmdMoveAll.Enabled = bDoEnable
  201.         .cmdMoveSelected.Enabled = bDoEnable
  202.     End With
  203.     If bDoEnable Then
  204.         FormSetMoveRights()
  205.     End If
  206. End Sub    
  207.  
  208.  
  209. ' Enable or disable the buttons used for moving the available
  210. ' fields between the two list boxes.
  211. Sub FormSetMoveRights()
  212. Dim bIsFieldSelected as Boolean
  213. Dim bSelectSelected as Boolean
  214. Dim FieldCount as Integer
  215. Dim SelectCount as Integer
  216.     bIsFieldSelected = Ubound(oDialogModel.lstFields.SelectedItems()) <> -1
  217.     FieldCount = Ubound(oDialogModel.lstFields.StringItemList()) + 1
  218.     bSelectSelected = Ubound(oDialogModel.lstSelFields.SelectedItems()) > -1
  219.     SelectCount = Ubound(oDialogModel.lstSelFields.StringItemList()) + 1
  220.     oDialogModel.cmdRemoveAll.Enabled = SelectCount>=1
  221.     oDialogModel.cmdRemoveSelected.Enabled = bSelectSelected
  222.     oDialogModel.cmdMoveAll.Enabled = FieldCount >=1
  223.     oDialogModel.cmdMoveSelected.Enabled = bIsFieldSelected
  224.     oDialogModel.cmdGoOn.Enabled = SelectCount>=1
  225.     ' This flag is set to '1' when the lstSelFields has been modified 
  226. End Sub
  227.  
  228.  
  229. Function AddSingleItemToListbox(ByVal oListbox as Object, ListItem as String, Optional iSelIndex) as Object
  230. Dim MaxIndex as Integer
  231. Dim i as Integer
  232.  
  233.     MaxIndex = Ubound(oListbox.StringItemList())
  234. Dim LocList(MaxIndex + 1)
  235. ' Todo: This goes faster with the Redim LocList(MaxIndex + 1) Preserve function
  236.     For i = 0 To MaxIndex
  237.         LocList(i) = oListbox.StringItemList(i)
  238.     Next i
  239.     LocList(MaxIndex + 1) = ListItem
  240.     oListbox.StringItemList() = LocList()
  241.     If Not IsMissing(iSelIndex) Then
  242.         SelectListboxItem(oListbox, iSelIndex)
  243.     End If
  244.     AddSingleItemToListbox() = oListbox
  245. End Function
  246.  
  247.  
  248. Sub    EmptyListbox(oListbox as Object)
  249. Dim NullList() as String
  250.     oListbox.StringItemList() = NullList()
  251. End Sub
  252.  
  253.  
  254. Sub    SelectListboxItem(oListbox as Object, iSelIndex as Integer)
  255. Dim LocSelList(0) as Integer
  256.     LocSelList(0) = iSelIndex
  257.     oListbox.SelectedItems() = LocSelList()
  258. End Sub
  259.  
  260.  
  261. Function GetSelectedListboxItems(oListbox as Object)
  262. Dim SelList() as String
  263. Dim i as Integer
  264. Dim CurIndex as Integer
  265.     For i = 0 To Ubound(oListbox.SelectedItems())
  266.         CurIndex = oListbox.SelectedItems(i)
  267.         SelList(i) = oListbox.StringItemList(CurIndex)
  268.     Next i
  269.     GetSelectedListboxItems() = SelList()
  270. End Function
  271.  
  272.  
  273. ' Note: When using this Sub it must be ensured that the
  274. ' 'RemoveItem' appears only only once in the Listbox
  275. Sub RemoveListboxItemByName(oListbox as Object, RemoveItem as String)
  276. Dim OldList() as String
  277. Dim NullList() as String
  278. Dim i as Integer
  279. Dim a as Integer
  280. Dim MaxIndex as Integer
  281.     OldList = oListbox.StringItemList()
  282.     MaxIndex = Ubound(OldList())
  283.     If MaxIndex > 0 Then
  284.         a = 0
  285.         Dim NewList(MaxIndex -1)
  286.         For i = 0 To MaxIndex
  287.             If RemoveItem <> OldList(i) Then
  288.                 NewList(a) = OldList(i)
  289.                 a = a + 1
  290.             End If
  291.         Next i
  292.         oListbox.StringItemList() = NewList()
  293.     Else
  294.         oListBox.StringItemList() = NullList()
  295.     End If
  296. End Sub
  297.  
  298.  
  299. Function GetItemPos(oListBox as Object, sItem as String)
  300. Dim ItemList()
  301. Dim MaxIndex as Integer
  302. Dim i as Integer
  303.     ItemList() = oListBox.StringItemList()
  304.     MaxIndex = Ubound(ItemList())    
  305.     For i = 0 To MaxIndex
  306.         If sItem = ItemList(i) Then
  307.             GetItemPos() = i
  308.             Exit Function
  309.         End If
  310.     Next i
  311.     GetItemPos() = -1
  312. End Function</script:module>