1 # 11/6/2003 - Jeff Grimmett (grimmtooth@softhome.net)
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?
7 # 11/25/2003 - Jeff Grimmett (grimmtooth@softhome.net)
9 # o Robin confirms, this is tutorial code. But be warned! It has not been
10 # converted OR tested!
14 import wx
.grid
as gridlib
15 #import wx.lib.mixins.grid as mixins
17 #---------------------------------------------------------------------------
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)
26 self
.Bind(wx
.EVT_IDLE
, self
.OnIdle
)
28 self
.CreateGrid(25, 25) #, wxGrid.wxGridSelectRows)
29 ##self.EnableEditing(False)
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)
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())
49 self
.SetCellValue(6, 3, "You can veto editing this cell")
51 #self.SetRowLabelSize(0)
52 #self.SetColLabelSize(0)
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
))
61 # you can set cell attributes for the whole row (or column)
62 self
.SetRowAttr(5, attr
)
64 self
.SetColLabelValue(0, "Custom")
65 self
.SetColLabelValue(1, "column")
66 self
.SetColLabelValue(2, "labels")
68 self
.SetColLabelAlignment(wx
.ALIGN_LEFT
, wx
.ALIGN_BOTTOM
)
70 #self.SetDefaultCellOverflow(False)
71 #r = wxGridCellAutoWrapStringRenderer()
72 #self.SetCellRenderer(9, 1, r)
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");
81 editor
= gridlib
.GridCellTextEditor()
82 editor
.SetParameters('10')
83 self
.SetCellEditor(0, 4, editor
)
84 self
.SetCellValue(0, 4, "Limited text")
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
)
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
)
98 self
.Bind(gridlib
.EVT_GRID_ROW_SIZE
, self
.OnRowSize
)
99 self
.Bind(gridlib
.EVT_GRID_COL_SIZE
, self
.OnColSize
)
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
)
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
)
110 def OnCellLeftClick(self
, evt
):
111 self
.log
.write("OnCellLeftClick: (%d,%d) %s\n" %
112 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
115 def OnCellRightClick(self
, evt
):
116 self
.log
.write("OnCellRightClick: (%d,%d) %s\n" %
117 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
120 def OnCellLeftDClick(self
, evt
):
121 self
.log
.write("OnCellLeftDClick: (%d,%d) %s\n" %
122 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
125 def OnCellRightDClick(self
, evt
):
126 self
.log
.write("OnCellRightDClick: (%d,%d) %s\n" %
127 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
130 def OnLabelLeftClick(self
, evt
):
131 self
.log
.write("OnLabelLeftClick: (%d,%d) %s\n" %
132 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
135 def OnLabelRightClick(self
, evt
):
136 self
.log
.write("OnLabelRightClick: (%d,%d) %s\n" %
137 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
140 def OnLabelLeftDClick(self
, evt
):
141 self
.log
.write("OnLabelLeftDClick: (%d,%d) %s\n" %
142 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
145 def OnLabelRightDClick(self
, evt
):
146 self
.log
.write("OnLabelRightDClick: (%d,%d) %s\n" %
147 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
150 def OnRowSize(self
, evt
):
151 self
.log
.write("OnRowSize: row %d, %s\n" %
152 (evt
.GetRowOrCol(), evt
.GetPosition()))
155 def OnColSize(self
, evt
):
156 self
.log
.write("OnColSize: col %d, %s\n" %
157 (evt
.GetRowOrCol(), evt
.GetPosition()))
160 def OnRangeSelect(self
, evt
):
162 self
.log
.write("OnRangeSelect: top-left %s, bottom-right %s\n" %
163 (evt
.GetTopLeftCoords(), evt
.GetBottomRightCoords()))
167 def OnCellChange(self
, evt
):
168 self
.log
.write("OnCellChange: (%d,%d) %s\n" %
169 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
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
175 value
= self
.GetCellValue(evt
.GetRow(), evt
.GetCol())
177 if value
== 'no good':
178 self
.moveTo
= evt
.GetRow(), evt
.GetCol()
181 def OnIdle(self
, evt
):
182 if self
.moveTo
!= None:
183 self
.SetGridCursor(self
.moveTo
[0], self
.moveTo
[1])
189 def OnSelectCell(self
, evt
):
190 self
.log
.write("OnSelectCell: (%d,%d) %s\n" %
191 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
193 # Another way to stay in a cell that has a bad value...
194 row
= self
.GetGridCursorRow()
195 col
= self
.GetGridCursorCol()
197 if self
.IsCellEditControlEnabled():
198 self
.HideCellEditControl()
199 self
.DisableCellEditControl()
201 value
= self
.GetCellValue(row
, col
)
203 if value
== 'no good 2':
204 return # cancels the cell selection
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
:
216 self
.log
.write("OnEditorShown: (%d,%d) %s\n" %
217 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
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
:
228 self
.log
.write("OnEditorHidden: (%d,%d) %s\n" %
229 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
233 def OnEditorCreated(self
, evt
):
234 self
.log
.write("OnEditorCreated: (%d, %d) %s\n" %
235 (evt
.GetRow(), evt
.GetCol(), evt
.GetControl()))
239 #---------------------------------------------------------------------------
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
)
248 #---------------------------------------------------------------------------
250 if __name__
== '__main__':
252 app
= wx
.PySimpleApp()
253 frame
= TestFrame(None, sys
.stdout
)
258 #---------------------------------------------------------------------------