]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/GridSimple.py
some little demo tweaks
[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
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")
24 self.SetCellValue(3, 3, "This cell is read-only")
25 self.SetCellFont(0, 0, wxFont(12, wxROMAN, wxITALIC, wxNORMAL))
26 self.SetCellTextColour(1, 1, wxRED)
27 self.SetCellBackgroundColour(2, 2, wxCYAN)
28 self.SetReadOnly(3, 3, true)
29
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")
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
45 self.SetColLabelValue(0, "Custom")
46 self.SetColLabelValue(1, "column")
47 self.SetColLabelValue(2, "labels")
48
49 self.SetColLabelAlignment(wxALIGN_LEFT, wxALIGN_BOTTOM)
50
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)
71 EVT_GRID_EDITOR_CREATED(self, self.OnEditorCreated)
72
73
74 def OnCellLeftClick(self, evt):
75 self.log.write("OnCellLeftClick: (%d,%d) %s\n" %
76 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
77 evt.Skip()
78
79 def OnCellRightClick(self, evt):
80 self.log.write("OnCellRightClick: (%d,%d) %s\n" %
81 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
82 evt.Skip()
83
84 def OnCellLeftDClick(self, evt):
85 self.log.write("OnCellLeftDClick: (%d,%d) %s\n" %
86 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
87 evt.Skip()
88
89 def OnCellRightDClick(self, evt):
90 self.log.write("OnCellRightDClick: (%d,%d) %s\n" %
91 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
92 evt.Skip()
93
94 def OnLabelLeftClick(self, evt):
95 self.log.write("OnLabelLeftClick: (%d,%d) %s\n" %
96 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
97 evt.Skip()
98
99 def OnLabelRightClick(self, evt):
100 self.log.write("OnLabelRightClick: (%d,%d) %s\n" %
101 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
102 evt.Skip()
103
104 def OnLabelLeftDClick(self, evt):
105 self.log.write("OnLabelLeftDClick: (%d,%d) %s\n" %
106 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
107 evt.Skip()
108
109 def OnLabelRightDClick(self, evt):
110 self.log.write("OnLabelRightDClick: (%d,%d) %s\n" %
111 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
112 evt.Skip()
113
114
115 def OnRowSize(self, evt):
116 self.log.write("OnRowSize: row %d, %s\n" %
117 (evt.GetRowOrCol(), evt.GetPosition()))
118 evt.Skip()
119
120 def OnColSize(self, evt):
121 self.log.write("OnColSize: col %d, %s\n" %
122 (evt.GetRowOrCol(), evt.GetPosition()))
123 evt.Skip()
124
125 def OnRangeSelect(self, evt):
126 if evt.Selecting():
127 self.log.write("OnRangeSelect: top-left %s, bottom-right %s\n" %
128 (evt.GetTopLeftCoords(), evt.GetBottomRightCoords()))
129 evt.Skip()
130
131
132 def OnCellChange(self, evt):
133 self.log.write("OnCellChange: (%d,%d) %s\n" %
134 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
135
136 # Show how to stay in a cell that has bad data. We can't just
137 # call SetGridCursor here since we are nested inside one so it
138 # won't have any effect. Instead, set coordinants to move to in
139 # idle time.
140 value = self.GetCellValue(evt.GetRow(), evt.GetCol())
141 if value == 'no good':
142 self.moveTo = evt.GetRow(), evt.GetCol()
143
144
145 def OnIdle(self, evt):
146 if self.moveTo != None:
147 self.SetGridCursor(self.moveTo[0], self.moveTo[1])
148 self.moveTo = None
149 evt.Skip()
150
151
152 def OnSelectCell(self, evt):
153 self.log.write("OnSelectCell: (%d,%d) %s\n" %
154 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
155
156 # Another way to stay in a cell that has a bad value...
157 row = self.GetGridCursorRow()
158 col = self.GetGridCursorCol()
159 if self.IsCellEditControlEnabled():
160 self.HideCellEditControl()
161 self.DisableCellEditControl()
162 value = self.GetCellValue(row, col)
163 if value == 'no good 2':
164 return # cancels the cell selection
165 evt.Skip()
166
167
168 def OnEditorShown(self, evt):
169 self.log.write("OnEditorShown: (%d,%d) %s\n" %
170 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
171 evt.Skip()
172
173 def OnEditorHidden(self, evt):
174 self.log.write("OnEditorHidden: (%d,%d) %s\n" %
175 (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
176 evt.Skip()
177
178 def OnEditorCreated(self, evt):
179 self.log.write("OnEditorCreated: (%d, %d) %s\n" %
180 (evt.GetRow(), evt.GetCol(), evt.GetControl()))
181
182
183
184 #---------------------------------------------------------------------------
185
186 class TestFrame(wxFrame):
187 def __init__(self, parent, log):
188 wxFrame.__init__(self, parent, -1, "Simple Grid Demo", size=(640,480))
189 self.grid = grid = SimpleGrid(self, log)
190
191 self.mainmenu = wxMenuBar()
192
193 menu = wxMenu()
194
195 mID = NewId()
196 menu.Append(mID, 'Preview Grid', 'Print Displayed Grid')
197 EVT_MENU(self, mID, self.OnPreviewGrid)
198 self.mainmenu.Append(menu, '&Print')
199
200 self.SetMenuBar(self.mainmenu)
201
202 def OnPreviewGrid(self, event):
203 from wxPython.lib.printout import PrintGrid
204 grid = self.grid
205
206 total_col = 4 # not all columns to be used for printing
207 total_row = 4
208
209 format = [1, 1.5, 2.5, 4, 5, 7] # column spacing
210
211 # class to print and preview
212 prt = PrintGrid(self, grid, format, total_col, total_row )
213 prt.SetAttributes() # get the colour and text attributes
214
215 table = prt.GetTable() # the table print control class
216 table.SetHeader("Simple Grid Test Header")
217 table.SetFooter()
218
219 prt.Preview() # preview the table
220
221
222 #---------------------------------------------------------------------------
223
224 if __name__ == '__main__':
225 import sys
226 app = wxPySimpleApp()
227 frame = TestFrame(None, sys.stdout)
228 frame.Show(true)
229 app.MainLoop()
230
231
232 #---------------------------------------------------------------------------
233
234