blob: e08e49681196193403e6b65396a9c7a4ec5e791b [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 #
Guido van Rossum89a78691992-12-14 12:57:56 +000010 def create(self, parent, (cols, rows)):
Guido van Rossum2d844d11991-04-07 13:41:50 +000011 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 #
Guido van Rossum89a78691992-12-14 12:57:56 +000021 def createboxed(self, parent, (cols, rows), (dh, dv)):
Guido van Rossum3c8045a1991-08-16 13:19:43 +000022 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 Rossum2d4aa4f1992-08-06 22:33:41 +000030 def gettext(self):
31 return self.editor.gettext(text)
32 #
Guido van Rossum2d844d11991-04-07 13:41:50 +000033 # Downcalls from parent to child
34 #
35 def destroy(self):
36 del self.parent
37 del self.editor
38 del self.window
39 #
Guido van Rossum89a78691992-12-14 12:57:56 +000040 def getminsize(self, m, (width, height)):
Guido van Rossum3c8045a1991-08-16 13:19:43 +000041 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
53 #
Guido van Rossum2d844d11991-04-07 13:41:50 +000054 def setbounds(self, bounds):
55 self.bounds = bounds
56 if self.editor:
Guido van Rossum3c8045a1991-08-16 13:19:43 +000057 (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)
71 #
72 def getbounds(self):
73 return self.bounds
74 #
Guido van Rossum2d844d11991-04-07 13:41:50 +000075 def realize(self):
76 self.window = self.parent.getwindow()
Guido van Rossum3c8045a1991-08-16 13:19:43 +000077 (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)
Guido van Rossuma82a2751991-04-21 19:27:48 +000094 self.editor.settext(self.text)
Guido van Rossum2d844d11991-04-07 13:41:50 +000095 self.parent.need_mouse(self)
96 self.parent.need_keybd(self)
97 self.parent.need_altdraw(self)
Guido van Rossum3c8045a1991-08-16 13:19:43 +000098 #
Guido van Rossum89a78691992-12-14 12:57:56 +000099 def draw(self, d, area):
Guido van Rossum3c8045a1991-08-16 13:19:43 +0000100 if self.dh and self.dv:
101 d.box(self.bounds)
102 #
Guido van Rossum2d844d11991-04-07 13:41:50 +0000103 def altdraw(self, area):
104 self.editor.draw(area)
105 #
106 # Event downcalls
107 #
108 def mouse_down(self, detail):
109 x = self.editor.event(WE_MOUSE_DOWN, self.window, detail)
Guido van Rossum3c8045a1991-08-16 13:19:43 +0000110 #
Guido van Rossum2d844d11991-04-07 13:41:50 +0000111 def mouse_move(self, detail):
112 x = self.editor.event(WE_MOUSE_MOVE, self.window, detail)
Guido van Rossum3c8045a1991-08-16 13:19:43 +0000113 #
Guido van Rossum2d844d11991-04-07 13:41:50 +0000114 def mouse_up(self, detail):
115 x = self.editor.event(WE_MOUSE_UP, self.window, detail)
116 #
Guido van Rossum89a78691992-12-14 12:57:56 +0000117 def keybd(self, type, detail):
Guido van Rossum2d844d11991-04-07 13:41:50 +0000118 x = self.editor.event(type, self.window, detail)
119 #
Guido van Rossum3c8045a1991-08-16 13:19:43 +0000120 def activate(self):
121 self.editor.setfocus(0, 30000)
122 self.editor.setactive(1)
123 #
124 def deactivate(self):
125 self.editor.setactive(0)
126 #