1991-04-07 15:41:50 +02:00
|
|
|
# Text editing widget
|
|
|
|
|
1991-08-16 15:19:43 +02:00
|
|
|
# NB: this always assumes fixed bounds.
|
|
|
|
# For auto-growing TextEdit windows, different code would be needed.
|
|
|
|
|
1991-04-07 15:41:50 +02:00
|
|
|
from stdwinevents import *
|
|
|
|
|
1991-12-30 17:02:10 +01:00
|
|
|
class TextEdit:
|
1991-04-07 15:41:50 +02:00
|
|
|
#
|
1992-12-14 13:57:56 +01:00
|
|
|
def create(self, parent, (cols, rows)):
|
1991-04-07 15:41:50 +02:00
|
|
|
parent.addchild(self)
|
|
|
|
self.parent = parent
|
|
|
|
self.cols = cols
|
|
|
|
self.rows = rows
|
|
|
|
self.text = ''
|
|
|
|
# Creation of the editor is done in realize()
|
1991-08-16 15:19:43 +02:00
|
|
|
self.editor = None
|
|
|
|
self.dh = self.dv = 0
|
|
|
|
return self
|
|
|
|
#
|
1992-12-14 13:57:56 +01:00
|
|
|
def createboxed(self, parent, (cols, rows), (dh, dv)):
|
1991-08-16 15:19:43 +02:00
|
|
|
self = self.create(parent, (cols, rows))
|
|
|
|
self.dh = max(0, dh)
|
|
|
|
self.dv = max(0, dv)
|
1991-04-07 15:41:50 +02:00
|
|
|
return self
|
|
|
|
#
|
1991-04-21 21:27:48 +02:00
|
|
|
def settext(self, text):
|
|
|
|
self.editor.settext(text)
|
|
|
|
#
|
1992-08-07 00:33:41 +02:00
|
|
|
def gettext(self):
|
|
|
|
return self.editor.gettext(text)
|
|
|
|
#
|
1991-04-07 15:41:50 +02:00
|
|
|
# Downcalls from parent to child
|
|
|
|
#
|
|
|
|
def destroy(self):
|
|
|
|
del self.parent
|
|
|
|
del self.editor
|
|
|
|
del self.window
|
|
|
|
#
|
1992-12-14 13:57:56 +01:00
|
|
|
def getminsize(self, m, (width, height)):
|
1991-08-16 15:19:43 +02:00
|
|
|
width = max(0, width - 2*self.dh)
|
|
|
|
height = max(0, height - 2*self.dv)
|
|
|
|
if width > 0 and self.editor:
|
|
|
|
(left, top), (right, bottom) = self.editor.getrect()
|
|
|
|
act_width, act_height = right - left, bottom - top
|
|
|
|
if width >= act_width:
|
|
|
|
width = width + 2*self.dh
|
|
|
|
height = max(height, act_height) + 2*self.dv
|
|
|
|
return width, height
|
|
|
|
width = max(width, self.cols*m.textwidth('in')/2) + 2*self.dh
|
|
|
|
height = max(height, self.rows*m.lineheight()) + 2*self.dv
|
|
|
|
return width, height
|
|
|
|
#
|
1991-04-07 15:41:50 +02:00
|
|
|
def setbounds(self, bounds):
|
|
|
|
self.bounds = bounds
|
|
|
|
if self.editor:
|
1991-08-16 15:19:43 +02:00
|
|
|
(left, top), (right, bottom) = bounds
|
|
|
|
left = left + self.dh
|
|
|
|
top = top + self.dv
|
|
|
|
right = right - self.dh
|
|
|
|
bottom = bottom - self.dv
|
|
|
|
self.editor.move((left, top), (right, bottom))
|
|
|
|
if self.dh and self.dv:
|
|
|
|
(left, top), (right, bottom) = bounds
|
|
|
|
left = left + 1
|
|
|
|
top = top + 1
|
|
|
|
right = right - 1
|
|
|
|
bottom = bottom - 1
|
|
|
|
bounds = (left, top), (right, bottom)
|
|
|
|
self.editor.setview(bounds)
|
|
|
|
#
|
|
|
|
def getbounds(self):
|
|
|
|
return self.bounds
|
|
|
|
#
|
1991-04-07 15:41:50 +02:00
|
|
|
def realize(self):
|
|
|
|
self.window = self.parent.getwindow()
|
1991-08-16 15:19:43 +02:00
|
|
|
(left, top), (right, bottom) = self.bounds
|
|
|
|
left = left + self.dh
|
|
|
|
top = top + self.dv
|
|
|
|
right = right - self.dh
|
|
|
|
bottom = bottom - self.dv
|
|
|
|
self.editor = \
|
|
|
|
self.window.textcreate((left, top), (right, bottom))
|
|
|
|
self.editor.setactive(0)
|
|
|
|
bounds = self.bounds
|
|
|
|
if self.dh and self.dv:
|
|
|
|
(left, top), (right, bottom) = bounds
|
|
|
|
left = left + 1
|
|
|
|
top = top + 1
|
|
|
|
right = right - 1
|
|
|
|
bottom = bottom - 1
|
|
|
|
bounds = (left, top), (right, bottom)
|
|
|
|
self.editor.setview(bounds)
|
1991-04-21 21:27:48 +02:00
|
|
|
self.editor.settext(self.text)
|
1991-04-07 15:41:50 +02:00
|
|
|
self.parent.need_mouse(self)
|
|
|
|
self.parent.need_keybd(self)
|
|
|
|
self.parent.need_altdraw(self)
|
1991-08-16 15:19:43 +02:00
|
|
|
#
|
1992-12-14 13:57:56 +01:00
|
|
|
def draw(self, d, area):
|
1991-08-16 15:19:43 +02:00
|
|
|
if self.dh and self.dv:
|
|
|
|
d.box(self.bounds)
|
|
|
|
#
|
1991-04-07 15:41:50 +02:00
|
|
|
def altdraw(self, area):
|
|
|
|
self.editor.draw(area)
|
|
|
|
#
|
|
|
|
# Event downcalls
|
|
|
|
#
|
|
|
|
def mouse_down(self, detail):
|
|
|
|
x = self.editor.event(WE_MOUSE_DOWN, self.window, detail)
|
1991-08-16 15:19:43 +02:00
|
|
|
#
|
1991-04-07 15:41:50 +02:00
|
|
|
def mouse_move(self, detail):
|
|
|
|
x = self.editor.event(WE_MOUSE_MOVE, self.window, detail)
|
1991-08-16 15:19:43 +02:00
|
|
|
#
|
1991-04-07 15:41:50 +02:00
|
|
|
def mouse_up(self, detail):
|
|
|
|
x = self.editor.event(WE_MOUSE_UP, self.window, detail)
|
|
|
|
#
|
1992-12-14 13:57:56 +01:00
|
|
|
def keybd(self, type, detail):
|
1991-04-07 15:41:50 +02:00
|
|
|
x = self.editor.event(type, self.window, detail)
|
|
|
|
#
|
1991-08-16 15:19:43 +02:00
|
|
|
def activate(self):
|
|
|
|
self.editor.setfocus(0, 30000)
|
|
|
|
self.editor.setactive(1)
|
|
|
|
#
|
|
|
|
def deactivate(self):
|
|
|
|
self.editor.setactive(0)
|
|
|
|
#
|