1 "A sort of application framework for the Mac"
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
.Menu
import *
17 from Carbon
.Menus
import *
18 from Carbon
.Qd
import *
19 from Carbon
.QuickDraw
import *
20 #from Carbon.Res import *
21 #from Carbon.Resources import *
22 #from Carbon.Snd import *
23 #from Carbon.Sound import *
24 from Carbon
.Win
import *
25 from Carbon
.Windows
import *
30 kHighLevelEvent
= 23 # Don't know what header file this should come from
31 SCROLLBARWIDTH
= 16 # Again, not a clue...
33 # Trick to forestall a set of SIOUX menus being added to our menubar
34 SIOUX_APPLEMENU_ID
=32000
37 # Map event 'what' field to strings
39 eventname
[1] = 'mouseDown'
40 eventname
[2] = 'mouseUp'
41 eventname
[3] = 'keyDown'
42 eventname
[4] = 'keyUp'
43 eventname
[5] = 'autoKey'
44 eventname
[6] = 'updateEvt'
45 eventname
[7] = 'diskEvt'
46 eventname
[8] = 'activateEvt'
47 eventname
[15] = 'osEvt'
48 eventname
[23] = 'kHighLevelEvent'
50 # Map part codes returned by WhichWindow() to strings
52 partname
[0] = 'inDesk'
53 partname
[1] = 'inMenuBar'
54 partname
[2] = 'inSysWindow'
55 partname
[3] = 'inContent'
56 partname
[4] = 'inDrag'
57 partname
[5] = 'inGrow'
58 partname
[6] = 'inGoAway'
59 partname
[7] = 'inZoomIn'
60 partname
[8] = 'inZoomOut'
63 # The useable portion of the screen
64 # ## but what happens with multiple screens? jvr
65 screenbounds
= qd
.screenBits
.bounds
66 screenbounds
= screenbounds
[0]+4, screenbounds
[1]+4, \
67 screenbounds
[2]-4, screenbounds
[3]-4
69 next_window_x
= 16 # jvr
70 next_window_y
= 44 # jvr
72 def windowbounds(width
, height
):
73 "Return sensible window bounds"
74 global next_window_x
, next_window_y
75 r
, b
= next_window_x
+width
, next_window_y
+height
76 if r
> screenbounds
[2]:
78 if b
> screenbounds
[3]:
80 l
, t
= next_window_x
, next_window_y
81 r
, b
= next_window_x
+width
, next_window_y
+height
82 next_window_x
, next_window_y
= next_window_x
+ 8, next_window_y
+ 20 # jvr
90 _watch
= GetCursor(4).data
98 "Application framework -- your application should be a derived class"
100 def __init__(self
, nomenubar
=0):
101 self
._doing
_asyncevents
= 0
103 self
.needmenubarredraw
= 0
111 if self
._doing
_asyncevents
:
112 self
._doing
_asyncevents
= 0
113 MacOS
.SetEventHandler()
115 def makemenubar(self
):
116 self
.menubar
= MenuBar(self
)
117 AppleMenu(self
.menubar
, self
.getabouttext(), self
.do_about
)
120 def makeusermenus(self
):
121 self
.filemenu
= m
= Menu(self
.menubar
, "File")
122 self
._quititem
= MenuItem(m
, "Quit", "Q", self
._quit
)
124 def _quit(self
, *args
):
128 for w
in self
._windows
.values():
130 return self
._windows
== {}
132 def appendwindow(self
, wid
, window
):
133 self
._windows
[wid
] = window
135 def removewindow(self
, wid
):
136 del self
._windows
[wid
]
138 def getabouttext(self
):
139 return "About %s..." % self
.__class
__.__name
__
141 def do_about(self
, id, item
, window
, event
):
142 EasyDialogs
.Message("Hello, world!" + "\015(%s)" % self
.__class
__.__name
__)
144 # The main event loop is broken up in several simple steps.
145 # This is done so you can override each individual part,
146 # if you have a need to do extra processing independent of the
148 # Normally, however, you'd just define handlers for individual
151 schedparams
= (0, 0) # By default disable Python's event handling
152 default_wait
= None # By default we wait GetCaretTime in WaitNextEvent
154 def mainloop(self
, mask
= everyEvent
, wait
= None):
156 saveparams
= apply(MacOS
.SchedParams
, self
.schedparams
)
158 while not self
.quitting
:
160 self
.do1event(mask
, wait
)
161 except (Application
, SystemExit):
162 # Note: the raising of "self" is old-fashioned idiom to
163 # exit the mainloop. Calling _quit() is better for new
167 apply(MacOS
.SchedParams
, saveparams
)
169 def dopendingevents(self
, mask
= everyEvent
):
170 """dopendingevents - Handle all pending events"""
171 while self
.do1event(mask
, wait
=0):
174 def do1event(self
, mask
= everyEvent
, wait
= None):
175 ok
, event
= self
.getevent(mask
, wait
)
176 if IsDialogEvent(event
):
177 if self
.do_dialogevent(event
):
184 def idle(self
, event
):
187 def getevent(self
, mask
= everyEvent
, wait
= None):
188 if self
.needmenubarredraw
:
190 self
.needmenubarredraw
= 0
192 wait
= self
.default_wait
194 wait
= GetCaretTime()
195 ok
, event
= WaitNextEvent(mask
, wait
)
198 def dispatch(self
, event
):
199 # The following appears to be double work (already done in do1event)
200 # but we need it for asynchronous event handling
201 if IsDialogEvent(event
):
202 if self
.do_dialogevent(event
):
204 (what
, message
, when
, where
, modifiers
) = event
205 if eventname
.has_key(what
):
206 name
= "do_" + eventname
[what
]
208 name
= "do_%d" % what
210 handler
= getattr(self
, name
)
211 except AttributeError:
212 handler
= self
.do_unknownevent
215 def asyncevents(self
, onoff
):
216 """asyncevents - Set asynchronous event handling on or off"""
217 old
= self
._doing
_asyncevents
219 MacOS
.SetEventHandler()
220 apply(MacOS
.SchedParams
, self
.schedparams
)
222 MacOS
.SetEventHandler(self
.dispatch
)
223 doint
, dummymask
, benice
, howoften
, bgyield
= \
225 MacOS
.SchedParams(doint
, everyEvent
, benice
,
227 self
._doing
_asyncevents
= onoff
230 def do_dialogevent(self
, event
):
231 gotone
, dlg
, item
= DialogSelect(event
)
233 window
= dlg
.GetDialogWindow()
234 if self
._windows
.has_key(window
):
235 self
._windows
[window
].do_itemhit(item
, event
)
237 print 'Dialog event for unknown dialog'
241 def do_mouseDown(self
, event
):
242 (what
, message
, when
, where
, modifiers
) = event
243 partcode
, wid
= FindWindow(where
)
246 # Find the correct name.
248 if partname
.has_key(partcode
):
249 name
= "do_" + partname
[partcode
]
251 name
= "do_%d" % partcode
254 # No window, or a non-python window
256 handler
= getattr(self
, name
)
257 except AttributeError:
258 # Not menubar or something, so assume someone
260 MacOS
.HandleEvent(event
)
262 elif self
._windows
.has_key(wid
):
263 # It is a window. Hand off to correct window.
264 window
= self
._windows
[wid
]
266 handler
= getattr(window
, name
)
267 except AttributeError:
268 handler
= self
.do_unknownpartcode
270 # It is a python-toolbox window, but not ours.
271 handler
= self
.do_unknownwindow
272 handler(partcode
, wid
, event
)
274 def do_inSysWindow(self
, partcode
, window
, event
):
275 MacOS
.HandleEvent(event
)
277 def do_inDesk(self
, partcode
, window
, event
):
278 MacOS
.HandleEvent(event
)
280 def do_inMenuBar(self
, partcode
, window
, event
):
282 MacOS
.HandleEvent(event
)
284 (what
, message
, when
, where
, modifiers
) = event
285 result
= MenuSelect(where
)
286 id = (result
>>16) & 0xffff # Hi word
287 item
= result
& 0xffff # Lo word
288 self
.do_rawmenu(id, item
, window
, event
)
290 def do_rawmenu(self
, id, item
, window
, event
):
292 self
.do_menu(id, item
, window
, event
)
296 def do_menu(self
, id, item
, window
, event
):
298 self
.menubar
.dispatch(id, item
, window
, event
)
301 def do_unknownpartcode(self
, partcode
, window
, event
):
302 (what
, message
, when
, where
, modifiers
) = event
303 if DEBUG
: print "Mouse down at global:", where
304 if DEBUG
: print "\tUnknown part code:", partcode
305 if DEBUG
: print "\tEvent:", self
.printevent(event
)
306 MacOS
.HandleEvent(event
)
308 def do_unknownwindow(self
, partcode
, window
, event
):
309 if DEBUG
: print 'Unknown window:', window
310 MacOS
.HandleEvent(event
)
312 def do_keyDown(self
, event
):
315 def do_autoKey(self
, event
):
316 if not event
[-1] & cmdKey
:
319 def do_key(self
, event
):
320 (what
, message
, when
, where
, modifiers
) = event
321 c
= chr(message
& charCodeMask
)
323 result
= MenuEvent(event
)
324 id = (result
>>16) & 0xffff # Hi word
325 item
= result
& 0xffff # Lo word
327 self
.do_rawmenu(id, item
, None, event
)
329 # Otherwise we fall-through
330 if modifiers
& cmdKey
:
335 MacOS
.HandleEvent(event
)
338 # See whether the front window wants it
340 if w
and self
._windows
.has_key(w
):
341 window
= self
._windows
[w
]
343 do_char
= window
.do_char
344 except AttributeError:
345 do_char
= self
.do_char
347 # else it wasn't for us, sigh...
349 def do_char(self
, c
, event
):
350 if DEBUG
: print "Character", `c`
352 def do_updateEvt(self
, event
):
353 (what
, message
, when
, where
, modifiers
) = event
354 wid
= WhichWindow(message
)
355 if wid
and self
._windows
.has_key(wid
):
356 window
= self
._windows
[wid
]
357 window
.do_rawupdate(wid
, event
)
359 MacOS
.HandleEvent(event
)
361 def do_activateEvt(self
, event
):
362 (what
, message
, when
, where
, modifiers
) = event
363 wid
= WhichWindow(message
)
364 if wid
and self
._windows
.has_key(wid
):
365 window
= self
._windows
[wid
]
366 window
.do_activate(modifiers
& 1, event
)
368 MacOS
.HandleEvent(event
)
370 def do_osEvt(self
, event
):
371 (what
, message
, when
, where
, modifiers
) = event
372 which
= (message
>> 24) & 0xff
373 if which
== 1: # suspend/resume
374 self
.do_suspendresume(event
)
377 print 'unknown osEvt:',
378 self
.printevent(event
)
380 def do_suspendresume(self
, event
):
381 (what
, message
, when
, where
, modifiers
) = event
383 if wid
and self
._windows
.has_key(wid
):
384 window
= self
._windows
[wid
]
385 window
.do_activate(message
& 1, event
)
387 def do_kHighLevelEvent(self
, event
):
388 (what
, message
, when
, where
, modifiers
) = event
390 print "High Level Event:",
391 self
.printevent(event
)
393 AEProcessAppleEvent(event
)
396 #print "AEProcessAppleEvent error:"
397 #traceback.print_exc()
399 def do_unknownevent(self
, event
):
401 print "Unhandled event:",
402 self
.printevent(event
)
404 def printevent(self
, event
):
405 (what
, message
, when
, where
, modifiers
) = event
407 if eventname
.has_key(what
):
408 nicewhat
= eventname
[what
]
410 if what
== kHighLevelEvent
:
412 print `
ostypecode(message
)`
, hex(when
), `
ostypecode(h |
(v
<<16))`
,
414 print hex(message
), hex(when
), where
,
419 """Represent a set of menus in a menu bar.
426 - addpopup (normally used internally)
427 - dispatch (called from Application)
430 nextid
= 1 # Necessarily a class variable
434 MenuBar
.nextid
= id+1
437 def __init__(self
, parent
=None):
440 self
.bar
= GetMenuBar()
449 def addmenu(self
, title
, after
= 0, id=None):
451 id = self
.getnextid()
452 if DEBUG
: print 'Newmenu', title
, id # XXXX
453 m
= NewMenu(id, title
)
457 self
.parent
.needmenubarredraw
= 1
462 def delmenu(self
, id):
463 if DEBUG
: print 'Delmenu', id # XXXX
466 def addpopup(self
, title
= ''):
467 return self
.addmenu(title
, -1)
471 # if not self.bar: return
472 # SetMenuBar(self.bar)
474 # self.parent.needmenubarredraw = 1
478 def fixmenudimstate(self
):
479 for m
in self
.menus
.keys():
481 if menu
.__class
__ == FrameWork
.AppleMenu
:
483 for i
in range(len(menu
.items
)):
484 label
, shortcut
, callback
, kind
= menu
.items
[i
]
485 if type(callback
) == types
.StringType
:
486 wid
= Win
.FrontWindow()
487 if wid
and self
.parent
._windows
.has_key(wid
):
488 window
= self
.parent
._windows
[wid
]
489 if hasattr(window
, "domenu_" + callback
):
490 menu
.menu
.EnableMenuItem(i
+ 1)
491 elif hasattr(self
.parent
, "domenu_" + callback
):
492 menu
.menu
.EnableMenuItem(i
+ 1)
494 menu
.menu
.DisableMenuItem(i
+ 1)
495 elif hasattr(self
.parent
, "domenu_" + callback
):
496 menu
.menu
.EnableMenuItem(i
+ 1)
498 menu
.menu
.DisableMenuItem(i
+ 1)
502 def dispatch(self
, id, item
, window
, event
):
503 if self
.menus
.has_key(id):
504 self
.menus
[id].dispatch(id, item
, window
, event
)
506 if DEBUG
: print "MenuBar.dispatch(%d, %d, %s, %s)" % \
507 (id, item
, window
, event
)
510 # XXX Need a way to get menus as resources and bind them to callbacks
515 def __init__(self
, bar
, title
, after
=0, id=None):
517 self
.id, self
.menu
= self
.bar
.addmenu(title
, after
, id)
518 bar
.menus
[self
.id] = self
523 self
.bar
.delmenu(self
.id)
524 del self
.bar
.menus
[self
.id]
525 self
.menu
.DisposeMenu()
532 def additem(self
, label
, shortcut
=None, callback
=None, kind
=None):
533 self
.menu
.AppendMenu('x') # add a dummy string
534 self
.items
.append((label
, shortcut
, callback
, kind
))
535 item
= len(self
.items
)
536 self
.menu
.SetMenuItemText(item
, label
) # set the actual text
537 if shortcut
and type(shortcut
) == type(()):
538 modifiers
, char
= shortcut
[:2]
539 self
.menu
.SetItemCmd(item
, ord(char
))
540 self
.menu
.SetMenuItemModifiers(item
, modifiers
)
541 if len(shortcut
) > 2:
542 self
.menu
.SetMenuItemKeyGlyph(item
, shortcut
[2])
544 self
.menu
.SetItemCmd(item
, ord(shortcut
))
547 def delitem(self
, item
):
548 if item
!= len(self
.items
):
549 raise 'Can only delete last item of a menu'
550 self
.menu
.DeleteMenuItem(item
)
551 del self
.items
[item
-1]
553 def addcheck(self
, label
, shortcut
=None, callback
=None):
554 return self
.additem(label
, shortcut
, callback
, 'check')
556 def addradio(self
, label
, shortcut
=None, callback
=None):
557 return self
.additem(label
, shortcut
, callback
, 'radio')
559 def addseparator(self
):
560 self
.menu
.AppendMenu('(-')
561 self
.items
.append(('', None, None, 'separator'))
563 def addsubmenu(self
, label
, title
=''):
564 sub
= Menu(self
.bar
, title
, -1)
565 item
= self
.additem(label
, '\x1B', None, 'submenu')
566 self
.menu
.SetItemMark(item
, sub
.id)
568 sub
._parent
_item
= item
571 def dispatch(self
, id, item
, window
, event
):
572 title
, shortcut
, callback
, mtype
= self
.items
[item
-1]
574 if not self
.bar
.parent
or type(callback
) <> types
.StringType
:
575 menuhandler
= callback
578 wid
= Win
.FrontWindow()
579 if wid
and self
.bar
.parent
._windows
.has_key(wid
):
580 window
= self
.bar
.parent
._windows
[wid
]
581 if hasattr(window
, "domenu_" + callback
):
582 menuhandler
= getattr(window
, "domenu_" + callback
)
583 elif hasattr(self
.bar
.parent
, "domenu_" + callback
):
584 menuhandler
= getattr(self
.bar
.parent
, "domenu_" + callback
)
586 # nothing we can do. we shouldn't have come this far
587 # since the menu item should have been disabled...
589 elif hasattr(self
.bar
.parent
, "domenu_" + callback
):
590 menuhandler
= getattr(self
.bar
.parent
, "domenu_" + callback
)
592 # nothing we can do. we shouldn't have come this far
593 # since the menu item should have been disabled...
595 menuhandler(id, item
, window
, event
)
597 def enable(self
, onoff
):
599 self
.menu
.EnableMenuItem(0)
601 self
._parent
.menu
.EnableMenuItem(self
._parent
_item
)
603 self
.menu
.DisableMenuItem(0)
605 self
._parent
.menu
.DisableMenuItem(self
._parent
_item
)
606 if self
.bar
and self
.bar
.parent
:
607 self
.bar
.parent
.needmenubarredraw
= 1
609 class PopupMenu(Menu
):
610 def __init__(self
, bar
):
611 Menu
.__init
__(self
, bar
, '(popup)', -1)
613 def popup(self
, x
, y
, event
, default
=1, window
=None):
614 # NOTE that x and y are global coordinates, and they should probably
615 # be topleft of the button the user clicked (not mouse-coordinates),
616 # so the popup nicely overlaps.
617 reply
= self
.menu
.PopUpMenuSelect(x
, y
, default
)
620 id = (reply
& 0xffff0000) >> 16
621 item
= reply
& 0xffff
623 wid
= Win
.FrontWindow()
625 window
= self
.bar
.parent
._windows
[wid
]
627 pass # If we can't find the window we pass None
628 self
.dispatch(id, item
, window
, event
)
631 def __init__(self
, menu
, title
, shortcut
=None, callback
=None, kind
=None):
632 self
.item
= menu
.additem(title
, shortcut
, callback
)
636 self
.menu
.delitem(self
.item
)
640 def check(self
, onoff
):
641 self
.menu
.menu
.CheckMenuItem(self
.item
, onoff
)
643 def enable(self
, onoff
):
645 self
.menu
.menu
.EnableMenuItem(self
.item
)
647 self
.menu
.menu
.DisableMenuItem(self
.item
)
649 def settext(self
, text
):
650 self
.menu
.menu
.SetMenuItemText(self
.item
, text
)
652 def setstyle(self
, style
):
653 self
.menu
.menu
.SetItemStyle(self
.item
, style
)
655 def seticon(self
, icon
):
656 self
.menu
.menu
.SetItemIcon(self
.item
, icon
)
658 def setcmd(self
, cmd
):
659 self
.menu
.menu
.SetItemCmd(self
.item
, cmd
)
661 def setmark(self
, cmd
):
662 self
.menu
.menu
.SetItemMark(self
.item
, cmd
)
665 class RadioItem(MenuItem
):
666 def __init__(self
, menu
, title
, shortcut
=None, callback
=None):
667 MenuItem
.__init
__(self
, menu
, title
, shortcut
, callback
, 'radio')
669 class CheckItem(MenuItem
):
670 def __init__(self
, menu
, title
, shortcut
=None, callback
=None):
671 MenuItem
.__init
__(self
, menu
, title
, shortcut
, callback
, 'check')
676 def SubMenu(menu
, label
, title
=''):
677 return menu
.addsubmenu(label
, title
)
680 class AppleMenu(Menu
):
682 def __init__(self
, bar
, abouttext
="About me...", aboutcallback
=None):
683 Menu
.__init
__(self
, bar
, "\024", id=SIOUX_APPLEMENU_ID
)
684 if MacOS
.runtimemodel
== 'ppc':
685 self
.additem(abouttext
, None, aboutcallback
)
687 self
.menu
.AppendResMenu('DRVR')
689 # Additem()'s tricks do not work for "apple" menu under Carbon
690 self
.menu
.InsertMenuItem(abouttext
, 0)
691 self
.items
.append((abouttext
, None, aboutcallback
, None))
693 def dispatch(self
, id, item
, window
, event
):
695 Menu
.dispatch(self
, id, item
, window
, event
)
696 elif MacOS
.runtimemodel
== 'ppc':
697 name
= self
.menu
.GetMenuItemText(item
)
701 """A single window belonging to an application"""
703 def __init__(self
, parent
):
707 def open(self
, bounds
=(40, 40, 400, 400), resid
=None):
709 self
.wid
= GetNewWindow(resid
, -1)
711 self
.wid
= NewWindow(bounds
, self
.__class
__.__name
__, 1,
712 8, -1, 1, 0) # changed to proc id 8 to include zoom box. jvr
715 def do_postopen(self
):
716 """Tell our parent we exist"""
717 self
.parent
.appendwindow(self
.wid
, self
)
722 def do_postclose(self
):
723 self
.parent
.removewindow(self
.wid
)
734 def do_inDrag(self
, partcode
, window
, event
):
736 window
.DragWindow(where
, self
.draglimit
)
738 draglimit
= screenbounds
740 def do_inGoAway(self
, partcode
, window
, event
):
742 if window
.TrackGoAway(where
):
745 def do_inZoom(self
, partcode
, window
, event
):
746 (what
, message
, when
, where
, modifiers
) = event
747 if window
.TrackBox(where
, partcode
):
748 window
.ZoomWindow(partcode
, 1)
749 rect
= window
.GetWindowUserState() # so that zoom really works... jvr
750 self
.do_postresize(rect
[2] - rect
[0], rect
[3] - rect
[1], window
) # jvr
752 def do_inZoomIn(self
, partcode
, window
, event
):
753 SetPort(window
) # !!!
754 self
.do_inZoom(partcode
, window
, event
)
756 def do_inZoomOut(self
, partcode
, window
, event
):
757 SetPort(window
) # !!!
758 self
.do_inZoom(partcode
, window
, event
)
760 def do_inGrow(self
, partcode
, window
, event
):
761 (what
, message
, when
, where
, modifiers
) = event
762 result
= window
.GrowWindow(where
, self
.growlimit
)
764 height
= (result
>>16) & 0xffff # Hi word
765 width
= result
& 0xffff # Lo word
766 self
.do_resize(width
, height
, window
)
768 growlimit
= (50, 50, screenbounds
[2] - screenbounds
[0], screenbounds
[3] - screenbounds
[1]) # jvr
770 def do_resize(self
, width
, height
, window
):
771 l
, t
, r
, b
= self
.wid
.GetWindowPort().portRect
# jvr, forGrowIcon
773 self
.wid
.InvalWindowRect((r
- SCROLLBARWIDTH
+ 1, b
- SCROLLBARWIDTH
+ 1, r
, b
)) # jvr
774 window
.SizeWindow(width
, height
, 1) # changed updateFlag to true jvr
775 self
.do_postresize(width
, height
, window
)
777 def do_postresize(self
, width
, height
, window
):
779 self
.wid
.InvalWindowRect(window
.GetWindowPort().portRect
)
781 def do_inContent(self
, partcode
, window
, event
):
783 # If we're not frontmost, select ourselves and wait for
784 # the activate event.
786 if FrontWindow() <> window
:
787 window
.SelectWindow()
789 # We are. Handle the event.
790 (what
, message
, when
, where
, modifiers
) = event
792 local
= GlobalToLocal(where
)
793 self
.do_contentclick(local
, modifiers
, event
)
795 def do_contentclick(self
, local
, modifiers
, event
):
797 print 'Click in contents at %s, modifiers %s'%(local
, modifiers
)
799 def do_rawupdate(self
, window
, event
):
800 if DEBUG
: print "raw update for", window
803 self
.do_update(window
, event
)
806 def do_update(self
, window
, event
):
811 InvertRgn(window
.GetWindowPort().visRgn
)
812 FillRgn(window
.GetWindowPort().visRgn
, qd
.gray
)
814 EraseRgn(window
.GetWindowPort().visRgn
)
816 def do_activate(self
, activate
, event
):
817 if DEBUG
: print 'Activate %d for %s'%(activate
, self
.wid
)
819 class ControlsWindow(Window
):
821 def do_rawupdate(self
, window
, event
):
822 if DEBUG
: print "raw update for", window
825 self
.do_update(window
, event
)
826 #DrawControls(window) # jvr
827 UpdateControls(window
, window
.GetWindowPort().visRgn
) # jvr
828 window
.DrawGrowIcon()
831 def do_controlhit(self
, window
, control
, pcode
, event
):
832 if DEBUG
: print "control hit in", window
, "on", control
, "; pcode =", pcode
834 def do_inContent(self
, partcode
, window
, event
):
835 if FrontWindow() <> window
:
836 window
.SelectWindow()
838 (what
, message
, when
, where
, modifiers
) = event
839 SetPort(window
) # XXXX Needed?
840 local
= GlobalToLocal(where
)
841 pcode
, control
= FindControl(local
, window
)
842 if pcode
and control
:
843 self
.do_rawcontrolhit(window
, control
, pcode
, local
, event
)
845 if DEBUG
: print "FindControl(%s, %s) -> (%s, %s)" % \
846 (local
, window
, pcode
, control
)
847 self
.do_contentclick(local
, modifiers
, event
)
849 def do_rawcontrolhit(self
, window
, control
, pcode
, local
, event
):
850 pcode
= control
.TrackControl(local
)
852 self
.do_controlhit(window
, control
, pcode
, event
)
854 class ScrolledWindow(ControlsWindow
):
855 def __init__(self
, parent
):
856 self
.barx
= self
.bary
= None
857 self
.barx_enabled
= self
.bary_enabled
= 1
859 ControlsWindow
.__init
__(self
, parent
)
861 def scrollbars(self
, wantx
=1, wanty
=1):
863 self
.barx
= self
.bary
= None
864 self
.barx_enabled
= self
.bary_enabled
= 1
865 x0
, y0
, x1
, y1
= self
.wid
.GetWindowPort().portRect
866 vx
, vy
= self
.getscrollbarvalues()
867 if vx
== None: self
.barx_enabled
, vx
= 0, 0
868 if vy
== None: self
.bary_enabled
, vy
= 0, 0
870 rect
= x0
-1, y1
-(SCROLLBARWIDTH
-1), x1
-(SCROLLBARWIDTH
-2), y1
+1
871 self
.barx
= NewControl(self
.wid
, rect
, "", 1, vx
, 0, 32767, 16, 0)
872 if not self
.barx_enabled
: self
.barx
.HiliteControl(255)
873 ## self.wid.InvalWindowRect(rect)
875 rect
= x1
-(SCROLLBARWIDTH
-1), y0
-1, x1
+1, y1
-(SCROLLBARWIDTH
-2)
876 self
.bary
= NewControl(self
.wid
, rect
, "", 1, vy
, 0, 32767, 16, 0)
877 if not self
.bary_enabled
: self
.bary
.HiliteControl(255)
878 ## self.wid.InvalWindowRect(rect)
880 def do_postclose(self
):
881 self
.barx
= self
.bary
= None
882 ControlsWindow
.do_postclose(self
)
884 def do_activate(self
, onoff
, event
):
885 self
.activated
= onoff
887 if self
.barx
and self
.barx_enabled
:
888 self
.barx
.ShowControl() # jvr
889 if self
.bary
and self
.bary_enabled
:
890 self
.bary
.ShowControl() # jvr
893 self
.barx
.HideControl() # jvr; An inactive window should have *hidden*
894 # scrollbars, not just dimmed (no matter what
895 # BBEdit does... look at the Finder)
897 self
.bary
.HideControl() # jvr
898 self
.wid
.DrawGrowIcon() # jvr
900 def do_postresize(self
, width
, height
, window
):
901 l
, t
, r
, b
= self
.wid
.GetWindowPort().portRect
904 self
.barx
.HideControl() # jvr
905 self
.barx
.MoveControl(l
-1, b
-(SCROLLBARWIDTH
-1))
906 self
.barx
.SizeControl((r
-l
)-(SCROLLBARWIDTH
-3), SCROLLBARWIDTH
) # jvr
908 self
.bary
.HideControl() # jvr
909 self
.bary
.MoveControl(r
-(SCROLLBARWIDTH
-1), t
-1)
910 self
.bary
.SizeControl(SCROLLBARWIDTH
, (b
-t
)-(SCROLLBARWIDTH
-3)) # jvr
912 self
.barx
.ShowControl() # jvr
913 self
.wid
.ValidWindowRect((l
, b
- SCROLLBARWIDTH
+ 1, r
- SCROLLBARWIDTH
+ 2, b
)) # jvr
915 self
.bary
.ShowControl() # jvr
916 self
.wid
.ValidWindowRect((r
- SCROLLBARWIDTH
+ 1, t
, r
, b
- SCROLLBARWIDTH
+ 2)) # jvr
917 self
.wid
.InvalWindowRect((r
- SCROLLBARWIDTH
+ 1, b
- SCROLLBARWIDTH
+ 1, r
, b
)) # jvr, growicon
920 def do_rawcontrolhit(self
, window
, control
, pcode
, local
, event
):
921 if control
== self
.barx
:
923 elif control
== self
.bary
:
927 if pcode
in (inUpButton
, inDownButton
, inPageUp
, inPageDown
):
928 # We do the work for the buttons and grey area in the tracker
929 dummy
= control
.TrackControl(local
, self
.do_controltrack
)
931 # but the thumb is handled here
932 pcode
= control
.TrackControl(local
)
934 value
= control
.GetControlValue()
935 print 'setbars', which
, value
#DBG
936 self
.scrollbar_callback(which
, 'set', value
)
937 self
.updatescrollbars()
939 print 'funny part', pcode
#DBG
942 def do_controltrack(self
, control
, pcode
):
943 if control
== self
.barx
:
945 elif control
== self
.bary
:
950 if pcode
== inUpButton
:
952 elif pcode
== inDownButton
:
954 elif pcode
== inPageUp
:
956 elif pcode
== inPageDown
:
960 self
.scrollbar_callback(which
, what
, None)
961 self
.updatescrollbars()
963 def updatescrollbars(self
):
965 vx
, vy
= self
.getscrollbarvalues()
968 self
.barx
.HiliteControl(255)
969 self
.barx_enabled
= 0
971 if not self
.barx_enabled
:
972 self
.barx_enabled
= 1
974 self
.barx
.HiliteControl(0)
975 self
.barx
.SetControlValue(vx
)
978 self
.bary
.HiliteControl(255)
979 self
.bary_enabled
= 0
981 if not self
.bary_enabled
:
982 self
.bary_enabled
= 1
984 self
.bary
.HiliteControl(0)
985 self
.bary
.SetControlValue(vy
)
987 # Auxiliary function: convert standard text/image/etc coordinate
988 # to something palatable as getscrollbarvalues() return
989 def scalebarvalue(self
, absmin
, absmax
, curmin
, curmax
):
990 if curmin
<= absmin
and curmax
>= absmax
:
996 perc
= float(curmin
-absmin
)/float(absmax
-absmin
)
997 return int(perc
*32767)
1001 def getscrollbarvalues(self
):
1004 def scrollbar_callback(self
, which
, what
, value
):
1005 print 'scroll', which
, what
, value
1007 class DialogWindow(Window
):
1008 """A modeless dialog window"""
1010 def open(self
, resid
):
1011 self
.dlg
= GetNewDialog(resid
, -1)
1012 self
.wid
= self
.dlg
.GetDialogWindow()
1018 def do_postclose(self
):
1020 Window
.do_postclose(self
)
1022 def do_itemhit(self
, item
, event
):
1023 print 'Dialog %s, item %d hit'%(self
.dlg
, item
)
1025 def do_rawupdate(self
, window
, event
):
1029 "Convert a long int to the 4-character code it really is"
1032 x
, c
= divmod(x
, 256)
1037 class TestApp(Application
):
1039 "This class is used by the test() function"
1041 def makeusermenus(self
):
1042 self
.filemenu
= m
= Menu(self
.menubar
, "File")
1043 self
.saveitem
= MenuItem(m
, "Save", "S", self
.save
)
1045 self
.optionsmenu
= mm
= SubMenu(m
, "Options")
1046 self
.opt1
= CheckItem(mm
, "Arguments", "A")
1047 self
.opt2
= CheckItem(mm
, "Being hit on the head lessons", (kMenuOptionModifier
, "A"))
1048 self
.opt3
= CheckItem(mm
, "Complaints", (kMenuOptionModifier|kMenuNoCommandModifier
, "A"))
1050 self
.quititem
= MenuItem(m
, "Quit", "Q", self
.quit
)
1052 def save(self
, *args
):
1055 def quit(self
, *args
):
1065 if __name__
== '__main__':