blob: 8d758c33448f6b8133400595e7b43d7bdf5800e7 [file] [log] [blame]
Jack Jansend0fc42f2001-08-19 22:05:06 +00001"""MiniAEFrame - A minimal AppleEvent Application framework.
2
3There are two classes:
Just van Rossum35b50e22003-06-21 14:41:32 +00004 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 Jansend0fc42f2001-08-19 22:05:06 +00007"""
8
Benjamin Peterson23681932008-05-12 21:42:13 +00009from warnings import warnpy3k
Benjamin Petersona6864e02008-07-14 17:42:17 +000010warnpy3k("In 3.x, the MiniAEFrame module is removed.", stacklevel=2)
Benjamin Peterson23681932008-05-12 21:42:13 +000011
Jack Jansend0fc42f2001-08-19 22:05:06 +000012import traceback
13import MacOS
Jack Jansen5a6fdcd2001-08-25 12:15:04 +000014from Carbon import AE
15from Carbon.AppleEvents import *
16from Carbon import Evt
17from Carbon.Events import *
18from Carbon import Menu
19from Carbon import Win
20from Carbon.Windows import *
21from Carbon import Qd
Jack Jansend0fc42f2001-08-19 22:05:06 +000022
23import aetools
24import EasyDialogs
25
Just van Rossum35b50e22003-06-21 14:41:32 +000026kHighLevelEvent = 23 # Not defined anywhere for Python yet?
Jack Jansend0fc42f2001-08-19 22:05:06 +000027
28
29class MiniApplication:
Raymond Hettingerff41c482003-04-06 09:01:11 +000030
Just van Rossum35b50e22003-06-21 14:41:32 +000031 """A minimal FrameWork.Application-like class"""
Raymond Hettingerff41c482003-04-06 09:01:11 +000032
Just van Rossum35b50e22003-06-21 14:41:32 +000033 def __init__(self):
34 self.quitting = 0
35 # Initialize menu
36 self.appleid = 1
37 self.quitid = 2
38 Menu.ClearMenuBar()
39 self.applemenu = applemenu = Menu.NewMenu(self.appleid, "\024")
40 applemenu.AppendMenu("%s;(-" % self.getaboutmenutext())
41 if MacOS.runtimemodel == 'ppc':
42 applemenu.AppendResMenu('DRVR')
43 applemenu.InsertMenu(0)
44 self.quitmenu = Menu.NewMenu(self.quitid, "File")
45 self.quitmenu.AppendMenu("Quit")
46 self.quitmenu.SetItemCmd(1, ord("Q"))
47 self.quitmenu.InsertMenu(0)
48 Menu.DrawMenuBar()
Raymond Hettingerff41c482003-04-06 09:01:11 +000049
Just van Rossum35b50e22003-06-21 14:41:32 +000050 def __del__(self):
51 self.close()
Raymond Hettingerff41c482003-04-06 09:01:11 +000052
Just van Rossum35b50e22003-06-21 14:41:32 +000053 def close(self):
54 pass
Raymond Hettingerff41c482003-04-06 09:01:11 +000055
Just van Rossum35b50e22003-06-21 14:41:32 +000056 def mainloop(self, mask = everyEvent, timeout = 60*60):
57 while not self.quitting:
58 self.dooneevent(mask, timeout)
Raymond Hettingerff41c482003-04-06 09:01:11 +000059
Just van Rossum35b50e22003-06-21 14:41:32 +000060 def _quit(self):
61 self.quitting = 1
Raymond Hettingerff41c482003-04-06 09:01:11 +000062
Just van Rossum35b50e22003-06-21 14:41:32 +000063 def dooneevent(self, mask = everyEvent, timeout = 60*60):
Tim Peters182b5ac2004-07-18 06:16:08 +000064 got, event = Evt.WaitNextEvent(mask, timeout)
65 if got:
66 self.lowlevelhandler(event)
Raymond Hettingerff41c482003-04-06 09:01:11 +000067
Just van Rossum35b50e22003-06-21 14:41:32 +000068 def lowlevelhandler(self, event):
69 what, message, when, where, modifiers = event
70 h, v = where
71 if what == kHighLevelEvent:
Walter Dörwald70a6b492004-02-12 17:35:32 +000072 msg = "High Level Event: %r %r" % (code(message), code(h | (v<<16)))
Just van Rossum35b50e22003-06-21 14:41:32 +000073 try:
74 AE.AEProcessAppleEvent(event)
75 except AE.Error, err:
76 print 'AE error: ', err
77 print 'in', msg
78 traceback.print_exc()
79 return
80 elif what == keyDown:
81 c = chr(message & charCodeMask)
82 if modifiers & cmdKey:
83 if c == '.':
84 raise KeyboardInterrupt, "Command-period"
85 if c == 'q':
86 if hasattr(MacOS, 'OutputSeen'):
87 MacOS.OutputSeen()
88 self.quitting = 1
89 return
90 elif what == mouseDown:
91 partcode, window = Win.FindWindow(where)
92 if partcode == inMenuBar:
93 result = Menu.MenuSelect(where)
94 id = (result>>16) & 0xffff # Hi word
95 item = result & 0xffff # Lo word
96 if id == self.appleid:
97 if item == 1:
98 EasyDialogs.Message(self.getabouttext())
99 elif item > 1 and hasattr(Menu, 'OpenDeskAcc'):
100 name = self.applemenu.GetMenuItemText(item)
101 Menu.OpenDeskAcc(name)
102 elif id == self.quitid and item == 1:
103 if hasattr(MacOS, 'OutputSeen'):
104 MacOS.OutputSeen()
105 self.quitting = 1
106 Menu.HiliteMenu(0)
107 return
108 # Anything not handled is passed to Python/SIOUX
109 if hasattr(MacOS, 'HandleEvent'):
110 MacOS.HandleEvent(event)
111 else:
112 print "Unhandled event:", event
Raymond Hettingerff41c482003-04-06 09:01:11 +0000113
Just van Rossum35b50e22003-06-21 14:41:32 +0000114 def getabouttext(self):
115 return self.__class__.__name__
Raymond Hettingerff41c482003-04-06 09:01:11 +0000116
Just van Rossum35b50e22003-06-21 14:41:32 +0000117 def getaboutmenutext(self):
118 return "About %s\311" % self.__class__.__name__
Jack Jansend0fc42f2001-08-19 22:05:06 +0000119
120
121class AEServer:
Raymond Hettingerff41c482003-04-06 09:01:11 +0000122
Just van Rossum35b50e22003-06-21 14:41:32 +0000123 def __init__(self):
124 self.ae_handlers = {}
Raymond Hettingerff41c482003-04-06 09:01:11 +0000125
Just van Rossum35b50e22003-06-21 14:41:32 +0000126 def installaehandler(self, classe, type, callback):
127 AE.AEInstallEventHandler(classe, type, self.callback_wrapper)
128 self.ae_handlers[(classe, type)] = callback
Raymond Hettingerff41c482003-04-06 09:01:11 +0000129
Just van Rossum35b50e22003-06-21 14:41:32 +0000130 def close(self):
131 for classe, type in self.ae_handlers.keys():
132 AE.AERemoveEventHandler(classe, type)
Raymond Hettingerff41c482003-04-06 09:01:11 +0000133
Just van Rossum35b50e22003-06-21 14:41:32 +0000134 def callback_wrapper(self, _request, _reply):
135 _parameters, _attributes = aetools.unpackevent(_request)
136 _class = _attributes['evcl'].type
137 _type = _attributes['evid'].type
Raymond Hettingerff41c482003-04-06 09:01:11 +0000138
Just van Rossum35b50e22003-06-21 14:41:32 +0000139 if self.ae_handlers.has_key((_class, _type)):
140 _function = self.ae_handlers[(_class, _type)]
141 elif self.ae_handlers.has_key((_class, '****')):
142 _function = self.ae_handlers[(_class, '****')]
143 elif self.ae_handlers.has_key(('****', '****')):
144 _function = self.ae_handlers[('****', '****')]
145 else:
146 raise 'Cannot happen: AE callback without handler', (_class, _type)
Raymond Hettingerff41c482003-04-06 09:01:11 +0000147
Just van Rossum35b50e22003-06-21 14:41:32 +0000148 # XXXX Do key-to-name mapping here
Raymond Hettingerff41c482003-04-06 09:01:11 +0000149
Just van Rossum35b50e22003-06-21 14:41:32 +0000150 _parameters['_attributes'] = _attributes
151 _parameters['_class'] = _class
152 _parameters['_type'] = _type
153 if _parameters.has_key('----'):
154 _object = _parameters['----']
155 del _parameters['----']
156 # The try/except that used to be here can mask programmer errors.
157 # Let the program crash, the programmer can always add a **args
158 # to the formal parameter list.
159 rv = _function(_object, **_parameters)
160 else:
161 #Same try/except comment as above
162 rv = _function(**_parameters)
Raymond Hettingerff41c482003-04-06 09:01:11 +0000163
Benjamin Peterson5b63acd2008-03-29 15:24:25 +0000164 if rv is None:
Just van Rossum35b50e22003-06-21 14:41:32 +0000165 aetools.packevent(_reply, {})
166 else:
167 aetools.packevent(_reply, {'----':rv})
Jack Jansend0fc42f2001-08-19 22:05:06 +0000168
169
170def code(x):
Just van Rossum35b50e22003-06-21 14:41:32 +0000171 "Convert a long int to the 4-character code it really is"
172 s = ''
173 for i in range(4):
174 x, c = divmod(x, 256)
175 s = chr(c) + s
176 return s
Jack Jansend0fc42f2001-08-19 22:05:06 +0000177
178class _Test(AEServer, MiniApplication):
Just van Rossum35b50e22003-06-21 14:41:32 +0000179 """Mini test application, handles required events"""
Jack Jansend0fc42f2001-08-19 22:05:06 +0000180
Just van Rossum35b50e22003-06-21 14:41:32 +0000181 def __init__(self):
182 MiniApplication.__init__(self)
183 AEServer.__init__(self)
184 self.installaehandler('aevt', 'oapp', self.open_app)
185 self.installaehandler('aevt', 'quit', self.quit)
186 self.installaehandler('****', '****', self.other)
187 self.mainloop()
Raymond Hettingerff41c482003-04-06 09:01:11 +0000188
Just van Rossum35b50e22003-06-21 14:41:32 +0000189 def quit(self, **args):
190 self._quit()
Raymond Hettingerff41c482003-04-06 09:01:11 +0000191
Just van Rossum35b50e22003-06-21 14:41:32 +0000192 def open_app(self, **args):
193 pass
Raymond Hettingerff41c482003-04-06 09:01:11 +0000194
Just van Rossum35b50e22003-06-21 14:41:32 +0000195 def other(self, _object=None, _class=None, _type=None, **args):
196 print 'AppleEvent', (_class, _type), 'for', _object, 'Other args:', args
Raymond Hettingerff41c482003-04-06 09:01:11 +0000197
Jack Jansend0fc42f2001-08-19 22:05:06 +0000198
199if __name__ == '__main__':
Just van Rossum35b50e22003-06-21 14:41:32 +0000200 _Test()