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