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