blob: fbb6f1b7b76ad4e9cb5811d87e1c7b04be90c9a7 [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 Jansen01a2d9e2001-01-29 15:32:00 +000039 if MacOS.runtimemodel == 'ppc':
40 applemenu.AppendResMenu('DRVR')
Jack Jansenf4c4f9e1996-09-09 01:46:11 +000041 applemenu.InsertMenu(0)
42 self.quitmenu = Menu.NewMenu(self.quitid, "File")
43 self.quitmenu.AppendMenu("Quit")
Just van Rossum7ec7c8a2000-03-27 16:22:53 +000044 self.quitmenu.SetItemCmd(1, ord("Q"))
Jack Jansenf4c4f9e1996-09-09 01:46:11 +000045 self.quitmenu.InsertMenu(0)
46 Menu.DrawMenuBar()
47
48 def __del__(self):
49 self.close()
Just van Rossum7ec7c8a2000-03-27 16:22:53 +000050
Jack Jansenf4c4f9e1996-09-09 01:46:11 +000051 def close(self):
52 pass
Just van Rossum7ec7c8a2000-03-27 16:22:53 +000053
Jack Jansenf4c4f9e1996-09-09 01:46:11 +000054 def mainloop(self, mask = everyEvent, timeout = 60*60):
55 while not self.quitting:
56 self.dooneevent(mask, timeout)
Just van Rossum7ec7c8a2000-03-27 16:22:53 +000057
Jack Jansen4cb94541996-09-17 12:36:35 +000058 def _quit(self):
59 self.quitting = 1
Jack Jansenf4c4f9e1996-09-09 01:46:11 +000060
61 def dooneevent(self, mask = everyEvent, timeout = 60*60):
62 got, event = Evt.WaitNextEvent(mask, timeout)
63 if got:
64 self.lowlevelhandler(event)
65
66 def lowlevelhandler(self, event):
67 what, message, when, where, modifiers = event
68 h, v = where
69 if what == kHighLevelEvent:
70 msg = "High Level Event: %s %s" % \
71 (`code(message)`, `code(h | (v<<16))`)
72 try:
73 AE.AEProcessAppleEvent(event)
74 except AE.Error, err:
75 print 'AE error: ', err
76 print 'in', msg
77 traceback.print_exc()
78 return
79 elif what == keyDown:
80 c = chr(message & charCodeMask)
Just van Rossum7ec7c8a2000-03-27 16:22:53 +000081 if modifiers & cmdKey:
82 if c == '.':
83 raise KeyboardInterrupt, "Command-period"
84 if c == 'q':
Jack Jansen74902362000-10-19 20:32:35 +000085 MacOS.OutputSeen()
Just van Rossum7ec7c8a2000-03-27 16:22:53 +000086 self.quitting = 1
Just van Rossum0c3baaf2000-03-27 17:13:32 +000087 return
Jack Jansenf4c4f9e1996-09-09 01:46:11 +000088 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:
Just van Rossum7ec7c8a2000-03-27 16:22:53 +000096 EasyDialogs.Message(self.getabouttext())
Jack Jansen01a2d9e2001-01-29 15:32:00 +000097 elif item > 1 and hasattr(Menu, 'OpenDeskAcc'):
Just van Rossum7ec7c8a2000-03-27 16:22:53 +000098 name = self.applemenu.GetMenuItemText(item)
99 Menu.OpenDeskAcc(name)
100 elif id == self.quitid and item == 1:
Jack Jansen74902362000-10-19 20:32:35 +0000101 MacOS.OutputSeen()
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000102 self.quitting = 1
Just van Rossum7ec7c8a2000-03-27 16:22:53 +0000103 Menu.HiliteMenu(0)
Just van Rossum0c3baaf2000-03-27 17:13:32 +0000104 return
105 # Anything not handled is passed to Python/SIOUX
106 MacOS.HandleEvent(event)
Just van Rossum7ec7c8a2000-03-27 16:22:53 +0000107
108 def getabouttext(self):
109 return self.__class__.__name__
Just van Rossumf5187272000-03-28 13:57:34 +0000110
111 def getaboutmenutext(self):
112 return "About %s\311" % self.__class__.__name__
Just van Rossum7ec7c8a2000-03-27 16:22:53 +0000113
114
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000115class AEServer:
116
117 def __init__(self):
118 self.ae_handlers = {}
Just van Rossum7ec7c8a2000-03-27 16:22:53 +0000119
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000120 def installaehandler(self, classe, type, callback):
121 AE.AEInstallEventHandler(classe, type, self.callback_wrapper)
122 self.ae_handlers[(classe, type)] = callback
123
124 def close(self):
125 for classe, type in self.ae_handlers.keys():
126 AE.AERemoveEventHandler(classe, type)
Just van Rossum7ec7c8a2000-03-27 16:22:53 +0000127
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000128 def callback_wrapper(self, _request, _reply):
129 _parameters, _attributes = aetools.unpackevent(_request)
130 _class = _attributes['evcl'].type
131 _type = _attributes['evid'].type
132
133 if self.ae_handlers.has_key((_class, _type)):
134 _function = self.ae_handlers[(_class, _type)]
135 elif self.ae_handlers.has_key((_class, '****')):
136 _function = self.ae_handlers[(_class, '****')]
137 elif self.ae_handlers.has_key(('****', '****')):
138 _function = self.ae_handlers[('****', '****')]
139 else:
140 raise 'Cannot happen: AE callback without handler', (_class, _type)
141
142 # XXXX Do key-to-name mapping here
143
144 _parameters['_attributes'] = _attributes
145 _parameters['_class'] = _class
146 _parameters['_type'] = _type
147 if _parameters.has_key('----'):
148 _object = _parameters['----']
149 del _parameters['----']
Jack Jansen62e38432000-10-19 20:49:12 +0000150 # The try/except that used to be here can mask programmer errors.
151 # Let the program crash, the programmer can always add a **args
152 # to the formal parameter list.
153 rv = apply(_function, (_object,), _parameters)
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000154 else:
Jack Jansen62e38432000-10-19 20:49:12 +0000155 #Same try/except comment as above
156 rv = apply(_function, (), _parameters)
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000157
158 if rv == None:
159 aetools.packevent(_reply, {})
160 else:
161 aetools.packevent(_reply, {'----':rv})
Just van Rossum7ec7c8a2000-03-27 16:22:53 +0000162
163
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000164def code(x):
165 "Convert a long int to the 4-character code it really is"
166 s = ''
167 for i in range(4):
168 x, c = divmod(x, 256)
169 s = chr(c) + s
170 return s
171
172class _Test(AEServer, MiniApplication):
173 """Mini test application, handles required events"""
174
175 def __init__(self):
176 MiniApplication.__init__(self)
177 AEServer.__init__(self)
178 self.installaehandler('aevt', 'oapp', self.open_app)
179 self.installaehandler('aevt', 'quit', self.quit)
Jack Jansen55a0bde2000-09-24 22:00:11 +0000180 self.installaehandler('****', '****', self.other)
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000181 self.mainloop()
182
183 def quit(self, **args):
Jack Jansen4cb94541996-09-17 12:36:35 +0000184 self._quit()
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000185
186 def open_app(self, **args):
187 pass
188
189 def other(self, _object=None, _class=None, _type=None, **args):
190 print 'AppleEvent', (_class, _type), 'for', _object, 'Other args:', args
191
Just van Rossum7ec7c8a2000-03-27 16:22:53 +0000192
Jack Jansenf4c4f9e1996-09-09 01:46:11 +0000193if __name__ == '__main__':
194 _Test()