// 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.

//
// Alias Script File
// MODIFY THIS AT YOUR OWN RISK
//
// Creation Date: Oct 1999
//
//<doc>
//<name findMenuItem>
//<owner "Alias Unsupported">
//
//<synopsis>
//		findMenuItem;
//
//<description>
//		Displays a window that takes a text string and searches
//		the main Maya window menus for that string. 
//<P>
//		The search can be slow if Maya has just started as the
//		menus need to be built before they can be searched.
//
//<returns>
//		None.
//
//</doc>


global proc int searchOneMenu(
	string $thisMenu, string $searchString, string $path)
{
	global string $gMainWindow;
	setParent $gMainWindow;

	int $numItems = `menu -q -ni $thisMenu`;
	if ($numItems == 0) { 
		// Try to build the menu
		string $pmCmd = `menu -q -pmc $thisMenu`;
		catch(eval($pmCmd));
		$numItems = `menu -q -ni $thisMenu`;
	}

	if ($numItems == 0) return 0;

	int $gotOne = 0;
	
	string $items[] = `menu -q -ia $thisMenu`;
	setParent -m $thisMenu;
	for ($i=0; $i < $numItems; ++$i) {

		// Handle option boxes
		if (`menuItem -q -iob $items[$i]`) continue;
		// Handle the dividers
		if (`menuItem -q -d $items[$i]`) continue;

		string $label = `menuItem -q -l $items[$i]`;
		string $menuLabel;

		if (!`checkBox -q -v searchCaseSensitive`) {
			$menuLabel = tolower ($label);
		} else {
			$menuLabel = $label;
		}
		// Remove the ... on menu items
		$menuLabel = substituteAllString($menuLabel, ".", "");

		if (`gmatch $menuLabel $searchString`) {
			scrollField -edit -it ($path+$label+"\n") 
				searchResultsTextField;
			$gotOne = 1;
		} 
		
		if (`menuItem -q -sm $items[$i]`) {
			// Check for submenus
			string $newPath = $path+$label+"->";
			if (searchOneMenu($items[$i], $searchString, $newPath)) {
				$gotOne = 1;
			}
		}
		setParent -m ..;
	}
	return $gotOne;
}

global proc int oldMenuSearch (string $searchString)
{
	int $retVal = false;
	string $version = `about -v`;

	// Note: The missing break statements in the outer loop	are intentional
	// If the item isn't found for the current version, look at old versions.
	// Additions to this should always put new versions at the top.

	switch ($version) { 	

	// Maya 4 cases
	//
	case "4.0Beta1":
	case "4.0":
		$retVal = true;
		switch ($searchString) {
			//
			// Removed items
			//
			case "*layer bar*":
			case "*display layer editor*":
				scrollField -edit 
					-text ("This menu item has been removed.\n"
					+ "Use the Layer Editor below the Channel Box instead.")
					searchResultsTextField;
				break;

			case "*uninstall current settings*":
				scrollField -edit 
					-text ("This menu item has been renamed to\n"
					+ "Polygons->Tool Options->Reset to Default Settings")
					searchResultsTextField;
				break;

			default:
				$retVal = false;
				break;
		}
		if ($retVal) break;

	// Maya 3 cases
	//
	case "3.0Beta2":
	case "3.0":
	case "3.0.1":
		$retVal = true;
		switch ($searchString) {
			//
			// Removed items
			//
			case "*create display layer*":
				scrollField -edit 
					-text ("This menu item has been removed.\n"
					+ "Use the Layer Bar or Display Layer Editor instead.")
					searchResultsTextField;
				break;

			// No helpful suggestions for these
			case "*shading groups*":
			case "*show edits one level finer*":
			case "*run-up and cache*":
			case "*cache current frame*":
				scrollField -edit 
					-text "This menu item has been removed."
					searchResultsTextField;
				break;

			case "*reverse and propagate*":
				scrollField -edit 
					-text ("This menu item has been removed.\n"
					+ "It is now an option in the Reverse Option Box.")
					searchResultsTextField;
				break;

			case "*nurbs geometry*":
				scrollField -edit 
					-text ("This menu item has been removed.\n\n"
					+ "Use one of the below instead:\n"
					+ "Edit->Select All by Type->NURBS Curves\n"
					+ "Edit->Select All by Type->NURBS Surfaces")
					searchResultsTextField;
				break;

			case "*add air*":
			case "*add drag*":
			case "*add gravity*":
			case "*add newton*":
			case "*add radial*":
			case "*add turbulence*":
			case "*add uniform*":
			case "*add vortex*":
				scrollField -edit 
					-text ("This menu item has been removed.\n"
					+ "Create the field, then use \n"
					+ "Fields->Attach to Selected Object as Source")
					searchResultsTextField;
				break;

			//
			// Renamed items
			//
			case "*add emitter*":
				scrollField -edit 
					-text ("This menu item has been renamed.\n"
					+ "Particles->Emit from Object")
					searchResultsTextField;
				break;
			case "*connect to field*":
				scrollField -edit 
					-text ("This menu item has been renamed.\n"
					+ "Fields->Affect Selected Object")
					searchResultsTextField;
				break;
			case "*connect to emitter*":
				scrollField -edit 
					-text ("This menu item has been renamed.\n"
					+ "Particles->Use Selected Emitter")
					searchResultsTextField;
				break;
			case "*connect to collision*":
				scrollField -edit 
					-text ("This menu item has been renamed.\n"
					+ "Particles->Make Collide")
					searchResultsTextField;
				break;
			case "*add to owner*":
				scrollField -edit 
					-text ("This menu item has been renamed.\n"
					+ "Fields->Attach to Selected Object as Source")
					searchResultsTextField;
				break;
			case "*scene caching*":
				scrollField -edit 
					-text ("This menu item has been renamed.\n"
					+ "Solvers->Memory Caching")
					searchResultsTextField;
				break;

			case "*show only viewing panes*":
				scrollField -edit 
					-text ("This menu item has been renamed to Hide UI Elements.\n"
					+ "Display->UI Elements->Hide UI Elements")
					searchResultsTextField;
				break;
			case "*show all panes*":
				scrollField -edit 
					-text ("This menu item has been renamed to Restore UI Elements.\n"
					+ "Display->UI Elements->Restore UI Elements")
					searchResultsTextField;
				break;

			case "*display poly count*":
				scrollField -edit 
					-text ("This menu item has been renamed to Poly Count.\n"
					+ "Display->Heads Up Display->Poly Count")
					searchResultsTextField;
				break;
			case "*set normal*":
				scrollField -edit 
					-text ("This menu item has been renamed to Set Vertex Normal.\n"
					+ "Edit Polygons->Normals->Set Vertex Normal")
					searchResultsTextField;
				break;
			case "*show edits at current level*":
				scrollField -edit 
					-text ("This menu item has been renamed to\n"
					+ "Subdiv Surfaces->Component Display Filter->Edits")
					searchResultsTextField;
				break;
			case "*refine display region*":
				scrollField -edit 
					-text ("This menu item has been renamed to\n"
					+ "Subdiv Surfaces->Refine Selected Components")
					searchResultsTextField;
				break;
			case "*expand display region*":
				scrollField -edit 
					-text ("This menu item has been renamed to\n"
					+ "Subdiv Surfaces->Expand Selected Components")
					searchResultsTextField;
				break;

			case "*ui preferences*":
			case "*general preferences*":
				scrollField -edit 
					-text ("This menu item has been renamed to Preferences.\n"
					+ "Window->Settings/Preferences->Preferences")
					searchResultsTextField;
				break;
		
			default:
				$retVal = false;
				break;
		}
		if ($retVal) break;
	}
	
	return $retVal;
}


global proc searchAllMenus (string $searchStringArg)
{
	if ($searchStringArg == "") {
		scrollField -edit 
			-text "Enter a string in the field above."
			searchResultsTextField;
		return;
	}

	// Clear any old searches
	scrollField -edit -clear searchResultsTextField;
	
	string $searchString;
	if (!`checkBox -q -v searchCaseSensitive`) {
		$searchString = tolower($searchStringArg);
	} else {
		$searchString = $searchStringArg;
	}
	$searchString = "*"+$searchString+"*";

	global string $gSearchLastString;
	$gSearchLastString = $searchStringArg;
	
	global string $gMainWindow;
	int $numMenus = `window -q -nm $gMainWindow`;

	// This should never tbe true
	if ($numMenus < 1 ) exit;

	waitCursor -state on;
	
	string $aMenu;
	string $menuLabel;
	string $menuList[] = `window -q -ma $gMainWindow`;
	string $path;
	int $gotOne = 0;

	for ($aMenu in $menuList) {

		// Make sure nothing really strange is going on
		if (!`menu -exists $aMenu`) continue;

		$menuLabel = `menu -q -l $aMenu`;
			
		// Skip the hotbox menus
		string $s = match("Hotbox", $menuLabel);
		if ($s == "Hotbox") continue;
		if ($aMenu == "HotBoxRecentCommandsMenu") continue;

		$path = $menuLabel+"->";
		if (searchOneMenu($aMenu,$searchString, $path)) {
			$gotOne = 1;
		}
	}

	if ($gotOne) {
		scrollField -edit 
			-it "\nNo more matches found.\n"
			searchResultsTextField;
	} else {
		// Look for menu items from previous versions
		if (!$gotOne) {
			$gotOne = `oldMenuSearch ($searchString)`;
		}
		
		// If nothing was found then help a little
		if (!$gotOne) {
			scrollField -edit 
				-text ("No matches found.\n\n"
				+ "Use wildcard characters (*) to expand your search.\n"
				+ "Type a full name to find removed or renamed items.")
				searchResultsTextField;
		}
	}
	
	waitCursor -state off;
}


global proc findMenuItem ()
{
	global string $gSearchLastString = "";

	//	If the window already exists then just show it and return.
	//
	if (`window -exists menuSearchWindow`) {
		showWindow menuSearchWindow;
		return;
	}

	window 
		-title "Find a Menu Item" 
		-iconName "Find Menu"
		-w 435 -h 250
		menuSearchWindow;
		
	formLayout searchForm;
		text -l "Enter a string to search for:  " searchDirections;

		textField 
			-cc "searchAllMenus (`textField -q -tx searchStringField`)"
			searchStringField;

		checkBox -l "Use Case Sensitive Search" -v false searchCaseSensitive;

		scrollField -editable false -height 250 searchResultsTextField;

		button -l "Close" 
			-c "window -e -visible false menuSearchWindow"
			searchCloseButton;

	setParent ..;
	
	int $spacing = 5;
	formLayout -e
		-af searchDirections "left" $spacing
		-af searchDirections "top" $spacing
		
		-af searchStringField "left" $spacing
		-ac searchStringField "top" $spacing searchDirections
		-af searchStringField "right" $spacing
		
		-af searchCaseSensitive "left" $spacing
		-ac searchCaseSensitive "top" 1 searchStringField
		-af searchCaseSensitive "right" $spacing
		
		-af searchResultsTextField "left" $spacing
		-af searchResultsTextField "right" $spacing
		-ac searchResultsTextField "top" 0 searchCaseSensitive
		-ac searchResultsTextField "bottom" $spacing searchCloseButton
		
		-af searchCloseButton "left" $spacing
		-af searchCloseButton "right" $spacing
		-an searchCloseButton "top" 
		-af searchCloseButton "bottom" $spacing
		
		searchForm;
		
	showWindow menuSearchWindow;
}
