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