]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/TextCtrl.py
wxGridBahSizer support
[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 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)
43
44 t3.SetInsertionPoint(0)
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)
52 self.tc = t3
53
54
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)
58 t4.SetInsertionPoint(0)
59 t4.SetStyle(44, 47, wx.TextAttr("RED", "YELLOW"))
60 points = t4.GetFont().GetPointSize() # get the current size
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
69 )
70 t5.Bind(wx.EVT_LEFT_DOWN, self.OnT5LeftDown)
71 self.t5 = t5
72
73 bsizer = wx.BoxSizer(wx.VERTICAL)
74 bsizer.Add(b, 0, wx.GROW|wx.ALL, 4)
75 bsizer.Add(b2, 0, wx.GROW|wx.ALL, 4)
76 bsizer.Add(b3, 0, wx.GROW|wx.ALL, 4)
77
78 sizer = wx.FlexGridSizer(cols=3, hgap=6, vgap=6)
79 sizer.AddMany([ l1, t1, (0,0),
80 l2, t2, (0,0),
81 l3, t3, bsizer,
82 l4, t4, (0,0),
83 l5, t5, (0,0),
84 ])
85 border = wx.BoxSizer(wx.VERTICAL)
86 border.Add(sizer, 0, wx.ALL, 25)
87 self.SetSizer(border)
88 self.SetAutoLayout(True)
89
90
91 def EvtText(self, event):
92 self.log.WriteText('EvtText: %s\n' % event.GetString())
93
94
95 def EvtChar(self, event):
96 self.log.WriteText('EvtChar: %d\n' % event.GetKeyCode())
97 event.Skip()
98
99
100 def OnTestReplace(self, evt):
101 self.tc.Replace(5, 9, "IS A")
102 #self.tc.Remove(5, 9)
103
104 def OnTestWriteText(self, evt):
105 self.tc.WriteText("TEXT")
106
107 def OnTestGetSelection(self, evt):
108 start, end = self.tc.GetSelection()
109 text = self.tc.GetValue()
110 if wx.Platform == "__WXMSW__": # This is why GetStringSelection was added
111 text = text.replace('\n', '\r\n')
112
113 self.log.write("multi-line GetSelection(): (%d, %d)\n"
114 "\tGetStringSelection(): %s\n"
115 "\tSelectedText: %s\n" %
116 (start, end,
117 self.tc.GetStringSelection(),
118 repr(text[start:end])))
119
120 start, end = self.tc1.GetSelection()
121 text = self.tc1.GetValue()
122
123 if wx.Platform == "__WXMSW__": # This is why GetStringSelection was added
124 text = text.replace('\n', '\r\n')
125
126 self.log.write("single-line GetSelection(): (%d, %d)\n"
127 "\tGetStringSelection(): %s\n"
128 "\tSelectedText: %s\n" %
129 (start, end,
130 self.tc1.GetStringSelection(),
131 repr(text[start:end])))
132
133
134 def OnT5LeftDown(self, evt):
135 evt.Skip()
136 wx.CallAfter(self.LogT5Position, evt)
137
138 def LogT5Position(self, evt):
139 text = self.t5.GetValue()
140 ip = self.t5.GetInsertionPoint()
141 lp = self.t5.GetLastPosition()
142 self.log.write("LogT5Position:\n"
143 "\tGetInsertionPoint:\t%d\n"
144 "\ttext[insertionpoint]:\t%s\n"
145 "\tGetLastPosition:\t%d\n"
146 "\tlen(text):\t\t%d\n"
147 % (ip, text[ip], lp, len(text)))
148
149
150 #---------------------------------------------------------------------------
151
152 def runTest(frame, nb, log):
153 win = TestPanel(nb, log)
154 return win
155
156 #---------------------------------------------------------------------------
157
158
159 overview = """\
160 A TextCtrl allows text to be displayed and (possibly) edited. It may be single
161 line or multi-line, support styles or not, be read-only or not, and even supports
162 text masking for such things as passwords.
163
164
165 """
166
167
168 if __name__ == '__main__':
169 import sys,os
170 import run
171 run.main(['', os.path.basename(sys.argv[0])])
172