Menu callbacks can be strings, in which case they will be looked up in

the top window.
The menubar is redrawn in the event loop in stead of for every change.
This commit is contained in:
Jack Jansen 1996-09-26 16:17:08 +00:00
parent e7b9fde1b8
commit b1667ef782
1 changed files with 75 additions and 11 deletions

View File

@ -23,6 +23,7 @@ from QuickDraw import *
#from Sound import * #from Sound import *
from Win import * from Win import *
from Windows import * from Windows import *
import types
import EasyDialogs import EasyDialogs
@ -95,6 +96,7 @@ class Application:
def __init__(self, nomenubar=0): def __init__(self, nomenubar=0):
self.quitting = 0 self.quitting = 0
self.needmenubarredraw = 0
self._windows = {} self._windows = {}
if nomenubar: if nomenubar:
self.menubar = None self.menubar = None
@ -102,7 +104,7 @@ class Application:
self.makemenubar() self.makemenubar()
def makemenubar(self): def makemenubar(self):
self.menubar = MenuBar() self.menubar = MenuBar(self)
AppleMenu(self.menubar, self.getabouttext(), self.do_about) AppleMenu(self.menubar, self.getabouttext(), self.do_about)
self.makeusermenus() self.makeusermenus()
@ -165,6 +167,9 @@ class Application:
pass pass
def getevent(self, mask = everyEvent, wait = 0): def getevent(self, mask = everyEvent, wait = 0):
if self.needmenubarredraw:
DrawMenuBar()
self.needmenubarredraw = 0
ok, event = WaitNextEvent(mask, wait) ok, event = WaitNextEvent(mask, wait)
return ok, event return ok, event
@ -391,21 +396,30 @@ class MenuBar:
nextid = 1 # Necessarily a class variable nextid = 1 # Necessarily a class variable
def getnextid(self): def getnextid(self):
id = self.nextid id = MenuBar.nextid
self.nextid = id+1 MenuBar.nextid = id+1
return id return id
def __init__(self): def __init__(self, parent=None):
self.parent = parent
ClearMenuBar() ClearMenuBar()
self.bar = GetMenuBar() self.bar = GetMenuBar()
self.menus = {} self.menus = {}
# XXX necessary?
def close(self):
self.parent = None
self.bar = None
def addmenu(self, title, after = 0): def addmenu(self, title, after = 0):
id = self.getnextid() id = self.getnextid()
if DEBUG: print 'Newmenu', title, id # XXXX if DEBUG: print 'Newmenu', title, id # XXXX
m = NewMenu(id, title) m = NewMenu(id, title)
m.InsertMenu(after) m.InsertMenu(after)
DrawMenuBar() # XXX appears slow! better do this when we're done. jvr if self.parent:
self.parent.needmenubarredraw = 1
else:
DrawMenuBar()
return id, m return id, m
def delmenu(self, id): def delmenu(self, id):
@ -414,11 +428,40 @@ class MenuBar:
def addpopup(self, title = ''): def addpopup(self, title = ''):
return self.addmenu(title, -1) return self.addmenu(title, -1)
# Useless:
# def install(self):
# if not self.bar: return
# SetMenuBar(self.bar)
# if self.parent:
# self.parent.needmenubarredraw = 1
# else:
# DrawMenuBar()
def install(self): def fixmenudimstate(self):
self.bar.SetMenuBar() for m in self.menus.keys():
DrawMenuBar() menu = self.menus[m]
if menu.__class__ == FrameWork.AppleMenu:
continue
for i in range(len(menu.items)):
label, shortcut, callback, kind = menu.items[i]
if type(callback) == types.StringType:
wid = Win.FrontWindow()
if wid and self.parent._windows.has_key(wid):
window = self.parent._windows[wid]
if hasattr(window, "domenu_" + callback):
menu.menu.EnableItem(i + 1)
elif hasattr(self.parent, "domenu_" + callback):
menu.menu.EnableItem(i + 1)
else:
menu.menu.DisableItem(i + 1)
elif hasattr(self.parent, "domenu_" + callback):
menu.menu.EnableItem(i + 1)
else:
menu.menu.DisableItem(i + 1)
elif callback:
pass
def dispatch(self, id, item, window, event): def dispatch(self, id, item, window, event):
if self.menus.has_key(id): if self.menus.has_key(id):
self.menus[id].dispatch(id, item, window, event) self.menus[id].dispatch(id, item, window, event)
@ -472,9 +515,30 @@ class Menu:
return sub return sub
def dispatch(self, id, item, window, event): def dispatch(self, id, item, window, event):
title, shortcut, callback, type = self.items[item-1] title, shortcut, callback, mtype = self.items[item-1]
if callback: if callback:
callback(id, item, window, event) if not self.bar.parent or type(callback) <> types.StringType:
menuhandler = callback
else:
# callback is string
wid = Win.FrontWindow()
if wid and self.bar.parent._windows.has_key(wid):
window = self.bar.parent._windows[wid]
if hasattr(window, "domenu_" + callback):
menuhandler = getattr(window, "domenu_" + callback)
elif hasattr(self.bar.parent, "domenu_" + callback):
menuhandler = getattr(self.bar.parent, "domenu_" + callback)
else:
# nothing we can do. we shouldn't have come this far
# since the menu item should have been disabled...
return
elif hasattr(self.bar.parent, "domenu_" + callback):
menuhandler = getattr(self.bar.parent, "domenu_" + callback)
else:
# nothing we can do. we shouldn't have come this far
# since the menu item should have been disabled...
return
menuhandler(id, item, window, event)
def enable(self, onoff): def enable(self, onoff):
if onoff: if onoff: