]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/TextCtrl.py
Fix "warning: operation on 'y' may be undefined".
[wxWidgets.git] / wxPython / demo / TextCtrl.py
CommitLineData
8fa876ca
RD
1
2import sys
3import wx
cf694132
RD
4
5#---------------------------------------------------------------------------
6
8fa876ca 7class TestPanel(wx.Panel):
2f9be787
RD
8 def OnSetFocus(self, evt):
9 print "OnSetFocus"
10 evt.Skip()
11 def OnKillFocus(self, evt):
12 print "OnKillFocus"
13 evt.Skip()
0122b7e3
RD
14 def OnWindowDestroy(self, evt):
15 print "OnWindowDestroy"
16 evt.Skip()
17
2f9be787 18
cf694132 19 def __init__(self, parent, log):
8fa876ca 20 wx.Panel.__init__(self, parent, -1)
cf694132
RD
21 self.log = log
22
8fa876ca
RD
23 l1 = wx.StaticText(self, -1, "wx.TextCtrl")
24 t1 = wx.TextCtrl(self, -1, "Test it out and see", size=(125, -1))
a472b1f9 25 wx.CallAfter(t1.SetInsertionPoint, 0)
1e4a197e 26 self.tc1 = t1
8fa876ca
RD
27
28 self.Bind(wx.EVT_TEXT, self.EvtText, t1)
29 t1.Bind(wx.EVT_CHAR, self.EvtChar)
30 t1.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus)
31 t1.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus)
32 t1.Bind(wx.EVT_WINDOW_DESTROY, self.OnWindowDestroy)
33
34 l2 = wx.StaticText(self, -1, "Password")
35 t2 = wx.TextCtrl(self, -1, "", size=(125, -1), style=wx.TE_PASSWORD)
36 self.Bind(wx.EVT_TEXT, self.EvtText, t2)
37
38 l3 = wx.StaticText(self, -1, "Multi-line")
39 t3 = wx.TextCtrl(self, -1,
4deafe31
RD
40 "Here is a looooooooooooooong line of text set in the control.\n\n"
41 "The quick brown fox jumped over the lazy dog...",
8fa876ca
RD
42 size=(200, 100), style=wx.TE_MULTILINE)
43
d56cebe7 44 t3.SetInsertionPoint(0)
8fa876ca
RD
45 self.Bind(wx.EVT_TEXT, self.EvtText, t3)
46 b = wx.Button(self, -1, "Test Replace")
47 self.Bind(wx.EVT_BUTTON, self.OnTestReplace, b)
48 b2 = wx.Button(self, -1, "Test GetSelection")
49 self.Bind(wx.EVT_BUTTON, self.OnTestGetSelection, b2)
50 b3 = wx.Button(self, -1, "Test WriteText")
51 self.Bind(wx.EVT_BUTTON, self.OnTestWriteText, b3)
c7e7022c 52 self.tc = t3
1e4a197e 53
cf694132 54
8fa876ca
RD
55 l4 = wx.StaticText(self, -1, "Rich Text")
56 t4 = wx.TextCtrl(self, -1, "If supported by the native control, this is red, and this is a different font.",
57 size=(200, 100), style=wx.TE_MULTILINE|wx.TE_RICH2)
d56cebe7 58 t4.SetInsertionPoint(0)
8fa876ca 59 t4.SetStyle(44, 47, wx.TextAttr("RED", "YELLOW"))
d56cebe7 60 points = t4.GetFont().GetPointSize() # get the current size
8fa876ca
RD
61 f = wx.Font(points+3, wx.ROMAN, wx.ITALIC, wx.BOLD, True)
62 t4.SetStyle(63, 77, wx.TextAttr("BLUE", wx.NullColour, f))
63
64 l5 = wx.StaticText(self, -1, "Test Positions")
65 t5 = wx.TextCtrl(self, -1, "0123456789\n" * 5, size=(200, 100),
66 style = wx.TE_MULTILINE
67 #| wx.TE_RICH
68 | wx.TE_RICH2
1e4a197e 69 )
8fa876ca 70 t5.Bind(wx.EVT_LEFT_DOWN, self.OnT5LeftDown)
1e4a197e
RD
71 self.t5 = t5
72
90f7e9c7 73 space = 4
8fa876ca 74 bsizer = wx.BoxSizer(wx.VERTICAL)
90f7e9c7
RD
75 bsizer.Add(b, 0, wx.GROW|wx.ALL, space)
76 bsizer.Add(b2, 0, wx.GROW|wx.ALL, space)
77 bsizer.Add(b3, 0, wx.GROW|wx.ALL, space)
1e4a197e 78
90f7e9c7 79 sizer = wx.FlexGridSizer(cols=3, hgap=space, vgap=space)
c7e7022c
RD
80 sizer.AddMany([ l1, t1, (0,0),
81 l2, t2, (0,0),
4deafe31 82 l3, t3, bsizer,
c7e7022c 83 l4, t4, (0,0),
1e4a197e 84 l5, t5, (0,0),
d56cebe7 85 ])
8fa876ca
RD
86 border = wx.BoxSizer(wx.VERTICAL)
87 border.Add(sizer, 0, wx.ALL, 25)
d56cebe7 88 self.SetSizer(border)
1e4a197e 89 self.SetAutoLayout(True)
d56cebe7
RD
90
91
cf694132
RD
92 def EvtText(self, event):
93 self.log.WriteText('EvtText: %s\n' % event.GetString())
94
95
c368d904
RD
96 def EvtChar(self, event):
97 self.log.WriteText('EvtChar: %d\n' % event.GetKeyCode())
98 event.Skip()
99
cf694132 100
c7e7022c 101 def OnTestReplace(self, evt):
4deafe31
RD
102 self.tc.Replace(5, 9, "IS A")
103 #self.tc.Remove(5, 9)
104
1e4a197e
RD
105 def OnTestWriteText(self, evt):
106 self.tc.WriteText("TEXT")
107
4deafe31
RD
108 def OnTestGetSelection(self, evt):
109 start, end = self.tc.GetSelection()
110 text = self.tc.GetValue()
8fa876ca 111 if wx.Platform == "__WXMSW__": # This is why GetStringSelection was added
1e4a197e 112 text = text.replace('\n', '\r\n')
8fa876ca 113
3628e088 114 self.log.write("multi-line GetSelection(): (%d, %d)\n"
f0db4f38
RD
115 "\tGetStringSelection(): %s\n"
116 "\tSelectedText: %s\n" %
117 (start, end,
118 self.tc.GetStringSelection(),
119 repr(text[start:end])))
c7e7022c 120
3628e088
RD
121 start, end = self.tc1.GetSelection()
122 text = self.tc1.GetValue()
8fa876ca
RD
123
124 if wx.Platform == "__WXMSW__": # This is why GetStringSelection was added
3628e088 125 text = text.replace('\n', '\r\n')
8fa876ca 126
3628e088
RD
127 self.log.write("single-line GetSelection(): (%d, %d)\n"
128 "\tGetStringSelection(): %s\n"
129 "\tSelectedText: %s\n" %
130 (start, end,
131 self.tc1.GetStringSelection(),
132 repr(text[start:end])))
133
134
1e4a197e
RD
135 def OnT5LeftDown(self, evt):
136 evt.Skip()
8fa876ca 137 wx.CallAfter(self.LogT5Position, evt)
1e4a197e
RD
138
139 def LogT5Position(self, evt):
140 text = self.t5.GetValue()
141 ip = self.t5.GetInsertionPoint()
142 lp = self.t5.GetLastPosition()
143 self.log.write("LogT5Position:\n"
144 "\tGetInsertionPoint:\t%d\n"
145 "\ttext[insertionpoint]:\t%s\n"
146 "\tGetLastPosition:\t%d\n"
147 "\tlen(text):\t\t%d\n"
148 % (ip, text[ip], lp, len(text)))
149
150
cf694132
RD
151#---------------------------------------------------------------------------
152
153def runTest(frame, nb, log):
154 win = TestPanel(nb, log)
155 return win
156
157#---------------------------------------------------------------------------
158
159
cf694132 160overview = """\
95bfd958 161A TextCtrl allows text to be displayed and (possibly) edited. It may be single
8fa876ca
RD
162line or multi-line, support styles or not, be read-only or not, and even supports
163text masking for such things as passwords.
63b6646e
RD
164
165
8fa876ca 166"""
63b6646e
RD
167
168
169if __name__ == '__main__':
170 import sys,os
171 import run
8eca4fef 172 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
63b6646e 173