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