zhao
2021-07-02 23ee356c6f260ecc1a48bbb8bd60932b979e4698
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* ====================================================================
   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;
    using System.Collections;
    using HH.WMS.Utils.NPOI.DDF;
    using HH.WMS.Utils.NPOI.HSSF.Record;
    using HH.WMS.Utils.NPOI.Util;
    using HH.WMS.Utils.NPOI.SS.UserModel;
    using System.Collections.Generic;
 
    /// <summary>
    /// The patriarch is the toplevel container for shapes in a sheet.  It does
    /// little other than act as a container for other shapes and Groups.
    /// @author Glen Stampoultzis (glens at apache.org)
    /// </summary>
    public class HSSFPatriarch : HSSFShapeContainer,IDrawing
    {
        List<HSSFShape> _shapes = new List<HSSFShape>();
        public HSSFSheet _sheet;
        int x1 = 0;
        int y1 = 0;
        int x2 = 1023;
        int y2 = 255;
 
        /**
         * The EscherAggregate we have been bound to.
         * (This will handle writing us out into records,
         *  and building up our shapes from the records)
         */
        private EscherAggregate _boundAggregate;
 
        /// <summary>
        /// Creates the patriarch.
        /// </summary>
        /// <param name="sheet">the sheet this patriarch is stored in.</param>
        /// <param name="boundAggregate">The bound aggregate.</param>
        public HSSFPatriarch(HSSFSheet sheet, EscherAggregate boundAggregate)
        {
            this._boundAggregate = boundAggregate;
            this._sheet = sheet;
        }
 
        /// <summary>
        /// Creates a new Group record stored Under this patriarch.
        /// </summary>
        /// <param name="anchor">the client anchor describes how this Group is attached
        /// to the sheet.</param>
        /// <returns>the newly created Group.</returns>
        public HSSFShapeGroup CreateGroup(HSSFClientAnchor anchor)
        {
            HSSFShapeGroup group = new HSSFShapeGroup(null, anchor);
            group.Anchor = anchor;
            AddShape(group);
            return group;
        }
 
        /// <summary>
        /// Creates a simple shape.  This includes such shapes as lines, rectangles,
        /// and ovals.
        /// </summary>
        /// <param name="anchor">the client anchor describes how this Group is attached
        /// to the sheet.</param>
        /// <returns>the newly created shape.</returns>
        public HSSFSimpleShape CreateSimpleShape(HSSFClientAnchor anchor)
        {
            HSSFSimpleShape shape = new HSSFSimpleShape(null, anchor);
            shape.Anchor = anchor;
            AddShape(shape);
            return shape;
        }
 
        /// <summary>
        /// Creates a picture.
        /// </summary>
        /// <param name="anchor">the client anchor describes how this Group is attached
        /// to the sheet.</param>
        /// <param name="pictureIndex">Index of the picture.</param>
        /// <returns>the newly created shape.</returns>
        public IPicture CreatePicture(HSSFClientAnchor anchor, int pictureIndex)
        {
            HSSFPicture shape = new HSSFPicture(null, (HSSFClientAnchor)anchor);
            shape.PictureIndex=pictureIndex;
            shape.Anchor = (HSSFClientAnchor)anchor;
            AddShape(shape);
 
            EscherBSERecord bse = (_sheet.Workbook as HSSFWorkbook).Workbook.GetBSERecord(pictureIndex);
            bse.Ref = (bse.Ref + 1);
            return shape;
        }
        public IPicture CreatePicture(IClientAnchor anchor, int pictureIndex)
        {
            return CreatePicture((HSSFClientAnchor)anchor, pictureIndex);
        }
 
        /// <summary>
        /// Creates a polygon
        /// </summary>
        /// <param name="anchor">the client anchor describes how this Group is attached
        /// to the sheet.</param>
        /// <returns>the newly Created shape.</returns>
        public HSSFPolygon CreatePolygon(IClientAnchor anchor)
        {
            HSSFPolygon shape = new HSSFPolygon(null, (HSSFAnchor)anchor);
            shape.Anchor = (HSSFAnchor)anchor;
            AddShape(shape);
            return shape;
        }
 
        /// <summary>
        /// Constructs a textbox Under the patriarch.
        /// </summary>
        /// <param name="anchor">the client anchor describes how this Group is attached
        /// to the sheet.</param>
        /// <returns>the newly Created textbox.</returns>
        public HSSFSimpleShape CreateTextbox(IClientAnchor anchor)
        {
            HSSFTextbox shape = new HSSFTextbox(null, (HSSFAnchor)anchor);
            shape.Anchor = (HSSFAnchor)anchor;
            AddShape(shape);
            return shape;
        }
        /**
         * Constructs a cell comment.
         *
         * @param anchor    the client anchor describes how this comment is attached
         *                  to the sheet.
         * @return      the newly created comment.
         */
        public HSSFComment CreateComment(HSSFAnchor anchor)
        {
            HSSFComment shape = new HSSFComment(null, anchor);
            shape.Anchor = anchor;
            AddShape(shape);
            return shape;
        }
        /**
         * YK: used to create autofilters
         *
         * @see org.apache.poi.hssf.usermodel.HSSFSheet#setAutoFilter(int, int, int, int)
         */
        public HSSFSimpleShape CreateComboBox(HSSFAnchor anchor)
        {
            HSSFSimpleShape shape = new HSSFSimpleShape(null, anchor);
            shape.ShapeType = HSSFSimpleShape.OBJECT_TYPE_COMBO_BOX;
            shape.Anchor = anchor;
            AddShape(shape);
            return shape;
        }
 
        /// <summary>
        /// Constructs a cell comment.
        /// </summary>
        /// <param name="anchor">the client anchor describes how this comment is attached
        /// to the sheet.</param>
        /// <returns>the newly created comment.</returns>
        public IComment CreateCellComment(IClientAnchor anchor)
        {
            return CreateComment((HSSFAnchor)anchor);
        }
 
        /// <summary>
        /// Returns a list of all shapes contained by the patriarch.
        /// </summary>
        /// <value>The children.</value>
        public System.Collections.IList Children
        {
            get { return _shapes; }
        }
 
        /**
         * add a shape to this drawing
         */
        internal void AddShape(HSSFShape shape)
        {
            shape._patriarch = this;
            _shapes.Add(shape);
        }
        /// <summary>
        /// Total count of all children and their children's children.
        /// </summary>
        /// <value>The count of all children.</value>
        public int CountOfAllChildren
        {
            get
            {
                int count = _shapes.Count;
                for (IEnumerator iterator = _shapes.GetEnumerator(); iterator.MoveNext(); )
                {
                    HSSFShape shape = (HSSFShape)iterator.Current;
                    count += shape.CountOfAllChildren;
                }
                return count;
            }
        }
        /// <summary>
        /// Sets the coordinate space of this Group.  All children are contrained
        /// to these coordinates.
        /// </summary>
        /// <param name="x1">The x1.</param>
        /// <param name="y1">The y1.</param>
        /// <param name="x2">The x2.</param>
        /// <param name="y2">The y2.</param>
        public void SetCoordinates(int x1, int y1, int x2, int y2)
        {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
        }
 
        /// <summary>
        /// Does this HSSFPatriarch contain a chart?
        /// (Technically a reference to a chart, since they
        /// Get stored in a different block of records)
        /// FIXME - detect chart in all cases (only seems
        /// to work on some charts so far)
        /// </summary>
        /// <returns>
        ///     <c>true</c> if this instance contains chart; otherwise, <c>false</c>.
        /// </returns>
        public bool ContainsChart()
        {
            // TODO - support charts properly in usermodel
 
            // We're looking for a EscherOptRecord
            EscherOptRecord optRecord = (EscherOptRecord)
                _boundAggregate.FindFirstWithId(EscherOptRecord.RECORD_ID);
            if (optRecord == null)
            {
                // No opt record, can't have chart
                return false;
            }
 
            for (IEnumerator it = optRecord.EscherProperties.GetEnumerator(); it.MoveNext(); )
            {
                EscherProperty prop = (EscherProperty)it.Current;
                if (prop.PropertyNumber == 896 && prop.IsComplex)
                {
                    EscherComplexProperty cp = (EscherComplexProperty)prop;
                    String str = StringUtil.GetFromUnicodeLE(cp.ComplexData);
                    //Console.Error.WriteLine(str);
                    if (str.Equals("Chart 1\0"))
                    {
                        return true;
                    }
                }
            }
 
            return false;
        }
 
        /// <summary>
        /// The top left x coordinate of this Group.
        /// </summary>
        /// <value>The x1.</value>
        public int X1
        {
            get { return x1; }
        }
 
        /// <summary>
        /// The top left y coordinate of this Group.
        /// </summary>
        /// <value>The y1.</value>
        public int Y1
        {
            get { return y1; }
        }
 
        /// <summary>
        /// The bottom right x coordinate of this Group.
        /// </summary>
        /// <value>The x2.</value>
        public int X2
        {
            get { return x2; }
        }
 
        /// <summary>
        /// The bottom right y coordinate of this Group.
        /// </summary>
        /// <value>The y2.</value>
        public int Y2
        {
            get { return y2; }
        }
 
        /// <summary>
        /// Returns the aggregate escher record we're bound to
        /// </summary>
        /// <returns></returns>
        protected EscherAggregate _GetBoundAggregate()
        {
            return _boundAggregate;
        }
 
        /**
         * Creates a new client anchor and sets the top-left and bottom-right
         * coordinates of the anchor.
         *
         * @param dx1  the x coordinate in EMU within the first cell.
         * @param dy1  the y coordinate in EMU within the first cell.
         * @param dx2  the x coordinate in EMU within the second cell.
         * @param dy2  the y coordinate in EMU within the second cell.
         * @param col1 the column (0 based) of the first cell.
         * @param row1 the row (0 based) of the first cell.
         * @param col2 the column (0 based) of the second cell.
         * @param row2 the row (0 based) of the second cell.
         * @return the newly created client anchor
         */
        public IClientAnchor CreateAnchor(int dx1, int dy1, int dx2, int dy2, int col1, int row1, int col2, int row2)
        {
            return new HSSFClientAnchor(dx1, dy1, dx2, dy2, (short)col1, row1, (short)col2, row2);
        }
 
        public IChart CreateChart(IClientAnchor anchor)
        {
            throw new RuntimeException("NotImplemented");
        }
    }
}