]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/GridSimple.py
Fixed compilation error
[wxWidgets.git] / wxPython / demo / GridSimple.py
CommitLineData
f6bcfd97
BP
1from wxPython.wx import *
2from wxPython.grid import *
d56cebe7 3from wxPython.lib.mixins.grid import wxGridAutoEditMixin
f6bcfd97
BP
4
5#---------------------------------------------------------------------------
6
b166c703 7class SimpleGrid(wxGrid): ##, wxGridAutoEditMixin):
f6bcfd97
BP
8 def __init__(self, parent, log):
9 wxGrid.__init__(self, parent, -1)
b166c703 10 ##wxGridAutoEditMixin.__init__(self)
f6bcfd97 11 self.log = log
c368d904
RD
12 self.moveTo = None
13
14 EVT_IDLE(self, self.OnIdle)
f6bcfd97
BP
15
16 self.CreateGrid(25, 25)
17
18 # simple cell formatting
19 self.SetColSize(3, 200)
20 self.SetRowSize(4, 45)
21 self.SetCellValue(0, 0, "First cell")
22 self.SetCellValue(1, 1, "Another cell")
23 self.SetCellValue(2, 2, "Yet another cell")
9416aa89 24 self.SetCellValue(3, 3, "This cell is read-only")
f6bcfd97
BP
25 self.SetCellFont(0, 0, wxFont(12, wxROMAN, wxITALIC, wxNORMAL))
26 self.SetCellTextColour(1, 1, wxRED)
27 self.SetCellBackgroundColour(2, 2, wxCYAN)
9416aa89
RD
28 self.SetReadOnly(3, 3, true)
29
d56cebe7
RD
30 self.SetCellEditor(5, 0, wxGridCellNumberEditor())
31 self.SetCellValue(5, 0, "123")
32 self.SetCellEditor(6, 0, wxGridCellFloatEditor())
33 self.SetCellValue(6, 0, "123.34")
f6bcfd97
BP
34
35 # attribute objects let you keep a set of formatting values
36 # in one spot, and reuse them if needed
37 attr = wxGridCellAttr()
38 attr.SetTextColour(wxBLACK)
39 attr.SetBackgroundColour(wxRED)
40 attr.SetFont(wxFont(10, wxSWISS, wxNORMAL, wxBOLD))
41
42 # you can set cell attributes for the whole row (or column)
43 self.SetRowAttr(5, attr)
44
3ca6a5f0
BP
45 self.SetColLabelValue(0, "Custom")
46 self.SetColLabelValue(1, "column")
47 self.SetColLabelValue(2, "labels")
f6bcfd97 48
f3d9dc1d
RD
49 self.SetColLabelAlignment(wxALIGN_LEFT, wxALIGN_BOTTOM)
50
f6bcfd97
BP
51 # test all the events
52 EVT_GRID_CELL_LEFT_CLICK(self, self.OnCellLeftClick)
53 EVT_GRID_CELL_RIGHT_CLICK(self, self.OnCellRightClick)
54 EVT_GRID_CELL_LEFT_DCLICK(self, self.OnCellLeftDClick)
55 EVT_GRID_CELL_RIGHT_DCLICK(self, self.OnCellRightDClick)
56
57 EVT_GRID_LABEL_LEFT_CLICK(self, self.OnLabelLeftClick)
58 EVT_GRID_LABEL_RIGHT_CLICK(self, self.OnLabelRightClick)
59 EVT_GRID_LABEL_LEFT_DCLICK(self, self.OnLabelLeftDClick)
60 EVT_GRID_LABEL_RIGHT_DCLICK(self, self.OnLabelRightDClick)
61
62 EVT_GRID_ROW_SIZE(self, self.OnRowSize)
63 EVT_GRID_COL_SIZE(self, self.OnColSize)
64
65 EVT_GRID_RANGE_SELECT(self, self.OnRangeSelect)
66 EVT_GRID_CELL_CHANGE(self, self.OnCellChange)
67 EVT_GRID_SELECT_CELL(self, self.OnSelectCell)
68
69 EVT_GRID_EDITOR_SHOWN(self, self.OnEditorShown)
70 EVT_GRID_EDITOR_HIDDEN(self, self.OnEditorHidden)
bf7945ce 71 EVT_GRID_EDITOR_CREATED(self, self.OnEditorCreated)
f6bcfd97
BP
72
73
19a97bd6 74
f6bcfd97
BP
75 def OnCellLeftClick(self, evt):
76 self.log.write("OnCellLeftClick: (%d,%d) %s\n" %
77 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
78 evt.Skip()
79
80 def OnCellRightClick(self, evt):
81 self.log.write("OnCellRightClick: (%d,%d) %s\n" %
82 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
83 evt.Skip()
84
85 def OnCellLeftDClick(self, evt):
86 self.log.write("OnCellLeftDClick: (%d,%d) %s\n" %
87 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
88 evt.Skip()
89
90 def OnCellRightDClick(self, evt):
91 self.log.write("OnCellRightDClick: (%d,%d) %s\n" %
92 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
93 evt.Skip()
94
95 def OnLabelLeftClick(self, evt):
96 self.log.write("OnLabelLeftClick: (%d,%d) %s\n" %
97 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
98 evt.Skip()
99
100 def OnLabelRightClick(self, evt):
101 self.log.write("OnLabelRightClick: (%d,%d) %s\n" %
102 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
103 evt.Skip()
104
105 def OnLabelLeftDClick(self, evt):
106 self.log.write("OnLabelLeftDClick: (%d,%d) %s\n" %
107 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
108 evt.Skip()
109
110 def OnLabelRightDClick(self, evt):
111 self.log.write("OnLabelRightDClick: (%d,%d) %s\n" %
112 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
113 evt.Skip()
114
115
116 def OnRowSize(self, evt):
117 self.log.write("OnRowSize: row %d, %s\n" %
118 (evt.GetRowOrCol(), evt.GetPosition()))
119 evt.Skip()
120
121 def OnColSize(self, evt):
122 self.log.write("OnColSize: col %d, %s\n" %
123 (evt.GetRowOrCol(), evt.GetPosition()))
124 evt.Skip()
125
126 def OnRangeSelect(self, evt):
127 if evt.Selecting():
128 self.log.write("OnRangeSelect: top-left %s, bottom-right %s\n" %
129 (evt.GetTopLeftCoords(), evt.GetBottomRightCoords()))
130 evt.Skip()
131
c368d904 132
f6bcfd97
BP
133 def OnCellChange(self, evt):
134 self.log.write("OnCellChange: (%d,%d) %s\n" %
135 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
c368d904
RD
136
137 # Show how to stay in a cell that has bad data. We can't just
138 # call SetGridCursor here since we are nested inside one so it
139 # won't have any effect. Instead, set coordinants to move to in
140 # idle time.
141 value = self.GetCellValue(evt.GetRow(), evt.GetCol())
142 if value == 'no good':
143 self.moveTo = evt.GetRow(), evt.GetCol()
144
d56cebe7 145
c368d904
RD
146 def OnIdle(self, evt):
147 if self.moveTo != None:
148 self.SetGridCursor(self.moveTo[0], self.moveTo[1])
149 self.moveTo = None
d56cebe7 150 evt.Skip()
c368d904 151
f6bcfd97
BP
152
153 def OnSelectCell(self, evt):
154 self.log.write("OnSelectCell: (%d,%d) %s\n" %
155 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
c368d904
RD
156
157 # Another way to stay in a cell that has a bad value...
158 row = self.GetGridCursorRow()
159 col = self.GetGridCursorCol()
160 if self.IsCellEditControlEnabled():
161 self.HideCellEditControl()
162 self.DisableCellEditControl()
163 value = self.GetCellValue(row, col)
164 if value == 'no good 2':
165 return # cancels the cell selection
d56cebe7 166 evt.Skip()
c368d904 167
f6bcfd97
BP
168
169 def OnEditorShown(self, evt):
170 self.log.write("OnEditorShown: (%d,%d) %s\n" %
171 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
172 evt.Skip()
173
174 def OnEditorHidden(self, evt):
175 self.log.write("OnEditorHidden: (%d,%d) %s\n" %
176 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
177 evt.Skip()
178
bf7945ce
RD
179 def OnEditorCreated(self, evt):
180 self.log.write("OnEditorCreated: (%d, %d) %s\n" %
181 (evt.GetRow(), evt.GetCol(), evt.GetControl()))
182
183
f6bcfd97
BP
184
185#---------------------------------------------------------------------------
186
187class TestFrame(wxFrame):
188 def __init__(self, parent, log):
189 wxFrame.__init__(self, parent, -1, "Simple Grid Demo", size=(640,480))
19a97bd6 190 grid = SimpleGrid(self, log)
53fe40ba 191
f6bcfd97
BP
192
193
194#---------------------------------------------------------------------------
195
196if __name__ == '__main__':
197 import sys
198 app = wxPySimpleApp()
199 frame = TestFrame(None, sys.stdout)
200 frame.Show(true)
201 app.MainLoop()
202
203
204#---------------------------------------------------------------------------
c368d904
RD
205
206