// Copyright (C) 1997-2004 Alias Systems Corp.
// 
// The information in this file is provided for the exclusive use of the
// licensees of Alias.  Such users have the right to use, modify,
// and incorporate this code into other products for purposes authorized
// by the Alias license agreement, without fee.
// 
// ALIAS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
// INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
// EVENT SHALL ALIAS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
// DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

//
//  Description:
//	This file contains procedures associated with
//	the drag-n-drop Marking Menus editor.
//

// ========= These routines are used internally by the MM Editor. =============

global proc string generateMarkingMenuEditorFilenameFromAnnotation(string $annotation) {

	return ("menu_" + $annotation);
}

global proc int isMenuRegisteredWithMenuEditor(string $annotation) {

	if (`optionVar -exists markingMenuEditorAnnotations`) {
		string $annotationArray[] = `optionVar -q markingMenuEditorAnnotations`;
		int $j;
		for ($j = size($annotationArray) - 1; $j >= 0; --$j)
			if ($annotationArray[$j] == $annotation)
				return true;
	}

	return false;
}

global proc int registerMenuWithMenuEditor(string $fileName, string $annotation) {

	// Ensure that the user respects the
	// naming convention used in this file.
	//
	string $fullName;
	
	$fullName = `internalVar -userMarkingMenuDir` + $fileName;

	if (
		// Don't register the menu if it doesn't respect the naming conventions.
		//
		$fileName != generateMarkingMenuEditorFilenameFromAnnotation($annotation)

		// Don't register the menu if the script to create it doesn't exist.
		//
	||	(!`exists $fullName` && !`exists $fileName`)

		// Don't register the menu if another menu is already registered with the same annotation.
		//
	||	isMenuRegisteredWithMenuEditor($annotation)
	) {
		return 0;   // failure
	}


	optionVar
		-stringValueAppend markingMenuEditorFilenames $fileName
		-stringValueAppend markingMenuEditorAnnotations $annotation
		-intValueAppend markingMenuEditorDisplayAsMMFlags 1
		-intValueAppend markingMenuEditorIsNamedCommandFlags 0;

	return 1;   // success
}

// $region is one of "N", "S", "E", "W", or "C" ("C" for center)
// $button is one of 1, 2, 3, for Left, Middle, Right - respectively
//
global proc string generateNameOfHotBoxOptionVar(string $region, int $button) {

	return ("nameOfHotBox" + $region + $button + "MarkingMenu");
}

// ========= routines are a public interface to the hotBox scripts. ==========
global proc int isHotBoxMenuDefined(string $region, int $button) {

	string $nameOfOptionVar = generateNameOfHotBoxOptionVar($region,$button);

	return (`optionVar -exists $nameOfOptionVar`);
}

global proc string getScriptNameForHotBoxMenu(string $region, int $button) {

	string $nameOfOptionVar = generateNameOfHotBoxOptionVar($region,$button);

	if (`optionVar -exists $nameOfOptionVar`) {
		string $annotation = `optionVar -q $nameOfOptionVar`;
		if ($annotation == "")
			return "";   // The menu is "empty".
		else {
			string $filename = generateMarkingMenuEditorFilenameFromAnnotation($annotation);

			string $filename2 = `internalVar -userMarkingMenuDir` + $filename;

			// Check if the file exists.
			//
			if (`exists $filename2`)
				return $filename2;
			else if (`exists $filename`)
				return $filename;
			else
				return "";
		}
	}
	else
		return "";
}

global proc registerHotBoxMenuWithMenuEditor(string $fileName, string $annotation, string $region, int $isLeft, int $isMiddle, int $isRight) {

	if (registerMenuWithMenuEditor($fileName, $annotation) == 0)
		return;   // failure

	string $nameOfOptionVar;

	if ($isLeft) {
		$nameOfOptionVar = generateNameOfHotBoxOptionVar($region,1);
		optionVar -stringValue $nameOfOptionVar $annotation;
	}
	if ($isMiddle) {
		$nameOfOptionVar = generateNameOfHotBoxOptionVar($region,2);
		optionVar -stringValue $nameOfOptionVar $annotation;
	}
	if ($isRight) {
		$nameOfOptionVar = generateNameOfHotBoxOptionVar($region,3);
		optionVar -stringValue $nameOfOptionVar $annotation;
	}
}
