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 | *******************************************************************************/
|
---|
32 | using System;
|
---|
33 | using System.Collections.Generic;
|
---|
34 | using System.Text;
|
---|
35 | using System.Xml;
|
---|
36 | using System.Collections;
|
---|
37 | using System.IO.Packaging;
|
---|
38 |
|
---|
39 | namespace OfficeOpenXml.Drawing.Vml
|
---|
40 | {
|
---|
41 | internal class ExcelVmlDrawingCommentCollection : ExcelVmlDrawingBaseCollection, IEnumerable
|
---|
42 | {
|
---|
43 | internal RangeCollection _drawings;
|
---|
44 | internal ExcelVmlDrawingCommentCollection(ExcelPackage pck, ExcelWorksheet ws, Uri uri) :
|
---|
45 | base(pck, ws,uri)
|
---|
46 | {
|
---|
47 | if (uri == null)
|
---|
48 | {
|
---|
49 | VmlDrawingXml.LoadXml(CreateVmlDrawings());
|
---|
50 | _drawings = new RangeCollection(new List<IRangeID>());
|
---|
51 | }
|
---|
52 | else
|
---|
53 | {
|
---|
54 | AddDrawingsFromXml(ws);
|
---|
55 | }
|
---|
56 | }
|
---|
57 | protected void AddDrawingsFromXml(ExcelWorksheet ws)
|
---|
58 | {
|
---|
59 | var nodes = VmlDrawingXml.SelectNodes("//v:shape", NameSpaceManager);
|
---|
60 | var list = new List<IRangeID>();
|
---|
61 | foreach (XmlNode node in nodes)
|
---|
62 | {
|
---|
63 | var rowNode = node.SelectSingleNode("x:ClientData/x:Row", NameSpaceManager);
|
---|
64 | var colNode = node.SelectSingleNode("x:ClientData/x:Column", NameSpaceManager);
|
---|
65 | if (rowNode != null && colNode != null)
|
---|
66 | {
|
---|
67 | var row = int.Parse(rowNode.InnerText) + 1;
|
---|
68 | var col = int.Parse(colNode.InnerText) + 1;
|
---|
69 | list.Add(new ExcelVmlDrawingComment(node, ws.Cells[row, col], NameSpaceManager));
|
---|
70 | }
|
---|
71 | else
|
---|
72 | {
|
---|
73 | list.Add(new ExcelVmlDrawingComment(node, ws.Cells[1, 1], NameSpaceManager));
|
---|
74 | }
|
---|
75 | }
|
---|
76 | 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.
|
---|
77 | _drawings = new RangeCollection(list);
|
---|
78 | }
|
---|
79 | private string CreateVmlDrawings()
|
---|
80 | {
|
---|
81 | string vml=string.Format("<xml xmlns:v=\"{0}\" xmlns:o=\"{1}\" xmlns:x=\"{2}\">",
|
---|
82 | ExcelPackage.schemaMicrosoftVml,
|
---|
83 | ExcelPackage.schemaMicrosoftOffice,
|
---|
84 | ExcelPackage.schemaMicrosoftExcel);
|
---|
85 |
|
---|
86 | vml+="<o:shapelayout v:ext=\"edit\">";
|
---|
87 | vml+="<o:idmap v:ext=\"edit\" data=\"1\"/>";
|
---|
88 | vml+="</o:shapelayout>";
|
---|
89 |
|
---|
90 | vml+="<v:shapetype id=\"_x0000_t202\" coordsize=\"21600,21600\" o:spt=\"202\" path=\"m,l,21600r21600,l21600,xe\">";
|
---|
91 | vml+="<v:stroke joinstyle=\"miter\" />";
|
---|
92 | vml+="<v:path gradientshapeok=\"t\" o:connecttype=\"rect\" />";
|
---|
93 | vml+="</v:shapetype>";
|
---|
94 | vml+= "</xml>";
|
---|
95 |
|
---|
96 | return vml;
|
---|
97 | }
|
---|
98 | internal ExcelVmlDrawingComment Add(ExcelRangeBase cell)
|
---|
99 | {
|
---|
100 | XmlNode node = AddDrawing(cell);
|
---|
101 | var draw = new ExcelVmlDrawingComment(node, cell, NameSpaceManager);
|
---|
102 | _drawings.Add(draw);
|
---|
103 | return draw;
|
---|
104 | }
|
---|
105 | private XmlNode AddDrawing(ExcelRangeBase cell)
|
---|
106 | {
|
---|
107 | int row = cell.Start.Row, col = cell.Start.Column;
|
---|
108 | var node = VmlDrawingXml.CreateElement("v", "shape", ExcelPackage.schemaMicrosoftVml);
|
---|
109 |
|
---|
110 | var id = ExcelCellBase.GetCellID(cell.Worksheet.SheetID, cell._fromRow, cell._fromCol);
|
---|
111 | var ix = _drawings.IndexOf(id);
|
---|
112 | if (ix < 0 && (~ix < _drawings.Count))
|
---|
113 | {
|
---|
114 | ix = ~ix;
|
---|
115 | var prevDraw = _drawings[ix] as ExcelVmlDrawingBase;
|
---|
116 | prevDraw.TopNode.ParentNode.InsertBefore(node, prevDraw.TopNode);
|
---|
117 | }
|
---|
118 | else
|
---|
119 | {
|
---|
120 | VmlDrawingXml.DocumentElement.AppendChild(node);
|
---|
121 | }
|
---|
122 |
|
---|
123 | node.SetAttribute("id", GetNewId());
|
---|
124 | node.SetAttribute("type", "#_x0000_t202");
|
---|
125 | node.SetAttribute("style", "position:absolute;z-index:1; visibility:hidden");
|
---|
126 | //node.SetAttribute("style", "position:absolute; margin-left:59.25pt;margin-top:1.5pt;width:108pt;height:59.25pt;z-index:1; visibility:hidden");
|
---|
127 | node.SetAttribute("fillcolor", "#ffffe1");
|
---|
128 | node.SetAttribute("insetmode",ExcelPackage.schemaMicrosoftOffice,"auto");
|
---|
129 |
|
---|
130 | string vml = "<v:fill color2=\"#ffffe1\" />";
|
---|
131 | vml += "<v:shadow on=\"t\" color=\"black\" obscured=\"t\" />";
|
---|
132 | vml += "<v:path o:connecttype=\"none\" />";
|
---|
133 | vml += "<v:textbox style=\"mso-direction-alt:auto\">";
|
---|
134 | vml += "<div style=\"text-align:left\" />";
|
---|
135 | vml += "</v:textbox>";
|
---|
136 | vml += "<x:ClientData ObjectType=\"Note\">";
|
---|
137 | vml += "<x:MoveWithCells />";
|
---|
138 | vml += "<x:SizeWithCells />";
|
---|
139 | vml += string.Format("<x:Anchor>{0}, 15, {1}, 2, {2}, 31, {3}, 1</x:Anchor>", col, row - 1, col + 2, row + 3);
|
---|
140 | vml += "<x:AutoFill>False</x:AutoFill>";
|
---|
141 | vml += string.Format("<x:Row>{0}</x:Row>", row - 1); ;
|
---|
142 | vml += string.Format("<x:Column>{0}</x:Column>", col - 1);
|
---|
143 | vml += "</x:ClientData>";
|
---|
144 |
|
---|
145 | node.InnerXml = vml;
|
---|
146 | return node;
|
---|
147 | }
|
---|
148 | int _nextID = 0;
|
---|
149 | /// <summary>
|
---|
150 | /// returns the next drawing id.
|
---|
151 | /// </summary>
|
---|
152 | /// <returns></returns>
|
---|
153 | internal string GetNewId()
|
---|
154 | {
|
---|
155 | if (_nextID == 0)
|
---|
156 | {
|
---|
157 | foreach (ExcelVmlDrawingComment draw in this)
|
---|
158 | {
|
---|
159 | if (draw.Id.Length > 3 && draw.Id.StartsWith("vml"))
|
---|
160 | {
|
---|
161 | int id;
|
---|
162 | if (int.TryParse(draw.Id.Substring(3, draw.Id.Length - 3), out id))
|
---|
163 | {
|
---|
164 | if (id > _nextID)
|
---|
165 | {
|
---|
166 | _nextID = id;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|
172 | _nextID++;
|
---|
173 | return "vml" + _nextID.ToString();
|
---|
174 | }
|
---|
175 | internal ExcelVmlDrawingBase this[ulong rangeID]
|
---|
176 | {
|
---|
177 | get
|
---|
178 | {
|
---|
179 | return _drawings[rangeID] as ExcelVmlDrawingComment;
|
---|
180 | }
|
---|
181 | }
|
---|
182 | internal bool ContainsKey(ulong rangeID)
|
---|
183 | {
|
---|
184 | return _drawings.ContainsKey(rangeID);
|
---|
185 | }
|
---|
186 | internal int Count
|
---|
187 | {
|
---|
188 | get
|
---|
189 | {
|
---|
190 | return _drawings.Count;
|
---|
191 | }
|
---|
192 | }
|
---|
193 | #region IEnumerable Members
|
---|
194 |
|
---|
195 | #endregion
|
---|
196 |
|
---|
197 | public IEnumerator GetEnumerator()
|
---|
198 | {
|
---|
199 | return _drawings;
|
---|
200 | }
|
---|
201 |
|
---|
202 | IEnumerator IEnumerable.GetEnumerator()
|
---|
203 | {
|
---|
204 | return _drawings;
|
---|
205 | }
|
---|
206 | }
|
---|
207 | }
|
---|