/**
         * @return the next available record, or <code>null</code> if
         * this pass didn't return a record that's
         * suitable for returning (eg was a continue record).
         */
        private Record ReadNextRecord()
        {
            Record record = RecordFactory.CreateSingleRecord(_recStream);

            _lastRecordWasEOFLevelZero = false;

            if (record is BOFRecord)
            {
                _bofDepth++;
                return(record);
            }

            if (record is EOFRecord)
            {
                _bofDepth--;
                if (_bofDepth < 1)
                {
                    _lastRecordWasEOFLevelZero = true;
                }

                return(record);
            }

            if (record is DBCellRecord)
            {
                // Not needed by POI.  Regenerated from scratch by POI when spreadsheet is written
                return(null);
            }

            if (record is RKRecord)
            {
                return(RecordFactory.ConvertToNumberRecord((RKRecord)record));
            }

            if (record is MulRKRecord)
            {
                Record[] records = RecordFactory.ConvertRKRecords((MulRKRecord)record);

                _unreadRecordBuffer = records;
                _unreadRecordIndex  = 1;
                return(records[0]);
            }

            if (record.Sid == DrawingGroupRecord.sid &&
                _lastRecord is DrawingGroupRecord)
            {
                DrawingGroupRecord lastDGRecord = (DrawingGroupRecord)_lastRecord;
                lastDGRecord.Join((AbstractEscherHolderRecord)record);
                return(null);
            }
            if (record.Sid == ContinueRecord.sid)
            {
                ContinueRecord contRec = (ContinueRecord)record;

                if (_lastRecord is ObjRecord || _lastRecord is TextObjectRecord)
                {
                    // Drawing records have a very strange continue behaviour.
                    //There can actually be OBJ records mixed between the continues.
                    _lastDrawingRecord.ProcessContinueRecord(contRec.Data);
                    //we must remember the position of the continue record.
                    //in the serialization procedure the original structure of records must be preserved
                    if (_shouldIncludeContinueRecords)
                    {
                        return(record);
                    }
                    return(null);
                }
                if (_lastRecord is DrawingGroupRecord)
                {
                    ((DrawingGroupRecord)_lastRecord).ProcessContinueRecord(contRec.Data);
                    return(null);
                }
                if (_lastRecord is DrawingRecord)
                {
                    ((DrawingRecord)_lastRecord).ProcessContinueRecord(contRec.Data);
                    return(null);
                }
                if (_lastRecord is UnknownRecord)
                {
                    //Gracefully handle records that we don't know about,
                    //that happen to be continued
                    return(record);
                }
                if (_lastRecord is EOFRecord)
                {
                    // This is really odd, but excel still sometimes
                    //  outPuts a file like this all the same
                    return(record);
                }
                //if (_lastRecord is StringRecord)
                //{
                //    ((StringRecord)_lastRecord).ProcessContinueRecord(contRec.Data);
                //    return null;
                //}
                throw new RecordFormatException("Unhandled Continue Record");
            }
            _lastRecord = record;
            if (record is DrawingRecord)
            {
                _lastDrawingRecord = (DrawingRecord)record;
            }
            return(record);
        }
        /**
         * Creates a primary drawing Group record.  If it already 
         *  exists then it's modified.
         */
        public void CreateDrawingGroup()
        {
            if (drawingManager == null)
            {
                EscherContainerRecord dggContainer = new EscherContainerRecord();
                EscherDggRecord dgg = new EscherDggRecord();
                EscherOptRecord opt = new EscherOptRecord();
                EscherSplitMenuColorsRecord splitMenuColors = new EscherSplitMenuColorsRecord();

                dggContainer.RecordId=unchecked((short)0xF000);
                dggContainer.Options=(short)0x000F;
                dgg.RecordId=EscherDggRecord.RECORD_ID;
                dgg.Options=(short)0x0000;
                dgg.ShapeIdMax=1024;
                dgg.NumShapesSaved=0;
                dgg.DrawingsSaved=0;
                dgg.FileIdClusters=new EscherDggRecord.FileIdCluster[] { };
                drawingManager = new DrawingManager2(dgg);
                EscherContainerRecord bstoreContainer = null;
                if (escherBSERecords.Count > 0)
                {
                    bstoreContainer = new EscherContainerRecord();
                    bstoreContainer.RecordId=EscherContainerRecord.BSTORE_CONTAINER;
                    bstoreContainer.Options=(short)((escherBSERecords.Count << 4) | 0xF);
                    for (IEnumerator iterator = escherBSERecords.GetEnumerator(); iterator.MoveNext(); )
                    {
                        EscherRecord escherRecord = (EscherRecord)iterator.Current;
                        bstoreContainer.AddChildRecord(escherRecord);
                    }
                }
                opt.RecordId=unchecked((short)0xF00B);
                opt.Options=(short)0x0033;
                opt.AddEscherProperty(new EscherBoolProperty(EscherProperties.TEXT__SIZE_TEXT_TO_FIT_SHAPE, 524296));
                opt.AddEscherProperty(new EscherRGBProperty(EscherProperties.Fill__FillCOLOR, 0x08000041));
                opt.AddEscherProperty(new EscherRGBProperty(EscherProperties.LINESTYLE__COLOR, 134217792));
                splitMenuColors.RecordId=unchecked((short)0xF11E);
                splitMenuColors.Options=(short)0x0040;
                splitMenuColors.Color1=0x0800000D;
                splitMenuColors.Color2=0x0800000C;
                splitMenuColors.Color3=0x08000017;
                splitMenuColors.Color4=0x100000F7;

                dggContainer.AddChildRecord(dgg);
                if (bstoreContainer != null)
                    dggContainer.AddChildRecord(bstoreContainer);
                dggContainer.AddChildRecord(opt);
                dggContainer.AddChildRecord(splitMenuColors);

                int dgLoc = FindFirstRecordLocBySid(DrawingGroupRecord.sid);
                if (dgLoc == -1)
                {
                    DrawingGroupRecord drawingGroup = new DrawingGroupRecord();
                    drawingGroup.AddEscherRecord(dggContainer);
                    int loc = FindFirstRecordLocBySid(CountryRecord.sid);

                    Records.Insert(loc + 1, drawingGroup);
                }
                else
                {
                    DrawingGroupRecord drawingGroup = new DrawingGroupRecord();
                    drawingGroup.AddEscherRecord(dggContainer);
                    Records[dgLoc]= drawingGroup;
                }

            }
        }