blob: bb5725b42e7fd177713e271957d5a283ff5081a5 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclairc411eb92017-07-25 09:39:30 -04007#include "fpdfsdk/pwl/cpwl_edit_impl.h"
Lei Zhangc2fb35f2016-01-05 16:46:58 -08008
Lei Zhang375a8642016-01-11 11:59:17 -08009#include <algorithm>
thestigd4c34f22016-09-28 17:04:51 -070010#include <memory>
Henrique Nakashima2e2da132017-06-27 13:43:22 -040011#include <sstream>
thestigd4c34f22016-09-28 17:04:51 -070012#include <utility>
Lei Zhang375a8642016-01-11 11:59:17 -080013
dsinclairbc5e6d22016-10-04 11:08:49 -070014#include "core/fpdfapi/font/cpdf_font.h"
dsinclair41872fa2016-10-04 11:29:35 -070015#include "core/fpdfapi/page/cpdf_pageobject.h"
16#include "core/fpdfapi/page/cpdf_pageobjectholder.h"
17#include "core/fpdfapi/page/cpdf_pathobject.h"
18#include "core/fpdfapi/page/cpdf_textobject.h"
dsinclair488b7ad2016-10-04 11:55:50 -070019#include "core/fpdfapi/parser/fpdf_parser_decode.h"
dsinclair69d9c682016-10-04 12:18:35 -070020#include "core/fpdfapi/render/cpdf_renderoptions.h"
21#include "core/fpdfapi/render/cpdf_textrenderer.h"
dsinclair1727aee2016-09-29 13:12:56 -070022#include "core/fpdfdoc/cpvt_section.h"
23#include "core/fpdfdoc/cpvt_word.h"
24#include "core/fpdfdoc/ipvt_fontmap.h"
Dan Sinclairf51a02a2017-04-19 12:46:53 -040025#include "core/fxcrt/fx_codepage.h"
dsinclair74a34fc2016-09-29 16:41:42 -070026#include "core/fxge/cfx_graphstatedata.h"
27#include "core/fxge/cfx_pathdata.h"
28#include "core/fxge/cfx_renderdevice.h"
dsinclaire35af1e2016-07-13 11:26:20 -070029#include "fpdfsdk/cfx_systemhandler.h"
Dan Sinclairc411eb92017-07-25 09:39:30 -040030#include "fpdfsdk/pwl/cpwl_edit.h"
31#include "fpdfsdk/pwl/cpwl_edit_ctrl.h"
32#include "fpdfsdk/pwl/cpwl_scroll_bar.h"
tsepez36eb4bd2016-10-03 15:24:27 -070033#include "third_party/base/ptr_util.h"
Tom Sepez3509d162017-01-30 13:22:02 -080034#include "third_party/base/stl_util.h"
dsinclaire35af1e2016-07-13 11:26:20 -070035
dsinclair8f4bf9a2016-05-04 13:51:51 -070036namespace {
37
38const int kEditUndoMaxItems = 10000;
39
dsinclaire35af1e2016-07-13 11:26:20 -070040void DrawTextString(CFX_RenderDevice* pDevice,
Dan Sinclairf528eee2017-02-14 11:52:07 -050041 const CFX_PointF& pt,
dsinclaire35af1e2016-07-13 11:26:20 -070042 CPDF_Font* pFont,
Dan Sinclair05df0752017-03-14 14:43:42 -040043 float fFontSize,
dsinclaire35af1e2016-07-13 11:26:20 -070044 CFX_Matrix* pUser2Device,
45 const CFX_ByteString& str,
46 FX_ARGB crTextFill,
dsinclaire35af1e2016-07-13 11:26:20 -070047 int32_t nHorzScale) {
Dan Sinclaira0061af2017-02-23 09:25:17 -050048 CFX_PointF pos = pUser2Device->Transform(pt);
dsinclaire35af1e2016-07-13 11:26:20 -070049
50 if (pFont) {
51 if (nHorzScale != 100) {
52 CFX_Matrix mt(nHorzScale / 100.0f, 0, 0, 1, 0, 0);
53 mt.Concat(*pUser2Device);
54
55 CPDF_RenderOptions ro;
56 ro.m_Flags = RENDER_CLEARTYPE;
Dan Sinclairf55e72e2017-07-13 14:53:28 -040057 ro.m_ColorMode = CPDF_RenderOptions::kNormal;
dsinclaire35af1e2016-07-13 11:26:20 -070058
Dan Sinclaira0061af2017-02-23 09:25:17 -050059 CPDF_TextRenderer::DrawTextString(pDevice, pos.x, pos.y, pFont, fFontSize,
60 &mt, str, crTextFill, nullptr, &ro);
dsinclaire35af1e2016-07-13 11:26:20 -070061 } else {
62 CPDF_RenderOptions ro;
63 ro.m_Flags = RENDER_CLEARTYPE;
Dan Sinclairf55e72e2017-07-13 14:53:28 -040064 ro.m_ColorMode = CPDF_RenderOptions::kNormal;
dsinclaire35af1e2016-07-13 11:26:20 -070065
Dan Sinclaira0061af2017-02-23 09:25:17 -050066 CPDF_TextRenderer::DrawTextString(pDevice, pos.x, pos.y, pFont, fFontSize,
67 pUser2Device, str, crTextFill, nullptr,
68 &ro);
dsinclaire35af1e2016-07-13 11:26:20 -070069 }
70 }
71}
72
dsinclair8f4bf9a2016-05-04 13:51:51 -070073} // namespace
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070074
Dan Sinclair6b0158f2017-07-24 09:42:55 -040075CPWL_EditImpl_Iterator::CPWL_EditImpl_Iterator(
76 CPWL_EditImpl* pEdit,
77 CPDF_VariableText::Iterator* pVTIterator)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 : m_pEdit(pEdit), m_pVTIterator(pVTIterator) {}
79
Dan Sinclair6b0158f2017-07-24 09:42:55 -040080CPWL_EditImpl_Iterator::~CPWL_EditImpl_Iterator() {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081
Dan Sinclair6b0158f2017-07-24 09:42:55 -040082bool CPWL_EditImpl_Iterator::NextWord() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083 return m_pVTIterator->NextWord();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070084}
85
Dan Sinclair6b0158f2017-07-24 09:42:55 -040086bool CPWL_EditImpl_Iterator::PrevWord() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087 return m_pVTIterator->PrevWord();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088}
89
Dan Sinclair6b0158f2017-07-24 09:42:55 -040090bool CPWL_EditImpl_Iterator::GetWord(CPVT_Word& word) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 ASSERT(m_pEdit);
92
93 if (m_pVTIterator->GetWord(word)) {
94 word.ptWord = m_pEdit->VTToEdit(word.ptWord);
tsepez4cf55152016-11-02 14:37:54 -070095 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 }
tsepez4cf55152016-11-02 14:37:54 -070097 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070098}
99
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400100bool CPWL_EditImpl_Iterator::GetLine(CPVT_Line& line) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 ASSERT(m_pEdit);
102
103 if (m_pVTIterator->GetLine(line)) {
104 line.ptLine = m_pEdit->VTToEdit(line.ptLine);
tsepez4cf55152016-11-02 14:37:54 -0700105 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106 }
tsepez4cf55152016-11-02 14:37:54 -0700107 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700108}
109
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400110void CPWL_EditImpl_Iterator::SetAt(int32_t nWordIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111 m_pVTIterator->SetAt(nWordIndex);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700112}
113
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400114void CPWL_EditImpl_Iterator::SetAt(const CPVT_WordPlace& place) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700115 m_pVTIterator->SetAt(place);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700116}
117
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400118const CPVT_WordPlace& CPWL_EditImpl_Iterator::GetAt() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700119 return m_pVTIterator->GetAt();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700120}
121
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400122CPWL_EditImpl_Provider::CPWL_EditImpl_Provider(IPVT_FontMap* pFontMap)
dsinclairc7a73492016-04-05 12:01:42 -0700123 : CPDF_VariableText::Provider(pFontMap), m_pFontMap(pFontMap) {
Lei Zhang96660d62015-12-14 18:27:25 -0800124 ASSERT(m_pFontMap);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700125}
126
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400127CPWL_EditImpl_Provider::~CPWL_EditImpl_Provider() {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400129IPVT_FontMap* CPWL_EditImpl_Provider::GetFontMap() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 return m_pFontMap;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700131}
132
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400133int32_t CPWL_EditImpl_Provider::GetCharWidth(int32_t nFontIndex,
134 uint16_t word) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135 if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex)) {
tsepezc3255f52016-03-25 14:52:27 -0700136 uint32_t charcode = word;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700137
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138 if (pPDFFont->IsUnicodeCompatible())
139 charcode = pPDFFont->CharCodeFromUnicode(word);
140 else
141 charcode = m_pFontMap->CharCodeFromUnicode(nFontIndex, word);
142
Wei Li89409932016-03-28 10:33:33 -0700143 if (charcode != CPDF_Font::kInvalidCharCode)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700144 return pPDFFont->GetCharWidthF(charcode);
145 }
146
147 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700148}
149
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400150int32_t CPWL_EditImpl_Provider::GetTypeAscent(int32_t nFontIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151 if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex))
152 return pPDFFont->GetTypeAscent();
153
154 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700155}
156
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400157int32_t CPWL_EditImpl_Provider::GetTypeDescent(int32_t nFontIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158 if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex))
159 return pPDFFont->GetTypeDescent();
160
161 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700162}
163
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400164int32_t CPWL_EditImpl_Provider::GetWordFontIndex(uint16_t word,
165 int32_t charset,
166 int32_t nFontIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167 return m_pFontMap->GetWordFontIndex(word, charset, nFontIndex);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700168}
169
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400170int32_t CPWL_EditImpl_Provider::GetDefaultFontIndex() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700172}
173
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400174bool CPWL_EditImpl_Provider::IsLatinWord(uint16_t word) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175 return FX_EDIT_ISLATINWORD(word);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700176}
177
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400178CPWL_EditImpl_Refresh::CPWL_EditImpl_Refresh() {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700179
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400180CPWL_EditImpl_Refresh::~CPWL_EditImpl_Refresh() {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700181
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400182void CPWL_EditImpl_Refresh::BeginRefresh() {
Tom Sepez3509d162017-01-30 13:22:02 -0800183 m_RefreshRects.Clear();
184 m_OldLineRects = std::move(m_NewLineRects);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700185}
186
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400187void CPWL_EditImpl_Refresh::Push(const CPVT_WordRange& linerange,
188 const CFX_FloatRect& rect) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700189 m_NewLineRects.Add(linerange, rect);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700190}
191
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400192void CPWL_EditImpl_Refresh::NoAnalyse() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700193 {
194 for (int32_t i = 0, sz = m_OldLineRects.GetSize(); i < sz; i++)
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400195 if (CPWL_EditImpl_LineRect* pOldRect = m_OldLineRects.GetAt(i))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196 m_RefreshRects.Add(pOldRect->m_rcLine);
197 }
198
199 {
200 for (int32_t i = 0, sz = m_NewLineRects.GetSize(); i < sz; i++)
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400201 if (CPWL_EditImpl_LineRect* pNewRect = m_NewLineRects.GetAt(i))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202 m_RefreshRects.Add(pNewRect->m_rcLine);
203 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700204}
205
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400206const CPWL_EditImpl_RectArray* CPWL_EditImpl_Refresh::GetRefreshRects() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700207 return &m_RefreshRects;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700208}
209
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400210void CPWL_EditImpl_Refresh::EndRefresh() {
Tom Sepez3509d162017-01-30 13:22:02 -0800211 m_RefreshRects.Clear();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700212}
213
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400214CPWL_EditImpl_Undo::CPWL_EditImpl_Undo(int32_t nBufsize)
215 : m_nCurUndoPos(0), m_nBufSize(nBufsize), m_bWorking(false) {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400217CPWL_EditImpl_Undo::~CPWL_EditImpl_Undo() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218 Reset();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700219}
220
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400221bool CPWL_EditImpl_Undo::CanUndo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222 return m_nCurUndoPos > 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700223}
224
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400225void CPWL_EditImpl_Undo::Undo() {
tsepez4cf55152016-11-02 14:37:54 -0700226 m_bWorking = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227 if (m_nCurUndoPos > 0) {
Tom Sepez3509d162017-01-30 13:22:02 -0800228 m_UndoItemStack[m_nCurUndoPos - 1]->Undo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700229 m_nCurUndoPos--;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700230 }
tsepez4cf55152016-11-02 14:37:54 -0700231 m_bWorking = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700232}
233
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400234bool CPWL_EditImpl_Undo::CanRedo() const {
Tom Sepez3509d162017-01-30 13:22:02 -0800235 return m_nCurUndoPos < m_UndoItemStack.size();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700236}
237
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400238void CPWL_EditImpl_Undo::Redo() {
tsepez4cf55152016-11-02 14:37:54 -0700239 m_bWorking = true;
Tom Sepez3509d162017-01-30 13:22:02 -0800240 if (m_nCurUndoPos < m_UndoItemStack.size()) {
241 m_UndoItemStack[m_nCurUndoPos]->Redo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 m_nCurUndoPos++;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 }
tsepez4cf55152016-11-02 14:37:54 -0700244 m_bWorking = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700245}
246
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400247void CPWL_EditImpl_Undo::AddItem(std::unique_ptr<IFX_Edit_UndoItem> pItem) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248 ASSERT(!m_bWorking);
Lei Zhang96660d62015-12-14 18:27:25 -0800249 ASSERT(pItem);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700250 ASSERT(m_nBufSize > 1);
Tom Sepez3509d162017-01-30 13:22:02 -0800251 if (m_nCurUndoPos < m_UndoItemStack.size())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252 RemoveTails();
253
Lei Zhang1a89e362017-03-23 15:27:25 -0700254 if (m_UndoItemStack.size() >= m_nBufSize)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700255 RemoveHeads();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700256
Tom Sepez3509d162017-01-30 13:22:02 -0800257 m_UndoItemStack.push_back(std::move(pItem));
258 m_nCurUndoPos = m_UndoItemStack.size();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700259}
260
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400261void CPWL_EditImpl_Undo::RemoveHeads() {
Tom Sepez3509d162017-01-30 13:22:02 -0800262 ASSERT(m_UndoItemStack.size() > 1);
263 m_UndoItemStack.pop_front();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700264}
265
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400266void CPWL_EditImpl_Undo::RemoveTails() {
Tom Sepez3509d162017-01-30 13:22:02 -0800267 while (m_UndoItemStack.size() > m_nCurUndoPos)
268 m_UndoItemStack.pop_back();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269}
270
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400271void CPWL_EditImpl_Undo::Reset() {
Tom Sepez3509d162017-01-30 13:22:02 -0800272 m_UndoItemStack.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273 m_nCurUndoPos = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274}
275
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400276CFXEU_InsertWord::CFXEU_InsertWord(CPWL_EditImpl* pEdit,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277 const CPVT_WordPlace& wpOldPlace,
278 const CPVT_WordPlace& wpNewPlace,
Tom Sepez62a70f92016-03-21 15:00:20 -0700279 uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280 int32_t charset,
281 const CPVT_WordProps* pWordProps)
282 : m_pEdit(pEdit),
283 m_wpOld(wpOldPlace),
284 m_wpNew(wpNewPlace),
285 m_Word(word),
286 m_nCharset(charset),
287 m_WordProps() {
288 if (pWordProps)
289 m_WordProps = *pWordProps;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700290}
291
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700292CFXEU_InsertWord::~CFXEU_InsertWord() {}
293
294void CFXEU_InsertWord::Redo() {
295 if (m_pEdit) {
296 m_pEdit->SelectNone();
297 m_pEdit->SetCaret(m_wpOld);
tsepez4cf55152016-11-02 14:37:54 -0700298 m_pEdit->InsertWord(m_Word, m_nCharset, &m_WordProps, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700299 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700300}
301
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700302void CFXEU_InsertWord::Undo() {
303 if (m_pEdit) {
304 m_pEdit->SelectNone();
305 m_pEdit->SetCaret(m_wpNew);
tsepez4cf55152016-11-02 14:37:54 -0700306 m_pEdit->Backspace(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700307 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700308}
309
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400310CFXEU_InsertReturn::CFXEU_InsertReturn(CPWL_EditImpl* pEdit,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700311 const CPVT_WordPlace& wpOldPlace,
312 const CPVT_WordPlace& wpNewPlace,
313 const CPVT_SecProps* pSecProps,
314 const CPVT_WordProps* pWordProps)
315 : m_pEdit(pEdit),
316 m_wpOld(wpOldPlace),
317 m_wpNew(wpNewPlace),
318 m_SecProps(),
319 m_WordProps() {
320 if (pSecProps)
321 m_SecProps = *pSecProps;
322 if (pWordProps)
323 m_WordProps = *pWordProps;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700324}
325
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700326CFXEU_InsertReturn::~CFXEU_InsertReturn() {}
327
328void CFXEU_InsertReturn::Redo() {
329 if (m_pEdit) {
330 m_pEdit->SelectNone();
331 m_pEdit->SetCaret(m_wpOld);
tsepez4cf55152016-11-02 14:37:54 -0700332 m_pEdit->InsertReturn(&m_SecProps, &m_WordProps, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700333 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700334}
335
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336void CFXEU_InsertReturn::Undo() {
337 if (m_pEdit) {
338 m_pEdit->SelectNone();
339 m_pEdit->SetCaret(m_wpNew);
tsepez4cf55152016-11-02 14:37:54 -0700340 m_pEdit->Backspace(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700341 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700342}
343
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400344CFXEU_Backspace::CFXEU_Backspace(CPWL_EditImpl* pEdit,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700345 const CPVT_WordPlace& wpOldPlace,
346 const CPVT_WordPlace& wpNewPlace,
Tom Sepez62a70f92016-03-21 15:00:20 -0700347 uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700348 int32_t charset,
349 const CPVT_SecProps& SecProps,
350 const CPVT_WordProps& WordProps)
351 : m_pEdit(pEdit),
352 m_wpOld(wpOldPlace),
353 m_wpNew(wpNewPlace),
354 m_Word(word),
355 m_nCharset(charset),
356 m_SecProps(SecProps),
357 m_WordProps(WordProps) {}
358
359CFXEU_Backspace::~CFXEU_Backspace() {}
360
361void CFXEU_Backspace::Redo() {
Tom Sepez52f69b32017-03-21 13:42:38 -0700362 if (!m_pEdit)
363 return;
364
365 m_pEdit->SelectNone();
366 m_pEdit->SetCaret(m_wpOld);
367 m_pEdit->Backspace(false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700368}
369
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700370void CFXEU_Backspace::Undo() {
Tom Sepez52f69b32017-03-21 13:42:38 -0700371 if (!m_pEdit)
372 return;
373
374 m_pEdit->SelectNone();
375 m_pEdit->SetCaret(m_wpNew);
376 if (m_wpNew.nSecIndex != m_wpOld.nSecIndex)
377 m_pEdit->InsertReturn(&m_SecProps, &m_WordProps, false, true);
378 else
379 m_pEdit->InsertWord(m_Word, m_nCharset, &m_WordProps, false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700380}
381
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400382CFXEU_Delete::CFXEU_Delete(CPWL_EditImpl* pEdit,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700383 const CPVT_WordPlace& wpOldPlace,
384 const CPVT_WordPlace& wpNewPlace,
Tom Sepez62a70f92016-03-21 15:00:20 -0700385 uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700386 int32_t charset,
387 const CPVT_SecProps& SecProps,
388 const CPVT_WordProps& WordProps,
tsepez4cf55152016-11-02 14:37:54 -0700389 bool bSecEnd)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390 : m_pEdit(pEdit),
391 m_wpOld(wpOldPlace),
392 m_wpNew(wpNewPlace),
393 m_Word(word),
394 m_nCharset(charset),
395 m_SecProps(SecProps),
396 m_WordProps(WordProps),
397 m_bSecEnd(bSecEnd) {}
398
399CFXEU_Delete::~CFXEU_Delete() {}
400
401void CFXEU_Delete::Redo() {
402 if (m_pEdit) {
403 m_pEdit->SelectNone();
404 m_pEdit->SetCaret(m_wpOld);
tsepez4cf55152016-11-02 14:37:54 -0700405 m_pEdit->Delete(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700406 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700407}
408
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409void CFXEU_Delete::Undo() {
410 if (m_pEdit) {
411 m_pEdit->SelectNone();
412 m_pEdit->SetCaret(m_wpNew);
413 if (m_bSecEnd) {
tsepez4cf55152016-11-02 14:37:54 -0700414 m_pEdit->InsertReturn(&m_SecProps, &m_WordProps, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700415 } else {
tsepez4cf55152016-11-02 14:37:54 -0700416 m_pEdit->InsertWord(m_Word, m_nCharset, &m_WordProps, false, true);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700417 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700418 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700419}
420
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400421CFXEU_Clear::CFXEU_Clear(CPWL_EditImpl* pEdit,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700422 const CPVT_WordRange& wrSel,
423 const CFX_WideString& swText)
424 : m_pEdit(pEdit), m_wrSel(wrSel), m_swText(swText) {}
425
426CFXEU_Clear::~CFXEU_Clear() {}
427
428void CFXEU_Clear::Redo() {
429 if (m_pEdit) {
430 m_pEdit->SelectNone();
Diana Gage4d02e902017-07-20 17:20:31 -0700431 m_pEdit->SetSelection(m_wrSel.BeginPos, m_wrSel.EndPos);
tsepez4cf55152016-11-02 14:37:54 -0700432 m_pEdit->Clear(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700433 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700434}
435
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700436void CFXEU_Clear::Undo() {
437 if (m_pEdit) {
438 m_pEdit->SelectNone();
439 m_pEdit->SetCaret(m_wrSel.BeginPos);
Dan Sinclairf51a02a2017-04-19 12:46:53 -0400440 m_pEdit->InsertText(m_swText, FX_CHARSET_Default, false, true);
Diana Gage4d02e902017-07-20 17:20:31 -0700441 m_pEdit->SetSelection(m_wrSel.BeginPos, m_wrSel.EndPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700442 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700443}
444
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400445CFXEU_InsertText::CFXEU_InsertText(CPWL_EditImpl* pEdit,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700446 const CPVT_WordPlace& wpOldPlace,
447 const CPVT_WordPlace& wpNewPlace,
448 const CFX_WideString& swText,
dsinclairefd5a992016-07-18 10:04:07 -0700449 int32_t charset)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700450 : m_pEdit(pEdit),
451 m_wpOld(wpOldPlace),
452 m_wpNew(wpNewPlace),
453 m_swText(swText),
dsinclairefd5a992016-07-18 10:04:07 -0700454 m_nCharset(charset) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700455
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700456CFXEU_InsertText::~CFXEU_InsertText() {}
457
458void CFXEU_InsertText::Redo() {
Lei Zhangae9c5ca2017-08-12 07:15:14 -0700459 if (m_pEdit) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700460 m_pEdit->SelectNone();
461 m_pEdit->SetCaret(m_wpOld);
tsepez4cf55152016-11-02 14:37:54 -0700462 m_pEdit->InsertText(m_swText, m_nCharset, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700463 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700464}
465
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700466void CFXEU_InsertText::Undo() {
467 if (m_pEdit) {
468 m_pEdit->SelectNone();
Diana Gage4d02e902017-07-20 17:20:31 -0700469 m_pEdit->SetSelection(m_wpOld, m_wpNew);
tsepez4cf55152016-11-02 14:37:54 -0700470 m_pEdit->Clear(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700471 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700472}
473
dsinclaire35af1e2016-07-13 11:26:20 -0700474// static
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400475void CPWL_EditImpl::DrawEdit(CFX_RenderDevice* pDevice,
476 CFX_Matrix* pUser2Device,
477 CPWL_EditImpl* pEdit,
478 FX_COLORREF crTextFill,
479 const CFX_FloatRect& rcClip,
480 const CFX_PointF& ptOffset,
481 const CPVT_WordRange* pRange,
482 CFX_SystemHandler* pSystemHandler,
483 CFFL_FormFiller* pFFLData) {
dsinclaire35af1e2016-07-13 11:26:20 -0700484 const bool bContinuous =
485 pEdit->GetCharArray() == 0 && pEdit->GetCharSpace() <= 0.0f;
486 uint16_t SubWord = pEdit->GetPasswordChar();
Dan Sinclair05df0752017-03-14 14:43:42 -0400487 float fFontSize = pEdit->GetFontSize();
dsinclaire35af1e2016-07-13 11:26:20 -0700488 CPVT_WordRange wrSelect = pEdit->GetSelectWordRange();
489 int32_t nHorzScale = pEdit->GetHorzScale();
490
491 FX_COLORREF crCurFill = crTextFill;
492 FX_COLORREF crOldFill = crCurFill;
493
tsepez4cf55152016-11-02 14:37:54 -0700494 bool bSelect = false;
dsinclaire35af1e2016-07-13 11:26:20 -0700495 const FX_COLORREF crWhite = ArgbEncode(255, 255, 255, 255);
496 const FX_COLORREF crSelBK = ArgbEncode(255, 0, 51, 113);
497
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400498 std::ostringstream sTextBuf;
dsinclaire35af1e2016-07-13 11:26:20 -0700499 int32_t nFontIndex = -1;
Dan Sinclairf528eee2017-02-14 11:52:07 -0500500 CFX_PointF ptBT;
Tom Sepez1629f602017-04-21 14:11:26 -0700501 CFX_RenderDevice::StateRestorer restorer(pDevice);
dsinclaire35af1e2016-07-13 11:26:20 -0700502 if (!rcClip.IsEmpty()) {
503 CFX_FloatRect rcTemp = rcClip;
504 pUser2Device->TransformRect(rcTemp);
505 pDevice->SetClip_Rect(rcTemp.ToFxRect());
506 }
507
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400508 CPWL_EditImpl_Iterator* pIterator = pEdit->GetIterator();
dsinclaire35af1e2016-07-13 11:26:20 -0700509 if (IPVT_FontMap* pFontMap = pEdit->GetFontMap()) {
510 if (pRange)
511 pIterator->SetAt(pRange->BeginPos);
512 else
513 pIterator->SetAt(0);
514
515 CPVT_WordPlace oldplace;
516 while (pIterator->NextWord()) {
517 CPVT_WordPlace place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -0700518 if (pRange && place > pRange->EndPos)
dsinclaire35af1e2016-07-13 11:26:20 -0700519 break;
520
Tom Sepez52f69b32017-03-21 13:42:38 -0700521 if (!wrSelect.IsEmpty()) {
522 bSelect = place > wrSelect.BeginPos && place <= wrSelect.EndPos;
dsinclaire35af1e2016-07-13 11:26:20 -0700523 crCurFill = bSelect ? crWhite : crTextFill;
524 }
525 if (pSystemHandler && pSystemHandler->IsSelectionImplemented()) {
526 crCurFill = crTextFill;
527 crOldFill = crCurFill;
528 }
529 CPVT_Word word;
530 if (pIterator->GetWord(word)) {
531 if (bSelect) {
532 CPVT_Line line;
533 pIterator->GetLine(line);
534
535 if (pSystemHandler && pSystemHandler->IsSelectionImplemented()) {
536 CFX_FloatRect rc(word.ptWord.x, line.ptLine.y + line.fLineDescent,
537 word.ptWord.x + word.fWidth,
538 line.ptLine.y + line.fLineAscent);
539 rc.Intersect(rcClip);
540 pSystemHandler->OutputSelectedRect(pFFLData, rc);
541 } else {
542 CFX_PathData pathSelBK;
543 pathSelBK.AppendRect(
544 word.ptWord.x, line.ptLine.y + line.fLineDescent,
545 word.ptWord.x + word.fWidth, line.ptLine.y + line.fLineAscent);
546
547 pDevice->DrawPath(&pathSelBK, pUser2Device, nullptr, crSelBK, 0,
548 FXFILL_WINDING);
549 }
550 }
551
552 if (bContinuous) {
553 if (place.LineCmp(oldplace) != 0 || word.nFontIndex != nFontIndex ||
554 crOldFill != crCurFill) {
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400555 if (sTextBuf.tellp() > 0) {
dsinclaire35af1e2016-07-13 11:26:20 -0700556 DrawTextString(
Dan Sinclairf528eee2017-02-14 11:52:07 -0500557 pDevice, CFX_PointF(ptBT.x + ptOffset.x, ptBT.y + ptOffset.y),
dsinclaire35af1e2016-07-13 11:26:20 -0700558 pFontMap->GetPDFFont(nFontIndex), fFontSize, pUser2Device,
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400559 CFX_ByteString(sTextBuf), crOldFill, nHorzScale);
dsinclaire35af1e2016-07-13 11:26:20 -0700560
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400561 sTextBuf.str("");
dsinclaire35af1e2016-07-13 11:26:20 -0700562 }
563 nFontIndex = word.nFontIndex;
564 ptBT = word.ptWord;
565 crOldFill = crCurFill;
566 }
567
Dan Sinclairc08dc392017-07-24 08:57:35 -0400568 sTextBuf << pEdit->GetPDFWordString(word.nFontIndex, word.Word,
569 SubWord);
dsinclaire35af1e2016-07-13 11:26:20 -0700570 } else {
571 DrawTextString(
Dan Sinclairc08dc392017-07-24 08:57:35 -0400572 pDevice,
573 CFX_PointF(word.ptWord.x + ptOffset.x,
574 word.ptWord.y + ptOffset.y),
dsinclaire35af1e2016-07-13 11:26:20 -0700575 pFontMap->GetPDFFont(word.nFontIndex), fFontSize, pUser2Device,
Dan Sinclairc08dc392017-07-24 08:57:35 -0400576 pEdit->GetPDFWordString(word.nFontIndex, word.Word, SubWord),
Dan Sinclaira0061af2017-02-23 09:25:17 -0500577 crCurFill, nHorzScale);
dsinclaire35af1e2016-07-13 11:26:20 -0700578 }
579 oldplace = place;
580 }
581 }
582
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400583 if (sTextBuf.tellp() > 0) {
Dan Sinclaira0061af2017-02-23 09:25:17 -0500584 DrawTextString(pDevice,
585 CFX_PointF(ptBT.x + ptOffset.x, ptBT.y + ptOffset.y),
586 pFontMap->GetPDFFont(nFontIndex), fFontSize, pUser2Device,
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400587 CFX_ByteString(sTextBuf), crOldFill, nHorzScale);
dsinclaire35af1e2016-07-13 11:26:20 -0700588 }
589 }
dsinclaire35af1e2016-07-13 11:26:20 -0700590}
591
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400592CPWL_EditImpl::CPWL_EditImpl()
Lei Zhang5688d622017-08-12 07:04:19 -0700593 : m_pVT(pdfium::MakeUnique<CPDF_VariableText>()),
tsepez4cf55152016-11-02 14:37:54 -0700594 m_bEnableScroll(false),
dsinclair8f4bf9a2016-05-04 13:51:51 -0700595 m_Undo(kEditUndoMaxItems),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700596 m_nAlignment(0),
tsepez4cf55152016-11-02 14:37:54 -0700597 m_bNotifyFlag(false),
598 m_bEnableOverflow(false),
599 m_bEnableRefresh(true),
Lei Zhang5688d622017-08-12 07:04:19 -0700600 m_bEnableUndo(true) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700601
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400602CPWL_EditImpl::~CPWL_EditImpl() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700603
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400604void CPWL_EditImpl::Initialize() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700605 m_pVT->Initialize();
606 SetCaret(m_pVT->GetBeginWordPlace());
607 SetCaretOrigin();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700608}
609
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400610void CPWL_EditImpl::SetFontMap(IPVT_FontMap* pFontMap) {
611 m_pVTProvider = pdfium::MakeUnique<CPWL_EditImpl_Provider>(pFontMap);
thestig821d59e2016-05-11 12:59:22 -0700612 m_pVT->SetProvider(m_pVTProvider.get());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700613}
614
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400615void CPWL_EditImpl::SetNotify(CPWL_EditCtrl* pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700616 m_pNotify = pNotify;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700617}
618
Lei Zhang5688d622017-08-12 07:04:19 -0700619void CPWL_EditImpl::SetOperationNotify(CPWL_Edit* pOperationNotify) {
620 m_pOperationNotify = pOperationNotify;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700621}
622
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400623CPWL_EditImpl_Iterator* CPWL_EditImpl::GetIterator() {
tsepez36eb4bd2016-10-03 15:24:27 -0700624 if (!m_pIterator) {
625 m_pIterator =
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400626 pdfium::MakeUnique<CPWL_EditImpl_Iterator>(this, m_pVT->GetIterator());
tsepez36eb4bd2016-10-03 15:24:27 -0700627 }
thestig821d59e2016-05-11 12:59:22 -0700628 return m_pIterator.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700629}
630
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400631IPVT_FontMap* CPWL_EditImpl::GetFontMap() {
thestig821d59e2016-05-11 12:59:22 -0700632 return m_pVTProvider ? m_pVTProvider->GetFontMap() : nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700633}
634
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400635void CPWL_EditImpl::SetPlateRect(const CFX_FloatRect& rect) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700636 m_pVT->SetPlateRect(rect);
Dan Sinclairf528eee2017-02-14 11:52:07 -0500637 m_ptScrollPos = CFX_PointF(rect.left, rect.top);
dsinclairefd5a992016-07-18 10:04:07 -0700638 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700639}
640
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400641void CPWL_EditImpl::SetAlignmentH(int32_t nFormat, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700642 m_pVT->SetAlignment(nFormat);
643 if (bPaint)
644 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700645}
646
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400647void CPWL_EditImpl::SetAlignmentV(int32_t nFormat, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700648 m_nAlignment = nFormat;
649 if (bPaint)
650 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700651}
652
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400653void CPWL_EditImpl::SetPasswordChar(uint16_t wSubWord, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700654 m_pVT->SetPasswordChar(wSubWord);
655 if (bPaint)
656 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700657}
658
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400659void CPWL_EditImpl::SetLimitChar(int32_t nLimitChar) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700660 m_pVT->SetLimitChar(nLimitChar);
dsinclairefd5a992016-07-18 10:04:07 -0700661 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700662}
663
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400664void CPWL_EditImpl::SetCharArray(int32_t nCharArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700665 m_pVT->SetCharArray(nCharArray);
dsinclairefd5a992016-07-18 10:04:07 -0700666 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700667}
668
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400669void CPWL_EditImpl::SetCharSpace(float fCharSpace) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700670 m_pVT->SetCharSpace(fCharSpace);
dsinclairefd5a992016-07-18 10:04:07 -0700671 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700672}
673
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400674void CPWL_EditImpl::SetMultiLine(bool bMultiLine, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700675 m_pVT->SetMultiLine(bMultiLine);
676 if (bPaint)
677 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700678}
679
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400680void CPWL_EditImpl::SetAutoReturn(bool bAuto, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700681 m_pVT->SetAutoReturn(bAuto);
682 if (bPaint)
683 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700684}
685
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400686void CPWL_EditImpl::SetAutoFontSize(bool bAuto, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700687 m_pVT->SetAutoFontSize(bAuto);
688 if (bPaint)
689 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700690}
691
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400692void CPWL_EditImpl::SetFontSize(float fFontSize) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700693 m_pVT->SetFontSize(fFontSize);
dsinclairefd5a992016-07-18 10:04:07 -0700694 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700695}
696
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400697void CPWL_EditImpl::SetAutoScroll(bool bAuto, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700698 m_bEnableScroll = bAuto;
699 if (bPaint)
700 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700701}
702
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400703void CPWL_EditImpl::SetTextOverflow(bool bAllowed, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700704 m_bEnableOverflow = bAllowed;
705 if (bPaint)
706 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700707}
708
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400709void CPWL_EditImpl::SetSelection(int32_t nStartChar, int32_t nEndChar) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700710 if (m_pVT->IsValid()) {
711 if (nStartChar == 0 && nEndChar < 0) {
712 SelectAll();
713 } else if (nStartChar < 0) {
714 SelectNone();
715 } else {
716 if (nStartChar < nEndChar) {
Diana Gage4d02e902017-07-20 17:20:31 -0700717 SetSelection(m_pVT->WordIndexToWordPlace(nStartChar),
718 m_pVT->WordIndexToWordPlace(nEndChar));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700719 } else {
Diana Gage4d02e902017-07-20 17:20:31 -0700720 SetSelection(m_pVT->WordIndexToWordPlace(nEndChar),
721 m_pVT->WordIndexToWordPlace(nStartChar));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700722 }
723 }
724 }
725}
726
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400727void CPWL_EditImpl::SetSelection(const CPVT_WordPlace& begin,
728 const CPVT_WordPlace& end) {
Tom Sepez52f69b32017-03-21 13:42:38 -0700729 if (!m_pVT->IsValid())
730 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700731
Tom Sepez52f69b32017-03-21 13:42:38 -0700732 SelectNone();
733 m_SelState.Set(begin, end);
734 SetCaret(m_SelState.EndPos);
735 ScrollToCaret();
736 if (!m_SelState.IsEmpty())
737 Refresh();
738 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700739}
740
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400741void CPWL_EditImpl::GetSelection(int32_t& nStartChar, int32_t& nEndChar) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700742 nStartChar = -1;
743 nEndChar = -1;
Tom Sepez52f69b32017-03-21 13:42:38 -0700744 if (!m_pVT->IsValid())
745 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700746
Tom Sepez52f69b32017-03-21 13:42:38 -0700747 if (m_SelState.IsEmpty()) {
748 nStartChar = m_pVT->WordPlaceToWordIndex(m_wpCaret);
749 nEndChar = m_pVT->WordPlaceToWordIndex(m_wpCaret);
750 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700751 }
Tom Sepez52f69b32017-03-21 13:42:38 -0700752 if (m_SelState.BeginPos < m_SelState.EndPos) {
753 nStartChar = m_pVT->WordPlaceToWordIndex(m_SelState.BeginPos);
754 nEndChar = m_pVT->WordPlaceToWordIndex(m_SelState.EndPos);
755 return;
756 }
757 nStartChar = m_pVT->WordPlaceToWordIndex(m_SelState.EndPos);
758 nEndChar = m_pVT->WordPlaceToWordIndex(m_SelState.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700759}
760
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400761int32_t CPWL_EditImpl::GetCaret() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700762 if (m_pVT->IsValid())
763 return m_pVT->WordPlaceToWordIndex(m_wpCaret);
764
765 return -1;
766}
767
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400768CPVT_WordPlace CPWL_EditImpl::GetCaretWordPlace() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700769 return m_wpCaret;
770}
771
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400772CFX_WideString CPWL_EditImpl::GetText() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700773 CFX_WideString swRet;
thestig821d59e2016-05-11 12:59:22 -0700774 if (!m_pVT->IsValid())
775 return swRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700776
thestig821d59e2016-05-11 12:59:22 -0700777 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
778 pIterator->SetAt(0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700779
thestig821d59e2016-05-11 12:59:22 -0700780 CPVT_Word wordinfo;
781 CPVT_WordPlace oldplace = pIterator->GetAt();
782 while (pIterator->NextWord()) {
783 CPVT_WordPlace place = pIterator->GetAt();
thestig821d59e2016-05-11 12:59:22 -0700784 if (pIterator->GetWord(wordinfo))
785 swRet += wordinfo.Word;
Tom Sepez52f69b32017-03-21 13:42:38 -0700786 if (oldplace.nSecIndex != place.nSecIndex)
thestig821d59e2016-05-11 12:59:22 -0700787 swRet += L"\r\n";
thestig821d59e2016-05-11 12:59:22 -0700788 oldplace = place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700789 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700790 return swRet;
791}
792
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400793CFX_WideString CPWL_EditImpl::GetRangeText(const CPVT_WordRange& range) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700794 CFX_WideString swRet;
thestig821d59e2016-05-11 12:59:22 -0700795 if (!m_pVT->IsValid())
796 return swRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700797
thestig821d59e2016-05-11 12:59:22 -0700798 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
799 CPVT_WordRange wrTemp = range;
800 m_pVT->UpdateWordPlace(wrTemp.BeginPos);
801 m_pVT->UpdateWordPlace(wrTemp.EndPos);
802 pIterator->SetAt(wrTemp.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700803
thestig821d59e2016-05-11 12:59:22 -0700804 CPVT_Word wordinfo;
805 CPVT_WordPlace oldplace = wrTemp.BeginPos;
806 while (pIterator->NextWord()) {
807 CPVT_WordPlace place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -0700808 if (place > wrTemp.EndPos)
thestig821d59e2016-05-11 12:59:22 -0700809 break;
thestig821d59e2016-05-11 12:59:22 -0700810 if (pIterator->GetWord(wordinfo))
811 swRet += wordinfo.Word;
Tom Sepez52f69b32017-03-21 13:42:38 -0700812 if (oldplace.nSecIndex != place.nSecIndex)
thestig821d59e2016-05-11 12:59:22 -0700813 swRet += L"\r\n";
thestig821d59e2016-05-11 12:59:22 -0700814 oldplace = place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700815 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700816 return swRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700817}
818
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400819CFX_WideString CPWL_EditImpl::GetSelectedText() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700820 return GetRangeText(m_SelState.ConvertToWordRange());
821}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700822
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400823int32_t CPWL_EditImpl::GetTotalLines() const {
thestig821d59e2016-05-11 12:59:22 -0700824 int32_t nLines = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700825
thestig821d59e2016-05-11 12:59:22 -0700826 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
827 pIterator->SetAt(0);
828 while (pIterator->NextLine())
829 ++nLines;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700830
thestig821d59e2016-05-11 12:59:22 -0700831 return nLines;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700832}
833
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400834CPVT_WordRange CPWL_EditImpl::GetSelectWordRange() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700835 return m_SelState.ConvertToWordRange();
836}
837
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400838void CPWL_EditImpl::SetText(const CFX_WideString& sText) {
dsinclairefd5a992016-07-18 10:04:07 -0700839 Empty();
Dan Sinclairf51a02a2017-04-19 12:46:53 -0400840 DoInsertText(CPVT_WordPlace(0, 0, -1), sText, FX_CHARSET_Default);
dsinclairefd5a992016-07-18 10:04:07 -0700841 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700842}
843
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400844bool CPWL_EditImpl::InsertWord(uint16_t word, int32_t charset) {
tsepez4cf55152016-11-02 14:37:54 -0700845 return InsertWord(word, charset, nullptr, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700846}
847
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400848bool CPWL_EditImpl::InsertReturn() {
tsepez4cf55152016-11-02 14:37:54 -0700849 return InsertReturn(nullptr, nullptr, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700850}
851
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400852bool CPWL_EditImpl::Backspace() {
tsepez4cf55152016-11-02 14:37:54 -0700853 return Backspace(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700854}
855
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400856bool CPWL_EditImpl::Delete() {
tsepez4cf55152016-11-02 14:37:54 -0700857 return Delete(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700858}
859
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400860bool CPWL_EditImpl::ClearSelection() {
tsepez4cf55152016-11-02 14:37:54 -0700861 return Clear(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700862}
863
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400864bool CPWL_EditImpl::InsertText(const CFX_WideString& sText, int32_t charset) {
tsepez4cf55152016-11-02 14:37:54 -0700865 return InsertText(sText, charset, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700866}
867
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400868float CPWL_EditImpl::GetFontSize() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700869 return m_pVT->GetFontSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700870}
871
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400872uint16_t CPWL_EditImpl::GetPasswordChar() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700873 return m_pVT->GetPasswordChar();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700874}
875
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400876int32_t CPWL_EditImpl::GetCharArray() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700877 return m_pVT->GetCharArray();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700878}
879
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400880CFX_FloatRect CPWL_EditImpl::GetContentRect() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700881 return VTToEdit(m_pVT->GetContentRect());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700882}
883
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400884int32_t CPWL_EditImpl::GetHorzScale() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700885 return m_pVT->GetHorzScale();
886}
887
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400888float CPWL_EditImpl::GetCharSpace() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700889 return m_pVT->GetCharSpace();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700890}
891
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400892CPVT_WordRange CPWL_EditImpl::GetWholeWordRange() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700893 if (m_pVT->IsValid())
894 return CPVT_WordRange(m_pVT->GetBeginWordPlace(), m_pVT->GetEndWordPlace());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700895
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700896 return CPVT_WordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700897}
898
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400899CPVT_WordRange CPWL_EditImpl::GetVisibleWordRange() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700900 if (m_bEnableOverflow)
901 return GetWholeWordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700902
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700903 if (m_pVT->IsValid()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800904 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700905
Dan Sinclairf528eee2017-02-14 11:52:07 -0500906 CPVT_WordPlace place1 =
907 m_pVT->SearchWordPlace(EditToVT(CFX_PointF(rcPlate.left, rcPlate.top)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700908 CPVT_WordPlace place2 = m_pVT->SearchWordPlace(
Dan Sinclairf528eee2017-02-14 11:52:07 -0500909 EditToVT(CFX_PointF(rcPlate.right, rcPlate.bottom)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700910
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700911 return CPVT_WordRange(place1, place2);
912 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700913
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700914 return CPVT_WordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700915}
916
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400917CPVT_WordPlace CPWL_EditImpl::SearchWordPlace(const CFX_PointF& point) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700918 if (m_pVT->IsValid()) {
919 return m_pVT->SearchWordPlace(EditToVT(point));
920 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700921
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700922 return CPVT_WordPlace();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700923}
924
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400925void CPWL_EditImpl::Paint() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700926 if (m_pVT->IsValid()) {
927 RearrangeAll();
928 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -0700929 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700930 SetCaretOrigin();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700931 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700932 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700933}
934
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400935void CPWL_EditImpl::RearrangeAll() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700936 if (m_pVT->IsValid()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700937 m_pVT->UpdateWordPlace(m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700938 m_pVT->RearrangeAll();
939 m_pVT->UpdateWordPlace(m_wpCaret);
940 SetScrollInfo();
941 SetContentChanged();
942 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700943}
944
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400945void CPWL_EditImpl::RearrangePart(const CPVT_WordRange& range) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700946 if (m_pVT->IsValid()) {
947 m_pVT->UpdateWordPlace(m_wpCaret);
948 m_pVT->RearrangePart(range);
949 m_pVT->UpdateWordPlace(m_wpCaret);
950 SetScrollInfo();
951 SetContentChanged();
952 }
953}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700954
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400955void CPWL_EditImpl::SetContentChanged() {
dsinclaira2919b32016-07-13 10:55:48 -0700956 if (m_pNotify) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800957 CFX_FloatRect rcContent = m_pVT->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700958 if (rcContent.Width() != m_rcOldContent.Width() ||
959 rcContent.Height() != m_rcOldContent.Height()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700960 m_rcOldContent = rcContent;
961 }
962 }
963}
964
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400965void CPWL_EditImpl::SelectAll() {
Tom Sepez52f69b32017-03-21 13:42:38 -0700966 if (!m_pVT->IsValid())
967 return;
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400968 m_SelState = CPWL_EditImpl_Select(GetWholeWordRange());
Tom Sepez52f69b32017-03-21 13:42:38 -0700969 SetCaret(m_SelState.EndPos);
970 ScrollToCaret();
971 Refresh();
972 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700973}
974
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400975void CPWL_EditImpl::SelectNone() {
Tom Sepez52f69b32017-03-21 13:42:38 -0700976 if (!m_pVT->IsValid() || m_SelState.IsEmpty())
977 return;
978
979 m_SelState.Reset();
980 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700981}
982
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400983bool CPWL_EditImpl::IsSelected() const {
Tom Sepez52f69b32017-03-21 13:42:38 -0700984 return !m_SelState.IsEmpty();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700985}
986
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400987CFX_PointF CPWL_EditImpl::VTToEdit(const CFX_PointF& point) const {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800988 CFX_FloatRect rcContent = m_pVT->GetContentRect();
989 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700990
Dan Sinclair05df0752017-03-14 14:43:42 -0400991 float fPadding = 0.0f;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700992
993 switch (m_nAlignment) {
994 case 0:
995 fPadding = 0.0f;
996 break;
997 case 1:
998 fPadding = (rcPlate.Height() - rcContent.Height()) * 0.5f;
999 break;
1000 case 2:
1001 fPadding = rcPlate.Height() - rcContent.Height();
1002 break;
1003 }
1004
Dan Sinclairf528eee2017-02-14 11:52:07 -05001005 return CFX_PointF(point.x - (m_ptScrollPos.x - rcPlate.left),
1006 point.y - (m_ptScrollPos.y + fPadding - rcPlate.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001007}
1008
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001009CFX_PointF CPWL_EditImpl::EditToVT(const CFX_PointF& point) const {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001010 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1011 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001012
Dan Sinclair05df0752017-03-14 14:43:42 -04001013 float fPadding = 0.0f;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001014
1015 switch (m_nAlignment) {
1016 case 0:
1017 fPadding = 0.0f;
1018 break;
1019 case 1:
1020 fPadding = (rcPlate.Height() - rcContent.Height()) * 0.5f;
1021 break;
1022 case 2:
1023 fPadding = rcPlate.Height() - rcContent.Height();
1024 break;
1025 }
1026
Dan Sinclairf528eee2017-02-14 11:52:07 -05001027 return CFX_PointF(point.x + (m_ptScrollPos.x - rcPlate.left),
1028 point.y + (m_ptScrollPos.y + fPadding - rcPlate.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001029}
1030
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001031CFX_FloatRect CPWL_EditImpl::VTToEdit(const CFX_FloatRect& rect) const {
Dan Sinclairf528eee2017-02-14 11:52:07 -05001032 CFX_PointF ptLeftBottom = VTToEdit(CFX_PointF(rect.left, rect.bottom));
1033 CFX_PointF ptRightTop = VTToEdit(CFX_PointF(rect.right, rect.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001034
Tom Sepez281a9ea2016-02-26 14:24:28 -08001035 return CFX_FloatRect(ptLeftBottom.x, ptLeftBottom.y, ptRightTop.x,
1036 ptRightTop.y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001037}
1038
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001039void CPWL_EditImpl::SetScrollInfo() {
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001040 if (!m_pNotify)
1041 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001042
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001043 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
1044 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1045 if (m_bNotifyFlag)
1046 return;
1047
1048 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
1049 m_bNotifyFlag = true;
1050
1051 PWL_SCROLL_INFO Info;
1052 Info.fPlateWidth = rcPlate.top - rcPlate.bottom;
1053 Info.fContentMin = rcContent.bottom;
1054 Info.fContentMax = rcContent.top;
1055 Info.fSmallStep = rcPlate.Height() / 3;
1056 Info.fBigStep = rcPlate.Height();
1057 m_pNotify->SetScrollInfo(Info);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001058}
1059
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001060void CPWL_EditImpl::SetScrollPosX(float fx) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001061 if (!m_bEnableScroll)
1062 return;
1063
1064 if (m_pVT->IsValid()) {
dsinclair448c4332016-08-02 12:07:35 -07001065 if (!IsFloatEqual(m_ptScrollPos.x, fx)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001066 m_ptScrollPos.x = fx;
dsinclairefd5a992016-07-18 10:04:07 -07001067 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001068 }
1069 }
1070}
1071
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001072void CPWL_EditImpl::SetScrollPosY(float fy) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001073 if (!m_bEnableScroll)
1074 return;
1075
1076 if (m_pVT->IsValid()) {
dsinclair448c4332016-08-02 12:07:35 -07001077 if (!IsFloatEqual(m_ptScrollPos.y, fy)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001078 m_ptScrollPos.y = fy;
dsinclairefd5a992016-07-18 10:04:07 -07001079 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001080
dsinclaira2919b32016-07-13 10:55:48 -07001081 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001082 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001083 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001084 m_bNotifyFlag = true;
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001085 m_pNotify->SetScrollPosition(fy);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001086 }
1087 }
1088 }
1089 }
1090}
1091
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001092void CPWL_EditImpl::SetScrollPos(const CFX_PointF& point) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001093 SetScrollPosX(point.x);
1094 SetScrollPosY(point.y);
1095 SetScrollLimit();
1096 SetCaretInfo();
1097}
1098
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001099CFX_PointF CPWL_EditImpl::GetScrollPos() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001100 return m_ptScrollPos;
1101}
1102
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001103void CPWL_EditImpl::SetScrollLimit() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001104 if (m_pVT->IsValid()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001105 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1106 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001107
1108 if (rcPlate.Width() > rcContent.Width()) {
1109 SetScrollPosX(rcPlate.left);
1110 } else {
dsinclair448c4332016-08-02 12:07:35 -07001111 if (IsFloatSmaller(m_ptScrollPos.x, rcContent.left)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001112 SetScrollPosX(rcContent.left);
dsinclair448c4332016-08-02 12:07:35 -07001113 } else if (IsFloatBigger(m_ptScrollPos.x,
1114 rcContent.right - rcPlate.Width())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001115 SetScrollPosX(rcContent.right - rcPlate.Width());
1116 }
1117 }
1118
1119 if (rcPlate.Height() > rcContent.Height()) {
1120 SetScrollPosY(rcPlate.top);
1121 } else {
dsinclair448c4332016-08-02 12:07:35 -07001122 if (IsFloatSmaller(m_ptScrollPos.y,
1123 rcContent.bottom + rcPlate.Height())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001124 SetScrollPosY(rcContent.bottom + rcPlate.Height());
dsinclair448c4332016-08-02 12:07:35 -07001125 } else if (IsFloatBigger(m_ptScrollPos.y, rcContent.top)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001126 SetScrollPosY(rcContent.top);
1127 }
1128 }
1129 }
1130}
1131
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001132void CPWL_EditImpl::ScrollToCaret() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001133 SetScrollLimit();
1134
thestig821d59e2016-05-11 12:59:22 -07001135 if (!m_pVT->IsValid())
1136 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001137
thestig821d59e2016-05-11 12:59:22 -07001138 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1139 pIterator->SetAt(m_wpCaret);
1140
Dan Sinclairf528eee2017-02-14 11:52:07 -05001141 CFX_PointF ptHead;
1142 CFX_PointF ptFoot;
thestig821d59e2016-05-11 12:59:22 -07001143 CPVT_Word word;
1144 CPVT_Line line;
1145 if (pIterator->GetWord(word)) {
1146 ptHead.x = word.ptWord.x + word.fWidth;
1147 ptHead.y = word.ptWord.y + word.fAscent;
1148 ptFoot.x = word.ptWord.x + word.fWidth;
1149 ptFoot.y = word.ptWord.y + word.fDescent;
1150 } else if (pIterator->GetLine(line)) {
1151 ptHead.x = line.ptLine.x;
1152 ptHead.y = line.ptLine.y + line.fLineAscent;
1153 ptFoot.x = line.ptLine.x;
1154 ptFoot.y = line.ptLine.y + line.fLineDescent;
1155 }
1156
Dan Sinclairf528eee2017-02-14 11:52:07 -05001157 CFX_PointF ptHeadEdit = VTToEdit(ptHead);
1158 CFX_PointF ptFootEdit = VTToEdit(ptFoot);
thestig821d59e2016-05-11 12:59:22 -07001159 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
dsinclair448c4332016-08-02 12:07:35 -07001160 if (!IsFloatEqual(rcPlate.left, rcPlate.right)) {
1161 if (IsFloatSmaller(ptHeadEdit.x, rcPlate.left) ||
1162 IsFloatEqual(ptHeadEdit.x, rcPlate.left)) {
thestig821d59e2016-05-11 12:59:22 -07001163 SetScrollPosX(ptHead.x);
dsinclair448c4332016-08-02 12:07:35 -07001164 } else if (IsFloatBigger(ptHeadEdit.x, rcPlate.right)) {
thestig821d59e2016-05-11 12:59:22 -07001165 SetScrollPosX(ptHead.x - rcPlate.Width());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001166 }
thestig821d59e2016-05-11 12:59:22 -07001167 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001168
dsinclair448c4332016-08-02 12:07:35 -07001169 if (!IsFloatEqual(rcPlate.top, rcPlate.bottom)) {
1170 if (IsFloatSmaller(ptFootEdit.y, rcPlate.bottom) ||
1171 IsFloatEqual(ptFootEdit.y, rcPlate.bottom)) {
1172 if (IsFloatSmaller(ptHeadEdit.y, rcPlate.top)) {
thestig821d59e2016-05-11 12:59:22 -07001173 SetScrollPosY(ptFoot.y + rcPlate.Height());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001174 }
dsinclair448c4332016-08-02 12:07:35 -07001175 } else if (IsFloatBigger(ptHeadEdit.y, rcPlate.top)) {
1176 if (IsFloatBigger(ptFootEdit.y, rcPlate.bottom)) {
thestig821d59e2016-05-11 12:59:22 -07001177 SetScrollPosY(ptHead.y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001178 }
1179 }
1180 }
1181}
1182
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001183void CPWL_EditImpl::Refresh() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001184 if (m_bEnableRefresh && m_pVT->IsValid()) {
1185 m_Refresh.BeginRefresh();
1186 RefreshPushLineRects(GetVisibleWordRange());
1187
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001188 m_Refresh.NoAnalyse();
1189 m_ptRefreshScrollPos = m_ptScrollPos;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001190
dsinclaira2919b32016-07-13 10:55:48 -07001191 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001192 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001193 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001194 m_bNotifyFlag = true;
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001195 if (const CPWL_EditImpl_RectArray* pRects =
1196 m_Refresh.GetRefreshRects()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001197 for (int32_t i = 0, sz = pRects->GetSize(); i < sz; i++)
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001198 m_pNotify->InvalidateRect(pRects->GetAt(i));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001199 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001200 }
1201 }
1202
1203 m_Refresh.EndRefresh();
1204 }
1205}
1206
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001207void CPWL_EditImpl::RefreshPushLineRects(const CPVT_WordRange& wr) {
thestig821d59e2016-05-11 12:59:22 -07001208 if (!m_pVT->IsValid())
1209 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001210
thestig821d59e2016-05-11 12:59:22 -07001211 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1212 CPVT_WordPlace wpBegin = wr.BeginPos;
1213 m_pVT->UpdateWordPlace(wpBegin);
1214 CPVT_WordPlace wpEnd = wr.EndPos;
1215 m_pVT->UpdateWordPlace(wpEnd);
1216 pIterator->SetAt(wpBegin);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001217
thestig821d59e2016-05-11 12:59:22 -07001218 CPVT_Line lineinfo;
1219 do {
1220 if (!pIterator->GetLine(lineinfo))
1221 break;
1222 if (lineinfo.lineplace.LineCmp(wpEnd) > 0)
1223 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001224
thestig821d59e2016-05-11 12:59:22 -07001225 CFX_FloatRect rcLine(lineinfo.ptLine.x,
1226 lineinfo.ptLine.y + lineinfo.fLineDescent,
1227 lineinfo.ptLine.x + lineinfo.fLineWidth,
1228 lineinfo.ptLine.y + lineinfo.fLineAscent);
1229
1230 m_Refresh.Push(CPVT_WordRange(lineinfo.lineplace, lineinfo.lineEnd),
1231 VTToEdit(rcLine));
1232 } while (pIterator->NextLine());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001233}
1234
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001235void CPWL_EditImpl::RefreshWordRange(const CPVT_WordRange& wr) {
thestig821d59e2016-05-11 12:59:22 -07001236 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1237 CPVT_WordRange wrTemp = wr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001238
thestig821d59e2016-05-11 12:59:22 -07001239 m_pVT->UpdateWordPlace(wrTemp.BeginPos);
1240 m_pVT->UpdateWordPlace(wrTemp.EndPos);
1241 pIterator->SetAt(wrTemp.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001242
thestig821d59e2016-05-11 12:59:22 -07001243 CPVT_Word wordinfo;
1244 CPVT_Line lineinfo;
1245 CPVT_WordPlace place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001246
thestig821d59e2016-05-11 12:59:22 -07001247 while (pIterator->NextWord()) {
1248 place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -07001249 if (place > wrTemp.EndPos)
thestig821d59e2016-05-11 12:59:22 -07001250 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001251
thestig821d59e2016-05-11 12:59:22 -07001252 pIterator->GetWord(wordinfo);
1253 pIterator->GetLine(lineinfo);
thestig821d59e2016-05-11 12:59:22 -07001254 if (place.LineCmp(wrTemp.BeginPos) == 0 ||
1255 place.LineCmp(wrTemp.EndPos) == 0) {
1256 CFX_FloatRect rcWord(wordinfo.ptWord.x,
1257 lineinfo.ptLine.y + lineinfo.fLineDescent,
1258 wordinfo.ptWord.x + wordinfo.fWidth,
1259 lineinfo.ptLine.y + lineinfo.fLineAscent);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001260
dsinclaira2919b32016-07-13 10:55:48 -07001261 if (m_pNotify) {
thestig821d59e2016-05-11 12:59:22 -07001262 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001263 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001264 m_bNotifyFlag = true;
thestig821d59e2016-05-11 12:59:22 -07001265 CFX_FloatRect rcRefresh = VTToEdit(rcWord);
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001266 m_pNotify->InvalidateRect(&rcRefresh);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001267 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001268 }
thestig821d59e2016-05-11 12:59:22 -07001269 } else {
1270 CFX_FloatRect rcLine(lineinfo.ptLine.x,
1271 lineinfo.ptLine.y + lineinfo.fLineDescent,
1272 lineinfo.ptLine.x + lineinfo.fLineWidth,
1273 lineinfo.ptLine.y + lineinfo.fLineAscent);
1274
dsinclaira2919b32016-07-13 10:55:48 -07001275 if (m_pNotify) {
thestig821d59e2016-05-11 12:59:22 -07001276 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001277 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001278 m_bNotifyFlag = true;
thestig821d59e2016-05-11 12:59:22 -07001279 CFX_FloatRect rcRefresh = VTToEdit(rcLine);
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001280 m_pNotify->InvalidateRect(&rcRefresh);
thestig821d59e2016-05-11 12:59:22 -07001281 }
1282 }
1283
1284 pIterator->NextLine();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001285 }
1286 }
1287}
1288
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001289void CPWL_EditImpl::SetCaret(const CPVT_WordPlace& place) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001290 m_wpOldCaret = m_wpCaret;
1291 m_wpCaret = place;
1292}
1293
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001294void CPWL_EditImpl::SetCaretInfo() {
dsinclaira2919b32016-07-13 10:55:48 -07001295 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001296 if (!m_bNotifyFlag) {
thestig821d59e2016-05-11 12:59:22 -07001297 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1298 pIterator->SetAt(m_wpCaret);
tsepez63f545c2016-09-13 16:08:49 -07001299
Dan Sinclairf528eee2017-02-14 11:52:07 -05001300 CFX_PointF ptHead;
1301 CFX_PointF ptFoot;
thestig821d59e2016-05-11 12:59:22 -07001302 CPVT_Word word;
1303 CPVT_Line line;
1304 if (pIterator->GetWord(word)) {
1305 ptHead.x = word.ptWord.x + word.fWidth;
1306 ptHead.y = word.ptWord.y + word.fAscent;
1307 ptFoot.x = word.ptWord.x + word.fWidth;
1308 ptFoot.y = word.ptWord.y + word.fDescent;
1309 } else if (pIterator->GetLine(line)) {
1310 ptHead.x = line.ptLine.x;
1311 ptHead.y = line.ptLine.y + line.fLineAscent;
1312 ptFoot.x = line.ptLine.x;
1313 ptFoot.y = line.ptLine.y + line.fLineDescent;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001314 }
1315
Lei Zhanga8c2b912017-03-22 17:41:02 -07001316 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001317 m_bNotifyFlag = true;
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001318 m_pNotify->SetCaret(m_SelState.IsEmpty(), VTToEdit(ptHead),
1319 VTToEdit(ptFoot));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001320 }
1321 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001322}
1323
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001324void CPWL_EditImpl::OnMouseDown(const CFX_PointF& point,
1325 bool bShift,
1326 bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001327 if (!m_pVT->IsValid())
1328 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001329
Tom Sepez52f69b32017-03-21 13:42:38 -07001330 SelectNone();
1331 SetCaret(m_pVT->SearchWordPlace(EditToVT(point)));
1332 m_SelState.Set(m_wpCaret, m_wpCaret);
1333 ScrollToCaret();
1334 SetCaretOrigin();
1335 SetCaretInfo();
1336}
1337
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001338void CPWL_EditImpl::OnMouseMove(const CFX_PointF& point,
1339 bool bShift,
1340 bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001341 if (!m_pVT->IsValid())
1342 return;
1343
1344 SetCaret(m_pVT->SearchWordPlace(EditToVT(point)));
1345 if (m_wpCaret == m_wpOldCaret)
1346 return;
1347
1348 m_SelState.SetEndPos(m_wpCaret);
1349 ScrollToCaret();
1350 Refresh();
1351 SetCaretOrigin();
1352 SetCaretInfo();
1353}
1354
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001355void CPWL_EditImpl::OnVK_UP(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001356 if (!m_pVT->IsValid())
1357 return;
1358
1359 SetCaret(m_pVT->GetUpWordPlace(m_wpCaret, m_ptCaret));
1360 if (bShift) {
1361 if (m_SelState.IsEmpty())
1362 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1363 else
1364 m_SelState.SetEndPos(m_wpCaret);
1365
1366 if (m_wpOldCaret != m_wpCaret) {
1367 ScrollToCaret();
1368 Refresh();
1369 SetCaretInfo();
1370 }
1371 } else {
1372 SelectNone();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001373 ScrollToCaret();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001374 SetCaretInfo();
1375 }
1376}
1377
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001378void CPWL_EditImpl::OnVK_DOWN(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001379 if (!m_pVT->IsValid())
1380 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001381
Tom Sepez52f69b32017-03-21 13:42:38 -07001382 SetCaret(m_pVT->GetDownWordPlace(m_wpCaret, m_ptCaret));
1383 if (bShift) {
1384 if (m_SelState.IsEmpty())
1385 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1386 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001387 m_SelState.SetEndPos(m_wpCaret);
1388
Tom Sepez52f69b32017-03-21 13:42:38 -07001389 if (m_wpOldCaret != m_wpCaret) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001390 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001391 Refresh();
Tom Sepez52f69b32017-03-21 13:42:38 -07001392 SetCaretInfo();
1393 }
1394 } else {
1395 SelectNone();
1396 ScrollToCaret();
1397 SetCaretInfo();
1398 }
1399}
1400
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001401void CPWL_EditImpl::OnVK_LEFT(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001402 if (!m_pVT->IsValid())
1403 return;
1404
1405 if (bShift) {
1406 if (m_wpCaret == m_pVT->GetLineBeginPlace(m_wpCaret) &&
1407 m_wpCaret != m_pVT->GetSectionBeginPlace(m_wpCaret)) {
1408 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1409 }
1410 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1411 if (m_SelState.IsEmpty())
1412 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1413 else
1414 m_SelState.SetEndPos(m_wpCaret);
1415
1416 if (m_wpOldCaret != m_wpCaret) {
1417 ScrollToCaret();
1418 Refresh();
1419 SetCaretInfo();
1420 }
1421 } else {
1422 if (!m_SelState.IsEmpty()) {
1423 if (m_SelState.BeginPos < m_SelState.EndPos)
1424 SetCaret(m_SelState.BeginPos);
1425 else
1426 SetCaret(m_SelState.EndPos);
1427
1428 SelectNone();
1429 ScrollToCaret();
1430 SetCaretInfo();
1431 } else {
1432 if (m_wpCaret == m_pVT->GetLineBeginPlace(m_wpCaret) &&
1433 m_wpCaret != m_pVT->GetSectionBeginPlace(m_wpCaret)) {
1434 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1435 }
1436 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1437 ScrollToCaret();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001438 SetCaretOrigin();
1439 SetCaretInfo();
1440 }
1441 }
1442}
1443
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001444void CPWL_EditImpl::OnVK_RIGHT(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001445 if (!m_pVT->IsValid())
1446 return;
1447
1448 if (bShift) {
1449 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1450 if (m_wpCaret == m_pVT->GetLineEndPlace(m_wpCaret) &&
1451 m_wpCaret != m_pVT->GetSectionEndPlace(m_wpCaret))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001452 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1453
Tom Sepez52f69b32017-03-21 13:42:38 -07001454 if (m_SelState.IsEmpty())
1455 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1456 else
1457 m_SelState.SetEndPos(m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001458
Tom Sepez52f69b32017-03-21 13:42:38 -07001459 if (m_wpOldCaret != m_wpCaret) {
1460 ScrollToCaret();
1461 Refresh();
1462 SetCaretInfo();
1463 }
1464 } else {
1465 if (!m_SelState.IsEmpty()) {
1466 if (m_SelState.BeginPos > m_SelState.EndPos)
1467 SetCaret(m_SelState.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001468 else
Tom Sepez52f69b32017-03-21 13:42:38 -07001469 SetCaret(m_SelState.EndPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001470
Tom Sepez52f69b32017-03-21 13:42:38 -07001471 SelectNone();
1472 ScrollToCaret();
1473 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001474 } else {
Tom Sepez52f69b32017-03-21 13:42:38 -07001475 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1476 if (m_wpCaret == m_pVT->GetLineEndPlace(m_wpCaret) &&
1477 m_wpCaret != m_pVT->GetSectionEndPlace(m_wpCaret)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001478 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001479 }
Tom Sepez52f69b32017-03-21 13:42:38 -07001480 ScrollToCaret();
1481 SetCaretOrigin();
1482 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001483 }
1484 }
1485}
1486
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001487void CPWL_EditImpl::OnVK_HOME(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001488 if (!m_pVT->IsValid())
1489 return;
1490
1491 if (bShift) {
1492 if (bCtrl)
1493 SetCaret(m_pVT->GetBeginWordPlace());
1494 else
1495 SetCaret(m_pVT->GetLineBeginPlace(m_wpCaret));
1496
1497 if (m_SelState.IsEmpty())
1498 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1499 else
1500 m_SelState.SetEndPos(m_wpCaret);
1501
1502 ScrollToCaret();
1503 Refresh();
1504 SetCaretInfo();
1505 } else {
1506 if (!m_SelState.IsEmpty()) {
1507 SetCaret(std::min(m_SelState.BeginPos, m_SelState.EndPos));
1508 SelectNone();
1509 ScrollToCaret();
1510 SetCaretInfo();
1511 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001512 if (bCtrl)
1513 SetCaret(m_pVT->GetBeginWordPlace());
1514 else
1515 SetCaret(m_pVT->GetLineBeginPlace(m_wpCaret));
1516
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001517 ScrollToCaret();
Tom Sepez52f69b32017-03-21 13:42:38 -07001518 SetCaretOrigin();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001519 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001520 }
1521 }
1522}
1523
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001524void CPWL_EditImpl::OnVK_END(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001525 if (!m_pVT->IsValid())
1526 return;
1527
1528 if (bShift) {
1529 if (bCtrl)
1530 SetCaret(m_pVT->GetEndWordPlace());
1531 else
1532 SetCaret(m_pVT->GetLineEndPlace(m_wpCaret));
1533
1534 if (m_SelState.IsEmpty())
1535 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1536 else
1537 m_SelState.SetEndPos(m_wpCaret);
1538
1539 ScrollToCaret();
1540 Refresh();
1541 SetCaretInfo();
1542 } else {
1543 if (!m_SelState.IsEmpty()) {
1544 SetCaret(std::max(m_SelState.BeginPos, m_SelState.EndPos));
1545 SelectNone();
1546 ScrollToCaret();
1547 SetCaretInfo();
1548 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001549 if (bCtrl)
1550 SetCaret(m_pVT->GetEndWordPlace());
1551 else
1552 SetCaret(m_pVT->GetLineEndPlace(m_wpCaret));
1553
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001554 ScrollToCaret();
Tom Sepez52f69b32017-03-21 13:42:38 -07001555 SetCaretOrigin();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001556 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001557 }
1558 }
1559}
1560
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001561bool CPWL_EditImpl::InsertWord(uint16_t word,
1562 int32_t charset,
1563 const CPVT_WordProps* pWordProps,
1564 bool bAddUndo,
1565 bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001566 if (IsTextOverflow() || !m_pVT->IsValid())
tsepez4cf55152016-11-02 14:37:54 -07001567 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001568
Tom Sepez3509d162017-01-30 13:22:02 -08001569 m_pVT->UpdateWordPlace(m_wpCaret);
1570 SetCaret(m_pVT->InsertWord(m_wpCaret, word,
1571 GetCharSetFromUnicode(word, charset), pWordProps));
1572 m_SelState.Set(m_wpCaret, m_wpCaret);
1573 if (m_wpCaret == m_wpOldCaret)
1574 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001575
Tom Sepez3509d162017-01-30 13:22:02 -08001576 if (bAddUndo && m_bEnableUndo) {
1577 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertWord>(
1578 this, m_wpOldCaret, m_wpCaret, word, charset, pWordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001579 }
Tom Sepez3509d162017-01-30 13:22:02 -08001580 if (bPaint)
1581 PaintInsertText(m_wpOldCaret, m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001582
Lei Zhang5688d622017-08-12 07:04:19 -07001583 if (m_pOperationNotify)
1584 m_pOperationNotify->OnInsertWord(m_wpCaret, m_wpOldCaret);
Tom Sepez3509d162017-01-30 13:22:02 -08001585
1586 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001587}
1588
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001589bool CPWL_EditImpl::InsertReturn(const CPVT_SecProps* pSecProps,
1590 const CPVT_WordProps* pWordProps,
1591 bool bAddUndo,
1592 bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001593 if (IsTextOverflow() || !m_pVT->IsValid())
tsepez4cf55152016-11-02 14:37:54 -07001594 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001595
Tom Sepez3509d162017-01-30 13:22:02 -08001596 m_pVT->UpdateWordPlace(m_wpCaret);
1597 SetCaret(m_pVT->InsertSection(m_wpCaret, pSecProps, pWordProps));
1598 m_SelState.Set(m_wpCaret, m_wpCaret);
1599 if (m_wpCaret == m_wpOldCaret)
1600 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001601
Tom Sepez3509d162017-01-30 13:22:02 -08001602 if (bAddUndo && m_bEnableUndo) {
1603 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertReturn>(
1604 this, m_wpOldCaret, m_wpCaret, pSecProps, pWordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001605 }
Tom Sepez3509d162017-01-30 13:22:02 -08001606 if (bPaint) {
1607 RearrangePart(CPVT_WordRange(m_wpOldCaret, m_wpCaret));
1608 ScrollToCaret();
1609 Refresh();
1610 SetCaretOrigin();
1611 SetCaretInfo();
1612 }
Lei Zhang5688d622017-08-12 07:04:19 -07001613 if (m_pOperationNotify)
1614 m_pOperationNotify->OnInsertReturn(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001615
Tom Sepez3509d162017-01-30 13:22:02 -08001616 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001617}
1618
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001619bool CPWL_EditImpl::Backspace(bool bAddUndo, bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001620 if (!m_pVT->IsValid() || m_wpCaret == m_pVT->GetBeginWordPlace())
1621 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001622
Tom Sepez3509d162017-01-30 13:22:02 -08001623 CPVT_Section section;
1624 CPVT_Word word;
1625 if (bAddUndo) {
1626 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1627 pIterator->SetAt(m_wpCaret);
1628 pIterator->GetSection(section);
1629 pIterator->GetWord(word);
1630 }
1631 m_pVT->UpdateWordPlace(m_wpCaret);
1632 SetCaret(m_pVT->BackSpaceWord(m_wpCaret));
1633 m_SelState.Set(m_wpCaret, m_wpCaret);
1634 if (m_wpCaret == m_wpOldCaret)
1635 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001636
Tom Sepez3509d162017-01-30 13:22:02 -08001637 if (bAddUndo && m_bEnableUndo) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001638 if (m_wpCaret.nSecIndex != m_wpOldCaret.nSecIndex) {
Tom Sepez3509d162017-01-30 13:22:02 -08001639 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Backspace>(
1640 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1641 section.SecProps, section.WordProps));
1642 } else {
1643 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Backspace>(
1644 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1645 section.SecProps, word.WordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001646 }
1647 }
Tom Sepez3509d162017-01-30 13:22:02 -08001648 if (bPaint) {
1649 RearrangePart(CPVT_WordRange(m_wpCaret, m_wpOldCaret));
1650 ScrollToCaret();
1651 Refresh();
1652 SetCaretOrigin();
1653 SetCaretInfo();
1654 }
Lei Zhang5688d622017-08-12 07:04:19 -07001655 if (m_pOperationNotify)
1656 m_pOperationNotify->OnBackSpace(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001657
Tom Sepez3509d162017-01-30 13:22:02 -08001658 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001659}
1660
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001661bool CPWL_EditImpl::Delete(bool bAddUndo, bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001662 if (!m_pVT->IsValid() || m_wpCaret == m_pVT->GetEndWordPlace())
1663 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001664
Tom Sepez3509d162017-01-30 13:22:02 -08001665 CPVT_Section section;
1666 CPVT_Word word;
1667 if (bAddUndo) {
1668 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1669 pIterator->SetAt(m_pVT->GetNextWordPlace(m_wpCaret));
1670 pIterator->GetSection(section);
1671 pIterator->GetWord(word);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001672 }
Tom Sepez3509d162017-01-30 13:22:02 -08001673 m_pVT->UpdateWordPlace(m_wpCaret);
1674 bool bSecEnd = (m_wpCaret == m_pVT->GetSectionEndPlace(m_wpCaret));
1675 SetCaret(m_pVT->DeleteWord(m_wpCaret));
1676 m_SelState.Set(m_wpCaret, m_wpCaret);
1677 if (bAddUndo && m_bEnableUndo) {
1678 if (bSecEnd) {
1679 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Delete>(
1680 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1681 section.SecProps, section.WordProps, bSecEnd));
1682 } else {
1683 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Delete>(
1684 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1685 section.SecProps, word.WordProps, bSecEnd));
1686 }
1687 }
1688 if (bPaint) {
1689 RearrangePart(CPVT_WordRange(m_wpOldCaret, m_wpCaret));
1690 ScrollToCaret();
1691 Refresh();
1692 SetCaretOrigin();
1693 SetCaretInfo();
1694 }
Lei Zhang5688d622017-08-12 07:04:19 -07001695 if (m_pOperationNotify)
1696 m_pOperationNotify->OnDelete(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001697
Tom Sepez3509d162017-01-30 13:22:02 -08001698 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001699}
1700
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001701bool CPWL_EditImpl::Empty() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001702 if (m_pVT->IsValid()) {
1703 m_pVT->DeleteWords(GetWholeWordRange());
1704 SetCaret(m_pVT->GetBeginWordPlace());
1705
tsepez4cf55152016-11-02 14:37:54 -07001706 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001707 }
1708
tsepez4cf55152016-11-02 14:37:54 -07001709 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001710}
1711
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001712bool CPWL_EditImpl::Clear(bool bAddUndo, bool bPaint) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001713 if (!m_pVT->IsValid() || m_SelState.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001714 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001715
thestig821d59e2016-05-11 12:59:22 -07001716 CPVT_WordRange range = m_SelState.ConvertToWordRange();
Diana Gage22bf7a52017-07-21 11:33:18 -07001717 if (bAddUndo && m_bEnableUndo) {
Diana Gage89e65622017-07-20 18:09:31 -07001718 AddEditUndoItem(
1719 pdfium::MakeUnique<CFXEU_Clear>(this, range, GetSelectedText()));
Diana Gage22bf7a52017-07-21 11:33:18 -07001720 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001721
thestig821d59e2016-05-11 12:59:22 -07001722 SelectNone();
1723 SetCaret(m_pVT->DeleteWords(range));
1724 m_SelState.Set(m_wpCaret, m_wpCaret);
thestig821d59e2016-05-11 12:59:22 -07001725 if (bPaint) {
1726 RearrangePart(range);
1727 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001728 Refresh();
thestig821d59e2016-05-11 12:59:22 -07001729 SetCaretOrigin();
1730 SetCaretInfo();
1731 }
Lei Zhang5688d622017-08-12 07:04:19 -07001732 if (m_pOperationNotify)
1733 m_pOperationNotify->OnClear(m_wpCaret, m_wpOldCaret);
thestig821d59e2016-05-11 12:59:22 -07001734
tsepez4cf55152016-11-02 14:37:54 -07001735 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001736}
1737
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001738bool CPWL_EditImpl::InsertText(const CFX_WideString& sText,
1739 int32_t charset,
1740 bool bAddUndo,
1741 bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001742 if (IsTextOverflow())
tsepez4cf55152016-11-02 14:37:54 -07001743 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001744
1745 m_pVT->UpdateWordPlace(m_wpCaret);
tsepeza31da742016-09-08 11:28:14 -07001746 SetCaret(DoInsertText(m_wpCaret, sText, charset));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001747 m_SelState.Set(m_wpCaret, m_wpCaret);
tsepeza31da742016-09-08 11:28:14 -07001748 if (m_wpCaret == m_wpOldCaret)
tsepez4cf55152016-11-02 14:37:54 -07001749 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001750
tsepeza31da742016-09-08 11:28:14 -07001751 if (bAddUndo && m_bEnableUndo) {
Tom Sepez3509d162017-01-30 13:22:02 -08001752 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertText>(
1753 this, m_wpOldCaret, m_wpCaret, sText, charset));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001754 }
tsepeza31da742016-09-08 11:28:14 -07001755 if (bPaint)
1756 PaintInsertText(m_wpOldCaret, m_wpCaret);
1757
Lei Zhang5688d622017-08-12 07:04:19 -07001758 if (m_pOperationNotify)
1759 m_pOperationNotify->OnInsertText(m_wpCaret, m_wpOldCaret);
tsepeza31da742016-09-08 11:28:14 -07001760
tsepez4cf55152016-11-02 14:37:54 -07001761 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001762}
1763
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001764void CPWL_EditImpl::PaintInsertText(const CPVT_WordPlace& wpOld,
1765 const CPVT_WordPlace& wpNew) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001766 if (m_pVT->IsValid()) {
1767 RearrangePart(CPVT_WordRange(wpOld, wpNew));
1768 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001769 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001770 SetCaretOrigin();
1771 SetCaretInfo();
1772 }
1773}
1774
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001775bool CPWL_EditImpl::Redo() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001776 if (m_bEnableUndo) {
1777 if (m_Undo.CanRedo()) {
1778 m_Undo.Redo();
tsepez4cf55152016-11-02 14:37:54 -07001779 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001780 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001781 }
1782
tsepez4cf55152016-11-02 14:37:54 -07001783 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001784}
1785
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001786bool CPWL_EditImpl::Undo() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001787 if (m_bEnableUndo) {
1788 if (m_Undo.CanUndo()) {
1789 m_Undo.Undo();
tsepez4cf55152016-11-02 14:37:54 -07001790 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001791 }
1792 }
1793
tsepez4cf55152016-11-02 14:37:54 -07001794 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001795}
1796
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001797void CPWL_EditImpl::SetCaretOrigin() {
thestig821d59e2016-05-11 12:59:22 -07001798 if (!m_pVT->IsValid())
1799 return;
1800
1801 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1802 pIterator->SetAt(m_wpCaret);
1803 CPVT_Word word;
1804 CPVT_Line line;
1805 if (pIterator->GetWord(word)) {
1806 m_ptCaret.x = word.ptWord.x + word.fWidth;
1807 m_ptCaret.y = word.ptWord.y;
1808 } else if (pIterator->GetLine(line)) {
1809 m_ptCaret.x = line.ptLine.x;
1810 m_ptCaret.y = line.ptLine.y;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001811 }
1812}
1813
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001814CPVT_WordPlace CPWL_EditImpl::WordIndexToWordPlace(int32_t index) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001815 if (m_pVT->IsValid())
1816 return m_pVT->WordIndexToWordPlace(index);
1817
1818 return CPVT_WordPlace();
1819}
1820
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001821bool CPWL_EditImpl::IsTextFull() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001822 int32_t nTotalWords = m_pVT->GetTotalWords();
1823 int32_t nLimitChar = m_pVT->GetLimitChar();
1824 int32_t nCharArray = m_pVT->GetCharArray();
1825
1826 return IsTextOverflow() || (nLimitChar > 0 && nTotalWords >= nLimitChar) ||
1827 (nCharArray > 0 && nTotalWords >= nCharArray);
1828}
1829
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001830bool CPWL_EditImpl::IsTextOverflow() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001831 if (!m_bEnableScroll && !m_bEnableOverflow) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001832 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
1833 CFX_FloatRect rcContent = m_pVT->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001834
dsinclair448c4332016-08-02 12:07:35 -07001835 if (m_pVT->IsMultiLine() && GetTotalLines() > 1 &&
1836 IsFloatBigger(rcContent.Height(), rcPlate.Height())) {
tsepez4cf55152016-11-02 14:37:54 -07001837 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001838 }
1839
dsinclair448c4332016-08-02 12:07:35 -07001840 if (IsFloatBigger(rcContent.Width(), rcPlate.Width()))
tsepez4cf55152016-11-02 14:37:54 -07001841 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001842 }
1843
tsepez4cf55152016-11-02 14:37:54 -07001844 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001845}
1846
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001847bool CPWL_EditImpl::CanUndo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001848 if (m_bEnableUndo) {
1849 return m_Undo.CanUndo();
1850 }
1851
tsepez4cf55152016-11-02 14:37:54 -07001852 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001853}
1854
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001855bool CPWL_EditImpl::CanRedo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001856 if (m_bEnableUndo) {
1857 return m_Undo.CanRedo();
1858 }
1859
tsepez4cf55152016-11-02 14:37:54 -07001860 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001861}
1862
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001863void CPWL_EditImpl::EnableRefresh(bool bRefresh) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001864 m_bEnableRefresh = bRefresh;
1865}
1866
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001867void CPWL_EditImpl::EnableUndo(bool bUndo) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001868 m_bEnableUndo = bUndo;
1869}
1870
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001871CPVT_WordPlace CPWL_EditImpl::DoInsertText(const CPVT_WordPlace& place,
1872 const CFX_WideString& sText,
1873 int32_t charset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001874 CPVT_WordPlace wp = place;
1875
1876 if (m_pVT->IsValid()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001877 for (int32_t i = 0, sz = sText.GetLength(); i < sz; i++) {
Tom Sepez62a70f92016-03-21 15:00:20 -07001878 uint16_t word = sText[i];
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001879 switch (word) {
1880 case 0x0D:
dsinclairefd5a992016-07-18 10:04:07 -07001881 wp = m_pVT->InsertSection(wp, nullptr, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001882 if (sText[i + 1] == 0x0A)
1883 i++;
1884 break;
1885 case 0x0A:
dsinclairefd5a992016-07-18 10:04:07 -07001886 wp = m_pVT->InsertSection(wp, nullptr, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001887 if (sText[i + 1] == 0x0D)
1888 i++;
1889 break;
1890 case 0x09:
1891 word = 0x20;
1892 default:
1893 wp = m_pVT->InsertWord(wp, word, GetCharSetFromUnicode(word, charset),
dsinclairefd5a992016-07-18 10:04:07 -07001894 nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001895 break;
1896 }
1897 }
1898 }
1899
1900 return wp;
1901}
1902
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001903int32_t CPWL_EditImpl::GetCharSetFromUnicode(uint16_t word,
1904 int32_t nOldCharset) {
dsinclairc7a73492016-04-05 12:01:42 -07001905 if (IPVT_FontMap* pFontMap = GetFontMap())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001906 return pFontMap->CharSetFromUnicode(word, nOldCharset);
1907 return nOldCharset;
1908}
1909
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001910void CPWL_EditImpl::AddEditUndoItem(
Lei Zhangae9c5ca2017-08-12 07:15:14 -07001911 std::unique_ptr<IFX_Edit_UndoItem> pEditUndoItem) {
Lei Zhang1a89e362017-03-23 15:27:25 -07001912 m_Undo.AddItem(std::move(pEditUndoItem));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001913}
1914
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001915CFX_ByteString CPWL_EditImpl::GetPDFWordString(int32_t nFontIndex,
1916 uint16_t Word,
1917 uint16_t SubWord) {
Dan Sinclairc08dc392017-07-24 08:57:35 -04001918 IPVT_FontMap* pFontMap = GetFontMap();
1919 CPDF_Font* pPDFFont = pFontMap->GetPDFFont(nFontIndex);
1920 if (!pPDFFont)
1921 return CFX_ByteString();
1922
1923 CFX_ByteString sWord;
1924 if (SubWord > 0) {
1925 Word = SubWord;
1926 } else {
1927 uint32_t dwCharCode = pPDFFont->IsUnicodeCompatible()
1928 ? pPDFFont->CharCodeFromUnicode(Word)
1929 : pFontMap->CharCodeFromUnicode(nFontIndex, Word);
1930 if (dwCharCode > 0) {
1931 pPDFFont->AppendChar(&sWord, dwCharCode);
1932 return sWord;
1933 }
1934 }
1935 pPDFFont->AppendChar(&sWord, Word);
1936 return sWord;
1937}
1938
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001939CPWL_EditImpl_LineRectArray::CPWL_EditImpl_LineRectArray() {}
weili625ad662016-06-15 11:21:33 -07001940
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001941CPWL_EditImpl_LineRectArray::~CPWL_EditImpl_LineRectArray() {}
weili625ad662016-06-15 11:21:33 -07001942
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001943void CPWL_EditImpl_LineRectArray::operator=(
1944 CPWL_EditImpl_LineRectArray&& that) {
Tom Sepez3509d162017-01-30 13:22:02 -08001945 m_LineRects = std::move(that.m_LineRects);
weili625ad662016-06-15 11:21:33 -07001946}
1947
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001948void CPWL_EditImpl_LineRectArray::Add(const CPVT_WordRange& wrLine,
1949 const CFX_FloatRect& rcLine) {
1950 m_LineRects.push_back(
1951 pdfium::MakeUnique<CPWL_EditImpl_LineRect>(wrLine, rcLine));
weili625ad662016-06-15 11:21:33 -07001952}
1953
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001954int32_t CPWL_EditImpl_LineRectArray::GetSize() const {
Tom Sepez3509d162017-01-30 13:22:02 -08001955 return pdfium::CollectionSize<int32_t>(m_LineRects);
weili625ad662016-06-15 11:21:33 -07001956}
1957
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001958CPWL_EditImpl_LineRect* CPWL_EditImpl_LineRectArray::GetAt(
1959 int32_t nIndex) const {
Tom Sepez3509d162017-01-30 13:22:02 -08001960 if (nIndex < 0 || nIndex >= GetSize())
weili625ad662016-06-15 11:21:33 -07001961 return nullptr;
1962
Tom Sepez3509d162017-01-30 13:22:02 -08001963 return m_LineRects[nIndex].get();
weili625ad662016-06-15 11:21:33 -07001964}
1965
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001966CPWL_EditImpl_Select::CPWL_EditImpl_Select() {}
weili625ad662016-06-15 11:21:33 -07001967
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001968CPWL_EditImpl_Select::CPWL_EditImpl_Select(const CPVT_WordRange& range) {
weili625ad662016-06-15 11:21:33 -07001969 Set(range.BeginPos, range.EndPos);
1970}
1971
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001972CPVT_WordRange CPWL_EditImpl_Select::ConvertToWordRange() const {
weili625ad662016-06-15 11:21:33 -07001973 return CPVT_WordRange(BeginPos, EndPos);
1974}
1975
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001976void CPWL_EditImpl_Select::Reset() {
Tom Sepez52f69b32017-03-21 13:42:38 -07001977 BeginPos.Reset();
1978 EndPos.Reset();
weili625ad662016-06-15 11:21:33 -07001979}
1980
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001981void CPWL_EditImpl_Select::Set(const CPVT_WordPlace& begin,
1982 const CPVT_WordPlace& end) {
weili625ad662016-06-15 11:21:33 -07001983 BeginPos = begin;
1984 EndPos = end;
1985}
1986
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001987void CPWL_EditImpl_Select::SetEndPos(const CPVT_WordPlace& end) {
weili625ad662016-06-15 11:21:33 -07001988 EndPos = end;
1989}
1990
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001991bool CPWL_EditImpl_Select::IsEmpty() const {
Tom Sepez52f69b32017-03-21 13:42:38 -07001992 return BeginPos == EndPos;
weili625ad662016-06-15 11:21:33 -07001993}
1994
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001995CPWL_EditImpl_RectArray::CPWL_EditImpl_RectArray() {}
weili625ad662016-06-15 11:21:33 -07001996
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001997CPWL_EditImpl_RectArray::~CPWL_EditImpl_RectArray() {}
weili625ad662016-06-15 11:21:33 -07001998
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001999void CPWL_EditImpl_RectArray::Clear() {
Tom Sepez3509d162017-01-30 13:22:02 -08002000 m_Rects.clear();
weili625ad662016-06-15 11:21:33 -07002001}
2002
Dan Sinclair6b0158f2017-07-24 09:42:55 -04002003void CPWL_EditImpl_RectArray::Add(const CFX_FloatRect& rect) {
weili625ad662016-06-15 11:21:33 -07002004 // check for overlapped area
Tom Sepez3509d162017-01-30 13:22:02 -08002005 for (const auto& pRect : m_Rects) {
weili625ad662016-06-15 11:21:33 -07002006 if (pRect && pRect->Contains(rect))
2007 return;
2008 }
Tom Sepez3509d162017-01-30 13:22:02 -08002009 m_Rects.push_back(pdfium::MakeUnique<CFX_FloatRect>(rect));
weili625ad662016-06-15 11:21:33 -07002010}
2011
Dan Sinclair6b0158f2017-07-24 09:42:55 -04002012int32_t CPWL_EditImpl_RectArray::GetSize() const {
Tom Sepez3509d162017-01-30 13:22:02 -08002013 return pdfium::CollectionSize<int32_t>(m_Rects);
weili625ad662016-06-15 11:21:33 -07002014}
2015
Dan Sinclair6b0158f2017-07-24 09:42:55 -04002016CFX_FloatRect* CPWL_EditImpl_RectArray::GetAt(int32_t nIndex) const {
Tom Sepez3509d162017-01-30 13:22:02 -08002017 if (nIndex < 0 || nIndex >= GetSize())
weili625ad662016-06-15 11:21:33 -07002018 return nullptr;
2019
Tom Sepez3509d162017-01-30 13:22:02 -08002020 return m_Rects[nIndex].get();
weili625ad662016-06-15 11:21:33 -07002021}