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