blob: 02b9723b3ce8dd8c2f1b99d850204f5bf0815d3b [file] [log] [blame]
Jack Jansenf4c4f9e1996-09-09 01:46:11 +00001"""MiniAEFrame - A first stab at an AE Application framework.
2This framework is still work-in-progress, so do not rely on it remaining
3unchanged.
4"""
5
6import sys
7import traceback
8import MacOS
9import AE
10from AppleEvents import *
11import Evt
12from Events import *
13import Menu
14import Win
15from Windows import *
16import Qd
17
18import aetools
19import EasyDialogs
20
21kHighLevelEvent = 23 # Not defined anywhere for Python yet?
22
23class MiniApplication:
24 """A minimal FrameWork.Application-like class"""
25
26 def __init__(self):
27 self.quitting = 0
28 # Initialize menu
29 self.appleid = 1
30 self.quitid = 2
31 Menu.ClearMenuBar()
32 self.applemenu = applemenu = Menu.NewMenu(self.appleid, "\024")
33 applemenu.AppendMenu("All about cgitest...;(-")
34 applemenu.AppendResMenu('DRVR')
35 applemenu.InsertMenu(0)
36 self.quitmenu = Menu.NewMenu(self.quitid, "File")
37 self.quitmenu.AppendMenu("Quit")
38 self.quitmenu.InsertMenu(0)
39 Menu.DrawMenuBar()
40
41 def __del__(self):
42 self.close()
43
44 def close(self):
45 pass
46
47 def mainloop(self, mask = everyEvent, timeout = 60*60):
48 while not self.quitting:
49 self.dooneevent(mask, timeout)
50
51 def dooneevent(self, mask = everyEvent, timeout = 60*60):
52 got, event = Evt.WaitNextEvent(mask, timeout)
53 if got:
54 self.lowlevelhandler(event)
55
56 def lowlevelhandler(self, event):
57 what, message, when, where, modifiers = event
58 h, v = where
59 if what == kHighLevelEvent:
60 msg = "High Level Event: %s %s" % \
61 (`code(message)`, `code(h | (v<<16))`)
62 try:
63 AE.AEProcessAppleEvent(event)
64 except AE.Error, err:
65 print 'AE error: ', err
66 print 'in', msg
67 traceback.print_exc()
68 return
69 elif what == keyDown:
70 c = chr(message & charCodeMask)
71 if c == '.' and modifiers & cmdKey:
72 raise KeyboardInterrupt, "Command-period"
73 elif what == mouseDown:
74 partcode, window = Win.FindWindow(where)
75 if partcode == inMenuBar:
76 result = Menu.MenuSelect(where)
77 id = (result>>16) & 0xffff # Hi word
78 item = result & 0xffff # Lo word
79 if id == self.appleid:
80 if item == 1:
81 EasyDialogs.Message("cgitest - First cgi test")
82 return
83 elif item > 1:
84 name = self.applemenu.GetItem(item)
85 Qd.OpenDeskAcc(name)
86 return
87 if id == self.quitid and item == 1:
88 print "Menu-requested QUIT"
89 self.quitting = 1
90 # Anything not handled is passed to Python/SIOUX
91 MacOS.HandleEvent(event)
92
93class AEServer:
94
95 def __init__(self):
96 self.ae_handlers = {}
97
98 def installaehandler(self, classe, type, callback):
99 AE.AEInstallEventHandler(classe, type, self.callback_wrapper)
100 self.ae_handlers[(classe, type)] = callback
101
102 def close(self):
103 for classe, type in self.ae_handlers.keys():
104 AE.AERemoveEventHandler(classe, type)
105
106 def callback_wrapper(self, _request, _reply):
107 _parameters, _attributes = aetools.unpackevent(_request)
108 _class = _attributes['evcl'].type
109 _type = _attributes['evid'].type
110
111 if self.ae_handlers.has_key((_class, _type)):
112 _function = self.ae_handlers[(_class, _type)]
113 elif self.ae_handlers.has_key((_class, '****')):
114 _function = self.ae_handlers[(_class, '****')]
115 elif self.ae_handlers.has_key(('****', '****')):
116 _function = self.ae_handlers[('****', '****')]
117 else:
118 raise 'Cannot happen: AE callback without handler', (_class, _type)
119
120 # XXXX Do key-to-name mapping here
121
122 _parameters['_attributes'] = _attributes
123 _parameters['_class'] = _class
124 _parameters['_type'] = _type
125 if _parameters.has_key('----'):
126 _object = _parameters['----']
127 del _parameters['----']
128 rv = apply(_function, (_object,), _parameters)
129 else:
130 rv = apply(_function, (), _parameters)
131
132 if rv == None:
133 aetools.packevent(_reply, {})
134 else:
135 aetools.packevent(_reply, {'----':rv})
136
137def code(x):
138 "Convert a long int to the 4-character code it really is"
139 s = ''
140 for i in range(4):
141 x, c = divmod(x, 256)
142 s = chr(c) + s
143 return s
144
145class _Test(AEServer, MiniApplication):
146 """Mini test application, handles required events"""
147
148 def __init__(self):
149 MiniApplication.__init__(self)
150 AEServer.__init__(self)
151 self.installaehandler('aevt', 'oapp', self.open_app)
152 self.installaehandler('aevt', 'quit', self.quit)
153 self.installaehandler('aevt', '****', self.other)
154 self.mainloop()
155
156 def quit(self, **args):
157 self.quitting = 1
158
159 def open_app(self, **args):
160 pass
161
162 def other(self, _object=None, _class=None, _type=None, **args):
163 print 'AppleEvent', (_class, _type), 'for', _object, 'Other args:', args
164
165if __name__ == '__main__':
166 _Test()