1 "A sort of application framework for the Mac"
9 from AppleEvents
import *
11 from Controls
import *
19 from QuickDraw
import *
21 #from Resources import *
30 kHighLevelEvent
= 23 # Don't know what header file this should come from
31 SCROLLBARWIDTH
= 16 # Again, not a clue...
34 # Map event 'what' field to strings
36 eventname
[1] = 'mouseDown'
37 eventname
[2] = 'mouseUp'
38 eventname
[3] = 'keyDown'
39 eventname
[4] = 'keyUp'
40 eventname
[5] = 'autoKey'
41 eventname
[6] = 'updateEvt'
42 eventname
[7] = 'diskEvt'
43 eventname
[8] = 'activateEvt'
44 eventname
[15] = 'osEvt'
45 eventname
[23] = 'kHighLevelEvent'
47 # Map part codes returned by WhichWindow() to strings
49 partname
[0] = 'inDesk'
50 partname
[1] = 'inMenuBar'
51 partname
[2] = 'inSysWindow'
52 partname
[3] = 'inContent'
53 partname
[4] = 'inDrag'
54 partname
[5] = 'inGrow'
55 partname
[6] = 'inGoAway'
56 partname
[7] = 'inZoomIn'
57 partname
[8] = 'inZoomOut'
60 # The useable portion of the screen
61 # ## but what happens with multiple screens? jvr
62 screenbounds
= qd
.screenBits
.bounds
63 screenbounds
= screenbounds
[0]+4, screenbounds
[1]+4, \
64 screenbounds
[2]-4, screenbounds
[3]-4
66 next_window_x
= 16 # jvr
67 next_window_y
= 44 # jvr
69 def windowbounds(width
, height
):
70 "Return sensible window bounds"
71 global next_window_x
, next_window_y
72 r
, b
= next_window_x
+width
, next_window_y
+height
73 if r
> screenbounds
[2]:
75 if b
> screenbounds
[3]:
77 l
, t
= next_window_x
, next_window_y
78 r
, b
= next_window_x
+width
, next_window_y
+height
79 next_window_x
, next_window_y
= next_window_x
+ 8, next_window_y
+ 20 # jvr
87 _watch
= GetCursor(4).data
95 "Application framework -- your application should be a derived class"
97 def __init__(self
, nomenubar
=0):
98 self
._doing
_asyncevents
= 0
100 self
.needmenubarredraw
= 0
108 if self
._doing
_asyncevents
:
109 self
._doing
_asyncevents
= 0
110 MacOS
.SetEventHandler()
112 def makemenubar(self
):
113 self
.menubar
= MenuBar(self
)
114 AppleMenu(self
.menubar
, self
.getabouttext(), self
.do_about
)
117 def makeusermenus(self
):
118 self
.filemenu
= m
= Menu(self
.menubar
, "File")
119 self
._quititem
= MenuItem(m
, "Quit", "Q", self
._quit
)
121 def _quit(self
, *args
):
125 for w
in self
._windows
.values():
127 return self
._windows
== {}
129 def appendwindow(self
, wid
, window
):
130 self
._windows
[wid
] = window
132 def removewindow(self
, wid
):
133 del self
._windows
[wid
]
135 def getabouttext(self
):
136 return "About %s..." % self
.__class
__.__name
__
138 def do_about(self
, id, item
, window
, event
):
139 EasyDialogs
.Message("Hello, world!" + "\015(%s)" % self
.__class
__.__name
__)
141 # The main event loop is broken up in several simple steps.
142 # This is done so you can override each individual part,
143 # if you have a need to do extra processing independent of the
145 # Normally, however, you'd just define handlers for individual
147 # (XXX I'm not sure if using default parameter values is the right
148 # way to define the mask and wait time passed to WaitNextEvent.)
150 def mainloop(self
, mask
= everyEvent
, wait
= 0):
152 saveparams
= apply(MacOS
.SchedParams
, self
.schedparams
)
154 while not self
.quitting
:
156 self
.do1event(mask
, wait
)
157 except (Application
, SystemExit):
158 # Note: the raising of "self" is old-fashioned idiom to
159 # exit the mainloop. Calling _quit() is better for new
163 apply(MacOS
.SchedParams
, saveparams
)
165 schedparams
= (0, 0) # By default disable Python's event handling
167 def dopendingevents(self
, mask
= everyEvent
):
168 """dopendingevents - Handle all pending events"""
169 while self
.do1event(mask
, wait
=0):
172 def do1event(self
, mask
= everyEvent
, wait
= 0):
173 ok
, event
= self
.getevent(mask
, wait
)
174 if IsDialogEvent(event
):
175 if self
.do_dialogevent(event
):
182 def idle(self
, event
):
185 def getevent(self
, mask
= everyEvent
, wait
= 0):
186 if self
.needmenubarredraw
:
188 self
.needmenubarredraw
= 0
189 ok
, event
= WaitNextEvent(mask
, wait
)
192 def dispatch(self
, event
):
193 # The following appears to be double work (already done in do1event)
194 # but we need it for asynchronous event handling
195 if IsDialogEvent(event
):
196 if self
.do_dialogevent(event
):
198 (what
, message
, when
, where
, modifiers
) = event
199 if eventname
.has_key(what
):
200 name
= "do_" + eventname
[what
]
202 name
= "do_%d" % what
204 handler
= getattr(self
, name
)
205 except AttributeError:
206 handler
= self
.do_unknownevent
209 def asyncevents(self
, onoff
):
210 """asyncevents - Set asynchronous event handling on or off"""
211 old
= self
._doing
_asyncevents
213 MacOS
.SetEventHandler()
214 apply(MacOS
.SchedParams
, self
.schedparams
)
216 MacOS
.SetEventHandler(self
.dispatch
)
217 doint
, dummymask
, benice
, howoften
, bgyield
= \
219 MacOS
.SchedParams(doint
, everyEvent
, benice
,
221 self
._doing
_asyncevents
= onoff
224 def do_dialogevent(self
, event
):
225 gotone
, window
, item
= DialogSelect(event
)
227 if self
._windows
.has_key(window
):
228 self
._windows
[window
].do_itemhit(item
, event
)
230 print 'Dialog event for unknown dialog'
234 def do_mouseDown(self
, event
):
235 (what
, message
, when
, where
, modifiers
) = event
236 partcode
, wid
= FindWindow(where
)
239 # Find the correct name.
241 if partname
.has_key(partcode
):
242 name
= "do_" + partname
[partcode
]
244 name
= "do_%d" % partcode
247 # No window, or a non-python window
249 handler
= getattr(self
, name
)
250 except AttributeError:
251 # Not menubar or something, so assume someone
253 MacOS
.HandleEvent(event
)
255 elif self
._windows
.has_key(wid
):
256 # It is a window. Hand off to correct window.
257 window
= self
._windows
[wid
]
259 handler
= getattr(window
, name
)
260 except AttributeError:
261 handler
= self
.do_unknownpartcode
263 # It is a python-toolbox window, but not ours.
264 handler
= self
.do_unknownwindow
265 handler(partcode
, wid
, event
)
267 def do_inSysWindow(self
, partcode
, window
, event
):
268 MacOS
.HandleEvent(event
)
270 def do_inDesk(self
, partcode
, window
, event
):
271 MacOS
.HandleEvent(event
)
273 def do_inMenuBar(self
, partcode
, window
, event
):
275 MacOS
.HandleEvent(event
)
277 (what
, message
, when
, where
, modifiers
) = event
278 result
= MenuSelect(where
)
279 id = (result
>>16) & 0xffff # Hi word
280 item
= result
& 0xffff # Lo word
281 self
.do_rawmenu(id, item
, window
, event
)
283 def do_rawmenu(self
, id, item
, window
, event
):
285 self
.do_menu(id, item
, window
, event
)
289 def do_menu(self
, id, item
, window
, event
):
290 self
.menubar
.dispatch(id, item
, window
, event
)
293 def do_unknownpartcode(self
, partcode
, window
, event
):
294 (what
, message
, when
, where
, modifiers
) = event
295 if DEBUG
: print "Mouse down at global:", where
296 if DEBUG
: print "\tUnknown part code:", partcode
297 if DEBUG
: print "\tEvent:", self
.printevent(event
)
298 MacOS
.HandleEvent(event
)
300 def do_unknownwindow(self
, partcode
, window
, event
):
301 if DEBUG
: print 'Unknown window:', window
302 MacOS
.HandleEvent(event
)
304 def do_keyDown(self
, event
):
307 def do_autoKey(self
, event
):
308 if not event
[-1] & cmdKey
:
311 def do_key(self
, event
):
312 (what
, message
, when
, where
, modifiers
) = event
313 c
= chr(message
& charCodeMask
)
315 result
= MenuEvent(event
)
316 id = (result
>>16) & 0xffff # Hi word
317 item
= result
& 0xffff # Lo word
319 self
.do_rawmenu(id, item
, None, event
)
321 # Otherwise we fall-through
322 if modifiers
& cmdKey
:
327 MacOS
.HandleEvent(event
)
330 # See whether the front window wants it
332 if w
and self
._windows
.has_key(w
):
333 window
= self
._windows
[w
]
335 do_char
= window
.do_char
336 except AttributeError:
337 do_char
= self
.do_char
339 # else it wasn't for us, sigh...
341 def do_char(self
, c
, event
):
342 if DEBUG
: print "Character", `c`
344 def do_updateEvt(self
, event
):
345 (what
, message
, when
, where
, modifiers
) = event
346 wid
= WhichWindow(message
)
347 if wid
and self
._windows
.has_key(wid
):
348 window
= self
._windows
[wid
]
349 window
.do_rawupdate(wid
, event
)
351 MacOS
.HandleEvent(event
)
353 def do_activateEvt(self
, event
):
354 (what
, message
, when
, where
, modifiers
) = event
355 wid
= WhichWindow(message
)
356 if wid
and self
._windows
.has_key(wid
):
357 window
= self
._windows
[wid
]
358 window
.do_activate(modifiers
& 1, event
)
360 MacOS
.HandleEvent(event
)
362 def do_osEvt(self
, event
):
363 (what
, message
, when
, where
, modifiers
) = event
364 which
= (message
>> 24) & 0xff
365 if which
== 1: # suspend/resume
366 self
.do_suspendresume(event
)
369 print 'unknown osEvt:',
370 self
.printevent(event
)
372 def do_suspendresume(self
, event
):
373 (what
, message
, when
, where
, modifiers
) = event
375 if wid
and self
._windows
.has_key(wid
):
376 window
= self
._windows
[wid
]
377 window
.do_activate(message
& 1, event
)
379 def do_kHighLevelEvent(self
, event
):
380 (what
, message
, when
, where
, modifiers
) = event
382 print "High Level Event:",
383 self
.printevent(event
)
385 AEProcessAppleEvent(event
)
387 print "AEProcessAppleEvent error:"
388 traceback
.print_exc()
390 def do_unknownevent(self
, event
):
392 print "Unhandled event:",
393 self
.printevent(event
)
395 def printevent(self
, event
):
396 (what
, message
, when
, where
, modifiers
) = event
398 if eventname
.has_key(what
):
399 nicewhat
= eventname
[what
]
401 if what
== kHighLevelEvent
:
403 print `
ostypecode(message
)`
, hex(when
), `
ostypecode(h |
(v
<<16))`
,
405 print hex(message
), hex(when
), where
,
410 """Represent a set of menus in a menu bar.
417 - addpopup (normally used internally)
418 - dispatch (called from Application)
421 nextid
= 1 # Necessarily a class variable
425 MenuBar
.nextid
= id+1
428 def __init__(self
, parent
=None):
431 self
.bar
= GetMenuBar()
440 def addmenu(self
, title
, after
= 0):
441 id = self
.getnextid()
442 if DEBUG
: print 'Newmenu', title
, id # XXXX
443 m
= NewMenu(id, title
)
447 self
.parent
.needmenubarredraw
= 1
452 def delmenu(self
, id):
453 if DEBUG
: print 'Delmenu', id # XXXX
456 def addpopup(self
, title
= ''):
457 return self
.addmenu(title
, -1)
461 # if not self.bar: return
462 # SetMenuBar(self.bar)
464 # self.parent.needmenubarredraw = 1
468 def fixmenudimstate(self
):
469 for m
in self
.menus
.keys():
471 if menu
.__class
__ == FrameWork
.AppleMenu
:
473 for i
in range(len(menu
.items
)):
474 label
, shortcut
, callback
, kind
= menu
.items
[i
]
475 if type(callback
) == types
.StringType
:
476 wid
= Win
.FrontWindow()
477 if wid
and self
.parent
._windows
.has_key(wid
):
478 window
= self
.parent
._windows
[wid
]
479 if hasattr(window
, "domenu_" + callback
):
480 menu
.menu
.EnableItem(i
+ 1)
481 elif hasattr(self
.parent
, "domenu_" + callback
):
482 menu
.menu
.EnableItem(i
+ 1)
484 menu
.menu
.DisableItem(i
+ 1)
485 elif hasattr(self
.parent
, "domenu_" + callback
):
486 menu
.menu
.EnableItem(i
+ 1)
488 menu
.menu
.DisableItem(i
+ 1)
492 def dispatch(self
, id, item
, window
, event
):
493 if self
.menus
.has_key(id):
494 self
.menus
[id].dispatch(id, item
, window
, event
)
496 if DEBUG
: print "MenuBar.dispatch(%d, %d, %s, %s)" % \
497 (id, item
, window
, event
)
500 # XXX Need a way to get menus as resources and bind them to callbacks
505 def __init__(self
, bar
, title
, after
=0):
507 self
.id, self
.menu
= self
.bar
.addmenu(title
, after
)
508 bar
.menus
[self
.id] = self
513 self
.bar
.delmenu(self
.id)
514 del self
.bar
.menus
[self
.id]
521 def additem(self
, label
, shortcut
=None, callback
=None, kind
=None):
522 self
.menu
.AppendMenu('x') # add a dummy string
523 self
.items
.append(label
, shortcut
, callback
, kind
)
524 item
= len(self
.items
)
525 self
.menu
.SetMenuItemText(item
, label
) # set the actual text
526 if shortcut
and type(shortcut
) == type(()):
527 modifiers
, char
= shortcut
[:2]
528 self
.menu
.SetItemCmd(item
, ord(char
))
529 self
.menu
.SetMenuItemModifiers(item
, modifiers
)
530 if len(shortcut
) > 2:
531 self
.menu
.SetMenuItemKeyGlyph(item
, shortcut
[2])
533 self
.menu
.SetItemCmd(item
, ord(shortcut
))
536 def delitem(self
, item
):
537 if item
!= len(self
.items
):
538 raise 'Can only delete last item of a menu'
539 self
.menu
.DeleteMenuItem(item
)
540 del self
.items
[item
-1]
542 def addcheck(self
, label
, shortcut
=None, callback
=None):
543 return self
.additem(label
, shortcut
, callback
, 'check')
545 def addradio(self
, label
, shortcut
=None, callback
=None):
546 return self
.additem(label
, shortcut
, callback
, 'radio')
548 def addseparator(self
):
549 self
.menu
.AppendMenu('(-')
550 self
.items
.append('', None, None, 'separator')
552 def addsubmenu(self
, label
, title
=''):
553 sub
= Menu(self
.bar
, title
, -1)
554 item
= self
.additem(label
, '\x1B', None, 'submenu')
555 self
.menu
.SetItemMark(item
, sub
.id)
557 sub
._parent
_item
= item
560 def dispatch(self
, id, item
, window
, event
):
561 title
, shortcut
, callback
, mtype
= self
.items
[item
-1]
563 if not self
.bar
.parent
or type(callback
) <> types
.StringType
:
564 menuhandler
= callback
567 wid
= Win
.FrontWindow()
568 if wid
and self
.bar
.parent
._windows
.has_key(wid
):
569 window
= self
.bar
.parent
._windows
[wid
]
570 if hasattr(window
, "domenu_" + callback
):
571 menuhandler
= getattr(window
, "domenu_" + callback
)
572 elif hasattr(self
.bar
.parent
, "domenu_" + callback
):
573 menuhandler
= getattr(self
.bar
.parent
, "domenu_" + callback
)
575 # nothing we can do. we shouldn't have come this far
576 # since the menu item should have been disabled...
578 elif hasattr(self
.bar
.parent
, "domenu_" + callback
):
579 menuhandler
= getattr(self
.bar
.parent
, "domenu_" + callback
)
581 # nothing we can do. we shouldn't have come this far
582 # since the menu item should have been disabled...
584 menuhandler(id, item
, window
, event
)
586 def enable(self
, onoff
):
588 self
.menu
.EnableItem(0)
590 self
._parent
.menu
.EnableItem(self
._parent
_item
)
592 self
.menu
.DisableItem(0)
594 self
._parent
.menu
.DisableItem(self
._parent
_item
)
595 if self
.bar
and self
.bar
.parent
:
596 self
.bar
.parent
.needmenubarredraw
= 1
598 class PopupMenu(Menu
):
599 def __init__(self
, bar
):
600 Menu
.__init
__(self
, bar
, '(popup)', -1)
602 def popup(self
, x
, y
, event
, default
=1, window
=None):
603 # NOTE that x and y are global coordinates, and they should probably
604 # be topleft of the button the user clicked (not mouse-coordinates),
605 # so the popup nicely overlaps.
606 reply
= self
.menu
.PopUpMenuSelect(x
, y
, default
)
609 id = (reply
& 0xffff0000) >> 16
610 item
= reply
& 0xffff
612 wid
= Win
.FrontWindow()
614 window
= self
.bar
.parent
._windows
[wid
]
616 pass # If we can't find the window we pass None
617 self
.dispatch(id, item
, window
, event
)
620 def __init__(self
, menu
, title
, shortcut
=None, callback
=None, kind
=None):
621 self
.item
= menu
.additem(title
, shortcut
, callback
)
625 self
.menu
.delitem(self
.item
)
629 def check(self
, onoff
):
630 self
.menu
.menu
.CheckItem(self
.item
, onoff
)
632 def enable(self
, onoff
):
634 self
.menu
.menu
.EnableItem(self
.item
)
636 self
.menu
.menu
.DisableItem(self
.item
)
638 def settext(self
, text
):
639 self
.menu
.menu
.SetMenuItemText(self
.item
, text
)
641 def setstyle(self
, style
):
642 self
.menu
.menu
.SetItemStyle(self
.item
, style
)
644 def seticon(self
, icon
):
645 self
.menu
.menu
.SetItemIcon(self
.item
, icon
)
647 def setcmd(self
, cmd
):
648 self
.menu
.menu
.SetItemCmd(self
.item
, cmd
)
650 def setmark(self
, cmd
):
651 self
.menu
.menu
.SetItemMark(self
.item
, cmd
)
654 class RadioItem(MenuItem
):
655 def __init__(self
, menu
, title
, shortcut
=None, callback
=None):
656 MenuItem
.__init
__(self
, menu
, title
, shortcut
, callback
, 'radio')
658 class CheckItem(MenuItem
):
659 def __init__(self
, menu
, title
, shortcut
=None, callback
=None):
660 MenuItem
.__init
__(self
, menu
, title
, shortcut
, callback
, 'check')
665 def SubMenu(menu
, label
, title
=''):
666 return menu
.addsubmenu(label
, title
)
669 class AppleMenu(Menu
):
671 def __init__(self
, bar
, abouttext
="About me...", aboutcallback
=None):
672 Menu
.__init
__(self
, bar
, "\024")
673 self
.additem(abouttext
, None, aboutcallback
)
675 self
.menu
.AppendResMenu('DRVR')
677 def dispatch(self
, id, item
, window
, event
):
679 Menu
.dispatch(self
, id, item
, window
, event
)
681 name
= self
.menu
.GetMenuItemText(item
)
685 """A single window belonging to an application"""
687 def __init__(self
, parent
):
691 def open(self
, bounds
=(40, 40, 400, 400), resid
=None):
693 self
.wid
= GetNewWindow(resid
, -1)
695 self
.wid
= NewWindow(bounds
, self
.__class
__.__name
__, 1,
696 8, -1, 1, 0) # changed to proc id 8 to include zoom box. jvr
699 def do_postopen(self
):
700 """Tell our parent we exist"""
701 self
.parent
.appendwindow(self
.wid
, self
)
706 def do_postclose(self
):
707 self
.parent
.removewindow(self
.wid
)
715 def do_inDrag(self
, partcode
, window
, event
):
717 window
.DragWindow(where
, self
.draglimit
)
719 draglimit
= screenbounds
721 def do_inGoAway(self
, partcode
, window
, event
):
723 if window
.TrackGoAway(where
):
726 def do_inZoom(self
, partcode
, window
, event
):
727 (what
, message
, when
, where
, modifiers
) = event
728 if window
.TrackBox(where
, partcode
):
729 window
.ZoomWindow(partcode
, 1)
730 rect
= window
.GetWindowUserState() # so that zoom really works... jvr
731 self
.do_postresize(rect
[2] - rect
[0], rect
[3] - rect
[1], window
) # jvr
733 def do_inZoomIn(self
, partcode
, window
, event
):
734 SetPort(window
) # !!!
735 self
.do_inZoom(partcode
, window
, event
)
737 def do_inZoomOut(self
, partcode
, window
, event
):
738 SetPort(window
) # !!!
739 self
.do_inZoom(partcode
, window
, event
)
741 def do_inGrow(self
, partcode
, window
, event
):
742 (what
, message
, when
, where
, modifiers
) = event
743 result
= window
.GrowWindow(where
, self
.growlimit
)
745 height
= (result
>>16) & 0xffff # Hi word
746 width
= result
& 0xffff # Lo word
747 self
.do_resize(width
, height
, window
)
749 growlimit
= (50, 50, screenbounds
[2] - screenbounds
[0], screenbounds
[3] - screenbounds
[1]) # jvr
751 def do_resize(self
, width
, height
, window
):
752 l
, t
, r
, b
= self
.wid
.GetWindowPort().portRect
# jvr, forGrowIcon
754 InvalRect((r
- SCROLLBARWIDTH
+ 1, b
- SCROLLBARWIDTH
+ 1, r
, b
)) # jvr
755 window
.SizeWindow(width
, height
, 1) # changed updateFlag to true jvr
756 self
.do_postresize(width
, height
, window
)
758 def do_postresize(self
, width
, height
, window
):
760 InvalRect(window
.GetWindowPort().portRect
)
762 def do_inContent(self
, partcode
, window
, event
):
764 # If we're not frontmost, select ourselves and wait for
765 # the activate event.
767 if FrontWindow() <> window
:
768 window
.SelectWindow()
770 # We are. Handle the event.
771 (what
, message
, when
, where
, modifiers
) = event
773 local
= GlobalToLocal(where
)
774 self
.do_contentclick(local
, modifiers
, event
)
776 def do_contentclick(self
, local
, modifiers
, event
):
778 print 'Click in contents at %s, modifiers %s'%(local
, modifiers
)
780 def do_rawupdate(self
, window
, event
):
781 if DEBUG
: print "raw update for", window
784 self
.do_update(window
, event
)
787 def do_update(self
, window
, event
):
792 InvertRgn(window
.GetWindowPort().visRgn
)
793 FillRgn(window
.GetWindowPort().visRgn
, qd
.gray
)
795 EraseRgn(window
.GetWindowPort().visRgn
)
797 def do_activate(self
, activate
, event
):
798 if DEBUG
: print 'Activate %d for %s'%(activate
, self
.wid
)
800 class ControlsWindow(Window
):
802 def do_rawupdate(self
, window
, event
):
803 if DEBUG
: print "raw update for", window
806 self
.do_update(window
, event
)
807 #DrawControls(window) # jvr
808 UpdateControls(window
, window
.GetWindowPort().visRgn
) # jvr
809 window
.DrawGrowIcon()
812 def do_controlhit(self
, window
, control
, pcode
, event
):
813 if DEBUG
: print "control hit in", window
, "on", control
, "; pcode =", pcode
815 def do_inContent(self
, partcode
, window
, event
):
816 if FrontWindow() <> window
:
817 window
.SelectWindow()
819 (what
, message
, when
, where
, modifiers
) = event
820 SetPort(window
) # XXXX Needed?
821 local
= GlobalToLocal(where
)
822 pcode
, control
= FindControl(local
, window
)
823 if pcode
and control
:
824 self
.do_rawcontrolhit(window
, control
, pcode
, local
, event
)
826 if DEBUG
: print "FindControl(%s, %s) -> (%s, %s)" % \
827 (local
, window
, pcode
, control
)
828 self
.do_contentclick(local
, modifiers
, event
)
830 def do_rawcontrolhit(self
, window
, control
, pcode
, local
, event
):
831 pcode
= control
.TrackControl(local
)
833 self
.do_controlhit(window
, control
, pcode
, event
)
835 class ScrolledWindow(ControlsWindow
):
836 def __init__(self
, parent
):
837 self
.barx
= self
.bary
= None
838 self
.barx_enabled
= self
.bary_enabled
= 1
840 ControlsWindow
.__init
__(self
, parent
)
842 def scrollbars(self
, wantx
=1, wanty
=1):
844 self
.barx
= self
.bary
= None
845 self
.barx_enabled
= self
.bary_enabled
= 1
846 x0
, y0
, x1
, y1
= self
.wid
.GetWindowPort().portRect
847 vx
, vy
= self
.getscrollbarvalues()
848 if vx
== None: self
.barx_enabled
, vx
= 0, 0
849 if vy
== None: self
.bary_enabled
, vy
= 0, 0
851 rect
= x0
-1, y1
-(SCROLLBARWIDTH
-1), x1
-(SCROLLBARWIDTH
-2), y1
+1
852 self
.barx
= NewControl(self
.wid
, rect
, "", 1, vx
, 0, 32767, 16, 0)
853 if not self
.barx_enabled
: self
.barx
.HiliteControl(255)
856 rect
= x1
-(SCROLLBARWIDTH
-1), y0
-1, x1
+1, y1
-(SCROLLBARWIDTH
-2)
857 self
.bary
= NewControl(self
.wid
, rect
, "", 1, vy
, 0, 32767, 16, 0)
858 if not self
.bary_enabled
: self
.bary
.HiliteControl(255)
861 def do_postclose(self
):
862 self
.barx
= self
.bary
= None
863 ControlsWindow
.do_postclose(self
)
865 def do_activate(self
, onoff
, event
):
866 self
.activated
= onoff
868 if self
.barx
and self
.barx_enabled
:
869 self
.barx
.ShowControl() # jvr
870 if self
.bary
and self
.bary_enabled
:
871 self
.bary
.ShowControl() # jvr
874 self
.barx
.HideControl() # jvr; An inactive window should have *hidden*
875 # scrollbars, not just dimmed (no matter what
876 # BBEdit does... look at the Finder)
878 self
.bary
.HideControl() # jvr
879 self
.wid
.DrawGrowIcon() # jvr
881 def do_postresize(self
, width
, height
, window
):
882 l
, t
, r
, b
= self
.wid
.GetWindowPort().portRect
885 self
.barx
.HideControl() # jvr
886 self
.barx
.MoveControl(l
-1, b
-(SCROLLBARWIDTH
-1))
887 self
.barx
.SizeControl((r
-l
)-(SCROLLBARWIDTH
-3), SCROLLBARWIDTH
) # jvr
889 self
.bary
.HideControl() # jvr
890 self
.bary
.MoveControl(r
-(SCROLLBARWIDTH
-1), t
-1)
891 self
.bary
.SizeControl(SCROLLBARWIDTH
, (b
-t
)-(SCROLLBARWIDTH
-3)) # jvr
893 self
.barx
.ShowControl() # jvr
894 ValidRect((l
, b
- SCROLLBARWIDTH
+ 1, r
- SCROLLBARWIDTH
+ 2, b
)) # jvr
896 self
.bary
.ShowControl() # jvr
897 ValidRect((r
- SCROLLBARWIDTH
+ 1, t
, r
, b
- SCROLLBARWIDTH
+ 2)) # jvr
898 InvalRect((r
- SCROLLBARWIDTH
+ 1, b
- SCROLLBARWIDTH
+ 1, r
, b
)) # jvr, growicon
901 def do_rawcontrolhit(self
, window
, control
, pcode
, local
, event
):
902 if control
== self
.barx
:
904 elif control
== self
.bary
:
908 if pcode
in (inUpButton
, inDownButton
, inPageUp
, inPageDown
):
909 # We do the work for the buttons and grey area in the tracker
910 dummy
= control
.TrackControl(local
, self
.do_controltrack
)
912 # but the thumb is handled here
913 pcode
= control
.TrackControl(local
)
915 value
= control
.GetControlValue()
916 print 'setbars', which
, value
#DBG
917 self
.scrollbar_callback(which
, 'set', value
)
918 self
.updatescrollbars()
920 print 'funny part', pcode
#DBG
923 def do_controltrack(self
, control
, pcode
):
924 if control
== self
.barx
:
926 elif control
== self
.bary
:
931 if pcode
== inUpButton
:
933 elif pcode
== inDownButton
:
935 elif pcode
== inPageUp
:
937 elif pcode
== inPageDown
:
941 self
.scrollbar_callback(which
, what
, None)
942 self
.updatescrollbars()
944 def updatescrollbars(self
):
946 vx
, vy
= self
.getscrollbarvalues()
949 self
.barx
.HiliteControl(255)
950 self
.barx_enabled
= 0
952 if not self
.barx_enabled
:
953 self
.barx_enabled
= 1
955 self
.barx
.HiliteControl(0)
956 self
.barx
.SetControlValue(vx
)
959 self
.bary
.HiliteControl(255)
960 self
.bary_enabled
= 0
962 if not self
.bary_enabled
:
963 self
.bary_enabled
= 1
965 self
.bary
.HiliteControl(0)
966 self
.bary
.SetControlValue(vy
)
968 # Auxiliary function: convert standard text/image/etc coordinate
969 # to something palatable as getscrollbarvalues() return
970 def scalebarvalue(self
, absmin
, absmax
, curmin
, curmax
):
971 if curmin
<= absmin
and curmax
>= absmax
:
977 perc
= float(curmin
-absmin
)/float(absmax
-absmin
)
978 return int(perc
*32767)
982 def getscrollbarvalues(self
):
985 def scrollbar_callback(self
, which
, what
, value
):
986 print 'scroll', which
, what
, value
988 class DialogWindow(Window
):
989 """A modeless dialog window"""
991 def open(self
, resid
):
992 self
.wid
= GetNewDialog(resid
, -1)
998 def do_itemhit(self
, item
, event
):
999 print 'Dialog %s, item %d hit'%(self
.wid
, item
)
1001 def do_rawupdate(self
, window
, event
):
1005 "Convert a long int to the 4-character code it really is"
1008 x
, c
= divmod(x
, 256)
1013 class TestApp(Application
):
1015 "This class is used by the test() function"
1017 def makeusermenus(self
):
1018 self
.filemenu
= m
= Menu(self
.menubar
, "File")
1019 self
.saveitem
= MenuItem(m
, "Save", "S", self
.save
)
1021 self
.optionsmenu
= mm
= SubMenu(m
, "Options")
1022 self
.opt1
= CheckItem(mm
, "Arguments", "A")
1023 self
.opt2
= CheckItem(mm
, "Being hit on the head lessons", (kMenuOptionModifier
, "A"))
1024 self
.opt3
= CheckItem(mm
, "Complaints", (kMenuOptionModifier|kMenuNoCommandModifier
, "A"))
1026 self
.quititem
= MenuItem(m
, "Quit", "Q", self
.quit
)
1028 def save(self
, *args
):
1031 def quit(self
, *args
):
1041 if __name__
== '__main__':