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