1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #ifndef _FDE_TXTEDTENGINE_H
8 #define _FDE_TXTEDTENGINE_H
9 class CFDE_TxtEdtBuf;
10 class CFDE_TxtEdtPage;
11 class IFX_TxtBreak;
12 class CFDE_TxtEdtParag;
13 class CFDE_TxtEdtKMPMatch;
14 class IFDE_TxtEdtFormator;
15 class CFDE_TxtEdtField;
16 class CFDE_TxtEdtBlock;
17 class IFX_CharIter;
18 class CFDE_TxtEdtEngine;
19 class CFDE_TxtEdtDoRecord_Insert;
20 class CFDE_TxtEdtDoRecord_DeleteRange;
21 #ifdef FDE_USEFORMATBLOCK
22 class CFDE_TxtEdtDoRecord_FormatInsert;
23 class CFDE_TxtEdtDoRecord_FormatDelete;
24 class CFDE_TxtEdtDoRecord_FormatReplace;
25 class CFDE_TxtEdtDoRecord_FieldInsert;
26 class CFDE_TxtEdtDoRecord_FieldDelete;
27 class CFDE_TxtEdtDoRecord_FieldReplace;
28 #endif
29 class IFDE_TxtEdtDoRecord {
30  public:
31   static IFDE_TxtEdtDoRecord* Create(const CFX_ByteStringC& bsDoRecord);
~IFDE_TxtEdtDoRecord()32   virtual ~IFDE_TxtEdtDoRecord() {}
33   virtual void Release() = 0;
34   virtual FX_BOOL Redo() = 0;
35   virtual FX_BOOL Undo() = 0;
36   virtual void Serialize(CFX_ByteString& bsDoRecord) const = 0;
37 };
38 class CFDE_TxtEdtEngine : public IFDE_TxtEdtEngine {
39   friend class CFDE_TxtEdtDoRecord_Insert;
40   friend class CFDE_TxtEdtDoRecord_DeleteRange;
41   friend class CFDE_TxtEdtPage;
42 #ifdef FDE_USEFORMATBLOCK
43   friend class CFDE_TxtEdtDoRecord_FormatInsert;
44   friend class CFDE_TxtEdtDoRecord_FormatDelete;
45   friend class CFDE_TxtEdtDoRecord_FormatReplace;
46   friend class CFDE_TxtEdtBlock;
47 #endif
48   struct _FDE_TXTEDTSELRANGE {
49     int32_t nStart;
50     int32_t nCount;
51   };
52   typedef _FDE_TXTEDTSELRANGE FDE_TXTEDTSELRANGE;
53   typedef _FDE_TXTEDTSELRANGE* FDE_LPTXTEDTSELRANGE;
54   struct _FDE_TXTEDTPARAGPOS {
55     int32_t nParagIndex;
56     int32_t nCharIndex;
57   };
58   typedef _FDE_TXTEDTPARAGPOS FDE_TXTEDTPARAGPOS;
59   typedef _FDE_TXTEDTPARAGPOS* FDE_LPTXTEDTPARAGPOS;
60 
61  public:
62   CFDE_TxtEdtEngine();
63   virtual void Release();
64 
65   virtual void SetEditParams(const FDE_TXTEDTPARAMS& params);
66   virtual const FDE_TXTEDTPARAMS* GetEditParams() const;
67 
68   virtual int32_t CountPages() const;
69   virtual IFDE_TxtEdtPage* GetPage(int32_t nIndex);
70 
71   virtual FX_BOOL SetBufChunkSize(int32_t nChunkSize);
72   virtual void SetTextByStream(IFX_Stream* pStream);
73   virtual void SetText(const CFX_WideString& wsText);
74   virtual int32_t GetTextLength() const;
75   virtual void GetText(CFX_WideString& wsText,
76                        int32_t nStart,
77                        int32_t nCount = -1);
78   virtual void ClearText();
79 
80   virtual int32_t GetCaretRect(CFX_RectF& rtCaret) const;
81   virtual int32_t GetCaretPos() const;
82   virtual int32_t SetCaretPos(int32_t nIndex, FX_BOOL bBefore);
83   virtual int32_t MoveCaretPos(FDE_TXTEDTMOVECARET eMoveCaret,
84                                FX_BOOL bShift = FALSE,
85                                FX_BOOL bCtrl = FALSE);
86   virtual void Lock();
87   virtual void Unlock();
88   virtual FX_BOOL IsLocked() const;
89 
90   virtual int32_t Insert(int32_t nStart,
91                          const FX_WCHAR* lpText,
92                          int32_t nLength);
93   virtual int32_t Delete(int32_t nStart, FX_BOOL bBackspace = FALSE);
94   virtual int32_t DeleteRange(int32_t nStart, int32_t nCount = -1);
95   virtual int32_t Replace(int32_t nStart,
96                           int32_t nLength,
97                           const CFX_WideString& wsReplace);
98 
99   virtual void SetLimit(int32_t nLimit);
100   virtual void SetAliasChar(FX_WCHAR wcAlias);
101   virtual void SetFormatBlock(int32_t nIndex,
102                               const CFX_WideString& wsBlockFormat);
103   virtual int32_t CountEditBlocks() const;
104   virtual void GetEditBlockText(int32_t nIndex,
105                                 CFX_WideString& wsBlockText) const;
106   virtual int32_t CountEditFields(int32_t nBlockIndex) const;
107   virtual void GetEditFieldText(int32_t nBlockIndex,
108                                 int32_t nFieldIndex,
109                                 CFX_WideString& wsFieldText) const;
110   virtual void StartEdit();
111   virtual void EndEdit();
112 
113   void RemoveSelRange(int32_t nStart, int32_t nCount = -1);
114 
115   virtual void AddSelRange(int32_t nStart, int32_t nCount = -1);
116   virtual int32_t CountSelRanges();
117   virtual int32_t GetSelRange(int32_t nIndex, int32_t& nStart);
118   virtual void ClearSelection();
119 
120   virtual FX_BOOL Redo(const CFX_ByteStringC& bsRedo);
121   virtual FX_BOOL Undo(const CFX_ByteStringC& bsUndo);
122 
123   virtual int32_t StartLayout();
124   virtual int32_t DoLayout(IFX_Pause* pPause);
125   virtual void EndLayout();
126 
127   virtual FX_BOOL Optimize(IFX_Pause* pPause = NULL);
128   virtual int32_t CountParags() const;
129   virtual IFDE_TxtEdtParag* GetParag(int32_t nParagIndex) const;
130   virtual IFX_CharIter* CreateCharIter();
131   IFDE_TxtEdtBuf* GetTextBuf() const;
132   int32_t GetTextBufLength() const;
133   IFX_TxtBreak* GetTextBreak() const;
134   int32_t GetLineCount() const;
135   int32_t GetPageLineCount() const;
136 
137   int32_t Line2Parag(int32_t nStartParag,
138                      int32_t nStartLineofParag,
139                      int32_t nLineIndex,
140                      int32_t& nStartLine) const;
GetAliasChar()141   FX_WCHAR GetAliasChar() const { return m_wcAliasChar; }
142 
143  protected:
144   virtual ~CFDE_TxtEdtEngine();
145 
146  private:
147   void Inner_Insert(int32_t nStart, const FX_WCHAR* lpText, int32_t nLength);
148 #ifdef FDE_USEFORMATBLOCK
149   void RawInsert(int32_t nStart, const FX_WCHAR* lpText, int32_t nLength);
150 #endif
151   void GetPreDeleteText(CFX_WideString& wsText,
152                         int32_t nIndex,
153                         int32_t nLength);
154   void GetPreInsertText(CFX_WideString& wsText,
155                         int32_t nIndex,
156                         const FX_WCHAR* lpText,
157                         int32_t nLength);
158   void GetPreReplaceText(CFX_WideString& wsText,
159                          int32_t nIndex,
160                          int32_t nOriginLength,
161                          const FX_WCHAR* lpText,
162                          int32_t nLength);
163 
164   void Inner_DeleteRange(int32_t nStart, int32_t nCount = -1);
165   void DeleteRange_DoRecord(int32_t nStart,
166                             int32_t nCount,
167                             FX_BOOL bSel = FALSE);
168   void ResetEngine();
169   void RebuildParagraphs();
170   void RemoveAllParags();
171   void RemoveAllPages();
172   void UpdateParags();
173   void UpdatePages();
174   void UpdateTxtBreak();
175 
176   FX_BOOL ReplaceParagEnd(FX_WCHAR*& lpText,
177                           int32_t& nLength,
178                           FX_BOOL bPreIsCR = FALSE);
179   void RecoverParagEnd(CFX_WideString& wsText);
180   int32_t MovePage2Char(int32_t nIndex);
181   void TextPos2ParagPos(int32_t nIndex, FDE_TXTEDTPARAGPOS& ParagPos) const;
182   int32_t MoveForward(FX_BOOL& bBefore);
183   int32_t MoveBackward(FX_BOOL& bBefore);
184   FX_BOOL MoveUp(CFX_PointF& ptCaret);
185   FX_BOOL MoveDown(CFX_PointF& ptCaret);
186   FX_BOOL MoveLineStart();
187   FX_BOOL MoveLineEnd();
188   FX_BOOL MoveParagStart();
189   FX_BOOL MoveParagEnd();
190   FX_BOOL MoveHome();
191   FX_BOOL MoveEnd();
192   FX_BOOL IsFitArea(CFX_WideString& wsText);
193   void UpdateCaretRect(int32_t nIndex, FX_BOOL bBefore = TRUE);
194   void GetCaretRect(CFX_RectF& rtCaret,
195                     int32_t nPageIndex,
196                     int32_t nCaret,
197                     FX_BOOL bBefore = TRUE);
198   void UpdateCaretIndex(const CFX_PointF& ptCaret);
199 
200   FX_BOOL IsSelect();
201   void DeleteSelect();
202 
203   IFDE_TxtEdtBuf* m_pTxtBuf;
204   IFX_TxtBreak* m_pTextBreak;
205   FDE_TXTEDTPARAMS m_Param;
206   CFX_ArrayTemplate<IFDE_TxtEdtPage*> m_PagePtrArray;
207   CFX_ArrayTemplate<CFDE_TxtEdtParag*> m_ParagPtrArray;
208   CFX_ArrayTemplate<FDE_LPTXTEDTSELRANGE> m_SelRangePtrArr;
209   int32_t m_nPageLineCount;
210   int32_t m_nLineCount;
211   int32_t m_nAnchorPos;
212   int32_t m_nLayoutPos;
213   FX_FLOAT m_fCaretPosReserve;
214   int32_t m_nCaret;
215   FX_BOOL m_bBefore;
216   int32_t m_nCaretPage;
217   CFX_RectF m_rtCaret;
218   FX_DWORD m_dwFindFlags;
219   FX_BOOL m_bLock;
220   int32_t m_nLimit;
221   FX_WCHAR m_wcAliasChar;
222   int32_t m_nFirstLineEnd;
223   FX_BOOL m_bAutoLineEnd;
224   FX_WCHAR m_wLineEnd;
225   FDE_TXTEDT_TEXTCHANGE_INFO m_ChangeInfo;
226 };
227 class CFDE_TxtEdtDoRecord_Insert : public IFDE_TxtEdtDoRecord {
228  public:
229   CFDE_TxtEdtDoRecord_Insert(const CFX_ByteStringC& bsDoRecord);
230   CFDE_TxtEdtDoRecord_Insert(CFDE_TxtEdtEngine* pEngine,
231                              int32_t nCaret,
232                              const FX_WCHAR* lpText,
233                              int32_t nLength);
234   virtual void Release();
235   virtual FX_BOOL Undo();
236   virtual FX_BOOL Redo();
237   virtual void Serialize(CFX_ByteString& bsDoRecord) const;
238 
239  protected:
240   ~CFDE_TxtEdtDoRecord_Insert();
241   void Deserialize(const CFX_ByteStringC& bsDoRecord);
242 
243  private:
244   CFDE_TxtEdtEngine* m_pEngine;
245   int32_t m_nCaret;
246   CFX_WideString m_wsInsert;
247 };
248 class CFDE_TxtEdtDoRecord_DeleteRange : public IFDE_TxtEdtDoRecord {
249  public:
250   CFDE_TxtEdtDoRecord_DeleteRange(const CFX_ByteStringC& bsDoRecord);
251   CFDE_TxtEdtDoRecord_DeleteRange(CFDE_TxtEdtEngine* pEngine,
252                                   int32_t nIndex,
253                                   int32_t nCaret,
254                                   const CFX_WideString& wsRange,
255                                   FX_BOOL bSel = FALSE);
256   virtual void Release();
257   virtual FX_BOOL Undo();
258   virtual FX_BOOL Redo();
259   virtual void Serialize(CFX_ByteString& bsDoRecord) const;
260 
261  protected:
262   ~CFDE_TxtEdtDoRecord_DeleteRange();
263   void Deserialize(const CFX_ByteStringC& bsDoRecord);
264 
265  private:
266   CFDE_TxtEdtEngine* m_pEngine;
267   FX_BOOL m_bSel;
268   int32_t m_nIndex;
269   int32_t m_nCaret;
270   CFX_WideString m_wsRange;
271 };
272 #ifdef FDE_USEFORMATBLOCK
273 class CFDE_TxtEdtDoRecord_FieldInsert : public IFDE_TxtEdtDoRecord {
274  public:
275   CFDE_TxtEdtDoRecord_FieldInsert(const CFX_ByteStringC& bsDoRecord);
276   CFDE_TxtEdtDoRecord_FieldInsert(CFDE_TxtEdtEngine* pEngine,
277                                   int32_t nCaret,
278                                   CFDE_TxtEdtField* pField,
279                                   int32_t nIndexInField,
280                                   int32_t nFieldBgn,
281                                   int32_t nOldFieldLength,
282                                   int32_t nNewFieldLength,
283                                   const CFX_WideString& wsIns,
284                                   FX_BOOL bSel = FALSE);
285   virtual void Release();
286   virtual FX_BOOL Undo();
287   virtual FX_BOOL Redo();
288   virtual void Serialize(CFX_ByteString& bsDoRecord) const;
289 
290  protected:
291   ~CFDE_TxtEdtDoRecord_FieldInsert();
292   void Deserialize(const CFX_ByteStringC& bsDoRecord);
293 
294  private:
295   CFDE_TxtEdtEngine* m_pEngine;
296   int32_t m_nCaret;
297   CFDE_TxtEdtField* m_pField;
298   int32_t m_nIndexInField;
299   int32_t m_nFieldBgn;
300   int32_t m_nOldFieldLength;
301   int32_t m_nNewFieldLength;
302   CFX_WideString m_wsIns;
303   FX_BOOL m_bSel;
304 };
305 class CFDE_TxtEdtDoRecord_FieldDelete : public IFDE_TxtEdtDoRecord {
306  public:
307   CFDE_TxtEdtDoRecord_FieldDelete(const CFX_ByteStringC& bsDoRecord);
308   CFDE_TxtEdtDoRecord_FieldDelete(CFDE_TxtEdtEngine* pEngine,
309                                   int32_t nCaret,
310                                   CFDE_TxtEdtField* pField,
311                                   int32_t nIndexInField,
312                                   int32_t nFieldBgn,
313                                   int32_t nOldLength,
314                                   int32_t nNewLength,
315                                   const CFX_WideString& wsDel,
316                                   FX_BOOL bSel = FALSE);
317   virtual void Release();
318   virtual FX_BOOL Undo();
319   virtual FX_BOOL Redo();
320   virtual void Serialize(CFX_ByteString& bsDoRecord) const;
321 
322  protected:
323   ~CFDE_TxtEdtDoRecord_FieldDelete();
324   void Deserialize(const CFX_ByteStringC& bsDoRecord);
325 
326  private:
327   CFDE_TxtEdtEngine* m_pEngine;
328   int32_t m_nCaret;
329   CFDE_TxtEdtField* m_pField;
330   int32_t m_nIndexInField;
331   int32_t m_nFieldBgn;
332   int32_t m_nOldFieldLength;
333   int32_t m_nNewFieldLength;
334   CFX_WideString m_wsDel;
335   FX_BOOL m_bSel;
336 };
337 class CFDE_TxtEdtDoRecord_FieldReplace : public IFDE_TxtEdtDoRecord {
338  public:
339   CFDE_TxtEdtDoRecord_FieldReplace(const CFX_ByteStringC& bsDoRecord);
340   CFDE_TxtEdtDoRecord_FieldReplace(CFDE_TxtEdtEngine* pEngine,
341                                    int32_t nCaret,
342                                    int32_t nNewCaret,
343                                    CFDE_TxtEdtField* pField,
344                                    int32_t nIndexInField,
345                                    int32_t nFieldBgn,
346                                    int32_t nFieldNewLength,
347                                    const CFX_WideString& wsDel,
348                                    const CFX_WideString& wsIns,
349                                    FX_BOOL bSel);
350   virtual void Release();
351   virtual FX_BOOL Undo();
352   virtual FX_BOOL Redo();
353   virtual void Serialize(CFX_ByteString& bsDoRecord) const;
354 
355  protected:
356   ~CFDE_TxtEdtDoRecord_FieldReplace();
357   void Deserialize(const CFX_ByteStringC& bsDoRecord);
358 
359  private:
360   CFDE_TxtEdtEngine* m_pEngine;
361   int32_t m_nCaret;
362   int32_t m_nNewCaret;
363   CFDE_TxtEdtField* m_pField;
364   int32_t m_nIndexInField;
365   int32_t m_nFieldBgn;
366   int32_t m_nFieldNewLength;
367   CFX_WideString m_wsDel;
368   CFX_WideString m_wsIns;
369   FX_BOOL m_bSel;
370 };
371 #endif
372 #endif
373