wxListCtrl

BlitzMax Modules Forums/Brucey's Modules/wxListCtrl

Here is a working example of the ListCtrl with some comments.
Use the ListCtrlTestGUI.bmx from the third post

Thanks to Brucey and David for their help.

Cheers
Glenn

'
' Example application stub generated by wxCodeGen v1.00 : 02 Mar 2008 15:03:08
'
SuperStrict

Framework wx.wxApp
' import the GUI file
Import "ListCtrlTestGUI.bmx"
Import brl.LinkedList

New MyApp.run()

Type MyApp Extends wxApp

	Method OnInit:Int()

?macos
		' make the default Mac font for controls not so big
		wxSystemOptions.SetOption(wxWINDOW_DEFAULT_VARIANT, wxWINDOW_VARIANT_SMALL)
?
		wxInitAllImageHandlers()

		wxImage.AddHandler( New wxXPMHandler ) ' for Mac/Win32 (it is default on Linux)

		Local frame:MyFrame2 = MyFrame2(New MyFrame2.Create(, , "Glenns ListCtrl Test"))

		frame.show()
		' call the method that reads my Data Type and Populates the ListCtrl
		frame.PopulateList(frame.m_listCtrlTEST)
		
		SetTopWindow(frame)

		Return True
	End Method

End Type



Type MyFrame2 Extends MyFrame2Base

	Method OnInit()
		Super.OnInit()
	End Method

	Method SetColumnHeadings()
		' m_listCtrlTEST is the name of the ListCtrl from the GUI file
		m_listCtrlTEST.InsertColumn(0, ("C1"), wxLIST_FORMAT_CENTRE, 20)	' Small Column, Centred
		m_listCtrlTEST.InsertColumn(1, ("C2"), wxLIST_FORMAT_LEFT, 140)		' Larger Column, Left Justified
		m_listCtrlTEST.InsertColumn(2, ("C3"), wxLIST_FORMAT_RIGHT, 120)	' Larger Column, Right Justified
		m_listCtrlTEST.InsertColumn(3, ("C4"), wxLIST_FORMAT_CENTRE, 40)	' Small Column, Centred
		m_listCtrlTEST.InsertColumn(4, ("C5"), wxLIST_FORMAT_CENTRE, 80)	' Medium Column, Centred.
																			' This columns data is quite Long and the column needs to be expanded with the mouse to see it all.

	End Method

	Method PopulateList(ListControl:wxListCtrl)
		' Clear the listctrl, including the column definitions
		ListControl.ClearAll()
		' Reset the Column Definitions
		SetColumnHeadings()
		' Call the Create Function for the TDataSet type multiple times
		TDataSet.Create(1)
		TDataSet.Create(2)
		TDataSet.Create(3)
		TDataSet.Create(4)
		' If the ListCtrl exists (m_listCtrlTEST) then get the list from the TDataSet type
		If ListControl Then
			Local list:TList = TDataSet.DataList
					
			Local i:Int = 0	' i is the row number in the listctrl
			For Local dataset:TDataSet = EachIn list
				' InsertIntoList(Row Number, the data for this element of the list, the name of the ListCtrl)
				InsertIntoList(i, dataset, ListControl)
				' Increment i so you arer always adding to the end of the ListCtrl
				' comment this line out if you want your listctrl populated in reverse, ie i = 0
				i:+ 1
			Next

		End If
	End Method

	Method InsertIntoList(index:Int, dataSet:TDataSet, list:wxListCtrl)
		' wxListItem - This type stores information about a wxListCtrl item or column.
		
		' Some comments taken from <a href="http://www.gibbongames.com/content/view/52/50/" target="_blank">http://www.gibbongames.com/content/view/52/50/</a>
		' wxListItem encapsulates the contents of a given listbox cell.
		' So each row/column combination has its own wxListItem structure.
		Local item:wxListItem = New wxListItem.Create()
		' Define all the parameters for the column, then use list.SetItem(item) to bind it
		item.SetImage(-1)
		item.SetText(dataSet.c1)
		item.SetId(index)
		
		' Apparently Column 0 requires InsertItem, but Column n > 0 requires SetItem (assuming that Column 0 has been previously inserted).
		list.InsertItem(item)

		item.SetText(dataSet.c2)
		item.SetColumn(1)
		list.SetItem(item)

		item.SetText(dataSet.c3)
		item.SetColumn(2)
		list.SetItem(item)

		' you can use SetImage here but you need bitmap array for the listctrl (which i haven't looked up yet)
		If dataSet.c4 = 1 Then
			item.SetText("Yes")
			' itemSetImage(1) ' or img_Checked if you have defined it as a CONST
		Else
			item.SetText("No")
			' itemSetImage(0) ' or img_NotChecked if you have defined it as a CONST
		End If
		item.SetColumn(3)
		list.SetItem(item)

		item.SetText(dataSet.c5)
		item.SetColumn(4)
		list.SetItem(item)

	End Method


End Type

Type TDataSet
	' just a simple DataSet being stored in a TList
	
	Field c1:Int
	Field c2:String
	Field c3:String
	Field c4:Int
	Field c5:String
		
	Global DataList:TList
	
	Method Init:TDataSet()
		Return Self
	End Method
	
	Function Create(RowNumber:Int)

		Local DataSet:TDataSet = New TDataSet

		DataSet.c1 = RowNumber
		DataSet.c2 = "Column 2 : Row No. " + RowNumber
		DataSet.c3 = "Row Number " + RowNumber
		' if RowNumber is even then set to 1 else 0 - used for the Yes/No or the img_Checked/img_NotChecked image 
		If RowNumber Mod 2 = 0 Then 
			DataSet.c4 = 1
		Else
			DataSet.c4 = 0
		EndIf
		DataSet.c5 = "Column 5 for Row Number " + RowNumber + " Has a long piece of Data to display"
		
		If Not DataList Then DataList = CreateList()
		DataList.AddLast( DataSet )

	End Function

End Type