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