Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 1 | # Text editing widget |
| 2 | |
Guido van Rossum | 3c8045a | 1991-08-16 13:19:43 +0000 | [diff] [blame] | 3 | # NB: this always assumes fixed bounds. |
| 4 | # For auto-growing TextEdit windows, different code would be needed. |
| 5 | |
Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 6 | from stdwinevents import * |
| 7 | |
Guido van Rossum | d404569 | 1991-12-30 16:02:10 +0000 | [diff] [blame^] | 8 | class TextEdit: |
Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 9 | # |
| 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 Rossum | 3c8045a | 1991-08-16 13:19:43 +0000 | [diff] [blame] | 17 | 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 Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 25 | return self |
| 26 | # |
Guido van Rossum | a82a275 | 1991-04-21 19:27:48 +0000 | [diff] [blame] | 27 | def settext(self, text): |
| 28 | self.editor.settext(text) |
| 29 | # |
Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 30 | # 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 Rossum | 3c8045a | 1991-08-16 13:19:43 +0000 | [diff] [blame] | 37 | 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 Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 51 | def setbounds(self, bounds): |
| 52 | self.bounds = bounds |
| 53 | if self.editor: |
Guido van Rossum | 3c8045a | 1991-08-16 13:19:43 +0000 | [diff] [blame] | 54 | (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 Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 72 | def realize(self): |
| 73 | self.window = self.parent.getwindow() |
Guido van Rossum | 3c8045a | 1991-08-16 13:19:43 +0000 | [diff] [blame] | 74 | (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 Rossum | a82a275 | 1991-04-21 19:27:48 +0000 | [diff] [blame] | 91 | self.editor.settext(self.text) |
Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 92 | self.parent.need_mouse(self) |
| 93 | self.parent.need_keybd(self) |
| 94 | self.parent.need_altdraw(self) |
Guido van Rossum | 3c8045a | 1991-08-16 13:19:43 +0000 | [diff] [blame] | 95 | # |
Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 96 | def draw(self, (d, area)): |
Guido van Rossum | 3c8045a | 1991-08-16 13:19:43 +0000 | [diff] [blame] | 97 | if self.dh and self.dv: |
| 98 | d.box(self.bounds) |
| 99 | # |
Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 100 | 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 Rossum | 3c8045a | 1991-08-16 13:19:43 +0000 | [diff] [blame] | 107 | # |
Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 108 | def mouse_move(self, detail): |
| 109 | x = self.editor.event(WE_MOUSE_MOVE, self.window, detail) |
Guido van Rossum | 3c8045a | 1991-08-16 13:19:43 +0000 | [diff] [blame] | 110 | # |
Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 111 | 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 Rossum | 3c8045a | 1991-08-16 13:19:43 +0000 | [diff] [blame] | 117 | 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 | # |