November 25, 2020, 05:39:48 AM

Author Topic: [bb] Multi-Column ListBox by MattVonFat [ 1+ years ago ]  (Read 591 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Multi-Column ListBox by MattVonFat [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
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:
Code: [Select]
lst = CreateListView(10, 10, 300, 300, window)

To add a column use:
Code: [Select]
AddListViewColumn(lst,"Column One",90)

To add an item:
Code: [Select]
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:
Code: [Select]
DeleteListViewColumn(lst,1)

To remove an item:
Code: [Select]
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:
Code: [Select]
LoadCursor%( ID, Cursor ):"LoadCursorA"
SetCursor%( ID ):"SetCursor"

In the user23.decls file and:
Code: [Select]
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:
Code: [Select]
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
  1. ;------------------------------------------------------
  2. ;CREATES A LISTVIEW
  3. Function CreateListView(x%,y%,width%,height%,parent%)
  4.        
  5.         bank = CreateBank(12)
  6.         canvas = CreateCanvas(x,y,width,height,parent)
  7.        
  8.         PokeInt bank, 0, canvas
  9.         PokeInt bank, 4, x%
  10.         PokeInt bank, 8, y%
  11.                
  12.         UpdateListView(bank)
  13.        
  14.         Return bank
  15.        
  16. End Function
  17.  
  18. ;------------------------------------------------------
  19. ;UPDATES THE LISTVIEW
  20. Function UpdateListView(bank%,params%=0)
  21.        
  22.         If bank<12 Then RuntimeError("Gadget ListView Error")
  23.        
  24.         canvas = PeekInt(bank,0)
  25.        
  26.        
  27.         xwidth = 0
  28.         width = ClientWidth(canvas)
  29.         height = ClientHeight(canvas)
  30.        
  31.         colNum = 1
  32.        
  33.         SetBuffer CanvasBuffer(canvas)
  34.         Cls
  35.         For c.column = Each column
  36.                 Color 235, 234, 219
  37.                 Rect xwidth%, 0, xwidth%+ccolWidth%, 18, True
  38.                 Color 226, 222, 205
  39.                 Line xwidth%, 18, xwidth%+ccolWidth%, 18
  40.                 Color 214, 210, 194
  41.                 Line xwidth%, 19, xwidth%+ccolWidth%, 19
  42.                 Color 203, 199, 184
  43.                 Line xwidth%, 20, xwidth%+ccolWidth%, 20
  44.                 SetFont listViewFont%
  45.                 Color 0, 0, 0
  46.                 Text xwidth%+8, 3, ccolName%
  47.                 Color 255, 255, 255
  48.                 Rect xwidth%, 20, width%-xwidth%, height, True
  49.                 Color 199, 197, 178
  50.                 Line xwidth%+ccolWidth%-2, 3, xwidth%+ccolWidth%-2, 17
  51.                 Color 255, 255, 255
  52.                 Line xwidth%+ccolWidth%-1, 3, xwidth%+ccolWidth%-1, 17
  53.                 textStart% = 20
  54.                 highexist = False
  55.                 deleted = False
  56.                 For ci.item = Each item
  57.                 If params = textStart% And deleted = False And cicol = colNum
  58.                         Delete ci
  59.                         deleted = True
  60.                 Else
  61.                         If cicol = colNum
  62.                                 If highrow% > textStart Or highrow% = textStart And highrow < textStart+20
  63.                                         highExist = True
  64.                                         Color 49, 106, 197
  65.                                         Rect xwidth%, textStart, ccolWidth%, 20, True
  66.                                         Color 255, 255, 255
  67.                                         Text xwidth%+10, textStart+3, ciitemText$
  68.                                 Else
  69.                                         Color 0, 0, 0
  70.                                         Text xwidth%+10, textStart+3, ciitemText$
  71.                                 End If
  72.                                 textStart = textStart + 20
  73.                         End If
  74.                 End If
  75.                 Next
  76.                 If highexist = False Then highrow = 0
  77.                 xwidth% = xwidth% + ccolWidth%
  78.                 colNum = colNum+1
  79.         Next
  80.         Color 235, 234, 219
  81.         Rect xwidth%, 0, width%+50, 18, True
  82.         Color 226, 222, 205
  83.         Line xwidth%, 18, width%+50, 18
  84.         Color 214, 210, 194
  85.         Line xwidth%, 19, width%+50, 19
  86.         Color 203, 199, 184
  87.         Line xwidth%, 20, width%+50, 20
  88.         Color 255, 255, 255
  89.         Rect xwidth%, 20, width%+50, height, True
  90.         Color 0, 0, 0
  91.         Rect 0, 0, width%, height, False
  92.         FlipCanvas(canvas)
  93.        
  94.         Return
  95.        
  96. End Function
  97.  
  98. ;------------------------------------------------------
  99. ;ADDS A COLUMN TO THE LIST VIEW
  100. Function AddListViewColumn(listView,columnName$,width%)
  101.        
  102.         c.column = New column
  103.         ccolWidth% = width%
  104.         ccolName$ = columnName$
  105.         UpdateListView(listView)
  106.        
  107. End Function
  108.  
  109. ;------------------------------------------------------
  110. ;REMOVES A COLUMN FROM A LISTVIEW
  111. Function DeleteListViewColumn(listView,col1%)
  112.        
  113.         colNum% = 1
  114.        
  115.         For c.column = Each column
  116.                 If colNum% = col1%
  117.                         For ci.item = Each item
  118.                                 If cicol = col1%
  119.                                         Delete ci
  120.                                 Else
  121.                                         cicol = cicol-1
  122.                                 End If
  123.                         Next
  124.                         Delete c
  125.                         Exit
  126.                 Else
  127.                         colNum% = colNum+1
  128.                 End If
  129.         Next
  130.                
  131.         UpdateListView(listView)
  132.                        
  133. End Function
  134.  
  135. ;------------------------------------------------------
  136. ;ADDS AN ITEM TO LISTVIEW
  137. Function AddListViewItem(listView,items$) ;A COMMA SEPARATED LIST OF VALUES
  138.        
  139.         off% = 0
  140.         count% = 1
  141.         Repeat
  142.                 pos% = Instr(items$,",",off%)
  143.                 If pos% = 0
  144.                         Exit
  145.                 Else
  146.                         count% = count% + 1
  147.                         off% = pos%+1
  148.                 End If
  149.         Forever
  150.        
  151.         Dim words(count%)
  152.        
  153.         begin% = 0
  154.        
  155.         For a = 1 To count%
  156.                 pos% = Instr(items$,",",begin%)
  157.                 If pos% = 0
  158.                         words(a) = Mid(items$,begin,Len(items$)-begin+1)
  159.                 Else
  160.                         words(a) = Mid(items$,begin,pos%-begin)
  161.                         begin% = pos%+1
  162.                 End If
  163.         Next
  164.        
  165.         count% = 1
  166.        
  167.         For c.column = Each column
  168.                 ci.item = New item
  169.                 cicol = count%
  170.                 ciitemText$ = words(count%)
  171.                 count% = count%+1
  172.         Next
  173.        
  174.         rows% = rows% + 1
  175.         UpdateListView(listView)
  176.        
  177. End Function
  178.  
  179. ;------------------------------------------------------
  180. ;REMOVES AN ITEM FROM LISTVIEW
  181. Function DeleteListViewItem(listView,item%)
  182.        
  183.         rowDel% = (item%*20)
  184.         UpdateListView(listView,rowDel%)
  185.  
  186. End Function
  187.  
  188. ;------------------------------------------------------
  189. ;REFRESHES LISTVIEW
  190. Function ListViewRefresh(listView,event)
  191.        
  192.         If event = $203
  193.                 If Not EventSource() = PeekInt(listView,0) Then Return
  194.                
  195.                 mx = EventX()
  196.                 my = EventY()
  197.                
  198.                 For c.column = Each column
  199.                         If mx > (ccolWidth%+x)-4 And mx < (ccolWidth%+x)+4 And my > 0 And my <20
  200.                                 cursor = LoadCursor(0,32644)
  201.                                 SetCursor(cursor)
  202.                         End If
  203.                         x = x + ccolWidth
  204.                 Next           
  205.         ElseIf event = $201
  206.                 If Not EventSource() = PeekInt(listView,0) Then Return
  207.                
  208.                 x = 0
  209.                
  210.                 For c.column = Each column
  211.                         If mx > (ccolWidth%+x)-4 And mx < (ccolWidth%+x)+4 And my > 0 And my <20
  212.                                 cursor = LoadCursor(0,32644)
  213.                                 SetCursor(cursor)
  214.                                 While WaitEvent() <> $202
  215.                                         If EventID() = $203
  216.                                                 ccolWidth% = EventX()-x
  217.                                         End If
  218.                                 Wend
  219.                                 UpdateListView(listView)
  220.                                 Exit
  221.                         End If
  222.                         x = ccolWidth%
  223.                 Next
  224.                
  225.                 If my > 20
  226.                         mystr$ = Str(my)
  227.                         rowstr$ = Left(mystr,Len(my)-1) + "0"
  228.                         highrow% = Int(rowstr$)
  229.                 End If
  230.                
  231.         End If
  232.        
  233.         UpdateListView(listView)
  234.        
  235. End Function
  236.  
  237. ;------------------------------------------------------
  238. ;GETS SELECTED LISTVIEW ITEM
  239. Function SelectedListViewItem()
  240.        
  241.         If highrow = 0 Then Return 0
  242.        
  243.         selected% = highrow/20
  244.         Return selected%
  245.        
  246. End Function
  247.  
  248. ;------------------------------------------------------
  249. ;SELECTS LIST VIEW ITEM
  250. Function SelectListViewItem(listView,item%)
  251.        
  252.         highrow% = (item%*20)
  253.         UpdateListView(listView)
  254.        
  255. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal