]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/GridCustTable.py
reSWIGged
[wxWidgets.git] / wxPython / demo / GridCustTable.py
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.
7
8 import wx
9 import wx.grid as gridlib
10
11 #---------------------------------------------------------------------------
12
13 class CustomDataTable(gridlib.PyGridTableBase):
14 def __init__(self, log):
15 gridlib.PyGridTableBase.__init__(self)
16 self.log = log
17
18 self.colLabels = ['ID', 'Description', 'Severity', 'Priority', 'Platform',
19 'Opened?', 'Fixed?', 'Tested?', 'TestFloat']
20
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',
30 ]
31
32 self.data = [
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]
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):
50 try:
51 return not self.data[row][col]
52 except IndexError:
53 return True
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
74 msg = gridlib.GridTableMessage(self, # The table
75 gridlib.GRIDTABLE_NOTIFY_ROWS_APPENDED, # what we did to it
76 1 # how many
77 )
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
91 # natively by the editor/renderer if they know how to convert.
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):
99 colType = self.dataTypes[col].split(':')[0]
100 if typeName == colType:
101 return True
102 else:
103 return False
104
105 def CanSetValueAs(self, row, col, typeName):
106 return self.CanGetValueAs(row, col, typeName)
107
108
109
110
111
112 #---------------------------------------------------------------------------
113
114
115
116 class CustTableGrid(gridlib.Grid):
117 def __init__(self, parent, log):
118 gridlib.Grid.__init__(self, parent, -1)
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.
125 self.SetTable(table, True)
126
127 self.SetRowLabelSize(0)
128 self.SetMargins(0,0)
129 self.AutoSizeColumns(False)
130
131 gridlib.EVT_GRID_CELL_LEFT_DCLICK(self, self.OnLeftDClick)
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
143 class TestFrame(wx.Frame):
144 def __init__(self, parent, log):
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)
151 grid = CustTableGrid(p, log)
152 b = wx.Button(p, -1, "Another Control...")
153 b.SetDefault()
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)
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"
166
167
168 #---------------------------------------------------------------------------
169
170 if __name__ == '__main__':
171 import sys
172 app = wx.PySimpleApp()
173 frame = TestFrame(None, sys.stdout)
174 frame.Show(True)
175 app.MainLoop()
176
177
178 #---------------------------------------------------------------------------