blob: 540692ce2431498ba97d1c7cab9a3de77b7dc5e1 [file] [log] [blame]
Guido van Rossum2d844d11991-04-07 13:41:50 +00001# Text editing widget
2
Guido van Rossum3c8045a1991-08-16 13:19:43 +00003# NB: this always assumes fixed bounds.
4# For auto-growing TextEdit windows, different code would be needed.
5
Guido van Rossum2d844d11991-04-07 13:41:50 +00006from stdwinevents import *
7
Guido van Rossumd4045691991-12-30 16:02:10 +00008class TextEdit:
Guido van Rossum2d844d11991-04-07 13:41:50 +00009 #
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()
Guido van Rossum3c8045a1991-08-16 13:19:43 +000017 self.editor = None
18 self.dh = self.dv = 0
19 return self
20 #
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)
Guido van Rossum2d844d11991-04-07 13:41:50 +000025 return self
26 #
Guido van Rossuma82a2751991-04-21 19:27:48 +000027 def settext(self, text):
28 self.editor.settext(text)
29 #
Guido van Rossum2d844d11991-04-07 13:41:50 +000030 # Downcalls from parent to child
31 #
32 def destroy(self):
33 del self.parent
34 del self.editor
35 del self.window
36 #
Guido van Rossum3c8045a1991-08-16 13:19:43 +000037 def getminsize(self, (m, (width, height))):
38 width = max(0, width - 2*self.dh)
39 height = max(0, height - 2*self.dv)
40 if width > 0 and self.editor:
41 (left, top), (right, bottom) = self.editor.getrect()
42 act_width, act_height = right - left, bottom - top
43 if width >= act_width:
44 width = width + 2*self.dh
45 height = max(height, act_height) + 2*self.dv
46 return width, height
47 width = max(width, self.cols*m.textwidth('in')/2) + 2*self.dh
48 height = max(height, self.rows*m.lineheight()) + 2*self.dv
49 return width, height
50 #
Guido van Rossum2d844d11991-04-07 13:41:50 +000051 def setbounds(self, bounds):
52 self.bounds = bounds
53 if self.editor:
Guido van Rossum3c8045a1991-08-16 13:19:43 +000054 (left, top), (right, bottom) = bounds
55 left = left + self.dh
56 top = top + self.dv
57 right = right - self.dh
58 bottom = bottom - self.dv
59 self.editor.move((left, top), (right, bottom))
60 if self.dh and self.dv:
61 (left, top), (right, bottom) = bounds
62 left = left + 1
63 top = top + 1
64 right = right - 1
65 bottom = bottom - 1
66 bounds = (left, top), (right, bottom)
67 self.editor.setview(bounds)
68 #
69 def getbounds(self):
70 return self.bounds
71 #
Guido van Rossum2d844d11991-04-07 13:41:50 +000072 def realize(self):
73 self.window = self.parent.getwindow()
Guido van Rossum3c8045a1991-08-16 13:19:43 +000074 (left, top), (right, bottom) = self.bounds
75 left = left + self.dh
76 top = top + self.dv
77 right = right - self.dh
78 bottom = bottom - self.dv
79 self.editor = \
80 self.window.textcreate((left, top), (right, bottom))
81 self.editor.setactive(0)
82 bounds = self.bounds
83 if self.dh and self.dv:
84 (left, top), (right, bottom) = bounds
85 left = left + 1
86 top = top + 1
87 right = right - 1
88 bottom = bottom - 1
89 bounds = (left, top), (right, bottom)
90 self.editor.setview(bounds)
Guido van Rossuma82a2751991-04-21 19:27:48 +000091 self.editor.settext(self.text)
Guido van Rossum2d844d11991-04-07 13:41:50 +000092 self.parent.need_mouse(self)
93 self.parent.need_keybd(self)
94 self.parent.need_altdraw(self)
Guido van Rossum3c8045a1991-08-16 13:19:43 +000095 #
Guido van Rossum2d844d11991-04-07 13:41:50 +000096 def draw(self, (d, area)):
Guido van Rossum3c8045a1991-08-16 13:19:43 +000097 if self.dh and self.dv:
98 d.box(self.bounds)
99 #
Guido van Rossum2d844d11991-04-07 13:41:50 +0000100 def altdraw(self, area):
101 self.editor.draw(area)
102 #
103 # Event downcalls
104 #
105 def mouse_down(self, detail):
106 x = self.editor.event(WE_MOUSE_DOWN, self.window, detail)
Guido van Rossum3c8045a1991-08-16 13:19:43 +0000107 #
Guido van Rossum2d844d11991-04-07 13:41:50 +0000108 def mouse_move(self, detail):
109 x = self.editor.event(WE_MOUSE_MOVE, self.window, detail)
Guido van Rossum3c8045a1991-08-16 13:19:43 +0000110 #
Guido van Rossum2d844d11991-04-07 13:41:50 +0000111 def mouse_up(self, detail):
112 x = self.editor.event(WE_MOUSE_UP, self.window, detail)
113 #
114 def keybd(self, (type, detail)):
115 x = self.editor.event(type, self.window, detail)
116 #
Guido van Rossum3c8045a1991-08-16 13:19:43 +0000117 def activate(self):
118 self.editor.setfocus(0, 30000)
119 self.editor.setactive(1)
120 #
121 def deactivate(self):
122 self.editor.setactive(0)
123 #