Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / conch / examples / window.tac
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 """
5 Widgets demo.
6
7 You can run this .tac file directly with:
8     twistd -ny window.tac
9
10 Demonstrates various widgets or buttons, such as scrollable regions,
11 drawable canvas, etc.
12
13 This demo sets up two listening ports: one on 6022 which accepts ssh
14 connections; one on 6023 which accepts telnet connections.  No login for the
15 telnet server is required; for the ssh server, "username" is the username and
16 "password" is the password.
17 """
18
19 from __future__ import division
20
21 import string, random
22
23 from twisted.python import log
24 from twisted.internet import protocol, task
25 from twisted.application import internet, service
26 from twisted.cred import checkers, portal
27
28 from twisted.conch.insults import insults, window
29 from twisted.conch.telnet import TelnetTransport, TelnetBootstrapProtocol
30 from twisted.conch.manhole_ssh import ConchFactory, TerminalRealm
31
32 from twisted.internet import reactor
33
34 class DrawableCanvas(window.Canvas):
35     x = 0
36     y = 0
37
38     def func_LEFT_ARROW(self, modifier):
39         self.x -= 1
40         self.repaint()
41
42     def func_RIGHT_ARROW(self, modifier):
43         self.x += 1
44         self.repaint()
45
46     def func_UP_ARROW(self, modifier):
47         self.y -= 1
48         self.repaint()
49
50     def func_DOWN_ARROW(self, modifier):
51         self.y += 1
52         self.repaint()
53
54     def characterReceived(self, keyID, modifier):
55         self[self.x, self.y] = keyID
56         self.x += 1
57         self.repaint()
58
59     def keystrokeReceived(self, keyID, modifier):
60         if keyID == '\r' or keyID == '\v':
61             return
62         window.Canvas.keystrokeReceived(self, keyID, modifier)
63         if self.x >= self.width:
64             self.x = 0
65         elif self.x < 0:
66             self.x = self.width - 1
67
68         if self.y >= self.height:
69             self.y = 0
70         elif self.y < 0:
71             self.y = self.height - 1
72         self.repaint()
73
74     def render(self, width, height, terminal):
75         window.Canvas.render(self, width, height, terminal)
76         if self.focused:
77             terminal.cursorPosition(self.x, self.y)
78             window.cursor(terminal, self[self.x, self.y])
79
80
81 class ButtonDemo(insults.TerminalProtocol):
82     width = 80
83     height = 24
84
85     def _draw(self):
86         self.window.draw(self.width, self.height, self.terminal)
87
88     def _redraw(self):
89         self.window.filthy()
90         self._draw()
91
92     def _schedule(self, f):
93         reactor.callLater(0, f)
94
95     def connectionMade(self):
96         self.terminal.eraseDisplay()
97         self.terminal.resetPrivateModes([insults.privateModes.CURSOR_MODE])
98
99         self.window = window.TopWindow(self._draw, self._schedule)
100         self.output = window.TextOutput((15, 1))
101         self.input = window.TextInput(15, self._setText)
102         self.select1 = window.Selection(map(str, range(100)), self._setText, 10)
103         self.select2 = window.Selection(map(str, range(200, 300)), self._setText, 10)
104         self.button = window.Button("Clear", self._clear)
105         self.canvas = DrawableCanvas()
106
107         hbox = window.HBox()
108         hbox.addChild(self.input)
109         hbox.addChild(self.output)
110         hbox.addChild(window.Border(self.button))
111         hbox.addChild(window.Border(self.select1))
112         hbox.addChild(window.Border(self.select2))
113
114         t1 = window.TextOutputArea(longLines=window.TextOutputArea.WRAP)
115         t2 = window.TextOutputArea(longLines=window.TextOutputArea.TRUNCATE)
116         t3 = window.TextOutputArea(longLines=window.TextOutputArea.TRUNCATE)
117         t4 = window.TextOutputArea(longLines=window.TextOutputArea.TRUNCATE)
118         for _t in t1, t2, t3, t4:
119             _t.setText((('This is a very long string.  ' * 3) + '\n') * 3)
120
121         vp = window.Viewport(t3)
122         d = [1]
123         def spin():
124             vp.xOffset += d[0]
125             if vp.xOffset == 0 or vp.xOffset == 25:
126                 d[0] *= -1
127         self.call = task.LoopingCall(spin)
128         self.call.start(0.25, now=False)
129         hbox.addChild(window.Border(vp))
130
131         vp2 = window.ScrolledArea(t4)
132         hbox.addChild(vp2)
133
134         texts = window.VBox()
135         texts.addChild(window.Border(t1))
136         texts.addChild(window.Border(t2))
137
138         areas = window.HBox()
139         areas.addChild(window.Border(self.canvas))
140         areas.addChild(texts)
141
142         vbox = window.VBox()
143         vbox.addChild(hbox)
144         vbox.addChild(areas)
145         self.window.addChild(vbox)
146         self.terminalSize(self.width, self.height)
147
148     def connectionLost(self, reason):
149         self.call.stop()
150         insults.TerminalProtocol.connectionLost(self, reason)
151
152     def terminalSize(self, width, height):
153         self.width = width
154         self.height = height
155         self.terminal.eraseDisplay()
156         self._redraw()
157
158
159     def keystrokeReceived(self, keyID, modifier):
160         self.window.keystrokeReceived(keyID, modifier)
161
162     def _clear(self):
163         self.canvas.clear()
164
165     def _setText(self, text):
166         self.input.setText('')
167         self.output.setText(text)
168
169
170 def makeService(args):
171     checker = checkers.InMemoryUsernamePasswordDatabaseDontUse(username="password")
172
173     f = protocol.ServerFactory()
174     f.protocol = lambda: TelnetTransport(TelnetBootstrapProtocol,
175                                          insults.ServerProtocol,
176                                          args['protocolFactory'],
177                                          *args.get('protocolArgs', ()),
178                                          **args.get('protocolKwArgs', {}))
179     tsvc = internet.TCPServer(args['telnet'], f)
180
181     def chainProtocolFactory():
182         return insults.ServerProtocol(
183             args['protocolFactory'],
184             *args.get('protocolArgs', ()),
185             **args.get('protocolKwArgs', {}))
186
187     rlm = TerminalRealm()
188     rlm.chainedProtocolFactory = chainProtocolFactory
189     ptl = portal.Portal(rlm, [checker])
190     f = ConchFactory(ptl)
191     csvc = internet.TCPServer(args['ssh'], f)
192
193     m = service.MultiService()
194     tsvc.setServiceParent(m)
195     csvc.setServiceParent(m)
196     return m
197
198 application = service.Application("Window Demo")
199
200 makeService({'protocolFactory': ButtonDemo,
201              'telnet': 6023,
202              'ssh': 6022}).setServiceParent(application)