]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/GridSimple.py
A little tweak to the usage text
[wxWidgets.git] / wxPython / demo / GridSimple.py
1 # 11/6/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2 #
3 # o Updated for V2.5
4 # o The mixin features were all commented out. Is it broke? Should it even
5 # be in the source? Or is it left as an exercise to the reader?
6 #
7 # 11/25/2003 - Jeff Grimmett (grimmtooth@softhome.net)
8 #
9 # o Robin confirms, this is tutorial code. But be warned! It has not been
10 # converted OR tested!
11 #
12
13 import wx
14 import wx.grid as gridlib
15 #import wx.lib.mixins.grid as mixins
16
17 #---------------------------------------------------------------------------
18
19 class SimpleGrid(gridlib.Grid): ##, mixins.GridAutoEditMixin):
20 def __init__(self, parent, log):
21 gridlib.Grid.__init__(self, parent, -1)
22 ##mixins.GridAutoEditMixin.__init__(self)
23 self.log = log
24 self.moveTo = None
25
26 self.Bind(wx.EVT_IDLE, self.OnIdle)
27
28 self.CreateGrid(25, 25) #, wxGrid.wxGridSelectRows)
29 ##self.EnableEditing(False)
30
31 # simple cell formatting
32 self.SetColSize(3, 200)
33 self.SetRowSize(4, 45)
34 self.SetCellValue(0, 0, "First cell")
35 self.SetCellValue(1, 1, "Another cell")
36 self.SetCellValue(2, 2, "Yet another cell")
37 self.SetCellValue(3, 3, "This cell is read-only")
38 self.SetCellFont(0, 0, wx.Font(12, wx.ROMAN, wx.ITALIC, wx.NORMAL))
39 self.SetCellTextColour(1, 1, wx.RED)
40 self.SetCellBackgroundColour(2, 2, wx.CYAN)
41 self.SetReadOnly(3, 3, True)
42
43 self.SetCellEditor(5, 0, gridlib.GridCellNumberEditor(1,1000))
44 self.SetCellValue(5, 0, "123")
45 self.SetCellEditor(6, 0, gridlib.GridCellFloatEditor())
46 self.SetCellValue(6, 0, "123.34")
47 self.SetCellEditor(7, 0, gridlib.GridCellNumberEditor())
48
49 self.SetCellValue(6, 3, "You can veto editing this cell")
50
51 #self.SetRowLabelSize(0)
52 #self.SetColLabelSize(0)
53
54 # attribute objects let you keep a set of formatting values
55 # in one spot, and reuse them if needed
56 attr = gridlib.GridCellAttr()
57 attr.SetTextColour(wx.BLACK)
58 attr.SetBackgroundColour(wx.RED)
59 attr.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))
60
61 # you can set cell attributes for the whole row (or column)
62 self.SetRowAttr(5, attr)
63
64 self.SetColLabelValue(0, "Custom")
65 self.SetColLabelValue(1, "column")
66 self.SetColLabelValue(2, "labels")
67
68 self.SetColLabelAlignment(wx.ALIGN_LEFT, wx.ALIGN_BOTTOM)
69
70 #self.SetDefaultCellOverflow(False)
71 #r = wxGridCellAutoWrapStringRenderer()
72 #self.SetCellRenderer(9, 1, r)
73
74 # overflow cells
75 self.SetCellValue( 9, 1, "This default cell will overflow into neighboring cells, but not if you turn overflow off.");
76 self.SetCellSize(11, 1, 3, 3);
77 self.SetCellAlignment(11, 1, wx.ALIGN_CENTRE, wx.ALIGN_CENTRE);
78 self.SetCellValue(11, 1, "This cell is set to span 3 rows and 3 columns");
79
80
81 editor = gridlib.GridCellTextEditor()
82 editor.SetParameters('10')
83 self.SetCellEditor(0, 4, editor)
84 self.SetCellValue(0, 4, "Limited text")
85
86
87 # test all the events
88 self.Bind(gridlib.EVT_GRID_CELL_LEFT_CLICK, self.OnCellLeftClick)
89 self.Bind(gridlib.EVT_GRID_CELL_RIGHT_CLICK, self.OnCellRightClick)
90 self.Bind(gridlib.EVT_GRID_CELL_LEFT_DCLICK, self.OnCellLeftDClick)
91 self.Bind(gridlib.EVT_GRID_CELL_RIGHT_DCLICK, self.OnCellRightDClick)
92
93 self.Bind(gridlib.EVT_GRID_LABEL_LEFT_CLICK, self.OnLabelLeftClick)
94 self.Bind(gridlib.EVT_GRID_LABEL_RIGHT_CLICK, self.OnLabelRightClick)
95 self.Bind(gridlib.EVT_GRID_LABEL_LEFT_DCLICK, self.OnLabelLeftDClick)
96 self.Bind(gridlib.EVT_GRID_LABEL_RIGHT_DCLICK, self.OnLabelRightDClick)
97
98 self.Bind(gridlib.EVT_GRID_ROW_SIZE, self.OnRowSize)
99 self.Bind(gridlib.EVT_GRID_COL_SIZE, self.OnColSize)
100
101 self.Bind(gridlib.EVT_GRID_RANGE_SELECT, self.OnRangeSelect)
102 self.Bind(gridlib.EVT_GRID_CELL_CHANGE, self.OnCellChange)
103 self.Bind(gridlib.EVT_GRID_SELECT_CELL, self.OnSelectCell)
104
105 self.Bind(gridlib.EVT_GRID_EDITOR_SHOWN, self.OnEditorShown)
106 self.Bind(gridlib.EVT_GRID_EDITOR_HIDDEN, self.OnEditorHidden)
107 self.Bind(gridlib.EVT_GRID_EDITOR_CREATED, self.OnEditorCreated)
108
109
110 def OnCellLeftClick(self, evt):
111 self.log.write("OnCellLeftClick: (%d,%d) %s\n" %
112 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
113 evt.Skip()
114
115 def OnCellRightClick(self, evt):
116 self.log.write("OnCellRightClick: (%d,%d) %s\n" %
117 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
118 evt.Skip()
119
120 def OnCellLeftDClick(self, evt):
121 self.log.write("OnCellLeftDClick: (%d,%d) %s\n" %
122 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
123 evt.Skip()
124
125 def OnCellRightDClick(self, evt):
126 self.log.write("OnCellRightDClick: (%d,%d) %s\n" %
127 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
128 evt.Skip()
129
130 def OnLabelLeftClick(self, evt):
131 self.log.write("OnLabelLeftClick: (%d,%d) %s\n" %
132 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
133 evt.Skip()
134
135 def OnLabelRightClick(self, evt):
136 self.log.write("OnLabelRightClick: (%d,%d) %s\n" %
137 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
138 evt.Skip()
139
140 def OnLabelLeftDClick(self, evt):
141 self.log.write("OnLabelLeftDClick: (%d,%d) %s\n" %
142 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
143 evt.Skip()
144
145 def OnLabelRightDClick(self, evt):
146 self.log.write("OnLabelRightDClick: (%d,%d) %s\n" %
147 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
148 evt.Skip()
149
150 def OnRowSize(self, evt):
151 self.log.write("OnRowSize: row %d, %s\n" %
152 (evt.GetRowOrCol(), evt.GetPosition()))
153 evt.Skip()
154
155 def OnColSize(self, evt):
156 self.log.write("OnColSize: col %d, %s\n" %
157 (evt.GetRowOrCol(), evt.GetPosition()))
158 evt.Skip()
159
160 def OnRangeSelect(self, evt):
161 if evt.Selecting():
162 self.log.write("OnRangeSelect: top-left %s, bottom-right %s\n" %
163 (evt.GetTopLeftCoords(), evt.GetBottomRightCoords()))
164 evt.Skip()
165
166
167 def OnCellChange(self, evt):
168 self.log.write("OnCellChange: (%d,%d) %s\n" %
169 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
170
171 # Show how to stay in a cell that has bad data. We can't just
172 # call SetGridCursor here since we are nested inside one so it
173 # won't have any effect. Instead, set coordinates to move to in
174 # idle time.
175 value = self.GetCellValue(evt.GetRow(), evt.GetCol())
176
177 if value == 'no good':
178 self.moveTo = evt.GetRow(), evt.GetCol()
179
180
181 def OnIdle(self, evt):
182 if self.moveTo != None:
183 self.SetGridCursor(self.moveTo[0], self.moveTo[1])
184 self.moveTo = None
185
186 evt.Skip()
187
188
189 def OnSelectCell(self, evt):
190 self.log.write("OnSelectCell: (%d,%d) %s\n" %
191 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
192
193 # Another way to stay in a cell that has a bad value...
194 row = self.GetGridCursorRow()
195 col = self.GetGridCursorCol()
196
197 if self.IsCellEditControlEnabled():
198 self.HideCellEditControl()
199 self.DisableCellEditControl()
200
201 value = self.GetCellValue(row, col)
202
203 if value == 'no good 2':
204 return # cancels the cell selection
205
206 evt.Skip()
207
208
209 def OnEditorShown(self, evt):
210 if evt.GetRow() == 6 and evt.GetCol() == 3 and \
211 wx.MessageBox("Are you sure you wish to edit this cell?",
212 "Checking", wx.YES_NO) == wx.NO:
213 evt.Veto()
214 return
215
216 self.log.write("OnEditorShown: (%d,%d) %s\n" %
217 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
218 evt.Skip()
219
220
221 def OnEditorHidden(self, evt):
222 if evt.GetRow() == 6 and evt.GetCol() == 3 and \
223 wx.MessageBox("Are you sure you wish to finish editing this cell?",
224 "Checking", wx.YES_NO) == wx.NO:
225 evt.Veto()
226 return
227
228 self.log.write("OnEditorHidden: (%d,%d) %s\n" %
229 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
230 evt.Skip()
231
232
233 def OnEditorCreated(self, evt):
234 self.log.write("OnEditorCreated: (%d, %d) %s\n" %
235 (evt.GetRow(), evt.GetCol(), evt.GetControl()))
236
237
238
239 #---------------------------------------------------------------------------
240
241 class TestFrame(wx.Frame):
242 def __init__(self, parent, log):
243 wx.Frame.__init__(self, parent, -1, "Simple Grid Demo", size=(640,480))
244 grid = SimpleGrid(self, log)
245
246
247
248 #---------------------------------------------------------------------------
249
250 if __name__ == '__main__':
251 import sys
252 app = wx.PySimpleApp()
253 frame = TestFrame(None, sys.stdout)
254 frame.Show(True)
255 app.MainLoop()
256
257
258 #---------------------------------------------------------------------------
259
260