#
#		Python GUI - Menus - Macintosh version
#

from Carbon.Menu import NewMenu
from GMenus import Menu as GMenu

class Menu(GMenu):

    _mac_next_menu_id = 1
    # TODO: Recycle id numbers of discarded menus?
    
    def __init__(self, title, *item_descriptors):
        GMenu.__init__(self, title, *item_descriptors)
        id = Menu._mac_next_menu_id
        Menu._mac_next_menu_id = id + 1
        self._id = id
        self._mac_menu = NewMenu(id, title)
    
    def _clear_platform_menu(self):
        mac_menu = self._mac_menu
        mac_menu.DeleteMenuItems(1, mac_menu.CountMenuItems())
    
    def _add_separator_to_platform_menu(self):
        self._mac_menu.AppendMenu("-")
    
    def _add_item_to_platform_menu(self, item, command = None, index = None):
        modifiers = 0
        if item._shift:
            modifiers |= kMenuShiftModifier
        if item._option:
            modifiers |= kMenuOptionModifier
        mac_menu = self._mac_menu
        mac_menu.AppendMenuItemText(item._label)
        i = mac_menu.CountMenuItems()
        key = item._key
        if key:
            mac_menu.SetItemCmd(i, ord(key))
        mac_menu.SetMenuItemModifiers(i, modifiers)
        if not item.enabled:
            mac_menu.DisableMenuItem(i)
        if item.checked:
            mac_menu.CheckMenuItem(i, 1)
