]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxTextCtrl.py
Version number update
[wxWidgets.git] / wxPython / demo / wxTextCtrl.py
1 # 11/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2 #
3 # o Updated for wx namespace
4 #
5
6 import sys
7 import wx
8
9 #---------------------------------------------------------------------------
10
11 class TestPanel(wx.Panel):
12 def OnSetFocus(self, evt):
13 print "OnSetFocus"
14 evt.Skip()
15 def OnKillFocus(self, evt):
16 print "OnKillFocus"
17 evt.Skip()
18 def OnWindowDestroy(self, evt):
19 print "OnWindowDestroy"
20 evt.Skip()
21
22
23 def __init__(self, parent, log):
24 wx.Panel.__init__(self, parent, -1)
25 self.log = log
26
27 l1 = wx.StaticText(self, -1, "wx.TextCtrl")
28 t1 = wx.TextCtrl(self, -1, "Test it out and see", size=(125, -1))
29 t1.SetInsertionPoint(0)
30 self.tc1 = t1
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,
44 "Here is a looooooooooooooong line of text set in the control.\n\n"
45 "The quick brown fox jumped over the lazy dog...",
46 size=(200, 100), style=wx.TE_MULTILINE)
47
48 t3.SetInsertionPoint(0)
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)
56 self.tc = t3
57
58
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)
62 t4.SetInsertionPoint(0)
63 t4.SetStyle(44, 47, wx.TextAttr("RED", "YELLOW"))
64 points = t4.GetFont().GetPointSize() # get the current size
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
73 )
74 t5.Bind(wx.EVT_LEFT_DOWN, self.OnT5LeftDown)
75 self.t5 = t5
76
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)
81
82 sizer = wx.FlexGridSizer(cols=3, hgap=6, vgap=6)
83 sizer.AddMany([ l1, t1, (0,0),
84 l2, t2, (0,0),
85 l3, t3, bsizer,
86 l4, t4, (0,0),
87 l5, t5, (0,0),
88 ])
89 border = wx.BoxSizer(wx.VERTICAL)
90 border.Add(sizer, 0, wx.ALL, 25)
91 self.SetSizer(border)
92 self.SetAutoLayout(True)
93
94
95 def EvtText(self, event):
96 self.log.WriteText('EvtText: %s\n' % event.GetString())
97
98
99 def EvtChar(self, event):
100 self.log.WriteText('EvtChar: %d\n' % event.GetKeyCode())
101 event.Skip()
102
103
104 def OnTestReplace(self, evt):
105 self.tc.Replace(5, 9, "IS A")
106 #self.tc.Remove(5, 9)
107
108 def OnTestWriteText(self, evt):
109 self.tc.WriteText("TEXT")
110
111 def OnTestGetSelection(self, evt):
112 start, end = self.tc.GetSelection()
113 text = self.tc.GetValue()
114 if wx.Platform == "__WXMSW__": # This is why GetStringSelection was added
115 text = text.replace('\n', '\r\n')
116
117 self.log.write("multi-line GetSelection(): (%d, %d)\n"
118 "\tGetStringSelection(): %s\n"
119 "\tSelectedText: %s\n" %
120 (start, end,
121 self.tc.GetStringSelection(),
122 repr(text[start:end])))
123
124 start, end = self.tc1.GetSelection()
125 text = self.tc1.GetValue()
126
127 if wx.Platform == "__WXMSW__": # This is why GetStringSelection was added
128 text = text.replace('\n', '\r\n')
129
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
138 def OnT5LeftDown(self, evt):
139 evt.Skip()
140 wx.CallAfter(self.LogT5Position, evt)
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
154 #---------------------------------------------------------------------------
155
156 def runTest(frame, nb, log):
157 win = TestPanel(nb, log)
158 return win
159
160 #---------------------------------------------------------------------------
161
162
163 overview = """\
164 A text control allows text to be displayed and (possibly) edited. It may be single
165 line or multi-line, support styles or not, be read-only or not, and even supports
166 text masking for such things as passwords.
167
168
169 """
170
171
172 if __name__ == '__main__':
173 import sys,os
174 import run
175 run.main(['', os.path.basename(sys.argv[0])])
176