]> git.saurik.com Git - wxWidgets.git/blob - wxPython/tests/gridtest.py
for testing...
[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_MULTILINE | wxTE_NO_VSCROLL | wxTE_AUTO_SCROLL
24
25 self._tc = wxTextCtrl(parent, id, "", style=theStyle)
26 self._tc.SetInsertionPoint(0)
27 self.SetControl(self._tc)
28 if evtHandler:
29 print evtHandler
30 self._tc.PushEventHandler(evtHandler)
31
32
33 def SetSize(self, rect):
34 """
35 Called to position/size the edit control within the cell rectangle.
36 If you don't fill the cell (the rect) then be sure to override
37 PaintBackground and do something meaningful there.
38 """
39 self._tc.SetDimensions(rect.x-1, rect.y-1, rect.width+4, rect.height+4)
40
41 def BeginEdit(self, row, col, grid):
42 """
43 Fetch the value from the table and prepare the edit control
44 to begin editing. Set the focus to the edit control.
45 *Must Override*
46 """
47 print "BeginEdit"
48 self.startValue = grid.GetTable().GetValue(row, col)
49 self._tc.SetValue(self.startValue)
50 self._tc.SetFocus()
51
52 def EndEdit(self, row, col, grid):
53 """
54 Complete the editing of the current cell. Returns true if the value
55 has changed. If necessary, the control may be destroyed.
56 *Must Override*
57 """
58 changed = false
59 val = self._tc.GetValue()
60 if val != self.startValue:
61 changed = true
62 grid.GetTable().SetValue(row, col, val) # update the table
63
64 self.startValue = ''
65 self._tc.SetValue('')
66 return changed
67
68
69 def Reset(self):
70 """
71 Reset the value in the control back to its starting value.
72 *Must Override*
73 """
74 self._tc.SetValue(self.startValue)
75 self._tc.SetInsertionPointEnd()
76
77
78 ## def IsAcceptedKey(self, evt):
79 ## """
80 ## Return TRUE to allow the given key to start editing: the base class
81 ## version only checks that the event has no modifiers.
82 ## """
83 ## key = evt.GetKeyCode()
84 ## print "KeyCode:", key
85 ## # For linux the first range means number keys in main keyboard, and
86 ## # the second range means numeric keypad keys, with num-lock on.
87 ## if key in range(48,58) or key in range(326,336):
88 ## return true
89 ## else:
90 ## return false
91
92 ## def StartingKey(self, evt):
93 ## """
94 ## If the editor is enabled by pressing keys on the grid, this will be
95 ## called to let the editor do something about that first key if desired.
96 ## """
97 ## key = evt.GetKeyCode()
98 ## print "StartingKey", key
99 ## ch = None
100 ## if key in [WXK_NUMPAD0, WXK_NUMPAD1, WXK_NUMPAD2, WXK_NUMPAD3, WXK_NUMPAD4,
101 ## WXK_NUMPAD5, WXK_NUMPAD6, WXK_NUMPAD7, WXK_NUMPAD8, WXK_NUMPAD9]:
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()