blob: 506c5cb1a8a43f6d8d9a4733c35955fdbab403ae [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)
72 except AE.Error, err:
73 print 'AE error: ', err
74 print 'in', msg
75 traceback.print_exc()
76 return
77 elif what == keyDown:
78 c = chr(message & charCodeMask)
79 if modifiers & cmdKey:
80 if c == '.':
81 raise KeyboardInterrupt, "Command-period"
82 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:
109 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
Just van Rossum35b50e22003-06-21 14:41:32 +0000136 if self.ae_handlers.has_key((_class, _type)):
137 _function = self.ae_handlers[(_class, _type)]
138 elif self.ae_handlers.has_key((_class, '****')):
139 _function = self.ae_handlers[(_class, '****')]
140 elif self.ae_handlers.has_key(('****', '****')):
141 _function = self.ae_handlers[('****', '****')]
142 else:
143 raise 'Cannot happen: AE callback without handler', (_class, _type)
Raymond Hettingerff41c482003-04-06 09:01:11 +0000144
Just van Rossum35b50e22003-06-21 14:41:32 +0000145 # XXXX Do key-to-name mapping here
Raymond Hettingerff41c482003-04-06 09:01:11 +0000146
Just van Rossum35b50e22003-06-21 14:41:32 +0000147 _parameters['_attributes'] = _attributes
148 _parameters['_class'] = _class
149 _parameters['_type'] = _type
150 if _parameters.has_key('----'):
151 _object = _parameters['----']
152 del _parameters['----']
153 # The try/except that used to be here can mask programmer errors.
154 # Let the program crash, the programmer can always add a **args
155 # to the formal parameter list.
156 rv = _function(_object, **_parameters)
157 else:
158 #Same try/except comment as above
159 rv = _function(**_parameters)
Raymond Hettingerff41c482003-04-06 09:01:11 +0000160
Benjamin Peterson5b63acd2008-03-29 15:24:25 +0000161 if rv is None:
Just van Rossum35b50e22003-06-21 14:41:32 +0000162 aetools.packevent(_reply, {})
163 else:
164 aetools.packevent(_reply, {'----':rv})
Jack Jansend0fc42f2001-08-19 22:05:06 +0000165
166
167def code(x):
Just van Rossum35b50e22003-06-21 14:41:32 +0000168 "Convert a long int to the 4-character code it really is"
169 s = ''
170 for i in range(4):
171 x, c = divmod(x, 256)
172 s = chr(c) + s
173 return s
Jack Jansend0fc42f2001-08-19 22:05:06 +0000174
175class _Test(AEServer, MiniApplication):
Just van Rossum35b50e22003-06-21 14:41:32 +0000176 """Mini test application, handles required events"""
Jack Jansend0fc42f2001-08-19 22:05:06 +0000177
Just van Rossum35b50e22003-06-21 14:41:32 +0000178 def __init__(self):
179 MiniApplication.__init__(self)
180 AEServer.__init__(self)
181 self.installaehandler('aevt', 'oapp', self.open_app)
182 self.installaehandler('aevt', 'quit', self.quit)
183 self.installaehandler('****', '****', self.other)
184 self.mainloop()
Raymond Hettingerff41c482003-04-06 09:01:11 +0000185
Just van Rossum35b50e22003-06-21 14:41:32 +0000186 def quit(self, **args):
187 self._quit()
Raymond Hettingerff41c482003-04-06 09:01:11 +0000188
Just van Rossum35b50e22003-06-21 14:41:32 +0000189 def open_app(self, **args):
190 pass
Raymond Hettingerff41c482003-04-06 09:01:11 +0000191
Just van Rossum35b50e22003-06-21 14:41:32 +0000192 def other(self, _object=None, _class=None, _type=None, **args):
193 print 'AppleEvent', (_class, _type), 'for', _object, 'Other args:', args
Raymond Hettingerff41c482003-04-06 09:01:11 +0000194
Jack Jansend0fc42f2001-08-19 22:05:06 +0000195
196if __name__ == '__main__':
Just van Rossum35b50e22003-06-21 14:41:32 +0000197 _Test()