import wx
#from wxPython.wx import *
#from wxPython.grid import *
#import wx.grid
import wx.grid as gridlib
#from wx.grid import Grid
import os
import sys

class MyForm(wx.Frame):
   
    def __init__(self):
        #- Initialize the window:
        wx.Frame.__init__(self, None, wx.ID_ANY, "EC2 Tool", size=(400,350))

        # Add a panel so it looks correct on all platforms
        self.panel = wx.Panel(self, wx.ID_ANY)

        self.num_of_columns = 5
        self.num_of_rows = 10
        self.grid = gridlib.Grid(self.panel)
        self.grid.CreateGrid(self.num_of_rows, self.num_of_columns)

        # Add the click events:
        self.grid.Bind(gridlib.EVT_GRID_CELL_RIGHT_CLICK, self.handler_onRightClick)
        self.grid.Bind(gridlib.EVT_GRID_LABEL_LEFT_CLICK, self.handler_onRowClick)

        for i in range(self.num_of_columns):
            self.grid.SetColLabelValue(i, str(i))
            self.grid.SetColSize(i, 100)
            
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.grid, 1, wx.EXPAND, self.num_of_columns)
        self.panel.SetSizer(sizer)
        
        self.createGridData()
        

    def createGridData(self):
        print '------------create grid with dummy data'

        for c in range(self.num_of_columns):
            for r in range(self.num_of_rows):
                self.grid.SetCellBackgroundColour(r, c, 'white')
                self.grid.SetCellValue(r, c, str(r) + ' and ' + str(c))

    def handler_onRowClick(self, event):
        print 'row clicked!'

    def handler_onRightClick(self, event):
        print 'right click!'
        row = event.GetRow()
        print 'row: ', row
        
        #attr = gridlib.GridCellAttr()
        #attr.SetBackgroundColour('pink')
        #self.grid.SetSelectionMode(Grid.SelectRows) 
        #- Why doesn't this work:
        #self.grid.SetRowAttr(row, attr)
        self.grid.SetSelectionBackground(wx.NamedColour('pink'))
        self.grid.SelectRow(row, False) # True if the selection should be expanded
        self.grid.Refresh()

# Run the program 
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyForm().Show()
    app.MainLoop()
