[bb] Multi-Column ListBox by MattVonFat [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:42

Previous topic - Next topic

BlitzBot

Title : Multi-Column ListBox
Author : MattVonFat
Posted : 1+ years ago

Description : I needed one of these but didn't have another way to do it. I used the tutorial on gadgets by CS_TBL to create this. Also i apologise as the code is very messy! And there are probably bits that don't need to be there.

A limitation of this though is that at the moment you can only have one ListView in the app. Also there is nothing to deal with double clicking.

There are 9 functions:
CreateListView(x%,y%,width%,height%,parent%)
UpdateListView(listView%)
AddListViewColumn(listView%,columnName$,width%)
DeleteListViewColumn(listView%,column%)
AddListViewItem(listView%,items$)
DeleteListViewItem(listView%,item%)
ListViewRefresh(listView%,Event)
SelectedListViewItem()
SelectListViewItem(listView%,item%)

To create a ListView use:
lst = CreateListView(10, 10, 300, 300, window)

To add a column use:
AddListViewColumn(lst,"Column One",90)

To add an item:
AddListViewItem(lst,"Hello,Matthew")
Note that the item names are in one string separated by commas. So 'Hello' would be in the first column and 'Matthew' in the second.

To remove a column:
DeleteListViewColumn(lst,1)

To remove an item:
DeleteListViewItem(lst,1)

SelectedListViewItem and SelectListViewItem pretty much speak for them selves.

UpdateListViewItem doesn't need to be used as each function that changes something calls it anyway.

RefreshListViewItem should be included in a loop which recieves events and the EventID() should be passed to it as the second parameter. This lets the list view react to people clicking on it. It only uses the MouseUp, MouseDown and MouseMove Events.

Finally you need to include:
LoadCursor%( ID, Cursor ):"LoadCursorA"
SetCursor%( ID ):"SetCursor"

In the user23.decls file and:
Type column
Field colWidth%
Field colName$
Field i.item
End Type

Type item
Field col
Field itemText$
End Type

Global c.column
Global listViewFont% = LoadFont("Tahoma",14), rows = 0, highrow%
Global mx, my
Dim words$(0)

At the top of the code.

And this is a quick bit of code that you can use to test out the listview:
window = CreateWindow("", 0, 0, 500, 500, 0)
lst = CreateListView(10, 10, 300, 300, window)
AddListViewColumn(lst,"Column One",90)
AddListViewColumn(lst,"Column Two", 200)
AddListViewItem(lst,"Hello,Matthew")
AddListViewItem(lst,"Goodbye,Chris")
SelectListViewItem(lst,2)

btn = CreateButton("1", 370, 400, 30, 30, window)
btn2 = CreateButton("2", 400, 400, 30, 30, window)
btn3 = CreateButton("3", 430, 400, 30, 30, window)

While WaitEvent() <> $803

If EventID() = $401
If EventSource() = btn
Print SelectedListViewItem()
ElseIf EventSource() = btn2
DeleteListViewItem(lst,SelectedListViewItem())
ElseIf EventSource() = btn3
a$ = Input("col: ")
DeleteListViewColumn(lst,a$)
End If
Else
ListViewRefresh(lst,EventID())
End If

Wend


That wraps that up so here are the functions: [/i]

Code :
Code (blitzbasic) Select
;------------------------------------------------------
;CREATES A LISTVIEW
Function CreateListView(x%,y%,width%,height%,parent%)

bank = CreateBank(12)
canvas = CreateCanvas(x,y,width,height,parent)

PokeInt bank, 0, canvas
PokeInt bank, 4, x%
PokeInt bank, 8, y%

UpdateListView(bank)

Return bank

End Function

;------------------------------------------------------
;UPDATES THE LISTVIEW
Function UpdateListView(bank%,params%=0)

If bank<12 Then RuntimeError("Gadget ListView Error")

canvas = PeekInt(bank,0)


xwidth = 0
width = ClientWidth(canvas)
height = ClientHeight(canvas)

colNum = 1

SetBuffer CanvasBuffer(canvas)
Cls
For c.column = Each column
Color 235, 234, 219
Rect xwidth%, 0, xwidth%+ccolWidth%, 18, True
Color 226, 222, 205
Line xwidth%, 18, xwidth%+ccolWidth%, 18
Color 214, 210, 194
Line xwidth%, 19, xwidth%+ccolWidth%, 19
Color 203, 199, 184
Line xwidth%, 20, xwidth%+ccolWidth%, 20
SetFont listViewFont%
Color 0, 0, 0
Text xwidth%+8, 3, ccolName%
Color 255, 255, 255
Rect xwidth%, 20, width%-xwidth%, height, True
Color 199, 197, 178
Line xwidth%+ccolWidth%-2, 3, xwidth%+ccolWidth%-2, 17
Color 255, 255, 255
Line xwidth%+ccolWidth%-1, 3, xwidth%+ccolWidth%-1, 17
textStart% = 20
highexist = False
deleted = False
For ci.item = Each item
If params = textStart% And deleted = False And cicol = colNum
Delete ci
deleted = True
Else
If cicol = colNum
If highrow% > textStart Or highrow% = textStart And highrow < textStart+20
highExist = True
Color 49, 106, 197
Rect xwidth%, textStart, ccolWidth%, 20, True
Color 255, 255, 255
Text xwidth%+10, textStart+3, ciitemText$
Else
Color 0, 0, 0
Text xwidth%+10, textStart+3, ciitemText$
End If
textStart = textStart + 20
End If
End If
Next
If highexist = False Then highrow = 0
xwidth% = xwidth% + ccolWidth%
colNum = colNum+1
Next
Color 235, 234, 219
Rect xwidth%, 0, width%+50, 18, True
Color 226, 222, 205
Line xwidth%, 18, width%+50, 18
Color 214, 210, 194
Line xwidth%, 19, width%+50, 19
Color 203, 199, 184
Line xwidth%, 20, width%+50, 20
Color 255, 255, 255
Rect xwidth%, 20, width%+50, height, True
Color 0, 0, 0
Rect 0, 0, width%, height, False
FlipCanvas(canvas)

Return

End Function

;------------------------------------------------------
;ADDS A COLUMN TO THE LIST VIEW
Function AddListViewColumn(listView,columnName$,width%)

c.column = New column
ccolWidth% = width%
ccolName$ = columnName$
UpdateListView(listView)

End Function

;------------------------------------------------------
;REMOVES A COLUMN FROM A LISTVIEW
Function DeleteListViewColumn(listView,col1%)

colNum% = 1

For c.column = Each column
If colNum% = col1%
For ci.item = Each item
If cicol = col1%
Delete ci
Else
cicol = cicol-1
End If
Next
Delete c
Exit
Else
colNum% = colNum+1
End If
Next

UpdateListView(listView)

End Function

;------------------------------------------------------
;ADDS AN ITEM TO LISTVIEW
Function AddListViewItem(listView,items$) ;A COMMA SEPARATED LIST OF VALUES

off% = 0
count% = 1
Repeat
pos% = Instr(items$,",",off%)
If pos% = 0
Exit
Else
count% = count% + 1
off% = pos%+1
End If
Forever

Dim words(count%)

begin% = 0

For a = 1 To count%
pos% = Instr(items$,",",begin%)
If pos% = 0
words(a) = Mid(items$,begin,Len(items$)-begin+1)
Else
words(a) = Mid(items$,begin,pos%-begin)
begin% = pos%+1
End If
Next

count% = 1

For c.column = Each column
ci.item = New item
cicol = count%
ciitemText$ = words(count%)
count% = count%+1
Next

rows% = rows% + 1
UpdateListView(listView)

End Function

;------------------------------------------------------
;REMOVES AN ITEM FROM LISTVIEW
Function DeleteListViewItem(listView,item%)

rowDel% = (item%*20)
UpdateListView(listView,rowDel%)

End Function

;------------------------------------------------------
;REFRESHES LISTVIEW
Function ListViewRefresh(listView,event)

If event = $203
If Not EventSource() = PeekInt(listView,0) Then Return

mx = EventX()
my = EventY()

For c.column = Each column
If mx > (ccolWidth%+x)-4 And mx < (ccolWidth%+x)+4 And my > 0 And my <20
cursor = LoadCursor(0,32644)
SetCursor(cursor)
End If
x = x + ccolWidth
Next
ElseIf event = $201
If Not EventSource() = PeekInt(listView,0) Then Return

x = 0

For c.column = Each column
If mx > (ccolWidth%+x)-4 And mx < (ccolWidth%+x)+4 And my > 0 And my <20
cursor = LoadCursor(0,32644)
SetCursor(cursor)
While WaitEvent() <> $202
If EventID() = $203
ccolWidth% = EventX()-x
End If
Wend
UpdateListView(listView)
Exit
End If
x = ccolWidth%
Next

If my > 20
mystr$ = Str(my)
rowstr$ = Left(mystr,Len(my)-1) + "0"
highrow% = Int(rowstr$)
End If

End If

UpdateListView(listView)

End Function

;------------------------------------------------------
;GETS SELECTED LISTVIEW ITEM
Function SelectedListViewItem()

If highrow = 0 Then Return 0

selected% = highrow/20
Return selected%

End Function

;------------------------------------------------------
;SELECTS LIST VIEW ITEM
Function SelectListViewItem(listView,item%)

highrow% = (item%*20)
UpdateListView(listView)

End Function


Comments : none...