Re: stdwin examples

Bill Janssen (janssen@parc.xerox.com)
Fri, 4 Mar 1994 13:30:07 PST

No bitmaps, but here's a little emacs I wrote. (Now that we have
lambda, I have to re-write it.):

# this module defines an Emacs-style editing module for STDWIN

import stdwin
from stdwinevents import *
import string
import rect

font = 'lucidasanstypewriter-12'
fgcolor = 'black'
bgcolor = 'white'

BEGINNING = 0
END = 0x7FFFFFFF

read_only = 'Attempt to modify readonly buffer'

class emacs:

def init(self, label):
stdwin.setfont(font)
stdwin.setfgcolor(stdwin.fetchcolor(fgcolor))
stdwin.setbgcolor(stdwin.fetchcolor(bgcolor))
self.stdwindow = stdwin.open(label or '<Unlabelled>')
self.textgadget = self.stdwindow.textcreate(((0,0),
self.stdwindow.getwinsize()))
self.mark = None
self.active = 1
self.repcount = 1;
self.readonly = 0;
self.quotechar = 0;
self.gathercount = 0;
self.showevents = 1
return self

def setfont(self, font):
self.stdwindow.setfont(font)

def setcolors(self, fgcolor, bgcolor):
if (fgcolor):
self.stdwindow.setfgcolor(stdwin.fetchcolor(fgcolor))
if (bgcolor):
self.stdwindow.setbgcolor(stdwin.fetchcolor(bgcolor))

def region(self):
return (self.textgadget.getfocus())

def text(self, bounds):
t = self.textgadget.gettext()
if (not bounds): return t
else: return(t[bounds[0]:bounds[1]])

def regiontext(self):
return (self.text(self.textgadget.getfocus()))

def handleevent(self, type, detail):

if (type == WE_DRAW):
d = self.stdwindow.begindrawing()
d.shade(detail, 10)
i = rect.intersect([detail, self.textgadget.getrect()])
if i:
d.erase(i)
del d
if i:
self.textgadget.draw(i)

elif (type == WE_SIZE):
r = self.textgadget.getrect()
w = self.stdwindow.getwinsize()
r = rect.rect2geom(r[0], r[1])
print 'win size is ', w, ' and text size is ', r[1]
if (w[0] != r[1][0]): # width compared to width
self.textgadget.move((0, 0), (w[0], r[1][1]))
self.stdwindow.setdocsize((w[0], r[1][1]))

elif (type == WE_COMMAND):
if (detail == WC_BACKSPACE):
junk = self.delete(- self.repcount)
elif (detail == WC_RETURN):
junk = self.insert('\n')
elif ((detail == WC_UP) or (detail == WC_DOWN) or (detail == WC_LEFT)
or (detail == WC_RIGHT)):
for x in range(0,self.repcount):
self.textgadget.arrow(detail)
elif (type == WE_CHAR) and self.quotechar:
junk = self.insert(detail)
self.quotechar = 0
elif (type == WE_CHAR and self.gathercount and (detail in string.digits)):
self.repcount = self.repcount * (ord(detail[0]) - ord('a'[0]))
elif (type == WE_CHAR and detail == '\25'):
self.gathercount = 1
elif (type == WE_CHAR):
if (ord(detail[0]) >= ord(' '[0]) and ord(detail[0]) <= ord('~'[0])):
junk = self.insert(detail)
elif (detail == '\0'): # Control-@ (NUL)
self.mark = self.point()
self.setpoint(self.mark)
print 'self.mark is', self.mark
elif (detail == '\001'): # Control-A
self.setpoint(BEGINNING)
elif (detail == '\002'): # Control-B
junk = self.back(self.repcount)
elif (detail == '\3'): # Control-C
pass
elif (detail == '\4'): # Control-D
junk = self.delete(self.repcount)
elif (detail == '\5'): # Control-E
self.setpoint(END)
elif (detail == '\006'): # Control-F
junk = self.forward(self.repcount)
elif (detail == '\7'): # Control-G
raise KeyboardInterrupt
elif (detail == '\10'): # Control-H
self.textgadget.arrow(WC_LEFT)
elif (detail == '\11'): # Control-I
pass
elif (detail == '\12'): # Control-J
pass
elif (detail == '\13'): # Control-K
pass
elif (detail == '\14'): # Control-L
self.textgadget.draw(self.textgadget.getrect())
elif (detail == '\15'): # Control-M
pass
elif (detail == '\16'): # Control-N
for x in range(0, self.repcount):
self.textgadget.arrow(WC_DOWN)
elif (detail == '\17'): # Control-O
for x in range(0, self.repcount):
junk = self.insert('\n')
junk = self.back(self.repcount)
elif (detail == '\20'): # Control-P
for x in range(0, self.repcount):
self.textgadget.arrow(WC_UP)
elif (detail == '\21'): # Control-Q
self.quotechar = 1
elif (detail == '\22'): # Control-R
pass
elif (detail == '\23'): # Control-S
pass
elif (detail == '\24'): # Control-T
pass
# elif (detail == '\25'): # Control-U
# self.gathercount = 1
elif (detail == '\26'): # Control-V
pass
elif (detail == '\27'): # Control-W
self.cut()
elif (detail == '\30'): # Control-X
pass
elif (detail == '\31'): # Control-Y
self.paste()
elif (detail == '\32'): # Control-Z
self.active = 0
elif (detail == '\33'): # Control-[ (ESC)
pass
elif (detail == '\34'): # Control-\ (FS)
pass
elif (detail == '\35'): # Control-] (GS)
pass
elif (detail == '\36'): # Control-^ (RS)
pass
elif (detail == '\37'): # Control-_ (US)
pass
self.repcount = 1
self.gathercount = 0
elif (type == WE_CLOSE):
self.active = 0
else:
junk = self.textgadget.event(type, self.stdwindow, detail)

def handleevents(self):
self.active = 1
while self.active:
(type, win, detail) = stdwin.getevent()
if (self.showevents):
print 'type =', type, ' win =', win, ' detail = ', detail
if (win != self.stdwindow): continue
junk = self.handleevent(type, detail)

def cut (self):
stdwin.setcutbuffer(0, self.textgadget.getfocustext())
self.textgadget.replace('')
r = self.textgadget.getrect()
self.mark = None
self.stdwindow.setdocsize((r[1][0] - r[0][0], r[1][1] - r[0][1]))

def paste (self):
self.collapse(1)
self.mark = self.point()
self.insert(stdwin.getcutbuffer(0))
self.setpoint (self.point())

def collapse (self, beginning_p):
focus = self.textgadget.getfocus()
if (beginning_p):
self.textgadget.setfocus(focus[0], focus[0])
else:
self.textgadget.setfocus(focus[1], focus[1])
return focus

def point (self):
focus = self.textgadget.getfocus()
if (self.mark == None or focus[0] != self.mark):
return focus[0]
else:
return focus[1]

def setpoint (self, point):
p = max(0, point)
print 'p =', p
if (self.mark != None and self.mark != p):
if (self.mark < p):
self.textgadget.setfocus(self.mark, p)
elif (self.mark > p):
self.textgadget.setfocus(p, self.mark)
else:
self.textgadget.setfocus(p, p)
return p

def back (self, amount):
point = self.point()
if (not amount):
return (point)
return self.setpoint(point - amount)

def forward (self, amount):
point = self.point()
if (not amount):
return (point)
return self.setpoint(point + amount)

def insert (self, str):
if (self.readonly):
raise read_only
focus = self.collapse(0)
self.textgadget.replace(str)
self.setpoint(self.point())
r = self.textgadget.getrect()
self.stdwindow.setdocsize((r[1][0] - r[0][0], r[1][1] - r[0][1]))
return (focus[0] + len(str))

# "delete" is tricky. If a region is selected, delete it and ignore
the count.
# if no region is selected, delete "count" chars before point.
def delete (self, count):
if (self.readonly):
raise read_only
if (count == 0):
return
fo = self.textgadget.getfocus()
if (fo[1] == fo[0]):
if (count > 0):
junk = self.textgadget.setfocus(fo[0], fo[0] + count)
else:
junk = self.textgadget.setfocus(fo[0] + count, fo[0])
self.textgadget.replace('')
r = self.textgadget.getrect()
self.mark = None
self.stdwindow.setdocsize((r[1][0] - r[0][0], r[1][1] - r[0][1]))
return

def edittext(txt):
a = emacs().init('Control-Z to exit:')
junk = a.insert(txt)
a.setpoint(0)
a.handleevents()
return (a.text(None))

def test():
edittext('This is just\nsome sample text for editing.')