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
148 schedparams
= (0, 0) # By default disable Python's event handling
149 default_wait
= None # By default we wait GetCaretTime in WaitNextEvent
151 def mainloop(self
, mask
= everyEvent
, wait
= None):
153 saveparams
= apply(MacOS
.SchedParams
, self
.schedparams
)
155 while not self
.quitting
:
157 self
.do1event(mask
, wait
)
158 except (Application
, SystemExit):
159 # Note: the raising of "self" is old-fashioned idiom to
160 # exit the mainloop. Calling _quit() is better for new
164 apply(MacOS
.SchedParams
, saveparams
)
166 def dopendingevents(self
, mask
= everyEvent
):
167 """dopendingevents - Handle all pending events"""
168 while self
.do1event(mask
, wait
=0):
171 def do1event(self
, mask
= everyEvent
, wait
= None):
172 ok
, event
= self
.getevent(mask
, wait
)
173 if IsDialogEvent(event
):
174 if self
.do_dialogevent(event
):
181 def idle(self
, event
):
184 def getevent(self
, mask
= everyEvent
, wait
= None):
185 if self
.needmenubarredraw
:
187 self
.needmenubarredraw
= 0
189 wait
= self
.default_wait
191 wait
= GetCaretTime()
192 ok
, event
= WaitNextEvent(mask
, wait
)
195 def dispatch(self
, event
):
196 # The following appears to be double work (already done in do1event)
197 # but we need it for asynchronous event handling
198 if IsDialogEvent(event
):
199 if self
.do_dialogevent(event
):
201 (what
, message
, when
, where
, modifiers
) = event
202 if eventname
.has_key(what
):
203 name
= "do_" + eventname
[what
]
205 name
= "do_%d" % what
207 handler
= getattr(self
, name
)
208 except AttributeError:
209 handler
= self
.do_unknownevent
212 def asyncevents(self
, onoff
):
213 """asyncevents - Set asynchronous event handling on or off"""
214 old
= self
._doing
_asyncevents
216 MacOS
.SetEventHandler()
217 apply(MacOS
.SchedParams
, self
.schedparams
)
219 MacOS
.SetEventHandler(self
.dispatch
)
220 doint
, dummymask
, benice
, howoften
, bgyield
= \
222 MacOS
.SchedParams(doint
, everyEvent
, benice
,
224 self
._doing
_asyncevents
= onoff
227 def do_dialogevent(self
, event
):
228 gotone
, dlg
, item
= DialogSelect(event
)
230 window
= dlg
.GetDialogWindow()
231 if self
._windows
.has_key(window
):
232 self
._windows
[window
].do_itemhit(item
, event
)
234 print 'Dialog event for unknown dialog'
238 def do_mouseDown(self
, event
):
239 (what
, message
, when
, where
, modifiers
) = event
240 partcode
, wid
= FindWindow(where
)
243 # Find the correct name.
245 if partname
.has_key(partcode
):
246 name
= "do_" + partname
[partcode
]
248 name
= "do_%d" % partcode
251 # No window, or a non-python window
253 handler
= getattr(self
, name
)
254 except AttributeError:
255 # Not menubar or something, so assume someone
257 MacOS
.HandleEvent(event
)
259 elif self
._windows
.has_key(wid
):
260 # It is a window. Hand off to correct window.
261 window
= self
._windows
[wid
]
263 handler
= getattr(window
, name
)
264 except AttributeError:
265 handler
= self
.do_unknownpartcode
267 # It is a python-toolbox window, but not ours.
268 handler
= self
.do_unknownwindow
269 handler(partcode
, wid
, event
)
271 def do_inSysWindow(self
, partcode
, window
, event
):
272 MacOS
.HandleEvent(event
)
274 def do_inDesk(self
, partcode
, window
, event
):
275 MacOS
.HandleEvent(event
)
277 def do_inMenuBar(self
, partcode
, window
, event
):
279 MacOS
.HandleEvent(event
)
281 (what
, message
, when
, where
, modifiers
) = event
282 result
= MenuSelect(where
)
283 id = (result
>>16) & 0xffff # Hi word
284 item
= result
& 0xffff # Lo word
285 self
.do_rawmenu(id, item
, window
, event
)
287 def do_rawmenu(self
, id, item
, window
, event
):
289 self
.do_menu(id, item
, window
, event
)
293 def do_menu(self
, id, item
, window
, event
):
295 self
.menubar
.dispatch(id, item
, window
, event
)
298 def do_unknownpartcode(self
, partcode
, window
, event
):
299 (what
, message
, when
, where
, modifiers
) = event
300 if DEBUG
: print "Mouse down at global:", where
301 if DEBUG
: print "\tUnknown part code:", partcode
302 if DEBUG
: print "\tEvent:", self
.printevent(event
)
303 MacOS
.HandleEvent(event
)
305 def do_unknownwindow(self
, partcode
, window
, event
):
306 if DEBUG
: print 'Unknown window:', window
307 MacOS
.HandleEvent(event
)
309 def do_keyDown(self
, event
):
312 def do_autoKey(self
, event
):
313 if not event
[-1] & cmdKey
:
316 def do_key(self
, event
):
317 (what
, message
, when
, where
, modifiers
) = event
318 c
= chr(message
& charCodeMask
)
320 result
= MenuEvent(event
)
321 id = (result
>>16) & 0xffff # Hi word
322 item
= result
& 0xffff # Lo word
324 self
.do_rawmenu(id, item
, None, event
)
326 # Otherwise we fall-through
327 if modifiers
& cmdKey
:
332 MacOS
.HandleEvent(event
)
335 # See whether the front window wants it
337 if w
and self
._windows
.has_key(w
):
338 window
= self
._windows
[w
]
340 do_char
= window
.do_char
341 except AttributeError:
342 do_char
= self
.do_char
344 # else it wasn't for us, sigh...
346 def do_char(self
, c
, event
):
347 if DEBUG
: print "Character", `c`
349 def do_updateEvt(self
, event
):
350 (what
, message
, when
, where
, modifiers
) = event
351 wid
= WhichWindow(message
)
352 if wid
and self
._windows
.has_key(wid
):
353 window
= self
._windows
[wid
]
354 window
.do_rawupdate(wid
, event
)
356 MacOS
.HandleEvent(event
)
358 def do_activateEvt(self
, event
):
359 (what
, message
, when
, where
, modifiers
) = event
360 wid
= WhichWindow(message
)
361 if wid
and self
._windows
.has_key(wid
):
362 window
= self
._windows
[wid
]
363 window
.do_activate(modifiers
& 1, event
)
365 MacOS
.HandleEvent(event
)
367 def do_osEvt(self
, event
):
368 (what
, message
, when
, where
, modifiers
) = event
369 which
= (message
>> 24) & 0xff
370 if which
== 1: # suspend/resume
371 self
.do_suspendresume(event
)
374 print 'unknown osEvt:',
375 self
.printevent(event
)
377 def do_suspendresume(self
, event
):
378 (what
, message
, when
, where
, modifiers
) = event
380 if wid
and self
._windows
.has_key(wid
):
381 window
= self
._windows
[wid
]
382 window
.do_activate(message
& 1, event
)
384 def do_kHighLevelEvent(self
, event
):
385 (what
, message
, when
, where
, modifiers
) = event
387 print "High Level Event:",
388 self
.printevent(event
)
390 AEProcessAppleEvent(event
)
392 print "AEProcessAppleEvent error:"
393 traceback
.print_exc()
395 def do_unknownevent(self
, event
):
397 print "Unhandled event:",
398 self
.printevent(event
)
400 def printevent(self
, event
):
401 (what
, message
, when
, where
, modifiers
) = event
403 if eventname
.has_key(what
):
404 nicewhat
= eventname
[what
]
406 if what
== kHighLevelEvent
:
408 print `
ostypecode(message
)`
, hex(when
), `
ostypecode(h |
(v
<<16))`
,
410 print hex(message
), hex(when
), where
,
415 """Represent a set of menus in a menu bar.
422 - addpopup (normally used internally)
423 - dispatch (called from Application)
426 nextid
= 1 # Necessarily a class variable
430 MenuBar
.nextid
= id+1
433 def __init__(self
, parent
=None):
436 self
.bar
= GetMenuBar()
445 def addmenu(self
, title
, after
= 0):
446 id = self
.getnextid()
447 if DEBUG
: print 'Newmenu', title
, id # XXXX
448 m
= NewMenu(id, title
)
452 self
.parent
.needmenubarredraw
= 1
457 def delmenu(self
, id):
458 if DEBUG
: print 'Delmenu', id # XXXX
461 def addpopup(self
, title
= ''):
462 return self
.addmenu(title
, -1)
466 # if not self.bar: return
467 # SetMenuBar(self.bar)
469 # self.parent.needmenubarredraw = 1
473 def fixmenudimstate(self
):
474 for m
in self
.menus
.keys():
476 if menu
.__class
__ == FrameWork
.AppleMenu
:
478 for i
in range(len(menu
.items
)):
479 label
, shortcut
, callback
, kind
= menu
.items
[i
]
480 if type(callback
) == types
.StringType
:
481 wid
= Win
.FrontWindow()
482 if wid
and self
.parent
._windows
.has_key(wid
):
483 window
= self
.parent
._windows
[wid
]
484 if hasattr(window
, "domenu_" + callback
):
485 menu
.menu
.EnableMenuItem(i
+ 1)
486 elif hasattr(self
.parent
, "domenu_" + callback
):
487 menu
.menu
.EnableMenuItem(i
+ 1)
489 menu
.menu
.DisableMenuItem(i
+ 1)
490 elif hasattr(self
.parent
, "domenu_" + callback
):
491 menu
.menu
.EnableMenuItem(i
+ 1)
493 menu
.menu
.DisableMenuItem(i
+ 1)
497 def dispatch(self
, id, item
, window
, event
):
498 if self
.menus
.has_key(id):
499 self
.menus
[id].dispatch(id, item
, window
, event
)
501 if DEBUG
: print "MenuBar.dispatch(%d, %d, %s, %s)" % \
502 (id, item
, window
, event
)
505 # XXX Need a way to get menus as resources and bind them to callbacks
510 def __init__(self
, bar
, title
, after
=0):
512 self
.id, self
.menu
= self
.bar
.addmenu(title
, after
)
513 bar
.menus
[self
.id] = self
518 self
.bar
.delmenu(self
.id)
519 del self
.bar
.menus
[self
.id]
520 self
.menu
.DisposeMenu()
527 def additem(self
, label
, shortcut
=None, callback
=None, kind
=None):
528 self
.menu
.AppendMenu('x') # add a dummy string
529 self
.items
.append((label
, shortcut
, callback
, kind
))
530 item
= len(self
.items
)
531 self
.menu
.SetMenuItemText(item
, label
) # set the actual text
532 if shortcut
and type(shortcut
) == type(()):
533 modifiers
, char
= shortcut
[:2]
534 self
.menu
.SetItemCmd(item
, ord(char
))
535 self
.menu
.SetMenuItemModifiers(item
, modifiers
)
536 if len(shortcut
) > 2:
537 self
.menu
.SetMenuItemKeyGlyph(item
, shortcut
[2])
539 self
.menu
.SetItemCmd(item
, ord(shortcut
))
542 def delitem(self
, item
):
543 if item
!= len(self
.items
):
544 raise 'Can only delete last item of a menu'
545 self
.menu
.DeleteMenuItem(item
)
546 del self
.items
[item
-1]
548 def addcheck(self
, label
, shortcut
=None, callback
=None):
549 return self
.additem(label
, shortcut
, callback
, 'check')
551 def addradio(self
, label
, shortcut
=None, callback
=None):
552 return self
.additem(label
, shortcut
, callback
, 'radio')
554 def addseparator(self
):
555 self
.menu
.AppendMenu('(-')
556 self
.items
.append(('', None, None, 'separator'))
558 def addsubmenu(self
, label
, title
=''):
559 sub
= Menu(self
.bar
, title
, -1)
560 item
= self
.additem(label
, '\x1B', None, 'submenu')
561 self
.menu
.SetItemMark(item
, sub
.id)
563 sub
._parent
_item
= item
566 def dispatch(self
, id, item
, window
, event
):
567 title
, shortcut
, callback
, mtype
= self
.items
[item
-1]
569 if not self
.bar
.parent
or type(callback
) <> types
.StringType
:
570 menuhandler
= callback
573 wid
= Win
.FrontWindow()
574 if wid
and self
.bar
.parent
._windows
.has_key(wid
):
575 window
= self
.bar
.parent
._windows
[wid
]
576 if hasattr(window
, "domenu_" + callback
):
577 menuhandler
= getattr(window
, "domenu_" + callback
)
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 elif hasattr(self
.bar
.parent
, "domenu_" + callback
):
585 menuhandler
= getattr(self
.bar
.parent
, "domenu_" + callback
)
587 # nothing we can do. we shouldn't have come this far
588 # since the menu item should have been disabled...
590 menuhandler(id, item
, window
, event
)
592 def enable(self
, onoff
):
594 self
.menu
.EnableMenuItem(0)
596 self
._parent
.menu
.EnableMenuItem(self
._parent
_item
)
598 self
.menu
.DisableMenuItem(0)
600 self
._parent
.menu
.DisableMenuItem(self
._parent
_item
)
601 if self
.bar
and self
.bar
.parent
:
602 self
.bar
.parent
.needmenubarredraw
= 1
604 class PopupMenu(Menu
):
605 def __init__(self
, bar
):
606 Menu
.__init
__(self
, bar
, '(popup)', -1)
608 def popup(self
, x
, y
, event
, default
=1, window
=None):
609 # NOTE that x and y are global coordinates, and they should probably
610 # be topleft of the button the user clicked (not mouse-coordinates),
611 # so the popup nicely overlaps.
612 reply
= self
.menu
.PopUpMenuSelect(x
, y
, default
)
615 id = (reply
& 0xffff0000) >> 16
616 item
= reply
& 0xffff
618 wid
= Win
.FrontWindow()
620 window
= self
.bar
.parent
._windows
[wid
]
622 pass # If we can't find the window we pass None
623 self
.dispatch(id, item
, window
, event
)
626 def __init__(self
, menu
, title
, shortcut
=None, callback
=None, kind
=None):
627 self
.item
= menu
.additem(title
, shortcut
, callback
)
631 self
.menu
.delitem(self
.item
)
635 def check(self
, onoff
):
636 self
.menu
.menu
.CheckMenuItem(self
.item
, onoff
)
638 def enable(self
, onoff
):
640 self
.menu
.menu
.EnableMenuItem(self
.item
)
642 self
.menu
.menu
.DisableMenuItem(self
.item
)
644 def settext(self
, text
):
645 self
.menu
.menu
.SetMenuItemText(self
.item
, text
)
647 def setstyle(self
, style
):
648 self
.menu
.menu
.SetItemStyle(self
.item
, style
)
650 def seticon(self
, icon
):
651 self
.menu
.menu
.SetItemIcon(self
.item
, icon
)
653 def setcmd(self
, cmd
):
654 self
.menu
.menu
.SetItemCmd(self
.item
, cmd
)
656 def setmark(self
, cmd
):
657 self
.menu
.menu
.SetItemMark(self
.item
, cmd
)
660 class RadioItem(MenuItem
):
661 def __init__(self
, menu
, title
, shortcut
=None, callback
=None):
662 MenuItem
.__init
__(self
, menu
, title
, shortcut
, callback
, 'radio')
664 class CheckItem(MenuItem
):
665 def __init__(self
, menu
, title
, shortcut
=None, callback
=None):
666 MenuItem
.__init
__(self
, menu
, title
, shortcut
, callback
, 'check')
671 def SubMenu(menu
, label
, title
=''):
672 return menu
.addsubmenu(label
, title
)
675 class AppleMenu(Menu
):
677 def __init__(self
, bar
, abouttext
="About me...", aboutcallback
=None):
678 Menu
.__init
__(self
, bar
, "\024")
679 if MacOS
.runtimemodel
== 'ppc':
680 self
.additem(abouttext
, None, aboutcallback
)
682 self
.menu
.AppendResMenu('DRVR')
684 # Additem()'s tricks do not work for "apple" menu under Carbon
685 self
.menu
.InsertMenuItem(abouttext
, 0)
686 self
.items
.append((abouttext
, None, aboutcallback
, None))
688 def dispatch(self
, id, item
, window
, event
):
690 Menu
.dispatch(self
, id, item
, window
, event
)
691 elif MacOS
.runtimemodel
== 'ppc':
692 name
= self
.menu
.GetMenuItemText(item
)
696 """A single window belonging to an application"""
698 def __init__(self
, parent
):
702 def open(self
, bounds
=(40, 40, 400, 400), resid
=None):
704 self
.wid
= GetNewWindow(resid
, -1)
706 self
.wid
= NewWindow(bounds
, self
.__class
__.__name
__, 1,
707 8, -1, 1, 0) # changed to proc id 8 to include zoom box. jvr
710 def do_postopen(self
):
711 """Tell our parent we exist"""
712 self
.parent
.appendwindow(self
.wid
, self
)
717 def do_postclose(self
):
718 self
.parent
.removewindow(self
.wid
)
729 def do_inDrag(self
, partcode
, window
, event
):
731 window
.DragWindow(where
, self
.draglimit
)
733 draglimit
= screenbounds
735 def do_inGoAway(self
, partcode
, window
, event
):
737 if window
.TrackGoAway(where
):
740 def do_inZoom(self
, partcode
, window
, event
):
741 (what
, message
, when
, where
, modifiers
) = event
742 if window
.TrackBox(where
, partcode
):
743 window
.ZoomWindow(partcode
, 1)
744 rect
= window
.GetWindowUserState() # so that zoom really works... jvr
745 self
.do_postresize(rect
[2] - rect
[0], rect
[3] - rect
[1], window
) # jvr
747 def do_inZoomIn(self
, partcode
, window
, event
):
748 SetPort(window
) # !!!
749 self
.do_inZoom(partcode
, window
, event
)
751 def do_inZoomOut(self
, partcode
, window
, event
):
752 SetPort(window
) # !!!
753 self
.do_inZoom(partcode
, window
, event
)
755 def do_inGrow(self
, partcode
, window
, event
):
756 (what
, message
, when
, where
, modifiers
) = event
757 result
= window
.GrowWindow(where
, self
.growlimit
)
759 height
= (result
>>16) & 0xffff # Hi word
760 width
= result
& 0xffff # Lo word
761 self
.do_resize(width
, height
, window
)
763 growlimit
= (50, 50, screenbounds
[2] - screenbounds
[0], screenbounds
[3] - screenbounds
[1]) # jvr
765 def do_resize(self
, width
, height
, window
):
766 l
, t
, r
, b
= self
.wid
.GetWindowPort().portRect
# jvr, forGrowIcon
768 self
.wid
.InvalWindowRect((r
- SCROLLBARWIDTH
+ 1, b
- SCROLLBARWIDTH
+ 1, r
, b
)) # jvr
769 window
.SizeWindow(width
, height
, 1) # changed updateFlag to true jvr
770 self
.do_postresize(width
, height
, window
)
772 def do_postresize(self
, width
, height
, window
):
774 self
.wid
.InvalWindowRect(window
.GetWindowPort().portRect
)
776 def do_inContent(self
, partcode
, window
, event
):
778 # If we're not frontmost, select ourselves and wait for
779 # the activate event.
781 if FrontWindow() <> window
:
782 window
.SelectWindow()
784 # We are. Handle the event.
785 (what
, message
, when
, where
, modifiers
) = event
787 local
= GlobalToLocal(where
)
788 self
.do_contentclick(local
, modifiers
, event
)
790 def do_contentclick(self
, local
, modifiers
, event
):
792 print 'Click in contents at %s, modifiers %s'%(local
, modifiers
)
794 def do_rawupdate(self
, window
, event
):
795 if DEBUG
: print "raw update for", window
798 self
.do_update(window
, event
)
801 def do_update(self
, window
, event
):
806 InvertRgn(window
.GetWindowPort().visRgn
)
807 FillRgn(window
.GetWindowPort().visRgn
, qd
.gray
)
809 EraseRgn(window
.GetWindowPort().visRgn
)
811 def do_activate(self
, activate
, event
):
812 if DEBUG
: print 'Activate %d for %s'%(activate
, self
.wid
)
814 class ControlsWindow(Window
):
816 def do_rawupdate(self
, window
, event
):
817 if DEBUG
: print "raw update for", window
820 self
.do_update(window
, event
)
821 #DrawControls(window) # jvr
822 UpdateControls(window
, window
.GetWindowPort().visRgn
) # jvr
823 window
.DrawGrowIcon()
826 def do_controlhit(self
, window
, control
, pcode
, event
):
827 if DEBUG
: print "control hit in", window
, "on", control
, "; pcode =", pcode
829 def do_inContent(self
, partcode
, window
, event
):
830 if FrontWindow() <> window
:
831 window
.SelectWindow()
833 (what
, message
, when
, where
, modifiers
) = event
834 SetPort(window
) # XXXX Needed?
835 local
= GlobalToLocal(where
)
836 pcode
, control
= FindControl(local
, window
)
837 if pcode
and control
:
838 self
.do_rawcontrolhit(window
, control
, pcode
, local
, event
)
840 if DEBUG
: print "FindControl(%s, %s) -> (%s, %s)" % \
841 (local
, window
, pcode
, control
)
842 self
.do_contentclick(local
, modifiers
, event
)
844 def do_rawcontrolhit(self
, window
, control
, pcode
, local
, event
):
845 pcode
= control
.TrackControl(local
)
847 self
.do_controlhit(window
, control
, pcode
, event
)
849 class ScrolledWindow(ControlsWindow
):
850 def __init__(self
, parent
):
851 self
.barx
= self
.bary
= None
852 self
.barx_enabled
= self
.bary_enabled
= 1
854 ControlsWindow
.__init
__(self
, parent
)
856 def scrollbars(self
, wantx
=1, wanty
=1):
858 self
.barx
= self
.bary
= None
859 self
.barx_enabled
= self
.bary_enabled
= 1
860 x0
, y0
, x1
, y1
= self
.wid
.GetWindowPort().portRect
861 vx
, vy
= self
.getscrollbarvalues()
862 if vx
== None: self
.barx_enabled
, vx
= 0, 0
863 if vy
== None: self
.bary_enabled
, vy
= 0, 0
865 rect
= x0
-1, y1
-(SCROLLBARWIDTH
-1), x1
-(SCROLLBARWIDTH
-2), y1
+1
866 self
.barx
= NewControl(self
.wid
, rect
, "", 1, vx
, 0, 32767, 16, 0)
867 if not self
.barx_enabled
: self
.barx
.HiliteControl(255)
868 ## self.wid.InvalWindowRect(rect)
870 rect
= x1
-(SCROLLBARWIDTH
-1), y0
-1, x1
+1, y1
-(SCROLLBARWIDTH
-2)
871 self
.bary
= NewControl(self
.wid
, rect
, "", 1, vy
, 0, 32767, 16, 0)
872 if not self
.bary_enabled
: self
.bary
.HiliteControl(255)
873 ## self.wid.InvalWindowRect(rect)
875 def do_postclose(self
):
876 self
.barx
= self
.bary
= None
877 ControlsWindow
.do_postclose(self
)
879 def do_activate(self
, onoff
, event
):
880 self
.activated
= onoff
882 if self
.barx
and self
.barx_enabled
:
883 self
.barx
.ShowControl() # jvr
884 if self
.bary
and self
.bary_enabled
:
885 self
.bary
.ShowControl() # jvr
888 self
.barx
.HideControl() # jvr; An inactive window should have *hidden*
889 # scrollbars, not just dimmed (no matter what
890 # BBEdit does... look at the Finder)
892 self
.bary
.HideControl() # jvr
893 self
.wid
.DrawGrowIcon() # jvr
895 def do_postresize(self
, width
, height
, window
):
896 l
, t
, r
, b
= self
.wid
.GetWindowPort().portRect
899 self
.barx
.HideControl() # jvr
900 self
.barx
.MoveControl(l
-1, b
-(SCROLLBARWIDTH
-1))
901 self
.barx
.SizeControl((r
-l
)-(SCROLLBARWIDTH
-3), SCROLLBARWIDTH
) # jvr
903 self
.bary
.HideControl() # jvr
904 self
.bary
.MoveControl(r
-(SCROLLBARWIDTH
-1), t
-1)
905 self
.bary
.SizeControl(SCROLLBARWIDTH
, (b
-t
)-(SCROLLBARWIDTH
-3)) # jvr
907 self
.barx
.ShowControl() # jvr
908 self
.wid
.ValidWindowRect((l
, b
- SCROLLBARWIDTH
+ 1, r
- SCROLLBARWIDTH
+ 2, b
)) # jvr
910 self
.bary
.ShowControl() # jvr
911 self
.wid
.ValidWindowRect((r
- SCROLLBARWIDTH
+ 1, t
, r
, b
- SCROLLBARWIDTH
+ 2)) # jvr
912 self
.wid
.InvalWindowRect((r
- SCROLLBARWIDTH
+ 1, b
- SCROLLBARWIDTH
+ 1, r
, b
)) # jvr, growicon
915 def do_rawcontrolhit(self
, window
, control
, pcode
, local
, event
):
916 if control
== self
.barx
:
918 elif control
== self
.bary
:
922 if pcode
in (inUpButton
, inDownButton
, inPageUp
, inPageDown
):
923 # We do the work for the buttons and grey area in the tracker
924 dummy
= control
.TrackControl(local
, self
.do_controltrack
)
926 # but the thumb is handled here
927 pcode
= control
.TrackControl(local
)
929 value
= control
.GetControlValue()
930 print 'setbars', which
, value
#DBG
931 self
.scrollbar_callback(which
, 'set', value
)
932 self
.updatescrollbars()
934 print 'funny part', pcode
#DBG
937 def do_controltrack(self
, control
, pcode
):
938 if control
== self
.barx
:
940 elif control
== self
.bary
:
945 if pcode
== inUpButton
:
947 elif pcode
== inDownButton
:
949 elif pcode
== inPageUp
:
951 elif pcode
== inPageDown
:
955 self
.scrollbar_callback(which
, what
, None)
956 self
.updatescrollbars()
958 def updatescrollbars(self
):
960 vx
, vy
= self
.getscrollbarvalues()
963 self
.barx
.HiliteControl(255)
964 self
.barx_enabled
= 0
966 if not self
.barx_enabled
:
967 self
.barx_enabled
= 1
969 self
.barx
.HiliteControl(0)
970 self
.barx
.SetControlValue(vx
)
973 self
.bary
.HiliteControl(255)
974 self
.bary_enabled
= 0
976 if not self
.bary_enabled
:
977 self
.bary_enabled
= 1
979 self
.bary
.HiliteControl(0)
980 self
.bary
.SetControlValue(vy
)
982 # Auxiliary function: convert standard text/image/etc coordinate
983 # to something palatable as getscrollbarvalues() return
984 def scalebarvalue(self
, absmin
, absmax
, curmin
, curmax
):
985 if curmin
<= absmin
and curmax
>= absmax
:
991 perc
= float(curmin
-absmin
)/float(absmax
-absmin
)
992 return int(perc
*32767)
996 def getscrollbarvalues(self
):
999 def scrollbar_callback(self
, which
, what
, value
):
1000 print 'scroll', which
, what
, value
1002 class DialogWindow(Window
):
1003 """A modeless dialog window"""
1005 def open(self
, resid
):
1006 self
.dlg
= GetNewDialog(resid
, -1)
1007 self
.wid
= self
.dlg
.GetDialogWindow()
1013 def do_itemhit(self
, item
, event
):
1014 print 'Dialog %s, item %d hit'%(self
.dlg
, item
)
1016 def do_rawupdate(self
, window
, event
):
1020 "Convert a long int to the 4-character code it really is"
1023 x
, c
= divmod(x
, 256)
1028 class TestApp(Application
):
1030 "This class is used by the test() function"
1032 def makeusermenus(self
):
1033 self
.filemenu
= m
= Menu(self
.menubar
, "File")
1034 self
.saveitem
= MenuItem(m
, "Save", "S", self
.save
)
1036 self
.optionsmenu
= mm
= SubMenu(m
, "Options")
1037 self
.opt1
= CheckItem(mm
, "Arguments", "A")
1038 self
.opt2
= CheckItem(mm
, "Being hit on the head lessons", (kMenuOptionModifier
, "A"))
1039 self
.opt3
= CheckItem(mm
, "Complaints", (kMenuOptionModifier|kMenuNoCommandModifier
, "A"))
1041 self
.quititem
= MenuItem(m
, "Quit", "Q", self
.quit
)
1043 def save(self
, *args
):
1046 def quit(self
, *args
):
1056 if __name__
== '__main__':