]> git.saurik.com Git - wxWidgets.git/blob - wxPython/tests/gridtest.py
fixed a typo and uncommented some methods
[wxWidgets.git] / wxPython / tests / gridtest.py
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 # For linux the first range means number keys in main keyboard, and
87 # the second range means numeric keypad keys, with num-lock on.
88 if key in range(48,58) or key in range(326,336):
89 return true
90 else:
91 return false
92
93
94 def StartingKey(self, evt):
95 """
96 If the editor is enabled by pressing keys on the grid, this will be
97 called to let the editor do something about that first key if desired.
98 """
99 key = evt.GetKeyCode()
100 print "StartingKey", key
101 ch = None
102 if key in [WXK_NUMPAD0, WXK_NUMPAD1, WXK_NUMPAD2, WXK_NUMPAD3, WXK_NUMPAD4,
103 WXK_NUMPAD5, WXK_NUMPAD6, WXK_NUMPAD7, WXK_NUMPAD8, WXK_NUMPAD9]:
104 ch = ch = chr(ord('0') + key - WXK_NUMPAD0)
105
106 elif key < 256 and key >= 0 and chr(key) in string.printable:
107 ch = chr(key)
108 if not evt.ShiftDown():
109 ch = string.lower(ch)
110
111 if ch is not None:
112 # Replace the text. Other option would be to append it.
113 # self._tc.AppendText(ch)
114 self._tc.SetValue(ch)
115 self._tc.SetInsertionPointEnd()
116 else:
117 evt.Skip()
118
119
120 def Destroy(self):
121 """final cleanup"""
122 self.base_Destroy()
123
124
125 def Clone(self):
126 """
127 Create a new object which is the copy of this one
128 *Must Override*
129 """
130 print "clone"
131 return MyCellEditor()
132
133
134 class MyGrid(wxGrid):
135 def __init__(self, parent, cust):
136 wxGrid.__init__(self, parent, -1)
137 self.CreateGrid(10, 10)
138 for column in xrange(10):
139 self.SetColSize(column, 40)
140
141 self.SetDefaultCellAlignment(wxCENTRE, wxCENTRE)
142
143 import os
144 if cust:
145 for column in xrange(10):
146 # Note, we create attr and cell editor for each column
147 # otherwise segfault at exit, probably tries to free those
148 # multiple times from each column.
149 attr = wxGridCellAttr()
150 attr.SetEditor(MyCellEditor())
151 self.SetColAttr(column, attr)
152
153
154 #----------------------------------------------------------------------------
155
156 class CardNoteBook(wxNotebook):
157 def __init__(self, parent, id):
158 wxNotebook.__init__(self, parent, id)
159
160 for title, cust in [("Default", 0), ("Custom Cell Editor", 1)]:
161 win = MyGrid(self, cust)
162 self.AddPage(win, title)
163
164 EVT_NOTEBOOK_PAGE_CHANGED(self, self.GetId(), self.OnPageChanged)
165 EVT_KEY_DOWN(self, self.OnKeyDown)
166 EVT_NAVIGATION_KEY(self, self.OnNavKey)
167
168
169 def OnKeyDown(self, evt):
170 print 'CardNoteBook.OnKeyDown: ', evt.GetKeyCode()
171 evt.Skip()
172
173
174 def OnNavKey(self, evt):
175 print 'CardNoteBook.OnNavKey:', evt
176 evt.Skip()
177
178 def OnPageChanged(self, event):
179 event.Skip()
180
181
182 #----------------------------------------------------------------------------
183
184 class ScoreKeeper(wxFrame):
185 def __init__(self, parent, id, title):
186 wxFrame.__init__(self, parent, -1, title, size = (500, 400),
187 style=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE)
188 EVT_CLOSE(self, self.OnCloseWindow)
189 self.Centre(wxBOTH)
190
191 self.nb = CardNoteBook(self, -1)
192 #win=MyGrid(self, "GTK")
193 self.Show(true)
194
195 def OnCloseWindow(self, event):
196 self.dying = true
197 self.window = None
198 self.mainmenu = None
199 self.Destroy()
200
201 def OnFileExit(self, event):
202 self.Close()
203
204 #----------------------------------------------------------------------------
205
206 class MyApp(wxApp):
207 def OnInit(self):
208 frame = ScoreKeeper(None, -1, "ScoreKeeper: (A Demonstration)")
209 frame.Show(true)
210 self.SetTopWindow(frame)
211 return true
212
213
214
215 #---------------------------------------------------------------------------
216
217 def main():
218 app = MyApp(0)
219 app.MainLoop()
220
221 main()