blob: e57d78e4b71109cc6fc2d0345011ff104066bb8b [file] [log] [blame]
Just van Rossum7ec7c8a2000-03-27 16:22:53 +00001"""MiniAEFrame - A minimal AppleEvent Application framework.
2
3There are two classes:
4 AEServer -- a mixin class offering nice AE handling.
5 MiniApplication -- a very minimal alternative to FrameWork.py,
6 only suitable for the simplest of AppleEvent servers.
Jack Jansenf4c4f9e1996-09-09 01:46:11 +00007"""
8
9import sys
10import traceback
11import MacOS
12import AE
13from AppleEvents import *
14import Evt
15from Events import *
16import Menu
17import Win
18from Windows import *
19import Qd
20
21import aetools
22import EasyDialogs
23
24kHighLevelEvent = 23 # Not defined anywhere for Python yet?
25
Jack Jansenf4c4f9e1996-09-09 01:46:11 +000026
Just van Rossum7ec7c8a2000-03-27 16:22:53 +000027class MiniApplication:
28
29 """A minimal FrameWork.Application-like class"""
30
Jack Jansenf4c4f9e1996-09-09 01:46:11 +000031 def __init__(self):
32 self.quitting = 0
33 # Initialize menu
34 self.appleid = 1
35 self.quitid = 2
36 Menu.ClearMenuBar()
37 self.applemenu = applemenu = Menu.NewMenu(self.appleid, "\024")
Just van Rossum7ec7c8a2000-03-27 16:22:53 +000038 applemenu.AppendMenu("About %s...;(-" % self.__class__.__name__)
Jack Jansenf4c4f9e1996-09-09 01:46:11 +000039 applemenu.AppendResMenu('DRVR')
40 applemenu.InsertMenu(0)
41 self.quitmenu = Menu.NewMenu(self.quitid, "File")
42 self.quitmenu.AppendMenu("Quit")
Just van Rossum7ec7c8a2000-03-27 16:22:53 +000043 self.quitmenu.SetItemCmd(1, ord("Q"))
Jack Jansenf4c4f9e1996-09-09 01:46:11 +000044 self.quitmenu.InsertMenu(0)
45 Menu.DrawMenuBar()
46
47 def __del__(self):
48 self.close()
Just van Rossum7ec7c8a2000-03-27 16:22:53 +000049
Jack Jansenf4c4f9e1996-09-09 01:46:11 +000050 def close(self):
51 pass
Just van Rossum7ec7c8a2000-03-27 16:22:53 +000052
Jack Jansenf4c4f9e1996-09-09 01:46:11 +000053 def mainloop(self, mask = everyEvent, timeout = 60*60):
54 while not self.quitting:
55 self.dooneevent(mask, timeout)
Just van Rossum7ec7c8a2000-03-27 16:22:53 +000056
Jack Jansen4cb94541996-09-17 12:36:35 +000057 def _quit(self):
58 self.quitting = 1
Jack Jansenf4c4f9e1996-09-09 01:46:11 +000059
60 def dooneevent(self, mask = everyEvent, timeout = 60*60):
61 got, event = Evt.WaitNextEvent(mask, timeout)
62 if got:
63 self.lowlevelhandler(event)
64
65 def lowlevelhandler(self, event):
66 what, message, when, where, modifiers = event
67 h, v = where
68 if what == kHighLevelEvent:
69 msg = "High Level Event: %s %s" % \
70 (`code(message)`, `code(h | (v<<16))`)
71 try:
72 AE.AEProcessAppleEvent(event)
73 except AE.Error, err:
74 print 'AE error: ', err
75 print 'in', msg
76 traceback.print_exc()
77 return
78 elif what == keyDown:
79 c = chr(message & charCodeMask)
Just van Rossum7ec7c8a2000-03-27 16:22:53 +000080 if modifiers & cmdKey:
81 if c == '.':
82 raise KeyboardInterrupt, "Command-period"
83 if c == 'q':
84 self.quitting = 1
Just van Rossum0c3baaf2000-03-27 17:13:32 +000085 return
Jack Jansenf4c4f9e1996-09-09 01:46:11 +000086 elif what == mouseDown:
87 partcode, window = Win.FindWindow(where)
88 if partcode == inMenuBar:
89 result = Menu.MenuSelect(where)
90 id = (result>>16) & 0xffff # Hi word
91 item = result & 0xffff # Lo word
92 if id == self.appleid:
93 if item == 1:
Just van Rossum7ec7c8a2000-03-27 16:22:53 +000094 EasyDialogs.Message(self.getabouttext())
Jack Jansenf4c4f9e1996-09-09 01:46:11 +000095 elif item > 1:
Just van Rossum7ec7c8a2000-03-27 16:22:53 +000096 name = self.applemenu.GetMenuItemText(item)
97 Menu.OpenDeskAcc(name)
98 elif id == self.quitid and item == 1:
Jack Jansenf4c4f9e1996-09-09 01:46:11 +000099 self.quitting = 1
Just van Rossum7ec7c8a2000-03-27 16:22:53 +0000100 Menu.HiliteMenu(0)
Just van Rossum0c3baaf2000-03-27 17:13:32 +0000101 return
102 # Anything not handled is passed to Python/SIOUX
103 MacOS.HandleEvent(event)
Just van Rossum7ec7c8a2000-03-27 16:22:53 +0000104
105 def getabouttext(self):
106 return self.__class__.__name__
107
108
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000109class AEServer:
110
111 def __init__(self):
112 self.ae_handlers = {}
Just van Rossum7ec7c8a2000-03-27 16:22:53 +0000113
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000114 def installaehandler(self, classe, type, callback):
115 AE.AEInstallEventHandler(classe, type, self.callback_wrapper)
116 self.ae_handlers[(classe, type)] = callback
117
118 def close(self):
119 for classe, type in self.ae_handlers.keys():
120 AE.AERemoveEventHandler(classe, type)
Just van Rossum7ec7c8a2000-03-27 16:22:53 +0000121
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000122 def callback_wrapper(self, _request, _reply):
123 _parameters, _attributes = aetools.unpackevent(_request)
124 _class = _attributes['evcl'].type
125 _type = _attributes['evid'].type
126
127 if self.ae_handlers.has_key((_class, _type)):
128 _function = self.ae_handlers[(_class, _type)]
129 elif self.ae_handlers.has_key((_class, '****')):
130 _function = self.ae_handlers[(_class, '****')]
131 elif self.ae_handlers.has_key(('****', '****')):
132 _function = self.ae_handlers[('****', '****')]
133 else:
134 raise 'Cannot happen: AE callback without handler', (_class, _type)
135
136 # XXXX Do key-to-name mapping here
137
138 _parameters['_attributes'] = _attributes
139 _parameters['_class'] = _class
140 _parameters['_type'] = _type
141 if _parameters.has_key('----'):
142 _object = _parameters['----']
143 del _parameters['----']
Jack Jansenb7e82c11996-10-23 15:43:04 +0000144 try:
145 rv = apply(_function, (_object,), _parameters)
146 except TypeError, name:
147 raise TypeError, ('AppleEvent handler misses formal keyword argument', _function, name)
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000148 else:
Jack Jansenb7e82c11996-10-23 15:43:04 +0000149 try:
150 rv = apply(_function, (), _parameters)
151 except TypeError, name:
152 raise TypeError, ('AppleEvent handler misses formal keyword argument', _function, name)
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000153
154 if rv == None:
155 aetools.packevent(_reply, {})
156 else:
157 aetools.packevent(_reply, {'----':rv})
Just van Rossum7ec7c8a2000-03-27 16:22:53 +0000158
159
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000160def code(x):
161 "Convert a long int to the 4-character code it really is"
162 s = ''
163 for i in range(4):
164 x, c = divmod(x, 256)
165 s = chr(c) + s
166 return s
167
168class _Test(AEServer, MiniApplication):
169 """Mini test application, handles required events"""
170
171 def __init__(self):
172 MiniApplication.__init__(self)
173 AEServer.__init__(self)
174 self.installaehandler('aevt', 'oapp', self.open_app)
175 self.installaehandler('aevt', 'quit', self.quit)
176 self.installaehandler('aevt', '****', self.other)
177 self.mainloop()
178
179 def quit(self, **args):
Jack Jansen4cb94541996-09-17 12:36:35 +0000180 self._quit()
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000181
182 def open_app(self, **args):
183 pass
184
185 def other(self, _object=None, _class=None, _type=None, **args):
186 print 'AppleEvent', (_class, _type), 'for', _object, 'Other args:', args
187
Just van Rossum7ec7c8a2000-03-27 16:22:53 +0000188
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000189if __name__ == '__main__':
190 _Test()