2000-03-27 16:22:53 +00:00
|
|
|
"""MiniAEFrame - A minimal AppleEvent Application framework.
|
|
|
|
|
|
|
|
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.
|
1996-09-09 01:46:11 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
import MacOS
|
|
|
|
import AE
|
|
|
|
from AppleEvents import *
|
|
|
|
import Evt
|
|
|
|
from Events import *
|
|
|
|
import Menu
|
|
|
|
import Win
|
|
|
|
from Windows import *
|
|
|
|
import Qd
|
|
|
|
|
|
|
|
import aetools
|
|
|
|
import EasyDialogs
|
|
|
|
|
|
|
|
kHighLevelEvent = 23 # Not defined anywhere for Python yet?
|
|
|
|
|
2000-03-27 16:22:53 +00:00
|
|
|
|
1996-09-09 01:46:11 +00:00
|
|
|
class MiniApplication:
|
2000-03-27 16:22:53 +00:00
|
|
|
|
1996-09-09 01:46:11 +00:00
|
|
|
"""A minimal FrameWork.Application-like class"""
|
2000-03-27 16:22:53 +00:00
|
|
|
|
1996-09-09 01:46:11 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.quitting = 0
|
|
|
|
# Initialize menu
|
|
|
|
self.appleid = 1
|
|
|
|
self.quitid = 2
|
|
|
|
Menu.ClearMenuBar()
|
|
|
|
self.applemenu = applemenu = Menu.NewMenu(self.appleid, "\024")
|
2000-03-28 13:57:34 +00:00
|
|
|
applemenu.AppendMenu("%s;(-" % self.getaboutmenutext())
|
2001-01-29 15:32:00 +00:00
|
|
|
if MacOS.runtimemodel == 'ppc':
|
|
|
|
applemenu.AppendResMenu('DRVR')
|
1996-09-09 01:46:11 +00:00
|
|
|
applemenu.InsertMenu(0)
|
|
|
|
self.quitmenu = Menu.NewMenu(self.quitid, "File")
|
|
|
|
self.quitmenu.AppendMenu("Quit")
|
2000-03-27 16:22:53 +00:00
|
|
|
self.quitmenu.SetItemCmd(1, ord("Q"))
|
1996-09-09 01:46:11 +00:00
|
|
|
self.quitmenu.InsertMenu(0)
|
|
|
|
Menu.DrawMenuBar()
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
self.close()
|
2000-03-27 16:22:53 +00:00
|
|
|
|
1996-09-09 01:46:11 +00:00
|
|
|
def close(self):
|
|
|
|
pass
|
2000-03-27 16:22:53 +00:00
|
|
|
|
1996-09-09 01:46:11 +00:00
|
|
|
def mainloop(self, mask = everyEvent, timeout = 60*60):
|
|
|
|
while not self.quitting:
|
|
|
|
self.dooneevent(mask, timeout)
|
2000-03-27 16:22:53 +00:00
|
|
|
|
1996-09-17 12:36:35 +00:00
|
|
|
def _quit(self):
|
|
|
|
self.quitting = 1
|
1996-09-09 01:46:11 +00:00
|
|
|
|
|
|
|
def dooneevent(self, mask = everyEvent, timeout = 60*60):
|
|
|
|
got, event = Evt.WaitNextEvent(mask, timeout)
|
|
|
|
if got:
|
|
|
|
self.lowlevelhandler(event)
|
|
|
|
|
|
|
|
def lowlevelhandler(self, event):
|
|
|
|
what, message, when, where, modifiers = event
|
|
|
|
h, v = where
|
|
|
|
if what == kHighLevelEvent:
|
|
|
|
msg = "High Level Event: %s %s" % \
|
|
|
|
(`code(message)`, `code(h | (v<<16))`)
|
|
|
|
try:
|
|
|
|
AE.AEProcessAppleEvent(event)
|
|
|
|
except AE.Error, err:
|
|
|
|
print 'AE error: ', err
|
|
|
|
print 'in', msg
|
|
|
|
traceback.print_exc()
|
|
|
|
return
|
|
|
|
elif what == keyDown:
|
|
|
|
c = chr(message & charCodeMask)
|
2000-03-27 16:22:53 +00:00
|
|
|
if modifiers & cmdKey:
|
|
|
|
if c == '.':
|
|
|
|
raise KeyboardInterrupt, "Command-period"
|
|
|
|
if c == 'q':
|
2000-10-19 20:32:35 +00:00
|
|
|
MacOS.OutputSeen()
|
2000-03-27 16:22:53 +00:00
|
|
|
self.quitting = 1
|
2000-03-27 17:13:32 +00:00
|
|
|
return
|
1996-09-09 01:46:11 +00:00
|
|
|
elif what == mouseDown:
|
|
|
|
partcode, window = Win.FindWindow(where)
|
|
|
|
if partcode == inMenuBar:
|
|
|
|
result = Menu.MenuSelect(where)
|
|
|
|
id = (result>>16) & 0xffff # Hi word
|
|
|
|
item = result & 0xffff # Lo word
|
|
|
|
if id == self.appleid:
|
|
|
|
if item == 1:
|
2000-03-27 16:22:53 +00:00
|
|
|
EasyDialogs.Message(self.getabouttext())
|
2001-01-29 15:32:00 +00:00
|
|
|
elif item > 1 and hasattr(Menu, 'OpenDeskAcc'):
|
2000-03-27 16:22:53 +00:00
|
|
|
name = self.applemenu.GetMenuItemText(item)
|
|
|
|
Menu.OpenDeskAcc(name)
|
|
|
|
elif id == self.quitid and item == 1:
|
2000-10-19 20:32:35 +00:00
|
|
|
MacOS.OutputSeen()
|
1996-09-09 01:46:11 +00:00
|
|
|
self.quitting = 1
|
2000-03-27 16:22:53 +00:00
|
|
|
Menu.HiliteMenu(0)
|
2000-03-27 17:13:32 +00:00
|
|
|
return
|
|
|
|
# Anything not handled is passed to Python/SIOUX
|
|
|
|
MacOS.HandleEvent(event)
|
2000-03-27 16:22:53 +00:00
|
|
|
|
|
|
|
def getabouttext(self):
|
|
|
|
return self.__class__.__name__
|
2000-03-28 13:57:34 +00:00
|
|
|
|
|
|
|
def getaboutmenutext(self):
|
|
|
|
return "About %s\311" % self.__class__.__name__
|
2000-03-27 16:22:53 +00:00
|
|
|
|
|
|
|
|
1996-09-09 01:46:11 +00:00
|
|
|
class AEServer:
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.ae_handlers = {}
|
2000-03-27 16:22:53 +00:00
|
|
|
|
1996-09-09 01:46:11 +00:00
|
|
|
def installaehandler(self, classe, type, callback):
|
|
|
|
AE.AEInstallEventHandler(classe, type, self.callback_wrapper)
|
|
|
|
self.ae_handlers[(classe, type)] = callback
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
for classe, type in self.ae_handlers.keys():
|
|
|
|
AE.AERemoveEventHandler(classe, type)
|
2000-03-27 16:22:53 +00:00
|
|
|
|
1996-09-09 01:46:11 +00:00
|
|
|
def callback_wrapper(self, _request, _reply):
|
|
|
|
_parameters, _attributes = aetools.unpackevent(_request)
|
|
|
|
_class = _attributes['evcl'].type
|
|
|
|
_type = _attributes['evid'].type
|
|
|
|
|
|
|
|
if self.ae_handlers.has_key((_class, _type)):
|
|
|
|
_function = self.ae_handlers[(_class, _type)]
|
|
|
|
elif self.ae_handlers.has_key((_class, '****')):
|
|
|
|
_function = self.ae_handlers[(_class, '****')]
|
|
|
|
elif self.ae_handlers.has_key(('****', '****')):
|
|
|
|
_function = self.ae_handlers[('****', '****')]
|
|
|
|
else:
|
|
|
|
raise 'Cannot happen: AE callback without handler', (_class, _type)
|
|
|
|
|
|
|
|
# XXXX Do key-to-name mapping here
|
|
|
|
|
|
|
|
_parameters['_attributes'] = _attributes
|
|
|
|
_parameters['_class'] = _class
|
|
|
|
_parameters['_type'] = _type
|
|
|
|
if _parameters.has_key('----'):
|
|
|
|
_object = _parameters['----']
|
|
|
|
del _parameters['----']
|
2000-10-19 20:49:12 +00:00
|
|
|
# The try/except that used to be here can mask programmer errors.
|
|
|
|
# Let the program crash, the programmer can always add a **args
|
|
|
|
# to the formal parameter list.
|
|
|
|
rv = apply(_function, (_object,), _parameters)
|
1996-09-09 01:46:11 +00:00
|
|
|
else:
|
2000-10-19 20:49:12 +00:00
|
|
|
#Same try/except comment as above
|
|
|
|
rv = apply(_function, (), _parameters)
|
1996-09-09 01:46:11 +00:00
|
|
|
|
|
|
|
if rv == None:
|
|
|
|
aetools.packevent(_reply, {})
|
|
|
|
else:
|
|
|
|
aetools.packevent(_reply, {'----':rv})
|
2000-03-27 16:22:53 +00:00
|
|
|
|
|
|
|
|
1996-09-09 01:46:11 +00:00
|
|
|
def code(x):
|
|
|
|
"Convert a long int to the 4-character code it really is"
|
|
|
|
s = ''
|
|
|
|
for i in range(4):
|
|
|
|
x, c = divmod(x, 256)
|
|
|
|
s = chr(c) + s
|
|
|
|
return s
|
|
|
|
|
|
|
|
class _Test(AEServer, MiniApplication):
|
|
|
|
"""Mini test application, handles required events"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
MiniApplication.__init__(self)
|
|
|
|
AEServer.__init__(self)
|
|
|
|
self.installaehandler('aevt', 'oapp', self.open_app)
|
|
|
|
self.installaehandler('aevt', 'quit', self.quit)
|
2000-09-24 22:00:11 +00:00
|
|
|
self.installaehandler('****', '****', self.other)
|
1996-09-09 01:46:11 +00:00
|
|
|
self.mainloop()
|
|
|
|
|
|
|
|
def quit(self, **args):
|
1996-09-17 12:36:35 +00:00
|
|
|
self._quit()
|
1996-09-09 01:46:11 +00:00
|
|
|
|
|
|
|
def open_app(self, **args):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def other(self, _object=None, _class=None, _type=None, **args):
|
|
|
|
print 'AppleEvent', (_class, _type), 'for', _object, 'Other args:', args
|
|
|
|
|
2000-03-27 16:22:53 +00:00
|
|
|
|
1996-09-09 01:46:11 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
_Test()
|