// 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:  April 24, 1997
//
//  Description:
//      This script creates a panel which has the set editor within it.
//
//  Input Arguments:
//      None.
//
//  Return Value:
//      None.
//

source setEdBookmarkEditor.mel;

//string $setEd = "setEditor1SetEd";

global proc
setEditorStateCallback(string $setEd)
//
//	SYNOPSIS
//		Keeps the buttons on the toolbar in sync with
//		the actual settings of the set editor - called
//		when the trigger for the set editor, $setEd,
//		is fired.
{
	setParent $setEd;

	//	Construct unique names for the buttons
	//	for this instance of the editor

	string $setEdToolBarForm	= ($setEd + "toolbarForm");
	string $editModeBtn			= ($setEd + "editModeBtn");
	string $selectModeBtn		= ($setEd + "selectModeBtn");
	string $contentsModeBtn		= ($setEd + "contentsModeBtn");
	string $paintModeBtn		= ($setEd + "paintModeBtn");
	string $scrollBtn			= ($setEd + "scrollBtn");
	string $expandAllBtn		= ($setEd + "expandAllBtn");
	string $collapseAllBtn		= ($setEd + "collapseAllBtn");
	string $addItemsBtn			= ($setEd + "addItemsBtn");
	string $removeItemsBtn		= ($setEd + "removeItemsBtn");
	string $listSetsBtn			= ($setEd + "listSetsBtn");
	string $listPartitionsBtn	= ($setEd + "listPartitionsBtn");

	int $enableToolbar = !`setEditor -query -autoUpdate $setEd`;
	rowLayout -edit -enable $enableToolbar $setEdToolBarForm;
	if (!$enableToolbar)
		return;

	string $operatingMode = `setEditor -query -mode $setEd`;
	symbolCheckBox -edit
		-value ($operatingMode == "editing")
		$editModeBtn;
	symbolCheckBox -edit
		-value ($operatingMode == "selectNoExpand")
		$selectModeBtn;
	symbolCheckBox -edit
		-value ($operatingMode == "selectExpand")
		$contentsModeBtn;
	symbolCheckBox -edit
		-value ($operatingMode == "paint")
		$paintModeBtn;

	button -e -enable ($operatingMode == "editing") $addItemsBtn;
	button -e -enable ($operatingMode == "editing") $removeItemsBtn;

	symbolCheckBox -edit 
		-value `setEditor -query -editSets $setEd`
		$listSetsBtn;
	symbolCheckBox -edit 
		-value `setEditor -query -editPartitions $setEd`
		$listPartitionsBtn;

    if ($operatingMode == "selectExpand" || $operatingMode == "selectNoExpand")
    {
		button -e -enable false $scrollBtn;
		button -e -enable false $expandAllBtn;
		button -e -enable false $collapseAllBtn;
	}
	else
	{
		int $enableScroll = `setEditor -query -groupComponents $setEd`;
		button -e -enable $enableScroll $scrollBtn;
		button -e -enable true $expandAllBtn;
		button -e -enable true $collapseAllBtn;
	}
}

// *********************************************
// The next bit are all menu creation procedures
// *********************************************

global proc
buildSetEdModeMenu (string $setEd, string $parentMenu)
{
	setParent -menu $parentMenu;

	// These should be unique enough names for the widgets for this
	// instance of the set editor

	string $editingItem        = ($setEd + "editingItem");
	string $selectNoExpandItem = ($setEd + "selectNoExpandItem");
	string $selectExpandItem   = ($setEd + "selectExpandItem");
	string $paintItem          = ($setEd + "paintItem");

	// Check to see if we have to build the menu items

	if (`menu -query -numberOfItems $parentMenu` == 0)
	{
		radioMenuItemCollection;
		menuItem -label "Editing"
			-radioButton true
			-command ("setEditor -edit -mode editing " + $setEd)
			$editingItem;
		menuItem -label "Select"
			-radioButton false
			-command ("setEditor -edit -mode selectNoExpand " + $setEd)
			$selectNoExpandItem;
		menuItem -label "Select Contents"
			-radioButton false
			-command ("setEditor -edit -mode selectExpand " + $setEd)
			$selectExpandItem;
		menuItem -label "Paint Percentages"
			-radioButton false
			-command ("setEditor -edit -mode paint " + $setEd)
			$paintItem;
	}

	// Update radio button checks for operating mode

	string $operatingMode = `setEditor -query -mode $setEd`;
	menuItem -edit
		-radioButton ($operatingMode == "editing")
		$editingItem;
	menuItem -edit
		-radioButton ($operatingMode == "selectNoExpand")
		$selectNoExpandItem;
	menuItem -edit
		-radioButton ($operatingMode == "selectExpand")
		$selectExpandItem;
	menuItem -edit
		-radioButton ($operatingMode == "paint")
		$paintItem;
}

//
//  Procedure Name:
//      setEditorAddChannelBoxToSet
//
//  Description:
//		Adds the selected attributes in the channel box to the highlighted set.
//
//  Input Arguments:
//      $setEd - the set editor to add the items to
//
//  Return Value:
//      None.
//
global proc setEditorAddChannelBoxToSet( string $setEd ) 
{
	int $itemsAdded = false;
	string $mainAttrs[]	= 
		`channelBox -q -selectedMainAttributes mainChannelBox`;
	string $mainNodes[]	= 
		`channelBox -q -mainObjectList mainChannelBox`;
    string $shapeAttrs[] = 
		`channelBox -q -selectedShapeAttributes mainChannelBox`;
	string $shapeNodes[] = 
		`channelBox -q -shapeObjectList mainChannelBox`;
    string $historyAttrs[] = 
		`channelBox -q -selectedHistoryAttributes mainChannelBox`;
	string $historyNodes[] = 
		`channelBox -q -historyObjectList mainChannelBox`;

	// Check if there are attributes selected in the channel box.
	// If there are, we'll add those to the set instead
	//
	if ( size( $mainAttrs ) > 0 ) {
		for ( $node in $mainNodes ) {
			for ( $attr in $mainAttrs ) {
				setEditor -edit -addToSet -itemToAdd ( $node + "." + $attr ) 
					$setEd;
			}
		}
		$itemsAdded = true;
	}
	if ( size( $shapeAttrs ) > 0 ) {
		for ( $node in $shapeNodes ) {
			for ( $attr in $shapeAttrs ) {
				setEditor -edit -addToSet -itemToAdd ( $node + "." + $attr ) 
					$setEd;
			}
		}
		$itemsAdded = true;
	}
	if ( size( $historyAttrs ) > 0 ) {
		for ( $node in $historyNodes ) {
			for ( $attr in $historyAttrs ) {
				setEditor -edit -addToSet -itemToAdd ( $node + "." + $attr ) 
					$setEd;
			}
		}
		$itemsAdded = true;
	}

	if ( !$itemsAdded ) {
		// Nothing was selected in the channel box
		//
		warning( "No attributes are selected in the channel box." );
	}

}

global proc
buildSetEdEditMenu (string $setEd, string $parentMenu)
{
	setParent -menu $parentMenu;

	// These should be unique enough names for the widgets for this
	// instance of the set editor

	string $addItem        = ($setEd + "addItem");
	string $addChannelItem = ($setEd + "addChannelBoxItem");
	string $removeItem     = ($setEd + "removeItem");
	string $setKeyItem     = ($setEd + "setKeyItem");
	string $selectAttrItem = ($setEd + "selectAttrItem");

	// Check to see if we have to build the menu items

	if (`menu -query -numberOfItems $parentMenu` == 0)
	{
		menuItem -label "Add Items" 
			-command ( "setEditor -edit -addToSet " + $setEd )
			$addItem;
		menuItem -label "Add Items from Channel Box" 
			-command ( "setEditorAddChannelBoxToSet( \"" + $setEd + "\" )" )
			$addChannelItem;
		menuItem -label "Remove Items"
			-command ("setEditor -edit -removeFromSet " + $setEd)
			$removeItem;

		menuItem -l "Create Set"
			-annotation "Create Set: Make a set containing the selected object(s)"			
			-c "performCreateSet false";
		menuItem -optionBox true 
			-l "Create Set Option Box"
			-c "performCreateSet true";
		
		menuItem -l "Create Partition"
			-annotation "Create Partition: Make a partition containing the selected set(s)"			
			-c "performCreatePartition false";
		menuItem -optionBox true 
			-l "Create Partition Option Box"
			-c "performCreatePartition true";

		menuItem -divider true;

		menuItem -label "Key Selected Weight(s)"
			-command ("setEditor -edit -awc setKeyframe "+$setEd)
			$setKeyItem;

		menuItem -label "Select Weight Attribute(s)"
			-command ("setEditor -edit -awc select "+$setEd)
			$selectAttrItem;

	}

	// Disable add/remove entries if we are not in editing mode

	string $operatingMode = `setEditor -query -mode $setEd`;
	menuItem -edit -enable ($operatingMode == "editing") $addItem;
	menuItem -edit -enable ($operatingMode == "editing") $removeItem;
}

global proc
updateSetEdFiltersMenu (string $setEd, string $parentMenu)
{
	setParent -menu $parentMenu;

	// These should be unique enough names for the widgets for this
	// instance of the set editor

	string $showNormalSetsItem          = ($setEd + "showNormalSetsItem");
	string $showRenderSetsItem          = ($setEd + "showRenderSetsItem");
	string $showDeformerSetsItem        = ($setEd + "showDeformerSetsItem");
//	string $showCustomFiltersMenu       = ($setEd + "showCustomFiltersMenu");
//	string $showBoneLatticeSetsItem     = ($setEd + "showBoneLatticeSetsItem");
	string $showBlendShapeSetsItem      = ($setEd + "showBlendShapeSetsItem");
	string $showClusterSetsItem         = ($setEd + "showClusterSetsItem");
	string $showJointClusterSetsItem    = ($setEd + "showJointClusterSetsItem");
//	string $showJointLatticeSetsItem    = ($setEd + "showJointLatticeSetsItem");
	string $showLatticeSetsItem         = ($setEd + "showLatticeSetsItem");
	string $showLatticeDualBaseSetsItem = ($setEd + "showLatticeDualBaseSetsItem");
	string $showSculptSetsItem          = ($setEd + "showSculptSetsItem");
	string $showWireSetsItem            = ($setEd + "showWireSetsItem");

	menuItem -edit
		-checkBox `setEditor -query -showNormalSets $setEd`
		$showNormalSetsItem;
	menuItem -edit
		-checkBox `setEditor -query -showRenderSets $setEd`
		$showRenderSetsItem;
	menuItem -edit
		-checkBox `setEditor -query -showDeformerSets $setEd`
		$showDeformerSetsItem;
//	menuItem -edit
//		-checkBox `setEditor -query -showBoneLatticeSets $setEd`
//		$showBoneLatticeSetsItem;
	menuItem -edit
		-checkBox `setEditor -query -showBlendShapeSets $setEd`
		$showBlendShapeSetsItem;
	menuItem -edit
		-checkBox `setEditor -query -showClusterSets $setEd`
		$showClusterSetsItem;
	menuItem -edit
		-checkBox `setEditor -query -showJointClusterSets $setEd`
		$showJointClusterSetsItem;
//	menuItem -edit
//		-checkBox `setEditor -query -showJointLatticeSets $setEd`
//		$showJointLatticeSetsItem;
	menuItem -edit
		-checkBox `setEditor -query -showLatticeSets $setEd`
		$showLatticeSetsItem;
	menuItem -edit
		-checkBox `setEditor -query -showLatticeDualBaseSets $setEd`
		$showLatticeDualBaseSetsItem;
	menuItem -edit
		-checkBox `setEditor -query -showSculptSets $setEd`
		$showSculptSetsItem;
	menuItem -edit
		-checkBox `setEditor -query -showWireSets $setEd`
		$showWireSetsItem;

	int $enableDefs = !`setEditor -query -showDeformerSets $setEd`;

	int $inSetsMode = true;
	if (!`setEditor -query -editSets $setEd`)
	{
		$inSetsMode = false;
		$enableDefs = false;
	}

	menuItem -edit -enable $inSetsMode $showNormalSetsItem;
	menuItem -edit -enable $inSetsMode $showRenderSetsItem;
	menuItem -edit -enable $inSetsMode $showDeformerSetsItem;

//	menuItem -edit -enable $enableDefs $showBoneLatticeSetsItem;
	menuItem -edit -enable $enableDefs $showBlendShapeSetsItem;
	menuItem -edit -enable $enableDefs $showClusterSetsItem;
	menuItem -edit -enable $enableDefs $showJointClusterSetsItem;
//	menuItem -edit -enable $enableDefs $showJointLatticeSetsItem;
	menuItem -edit -enable $enableDefs $showLatticeSetsItem;
	menuItem -edit -enable $enableDefs $showLatticeDualBaseSetsItem;
	menuItem -edit -enable $enableDefs $showSculptSetsItem;
	menuItem -edit -enable $enableDefs $showWireSetsItem;
}

global proc
buildSetEdFiltersMenu (string $setEd, string $parentMenu)
{
	setParent -menu $parentMenu;

	// These should be unique enough names for the widgets for this
	// instance of the set editor

	string $showNormalSetsItem          = ($setEd + "showNormalSetsItem");
	string $showRenderSetsItem          = ($setEd + "showRenderSetsItem");
	string $showDeformerSetsItem        = ($setEd + "showDeformerSetsItem");
	string $showCustomFiltersMenu       = ($setEd + "showCustomFiltersMenu");
//	string $showBoneLatticeSetsItem     = ($setEd + "showBoneLatticeSetsItem");
	string $showBlendShapeSetsItem      = ($setEd + "showBlendShapeSetsItem");
	string $showClusterSetsItem         = ($setEd + "showClusterSetsItem");
	string $showJointClusterSetsItem    = ($setEd + "showJointClusterSetsItem");
//	string $showJointLatticeSetsItem    = ($setEd + "showJointLatticeSetsItem");
	string $showLatticeSetsItem         = ($setEd + "showLatticeSetsItem");
	string $showLatticeDualBaseSetsItem = ($setEd + "showLatticeDualBaseSetsItem");
	string $showSculptSetsItem          = ($setEd + "showSculptSetsItem");
	string $showWireSetsItem            = ($setEd + "showWireSetsItem");

	// Check to see if we have to build the menu items

	if (`menu -query -numberOfItems $parentMenu` == 0)
	{
		menuItem -label "All Regular Sets"
			-checkBox 0
			-command ("setEditor -edit -showNormalSets #1 " + $setEd)
			$showNormalSetsItem;
		menuItem -label "All Render Sets"
			-checkBox 0
			-command ("setEditor -edit -showRenderSets #1 " + $setEd)
			$showRenderSetsItem;
		menuItem -label "All Deformer Sets"
			-checkBox 0
			-command ("setEditor -edit -showDeformerSets #1 " + $setEd + " ; updateSetEdFiltersMenu " + $setEd + " " + $parentMenu)
			$showDeformerSetsItem;

		menuItem -label "Item Filters" -subMenu true $showCustomFiltersMenu;
		menuItem -edit
			-postMenuCommand ("buildSetEdCustomFiltersMenu " + $setEd + " " + $showCustomFiltersMenu)
			$showCustomFiltersMenu;
		setParent -menu ..;

		menuItem -divider true;

// This is the set containing the actual points in the bone lattice
// so it is not very interesting.
//
//		menuItem -label "Bone Lattice Sets"
//			-checkBox 0
//			-command ("setEditor -edit -showBoneLatticeSets #1 " + $setEd)
//			$showBoneLatticeSetsItem;
		menuItem -label "Blend Shape Sets"
			-checkBox 0
			-command ("setEditor -edit -showBlendShapeSets #1 " + $setEd)
			$showBlendShapeSetsItem;
		menuItem -label "Cluster Sets"
			-checkBox 0
			-command ("setEditor -edit -showClusterSets #1 " + $setEd)
			$showClusterSetsItem;
		menuItem -label "Joint Cluster Sets"
			-checkBox 0
			-command ("setEditor -edit -showJointClusterSets #1 " + $setEd)
			$showJointClusterSetsItem;
// This is the set containing the actual points in the joint lattice
// so it is not very interesting.
//
//		menuItem -label "Joint Lattice Flexor Sets"
//			-checkBox 0
//			-command ("setEditor -edit -showJointLatticeSets #1 " + $setEd)
//			$showJointLatticeSetsItem;
		menuItem -label "Joint Lattice Sets"
			-checkBox 0
			-command ("setEditor -edit -showLatticeDualBaseSets #1 " + $setEd)
			$showLatticeDualBaseSetsItem;
		menuItem -label "Lattice Sets"
			-checkBox 0
			-command ("setEditor -edit -showLatticeSets #1 " + $setEd)
			$showLatticeSetsItem;
		menuItem -label "Sculpt Sets"
			-checkBox 0
			-command ("setEditor -edit -showSculptSets #1 " + $setEd)
			$showSculptSetsItem;
		menuItem -label "Wire Sets"
			-checkBox 0
			-command ("setEditor -edit -showWireSets #1 " + $setEd)
			$showWireSetsItem;
	}

	updateSetEdFiltersMenu($setEd, $parentMenu);
}


global proc
buildSetEdListMenu (string $setEd, string $parentMenu)
{
	setParent -menu $parentMenu;

	// These should be unique enough names for the widgets for this
	// instance of the set editor. The filters item needs a wee bit
	// extra uniqueness since it gets used in the update filters callback.

	string $scrollItem     = ($setEd + "scrollItem");
	string $expandItem     = ($setEd + "expandItem");
	string $collapseItem   = ($setEd + "collapseItem");
	string $setsItem       = ($setEd + "setItem");
	string $partitionsItem = ($setEd + "partitionItem");
	string $updateItem     = ($setEd + "updateItem");
	string $addToListItem  = ($setEd + "addToListItem");
	string $allItem        = ($setEd + "allItem");
	string $filtersItem    = ($setEd + $parentMenu + "filtersItem");
	
	// Check to see if we have to build the menu items

	if (`menu -query -numberOfItems $parentMenu` == 0)
	{
		menuItem -label "Scroll Frames to Selection" 
			-command ("setEditor -edit -scrollFrames " + $setEd)
			$scrollItem;

		menuItem -label "Expand All Frames"
			-command ("setEditor -edit -expandFrames " + $setEd)
			$expandItem;

		menuItem -label "Collapse All Frames"
			-command ("setEditor -edit -collapseFrames " + $setEd)
			$collapseItem;

		menuItem -divider true;

		radioMenuItemCollection;
			menuItem -label "Sets"
				-radioButton true
				-command ("setEditor -edit -editSets " + $setEd) $setsItem;
			menuItem -label "Partitions"
				-radioButton false
				-command ("setEditor -edit -editPartitions " + $setEd) $partitionsItem;
		setParent -menu $parentMenu;

		menuItem -divider true;

		menuItem -label "Update Now" 
			-command ("setEditor -edit -reload " + $setEd)
			$updateItem;
		menuItem -label "Add Selected to List" 
			-command ("setEditor -edit -addToList " + $setEd)
			$addToListItem;
		menuItem -label "All Sets"
			-command ("setEditor -edit -allSets " + $setEd)
			$allItem;

		menuItem -divider true;

		menuItem -label "Filters" -subMenu true -tearOff true $filtersItem;
	}

	// Update state and enable/disable status

	buildSetEdFiltersMenu($setEd, $filtersItem);
	string $operatingMode = `setEditor -query -mode $setEd`;
	if ($operatingMode == "selectExpand" || $operatingMode == "selectNoExpand")
	{
		menuItem -edit -enable false $scrollItem;
		menuItem -edit -enable false $expandItem;
		menuItem -edit -enable false $collapseItem;
	}
	else
	{
		int $enableScroll = `setEditor -query -groupComponents $setEd`;
		menuItem -edit -enable $enableScroll $scrollItem;
		menuItem -edit -enable true $expandItem;
		menuItem -edit -enable true $collapseItem;
	}
	if (`setEditor -query -editSets $setEd`)
	{
		menuItem -edit -radioButton true $setsItem;
		menuItem -edit -enable true $updateItem;
		menuItem -edit -enable true $addToListItem;
		menuItem -edit -enable true $allItem;
	}
	else
	{
		menuItem -edit -radioButton true $partitionsItem;
		menuItem -edit -enable false $updateItem;
		menuItem -edit -enable false $addToListItem;
		menuItem -edit -enable false $allItem;
	}
	menuItem -edit -enable true $filtersItem;
}

global proc
buildSetEdBookmarksMenu (string $setEd, string $parentMenu)
{
	setParent -menu $parentMenu;

    menu -e -deleteAllItems $parentMenu;

    menuItem -label "Add Bookmark" -command ("setEdBookmarkAddCallback " + $setEd + " later");
    menuItem -optionBox true -l "Add Bookmark Option Box"
		-command ("setEdBookmarkAddCallback " + $setEd + " first");
    menuItem -label "Bookmark Editor..." -command ("setEdBookmarkEditor " + $setEd);
    menuItem -divider true;
    menuItem -divider true;
	
    // Create a menu item for each bookmark

    string $bookmarks[] = `setEditor -query -listBookmarks $setEd`;
    string $bookmark;

	for ($bookmark in $bookmarks)
	{
		menuItem -l $bookmark
			-command ("setEditor -edit -useBookmark " + $bookmark + " " + $setEd);
		menuItem -optionBox true -l ($bookmark + " Option Box")
			-command ("setEdBookmarkRenameMenuCallback " + $setEd + " " + $bookmark);
	}
}

global proc
buildSetEdCustomFiltersMenu (string $setEd, string $parentMenu)
{
	setParent -menu $parentMenu;

	// Get rid of previous menu contents
	menu -e -deleteAllItems $parentMenu;

	// Get list of all existing custom filters
	string $filterNames[] = `lsUI -filters`;

	// Determine current filter being used by the set editor
	string $currFilter = `setEditor -query -filter $setEd`;

	// Create an menu entry for each existing filter 

 	string $noFilterItem = `menuItem -rb 1 -label "No Filter"`;
	menuItem -e -command ("setEditor -edit -filter 0 " + $setEd) $noFilterItem;

	for ($i = 0; $i < size($filterNames); $i++)
	{
		string $fname = $filterNames[$i];

		menuItem -rb 0 -label `interToUI $fname`
			-c ("setEditor -edit -filter " + $fname + " " + $setEd)
			($fname + "Item");
		if ($fname == $currFilter)
		{
			menuItem -e -rb 1 ($fname + "Item");
			menuItem -e -rb 0 $noFilterItem;
		} 
	}
}

global proc
buildSetEdOptionsMenu (string $setEd, string $parentMenu)
{
	setParent -menu $parentMenu;

	// These should be unique enough names for the widgets for this
	// instance of the set editor

	string $autoUpdateItem     = ($setEd + "autoUpdateItem");
	string $autoExpandItem     = ($setEd + "autoExpandItem");
	string $autoScrollItem     = ($setEd + "autoScrollItem");
	string $listByObjectItem   = ($setEd + "listByObject");
	string $listLengthItem     = ($setEd + "listLengthItem");

	// Check to see if we have to build the menu items

	if (`menu -query -numberOfItems $parentMenu` == 0)
	{
		menuItem -label "Auto Update"
			-checkBox 0
			-command ("setEditor -edit -autoUpdate #1 " + $setEd + " ; updateMenuGreyStates " + $setEd)
			$autoUpdateItem;
		menuItem -label "Auto Expand Frames"
			-checkBox 0
			-command ("setEditor -edit -autoExpand #1 " + $setEd)
			$autoExpandItem;
		if (!`about -nt`) {
			menuItem -label "Auto Scroll to Selection"
				-checkBox 0
				-command ("setEditor -edit -autoScroll #1 " + $setEd)
				$autoScrollItem;
		}
		menuItem -label "List by Object"
			-checkBox 0
			-command ("setEditor -edit -groupComponents #1 " + $setEd)
			$listByObjectItem;
		if (!`about -nt`) {
			menuItem -label "Object List Length ..."
				-command ("performListLength " + $setEd)
				$listLengthItem;
		}
	}

	// Update the checkbox state for the menu items

	menuItem -edit
		-checkBox `setEditor -query -autoUpdate $setEd`
		$autoUpdateItem;
	menuItem -edit
		-checkBox `setEditor -query -autoExpand $setEd`
		$autoExpandItem;
	if (!`about -nt`) {
		menuItem -edit
			-checkBox `setEditor -query -autoScroll $setEd`
			$autoScrollItem;
	}
	menuItem -edit
		-checkBox `setEditor -query -groupComponents $setEd`
		$listByObjectItem;

	// Update the enable/disable state for the menu items

	string $operatingMode = `setEditor -query -mode $setEd`;
	if ($operatingMode == "selectExpand" || $operatingMode == "selectNoExpand"
		|| `setEditor -query -autoUpdate $setEd`)
	{
		menuItem -edit -enable false $autoExpandItem;
		menuItem -edit -enable false $listByObjectItem;
		if (!`about -nt`) {
			menuItem -edit -enable false $autoScrollItem;
			menuItem -edit -enable false $listLengthItem;
		}
	}
	else
	{
		menuItem -edit -enable true $autoExpandItem;
		menuItem -edit -enable true $listByObjectItem;
		if (!`about -nt` && `setEditor -query -groupComponents $setEd`)
		{
			if (!`about -nt`) {
				menuItem -edit -enable true $autoScrollItem;
				menuItem -edit -enable true $listLengthItem;
			}
		}
		else
		{
			if (!`about -nt`) {
				menuItem -edit -enable false $autoScrollItem;
				menuItem -edit -enable false $listLengthItem;
			}
		}
	}
}

global proc
performListLength (string $setEd)
{
	window -title "Mini Scroll Bar Length" -width 240 setTempWindow;
	columnLayout -adj true;
	intField -min 1 -max 100 -value 1 intItem;
	intField -edit -value (`setEditor -query -componentListLength $setEd`) intItem;
	intField -edit -cc ("setEditor -edit -componentListLength #1 " + $setEd) intItem;;
	showWindow setTempWindow;
}

global proc
//showSetEditorMenu (int $show, string $frameLayout)
showSetEditorMenu (int $show, string $menuBarLayout)
{
	if ($show) {
//		frameLayout -edit -collapse false $frameLayout;
		menuBarLayout -e -h 25 $menuBarLayout;
	}
	else {
//		frameLayout -edit -collapse true $frameLayout;
		menuBarLayout -e -h 1 $menuBarLayout;
	}
	optionVar -intValue showSetEditorMenu $show;
}

global proc SetEditorPopupMenu(string $setEd)
//
//  Description:
//      This script creates popup menu items for the set editor.
//
{
	// Define a unique name for the popup menu for this
	// instance of the set editor
	string $menuPopup = ($setEd + "menuPopup");

	popupMenu -ctrlModifier false
		-button 3 -aob false -parent $setEd
		$menuPopup;

		// These should be unique enough names for the widgets for this
		// instance of the set editor

		string $modePopupMenu = ($setEd + "modePopupMenu");
		string $editPopupMenu = ($setEd + "editPopupMenu");
		string $listPopupMenu = ($setEd + "listPopupMenu");
		string $bookmarksPopupMenu = ($setEd + "bookmarksPopupMenu");
		string $optionsPopupMenu = ($setEd + "optionsPopupMenu");

		menuItem -label "Mode" -tearOff true -subMenu true $modePopupMenu;
		menuItem -edit -postMenuCommand ("buildSetEdModeMenu " + $setEd + " " + $modePopupMenu) $modePopupMenu;
		setParent -menu ..;

		menuItem -label "Edit" -tearOff true -subMenu true -aob true $editPopupMenu;
		menuItem -edit -postMenuCommand ("buildSetEdEditMenu " + $setEd + " " + $editPopupMenu) $editPopupMenu;
		setParent -menu ..;

		menuItem -label "List" -tearOff true -subMenu true $listPopupMenu;
		menuItem -edit -postMenuCommand ("buildSetEdListMenu " + $setEd + " " + $listPopupMenu) $listPopupMenu;
		setParent -menu ..;

		// Since the bookmark menu is dynamic allowing it to be torn off
		// causes crashes
		menuItem -label "Bookmarks" -tearOff false -subMenu true -aob true $bookmarksPopupMenu;
		menuItem -edit -postMenuCommand ("buildSetEdBookmarksMenu " + $setEd + " " + $bookmarksPopupMenu) $bookmarksPopupMenu;
		setParent -menu ..;

		menuItem -label "Options" -tearOff true -subMenu true $optionsPopupMenu;
		menuItem -edit -postMenuCommand ("buildSetEdOptionsMenu " + $setEd + " " + $optionsPopupMenu) $optionsPopupMenu;
		setParent -menu ..;

	setParent -menu ..;
}

// *********************************************
// End of menu creation procedures.
// *********************************************


global proc
createSetEditor (string $whichPanel)
//
//  Description:
//		Define the editors that are used in this panel.  No
//		controls (widgets) are created at this point.
//
{
	// Create a unique name for the editor based on panel name

	string $setEd = ($whichPanel + "SetEd");
	setEditor -unParent $setEd;
}

global proc
addSetEditor (string $whichPanel)
//
//  Description:
//		Add the panel to a layout.
//		Parent the editors to that layout and create any other
//		controls (widgets) required.
//
{
	string $setEd = ($whichPanel + "SetEd");

	waitCursor -state on;

	// Define the standard set editing panel

	string $menuBarLayoutName = `scriptedPanel -q -control $whichPanel`;
	string $formLayoutName = `formLayout`;
		
	if (`optionVar -exists showSetEditorMenu`) {
		showSetEditorMenu (`optionVar -query showSetEditorMenu`) $menuBarLayoutName;
	}

	// Attach the menus to the menu form

//	setParent -menu $menuBarLayoutName;
	setParent $menuBarLayoutName;

		// These should be unique enough names for the widgets for this
		// instance of the set editor

		string $modeMenu = ($setEd + "modeMenu");
		string $editMenu = ($setEd + "editMenu");
		string $listMenu = ($setEd + "listMenu");
		string $bookmarksMenu = ($setEd + "bookmarksMenu");
		string $optionsMenu = ($setEd + "optionsMenu");

		menu -label "Mode" 
			-tearOff true 
			-familyImage "menuIconMode.xpm"
			$modeMenu;
		menu -edit -postMenuCommand ("buildSetEdModeMenu " + $setEd + " " + $modeMenu) $modeMenu;
		setParent -menu ..;

		menu -label "Edit" 
			-tearOff true 
			-aob true 
			-familyImage "menuIconEdit.xpm"
			$editMenu;
		menu -edit -postMenuCommand ("buildSetEdEditMenu " + $setEd + " " + $editMenu) $editMenu;
		setParent -menu ..;

		menu -label "List" 
			-tearOff true 
			-familyImage "menuIconList.xpm"
			$listMenu;
		menu -edit -postMenuCommand ("buildSetEdListMenu " + $setEd + " " + $listMenu) $listMenu;
		setParent -menu ..;

		// Since the bookmark menu is dynamic allowing it to be torn off
		// causes crashes
		menu -label "Bookmarks" 
			-tearOff false 
			-aob true 
			-familyImage "menuIconBookmarks.xpm"
			$bookmarksMenu;
		menu -edit -postMenuCommand ("buildSetEdBookmarksMenu " + $setEd + " " + $bookmarksMenu) $bookmarksMenu;
		setParent -menu ..;

		menu -label "Options" 
			-tearOff true 
			-familyImage "menuIconOptions.xpm"
			$optionsMenu;
		menu -edit -postMenuCommand ("buildSetEdOptionsMenu " + $setEd + " " + $optionsMenu) $optionsMenu;
		setParent -menu ..;

	//	Setup buttons for the toolbar

	setParent $formLayoutName;

	//	Construct unique names for the buttons
	//	for this instance of the editor

	string $setEdToolBarForm	= ($setEd + "toolbarForm");
	string $editModeBtn			= ($setEd + "editModeBtn");
	string $selectModeBtn		= ($setEd + "selectModeBtn");
	string $contentsModeBtn		= ($setEd + "contentsModeBtn");
	string $paintModeBtn		= ($setEd + "paintModeBtn");
	string $scrollBtn			= ($setEd + "scrollBtn");
	string $expandAllBtn		= ($setEd + "expandAllBtn");
	string $collapseAllBtn		= ($setEd + "collapseAllBtn");
	string $addItemsBtn			= ($setEd + "addItemsBtn");
	string $removeItemsBtn		= ($setEd + "removeItemsBtn");
	string $listSetsBtn			= ($setEd + "listSetsBtn");
	string $listPartitionsBtn	= ($setEd + "listPartitionsBtn");

	rowLayout -nc 13
		-cw 1 28 -cw 2 28 -cw 3 28 -cw 4 28
		-cw 5 10 -cw 6 28 -cw 7 28 -cw 8 28
		-cw 9 28 -cw 10 28 -cw 11 10 -cw 12 28 -cw 13 28
		-columnAttach 5 "both" 0 -columnAttach 11 "both" 0
		$setEdToolBarForm;

		symbolCheckBox -image "setEdEditMode.xpm"
			-height 28 -width 28
			-onCommand ("setEditor -edit -mode editing " + $setEd)
			-ann "Edit Mode" $editModeBtn;
		symbolCheckBox -image "setEdSelectMode.xpm"
			-height 28 -width 28
			-onCommand ("setEditor -edit -mode selectNoExpand " + $setEd)
			-ann "Select Mode" $selectModeBtn;
		symbolCheckBox -image "setEdSelectNEMode.xpm"
			-height 28 -width 28
			-onCommand ("setEditor -edit -mode selectExpand " + $setEd)
			-ann "Select Contents Mode" $contentsModeBtn;
		symbolCheckBox -image "setEdPaintMode.xpm"
			-height 28 -width 28
			-onCommand ("setEditor -edit -mode paint " + $setEd)
			-ann "Paint Percentages Mode" $paintModeBtn;

		separator -horizontal false -style "in" -height 28 setSep1;

		symbolButton -image "setEdScrollCmd.xpm"
			-height 28 -width 28
			-command ("setEditor -edit -scrollFrames " + $setEd)
			-ann "Scroll Frames to Selection"
			$scrollBtn;
		symbolButton -image "setEdExpandCmd.xpm" 
			-height 28 -width 28
			-command ("setEditor -edit -expandFrames " + $setEd)
			-ann "Expand All Frames"
			$expandAllBtn;
		symbolButton -image "setEdCollapseCmd.xpm" 
			-height 28 -width 28
			-command ("setEditor -edit -collapseFrames " + $setEd)
			-ann "Collapse All Frames"
			$collapseAllBtn;
		symbolButton -image "setEdAddCmd.xpm" 
			-height 28 -width 28
			-command ("setEditor -edit -addToSet " + $setEd)
			-ann "Add Items to Selected"
			$addItemsBtn;
		symbolButton -image "setEdRemoveCmd.xpm" 
			-height 28 -width 28
			-command ("setEditor -edit -removeFromSet " + $setEd)
			-ann "Remove Items from Selected"
			$removeItemsBtn;

		separator -horizontal false -style "in" -height 28 setSep2;

		symbolCheckBox -image "setEdListSetMode.xpm"
			-height 28 -width 28
			-onCommand ("setEditor -edit -editSets " + $setEd)
			-ann "List Sets Mode" $listSetsBtn;
		symbolCheckBox -image "setEdListPartMode.xpm"
			-height 28 -width 28
			-onCommand ("setEditor -edit -editPartitions " + $setEd)
			-ann "List Partitions Mode" $listPartitionsBtn;
	setParent ..;

	// Parent the editor to the editor layout

	setEditor -edit -parent $formLayoutName $setEd;
	formLayout -edit
		-attachForm $setEdToolBarForm "left" 0
		-attachForm $setEdToolBarForm "right" 0
		-attachForm $setEdToolBarForm "top" 0
		-attachNone $setEdToolBarForm "bottom" 

		-attachForm $setEd left 0
		-attachForm $setEd right 0
		-attachControl $setEd top 0 $setEdToolBarForm
		-attachForm $setEd bottom 0
		$formLayoutName;

	// Attach popup menus to the set editor

	SetEditorPopupMenu $setEd;

	// Create a scriptJob to keep the toolbar buttons
	// in sync with this instance of the setEditor.
	// Parent it to this instance of the set editor so
	// the script job goes away when the editor is
	// closed.

	scriptJob 
		-e "setEditorChanged" 
		("setEditorStateCallback " + $setEd)
		-p $setEd; 

	// Make sure the buttons start off in sync
	setEditorStateCallback $setEd;

	setParent -top;
	waitCursor -state off;
}

global proc
removeSetEditor (string $whichPanel)
//
//  Description:
//		Remove the panel from a layout.
//		Delete controls.
//
{
	string $setEd = ($whichPanel + "SetEd");

	if (`setEditor -exists $setEd`) {
		setEditor -edit -unParent $setEd;
	}
}

global proc
deleteSetEditor (string $whichPanel)
//
//  Description:
//		This proc will delete the contents of the panel, but not
//		the panel itself.
//
//  Note:
//		We only need to delete editors here.  Other UI will be taken care of
//		by the remove proc.
//
{
	string $setEd = ($whichPanel + "SetEd");

	if (`setEditor -exists $setEd`) {
		deleteUI -editor $setEd;
	}
}

global proc string
saveStateSetEditor (string $whichPanel)
//
//  Description:
//		This proc returns a string that when executed will restore the
//		current state of the panel elements.
//
{
	string $indent = "\n\t\t\t";
	string $setEd = ($whichPanel + "SetEd");

	return (
			$indent + "$editorName = ($panelName+\"SetEd\");\n" +
			`setEditor -query -stateString $setEd`
			);
}

global proc
updateMenuGreyStates (string $setEd)
{
	// Always keep the options menu enabled or the user won't
	// be able to turn auto update mode off!

	// Use the unique enough names to update the menubar and popup menu
	// enabled state

	string $modeMenu = ($setEd + "modeMenu");
	string $editMenu = ($setEd + "editMenu");
	string $listMenu = ($setEd + "listMenu");
	string $bookmarksMenu = ($setEd + "bookmarksMenu");
	//string $optionsMenu = ($setEd + "optionsMenu");

	string $modePopupMenu = ($setEd + "modePopupMenu");
	string $editPopupMenu = ($setEd + "editPopupMenu");
	string $listPopupMenu = ($setEd + "listPopupMenu");
	string $bookmarksPopupMenu = ($setEd + "bookmarksPopupMenu");
	//string $optionsPopupMenu = ($setEd + "optionsPopupMenu");

	int $enableMenus = !`setEditor -query -autoUpdate $setEd`;

	menu -edit -enable $enableMenus $modeMenu;
	menu -edit -enable $enableMenus $editMenu;
	menu -edit -enable $enableMenus $listMenu;
	menu -edit -enable $enableMenus $bookmarksMenu;
	menuItem -edit -enable $enableMenus $modePopupMenu;
	menuItem -edit -enable $enableMenus $editPopupMenu;
	menuItem -edit -enable $enableMenus $listPopupMenu;
	menuItem -edit -enable $enableMenus $bookmarksPopupMenu;
}
