// Copyright (C) 1997-2006 Autodesk, Inc., and/or its licensors.
// All rights reserved.
//
// The coded instructions, statements, computer programs, and/or related
// material (collectively the "Data") in these files contain unpublished
// information proprietary to Autodesk, Inc. ("Autodesk") and/or its licensors,
// which is protected by U.S. and Canadian federal copyright law and by
// international treaties.
//
// The Data is provided for use exclusively by You. You have the right to use,
// modify, and incorporate this Data into other products for purposes authorized 
// by the Autodesk software license agreement, without fee.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND. AUTODESK
// DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTIES
// INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF NON-INFRINGEMENT,
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, OR ARISING FROM A COURSE 
// OF DEALING, USAGE, OR TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS
// LICENSORS BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL,
// DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK AND/OR ITS
// LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY OR PROBABILITY OF SUCH DAMAGES.

//
// Alias Script File
// MODIFY THIS AT YOUR OWN RISK
//
// Creation Date: 1995
//
// Description:
//		This implements the group of controls that represents the
//		shelf.  The shelf can either be part of a window
//		or in a window of its own.
//
// Input Arguments:
//		The name of the layout that the shelf should add itself
//		to.  If empty, then this script will create a window.
//
// Return Value:
//		The name of the top level layout control.
//			(Used for embedding within another window)
//

global proc buildShelfMenu( string $parent ) 
//	
//	Create the popup menu that allows users to switch between
//	shelf tabs.  This is created each time it is selected to
//	keep it in sync.
//
{
	global string $gShelfTopLevel;
	global string $gShelfCollection = "";

	setParent -m $parent;
	menu -e -dai $parent;
	$gShelfCollection = `radioMenuItemCollection`;

	// Shelf tab listing
	string $curShelf = `tabLayout -q -selectTab $gShelfTopLevel`;
	string $shelfArray[] = `shelfTabLayout -q -childArray $gShelfTopLevel`;
	int $numShelves = size($shelfArray);

	if(!`exists shelfLabel_melToUI` ){
		source "shelfLabel.mel";
	}
	
	for ($i=0; $i<$numShelves; ++$i) {
		string $cmd = 
			"shelfTabLayout -edit -selectTabIndex "+($i+1)+" "+$gShelfTopLevel+"; "
			+"optionVar -iv selectedShelf `tabLayout -q -sti "+$gShelfTopLevel+"`";
		int $enabled = ($shelfArray[$i] == $curShelf);
		menuItem -ecr false -radioButton $enabled -cl $gShelfCollection
			-label `shelfLabel_melToUI( $shelfArray[$i] )` -c $cmd;
	}			
}

global proc shelfTabChange()
//
//	Description:
//		This procedure is called whenever the user selects a new
//		active shelf tab.
//
//		Update the active shelf preference.
//
{
	global string $gShelfTopLevel;

	int $activeShelfTab;

	$activeShelfTab = `shelfTabLayout -query -selectTabIndex $gShelfTopLevel`;

	optionVar -intValue "selectedShelf" $activeShelfTab;
}

global proc toggleShelfTabs ()
{
	global string $gShelfTopLevel;

	// Set optionVar to the opposite of the current state
	int $newState = !`optionVar -q shelfTabsVisible`;
	optionVar -intValue "shelfTabsVisible" $newState;
	shelfTabLayout -e -tv $newState $gShelfTopLevel;

	setShelfStyle `optionVar -query shelfItemStyle`
		`optionVar -query shelfItemSize`;

	// In order for the UI to resize the scroll window on Linux
	// force the shelfs to redraw completely.

	if(`about -linux`){
		ToggleShelf;
		ToggleShelf;
	}

}

global proc buildShelves() {

	global string $gShelfTopLevel;
	
	setParent $gShelfTopLevel;
	
	//	Define a template for shelf layouts.
	//
	if (!`uiTemplate -exists shelfLayoutTemplate`) {
		uiTemplate shelfLayoutTemplate;
		shelfLayout -dt shelfLayoutTemplate -h 34 -cwh 34 34;
	}

	//	Activate the shelf layout template.
	//
	setUITemplate -pushTemplate shelfLayoutTemplate;

	string $varName, $cmd, $shelfFile, $shelfName;
	int $i, $nShelves;
	
	if(!`exists shelfLabel_melToUI` ){
		source "shelfLabel.mel";
	}
	
	$nShelves = `optionVar -q numShelves`;
	int $isFile;
	for ($i = 1; $i <= $nShelves; $i++) {
		$varName = ("shelfName" + $i);
		$shelfName = `optionVar -q $varName`;

		if(`shelfLayout -exists $shelfName`) {
			string $warningMsg = (uiRes("m_shelf.kShelfExists"));
			string $shelfLabel = `shelfLabel_melToUI $shelfName`;
			warning(`format -s $shelfLabel $warningMsg`);
			continue;
		}

		$cmd = ("shelfLayout " + $shelfName);
		eval $cmd;

		$varName = ("shelfFile" + $i);
		$shelfFile = `optionVar -q $varName`;
		if ($shelfFile != 0) {
			$isFile = `exists $shelfFile`;
			if ($isFile != 0) {

// If we use evalContinue then we aren't notified if there are any errors
//				evalContinue $shelfFile;

				if (catch(eval($shelfFile))) {
					string $returnStr;
					string $quit = (uiRes("m_shelf.kQuit"));
					string $msg = (uiRes("m_shelf.kShelfItemsCantBeRead"));
					string $shelfLabel = `shelfLabel_melToUI $shelfName`;
					$msg = `format -s $shelfLabel $msg`;
					$returnStr = `confirmDialog -title (uiRes("m_shelf.kShelfError")) 
						-message $msg
						-button (uiRes("m_shelf.kContinue"))
						-button $quit`;
					if ($quit == $returnStr) {
						quit -f;
					}
				}
			}
		}			

		//Editing the shelf labels
		//
		shelfTabLayout -edit -tabLabel $shelfName (shelfLabel_melToUI($shelfName)) $gShelfTopLevel;

		setParent ..;
	}

	//	Deactivate the shelf layout template.
	//
	setUITemplate -popTemplate;

	//	Define the default active shelf if a preference does not
	//	exist yet.
	//
	if (!`optionVar -exists "selectedShelf"`) {
		// Maya Personal Learning Edition should default to showing
		// the General Tab for brand-new users.
		//
		if (`about -windows` && (!`about -evalVersion`) ) {
			optionVar -intValue "selectedShelf" 4;
		} else {
			optionVar -intValue "selectedShelf" 1;
		}
	}

	//	Get the selected tab preference and make that shelf tab active.
	//
	int $activeTab = `optionVar -query "selectedShelf"`;

	//	Make sure the tab preference is valid given the number of
	//	tabs in the shelf, ie. it should be a number ranging from
	//	1 to the number of tabs in the shelf.
	//
	if ($activeTab < 1
		|| $activeTab > `tabLayout -query -numberOfChildren $gShelfTopLevel`) {
		//
		//	The active tab preference is not valid given the number
		//	of tabs in the Shelf.
		//
		//	Reset the preference to the first shelf tab.
		//
		$activeTab = 1;
		optionVar -intValue "selectedShelf" $activeTab;
	}

	tabLayout -edit -selectTabIndex $activeTab $gShelfTopLevel;

	//	Set the display style for the buttons on the Shelf.
	//
	setShelfStyle `optionVar -query shelfItemStyle` 
				  `optionVar -query shelfItemSize`;
}

{
	global string $gShelfForm;
	global string $gShelfTopLevel;

	int $bottomSpacing;

	//	Create a layout appropriate for the Shelf.
	//
	string $shelfLayout = `formLayout -parent $gShelfForm`;

	//////////////////////////////////////////////////////////////////////
	//
	//	Create the Menu and Options area.
	//
	//////////////////////////////////////////////////////////////////////

	setParent $shelfLayout; 

	string $options = `frameLayout
		-parent         $shelfLayout
		-borderVisible  true
		-borderStyle    "in"
		-labelVisible   false
		-collapse       false
		-collapsable    false`;	

	string $optionsLayout = `formLayout`;

	string $shelvesButton = `iconTextButton
		-image1 "shelfTab.xpm" 
		-annotation (uiRes("m_shelf.kChangeShelfTab"))
		-height 15 -width 20`;
	
	string $shelvesSeparator = `separator`;

	string $optionsButton = `iconTextStaticLabel
		-image1 "shelfOptions.xpm" 
		-annotation (uiRes("m_shelf.kModifyShelf"))
		-height 16 -width 20`;

	setParent ..;

	//	For improving the alignment of shelves buttons.
	//
	if (`about -nt`) {
		$bottomSpacing = 0;
	} else {
		$bottomSpacing = 1;
	}

	formLayout -edit
		-attachNone     $shelvesButton    "top"
		-attachForm     $shelvesButton    "left"   0
		-attachControl  $shelvesButton    "bottom" $bottomSpacing $shelvesSeparator
		-attachForm     $shelvesButton    "right"  0

		-attachNone     $shelvesSeparator "top"
		-attachForm     $shelvesSeparator "left"   0
		-attachControl  $shelvesSeparator "bottom" 0 $optionsButton
		-attachForm     $shelvesSeparator "right"  0

		-attachNone     $optionsButton    "top"
		-attachForm     $optionsButton    "left"   0
		-attachForm     $optionsButton    "bottom" 0
		-attachForm     $optionsButton    "right"  0
		$optionsLayout;
	
	//	Get the tab visible state.
	//
	if (!`optionVar -exists "shelfTabsVisible"`) {
		//
		//	The shelf tabs are visible for Windows only.
		//
		int $visible = false;
		if (`about -windows`) {
			$visible = true;
		}
		optionVar -intValue "shelfTabsVisible" $visible;
	}
	int $tabVis = `optionVar -q shelfTabsVisible`;

	// Create the tab-switching menu
	//
	string $menu = `popupMenu -button 1 -parent $shelvesButton`;
	menu -edit -postMenuCommand ( "buildShelfMenu " + $menu ) $menu;

	// Create the options menu
	//
	$menu = `popupMenu -button 1 -parent $optionsButton`;
	menuItem -ecr false -cb $tabVis -c "toggleShelfTabs" -label (uiRes("m_shelf.kShelfTabs"));
	menuItem -d true;
	menuItem -ecr false -label (uiRes("m_shelf.kShelfEditor")) -c "shelfEditorDialog";
	menuItem -d true;
	menuItem -ecr false -label (uiRes("m_shelf.kNewShelf")) -c "createNewShelf";
	string $cmd = "deleteShelfTab(`tabLayout -q -selectTab $gShelfTopLevel`);";
	menuItem -ecr false -label (uiRes("m_shelf.kDeleteShelf")) -c $cmd;
	menuItem -d true;
	menuItem -ecr false -label (uiRes("m_shelf.kLoadShelf")) -c "loadNewShelf \"\"";
	menuItem -d true;
	menuItem -ecr false -label (uiRes("m_shelf.kSaveShelf")) -c "saveAllShelves $gShelfTopLevel";
	
	//////////////////////////////////////////////////////////////////////
	//
	//	Create the Shelf.
	//
	//////////////////////////////////////////////////////////////////////
	$gShelfTopLevel = `shelfTabLayout
		-parent        $shelfLayout 
		-tabsVisible   $tabVis
		-image         "smallTrash.xpm" 
		-imageVisible  true
		-changeCommand ("shelfTabChange")
		ShelfLayout`;

	//	For improving the alignment of options buttons.
	//
	if (`about -nt`) {
		$bottomSpacing = 1;
	} else {
		$bottomSpacing = 0;
	}

	//	Layout Shelf.
	//
	formLayout -edit
		-attachNone		$options		  "top"
		-attachForm     $options		  "left"	2
		-attachForm		$options		  "bottom"	$bottomSpacing
		-attachNone		$options		  "right"

		-attachForm		$gShelfTopLevel   "top"    0
		-attachControl	$gShelfTopLevel   "left"   0 $options
		-attachForm		$gShelfTopLevel   "bottom" 0
		-attachForm		$gShelfTopLevel   "right"  0
		$shelfLayout;

	//	Attach Shelf to parent.
	//
	formLayout -edit
		-attachForm $shelfLayout "top"    0
		-attachForm $shelfLayout "left"   0
		-attachForm $shelfLayout "bottom" 0
		-attachForm $shelfLayout "right"  0
		$gShelfForm;
}

