Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/Drawing/Vml/ExcelVmlDrawingCommentCollection.cs @ 12074

Last change on this file since 12074 was 12074, checked in by sraggl, 9 years ago

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 8.3 KB
Line 
1/*******************************************************************************
2 * You may amend and distribute as you like, but don't remove this header!
3 *
4 * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
5 * See http://www.codeplex.com/EPPlus for details.
6 *
7 * Copyright (C) 2011  Jan Källman
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
17 * See the GNU Lesser General Public License for more details.
18 *
19 * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
20 * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
21 *
22 * All code and executables are provided "as is" with no warranty either express or implied.
23 * The author accepts no liability for any damage or loss of business that this product may cause.
24 *
25 * Code change notes:
26 *
27 * Author             Change            Date
28 * ******************************************************************************
29 * Jan Källman    Initial Release           2010-06-01
30 * Jan Källman    License changed GPL-->LGPL 2011-12-16
31 *******************************************************************************/
32using System;
33using System.Collections.Generic;
34using System.Text;
35using System.Xml;
36using System.Collections;
37
38namespace OfficeOpenXml.Drawing.Vml
39{
40    internal class ExcelVmlDrawingCommentCollection : ExcelVmlDrawingBaseCollection, IEnumerable
41    {
42        internal RangeCollection _drawings;
43        internal ExcelVmlDrawingCommentCollection(ExcelPackage pck, ExcelWorksheet ws, Uri uri) :
44            base(pck, ws,uri)
45        {
46            if (uri == null)
47            {
48                VmlDrawingXml.LoadXml(CreateVmlDrawings());
49                _drawings = new RangeCollection(new List<IRangeID>());
50            }
51            else
52            {
53                AddDrawingsFromXml(ws);
54            }
55        }
56        protected void AddDrawingsFromXml(ExcelWorksheet ws)
57        {
58            var nodes = VmlDrawingXml.SelectNodes("//v:shape", NameSpaceManager);
59            var list = new List<IRangeID>();
60            foreach (XmlNode node in nodes)
61            {
62                var rowNode = node.SelectSingleNode("x:ClientData/x:Row", NameSpaceManager);
63                var colNode = node.SelectSingleNode("x:ClientData/x:Column", NameSpaceManager);
64                if (rowNode != null && colNode != null)
65                {
66                    var row = int.Parse(rowNode.InnerText) + 1;
67                    var col = int.Parse(colNode.InnerText) + 1;
68                    list.Add(new ExcelVmlDrawingComment(node, ws.Cells[row, col], NameSpaceManager));
69                }
70                else
71                {
72                    list.Add(new ExcelVmlDrawingComment(node, ws.Cells[1, 1], NameSpaceManager));
73                }
74            }
75            list.Sort(new Comparison<IRangeID>((r1, r2) => (r1.RangeID < r2.RangeID ? -1 : r1.RangeID > r2.RangeID ? 1 : 0)));  //Vml drawings are not sorted. Sort to avoid missmatches.
76            _drawings = new RangeCollection(list);
77        }
78        private string CreateVmlDrawings()
79        {
80            string vml=string.Format("<xml xmlns:v=\"{0}\" xmlns:o=\"{1}\" xmlns:x=\"{2}\">",
81                ExcelPackage.schemaMicrosoftVml,
82                ExcelPackage.schemaMicrosoftOffice,
83                ExcelPackage.schemaMicrosoftExcel);
84           
85             vml+="<o:shapelayout v:ext=\"edit\">";
86             vml+="<o:idmap v:ext=\"edit\" data=\"1\"/>";
87             vml+="</o:shapelayout>";
88
89             vml+="<v:shapetype id=\"_x0000_t202\" coordsize=\"21600,21600\" o:spt=\"202\" path=\"m,l,21600r21600,l21600,xe\">";
90             vml+="<v:stroke joinstyle=\"miter\" />";
91             vml+="<v:path gradientshapeok=\"t\" o:connecttype=\"rect\" />";
92             vml+="</v:shapetype>";
93             vml+= "</xml>";
94
95            return vml;
96        }
97        internal ExcelVmlDrawingComment Add(ExcelRangeBase cell)
98        {
99            XmlNode node = AddDrawing(cell);
100            var draw = new ExcelVmlDrawingComment(node, cell, NameSpaceManager);           
101            _drawings.Add(draw);
102            return draw;
103        }
104        private XmlNode AddDrawing(ExcelRangeBase cell)
105        {
106            int row = cell.Start.Row, col = cell.Start.Column;
107            var node = VmlDrawingXml.CreateElement("v", "shape", ExcelPackage.schemaMicrosoftVml);
108
109            var id = ExcelCellBase.GetCellID(cell.Worksheet.SheetID, cell._fromRow, cell._fromCol);
110            var ix = _drawings.IndexOf(id);
111            if (ix < 0 && (~ix < _drawings.Count))
112            {
113                ix = ~ix;
114                var prevDraw = _drawings[ix] as ExcelVmlDrawingBase;
115                prevDraw.TopNode.ParentNode.InsertBefore(node, prevDraw.TopNode);
116            }
117            else
118            {
119                VmlDrawingXml.DocumentElement.AppendChild(node);
120            }
121
122            node.SetAttribute("id", GetNewId());
123            node.SetAttribute("type", "#_x0000_t202");
124            node.SetAttribute("style", "position:absolute;z-index:1; visibility:hidden");
125            //node.SetAttribute("style", "position:absolute; margin-left:59.25pt;margin-top:1.5pt;width:108pt;height:59.25pt;z-index:1; visibility:hidden");
126            node.SetAttribute("fillcolor", "#ffffe1");
127            node.SetAttribute("insetmode",ExcelPackage.schemaMicrosoftOffice,"auto");
128
129            string vml = "<v:fill color2=\"#ffffe1\" />";
130            vml += "<v:shadow on=\"t\" color=\"black\" obscured=\"t\" />";
131            vml += "<v:path o:connecttype=\"none\" />";
132            vml += "<v:textbox style=\"mso-direction-alt:auto\">";
133            vml += "<div style=\"text-align:left\" />";
134            vml += "</v:textbox>";
135            vml += "<x:ClientData ObjectType=\"Note\">";
136            vml += "<x:MoveWithCells />";
137            vml += "<x:SizeWithCells />";
138            vml += string.Format("<x:Anchor>{0}, 15, {1}, 2, {2}, 31, {3}, 1</x:Anchor>", col, row - 1, col + 2, row + 3);
139            vml += "<x:AutoFill>False</x:AutoFill>";
140            vml += string.Format("<x:Row>{0}</x:Row>", row - 1); ;
141            vml += string.Format("<x:Column>{0}</x:Column>", col - 1);
142            vml += "</x:ClientData>";
143
144            node.InnerXml = vml;
145            return node;
146        }
147        int _nextID = 0;
148        /// <summary>
149        /// returns the next drawing id.
150        /// </summary>
151        /// <returns></returns>
152        internal string GetNewId()
153        {
154            if (_nextID == 0)
155            {
156                foreach (ExcelVmlDrawingComment draw in this)
157                {
158                    if (draw.Id.Length > 3 && draw.Id.StartsWith("vml"))
159                    {
160                        int id;
161                        if (int.TryParse(draw.Id.Substring(3, draw.Id.Length - 3), out id))
162                        {
163                            if (id > _nextID)
164                            {
165                                _nextID = id;
166                            }
167                        }
168                    }
169                }
170            }
171            _nextID++;
172            return "vml" + _nextID.ToString();
173        }
174        internal ExcelVmlDrawingBase this[ulong rangeID]
175        {
176            get
177            {
178                return _drawings[rangeID] as ExcelVmlDrawingComment;
179            }
180        }
181        internal bool ContainsKey(ulong rangeID)
182        {
183            return _drawings.ContainsKey(rangeID);
184        }
185        internal int Count
186        {
187            get
188            {
189                return _drawings.Count;
190            }
191        }
192        #region IEnumerable Members
193
194        #endregion
195
196        public IEnumerator GetEnumerator()
197        {
198            return _drawings;
199        }
200
201        IEnumerator IEnumerable.GetEnumerator()
202        {
203            return _drawings;
204        }
205    }
206}
Note: See TracBrowser for help on using the repository browser.