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