Added 'list_only' option (and modified 'run()' to respect it).
[python/dscho.git] / Lib / lib-stdwin / TextEdit.py
blobe08e49681196193403e6b65396a9c7a4ec5e791b
1 # Text editing widget
3 # NB: this always assumes fixed bounds.
4 # For auto-growing TextEdit windows, different code would be needed.
6 from stdwinevents import *
8 class TextEdit:
10 def create(self, parent, (cols, rows)):
11 parent.addchild(self)
12 self.parent = parent
13 self.cols = cols
14 self.rows = rows
15 self.text = ''
16 # Creation of the editor is done in realize()
17 self.editor = None
18 self.dh = self.dv = 0
19 return self
21 def createboxed(self, parent, (cols, rows), (dh, dv)):
22 self = self.create(parent, (cols, rows))
23 self.dh = max(0, dh)
24 self.dv = max(0, dv)
25 return self
27 def settext(self, text):
28 self.editor.settext(text)
30 def gettext(self):
31 return self.editor.gettext(text)
33 # Downcalls from parent to child
35 def destroy(self):
36 del self.parent
37 del self.editor
38 del self.window
40 def getminsize(self, m, (width, height)):
41 width = max(0, width - 2*self.dh)
42 height = max(0, height - 2*self.dv)
43 if width > 0 and self.editor:
44 (left, top), (right, bottom) = self.editor.getrect()
45 act_width, act_height = right - left, bottom - top
46 if width >= act_width:
47 width = width + 2*self.dh
48 height = max(height, act_height) + 2*self.dv
49 return width, height
50 width = max(width, self.cols*m.textwidth('in')/2) + 2*self.dh
51 height = max(height, self.rows*m.lineheight()) + 2*self.dv
52 return width, height
54 def setbounds(self, bounds):
55 self.bounds = bounds
56 if self.editor:
57 (left, top), (right, bottom) = bounds
58 left = left + self.dh
59 top = top + self.dv
60 right = right - self.dh
61 bottom = bottom - self.dv
62 self.editor.move((left, top), (right, bottom))
63 if self.dh and self.dv:
64 (left, top), (right, bottom) = bounds
65 left = left + 1
66 top = top + 1
67 right = right - 1
68 bottom = bottom - 1
69 bounds = (left, top), (right, bottom)
70 self.editor.setview(bounds)
72 def getbounds(self):
73 return self.bounds
75 def realize(self):
76 self.window = self.parent.getwindow()
77 (left, top), (right, bottom) = self.bounds
78 left = left + self.dh
79 top = top + self.dv
80 right = right - self.dh
81 bottom = bottom - self.dv
82 self.editor = \
83 self.window.textcreate((left, top), (right, bottom))
84 self.editor.setactive(0)
85 bounds = self.bounds
86 if self.dh and self.dv:
87 (left, top), (right, bottom) = bounds
88 left = left + 1
89 top = top + 1
90 right = right - 1
91 bottom = bottom - 1
92 bounds = (left, top), (right, bottom)
93 self.editor.setview(bounds)
94 self.editor.settext(self.text)
95 self.parent.need_mouse(self)
96 self.parent.need_keybd(self)
97 self.parent.need_altdraw(self)
99 def draw(self, d, area):
100 if self.dh and self.dv:
101 d.box(self.bounds)
103 def altdraw(self, area):
104 self.editor.draw(area)
106 # Event downcalls
108 def mouse_down(self, detail):
109 x = self.editor.event(WE_MOUSE_DOWN, self.window, detail)
111 def mouse_move(self, detail):
112 x = self.editor.event(WE_MOUSE_MOVE, self.window, detail)
114 def mouse_up(self, detail):
115 x = self.editor.event(WE_MOUSE_UP, self.window, detail)
117 def keybd(self, type, detail):
118 x = self.editor.event(type, self.window, detail)
120 def activate(self):
121 self.editor.setfocus(0, 30000)
122 self.editor.setactive(1)
124 def deactivate(self):
125 self.editor.setactive(0)