blob: 98247cbf3495ed01e87703d14951b2d250c1c13c [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
9import sys
10import traceback
11import MacOS
Jack Jansen5a6fdcd2001-08-25 12:15:04 +000012from Carbon import AE
13from Carbon.AppleEvents import *
14from Carbon import Evt
15from Carbon.Events import *
16from Carbon import Menu
17from Carbon import Win
18from Carbon.Windows import *
19from Carbon import Qd
Jack Jansend0fc42f2001-08-19 22:05:06 +000020
21import aetools
22import EasyDialogs
23
Just van Rossum35b50e22003-06-21 14:41:32 +000024kHighLevelEvent = 23 # Not defined anywhere for Python yet?
Jack Jansend0fc42f2001-08-19 22:05:06 +000025
26
27class MiniApplication:
Raymond Hettingerff41c482003-04-06 09:01:11 +000028
Just van Rossum35b50e22003-06-21 14:41:32 +000029 """A minimal FrameWork.Application-like class"""
Raymond Hettingerff41c482003-04-06 09:01:11 +000030
Just van Rossum35b50e22003-06-21 14:41:32 +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")
38 applemenu.AppendMenu("%s;(-" % self.getaboutmenutext())
39 if MacOS.runtimemodel == 'ppc':
40 applemenu.AppendResMenu('DRVR')
41 applemenu.InsertMenu(0)
42 self.quitmenu = Menu.NewMenu(self.quitid, "File")
43 self.quitmenu.AppendMenu("Quit")
44 self.quitmenu.SetItemCmd(1, ord("Q"))
45 self.quitmenu.InsertMenu(0)
46 Menu.DrawMenuBar()
Raymond Hettingerff41c482003-04-06 09:01:11 +000047
Just van Rossum35b50e22003-06-21 14:41:32 +000048 def __del__(self):
49 self.close()
Raymond Hettingerff41c482003-04-06 09:01:11 +000050
Just van Rossum35b50e22003-06-21 14:41:32 +000051 def close(self):
52 pass
Raymond Hettingerff41c482003-04-06 09:01:11 +000053
Just van Rossum35b50e22003-06-21 14:41:32 +000054 def mainloop(self, mask = everyEvent, timeout = 60*60):
55 while not self.quitting:
56 self.dooneevent(mask, timeout)
Raymond Hettingerff41c482003-04-06 09:01:11 +000057
Just van Rossum35b50e22003-06-21 14:41:32 +000058 def _quit(self):
59 self.quitting = 1
Raymond Hettingerff41c482003-04-06 09:01:11 +000060
Just van Rossum35b50e22003-06-21 14:41:32 +000061 def dooneevent(self, mask = everyEvent, timeout = 60*60):
Tim Peters182b5ac2004-07-18 06:16:08 +000062 got, event = Evt.WaitNextEvent(mask, timeout)
63 if got:
64 self.lowlevelhandler(event)
Raymond Hettingerff41c482003-04-06 09:01:11 +000065
Just van Rossum35b50e22003-06-21 14:41:32 +000066 def lowlevelhandler(self, event):
67 what, message, when, where, modifiers = event
68 h, v = where
69 if what == kHighLevelEvent:
Walter Dörwald70a6b492004-02-12 17:35:32 +000070 msg = "High Level Event: %r %r" % (code(message), code(h | (v<<16)))
Just van Rossum35b50e22003-06-21 14:41:32 +000071 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)
80 if modifiers & cmdKey:
81 if c == '.':
82 raise KeyboardInterrupt, "Command-period"
83 if c == 'q':
84 if hasattr(MacOS, 'OutputSeen'):
85 MacOS.OutputSeen()
86 self.quitting = 1
87 return
88 elif what == mouseDown:
89 partcode, window = Win.FindWindow(where)
90 if partcode == inMenuBar:
91 result = Menu.MenuSelect(where)
92 id = (result>>16) & 0xffff # Hi word
93 item = result & 0xffff # Lo word
94 if id == self.appleid:
95 if item == 1:
96 EasyDialogs.Message(self.getabouttext())
97 elif item > 1 and hasattr(Menu, 'OpenDeskAcc'):
98 name = self.applemenu.GetMenuItemText(item)
99 Menu.OpenDeskAcc(name)
100 elif id == self.quitid and item == 1:
101 if hasattr(MacOS, 'OutputSeen'):
102 MacOS.OutputSeen()
103 self.quitting = 1
104 Menu.HiliteMenu(0)
105 return
106 # Anything not handled is passed to Python/SIOUX
107 if hasattr(MacOS, 'HandleEvent'):
108 MacOS.HandleEvent(event)
109 else:
110 print "Unhandled event:", event
Raymond Hettingerff41c482003-04-06 09:01:11 +0000111
Just van Rossum35b50e22003-06-21 14:41:32 +0000112 def getabouttext(self):
113 return self.__class__.__name__
Raymond Hettingerff41c482003-04-06 09:01:11 +0000114
Just van Rossum35b50e22003-06-21 14:41:32 +0000115 def getaboutmenutext(self):
116 return "About %s\311" % self.__class__.__name__
Jack Jansend0fc42f2001-08-19 22:05:06 +0000117
118
119class AEServer:
Raymond Hettingerff41c482003-04-06 09:01:11 +0000120
Just van Rossum35b50e22003-06-21 14:41:32 +0000121 def __init__(self):
122 self.ae_handlers = {}
Raymond Hettingerff41c482003-04-06 09:01:11 +0000123
Just van Rossum35b50e22003-06-21 14:41:32 +0000124 def installaehandler(self, classe, type, callback):
125 AE.AEInstallEventHandler(classe, type, self.callback_wrapper)
126 self.ae_handlers[(classe, type)] = callback
Raymond Hettingerff41c482003-04-06 09:01:11 +0000127
Just van Rossum35b50e22003-06-21 14:41:32 +0000128 def close(self):
129 for classe, type in self.ae_handlers.keys():
130 AE.AERemoveEventHandler(classe, type)
Raymond Hettingerff41c482003-04-06 09:01:11 +0000131
Just van Rossum35b50e22003-06-21 14:41:32 +0000132 def callback_wrapper(self, _request, _reply):
133 _parameters, _attributes = aetools.unpackevent(_request)
134 _class = _attributes['evcl'].type
135 _type = _attributes['evid'].type
Raymond Hettingerff41c482003-04-06 09:01:11 +0000136
Just van Rossum35b50e22003-06-21 14:41:32 +0000137 if self.ae_handlers.has_key((_class, _type)):
138 _function = self.ae_handlers[(_class, _type)]
139 elif self.ae_handlers.has_key((_class, '****')):
140 _function = self.ae_handlers[(_class, '****')]
141 elif self.ae_handlers.has_key(('****', '****')):
142 _function = self.ae_handlers[('****', '****')]
143 else:
144 raise 'Cannot happen: AE callback without handler', (_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
151 if _parameters.has_key('----'):
152 _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):
194 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()