]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxTextCtrl.py
fix text scrolling in GTK2 (patch 703988)
[wxWidgets.git] / wxPython / demo / wxTextCtrl.py
1 import sys
2 from wxPython.wx import *
3
4 #---------------------------------------------------------------------------
5
6 class TestPanel(wxPanel):
7 def OnSetFocus(self, evt):
8 print "OnSetFocus"
9 evt.Skip()
10 def OnKillFocus(self, evt):
11 print "OnKillFocus"
12 evt.Skip()
13 def OnWindowDestroy(self, evt):
14 print "OnWindowDestroy"
15 evt.Skip()
16
17
18 def __init__(self, parent, log):
19 wxPanel.__init__(self, parent, -1)
20 self.log = log
21
22 l1 = wxStaticText(self, -1, "wxTextCtrl")
23 t1 = wxTextCtrl(self, -1, "Test it out and see", size=(125, -1))
24 t1.SetInsertionPoint(0)
25 self.tc1 = t1
26 EVT_TEXT(self, t1.GetId(), self.EvtText)
27 EVT_CHAR(t1, self.EvtChar)
28 EVT_SET_FOCUS(t1, self.OnSetFocus)
29 EVT_KILL_FOCUS(t1, self.OnKillFocus)
30 EVT_WINDOW_DESTROY(t1, self.OnWindowDestroy)
31
32 l2 = wxStaticText(self, -1, "Passsword")
33 t2 = wxTextCtrl(self, -1, "", size=(125, -1), style=wxTE_PASSWORD)
34 EVT_TEXT(self, t2.GetId(), self.EvtText)
35
36 l3 = wxStaticText(self, -1, "Multi-line")
37 t3 = wxTextCtrl(self, -1,
38 "Here is a looooooooooooooong line of text set in the control.\n\n"
39 "The quick brown fox jumped over the lazy dog...",
40 size=(200, 100), style=wxTE_MULTILINE)
41 t3.SetInsertionPoint(0)
42 EVT_TEXT(self, t3.GetId(), self.EvtText)
43 b = wxButton(self, -1, "Test Replace")
44 EVT_BUTTON(self, b.GetId(), self.OnTestReplace)
45 b2 = wxButton(self, -1, "Test GetSelection")
46 EVT_BUTTON(self, b2.GetId(), self.OnTestGetSelection)
47 b3 = wxButton(self, -1, "Test WriteText")
48 EVT_BUTTON(self, b3.GetId(), self.OnTestWriteText)
49 self.tc = t3
50 b4 = wxButton(self, -1, "Test Simulated Event")
51 EVT_BUTTON(self, b4.GetId(), self.OnTestEvent)
52
53
54 l4 = wxStaticText(self, -1, "Rich Text")
55 t4 = wxTextCtrl(self, -1, "If supported by the native control, this is red, and this is a different font.",
56 size=(200, 100), style=wxTE_MULTILINE|wxTE_RICH2)
57 t4.SetInsertionPoint(0)
58 t4.SetStyle(44, 47, wxTextAttr("RED", "YELLOW"))
59 points = t4.GetFont().GetPointSize() # get the current size
60 f = wxFont(points+3, wxROMAN, wxITALIC, wxBOLD, True)
61 t4.SetStyle(63, 77, wxTextAttr("BLUE", wxNullColour, f))
62
63 l5 = wxStaticText(self, -1, "Test Positions")
64 t5 = wxTextCtrl(self, -1, "0123456789\n" * 5, size=(200, 100),
65 style = wxTE_MULTILINE
66 #| wxTE_RICH
67 | wxTE_RICH2
68 )
69 EVT_LEFT_DOWN(t5, self.OnT5LeftDown)
70 self.t5 = t5
71
72
73 bsizer = wxBoxSizer(wxVERTICAL)
74 bsizer.Add(b, 0, wxGROW|wxALL, 4)
75 bsizer.Add(b2, 0, wxGROW|wxALL, 4)
76 bsizer.Add(b3, 0, wxGROW|wxALL, 4)
77 bsizer.Add(b4, 0, wxGROW|wxALL, 4)
78
79 sizer = wxFlexGridSizer(cols=3, hgap=6, vgap=6)
80 sizer.AddMany([ l1, t1, (0,0),
81 l2, t2, (0,0),
82 l3, t3, bsizer,
83 l4, t4, (0,0),
84 l5, t5, (0,0),
85 ])
86 border = wxBoxSizer(wxVERTICAL)
87 border.Add(sizer, 0, wxALL, 25)
88 self.SetSizer(border)
89 self.SetAutoLayout(True)
90
91
92 def EvtText(self, event):
93 self.log.WriteText('EvtText: %s\n' % event.GetString())
94
95
96 def EvtChar(self, event):
97 self.log.WriteText('EvtChar: %d\n' % event.GetKeyCode())
98 event.Skip()
99
100
101 def OnTestReplace(self, evt):
102 self.tc.Replace(5, 9, "IS A")
103 #self.tc.Remove(5, 9)
104
105 def OnTestWriteText(self, evt):
106 self.tc.WriteText("TEXT")
107
108 def OnTestGetSelection(self, evt):
109 start, end = self.tc.GetSelection()
110 text = self.tc.GetValue()
111 if wxPlatform == "__WXMSW__": # This is why GetStringSelection was added
112 text = text.replace('\n', '\r\n')
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 if wxPlatform == "__WXMSW__": # This is why GetStringSelection was added
123 text = text.replace('\n', '\r\n')
124 self.log.write("single-line GetSelection(): (%d, %d)\n"
125 "\tGetStringSelection(): %s\n"
126 "\tSelectedText: %s\n" %
127 (start, end,
128 self.tc1.GetStringSelection(),
129 repr(text[start:end])))
130
131
132 def OnT5LeftDown(self, evt):
133 evt.Skip()
134 wxCallAfter(self.LogT5Position, evt)
135
136 def LogT5Position(self, evt):
137 text = self.t5.GetValue()
138 ip = self.t5.GetInsertionPoint()
139 lp = self.t5.GetLastPosition()
140 self.log.write("LogT5Position:\n"
141 "\tGetInsertionPoint:\t%d\n"
142 "\ttext[insertionpoint]:\t%s\n"
143 "\tGetLastPosition:\t%d\n"
144 "\tlen(text):\t\t%d\n"
145 % (ip, text[ip], lp, len(text)))
146
147
148 def OnTestEvent(self, evt):
149 ke = wxKeyEvent(wxEVT_CHAR)
150 ke.SetEventObject(self.tc1)
151 ke.SetId(self.tc1.GetId())
152 ke.m_keyCode = ord('A')
153 self.tc1.GetEventHandler().ProcessEvent(ke)
154
155
156 #---------------------------------------------------------------------------
157
158 def runTest(frame, nb, log):
159 win = TestPanel(nb, log)
160 return win
161
162 #---------------------------------------------------------------------------
163
164
165
166
167 overview = """\
168 """
169
170
171
172
173 if __name__ == '__main__':
174 import sys,os
175 import run
176 run.main(['', os.path.basename(sys.argv[0])])
177