////////////////////////////////////////////////////////////////////////////
//                                                                        //
//          Copyright (C) 2011 ANSYS Inc.  All rights reserved.           //
//     Unauthorized use, distribution or duplication is prohibited.      //
//                                                                        //
// This file contains proprietary software licensed from ANSYS Inc.       //
// This header must remain in any source code despite modifications or    //
// enhancements by any party.                                             //
//                                                                        //
////////////////////////////////////////////////////////////////////////////
//
// $Workfile: $
// $Revision: 527404 $
// $Date: 2012-01-13 04:28:12 -0500 (Fri, 13 Jan 2012) $
// $Author: mbauskar $
//
// Decription: JScript for the AG (ANSYS Geometry) Workbench GUI
//             Implements Lattice
//
// All declared identifiers start with 'ag'
////////////////////////////////////////////////////////////////////////////

function agLatticeTable(check)
{
  // If displaying the lattice editor AND the control is not built yet, build it now!
  if (check && !ag.latticeControlCreated)
    createLatticeControl();
    
  // Activate/deactivates the lattice editor pane
  if(ag.latticeEditorVis != check)
  {
    ag.latticeEditorVis = check;
    // Have to call through the frame for unix
    ag.frame.Show(ag.latticeEditorPane, (check) ? true : false);
  }
  agLatticeEditorUpdate ();
  if (check) {
	ag.gb.BodyDisplayMode = 1;
	ag.LatticeEditor_prevhiddenbody = null;
	ag.LatticeEditor_prevshowninside = new Array ();
  } else {
	if ( ag.gb.BodyDisplayMode != 0 )
		ag.gb.BodyDisplayMode = 0;
  }
}

function createLatticeControl()
{
    ag.lattice = ag.latticeEditorPane.LoadControl(ag.pagesDirNoLocale + "/html/agLatticeEditor.htm",
                                                  ag.wb);
	ag.latticeControlCreated = true;
}	

function agLatticeEditorObject(obj)
{
  //Sets the current Lattice Editor Object
  if(obj != ag.latticeEditorObj)
  {
    ag.latticeEditorObj = obj;
    if(obj == null)
      agLatticeTable(false);
    else if(ag.latticeEditorVis)
      agLatticeEditorUpdate();
  }
}

function agLatticeEditorClear()
{
  if(ag.latticeEditorObj == null) return;

  ag.latticeEditorObj = null;
}

function agLatticeEditorUpdate()
{
  if(ag.latticeEditorVis)
  {
    // set the symmetry checkboxes
    if ( ag.latticeEditorScript )
    {
	  var sym = ag.latticeEditorObj.Symmetry;
      if ( sym & ag.c.LatticeSymmetryH )
	    ag.latticeEditorScript.ButtonSymH.checked = true;
      else
        ag.latticeEditorScript.ButtonSymH.checked = false;
      if ( sym & ag.c.LatticeSymmetryV )
	    ag.latticeEditorScript.ButtonSymV.checked = true;
      else
	    ag.latticeEditorScript.ButtonSymV.checked = false;
      if ( sym & ag.c.LatticeSymmetryD1 )
	    ag.latticeEditorScript.ButtonSymD1.checked = true;
	  else
	    ag.latticeEditorScript.ButtonSymD1.checked = false;
      if ( sym & ag.c.LatticeSymmetryD2 )
	    ag.latticeEditorScript.ButtonSymD2.checked = true;
	  else
	    ag.latticeEditorScript.ButtonSymD2.checked = false;
	}
    agLatticeEditor_PopulateTable(ag.latticeEditorCtrl);
  }
}

function agLatticeEditor_CloseButton ()
{
  var obj = ag.latticeEditorObj;
  if(obj == null) return;
  obj.LatticeMatrixEdit = 0;
  ag.LatticeEditor_prevhiddenbody = null;
  agLatticeTable(false);
  agListview(obj, ag.lvFEATURE);
}

function agLatticeEditor_BuildTable (ctrl)
{
	//Border style
var ccNone = 0;
var ccFixedSingle = 1;

//Appearance
var ccFlat = 0;
var cc3D = 1;


//drag modes
var ccOLEDragManual = 0;//I want to initiate the dragdrop operation by calling OLEDrag() - typically in MouseMove
var ccOLEDragAutomatic = 1;//control takes care automatically

//drop modes
var DG_ccOLEDropNone = 0; //no drop provided
var DG_ccOLEDropManual = 1; //I want to override OLEDragOver and OLEDragDrop events
var DG_ccOLEDropAutomatic = 2; //control takes care automatically

//Copy mode constants
var DG_cmcManual = 0;
var DG_cmcAutomatic = 1;

//Cut Mode Constants
var DG_cutmcManual = 0;
var DG_cutmcAutomatic = 1;

//Paste Mode Constants
var DG_pmcManual = 0;
var DG_pmcAutomatic = 1;

//Delete Mode Constants
var DG_dmcManual = 0;
var DG_dmcAutomatic = 1;

//Inplace editing control size constants
var DG_ecscCellSize = 0;
var DG_ecscCellTextSize = 1;

//Inplace editing control resize constants
var DG_ecrcContentSize = 0;
var DG_ecrcCellSize = 1;

  // This function builds the WBDataGridCtrl table by settings preferences
  // and adding lattice table columns to the grid.

  // Bail out now if the control has not been created
  if (!ctrl) return;

  // Set some preferences first
  ag.latticeEditorCtrl = ctrl;
  
  ctrl.Appearance = ccFlat;
  ctrl.BorderStyle = ccNone;
  
  ctrl.HorizontalLines = true;
  ctrl.VerticalLines = true;
  ctrl.AllowSelect = true;
  ctrl.AllowEdit = true;
  ctrl.AllowCheck = true;
  ctrl.AlwaysEdit = false;
  
  ctrl.AllowRowResize = true;
  ctrl.AllowColumnResize = true;
  ctrl.AllowAutoRowResize = true;
  ctrl.AllowAutoColumnResize = true;
  
  ctrl.AllowTitleTips = false;
  ctrl.ListMode = false;
  ctrl.SingleRowSelection = false;
  ctrl.ShowSelectionAlways = false;
  ctrl.ShowFocusRect = false;

  ctrl.AlwaysAutoSizeRows = false;
  ctrl.AlwaysAutoSizeColumns = false;
  ctrl.AlwaysExpandRowsToFit = false;
  ctrl.AlwaysExpandColumnsToFit = false;

  ctrl.CopyMode = DG_cmcAutomatic;
  ctrl.CutMode = DG_cutmcAutomatic;
  ctrl.PasteMode = DG_pmcAutomatic;
  ctrl.DeleteMode = DG_dmcAutomatic;

  ctrl.OLEDragMode = ccOLEDragManual;
  ctrl.OLEDropMode = DG_ccOLEDropNone;
  
  ctrl.EditingControlSize = DG_ecscCellTextSize;
  ctrl.EditingControlResize = DG_ecrcCellSize;
  
  agLatticeEditor_PopulateTable (ctrl);

}

function  agLatticeEditor_PopulateTable (ctrl)
{
  // Bail out now if the control has not been created
  if (!ctrl) return;

  var obj = ag.latticeEditorObj;

  ctrl.DeleteAllRows();
  
  // Populates the lattice matrix with data from the current lattice feature
  if(obj == null) return;

  var rows = obj.YRows; var rows2 = Math.floor(rows / 2) - (rows+1) % 2;
  var cols = obj.XRows; var cols2 = Math.floor(cols / 2) - (cols+1) % 2;
  ctrl.RowsCount = rows+1;
  ctrl.ColumnsCount = cols+1;
  ctrl.FixedRowsCount = 1;
  ctrl.FixedColumnsCount = 1;
    
  ctrl.Cell(1,1).Text = "Zrow = "+obj.CurrentZ;
    
  var  indexRow, indexCol;
 
  // titles for the rows
  for(indexRow=2; indexRow <= rows+1; indexRow++)
	ctrl.Cell(indexRow,1).Text = "Row "+(rows-indexRow+1-rows2);

  // titles for the columns	
  for(indexCol=2; indexCol <= cols+1; indexCol++)
	ctrl.Cell(1,indexCol).Text = "Column "+(indexCol-2-cols2);
	 
  for(indexRow=2; indexRow <= rows+1; indexRow++)
	{
		for(indexCol=2; indexCol <= cols+1; indexCol++)
		{
			var cell = ctrl.Cell(indexRow,indexCol);
			cell.Text = ""+obj.CellUniverse(indexCol-2, rows-indexRow+1, obj.CurrentZ);
		}
	}	
	
	ctrl.AutoSize();

	ctrl.Refresh ();
	agLatticeEditor_UpdateSymmetry (ctrl);
}

function agLatticeEditor_CellUniverseChanged (table, cell) {
	var obj = ag.latticeEditorObj;
	if (obj == null) return;

	try {
		var newVal = wb.Locale.CurrentLocaleString2Double (cell.Text);
		if (isNaN (newVal)) {
			cell.Text = "Invalid";
			cell.TextColor = 0x000000FF;
		} else {
			cell.Text = newVal;
			cell.TextColor = 0xFF000000;
			obj.CellUniverse (cell.Column-2, obj.YRows-cell.Row+1, obj.CurrentZ) = newVal;
		}
	} catch (e) {
		cell.Text = "Invalid";
		cell.TextColor = 0x000000FF;
	}

	agLatticeEditor_PopulateTable (table);

	agTreeRefresh(true);
}

function agResetLatticeBodyVisibility()
{
	if ( ag.LatticeEditor_prevhiddenbody )
	{
		ag.LatticeEditor_prevhiddenbody.setVisibility (true);
		ag.LatticeEditor_prevhiddenbody = null;
	}	
	for (var b in ag.LatticeEditor_prevshowninside) {
		ag.LatticeEditor_prevshowninside[b].setVisibility (false);
	}
	ag.LatticeEditor_prevshowninside = null;
	ag.LatticeEditor_prevshowninside = new Array ();
}

function agLatticeEditor_LeftArrow (table, zrow) {
	var obj = ag.latticeEditorObj;
	if (obj == null) return;

	agResetLatticeBodyVisibility();
	
	if (obj.CurrentZ > 0) {
		obj.CurrentZ--;
		agLatticeEditor_PopulateTable (table);
		var body;
		for (var i = 0;i < obj.XRows;i++) {
			for (var j = 0;j < obj.YRows;j++) {
				for (var k = 0;k <= obj.CurrentZ;k++) {
					body = obj.BodyAt (i, j, k);
					if (body) body.setVisibility (true);
				}
				for (var k = obj.CurrentZ+1;k < obj.ZRows;k++) {
					body = obj.BodyAt (i, j, k);
					if (body) body.setVisibility (false);
				}
			}
		}
		ag.gb.Redraw (ag.c.RedrawAll);
		zrow.value = ""+(obj.CurrentZ+1);
	}
}

function agLatticeEditor_RightArrow (table, zrow) {
	var obj = ag.latticeEditorObj;
	if (obj == null) return;

	agResetLatticeBodyVisibility();
	
	if (obj.CurrentZ < obj.ZRows-1) {
		obj.CurrentZ++;
    var body;
		for (var i = 0;i < obj.XRows;i++) {
			for (var j = 0;j < obj.YRows;j++) {
				for (var k = 0;k <= obj.CurrentZ;k++) {
					body = obj.BodyAt (i, j, k);
					if (body) body.setVisibility (true);
				}
				for (var k = obj.CurrentZ+1;k < obj.ZRows;k++) {
					body = obj.BodyAt (i, j, k);
					if (body) body.setVisibility (false);
				}
			}
		}
		ag.gb.Redraw (ag.c.RedrawAll);
		agLatticeEditor_PopulateTable (table);
		zrow.value = ""+(obj.CurrentZ+1);
	}
}

function agLatticeEditor_ZChanged (table, zrow) {
	var obj = ag.latticeEditorObj;
	if (obj == null) return;

	try  {
		obj.CurrentZ = zrow.value-1;
	} catch (e) {}
	agLatticeEditor_PopulateTable (table);
	zrow.value = ""+(obj.CurrentZ+1);	
}

function agLatticeEditor_IsCellEnabled (i, j, symH, symV, symD1, symD2, XRows, YRows, X_2, Y_2) {
	if (symH && (j >= Y_2)) return false;
	if (symV && (i >= X_2)) return false;
	if (symD1 && (i >= YRows-j)) return false;
	if (symD2 && (j < i)) return false;
	return true;
}

function agLatticeEditor_UpdateSymmetry (table) {
	var obj = ag.latticeEditorObj;
	if (obj == null) return;

	var XRows = obj.XRows;
	var YRows = obj.YRows;
	var X_2 = Math.ceil (XRows/2);
	var Y_2 = Math.ceil (YRows/2);

	var symH = (obj.Symmetry & ag.c.LatticeSymmetryH) != 0;
	var symV = (obj.Symmetry & ag.c.LatticeSymmetryV) != 0;
	var symD1 = (obj.Symmetry & ag.c.LatticeSymmetryD1) != 0;
	var symD2 = (obj.Symmetry & ag.c.LatticeSymmetryD2) != 0;

	if (symH || symV || symD1 || symD2) {
		for (var i = 0;i < XRows;i++) {
			for (var j = 0;j < YRows;j++) {
				var cell = table.Cell(j+2,i+2);
				if (agLatticeEditor_IsCellEnabled (i, j, symH, symV, symD1, symD2, XRows, YRows, X_2, Y_2)) {
					cell.BackgroundColor = 0xFF000000;
				} else {
					cell.BackgroundColor = 0xd8e9ec;
				}
			}
		}
	} else {
		for (var i = 0;i < XRows;i++) {
			for (var j = 0;j < YRows;j++) {
				var cell = table.Cell(j+2,i+2);
				cell.BackgroundColor = 0xFF000000;
			}
		}
	}

	table.Refresh ();
}

function agLatticeEditor_SymChanged (table, enabled, sym_type) {
	var obj = ag.latticeEditorObj;
	if (obj == null) return;
	if ((sym_type < 0) || (sym_type > 0x0F)) return;

	if (enabled) obj.Symmetry = obj.Symmetry | sym_type;
	else obj.Symmetry = obj.Symmetry & (0x0F - sym_type);

	agLatticeEditor_UpdateSymmetry (table);
}

function agLatticeEditor_SelChanged (gridctrl, cell) {
	agResetLatticeBodyVisibility();
	
	var obj = ag.latticeEditorObj;
	try {
		var body = obj.BodyAt (cell.Column-2, obj.YRows-cell.Row+1, obj.CurrentZ);
		body.setVisibility(false);
		ag.LatticeEditor_prevhiddenbody = body;
		var nbi = obj.NbInside (cell.Column-2, obj.YRows-cell.Row+1, obj.CurrentZ);
		for (var i = 0;i < nbi;i++) {
			var inside = obj.InsideAt (cell.Column-2, obj.YRows-cell.Row+1, obj.CurrentZ, i);
			inside.setVisibility(true);
			ag.LatticeEditor_prevshowninside.push (inside);
		}
	} catch (e) {
	}

	ag.gb.Redraw (ag.c.RedrawSelections2dModel);
	ag.gb.Redraw (ag.c.RedrawAll);
}
