merge the formfield patch from ooo-build
[ooovba.git] / wizards / source / tools / Listbox.xba
blob01e62e7d6715b8db0f6853cd732836c4a6253244
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
8 Sub MergeList(SourceListBox() as Object, SecondList() as String)
9 Dim i as Integer
10 Dim 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()
17 End Sub
20 Sub RemoveListItems(SourceListbox as Object, TargetListbox as Object, RemoveList() as String)
21 Dim i as Integer
22 Dim s as Integer
23 Dim MaxIndex as Integer
24 Dim 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()
41 End Sub
44 &apos; Note Boolean Parameter
45 Sub InitializeListboxProcedures(oModel as Object, SourceListbox as Object, TargetListbox as Object)
46 Dim EmptyList()
47 Set oDialogModel = oModel
48 OriginalList()= SourceListbox.StringItemList()
49 TargetListbox.StringItemList() = EmptyList()
50 End Sub
53 Sub CopyListboxItems(SourceListbox as Object, TargetListbox As Object)
54 Dim NullArray()
55 TargetListbox.StringItemList() = OriginalList()
56 SourceListbox.StringItemList() = NullArray()
57 End Sub
60 Sub FormMoveSelected()
61 Call MoveSelectedListBox(oDialogModel.lstFields, oDialogModel.lstSelFields)
62 Call FormSetMoveRights()
63 oDialogModel.lstSelFields.Tag = True
64 End Sub
67 Sub FormMoveAll()
68 Call CopyListboxItems(oDialogModel.lstFields, oDialogModel.lstSelFields)
69 Call FormSetMoveRights()
70 oDialogModel.lstSelFields.Tag = True
71 End Sub
74 Sub FormRemoveSelected()
75 Call MoveOrderedSelectedListbox(oDialogModel.lstFields, oDialogModel.lstSelFields, False)
76 Call FormSetMoveRights()
77 oDialogModel.lstSelFields.Tag = True
78 End Sub
81 Sub FormRemoveAll()
82 Call MoveOrderedSelectedListbox(oDialogModel.lstFields, oDialogModel.lstSelFields, True)
83 Call FormSetMoveRights()
84 oDialogModel.lstSelFields.Tag = 1
85 End Sub
88 Sub MoveSelectedListBox(SourceListbox as Object, TargetListbox as Object)
89 Dim MaxCurTarget as Integer
90 Dim MaxSourceSelected as Integer
91 Dim n as Integer
92 Dim m as Integer
93 Dim CurIndex
94 Dim iOldTargetSelect as Integer
95 Dim 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
119 End Sub
123 Sub MoveOrderedSelectedListbox(lstSource as Object, lstTarget as Object, bMoveAll as Boolean)
124 Dim NullArray()
125 Dim MaxSelected as Integer
126 Dim MaxSourceIndex as Integer
127 Dim MaxOriginalIndex as Integer
128 Dim MaxNewIndex as Integer
129 Dim n as Integer
130 Dim m as Integer
131 Dim CurIndex as Integer
132 Dim SearchString as String
133 Dim SourceList() as String
134 Dim iOldTargetSelect as Integer
135 Dim 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)
172 End Sub
175 Function RemoveSelected(oListbox as Object)
176 Dim MaxIndex as Integer
177 Dim MaxSelected as Integer
178 Dim n as Integer
179 Dim m as Integer
180 Dim CurIndex as Integer
181 Dim CurItem as String
182 Dim 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
207 End Function
210 Sub SetNewSelection(oListBox as Object, iLastSelection as Integer)
211 Dim MaxIndex as Integer
212 Dim SelIndex as Integer
213 Dim 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
224 End Sub
227 Sub 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
242 End Sub
245 &apos; Enable or disable the buttons used for moving the available
246 &apos; fields between the two list boxes.
247 Sub FormSetMoveRights()
248 Dim bIsFieldSelected as Boolean
249 Dim bSelectSelected as Boolean
250 Dim FieldCount as Integer
251 Dim 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
262 End Sub
265 Function AddSingleItemToListbox(ByVal oListbox as Object, ListItem as String, Optional iSelIndex) as Object
266 Dim MaxIndex as Integer
267 Dim i as Integer
269 MaxIndex = Ubound(oListbox.StringItemList())
270 Dim 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
281 End Function
284 Sub EmptyListbox(oListbox as Object)
285 Dim NullList() as String
286 oListbox.StringItemList() = NullList()
287 End Sub
290 Sub SelectListboxItem(oListbox as Object, iSelIndex as Integer)
291 Dim LocSelList(0) as Integer
292 If iSelIndex &lt;&gt; -1 Then
293 LocSelList(0) = iSelIndex
294 oListbox.SelectedItems() = LocSelList()
295 End If
296 End Sub
299 Function GetSelectedListboxItems(oListbox as Object)
300 Dim SelList(Ubound(oListBox.SelectedItems())) as String
301 Dim i as Integer
302 Dim 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()
308 End Function
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
313 Sub RemoveListboxItemByName(oListbox as Object, RemoveItem as String)
314 Dim OldList() as String
315 Dim NullList() as String
316 Dim i as Integer
317 Dim a as Integer
318 Dim 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
336 End Sub
339 Function GetItemPos(oListBox as Object, sItem as String)
340 Dim ItemList()
341 Dim MaxIndex as Integer
342 Dim 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
352 End Function
353 </script:module>