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