import wx
import wx.grid as gridlib

class MyGridTable(gridlib.PyGridTableBase):
	#----------------------------------------------------------------------
	def __init__(self, data):
		wx.grid.PyGridTableBase.__init__(self)
		
		self.data = data
		self.colLabels = ("Last", "First")

	def GetColLabelValue(self, col):
		return self.colLabels[col]
	
	#----------------------------------------------------------------------
	def GetNumberRows(self):
		"""Return the number of rows in the grid"""
		return len(self.data)

	#----------------------------------------------------------------------
	def GetNumberCols(self):
		"""Return the number of columns in the grid"""
		return len(self.data[0]) - 1

	#----------------------------------------------------------------------
	def IsEmptyCell(self, row, col):
		"""Return True if the cell is empty"""
		return False

	#----------------------------------------------------------------------
	def GetTypeName(self, row, col):
		"""Return the name of the data type of the value in the cell"""
		return type(self.data[row][col + 1])

	#----------------------------------------------------------------------
	def GetValue(self, row, col):
		"""Return the value of a cell"""
		return self.data[row][col + 1]

	#----------------------------------------------------------------------
	def SetValue(self, row, col, value):
		"""Set the value of a cell"""
		pass
	#----------------------------------------------------------------------
	def ResetView(self):
		"""Trim/extend the control's rows and update all values"""
		self.getGrid().BeginBatch()
		for current, new, delmsg, addmsg in [
				(self.currentRows, self.GetNumberRows(), wx.grid.GRIDTABLE_NOTIFY_ROWS_DELETED, wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED),
				(self.currentColumns, self.GetNumberCols(), wx.grid.GRIDTABLE_NOTIFY_COLS_DELETED, wx.grid.GRIDTABLE_NOTIFY_COLS_APPENDED),
		]:
				if new < current:
						msg = wx.grid.GridTableMessage(
								self,
								delmsg,
								new,	# position
								current-new,
						)
						self.getGrid().ProcessTableMessage(msg)
				elif new > current:
						msg = wx.grid.GridTableMessage(
								self,
								addmsg,
								new-current
						)
						self.getGrid().ProcessTableMessage(msg)
		self.UpdateValues()
		self.getGrid().EndBatch()

		# The scroll bars aren't resized (at least on windows)
		# Jiggling the size of the window rescales the scrollbars
		h,w = grid.GetSize()
		grid.SetSize((h+1, w))
		grid.SetSize((h, w))
		grid.ForceRefresh()
	#----------------------------------------------------------------------
	def UpdateValues( self ):
		"""Update all displayed values"""
		msg = wx.grid.GridTableMessage(self, wx.grid.GRIDTABLE_REQUEST_VIEW_GET_VALUES)
		self.getGrid().ProcessTableMessage(msg)
	#----------------------------------------------------------------------
	def DeleteRows(self, row, updateLabel=True):
		print "DeleteRows"
		# deleteCount = 0
		# rows = rows[:]
		# rows.sort()

		self.data.pop(row)
#----------------------------------------------------------------------
class TestFrame(wx.Frame):
	def __init__(self, parent):
		wx.Frame.__init__(self, parent, -1, "A Grid", size=(275, 275))
		panel = wx.Panel(self)
		grid = gridlib.Grid(panel)
		data = [("CF", "Bob", "Dernier"), ("2B", "Ryne", "Sandberg"),
					 ("LF", "Gary", "Matthews"), ("1B", "Leon", "Durham"),
					 ("RF", "Keith", "Moreland"), ("3B", "Ron", "Cey"),
					 ("C", "Jody", "Davis"), ("SS", "Larry", "Bowa"),
					 ("P", "Rick", "Sutcliffe")]
		table = MyGridTable(data)
		grid.SetTable(table, True)
		
		sizer = wx.BoxSizer(wx.VERTICAL)
		sizer.Add(grid, 1, wx.EXPAND)
		panel.SetSizer(sizer)
		self.grid = grid
		grid.Bind(gridlib.EVT_GRID_CELL_LEFT_CLICK, self.OnItemSelected)
	#----------------------------------------------------------------------
	def OnItemSelected(self, evt):
		print "OnItemSelected"
		rows = evt.GetRow()		
		self.grid.DeleteRows(1)
		self.grid.ForceRefresh()
		# self.grid.ResetView()
#----------------------------------------------------------------------
if __name__ == '__main__':
	app = wx.PySimpleApp()
	frame = TestFrame(None)
	frame.Show(True)
	app.MainLoop()