Jack Jansen | d0fc42f | 2001-08-19 22:05:06 +0000 | [diff] [blame] | 1 | """MiniAEFrame - A minimal AppleEvent Application framework. |
| 2 | |
| 3 | There 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. |
| 7 | """ |
| 8 | |
| 9 | import sys |
| 10 | import traceback |
| 11 | import MacOS |
Jack Jansen | 5a6fdcd | 2001-08-25 12:15:04 +0000 | [diff] [blame] | 12 | from Carbon import AE |
| 13 | from Carbon.AppleEvents import * |
| 14 | from Carbon import Evt |
| 15 | from Carbon.Events import * |
| 16 | from Carbon import Menu |
| 17 | from Carbon import Win |
| 18 | from Carbon.Windows import * |
| 19 | from Carbon import Qd |
Jack Jansen | d0fc42f | 2001-08-19 22:05:06 +0000 | [diff] [blame] | 20 | |
| 21 | import aetools |
| 22 | import EasyDialogs |
| 23 | |
| 24 | kHighLevelEvent = 23 # Not defined anywhere for Python yet? |
| 25 | |
| 26 | |
| 27 | class MiniApplication: |
| 28 | |
| 29 | """A minimal FrameWork.Application-like class""" |
| 30 | |
| 31 | 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() |
| 47 | |
| 48 | def __del__(self): |
| 49 | self.close() |
| 50 | |
| 51 | def close(self): |
| 52 | pass |
| 53 | |
| 54 | def mainloop(self, mask = everyEvent, timeout = 60*60): |
| 55 | while not self.quitting: |
| 56 | self.dooneevent(mask, timeout) |
| 57 | |
| 58 | def _quit(self): |
| 59 | self.quitting = 1 |
| 60 | |
| 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) |
| 81 | if modifiers & cmdKey: |
| 82 | if c == '.': |
| 83 | raise KeyboardInterrupt, "Command-period" |
| 84 | if c == 'q': |
Jack Jansen | 94416e5 | 2002-08-02 14:04:42 +0000 | [diff] [blame^] | 85 | if hasattr(MacOS, 'OutputSeen'): |
| 86 | MacOS.OutputSeen() |
Jack Jansen | d0fc42f | 2001-08-19 22:05:06 +0000 | [diff] [blame] | 87 | self.quitting = 1 |
| 88 | return |
| 89 | elif what == mouseDown: |
| 90 | partcode, window = Win.FindWindow(where) |
| 91 | if partcode == inMenuBar: |
| 92 | result = Menu.MenuSelect(where) |
| 93 | id = (result>>16) & 0xffff # Hi word |
| 94 | item = result & 0xffff # Lo word |
| 95 | if id == self.appleid: |
| 96 | if item == 1: |
| 97 | EasyDialogs.Message(self.getabouttext()) |
| 98 | elif item > 1 and hasattr(Menu, 'OpenDeskAcc'): |
| 99 | name = self.applemenu.GetMenuItemText(item) |
| 100 | Menu.OpenDeskAcc(name) |
| 101 | elif id == self.quitid and item == 1: |
Jack Jansen | 94416e5 | 2002-08-02 14:04:42 +0000 | [diff] [blame^] | 102 | if hasattr(MacOS, 'OutputSeen'): |
| 103 | MacOS.OutputSeen() |
Jack Jansen | d0fc42f | 2001-08-19 22:05:06 +0000 | [diff] [blame] | 104 | self.quitting = 1 |
| 105 | Menu.HiliteMenu(0) |
| 106 | return |
| 107 | # Anything not handled is passed to Python/SIOUX |
Jack Jansen | 94416e5 | 2002-08-02 14:04:42 +0000 | [diff] [blame^] | 108 | if hasattr(MacOS, 'HandleEvent'): |
| 109 | MacOS.HandleEvent(event) |
| 110 | else: |
| 111 | print "Unhandled event:", event |
Jack Jansen | d0fc42f | 2001-08-19 22:05:06 +0000 | [diff] [blame] | 112 | |
| 113 | def getabouttext(self): |
| 114 | return self.__class__.__name__ |
| 115 | |
| 116 | def getaboutmenutext(self): |
| 117 | return "About %s\311" % self.__class__.__name__ |
| 118 | |
| 119 | |
| 120 | class AEServer: |
| 121 | |
| 122 | def __init__(self): |
| 123 | self.ae_handlers = {} |
| 124 | |
| 125 | def installaehandler(self, classe, type, callback): |
| 126 | AE.AEInstallEventHandler(classe, type, self.callback_wrapper) |
| 127 | self.ae_handlers[(classe, type)] = callback |
| 128 | |
| 129 | def close(self): |
| 130 | for classe, type in self.ae_handlers.keys(): |
| 131 | AE.AERemoveEventHandler(classe, type) |
| 132 | |
| 133 | def callback_wrapper(self, _request, _reply): |
| 134 | _parameters, _attributes = aetools.unpackevent(_request) |
| 135 | _class = _attributes['evcl'].type |
| 136 | _type = _attributes['evid'].type |
| 137 | |
| 138 | if self.ae_handlers.has_key((_class, _type)): |
| 139 | _function = self.ae_handlers[(_class, _type)] |
| 140 | elif self.ae_handlers.has_key((_class, '****')): |
| 141 | _function = self.ae_handlers[(_class, '****')] |
| 142 | elif self.ae_handlers.has_key(('****', '****')): |
| 143 | _function = self.ae_handlers[('****', '****')] |
| 144 | else: |
| 145 | raise 'Cannot happen: AE callback without handler', (_class, _type) |
| 146 | |
| 147 | # XXXX Do key-to-name mapping here |
| 148 | |
| 149 | _parameters['_attributes'] = _attributes |
| 150 | _parameters['_class'] = _class |
| 151 | _parameters['_type'] = _type |
| 152 | if _parameters.has_key('----'): |
| 153 | _object = _parameters['----'] |
| 154 | del _parameters['----'] |
| 155 | # The try/except that used to be here can mask programmer errors. |
| 156 | # Let the program crash, the programmer can always add a **args |
| 157 | # to the formal parameter list. |
| 158 | rv = apply(_function, (_object,), _parameters) |
| 159 | else: |
| 160 | #Same try/except comment as above |
| 161 | rv = apply(_function, (), _parameters) |
| 162 | |
| 163 | if rv == None: |
| 164 | aetools.packevent(_reply, {}) |
| 165 | else: |
| 166 | aetools.packevent(_reply, {'----':rv}) |
| 167 | |
| 168 | |
| 169 | def code(x): |
| 170 | "Convert a long int to the 4-character code it really is" |
| 171 | s = '' |
| 172 | for i in range(4): |
| 173 | x, c = divmod(x, 256) |
| 174 | s = chr(c) + s |
| 175 | return s |
| 176 | |
| 177 | class _Test(AEServer, MiniApplication): |
| 178 | """Mini test application, handles required events""" |
| 179 | |
| 180 | def __init__(self): |
| 181 | MiniApplication.__init__(self) |
| 182 | AEServer.__init__(self) |
| 183 | self.installaehandler('aevt', 'oapp', self.open_app) |
| 184 | self.installaehandler('aevt', 'quit', self.quit) |
| 185 | self.installaehandler('****', '****', self.other) |
| 186 | self.mainloop() |
| 187 | |
| 188 | def quit(self, **args): |
| 189 | self._quit() |
| 190 | |
| 191 | def open_app(self, **args): |
| 192 | pass |
| 193 | |
| 194 | def other(self, _object=None, _class=None, _type=None, **args): |
| 195 | print 'AppleEvent', (_class, _type), 'for', _object, 'Other args:', args |
| 196 | |
| 197 | |
| 198 | if __name__ == '__main__': |
| 199 | _Test() |