Another batch of updates...
[python/dscho.git] / Mac / Lib / FrameWork.py
blob8663d1f699d522d3d4ce85186f24de3168d17c26
1 "A sort of application framework for the Mac"
3 DEBUG=0
5 import MacOS
6 import traceback
8 from AE import *
9 from AppleEvents import *
10 from Ctl import *
11 from Controls import *
12 from Dlg import *
13 from Dialogs import *
14 from Evt import *
15 from Events import *
16 from Menu import *
17 from Menus import *
18 from Qd import *
19 from QuickDraw import *
20 #from Res import *
21 #from Resources import *
22 #from Snd import *
23 #from Sound import *
24 from Win import *
25 from Windows import *
27 import EasyDialogs
29 kHighLevelEvent = 23 # Don't know what header file this should come from
30 SCROLLBARWIDTH = 16 # Again, not a clue...
33 # Map event 'what' field to strings
34 eventname = {}
35 eventname[1] = 'mouseDown'
36 eventname[2] = 'mouseUp'
37 eventname[3] = 'keyDown'
38 eventname[4] = 'keyUp'
39 eventname[5] = 'autoKey'
40 eventname[6] = 'updateEvt'
41 eventname[7] = 'diskEvt'
42 eventname[8] = 'activateEvt'
43 eventname[15] = 'osEvt'
44 eventname[23] = 'kHighLevelEvent'
46 # Map part codes returned by WhichWindow() to strings
47 partname = {}
48 partname[0] = 'inDesk'
49 partname[1] = 'inMenuBar'
50 partname[2] = 'inSysWindow'
51 partname[3] = 'inContent'
52 partname[4] = 'inDrag'
53 partname[5] = 'inGrow'
54 partname[6] = 'inGoAway'
55 partname[7] = 'inZoomIn'
56 partname[8] = 'inZoomOut'
59 # The useable portion of the screen
60 # ## but what happens with multiple screens? jvr
61 screenbounds = qd.screenBits.bounds
62 screenbounds = screenbounds[0]+4, screenbounds[1]+4, \
63 screenbounds[2]-4, screenbounds[3]-4
65 next_window_x = 16 # jvr
66 next_window_y = 44 # jvr
68 def windowbounds(width, height):
69 "Return sensible window bounds"
70 global next_window_x, next_window_y
71 r, b = next_window_x+width, next_window_y+height
72 if r > screenbounds[2]:
73 next_window_x = 16
74 if b > screenbounds[3]:
75 next_window_y = 44
76 l, t = next_window_x, next_window_y
77 r, b = next_window_x+width, next_window_y+height
78 next_window_x, next_window_y = next_window_x + 8, next_window_y + 20 # jvr
79 return l, t, r, b
82 class Application:
84 "Application framework -- your application should be a derived class"
86 def __init__(self):
87 self._windows = {}
88 self.makemenubar()
90 def makemenubar(self):
91 self.menubar = MenuBar()
92 AppleMenu(self.menubar, self.getabouttext(), self.do_about)
93 self.makeusermenus()
95 def makeusermenus(self):
96 self.filemenu = m = Menu(self.menubar, "File")
97 self._quititem = MenuItem(m, "Quit", "Q", self._quit)
99 def _quit(self, *args):
100 raise self
102 def appendwindow(self, wid, window):
103 self._windows[wid] = window
105 def removewindow(self, wid):
106 del self._windows[wid]
108 def getabouttext(self):
109 return "About %s..." % self.__class__.__name__
111 def do_about(self, id, item, window, event):
112 EasyDialogs.Message("Hello, world!" + "\015(%s)" % self.__class__.__name__)
114 # The main event loop is broken up in several simple steps.
115 # This is done so you can override each individual part,
116 # if you have a need to do extra processing independent of the
117 # event type.
118 # Normally, however, you'd just define handlers for individual
119 # events.
120 # (XXX I'm not sure if using default parameter values is the right
121 # way to define the mask and wait time passed to WaitNextEvent.)
123 def mainloop(self, mask = everyEvent, wait = 0):
124 saveyield = MacOS.EnableAppswitch(self.yield)
125 try:
126 while 1:
127 try:
128 self.do1event(mask, wait)
129 except (Application, SystemExit):
130 break
131 finally:
132 MacOS.EnableAppswitch(saveyield)
134 yield = -1
136 def do1event(self, mask = everyEvent, wait = 0):
137 ok, event = self.getevent(mask, wait)
138 if IsDialogEvent(event):
139 if self.do_dialogevent(event):
140 return
141 if ok:
142 self.dispatch(event)
143 else:
144 self.idle(event)
146 def idle(self, event):
147 pass
149 def getevent(self, mask = everyEvent, wait = 0):
150 ok, event = WaitNextEvent(mask, wait)
151 return ok, event
153 def dispatch(self, event):
154 (what, message, when, where, modifiers) = event
155 if eventname.has_key(what):
156 name = "do_" + eventname[what]
157 else:
158 name = "do_%d" % what
159 try:
160 handler = getattr(self, name)
161 except AttributeError:
162 handler = self.do_unknownevent
163 handler(event)
165 def do_dialogevent(self, event):
166 gotone, window, item = DialogSelect(event)
167 if gotone:
168 if self._windows.has_key(window):
169 self._windows[window].do_itemhit(item, event)
170 else:
171 print 'Dialog event for unknown dialog'
172 return 1
173 return 0
175 def do_mouseDown(self, event):
176 (what, message, when, where, modifiers) = event
177 partcode, wid = FindWindow(where)
180 # Find the correct name.
182 if partname.has_key(partcode):
183 name = "do_" + partname[partcode]
184 else:
185 name = "do_%d" % partcode
187 if wid == None:
188 # No window, or a non-python window
189 try:
190 handler = getattr(self, name)
191 except AttributeError:
192 # Not menubar or something, so assume someone
193 # else's window
194 MacOS.HandleEvent(event)
195 return
196 elif self._windows.has_key(wid):
197 # It is a window. Hand off to correct window.
198 window = self._windows[wid]
199 try:
200 handler = getattr(window, name)
201 except AttributeError:
202 handler = self.do_unknownpartcode
203 else:
204 # It is a python-toolbox window, but not ours.
205 handler = self.do_unknownwindow
206 handler(partcode, wid, event)
208 def do_inSysWindow(self, partcode, window, event):
209 MacOS.HandleEvent(event)
211 def do_inDesk(self, partcode, window, event):
212 MacOS.HandleEvent(event)
214 def do_inMenuBar(self, partcode, window, event):
215 (what, message, when, where, modifiers) = event
216 result = MenuSelect(where)
217 id = (result>>16) & 0xffff # Hi word
218 item = result & 0xffff # Lo word
219 self.do_rawmenu(id, item, window, event)
221 def do_rawmenu(self, id, item, window, event):
222 try:
223 self.do_menu(id, item, window, event)
224 finally:
225 HiliteMenu(0)
227 def do_menu(self, id, item, window, event):
228 self.menubar.dispatch(id, item, window, event)
231 def do_unknownpartcode(self, partcode, window, event):
232 (what, message, when, where, modifiers) = event
233 if DEBUG: print "Mouse down at global:", where
234 if DEBUG: print "\tUnknown part code:", partcode
235 if DEBUG: print "\tEvent:", self.printevent(event)
236 MacOS.HandleEvent(event)
238 def do_unknownwindow(self, partcode, window, event):
239 if DEBUG: print 'Unknown window:', window
240 MacOS.HandleEvent(event)
242 def do_keyDown(self, event):
243 self.do_key(event)
245 def do_autoKey(self, event):
246 if not event[-1] & cmdKey:
247 self.do_key(event)
249 def do_key(self, event):
250 (what, message, when, where, modifiers) = event
251 c = chr(message & charCodeMask)
252 if modifiers & cmdKey:
253 if c == '.':
254 raise self
255 else:
256 result = MenuKey(ord(c))
257 id = (result>>16) & 0xffff # Hi word
258 item = result & 0xffff # Lo word
259 if id:
260 self.do_rawmenu(id, item, None, event)
261 # elif c == 'w':
262 # w = FrontWindow()
263 # if w:
264 # self.do_close(w)
265 # else:
266 # if DEBUG: print 'Command-W without front window'
267 else:
268 if DEBUG: print "Command-" +`c`
269 else:
270 # See whether the front window wants it
271 w = FrontWindow()
272 if w and self._windows.has_key(w):
273 window = self._windows[w]
274 try:
275 do_char = window.do_char
276 except AttributeError:
277 do_char = self.do_char
278 do_char(c, event)
279 # else it wasn't for us, sigh...
281 def do_char(self, c, event):
282 if DEBUG: print "Character", `c`
284 def do_updateEvt(self, event):
285 (what, message, when, where, modifiers) = event
286 wid = WhichWindow(message)
287 if wid and self._windows.has_key(wid):
288 window = self._windows[wid]
289 window.do_rawupdate(wid, event)
290 else:
291 MacOS.HandleEvent(event)
293 def do_activateEvt(self, event):
294 (what, message, when, where, modifiers) = event
295 # XXXX Incorrect, should be fixed in suspendresume
296 if type(message) == type(1):
297 wid = WhichWindow(message)
298 else:
299 wid = message
300 if wid and self._windows.has_key(wid):
301 window = self._windows[wid]
302 window.do_activate(modifiers & 1, event)
303 else:
304 MacOS.HandleEvent(event)
306 def do_osEvt(self, event):
307 (what, message, when, where, modifiers) = event
308 which = (message >> 24) & 0xff
309 if which == 1: # suspend/resume
310 self.do_suspendresume(event)
311 else:
312 if DEBUG:
313 print 'unknown osEvt:',
314 self.printevent(event)
316 def do_suspendresume(self, event):
317 # Is this a good idea???
318 (what, message, when, where, modifiers) = event
319 w = FrontWindow()
320 if w:
321 # XXXX Incorrect, should stuff windowptr into message field
322 nev = (activateEvt, w, when, where, message&1)
323 self.do_activateEvt(nev)
325 def do_kHighLevelEvent(self, event):
326 (what, message, when, where, modifiers) = event
327 if DEBUG:
328 print "High Level Event:",
329 self.printevent(event)
330 try:
331 AEProcessAppleEvent(event)
332 except:
333 print "AEProcessAppleEvent error:"
334 traceback.print_exc()
336 def do_unknownevent(self, event):
337 if DEBUG:
338 print "Unhandled event:",
339 self.printevent(event)
341 def printevent(self, event):
342 (what, message, when, where, modifiers) = event
343 nicewhat = `what`
344 if eventname.has_key(what):
345 nicewhat = eventname[what]
346 print nicewhat,
347 if what == kHighLevelEvent:
348 h, v = where
349 print `ostypecode(message)`, hex(when), `ostypecode(h | (v<<16))`,
350 else:
351 print hex(message), hex(when), where,
352 print hex(modifiers)
355 class MenuBar:
356 """Represent a set of menus in a menu bar.
358 Interface:
360 - (constructor)
361 - (destructor)
362 - addmenu
363 - addpopup (normally used internally)
364 - dispatch (called from Application)
367 nextid = 1 # Necessarily a class variable
369 def getnextid(self):
370 id = self.nextid
371 self.nextid = id+1
372 return id
374 def __init__(self):
375 ClearMenuBar()
376 self.bar = GetMenuBar()
377 self.menus = {}
379 def addmenu(self, title, after = 0):
380 id = self.getnextid()
381 if DEBUG: print 'Newmenu', title, id # XXXX
382 m = NewMenu(id, title)
383 m.InsertMenu(after)
384 DrawMenuBar() # XXX appears slow! better do this when we're done. jvr
385 return id, m
387 def delmenu(self, id):
388 if DEBUG: print 'Delmenu', id # XXXX
389 DeleteMenu(id)
391 def addpopup(self, title = ''):
392 return self.addmenu(title, -1)
394 def install(self):
395 self.bar.SetMenuBar()
396 DrawMenuBar()
398 def dispatch(self, id, item, window, event):
399 if self.menus.has_key(id):
400 self.menus[id].dispatch(id, item, window, event)
401 else:
402 if DEBUG: print "MenuBar.dispatch(%d, %d, %s, %s)" % \
403 (id, item, window, event)
406 # XXX Need a way to get menus as resources and bind them to callbacks
408 class Menu:
409 "One menu."
411 def __init__(self, bar, title, after=0):
412 self.bar = bar
413 self.id, self.menu = self.bar.addmenu(title, after)
414 bar.menus[self.id] = self
415 self.items = []
417 def delete(self):
418 self.bar.delmenu(self.id)
419 del self.bar.menus[self.id]
420 del self.bar
421 del self.items
422 del self.menu
423 del self.id
425 def additem(self, label, shortcut=None, callback=None, kind=None):
426 self.menu.AppendMenu('x') # add a dummy string
427 self.items.append(label, shortcut, callback, kind)
428 item = len(self.items)
429 self.menu.SetMenuItemText(item, label) # set the actual text
430 if shortcut:
431 self.menu.SetItemCmd(item, ord(shortcut))
432 return item
434 def addcheck(self, label, shortcut=None, callback=None):
435 return self.additem(label, shortcut, callback, 'check')
437 def addradio(self, label, shortcut=None, callback=None):
438 return self.additem(label, shortcut, callback, 'radio')
440 def addseparator(self):
441 self.menu.AppendMenu('(-')
442 self.items.append('', None, None, 'separator')
444 def addsubmenu(self, label, title=''):
445 sub = Menu(self.bar, title, -1)
446 item = self.additem(label, '\x1B', None, 'submenu')
447 self.menu.SetItemMark(item, sub.id)
448 return sub
450 def dispatch(self, id, item, window, event):
451 title, shortcut, callback, type = self.items[item-1]
452 if callback:
453 callback(id, item, window, event)
455 def enable(self, onoff):
456 if onoff:
457 self.menu.EnableItem(0)
458 else:
459 self.menu.DisableItem(0)
461 class MenuItem:
462 def __init__(self, menu, title, shortcut=None, callback=None, kind=None):
463 self.item = menu.additem(title, shortcut, callback)
464 self.menu = menu
466 def check(self, onoff):
467 self.menu.menu.CheckItem(self.item, onoff)
469 def enable(self, onoff):
470 if onoff:
471 self.menu.menu.EnableItem(self.item)
472 else:
473 self.menu.menu.DisableItem(self.item)
475 def settext(self, text):
476 self.menu.menu.SetMenuItemText(self.item, text)
478 def setstyle(self, style):
479 self.menu.menu.SetItemStyle(self.item, style)
481 def seticon(self, icon):
482 self.menu.menu.SetItemIcon(self.item, icon)
484 def setcmd(self, cmd):
485 self.menu.menu.SetItemCmd(self.item, cmd)
487 def setmark(self, cmd):
488 self.menu.menu.SetItemMark(self.item, cmd)
491 class RadioItem(MenuItem):
492 def __init__(self, menu, title, shortcut=None, callback=None):
493 MenuItem.__init__(self, menu, title, shortcut, callback, 'radio')
495 class CheckItem(MenuItem):
496 def __init__(self, menu, title, shortcut=None, callback=None):
497 MenuItem.__init__(self, menu, title, shortcut, callback, 'check')
499 def Separator(menu):
500 menu.addseparator()
502 def SubMenu(menu, label, title=''):
503 return menu.addsubmenu(label, title)
506 class AppleMenu(Menu):
508 def __init__(self, bar, abouttext="About me...", aboutcallback=None):
509 Menu.__init__(self, bar, "\024")
510 self.additem(abouttext, None, aboutcallback)
511 self.addseparator()
512 self.menu.AppendResMenu('DRVR')
514 def dispatch(self, id, item, window, event):
515 if item == 1:
516 Menu.dispatch(self, id, item, window, event)
517 else:
518 name = self.menu.GetMenuItemText(item)
519 OpenDeskAcc(name)
521 class Window:
522 """A single window belonging to an application"""
524 def __init__(self, parent):
525 self.wid = None
526 self.parent = parent
528 def open(self, bounds=(40, 40, 400, 400), resid=None):
529 if resid <> None:
530 self.wid = GetNewWindow(resid, -1)
531 else:
532 self.wid = NewWindow(bounds, self.__class__.__name__, 1,
533 8, -1, 1, 0) # changed to proc id 8 to include zoom box. jvr
534 self.do_postopen()
536 def do_postopen(self):
537 """Tell our parent we exist"""
538 self.parent.appendwindow(self.wid, self)
540 def close(self):
541 self.do_postclose()
543 def do_postclose(self):
544 self.parent.removewindow(self.wid)
545 self.parent = None
546 self.wid = None
548 def SetPort(self):
549 # Convinience method
550 SetPort(self.wid)
552 def do_inDrag(self, partcode, window, event):
553 where = event[3]
554 window.DragWindow(where, self.draglimit)
556 draglimit = screenbounds
558 def do_inGoAway(self, partcode, window, event):
559 where = event[3]
560 if window.TrackGoAway(where):
561 self.close()
563 def do_inZoom(self, partcode, window, event):
564 (what, message, when, where, modifiers) = event
565 if window.TrackBox(where, partcode):
566 window.ZoomWindow(partcode, 1)
567 rect = window.GetWindowUserState() # so that zoom really works... jvr
568 self.do_postresize(rect[2] - rect[0], rect[3] - rect[1], window) # jvr
570 def do_inZoomIn(self, partcode, window, event):
571 SetPort(window) # !!!
572 self.do_inZoom(partcode, window, event)
574 def do_inZoomOut(self, partcode, window, event):
575 SetPort(window) # !!!
576 self.do_inZoom(partcode, window, event)
578 def do_inGrow(self, partcode, window, event):
579 (what, message, when, where, modifiers) = event
580 result = window.GrowWindow(where, self.growlimit)
581 if result:
582 height = (result>>16) & 0xffff # Hi word
583 width = result & 0xffff # Lo word
584 self.do_resize(width, height, window)
586 growlimit = (50, 50, screenbounds[2] - screenbounds[0], screenbounds[3] - screenbounds[1]) # jvr
588 def do_resize(self, width, height, window):
589 l, t, r, b = self.wid.GetWindowPort().portRect # jvr, forGrowIcon
590 self.SetPort() # jvr
591 InvalRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr
592 window.SizeWindow(width, height, 1) # changed updateFlag to true jvr
593 self.do_postresize(width, height, window)
595 def do_postresize(self, width, height, window):
596 SetPort(window)
597 InvalRect(window.GetWindowPort().portRect)
599 def do_inContent(self, partcode, window, event):
601 # If we're not frontmost, select ourselves and wait for
602 # the activate event.
604 if FrontWindow() <> window:
605 window.SelectWindow()
606 return
607 # We are. Handle the event.
608 (what, message, when, where, modifiers) = event
609 SetPort(window)
610 local = GlobalToLocal(where)
611 self.do_contentclick(local, modifiers, event)
613 def do_contentclick(self, local, modifiers, event):
614 if DEBUG:
615 print 'Click in contents at %s, modifiers %s'%(local, modifiers)
617 def do_rawupdate(self, window, event):
618 if DEBUG: print "raw update for", window
619 SetPort(window)
620 window.BeginUpdate()
621 self.do_update(window, event)
622 window.EndUpdate()
624 def do_update(self, window, event):
625 if DEBUG:
626 import time
627 for i in range(8):
628 time.sleep(0.1)
629 InvertRgn(window.GetWindowPort().visRgn)
630 FillRgn(window.GetWindowPort().visRgn, qd.gray)
631 else:
632 EraseRgn(window.GetWindowPort().visRgn)
634 def do_activate(self, activate, event):
635 if DEBUG: print 'Activate %d for %s'%(activate, self.wid)
637 class ControlsWindow(Window):
639 def do_rawupdate(self, window, event):
640 if DEBUG: print "raw update for", window
641 SetPort(window)
642 window.BeginUpdate()
643 self.do_update(window, event)
644 #DrawControls(window) # jvr
645 UpdateControls(window, window.GetWindowPort().visRgn) # jvr
646 window.DrawGrowIcon()
647 window.EndUpdate()
649 def do_controlhit(self, window, control, pcode, event):
650 if DEBUG: print "control hit in", window, "on", control, "; pcode =", pcode
652 def do_inContent(self, partcode, window, event):
653 if FrontWindow() <> window:
654 window.SelectWindow()
655 return
656 (what, message, when, where, modifiers) = event
657 SetPort(window) # XXXX Needed?
658 local = GlobalToLocal(where)
659 ctltype, control = FindControl(local, window)
660 if ctltype and control:
661 pcode = control.TrackControl(local)
662 if pcode:
663 self.do_controlhit(window, control, pcode, event)
664 else:
665 if DEBUG: print "FindControl(%s, %s) -> (%s, %s)" % \
666 (local, window, ctltype, control)
667 self.do_contentclick(local, modifiers, event)
669 class ScrolledWindow(ControlsWindow):
670 def __init__(self, parent):
671 self.barx = self.bary = None
672 self.barx_enabled = self.bary_enabled = 1
673 self.activated = 1
674 ControlsWindow.__init__(self, parent)
676 def scrollbars(self, wantx=1, wanty=1):
677 SetPort(self.wid)
678 self.barx = self.bary = None
679 self.barx_enabled = self.bary_enabled = 1
680 x0, y0, x1, y1 = self.wid.GetWindowPort().portRect
681 vx, vy = self.getscrollbarvalues()
682 if vx == None: self.barx_enabled, vx = 0, 0
683 if vy == None: self.bary_enabled, vy = 0, 0
684 if wantx:
685 rect = x0-1, y1-(SCROLLBARWIDTH-1), x1-(SCROLLBARWIDTH-2), y1+1
686 self.barx = NewControl(self.wid, rect, "", 1, vx, 0, 32767, 16, 0)
687 if not self.barx_enabled: self.barx.HiliteControl(255)
688 ## InvalRect(rect)
689 if wanty:
690 rect = x1-(SCROLLBARWIDTH-1), y0-1, x1+1, y1-(SCROLLBARWIDTH-2)
691 self.bary = NewControl(self.wid, rect, "", 1, vy, 0, 32767, 16, 0)
692 if not self.bary_enabled: self.bary.HiliteControl(255)
693 ## InvalRect(rect)
695 def do_postclose(self):
696 self.barx = self.bary = None
697 ControlsWindow.do_postclose(self)
699 def do_activate(self, onoff, event):
700 self.activated = onoff
701 if onoff:
702 if self.barx and self.barx_enabled:
703 self.barx.ShowControl() # jvr
704 if self.bary and self.bary_enabled:
705 self.bary.ShowControl() # jvr
706 else:
707 if self.barx:
708 self.barx.HideControl() # jvr; An inactive window should have *hidden*
709 # scrollbars, not just dimmed (no matter what
710 # BBEdit does... look at the Finder)
711 if self.bary:
712 self.bary.HideControl() # jvr
713 self.wid.DrawGrowIcon() # jvr
715 def do_postresize(self, width, height, window):
716 l, t, r, b = self.wid.GetWindowPort().portRect
717 self.SetPort()
718 if self.barx:
719 self.barx.HideControl() # jvr
720 self.barx.MoveControl(l-1, b-(SCROLLBARWIDTH-1))
721 self.barx.SizeControl((r-l)-(SCROLLBARWIDTH-3), SCROLLBARWIDTH) # jvr
722 if self.bary:
723 self.bary.HideControl() # jvr
724 self.bary.MoveControl(r-(SCROLLBARWIDTH-1), t-1)
725 self.bary.SizeControl(SCROLLBARWIDTH, (b-t)-(SCROLLBARWIDTH-3)) # jvr
726 if self.barx:
727 self.barx.ShowControl() # jvr
728 ValidRect((l, b - SCROLLBARWIDTH + 1, r - SCROLLBARWIDTH + 2, b)) # jvr
729 if self.bary:
730 self.bary.ShowControl() # jvr
731 ValidRect((r - SCROLLBARWIDTH + 1, t, r, b - SCROLLBARWIDTH + 2)) # jvr
732 InvalRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr, growicon
734 def do_controlhit(self, window, control, pcode, event):
735 if control == self.barx:
736 bar = self.barx
737 which = 'x'
738 elif control == self.bary:
739 bar = self.bary
740 which = 'y'
741 else:
742 return 0
743 value = None
744 if pcode == inUpButton:
745 what = '-'
746 elif pcode == inDownButton:
747 what = '+'
748 elif pcode == inPageUp:
749 what = '--'
750 elif pcode == inPageDown:
751 what = '++'
752 else:
753 what = 'set'
754 value = bar.GetControlValue()
755 self.scrollbar_callback(which, what, value)
756 self.updatescrollbars()
757 return 1
759 def updatescrollbars(self):
760 SetPort(self.wid)
761 vx, vy = self.getscrollbarvalues()
762 if self.barx:
763 if vx == None:
764 self.barx.HiliteControl(255)
765 self.barx_enabled = 0
766 else:
767 if not self.barx_enabled:
768 self.barx_enabled = 1
769 if self.activated:
770 self.barx.HiliteControl(0)
771 self.barx.SetControlValue(vx)
772 if self.bary:
773 if vy == None:
774 self.bary.HiliteControl(255)
775 self.bary_enabled = 0
776 else:
777 if not self.bary_enabled:
778 self.bary_enabled = 1
779 if self.activated:
780 self.bary.HiliteControl(0)
781 self.bary.SetControlValue(vy)
783 # Auxiliary function: convert standard text/image/etc coordinate
784 # to something palatable as getscrollbarvalues() return
785 def scalebarvalue(self, absmin, absmax, curmin, curmax):
786 if curmin <= absmin and curmax >= absmax:
787 return None
788 if curmin <= absmin:
789 return 0
790 if curmax >= absmax:
791 return 32767
792 perc = float(curmin-absmin)/float(absmax-absmin)
793 return int(perc*32767)
795 # To be overridden:
797 def getscrollbarvalues(self):
798 return 0, 0
800 def scrollbar_callback(self, which, what, value):
801 print 'scroll', which, what, value
803 class DialogWindow(Window):
804 """A modeless dialog window"""
806 def open(self, resid):
807 self.wid = GetNewDialog(resid, -1)
808 self.do_postopen()
810 def close(self):
811 self.do_postclose()
813 def do_itemhit(self, item, event):
814 print 'Dialog %s, item %d hit'%(self.wid, item)
816 def do_rawupdate(self, window, event):
817 pass
819 def ostypecode(x):
820 "Convert a long int to the 4-character code it really is"
821 s = ''
822 for i in range(4):
823 x, c = divmod(x, 256)
824 s = chr(c) + s
825 return s
828 class TestApp(Application):
830 "This class is used by the test() function"
832 def makeusermenus(self):
833 self.filemenu = m = Menu(self.menubar, "File")
834 self.saveitem = MenuItem(m, "Save", "S", self.save)
835 Separator(m)
836 self.optionsmenu = mm = SubMenu(m, "Options")
837 self.opt1 = CheckItem(mm, "Arguments")
838 self.opt2 = CheckItem(mm, "Being hit on the head lessons")
839 self.opt3 = CheckItem(mm, "Complaints")
840 Separator(m)
841 self.quititem = MenuItem(m, "Quit", "Q", self.quit)
843 def save(self, *args):
844 print "Save"
846 def quit(self, *args):
847 raise self
850 def test():
851 "Test program"
852 app = TestApp()
853 app.mainloop()
856 if __name__ == '__main__':
857 test()