// Function enabling/disabling a menu item in a menu of the Scilab Console
function setScilabMenuItemEnabled(menuLabel, menuItemLabel, enabled);

    function setMenuItemEnabled(menuItem, enabled)
        if enabled then
            menuItem.Foregroundcolor = [0 0 0];
            menuItem.enable = "on";
        else
            menuItem.Foregroundcolor = [0.5 0.5 0.5];
            menuItem.enable = "off";
        end
    endfunction
	
	if (argn(2) < 3) then
		error(msprintf(_("%s: Wrong number of input argument(s): At least #%d expected."), "setScilabMenuItemEnabled", 3));
	end	
	if (typeof(menuLabel) <> "string") then
		error(msprintf(_("%s: Wrong type for input argument #%d: A string expected."), "setScilabMenuItemEnabled", 1));
	end	
	if (typeof(menuItemLabel) <> "string") then
		error(msprintf(_("%s: Wrong type for input argument #%d: A string expected."), "setScilabMenuItemEnabled", 2));
	end
	if (typeof(enabled) <> "boolean") then
		error(msprintf(_("%s: Wrong type for input argument #%d: A boolean expected."), "setScilabMenuItemEnabled", 3));
	end	

    // get the handle of Scilab Console 
	console = get(0);
	// expose Scilab Console children handles, hidden by default
    console.showHiddenHandles = "on";

	// find the menu having the specified label
    menuToFind = [];
    for i=1:size(console.children, 'r')
		menu = console.children(i);
        if (menu.Label == menuLabel) | (menu.Label == "&" + menuLabel) then
            menuToFind = menu;
            break;
        end
    end	
	
    if ~isempty(menuToFind) then
        // menu found, now find the menu item having the specified label
		for i=1:size(menuToFind.children, 'r')            
			menuItem = menuToFind.children(i);
			if (menuItem.Label == menuItemLabel) | (menuItem.Label == "&" + menuItemLabel) then
				setMenuItemEnabled(menuItem, enabled);
				return;
			end
        end
		error(msprintf("Menu item ""%s"" not found in menu %s.", menuItemLabel, menuLabel));
    else
		error(msprintf("Menu ""%s"" not found.", menuLabel));
	end
endfunction
