WKSContentListener.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
00002 /* libwps
00003  * Version: MPL 2.0 / LGPLv2.1+
00004  *
00005  * This Source Code Form is subject to the terms of the Mozilla Public
00006  * License, v. 2.0. If a copy of the MPL was not distributed with this
00007  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
00008  *
00009  * Major Contributor(s):
00010  * Copyright (C) 2006, 2007 Andrew Ziem
00011  * Copyright (C) 2006 Fridrich Strba (fridrich.strba@bluewin.ch)
00012  * Copyright (C) 2003-2005 William Lachance (william.lachance@sympatico.ca)
00013  * Copyright (C) 2003 Marc Maurer (uwog@uwog.net)
00014  *
00015  * For minor contributions see the git repository.
00016  *
00017  * Alternatively, the contents of this file may be used under the terms
00018  * of the GNU Lesser General Public License Version 2.1 or later
00019  * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
00020  * applicable instead of those above.
00021  *
00022  * For further information visit http://libwps.sourceforge.net
00023  */
00024 
00025 #ifndef WKSCONTENTLISTENER_H
00026 #define WKSCONTENTLISTENER_H
00027 
00028 #include <vector>
00029 
00030 #include <librevenge/librevenge.h>
00031 
00032 #include "libwps_internal.h"
00033 
00034 #include "WPSEntry.h"
00035 #include "WPSGraphicStyle.h"
00036 
00037 #include "WPSListener.h"
00038 
00039 class WPSCellFormat;
00040 class WPSGraphicShape;
00041 class WPSGraphicStyle;
00042 class WPSList;
00043 class WPSPageSpan;
00044 struct WPSParagraph;
00045 struct WPSTabStop;
00046 
00047 struct WKSContentParsingState;
00048 struct WKSDocumentParsingState;
00049 
00050 class WKSContentListener : public WPSListener
00051 {
00052 public:
00054         struct FormulaInstruction
00055         {
00056                 enum What { F_Operator, F_Function, F_Cell, F_CellList, F_Long, F_Double, F_Text };
00058                 FormulaInstruction() : m_type(F_Text), m_content(""), m_longValue(0), m_doubleValue(0), m_sheetName("")
00059                 {
00060                         for (int i=0; i<2; ++i)
00061                         {
00062                                 m_position[i]=Vec2i(0,0);
00063                                 m_positionRelative[i]=Vec2b(false,false);
00064                         }
00065                 }
00067                 librevenge::RVNGPropertyList getPropertyList() const;
00069                 friend std::ostream &operator<<(std::ostream &o, FormulaInstruction const &inst);
00071                 What m_type;
00073                 std::string m_content;
00075                 double m_longValue;
00077                 double m_doubleValue;
00079                 Vec2i m_position[2];
00081                 Vec2b m_positionRelative[2];
00083                 librevenge::RVNGString m_sheetName;
00084         };
00086         struct CellContent
00087         {
00089                 enum ContentType { C_NONE, C_TEXT, C_NUMBER, C_FORMULA, C_UNKNOWN };
00091                 CellContent() : m_contentType(C_UNKNOWN), m_value(0.0), m_valueSet(false), m_textEntry(), m_formula() { }
00093                 ~CellContent() {}
00095                 friend std::ostream &operator<<(std::ostream &o, CellContent const &cell);
00096 
00098                 bool empty() const
00099                 {
00100                         if (m_contentType == C_NUMBER) return false;
00101                         if (m_contentType == C_TEXT && !m_textEntry.valid()) return false;
00102                         if (m_contentType == C_FORMULA && (m_formula.size() || isValueSet())) return false;
00103                         return true;
00104                 }
00106                 void setValue(double value)
00107                 {
00108                         m_value = value;
00109                         m_valueSet = true;
00110                 }
00112                 bool isValueSet() const
00113                 {
00114                         return m_valueSet;
00115                 }
00117                 bool hasText() const
00118                 {
00119                         return m_textEntry.valid();
00120                 }
00122                 static bool double2Date(double val, int &Y, int &M, int &D);
00124                 static bool double2Time(double val, int &H, int &M, int &S);
00125 
00127                 ContentType m_contentType;
00129                 double m_value;
00131                 bool m_valueSet;
00133                 WPSEntry m_textEntry;
00135                 std::vector<FormulaInstruction> m_formula;
00136         };
00137 
00138         WKSContentListener(std::vector<WPSPageSpan> const &pageList, librevenge::RVNGSpreadsheetInterface *documentInterface);
00139         virtual ~WKSContentListener();
00140 
00141         void setDocumentLanguage(int lcid);
00142 
00143         void startDocument();
00144         void endDocument();
00145         void handleSubDocument(WPSSubDocumentPtr &subDocument, libwps::SubDocumentType subDocumentType);
00146 
00147         // ------ text data -----------
00148 
00150         void insertCharacter(uint8_t character);
00154         void insertUnicode(uint32_t character);
00156         void insertUnicodeString(librevenge::RVNGString const &str);
00157 
00158         void insertTab();
00159         void insertEOL(bool softBreak=false);
00160         void insertBreak(const uint8_t breakType);
00161 
00162         // ------ text format -----------
00164         void setFont(const WPSFont &font);
00166         WPSFont const &getFont() const;
00167 
00168         // ------ paragraph format -----------
00170         bool isParagraphOpened() const;
00172         void setParagraph(const WPSParagraph &para);
00174         WPSParagraph const &getParagraph() const;
00175 
00176         // ------- fields ----------------
00178         void insertField(FieldType type);
00180         void insertDateTimeField(char const *format);
00181 
00182         // ------- subdocument -----------------
00184         void insertComment(WPSSubDocumentPtr &subDocument);
00186         void insertPicture(WPSPosition const &pos, const librevenge::RVNGBinaryData &binaryData,
00187                            std::string type="image/pict", WPSGraphicStyle const &style=WPSGraphicStyle::emptyStyle());
00189         void insertObject(WPSPosition const &pos, const WPSEmbeddedObject &obj,
00190                           WPSGraphicStyle const &style=WPSGraphicStyle::emptyStyle());
00192         void insertPicture(WPSPosition const &pos, WPSGraphicShape const &shape, WPSGraphicStyle const &style);
00194         void insertTextBox(WPSPosition const &pos, WPSSubDocumentPtr subDocument,
00195                            WPSGraphicStyle const &frameStyle=WPSGraphicStyle::emptyStyle());
00196 
00197         // ------- sheet -----------------
00199         void openSheet(std::vector<float> const &colWidth, librevenge::RVNGUnit unit, librevenge::RVNGString const &name="");
00201         void closeSheet();
00203         void openSheetRow(float h, librevenge::RVNGUnit unit, bool headerRow=false);
00205         void closeSheetRow();
00210         void openSheetCell(WPSCell const &cell, CellContent const &content, librevenge::RVNGPropertyList const &extras=librevenge::RVNGPropertyList());
00212         void closeSheetCell();
00213 
00214 protected:
00215         void _openPageSpan();
00216         void _closePageSpan();
00217 
00218         void _handleFrameParameters(librevenge::RVNGPropertyList &propList, WPSPosition const &pos);
00219         bool _openFrame(WPSPosition const &pos, WPSGraphicStyle const &style);
00220         void _closeFrame();
00221 
00222         void _startSubDocument();
00223         void _endSubDocument();
00224 
00225         void _openParagraph();
00226         void _closeParagraph();
00227         void _appendParagraphProperties(librevenge::RVNGPropertyList &propList, const bool isListElement=false);
00228         void _resetParagraphState(const bool isListElement=false);
00229 
00230         void _openSpan();
00231         void _closeSpan();
00232 
00233         void _flushText();
00234         void _flushDeferredTabs();
00235 
00236         void _insertBreakIfNecessary(librevenge::RVNGPropertyList &propList);
00237 
00241         shared_ptr<WKSContentParsingState> _pushParsingState();
00243         void _popParsingState();
00244 
00245 protected:
00246         shared_ptr<WKSDocumentParsingState> m_ds; // main parse state
00247         shared_ptr<WKSContentParsingState> m_ps; // parse state
00248         std::vector<shared_ptr<WKSContentParsingState> > m_psStack;
00249         librevenge::RVNGSpreadsheetInterface *m_documentInterface;
00250 
00251 private:
00252         WKSContentListener(const WKSContentListener &);
00253         WKSContentListener &operator=(const WKSContentListener &);
00254 };
00255 
00256 #endif
00257 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */