blob: 7e2cf04b3f93da5ed0ca82fff1e5a9c954f34359 [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
dsinclair0bb385b2016-09-29 17:03:59 -07007#include "fpdfsdk/fxedit/fxet_edit.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>
11#include <utility>
Lei Zhang375a8642016-01-11 11:59:17 -080012
dsinclairbc5e6d22016-10-04 11:08:49 -070013#include "core/fpdfapi/font/cpdf_font.h"
dsinclair41872fa2016-10-04 11:29:35 -070014#include "core/fpdfapi/page/cpdf_pageobject.h"
15#include "core/fpdfapi/page/cpdf_pageobjectholder.h"
16#include "core/fpdfapi/page/cpdf_pathobject.h"
17#include "core/fpdfapi/page/cpdf_textobject.h"
dsinclair488b7ad2016-10-04 11:55:50 -070018#include "core/fpdfapi/parser/fpdf_parser_decode.h"
dsinclair69d9c682016-10-04 12:18:35 -070019#include "core/fpdfapi/render/cpdf_renderoptions.h"
20#include "core/fpdfapi/render/cpdf_textrenderer.h"
dsinclair1727aee2016-09-29 13:12:56 -070021#include "core/fpdfdoc/cpvt_section.h"
22#include "core/fpdfdoc/cpvt_word.h"
23#include "core/fpdfdoc/ipvt_fontmap.h"
dsinclair74a34fc2016-09-29 16:41:42 -070024#include "core/fxge/cfx_graphstatedata.h"
25#include "core/fxge/cfx_pathdata.h"
26#include "core/fxge/cfx_renderdevice.h"
dsinclaire35af1e2016-07-13 11:26:20 -070027#include "fpdfsdk/cfx_systemhandler.h"
dsinclair0bb385b2016-09-29 17:03:59 -070028#include "fpdfsdk/fxedit/fx_edit.h"
dsinclaire35af1e2016-07-13 11:26:20 -070029#include "fpdfsdk/pdfwindow/PWL_Edit.h"
30#include "fpdfsdk/pdfwindow/PWL_EditCtrl.h"
tsepez36eb4bd2016-10-03 15:24:27 -070031#include "third_party/base/ptr_util.h"
Tom Sepez3509d162017-01-30 13:22:02 -080032#include "third_party/base/stl_util.h"
dsinclaire35af1e2016-07-13 11:26:20 -070033
dsinclair8f4bf9a2016-05-04 13:51:51 -070034namespace {
35
36const int kEditUndoMaxItems = 10000;
37
dsinclaire35af1e2016-07-13 11:26:20 -070038CFX_ByteString GetWordRenderString(const CFX_ByteString& strWords) {
39 if (strWords.GetLength() > 0)
40 return PDF_EncodeString(strWords) + " Tj\n";
41 return CFX_ByteString();
42}
43
44CFX_ByteString GetFontSetString(IPVT_FontMap* pFontMap,
45 int32_t nFontIndex,
Dan Sinclair05df0752017-03-14 14:43:42 -040046 float fFontSize) {
dsinclaire35af1e2016-07-13 11:26:20 -070047 if (!pFontMap)
48 return CFX_ByteString();
49
50 CFX_ByteString sFontAlias = pFontMap->GetPDFFontAlias(nFontIndex);
51 if (sFontAlias.GetLength() <= 0 || fFontSize <= 0)
52 return CFX_ByteString();
53
54 CFX_ByteTextBuf sRet;
55 sRet << "/" << sFontAlias << " " << fFontSize << " Tf\n";
56 return sRet.MakeString();
57}
58
dsinclaire35af1e2016-07-13 11:26:20 -070059void DrawTextString(CFX_RenderDevice* pDevice,
Dan Sinclairf528eee2017-02-14 11:52:07 -050060 const CFX_PointF& pt,
dsinclaire35af1e2016-07-13 11:26:20 -070061 CPDF_Font* pFont,
Dan Sinclair05df0752017-03-14 14:43:42 -040062 float fFontSize,
dsinclaire35af1e2016-07-13 11:26:20 -070063 CFX_Matrix* pUser2Device,
64 const CFX_ByteString& str,
65 FX_ARGB crTextFill,
dsinclaire35af1e2016-07-13 11:26:20 -070066 int32_t nHorzScale) {
Dan Sinclaira0061af2017-02-23 09:25:17 -050067 CFX_PointF pos = pUser2Device->Transform(pt);
dsinclaire35af1e2016-07-13 11:26:20 -070068
69 if (pFont) {
70 if (nHorzScale != 100) {
71 CFX_Matrix mt(nHorzScale / 100.0f, 0, 0, 1, 0, 0);
72 mt.Concat(*pUser2Device);
73
74 CPDF_RenderOptions ro;
75 ro.m_Flags = RENDER_CLEARTYPE;
76 ro.m_ColorMode = RENDER_COLOR_NORMAL;
77
Dan Sinclaira0061af2017-02-23 09:25:17 -050078 CPDF_TextRenderer::DrawTextString(pDevice, pos.x, pos.y, pFont, fFontSize,
79 &mt, str, crTextFill, nullptr, &ro);
dsinclaire35af1e2016-07-13 11:26:20 -070080 } else {
81 CPDF_RenderOptions ro;
82 ro.m_Flags = RENDER_CLEARTYPE;
83 ro.m_ColorMode = RENDER_COLOR_NORMAL;
84
Dan Sinclaira0061af2017-02-23 09:25:17 -050085 CPDF_TextRenderer::DrawTextString(pDevice, pos.x, pos.y, pFont, fFontSize,
86 pUser2Device, str, crTextFill, nullptr,
87 &ro);
dsinclaire35af1e2016-07-13 11:26:20 -070088 }
89 }
90}
91
dsinclair8f4bf9a2016-05-04 13:51:51 -070092} // namespace
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070093
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094CFX_Edit_Iterator::CFX_Edit_Iterator(CFX_Edit* pEdit,
dsinclairc7a73492016-04-05 12:01:42 -070095 CPDF_VariableText::Iterator* pVTIterator)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 : m_pEdit(pEdit), m_pVTIterator(pVTIterator) {}
97
98CFX_Edit_Iterator::~CFX_Edit_Iterator() {}
99
tsepez4cf55152016-11-02 14:37:54 -0700100bool CFX_Edit_Iterator::NextWord() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 return m_pVTIterator->NextWord();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700102}
103
tsepez4cf55152016-11-02 14:37:54 -0700104bool CFX_Edit_Iterator::PrevWord() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105 return m_pVTIterator->PrevWord();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700106}
107
tsepez4cf55152016-11-02 14:37:54 -0700108bool CFX_Edit_Iterator::GetWord(CPVT_Word& word) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700109 ASSERT(m_pEdit);
110
111 if (m_pVTIterator->GetWord(word)) {
112 word.ptWord = m_pEdit->VTToEdit(word.ptWord);
tsepez4cf55152016-11-02 14:37:54 -0700113 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114 }
tsepez4cf55152016-11-02 14:37:54 -0700115 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700116}
117
tsepez4cf55152016-11-02 14:37:54 -0700118bool CFX_Edit_Iterator::GetLine(CPVT_Line& line) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700119 ASSERT(m_pEdit);
120
121 if (m_pVTIterator->GetLine(line)) {
122 line.ptLine = m_pEdit->VTToEdit(line.ptLine);
tsepez4cf55152016-11-02 14:37:54 -0700123 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 }
tsepez4cf55152016-11-02 14:37:54 -0700125 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700126}
127
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128void CFX_Edit_Iterator::SetAt(int32_t nWordIndex) {
129 m_pVTIterator->SetAt(nWordIndex);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700130}
131
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132void CFX_Edit_Iterator::SetAt(const CPVT_WordPlace& place) {
133 m_pVTIterator->SetAt(place);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700134}
135
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136const CPVT_WordPlace& CFX_Edit_Iterator::GetAt() const {
137 return m_pVTIterator->GetAt();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700138}
139
dsinclairc7a73492016-04-05 12:01:42 -0700140CFX_Edit_Provider::CFX_Edit_Provider(IPVT_FontMap* pFontMap)
141 : CPDF_VariableText::Provider(pFontMap), m_pFontMap(pFontMap) {
Lei Zhang96660d62015-12-14 18:27:25 -0800142 ASSERT(m_pFontMap);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700143}
144
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145CFX_Edit_Provider::~CFX_Edit_Provider() {}
146
dsinclairc7a73492016-04-05 12:01:42 -0700147IPVT_FontMap* CFX_Edit_Provider::GetFontMap() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700148 return m_pFontMap;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700149}
150
npm41d6bbe2016-09-14 11:54:44 -0700151int32_t CFX_Edit_Provider::GetCharWidth(int32_t nFontIndex, uint16_t word) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152 if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex)) {
tsepezc3255f52016-03-25 14:52:27 -0700153 uint32_t charcode = word;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700154
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700155 if (pPDFFont->IsUnicodeCompatible())
156 charcode = pPDFFont->CharCodeFromUnicode(word);
157 else
158 charcode = m_pFontMap->CharCodeFromUnicode(nFontIndex, word);
159
Wei Li89409932016-03-28 10:33:33 -0700160 if (charcode != CPDF_Font::kInvalidCharCode)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 return pPDFFont->GetCharWidthF(charcode);
162 }
163
164 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700165}
166
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167int32_t CFX_Edit_Provider::GetTypeAscent(int32_t nFontIndex) {
168 if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex))
169 return pPDFFont->GetTypeAscent();
170
171 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700172}
173
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700174int32_t CFX_Edit_Provider::GetTypeDescent(int32_t nFontIndex) {
175 if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex))
176 return pPDFFont->GetTypeDescent();
177
178 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700179}
180
Tom Sepez62a70f92016-03-21 15:00:20 -0700181int32_t CFX_Edit_Provider::GetWordFontIndex(uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182 int32_t charset,
183 int32_t nFontIndex) {
184 return m_pFontMap->GetWordFontIndex(word, charset, nFontIndex);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700185}
186
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700187int32_t CFX_Edit_Provider::GetDefaultFontIndex() {
188 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700189}
190
tsepez4cf55152016-11-02 14:37:54 -0700191bool CFX_Edit_Provider::IsLatinWord(uint16_t word) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700192 return FX_EDIT_ISLATINWORD(word);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700193}
194
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195CFX_Edit_Refresh::CFX_Edit_Refresh() {}
196
197CFX_Edit_Refresh::~CFX_Edit_Refresh() {}
198
199void CFX_Edit_Refresh::BeginRefresh() {
Tom Sepez3509d162017-01-30 13:22:02 -0800200 m_RefreshRects.Clear();
201 m_OldLineRects = std::move(m_NewLineRects);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700202}
203
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700204void CFX_Edit_Refresh::Push(const CPVT_WordRange& linerange,
Tom Sepez281a9ea2016-02-26 14:24:28 -0800205 const CFX_FloatRect& rect) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206 m_NewLineRects.Add(linerange, rect);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700207}
208
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209void CFX_Edit_Refresh::NoAnalyse() {
210 {
211 for (int32_t i = 0, sz = m_OldLineRects.GetSize(); i < sz; i++)
212 if (CFX_Edit_LineRect* pOldRect = m_OldLineRects.GetAt(i))
213 m_RefreshRects.Add(pOldRect->m_rcLine);
214 }
215
216 {
217 for (int32_t i = 0, sz = m_NewLineRects.GetSize(); i < sz; i++)
218 if (CFX_Edit_LineRect* pNewRect = m_NewLineRects.GetAt(i))
219 m_RefreshRects.Add(pNewRect->m_rcLine);
220 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700221}
222
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223const CFX_Edit_RectArray* CFX_Edit_Refresh::GetRefreshRects() const {
224 return &m_RefreshRects;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700225}
226
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227void CFX_Edit_Refresh::EndRefresh() {
Tom Sepez3509d162017-01-30 13:22:02 -0800228 m_RefreshRects.Clear();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700229}
230
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231CFX_Edit_Undo::CFX_Edit_Undo(int32_t nBufsize)
232 : m_nCurUndoPos(0),
233 m_nBufSize(nBufsize),
tsepez4cf55152016-11-02 14:37:54 -0700234 m_bWorking(false) {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700235
236CFX_Edit_Undo::~CFX_Edit_Undo() {
237 Reset();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700238}
239
tsepez4cf55152016-11-02 14:37:54 -0700240bool CFX_Edit_Undo::CanUndo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241 return m_nCurUndoPos > 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700242}
243
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244void CFX_Edit_Undo::Undo() {
tsepez4cf55152016-11-02 14:37:54 -0700245 m_bWorking = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700246 if (m_nCurUndoPos > 0) {
Tom Sepez3509d162017-01-30 13:22:02 -0800247 m_UndoItemStack[m_nCurUndoPos - 1]->Undo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248 m_nCurUndoPos--;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249 }
tsepez4cf55152016-11-02 14:37:54 -0700250 m_bWorking = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700251}
252
tsepez4cf55152016-11-02 14:37:54 -0700253bool CFX_Edit_Undo::CanRedo() const {
Tom Sepez3509d162017-01-30 13:22:02 -0800254 return m_nCurUndoPos < m_UndoItemStack.size();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700255}
256
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257void CFX_Edit_Undo::Redo() {
tsepez4cf55152016-11-02 14:37:54 -0700258 m_bWorking = true;
Tom Sepez3509d162017-01-30 13:22:02 -0800259 if (m_nCurUndoPos < m_UndoItemStack.size()) {
260 m_UndoItemStack[m_nCurUndoPos]->Redo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 m_nCurUndoPos++;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262 }
tsepez4cf55152016-11-02 14:37:54 -0700263 m_bWorking = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700264}
265
Tom Sepez3509d162017-01-30 13:22:02 -0800266void CFX_Edit_Undo::AddItem(std::unique_ptr<IFX_Edit_UndoItem> pItem) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 ASSERT(!m_bWorking);
Lei Zhang96660d62015-12-14 18:27:25 -0800268 ASSERT(pItem);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269 ASSERT(m_nBufSize > 1);
Tom Sepez3509d162017-01-30 13:22:02 -0800270 if (m_nCurUndoPos < m_UndoItemStack.size())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700271 RemoveTails();
272
Lei Zhang1a89e362017-03-23 15:27:25 -0700273 if (m_UndoItemStack.size() >= m_nBufSize)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274 RemoveHeads();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275
Tom Sepez3509d162017-01-30 13:22:02 -0800276 m_UndoItemStack.push_back(std::move(pItem));
277 m_nCurUndoPos = m_UndoItemStack.size();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700278}
279
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280void CFX_Edit_Undo::RemoveHeads() {
Tom Sepez3509d162017-01-30 13:22:02 -0800281 ASSERT(m_UndoItemStack.size() > 1);
282 m_UndoItemStack.pop_front();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700283}
284
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285void CFX_Edit_Undo::RemoveTails() {
Tom Sepez3509d162017-01-30 13:22:02 -0800286 while (m_UndoItemStack.size() > m_nCurUndoPos)
287 m_UndoItemStack.pop_back();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700288}
289
290void CFX_Edit_Undo::Reset() {
Tom Sepez3509d162017-01-30 13:22:02 -0800291 m_UndoItemStack.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700292 m_nCurUndoPos = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700293}
294
tsepez4cf55152016-11-02 14:37:54 -0700295CFX_Edit_UndoItem::CFX_Edit_UndoItem() : m_bFirst(true), m_bLast(true) {}
weili625ad662016-06-15 11:21:33 -0700296
297CFX_Edit_UndoItem::~CFX_Edit_UndoItem() {}
298
Tom Sepez3509d162017-01-30 13:22:02 -0800299CFX_WideString CFX_Edit_UndoItem::GetUndoTitle() const {
300 return CFX_WideString();
weili625ad662016-06-15 11:21:33 -0700301}
302
tsepez4cf55152016-11-02 14:37:54 -0700303void CFX_Edit_UndoItem::SetFirst(bool bFirst) {
weili625ad662016-06-15 11:21:33 -0700304 m_bFirst = bFirst;
305}
306
tsepez4cf55152016-11-02 14:37:54 -0700307void CFX_Edit_UndoItem::SetLast(bool bLast) {
weili625ad662016-06-15 11:21:33 -0700308 m_bLast = bLast;
309}
310
tsepez4cf55152016-11-02 14:37:54 -0700311bool CFX_Edit_UndoItem::IsLast() {
weili625ad662016-06-15 11:21:33 -0700312 return m_bLast;
313}
314
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700315CFXEU_InsertWord::CFXEU_InsertWord(CFX_Edit* pEdit,
316 const CPVT_WordPlace& wpOldPlace,
317 const CPVT_WordPlace& wpNewPlace,
Tom Sepez62a70f92016-03-21 15:00:20 -0700318 uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319 int32_t charset,
320 const CPVT_WordProps* pWordProps)
321 : m_pEdit(pEdit),
322 m_wpOld(wpOldPlace),
323 m_wpNew(wpNewPlace),
324 m_Word(word),
325 m_nCharset(charset),
326 m_WordProps() {
327 if (pWordProps)
328 m_WordProps = *pWordProps;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700329}
330
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700331CFXEU_InsertWord::~CFXEU_InsertWord() {}
332
333void CFXEU_InsertWord::Redo() {
334 if (m_pEdit) {
335 m_pEdit->SelectNone();
336 m_pEdit->SetCaret(m_wpOld);
tsepez4cf55152016-11-02 14:37:54 -0700337 m_pEdit->InsertWord(m_Word, m_nCharset, &m_WordProps, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700338 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700339}
340
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700341void CFXEU_InsertWord::Undo() {
342 if (m_pEdit) {
343 m_pEdit->SelectNone();
344 m_pEdit->SetCaret(m_wpNew);
tsepez4cf55152016-11-02 14:37:54 -0700345 m_pEdit->Backspace(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700346 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700347}
348
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700349CFXEU_InsertReturn::CFXEU_InsertReturn(CFX_Edit* pEdit,
350 const CPVT_WordPlace& wpOldPlace,
351 const CPVT_WordPlace& wpNewPlace,
352 const CPVT_SecProps* pSecProps,
353 const CPVT_WordProps* pWordProps)
354 : m_pEdit(pEdit),
355 m_wpOld(wpOldPlace),
356 m_wpNew(wpNewPlace),
357 m_SecProps(),
358 m_WordProps() {
359 if (pSecProps)
360 m_SecProps = *pSecProps;
361 if (pWordProps)
362 m_WordProps = *pWordProps;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700363}
364
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700365CFXEU_InsertReturn::~CFXEU_InsertReturn() {}
366
367void CFXEU_InsertReturn::Redo() {
368 if (m_pEdit) {
369 m_pEdit->SelectNone();
370 m_pEdit->SetCaret(m_wpOld);
tsepez4cf55152016-11-02 14:37:54 -0700371 m_pEdit->InsertReturn(&m_SecProps, &m_WordProps, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700372 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700373}
374
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375void CFXEU_InsertReturn::Undo() {
376 if (m_pEdit) {
377 m_pEdit->SelectNone();
378 m_pEdit->SetCaret(m_wpNew);
tsepez4cf55152016-11-02 14:37:54 -0700379 m_pEdit->Backspace(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700380 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700381}
382
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700383CFXEU_Backspace::CFXEU_Backspace(CFX_Edit* pEdit,
384 const CPVT_WordPlace& wpOldPlace,
385 const CPVT_WordPlace& wpNewPlace,
Tom Sepez62a70f92016-03-21 15:00:20 -0700386 uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700387 int32_t charset,
388 const CPVT_SecProps& SecProps,
389 const CPVT_WordProps& WordProps)
390 : 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
398CFXEU_Backspace::~CFXEU_Backspace() {}
399
400void CFXEU_Backspace::Redo() {
Tom Sepez52f69b32017-03-21 13:42:38 -0700401 if (!m_pEdit)
402 return;
403
404 m_pEdit->SelectNone();
405 m_pEdit->SetCaret(m_wpOld);
406 m_pEdit->Backspace(false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700407}
408
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409void CFXEU_Backspace::Undo() {
Tom Sepez52f69b32017-03-21 13:42:38 -0700410 if (!m_pEdit)
411 return;
412
413 m_pEdit->SelectNone();
414 m_pEdit->SetCaret(m_wpNew);
415 if (m_wpNew.nSecIndex != m_wpOld.nSecIndex)
416 m_pEdit->InsertReturn(&m_SecProps, &m_WordProps, false, true);
417 else
418 m_pEdit->InsertWord(m_Word, m_nCharset, &m_WordProps, false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700419}
420
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700421CFXEU_Delete::CFXEU_Delete(CFX_Edit* pEdit,
422 const CPVT_WordPlace& wpOldPlace,
423 const CPVT_WordPlace& wpNewPlace,
Tom Sepez62a70f92016-03-21 15:00:20 -0700424 uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700425 int32_t charset,
426 const CPVT_SecProps& SecProps,
427 const CPVT_WordProps& WordProps,
tsepez4cf55152016-11-02 14:37:54 -0700428 bool bSecEnd)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700429 : m_pEdit(pEdit),
430 m_wpOld(wpOldPlace),
431 m_wpNew(wpNewPlace),
432 m_Word(word),
433 m_nCharset(charset),
434 m_SecProps(SecProps),
435 m_WordProps(WordProps),
436 m_bSecEnd(bSecEnd) {}
437
438CFXEU_Delete::~CFXEU_Delete() {}
439
440void CFXEU_Delete::Redo() {
441 if (m_pEdit) {
442 m_pEdit->SelectNone();
443 m_pEdit->SetCaret(m_wpOld);
tsepez4cf55152016-11-02 14:37:54 -0700444 m_pEdit->Delete(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700445 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700446}
447
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700448void CFXEU_Delete::Undo() {
449 if (m_pEdit) {
450 m_pEdit->SelectNone();
451 m_pEdit->SetCaret(m_wpNew);
452 if (m_bSecEnd) {
tsepez4cf55152016-11-02 14:37:54 -0700453 m_pEdit->InsertReturn(&m_SecProps, &m_WordProps, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700454 } else {
tsepez4cf55152016-11-02 14:37:54 -0700455 m_pEdit->InsertWord(m_Word, m_nCharset, &m_WordProps, false, true);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700456 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700457 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700458}
459
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700460CFXEU_Clear::CFXEU_Clear(CFX_Edit* pEdit,
461 const CPVT_WordRange& wrSel,
462 const CFX_WideString& swText)
463 : m_pEdit(pEdit), m_wrSel(wrSel), m_swText(swText) {}
464
465CFXEU_Clear::~CFXEU_Clear() {}
466
467void CFXEU_Clear::Redo() {
468 if (m_pEdit) {
469 m_pEdit->SelectNone();
470 m_pEdit->SetSel(m_wrSel.BeginPos, m_wrSel.EndPos);
tsepez4cf55152016-11-02 14:37:54 -0700471 m_pEdit->Clear(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700472 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700473}
474
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700475void CFXEU_Clear::Undo() {
476 if (m_pEdit) {
477 m_pEdit->SelectNone();
478 m_pEdit->SetCaret(m_wrSel.BeginPos);
tsepez4cf55152016-11-02 14:37:54 -0700479 m_pEdit->InsertText(m_swText, FXFONT_DEFAULT_CHARSET, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700480 m_pEdit->SetSel(m_wrSel.BeginPos, m_wrSel.EndPos);
481 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700482}
483
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700484CFXEU_InsertText::CFXEU_InsertText(CFX_Edit* pEdit,
485 const CPVT_WordPlace& wpOldPlace,
486 const CPVT_WordPlace& wpNewPlace,
487 const CFX_WideString& swText,
dsinclairefd5a992016-07-18 10:04:07 -0700488 int32_t charset)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700489 : m_pEdit(pEdit),
490 m_wpOld(wpOldPlace),
491 m_wpNew(wpNewPlace),
492 m_swText(swText),
dsinclairefd5a992016-07-18 10:04:07 -0700493 m_nCharset(charset) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700494
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700495CFXEU_InsertText::~CFXEU_InsertText() {}
496
497void CFXEU_InsertText::Redo() {
498 if (m_pEdit && IsLast()) {
499 m_pEdit->SelectNone();
500 m_pEdit->SetCaret(m_wpOld);
tsepez4cf55152016-11-02 14:37:54 -0700501 m_pEdit->InsertText(m_swText, m_nCharset, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700502 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700503}
504
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700505void CFXEU_InsertText::Undo() {
506 if (m_pEdit) {
507 m_pEdit->SelectNone();
508 m_pEdit->SetSel(m_wpOld, m_wpNew);
tsepez4cf55152016-11-02 14:37:54 -0700509 m_pEdit->Clear(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700510 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700511}
512
dsinclaire35af1e2016-07-13 11:26:20 -0700513// static
514CFX_ByteString CFX_Edit::GetEditAppearanceStream(CFX_Edit* pEdit,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500515 const CFX_PointF& ptOffset,
dsinclaire35af1e2016-07-13 11:26:20 -0700516 const CPVT_WordRange* pRange,
tsepez4cf55152016-11-02 14:37:54 -0700517 bool bContinuous,
dsinclaire35af1e2016-07-13 11:26:20 -0700518 uint16_t SubWord) {
519 CFX_Edit_Iterator* pIterator = pEdit->GetIterator();
520 if (pRange)
521 pIterator->SetAt(pRange->BeginPos);
522 else
523 pIterator->SetAt(0);
524
525 CFX_ByteTextBuf sEditStream;
526 CFX_ByteTextBuf sWords;
527 int32_t nCurFontIndex = -1;
Dan Sinclairf528eee2017-02-14 11:52:07 -0500528 CFX_PointF ptOld;
529 CFX_PointF ptNew;
dsinclaire35af1e2016-07-13 11:26:20 -0700530 CPVT_WordPlace oldplace;
tsepez63f545c2016-09-13 16:08:49 -0700531
dsinclaire35af1e2016-07-13 11:26:20 -0700532 while (pIterator->NextWord()) {
533 CPVT_WordPlace place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -0700534 if (pRange && place > pRange->EndPos)
dsinclaire35af1e2016-07-13 11:26:20 -0700535 break;
536
537 if (bContinuous) {
538 if (place.LineCmp(oldplace) != 0) {
539 if (sWords.GetSize() > 0) {
540 sEditStream << GetWordRenderString(sWords.MakeString());
541 sWords.Clear();
542 }
543
544 CPVT_Word word;
545 if (pIterator->GetWord(word)) {
Dan Sinclairf528eee2017-02-14 11:52:07 -0500546 ptNew = CFX_PointF(word.ptWord.x + ptOffset.x,
547 word.ptWord.y + ptOffset.y);
dsinclaire35af1e2016-07-13 11:26:20 -0700548 } else {
549 CPVT_Line line;
550 pIterator->GetLine(line);
Dan Sinclairf528eee2017-02-14 11:52:07 -0500551 ptNew = CFX_PointF(line.ptLine.x + ptOffset.x,
552 line.ptLine.y + ptOffset.y);
dsinclaire35af1e2016-07-13 11:26:20 -0700553 }
554
555 if (ptNew.x != ptOld.x || ptNew.y != ptOld.y) {
556 sEditStream << ptNew.x - ptOld.x << " " << ptNew.y - ptOld.y
557 << " Td\n";
558
559 ptOld = ptNew;
560 }
561 }
562
563 CPVT_Word word;
564 if (pIterator->GetWord(word)) {
565 if (word.nFontIndex != nCurFontIndex) {
566 if (sWords.GetSize() > 0) {
567 sEditStream << GetWordRenderString(sWords.MakeString());
568 sWords.Clear();
569 }
570 sEditStream << GetFontSetString(pEdit->GetFontMap(), word.nFontIndex,
571 word.fFontSize);
572 nCurFontIndex = word.nFontIndex;
573 }
574
575 sWords << GetPDFWordString(pEdit->GetFontMap(), nCurFontIndex,
576 word.Word, SubWord);
577 }
578
579 oldplace = place;
580 } else {
581 CPVT_Word word;
582 if (pIterator->GetWord(word)) {
Dan Sinclairf528eee2017-02-14 11:52:07 -0500583 ptNew =
584 CFX_PointF(word.ptWord.x + ptOffset.x, word.ptWord.y + ptOffset.y);
dsinclaire35af1e2016-07-13 11:26:20 -0700585
586 if (ptNew.x != ptOld.x || ptNew.y != ptOld.y) {
587 sEditStream << ptNew.x - ptOld.x << " " << ptNew.y - ptOld.y
588 << " Td\n";
589 ptOld = ptNew;
590 }
591
592 if (word.nFontIndex != nCurFontIndex) {
593 sEditStream << GetFontSetString(pEdit->GetFontMap(), word.nFontIndex,
594 word.fFontSize);
595 nCurFontIndex = word.nFontIndex;
596 }
597
598 sEditStream << GetWordRenderString(GetPDFWordString(
599 pEdit->GetFontMap(), nCurFontIndex, word.Word, SubWord));
600 }
601 }
602 }
603
604 if (sWords.GetSize() > 0) {
605 sEditStream << GetWordRenderString(sWords.MakeString());
606 sWords.Clear();
607 }
608
609 CFX_ByteTextBuf sAppStream;
610 if (sEditStream.GetSize() > 0) {
611 int32_t nHorzScale = pEdit->GetHorzScale();
612 if (nHorzScale != 100) {
613 sAppStream << nHorzScale << " Tz\n";
614 }
615
Dan Sinclair05df0752017-03-14 14:43:42 -0400616 float fCharSpace = pEdit->GetCharSpace();
dsinclair448c4332016-08-02 12:07:35 -0700617 if (!IsFloatZero(fCharSpace)) {
dsinclaire35af1e2016-07-13 11:26:20 -0700618 sAppStream << fCharSpace << " Tc\n";
619 }
620
621 sAppStream << sEditStream;
622 }
623
624 return sAppStream.MakeString();
625}
626
627// static
628CFX_ByteString CFX_Edit::GetSelectAppearanceStream(
629 CFX_Edit* pEdit,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500630 const CFX_PointF& ptOffset,
dsinclaire35af1e2016-07-13 11:26:20 -0700631 const CPVT_WordRange* pRange) {
Tom Sepez52f69b32017-03-21 13:42:38 -0700632 if (!pRange || pRange->IsEmpty())
dsinclaire35af1e2016-07-13 11:26:20 -0700633 return CFX_ByteString();
634
635 CFX_Edit_Iterator* pIterator = pEdit->GetIterator();
636 pIterator->SetAt(pRange->BeginPos);
637
638 CFX_ByteTextBuf sRet;
639 while (pIterator->NextWord()) {
640 CPVT_WordPlace place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -0700641 if (place > pRange->EndPos)
dsinclaire35af1e2016-07-13 11:26:20 -0700642 break;
643
644 CPVT_Word word;
645 CPVT_Line line;
646 if (pIterator->GetWord(word) && pIterator->GetLine(line)) {
647 sRet << word.ptWord.x + ptOffset.x << " "
648 << line.ptLine.y + line.fLineDescent << " " << word.fWidth << " "
649 << line.fLineAscent - line.fLineDescent << " re\nf\n";
650 }
651 }
652
653 return sRet.MakeString();
654}
655
656// static
dsinclaire35af1e2016-07-13 11:26:20 -0700657void CFX_Edit::DrawEdit(CFX_RenderDevice* pDevice,
658 CFX_Matrix* pUser2Device,
659 CFX_Edit* pEdit,
660 FX_COLORREF crTextFill,
dsinclaire35af1e2016-07-13 11:26:20 -0700661 const CFX_FloatRect& rcClip,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500662 const CFX_PointF& ptOffset,
dsinclaire35af1e2016-07-13 11:26:20 -0700663 const CPVT_WordRange* pRange,
664 CFX_SystemHandler* pSystemHandler,
dsinclair8faac622016-09-15 12:41:50 -0700665 CFFL_FormFiller* pFFLData) {
dsinclaire35af1e2016-07-13 11:26:20 -0700666 const bool bContinuous =
667 pEdit->GetCharArray() == 0 && pEdit->GetCharSpace() <= 0.0f;
668 uint16_t SubWord = pEdit->GetPasswordChar();
Dan Sinclair05df0752017-03-14 14:43:42 -0400669 float fFontSize = pEdit->GetFontSize();
dsinclaire35af1e2016-07-13 11:26:20 -0700670 CPVT_WordRange wrSelect = pEdit->GetSelectWordRange();
671 int32_t nHorzScale = pEdit->GetHorzScale();
672
673 FX_COLORREF crCurFill = crTextFill;
674 FX_COLORREF crOldFill = crCurFill;
675
tsepez4cf55152016-11-02 14:37:54 -0700676 bool bSelect = false;
dsinclaire35af1e2016-07-13 11:26:20 -0700677 const FX_COLORREF crWhite = ArgbEncode(255, 255, 255, 255);
678 const FX_COLORREF crSelBK = ArgbEncode(255, 0, 51, 113);
679
680 CFX_ByteTextBuf sTextBuf;
681 int32_t nFontIndex = -1;
Dan Sinclairf528eee2017-02-14 11:52:07 -0500682 CFX_PointF ptBT;
dsinclaire35af1e2016-07-13 11:26:20 -0700683 pDevice->SaveState();
dsinclaire35af1e2016-07-13 11:26:20 -0700684 if (!rcClip.IsEmpty()) {
685 CFX_FloatRect rcTemp = rcClip;
686 pUser2Device->TransformRect(rcTemp);
687 pDevice->SetClip_Rect(rcTemp.ToFxRect());
688 }
689
690 CFX_Edit_Iterator* pIterator = pEdit->GetIterator();
691 if (IPVT_FontMap* pFontMap = pEdit->GetFontMap()) {
692 if (pRange)
693 pIterator->SetAt(pRange->BeginPos);
694 else
695 pIterator->SetAt(0);
696
697 CPVT_WordPlace oldplace;
698 while (pIterator->NextWord()) {
699 CPVT_WordPlace place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -0700700 if (pRange && place > pRange->EndPos)
dsinclaire35af1e2016-07-13 11:26:20 -0700701 break;
702
Tom Sepez52f69b32017-03-21 13:42:38 -0700703 if (!wrSelect.IsEmpty()) {
704 bSelect = place > wrSelect.BeginPos && place <= wrSelect.EndPos;
dsinclaire35af1e2016-07-13 11:26:20 -0700705 crCurFill = bSelect ? crWhite : crTextFill;
706 }
707 if (pSystemHandler && pSystemHandler->IsSelectionImplemented()) {
708 crCurFill = crTextFill;
709 crOldFill = crCurFill;
710 }
711 CPVT_Word word;
712 if (pIterator->GetWord(word)) {
713 if (bSelect) {
714 CPVT_Line line;
715 pIterator->GetLine(line);
716
717 if (pSystemHandler && pSystemHandler->IsSelectionImplemented()) {
718 CFX_FloatRect rc(word.ptWord.x, line.ptLine.y + line.fLineDescent,
719 word.ptWord.x + word.fWidth,
720 line.ptLine.y + line.fLineAscent);
721 rc.Intersect(rcClip);
722 pSystemHandler->OutputSelectedRect(pFFLData, rc);
723 } else {
724 CFX_PathData pathSelBK;
725 pathSelBK.AppendRect(
726 word.ptWord.x, line.ptLine.y + line.fLineDescent,
727 word.ptWord.x + word.fWidth, line.ptLine.y + line.fLineAscent);
728
729 pDevice->DrawPath(&pathSelBK, pUser2Device, nullptr, crSelBK, 0,
730 FXFILL_WINDING);
731 }
732 }
733
734 if (bContinuous) {
735 if (place.LineCmp(oldplace) != 0 || word.nFontIndex != nFontIndex ||
736 crOldFill != crCurFill) {
737 if (sTextBuf.GetLength() > 0) {
738 DrawTextString(
Dan Sinclairf528eee2017-02-14 11:52:07 -0500739 pDevice, CFX_PointF(ptBT.x + ptOffset.x, ptBT.y + ptOffset.y),
dsinclaire35af1e2016-07-13 11:26:20 -0700740 pFontMap->GetPDFFont(nFontIndex), fFontSize, pUser2Device,
Dan Sinclaira0061af2017-02-23 09:25:17 -0500741 sTextBuf.MakeString(), crOldFill, nHorzScale);
dsinclaire35af1e2016-07-13 11:26:20 -0700742
743 sTextBuf.Clear();
744 }
745 nFontIndex = word.nFontIndex;
746 ptBT = word.ptWord;
747 crOldFill = crCurFill;
748 }
749
750 sTextBuf << GetPDFWordString(pFontMap, word.nFontIndex, word.Word,
751 SubWord)
752 .AsStringC();
753 } else {
754 DrawTextString(
Dan Sinclairf528eee2017-02-14 11:52:07 -0500755 pDevice, CFX_PointF(word.ptWord.x + ptOffset.x,
756 word.ptWord.y + ptOffset.y),
dsinclaire35af1e2016-07-13 11:26:20 -0700757 pFontMap->GetPDFFont(word.nFontIndex), fFontSize, pUser2Device,
758 GetPDFWordString(pFontMap, word.nFontIndex, word.Word, SubWord),
Dan Sinclaira0061af2017-02-23 09:25:17 -0500759 crCurFill, nHorzScale);
dsinclaire35af1e2016-07-13 11:26:20 -0700760 }
761 oldplace = place;
762 }
763 }
764
765 if (sTextBuf.GetLength() > 0) {
Dan Sinclaira0061af2017-02-23 09:25:17 -0500766 DrawTextString(pDevice,
767 CFX_PointF(ptBT.x + ptOffset.x, ptBT.y + ptOffset.y),
768 pFontMap->GetPDFFont(nFontIndex), fFontSize, pUser2Device,
769 sTextBuf.MakeString(), crOldFill, nHorzScale);
dsinclaire35af1e2016-07-13 11:26:20 -0700770 }
771 }
772
773 pDevice->RestoreState(false);
774}
775
dsinclaire35af1e2016-07-13 11:26:20 -0700776CFX_Edit::CFX_Edit()
777 : m_pVT(new CPDF_VariableText),
thestig821d59e2016-05-11 12:59:22 -0700778 m_pNotify(nullptr),
779 m_pOprNotify(nullptr),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700780 m_wpCaret(-1, -1, -1),
781 m_wpOldCaret(-1, -1, -1),
782 m_SelState(),
tsepez4cf55152016-11-02 14:37:54 -0700783 m_bEnableScroll(false),
dsinclair8f4bf9a2016-05-04 13:51:51 -0700784 m_Undo(kEditUndoMaxItems),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700785 m_nAlignment(0),
tsepez4cf55152016-11-02 14:37:54 -0700786 m_bNotifyFlag(false),
787 m_bEnableOverflow(false),
788 m_bEnableRefresh(true),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700789 m_rcOldContent(0.0f, 0.0f, 0.0f, 0.0f),
tsepez4cf55152016-11-02 14:37:54 -0700790 m_bEnableUndo(true),
Lei Zhang1a89e362017-03-23 15:27:25 -0700791 m_bOprNotify(false) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700792
Lei Zhang1a89e362017-03-23 15:27:25 -0700793CFX_Edit::~CFX_Edit() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700794
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700795void CFX_Edit::Initialize() {
796 m_pVT->Initialize();
797 SetCaret(m_pVT->GetBeginWordPlace());
798 SetCaretOrigin();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700799}
800
dsinclairc7a73492016-04-05 12:01:42 -0700801void CFX_Edit::SetFontMap(IPVT_FontMap* pFontMap) {
tsepez36eb4bd2016-10-03 15:24:27 -0700802 m_pVTProvider = pdfium::MakeUnique<CFX_Edit_Provider>(pFontMap);
thestig821d59e2016-05-11 12:59:22 -0700803 m_pVT->SetProvider(m_pVTProvider.get());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700804}
805
dsinclaire35af1e2016-07-13 11:26:20 -0700806void CFX_Edit::SetNotify(CPWL_EditCtrl* pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700807 m_pNotify = pNotify;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700808}
809
dsinclaire35af1e2016-07-13 11:26:20 -0700810void CFX_Edit::SetOprNotify(CPWL_Edit* pOprNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700811 m_pOprNotify = pOprNotify;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700812}
813
dsinclaire35af1e2016-07-13 11:26:20 -0700814CFX_Edit_Iterator* CFX_Edit::GetIterator() {
tsepez36eb4bd2016-10-03 15:24:27 -0700815 if (!m_pIterator) {
816 m_pIterator =
817 pdfium::MakeUnique<CFX_Edit_Iterator>(this, m_pVT->GetIterator());
818 }
thestig821d59e2016-05-11 12:59:22 -0700819 return m_pIterator.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700820}
821
dsinclairc7a73492016-04-05 12:01:42 -0700822IPVT_FontMap* CFX_Edit::GetFontMap() {
thestig821d59e2016-05-11 12:59:22 -0700823 return m_pVTProvider ? m_pVTProvider->GetFontMap() : nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700824}
825
dsinclairefd5a992016-07-18 10:04:07 -0700826void CFX_Edit::SetPlateRect(const CFX_FloatRect& rect) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700827 m_pVT->SetPlateRect(rect);
Dan Sinclairf528eee2017-02-14 11:52:07 -0500828 m_ptScrollPos = CFX_PointF(rect.left, rect.top);
dsinclairefd5a992016-07-18 10:04:07 -0700829 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700830}
831
tsepez4cf55152016-11-02 14:37:54 -0700832void CFX_Edit::SetAlignmentH(int32_t nFormat, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700833 m_pVT->SetAlignment(nFormat);
834 if (bPaint)
835 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700836}
837
tsepez4cf55152016-11-02 14:37:54 -0700838void CFX_Edit::SetAlignmentV(int32_t nFormat, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700839 m_nAlignment = nFormat;
840 if (bPaint)
841 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700842}
843
tsepez4cf55152016-11-02 14:37:54 -0700844void CFX_Edit::SetPasswordChar(uint16_t wSubWord, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700845 m_pVT->SetPasswordChar(wSubWord);
846 if (bPaint)
847 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700848}
849
dsinclairefd5a992016-07-18 10:04:07 -0700850void CFX_Edit::SetLimitChar(int32_t nLimitChar) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700851 m_pVT->SetLimitChar(nLimitChar);
dsinclairefd5a992016-07-18 10:04:07 -0700852 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700853}
854
dsinclairefd5a992016-07-18 10:04:07 -0700855void CFX_Edit::SetCharArray(int32_t nCharArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700856 m_pVT->SetCharArray(nCharArray);
dsinclairefd5a992016-07-18 10:04:07 -0700857 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700858}
859
Dan Sinclair05df0752017-03-14 14:43:42 -0400860void CFX_Edit::SetCharSpace(float fCharSpace) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700861 m_pVT->SetCharSpace(fCharSpace);
dsinclairefd5a992016-07-18 10:04:07 -0700862 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700863}
864
tsepez4cf55152016-11-02 14:37:54 -0700865void CFX_Edit::SetMultiLine(bool bMultiLine, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700866 m_pVT->SetMultiLine(bMultiLine);
867 if (bPaint)
868 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700869}
870
tsepez4cf55152016-11-02 14:37:54 -0700871void CFX_Edit::SetAutoReturn(bool bAuto, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700872 m_pVT->SetAutoReturn(bAuto);
873 if (bPaint)
874 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700875}
876
tsepez4cf55152016-11-02 14:37:54 -0700877void CFX_Edit::SetAutoFontSize(bool bAuto, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700878 m_pVT->SetAutoFontSize(bAuto);
879 if (bPaint)
880 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700881}
882
Dan Sinclair05df0752017-03-14 14:43:42 -0400883void CFX_Edit::SetFontSize(float fFontSize) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700884 m_pVT->SetFontSize(fFontSize);
dsinclairefd5a992016-07-18 10:04:07 -0700885 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700886}
887
tsepez4cf55152016-11-02 14:37:54 -0700888void CFX_Edit::SetAutoScroll(bool bAuto, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700889 m_bEnableScroll = bAuto;
890 if (bPaint)
891 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700892}
893
tsepez4cf55152016-11-02 14:37:54 -0700894void CFX_Edit::SetTextOverflow(bool bAllowed, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700895 m_bEnableOverflow = bAllowed;
896 if (bPaint)
897 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700898}
899
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700900void CFX_Edit::SetSel(int32_t nStartChar, int32_t nEndChar) {
901 if (m_pVT->IsValid()) {
902 if (nStartChar == 0 && nEndChar < 0) {
903 SelectAll();
904 } else if (nStartChar < 0) {
905 SelectNone();
906 } else {
907 if (nStartChar < nEndChar) {
908 SetSel(m_pVT->WordIndexToWordPlace(nStartChar),
909 m_pVT->WordIndexToWordPlace(nEndChar));
910 } else {
911 SetSel(m_pVT->WordIndexToWordPlace(nEndChar),
912 m_pVT->WordIndexToWordPlace(nStartChar));
913 }
914 }
915 }
916}
917
918void CFX_Edit::SetSel(const CPVT_WordPlace& begin, const CPVT_WordPlace& end) {
Tom Sepez52f69b32017-03-21 13:42:38 -0700919 if (!m_pVT->IsValid())
920 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700921
Tom Sepez52f69b32017-03-21 13:42:38 -0700922 SelectNone();
923 m_SelState.Set(begin, end);
924 SetCaret(m_SelState.EndPos);
925 ScrollToCaret();
926 if (!m_SelState.IsEmpty())
927 Refresh();
928 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700929}
930
931void CFX_Edit::GetSel(int32_t& nStartChar, int32_t& nEndChar) const {
932 nStartChar = -1;
933 nEndChar = -1;
Tom Sepez52f69b32017-03-21 13:42:38 -0700934 if (!m_pVT->IsValid())
935 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700936
Tom Sepez52f69b32017-03-21 13:42:38 -0700937 if (m_SelState.IsEmpty()) {
938 nStartChar = m_pVT->WordPlaceToWordIndex(m_wpCaret);
939 nEndChar = m_pVT->WordPlaceToWordIndex(m_wpCaret);
940 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700941 }
Tom Sepez52f69b32017-03-21 13:42:38 -0700942 if (m_SelState.BeginPos < m_SelState.EndPos) {
943 nStartChar = m_pVT->WordPlaceToWordIndex(m_SelState.BeginPos);
944 nEndChar = m_pVT->WordPlaceToWordIndex(m_SelState.EndPos);
945 return;
946 }
947 nStartChar = m_pVT->WordPlaceToWordIndex(m_SelState.EndPos);
948 nEndChar = m_pVT->WordPlaceToWordIndex(m_SelState.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700949}
950
951int32_t CFX_Edit::GetCaret() const {
952 if (m_pVT->IsValid())
953 return m_pVT->WordPlaceToWordIndex(m_wpCaret);
954
955 return -1;
956}
957
958CPVT_WordPlace CFX_Edit::GetCaretWordPlace() const {
959 return m_wpCaret;
960}
961
962CFX_WideString CFX_Edit::GetText() const {
963 CFX_WideString swRet;
thestig821d59e2016-05-11 12:59:22 -0700964 if (!m_pVT->IsValid())
965 return swRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700966
thestig821d59e2016-05-11 12:59:22 -0700967 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
968 pIterator->SetAt(0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700969
thestig821d59e2016-05-11 12:59:22 -0700970 CPVT_Word wordinfo;
971 CPVT_WordPlace oldplace = pIterator->GetAt();
972 while (pIterator->NextWord()) {
973 CPVT_WordPlace place = pIterator->GetAt();
thestig821d59e2016-05-11 12:59:22 -0700974 if (pIterator->GetWord(wordinfo))
975 swRet += wordinfo.Word;
Tom Sepez52f69b32017-03-21 13:42:38 -0700976 if (oldplace.nSecIndex != place.nSecIndex)
thestig821d59e2016-05-11 12:59:22 -0700977 swRet += L"\r\n";
thestig821d59e2016-05-11 12:59:22 -0700978 oldplace = place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700979 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700980 return swRet;
981}
982
983CFX_WideString CFX_Edit::GetRangeText(const CPVT_WordRange& range) const {
984 CFX_WideString swRet;
thestig821d59e2016-05-11 12:59:22 -0700985 if (!m_pVT->IsValid())
986 return swRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700987
thestig821d59e2016-05-11 12:59:22 -0700988 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
989 CPVT_WordRange wrTemp = range;
990 m_pVT->UpdateWordPlace(wrTemp.BeginPos);
991 m_pVT->UpdateWordPlace(wrTemp.EndPos);
992 pIterator->SetAt(wrTemp.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700993
thestig821d59e2016-05-11 12:59:22 -0700994 CPVT_Word wordinfo;
995 CPVT_WordPlace oldplace = wrTemp.BeginPos;
996 while (pIterator->NextWord()) {
997 CPVT_WordPlace place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -0700998 if (place > wrTemp.EndPos)
thestig821d59e2016-05-11 12:59:22 -0700999 break;
thestig821d59e2016-05-11 12:59:22 -07001000 if (pIterator->GetWord(wordinfo))
1001 swRet += wordinfo.Word;
Tom Sepez52f69b32017-03-21 13:42:38 -07001002 if (oldplace.nSecIndex != place.nSecIndex)
thestig821d59e2016-05-11 12:59:22 -07001003 swRet += L"\r\n";
thestig821d59e2016-05-11 12:59:22 -07001004 oldplace = place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001005 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001006 return swRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001007}
1008
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001009CFX_WideString CFX_Edit::GetSelText() const {
1010 return GetRangeText(m_SelState.ConvertToWordRange());
1011}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001012
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001013int32_t CFX_Edit::GetTotalLines() const {
thestig821d59e2016-05-11 12:59:22 -07001014 int32_t nLines = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001015
thestig821d59e2016-05-11 12:59:22 -07001016 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1017 pIterator->SetAt(0);
1018 while (pIterator->NextLine())
1019 ++nLines;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001020
thestig821d59e2016-05-11 12:59:22 -07001021 return nLines;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001022}
1023
1024CPVT_WordRange CFX_Edit::GetSelectWordRange() const {
1025 return m_SelState.ConvertToWordRange();
1026}
1027
tsepeza31da742016-09-08 11:28:14 -07001028void CFX_Edit::SetText(const CFX_WideString& sText) {
dsinclairefd5a992016-07-18 10:04:07 -07001029 Empty();
npmea3c3be2016-09-19 07:24:33 -07001030 DoInsertText(CPVT_WordPlace(0, 0, -1), sText, FXFONT_DEFAULT_CHARSET);
dsinclairefd5a992016-07-18 10:04:07 -07001031 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001032}
1033
tsepez4cf55152016-11-02 14:37:54 -07001034bool CFX_Edit::InsertWord(uint16_t word, int32_t charset) {
1035 return InsertWord(word, charset, nullptr, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001036}
1037
tsepez4cf55152016-11-02 14:37:54 -07001038bool CFX_Edit::InsertReturn() {
1039 return InsertReturn(nullptr, nullptr, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001040}
1041
tsepez4cf55152016-11-02 14:37:54 -07001042bool CFX_Edit::Backspace() {
1043 return Backspace(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001044}
1045
tsepez4cf55152016-11-02 14:37:54 -07001046bool CFX_Edit::Delete() {
1047 return Delete(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001048}
1049
tsepez4cf55152016-11-02 14:37:54 -07001050bool CFX_Edit::Clear() {
1051 return Clear(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001052}
1053
tsepez4cf55152016-11-02 14:37:54 -07001054bool CFX_Edit::InsertText(const CFX_WideString& sText, int32_t charset) {
1055 return InsertText(sText, charset, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001056}
1057
Dan Sinclair05df0752017-03-14 14:43:42 -04001058float CFX_Edit::GetFontSize() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001059 return m_pVT->GetFontSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001060}
1061
Tom Sepez62a70f92016-03-21 15:00:20 -07001062uint16_t CFX_Edit::GetPasswordChar() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001063 return m_pVT->GetPasswordChar();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001064}
1065
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001066int32_t CFX_Edit::GetCharArray() const {
1067 return m_pVT->GetCharArray();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001068}
1069
Tom Sepez281a9ea2016-02-26 14:24:28 -08001070CFX_FloatRect CFX_Edit::GetContentRect() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001071 return VTToEdit(m_pVT->GetContentRect());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001072}
1073
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001074int32_t CFX_Edit::GetHorzScale() const {
1075 return m_pVT->GetHorzScale();
1076}
1077
Dan Sinclair05df0752017-03-14 14:43:42 -04001078float CFX_Edit::GetCharSpace() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001079 return m_pVT->GetCharSpace();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001080}
1081
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001082CPVT_WordRange CFX_Edit::GetWholeWordRange() const {
1083 if (m_pVT->IsValid())
1084 return CPVT_WordRange(m_pVT->GetBeginWordPlace(), m_pVT->GetEndWordPlace());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001085
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001086 return CPVT_WordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001087}
1088
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001089CPVT_WordRange CFX_Edit::GetVisibleWordRange() const {
1090 if (m_bEnableOverflow)
1091 return GetWholeWordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001092
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001093 if (m_pVT->IsValid()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001094 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001095
Dan Sinclairf528eee2017-02-14 11:52:07 -05001096 CPVT_WordPlace place1 =
1097 m_pVT->SearchWordPlace(EditToVT(CFX_PointF(rcPlate.left, rcPlate.top)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001098 CPVT_WordPlace place2 = m_pVT->SearchWordPlace(
Dan Sinclairf528eee2017-02-14 11:52:07 -05001099 EditToVT(CFX_PointF(rcPlate.right, rcPlate.bottom)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001100
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001101 return CPVT_WordRange(place1, place2);
1102 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001103
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001104 return CPVT_WordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001105}
1106
Dan Sinclairf528eee2017-02-14 11:52:07 -05001107CPVT_WordPlace CFX_Edit::SearchWordPlace(const CFX_PointF& point) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001108 if (m_pVT->IsValid()) {
1109 return m_pVT->SearchWordPlace(EditToVT(point));
1110 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001111
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001112 return CPVT_WordPlace();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001113}
1114
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001115void CFX_Edit::Paint() {
1116 if (m_pVT->IsValid()) {
1117 RearrangeAll();
1118 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001119 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001120 SetCaretOrigin();
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001121 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001122 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001123}
1124
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001125void CFX_Edit::RearrangeAll() {
1126 if (m_pVT->IsValid()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001127 m_pVT->UpdateWordPlace(m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001128 m_pVT->RearrangeAll();
1129 m_pVT->UpdateWordPlace(m_wpCaret);
1130 SetScrollInfo();
1131 SetContentChanged();
1132 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001133}
1134
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001135void CFX_Edit::RearrangePart(const CPVT_WordRange& range) {
1136 if (m_pVT->IsValid()) {
1137 m_pVT->UpdateWordPlace(m_wpCaret);
1138 m_pVT->RearrangePart(range);
1139 m_pVT->UpdateWordPlace(m_wpCaret);
1140 SetScrollInfo();
1141 SetContentChanged();
1142 }
1143}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001144
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001145void CFX_Edit::SetContentChanged() {
dsinclaira2919b32016-07-13 10:55:48 -07001146 if (m_pNotify) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001147 CFX_FloatRect rcContent = m_pVT->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001148 if (rcContent.Width() != m_rcOldContent.Width() ||
1149 rcContent.Height() != m_rcOldContent.Height()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001150 m_rcOldContent = rcContent;
1151 }
1152 }
1153}
1154
1155void CFX_Edit::SelectAll() {
Tom Sepez52f69b32017-03-21 13:42:38 -07001156 if (!m_pVT->IsValid())
1157 return;
1158 m_SelState = CFX_Edit_Select(GetWholeWordRange());
1159 SetCaret(m_SelState.EndPos);
1160 ScrollToCaret();
1161 Refresh();
1162 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001163}
1164
1165void CFX_Edit::SelectNone() {
Tom Sepez52f69b32017-03-21 13:42:38 -07001166 if (!m_pVT->IsValid() || m_SelState.IsEmpty())
1167 return;
1168
1169 m_SelState.Reset();
1170 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001171}
1172
tsepez4cf55152016-11-02 14:37:54 -07001173bool CFX_Edit::IsSelected() const {
Tom Sepez52f69b32017-03-21 13:42:38 -07001174 return !m_SelState.IsEmpty();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001175}
1176
Dan Sinclairf528eee2017-02-14 11:52:07 -05001177CFX_PointF CFX_Edit::VTToEdit(const CFX_PointF& point) const {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001178 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1179 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001180
Dan Sinclair05df0752017-03-14 14:43:42 -04001181 float fPadding = 0.0f;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001182
1183 switch (m_nAlignment) {
1184 case 0:
1185 fPadding = 0.0f;
1186 break;
1187 case 1:
1188 fPadding = (rcPlate.Height() - rcContent.Height()) * 0.5f;
1189 break;
1190 case 2:
1191 fPadding = rcPlate.Height() - rcContent.Height();
1192 break;
1193 }
1194
Dan Sinclairf528eee2017-02-14 11:52:07 -05001195 return CFX_PointF(point.x - (m_ptScrollPos.x - rcPlate.left),
1196 point.y - (m_ptScrollPos.y + fPadding - rcPlate.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001197}
1198
Dan Sinclairf528eee2017-02-14 11:52:07 -05001199CFX_PointF CFX_Edit::EditToVT(const CFX_PointF& point) const {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001200 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1201 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001202
Dan Sinclair05df0752017-03-14 14:43:42 -04001203 float fPadding = 0.0f;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001204
1205 switch (m_nAlignment) {
1206 case 0:
1207 fPadding = 0.0f;
1208 break;
1209 case 1:
1210 fPadding = (rcPlate.Height() - rcContent.Height()) * 0.5f;
1211 break;
1212 case 2:
1213 fPadding = rcPlate.Height() - rcContent.Height();
1214 break;
1215 }
1216
Dan Sinclairf528eee2017-02-14 11:52:07 -05001217 return CFX_PointF(point.x + (m_ptScrollPos.x - rcPlate.left),
1218 point.y + (m_ptScrollPos.y + fPadding - rcPlate.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001219}
1220
Tom Sepez281a9ea2016-02-26 14:24:28 -08001221CFX_FloatRect CFX_Edit::VTToEdit(const CFX_FloatRect& rect) const {
Dan Sinclairf528eee2017-02-14 11:52:07 -05001222 CFX_PointF ptLeftBottom = VTToEdit(CFX_PointF(rect.left, rect.bottom));
1223 CFX_PointF ptRightTop = VTToEdit(CFX_PointF(rect.right, rect.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001224
Tom Sepez281a9ea2016-02-26 14:24:28 -08001225 return CFX_FloatRect(ptLeftBottom.x, ptLeftBottom.y, ptRightTop.x,
1226 ptRightTop.y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001227}
1228
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001229void CFX_Edit::SetScrollInfo() {
dsinclaira2919b32016-07-13 10:55:48 -07001230 if (m_pNotify) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001231 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
1232 CFX_FloatRect rcContent = m_pVT->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001233
1234 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001235 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001236 m_bNotifyFlag = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001237 m_pNotify->IOnSetScrollInfoY(rcPlate.bottom, rcPlate.top,
1238 rcContent.bottom, rcContent.top,
1239 rcPlate.Height() / 3, rcPlate.Height());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001240 }
1241 }
1242}
1243
Dan Sinclair05df0752017-03-14 14:43:42 -04001244void CFX_Edit::SetScrollPosX(float fx) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001245 if (!m_bEnableScroll)
1246 return;
1247
1248 if (m_pVT->IsValid()) {
dsinclair448c4332016-08-02 12:07:35 -07001249 if (!IsFloatEqual(m_ptScrollPos.x, fx)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001250 m_ptScrollPos.x = fx;
dsinclairefd5a992016-07-18 10:04:07 -07001251 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001252 }
1253 }
1254}
1255
Dan Sinclair05df0752017-03-14 14:43:42 -04001256void CFX_Edit::SetScrollPosY(float fy) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001257 if (!m_bEnableScroll)
1258 return;
1259
1260 if (m_pVT->IsValid()) {
dsinclair448c4332016-08-02 12:07:35 -07001261 if (!IsFloatEqual(m_ptScrollPos.y, fy)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001262 m_ptScrollPos.y = fy;
dsinclairefd5a992016-07-18 10:04:07 -07001263 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001264
dsinclaira2919b32016-07-13 10:55:48 -07001265 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001266 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001267 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001268 m_bNotifyFlag = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001269 m_pNotify->IOnSetScrollPosY(fy);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001270 }
1271 }
1272 }
1273 }
1274}
1275
Dan Sinclairf528eee2017-02-14 11:52:07 -05001276void CFX_Edit::SetScrollPos(const CFX_PointF& point) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001277 SetScrollPosX(point.x);
1278 SetScrollPosY(point.y);
1279 SetScrollLimit();
1280 SetCaretInfo();
1281}
1282
Dan Sinclairf528eee2017-02-14 11:52:07 -05001283CFX_PointF CFX_Edit::GetScrollPos() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001284 return m_ptScrollPos;
1285}
1286
1287void CFX_Edit::SetScrollLimit() {
1288 if (m_pVT->IsValid()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001289 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1290 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001291
1292 if (rcPlate.Width() > rcContent.Width()) {
1293 SetScrollPosX(rcPlate.left);
1294 } else {
dsinclair448c4332016-08-02 12:07:35 -07001295 if (IsFloatSmaller(m_ptScrollPos.x, rcContent.left)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001296 SetScrollPosX(rcContent.left);
dsinclair448c4332016-08-02 12:07:35 -07001297 } else if (IsFloatBigger(m_ptScrollPos.x,
1298 rcContent.right - rcPlate.Width())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001299 SetScrollPosX(rcContent.right - rcPlate.Width());
1300 }
1301 }
1302
1303 if (rcPlate.Height() > rcContent.Height()) {
1304 SetScrollPosY(rcPlate.top);
1305 } else {
dsinclair448c4332016-08-02 12:07:35 -07001306 if (IsFloatSmaller(m_ptScrollPos.y,
1307 rcContent.bottom + rcPlate.Height())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001308 SetScrollPosY(rcContent.bottom + rcPlate.Height());
dsinclair448c4332016-08-02 12:07:35 -07001309 } else if (IsFloatBigger(m_ptScrollPos.y, rcContent.top)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001310 SetScrollPosY(rcContent.top);
1311 }
1312 }
1313 }
1314}
1315
1316void CFX_Edit::ScrollToCaret() {
1317 SetScrollLimit();
1318
thestig821d59e2016-05-11 12:59:22 -07001319 if (!m_pVT->IsValid())
1320 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001321
thestig821d59e2016-05-11 12:59:22 -07001322 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1323 pIterator->SetAt(m_wpCaret);
1324
Dan Sinclairf528eee2017-02-14 11:52:07 -05001325 CFX_PointF ptHead;
1326 CFX_PointF ptFoot;
thestig821d59e2016-05-11 12:59:22 -07001327 CPVT_Word word;
1328 CPVT_Line line;
1329 if (pIterator->GetWord(word)) {
1330 ptHead.x = word.ptWord.x + word.fWidth;
1331 ptHead.y = word.ptWord.y + word.fAscent;
1332 ptFoot.x = word.ptWord.x + word.fWidth;
1333 ptFoot.y = word.ptWord.y + word.fDescent;
1334 } else if (pIterator->GetLine(line)) {
1335 ptHead.x = line.ptLine.x;
1336 ptHead.y = line.ptLine.y + line.fLineAscent;
1337 ptFoot.x = line.ptLine.x;
1338 ptFoot.y = line.ptLine.y + line.fLineDescent;
1339 }
1340
Dan Sinclairf528eee2017-02-14 11:52:07 -05001341 CFX_PointF ptHeadEdit = VTToEdit(ptHead);
1342 CFX_PointF ptFootEdit = VTToEdit(ptFoot);
thestig821d59e2016-05-11 12:59:22 -07001343 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
dsinclair448c4332016-08-02 12:07:35 -07001344 if (!IsFloatEqual(rcPlate.left, rcPlate.right)) {
1345 if (IsFloatSmaller(ptHeadEdit.x, rcPlate.left) ||
1346 IsFloatEqual(ptHeadEdit.x, rcPlate.left)) {
thestig821d59e2016-05-11 12:59:22 -07001347 SetScrollPosX(ptHead.x);
dsinclair448c4332016-08-02 12:07:35 -07001348 } else if (IsFloatBigger(ptHeadEdit.x, rcPlate.right)) {
thestig821d59e2016-05-11 12:59:22 -07001349 SetScrollPosX(ptHead.x - rcPlate.Width());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001350 }
thestig821d59e2016-05-11 12:59:22 -07001351 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001352
dsinclair448c4332016-08-02 12:07:35 -07001353 if (!IsFloatEqual(rcPlate.top, rcPlate.bottom)) {
1354 if (IsFloatSmaller(ptFootEdit.y, rcPlate.bottom) ||
1355 IsFloatEqual(ptFootEdit.y, rcPlate.bottom)) {
1356 if (IsFloatSmaller(ptHeadEdit.y, rcPlate.top)) {
thestig821d59e2016-05-11 12:59:22 -07001357 SetScrollPosY(ptFoot.y + rcPlate.Height());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001358 }
dsinclair448c4332016-08-02 12:07:35 -07001359 } else if (IsFloatBigger(ptHeadEdit.y, rcPlate.top)) {
1360 if (IsFloatBigger(ptFootEdit.y, rcPlate.bottom)) {
thestig821d59e2016-05-11 12:59:22 -07001361 SetScrollPosY(ptHead.y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001362 }
1363 }
1364 }
1365}
1366
dsinclairefd5a992016-07-18 10:04:07 -07001367void CFX_Edit::Refresh() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001368 if (m_bEnableRefresh && m_pVT->IsValid()) {
1369 m_Refresh.BeginRefresh();
1370 RefreshPushLineRects(GetVisibleWordRange());
1371
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001372 m_Refresh.NoAnalyse();
1373 m_ptRefreshScrollPos = m_ptScrollPos;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001374
dsinclaira2919b32016-07-13 10:55:48 -07001375 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001376 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001377 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001378 m_bNotifyFlag = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001379 if (const CFX_Edit_RectArray* pRects = m_Refresh.GetRefreshRects()) {
1380 for (int32_t i = 0, sz = pRects->GetSize(); i < sz; i++)
1381 m_pNotify->IOnInvalidateRect(pRects->GetAt(i));
1382 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001383 }
1384 }
1385
1386 m_Refresh.EndRefresh();
1387 }
1388}
1389
1390void CFX_Edit::RefreshPushLineRects(const CPVT_WordRange& wr) {
thestig821d59e2016-05-11 12:59:22 -07001391 if (!m_pVT->IsValid())
1392 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001393
thestig821d59e2016-05-11 12:59:22 -07001394 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1395 CPVT_WordPlace wpBegin = wr.BeginPos;
1396 m_pVT->UpdateWordPlace(wpBegin);
1397 CPVT_WordPlace wpEnd = wr.EndPos;
1398 m_pVT->UpdateWordPlace(wpEnd);
1399 pIterator->SetAt(wpBegin);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001400
thestig821d59e2016-05-11 12:59:22 -07001401 CPVT_Line lineinfo;
1402 do {
1403 if (!pIterator->GetLine(lineinfo))
1404 break;
1405 if (lineinfo.lineplace.LineCmp(wpEnd) > 0)
1406 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001407
thestig821d59e2016-05-11 12:59:22 -07001408 CFX_FloatRect rcLine(lineinfo.ptLine.x,
1409 lineinfo.ptLine.y + lineinfo.fLineDescent,
1410 lineinfo.ptLine.x + lineinfo.fLineWidth,
1411 lineinfo.ptLine.y + lineinfo.fLineAscent);
1412
1413 m_Refresh.Push(CPVT_WordRange(lineinfo.lineplace, lineinfo.lineEnd),
1414 VTToEdit(rcLine));
1415 } while (pIterator->NextLine());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001416}
1417
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001418void CFX_Edit::RefreshWordRange(const CPVT_WordRange& wr) {
thestig821d59e2016-05-11 12:59:22 -07001419 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1420 CPVT_WordRange wrTemp = wr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001421
thestig821d59e2016-05-11 12:59:22 -07001422 m_pVT->UpdateWordPlace(wrTemp.BeginPos);
1423 m_pVT->UpdateWordPlace(wrTemp.EndPos);
1424 pIterator->SetAt(wrTemp.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001425
thestig821d59e2016-05-11 12:59:22 -07001426 CPVT_Word wordinfo;
1427 CPVT_Line lineinfo;
1428 CPVT_WordPlace place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001429
thestig821d59e2016-05-11 12:59:22 -07001430 while (pIterator->NextWord()) {
1431 place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -07001432 if (place > wrTemp.EndPos)
thestig821d59e2016-05-11 12:59:22 -07001433 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001434
thestig821d59e2016-05-11 12:59:22 -07001435 pIterator->GetWord(wordinfo);
1436 pIterator->GetLine(lineinfo);
thestig821d59e2016-05-11 12:59:22 -07001437 if (place.LineCmp(wrTemp.BeginPos) == 0 ||
1438 place.LineCmp(wrTemp.EndPos) == 0) {
1439 CFX_FloatRect rcWord(wordinfo.ptWord.x,
1440 lineinfo.ptLine.y + lineinfo.fLineDescent,
1441 wordinfo.ptWord.x + wordinfo.fWidth,
1442 lineinfo.ptLine.y + lineinfo.fLineAscent);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001443
dsinclaira2919b32016-07-13 10:55:48 -07001444 if (m_pNotify) {
thestig821d59e2016-05-11 12:59:22 -07001445 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001446 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001447 m_bNotifyFlag = true;
thestig821d59e2016-05-11 12:59:22 -07001448 CFX_FloatRect rcRefresh = VTToEdit(rcWord);
1449 m_pNotify->IOnInvalidateRect(&rcRefresh);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001450 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001451 }
thestig821d59e2016-05-11 12:59:22 -07001452 } else {
1453 CFX_FloatRect rcLine(lineinfo.ptLine.x,
1454 lineinfo.ptLine.y + lineinfo.fLineDescent,
1455 lineinfo.ptLine.x + lineinfo.fLineWidth,
1456 lineinfo.ptLine.y + lineinfo.fLineAscent);
1457
dsinclaira2919b32016-07-13 10:55:48 -07001458 if (m_pNotify) {
thestig821d59e2016-05-11 12:59:22 -07001459 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001460 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001461 m_bNotifyFlag = true;
thestig821d59e2016-05-11 12:59:22 -07001462 CFX_FloatRect rcRefresh = VTToEdit(rcLine);
1463 m_pNotify->IOnInvalidateRect(&rcRefresh);
thestig821d59e2016-05-11 12:59:22 -07001464 }
1465 }
1466
1467 pIterator->NextLine();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001468 }
1469 }
1470}
1471
1472void CFX_Edit::SetCaret(const CPVT_WordPlace& place) {
1473 m_wpOldCaret = m_wpCaret;
1474 m_wpCaret = place;
1475}
1476
1477void CFX_Edit::SetCaretInfo() {
dsinclaira2919b32016-07-13 10:55:48 -07001478 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001479 if (!m_bNotifyFlag) {
thestig821d59e2016-05-11 12:59:22 -07001480 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1481 pIterator->SetAt(m_wpCaret);
tsepez63f545c2016-09-13 16:08:49 -07001482
Dan Sinclairf528eee2017-02-14 11:52:07 -05001483 CFX_PointF ptHead;
1484 CFX_PointF ptFoot;
thestig821d59e2016-05-11 12:59:22 -07001485 CPVT_Word word;
1486 CPVT_Line line;
1487 if (pIterator->GetWord(word)) {
1488 ptHead.x = word.ptWord.x + word.fWidth;
1489 ptHead.y = word.ptWord.y + word.fAscent;
1490 ptFoot.x = word.ptWord.x + word.fWidth;
1491 ptFoot.y = word.ptWord.y + word.fDescent;
1492 } else if (pIterator->GetLine(line)) {
1493 ptHead.x = line.ptLine.x;
1494 ptHead.y = line.ptLine.y + line.fLineAscent;
1495 ptFoot.x = line.ptLine.x;
1496 ptFoot.y = line.ptLine.y + line.fLineDescent;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001497 }
1498
Lei Zhanga8c2b912017-03-22 17:41:02 -07001499 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001500 m_bNotifyFlag = true;
Tom Sepez52f69b32017-03-21 13:42:38 -07001501 m_pNotify->IOnSetCaret(m_SelState.IsEmpty(), VTToEdit(ptHead),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001502 VTToEdit(ptFoot), m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001503 }
1504 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001505}
1506
Dan Sinclairf528eee2017-02-14 11:52:07 -05001507void CFX_Edit::OnMouseDown(const CFX_PointF& point, bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001508 if (!m_pVT->IsValid())
1509 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001510
Tom Sepez52f69b32017-03-21 13:42:38 -07001511 SelectNone();
1512 SetCaret(m_pVT->SearchWordPlace(EditToVT(point)));
1513 m_SelState.Set(m_wpCaret, m_wpCaret);
1514 ScrollToCaret();
1515 SetCaretOrigin();
1516 SetCaretInfo();
1517}
1518
1519void CFX_Edit::OnMouseMove(const CFX_PointF& point, bool bShift, bool bCtrl) {
1520 if (!m_pVT->IsValid())
1521 return;
1522
1523 SetCaret(m_pVT->SearchWordPlace(EditToVT(point)));
1524 if (m_wpCaret == m_wpOldCaret)
1525 return;
1526
1527 m_SelState.SetEndPos(m_wpCaret);
1528 ScrollToCaret();
1529 Refresh();
1530 SetCaretOrigin();
1531 SetCaretInfo();
1532}
1533
1534void CFX_Edit::OnVK_UP(bool bShift, bool bCtrl) {
1535 if (!m_pVT->IsValid())
1536 return;
1537
1538 SetCaret(m_pVT->GetUpWordPlace(m_wpCaret, m_ptCaret));
1539 if (bShift) {
1540 if (m_SelState.IsEmpty())
1541 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1542 else
1543 m_SelState.SetEndPos(m_wpCaret);
1544
1545 if (m_wpOldCaret != m_wpCaret) {
1546 ScrollToCaret();
1547 Refresh();
1548 SetCaretInfo();
1549 }
1550 } else {
1551 SelectNone();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001552 ScrollToCaret();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001553 SetCaretInfo();
1554 }
1555}
1556
Tom Sepez52f69b32017-03-21 13:42:38 -07001557void CFX_Edit::OnVK_DOWN(bool bShift, bool bCtrl) {
1558 if (!m_pVT->IsValid())
1559 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001560
Tom Sepez52f69b32017-03-21 13:42:38 -07001561 SetCaret(m_pVT->GetDownWordPlace(m_wpCaret, m_ptCaret));
1562 if (bShift) {
1563 if (m_SelState.IsEmpty())
1564 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1565 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001566 m_SelState.SetEndPos(m_wpCaret);
1567
Tom Sepez52f69b32017-03-21 13:42:38 -07001568 if (m_wpOldCaret != m_wpCaret) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001569 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001570 Refresh();
Tom Sepez52f69b32017-03-21 13:42:38 -07001571 SetCaretInfo();
1572 }
1573 } else {
1574 SelectNone();
1575 ScrollToCaret();
1576 SetCaretInfo();
1577 }
1578}
1579
1580void CFX_Edit::OnVK_LEFT(bool bShift, bool bCtrl) {
1581 if (!m_pVT->IsValid())
1582 return;
1583
1584 if (bShift) {
1585 if (m_wpCaret == m_pVT->GetLineBeginPlace(m_wpCaret) &&
1586 m_wpCaret != m_pVT->GetSectionBeginPlace(m_wpCaret)) {
1587 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1588 }
1589 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1590 if (m_SelState.IsEmpty())
1591 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1592 else
1593 m_SelState.SetEndPos(m_wpCaret);
1594
1595 if (m_wpOldCaret != m_wpCaret) {
1596 ScrollToCaret();
1597 Refresh();
1598 SetCaretInfo();
1599 }
1600 } else {
1601 if (!m_SelState.IsEmpty()) {
1602 if (m_SelState.BeginPos < m_SelState.EndPos)
1603 SetCaret(m_SelState.BeginPos);
1604 else
1605 SetCaret(m_SelState.EndPos);
1606
1607 SelectNone();
1608 ScrollToCaret();
1609 SetCaretInfo();
1610 } else {
1611 if (m_wpCaret == m_pVT->GetLineBeginPlace(m_wpCaret) &&
1612 m_wpCaret != m_pVT->GetSectionBeginPlace(m_wpCaret)) {
1613 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1614 }
1615 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1616 ScrollToCaret();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001617 SetCaretOrigin();
1618 SetCaretInfo();
1619 }
1620 }
1621}
1622
tsepez4cf55152016-11-02 14:37:54 -07001623void CFX_Edit::OnVK_RIGHT(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001624 if (!m_pVT->IsValid())
1625 return;
1626
1627 if (bShift) {
1628 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1629 if (m_wpCaret == m_pVT->GetLineEndPlace(m_wpCaret) &&
1630 m_wpCaret != m_pVT->GetSectionEndPlace(m_wpCaret))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001631 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1632
Tom Sepez52f69b32017-03-21 13:42:38 -07001633 if (m_SelState.IsEmpty())
1634 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1635 else
1636 m_SelState.SetEndPos(m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001637
Tom Sepez52f69b32017-03-21 13:42:38 -07001638 if (m_wpOldCaret != m_wpCaret) {
1639 ScrollToCaret();
1640 Refresh();
1641 SetCaretInfo();
1642 }
1643 } else {
1644 if (!m_SelState.IsEmpty()) {
1645 if (m_SelState.BeginPos > m_SelState.EndPos)
1646 SetCaret(m_SelState.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001647 else
Tom Sepez52f69b32017-03-21 13:42:38 -07001648 SetCaret(m_SelState.EndPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001649
Tom Sepez52f69b32017-03-21 13:42:38 -07001650 SelectNone();
1651 ScrollToCaret();
1652 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001653 } else {
Tom Sepez52f69b32017-03-21 13:42:38 -07001654 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1655 if (m_wpCaret == m_pVT->GetLineEndPlace(m_wpCaret) &&
1656 m_wpCaret != m_pVT->GetSectionEndPlace(m_wpCaret)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001657 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001658 }
Tom Sepez52f69b32017-03-21 13:42:38 -07001659 ScrollToCaret();
1660 SetCaretOrigin();
1661 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001662 }
1663 }
1664}
1665
tsepez4cf55152016-11-02 14:37:54 -07001666void CFX_Edit::OnVK_HOME(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001667 if (!m_pVT->IsValid())
1668 return;
1669
1670 if (bShift) {
1671 if (bCtrl)
1672 SetCaret(m_pVT->GetBeginWordPlace());
1673 else
1674 SetCaret(m_pVT->GetLineBeginPlace(m_wpCaret));
1675
1676 if (m_SelState.IsEmpty())
1677 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1678 else
1679 m_SelState.SetEndPos(m_wpCaret);
1680
1681 ScrollToCaret();
1682 Refresh();
1683 SetCaretInfo();
1684 } else {
1685 if (!m_SelState.IsEmpty()) {
1686 SetCaret(std::min(m_SelState.BeginPos, m_SelState.EndPos));
1687 SelectNone();
1688 ScrollToCaret();
1689 SetCaretInfo();
1690 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001691 if (bCtrl)
1692 SetCaret(m_pVT->GetBeginWordPlace());
1693 else
1694 SetCaret(m_pVT->GetLineBeginPlace(m_wpCaret));
1695
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001696 ScrollToCaret();
Tom Sepez52f69b32017-03-21 13:42:38 -07001697 SetCaretOrigin();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001698 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001699 }
1700 }
1701}
1702
tsepez4cf55152016-11-02 14:37:54 -07001703void CFX_Edit::OnVK_END(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001704 if (!m_pVT->IsValid())
1705 return;
1706
1707 if (bShift) {
1708 if (bCtrl)
1709 SetCaret(m_pVT->GetEndWordPlace());
1710 else
1711 SetCaret(m_pVT->GetLineEndPlace(m_wpCaret));
1712
1713 if (m_SelState.IsEmpty())
1714 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1715 else
1716 m_SelState.SetEndPos(m_wpCaret);
1717
1718 ScrollToCaret();
1719 Refresh();
1720 SetCaretInfo();
1721 } else {
1722 if (!m_SelState.IsEmpty()) {
1723 SetCaret(std::max(m_SelState.BeginPos, m_SelState.EndPos));
1724 SelectNone();
1725 ScrollToCaret();
1726 SetCaretInfo();
1727 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001728 if (bCtrl)
1729 SetCaret(m_pVT->GetEndWordPlace());
1730 else
1731 SetCaret(m_pVT->GetLineEndPlace(m_wpCaret));
1732
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001733 ScrollToCaret();
Tom Sepez52f69b32017-03-21 13:42:38 -07001734 SetCaretOrigin();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001735 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001736 }
1737 }
1738}
1739
tsepez4cf55152016-11-02 14:37:54 -07001740bool CFX_Edit::InsertWord(uint16_t word,
1741 int32_t charset,
1742 const CPVT_WordProps* pWordProps,
1743 bool bAddUndo,
1744 bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001745 if (IsTextOverflow() || !m_pVT->IsValid())
tsepez4cf55152016-11-02 14:37:54 -07001746 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001747
Tom Sepez3509d162017-01-30 13:22:02 -08001748 m_pVT->UpdateWordPlace(m_wpCaret);
1749 SetCaret(m_pVT->InsertWord(m_wpCaret, word,
1750 GetCharSetFromUnicode(word, charset), pWordProps));
1751 m_SelState.Set(m_wpCaret, m_wpCaret);
1752 if (m_wpCaret == m_wpOldCaret)
1753 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001754
Tom Sepez3509d162017-01-30 13:22:02 -08001755 if (bAddUndo && m_bEnableUndo) {
1756 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertWord>(
1757 this, m_wpOldCaret, m_wpCaret, word, charset, pWordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001758 }
Tom Sepez3509d162017-01-30 13:22:02 -08001759 if (bPaint)
1760 PaintInsertText(m_wpOldCaret, m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001761
Tom Sepez3509d162017-01-30 13:22:02 -08001762 if (m_bOprNotify && m_pOprNotify)
1763 m_pOprNotify->OnInsertWord(m_wpCaret, m_wpOldCaret);
1764
1765 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001766}
1767
tsepez4cf55152016-11-02 14:37:54 -07001768bool CFX_Edit::InsertReturn(const CPVT_SecProps* pSecProps,
1769 const CPVT_WordProps* pWordProps,
1770 bool bAddUndo,
1771 bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001772 if (IsTextOverflow() || !m_pVT->IsValid())
tsepez4cf55152016-11-02 14:37:54 -07001773 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001774
Tom Sepez3509d162017-01-30 13:22:02 -08001775 m_pVT->UpdateWordPlace(m_wpCaret);
1776 SetCaret(m_pVT->InsertSection(m_wpCaret, pSecProps, pWordProps));
1777 m_SelState.Set(m_wpCaret, m_wpCaret);
1778 if (m_wpCaret == m_wpOldCaret)
1779 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001780
Tom Sepez3509d162017-01-30 13:22:02 -08001781 if (bAddUndo && m_bEnableUndo) {
1782 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertReturn>(
1783 this, m_wpOldCaret, m_wpCaret, pSecProps, pWordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001784 }
Tom Sepez3509d162017-01-30 13:22:02 -08001785 if (bPaint) {
1786 RearrangePart(CPVT_WordRange(m_wpOldCaret, m_wpCaret));
1787 ScrollToCaret();
1788 Refresh();
1789 SetCaretOrigin();
1790 SetCaretInfo();
1791 }
1792 if (m_bOprNotify && m_pOprNotify)
1793 m_pOprNotify->OnInsertReturn(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001794
Tom Sepez3509d162017-01-30 13:22:02 -08001795 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001796}
1797
tsepez4cf55152016-11-02 14:37:54 -07001798bool CFX_Edit::Backspace(bool bAddUndo, bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001799 if (!m_pVT->IsValid() || m_wpCaret == m_pVT->GetBeginWordPlace())
1800 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001801
Tom Sepez3509d162017-01-30 13:22:02 -08001802 CPVT_Section section;
1803 CPVT_Word word;
1804 if (bAddUndo) {
1805 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1806 pIterator->SetAt(m_wpCaret);
1807 pIterator->GetSection(section);
1808 pIterator->GetWord(word);
1809 }
1810 m_pVT->UpdateWordPlace(m_wpCaret);
1811 SetCaret(m_pVT->BackSpaceWord(m_wpCaret));
1812 m_SelState.Set(m_wpCaret, m_wpCaret);
1813 if (m_wpCaret == m_wpOldCaret)
1814 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001815
Tom Sepez3509d162017-01-30 13:22:02 -08001816 if (bAddUndo && m_bEnableUndo) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001817 if (m_wpCaret.nSecIndex != m_wpOldCaret.nSecIndex) {
Tom Sepez3509d162017-01-30 13:22:02 -08001818 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Backspace>(
1819 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1820 section.SecProps, section.WordProps));
1821 } else {
1822 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Backspace>(
1823 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1824 section.SecProps, word.WordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001825 }
1826 }
Tom Sepez3509d162017-01-30 13:22:02 -08001827 if (bPaint) {
1828 RearrangePart(CPVT_WordRange(m_wpCaret, m_wpOldCaret));
1829 ScrollToCaret();
1830 Refresh();
1831 SetCaretOrigin();
1832 SetCaretInfo();
1833 }
1834 if (m_bOprNotify && m_pOprNotify)
1835 m_pOprNotify->OnBackSpace(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001836
Tom Sepez3509d162017-01-30 13:22:02 -08001837 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001838}
1839
tsepez4cf55152016-11-02 14:37:54 -07001840bool CFX_Edit::Delete(bool bAddUndo, bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001841 if (!m_pVT->IsValid() || m_wpCaret == m_pVT->GetEndWordPlace())
1842 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001843
Tom Sepez3509d162017-01-30 13:22:02 -08001844 CPVT_Section section;
1845 CPVT_Word word;
1846 if (bAddUndo) {
1847 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1848 pIterator->SetAt(m_pVT->GetNextWordPlace(m_wpCaret));
1849 pIterator->GetSection(section);
1850 pIterator->GetWord(word);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001851 }
Tom Sepez3509d162017-01-30 13:22:02 -08001852 m_pVT->UpdateWordPlace(m_wpCaret);
1853 bool bSecEnd = (m_wpCaret == m_pVT->GetSectionEndPlace(m_wpCaret));
1854 SetCaret(m_pVT->DeleteWord(m_wpCaret));
1855 m_SelState.Set(m_wpCaret, m_wpCaret);
1856 if (bAddUndo && m_bEnableUndo) {
1857 if (bSecEnd) {
1858 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Delete>(
1859 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1860 section.SecProps, section.WordProps, bSecEnd));
1861 } else {
1862 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Delete>(
1863 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1864 section.SecProps, word.WordProps, bSecEnd));
1865 }
1866 }
1867 if (bPaint) {
1868 RearrangePart(CPVT_WordRange(m_wpOldCaret, m_wpCaret));
1869 ScrollToCaret();
1870 Refresh();
1871 SetCaretOrigin();
1872 SetCaretInfo();
1873 }
1874 if (m_bOprNotify && m_pOprNotify)
1875 m_pOprNotify->OnDelete(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001876
Tom Sepez3509d162017-01-30 13:22:02 -08001877 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001878}
1879
tsepez4cf55152016-11-02 14:37:54 -07001880bool CFX_Edit::Empty() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001881 if (m_pVT->IsValid()) {
1882 m_pVT->DeleteWords(GetWholeWordRange());
1883 SetCaret(m_pVT->GetBeginWordPlace());
1884
tsepez4cf55152016-11-02 14:37:54 -07001885 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001886 }
1887
tsepez4cf55152016-11-02 14:37:54 -07001888 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001889}
1890
tsepez4cf55152016-11-02 14:37:54 -07001891bool CFX_Edit::Clear(bool bAddUndo, bool bPaint) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001892 if (!m_pVT->IsValid() || m_SelState.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001893 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001894
thestig821d59e2016-05-11 12:59:22 -07001895 CPVT_WordRange range = m_SelState.ConvertToWordRange();
dsinclaira2919b32016-07-13 10:55:48 -07001896 if (bAddUndo && m_bEnableUndo)
Tom Sepez3509d162017-01-30 13:22:02 -08001897 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Clear>(this, range, GetSelText()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001898
thestig821d59e2016-05-11 12:59:22 -07001899 SelectNone();
1900 SetCaret(m_pVT->DeleteWords(range));
1901 m_SelState.Set(m_wpCaret, m_wpCaret);
thestig821d59e2016-05-11 12:59:22 -07001902 if (bPaint) {
1903 RearrangePart(range);
1904 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001905 Refresh();
thestig821d59e2016-05-11 12:59:22 -07001906 SetCaretOrigin();
1907 SetCaretInfo();
1908 }
thestig821d59e2016-05-11 12:59:22 -07001909 if (m_bOprNotify && m_pOprNotify)
1910 m_pOprNotify->OnClear(m_wpCaret, m_wpOldCaret);
1911
tsepez4cf55152016-11-02 14:37:54 -07001912 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001913}
1914
tsepez4cf55152016-11-02 14:37:54 -07001915bool CFX_Edit::InsertText(const CFX_WideString& sText,
1916 int32_t charset,
1917 bool bAddUndo,
1918 bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001919 if (IsTextOverflow())
tsepez4cf55152016-11-02 14:37:54 -07001920 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001921
1922 m_pVT->UpdateWordPlace(m_wpCaret);
tsepeza31da742016-09-08 11:28:14 -07001923 SetCaret(DoInsertText(m_wpCaret, sText, charset));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001924 m_SelState.Set(m_wpCaret, m_wpCaret);
tsepeza31da742016-09-08 11:28:14 -07001925 if (m_wpCaret == m_wpOldCaret)
tsepez4cf55152016-11-02 14:37:54 -07001926 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001927
tsepeza31da742016-09-08 11:28:14 -07001928 if (bAddUndo && m_bEnableUndo) {
Tom Sepez3509d162017-01-30 13:22:02 -08001929 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertText>(
1930 this, m_wpOldCaret, m_wpCaret, sText, charset));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001931 }
tsepeza31da742016-09-08 11:28:14 -07001932 if (bPaint)
1933 PaintInsertText(m_wpOldCaret, m_wpCaret);
1934
1935 if (m_bOprNotify && m_pOprNotify)
1936 m_pOprNotify->OnInsertText(m_wpCaret, m_wpOldCaret);
1937
tsepez4cf55152016-11-02 14:37:54 -07001938 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001939}
1940
1941void CFX_Edit::PaintInsertText(const CPVT_WordPlace& wpOld,
1942 const CPVT_WordPlace& wpNew) {
1943 if (m_pVT->IsValid()) {
1944 RearrangePart(CPVT_WordRange(wpOld, wpNew));
1945 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001946 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001947 SetCaretOrigin();
1948 SetCaretInfo();
1949 }
1950}
1951
tsepez4cf55152016-11-02 14:37:54 -07001952bool CFX_Edit::Redo() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001953 if (m_bEnableUndo) {
1954 if (m_Undo.CanRedo()) {
1955 m_Undo.Redo();
tsepez4cf55152016-11-02 14:37:54 -07001956 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001957 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001958 }
1959
tsepez4cf55152016-11-02 14:37:54 -07001960 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001961}
1962
tsepez4cf55152016-11-02 14:37:54 -07001963bool CFX_Edit::Undo() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001964 if (m_bEnableUndo) {
1965 if (m_Undo.CanUndo()) {
1966 m_Undo.Undo();
tsepez4cf55152016-11-02 14:37:54 -07001967 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001968 }
1969 }
1970
tsepez4cf55152016-11-02 14:37:54 -07001971 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001972}
1973
1974void CFX_Edit::SetCaretOrigin() {
thestig821d59e2016-05-11 12:59:22 -07001975 if (!m_pVT->IsValid())
1976 return;
1977
1978 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1979 pIterator->SetAt(m_wpCaret);
1980 CPVT_Word word;
1981 CPVT_Line line;
1982 if (pIterator->GetWord(word)) {
1983 m_ptCaret.x = word.ptWord.x + word.fWidth;
1984 m_ptCaret.y = word.ptWord.y;
1985 } else if (pIterator->GetLine(line)) {
1986 m_ptCaret.x = line.ptLine.x;
1987 m_ptCaret.y = line.ptLine.y;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001988 }
1989}
1990
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001991CPVT_WordPlace CFX_Edit::WordIndexToWordPlace(int32_t index) const {
1992 if (m_pVT->IsValid())
1993 return m_pVT->WordIndexToWordPlace(index);
1994
1995 return CPVT_WordPlace();
1996}
1997
tsepez4cf55152016-11-02 14:37:54 -07001998bool CFX_Edit::IsTextFull() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001999 int32_t nTotalWords = m_pVT->GetTotalWords();
2000 int32_t nLimitChar = m_pVT->GetLimitChar();
2001 int32_t nCharArray = m_pVT->GetCharArray();
2002
2003 return IsTextOverflow() || (nLimitChar > 0 && nTotalWords >= nLimitChar) ||
2004 (nCharArray > 0 && nTotalWords >= nCharArray);
2005}
2006
tsepez4cf55152016-11-02 14:37:54 -07002007bool CFX_Edit::IsTextOverflow() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002008 if (!m_bEnableScroll && !m_bEnableOverflow) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08002009 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
2010 CFX_FloatRect rcContent = m_pVT->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002011
dsinclair448c4332016-08-02 12:07:35 -07002012 if (m_pVT->IsMultiLine() && GetTotalLines() > 1 &&
2013 IsFloatBigger(rcContent.Height(), rcPlate.Height())) {
tsepez4cf55152016-11-02 14:37:54 -07002014 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002015 }
2016
dsinclair448c4332016-08-02 12:07:35 -07002017 if (IsFloatBigger(rcContent.Width(), rcPlate.Width()))
tsepez4cf55152016-11-02 14:37:54 -07002018 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002019 }
2020
tsepez4cf55152016-11-02 14:37:54 -07002021 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002022}
2023
tsepez4cf55152016-11-02 14:37:54 -07002024bool CFX_Edit::CanUndo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002025 if (m_bEnableUndo) {
2026 return m_Undo.CanUndo();
2027 }
2028
tsepez4cf55152016-11-02 14:37:54 -07002029 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002030}
2031
tsepez4cf55152016-11-02 14:37:54 -07002032bool CFX_Edit::CanRedo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002033 if (m_bEnableUndo) {
2034 return m_Undo.CanRedo();
2035 }
2036
tsepez4cf55152016-11-02 14:37:54 -07002037 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002038}
2039
tsepez4cf55152016-11-02 14:37:54 -07002040void CFX_Edit::EnableRefresh(bool bRefresh) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002041 m_bEnableRefresh = bRefresh;
2042}
2043
tsepez4cf55152016-11-02 14:37:54 -07002044void CFX_Edit::EnableUndo(bool bUndo) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002045 m_bEnableUndo = bUndo;
2046}
2047
tsepez4cf55152016-11-02 14:37:54 -07002048void CFX_Edit::EnableOprNotify(bool bNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002049 m_bOprNotify = bNotify;
2050}
2051
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002052CPVT_WordPlace CFX_Edit::DoInsertText(const CPVT_WordPlace& place,
tsepeza31da742016-09-08 11:28:14 -07002053 const CFX_WideString& sText,
dsinclairefd5a992016-07-18 10:04:07 -07002054 int32_t charset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002055 CPVT_WordPlace wp = place;
2056
2057 if (m_pVT->IsValid()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002058 for (int32_t i = 0, sz = sText.GetLength(); i < sz; i++) {
Tom Sepez62a70f92016-03-21 15:00:20 -07002059 uint16_t word = sText[i];
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002060 switch (word) {
2061 case 0x0D:
dsinclairefd5a992016-07-18 10:04:07 -07002062 wp = m_pVT->InsertSection(wp, nullptr, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002063 if (sText[i + 1] == 0x0A)
2064 i++;
2065 break;
2066 case 0x0A:
dsinclairefd5a992016-07-18 10:04:07 -07002067 wp = m_pVT->InsertSection(wp, nullptr, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002068 if (sText[i + 1] == 0x0D)
2069 i++;
2070 break;
2071 case 0x09:
2072 word = 0x20;
2073 default:
2074 wp = m_pVT->InsertWord(wp, word, GetCharSetFromUnicode(word, charset),
dsinclairefd5a992016-07-18 10:04:07 -07002075 nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002076 break;
2077 }
2078 }
2079 }
2080
2081 return wp;
2082}
2083
Tom Sepez62a70f92016-03-21 15:00:20 -07002084int32_t CFX_Edit::GetCharSetFromUnicode(uint16_t word, int32_t nOldCharset) {
dsinclairc7a73492016-04-05 12:01:42 -07002085 if (IPVT_FontMap* pFontMap = GetFontMap())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002086 return pFontMap->CharSetFromUnicode(word, nOldCharset);
2087 return nOldCharset;
2088}
2089
Tom Sepez3509d162017-01-30 13:22:02 -08002090void CFX_Edit::AddEditUndoItem(
2091 std::unique_ptr<CFX_Edit_UndoItem> pEditUndoItem) {
Lei Zhang1a89e362017-03-23 15:27:25 -07002092 m_Undo.AddItem(std::move(pEditUndoItem));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002093}
2094
weili625ad662016-06-15 11:21:33 -07002095CFX_Edit_LineRectArray::CFX_Edit_LineRectArray() {}
2096
Tom Sepez3509d162017-01-30 13:22:02 -08002097CFX_Edit_LineRectArray::~CFX_Edit_LineRectArray() {}
weili625ad662016-06-15 11:21:33 -07002098
Tom Sepez3509d162017-01-30 13:22:02 -08002099void CFX_Edit_LineRectArray::operator=(CFX_Edit_LineRectArray&& that) {
2100 m_LineRects = std::move(that.m_LineRects);
weili625ad662016-06-15 11:21:33 -07002101}
2102
2103void CFX_Edit_LineRectArray::Add(const CPVT_WordRange& wrLine,
2104 const CFX_FloatRect& rcLine) {
Tom Sepez3509d162017-01-30 13:22:02 -08002105 m_LineRects.push_back(pdfium::MakeUnique<CFX_Edit_LineRect>(wrLine, rcLine));
weili625ad662016-06-15 11:21:33 -07002106}
2107
2108int32_t CFX_Edit_LineRectArray::GetSize() const {
Tom Sepez3509d162017-01-30 13:22:02 -08002109 return pdfium::CollectionSize<int32_t>(m_LineRects);
weili625ad662016-06-15 11:21:33 -07002110}
2111
2112CFX_Edit_LineRect* CFX_Edit_LineRectArray::GetAt(int32_t nIndex) const {
Tom Sepez3509d162017-01-30 13:22:02 -08002113 if (nIndex < 0 || nIndex >= GetSize())
weili625ad662016-06-15 11:21:33 -07002114 return nullptr;
2115
Tom Sepez3509d162017-01-30 13:22:02 -08002116 return m_LineRects[nIndex].get();
weili625ad662016-06-15 11:21:33 -07002117}
2118
2119CFX_Edit_Select::CFX_Edit_Select() {}
2120
weili625ad662016-06-15 11:21:33 -07002121CFX_Edit_Select::CFX_Edit_Select(const CPVT_WordRange& range) {
2122 Set(range.BeginPos, range.EndPos);
2123}
2124
2125CPVT_WordRange CFX_Edit_Select::ConvertToWordRange() const {
2126 return CPVT_WordRange(BeginPos, EndPos);
2127}
2128
Tom Sepez52f69b32017-03-21 13:42:38 -07002129void CFX_Edit_Select::Reset() {
2130 BeginPos.Reset();
2131 EndPos.Reset();
weili625ad662016-06-15 11:21:33 -07002132}
2133
2134void CFX_Edit_Select::Set(const CPVT_WordPlace& begin,
2135 const CPVT_WordPlace& end) {
2136 BeginPos = begin;
2137 EndPos = end;
2138}
2139
weili625ad662016-06-15 11:21:33 -07002140void CFX_Edit_Select::SetEndPos(const CPVT_WordPlace& end) {
2141 EndPos = end;
2142}
2143
Tom Sepez52f69b32017-03-21 13:42:38 -07002144bool CFX_Edit_Select::IsEmpty() const {
2145 return BeginPos == EndPos;
weili625ad662016-06-15 11:21:33 -07002146}
2147
weili625ad662016-06-15 11:21:33 -07002148CFX_Edit_RectArray::CFX_Edit_RectArray() {}
2149
Tom Sepez3509d162017-01-30 13:22:02 -08002150CFX_Edit_RectArray::~CFX_Edit_RectArray() {}
weili625ad662016-06-15 11:21:33 -07002151
Tom Sepez3509d162017-01-30 13:22:02 -08002152void CFX_Edit_RectArray::Clear() {
2153 m_Rects.clear();
weili625ad662016-06-15 11:21:33 -07002154}
2155
2156void CFX_Edit_RectArray::Add(const CFX_FloatRect& rect) {
2157 // check for overlapped area
Tom Sepez3509d162017-01-30 13:22:02 -08002158 for (const auto& pRect : m_Rects) {
weili625ad662016-06-15 11:21:33 -07002159 if (pRect && pRect->Contains(rect))
2160 return;
2161 }
Tom Sepez3509d162017-01-30 13:22:02 -08002162 m_Rects.push_back(pdfium::MakeUnique<CFX_FloatRect>(rect));
weili625ad662016-06-15 11:21:33 -07002163}
2164
2165int32_t CFX_Edit_RectArray::GetSize() const {
Tom Sepez3509d162017-01-30 13:22:02 -08002166 return pdfium::CollectionSize<int32_t>(m_Rects);
weili625ad662016-06-15 11:21:33 -07002167}
2168
2169CFX_FloatRect* CFX_Edit_RectArray::GetAt(int32_t nIndex) const {
Tom Sepez3509d162017-01-30 13:22:02 -08002170 if (nIndex < 0 || nIndex >= GetSize())
weili625ad662016-06-15 11:21:33 -07002171 return nullptr;
2172
Tom Sepez3509d162017-01-30 13:22:02 -08002173 return m_Rects[nIndex].get();
weili625ad662016-06-15 11:21:33 -07002174}