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