]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/GridCustTable.py
A little tweak to the usage text
[wxWidgets.git] / wxPython / demo / GridCustTable.py
CommitLineData
8fa876ca
RD
1# 11/6/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2#
3# o Updated for wx namespace
4# o Also corrected minor bug where 'true' was being used instead of 'True'.
5# Doesn't fail for * import (I guess that is still defined in wx), but does
6# in the manner we're importing wx now.
f6bcfd97 7
8fa876ca
RD
8import wx
9import wx.grid as gridlib
f6bcfd97 10
8fa876ca 11#---------------------------------------------------------------------------
f6bcfd97 12
8fa876ca 13class CustomDataTable(gridlib.PyGridTableBase):
f6bcfd97 14 def __init__(self, log):
8fa876ca 15 gridlib.PyGridTableBase.__init__(self)
f6bcfd97
BP
16 self.log = log
17
18 self.colLabels = ['ID', 'Description', 'Severity', 'Priority', 'Platform',
f19533bf 19 'Opened?', 'Fixed?', 'Tested?', 'TestFloat']
f6bcfd97 20
8fa876ca
RD
21 self.dataTypes = [gridlib.GRID_VALUE_NUMBER,
22 gridlib.GRID_VALUE_STRING,
23 gridlib.GRID_VALUE_CHOICE + ':only in a million years!,wish list,minor,normal,major,critical',
24 gridlib.GRID_VALUE_NUMBER + ':1,5',
25 gridlib.GRID_VALUE_CHOICE + ':all,MSW,GTK,other',
26 gridlib.GRID_VALUE_BOOL,
27 gridlib.GRID_VALUE_BOOL,
28 gridlib.GRID_VALUE_BOOL,
29 gridlib.GRID_VALUE_FLOAT + ':6,2',
f19533bf 30 ]
f6bcfd97
BP
31
32 self.data = [
f19533bf
RD
33 [1010, "The foo doesn't bar", "major", 1, 'MSW', 1, 1, 1, 1.12],
34 [1011, "I've got a wicket in my wocket", "wish list", 2, 'other', 0, 0, 0, 1.50],
35 [1012, "Rectangle() returns a triangle", "critical", 5, 'all', 0, 0, 0, 1.56]
f6bcfd97
BP
36
37 ]
38
39
40 #--------------------------------------------------
41 # required methods for the wxPyGridTableBase interface
42
43 def GetNumberRows(self):
44 return len(self.data) + 1
45
46 def GetNumberCols(self):
47 return len(self.data[0])
48
49 def IsEmptyCell(self, row, col):
1e4a197e
RD
50 try:
51 return not self.data[row][col]
52 except IndexError:
d14a1e28 53 return True
f6bcfd97
BP
54
55 # Get/Set values in the table. The Python version of these
56 # methods can handle any data-type, (as long as the Editor and
57 # Renderer understands the type too,) not just strings as in the
58 # C++ version.
59 def GetValue(self, row, col):
60 try:
61 return self.data[row][col]
62 except IndexError:
63 return ''
64
65 def SetValue(self, row, col, value):
66 try:
67 self.data[row][col] = value
68 except IndexError:
69 # add a new row
70 self.data.append([''] * self.GetNumberCols())
71 self.SetValue(row, col, value)
72
73 # tell the grid we've added a row
8fa876ca
RD
74 msg = gridlib.GridTableMessage(self, # The table
75 gridlib.GRIDTABLE_NOTIFY_ROWS_APPENDED, # what we did to it
76 1 # how many
77 )
f6bcfd97
BP
78
79 self.GetView().ProcessTableMessage(msg)
80
81
82 #--------------------------------------------------
83 # Some optional methods
84
85 # Called when the grid needs to display labels
86 def GetColLabelValue(self, col):
87 return self.colLabels[col]
88
89 # Called to determine the kind of editor/renderer to use by
90 # default, doesn't necessarily have to be the same type used
8b9a4190 91 # natively by the editor/renderer if they know how to convert.
f6bcfd97
BP
92 def GetTypeName(self, row, col):
93 return self.dataTypes[col]
94
95 # Called to determine how the data can be fetched and stored by the
96 # editor and renderer. This allows you to enforce some type-safety
97 # in the grid.
98 def CanGetValueAs(self, row, col, typeName):
1e4a197e 99 colType = self.dataTypes[col].split(':')[0]
f6bcfd97 100 if typeName == colType:
d14a1e28 101 return True
f6bcfd97 102 else:
1e4a197e 103 return False
f6bcfd97
BP
104
105 def CanSetValueAs(self, row, col, typeName):
106 return self.CanGetValueAs(row, col, typeName)
107
108
109
110
111
112#---------------------------------------------------------------------------
113
114
115
8fa876ca 116class CustTableGrid(gridlib.Grid):
f6bcfd97 117 def __init__(self, parent, log):
8fa876ca 118 gridlib.Grid.__init__(self, parent, -1)
f6bcfd97
BP
119
120 table = CustomDataTable(log)
121
122 # The second parameter means that the grid is to take ownership of the
123 # table and will destroy it when done. Otherwise you would need to keep
124 # a reference to it and call it's Destroy method later.
d14a1e28 125 self.SetTable(table, True)
f6bcfd97
BP
126
127 self.SetRowLabelSize(0)
128 self.SetMargins(0,0)
1e4a197e 129 self.AutoSizeColumns(False)
f6bcfd97 130
8fa876ca 131 gridlib.EVT_GRID_CELL_LEFT_DCLICK(self, self.OnLeftDClick)
f6bcfd97
BP
132
133
134 # I do this because I don't like the default behaviour of not starting the
135 # cell editor on double clicks, but only a second click.
136 def OnLeftDClick(self, evt):
137 if self.CanEnableCellControl():
138 self.EnableCellEditControl()
139
140
141#---------------------------------------------------------------------------
142
8fa876ca 143class TestFrame(wx.Frame):
f6bcfd97 144 def __init__(self, parent, log):
8fa876ca
RD
145
146 wx.Frame.__init__(
147 self, parent, -1, "Custom Table, data driven Grid Demo", size=(640,480)
148 )
149
150 p = wx.Panel(self, -1, style=0)
1e4a197e 151 grid = CustTableGrid(p, log)
8fa876ca 152 b = wx.Button(p, -1, "Another Control...")
1e4a197e 153 b.SetDefault()
8fa876ca
RD
154 self.Bind(wx.EVT_BUTTON, self.OnButton, b)
155 b.Bind(wx.EVT_SET_FOCUS, self.OnButtonFocus)
156 bs = wx.BoxSizer(wx.VERTICAL)
157 bs.Add(grid, 1, wx.GROW|wx.ALL, 5)
1e4a197e
RD
158 bs.Add(b)
159 p.SetSizer(bs)
160
161 def OnButton(self, evt):
162 print "button selected"
163
164 def OnButtonFocus(self, evt):
165 print "button focus"
f6bcfd97
BP
166
167
168#---------------------------------------------------------------------------
169
170if __name__ == '__main__':
171 import sys
8fa876ca 172 app = wx.PySimpleApp()
f6bcfd97 173 frame = TestFrame(None, sys.stdout)
d14a1e28 174 frame.Show(True)
f6bcfd97
BP
175 app.MainLoop()
176
177
178#---------------------------------------------------------------------------