1 // Copyright 2016 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 #include "xfa/fde/cfde_txtedtdorecord_insert.h"
8 
9 #include "xfa/fde/cfde_txtedtengine.h"
10 #include "xfa/fwl/cfwl_edit.h"
11 
CFDE_TxtEdtDoRecord_Insert(CFDE_TxtEdtEngine * pEngine,int32_t nCaret,const FX_WCHAR * lpText,int32_t nLength)12 CFDE_TxtEdtDoRecord_Insert::CFDE_TxtEdtDoRecord_Insert(
13     CFDE_TxtEdtEngine* pEngine,
14     int32_t nCaret,
15     const FX_WCHAR* lpText,
16     int32_t nLength)
17     : m_pEngine(pEngine), m_nCaret(nCaret) {
18   ASSERT(pEngine);
19   FX_WCHAR* lpBuffer = m_wsInsert.GetBuffer(nLength);
20   FXSYS_memcpy(lpBuffer, lpText, nLength * sizeof(FX_WCHAR));
21   m_wsInsert.ReleaseBuffer();
22 }
23 
~CFDE_TxtEdtDoRecord_Insert()24 CFDE_TxtEdtDoRecord_Insert::~CFDE_TxtEdtDoRecord_Insert() {}
25 
Undo() const26 bool CFDE_TxtEdtDoRecord_Insert::Undo() const {
27   if (m_pEngine->IsSelect())
28     m_pEngine->ClearSelection();
29 
30   m_pEngine->Inner_DeleteRange(m_nCaret, m_wsInsert.GetLength());
31   FDE_TXTEDTPARAMS& Param = m_pEngine->m_Param;
32   m_pEngine->m_ChangeInfo.nChangeType = FDE_TXTEDT_TEXTCHANGE_TYPE_Delete;
33   m_pEngine->m_ChangeInfo.wsDelete = m_wsInsert;
34   Param.pEventSink->OnTextChanged(m_pEngine->m_ChangeInfo);
35   m_pEngine->SetCaretPos(m_nCaret, true);
36   return true;
37 }
38 
Redo() const39 bool CFDE_TxtEdtDoRecord_Insert::Redo() const {
40   m_pEngine->Inner_Insert(m_nCaret, m_wsInsert.c_str(), m_wsInsert.GetLength());
41   FDE_TXTEDTPARAMS& Param = m_pEngine->m_Param;
42   m_pEngine->m_ChangeInfo.nChangeType = FDE_TXTEDT_TEXTCHANGE_TYPE_Insert;
43   m_pEngine->m_ChangeInfo.wsDelete = m_wsInsert;
44   Param.pEventSink->OnTextChanged(m_pEngine->m_ChangeInfo);
45   m_pEngine->SetCaretPos(m_nCaret, false);
46   return true;
47 }
48