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 
00036 #include "WPSListener.h"
00037 
00038 class WPSCellFormat;
00039 class WPSList;
00040 class WPSPageSpan;
00041 struct WPSParagraph;
00042 struct WPSTabStop;
00043 
00044 struct WKSContentParsingState;
00045 struct WKSDocumentParsingState;
00046 
00047 class WKSContentListener : public WPSListener
00048 {
00049 public:
00051         struct FormulaInstruction
00052         {
00053                 enum What { F_Operator, F_Function, F_Cell, F_CellList, F_Long, F_Double, F_Text };
00055                 FormulaInstruction() : m_type(F_Text), m_content(""), m_longValue(0), m_doubleValue(0)
00056                 {
00057                         for (int i=0; i<2; ++i)
00058                         {
00059                                 m_position[i]=Vec2i(0,0);
00060                                 m_positionRelative[i]=Vec2b(false,false);
00061                         }
00062                 }
00064                 librevenge::RVNGPropertyList getPropertyList() const;
00066                 friend std::ostream &operator<<(std::ostream &o, FormulaInstruction const &inst);
00068                 What m_type;
00070                 std::string m_content;
00072                 double m_longValue;
00074                 double m_doubleValue;
00076                 Vec2i m_position[2];
00078                 Vec2b m_positionRelative[2];
00079         };
00081         struct CellContent
00082         {
00084                 enum ContentType { C_NONE, C_TEXT, C_NUMBER, C_FORMULA, C_UNKNOWN };
00086                 CellContent() : m_contentType(C_UNKNOWN), m_value(0.0), m_valueSet(false), m_textEntry(), m_formula() { }
00088                 ~CellContent() {}
00090                 friend std::ostream &operator<<(std::ostream &o, CellContent const &cell);
00091 
00093                 bool empty() const
00094                 {
00095                         if (m_contentType == C_NUMBER) return false;
00096                         if (m_contentType == C_TEXT && !m_textEntry.valid()) return false;
00097                         if (m_contentType == C_FORMULA && (m_formula.size() || isValueSet())) return false;
00098                         return true;
00099                 }
00101                 void setValue(double value)
00102                 {
00103                         m_value = value;
00104                         m_valueSet = true;
00105                 }
00107                 bool isValueSet() const
00108                 {
00109                         return m_valueSet;
00110                 }
00112                 bool hasText() const
00113                 {
00114                         return m_textEntry.valid();
00115                 }
00117                 static bool double2Date(double val, int &Y, int &M, int &D);
00119                 static bool double2Time(double val, int &H, int &M, int &S);
00120 
00122                 ContentType m_contentType;
00124                 double m_value;
00126                 bool m_valueSet;
00128                 WPSEntry m_textEntry;
00130                 std::vector<FormulaInstruction> m_formula;
00131         };
00132 
00133         WKSContentListener(std::vector<WPSPageSpan> const &pageList, librevenge::RVNGSpreadsheetInterface *documentInterface);
00134         virtual ~WKSContentListener();
00135 
00136         void setDocumentLanguage(int lcid);
00137 
00138         void startDocument();
00139         void endDocument();
00140         void handleSubDocument(WPSSubDocumentPtr &subDocument, libwps::SubDocumentType subDocumentType);
00141 
00142         // ------ text data -----------
00143 
00145         void insertCharacter(uint8_t character);
00149         void insertUnicode(uint32_t character);
00151         void insertUnicodeString(librevenge::RVNGString const &str);
00152 
00153         void insertTab();
00154         void insertEOL(bool softBreak=false);
00155         void insertBreak(const uint8_t breakType);
00156 
00157         // ------ text format -----------
00159         void setFont(const WPSFont &font);
00161         WPSFont const &getFont() const;
00162 
00163         // ------ paragraph format -----------
00165         bool isParagraphOpened() const;
00167         void setParagraph(const WPSParagraph &para);
00169         WPSParagraph const &getParagraph() const;
00170 
00171         // ------- fields ----------------
00173         void insertField(FieldType type);
00175         void insertDateTimeField(char const *format);
00176 
00177         // ------- subdocument -----------------
00179         void insertComment(WPSSubDocumentPtr &subDocument);
00180 
00181         // ------- sheet -----------------
00183         void openSheet(std::vector<float> const &colWidth, librevenge::RVNGUnit unit);
00185         void closeSheet();
00187         void openSheetRow(float h, librevenge::RVNGUnit unit, bool headerRow=false);
00189         void closeSheetRow();
00194         void openSheetCell(WPSCell const &cell, CellContent const &content, librevenge::RVNGPropertyList const &extras=librevenge::RVNGPropertyList());
00196         void closeSheetCell();
00197 
00198 protected:
00199         void _openPageSpan();
00200         void _closePageSpan();
00201 
00202         void _startSubDocument();
00203         void _endSubDocument();
00204 
00205         void _openParagraph();
00206         void _closeParagraph();
00207         void _appendParagraphProperties(librevenge::RVNGPropertyList &propList, const bool isListElement=false);
00208         void _resetParagraphState(const bool isListElement=false);
00209 
00210         void _openSpan();
00211         void _closeSpan();
00212 
00213         void _flushText();
00214         void _flushDeferredTabs();
00215 
00216         void _insertBreakIfNecessary(librevenge::RVNGPropertyList &propList);
00217 
00221         shared_ptr<WKSContentParsingState> _pushParsingState();
00223         void _popParsingState();
00224 
00225 protected:
00226         shared_ptr<WKSDocumentParsingState> m_ds; // main parse state
00227         shared_ptr<WKSContentParsingState> m_ps; // parse state
00228         std::vector<shared_ptr<WKSContentParsingState> > m_psStack;
00229         librevenge::RVNGSpreadsheetInterface *m_documentInterface;
00230 
00231 private:
00232         WKSContentListener(const WKSContentListener &);
00233         WKSContentListener &operator=(const WKSContentListener &);
00234 };
00235 
00236 #endif
00237 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */