- Use a flag (self.quitting) as preferred method of exiting mainloop

- Added optional "nomenubar" argument to Application.__init__
This commit is contained in:
Jack Jansen 1996-09-17 12:35:43 +00:00
parent 29a3355ab9
commit 647535d390

View file

@ -93,8 +93,12 @@ class Application:
"Application framework -- your application should be a derived class" "Application framework -- your application should be a derived class"
def __init__(self): def __init__(self, nomenubar=0):
self.quitting = 0
self._windows = {} self._windows = {}
if nomenubar:
self.menubar = None
else:
self.makemenubar() self.makemenubar()
def makemenubar(self): def makemenubar(self):
@ -107,7 +111,7 @@ class Application:
self._quititem = MenuItem(m, "Quit", "Q", self._quit) self._quititem = MenuItem(m, "Quit", "Q", self._quit)
def _quit(self, *args): def _quit(self, *args):
raise self self.quitting = 1
def appendwindow(self, wid, window): def appendwindow(self, wid, window):
self._windows[wid] = window self._windows[wid] = window
@ -131,12 +135,16 @@ class Application:
# way to define the mask and wait time passed to WaitNextEvent.) # way to define the mask and wait time passed to WaitNextEvent.)
def mainloop(self, mask = everyEvent, wait = 0): def mainloop(self, mask = everyEvent, wait = 0):
self.quitting = 0
saveyield = MacOS.EnableAppswitch(self.yield) saveyield = MacOS.EnableAppswitch(self.yield)
try: try:
while 1: while not self.quitting:
try: try:
self.do1event(mask, wait) self.do1event(mask, wait)
except (Application, SystemExit): except (Application, SystemExit):
# Note: the raising of "self" is old-fashioned idiom to
# exit the mainloop. Calling _quit() is better for new
# applications.
break break
finally: finally:
MacOS.EnableAppswitch(saveyield) MacOS.EnableAppswitch(saveyield)
@ -222,6 +230,9 @@ class Application:
MacOS.HandleEvent(event) MacOS.HandleEvent(event)
def do_inMenuBar(self, partcode, window, event): def do_inMenuBar(self, partcode, window, event):
if not self.menubar:
MacOS.HandleEvent(event)
return
(what, message, when, where, modifiers) = event (what, message, when, where, modifiers) = event
result = MenuSelect(where) result = MenuSelect(where)
id = (result>>16) & 0xffff # Hi word id = (result>>16) & 0xffff # Hi word
@ -263,6 +274,9 @@ class Application:
if c == '.': if c == '.':
raise self raise self
else: else:
if not self.menubar:
MacOS.HandleEvent(event)
return
result = MenuKey(ord(c)) result = MenuKey(ord(c))
id = (result>>16) & 0xffff # Hi word id = (result>>16) & 0xffff # Hi word
item = result & 0xffff # Lo word item = result & 0xffff # Lo word