1 /* AbiWord 2 * Copyright (C) 1998 AbiSource, Inc. 3 * Updates by Ben Martin in 2011. 4 * 5 * This program is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU General Public License 7 * as published by the Free Software Foundation; either version 2 8 * of the License, or (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 18 * 02110-1301 USA. 19 */ 20 21 22 #ifndef PF_FRAG_H 23 #define PF_FRAG_H 24 25 #include <stdio.h> 26 #include "ut_types.h" 27 #include "pt_Types.h" 28 #include "pf_Fragments.h" 29 #include <string> 30 31 class pt_PieceTable; 32 class PX_ChangeRecord; 33 class fd_Field; 34 class pf_Frag_Strux; 35 36 37 /*! 38 pf_Frag represents a fragment of the document. This may be text 39 (pf_Frag_Text), an inline object such as an image (pf_Frag_Object), 40 or structure information such as a paragraph or section 41 (pf_Frag_Strux_Block, pf_Frag_Strux_Section). 42 43 pf_Frag is an abstract base class. 44 We use an enum to remember type, rather than use any of the 45 run-time stuff. 46 */ 47 48 class ABI_EXPORT pf_Frag 49 { 50 friend class pf_Fragments; 51 public: 52 typedef enum _PFType { PFT_Text = 0, PFT_Object, PFT_Strux, PFT_EndOfDoc, PFT_FmtMark } PFType; 53 54 pf_Frag(pt_PieceTable * pPT, PFType type, UT_uint32 length); 55 virtual ~pf_Frag(); 56 getType(void)57 inline PFType getType(void) const { return m_type; } 58 pf_Frag * getNext(void) const; 59 pf_Frag * getPrev(void) const; 60 pf_Frag_Strux* getNextStrux(PTStruxType t) const; 61 pf_Frag_Strux* tryDownCastStrux(PTStruxType t) const; 62 getLength(void)63 inline UT_uint32 getLength(void) const { return m_length; } zero(void)64 inline void zero(void) 65 { m_length = 0;} 66 getPieceTable(void)67 pt_PieceTable * getPieceTable(void) const { return m_pPieceTable;} 68 fd_Field * getField(void) const; 69 PT_DocPosition getPos(void) const; 70 void lengthChanged(UT_sint32 delta); getLeftTreeLength(void)71 PT_DocPosition getLeftTreeLength(void) const { return m_leftTreeLength; } setLeftTreeLength(PT_DocPosition length)72 void setLeftTreeLength(PT_DocPosition length) const { m_leftTreeLength = length;} 73 74 /* We need the following function to accumulate left tree length */ 75 void accLeftTreeLength(PT_DocPosition length); 76 getIndexAP(void)77 inline PT_AttrPropIndex getIndexAP(void) const {return m_indexAP;} setIndexAP(PT_AttrPropIndex indexNewAP)78 virtual void setIndexAP(PT_AttrPropIndex indexNewAP) {m_indexAP = indexNewAP;} 79 80 // createSpecialChangeRecord() constructs a change 81 // record which describes the fragment itself and 82 // not an actual change (editing) operation. the 83 // is used to initialize the listeners. 84 virtual bool createSpecialChangeRecord(PX_ChangeRecord ** ppcr, 85 PT_DocPosition dpos) const; 86 87 // compare contents of two fragments, ignoring format 88 bool isContentEqual(const pf_Frag & f2) const; 89 getXID()90 UT_uint32 getXID() const {return m_iXID;} setXID(UT_uint32 xid)91 void setXID(UT_uint32 xid) {m_iXID = xid;} 92 93 // I would much prefer if this was a pure vitual, but we do not have Eod frag usesXID()94 virtual bool usesXID() const {return false;} 95 96 // compare contents and format of two fragments 97 bool operator == (const pf_Frag & f2) const; 98 99 std::string getXMLID() const; 100 101 #ifdef PT_TEST 102 virtual void __dump(FILE * fp) const; 103 #endif 104 105 106 protected: 107 /*! 108 _isContentEqual() is a helper function for operator==() and 109 isContentEqual(). 110 111 This function compares the contents of the two fragments, but not 112 their formatting properties. 113 114 This function and its descendants in the derived classes assume that 115 the two fragments are of the same type; it is the responsibility 116 of the caller to ensure that !!! 117 118 Implementations in derived classes should first call 119 _isContentEqual() of their immediate base and if it returns false, 120 also return false. If the base function returns true, the function 121 in derived class should carry out any further processing specific 122 to that class; in doing so it must ignore general formating 123 properties. For example, pf_Frag_Text::_isContentEqual() should 124 only examine the characters contained in the fragment, but not 125 font face, font size, etc. 126 */ 127 _isContentEqual(const pf_Frag &)128 virtual bool _isContentEqual(const pf_Frag & /*f2*/) const {return true;} 129 130 PFType m_type; 131 132 fd_Field * m_pField; 133 pt_PieceTable * m_pPieceTable; 134 PT_AttrPropIndex m_indexAP; 135 136 /* in PT_DocPosition-space - gives length of this fragment */ 137 UT_uint32 m_length; 138 139 private: 140 void _setNode(pf_Fragments::Node * pNode); 141 pf_Fragments::Node * _getNode(void) const; 142 /* In PT_DocPosition space - specifies size of left subtree */ 143 mutable UT_uint32 m_leftTreeLength; 144 UT_uint32 m_iXID; 145 pf_Fragments::Node * m_pMyNode; 146 }; 147 148 // This is like pf->tryDownCastStrux() but you can pass a null pf as arg1 149 // safely. 150 pf_Frag_Strux* tryDownCastStrux( pf_Frag* pf, PTStruxType t); 151 152 #endif /* PF_FRAG_H */ 153