// 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:  Feb 20 1997
//
//  Description:
//      This script will update the specified  panel arrangement to reflect
//		the current state of the panels in the main pane layout ($gMainPane).
//
//  Input Arguments:
//      $whichNamedLayout:  The panel arrangement to be modified.
//
//  Return Value:
//      None.
//
//	Note:
//		None.
// 

global proc updatePanelLayoutFromCurrent( string $whichNamedLayout ) {

	global string $gMainPane;

	string $panelName;
	string $controlName;
	string $label;
	int    $wh[];
	int    $nPanes;
	int	   $i,$n;
	int	   $exists;

	string $buffStr, $buffStr2;;
	string $kCameraFlagStr = "-cam[a-z]*[ ][^ ^;]*";
	string $kFindPerspStr = "-cam `findStartUpCamera persp`";
	string $kFindTopStr = "-cam `findStartUpCamera top`";
	string $kFindSideStr = "-cam `findStartUpCamera side`";
	string $kFindFrontStr = "-cam `findStartUpCamera front`";

	string $pCam = `findStartUpCamera persp`;
	string $fCam = `findStartUpCamera front`;
	string $sCam = `findStartUpCamera side`;
	string $tCam = `findStartUpCamera top`;

	//  check to make sure the specified arrangement exists
	//
	string $pConfig = `getPanel -cwl $whichNamedLayout`;
	if ($pConfig != "") {
		$exists = true;
	} else {
		$exists = false;
	}


	if (!$exists) {
		warning ("Panel Arrangement not found: " + $whichNamedLayout);
	} else {
		if (`paneLayout -exists $gMainPane`) {
			$wh = `paneLayout -q -ps $gMainPane`;
			$nPanes = size($wh)/2;
			//
			//  first do config string
			//
			string $configStr = ("global string $gMainPane; paneLayout -e -cn \"" + `paneLayout -q -cn $gMainPane` + "\" ");
			for ($i = 0; $i < $nPanes; $i++) {
				$j = $i+1;
				$configStr += ("-ps " + $j + " " + $wh[2*$i] + " " + $wh[2*$i+1] + " ");
			}
			$configStr += "$gMainPane;";
			panelConfiguration -e -cfs $configStr $pConfig;

			//
			//  Update the panels
			//
			string $panelType, $panelName, $label, $panelCreate, $panelEdit;
			int    $isFixed[] = `panelConfiguration -q -if $pConfig`;
			int    $nPanels = `panelConfiguration -q -np $pConfig`;

			for ($i = 0; $i < $nPanes; $i++) {
				switch ($i) {
				case 0:
					$controlName = `paneLayout -q -p1 $gMainPane`;
					break;
				case 1:
					$controlName = `paneLayout -q -p2 $gMainPane`;
					break;
				case 2:
					$controlName = `paneLayout -q -p3 $gMainPane`;
					break;
				case 3:
					$controlName = `paneLayout -q -p4 $gMainPane`;
					break;
				}

				$panelType = "";
				$panelName = "";
				$label = "";
				$panelCreate = "";
				$panelEdit = "";
				if ("" != $controlName) {
					$panelName = `getPanel -containing $controlName`;
					if ("" != $panelName) {
						$label = `panel -q -label $panelName`;
						$panelType = `getPanel -typeOf $panelName`;
						$panelCreate = `panel -q -cs $panelName`;
						$panelEdit = `panel -q -es $panelName`;
					}

					//
					//  Substitute dynamic camera function for top,persp,front,side panels.
					//
					if ("modelPanel" == $panelType) {
						if ("Persp View" == $label) {
							if (`modelPanel -q -cam $panelName` == $pCam) {
								$buffStr = substitute ($kCameraFlagStr, $panelEdit, $kFindPerspStr);
								$buffStr2 = substitute ($kCameraFlagStr, $panelCreate, $kFindPerspStr);
								$panelEdit = $buffStr;
								$panelCreate = $buffStr2;
							}
						} else if ("Top View" == $label) {
							if (`modelPanel -q -cam $panelName` == $tCam) {
								$buffStr = substitute ($kCameraFlagStr, $panelEdit, $kFindTopStr);
								$buffStr2 = substitute ($kCameraFlagStr, $panelCreate, $kFindTopStr);
								$panelEdit = $buffStr;
								$panelCreate = $buffStr2;
							}
						} else if ("Side View" == $label) {
							if (`modelPanel -q -cam $panelName` == $sCam) {
								$buffStr = substitute ($kCameraFlagStr, $panelEdit, $kFindSideStr);
								$buffStr2 = substitute ($kCameraFlagStr, $panelCreate, $kFindSideStr);
								$panelEdit = $buffStr;
								$panelCreate = $buffStr2;
							}
						} else if ("Front View" == $label) {
							if (`modelPanel -q -cam $panelName` == $fCam) {
								$buffStr = substitute ($kCameraFlagStr, $panelEdit, $kFindFrontStr);
								$buffStr2 = substitute ($kCameraFlagStr, $panelCreate, $kFindFrontStr);
								$panelEdit = $buffStr;
								$panelCreate = $buffStr2;
							}
						}
					}
				}

				if ($i < $nPanels) {
					panelConfiguration -e -rp ($i+1) $isFixed[$i] $label $panelType 
						$panelCreate $panelEdit $pConfig;
				} else {
					panelConfiguration -e -ap false $label $panelType 
						$panelCreate $panelEdit $pConfig;
				}
			}

			// 
			//  Remove any excess panels from the config.
			//
			for (; $i < $nPanels; $i++) {
				panelConfiguration -e -removeLastPanel $pConfig;
			}
		}
	}
}

