/* ====================================================================
Licensed to the Apache Software Foundation (ASF) Under one or more
contributor license agreements. See the NOTICE file distributed with
this work for Additional information regarding copyright ownership.
The ASF licenses this file to You Under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed Under the License is distributed on an "AS Is" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations Under the License.
==================================================================== */
namespace HH.WMS.Utils.NPOI.HSSF.UserModel
{
using System;
///
/// A client anchor Is attached to an excel worksheet. It anchors against a
/// top-left and buttom-right cell.
/// @author Glen Stampoultzis (glens at apache.org)
///
public class HSSFClientAnchor : HSSFAnchor, HH.WMS.Utils.NPOI.SS.UserModel.IClientAnchor
{
int col1;
int row1;
int col2;
int row2;
int anchorType;
///
/// Creates a new client anchor and defaults all the anchor positions to 0.
///
public HSSFClientAnchor()
{
}
///
/// Creates a new client anchor and Sets the top-left and bottom-right
/// coordinates of the anchor.
///
/// the x coordinate within the first cell.
/// the y coordinate within the first cell.
/// the x coordinate within the second cell.
/// the y coordinate within the second cell.
/// the column (0 based) of the first cell.
/// the row (0 based) of the first cell.
/// the column (0 based) of the second cell.
/// the row (0 based) of the second cell.
public HSSFClientAnchor(int dx1, int dy1, int dx2, int dy2, int col1, int row1, int col2, int row2)
: base(dx1, dy1, dx2, dy2)
{
CheckRange(dx1, 0, 1023, "dx1");
CheckRange(dx2, 0, 1023, "dx2");
CheckRange(dy1, 0, 255, "dy1");
CheckRange(dy2, 0, 255, "dy2");
CheckRange(col1, 0, 255, "col1");
CheckRange(col2, 0, 255, "col2");
CheckRange(row1, 0, 255 * 256, "row1");
CheckRange(row2, 0, 255 * 256, "row2");
this.col1 = col1;
this.row1 = row1;
this.col2 = col2;
this.row2 = row2;
}
///
/// Calculates the height of a client anchor in points.
///
/// the sheet the anchor will be attached to
/// the shape height.
public float GetAnchorHeightInPoints(HH.WMS.Utils.NPOI.SS.UserModel.ISheet sheet)
{
int y1 = Dy1;
int y2 = Dy2;
int row1 = Math.Min(Row1, Row2);
int row2 = Math.Max(Row1, Row2);
float points = 0;
if (row1 == row2)
{
points = ((y2 - y1) / 256.0f) * GetRowHeightInPoints(sheet, row2);
}
else
{
points += ((256.0f - y1) / 256.0f) * GetRowHeightInPoints(sheet, row1);
for (int i = row1 + 1; i < row2; i++)
{
points += GetRowHeightInPoints(sheet, i);
}
points += (y2 / 256.0f) * GetRowHeightInPoints(sheet, row2);
}
return points;
}
///
/// Gets the row height in points.
///
/// The sheet.
/// The row num.
///
private float GetRowHeightInPoints(HH.WMS.Utils.NPOI.SS.UserModel.ISheet sheet, int rowNum)
{
HH.WMS.Utils.NPOI.SS.UserModel.IRow row = sheet.GetRow(rowNum);
if (row == null)
return sheet.DefaultRowHeightInPoints;
else
return row.HeightInPoints;
}
///
/// Gets or sets the col1.
///
/// The col1.
public int Col1
{
get { return col1; }
set
{
CheckRange(value, 0, 255, "col1");
this.col1 = value;
}
}
///
/// Gets or sets the col2.
///
/// The col2.
public int Col2
{
get { return col2; }
set
{
CheckRange(value, 0, 255, "col2");
this.col2 = value;
}
}
///
/// Gets or sets the row1.
///
/// The row1.
public int Row1
{
get { return row1; }
set
{
CheckRange(value, 0, 256 * 256, "row1");
this.row1 = value;
}
}
///
/// Gets or sets the row2.
///
/// The row2.
public int Row2
{
get { return row2; }
set
{
CheckRange(value, 0, 256 * 256, "row2");
this.row2 = value;
}
}
///
/// Sets the top-left and bottom-right
/// coordinates of the anchor
///
/// the column (0 based) of the first cell.
/// the row (0 based) of the first cell.
/// the x coordinate within the first cell.
/// the y coordinate within the first cell.
/// the column (0 based) of the second cell.
/// the row (0 based) of the second cell.
/// the x coordinate within the second cell.
/// the y coordinate within the second cell.
public void SetAnchor(short col1, int row1, int x1, int y1, short col2, int row2, int x2, int y2)
{
CheckRange(x1, 0, 1023, "dx1");
CheckRange(x2, 0, 1023, "dx2");
CheckRange(y1, 0, 255, "dy1");
CheckRange(y2, 0, 255, "dy2");
CheckRange(col1, 0, 255, "col1");
CheckRange(col2, 0, 255, "col2");
CheckRange(row1, 0, 255 * 256, "row1");
CheckRange(row2, 0, 255 * 256, "row2");
this.col1 = col1;
this.row1 = row1;
this.Dx1 = x1;
this.Dy1 = y1;
this.col2 = col2;
this.row2 = row2;
this.Dx2 = x2;
this.Dy2 = y2;
}
///
/// Gets a value indicating whether this instance is horizontally flipped.
///
///
/// true if the anchor goes from right to left; otherwise, false.
///
public override bool IsHorizontallyFlipped
{
get
{
if (col1 == col2)
return Dx1 > Dx2;
else
return col1 > col2;
}
}
///
/// Gets a value indicating whether this instance is vertically flipped.
///
///
/// true if the anchor goes from bottom to top.; otherwise, false.
///
public override bool IsVerticallyFlipped
{
get
{
if (row1 == row2)
return Dy1 > Dy2;
else
return row1 > row2;
}
}
///
/// Gets the anchor type
/// 0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells.
///
/// The type of the anchor.
public int AnchorType
{
get{return anchorType;}
set { this.anchorType = value; }
}
///
/// Checks the range.
///
/// The value.
/// The min range.
/// The max range.
/// Name of the variable.
private void CheckRange(int value, int minRange, int maxRange, String varName)
{
if (value < minRange || value > maxRange)
throw new ArgumentException(varName + " must be between " + minRange + " and " + maxRange);
}
}
}