Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2213_irace/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/Drawing/ExcelShape.cs @ 18242

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

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 16.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           2009-10-01
30 * Jan Källman    License changed GPL-->LGPL 2011-12-16
31 *******************************************************************************/
32using System;
33using System.Collections.Generic;
34using System.Globalization;
35using System.Text;
36using System.Xml;
37using OfficeOpenXml.Style.XmlAccess;
38using OfficeOpenXml.Drawing;
39using OfficeOpenXml.Style;
40/// <summary>
41/// Shape style
42/// </summary>
43public enum eShapeStyle
44{
45    AccentBorderCallout1,
46    AccentBorderCallout2,
47    AccentBorderCallout3,
48    AccentCallout1,
49    AccentCallout2,
50    AccentCallout3,
51    ActionButtonBackPrevious,
52    ActionButtonBeginning,
53    ActionButtonBlank,
54    ActionButtonDocument,
55    ActionButtonEnd,
56    ActionButtonForwardNext,
57    ActionButtonHelp,
58    ActionButtonHome,
59    ActionButtonInformation,
60    ActionButtonMovie,
61    ActionButtonReturn,
62    ActionButtonSound,
63    Arc,
64    BentArrow,
65    BentConnector2,
66    BentConnector3,
67    BentConnector4,
68    BentConnector5,
69    BentUpArrow,
70    Bevel,
71    BlockArc,
72    BorderCallout1,
73    BorderCallout2,
74    BorderCallout3,
75    BracePair,
76    BracketPair,
77    Callout1,
78    Callout2,
79    Callout3,
80    Can,
81    ChartPlus,
82    ChartStar,
83    ChartX,
84    Chevron,
85    Chord,
86    CircularArrow,
87    Cloud,
88    CloudCallout,
89    Corner,
90    CornerTabs,
91    Cube,
92    CurvedConnector2,
93    CurvedConnector3,
94    CurvedConnector4,
95    CurvedConnector5,
96    CurvedDownArrow,
97    CurvedLeftArrow,
98    CurvedRightArrow,
99    CurvedUpArrow,
100    Decagon,
101    DiagStripe,
102    Diamond,
103    Dodecagon,
104    Donut,
105    DoubleWave,
106    DownArrow,
107    DownArrowCallout,
108    Ellipse,
109    EllipseRibbon,
110    EllipseRibbon2,
111    FlowChartAlternateProcess,
112    FlowChartCollate,
113    FlowChartConnector,
114    FlowChartDecision,
115    FlowChartDelay,
116    FlowChartDisplay,
117    FlowChartDocument,
118    FlowChartExtract,
119    FlowChartInputOutput,
120    FlowChartInternalStorage,
121    FlowChartMagneticDisk,
122    FlowChartMagneticDrum,
123    FlowChartMagneticTape,
124    FlowChartManualInput,
125    FlowChartManualOperation,
126    FlowChartMerge,
127    FlowChartMultidocument,
128    FlowChartOfflineStorage,
129    FlowChartOffpageConnector,
130    FlowChartOnlineStorage,
131    FlowChartOr,
132    FlowChartPredefinedProcess,
133    FlowChartPreparation,
134    FlowChartProcess,
135    FlowChartPunchedCard,
136    FlowChartPunchedTape,
137    FlowChartSort,
138    FlowChartSummingJunction,
139    FlowChartTerminator,
140    FoldedCorner,
141    Frame,
142    Funnel,
143    Gear6,
144    Gear9,
145    HalfFrame,
146    Heart,
147    Heptagon,
148    Hexagon,
149    HomePlate,
150    HorizontalScroll,
151    IrregularSeal1,
152    IrregularSeal2,
153    LeftArrow,
154    LeftArrowCallout,
155    LeftBrace,
156    LeftBracket,
157    LeftCircularArrow,
158    LeftRightArrow,
159    LeftRightArrowCallout,
160    LeftRightCircularArrow,
161    LeftRightRibbon,
162    LeftRightUpArrow,
163    LeftUpArrow,
164    LightningBolt,
165    Line,
166    LineInv,
167    MathDivide,
168    MathEqual,
169    MathMinus,
170    MathMultiply,
171    MathNotEqual,
172    MathPlus,
173    Moon,
174    NonIsoscelesTrapezoid,
175    NoSmoking,
176    NotchedRightArrow,
177    Octagon,
178    Parallelogram,
179    Pentagon,
180    Pie,
181    PieWedge,
182    Plaque,
183    PlaqueTabs,
184    Plus,
185    QuadArrow,
186    QuadArrowCallout,
187    Rect,
188    Ribbon,
189    Ribbon2,
190    RightArrow,
191    RightArrowCallout,
192    RightBrace,
193    RightBracket,
194    Round1Rect,
195    Round2DiagRect,
196    Round2SameRect,
197    RoundRect,
198    RtTriangle,
199    SmileyFace,
200    Snip1Rect,
201    Snip2DiagRect,
202    Snip2SameRect,
203    SnipRoundRect,
204    SquareTabs,
205    Star10,
206    Star12,
207    Star16,
208    Star24,
209    Star32,
210    Star4,
211    Star5,
212    Star6,
213    Star7,
214    Star8,
215    StraightConnector1,
216    StripedRightArrow,
217    Sun,
218    SwooshArrow,
219    Teardrop,
220    Trapezoid,
221    Triangle,
222    UpArrow,
223    UpArrowCallout,
224    UpDownArrow,
225    UpDownArrowCallout,
226    UturnArrow,
227    Wave,
228    WedgeEllipseCallout,
229    WedgeRectCallout,
230    WedgeRoundRectCallout,
231    VerticalScroll
232}
233/// <summary>
234/// Text alignment
235/// </summary>
236public enum eTextAlignment
237{
238    Left,
239    Center,
240    Right,
241    Distributed,
242    Justified,
243    JustifiedLow,
244    ThaiDistributed
245}
246/// <summary>
247/// Fillstyle.
248/// </summary>
249public enum eFillStyle
250{
251    NoFill,
252    SolidFill,
253    GradientFill,
254    PatternFill,
255    BlipFill,
256    GroupFill
257}
258namespace OfficeOpenXml.Drawing
259{
260    /// <summary>
261    /// An Excel shape.
262    /// </summary>
263    public sealed class ExcelShape : ExcelDrawing
264    {
265        internal ExcelShape(ExcelDrawings drawings, XmlNode node) :
266            base(drawings, node, "xdr:sp/xdr:nvSpPr/xdr:cNvPr/@name")
267        {
268            init();
269        }
270        internal ExcelShape(ExcelDrawings drawings, XmlNode node, eShapeStyle style) :
271            base(drawings, node, "xdr:sp/xdr:nvSpPr/xdr:cNvPr/@name")
272        {
273            init();
274            XmlElement shapeNode = node.OwnerDocument.CreateElement("xdr", "sp", ExcelPackage.schemaSheetDrawings);
275            shapeNode.SetAttribute("macro", "");
276            shapeNode.SetAttribute("textlink", "");
277            node.AppendChild(shapeNode);
278
279            shapeNode.InnerXml = ShapeStartXml();
280            node.AppendChild(shapeNode.OwnerDocument.CreateElement("xdr", "clientData", ExcelPackage.schemaSheetDrawings));
281        }
282        private void init()
283        {
284            SchemaNodeOrder = new string[] { "prstGeom", "ln", "pPr", "defRPr", "solidFill", "uFill", "latin", "cs", "r", "rPr", "t" };
285        }
286        #region "public methods"
287        const string ShapeStylePath = "xdr:sp/xdr:spPr/a:prstGeom/@prst";
288        /// <summary>
289        /// Shape style
290        /// </summary>
291        public eShapeStyle Style
292        {
293            get
294            {
295                string v = GetXmlNodeString(ShapeStylePath);
296                try
297                {
298                    return (eShapeStyle)Enum.Parse(typeof(eShapeStyle), v, true);
299                }
300                catch
301                {
302                    throw (new Exception(string.Format("Invalid shapetype {0}", v)));
303                }
304            }
305            set
306            {
307                string v = value.ToString();
308                v = v.Substring(0, 1).ToLower(CultureInfo.InvariantCulture) + v.Substring(1, v.Length - 1);
309                SetXmlNodeString(ShapeStylePath, v);
310            }
311        }
312        ExcelDrawingFill _fill = null;
313        /// <summary>
314        /// Fill
315        /// </summary>
316        public ExcelDrawingFill Fill
317        {
318            get
319            {
320                if (_fill == null)
321                {
322                    _fill = new ExcelDrawingFill(NameSpaceManager, TopNode, "xdr:sp/xdr:spPr");
323                }
324                return _fill;
325            }
326        }
327        ExcelDrawingBorder _border = null;
328        /// <summary>
329        /// Border
330        /// </summary>
331        public ExcelDrawingBorder Border       
332        {
333            get
334            {
335                if (_border == null)
336                {
337                    _border = new ExcelDrawingBorder(NameSpaceManager, TopNode, "xdr:sp/xdr:spPr/a:ln");
338                }
339                return _border;
340            }
341        }
342        string[] paragraphNodeOrder = new string[] { "pPr", "defRPr", "solidFill", "uFill", "latin", "cs", "r", "rPr", "t" };
343        const string PARAGRAPH_PATH = "xdr:sp/xdr:txBody/a:p";
344        ExcelTextFont _font=null;
345        public ExcelTextFont Font
346        {
347            get
348            {
349                if (_font == null)
350                {
351                    XmlNode node = TopNode.SelectSingleNode(PARAGRAPH_PATH, NameSpaceManager);
352                    if(node==null)
353                    {
354                        Text="";    //Creates the node p element
355                        node = TopNode.SelectSingleNode(PARAGRAPH_PATH, NameSpaceManager);
356                    }
357                    _font = new ExcelTextFont(NameSpaceManager, TopNode, "xdr:sp/xdr:txBody/a:p/a:pPr/a:defRPr", paragraphNodeOrder);
358                }
359                return _font;
360            }
361        }
362        const string TextPath = "xdr:sp/xdr:txBody/a:p/a:r/a:t";
363        /// <summary>
364        /// Text inside the shape
365        /// </summary>
366        public string Text
367        {
368            get
369            {
370                return GetXmlNodeString(TextPath);
371            }
372            set
373            {
374                SetXmlNodeString(TextPath, value);
375            }
376
377        }
378        string lockTextPath = "xdr:sp/@fLocksText";
379        /// <summary>
380        /// Lock drawing
381        /// </summary>
382        public bool LockText
383        {
384            get
385            {
386                return GetXmlNodeBool(lockTextPath, true);
387            }
388            set
389            {
390                SetXmlNodeBool(lockTextPath, value);
391            }
392        }
393        ExcelParagraphCollection _richText = null;
394        /// <summary>
395        /// Richtext collection. Used to format specific parts of the text
396        /// </summary>
397        public ExcelParagraphCollection RichText
398        {
399            get
400            {
401                if (_richText == null)
402                {
403                    //XmlNode node=TopNode.SelectSingleNode(PARAGRAPH_PATH, NameSpaceManager);
404                    //if (node == null)
405                    //{
406                    //    CreateNode(PARAGRAPH_PATH);
407                    //}
408                        _richText = new ExcelParagraphCollection(NameSpaceManager, TopNode, PARAGRAPH_PATH, paragraphNodeOrder);
409                }
410                return _richText;
411            }
412        }
413        const string TextAnchoringPath = "xdr:sp/xdr:txBody/a:bodyPr/@anchor";
414        /// <summary>
415        /// Text Anchoring
416        /// </summary>
417        public eTextAnchoringType TextAnchoring
418        {
419            get
420            {
421                return GetTextAchoringEnum(GetXmlNodeString(TextAnchoringPath));
422            }
423            set
424            {
425                SetXmlNodeString(TextAnchoringPath, GetTextAchoringText(value));
426            }
427        }
428        const string TextAnchoringCtlPath = "xdr:sp/xdr:txBody/a:bodyPr/@anchorCtr";
429        /// <summary>
430        /// Specifies the centering of the text box.
431        /// </summary>
432        public bool TextAnchoringControl
433        {
434            get
435            {
436                return GetXmlNodeBool(TextAnchoringCtlPath);
437            }
438            set
439            {
440                if (value)
441                {
442                    SetXmlNodeString(TextAnchoringCtlPath, "1");
443                }
444                else
445                {
446                    SetXmlNodeString(TextAnchoringCtlPath, "0");
447                }
448            }
449        }
450        const string TEXT_ALIGN_PATH = "xdr:sp/xdr:txBody/a:p/a:pPr/@algn";
451        /// <summary>
452        /// How the text is aligned
453        /// </summary>
454        public eTextAlignment TextAlignment
455        {
456            get
457            {
458               switch(GetXmlNodeString(TEXT_ALIGN_PATH))
459               {
460                   case "ctr":
461                       return eTextAlignment.Center;
462                   case "r":
463                       return eTextAlignment.Right;
464                   case "dist":
465                       return eTextAlignment.Distributed;
466                   case "just":
467                       return eTextAlignment.Justified;
468                   case "justLow":
469                       return eTextAlignment.JustifiedLow;
470                   case "thaiDist":
471                       return eTextAlignment.ThaiDistributed;
472                   default:
473                       return eTextAlignment.Left;
474               }
475            }
476            set
477            {
478                switch (value)
479                {
480                    case eTextAlignment.Right:
481                        SetXmlNodeString(TEXT_ALIGN_PATH, "r");
482                        break;
483                    case eTextAlignment.Center:
484                        SetXmlNodeString(TEXT_ALIGN_PATH, "ctr");
485                        break;
486                    case eTextAlignment.Distributed:
487                        SetXmlNodeString(TEXT_ALIGN_PATH, "dist");
488                        break;
489                    case eTextAlignment.Justified:
490                        SetXmlNodeString(TEXT_ALIGN_PATH, "just");
491                        break;
492                    case eTextAlignment.JustifiedLow:
493                        SetXmlNodeString(TEXT_ALIGN_PATH, "justLow");
494                        break;
495                    case eTextAlignment.ThaiDistributed:
496                        SetXmlNodeString(TEXT_ALIGN_PATH, "thaiDist");
497                        break;
498                    default:
499                        DeleteNode(TEXT_ALIGN_PATH);
500                        break;
501                }               
502            }
503        }
504        const string INDENT_ALIGN_PATH = "xdr:sp/xdr:txBody/a:p/a:pPr/@lvl";
505        /// <summary>
506        /// Indentation
507        /// </summary>
508        public int Indent
509        {
510            get
511            {
512                return GetXmlNodeInt(INDENT_ALIGN_PATH);
513            }
514            set
515            {
516                if (value < 0 || value > 8)
517                {
518                    throw(new ArgumentOutOfRangeException("Indent level must be between 0 and 8"));
519                }
520                SetXmlNodeString(INDENT_ALIGN_PATH, value.ToString());
521            }
522        }
523        const string TextVerticalPath = "xdr:sp/xdr:txBody/a:bodyPr/@vert";
524        /// <summary>
525        /// Vertical text
526        /// </summary>
527        public eTextVerticalType TextVertical
528        {
529            get
530            {
531                return GetTextVerticalEnum(GetXmlNodeString(TextVerticalPath));
532            }
533            set
534            {
535                SetXmlNodeString(TextVerticalPath, GetTextVerticalText(value));
536            }
537        }
538
539        #endregion
540        #region "Private Methods"
541        private string ShapeStartXml()
542        {
543            StringBuilder xml = new StringBuilder();
544            xml.AppendFormat("<xdr:nvSpPr><xdr:cNvPr id=\"{0}\" name=\"{1}\" /><xdr:cNvSpPr /></xdr:nvSpPr><xdr:spPr><a:prstGeom prst=\"rect\"><a:avLst /></a:prstGeom></xdr:spPr><xdr:style><a:lnRef idx=\"2\"><a:schemeClr val=\"accent1\"><a:shade val=\"50000\" /></a:schemeClr></a:lnRef><a:fillRef idx=\"1\"><a:schemeClr val=\"accent1\" /></a:fillRef><a:effectRef idx=\"0\"><a:schemeClr val=\"accent1\" /></a:effectRef><a:fontRef idx=\"minor\"><a:schemeClr val=\"lt1\" /></a:fontRef></xdr:style><xdr:txBody><a:bodyPr vertOverflow=\"clip\" rtlCol=\"0\" anchor=\"ctr\" /><a:lstStyle /><a:p></a:p></xdr:txBody>", _id, Name);
545            return xml.ToString();
546        }
547        #endregion
548        internal new string Id
549        {
550            get { return Name + Text; }
551        }
552    }
553}
Note: See TracBrowser for help on using the repository browser.