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
.Help
import *
17 from Carbon
.Menu
import *
18 from Carbon
.Menus
import *
19 from Carbon
.Qd
import *
20 from Carbon
.QuickDraw
import *
21 #from Carbon.Res import *
22 #from Carbon.Resources import *
23 #from Carbon.Snd import *
24 #from Carbon.Sound import *
25 from Carbon
.Win
import *
26 from Carbon
.Windows
import *
32 MyFrontWindow
= FrontNonFloatingWindow
34 MyFrontWindow
= FrontWindow
36 kHighLevelEvent
= 23 # Don't know what header file this should come from
37 SCROLLBARWIDTH
= 16 # Again, not a clue...
39 # Trick to forestall a set of SIOUX menus being added to our menubar
40 SIOUX_APPLEMENU_ID
=32000
43 # Map event 'what' field to strings
45 eventname
[1] = 'mouseDown'
46 eventname
[2] = 'mouseUp'
47 eventname
[3] = 'keyDown'
48 eventname
[4] = 'keyUp'
49 eventname
[5] = 'autoKey'
50 eventname
[6] = 'updateEvt'
51 eventname
[7] = 'diskEvt'
52 eventname
[8] = 'activateEvt'
53 eventname
[15] = 'osEvt'
54 eventname
[23] = 'kHighLevelEvent'
56 # Map part codes returned by WhichWindow() to strings
58 partname
[0] = 'inDesk'
59 partname
[1] = 'inMenuBar'
60 partname
[2] = 'inSysWindow'
61 partname
[3] = 'inContent'
62 partname
[4] = 'inDrag'
63 partname
[5] = 'inGrow'
64 partname
[6] = 'inGoAway'
65 partname
[7] = 'inZoomIn'
66 partname
[8] = 'inZoomOut'
69 # The useable portion of the screen
70 # ## but what happens with multiple screens? jvr
71 screenbounds
= GetQDGlobalsScreenBits().bounds
72 screenbounds
= screenbounds
[0]+4, screenbounds
[1]+4, \
73 screenbounds
[2]-4, screenbounds
[3]-4
75 next_window_x
= 16 # jvr
76 next_window_y
= 44 # jvr
78 def windowbounds(width
, height
):
79 "Return sensible window bounds"
80 global next_window_x
, next_window_y
81 r
, b
= next_window_x
+width
, next_window_y
+height
82 if r
> screenbounds
[2]:
84 if b
> screenbounds
[3]:
86 l
, t
= next_window_x
, next_window_y
87 r
, b
= next_window_x
+width
, next_window_y
+height
88 next_window_x
, next_window_y
= next_window_x
+ 8, next_window_y
+ 20 # jvr
96 _watch
= GetCursor(4).data
100 SetCursor(GetQDGlobalsArrow())
104 "Application framework -- your application should be a derived class"
106 def __init__(self
, nomenubar
=0):
107 self
._doing
_asyncevents
= 0
109 self
.needmenubarredraw
= 0
111 self
._helpmenu
= None
118 if self
._doing
_asyncevents
:
119 self
._doing
_asyncevents
= 0
120 MacOS
.SetEventHandler()
122 def makemenubar(self
):
123 self
.menubar
= MenuBar(self
)
124 AppleMenu(self
.menubar
, self
.getabouttext(), self
.do_about
)
127 def makeusermenus(self
):
128 self
.filemenu
= m
= Menu(self
.menubar
, "File")
129 self
._quititem
= MenuItem(m
, "Quit", "Q", self
._quit
)
131 def gethelpmenu(self
):
132 if self
._helpmenu
== None:
133 self
._helpmenu
= HelpMenu(self
.menubar
)
134 return self
._helpmenu
136 def _quit(self
, *args
):
140 for w
in self
._windows
.values():
142 return self
._windows
== {}
144 def appendwindow(self
, wid
, window
):
145 self
._windows
[wid
] = window
147 def removewindow(self
, wid
):
148 del self
._windows
[wid
]
150 def getabouttext(self
):
151 return "About %s..." % self
.__class
__.__name
__
153 def do_about(self
, id, item
, window
, event
):
154 EasyDialogs
.Message("Hello, world!" + "\015(%s)" % self
.__class
__.__name
__)
156 # The main event loop is broken up in several simple steps.
157 # This is done so you can override each individual part,
158 # if you have a need to do extra processing independent of the
160 # Normally, however, you'd just define handlers for individual
163 schedparams
= (0, 0) # By default disable Python's event handling
164 default_wait
= None # By default we wait GetCaretTime in WaitNextEvent
166 def mainloop(self
, mask
= everyEvent
, wait
= None):
168 if hasattr(MacOS
, 'SchedParams'):
169 saveparams
= MacOS
.SchedParams(*self
.schedparams
)
171 while not self
.quitting
:
173 self
.do1event(mask
, wait
)
174 except (Application
, SystemExit):
175 # Note: the raising of "self" is old-fashioned idiom to
176 # exit the mainloop. Calling _quit() is better for new
180 if hasattr(MacOS
, 'SchedParams'):
181 MacOS
.SchedParams(*saveparams
)
183 def dopendingevents(self
, mask
= everyEvent
):
184 """dopendingevents - Handle all pending events"""
185 while self
.do1event(mask
, wait
=0):
188 def do1event(self
, mask
= everyEvent
, wait
= None):
189 ok
, event
= self
.getevent(mask
, wait
)
190 if IsDialogEvent(event
):
191 if self
.do_dialogevent(event
):
198 def idle(self
, event
):
201 def getevent(self
, mask
= everyEvent
, wait
= None):
202 if self
.needmenubarredraw
:
204 self
.needmenubarredraw
= 0
206 wait
= self
.default_wait
208 wait
= GetCaretTime()
209 ok
, event
= WaitNextEvent(mask
, wait
)
212 def dispatch(self
, event
):
213 # The following appears to be double work (already done in do1event)
214 # but we need it for asynchronous event handling
215 if IsDialogEvent(event
):
216 if self
.do_dialogevent(event
):
218 (what
, message
, when
, where
, modifiers
) = event
219 if eventname
.has_key(what
):
220 name
= "do_" + eventname
[what
]
222 name
= "do_%d" % what
224 handler
= getattr(self
, name
)
225 except AttributeError:
226 handler
= self
.do_unknownevent
229 def asyncevents(self
, onoff
):
230 """asyncevents - Set asynchronous event handling on or off"""
231 if MacOS
.runtimemodel
== 'macho':
232 raise 'Unsupported in MachoPython'
233 old
= self
._doing
_asyncevents
235 MacOS
.SetEventHandler()
236 MacOS
.SchedParams(*self
.schedparams
)
238 MacOS
.SetEventHandler(self
.dispatch
)
239 doint
, dummymask
, benice
, howoften
, bgyield
= \
241 MacOS
.SchedParams(doint
, everyEvent
, benice
,
243 self
._doing
_asyncevents
= onoff
246 def do_dialogevent(self
, event
):
247 gotone
, dlg
, item
= DialogSelect(event
)
249 window
= dlg
.GetDialogWindow()
250 if self
._windows
.has_key(window
):
251 self
._windows
[window
].do_itemhit(item
, event
)
253 print 'Dialog event for unknown dialog'
257 def do_mouseDown(self
, event
):
258 (what
, message
, when
, where
, modifiers
) = event
259 partcode
, wid
= FindWindow(where
)
262 # Find the correct name.
264 if partname
.has_key(partcode
):
265 name
= "do_" + partname
[partcode
]
267 name
= "do_%d" % partcode
270 # No window, or a non-python window
272 handler
= getattr(self
, name
)
273 except AttributeError:
274 # Not menubar or something, so assume someone
276 if hasattr(MacOS
, 'HandleEvent'):
277 MacOS
.HandleEvent(event
)
279 elif self
._windows
.has_key(wid
):
280 # It is a window. Hand off to correct window.
281 window
= self
._windows
[wid
]
283 handler
= getattr(window
, name
)
284 except AttributeError:
285 handler
= self
.do_unknownpartcode
287 # It is a python-toolbox window, but not ours.
288 handler
= self
.do_unknownwindow
289 handler(partcode
, wid
, event
)
291 def do_inSysWindow(self
, partcode
, window
, event
):
292 if hasattr(MacOS
, 'HandleEvent'):
293 MacOS
.HandleEvent(event
)
295 def do_inDesk(self
, partcode
, window
, event
):
296 if hasattr(MacOS
, 'HandleEvent'):
297 MacOS
.HandleEvent(event
)
299 def do_inMenuBar(self
, partcode
, window
, event
):
301 if hasattr(MacOS
, 'HandleEvent'):
302 MacOS
.HandleEvent(event
)
304 (what
, message
, when
, where
, modifiers
) = event
305 result
= MenuSelect(where
)
306 id = (result
>>16) & 0xffff # Hi word
309 item
= result
& 0xffff # Lo word
310 self
.do_rawmenu(id, item
, window
, event
)
312 def do_rawmenu(self
, id, item
, window
, event
):
314 self
.do_menu(id, item
, window
, event
)
318 def do_menu(self
, id, item
, window
, event
):
319 if hasattr(MacOS
, 'OutputSeen'):
321 self
.menubar
.dispatch(id, item
, window
, event
)
324 def do_unknownpartcode(self
, partcode
, window
, event
):
325 (what
, message
, when
, where
, modifiers
) = event
326 if DEBUG
: print "Mouse down at global:", where
327 if DEBUG
: print "\tUnknown part code:", partcode
328 if DEBUG
: print "\tEvent:", self
.printevent(event
)
329 if hasattr(MacOS
, 'HandleEvent'):
330 MacOS
.HandleEvent(event
)
332 def do_unknownwindow(self
, partcode
, window
, event
):
333 if DEBUG
: print 'Unknown window:', window
334 if hasattr(MacOS
, 'HandleEvent'):
335 MacOS
.HandleEvent(event
)
337 def do_keyDown(self
, event
):
340 def do_autoKey(self
, event
):
341 if not event
[-1] & cmdKey
:
344 def do_key(self
, event
):
345 (what
, message
, when
, where
, modifiers
) = event
346 c
= chr(message
& charCodeMask
)
348 result
= MenuEvent(event
)
349 id = (result
>>16) & 0xffff # Hi word
350 item
= result
& 0xffff # Lo word
352 self
.do_rawmenu(id, item
, None, event
)
354 # Otherwise we fall-through
355 if modifiers
& cmdKey
:
360 if hasattr(MacOS
, 'HandleEvent'):
361 MacOS
.HandleEvent(event
)
364 # See whether the front window wants it
366 if w
and self
._windows
.has_key(w
):
367 window
= self
._windows
[w
]
369 do_char
= window
.do_char
370 except AttributeError:
371 do_char
= self
.do_char
373 # else it wasn't for us, sigh...
375 def do_char(self
, c
, event
):
376 if DEBUG
: print "Character", repr(c
)
378 def do_updateEvt(self
, event
):
379 (what
, message
, when
, where
, modifiers
) = event
380 wid
= WhichWindow(message
)
381 if wid
and self
._windows
.has_key(wid
):
382 window
= self
._windows
[wid
]
383 window
.do_rawupdate(wid
, event
)
385 if hasattr(MacOS
, 'HandleEvent'):
386 MacOS
.HandleEvent(event
)
388 def do_activateEvt(self
, event
):
389 (what
, message
, when
, where
, modifiers
) = event
390 wid
= WhichWindow(message
)
391 if wid
and self
._windows
.has_key(wid
):
392 window
= self
._windows
[wid
]
393 window
.do_activate(modifiers
& 1, event
)
395 if hasattr(MacOS
, 'HandleEvent'):
396 MacOS
.HandleEvent(event
)
398 def do_osEvt(self
, event
):
399 (what
, message
, when
, where
, modifiers
) = event
400 which
= (message
>> 24) & 0xff
401 if which
== 1: # suspend/resume
402 self
.do_suspendresume(event
)
405 print 'unknown osEvt:',
406 self
.printevent(event
)
408 def do_suspendresume(self
, event
):
409 (what
, message
, when
, where
, modifiers
) = event
410 wid
= MyFrontWindow()
411 if wid
and self
._windows
.has_key(wid
):
412 window
= self
._windows
[wid
]
413 window
.do_activate(message
& 1, event
)
415 def do_kHighLevelEvent(self
, event
):
416 (what
, message
, when
, where
, modifiers
) = event
418 print "High Level Event:",
419 self
.printevent(event
)
421 AEProcessAppleEvent(event
)
424 #print "AEProcessAppleEvent error:"
425 #traceback.print_exc()
427 def do_unknownevent(self
, event
):
429 print "Unhandled event:",
430 self
.printevent(event
)
432 def printevent(self
, event
):
433 (what
, message
, when
, where
, modifiers
) = event
434 nicewhat
= repr(what
)
435 if eventname
.has_key(what
):
436 nicewhat
= eventname
[what
]
438 if what
== kHighLevelEvent
:
440 print repr(ostypecode(message
)), hex(when
), repr(ostypecode(h |
(v
<<16))),
442 print hex(message
), hex(when
), where
,
447 """Represent a set of menus in a menu bar.
454 - addpopup (normally used internally)
455 - dispatch (called from Application)
458 nextid
= 1 # Necessarily a class variable
462 MenuBar
.nextid
= id+1
465 def __init__(self
, parent
=None):
468 self
.bar
= GetMenuBar()
477 def addmenu(self
, title
, after
= 0, id=None):
479 id = self
.getnextid()
480 if DEBUG
: print 'Newmenu', title
, id # XXXX
481 m
= NewMenu(id, title
)
485 self
.parent
.needmenubarredraw
= 1
490 def delmenu(self
, id):
491 if DEBUG
: print 'Delmenu', id # XXXX
494 def addpopup(self
, title
= ''):
495 return self
.addmenu(title
, -1)
499 # if not self.bar: return
500 # SetMenuBar(self.bar)
502 # self.parent.needmenubarredraw = 1
506 def fixmenudimstate(self
):
507 for m
in self
.menus
.keys():
509 if menu
.__class
__ == FrameWork
.AppleMenu
:
511 for i
in range(len(menu
.items
)):
512 label
, shortcut
, callback
, kind
= menu
.items
[i
]
513 if type(callback
) == types
.StringType
:
514 wid
= MyFrontWindow()
515 if wid
and self
.parent
._windows
.has_key(wid
):
516 window
= self
.parent
._windows
[wid
]
517 if hasattr(window
, "domenu_" + callback
):
518 menu
.menu
.EnableMenuItem(i
+ 1)
519 elif hasattr(self
.parent
, "domenu_" + callback
):
520 menu
.menu
.EnableMenuItem(i
+ 1)
522 menu
.menu
.DisableMenuItem(i
+ 1)
523 elif hasattr(self
.parent
, "domenu_" + callback
):
524 menu
.menu
.EnableMenuItem(i
+ 1)
526 menu
.menu
.DisableMenuItem(i
+ 1)
530 def dispatch(self
, id, item
, window
, event
):
531 if self
.menus
.has_key(id):
532 self
.menus
[id].dispatch(id, item
, window
, event
)
534 if DEBUG
: print "MenuBar.dispatch(%d, %d, %s, %s)" % \
535 (id, item
, window
, event
)
538 # XXX Need a way to get menus as resources and bind them to callbacks
543 def __init__(self
, bar
, title
, after
=0, id=None):
545 self
.id, self
.menu
= self
.bar
.addmenu(title
, after
, id)
546 bar
.menus
[self
.id] = self
551 self
.bar
.delmenu(self
.id)
552 del self
.bar
.menus
[self
.id]
553 self
.menu
.DisposeMenu()
560 def additem(self
, label
, shortcut
=None, callback
=None, kind
=None):
561 self
.menu
.AppendMenu('x') # add a dummy string
562 self
.items
.append((label
, shortcut
, callback
, kind
))
563 item
= len(self
.items
)
564 if isinstance(label
, unicode):
565 self
.menu
.SetMenuItemTextWithCFString(item
, label
)
567 self
.menu
.SetMenuItemText(item
, label
)
568 if shortcut
and type(shortcut
) == type(()):
569 modifiers
, char
= shortcut
[:2]
570 self
.menu
.SetItemCmd(item
, ord(char
))
571 self
.menu
.SetMenuItemModifiers(item
, modifiers
)
572 if len(shortcut
) > 2:
573 self
.menu
.SetMenuItemKeyGlyph(item
, shortcut
[2])
575 self
.menu
.SetItemCmd(item
, ord(shortcut
))
578 def delitem(self
, item
):
579 if item
!= len(self
.items
):
580 raise 'Can only delete last item of a menu'
581 self
.menu
.DeleteMenuItem(item
)
582 del self
.items
[item
-1]
584 def addcheck(self
, label
, shortcut
=None, callback
=None):
585 return self
.additem(label
, shortcut
, callback
, 'check')
587 def addradio(self
, label
, shortcut
=None, callback
=None):
588 return self
.additem(label
, shortcut
, callback
, 'radio')
590 def addseparator(self
):
591 self
.menu
.AppendMenu('(-')
592 self
.items
.append(('', None, None, 'separator'))
594 def addsubmenu(self
, label
, title
=''):
595 sub
= Menu(self
.bar
, title
, -1)
596 item
= self
.additem(label
, '\x1B', None, 'submenu')
597 self
.menu
.SetItemMark(item
, sub
.id)
599 sub
._parent
_item
= item
602 def dispatch(self
, id, item
, window
, event
):
603 title
, shortcut
, callback
, mtype
= self
.items
[item
-1]
605 if not self
.bar
.parent
or type(callback
) <> types
.StringType
:
606 menuhandler
= callback
609 wid
= MyFrontWindow()
610 if wid
and self
.bar
.parent
._windows
.has_key(wid
):
611 window
= self
.bar
.parent
._windows
[wid
]
612 if hasattr(window
, "domenu_" + callback
):
613 menuhandler
= getattr(window
, "domenu_" + callback
)
614 elif hasattr(self
.bar
.parent
, "domenu_" + callback
):
615 menuhandler
= getattr(self
.bar
.parent
, "domenu_" + callback
)
617 # nothing we can do. we shouldn't have come this far
618 # since the menu item should have been disabled...
620 elif hasattr(self
.bar
.parent
, "domenu_" + callback
):
621 menuhandler
= getattr(self
.bar
.parent
, "domenu_" + callback
)
623 # nothing we can do. we shouldn't have come this far
624 # since the menu item should have been disabled...
626 menuhandler(id, item
, window
, event
)
628 def enable(self
, onoff
):
630 self
.menu
.EnableMenuItem(0)
632 self
._parent
.menu
.EnableMenuItem(self
._parent
_item
)
634 self
.menu
.DisableMenuItem(0)
636 self
._parent
.menu
.DisableMenuItem(self
._parent
_item
)
637 if self
.bar
and self
.bar
.parent
:
638 self
.bar
.parent
.needmenubarredraw
= 1
640 class PopupMenu(Menu
):
641 def __init__(self
, bar
):
642 Menu
.__init
__(self
, bar
, '(popup)', -1)
644 def popup(self
, x
, y
, event
, default
=1, window
=None):
645 # NOTE that x and y are global coordinates, and they should probably
646 # be topleft of the button the user clicked (not mouse-coordinates),
647 # so the popup nicely overlaps.
648 reply
= self
.menu
.PopUpMenuSelect(x
, y
, default
)
651 id = (reply
>> 16) & 0xffff
652 item
= reply
& 0xffff
654 wid
= MyFrontWindow()
656 window
= self
.bar
.parent
._windows
[wid
]
658 pass # If we can't find the window we pass None
659 self
.dispatch(id, item
, window
, event
)
662 def __init__(self
, menu
, title
, shortcut
=None, callback
=None, kind
=None):
663 self
.item
= menu
.additem(title
, shortcut
, callback
)
667 self
.menu
.delitem(self
.item
)
671 def check(self
, onoff
):
672 self
.menu
.menu
.CheckMenuItem(self
.item
, onoff
)
674 def enable(self
, onoff
):
676 self
.menu
.menu
.EnableMenuItem(self
.item
)
678 self
.menu
.menu
.DisableMenuItem(self
.item
)
680 def settext(self
, text
):
681 self
.menu
.menu
.SetMenuItemText(self
.item
, text
)
683 def setstyle(self
, style
):
684 self
.menu
.menu
.SetItemStyle(self
.item
, style
)
686 def seticon(self
, icon
):
687 self
.menu
.menu
.SetItemIcon(self
.item
, icon
)
689 def setcmd(self
, cmd
):
690 self
.menu
.menu
.SetItemCmd(self
.item
, cmd
)
692 def setmark(self
, cmd
):
693 self
.menu
.menu
.SetItemMark(self
.item
, cmd
)
696 class RadioItem(MenuItem
):
697 def __init__(self
, menu
, title
, shortcut
=None, callback
=None):
698 MenuItem
.__init
__(self
, menu
, title
, shortcut
, callback
, 'radio')
700 class CheckItem(MenuItem
):
701 def __init__(self
, menu
, title
, shortcut
=None, callback
=None):
702 MenuItem
.__init
__(self
, menu
, title
, shortcut
, callback
, 'check')
707 def SubMenu(menu
, label
, title
=''):
708 return menu
.addsubmenu(label
, title
)
711 class AppleMenu(Menu
):
713 def __init__(self
, bar
, abouttext
="About me...", aboutcallback
=None):
714 Menu
.__init
__(self
, bar
, "\024", id=SIOUX_APPLEMENU_ID
)
715 if MacOS
.runtimemodel
== 'ppc':
716 self
.additem(abouttext
, None, aboutcallback
)
718 self
.menu
.AppendResMenu('DRVR')
720 # Additem()'s tricks do not work for "apple" menu under Carbon
721 self
.menu
.InsertMenuItem(abouttext
, 0)
722 self
.items
.append((abouttext
, None, aboutcallback
, None))
724 def dispatch(self
, id, item
, window
, event
):
726 Menu
.dispatch(self
, id, item
, window
, event
)
727 elif MacOS
.runtimemodel
== 'ppc':
728 name
= self
.menu
.GetMenuItemText(item
)
731 class HelpMenu(Menu
):
732 def __init__(self
, bar
):
733 # Note we don't call Menu.__init__, we do the necessary things by hand
735 self
.menu
, index
= HMGetHelpMenu()
736 self
.id = self
.menu
.GetMenuID()
737 bar
.menus
[self
.id] = self
738 # The next line caters for the entries the system already handles for us
739 self
.items
= [None]*(index
-1)
744 """A single window belonging to an application"""
746 def __init__(self
, parent
):
750 def open(self
, bounds
=(40, 40, 400, 400), resid
=None):
752 self
.wid
= GetNewWindow(resid
, -1)
754 self
.wid
= NewWindow(bounds
, self
.__class
__.__name
__, 1,
755 8, -1, 1, 0) # changed to proc id 8 to include zoom box. jvr
758 def do_postopen(self
):
759 """Tell our parent we exist"""
760 self
.parent
.appendwindow(self
.wid
, self
)
765 def do_postclose(self
):
766 self
.parent
.removewindow(self
.wid
)
777 def do_inDrag(self
, partcode
, window
, event
):
779 window
.DragWindow(where
, self
.draglimit
)
781 draglimit
= screenbounds
783 def do_inGoAway(self
, partcode
, window
, event
):
785 if window
.TrackGoAway(where
):
788 def do_inZoom(self
, partcode
, window
, event
):
789 (what
, message
, when
, where
, modifiers
) = event
790 if window
.TrackBox(where
, partcode
):
791 window
.ZoomWindow(partcode
, 1)
792 rect
= window
.GetWindowUserState() # so that zoom really works... jvr
793 self
.do_postresize(rect
[2] - rect
[0], rect
[3] - rect
[1], window
) # jvr
795 def do_inZoomIn(self
, partcode
, window
, event
):
796 SetPort(window
) # !!!
797 self
.do_inZoom(partcode
, window
, event
)
799 def do_inZoomOut(self
, partcode
, window
, event
):
800 SetPort(window
) # !!!
801 self
.do_inZoom(partcode
, window
, event
)
803 def do_inGrow(self
, partcode
, window
, event
):
804 (what
, message
, when
, where
, modifiers
) = event
805 result
= window
.GrowWindow(where
, self
.growlimit
)
807 height
= (result
>>16) & 0xffff # Hi word
808 width
= result
& 0xffff # Lo word
809 self
.do_resize(width
, height
, window
)
811 growlimit
= (50, 50, screenbounds
[2] - screenbounds
[0], screenbounds
[3] - screenbounds
[1]) # jvr
813 def do_resize(self
, width
, height
, window
):
814 l
, t
, r
, b
= self
.wid
.GetWindowPort().GetPortBounds() # jvr, forGrowIcon
816 self
.wid
.InvalWindowRect((r
- SCROLLBARWIDTH
+ 1, b
- SCROLLBARWIDTH
+ 1, r
, b
)) # jvr
817 window
.SizeWindow(width
, height
, 1) # changed updateFlag to true jvr
818 self
.do_postresize(width
, height
, window
)
820 def do_postresize(self
, width
, height
, window
):
822 self
.wid
.InvalWindowRect(window
.GetWindowPort().GetPortBounds())
824 def do_inContent(self
, partcode
, window
, event
):
826 # If we're not frontmost, select ourselves and wait for
827 # the activate event.
829 if MyFrontWindow() <> window
:
830 window
.SelectWindow()
832 # We are. Handle the event.
833 (what
, message
, when
, where
, modifiers
) = event
835 local
= GlobalToLocal(where
)
836 self
.do_contentclick(local
, modifiers
, event
)
838 def do_contentclick(self
, local
, modifiers
, event
):
840 print 'Click in contents at %s, modifiers %s'%(local
, modifiers
)
842 def do_rawupdate(self
, window
, event
):
843 if DEBUG
: print "raw update for", window
846 self
.do_update(window
, event
)
849 def do_update(self
, window
, event
):
854 InvertRgn(window
.GetWindowPort().visRgn
)
855 FillRgn(window
.GetWindowPort().visRgn
, GetQDGlobalsGray())
857 EraseRgn(window
.GetWindowPort().visRgn
)
859 def do_activate(self
, activate
, event
):
860 if DEBUG
: print 'Activate %d for %s'%(activate
, self
.wid
)
862 class ControlsWindow(Window
):
864 def do_rawupdate(self
, window
, event
):
865 if DEBUG
: print "raw update for", window
868 self
.do_update(window
, event
)
869 #DrawControls(window) # jvr
870 UpdateControls(window
, window
.GetWindowPort().visRgn
) # jvr
871 window
.DrawGrowIcon()
874 def do_controlhit(self
, window
, control
, pcode
, event
):
875 if DEBUG
: print "control hit in", window
, "on", control
, "; pcode =", pcode
877 def do_inContent(self
, partcode
, window
, event
):
878 if MyFrontWindow() <> window
:
879 window
.SelectWindow()
881 (what
, message
, when
, where
, modifiers
) = event
882 SetPort(window
) # XXXX Needed?
883 local
= GlobalToLocal(where
)
884 pcode
, control
= FindControl(local
, window
)
885 if pcode
and control
:
886 self
.do_rawcontrolhit(window
, control
, pcode
, local
, event
)
888 if DEBUG
: print "FindControl(%s, %s) -> (%s, %s)" % \
889 (local
, window
, pcode
, control
)
890 self
.do_contentclick(local
, modifiers
, event
)
892 def do_rawcontrolhit(self
, window
, control
, pcode
, local
, event
):
893 pcode
= control
.TrackControl(local
)
895 self
.do_controlhit(window
, control
, pcode
, event
)
897 class ScrolledWindow(ControlsWindow
):
898 def __init__(self
, parent
):
899 self
.barx
= self
.bary
= None
900 self
.barx_enabled
= self
.bary_enabled
= 1
902 ControlsWindow
.__init
__(self
, parent
)
904 def scrollbars(self
, wantx
=1, wanty
=1):
906 self
.barx
= self
.bary
= None
907 self
.barx_enabled
= self
.bary_enabled
= 1
908 x0
, y0
, x1
, y1
= self
.wid
.GetWindowPort().GetPortBounds()
909 vx
, vy
= self
.getscrollbarvalues()
910 if vx
== None: self
.barx_enabled
, vx
= 0, 0
911 if vy
== None: self
.bary_enabled
, vy
= 0, 0
913 rect
= x0
-1, y1
-(SCROLLBARWIDTH
-1), x1
-(SCROLLBARWIDTH
-2), y1
+1
914 self
.barx
= NewControl(self
.wid
, rect
, "", 1, vx
, 0, 32767, 16, 0)
915 if not self
.barx_enabled
: self
.barx
.HiliteControl(255)
916 ## self.wid.InvalWindowRect(rect)
918 rect
= x1
-(SCROLLBARWIDTH
-1), y0
-1, x1
+1, y1
-(SCROLLBARWIDTH
-2)
919 self
.bary
= NewControl(self
.wid
, rect
, "", 1, vy
, 0, 32767, 16, 0)
920 if not self
.bary_enabled
: self
.bary
.HiliteControl(255)
921 ## self.wid.InvalWindowRect(rect)
923 def do_postclose(self
):
924 self
.barx
= self
.bary
= None
925 ControlsWindow
.do_postclose(self
)
927 def do_activate(self
, onoff
, event
):
928 self
.activated
= onoff
930 if self
.barx
and self
.barx_enabled
:
931 self
.barx
.ShowControl() # jvr
932 if self
.bary
and self
.bary_enabled
:
933 self
.bary
.ShowControl() # jvr
936 self
.barx
.HideControl() # jvr; An inactive window should have *hidden*
937 # scrollbars, not just dimmed (no matter what
938 # BBEdit does... look at the Finder)
940 self
.bary
.HideControl() # jvr
941 self
.wid
.DrawGrowIcon() # jvr
943 def do_postresize(self
, width
, height
, window
):
944 l
, t
, r
, b
= self
.wid
.GetWindowPort().GetPortBounds()
947 self
.barx
.HideControl() # jvr
948 self
.barx
.MoveControl(l
-1, b
-(SCROLLBARWIDTH
-1))
949 self
.barx
.SizeControl((r
-l
)-(SCROLLBARWIDTH
-3), SCROLLBARWIDTH
) # jvr
951 self
.bary
.HideControl() # jvr
952 self
.bary
.MoveControl(r
-(SCROLLBARWIDTH
-1), t
-1)
953 self
.bary
.SizeControl(SCROLLBARWIDTH
, (b
-t
)-(SCROLLBARWIDTH
-3)) # jvr
955 self
.barx
.ShowControl() # jvr
956 self
.wid
.ValidWindowRect((l
, b
- SCROLLBARWIDTH
+ 1, r
- SCROLLBARWIDTH
+ 2, b
)) # jvr
958 self
.bary
.ShowControl() # jvr
959 self
.wid
.ValidWindowRect((r
- SCROLLBARWIDTH
+ 1, t
, r
, b
- SCROLLBARWIDTH
+ 2)) # jvr
960 self
.wid
.InvalWindowRect((r
- SCROLLBARWIDTH
+ 1, b
- SCROLLBARWIDTH
+ 1, r
, b
)) # jvr, growicon
963 def do_rawcontrolhit(self
, window
, control
, pcode
, local
, event
):
964 if control
== self
.barx
:
966 elif control
== self
.bary
:
970 if pcode
in (inUpButton
, inDownButton
, inPageUp
, inPageDown
):
971 # We do the work for the buttons and grey area in the tracker
972 dummy
= control
.TrackControl(local
, self
.do_controltrack
)
974 # but the thumb is handled here
975 pcode
= control
.TrackControl(local
)
977 value
= control
.GetControlValue()
978 print 'setbars', which
, value
#DBG
979 self
.scrollbar_callback(which
, 'set', value
)
980 self
.updatescrollbars()
982 print 'funny part', pcode
#DBG
985 def do_controltrack(self
, control
, pcode
):
986 if control
== self
.barx
:
988 elif control
== self
.bary
:
993 if pcode
== inUpButton
:
995 elif pcode
== inDownButton
:
997 elif pcode
== inPageUp
:
999 elif pcode
== inPageDown
:
1003 self
.scrollbar_callback(which
, what
, None)
1004 self
.updatescrollbars()
1006 def updatescrollbars(self
):
1008 vx
, vy
= self
.getscrollbarvalues()
1011 self
.barx
.HiliteControl(255)
1012 self
.barx_enabled
= 0
1014 if not self
.barx_enabled
:
1015 self
.barx_enabled
= 1
1017 self
.barx
.HiliteControl(0)
1018 self
.barx
.SetControlValue(vx
)
1021 self
.bary
.HiliteControl(255)
1022 self
.bary_enabled
= 0
1024 if not self
.bary_enabled
:
1025 self
.bary_enabled
= 1
1027 self
.bary
.HiliteControl(0)
1028 self
.bary
.SetControlValue(vy
)
1030 # Auxiliary function: convert standard text/image/etc coordinate
1031 # to something palatable as getscrollbarvalues() return
1032 def scalebarvalue(self
, absmin
, absmax
, curmin
, curmax
):
1033 if curmin
<= absmin
and curmax
>= absmax
:
1035 if curmin
<= absmin
:
1037 if curmax
>= absmax
:
1039 perc
= float(curmin
-absmin
)/float(absmax
-absmin
)
1040 return int(perc
*32767)
1044 def getscrollbarvalues(self
):
1047 def scrollbar_callback(self
, which
, what
, value
):
1048 print 'scroll', which
, what
, value
1050 class DialogWindow(Window
):
1051 """A modeless dialog window"""
1053 def open(self
, resid
):
1054 self
.dlg
= GetNewDialog(resid
, -1)
1055 self
.wid
= self
.dlg
.GetDialogWindow()
1061 def do_postclose(self
):
1063 Window
.do_postclose(self
)
1065 def do_itemhit(self
, item
, event
):
1066 print 'Dialog %s, item %d hit'%(self
.dlg
, item
)
1068 def do_rawupdate(self
, window
, event
):
1072 "Convert a long int to the 4-character code it really is"
1075 x
, c
= divmod(x
, 256)
1080 class TestApp(Application
):
1082 "This class is used by the test() function"
1084 def makeusermenus(self
):
1085 self
.filemenu
= m
= Menu(self
.menubar
, "File")
1086 self
.saveitem
= MenuItem(m
, "Save", "S", self
.save
)
1088 self
.optionsmenu
= mm
= SubMenu(m
, "Options")
1089 self
.opt1
= CheckItem(mm
, "Arguments", "A")
1090 self
.opt2
= CheckItem(mm
, "Being hit on the head lessons", (kMenuOptionModifier
, "A"))
1091 self
.opt3
= CheckItem(mm
, "Complaints", (kMenuOptionModifier|kMenuNoCommandModifier
, "A"))
1093 self
.itemeh
= MenuItem(m
, "Enable Help", None, self
.enablehelp
)
1094 self
.itemdbg
= MenuItem(m
, "Debug", None, self
.debug
)
1096 self
.quititem
= MenuItem(m
, "Quit", "Q", self
.quit
)
1098 def save(self
, *args
):
1101 def quit(self
, *args
):
1104 def enablehelp(self
, *args
):
1105 hm
= self
.gethelpmenu()
1106 self
.nohelpitem
= MenuItem(hm
, "There isn't any", None, self
.nohelp
)
1108 def nohelp(self
, *args
):
1109 print "I told you there isn't any!"
1111 def debug(self
, *args
):
1122 if __name__
== '__main__':