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
= apply(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 apply(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 apply(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", `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
435 if eventname
.has_key(what
):
436 nicewhat
= eventname
[what
]
438 if what
== kHighLevelEvent
:
440 print `
ostypecode(message
)`
, hex(when
), `
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 self
.menu
.SetMenuItemText(item
, label
) # set the actual text
565 if shortcut
and type(shortcut
) == type(()):
566 modifiers
, char
= shortcut
[:2]
567 self
.menu
.SetItemCmd(item
, ord(char
))
568 self
.menu
.SetMenuItemModifiers(item
, modifiers
)
569 if len(shortcut
) > 2:
570 self
.menu
.SetMenuItemKeyGlyph(item
, shortcut
[2])
572 self
.menu
.SetItemCmd(item
, ord(shortcut
))
575 def delitem(self
, item
):
576 if item
!= len(self
.items
):
577 raise 'Can only delete last item of a menu'
578 self
.menu
.DeleteMenuItem(item
)
579 del self
.items
[item
-1]
581 def addcheck(self
, label
, shortcut
=None, callback
=None):
582 return self
.additem(label
, shortcut
, callback
, 'check')
584 def addradio(self
, label
, shortcut
=None, callback
=None):
585 return self
.additem(label
, shortcut
, callback
, 'radio')
587 def addseparator(self
):
588 self
.menu
.AppendMenu('(-')
589 self
.items
.append(('', None, None, 'separator'))
591 def addsubmenu(self
, label
, title
=''):
592 sub
= Menu(self
.bar
, title
, -1)
593 item
= self
.additem(label
, '\x1B', None, 'submenu')
594 self
.menu
.SetItemMark(item
, sub
.id)
596 sub
._parent
_item
= item
599 def dispatch(self
, id, item
, window
, event
):
600 title
, shortcut
, callback
, mtype
= self
.items
[item
-1]
602 if not self
.bar
.parent
or type(callback
) <> types
.StringType
:
603 menuhandler
= callback
606 wid
= MyFrontWindow()
607 if wid
and self
.bar
.parent
._windows
.has_key(wid
):
608 window
= self
.bar
.parent
._windows
[wid
]
609 if hasattr(window
, "domenu_" + callback
):
610 menuhandler
= getattr(window
, "domenu_" + callback
)
611 elif hasattr(self
.bar
.parent
, "domenu_" + callback
):
612 menuhandler
= getattr(self
.bar
.parent
, "domenu_" + callback
)
614 # nothing we can do. we shouldn't have come this far
615 # since the menu item should have been disabled...
617 elif hasattr(self
.bar
.parent
, "domenu_" + callback
):
618 menuhandler
= getattr(self
.bar
.parent
, "domenu_" + callback
)
620 # nothing we can do. we shouldn't have come this far
621 # since the menu item should have been disabled...
623 menuhandler(id, item
, window
, event
)
625 def enable(self
, onoff
):
627 self
.menu
.EnableMenuItem(0)
629 self
._parent
.menu
.EnableMenuItem(self
._parent
_item
)
631 self
.menu
.DisableMenuItem(0)
633 self
._parent
.menu
.DisableMenuItem(self
._parent
_item
)
634 if self
.bar
and self
.bar
.parent
:
635 self
.bar
.parent
.needmenubarredraw
= 1
637 class PopupMenu(Menu
):
638 def __init__(self
, bar
):
639 Menu
.__init
__(self
, bar
, '(popup)', -1)
641 def popup(self
, x
, y
, event
, default
=1, window
=None):
642 # NOTE that x and y are global coordinates, and they should probably
643 # be topleft of the button the user clicked (not mouse-coordinates),
644 # so the popup nicely overlaps.
645 reply
= self
.menu
.PopUpMenuSelect(x
, y
, default
)
648 id = (reply
& 0xffff0000) >> 16
649 item
= reply
& 0xffff
651 wid
= MyFrontWindow()
653 window
= self
.bar
.parent
._windows
[wid
]
655 pass # If we can't find the window we pass None
656 self
.dispatch(id, item
, window
, event
)
659 def __init__(self
, menu
, title
, shortcut
=None, callback
=None, kind
=None):
660 self
.item
= menu
.additem(title
, shortcut
, callback
)
664 self
.menu
.delitem(self
.item
)
668 def check(self
, onoff
):
669 self
.menu
.menu
.CheckMenuItem(self
.item
, onoff
)
671 def enable(self
, onoff
):
673 self
.menu
.menu
.EnableMenuItem(self
.item
)
675 self
.menu
.menu
.DisableMenuItem(self
.item
)
677 def settext(self
, text
):
678 self
.menu
.menu
.SetMenuItemText(self
.item
, text
)
680 def setstyle(self
, style
):
681 self
.menu
.menu
.SetItemStyle(self
.item
, style
)
683 def seticon(self
, icon
):
684 self
.menu
.menu
.SetItemIcon(self
.item
, icon
)
686 def setcmd(self
, cmd
):
687 self
.menu
.menu
.SetItemCmd(self
.item
, cmd
)
689 def setmark(self
, cmd
):
690 self
.menu
.menu
.SetItemMark(self
.item
, cmd
)
693 class RadioItem(MenuItem
):
694 def __init__(self
, menu
, title
, shortcut
=None, callback
=None):
695 MenuItem
.__init
__(self
, menu
, title
, shortcut
, callback
, 'radio')
697 class CheckItem(MenuItem
):
698 def __init__(self
, menu
, title
, shortcut
=None, callback
=None):
699 MenuItem
.__init
__(self
, menu
, title
, shortcut
, callback
, 'check')
704 def SubMenu(menu
, label
, title
=''):
705 return menu
.addsubmenu(label
, title
)
708 class AppleMenu(Menu
):
710 def __init__(self
, bar
, abouttext
="About me...", aboutcallback
=None):
711 Menu
.__init
__(self
, bar
, "\024", id=SIOUX_APPLEMENU_ID
)
712 if MacOS
.runtimemodel
== 'ppc':
713 self
.additem(abouttext
, None, aboutcallback
)
715 self
.menu
.AppendResMenu('DRVR')
717 # Additem()'s tricks do not work for "apple" menu under Carbon
718 self
.menu
.InsertMenuItem(abouttext
, 0)
719 self
.items
.append((abouttext
, None, aboutcallback
, None))
721 def dispatch(self
, id, item
, window
, event
):
723 Menu
.dispatch(self
, id, item
, window
, event
)
724 elif MacOS
.runtimemodel
== 'ppc':
725 name
= self
.menu
.GetMenuItemText(item
)
728 class HelpMenu(Menu
):
729 def __init__(self
, bar
):
730 # Note we don't call Menu.__init__, we do the necessary things by hand
732 self
.menu
, index
= HMGetHelpMenu()
733 self
.id = self
.menu
.GetMenuID()
734 bar
.menus
[self
.id] = self
735 # The next line caters for the entries the system already handles for us
736 self
.items
= [None]*(index
-1)
741 """A single window belonging to an application"""
743 def __init__(self
, parent
):
747 def open(self
, bounds
=(40, 40, 400, 400), resid
=None):
749 self
.wid
= GetNewWindow(resid
, -1)
751 self
.wid
= NewWindow(bounds
, self
.__class
__.__name
__, 1,
752 8, -1, 1, 0) # changed to proc id 8 to include zoom box. jvr
755 def do_postopen(self
):
756 """Tell our parent we exist"""
757 self
.parent
.appendwindow(self
.wid
, self
)
762 def do_postclose(self
):
763 self
.parent
.removewindow(self
.wid
)
774 def do_inDrag(self
, partcode
, window
, event
):
776 window
.DragWindow(where
, self
.draglimit
)
778 draglimit
= screenbounds
780 def do_inGoAway(self
, partcode
, window
, event
):
782 if window
.TrackGoAway(where
):
785 def do_inZoom(self
, partcode
, window
, event
):
786 (what
, message
, when
, where
, modifiers
) = event
787 if window
.TrackBox(where
, partcode
):
788 window
.ZoomWindow(partcode
, 1)
789 rect
= window
.GetWindowUserState() # so that zoom really works... jvr
790 self
.do_postresize(rect
[2] - rect
[0], rect
[3] - rect
[1], window
) # jvr
792 def do_inZoomIn(self
, partcode
, window
, event
):
793 SetPort(window
) # !!!
794 self
.do_inZoom(partcode
, window
, event
)
796 def do_inZoomOut(self
, partcode
, window
, event
):
797 SetPort(window
) # !!!
798 self
.do_inZoom(partcode
, window
, event
)
800 def do_inGrow(self
, partcode
, window
, event
):
801 (what
, message
, when
, where
, modifiers
) = event
802 result
= window
.GrowWindow(where
, self
.growlimit
)
804 height
= (result
>>16) & 0xffff # Hi word
805 width
= result
& 0xffff # Lo word
806 self
.do_resize(width
, height
, window
)
808 growlimit
= (50, 50, screenbounds
[2] - screenbounds
[0], screenbounds
[3] - screenbounds
[1]) # jvr
810 def do_resize(self
, width
, height
, window
):
811 l
, t
, r
, b
= self
.wid
.GetWindowPort().GetPortBounds() # jvr, forGrowIcon
813 self
.wid
.InvalWindowRect((r
- SCROLLBARWIDTH
+ 1, b
- SCROLLBARWIDTH
+ 1, r
, b
)) # jvr
814 window
.SizeWindow(width
, height
, 1) # changed updateFlag to true jvr
815 self
.do_postresize(width
, height
, window
)
817 def do_postresize(self
, width
, height
, window
):
819 self
.wid
.InvalWindowRect(window
.GetWindowPort().GetPortBounds())
821 def do_inContent(self
, partcode
, window
, event
):
823 # If we're not frontmost, select ourselves and wait for
824 # the activate event.
826 if MyFrontWindow() <> window
:
827 window
.SelectWindow()
829 # We are. Handle the event.
830 (what
, message
, when
, where
, modifiers
) = event
832 local
= GlobalToLocal(where
)
833 self
.do_contentclick(local
, modifiers
, event
)
835 def do_contentclick(self
, local
, modifiers
, event
):
837 print 'Click in contents at %s, modifiers %s'%(local
, modifiers
)
839 def do_rawupdate(self
, window
, event
):
840 if DEBUG
: print "raw update for", window
843 self
.do_update(window
, event
)
846 def do_update(self
, window
, event
):
851 InvertRgn(window
.GetWindowPort().visRgn
)
852 FillRgn(window
.GetWindowPort().visRgn
, GetQDGlobalsGray())
854 EraseRgn(window
.GetWindowPort().visRgn
)
856 def do_activate(self
, activate
, event
):
857 if DEBUG
: print 'Activate %d for %s'%(activate
, self
.wid
)
859 class ControlsWindow(Window
):
861 def do_rawupdate(self
, window
, event
):
862 if DEBUG
: print "raw update for", window
865 self
.do_update(window
, event
)
866 #DrawControls(window) # jvr
867 UpdateControls(window
, window
.GetWindowPort().visRgn
) # jvr
868 window
.DrawGrowIcon()
871 def do_controlhit(self
, window
, control
, pcode
, event
):
872 if DEBUG
: print "control hit in", window
, "on", control
, "; pcode =", pcode
874 def do_inContent(self
, partcode
, window
, event
):
875 if MyFrontWindow() <> window
:
876 window
.SelectWindow()
878 (what
, message
, when
, where
, modifiers
) = event
879 SetPort(window
) # XXXX Needed?
880 local
= GlobalToLocal(where
)
881 pcode
, control
= FindControl(local
, window
)
882 if pcode
and control
:
883 self
.do_rawcontrolhit(window
, control
, pcode
, local
, event
)
885 if DEBUG
: print "FindControl(%s, %s) -> (%s, %s)" % \
886 (local
, window
, pcode
, control
)
887 self
.do_contentclick(local
, modifiers
, event
)
889 def do_rawcontrolhit(self
, window
, control
, pcode
, local
, event
):
890 pcode
= control
.TrackControl(local
)
892 self
.do_controlhit(window
, control
, pcode
, event
)
894 class ScrolledWindow(ControlsWindow
):
895 def __init__(self
, parent
):
896 self
.barx
= self
.bary
= None
897 self
.barx_enabled
= self
.bary_enabled
= 1
899 ControlsWindow
.__init
__(self
, parent
)
901 def scrollbars(self
, wantx
=1, wanty
=1):
903 self
.barx
= self
.bary
= None
904 self
.barx_enabled
= self
.bary_enabled
= 1
905 x0
, y0
, x1
, y1
= self
.wid
.GetWindowPort().GetPortBounds()
906 vx
, vy
= self
.getscrollbarvalues()
907 if vx
== None: self
.barx_enabled
, vx
= 0, 0
908 if vy
== None: self
.bary_enabled
, vy
= 0, 0
910 rect
= x0
-1, y1
-(SCROLLBARWIDTH
-1), x1
-(SCROLLBARWIDTH
-2), y1
+1
911 self
.barx
= NewControl(self
.wid
, rect
, "", 1, vx
, 0, 32767, 16, 0)
912 if not self
.barx_enabled
: self
.barx
.HiliteControl(255)
913 ## self.wid.InvalWindowRect(rect)
915 rect
= x1
-(SCROLLBARWIDTH
-1), y0
-1, x1
+1, y1
-(SCROLLBARWIDTH
-2)
916 self
.bary
= NewControl(self
.wid
, rect
, "", 1, vy
, 0, 32767, 16, 0)
917 if not self
.bary_enabled
: self
.bary
.HiliteControl(255)
918 ## self.wid.InvalWindowRect(rect)
920 def do_postclose(self
):
921 self
.barx
= self
.bary
= None
922 ControlsWindow
.do_postclose(self
)
924 def do_activate(self
, onoff
, event
):
925 self
.activated
= onoff
927 if self
.barx
and self
.barx_enabled
:
928 self
.barx
.ShowControl() # jvr
929 if self
.bary
and self
.bary_enabled
:
930 self
.bary
.ShowControl() # jvr
933 self
.barx
.HideControl() # jvr; An inactive window should have *hidden*
934 # scrollbars, not just dimmed (no matter what
935 # BBEdit does... look at the Finder)
937 self
.bary
.HideControl() # jvr
938 self
.wid
.DrawGrowIcon() # jvr
940 def do_postresize(self
, width
, height
, window
):
941 l
, t
, r
, b
= self
.wid
.GetWindowPort().GetPortBounds()
944 self
.barx
.HideControl() # jvr
945 self
.barx
.MoveControl(l
-1, b
-(SCROLLBARWIDTH
-1))
946 self
.barx
.SizeControl((r
-l
)-(SCROLLBARWIDTH
-3), SCROLLBARWIDTH
) # jvr
948 self
.bary
.HideControl() # jvr
949 self
.bary
.MoveControl(r
-(SCROLLBARWIDTH
-1), t
-1)
950 self
.bary
.SizeControl(SCROLLBARWIDTH
, (b
-t
)-(SCROLLBARWIDTH
-3)) # jvr
952 self
.barx
.ShowControl() # jvr
953 self
.wid
.ValidWindowRect((l
, b
- SCROLLBARWIDTH
+ 1, r
- SCROLLBARWIDTH
+ 2, b
)) # jvr
955 self
.bary
.ShowControl() # jvr
956 self
.wid
.ValidWindowRect((r
- SCROLLBARWIDTH
+ 1, t
, r
, b
- SCROLLBARWIDTH
+ 2)) # jvr
957 self
.wid
.InvalWindowRect((r
- SCROLLBARWIDTH
+ 1, b
- SCROLLBARWIDTH
+ 1, r
, b
)) # jvr, growicon
960 def do_rawcontrolhit(self
, window
, control
, pcode
, local
, event
):
961 if control
== self
.barx
:
963 elif control
== self
.bary
:
967 if pcode
in (inUpButton
, inDownButton
, inPageUp
, inPageDown
):
968 # We do the work for the buttons and grey area in the tracker
969 dummy
= control
.TrackControl(local
, self
.do_controltrack
)
971 # but the thumb is handled here
972 pcode
= control
.TrackControl(local
)
974 value
= control
.GetControlValue()
975 print 'setbars', which
, value
#DBG
976 self
.scrollbar_callback(which
, 'set', value
)
977 self
.updatescrollbars()
979 print 'funny part', pcode
#DBG
982 def do_controltrack(self
, control
, pcode
):
983 if control
== self
.barx
:
985 elif control
== self
.bary
:
990 if pcode
== inUpButton
:
992 elif pcode
== inDownButton
:
994 elif pcode
== inPageUp
:
996 elif pcode
== inPageDown
:
1000 self
.scrollbar_callback(which
, what
, None)
1001 self
.updatescrollbars()
1003 def updatescrollbars(self
):
1005 vx
, vy
= self
.getscrollbarvalues()
1008 self
.barx
.HiliteControl(255)
1009 self
.barx_enabled
= 0
1011 if not self
.barx_enabled
:
1012 self
.barx_enabled
= 1
1014 self
.barx
.HiliteControl(0)
1015 self
.barx
.SetControlValue(vx
)
1018 self
.bary
.HiliteControl(255)
1019 self
.bary_enabled
= 0
1021 if not self
.bary_enabled
:
1022 self
.bary_enabled
= 1
1024 self
.bary
.HiliteControl(0)
1025 self
.bary
.SetControlValue(vy
)
1027 # Auxiliary function: convert standard text/image/etc coordinate
1028 # to something palatable as getscrollbarvalues() return
1029 def scalebarvalue(self
, absmin
, absmax
, curmin
, curmax
):
1030 if curmin
<= absmin
and curmax
>= absmax
:
1032 if curmin
<= absmin
:
1034 if curmax
>= absmax
:
1036 perc
= float(curmin
-absmin
)/float(absmax
-absmin
)
1037 return int(perc
*32767)
1041 def getscrollbarvalues(self
):
1044 def scrollbar_callback(self
, which
, what
, value
):
1045 print 'scroll', which
, what
, value
1047 class DialogWindow(Window
):
1048 """A modeless dialog window"""
1050 def open(self
, resid
):
1051 self
.dlg
= GetNewDialog(resid
, -1)
1052 self
.wid
= self
.dlg
.GetDialogWindow()
1058 def do_postclose(self
):
1060 Window
.do_postclose(self
)
1062 def do_itemhit(self
, item
, event
):
1063 print 'Dialog %s, item %d hit'%(self
.dlg
, item
)
1065 def do_rawupdate(self
, window
, event
):
1069 "Convert a long int to the 4-character code it really is"
1072 x
, c
= divmod(x
, 256)
1077 class TestApp(Application
):
1079 "This class is used by the test() function"
1081 def makeusermenus(self
):
1082 self
.filemenu
= m
= Menu(self
.menubar
, "File")
1083 self
.saveitem
= MenuItem(m
, "Save", "S", self
.save
)
1085 self
.optionsmenu
= mm
= SubMenu(m
, "Options")
1086 self
.opt1
= CheckItem(mm
, "Arguments", "A")
1087 self
.opt2
= CheckItem(mm
, "Being hit on the head lessons", (kMenuOptionModifier
, "A"))
1088 self
.opt3
= CheckItem(mm
, "Complaints", (kMenuOptionModifier|kMenuNoCommandModifier
, "A"))
1090 self
.itemeh
= MenuItem(m
, "Enable Help", None, self
.enablehelp
)
1091 self
.itemdbg
= MenuItem(m
, "Debug", None, self
.debug
)
1093 self
.quititem
= MenuItem(m
, "Quit", "Q", self
.quit
)
1095 def save(self
, *args
):
1098 def quit(self
, *args
):
1101 def enablehelp(self
, *args
):
1102 hm
= self
.gethelpmenu()
1103 self
.nohelpitem
= MenuItem(hm
, "There isn't any", None, self
.nohelp
)
1105 def nohelp(self
, *args
):
1106 print "I told you there isn't any!"
1108 def debug(self
, *args
):
1119 if __name__
== '__main__':