wxListCtrl

BlitzMax Modules Forums/Brucey's Modules/wxListCtrl

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

I have changed the example to allow for the Last column to resize to the maximum length to fit the ListCtrl while the window is being resized.
There is a refreshed GUI file too.

ListCtrlTestGUI.bmx

'
' BlitzMax code generated with wxCodeGen v1.00 : 08 Mar 2008 21:44:49
' 
' 
' PLEASE DO "NOT" EDIT THIS FILE!
' 
SuperStrict

Import wx.wxFrame
Import wx.wxListCtrl
Import wx.wxPanel
Import wx.wxWindow


Type MyFrame2Base Extends wxFrame

	Field m_panel2:wxPanel
	Field m_listCtrlTEST:wxListCtrl

	Const IDListCtrlTEST:Int = 1000

	Method Create:MyFrame2Base(parent:wxWindow = Null,id:Int = wxID_ANY, title:String = "", x:Int = -1, y:Int = -1, w:Int = -1, h:Int = -1, style:Int = wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL)
		Return MyFrame2Base(Super.Create(parent, id, title, x, y, w, h, style))
	End Method

	Method OnInit()
		SetMinSize(400,300)

		Local bSizer18:wxBoxSizer
		bSizer18 = New wxBoxSizer.Create(wxHORIZONTAL)
		m_panel2 = New wxPanel.Create(Self, wxID_ANY,,,,, wxTAB_TRAVERSAL)

		Local bSizer19:wxBoxSizer
		bSizer19 = New wxBoxSizer.Create(wxHORIZONTAL)
		m_listCtrlTEST = New wxListCtrl.Create(m_panel2, IDListCtrlTEST,,,,, wxLC_HRULES|wxLC_REPORT|wxLC_SINGLE_SEL)
		m_listCtrlTEST.SetMinSize(400,200)
		bSizer19.Add(m_listCtrlTEST, 1, wxALL, 5)

		m_panel2.SetSizer(bSizer19)
		m_panel2.Layout()
		bSizer18.Add(m_panel2, 1, wxEXPAND | wxALL, 5)

		SetSizer(bSizer18)
		Layout()
		bSizer18.Fit(Self)

		ConnectAny(wxEVT_SIZE, _WindowResized)
	End Method

	Function _WindowResized(event:wxEvent)
		MyFrame2Base(event.parent).WindowResized(wxSizeEvent(event))
	End Function

	Method WindowResized(event:wxSizeEvent)
		DebugLog "Please override MyFrame2.WindowResized()"
		event.Skip()
	End Method

End Type


ListCtrlTest.bmx
'
' 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.SetColumnHeadings(frame.m_listCtrlTEST)
		frame.PopulateList(frame.m_listCtrlTEST)
		
		SetTopWindow(frame)

		Return True
	End Method

End Type



Type MyFrame2 Extends MyFrame2Base

	Method OnInit()
		Super.OnInit()
	End Method

' **************************************************
' These 2 methods are used to OverRide the Resizing event from the GUI file	
	Method ChangeColumnWidth(ListControl:wxListCtrl)
		DebugLog "ChangeColumnWidth Method Called"
		
		Local rect:wxRect = ListControl.GetRectRect()
		Local ListWidth:Int = rect.GetWidth()
		DebugLog ListWidth
		
		ListControl.SetColumnWidth(4,ListWidth - (20 + 140 + 120 + 40 + 3)) ' The Plus 3 on the end is to remove the scroll bar. Not sure why though
		
	End Method
	
	Method WindowResized(event:wxSizeEvent)
		DebugLog "Called First"
		ChangeColumnWidth(m_listCtrlTEST)
		' LEAVE this line IN or the ListCtrl won't resize with the Window...
		event.Skip()
	End Method
' **************************************************

	Method SetColumnHeadings(ListControl:wxListCtrl)
		' m_listCtrlTEST is the name of the ListCtrl from the GUI file
		ListControl.InsertColumn(0, ("C1"), wxLIST_FORMAT_CENTRE, 20)	' Small Column, Centred
		ListControl.InsertColumn(1, ("C2"), wxLIST_FORMAT_LEFT, 140)		' Larger Column, Left Justified
		ListControl.InsertColumn(2, ("C3"), wxLIST_FORMAT_RIGHT, 120)	' Larger Column, Right Justified
		ListControl.InsertColumn(3, ("C4"), wxLIST_FORMAT_CENTRE, 40)	' Small Column, Centred
		ListControl.InsertColumn(4, ("C5"), wxLIST_FORMAT_CENTRE, 140)	' AutoSized for Contents, 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.DeleteAllItems()
		' 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 = "Column 2 : Row No. " + 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 : Row No. " + RowNumber
		
		If Not DataList Then DataList = CreateList()
		DataList.AddLast( DataSet )

	End Function

End Type


I have asked Brucey if there is better way to do this, even though this works fine.

Cheers
Glenn