| 1 | n/a | "A sort of application framework for the Mac" |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | DEBUG=0 |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | from warnings import warnpy3k |
|---|
| 6 | n/a | warnpy3k("In 3.x, the FrameWork module is removed.", stacklevel=2) |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | import MacOS |
|---|
| 9 | n/a | import traceback |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | from Carbon.AE import * |
|---|
| 12 | n/a | from Carbon.AppleEvents import * |
|---|
| 13 | n/a | from Carbon.Ctl import * |
|---|
| 14 | n/a | from Carbon.Controls import * |
|---|
| 15 | n/a | from Carbon.Dlg import * |
|---|
| 16 | n/a | from Carbon.Dialogs import * |
|---|
| 17 | n/a | from Carbon.Evt import * |
|---|
| 18 | n/a | from Carbon.Events import * |
|---|
| 19 | n/a | from Carbon.Help import * |
|---|
| 20 | n/a | from Carbon.Menu import * |
|---|
| 21 | n/a | from Carbon.Menus import * |
|---|
| 22 | n/a | from Carbon.Qd import * |
|---|
| 23 | n/a | from Carbon.QuickDraw import * |
|---|
| 24 | n/a | #from Carbon.Res import * |
|---|
| 25 | n/a | #from Carbon.Resources import * |
|---|
| 26 | n/a | #from Carbon.Snd import * |
|---|
| 27 | n/a | #from Carbon.Sound import * |
|---|
| 28 | n/a | from Carbon.Win import * |
|---|
| 29 | n/a | from Carbon.Windows import * |
|---|
| 30 | n/a | import types |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | import EasyDialogs |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | try: |
|---|
| 35 | n/a | MyFrontWindow = FrontNonFloatingWindow |
|---|
| 36 | n/a | except NameError: |
|---|
| 37 | n/a | MyFrontWindow = FrontWindow |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | kHighLevelEvent = 23 # Don't know what header file this should come from |
|---|
| 40 | n/a | SCROLLBARWIDTH = 16 # Again, not a clue... |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | # Trick to forestall a set of SIOUX menus being added to our menubar |
|---|
| 43 | n/a | SIOUX_APPLEMENU_ID=32000 |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | # Map event 'what' field to strings |
|---|
| 47 | n/a | eventname = {} |
|---|
| 48 | n/a | eventname[1] = 'mouseDown' |
|---|
| 49 | n/a | eventname[2] = 'mouseUp' |
|---|
| 50 | n/a | eventname[3] = 'keyDown' |
|---|
| 51 | n/a | eventname[4] = 'keyUp' |
|---|
| 52 | n/a | eventname[5] = 'autoKey' |
|---|
| 53 | n/a | eventname[6] = 'updateEvt' |
|---|
| 54 | n/a | eventname[7] = 'diskEvt' |
|---|
| 55 | n/a | eventname[8] = 'activateEvt' |
|---|
| 56 | n/a | eventname[15] = 'osEvt' |
|---|
| 57 | n/a | eventname[23] = 'kHighLevelEvent' |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | # Map part codes returned by WhichWindow() to strings |
|---|
| 60 | n/a | partname = {} |
|---|
| 61 | n/a | partname[0] = 'inDesk' |
|---|
| 62 | n/a | partname[1] = 'inMenuBar' |
|---|
| 63 | n/a | partname[2] = 'inSysWindow' |
|---|
| 64 | n/a | partname[3] = 'inContent' |
|---|
| 65 | n/a | partname[4] = 'inDrag' |
|---|
| 66 | n/a | partname[5] = 'inGrow' |
|---|
| 67 | n/a | partname[6] = 'inGoAway' |
|---|
| 68 | n/a | partname[7] = 'inZoomIn' |
|---|
| 69 | n/a | partname[8] = 'inZoomOut' |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | # |
|---|
| 72 | n/a | # The useable portion of the screen |
|---|
| 73 | n/a | # ## but what happens with multiple screens? jvr |
|---|
| 74 | n/a | screenbounds = GetQDGlobalsScreenBits().bounds |
|---|
| 75 | n/a | screenbounds = screenbounds[0]+4, screenbounds[1]+4, \ |
|---|
| 76 | n/a | screenbounds[2]-4, screenbounds[3]-4 |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | next_window_x = 16 # jvr |
|---|
| 79 | n/a | next_window_y = 44 # jvr |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | def windowbounds(width, height): |
|---|
| 82 | n/a | "Return sensible window bounds" |
|---|
| 83 | n/a | global next_window_x, next_window_y |
|---|
| 84 | n/a | r, b = next_window_x+width, next_window_y+height |
|---|
| 85 | n/a | if r > screenbounds[2]: |
|---|
| 86 | n/a | next_window_x = 16 |
|---|
| 87 | n/a | if b > screenbounds[3]: |
|---|
| 88 | n/a | next_window_y = 44 |
|---|
| 89 | n/a | l, t = next_window_x, next_window_y |
|---|
| 90 | n/a | r, b = next_window_x+width, next_window_y+height |
|---|
| 91 | n/a | next_window_x, next_window_y = next_window_x + 8, next_window_y + 20 # jvr |
|---|
| 92 | n/a | return l, t, r, b |
|---|
| 93 | n/a | |
|---|
| 94 | n/a | _watch = None |
|---|
| 95 | n/a | def setwatchcursor(): |
|---|
| 96 | n/a | global _watch |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | if _watch is None: |
|---|
| 99 | n/a | _watch = GetCursor(4).data |
|---|
| 100 | n/a | SetCursor(_watch) |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | def setarrowcursor(): |
|---|
| 103 | n/a | SetCursor(GetQDGlobalsArrow()) |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | class Application: |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | "Application framework -- your application should be a derived class" |
|---|
| 108 | n/a | |
|---|
| 109 | n/a | def __init__(self, nomenubar=0): |
|---|
| 110 | n/a | self._doing_asyncevents = 0 |
|---|
| 111 | n/a | self.quitting = 0 |
|---|
| 112 | n/a | self.needmenubarredraw = 0 |
|---|
| 113 | n/a | self._windows = {} |
|---|
| 114 | n/a | self._helpmenu = None |
|---|
| 115 | n/a | if nomenubar: |
|---|
| 116 | n/a | self.menubar = None |
|---|
| 117 | n/a | else: |
|---|
| 118 | n/a | self.makemenubar() |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | def __del__(self): |
|---|
| 121 | n/a | if self._doing_asyncevents: |
|---|
| 122 | n/a | self._doing_asyncevents = 0 |
|---|
| 123 | n/a | MacOS.SetEventHandler() |
|---|
| 124 | n/a | |
|---|
| 125 | n/a | def makemenubar(self): |
|---|
| 126 | n/a | self.menubar = MenuBar(self) |
|---|
| 127 | n/a | AppleMenu(self.menubar, self.getabouttext(), self.do_about) |
|---|
| 128 | n/a | self.makeusermenus() |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | def makeusermenus(self): |
|---|
| 131 | n/a | self.filemenu = m = Menu(self.menubar, "File") |
|---|
| 132 | n/a | self._quititem = MenuItem(m, "Quit", "Q", self._quit) |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | def gethelpmenu(self): |
|---|
| 135 | n/a | if self._helpmenu is None: |
|---|
| 136 | n/a | self._helpmenu = HelpMenu(self.menubar) |
|---|
| 137 | n/a | return self._helpmenu |
|---|
| 138 | n/a | |
|---|
| 139 | n/a | def _quit(self, *args): |
|---|
| 140 | n/a | self.quitting = 1 |
|---|
| 141 | n/a | |
|---|
| 142 | n/a | def cleanup(self): |
|---|
| 143 | n/a | for w in self._windows.values(): |
|---|
| 144 | n/a | w.do_close() |
|---|
| 145 | n/a | return self._windows == {} |
|---|
| 146 | n/a | |
|---|
| 147 | n/a | def appendwindow(self, wid, window): |
|---|
| 148 | n/a | self._windows[wid] = window |
|---|
| 149 | n/a | |
|---|
| 150 | n/a | def removewindow(self, wid): |
|---|
| 151 | n/a | del self._windows[wid] |
|---|
| 152 | n/a | |
|---|
| 153 | n/a | def getabouttext(self): |
|---|
| 154 | n/a | return "About %s..." % self.__class__.__name__ |
|---|
| 155 | n/a | |
|---|
| 156 | n/a | def do_about(self, id, item, window, event): |
|---|
| 157 | n/a | EasyDialogs.Message("Hello, world!" + "\015(%s)" % self.__class__.__name__) |
|---|
| 158 | n/a | |
|---|
| 159 | n/a | # The main event loop is broken up in several simple steps. |
|---|
| 160 | n/a | # This is done so you can override each individual part, |
|---|
| 161 | n/a | # if you have a need to do extra processing independent of the |
|---|
| 162 | n/a | # event type. |
|---|
| 163 | n/a | # Normally, however, you'd just define handlers for individual |
|---|
| 164 | n/a | # events. |
|---|
| 165 | n/a | |
|---|
| 166 | n/a | schedparams = (0, 0) # By default disable Python's event handling |
|---|
| 167 | n/a | default_wait = None # By default we wait GetCaretTime in WaitNextEvent |
|---|
| 168 | n/a | |
|---|
| 169 | n/a | def mainloop(self, mask = everyEvent, wait = None): |
|---|
| 170 | n/a | self.quitting = 0 |
|---|
| 171 | n/a | if hasattr(MacOS, 'SchedParams'): |
|---|
| 172 | n/a | saveparams = MacOS.SchedParams(*self.schedparams) |
|---|
| 173 | n/a | try: |
|---|
| 174 | n/a | while not self.quitting: |
|---|
| 175 | n/a | try: |
|---|
| 176 | n/a | self.do1event(mask, wait) |
|---|
| 177 | n/a | except (Application, SystemExit): |
|---|
| 178 | n/a | # Note: the raising of "self" is old-fashioned idiom to |
|---|
| 179 | n/a | # exit the mainloop. Calling _quit() is better for new |
|---|
| 180 | n/a | # applications. |
|---|
| 181 | n/a | break |
|---|
| 182 | n/a | finally: |
|---|
| 183 | n/a | if hasattr(MacOS, 'SchedParams'): |
|---|
| 184 | n/a | MacOS.SchedParams(*saveparams) |
|---|
| 185 | n/a | |
|---|
| 186 | n/a | def dopendingevents(self, mask = everyEvent): |
|---|
| 187 | n/a | """dopendingevents - Handle all pending events""" |
|---|
| 188 | n/a | while self.do1event(mask, wait=0): |
|---|
| 189 | n/a | pass |
|---|
| 190 | n/a | |
|---|
| 191 | n/a | def do1event(self, mask = everyEvent, wait = None): |
|---|
| 192 | n/a | ok, event = self.getevent(mask, wait) |
|---|
| 193 | n/a | if IsDialogEvent(event): |
|---|
| 194 | n/a | if self.do_dialogevent(event): |
|---|
| 195 | n/a | return |
|---|
| 196 | n/a | if ok: |
|---|
| 197 | n/a | self.dispatch(event) |
|---|
| 198 | n/a | else: |
|---|
| 199 | n/a | self.idle(event) |
|---|
| 200 | n/a | |
|---|
| 201 | n/a | def idle(self, event): |
|---|
| 202 | n/a | pass |
|---|
| 203 | n/a | |
|---|
| 204 | n/a | def getevent(self, mask = everyEvent, wait = None): |
|---|
| 205 | n/a | if self.needmenubarredraw: |
|---|
| 206 | n/a | DrawMenuBar() |
|---|
| 207 | n/a | self.needmenubarredraw = 0 |
|---|
| 208 | n/a | if wait is None: |
|---|
| 209 | n/a | wait = self.default_wait |
|---|
| 210 | n/a | if wait is None: |
|---|
| 211 | n/a | wait = GetCaretTime() |
|---|
| 212 | n/a | ok, event = WaitNextEvent(mask, wait) |
|---|
| 213 | n/a | return ok, event |
|---|
| 214 | n/a | |
|---|
| 215 | n/a | def dispatch(self, event): |
|---|
| 216 | n/a | # The following appears to be double work (already done in do1event) |
|---|
| 217 | n/a | # but we need it for asynchronous event handling |
|---|
| 218 | n/a | if IsDialogEvent(event): |
|---|
| 219 | n/a | if self.do_dialogevent(event): |
|---|
| 220 | n/a | return |
|---|
| 221 | n/a | (what, message, when, where, modifiers) = event |
|---|
| 222 | n/a | if what in eventname: |
|---|
| 223 | n/a | name = "do_" + eventname[what] |
|---|
| 224 | n/a | else: |
|---|
| 225 | n/a | name = "do_%d" % what |
|---|
| 226 | n/a | try: |
|---|
| 227 | n/a | handler = getattr(self, name) |
|---|
| 228 | n/a | except AttributeError: |
|---|
| 229 | n/a | handler = self.do_unknownevent |
|---|
| 230 | n/a | handler(event) |
|---|
| 231 | n/a | |
|---|
| 232 | n/a | def asyncevents(self, onoff): |
|---|
| 233 | n/a | """asyncevents - Set asynchronous event handling on or off""" |
|---|
| 234 | n/a | if MacOS.runtimemodel == 'macho': |
|---|
| 235 | n/a | raise 'Unsupported in MachoPython' |
|---|
| 236 | n/a | old = self._doing_asyncevents |
|---|
| 237 | n/a | if old: |
|---|
| 238 | n/a | MacOS.SetEventHandler() |
|---|
| 239 | n/a | MacOS.SchedParams(*self.schedparams) |
|---|
| 240 | n/a | if onoff: |
|---|
| 241 | n/a | MacOS.SetEventHandler(self.dispatch) |
|---|
| 242 | n/a | doint, dummymask, benice, howoften, bgyield = \ |
|---|
| 243 | n/a | self.schedparams |
|---|
| 244 | n/a | MacOS.SchedParams(doint, everyEvent, benice, |
|---|
| 245 | n/a | howoften, bgyield) |
|---|
| 246 | n/a | self._doing_asyncevents = onoff |
|---|
| 247 | n/a | return old |
|---|
| 248 | n/a | |
|---|
| 249 | n/a | def do_dialogevent(self, event): |
|---|
| 250 | n/a | gotone, dlg, item = DialogSelect(event) |
|---|
| 251 | n/a | if gotone: |
|---|
| 252 | n/a | window = dlg.GetDialogWindow() |
|---|
| 253 | n/a | if window in self._windows: |
|---|
| 254 | n/a | self._windows[window].do_itemhit(item, event) |
|---|
| 255 | n/a | else: |
|---|
| 256 | n/a | print 'Dialog event for unknown dialog' |
|---|
| 257 | n/a | return 1 |
|---|
| 258 | n/a | return 0 |
|---|
| 259 | n/a | |
|---|
| 260 | n/a | def do_mouseDown(self, event): |
|---|
| 261 | n/a | (what, message, when, where, modifiers) = event |
|---|
| 262 | n/a | partcode, wid = FindWindow(where) |
|---|
| 263 | n/a | |
|---|
| 264 | n/a | # |
|---|
| 265 | n/a | # Find the correct name. |
|---|
| 266 | n/a | # |
|---|
| 267 | n/a | if partcode in partname: |
|---|
| 268 | n/a | name = "do_" + partname[partcode] |
|---|
| 269 | n/a | else: |
|---|
| 270 | n/a | name = "do_%d" % partcode |
|---|
| 271 | n/a | |
|---|
| 272 | n/a | if wid is None: |
|---|
| 273 | n/a | # No window, or a non-python window |
|---|
| 274 | n/a | try: |
|---|
| 275 | n/a | handler = getattr(self, name) |
|---|
| 276 | n/a | except AttributeError: |
|---|
| 277 | n/a | # Not menubar or something, so assume someone |
|---|
| 278 | n/a | # else's window |
|---|
| 279 | n/a | if hasattr(MacOS, 'HandleEvent'): |
|---|
| 280 | n/a | MacOS.HandleEvent(event) |
|---|
| 281 | n/a | return |
|---|
| 282 | n/a | elif wid in self._windows: |
|---|
| 283 | n/a | # It is a window. Hand off to correct window. |
|---|
| 284 | n/a | window = self._windows[wid] |
|---|
| 285 | n/a | try: |
|---|
| 286 | n/a | handler = getattr(window, name) |
|---|
| 287 | n/a | except AttributeError: |
|---|
| 288 | n/a | handler = self.do_unknownpartcode |
|---|
| 289 | n/a | else: |
|---|
| 290 | n/a | # It is a python-toolbox window, but not ours. |
|---|
| 291 | n/a | handler = self.do_unknownwindow |
|---|
| 292 | n/a | handler(partcode, wid, event) |
|---|
| 293 | n/a | |
|---|
| 294 | n/a | def do_inSysWindow(self, partcode, window, event): |
|---|
| 295 | n/a | if hasattr(MacOS, 'HandleEvent'): |
|---|
| 296 | n/a | MacOS.HandleEvent(event) |
|---|
| 297 | n/a | |
|---|
| 298 | n/a | def do_inDesk(self, partcode, window, event): |
|---|
| 299 | n/a | if hasattr(MacOS, 'HandleEvent'): |
|---|
| 300 | n/a | MacOS.HandleEvent(event) |
|---|
| 301 | n/a | |
|---|
| 302 | n/a | def do_inMenuBar(self, partcode, window, event): |
|---|
| 303 | n/a | if not self.menubar: |
|---|
| 304 | n/a | if hasattr(MacOS, 'HandleEvent'): |
|---|
| 305 | n/a | MacOS.HandleEvent(event) |
|---|
| 306 | n/a | return |
|---|
| 307 | n/a | (what, message, when, where, modifiers) = event |
|---|
| 308 | n/a | result = MenuSelect(where) |
|---|
| 309 | n/a | id = (result>>16) & 0xffff # Hi word |
|---|
| 310 | n/a | if id >= 0x8000: |
|---|
| 311 | n/a | id = -65536 + id |
|---|
| 312 | n/a | item = result & 0xffff # Lo word |
|---|
| 313 | n/a | self.do_rawmenu(id, item, window, event) |
|---|
| 314 | n/a | |
|---|
| 315 | n/a | def do_rawmenu(self, id, item, window, event): |
|---|
| 316 | n/a | try: |
|---|
| 317 | n/a | self.do_menu(id, item, window, event) |
|---|
| 318 | n/a | finally: |
|---|
| 319 | n/a | HiliteMenu(0) |
|---|
| 320 | n/a | |
|---|
| 321 | n/a | def do_menu(self, id, item, window, event): |
|---|
| 322 | n/a | if hasattr(MacOS, 'OutputSeen'): |
|---|
| 323 | n/a | MacOS.OutputSeen() |
|---|
| 324 | n/a | self.menubar.dispatch(id, item, window, event) |
|---|
| 325 | n/a | |
|---|
| 326 | n/a | |
|---|
| 327 | n/a | def do_unknownpartcode(self, partcode, window, event): |
|---|
| 328 | n/a | (what, message, when, where, modifiers) = event |
|---|
| 329 | n/a | if DEBUG: print "Mouse down at global:", where |
|---|
| 330 | n/a | if DEBUG: print "\tUnknown part code:", partcode |
|---|
| 331 | n/a | if DEBUG: print "\tEvent:", self.printevent(event) |
|---|
| 332 | n/a | if hasattr(MacOS, 'HandleEvent'): |
|---|
| 333 | n/a | MacOS.HandleEvent(event) |
|---|
| 334 | n/a | |
|---|
| 335 | n/a | def do_unknownwindow(self, partcode, window, event): |
|---|
| 336 | n/a | if DEBUG: print 'Unknown window:', window |
|---|
| 337 | n/a | if hasattr(MacOS, 'HandleEvent'): |
|---|
| 338 | n/a | MacOS.HandleEvent(event) |
|---|
| 339 | n/a | |
|---|
| 340 | n/a | def do_keyDown(self, event): |
|---|
| 341 | n/a | self.do_key(event) |
|---|
| 342 | n/a | |
|---|
| 343 | n/a | def do_autoKey(self, event): |
|---|
| 344 | n/a | if not event[-1] & cmdKey: |
|---|
| 345 | n/a | self.do_key(event) |
|---|
| 346 | n/a | |
|---|
| 347 | n/a | def do_key(self, event): |
|---|
| 348 | n/a | (what, message, when, where, modifiers) = event |
|---|
| 349 | n/a | c = chr(message & charCodeMask) |
|---|
| 350 | n/a | if self.menubar: |
|---|
| 351 | n/a | result = MenuEvent(event) |
|---|
| 352 | n/a | id = (result>>16) & 0xffff # Hi word |
|---|
| 353 | n/a | item = result & 0xffff # Lo word |
|---|
| 354 | n/a | if id: |
|---|
| 355 | n/a | self.do_rawmenu(id, item, None, event) |
|---|
| 356 | n/a | return |
|---|
| 357 | n/a | # Otherwise we fall-through |
|---|
| 358 | n/a | if modifiers & cmdKey: |
|---|
| 359 | n/a | if c == '.': |
|---|
| 360 | n/a | raise self |
|---|
| 361 | n/a | else: |
|---|
| 362 | n/a | if not self.menubar: |
|---|
| 363 | n/a | if hasattr(MacOS, 'HandleEvent'): |
|---|
| 364 | n/a | MacOS.HandleEvent(event) |
|---|
| 365 | n/a | return |
|---|
| 366 | n/a | else: |
|---|
| 367 | n/a | # See whether the front window wants it |
|---|
| 368 | n/a | w = MyFrontWindow() |
|---|
| 369 | n/a | if w and w in self._windows: |
|---|
| 370 | n/a | window = self._windows[w] |
|---|
| 371 | n/a | try: |
|---|
| 372 | n/a | do_char = window.do_char |
|---|
| 373 | n/a | except AttributeError: |
|---|
| 374 | n/a | do_char = self.do_char |
|---|
| 375 | n/a | do_char(c, event) |
|---|
| 376 | n/a | # else it wasn't for us, sigh... |
|---|
| 377 | n/a | |
|---|
| 378 | n/a | def do_char(self, c, event): |
|---|
| 379 | n/a | if DEBUG: print "Character", repr(c) |
|---|
| 380 | n/a | |
|---|
| 381 | n/a | def do_updateEvt(self, event): |
|---|
| 382 | n/a | (what, message, when, where, modifiers) = event |
|---|
| 383 | n/a | wid = WhichWindow(message) |
|---|
| 384 | n/a | if wid and wid in self._windows: |
|---|
| 385 | n/a | window = self._windows[wid] |
|---|
| 386 | n/a | window.do_rawupdate(wid, event) |
|---|
| 387 | n/a | else: |
|---|
| 388 | n/a | if hasattr(MacOS, 'HandleEvent'): |
|---|
| 389 | n/a | MacOS.HandleEvent(event) |
|---|
| 390 | n/a | |
|---|
| 391 | n/a | def do_activateEvt(self, event): |
|---|
| 392 | n/a | (what, message, when, where, modifiers) = event |
|---|
| 393 | n/a | wid = WhichWindow(message) |
|---|
| 394 | n/a | if wid and wid in self._windows: |
|---|
| 395 | n/a | window = self._windows[wid] |
|---|
| 396 | n/a | window.do_activate(modifiers & 1, event) |
|---|
| 397 | n/a | else: |
|---|
| 398 | n/a | if hasattr(MacOS, 'HandleEvent'): |
|---|
| 399 | n/a | MacOS.HandleEvent(event) |
|---|
| 400 | n/a | |
|---|
| 401 | n/a | def do_osEvt(self, event): |
|---|
| 402 | n/a | (what, message, when, where, modifiers) = event |
|---|
| 403 | n/a | which = (message >> 24) & 0xff |
|---|
| 404 | n/a | if which == 1: # suspend/resume |
|---|
| 405 | n/a | self.do_suspendresume(event) |
|---|
| 406 | n/a | else: |
|---|
| 407 | n/a | if DEBUG: |
|---|
| 408 | n/a | print 'unknown osEvt:', |
|---|
| 409 | n/a | self.printevent(event) |
|---|
| 410 | n/a | |
|---|
| 411 | n/a | def do_suspendresume(self, event): |
|---|
| 412 | n/a | (what, message, when, where, modifiers) = event |
|---|
| 413 | n/a | wid = MyFrontWindow() |
|---|
| 414 | n/a | if wid and wid in self._windows: |
|---|
| 415 | n/a | window = self._windows[wid] |
|---|
| 416 | n/a | window.do_activate(message & 1, event) |
|---|
| 417 | n/a | |
|---|
| 418 | n/a | def do_kHighLevelEvent(self, event): |
|---|
| 419 | n/a | (what, message, when, where, modifiers) = event |
|---|
| 420 | n/a | if DEBUG: |
|---|
| 421 | n/a | print "High Level Event:", |
|---|
| 422 | n/a | self.printevent(event) |
|---|
| 423 | n/a | try: |
|---|
| 424 | n/a | AEProcessAppleEvent(event) |
|---|
| 425 | n/a | except: |
|---|
| 426 | n/a | pass |
|---|
| 427 | n/a | #print "AEProcessAppleEvent error:" |
|---|
| 428 | n/a | #traceback.print_exc() |
|---|
| 429 | n/a | |
|---|
| 430 | n/a | def do_unknownevent(self, event): |
|---|
| 431 | n/a | if DEBUG: |
|---|
| 432 | n/a | print "Unhandled event:", |
|---|
| 433 | n/a | self.printevent(event) |
|---|
| 434 | n/a | |
|---|
| 435 | n/a | def printevent(self, event): |
|---|
| 436 | n/a | (what, message, when, where, modifiers) = event |
|---|
| 437 | n/a | nicewhat = repr(what) |
|---|
| 438 | n/a | if what in eventname: |
|---|
| 439 | n/a | nicewhat = eventname[what] |
|---|
| 440 | n/a | print nicewhat, |
|---|
| 441 | n/a | if what == kHighLevelEvent: |
|---|
| 442 | n/a | h, v = where |
|---|
| 443 | n/a | print repr(ostypecode(message)), hex(when), repr(ostypecode(h | (v<<16))), |
|---|
| 444 | n/a | else: |
|---|
| 445 | n/a | print hex(message), hex(when), where, |
|---|
| 446 | n/a | print hex(modifiers) |
|---|
| 447 | n/a | |
|---|
| 448 | n/a | |
|---|
| 449 | n/a | class MenuBar: |
|---|
| 450 | n/a | """Represent a set of menus in a menu bar. |
|---|
| 451 | n/a | |
|---|
| 452 | n/a | Interface: |
|---|
| 453 | n/a | |
|---|
| 454 | n/a | - (constructor) |
|---|
| 455 | n/a | - (destructor) |
|---|
| 456 | n/a | - addmenu |
|---|
| 457 | n/a | - addpopup (normally used internally) |
|---|
| 458 | n/a | - dispatch (called from Application) |
|---|
| 459 | n/a | """ |
|---|
| 460 | n/a | |
|---|
| 461 | n/a | nextid = 1 # Necessarily a class variable |
|---|
| 462 | n/a | |
|---|
| 463 | n/a | def getnextid(self): |
|---|
| 464 | n/a | id = MenuBar.nextid |
|---|
| 465 | n/a | MenuBar.nextid = id+1 |
|---|
| 466 | n/a | return id |
|---|
| 467 | n/a | |
|---|
| 468 | n/a | def __init__(self, parent=None): |
|---|
| 469 | n/a | self.parent = parent |
|---|
| 470 | n/a | ClearMenuBar() |
|---|
| 471 | n/a | self.bar = GetMenuBar() |
|---|
| 472 | n/a | self.menus = {} |
|---|
| 473 | n/a | |
|---|
| 474 | n/a | # XXX necessary? |
|---|
| 475 | n/a | def close(self): |
|---|
| 476 | n/a | self.parent = None |
|---|
| 477 | n/a | self.bar = None |
|---|
| 478 | n/a | self.menus = None |
|---|
| 479 | n/a | |
|---|
| 480 | n/a | def addmenu(self, title, after = 0, id=None): |
|---|
| 481 | n/a | if id is None: |
|---|
| 482 | n/a | id = self.getnextid() |
|---|
| 483 | n/a | if DEBUG: print 'Newmenu', title, id # XXXX |
|---|
| 484 | n/a | m = NewMenu(id, title) |
|---|
| 485 | n/a | m.InsertMenu(after) |
|---|
| 486 | n/a | if after >= 0: |
|---|
| 487 | n/a | if self.parent: |
|---|
| 488 | n/a | self.parent.needmenubarredraw = 1 |
|---|
| 489 | n/a | else: |
|---|
| 490 | n/a | DrawMenuBar() |
|---|
| 491 | n/a | return id, m |
|---|
| 492 | n/a | |
|---|
| 493 | n/a | def delmenu(self, id): |
|---|
| 494 | n/a | if DEBUG: print 'Delmenu', id # XXXX |
|---|
| 495 | n/a | DeleteMenu(id) |
|---|
| 496 | n/a | |
|---|
| 497 | n/a | def addpopup(self, title = ''): |
|---|
| 498 | n/a | return self.addmenu(title, -1) |
|---|
| 499 | n/a | |
|---|
| 500 | n/a | # Useless: |
|---|
| 501 | n/a | # def install(self): |
|---|
| 502 | n/a | # if not self.bar: return |
|---|
| 503 | n/a | # SetMenuBar(self.bar) |
|---|
| 504 | n/a | # if self.parent: |
|---|
| 505 | n/a | # self.parent.needmenubarredraw = 1 |
|---|
| 506 | n/a | # else: |
|---|
| 507 | n/a | # DrawMenuBar() |
|---|
| 508 | n/a | |
|---|
| 509 | n/a | def fixmenudimstate(self): |
|---|
| 510 | n/a | for m in self.menus.keys(): |
|---|
| 511 | n/a | menu = self.menus[m] |
|---|
| 512 | n/a | if menu.__class__ == FrameWork.AppleMenu: |
|---|
| 513 | n/a | continue |
|---|
| 514 | n/a | for i in range(len(menu.items)): |
|---|
| 515 | n/a | label, shortcut, callback, kind = menu.items[i] |
|---|
| 516 | n/a | if type(callback) == types.StringType: |
|---|
| 517 | n/a | wid = MyFrontWindow() |
|---|
| 518 | n/a | if wid and wid in self.parent._windows: |
|---|
| 519 | n/a | window = self.parent._windows[wid] |
|---|
| 520 | n/a | if hasattr(window, "domenu_" + callback): |
|---|
| 521 | n/a | menu.menu.EnableMenuItem(i + 1) |
|---|
| 522 | n/a | elif hasattr(self.parent, "domenu_" + callback): |
|---|
| 523 | n/a | menu.menu.EnableMenuItem(i + 1) |
|---|
| 524 | n/a | else: |
|---|
| 525 | n/a | menu.menu.DisableMenuItem(i + 1) |
|---|
| 526 | n/a | elif hasattr(self.parent, "domenu_" + callback): |
|---|
| 527 | n/a | menu.menu.EnableMenuItem(i + 1) |
|---|
| 528 | n/a | else: |
|---|
| 529 | n/a | menu.menu.DisableMenuItem(i + 1) |
|---|
| 530 | n/a | elif callback: |
|---|
| 531 | n/a | pass |
|---|
| 532 | n/a | |
|---|
| 533 | n/a | def dispatch(self, id, item, window, event): |
|---|
| 534 | n/a | if id in self.menus: |
|---|
| 535 | n/a | self.menus[id].dispatch(id, item, window, event) |
|---|
| 536 | n/a | else: |
|---|
| 537 | n/a | if DEBUG: print "MenuBar.dispatch(%d, %d, %s, %s)" % \ |
|---|
| 538 | n/a | (id, item, window, event) |
|---|
| 539 | n/a | |
|---|
| 540 | n/a | |
|---|
| 541 | n/a | # XXX Need a way to get menus as resources and bind them to callbacks |
|---|
| 542 | n/a | |
|---|
| 543 | n/a | class Menu: |
|---|
| 544 | n/a | "One menu." |
|---|
| 545 | n/a | |
|---|
| 546 | n/a | def __init__(self, bar, title, after=0, id=None): |
|---|
| 547 | n/a | self.bar = bar |
|---|
| 548 | n/a | self.id, self.menu = self.bar.addmenu(title, after, id) |
|---|
| 549 | n/a | bar.menus[self.id] = self |
|---|
| 550 | n/a | self.items = [] |
|---|
| 551 | n/a | self._parent = None |
|---|
| 552 | n/a | |
|---|
| 553 | n/a | def delete(self): |
|---|
| 554 | n/a | self.bar.delmenu(self.id) |
|---|
| 555 | n/a | del self.bar.menus[self.id] |
|---|
| 556 | n/a | self.menu.DisposeMenu() |
|---|
| 557 | n/a | del self.bar |
|---|
| 558 | n/a | del self.items |
|---|
| 559 | n/a | del self.menu |
|---|
| 560 | n/a | del self.id |
|---|
| 561 | n/a | del self._parent |
|---|
| 562 | n/a | |
|---|
| 563 | n/a | def additem(self, label, shortcut=None, callback=None, kind=None): |
|---|
| 564 | n/a | self.menu.AppendMenu('x') # add a dummy string |
|---|
| 565 | n/a | self.items.append((label, shortcut, callback, kind)) |
|---|
| 566 | n/a | item = len(self.items) |
|---|
| 567 | n/a | if isinstance(label, unicode): |
|---|
| 568 | n/a | self.menu.SetMenuItemTextWithCFString(item, label) |
|---|
| 569 | n/a | else: |
|---|
| 570 | n/a | self.menu.SetMenuItemText(item, label) |
|---|
| 571 | n/a | if shortcut and type(shortcut) == type(()): |
|---|
| 572 | n/a | modifiers, char = shortcut[:2] |
|---|
| 573 | n/a | self.menu.SetItemCmd(item, ord(char)) |
|---|
| 574 | n/a | self.menu.SetMenuItemModifiers(item, modifiers) |
|---|
| 575 | n/a | if len(shortcut) > 2: |
|---|
| 576 | n/a | self.menu.SetMenuItemKeyGlyph(item, shortcut[2]) |
|---|
| 577 | n/a | elif shortcut: |
|---|
| 578 | n/a | self.menu.SetItemCmd(item, ord(shortcut)) |
|---|
| 579 | n/a | return item |
|---|
| 580 | n/a | |
|---|
| 581 | n/a | def delitem(self, item): |
|---|
| 582 | n/a | if item != len(self.items): |
|---|
| 583 | n/a | raise 'Can only delete last item of a menu' |
|---|
| 584 | n/a | self.menu.DeleteMenuItem(item) |
|---|
| 585 | n/a | del self.items[item-1] |
|---|
| 586 | n/a | |
|---|
| 587 | n/a | def addcheck(self, label, shortcut=None, callback=None): |
|---|
| 588 | n/a | return self.additem(label, shortcut, callback, 'check') |
|---|
| 589 | n/a | |
|---|
| 590 | n/a | def addradio(self, label, shortcut=None, callback=None): |
|---|
| 591 | n/a | return self.additem(label, shortcut, callback, 'radio') |
|---|
| 592 | n/a | |
|---|
| 593 | n/a | def addseparator(self): |
|---|
| 594 | n/a | self.menu.AppendMenu('(-') |
|---|
| 595 | n/a | self.items.append(('', None, None, 'separator')) |
|---|
| 596 | n/a | |
|---|
| 597 | n/a | def addsubmenu(self, label, title=''): |
|---|
| 598 | n/a | sub = Menu(self.bar, title, -1) |
|---|
| 599 | n/a | item = self.additem(label, '\x1B', None, 'submenu') |
|---|
| 600 | n/a | self.menu.SetItemMark(item, sub.id) |
|---|
| 601 | n/a | sub._parent = self |
|---|
| 602 | n/a | sub._parent_item = item |
|---|
| 603 | n/a | return sub |
|---|
| 604 | n/a | |
|---|
| 605 | n/a | def dispatch(self, id, item, window, event): |
|---|
| 606 | n/a | title, shortcut, callback, mtype = self.items[item-1] |
|---|
| 607 | n/a | if callback: |
|---|
| 608 | n/a | if not self.bar.parent or type(callback) != types.StringType: |
|---|
| 609 | n/a | menuhandler = callback |
|---|
| 610 | n/a | else: |
|---|
| 611 | n/a | # callback is string |
|---|
| 612 | n/a | wid = MyFrontWindow() |
|---|
| 613 | n/a | if wid and wid in self.bar.parent._windows: |
|---|
| 614 | n/a | window = self.bar.parent._windows[wid] |
|---|
| 615 | n/a | if hasattr(window, "domenu_" + callback): |
|---|
| 616 | n/a | menuhandler = getattr(window, "domenu_" + callback) |
|---|
| 617 | n/a | elif hasattr(self.bar.parent, "domenu_" + callback): |
|---|
| 618 | n/a | menuhandler = getattr(self.bar.parent, "domenu_" + callback) |
|---|
| 619 | n/a | else: |
|---|
| 620 | n/a | # nothing we can do. we shouldn't have come this far |
|---|
| 621 | n/a | # since the menu item should have been disabled... |
|---|
| 622 | n/a | return |
|---|
| 623 | n/a | elif hasattr(self.bar.parent, "domenu_" + callback): |
|---|
| 624 | n/a | menuhandler = getattr(self.bar.parent, "domenu_" + callback) |
|---|
| 625 | n/a | else: |
|---|
| 626 | n/a | # nothing we can do. we shouldn't have come this far |
|---|
| 627 | n/a | # since the menu item should have been disabled... |
|---|
| 628 | n/a | return |
|---|
| 629 | n/a | menuhandler(id, item, window, event) |
|---|
| 630 | n/a | |
|---|
| 631 | n/a | def enable(self, onoff): |
|---|
| 632 | n/a | if onoff: |
|---|
| 633 | n/a | self.menu.EnableMenuItem(0) |
|---|
| 634 | n/a | if self._parent: |
|---|
| 635 | n/a | self._parent.menu.EnableMenuItem(self._parent_item) |
|---|
| 636 | n/a | else: |
|---|
| 637 | n/a | self.menu.DisableMenuItem(0) |
|---|
| 638 | n/a | if self._parent: |
|---|
| 639 | n/a | self._parent.menu.DisableMenuItem(self._parent_item) |
|---|
| 640 | n/a | if self.bar and self.bar.parent: |
|---|
| 641 | n/a | self.bar.parent.needmenubarredraw = 1 |
|---|
| 642 | n/a | |
|---|
| 643 | n/a | class PopupMenu(Menu): |
|---|
| 644 | n/a | def __init__(self, bar): |
|---|
| 645 | n/a | Menu.__init__(self, bar, '(popup)', -1) |
|---|
| 646 | n/a | |
|---|
| 647 | n/a | def popup(self, x, y, event, default=1, window=None): |
|---|
| 648 | n/a | # NOTE that x and y are global coordinates, and they should probably |
|---|
| 649 | n/a | # be topleft of the button the user clicked (not mouse-coordinates), |
|---|
| 650 | n/a | # so the popup nicely overlaps. |
|---|
| 651 | n/a | reply = self.menu.PopUpMenuSelect(x, y, default) |
|---|
| 652 | n/a | if not reply: |
|---|
| 653 | n/a | return |
|---|
| 654 | n/a | id = (reply >> 16) & 0xffff |
|---|
| 655 | n/a | item = reply & 0xffff |
|---|
| 656 | n/a | if not window: |
|---|
| 657 | n/a | wid = MyFrontWindow() |
|---|
| 658 | n/a | try: |
|---|
| 659 | n/a | window = self.bar.parent._windows[wid] |
|---|
| 660 | n/a | except: |
|---|
| 661 | n/a | pass # If we can't find the window we pass None |
|---|
| 662 | n/a | self.dispatch(id, item, window, event) |
|---|
| 663 | n/a | |
|---|
| 664 | n/a | class MenuItem: |
|---|
| 665 | n/a | def __init__(self, menu, title, shortcut=None, callback=None, kind=None): |
|---|
| 666 | n/a | self.item = menu.additem(title, shortcut, callback) |
|---|
| 667 | n/a | self.menu = menu |
|---|
| 668 | n/a | |
|---|
| 669 | n/a | def delete(self): |
|---|
| 670 | n/a | self.menu.delitem(self.item) |
|---|
| 671 | n/a | del self.menu |
|---|
| 672 | n/a | del self.item |
|---|
| 673 | n/a | |
|---|
| 674 | n/a | def check(self, onoff): |
|---|
| 675 | n/a | self.menu.menu.CheckMenuItem(self.item, onoff) |
|---|
| 676 | n/a | |
|---|
| 677 | n/a | def enable(self, onoff): |
|---|
| 678 | n/a | if onoff: |
|---|
| 679 | n/a | self.menu.menu.EnableMenuItem(self.item) |
|---|
| 680 | n/a | else: |
|---|
| 681 | n/a | self.menu.menu.DisableMenuItem(self.item) |
|---|
| 682 | n/a | |
|---|
| 683 | n/a | def settext(self, text): |
|---|
| 684 | n/a | self.menu.menu.SetMenuItemText(self.item, text) |
|---|
| 685 | n/a | |
|---|
| 686 | n/a | def setstyle(self, style): |
|---|
| 687 | n/a | self.menu.menu.SetItemStyle(self.item, style) |
|---|
| 688 | n/a | |
|---|
| 689 | n/a | def seticon(self, icon): |
|---|
| 690 | n/a | self.menu.menu.SetItemIcon(self.item, icon) |
|---|
| 691 | n/a | |
|---|
| 692 | n/a | def setcmd(self, cmd): |
|---|
| 693 | n/a | self.menu.menu.SetItemCmd(self.item, cmd) |
|---|
| 694 | n/a | |
|---|
| 695 | n/a | def setmark(self, cmd): |
|---|
| 696 | n/a | self.menu.menu.SetItemMark(self.item, cmd) |
|---|
| 697 | n/a | |
|---|
| 698 | n/a | |
|---|
| 699 | n/a | class RadioItem(MenuItem): |
|---|
| 700 | n/a | def __init__(self, menu, title, shortcut=None, callback=None): |
|---|
| 701 | n/a | MenuItem.__init__(self, menu, title, shortcut, callback, 'radio') |
|---|
| 702 | n/a | |
|---|
| 703 | n/a | class CheckItem(MenuItem): |
|---|
| 704 | n/a | def __init__(self, menu, title, shortcut=None, callback=None): |
|---|
| 705 | n/a | MenuItem.__init__(self, menu, title, shortcut, callback, 'check') |
|---|
| 706 | n/a | |
|---|
| 707 | n/a | def Separator(menu): |
|---|
| 708 | n/a | menu.addseparator() |
|---|
| 709 | n/a | |
|---|
| 710 | n/a | def SubMenu(menu, label, title=''): |
|---|
| 711 | n/a | return menu.addsubmenu(label, title) |
|---|
| 712 | n/a | |
|---|
| 713 | n/a | |
|---|
| 714 | n/a | class AppleMenu(Menu): |
|---|
| 715 | n/a | |
|---|
| 716 | n/a | def __init__(self, bar, abouttext="About me...", aboutcallback=None): |
|---|
| 717 | n/a | Menu.__init__(self, bar, "\024", id=SIOUX_APPLEMENU_ID) |
|---|
| 718 | n/a | if MacOS.runtimemodel == 'ppc': |
|---|
| 719 | n/a | self.additem(abouttext, None, aboutcallback) |
|---|
| 720 | n/a | self.addseparator() |
|---|
| 721 | n/a | self.menu.AppendResMenu('DRVR') |
|---|
| 722 | n/a | else: |
|---|
| 723 | n/a | # Additem()'s tricks do not work for "apple" menu under Carbon |
|---|
| 724 | n/a | self.menu.InsertMenuItem(abouttext, 0) |
|---|
| 725 | n/a | self.items.append((abouttext, None, aboutcallback, None)) |
|---|
| 726 | n/a | |
|---|
| 727 | n/a | def dispatch(self, id, item, window, event): |
|---|
| 728 | n/a | if item == 1: |
|---|
| 729 | n/a | Menu.dispatch(self, id, item, window, event) |
|---|
| 730 | n/a | elif MacOS.runtimemodel == 'ppc': |
|---|
| 731 | n/a | name = self.menu.GetMenuItemText(item) |
|---|
| 732 | n/a | OpenDeskAcc(name) |
|---|
| 733 | n/a | |
|---|
| 734 | n/a | class HelpMenu(Menu): |
|---|
| 735 | n/a | def __init__(self, bar): |
|---|
| 736 | n/a | # Note we don't call Menu.__init__, we do the necessary things by hand |
|---|
| 737 | n/a | self.bar = bar |
|---|
| 738 | n/a | self.menu, index = HMGetHelpMenu() |
|---|
| 739 | n/a | self.id = self.menu.GetMenuID() |
|---|
| 740 | n/a | bar.menus[self.id] = self |
|---|
| 741 | n/a | # The next line caters for the entries the system already handles for us |
|---|
| 742 | n/a | self.items = [None]*(index-1) |
|---|
| 743 | n/a | self._parent = None |
|---|
| 744 | n/a | |
|---|
| 745 | n/a | |
|---|
| 746 | n/a | class Window: |
|---|
| 747 | n/a | """A single window belonging to an application""" |
|---|
| 748 | n/a | |
|---|
| 749 | n/a | def __init__(self, parent): |
|---|
| 750 | n/a | self.wid = None |
|---|
| 751 | n/a | self.parent = parent |
|---|
| 752 | n/a | |
|---|
| 753 | n/a | def open(self, bounds=(40, 40, 400, 400), resid=None): |
|---|
| 754 | n/a | if resid != None: |
|---|
| 755 | n/a | self.wid = GetNewWindow(resid, -1) |
|---|
| 756 | n/a | else: |
|---|
| 757 | n/a | self.wid = NewWindow(bounds, self.__class__.__name__, 1, |
|---|
| 758 | n/a | 8, -1, 1, 0) # changed to proc id 8 to include zoom box. jvr |
|---|
| 759 | n/a | self.do_postopen() |
|---|
| 760 | n/a | |
|---|
| 761 | n/a | def do_postopen(self): |
|---|
| 762 | n/a | """Tell our parent we exist""" |
|---|
| 763 | n/a | self.parent.appendwindow(self.wid, self) |
|---|
| 764 | n/a | |
|---|
| 765 | n/a | def close(self): |
|---|
| 766 | n/a | self.do_postclose() |
|---|
| 767 | n/a | |
|---|
| 768 | n/a | def do_postclose(self): |
|---|
| 769 | n/a | self.parent.removewindow(self.wid) |
|---|
| 770 | n/a | self.parent = None |
|---|
| 771 | n/a | self.wid = None |
|---|
| 772 | n/a | |
|---|
| 773 | n/a | def SetPort(self): |
|---|
| 774 | n/a | # Convinience method |
|---|
| 775 | n/a | SetPort(self.wid) |
|---|
| 776 | n/a | |
|---|
| 777 | n/a | def GetWindow(self): |
|---|
| 778 | n/a | return self.wid |
|---|
| 779 | n/a | |
|---|
| 780 | n/a | def do_inDrag(self, partcode, window, event): |
|---|
| 781 | n/a | where = event[3] |
|---|
| 782 | n/a | window.DragWindow(where, self.draglimit) |
|---|
| 783 | n/a | |
|---|
| 784 | n/a | draglimit = screenbounds |
|---|
| 785 | n/a | |
|---|
| 786 | n/a | def do_inGoAway(self, partcode, window, event): |
|---|
| 787 | n/a | where = event[3] |
|---|
| 788 | n/a | if window.TrackGoAway(where): |
|---|
| 789 | n/a | self.close() |
|---|
| 790 | n/a | |
|---|
| 791 | n/a | def do_inZoom(self, partcode, window, event): |
|---|
| 792 | n/a | (what, message, when, where, modifiers) = event |
|---|
| 793 | n/a | if window.TrackBox(where, partcode): |
|---|
| 794 | n/a | window.ZoomWindow(partcode, 1) |
|---|
| 795 | n/a | rect = window.GetWindowUserState() # so that zoom really works... jvr |
|---|
| 796 | n/a | self.do_postresize(rect[2] - rect[0], rect[3] - rect[1], window) # jvr |
|---|
| 797 | n/a | |
|---|
| 798 | n/a | def do_inZoomIn(self, partcode, window, event): |
|---|
| 799 | n/a | SetPort(window) # !!! |
|---|
| 800 | n/a | self.do_inZoom(partcode, window, event) |
|---|
| 801 | n/a | |
|---|
| 802 | n/a | def do_inZoomOut(self, partcode, window, event): |
|---|
| 803 | n/a | SetPort(window) # !!! |
|---|
| 804 | n/a | self.do_inZoom(partcode, window, event) |
|---|
| 805 | n/a | |
|---|
| 806 | n/a | def do_inGrow(self, partcode, window, event): |
|---|
| 807 | n/a | (what, message, when, where, modifiers) = event |
|---|
| 808 | n/a | result = window.GrowWindow(where, self.growlimit) |
|---|
| 809 | n/a | if result: |
|---|
| 810 | n/a | height = (result>>16) & 0xffff # Hi word |
|---|
| 811 | n/a | width = result & 0xffff # Lo word |
|---|
| 812 | n/a | self.do_resize(width, height, window) |
|---|
| 813 | n/a | |
|---|
| 814 | n/a | growlimit = (50, 50, screenbounds[2] - screenbounds[0], screenbounds[3] - screenbounds[1]) # jvr |
|---|
| 815 | n/a | |
|---|
| 816 | n/a | def do_resize(self, width, height, window): |
|---|
| 817 | n/a | l, t, r, b = self.wid.GetWindowPort().GetPortBounds() # jvr, forGrowIcon |
|---|
| 818 | n/a | self.SetPort() # jvr |
|---|
| 819 | n/a | self.wid.InvalWindowRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr |
|---|
| 820 | n/a | window.SizeWindow(width, height, 1) # changed updateFlag to true jvr |
|---|
| 821 | n/a | self.do_postresize(width, height, window) |
|---|
| 822 | n/a | |
|---|
| 823 | n/a | def do_postresize(self, width, height, window): |
|---|
| 824 | n/a | SetPort(window) |
|---|
| 825 | n/a | self.wid.InvalWindowRect(window.GetWindowPort().GetPortBounds()) |
|---|
| 826 | n/a | |
|---|
| 827 | n/a | def do_inContent(self, partcode, window, event): |
|---|
| 828 | n/a | # |
|---|
| 829 | n/a | # If we're not frontmost, select ourselves and wait for |
|---|
| 830 | n/a | # the activate event. |
|---|
| 831 | n/a | # |
|---|
| 832 | n/a | if MyFrontWindow() != window: |
|---|
| 833 | n/a | window.SelectWindow() |
|---|
| 834 | n/a | return |
|---|
| 835 | n/a | # We are. Handle the event. |
|---|
| 836 | n/a | (what, message, when, where, modifiers) = event |
|---|
| 837 | n/a | SetPort(window) |
|---|
| 838 | n/a | local = GlobalToLocal(where) |
|---|
| 839 | n/a | self.do_contentclick(local, modifiers, event) |
|---|
| 840 | n/a | |
|---|
| 841 | n/a | def do_contentclick(self, local, modifiers, event): |
|---|
| 842 | n/a | if DEBUG: |
|---|
| 843 | n/a | print 'Click in contents at %s, modifiers %s'%(local, modifiers) |
|---|
| 844 | n/a | |
|---|
| 845 | n/a | def do_rawupdate(self, window, event): |
|---|
| 846 | n/a | if DEBUG: print "raw update for", window |
|---|
| 847 | n/a | SetPort(window) |
|---|
| 848 | n/a | window.BeginUpdate() |
|---|
| 849 | n/a | self.do_update(window, event) |
|---|
| 850 | n/a | window.EndUpdate() |
|---|
| 851 | n/a | |
|---|
| 852 | n/a | def do_update(self, window, event): |
|---|
| 853 | n/a | if DEBUG: |
|---|
| 854 | n/a | import time |
|---|
| 855 | n/a | for i in range(8): |
|---|
| 856 | n/a | time.sleep(0.1) |
|---|
| 857 | n/a | InvertRgn(window.GetWindowPort().visRgn) |
|---|
| 858 | n/a | FillRgn(window.GetWindowPort().visRgn, GetQDGlobalsGray()) |
|---|
| 859 | n/a | else: |
|---|
| 860 | n/a | EraseRgn(window.GetWindowPort().visRgn) |
|---|
| 861 | n/a | |
|---|
| 862 | n/a | def do_activate(self, activate, event): |
|---|
| 863 | n/a | if DEBUG: print 'Activate %d for %s'%(activate, self.wid) |
|---|
| 864 | n/a | |
|---|
| 865 | n/a | class ControlsWindow(Window): |
|---|
| 866 | n/a | |
|---|
| 867 | n/a | def do_rawupdate(self, window, event): |
|---|
| 868 | n/a | if DEBUG: print "raw update for", window |
|---|
| 869 | n/a | SetPort(window) |
|---|
| 870 | n/a | window.BeginUpdate() |
|---|
| 871 | n/a | self.do_update(window, event) |
|---|
| 872 | n/a | #DrawControls(window) # jvr |
|---|
| 873 | n/a | UpdateControls(window, window.GetWindowPort().visRgn) # jvr |
|---|
| 874 | n/a | window.DrawGrowIcon() |
|---|
| 875 | n/a | window.EndUpdate() |
|---|
| 876 | n/a | |
|---|
| 877 | n/a | def do_controlhit(self, window, control, pcode, event): |
|---|
| 878 | n/a | if DEBUG: print "control hit in", window, "on", control, "; pcode =", pcode |
|---|
| 879 | n/a | |
|---|
| 880 | n/a | def do_inContent(self, partcode, window, event): |
|---|
| 881 | n/a | if MyFrontWindow() != window: |
|---|
| 882 | n/a | window.SelectWindow() |
|---|
| 883 | n/a | return |
|---|
| 884 | n/a | (what, message, when, where, modifiers) = event |
|---|
| 885 | n/a | SetPort(window) # XXXX Needed? |
|---|
| 886 | n/a | local = GlobalToLocal(where) |
|---|
| 887 | n/a | pcode, control = FindControl(local, window) |
|---|
| 888 | n/a | if pcode and control: |
|---|
| 889 | n/a | self.do_rawcontrolhit(window, control, pcode, local, event) |
|---|
| 890 | n/a | else: |
|---|
| 891 | n/a | if DEBUG: print "FindControl(%s, %s) -> (%s, %s)" % \ |
|---|
| 892 | n/a | (local, window, pcode, control) |
|---|
| 893 | n/a | self.do_contentclick(local, modifiers, event) |
|---|
| 894 | n/a | |
|---|
| 895 | n/a | def do_rawcontrolhit(self, window, control, pcode, local, event): |
|---|
| 896 | n/a | pcode = control.TrackControl(local) |
|---|
| 897 | n/a | if pcode: |
|---|
| 898 | n/a | self.do_controlhit(window, control, pcode, event) |
|---|
| 899 | n/a | |
|---|
| 900 | n/a | class ScrolledWindow(ControlsWindow): |
|---|
| 901 | n/a | def __init__(self, parent): |
|---|
| 902 | n/a | self.barx = self.bary = None |
|---|
| 903 | n/a | self.barx_enabled = self.bary_enabled = 1 |
|---|
| 904 | n/a | self.activated = 1 |
|---|
| 905 | n/a | ControlsWindow.__init__(self, parent) |
|---|
| 906 | n/a | |
|---|
| 907 | n/a | def scrollbars(self, wantx=1, wanty=1): |
|---|
| 908 | n/a | SetPort(self.wid) |
|---|
| 909 | n/a | self.barx = self.bary = None |
|---|
| 910 | n/a | self.barx_enabled = self.bary_enabled = 1 |
|---|
| 911 | n/a | x0, y0, x1, y1 = self.wid.GetWindowPort().GetPortBounds() |
|---|
| 912 | n/a | vx, vy = self.getscrollbarvalues() |
|---|
| 913 | n/a | if vx is None: self.barx_enabled, vx = 0, 0 |
|---|
| 914 | n/a | if vy is None: self.bary_enabled, vy = 0, 0 |
|---|
| 915 | n/a | if wantx: |
|---|
| 916 | n/a | rect = x0-1, y1-(SCROLLBARWIDTH-1), x1-(SCROLLBARWIDTH-2), y1+1 |
|---|
| 917 | n/a | self.barx = NewControl(self.wid, rect, "", 1, vx, 0, 32767, 16, 0) |
|---|
| 918 | n/a | if not self.barx_enabled: self.barx.HiliteControl(255) |
|---|
| 919 | n/a | ## self.wid.InvalWindowRect(rect) |
|---|
| 920 | n/a | if wanty: |
|---|
| 921 | n/a | rect = x1-(SCROLLBARWIDTH-1), y0-1, x1+1, y1-(SCROLLBARWIDTH-2) |
|---|
| 922 | n/a | self.bary = NewControl(self.wid, rect, "", 1, vy, 0, 32767, 16, 0) |
|---|
| 923 | n/a | if not self.bary_enabled: self.bary.HiliteControl(255) |
|---|
| 924 | n/a | ## self.wid.InvalWindowRect(rect) |
|---|
| 925 | n/a | |
|---|
| 926 | n/a | def do_postclose(self): |
|---|
| 927 | n/a | self.barx = self.bary = None |
|---|
| 928 | n/a | ControlsWindow.do_postclose(self) |
|---|
| 929 | n/a | |
|---|
| 930 | n/a | def do_activate(self, onoff, event): |
|---|
| 931 | n/a | self.activated = onoff |
|---|
| 932 | n/a | if onoff: |
|---|
| 933 | n/a | if self.barx and self.barx_enabled: |
|---|
| 934 | n/a | self.barx.ShowControl() # jvr |
|---|
| 935 | n/a | if self.bary and self.bary_enabled: |
|---|
| 936 | n/a | self.bary.ShowControl() # jvr |
|---|
| 937 | n/a | else: |
|---|
| 938 | n/a | if self.barx: |
|---|
| 939 | n/a | self.barx.HideControl() # jvr; An inactive window should have *hidden* |
|---|
| 940 | n/a | # scrollbars, not just dimmed (no matter what |
|---|
| 941 | n/a | # BBEdit does... look at the Finder) |
|---|
| 942 | n/a | if self.bary: |
|---|
| 943 | n/a | self.bary.HideControl() # jvr |
|---|
| 944 | n/a | self.wid.DrawGrowIcon() # jvr |
|---|
| 945 | n/a | |
|---|
| 946 | n/a | def do_postresize(self, width, height, window): |
|---|
| 947 | n/a | l, t, r, b = self.wid.GetWindowPort().GetPortBounds() |
|---|
| 948 | n/a | self.SetPort() |
|---|
| 949 | n/a | if self.barx: |
|---|
| 950 | n/a | self.barx.HideControl() # jvr |
|---|
| 951 | n/a | self.barx.MoveControl(l-1, b-(SCROLLBARWIDTH-1)) |
|---|
| 952 | n/a | self.barx.SizeControl((r-l)-(SCROLLBARWIDTH-3), SCROLLBARWIDTH) # jvr |
|---|
| 953 | n/a | if self.bary: |
|---|
| 954 | n/a | self.bary.HideControl() # jvr |
|---|
| 955 | n/a | self.bary.MoveControl(r-(SCROLLBARWIDTH-1), t-1) |
|---|
| 956 | n/a | self.bary.SizeControl(SCROLLBARWIDTH, (b-t)-(SCROLLBARWIDTH-3)) # jvr |
|---|
| 957 | n/a | if self.barx: |
|---|
| 958 | n/a | self.barx.ShowControl() # jvr |
|---|
| 959 | n/a | self.wid.ValidWindowRect((l, b - SCROLLBARWIDTH + 1, r - SCROLLBARWIDTH + 2, b)) # jvr |
|---|
| 960 | n/a | if self.bary: |
|---|
| 961 | n/a | self.bary.ShowControl() # jvr |
|---|
| 962 | n/a | self.wid.ValidWindowRect((r - SCROLLBARWIDTH + 1, t, r, b - SCROLLBARWIDTH + 2)) # jvr |
|---|
| 963 | n/a | self.wid.InvalWindowRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr, growicon |
|---|
| 964 | n/a | |
|---|
| 965 | n/a | |
|---|
| 966 | n/a | def do_rawcontrolhit(self, window, control, pcode, local, event): |
|---|
| 967 | n/a | if control == self.barx: |
|---|
| 968 | n/a | which = 'x' |
|---|
| 969 | n/a | elif control == self.bary: |
|---|
| 970 | n/a | which = 'y' |
|---|
| 971 | n/a | else: |
|---|
| 972 | n/a | return 0 |
|---|
| 973 | n/a | if pcode in (inUpButton, inDownButton, inPageUp, inPageDown): |
|---|
| 974 | n/a | # We do the work for the buttons and grey area in the tracker |
|---|
| 975 | n/a | dummy = control.TrackControl(local, self.do_controltrack) |
|---|
| 976 | n/a | else: |
|---|
| 977 | n/a | # but the thumb is handled here |
|---|
| 978 | n/a | pcode = control.TrackControl(local) |
|---|
| 979 | n/a | if pcode == inThumb: |
|---|
| 980 | n/a | value = control.GetControlValue() |
|---|
| 981 | n/a | print 'setbars', which, value #DBG |
|---|
| 982 | n/a | self.scrollbar_callback(which, 'set', value) |
|---|
| 983 | n/a | self.updatescrollbars() |
|---|
| 984 | n/a | else: |
|---|
| 985 | n/a | print 'funny part', pcode #DBG |
|---|
| 986 | n/a | return 1 |
|---|
| 987 | n/a | |
|---|
| 988 | n/a | def do_controltrack(self, control, pcode): |
|---|
| 989 | n/a | if control == self.barx: |
|---|
| 990 | n/a | which = 'x' |
|---|
| 991 | n/a | elif control == self.bary: |
|---|
| 992 | n/a | which = 'y' |
|---|
| 993 | n/a | else: |
|---|
| 994 | n/a | return |
|---|
| 995 | n/a | |
|---|
| 996 | n/a | if pcode == inUpButton: |
|---|
| 997 | n/a | what = '-' |
|---|
| 998 | n/a | elif pcode == inDownButton: |
|---|
| 999 | n/a | what = '+' |
|---|
| 1000 | n/a | elif pcode == inPageUp: |
|---|
| 1001 | n/a | what = '--' |
|---|
| 1002 | n/a | elif pcode == inPageDown: |
|---|
| 1003 | n/a | what = '++' |
|---|
| 1004 | n/a | else: |
|---|
| 1005 | n/a | return |
|---|
| 1006 | n/a | self.scrollbar_callback(which, what, None) |
|---|
| 1007 | n/a | self.updatescrollbars() |
|---|
| 1008 | n/a | |
|---|
| 1009 | n/a | def updatescrollbars(self): |
|---|
| 1010 | n/a | SetPort(self.wid) |
|---|
| 1011 | n/a | vx, vy = self.getscrollbarvalues() |
|---|
| 1012 | n/a | if self.barx: |
|---|
| 1013 | n/a | if vx is None: |
|---|
| 1014 | n/a | self.barx.HiliteControl(255) |
|---|
| 1015 | n/a | self.barx_enabled = 0 |
|---|
| 1016 | n/a | else: |
|---|
| 1017 | n/a | if not self.barx_enabled: |
|---|
| 1018 | n/a | self.barx_enabled = 1 |
|---|
| 1019 | n/a | if self.activated: |
|---|
| 1020 | n/a | self.barx.HiliteControl(0) |
|---|
| 1021 | n/a | self.barx.SetControlValue(vx) |
|---|
| 1022 | n/a | if self.bary: |
|---|
| 1023 | n/a | if vy is None: |
|---|
| 1024 | n/a | self.bary.HiliteControl(255) |
|---|
| 1025 | n/a | self.bary_enabled = 0 |
|---|
| 1026 | n/a | else: |
|---|
| 1027 | n/a | if not self.bary_enabled: |
|---|
| 1028 | n/a | self.bary_enabled = 1 |
|---|
| 1029 | n/a | if self.activated: |
|---|
| 1030 | n/a | self.bary.HiliteControl(0) |
|---|
| 1031 | n/a | self.bary.SetControlValue(vy) |
|---|
| 1032 | n/a | |
|---|
| 1033 | n/a | # Auxiliary function: convert standard text/image/etc coordinate |
|---|
| 1034 | n/a | # to something palatable as getscrollbarvalues() return |
|---|
| 1035 | n/a | def scalebarvalue(self, absmin, absmax, curmin, curmax): |
|---|
| 1036 | n/a | if curmin <= absmin and curmax >= absmax: |
|---|
| 1037 | n/a | return None |
|---|
| 1038 | n/a | if curmin <= absmin: |
|---|
| 1039 | n/a | return 0 |
|---|
| 1040 | n/a | if curmax >= absmax: |
|---|
| 1041 | n/a | return 32767 |
|---|
| 1042 | n/a | perc = float(curmin-absmin)/float(absmax-absmin) |
|---|
| 1043 | n/a | return int(perc*32767) |
|---|
| 1044 | n/a | |
|---|
| 1045 | n/a | # To be overridden: |
|---|
| 1046 | n/a | |
|---|
| 1047 | n/a | def getscrollbarvalues(self): |
|---|
| 1048 | n/a | return 0, 0 |
|---|
| 1049 | n/a | |
|---|
| 1050 | n/a | def scrollbar_callback(self, which, what, value): |
|---|
| 1051 | n/a | print 'scroll', which, what, value |
|---|
| 1052 | n/a | |
|---|
| 1053 | n/a | class DialogWindow(Window): |
|---|
| 1054 | n/a | """A modeless dialog window""" |
|---|
| 1055 | n/a | |
|---|
| 1056 | n/a | def open(self, resid): |
|---|
| 1057 | n/a | self.dlg = GetNewDialog(resid, -1) |
|---|
| 1058 | n/a | self.wid = self.dlg.GetDialogWindow() |
|---|
| 1059 | n/a | self.do_postopen() |
|---|
| 1060 | n/a | |
|---|
| 1061 | n/a | def close(self): |
|---|
| 1062 | n/a | self.do_postclose() |
|---|
| 1063 | n/a | |
|---|
| 1064 | n/a | def do_postclose(self): |
|---|
| 1065 | n/a | self.dlg = None |
|---|
| 1066 | n/a | Window.do_postclose(self) |
|---|
| 1067 | n/a | |
|---|
| 1068 | n/a | def do_itemhit(self, item, event): |
|---|
| 1069 | n/a | print 'Dialog %s, item %d hit'%(self.dlg, item) |
|---|
| 1070 | n/a | |
|---|
| 1071 | n/a | def do_rawupdate(self, window, event): |
|---|
| 1072 | n/a | pass |
|---|
| 1073 | n/a | |
|---|
| 1074 | n/a | def ostypecode(x): |
|---|
| 1075 | n/a | "Convert a long int to the 4-character code it really is" |
|---|
| 1076 | n/a | s = '' |
|---|
| 1077 | n/a | for i in range(4): |
|---|
| 1078 | n/a | x, c = divmod(x, 256) |
|---|
| 1079 | n/a | s = chr(c) + s |
|---|
| 1080 | n/a | return s |
|---|
| 1081 | n/a | |
|---|
| 1082 | n/a | |
|---|
| 1083 | n/a | class TestApp(Application): |
|---|
| 1084 | n/a | |
|---|
| 1085 | n/a | "This class is used by the test() function" |
|---|
| 1086 | n/a | |
|---|
| 1087 | n/a | def makeusermenus(self): |
|---|
| 1088 | n/a | self.filemenu = m = Menu(self.menubar, "File") |
|---|
| 1089 | n/a | self.saveitem = MenuItem(m, "Save", "S", self.save) |
|---|
| 1090 | n/a | Separator(m) |
|---|
| 1091 | n/a | self.optionsmenu = mm = SubMenu(m, "Options") |
|---|
| 1092 | n/a | self.opt1 = CheckItem(mm, "Arguments", "A") |
|---|
| 1093 | n/a | self.opt2 = CheckItem(mm, "Being hit on the head lessons", (kMenuOptionModifier, "A")) |
|---|
| 1094 | n/a | self.opt3 = CheckItem(mm, "Complaints", (kMenuOptionModifier|kMenuNoCommandModifier, "A")) |
|---|
| 1095 | n/a | Separator(m) |
|---|
| 1096 | n/a | self.itemeh = MenuItem(m, "Enable Help", None, self.enablehelp) |
|---|
| 1097 | n/a | self.itemdbg = MenuItem(m, "Debug", None, self.debug) |
|---|
| 1098 | n/a | Separator(m) |
|---|
| 1099 | n/a | self.quititem = MenuItem(m, "Quit", "Q", self.quit) |
|---|
| 1100 | n/a | |
|---|
| 1101 | n/a | def save(self, *args): |
|---|
| 1102 | n/a | print "Save" |
|---|
| 1103 | n/a | |
|---|
| 1104 | n/a | def quit(self, *args): |
|---|
| 1105 | n/a | raise self |
|---|
| 1106 | n/a | |
|---|
| 1107 | n/a | def enablehelp(self, *args): |
|---|
| 1108 | n/a | hm = self.gethelpmenu() |
|---|
| 1109 | n/a | self.nohelpitem = MenuItem(hm, "There isn't any", None, self.nohelp) |
|---|
| 1110 | n/a | |
|---|
| 1111 | n/a | def nohelp(self, *args): |
|---|
| 1112 | n/a | print "I told you there isn't any!" |
|---|
| 1113 | n/a | |
|---|
| 1114 | n/a | def debug(self, *args): |
|---|
| 1115 | n/a | import pdb |
|---|
| 1116 | n/a | pdb.set_trace() |
|---|
| 1117 | n/a | |
|---|
| 1118 | n/a | |
|---|
| 1119 | n/a | def test(): |
|---|
| 1120 | n/a | "Test program" |
|---|
| 1121 | n/a | app = TestApp() |
|---|
| 1122 | n/a | app.mainloop() |
|---|
| 1123 | n/a | |
|---|
| 1124 | n/a | |
|---|
| 1125 | n/a | if __name__ == '__main__': |
|---|
| 1126 | n/a | test() |
|---|