blob: 87726e9543daacbccfa78019ba1886096d1c5593 [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 Rossumf5187272000-03-28 13:57:34 +000038 applemenu.AppendMenu("%s;(-" % self.getaboutmenutext())
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':
Jack Jansen74902362000-10-19 20:32:35 +000084 MacOS.OutputSeen()
Just van Rossum7ec7c8a2000-03-27 16:22:53 +000085 self.quitting = 1
Just van Rossum0c3baaf2000-03-27 17:13:32 +000086 return
Jack Jansenf4c4f9e1996-09-09 01:46:11 +000087 elif what == mouseDown:
88 partcode, window = Win.FindWindow(where)
89 if partcode == inMenuBar:
90 result = Menu.MenuSelect(where)
91 id = (result>>16) & 0xffff # Hi word
92 item = result & 0xffff # Lo word
93 if id == self.appleid:
94 if item == 1:
Just van Rossum7ec7c8a2000-03-27 16:22:53 +000095 EasyDialogs.Message(self.getabouttext())
Jack Jansenf4c4f9e1996-09-09 01:46:11 +000096 elif item > 1:
Just van Rossum7ec7c8a2000-03-27 16:22:53 +000097 name = self.applemenu.GetMenuItemText(item)
98 Menu.OpenDeskAcc(name)
99 elif id == self.quitid and item == 1:
Jack Jansen74902362000-10-19 20:32:35 +0000100 MacOS.OutputSeen()
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000101 self.quitting = 1
Just van Rossum7ec7c8a2000-03-27 16:22:53 +0000102 Menu.HiliteMenu(0)
Just van Rossum0c3baaf2000-03-27 17:13:32 +0000103 return
104 # Anything not handled is passed to Python/SIOUX
105 MacOS.HandleEvent(event)
Just van Rossum7ec7c8a2000-03-27 16:22:53 +0000106
107 def getabouttext(self):
108 return self.__class__.__name__
Just van Rossumf5187272000-03-28 13:57:34 +0000109
110 def getaboutmenutext(self):
111 return "About %s\311" % self.__class__.__name__
Just van Rossum7ec7c8a2000-03-27 16:22:53 +0000112
113
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000114class AEServer:
115
116 def __init__(self):
117 self.ae_handlers = {}
Just van Rossum7ec7c8a2000-03-27 16:22:53 +0000118
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000119 def installaehandler(self, classe, type, callback):
120 AE.AEInstallEventHandler(classe, type, self.callback_wrapper)
121 self.ae_handlers[(classe, type)] = callback
122
123 def close(self):
124 for classe, type in self.ae_handlers.keys():
125 AE.AERemoveEventHandler(classe, type)
Just van Rossum7ec7c8a2000-03-27 16:22:53 +0000126
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000127 def callback_wrapper(self, _request, _reply):
128 _parameters, _attributes = aetools.unpackevent(_request)
129 _class = _attributes['evcl'].type
130 _type = _attributes['evid'].type
131
132 if self.ae_handlers.has_key((_class, _type)):
133 _function = self.ae_handlers[(_class, _type)]
134 elif self.ae_handlers.has_key((_class, '****')):
135 _function = self.ae_handlers[(_class, '****')]
136 elif self.ae_handlers.has_key(('****', '****')):
137 _function = self.ae_handlers[('****', '****')]
138 else:
139 raise 'Cannot happen: AE callback without handler', (_class, _type)
140
141 # XXXX Do key-to-name mapping here
142
143 _parameters['_attributes'] = _attributes
144 _parameters['_class'] = _class
145 _parameters['_type'] = _type
146 if _parameters.has_key('----'):
147 _object = _parameters['----']
148 del _parameters['----']
Jack Jansenb7e82c11996-10-23 15:43:04 +0000149 try:
150 rv = apply(_function, (_object,), _parameters)
151 except TypeError, name:
152 raise TypeError, ('AppleEvent handler misses formal keyword argument', _function, name)
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000153 else:
Jack Jansenb7e82c11996-10-23 15:43:04 +0000154 try:
155 rv = apply(_function, (), _parameters)
156 except TypeError, name:
157 raise TypeError, ('AppleEvent handler misses formal keyword argument', _function, name)
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000158
159 if rv == None:
160 aetools.packevent(_reply, {})
161 else:
162 aetools.packevent(_reply, {'----':rv})
Just van Rossum7ec7c8a2000-03-27 16:22:53 +0000163
164
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000165def code(x):
166 "Convert a long int to the 4-character code it really is"
167 s = ''
168 for i in range(4):
169 x, c = divmod(x, 256)
170 s = chr(c) + s
171 return s
172
173class _Test(AEServer, MiniApplication):
174 """Mini test application, handles required events"""
175
176 def __init__(self):
177 MiniApplication.__init__(self)
178 AEServer.__init__(self)
179 self.installaehandler('aevt', 'oapp', self.open_app)
180 self.installaehandler('aevt', 'quit', self.quit)
Jack Jansen55a0bde2000-09-24 22:00:11 +0000181 self.installaehandler('****', '****', self.other)
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000182 self.mainloop()
183
184 def quit(self, **args):
Jack Jansen4cb94541996-09-17 12:36:35 +0000185 self._quit()
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000186
187 def open_app(self, **args):
188 pass
189
190 def other(self, _object=None, _class=None, _type=None, **args):
191 print 'AppleEvent', (_class, _type), 'for', _object, 'Other args:', args
192
Just van Rossum7ec7c8a2000-03-27 16:22:53 +0000193
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000194if __name__ == '__main__':
195 _Test()