]>
Commit | Line | Data |
---|---|---|
1 | # wxGrid workout | |
2 | ||
3 | import sys | |
4 | ||
5 | #---------------------------------------------------------------------------- | |
6 | from wxPython.wx import * | |
7 | from wxPython.grid import * | |
8 | ||
9 | class MyCellEditor(wxPyGridCellEditor): | |
10 | """ | |
11 | A custom GridCellEditor that only accepts numbers. | |
12 | Also tries to work around wxGTK anomaly that key cannot start edit. | |
13 | """ | |
14 | ||
15 | def Create(self, parent, id, evtHandler): | |
16 | """ | |
17 | Called to create the control, which must derive from wxControl. | |
18 | *Must Override* | |
19 | """ | |
20 | print "Create" | |
21 | theStyle = 0 | |
22 | if wxPlatform == '__WXMSW__': | |
23 | theStyle = wxTE_PROCESS_TAB | wxTE_MULTILINE | wxTE_NO_VSCROLL | wxTE_AUTO_SCROLL | |
24 | #theStyle = wxTE_PROCESS_TAB | wxTE_PROCESS_ENTER | |
25 | ||
26 | self._tc = wxTextCtrl(parent, id, "", style=theStyle) | |
27 | self._tc.SetInsertionPoint(0) | |
28 | self.SetControl(self._tc) | |
29 | if evtHandler: | |
30 | print evtHandler | |
31 | self._tc.PushEventHandler(evtHandler) | |
32 | ||
33 | ||
34 | def SetSize(self, rect): | |
35 | """ | |
36 | Called to position/size the edit control within the cell rectangle. | |
37 | If you don't fill the cell (the rect) then be sure to override | |
38 | PaintBackground and do something meaningful there. | |
39 | """ | |
40 | self._tc.SetDimensions(rect.x-1, rect.y-1, rect.width+4, rect.height+4) | |
41 | ||
42 | def BeginEdit(self, row, col, grid): | |
43 | """ | |
44 | Fetch the value from the table and prepare the edit control | |
45 | to begin editing. Set the focus to the edit control. | |
46 | *Must Override* | |
47 | """ | |
48 | print "BeginEdit" | |
49 | self.startValue = grid.GetTable().GetValue(row, col) | |
50 | self._tc.SetValue(self.startValue) | |
51 | self._tc.SetFocus() | |
52 | ||
53 | def EndEdit(self, row, col, grid): | |
54 | """ | |
55 | Complete the editing of the current cell. Returns true if the value | |
56 | has changed. If necessary, the control may be destroyed. | |
57 | *Must Override* | |
58 | """ | |
59 | changed = false | |
60 | val = self._tc.GetValue() | |
61 | if val != self.startValue: | |
62 | changed = true | |
63 | grid.GetTable().SetValue(row, col, val) # update the table | |
64 | ||
65 | self.startValue = '' | |
66 | self._tc.SetValue('') | |
67 | return changed | |
68 | ||
69 | ||
70 | def Reset(self): | |
71 | """ | |
72 | Reset the value in the control back to its starting value. | |
73 | *Must Override* | |
74 | """ | |
75 | self._tc.SetValue(self.startValue) | |
76 | self._tc.SetInsertionPointEnd() | |
77 | ||
78 | ||
79 | def IsAcceptedKey(self, evt): | |
80 | """ | |
81 | Return TRUE to allow the given key to start editing: the base class | |
82 | version only checks that the event has no modifiers. | |
83 | """ | |
84 | key = evt.GetKeyCode() | |
85 | print "KeyCode:", key | |
86 | if (key in range(ord('0'),ord('9')+1) or | |
87 | key in range(WXK_NUMPAD0, WXK_NUMPAD9+1)): | |
88 | return true | |
89 | else: | |
90 | return false | |
91 | ||
92 | ||
93 | def StartingKey(self, evt): | |
94 | """ | |
95 | If the editor is enabled by pressing keys on the grid, this will be | |
96 | called to let the editor do something about that first key if desired. | |
97 | """ | |
98 | key = evt.GetKeyCode() | |
99 | print "StartingKey", key | |
100 | ch = None | |
101 | if key in range(WXK_NUMPAD0, WXK_NUMPAD9+1): | |
102 | ch = ch = chr(ord('0') + key - WXK_NUMPAD0) | |
103 | ||
104 | elif key < 256 and key >= 0 and chr(key) in string.printable: | |
105 | ch = chr(key) | |
106 | if not evt.ShiftDown(): | |
107 | ch = string.lower(ch) | |
108 | ||
109 | if ch is not None: | |
110 | # Replace the text. Other option would be to append it. | |
111 | # self._tc.AppendText(ch) | |
112 | self._tc.SetValue(ch) | |
113 | self._tc.SetInsertionPointEnd() | |
114 | else: | |
115 | evt.Skip() | |
116 | ||
117 | ||
118 | def Destroy(self): | |
119 | """final cleanup""" | |
120 | self.base_Destroy() | |
121 | ||
122 | ||
123 | def Clone(self): | |
124 | """ | |
125 | Create a new object which is the copy of this one | |
126 | *Must Override* | |
127 | """ | |
128 | print "clone" | |
129 | return MyCellEditor() | |
130 | ||
131 | ||
132 | class MyGrid(wxGrid): | |
133 | def __init__(self, parent, cust): | |
134 | wxGrid.__init__(self, parent, -1) | |
135 | self.CreateGrid(10, 10) | |
136 | for column in xrange(10): | |
137 | self.SetColSize(column, 40) | |
138 | ||
139 | self.SetDefaultCellAlignment(wxCENTRE, wxCENTRE) | |
140 | ||
141 | import os | |
142 | if cust: | |
143 | for column in xrange(10): | |
144 | # Note, we create attr and cell editor for each column | |
145 | # otherwise segfault at exit, probably tries to free those | |
146 | # multiple times from each column. | |
147 | attr = wxGridCellAttr() | |
148 | attr.SetEditor(MyCellEditor()) | |
149 | self.SetColAttr(column, attr) | |
150 | ||
151 | ||
152 | #---------------------------------------------------------------------------- | |
153 | ||
154 | class CardNoteBook(wxNotebook): | |
155 | def __init__(self, parent, id): | |
156 | wxNotebook.__init__(self, parent, id) | |
157 | ||
158 | for title, cust in [("Default", 0), ("Custom Cell Editor", 1)]: | |
159 | win = MyGrid(self, cust) | |
160 | self.AddPage(win, title) | |
161 | ||
162 | EVT_NOTEBOOK_PAGE_CHANGED(self, self.GetId(), self.OnPageChanged) | |
163 | EVT_KEY_DOWN(self, self.OnKeyDown) | |
164 | EVT_NAVIGATION_KEY(self, self.OnNavKey) | |
165 | ||
166 | ||
167 | def OnKeyDown(self, evt): | |
168 | print 'CardNoteBook.OnKeyDown: ', evt.GetKeyCode() | |
169 | evt.Skip() | |
170 | ||
171 | ||
172 | def OnNavKey(self, evt): | |
173 | print 'CardNoteBook.OnNavKey:', evt | |
174 | evt.Skip() | |
175 | ||
176 | def OnPageChanged(self, event): | |
177 | event.Skip() | |
178 | ||
179 | ||
180 | #---------------------------------------------------------------------------- | |
181 | ||
182 | class ScoreKeeper(wxFrame): | |
183 | def __init__(self, parent, id, title): | |
184 | wxFrame.__init__(self, parent, -1, title, size = (500, 400), | |
185 | style=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE) | |
186 | EVT_CLOSE(self, self.OnCloseWindow) | |
187 | self.Centre(wxBOTH) | |
188 | ||
189 | self.nb = CardNoteBook(self, -1) | |
190 | #win=MyGrid(self, "GTK") | |
191 | self.Show(true) | |
192 | ||
193 | def OnCloseWindow(self, event): | |
194 | self.dying = true | |
195 | self.window = None | |
196 | self.mainmenu = None | |
197 | self.Destroy() | |
198 | ||
199 | def OnFileExit(self, event): | |
200 | self.Close() | |
201 | ||
202 | #---------------------------------------------------------------------------- | |
203 | ||
204 | class MyApp(wxApp): | |
205 | def OnInit(self): | |
206 | frame = ScoreKeeper(None, -1, "ScoreKeeper: (A Demonstration)") | |
207 | frame.Show(true) | |
208 | self.SetTopWindow(frame) | |
209 | return true | |
210 | ||
211 | ||
212 | ||
213 | #--------------------------------------------------------------------------- | |
214 | ||
215 | def main(): | |
216 | app = MyApp(0) | |
217 | app.MainLoop() | |
218 | ||
219 | main() |