blob: e2f7cec3391b29204e887c771522c69b7011b78d [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
Jack Jansend0fc42f2001-08-19 22:05:06 +00009import traceback
10import MacOS
Jack Jansen5a6fdcd2001-08-25 12:15:04 +000011from Carbon import AE
12from Carbon.AppleEvents import *
13from Carbon import Evt
14from Carbon.Events import *
15from Carbon import Menu
16from Carbon import Win
17from Carbon.Windows import *
18from Carbon import Qd
Jack Jansend0fc42f2001-08-19 22:05:06 +000019
20import aetools
21import EasyDialogs
22
Just van Rossum35b50e22003-06-21 14:41:32 +000023kHighLevelEvent = 23 # Not defined anywhere for Python yet?
Jack Jansend0fc42f2001-08-19 22:05:06 +000024
25
26class MiniApplication:
Raymond Hettingerff41c482003-04-06 09:01:11 +000027
Just van Rossum35b50e22003-06-21 14:41:32 +000028 """A minimal FrameWork.Application-like class"""
Raymond Hettingerff41c482003-04-06 09:01:11 +000029
Just van Rossum35b50e22003-06-21 14:41:32 +000030 def __init__(self):
31 self.quitting = 0
32 # Initialize menu
33 self.appleid = 1
34 self.quitid = 2
35 Menu.ClearMenuBar()
36 self.applemenu = applemenu = Menu.NewMenu(self.appleid, "\024")
37 applemenu.AppendMenu("%s;(-" % self.getaboutmenutext())
38 if MacOS.runtimemodel == 'ppc':
39 applemenu.AppendResMenu('DRVR')
40 applemenu.InsertMenu(0)
41 self.quitmenu = Menu.NewMenu(self.quitid, "File")
42 self.quitmenu.AppendMenu("Quit")
43 self.quitmenu.SetItemCmd(1, ord("Q"))
44 self.quitmenu.InsertMenu(0)
45 Menu.DrawMenuBar()
Raymond Hettingerff41c482003-04-06 09:01:11 +000046
Just van Rossum35b50e22003-06-21 14:41:32 +000047 def __del__(self):
48 self.close()
Raymond Hettingerff41c482003-04-06 09:01:11 +000049
Just van Rossum35b50e22003-06-21 14:41:32 +000050 def close(self):
51 pass
Raymond Hettingerff41c482003-04-06 09:01:11 +000052
Just van Rossum35b50e22003-06-21 14:41:32 +000053 def mainloop(self, mask = everyEvent, timeout = 60*60):
54 while not self.quitting:
55 self.dooneevent(mask, timeout)
Raymond Hettingerff41c482003-04-06 09:01:11 +000056
Just van Rossum35b50e22003-06-21 14:41:32 +000057 def _quit(self):
58 self.quitting = 1
Raymond Hettingerff41c482003-04-06 09:01:11 +000059
Just van Rossum35b50e22003-06-21 14:41:32 +000060 def dooneevent(self, mask = everyEvent, timeout = 60*60):
Tim Peters182b5ac2004-07-18 06:16:08 +000061 got, event = Evt.WaitNextEvent(mask, timeout)
62 if got:
63 self.lowlevelhandler(event)
Raymond Hettingerff41c482003-04-06 09:01:11 +000064
Just van Rossum35b50e22003-06-21 14:41:32 +000065 def lowlevelhandler(self, event):
66 what, message, when, where, modifiers = event
67 h, v = where
68 if what == kHighLevelEvent:
Walter Dörwald70a6b492004-02-12 17:35:32 +000069 msg = "High Level Event: %r %r" % (code(message), code(h | (v<<16)))
Just van Rossum35b50e22003-06-21 14:41:32 +000070 try:
71 AE.AEProcessAppleEvent(event)
Guido van Rossumb940e112007-01-10 16:19:56 +000072 except AE.Error as err:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000073 print('AE error: ', err)
74 print('in', msg)
Just van Rossum35b50e22003-06-21 14:41:32 +000075 traceback.print_exc()
76 return
77 elif what == keyDown:
78 c = chr(message & charCodeMask)
79 if modifiers & cmdKey:
80 if c == '.':
Collin Wintere45be282007-08-23 00:01:55 +000081 raise KeyboardInterrupt("Command-period")
Just van Rossum35b50e22003-06-21 14:41:32 +000082 if c == 'q':
83 if hasattr(MacOS, 'OutputSeen'):
84 MacOS.OutputSeen()
85 self.quitting = 1
86 return
87 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:
95 EasyDialogs.Message(self.getabouttext())
96 elif item > 1 and hasattr(Menu, 'OpenDeskAcc'):
97 name = self.applemenu.GetMenuItemText(item)
98 Menu.OpenDeskAcc(name)
99 elif id == self.quitid and item == 1:
100 if hasattr(MacOS, 'OutputSeen'):
101 MacOS.OutputSeen()
102 self.quitting = 1
103 Menu.HiliteMenu(0)
104 return
105 # Anything not handled is passed to Python/SIOUX
106 if hasattr(MacOS, 'HandleEvent'):
107 MacOS.HandleEvent(event)
108 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000109 print("Unhandled event:", event)
Raymond Hettingerff41c482003-04-06 09:01:11 +0000110
Just van Rossum35b50e22003-06-21 14:41:32 +0000111 def getabouttext(self):
112 return self.__class__.__name__
Raymond Hettingerff41c482003-04-06 09:01:11 +0000113
Just van Rossum35b50e22003-06-21 14:41:32 +0000114 def getaboutmenutext(self):
115 return "About %s\311" % self.__class__.__name__
Jack Jansend0fc42f2001-08-19 22:05:06 +0000116
117
118class AEServer:
Raymond Hettingerff41c482003-04-06 09:01:11 +0000119
Just van Rossum35b50e22003-06-21 14:41:32 +0000120 def __init__(self):
121 self.ae_handlers = {}
Raymond Hettingerff41c482003-04-06 09:01:11 +0000122
Just van Rossum35b50e22003-06-21 14:41:32 +0000123 def installaehandler(self, classe, type, callback):
124 AE.AEInstallEventHandler(classe, type, self.callback_wrapper)
125 self.ae_handlers[(classe, type)] = callback
Raymond Hettingerff41c482003-04-06 09:01:11 +0000126
Just van Rossum35b50e22003-06-21 14:41:32 +0000127 def close(self):
128 for classe, type in self.ae_handlers.keys():
129 AE.AERemoveEventHandler(classe, type)
Raymond Hettingerff41c482003-04-06 09:01:11 +0000130
Just van Rossum35b50e22003-06-21 14:41:32 +0000131 def callback_wrapper(self, _request, _reply):
132 _parameters, _attributes = aetools.unpackevent(_request)
133 _class = _attributes['evcl'].type
134 _type = _attributes['evid'].type
Raymond Hettingerff41c482003-04-06 09:01:11 +0000135
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000136 if (_class, _type) in self.ae_handlers:
Just van Rossum35b50e22003-06-21 14:41:32 +0000137 _function = self.ae_handlers[(_class, _type)]
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000138 elif (_class, '****') in self.ae_handlers:
Just van Rossum35b50e22003-06-21 14:41:32 +0000139 _function = self.ae_handlers[(_class, '****')]
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000140 elif ('****', '****') in self.ae_handlers:
Just van Rossum35b50e22003-06-21 14:41:32 +0000141 _function = self.ae_handlers[('****', '****')]
142 else:
Collin Winter6cd2a202007-08-30 18:11:48 +0000143 raise RuntimeError('AE callback without handler: '
144 + str((_class, _type)))
Raymond Hettingerff41c482003-04-06 09:01:11 +0000145
Just van Rossum35b50e22003-06-21 14:41:32 +0000146 # XXXX Do key-to-name mapping here
Raymond Hettingerff41c482003-04-06 09:01:11 +0000147
Just van Rossum35b50e22003-06-21 14:41:32 +0000148 _parameters['_attributes'] = _attributes
149 _parameters['_class'] = _class
150 _parameters['_type'] = _type
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000151 if '----' in _parameters:
Just van Rossum35b50e22003-06-21 14:41:32 +0000152 _object = _parameters['----']
153 del _parameters['----']
154 # The try/except that used to be here can mask programmer errors.
155 # Let the program crash, the programmer can always add a **args
156 # to the formal parameter list.
157 rv = _function(_object, **_parameters)
158 else:
159 #Same try/except comment as above
160 rv = _function(**_parameters)
Raymond Hettingerff41c482003-04-06 09:01:11 +0000161
Just van Rossum35b50e22003-06-21 14:41:32 +0000162 if rv == None:
163 aetools.packevent(_reply, {})
164 else:
165 aetools.packevent(_reply, {'----':rv})
Jack Jansend0fc42f2001-08-19 22:05:06 +0000166
167
168def code(x):
Just van Rossum35b50e22003-06-21 14:41:32 +0000169 "Convert a long int to the 4-character code it really is"
170 s = ''
171 for i in range(4):
172 x, c = divmod(x, 256)
173 s = chr(c) + s
174 return s
Jack Jansend0fc42f2001-08-19 22:05:06 +0000175
176class _Test(AEServer, MiniApplication):
Just van Rossum35b50e22003-06-21 14:41:32 +0000177 """Mini test application, handles required events"""
Jack Jansend0fc42f2001-08-19 22:05:06 +0000178
Just van Rossum35b50e22003-06-21 14:41:32 +0000179 def __init__(self):
180 MiniApplication.__init__(self)
181 AEServer.__init__(self)
182 self.installaehandler('aevt', 'oapp', self.open_app)
183 self.installaehandler('aevt', 'quit', self.quit)
184 self.installaehandler('****', '****', self.other)
185 self.mainloop()
Raymond Hettingerff41c482003-04-06 09:01:11 +0000186
Just van Rossum35b50e22003-06-21 14:41:32 +0000187 def quit(self, **args):
188 self._quit()
Raymond Hettingerff41c482003-04-06 09:01:11 +0000189
Just van Rossum35b50e22003-06-21 14:41:32 +0000190 def open_app(self, **args):
191 pass
Raymond Hettingerff41c482003-04-06 09:01:11 +0000192
Just van Rossum35b50e22003-06-21 14:41:32 +0000193 def other(self, _object=None, _class=None, _type=None, **args):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000194 print('AppleEvent', (_class, _type), 'for', _object, 'Other args:', args)
Raymond Hettingerff41c482003-04-06 09:01:11 +0000195
Jack Jansend0fc42f2001-08-19 22:05:06 +0000196
197if __name__ == '__main__':
Just van Rossum35b50e22003-06-21 14:41:32 +0000198 _Test()