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