#region MigraDoc - Creating Documents on the Fly // // Authors: // Stefan Lange // Klaus Potzesny // David Stephensen // // Copyright (c) 2001-2017 empira Software GmbH, Cologne Area (Germany) // // http://www.pdfsharp.com // http://www.migradoc.com // http://sourceforge.net/projects/pdfsharp // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. #endregion using System; using MigraDoc.DocumentObjectModel.publics; using MigraDoc.DocumentObjectModel.Visitors; using MigraDoc.DocumentObjectModel.Shapes; using MigraDoc.DocumentObjectModel.Shapes.Charts; namespace MigraDoc.DocumentObjectModel.Tables { /// /// Represents a cell of a table. /// public class Cell : DocumentObject, IVisitable { /// /// Initializes a new instance of the Cell class. /// public Cell() { } /// /// Initializes a new instance of the Cell class with the specified parent. /// public Cell(DocumentObject parent) : base(parent) { } #region Methods /// /// Creates a deep copy of this object. /// public new Cell Clone() { return (Cell)DeepCopy(); } /// /// Implements the deep copy of the object. /// protected override object DeepCopy() { Cell cell = (Cell)base.DeepCopy(); // Remove all references to the original object hierarchy. cell.ResetCachedValues(); // TODO Call ResetCachedValues() for all classes where this is needed! if (cell._format != null) { cell._format = cell._format.Clone(); cell._format._parent = cell; } if (cell._borders != null) { cell._borders = cell._borders.Clone(); cell._borders._parent = cell; } if (cell._shading != null) { cell._shading = cell._shading.Clone(); cell._shading._parent = cell; } if (cell._elements != null) { cell._elements = cell._elements.Clone(); cell._elements._parent = cell; } return cell; } /// /// Resets the cached values. /// public override void ResetCachedValues() { base.ResetCachedValues(); _row = null; _clm = null; _table = null; } /// /// Adds a new paragraph to the cell. /// public Paragraph AddParagraph() { return Elements.AddParagraph(); } /// /// Adds a new paragraph with the specified text to the cell. /// public Paragraph AddParagraph(string paragraphText) { return Elements.AddParagraph(paragraphText); } /// /// Adds a new chart with the specified type to the cell. /// public Chart AddChart(ChartType type) { return Elements.AddChart(type); } /// /// Adds a new chart to the cell. /// public Chart AddChart() { return Elements.AddChart(); } /// /// Adds a new Image to the cell. /// public Image AddImage(string fileName) { return Elements.AddImage(fileName); } /// /// Adds a new textframe to the cell. /// public TextFrame AddTextFrame() { return Elements.AddTextFrame(); } /// /// Adds a new paragraph to the cell. /// public void Add(Paragraph paragraph) { Elements.Add(paragraph); } /// /// Adds a new chart to the cell. /// public void Add(Chart chart) { Elements.Add(chart); } /// /// Adds a new image to the cell. /// public void Add(Image image) { Elements.Add(image); } /// /// Adds a new text frame to the cell. /// public void Add(TextFrame textFrame) { Elements.Add(textFrame); } #endregion #region Properties /// /// Gets the table the cell belongs to. /// public Table Table { get { if (_table == null) { Cells cls = Parent as Cells; if (cls != null) _table = cls.Table; } return _table; } } Table _table; /// /// Gets the column the cell belongs to. /// public Column Column { get { if (_clm == null) { Cells cells = Parent as Cells; for (int index = 0; index < cells.Count; ++index) { if (cells[index] == this) _clm = Table.Columns[index]; } } return _clm; } } Column _clm; /// /// Gets the row the cell belongs to. /// public Row Row { get { if (_row == null) { Cells cells = Parent as Cells; _row = cells.Row; } return _row; } } Row _row; /// /// Sets or gets the style name. /// public string Style { get { return _style.Value; } set { _style.Value = value; } } [DV] public NString _style = NString.NullValue; /// /// Gets the ParagraphFormat object of the paragraph. /// public ParagraphFormat Format { get { return _format ?? (_format = new ParagraphFormat(this)); } set { SetParent(value); _format = value; } } [DV] public ParagraphFormat _format; /// /// Gets or sets the vertical alignment of the cell. /// public VerticalAlignment VerticalAlignment { get { return (VerticalAlignment)_verticalAlignment.Value; } set { _verticalAlignment.Value = (int)value; } } [DV(Type = typeof(VerticalAlignment))] public NEnum _verticalAlignment = NEnum.NullValue(typeof(VerticalAlignment)); /// /// Gets the Borders object. /// public Borders Borders { get { if (_borders == null) { if (Document == null) // BUG CMYK GetType(); _borders = new Borders(this); } return _borders; } set { SetParent(value); _borders = value; } } [DV] public Borders _borders; /// /// Gets the shading object. /// public Shading Shading { get { return _shading ?? (_shading = new Shading(this)); } set { SetParent(value); _shading = value; } } [DV] public Shading _shading; /// /// Specifies if the Cell should be rendered as a rounded corner. /// public RoundedCorner RoundedCorner { get { return _roundedCorner; } set { _roundedCorner = value; } } [DV] public RoundedCorner _roundedCorner; /// /// Gets or sets the number of cells to be merged right. /// public int MergeRight { get { return _mergeRight.Value; } set { _mergeRight.Value = value; } } [DV] public NInt _mergeRight = NInt.NullValue; /// /// Gets or sets the number of cells to be merged down. /// public int MergeDown { get { return _mergeDown.Value; } set { _mergeDown.Value = value; } } [DV] public NInt _mergeDown = NInt.NullValue; /// /// Gets the collection of document objects that defines the cell. /// public DocumentElements Elements { get { return _elements ?? (_elements = new DocumentElements(this)); } set { SetParent(value); _elements = value; } } [DV(ItemType = typeof(DocumentObject))] public DocumentElements _elements; /// /// Gets or sets a comment associated with this object. /// public string Comment { get { return _comment.Value; } set { _comment.Value = value; } } [DV] public NString _comment = NString.NullValue; #endregion #region public /// /// Converts Cell into DDL. /// public override void Serialize(Serializer serializer) { serializer.WriteComment(_comment.Value); serializer.WriteLine("\\cell"); int pos = serializer.BeginAttributes(); if (_style.Value != String.Empty) serializer.WriteSimpleAttribute("Style", Style); if (!IsNull("Format")) _format.Serialize(serializer, "Format", null); if (!_mergeDown.IsNull) serializer.WriteSimpleAttribute("MergeDown", MergeDown); if (!_mergeRight.IsNull) serializer.WriteSimpleAttribute("MergeRight", MergeRight); if (!_verticalAlignment.IsNull) serializer.WriteSimpleAttribute("VerticalAlignment", VerticalAlignment); if (!IsNull("Borders")) _borders.Serialize(serializer, null); if (!IsNull("Shading")) _shading.Serialize(serializer); if (_roundedCorner != RoundedCorner.None) serializer.WriteSimpleAttribute("RoundedCorner", RoundedCorner); serializer.EndAttributes(pos); pos = serializer.BeginContent(); if (!IsNull("Elements")) _elements.Serialize(serializer); serializer.EndContent(pos); } /// /// Allows the visitor object to visit the document object and its child objects. /// void IVisitable.AcceptVisitor(DocumentObjectVisitor visitor, bool visitChildren) { visitor.VisitCell(this); if (visitChildren && _elements != null) ((IVisitable)_elements).AcceptVisitor(visitor, visitChildren); } /// /// Returns the meta object of this instance. /// public override Meta Meta { get { return _meta ?? (_meta = new Meta(typeof(Cell))); } } static Meta _meta; #endregion } }