# -*- coding: utf-8 -*-#

import wx
import wx.grid as gridlib

class MyTableBase(gridlib.PyGridTableBase):
    def __init__(self):
        gridlib.PyGridTableBase.__init__(self)

        self.data = {0:["value 1", "value 2"],
                     1:["value 3", "value 4", "value 5"]}

        self.column_labels = [unicode(u"Label 1"),
                              unicode(u"Label 2"),
                              unicode(u"Label 3")]

        self.myChoiceEditor = gridlib.GridCellChoiceEditor(["xxx", "yyy", "zzz"],
                                              allowOthers = False)


        self.currentRows = self.GetNumberRows()
        self.currentColumns = self.GetNumberCols()

    def GetColLabelValue(self, col):
        return self.column_labels[col]

    def GetNumberRows(self):
        return len(self.data.keys())

    def GetNumberCols(self):
        return len(self.column_labels)

    def GetValue(self, row, col):
        try:
            if col > self.GetNumberCols():
                raise IndexError
            return self.data[row][col]
        except IndexError:
            return None

    def IsEmptyCell(self, row, col):
        try:
            if self.data[row][col] is not None:
                return True
            else:
                return False
        except IndexError:
            pass

    def GetAttr(self, row, col, kind):
        attr = gridlib.GridCellAttr()
        attr.SetEditor(self.myChoiceEditor)
        attr.IncRef()
        return attr

class MyDataGrid(gridlib.Grid):
    def __init__(self, parent):
        gridlib.Grid.__init__(self, parent, wx.NewId())
        self.base_table = MyTableBase()
        self.SetTable(self.base_table)


if __name__ == '__main__':
    app = wx.App(redirect = False)
    dlg = wx.Dialog(None, wx.NewId(), title = u"Test",
                      style=wx.RESIZE_BORDER | wx.DEFAULT_DIALOG_STYLE)
#    frame = wx.Frame(None, wx.NewId(), title = u"Test")
#    panel = wx.Panel(dlg, wx.NewId())
#    panel = wx.Panel(frame, wx.NewId())
#    grid_ = MyDataGrid(panel)
    grid_ = MyDataGrid(dlg)
#    grid_ = MyDataGrid(frame)
    sb = wx.BoxSizer(wx.HORIZONTAL)
    sb.Add(grid_)
#    panel.SetSizer(sb)
    dlg.ShowModal()
    dlg.Destroy()
#    frame.Show()
    app.MainLoop()
