fixed several event handling buglets, added command Q support, added a new module doc string, some formatting nits. (jvr)

This commit is contained in:
Just van Rossum 2000-03-27 16:22:53 +00:00
parent 89489fa15d
commit 7ec7c8ad66
1 changed files with 38 additions and 24 deletions

View File

@ -1,6 +1,9 @@
"""MiniAEFrame - A first stab at an AE Application framework. """MiniAEFrame - A minimal AppleEvent Application framework.
This framework is still work-in-progress, so do not rely on it remaining
unchanged. There are two classes:
AEServer -- a mixin class offering nice AE handling.
MiniApplication -- a very minimal alternative to FrameWork.py,
only suitable for the simplest of AppleEvent servers.
""" """
import sys import sys
@ -20,9 +23,11 @@ import EasyDialogs
kHighLevelEvent = 23 # Not defined anywhere for Python yet? kHighLevelEvent = 23 # Not defined anywhere for Python yet?
class MiniApplication:
"""A minimal FrameWork.Application-like class"""
class MiniApplication:
"""A minimal FrameWork.Application-like class"""
def __init__(self): def __init__(self):
self.quitting = 0 self.quitting = 0
# Initialize menu # Initialize menu
@ -30,24 +35,25 @@ class MiniApplication:
self.quitid = 2 self.quitid = 2
Menu.ClearMenuBar() Menu.ClearMenuBar()
self.applemenu = applemenu = Menu.NewMenu(self.appleid, "\024") self.applemenu = applemenu = Menu.NewMenu(self.appleid, "\024")
applemenu.AppendMenu("All about cgitest...;(-") applemenu.AppendMenu("About %s...;(-" % self.__class__.__name__)
applemenu.AppendResMenu('DRVR') applemenu.AppendResMenu('DRVR')
applemenu.InsertMenu(0) applemenu.InsertMenu(0)
self.quitmenu = Menu.NewMenu(self.quitid, "File") self.quitmenu = Menu.NewMenu(self.quitid, "File")
self.quitmenu.AppendMenu("Quit") self.quitmenu.AppendMenu("Quit")
self.quitmenu.SetItemCmd(1, ord("Q"))
self.quitmenu.InsertMenu(0) self.quitmenu.InsertMenu(0)
Menu.DrawMenuBar() Menu.DrawMenuBar()
def __del__(self): def __del__(self):
self.close() self.close()
def close(self): def close(self):
pass pass
def mainloop(self, mask = everyEvent, timeout = 60*60): def mainloop(self, mask = everyEvent, timeout = 60*60):
while not self.quitting: while not self.quitting:
self.dooneevent(mask, timeout) self.dooneevent(mask, timeout)
def _quit(self): def _quit(self):
self.quitting = 1 self.quitting = 1
@ -71,8 +77,11 @@ class MiniApplication:
return return
elif what == keyDown: elif what == keyDown:
c = chr(message & charCodeMask) c = chr(message & charCodeMask)
if c == '.' and modifiers & cmdKey: if modifiers & cmdKey:
raise KeyboardInterrupt, "Command-period" if c == '.':
raise KeyboardInterrupt, "Command-period"
if c == 'q':
self.quitting = 1
elif what == mouseDown: elif what == mouseDown:
partcode, window = Win.FindWindow(where) partcode, window = Win.FindWindow(where)
if partcode == inMenuBar: if partcode == inMenuBar:
@ -81,23 +90,26 @@ class MiniApplication:
item = result & 0xffff # Lo word item = result & 0xffff # Lo word
if id == self.appleid: if id == self.appleid:
if item == 1: if item == 1:
EasyDialogs.Message("cgitest - First cgi test") EasyDialogs.Message(self.getabouttext())
return
elif item > 1: elif item > 1:
name = self.applemenu.GetItem(item) name = self.applemenu.GetMenuItemText(item)
Qd.OpenDeskAcc(name) Menu.OpenDeskAcc(name)
return elif id == self.quitid and item == 1:
if id == self.quitid and item == 1:
print "Menu-requested QUIT"
self.quitting = 1 self.quitting = 1
# Anything not handled is passed to Python/SIOUX Menu.HiliteMenu(0)
MacOS.HandleEvent(event) else:
# Anything not handled is passed to Python/SIOUX
MacOS.HandleEvent(event)
def getabouttext(self):
return self.__class__.__name__
class AEServer: class AEServer:
def __init__(self): def __init__(self):
self.ae_handlers = {} self.ae_handlers = {}
def installaehandler(self, classe, type, callback): def installaehandler(self, classe, type, callback):
AE.AEInstallEventHandler(classe, type, self.callback_wrapper) AE.AEInstallEventHandler(classe, type, self.callback_wrapper)
self.ae_handlers[(classe, type)] = callback self.ae_handlers[(classe, type)] = callback
@ -105,7 +117,7 @@ class AEServer:
def close(self): def close(self):
for classe, type in self.ae_handlers.keys(): for classe, type in self.ae_handlers.keys():
AE.AERemoveEventHandler(classe, type) AE.AERemoveEventHandler(classe, type)
def callback_wrapper(self, _request, _reply): def callback_wrapper(self, _request, _reply):
_parameters, _attributes = aetools.unpackevent(_request) _parameters, _attributes = aetools.unpackevent(_request)
_class = _attributes['evcl'].type _class = _attributes['evcl'].type
@ -142,7 +154,8 @@ class AEServer:
aetools.packevent(_reply, {}) aetools.packevent(_reply, {})
else: else:
aetools.packevent(_reply, {'----':rv}) aetools.packevent(_reply, {'----':rv})
def code(x): def code(x):
"Convert a long int to the 4-character code it really is" "Convert a long int to the 4-character code it really is"
s = '' s = ''
@ -171,5 +184,6 @@ class _Test(AEServer, MiniApplication):
def other(self, _object=None, _class=None, _type=None, **args): def other(self, _object=None, _class=None, _type=None, **args):
print 'AppleEvent', (_class, _type), 'for', _object, 'Other args:', args print 'AppleEvent', (_class, _type), 'for', _object, 'Other args:', args
if __name__ == '__main__': if __name__ == '__main__':
_Test() _Test()