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