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