blob: cf382f84267c221c6b8f61f5bb5f8f2154ae2f91 [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>
Henrique Nakashima2e2da132017-06-27 13:43:22 -040011#include <sstream>
thestigd4c34f22016-09-28 17:04:51 -070012#include <utility>
Lei Zhang375a8642016-01-11 11:59:17 -080013
dsinclairbc5e6d22016-10-04 11:08:49 -070014#include "core/fpdfapi/font/cpdf_font.h"
dsinclair41872fa2016-10-04 11:29:35 -070015#include "core/fpdfapi/page/cpdf_pageobject.h"
16#include "core/fpdfapi/page/cpdf_pageobjectholder.h"
17#include "core/fpdfapi/page/cpdf_pathobject.h"
18#include "core/fpdfapi/page/cpdf_textobject.h"
dsinclair488b7ad2016-10-04 11:55:50 -070019#include "core/fpdfapi/parser/fpdf_parser_decode.h"
dsinclair69d9c682016-10-04 12:18:35 -070020#include "core/fpdfapi/render/cpdf_renderoptions.h"
21#include "core/fpdfapi/render/cpdf_textrenderer.h"
dsinclair1727aee2016-09-29 13:12:56 -070022#include "core/fpdfdoc/cpvt_section.h"
23#include "core/fpdfdoc/cpvt_word.h"
24#include "core/fpdfdoc/ipvt_fontmap.h"
Dan Sinclairf51a02a2017-04-19 12:46:53 -040025#include "core/fxcrt/fx_codepage.h"
dsinclair74a34fc2016-09-29 16:41:42 -070026#include "core/fxge/cfx_graphstatedata.h"
27#include "core/fxge/cfx_pathdata.h"
28#include "core/fxge/cfx_renderdevice.h"
dsinclaire35af1e2016-07-13 11:26:20 -070029#include "fpdfsdk/cfx_systemhandler.h"
dsinclair0bb385b2016-09-29 17:03:59 -070030#include "fpdfsdk/fxedit/fx_edit.h"
Lei Zhang633a3b72017-06-02 15:27:22 -070031#include "fpdfsdk/pdfwindow/cpwl_edit.h"
32#include "fpdfsdk/pdfwindow/cpwl_edit_ctrl.h"
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -040033#include "fpdfsdk/pdfwindow/cpwl_scroll_bar.h"
tsepez36eb4bd2016-10-03 15:24:27 -070034#include "third_party/base/ptr_util.h"
Tom Sepez3509d162017-01-30 13:22:02 -080035#include "third_party/base/stl_util.h"
dsinclaire35af1e2016-07-13 11:26:20 -070036
dsinclair8f4bf9a2016-05-04 13:51:51 -070037namespace {
38
39const int kEditUndoMaxItems = 10000;
40
dsinclaire35af1e2016-07-13 11:26:20 -070041CFX_ByteString GetWordRenderString(const CFX_ByteString& strWords) {
42 if (strWords.GetLength() > 0)
Nicolas Pena995a6012017-06-20 17:42:39 -040043 return PDF_EncodeString(strWords, false) + " Tj\n";
dsinclaire35af1e2016-07-13 11:26:20 -070044 return CFX_ByteString();
45}
46
47CFX_ByteString GetFontSetString(IPVT_FontMap* pFontMap,
48 int32_t nFontIndex,
Dan Sinclair05df0752017-03-14 14:43:42 -040049 float fFontSize) {
dsinclaire35af1e2016-07-13 11:26:20 -070050 if (!pFontMap)
51 return CFX_ByteString();
52
53 CFX_ByteString sFontAlias = pFontMap->GetPDFFontAlias(nFontIndex);
54 if (sFontAlias.GetLength() <= 0 || fFontSize <= 0)
55 return CFX_ByteString();
56
Henrique Nakashima2e2da132017-06-27 13:43:22 -040057 std::ostringstream sRet;
dsinclaire35af1e2016-07-13 11:26:20 -070058 sRet << "/" << sFontAlias << " " << fFontSize << " Tf\n";
Henrique Nakashima2e2da132017-06-27 13:43:22 -040059 return CFX_ByteString(sRet);
dsinclaire35af1e2016-07-13 11:26:20 -070060}
61
dsinclaire35af1e2016-07-13 11:26:20 -070062void DrawTextString(CFX_RenderDevice* pDevice,
Dan Sinclairf528eee2017-02-14 11:52:07 -050063 const CFX_PointF& pt,
dsinclaire35af1e2016-07-13 11:26:20 -070064 CPDF_Font* pFont,
Dan Sinclair05df0752017-03-14 14:43:42 -040065 float fFontSize,
dsinclaire35af1e2016-07-13 11:26:20 -070066 CFX_Matrix* pUser2Device,
67 const CFX_ByteString& str,
68 FX_ARGB crTextFill,
dsinclaire35af1e2016-07-13 11:26:20 -070069 int32_t nHorzScale) {
Dan Sinclaira0061af2017-02-23 09:25:17 -050070 CFX_PointF pos = pUser2Device->Transform(pt);
dsinclaire35af1e2016-07-13 11:26:20 -070071
72 if (pFont) {
73 if (nHorzScale != 100) {
74 CFX_Matrix mt(nHorzScale / 100.0f, 0, 0, 1, 0, 0);
75 mt.Concat(*pUser2Device);
76
77 CPDF_RenderOptions ro;
78 ro.m_Flags = RENDER_CLEARTYPE;
Dan Sinclairf55e72e2017-07-13 14:53:28 -040079 ro.m_ColorMode = CPDF_RenderOptions::kNormal;
dsinclaire35af1e2016-07-13 11:26:20 -070080
Dan Sinclaira0061af2017-02-23 09:25:17 -050081 CPDF_TextRenderer::DrawTextString(pDevice, pos.x, pos.y, pFont, fFontSize,
82 &mt, str, crTextFill, nullptr, &ro);
dsinclaire35af1e2016-07-13 11:26:20 -070083 } else {
84 CPDF_RenderOptions ro;
85 ro.m_Flags = RENDER_CLEARTYPE;
Dan Sinclairf55e72e2017-07-13 14:53:28 -040086 ro.m_ColorMode = CPDF_RenderOptions::kNormal;
dsinclaire35af1e2016-07-13 11:26:20 -070087
Dan Sinclaira0061af2017-02-23 09:25:17 -050088 CPDF_TextRenderer::DrawTextString(pDevice, pos.x, pos.y, pFont, fFontSize,
89 pUser2Device, str, crTextFill, nullptr,
90 &ro);
dsinclaire35af1e2016-07-13 11:26:20 -070091 }
92 }
93}
94
dsinclair8f4bf9a2016-05-04 13:51:51 -070095} // namespace
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070096
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097CFX_Edit_Iterator::CFX_Edit_Iterator(CFX_Edit* pEdit,
dsinclairc7a73492016-04-05 12:01:42 -070098 CPDF_VariableText::Iterator* pVTIterator)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099 : m_pEdit(pEdit), m_pVTIterator(pVTIterator) {}
100
101CFX_Edit_Iterator::~CFX_Edit_Iterator() {}
102
tsepez4cf55152016-11-02 14:37:54 -0700103bool CFX_Edit_Iterator::NextWord() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104 return m_pVTIterator->NextWord();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700105}
106
tsepez4cf55152016-11-02 14:37:54 -0700107bool CFX_Edit_Iterator::PrevWord() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108 return m_pVTIterator->PrevWord();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700109}
110
tsepez4cf55152016-11-02 14:37:54 -0700111bool CFX_Edit_Iterator::GetWord(CPVT_Word& word) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700112 ASSERT(m_pEdit);
113
114 if (m_pVTIterator->GetWord(word)) {
115 word.ptWord = m_pEdit->VTToEdit(word.ptWord);
tsepez4cf55152016-11-02 14:37:54 -0700116 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700117 }
tsepez4cf55152016-11-02 14:37:54 -0700118 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700119}
120
tsepez4cf55152016-11-02 14:37:54 -0700121bool CFX_Edit_Iterator::GetLine(CPVT_Line& line) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700122 ASSERT(m_pEdit);
123
124 if (m_pVTIterator->GetLine(line)) {
125 line.ptLine = m_pEdit->VTToEdit(line.ptLine);
tsepez4cf55152016-11-02 14:37:54 -0700126 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127 }
tsepez4cf55152016-11-02 14:37:54 -0700128 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700129}
130
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131void CFX_Edit_Iterator::SetAt(int32_t nWordIndex) {
132 m_pVTIterator->SetAt(nWordIndex);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700133}
134
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135void CFX_Edit_Iterator::SetAt(const CPVT_WordPlace& place) {
136 m_pVTIterator->SetAt(place);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700137}
138
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139const CPVT_WordPlace& CFX_Edit_Iterator::GetAt() const {
140 return m_pVTIterator->GetAt();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700141}
142
dsinclairc7a73492016-04-05 12:01:42 -0700143CFX_Edit_Provider::CFX_Edit_Provider(IPVT_FontMap* pFontMap)
144 : CPDF_VariableText::Provider(pFontMap), m_pFontMap(pFontMap) {
Lei Zhang96660d62015-12-14 18:27:25 -0800145 ASSERT(m_pFontMap);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700146}
147
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700148CFX_Edit_Provider::~CFX_Edit_Provider() {}
149
Tom Sepezd0409af2017-05-25 15:53:57 -0700150IPVT_FontMap* CFX_Edit_Provider::GetFontMap() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151 return m_pFontMap;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700152}
153
npm41d6bbe2016-09-14 11:54:44 -0700154int32_t CFX_Edit_Provider::GetCharWidth(int32_t nFontIndex, uint16_t word) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700155 if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex)) {
tsepezc3255f52016-03-25 14:52:27 -0700156 uint32_t charcode = word;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700157
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158 if (pPDFFont->IsUnicodeCompatible())
159 charcode = pPDFFont->CharCodeFromUnicode(word);
160 else
161 charcode = m_pFontMap->CharCodeFromUnicode(nFontIndex, word);
162
Wei Li89409932016-03-28 10:33:33 -0700163 if (charcode != CPDF_Font::kInvalidCharCode)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700164 return pPDFFont->GetCharWidthF(charcode);
165 }
166
167 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700168}
169
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700170int32_t CFX_Edit_Provider::GetTypeAscent(int32_t nFontIndex) {
171 if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex))
172 return pPDFFont->GetTypeAscent();
173
174 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700175}
176
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177int32_t CFX_Edit_Provider::GetTypeDescent(int32_t nFontIndex) {
178 if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex))
179 return pPDFFont->GetTypeDescent();
180
181 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700182}
183
Tom Sepez62a70f92016-03-21 15:00:20 -0700184int32_t CFX_Edit_Provider::GetWordFontIndex(uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185 int32_t charset,
186 int32_t nFontIndex) {
187 return m_pFontMap->GetWordFontIndex(word, charset, nFontIndex);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700188}
189
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190int32_t CFX_Edit_Provider::GetDefaultFontIndex() {
191 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700192}
193
tsepez4cf55152016-11-02 14:37:54 -0700194bool CFX_Edit_Provider::IsLatinWord(uint16_t word) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195 return FX_EDIT_ISLATINWORD(word);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700196}
197
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198CFX_Edit_Refresh::CFX_Edit_Refresh() {}
199
200CFX_Edit_Refresh::~CFX_Edit_Refresh() {}
201
202void CFX_Edit_Refresh::BeginRefresh() {
Tom Sepez3509d162017-01-30 13:22:02 -0800203 m_RefreshRects.Clear();
204 m_OldLineRects = std::move(m_NewLineRects);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700205}
206
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700207void CFX_Edit_Refresh::Push(const CPVT_WordRange& linerange,
Tom Sepez281a9ea2016-02-26 14:24:28 -0800208 const CFX_FloatRect& rect) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209 m_NewLineRects.Add(linerange, rect);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700210}
211
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212void CFX_Edit_Refresh::NoAnalyse() {
213 {
214 for (int32_t i = 0, sz = m_OldLineRects.GetSize(); i < sz; i++)
215 if (CFX_Edit_LineRect* pOldRect = m_OldLineRects.GetAt(i))
216 m_RefreshRects.Add(pOldRect->m_rcLine);
217 }
218
219 {
220 for (int32_t i = 0, sz = m_NewLineRects.GetSize(); i < sz; i++)
221 if (CFX_Edit_LineRect* pNewRect = m_NewLineRects.GetAt(i))
222 m_RefreshRects.Add(pNewRect->m_rcLine);
223 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700224}
225
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700226const CFX_Edit_RectArray* CFX_Edit_Refresh::GetRefreshRects() const {
227 return &m_RefreshRects;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700228}
229
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700230void CFX_Edit_Refresh::EndRefresh() {
Tom Sepez3509d162017-01-30 13:22:02 -0800231 m_RefreshRects.Clear();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700232}
233
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234CFX_Edit_Undo::CFX_Edit_Undo(int32_t nBufsize)
235 : m_nCurUndoPos(0),
236 m_nBufSize(nBufsize),
tsepez4cf55152016-11-02 14:37:54 -0700237 m_bWorking(false) {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238
239CFX_Edit_Undo::~CFX_Edit_Undo() {
240 Reset();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700241}
242
tsepez4cf55152016-11-02 14:37:54 -0700243bool CFX_Edit_Undo::CanUndo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244 return m_nCurUndoPos > 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700245}
246
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700247void CFX_Edit_Undo::Undo() {
tsepez4cf55152016-11-02 14:37:54 -0700248 m_bWorking = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249 if (m_nCurUndoPos > 0) {
Tom Sepez3509d162017-01-30 13:22:02 -0800250 m_UndoItemStack[m_nCurUndoPos - 1]->Undo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251 m_nCurUndoPos--;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252 }
tsepez4cf55152016-11-02 14:37:54 -0700253 m_bWorking = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700254}
255
tsepez4cf55152016-11-02 14:37:54 -0700256bool CFX_Edit_Undo::CanRedo() const {
Tom Sepez3509d162017-01-30 13:22:02 -0800257 return m_nCurUndoPos < m_UndoItemStack.size();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700258}
259
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260void CFX_Edit_Undo::Redo() {
tsepez4cf55152016-11-02 14:37:54 -0700261 m_bWorking = true;
Tom Sepez3509d162017-01-30 13:22:02 -0800262 if (m_nCurUndoPos < m_UndoItemStack.size()) {
263 m_UndoItemStack[m_nCurUndoPos]->Redo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264 m_nCurUndoPos++;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265 }
tsepez4cf55152016-11-02 14:37:54 -0700266 m_bWorking = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700267}
268
Tom Sepez3509d162017-01-30 13:22:02 -0800269void CFX_Edit_Undo::AddItem(std::unique_ptr<IFX_Edit_UndoItem> pItem) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 ASSERT(!m_bWorking);
Lei Zhang96660d62015-12-14 18:27:25 -0800271 ASSERT(pItem);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272 ASSERT(m_nBufSize > 1);
Tom Sepez3509d162017-01-30 13:22:02 -0800273 if (m_nCurUndoPos < m_UndoItemStack.size())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274 RemoveTails();
275
Lei Zhang1a89e362017-03-23 15:27:25 -0700276 if (m_UndoItemStack.size() >= m_nBufSize)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277 RemoveHeads();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278
Tom Sepez3509d162017-01-30 13:22:02 -0800279 m_UndoItemStack.push_back(std::move(pItem));
280 m_nCurUndoPos = m_UndoItemStack.size();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700281}
282
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700283void CFX_Edit_Undo::RemoveHeads() {
Tom Sepez3509d162017-01-30 13:22:02 -0800284 ASSERT(m_UndoItemStack.size() > 1);
285 m_UndoItemStack.pop_front();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700286}
287
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700288void CFX_Edit_Undo::RemoveTails() {
Tom Sepez3509d162017-01-30 13:22:02 -0800289 while (m_UndoItemStack.size() > m_nCurUndoPos)
290 m_UndoItemStack.pop_back();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700291}
292
293void CFX_Edit_Undo::Reset() {
Tom Sepez3509d162017-01-30 13:22:02 -0800294 m_UndoItemStack.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700295 m_nCurUndoPos = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700296}
297
tsepez4cf55152016-11-02 14:37:54 -0700298CFX_Edit_UndoItem::CFX_Edit_UndoItem() : m_bFirst(true), m_bLast(true) {}
weili625ad662016-06-15 11:21:33 -0700299
300CFX_Edit_UndoItem::~CFX_Edit_UndoItem() {}
301
Tom Sepez3509d162017-01-30 13:22:02 -0800302CFX_WideString CFX_Edit_UndoItem::GetUndoTitle() const {
303 return CFX_WideString();
weili625ad662016-06-15 11:21:33 -0700304}
305
tsepez4cf55152016-11-02 14:37:54 -0700306void CFX_Edit_UndoItem::SetFirst(bool bFirst) {
weili625ad662016-06-15 11:21:33 -0700307 m_bFirst = bFirst;
308}
309
tsepez4cf55152016-11-02 14:37:54 -0700310void CFX_Edit_UndoItem::SetLast(bool bLast) {
weili625ad662016-06-15 11:21:33 -0700311 m_bLast = bLast;
312}
313
tsepez4cf55152016-11-02 14:37:54 -0700314bool CFX_Edit_UndoItem::IsLast() {
weili625ad662016-06-15 11:21:33 -0700315 return m_bLast;
316}
317
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700318CFXEU_InsertWord::CFXEU_InsertWord(CFX_Edit* pEdit,
319 const CPVT_WordPlace& wpOldPlace,
320 const CPVT_WordPlace& wpNewPlace,
Tom Sepez62a70f92016-03-21 15:00:20 -0700321 uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700322 int32_t charset,
323 const CPVT_WordProps* pWordProps)
324 : m_pEdit(pEdit),
325 m_wpOld(wpOldPlace),
326 m_wpNew(wpNewPlace),
327 m_Word(word),
328 m_nCharset(charset),
329 m_WordProps() {
330 if (pWordProps)
331 m_WordProps = *pWordProps;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700332}
333
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700334CFXEU_InsertWord::~CFXEU_InsertWord() {}
335
336void CFXEU_InsertWord::Redo() {
337 if (m_pEdit) {
338 m_pEdit->SelectNone();
339 m_pEdit->SetCaret(m_wpOld);
tsepez4cf55152016-11-02 14:37:54 -0700340 m_pEdit->InsertWord(m_Word, m_nCharset, &m_WordProps, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700341 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700342}
343
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700344void CFXEU_InsertWord::Undo() {
345 if (m_pEdit) {
346 m_pEdit->SelectNone();
347 m_pEdit->SetCaret(m_wpNew);
tsepez4cf55152016-11-02 14:37:54 -0700348 m_pEdit->Backspace(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700349 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700350}
351
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700352CFXEU_InsertReturn::CFXEU_InsertReturn(CFX_Edit* pEdit,
353 const CPVT_WordPlace& wpOldPlace,
354 const CPVT_WordPlace& wpNewPlace,
355 const CPVT_SecProps* pSecProps,
356 const CPVT_WordProps* pWordProps)
357 : m_pEdit(pEdit),
358 m_wpOld(wpOldPlace),
359 m_wpNew(wpNewPlace),
360 m_SecProps(),
361 m_WordProps() {
362 if (pSecProps)
363 m_SecProps = *pSecProps;
364 if (pWordProps)
365 m_WordProps = *pWordProps;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700366}
367
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700368CFXEU_InsertReturn::~CFXEU_InsertReturn() {}
369
370void CFXEU_InsertReturn::Redo() {
371 if (m_pEdit) {
372 m_pEdit->SelectNone();
373 m_pEdit->SetCaret(m_wpOld);
tsepez4cf55152016-11-02 14:37:54 -0700374 m_pEdit->InsertReturn(&m_SecProps, &m_WordProps, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700376}
377
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700378void CFXEU_InsertReturn::Undo() {
379 if (m_pEdit) {
380 m_pEdit->SelectNone();
381 m_pEdit->SetCaret(m_wpNew);
tsepez4cf55152016-11-02 14:37:54 -0700382 m_pEdit->Backspace(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700383 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700384}
385
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700386CFXEU_Backspace::CFXEU_Backspace(CFX_Edit* pEdit,
387 const CPVT_WordPlace& wpOldPlace,
388 const CPVT_WordPlace& wpNewPlace,
Tom Sepez62a70f92016-03-21 15:00:20 -0700389 uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390 int32_t charset,
391 const CPVT_SecProps& SecProps,
392 const CPVT_WordProps& WordProps)
393 : m_pEdit(pEdit),
394 m_wpOld(wpOldPlace),
395 m_wpNew(wpNewPlace),
396 m_Word(word),
397 m_nCharset(charset),
398 m_SecProps(SecProps),
399 m_WordProps(WordProps) {}
400
401CFXEU_Backspace::~CFXEU_Backspace() {}
402
403void CFXEU_Backspace::Redo() {
Tom Sepez52f69b32017-03-21 13:42:38 -0700404 if (!m_pEdit)
405 return;
406
407 m_pEdit->SelectNone();
408 m_pEdit->SetCaret(m_wpOld);
409 m_pEdit->Backspace(false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700410}
411
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700412void CFXEU_Backspace::Undo() {
Tom Sepez52f69b32017-03-21 13:42:38 -0700413 if (!m_pEdit)
414 return;
415
416 m_pEdit->SelectNone();
417 m_pEdit->SetCaret(m_wpNew);
418 if (m_wpNew.nSecIndex != m_wpOld.nSecIndex)
419 m_pEdit->InsertReturn(&m_SecProps, &m_WordProps, false, true);
420 else
421 m_pEdit->InsertWord(m_Word, m_nCharset, &m_WordProps, false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700422}
423
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700424CFXEU_Delete::CFXEU_Delete(CFX_Edit* pEdit,
425 const CPVT_WordPlace& wpOldPlace,
426 const CPVT_WordPlace& wpNewPlace,
Tom Sepez62a70f92016-03-21 15:00:20 -0700427 uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700428 int32_t charset,
429 const CPVT_SecProps& SecProps,
430 const CPVT_WordProps& WordProps,
tsepez4cf55152016-11-02 14:37:54 -0700431 bool bSecEnd)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700432 : m_pEdit(pEdit),
433 m_wpOld(wpOldPlace),
434 m_wpNew(wpNewPlace),
435 m_Word(word),
436 m_nCharset(charset),
437 m_SecProps(SecProps),
438 m_WordProps(WordProps),
439 m_bSecEnd(bSecEnd) {}
440
441CFXEU_Delete::~CFXEU_Delete() {}
442
443void CFXEU_Delete::Redo() {
444 if (m_pEdit) {
445 m_pEdit->SelectNone();
446 m_pEdit->SetCaret(m_wpOld);
tsepez4cf55152016-11-02 14:37:54 -0700447 m_pEdit->Delete(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700448 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700449}
450
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700451void CFXEU_Delete::Undo() {
452 if (m_pEdit) {
453 m_pEdit->SelectNone();
454 m_pEdit->SetCaret(m_wpNew);
455 if (m_bSecEnd) {
tsepez4cf55152016-11-02 14:37:54 -0700456 m_pEdit->InsertReturn(&m_SecProps, &m_WordProps, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700457 } else {
tsepez4cf55152016-11-02 14:37:54 -0700458 m_pEdit->InsertWord(m_Word, m_nCharset, &m_WordProps, false, true);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700459 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700460 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700461}
462
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700463CFXEU_Clear::CFXEU_Clear(CFX_Edit* pEdit,
464 const CPVT_WordRange& wrSel,
465 const CFX_WideString& swText)
466 : m_pEdit(pEdit), m_wrSel(wrSel), m_swText(swText) {}
467
468CFXEU_Clear::~CFXEU_Clear() {}
469
470void CFXEU_Clear::Redo() {
471 if (m_pEdit) {
472 m_pEdit->SelectNone();
473 m_pEdit->SetSel(m_wrSel.BeginPos, m_wrSel.EndPos);
tsepez4cf55152016-11-02 14:37:54 -0700474 m_pEdit->Clear(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700475 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700476}
477
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700478void CFXEU_Clear::Undo() {
479 if (m_pEdit) {
480 m_pEdit->SelectNone();
481 m_pEdit->SetCaret(m_wrSel.BeginPos);
Dan Sinclairf51a02a2017-04-19 12:46:53 -0400482 m_pEdit->InsertText(m_swText, FX_CHARSET_Default, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700483 m_pEdit->SetSel(m_wrSel.BeginPos, m_wrSel.EndPos);
484 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700485}
486
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700487CFXEU_InsertText::CFXEU_InsertText(CFX_Edit* pEdit,
488 const CPVT_WordPlace& wpOldPlace,
489 const CPVT_WordPlace& wpNewPlace,
490 const CFX_WideString& swText,
dsinclairefd5a992016-07-18 10:04:07 -0700491 int32_t charset)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700492 : m_pEdit(pEdit),
493 m_wpOld(wpOldPlace),
494 m_wpNew(wpNewPlace),
495 m_swText(swText),
dsinclairefd5a992016-07-18 10:04:07 -0700496 m_nCharset(charset) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700497
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700498CFXEU_InsertText::~CFXEU_InsertText() {}
499
500void CFXEU_InsertText::Redo() {
501 if (m_pEdit && IsLast()) {
502 m_pEdit->SelectNone();
503 m_pEdit->SetCaret(m_wpOld);
tsepez4cf55152016-11-02 14:37:54 -0700504 m_pEdit->InsertText(m_swText, m_nCharset, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700505 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700506}
507
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700508void CFXEU_InsertText::Undo() {
509 if (m_pEdit) {
510 m_pEdit->SelectNone();
511 m_pEdit->SetSel(m_wpOld, m_wpNew);
tsepez4cf55152016-11-02 14:37:54 -0700512 m_pEdit->Clear(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700513 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700514}
515
dsinclaire35af1e2016-07-13 11:26:20 -0700516// static
517CFX_ByteString CFX_Edit::GetEditAppearanceStream(CFX_Edit* pEdit,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500518 const CFX_PointF& ptOffset,
dsinclaire35af1e2016-07-13 11:26:20 -0700519 const CPVT_WordRange* pRange,
tsepez4cf55152016-11-02 14:37:54 -0700520 bool bContinuous,
dsinclaire35af1e2016-07-13 11:26:20 -0700521 uint16_t SubWord) {
522 CFX_Edit_Iterator* pIterator = pEdit->GetIterator();
523 if (pRange)
524 pIterator->SetAt(pRange->BeginPos);
525 else
526 pIterator->SetAt(0);
527
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400528 std::ostringstream sEditStream;
529 std::ostringstream sWords;
dsinclaire35af1e2016-07-13 11:26:20 -0700530 int32_t nCurFontIndex = -1;
Dan Sinclairf528eee2017-02-14 11:52:07 -0500531 CFX_PointF ptOld;
532 CFX_PointF ptNew;
dsinclaire35af1e2016-07-13 11:26:20 -0700533 CPVT_WordPlace oldplace;
tsepez63f545c2016-09-13 16:08:49 -0700534
dsinclaire35af1e2016-07-13 11:26:20 -0700535 while (pIterator->NextWord()) {
536 CPVT_WordPlace place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -0700537 if (pRange && place > pRange->EndPos)
dsinclaire35af1e2016-07-13 11:26:20 -0700538 break;
539
540 if (bContinuous) {
541 if (place.LineCmp(oldplace) != 0) {
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400542 if (sWords.tellp() > 0) {
543 sEditStream << GetWordRenderString(CFX_ByteString(sWords));
544 sWords.str("");
dsinclaire35af1e2016-07-13 11:26:20 -0700545 }
546
547 CPVT_Word word;
548 if (pIterator->GetWord(word)) {
Dan Sinclairf528eee2017-02-14 11:52:07 -0500549 ptNew = CFX_PointF(word.ptWord.x + ptOffset.x,
550 word.ptWord.y + ptOffset.y);
dsinclaire35af1e2016-07-13 11:26:20 -0700551 } else {
552 CPVT_Line line;
553 pIterator->GetLine(line);
Dan Sinclairf528eee2017-02-14 11:52:07 -0500554 ptNew = CFX_PointF(line.ptLine.x + ptOffset.x,
555 line.ptLine.y + ptOffset.y);
dsinclaire35af1e2016-07-13 11:26:20 -0700556 }
557
558 if (ptNew.x != ptOld.x || ptNew.y != ptOld.y) {
559 sEditStream << ptNew.x - ptOld.x << " " << ptNew.y - ptOld.y
560 << " Td\n";
561
562 ptOld = ptNew;
563 }
564 }
565
566 CPVT_Word word;
567 if (pIterator->GetWord(word)) {
568 if (word.nFontIndex != nCurFontIndex) {
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400569 if (sWords.tellp() > 0) {
570 sEditStream << GetWordRenderString(CFX_ByteString(sWords));
571 sWords.str("");
dsinclaire35af1e2016-07-13 11:26:20 -0700572 }
573 sEditStream << GetFontSetString(pEdit->GetFontMap(), word.nFontIndex,
574 word.fFontSize);
575 nCurFontIndex = word.nFontIndex;
576 }
577
578 sWords << GetPDFWordString(pEdit->GetFontMap(), nCurFontIndex,
579 word.Word, SubWord);
580 }
581
582 oldplace = place;
583 } else {
584 CPVT_Word word;
585 if (pIterator->GetWord(word)) {
Dan Sinclairf528eee2017-02-14 11:52:07 -0500586 ptNew =
587 CFX_PointF(word.ptWord.x + ptOffset.x, word.ptWord.y + ptOffset.y);
dsinclaire35af1e2016-07-13 11:26:20 -0700588
589 if (ptNew.x != ptOld.x || ptNew.y != ptOld.y) {
590 sEditStream << ptNew.x - ptOld.x << " " << ptNew.y - ptOld.y
591 << " Td\n";
592 ptOld = ptNew;
593 }
594
595 if (word.nFontIndex != nCurFontIndex) {
596 sEditStream << GetFontSetString(pEdit->GetFontMap(), word.nFontIndex,
597 word.fFontSize);
598 nCurFontIndex = word.nFontIndex;
599 }
600
601 sEditStream << GetWordRenderString(GetPDFWordString(
602 pEdit->GetFontMap(), nCurFontIndex, word.Word, SubWord));
603 }
604 }
605 }
606
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400607 if (sWords.tellp() > 0) {
608 sEditStream << GetWordRenderString(CFX_ByteString(sWords));
609 sWords.str("");
dsinclaire35af1e2016-07-13 11:26:20 -0700610 }
611
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400612 std::ostringstream sAppStream;
613 if (sEditStream.tellp() > 0) {
dsinclaire35af1e2016-07-13 11:26:20 -0700614 int32_t nHorzScale = pEdit->GetHorzScale();
615 if (nHorzScale != 100) {
616 sAppStream << nHorzScale << " Tz\n";
617 }
618
Dan Sinclair05df0752017-03-14 14:43:42 -0400619 float fCharSpace = pEdit->GetCharSpace();
dsinclair448c4332016-08-02 12:07:35 -0700620 if (!IsFloatZero(fCharSpace)) {
dsinclaire35af1e2016-07-13 11:26:20 -0700621 sAppStream << fCharSpace << " Tc\n";
622 }
623
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400624 sAppStream << sEditStream.str();
dsinclaire35af1e2016-07-13 11:26:20 -0700625 }
626
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400627 return CFX_ByteString(sAppStream);
dsinclaire35af1e2016-07-13 11:26:20 -0700628}
629
630// static
631CFX_ByteString CFX_Edit::GetSelectAppearanceStream(
632 CFX_Edit* pEdit,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500633 const CFX_PointF& ptOffset,
dsinclaire35af1e2016-07-13 11:26:20 -0700634 const CPVT_WordRange* pRange) {
Tom Sepez52f69b32017-03-21 13:42:38 -0700635 if (!pRange || pRange->IsEmpty())
dsinclaire35af1e2016-07-13 11:26:20 -0700636 return CFX_ByteString();
637
638 CFX_Edit_Iterator* pIterator = pEdit->GetIterator();
639 pIterator->SetAt(pRange->BeginPos);
640
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400641 std::ostringstream sRet;
dsinclaire35af1e2016-07-13 11:26:20 -0700642 while (pIterator->NextWord()) {
643 CPVT_WordPlace place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -0700644 if (place > pRange->EndPos)
dsinclaire35af1e2016-07-13 11:26:20 -0700645 break;
646
647 CPVT_Word word;
648 CPVT_Line line;
649 if (pIterator->GetWord(word) && pIterator->GetLine(line)) {
650 sRet << word.ptWord.x + ptOffset.x << " "
651 << line.ptLine.y + line.fLineDescent << " " << word.fWidth << " "
652 << line.fLineAscent - line.fLineDescent << " re\nf\n";
653 }
654 }
655
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400656 return CFX_ByteString(sRet);
dsinclaire35af1e2016-07-13 11:26:20 -0700657}
658
659// static
dsinclaire35af1e2016-07-13 11:26:20 -0700660void CFX_Edit::DrawEdit(CFX_RenderDevice* pDevice,
661 CFX_Matrix* pUser2Device,
662 CFX_Edit* pEdit,
663 FX_COLORREF crTextFill,
dsinclaire35af1e2016-07-13 11:26:20 -0700664 const CFX_FloatRect& rcClip,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500665 const CFX_PointF& ptOffset,
dsinclaire35af1e2016-07-13 11:26:20 -0700666 const CPVT_WordRange* pRange,
667 CFX_SystemHandler* pSystemHandler,
dsinclair8faac622016-09-15 12:41:50 -0700668 CFFL_FormFiller* pFFLData) {
dsinclaire35af1e2016-07-13 11:26:20 -0700669 const bool bContinuous =
670 pEdit->GetCharArray() == 0 && pEdit->GetCharSpace() <= 0.0f;
671 uint16_t SubWord = pEdit->GetPasswordChar();
Dan Sinclair05df0752017-03-14 14:43:42 -0400672 float fFontSize = pEdit->GetFontSize();
dsinclaire35af1e2016-07-13 11:26:20 -0700673 CPVT_WordRange wrSelect = pEdit->GetSelectWordRange();
674 int32_t nHorzScale = pEdit->GetHorzScale();
675
676 FX_COLORREF crCurFill = crTextFill;
677 FX_COLORREF crOldFill = crCurFill;
678
tsepez4cf55152016-11-02 14:37:54 -0700679 bool bSelect = false;
dsinclaire35af1e2016-07-13 11:26:20 -0700680 const FX_COLORREF crWhite = ArgbEncode(255, 255, 255, 255);
681 const FX_COLORREF crSelBK = ArgbEncode(255, 0, 51, 113);
682
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400683 std::ostringstream sTextBuf;
dsinclaire35af1e2016-07-13 11:26:20 -0700684 int32_t nFontIndex = -1;
Dan Sinclairf528eee2017-02-14 11:52:07 -0500685 CFX_PointF ptBT;
Tom Sepez1629f602017-04-21 14:11:26 -0700686 CFX_RenderDevice::StateRestorer restorer(pDevice);
dsinclaire35af1e2016-07-13 11:26:20 -0700687 if (!rcClip.IsEmpty()) {
688 CFX_FloatRect rcTemp = rcClip;
689 pUser2Device->TransformRect(rcTemp);
690 pDevice->SetClip_Rect(rcTemp.ToFxRect());
691 }
692
693 CFX_Edit_Iterator* pIterator = pEdit->GetIterator();
694 if (IPVT_FontMap* pFontMap = pEdit->GetFontMap()) {
695 if (pRange)
696 pIterator->SetAt(pRange->BeginPos);
697 else
698 pIterator->SetAt(0);
699
700 CPVT_WordPlace oldplace;
701 while (pIterator->NextWord()) {
702 CPVT_WordPlace place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -0700703 if (pRange && place > pRange->EndPos)
dsinclaire35af1e2016-07-13 11:26:20 -0700704 break;
705
Tom Sepez52f69b32017-03-21 13:42:38 -0700706 if (!wrSelect.IsEmpty()) {
707 bSelect = place > wrSelect.BeginPos && place <= wrSelect.EndPos;
dsinclaire35af1e2016-07-13 11:26:20 -0700708 crCurFill = bSelect ? crWhite : crTextFill;
709 }
710 if (pSystemHandler && pSystemHandler->IsSelectionImplemented()) {
711 crCurFill = crTextFill;
712 crOldFill = crCurFill;
713 }
714 CPVT_Word word;
715 if (pIterator->GetWord(word)) {
716 if (bSelect) {
717 CPVT_Line line;
718 pIterator->GetLine(line);
719
720 if (pSystemHandler && pSystemHandler->IsSelectionImplemented()) {
721 CFX_FloatRect rc(word.ptWord.x, line.ptLine.y + line.fLineDescent,
722 word.ptWord.x + word.fWidth,
723 line.ptLine.y + line.fLineAscent);
724 rc.Intersect(rcClip);
725 pSystemHandler->OutputSelectedRect(pFFLData, rc);
726 } else {
727 CFX_PathData pathSelBK;
728 pathSelBK.AppendRect(
729 word.ptWord.x, line.ptLine.y + line.fLineDescent,
730 word.ptWord.x + word.fWidth, line.ptLine.y + line.fLineAscent);
731
732 pDevice->DrawPath(&pathSelBK, pUser2Device, nullptr, crSelBK, 0,
733 FXFILL_WINDING);
734 }
735 }
736
737 if (bContinuous) {
738 if (place.LineCmp(oldplace) != 0 || word.nFontIndex != nFontIndex ||
739 crOldFill != crCurFill) {
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400740 if (sTextBuf.tellp() > 0) {
dsinclaire35af1e2016-07-13 11:26:20 -0700741 DrawTextString(
Dan Sinclairf528eee2017-02-14 11:52:07 -0500742 pDevice, CFX_PointF(ptBT.x + ptOffset.x, ptBT.y + ptOffset.y),
dsinclaire35af1e2016-07-13 11:26:20 -0700743 pFontMap->GetPDFFont(nFontIndex), fFontSize, pUser2Device,
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400744 CFX_ByteString(sTextBuf), crOldFill, nHorzScale);
dsinclaire35af1e2016-07-13 11:26:20 -0700745
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400746 sTextBuf.str("");
dsinclaire35af1e2016-07-13 11:26:20 -0700747 }
748 nFontIndex = word.nFontIndex;
749 ptBT = word.ptWord;
750 crOldFill = crCurFill;
751 }
752
753 sTextBuf << GetPDFWordString(pFontMap, word.nFontIndex, word.Word,
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400754 SubWord);
dsinclaire35af1e2016-07-13 11:26:20 -0700755 } else {
756 DrawTextString(
Dan Sinclairf528eee2017-02-14 11:52:07 -0500757 pDevice, CFX_PointF(word.ptWord.x + ptOffset.x,
758 word.ptWord.y + ptOffset.y),
dsinclaire35af1e2016-07-13 11:26:20 -0700759 pFontMap->GetPDFFont(word.nFontIndex), fFontSize, pUser2Device,
760 GetPDFWordString(pFontMap, word.nFontIndex, word.Word, SubWord),
Dan Sinclaira0061af2017-02-23 09:25:17 -0500761 crCurFill, nHorzScale);
dsinclaire35af1e2016-07-13 11:26:20 -0700762 }
763 oldplace = place;
764 }
765 }
766
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400767 if (sTextBuf.tellp() > 0) {
Dan Sinclaira0061af2017-02-23 09:25:17 -0500768 DrawTextString(pDevice,
769 CFX_PointF(ptBT.x + ptOffset.x, ptBT.y + ptOffset.y),
770 pFontMap->GetPDFFont(nFontIndex), fFontSize, pUser2Device,
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400771 CFX_ByteString(sTextBuf), crOldFill, nHorzScale);
dsinclaire35af1e2016-07-13 11:26:20 -0700772 }
773 }
dsinclaire35af1e2016-07-13 11:26:20 -0700774}
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();
Dan Sinclairf51a02a2017-04-19 12:46:53 -04001030 DoInsertText(CPVT_WordPlace(0, 0, -1), sText, FX_CHARSET_Default);
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() {
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001230 if (!m_pNotify)
1231 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001232
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001233 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
1234 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1235 if (m_bNotifyFlag)
1236 return;
1237
1238 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
1239 m_bNotifyFlag = true;
1240
1241 PWL_SCROLL_INFO Info;
1242 Info.fPlateWidth = rcPlate.top - rcPlate.bottom;
1243 Info.fContentMin = rcContent.bottom;
1244 Info.fContentMax = rcContent.top;
1245 Info.fSmallStep = rcPlate.Height() / 3;
1246 Info.fBigStep = rcPlate.Height();
1247 m_pNotify->SetScrollInfo(Info);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001248}
1249
Dan Sinclair05df0752017-03-14 14:43:42 -04001250void CFX_Edit::SetScrollPosX(float fx) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001251 if (!m_bEnableScroll)
1252 return;
1253
1254 if (m_pVT->IsValid()) {
dsinclair448c4332016-08-02 12:07:35 -07001255 if (!IsFloatEqual(m_ptScrollPos.x, fx)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001256 m_ptScrollPos.x = fx;
dsinclairefd5a992016-07-18 10:04:07 -07001257 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001258 }
1259 }
1260}
1261
Dan Sinclair05df0752017-03-14 14:43:42 -04001262void CFX_Edit::SetScrollPosY(float fy) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001263 if (!m_bEnableScroll)
1264 return;
1265
1266 if (m_pVT->IsValid()) {
dsinclair448c4332016-08-02 12:07:35 -07001267 if (!IsFloatEqual(m_ptScrollPos.y, fy)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001268 m_ptScrollPos.y = fy;
dsinclairefd5a992016-07-18 10:04:07 -07001269 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001270
dsinclaira2919b32016-07-13 10:55:48 -07001271 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001272 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001273 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001274 m_bNotifyFlag = true;
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001275 m_pNotify->SetScrollPosition(fy);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001276 }
1277 }
1278 }
1279 }
1280}
1281
Dan Sinclairf528eee2017-02-14 11:52:07 -05001282void CFX_Edit::SetScrollPos(const CFX_PointF& point) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001283 SetScrollPosX(point.x);
1284 SetScrollPosY(point.y);
1285 SetScrollLimit();
1286 SetCaretInfo();
1287}
1288
Dan Sinclairf528eee2017-02-14 11:52:07 -05001289CFX_PointF CFX_Edit::GetScrollPos() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001290 return m_ptScrollPos;
1291}
1292
1293void CFX_Edit::SetScrollLimit() {
1294 if (m_pVT->IsValid()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001295 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1296 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001297
1298 if (rcPlate.Width() > rcContent.Width()) {
1299 SetScrollPosX(rcPlate.left);
1300 } else {
dsinclair448c4332016-08-02 12:07:35 -07001301 if (IsFloatSmaller(m_ptScrollPos.x, rcContent.left)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001302 SetScrollPosX(rcContent.left);
dsinclair448c4332016-08-02 12:07:35 -07001303 } else if (IsFloatBigger(m_ptScrollPos.x,
1304 rcContent.right - rcPlate.Width())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001305 SetScrollPosX(rcContent.right - rcPlate.Width());
1306 }
1307 }
1308
1309 if (rcPlate.Height() > rcContent.Height()) {
1310 SetScrollPosY(rcPlate.top);
1311 } else {
dsinclair448c4332016-08-02 12:07:35 -07001312 if (IsFloatSmaller(m_ptScrollPos.y,
1313 rcContent.bottom + rcPlate.Height())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001314 SetScrollPosY(rcContent.bottom + rcPlate.Height());
dsinclair448c4332016-08-02 12:07:35 -07001315 } else if (IsFloatBigger(m_ptScrollPos.y, rcContent.top)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001316 SetScrollPosY(rcContent.top);
1317 }
1318 }
1319 }
1320}
1321
1322void CFX_Edit::ScrollToCaret() {
1323 SetScrollLimit();
1324
thestig821d59e2016-05-11 12:59:22 -07001325 if (!m_pVT->IsValid())
1326 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001327
thestig821d59e2016-05-11 12:59:22 -07001328 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1329 pIterator->SetAt(m_wpCaret);
1330
Dan Sinclairf528eee2017-02-14 11:52:07 -05001331 CFX_PointF ptHead;
1332 CFX_PointF ptFoot;
thestig821d59e2016-05-11 12:59:22 -07001333 CPVT_Word word;
1334 CPVT_Line line;
1335 if (pIterator->GetWord(word)) {
1336 ptHead.x = word.ptWord.x + word.fWidth;
1337 ptHead.y = word.ptWord.y + word.fAscent;
1338 ptFoot.x = word.ptWord.x + word.fWidth;
1339 ptFoot.y = word.ptWord.y + word.fDescent;
1340 } else if (pIterator->GetLine(line)) {
1341 ptHead.x = line.ptLine.x;
1342 ptHead.y = line.ptLine.y + line.fLineAscent;
1343 ptFoot.x = line.ptLine.x;
1344 ptFoot.y = line.ptLine.y + line.fLineDescent;
1345 }
1346
Dan Sinclairf528eee2017-02-14 11:52:07 -05001347 CFX_PointF ptHeadEdit = VTToEdit(ptHead);
1348 CFX_PointF ptFootEdit = VTToEdit(ptFoot);
thestig821d59e2016-05-11 12:59:22 -07001349 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
dsinclair448c4332016-08-02 12:07:35 -07001350 if (!IsFloatEqual(rcPlate.left, rcPlate.right)) {
1351 if (IsFloatSmaller(ptHeadEdit.x, rcPlate.left) ||
1352 IsFloatEqual(ptHeadEdit.x, rcPlate.left)) {
thestig821d59e2016-05-11 12:59:22 -07001353 SetScrollPosX(ptHead.x);
dsinclair448c4332016-08-02 12:07:35 -07001354 } else if (IsFloatBigger(ptHeadEdit.x, rcPlate.right)) {
thestig821d59e2016-05-11 12:59:22 -07001355 SetScrollPosX(ptHead.x - rcPlate.Width());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001356 }
thestig821d59e2016-05-11 12:59:22 -07001357 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001358
dsinclair448c4332016-08-02 12:07:35 -07001359 if (!IsFloatEqual(rcPlate.top, rcPlate.bottom)) {
1360 if (IsFloatSmaller(ptFootEdit.y, rcPlate.bottom) ||
1361 IsFloatEqual(ptFootEdit.y, rcPlate.bottom)) {
1362 if (IsFloatSmaller(ptHeadEdit.y, rcPlate.top)) {
thestig821d59e2016-05-11 12:59:22 -07001363 SetScrollPosY(ptFoot.y + rcPlate.Height());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001364 }
dsinclair448c4332016-08-02 12:07:35 -07001365 } else if (IsFloatBigger(ptHeadEdit.y, rcPlate.top)) {
1366 if (IsFloatBigger(ptFootEdit.y, rcPlate.bottom)) {
thestig821d59e2016-05-11 12:59:22 -07001367 SetScrollPosY(ptHead.y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001368 }
1369 }
1370 }
1371}
1372
dsinclairefd5a992016-07-18 10:04:07 -07001373void CFX_Edit::Refresh() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001374 if (m_bEnableRefresh && m_pVT->IsValid()) {
1375 m_Refresh.BeginRefresh();
1376 RefreshPushLineRects(GetVisibleWordRange());
1377
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001378 m_Refresh.NoAnalyse();
1379 m_ptRefreshScrollPos = m_ptScrollPos;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001380
dsinclaira2919b32016-07-13 10:55:48 -07001381 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001382 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001383 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001384 m_bNotifyFlag = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001385 if (const CFX_Edit_RectArray* pRects = m_Refresh.GetRefreshRects()) {
1386 for (int32_t i = 0, sz = pRects->GetSize(); i < sz; i++)
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001387 m_pNotify->InvalidateRect(pRects->GetAt(i));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001388 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001389 }
1390 }
1391
1392 m_Refresh.EndRefresh();
1393 }
1394}
1395
1396void CFX_Edit::RefreshPushLineRects(const CPVT_WordRange& wr) {
thestig821d59e2016-05-11 12:59:22 -07001397 if (!m_pVT->IsValid())
1398 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001399
thestig821d59e2016-05-11 12:59:22 -07001400 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1401 CPVT_WordPlace wpBegin = wr.BeginPos;
1402 m_pVT->UpdateWordPlace(wpBegin);
1403 CPVT_WordPlace wpEnd = wr.EndPos;
1404 m_pVT->UpdateWordPlace(wpEnd);
1405 pIterator->SetAt(wpBegin);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001406
thestig821d59e2016-05-11 12:59:22 -07001407 CPVT_Line lineinfo;
1408 do {
1409 if (!pIterator->GetLine(lineinfo))
1410 break;
1411 if (lineinfo.lineplace.LineCmp(wpEnd) > 0)
1412 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001413
thestig821d59e2016-05-11 12:59:22 -07001414 CFX_FloatRect rcLine(lineinfo.ptLine.x,
1415 lineinfo.ptLine.y + lineinfo.fLineDescent,
1416 lineinfo.ptLine.x + lineinfo.fLineWidth,
1417 lineinfo.ptLine.y + lineinfo.fLineAscent);
1418
1419 m_Refresh.Push(CPVT_WordRange(lineinfo.lineplace, lineinfo.lineEnd),
1420 VTToEdit(rcLine));
1421 } while (pIterator->NextLine());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001422}
1423
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001424void CFX_Edit::RefreshWordRange(const CPVT_WordRange& wr) {
thestig821d59e2016-05-11 12:59:22 -07001425 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1426 CPVT_WordRange wrTemp = wr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001427
thestig821d59e2016-05-11 12:59:22 -07001428 m_pVT->UpdateWordPlace(wrTemp.BeginPos);
1429 m_pVT->UpdateWordPlace(wrTemp.EndPos);
1430 pIterator->SetAt(wrTemp.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001431
thestig821d59e2016-05-11 12:59:22 -07001432 CPVT_Word wordinfo;
1433 CPVT_Line lineinfo;
1434 CPVT_WordPlace place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001435
thestig821d59e2016-05-11 12:59:22 -07001436 while (pIterator->NextWord()) {
1437 place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -07001438 if (place > wrTemp.EndPos)
thestig821d59e2016-05-11 12:59:22 -07001439 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001440
thestig821d59e2016-05-11 12:59:22 -07001441 pIterator->GetWord(wordinfo);
1442 pIterator->GetLine(lineinfo);
thestig821d59e2016-05-11 12:59:22 -07001443 if (place.LineCmp(wrTemp.BeginPos) == 0 ||
1444 place.LineCmp(wrTemp.EndPos) == 0) {
1445 CFX_FloatRect rcWord(wordinfo.ptWord.x,
1446 lineinfo.ptLine.y + lineinfo.fLineDescent,
1447 wordinfo.ptWord.x + wordinfo.fWidth,
1448 lineinfo.ptLine.y + lineinfo.fLineAscent);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001449
dsinclaira2919b32016-07-13 10:55:48 -07001450 if (m_pNotify) {
thestig821d59e2016-05-11 12:59:22 -07001451 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001452 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001453 m_bNotifyFlag = true;
thestig821d59e2016-05-11 12:59:22 -07001454 CFX_FloatRect rcRefresh = VTToEdit(rcWord);
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001455 m_pNotify->InvalidateRect(&rcRefresh);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001456 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001457 }
thestig821d59e2016-05-11 12:59:22 -07001458 } else {
1459 CFX_FloatRect rcLine(lineinfo.ptLine.x,
1460 lineinfo.ptLine.y + lineinfo.fLineDescent,
1461 lineinfo.ptLine.x + lineinfo.fLineWidth,
1462 lineinfo.ptLine.y + lineinfo.fLineAscent);
1463
dsinclaira2919b32016-07-13 10:55:48 -07001464 if (m_pNotify) {
thestig821d59e2016-05-11 12:59:22 -07001465 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001466 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001467 m_bNotifyFlag = true;
thestig821d59e2016-05-11 12:59:22 -07001468 CFX_FloatRect rcRefresh = VTToEdit(rcLine);
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001469 m_pNotify->InvalidateRect(&rcRefresh);
thestig821d59e2016-05-11 12:59:22 -07001470 }
1471 }
1472
1473 pIterator->NextLine();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001474 }
1475 }
1476}
1477
1478void CFX_Edit::SetCaret(const CPVT_WordPlace& place) {
1479 m_wpOldCaret = m_wpCaret;
1480 m_wpCaret = place;
1481}
1482
1483void CFX_Edit::SetCaretInfo() {
dsinclaira2919b32016-07-13 10:55:48 -07001484 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001485 if (!m_bNotifyFlag) {
thestig821d59e2016-05-11 12:59:22 -07001486 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1487 pIterator->SetAt(m_wpCaret);
tsepez63f545c2016-09-13 16:08:49 -07001488
Dan Sinclairf528eee2017-02-14 11:52:07 -05001489 CFX_PointF ptHead;
1490 CFX_PointF ptFoot;
thestig821d59e2016-05-11 12:59:22 -07001491 CPVT_Word word;
1492 CPVT_Line line;
1493 if (pIterator->GetWord(word)) {
1494 ptHead.x = word.ptWord.x + word.fWidth;
1495 ptHead.y = word.ptWord.y + word.fAscent;
1496 ptFoot.x = word.ptWord.x + word.fWidth;
1497 ptFoot.y = word.ptWord.y + word.fDescent;
1498 } else if (pIterator->GetLine(line)) {
1499 ptHead.x = line.ptLine.x;
1500 ptHead.y = line.ptLine.y + line.fLineAscent;
1501 ptFoot.x = line.ptLine.x;
1502 ptFoot.y = line.ptLine.y + line.fLineDescent;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001503 }
1504
Lei Zhanga8c2b912017-03-22 17:41:02 -07001505 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001506 m_bNotifyFlag = true;
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001507 m_pNotify->SetCaret(m_SelState.IsEmpty(), VTToEdit(ptHead),
1508 VTToEdit(ptFoot));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001509 }
1510 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001511}
1512
Dan Sinclairf528eee2017-02-14 11:52:07 -05001513void CFX_Edit::OnMouseDown(const CFX_PointF& point, bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001514 if (!m_pVT->IsValid())
1515 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001516
Tom Sepez52f69b32017-03-21 13:42:38 -07001517 SelectNone();
1518 SetCaret(m_pVT->SearchWordPlace(EditToVT(point)));
1519 m_SelState.Set(m_wpCaret, m_wpCaret);
1520 ScrollToCaret();
1521 SetCaretOrigin();
1522 SetCaretInfo();
1523}
1524
1525void CFX_Edit::OnMouseMove(const CFX_PointF& point, bool bShift, bool bCtrl) {
1526 if (!m_pVT->IsValid())
1527 return;
1528
1529 SetCaret(m_pVT->SearchWordPlace(EditToVT(point)));
1530 if (m_wpCaret == m_wpOldCaret)
1531 return;
1532
1533 m_SelState.SetEndPos(m_wpCaret);
1534 ScrollToCaret();
1535 Refresh();
1536 SetCaretOrigin();
1537 SetCaretInfo();
1538}
1539
1540void CFX_Edit::OnVK_UP(bool bShift, bool bCtrl) {
1541 if (!m_pVT->IsValid())
1542 return;
1543
1544 SetCaret(m_pVT->GetUpWordPlace(m_wpCaret, m_ptCaret));
1545 if (bShift) {
1546 if (m_SelState.IsEmpty())
1547 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1548 else
1549 m_SelState.SetEndPos(m_wpCaret);
1550
1551 if (m_wpOldCaret != m_wpCaret) {
1552 ScrollToCaret();
1553 Refresh();
1554 SetCaretInfo();
1555 }
1556 } else {
1557 SelectNone();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001558 ScrollToCaret();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001559 SetCaretInfo();
1560 }
1561}
1562
Tom Sepez52f69b32017-03-21 13:42:38 -07001563void CFX_Edit::OnVK_DOWN(bool bShift, bool bCtrl) {
1564 if (!m_pVT->IsValid())
1565 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001566
Tom Sepez52f69b32017-03-21 13:42:38 -07001567 SetCaret(m_pVT->GetDownWordPlace(m_wpCaret, m_ptCaret));
1568 if (bShift) {
1569 if (m_SelState.IsEmpty())
1570 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1571 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001572 m_SelState.SetEndPos(m_wpCaret);
1573
Tom Sepez52f69b32017-03-21 13:42:38 -07001574 if (m_wpOldCaret != m_wpCaret) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001575 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001576 Refresh();
Tom Sepez52f69b32017-03-21 13:42:38 -07001577 SetCaretInfo();
1578 }
1579 } else {
1580 SelectNone();
1581 ScrollToCaret();
1582 SetCaretInfo();
1583 }
1584}
1585
1586void CFX_Edit::OnVK_LEFT(bool bShift, bool bCtrl) {
1587 if (!m_pVT->IsValid())
1588 return;
1589
1590 if (bShift) {
1591 if (m_wpCaret == m_pVT->GetLineBeginPlace(m_wpCaret) &&
1592 m_wpCaret != m_pVT->GetSectionBeginPlace(m_wpCaret)) {
1593 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1594 }
1595 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1596 if (m_SelState.IsEmpty())
1597 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1598 else
1599 m_SelState.SetEndPos(m_wpCaret);
1600
1601 if (m_wpOldCaret != m_wpCaret) {
1602 ScrollToCaret();
1603 Refresh();
1604 SetCaretInfo();
1605 }
1606 } else {
1607 if (!m_SelState.IsEmpty()) {
1608 if (m_SelState.BeginPos < m_SelState.EndPos)
1609 SetCaret(m_SelState.BeginPos);
1610 else
1611 SetCaret(m_SelState.EndPos);
1612
1613 SelectNone();
1614 ScrollToCaret();
1615 SetCaretInfo();
1616 } else {
1617 if (m_wpCaret == m_pVT->GetLineBeginPlace(m_wpCaret) &&
1618 m_wpCaret != m_pVT->GetSectionBeginPlace(m_wpCaret)) {
1619 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1620 }
1621 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1622 ScrollToCaret();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001623 SetCaretOrigin();
1624 SetCaretInfo();
1625 }
1626 }
1627}
1628
tsepez4cf55152016-11-02 14:37:54 -07001629void CFX_Edit::OnVK_RIGHT(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001630 if (!m_pVT->IsValid())
1631 return;
1632
1633 if (bShift) {
1634 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1635 if (m_wpCaret == m_pVT->GetLineEndPlace(m_wpCaret) &&
1636 m_wpCaret != m_pVT->GetSectionEndPlace(m_wpCaret))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001637 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1638
Tom Sepez52f69b32017-03-21 13:42:38 -07001639 if (m_SelState.IsEmpty())
1640 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1641 else
1642 m_SelState.SetEndPos(m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001643
Tom Sepez52f69b32017-03-21 13:42:38 -07001644 if (m_wpOldCaret != m_wpCaret) {
1645 ScrollToCaret();
1646 Refresh();
1647 SetCaretInfo();
1648 }
1649 } else {
1650 if (!m_SelState.IsEmpty()) {
1651 if (m_SelState.BeginPos > m_SelState.EndPos)
1652 SetCaret(m_SelState.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001653 else
Tom Sepez52f69b32017-03-21 13:42:38 -07001654 SetCaret(m_SelState.EndPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001655
Tom Sepez52f69b32017-03-21 13:42:38 -07001656 SelectNone();
1657 ScrollToCaret();
1658 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001659 } else {
Tom Sepez52f69b32017-03-21 13:42:38 -07001660 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1661 if (m_wpCaret == m_pVT->GetLineEndPlace(m_wpCaret) &&
1662 m_wpCaret != m_pVT->GetSectionEndPlace(m_wpCaret)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001663 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001664 }
Tom Sepez52f69b32017-03-21 13:42:38 -07001665 ScrollToCaret();
1666 SetCaretOrigin();
1667 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001668 }
1669 }
1670}
1671
tsepez4cf55152016-11-02 14:37:54 -07001672void CFX_Edit::OnVK_HOME(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001673 if (!m_pVT->IsValid())
1674 return;
1675
1676 if (bShift) {
1677 if (bCtrl)
1678 SetCaret(m_pVT->GetBeginWordPlace());
1679 else
1680 SetCaret(m_pVT->GetLineBeginPlace(m_wpCaret));
1681
1682 if (m_SelState.IsEmpty())
1683 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1684 else
1685 m_SelState.SetEndPos(m_wpCaret);
1686
1687 ScrollToCaret();
1688 Refresh();
1689 SetCaretInfo();
1690 } else {
1691 if (!m_SelState.IsEmpty()) {
1692 SetCaret(std::min(m_SelState.BeginPos, m_SelState.EndPos));
1693 SelectNone();
1694 ScrollToCaret();
1695 SetCaretInfo();
1696 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001697 if (bCtrl)
1698 SetCaret(m_pVT->GetBeginWordPlace());
1699 else
1700 SetCaret(m_pVT->GetLineBeginPlace(m_wpCaret));
1701
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001702 ScrollToCaret();
Tom Sepez52f69b32017-03-21 13:42:38 -07001703 SetCaretOrigin();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001704 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001705 }
1706 }
1707}
1708
tsepez4cf55152016-11-02 14:37:54 -07001709void CFX_Edit::OnVK_END(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001710 if (!m_pVT->IsValid())
1711 return;
1712
1713 if (bShift) {
1714 if (bCtrl)
1715 SetCaret(m_pVT->GetEndWordPlace());
1716 else
1717 SetCaret(m_pVT->GetLineEndPlace(m_wpCaret));
1718
1719 if (m_SelState.IsEmpty())
1720 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1721 else
1722 m_SelState.SetEndPos(m_wpCaret);
1723
1724 ScrollToCaret();
1725 Refresh();
1726 SetCaretInfo();
1727 } else {
1728 if (!m_SelState.IsEmpty()) {
1729 SetCaret(std::max(m_SelState.BeginPos, m_SelState.EndPos));
1730 SelectNone();
1731 ScrollToCaret();
1732 SetCaretInfo();
1733 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001734 if (bCtrl)
1735 SetCaret(m_pVT->GetEndWordPlace());
1736 else
1737 SetCaret(m_pVT->GetLineEndPlace(m_wpCaret));
1738
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001739 ScrollToCaret();
Tom Sepez52f69b32017-03-21 13:42:38 -07001740 SetCaretOrigin();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001741 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001742 }
1743 }
1744}
1745
tsepez4cf55152016-11-02 14:37:54 -07001746bool CFX_Edit::InsertWord(uint16_t word,
1747 int32_t charset,
1748 const CPVT_WordProps* pWordProps,
1749 bool bAddUndo,
1750 bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001751 if (IsTextOverflow() || !m_pVT->IsValid())
tsepez4cf55152016-11-02 14:37:54 -07001752 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001753
Tom Sepez3509d162017-01-30 13:22:02 -08001754 m_pVT->UpdateWordPlace(m_wpCaret);
1755 SetCaret(m_pVT->InsertWord(m_wpCaret, word,
1756 GetCharSetFromUnicode(word, charset), pWordProps));
1757 m_SelState.Set(m_wpCaret, m_wpCaret);
1758 if (m_wpCaret == m_wpOldCaret)
1759 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001760
Tom Sepez3509d162017-01-30 13:22:02 -08001761 if (bAddUndo && m_bEnableUndo) {
1762 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertWord>(
1763 this, m_wpOldCaret, m_wpCaret, word, charset, pWordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001764 }
Tom Sepez3509d162017-01-30 13:22:02 -08001765 if (bPaint)
1766 PaintInsertText(m_wpOldCaret, m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001767
Tom Sepez3509d162017-01-30 13:22:02 -08001768 if (m_bOprNotify && m_pOprNotify)
1769 m_pOprNotify->OnInsertWord(m_wpCaret, m_wpOldCaret);
1770
1771 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001772}
1773
tsepez4cf55152016-11-02 14:37:54 -07001774bool CFX_Edit::InsertReturn(const CPVT_SecProps* pSecProps,
1775 const CPVT_WordProps* pWordProps,
1776 bool bAddUndo,
1777 bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001778 if (IsTextOverflow() || !m_pVT->IsValid())
tsepez4cf55152016-11-02 14:37:54 -07001779 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001780
Tom Sepez3509d162017-01-30 13:22:02 -08001781 m_pVT->UpdateWordPlace(m_wpCaret);
1782 SetCaret(m_pVT->InsertSection(m_wpCaret, pSecProps, pWordProps));
1783 m_SelState.Set(m_wpCaret, m_wpCaret);
1784 if (m_wpCaret == m_wpOldCaret)
1785 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001786
Tom Sepez3509d162017-01-30 13:22:02 -08001787 if (bAddUndo && m_bEnableUndo) {
1788 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertReturn>(
1789 this, m_wpOldCaret, m_wpCaret, pSecProps, pWordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001790 }
Tom Sepez3509d162017-01-30 13:22:02 -08001791 if (bPaint) {
1792 RearrangePart(CPVT_WordRange(m_wpOldCaret, m_wpCaret));
1793 ScrollToCaret();
1794 Refresh();
1795 SetCaretOrigin();
1796 SetCaretInfo();
1797 }
1798 if (m_bOprNotify && m_pOprNotify)
1799 m_pOprNotify->OnInsertReturn(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001800
Tom Sepez3509d162017-01-30 13:22:02 -08001801 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001802}
1803
tsepez4cf55152016-11-02 14:37:54 -07001804bool CFX_Edit::Backspace(bool bAddUndo, bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001805 if (!m_pVT->IsValid() || m_wpCaret == m_pVT->GetBeginWordPlace())
1806 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001807
Tom Sepez3509d162017-01-30 13:22:02 -08001808 CPVT_Section section;
1809 CPVT_Word word;
1810 if (bAddUndo) {
1811 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1812 pIterator->SetAt(m_wpCaret);
1813 pIterator->GetSection(section);
1814 pIterator->GetWord(word);
1815 }
1816 m_pVT->UpdateWordPlace(m_wpCaret);
1817 SetCaret(m_pVT->BackSpaceWord(m_wpCaret));
1818 m_SelState.Set(m_wpCaret, m_wpCaret);
1819 if (m_wpCaret == m_wpOldCaret)
1820 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001821
Tom Sepez3509d162017-01-30 13:22:02 -08001822 if (bAddUndo && m_bEnableUndo) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001823 if (m_wpCaret.nSecIndex != m_wpOldCaret.nSecIndex) {
Tom Sepez3509d162017-01-30 13:22:02 -08001824 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Backspace>(
1825 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1826 section.SecProps, section.WordProps));
1827 } else {
1828 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Backspace>(
1829 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1830 section.SecProps, word.WordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001831 }
1832 }
Tom Sepez3509d162017-01-30 13:22:02 -08001833 if (bPaint) {
1834 RearrangePart(CPVT_WordRange(m_wpCaret, m_wpOldCaret));
1835 ScrollToCaret();
1836 Refresh();
1837 SetCaretOrigin();
1838 SetCaretInfo();
1839 }
1840 if (m_bOprNotify && m_pOprNotify)
1841 m_pOprNotify->OnBackSpace(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001842
Tom Sepez3509d162017-01-30 13:22:02 -08001843 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001844}
1845
tsepez4cf55152016-11-02 14:37:54 -07001846bool CFX_Edit::Delete(bool bAddUndo, bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001847 if (!m_pVT->IsValid() || m_wpCaret == m_pVT->GetEndWordPlace())
1848 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001849
Tom Sepez3509d162017-01-30 13:22:02 -08001850 CPVT_Section section;
1851 CPVT_Word word;
1852 if (bAddUndo) {
1853 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1854 pIterator->SetAt(m_pVT->GetNextWordPlace(m_wpCaret));
1855 pIterator->GetSection(section);
1856 pIterator->GetWord(word);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001857 }
Tom Sepez3509d162017-01-30 13:22:02 -08001858 m_pVT->UpdateWordPlace(m_wpCaret);
1859 bool bSecEnd = (m_wpCaret == m_pVT->GetSectionEndPlace(m_wpCaret));
1860 SetCaret(m_pVT->DeleteWord(m_wpCaret));
1861 m_SelState.Set(m_wpCaret, m_wpCaret);
1862 if (bAddUndo && m_bEnableUndo) {
1863 if (bSecEnd) {
1864 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Delete>(
1865 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1866 section.SecProps, section.WordProps, bSecEnd));
1867 } else {
1868 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Delete>(
1869 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1870 section.SecProps, word.WordProps, bSecEnd));
1871 }
1872 }
1873 if (bPaint) {
1874 RearrangePart(CPVT_WordRange(m_wpOldCaret, m_wpCaret));
1875 ScrollToCaret();
1876 Refresh();
1877 SetCaretOrigin();
1878 SetCaretInfo();
1879 }
1880 if (m_bOprNotify && m_pOprNotify)
1881 m_pOprNotify->OnDelete(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001882
Tom Sepez3509d162017-01-30 13:22:02 -08001883 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001884}
1885
tsepez4cf55152016-11-02 14:37:54 -07001886bool CFX_Edit::Empty() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001887 if (m_pVT->IsValid()) {
1888 m_pVT->DeleteWords(GetWholeWordRange());
1889 SetCaret(m_pVT->GetBeginWordPlace());
1890
tsepez4cf55152016-11-02 14:37:54 -07001891 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001892 }
1893
tsepez4cf55152016-11-02 14:37:54 -07001894 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001895}
1896
tsepez4cf55152016-11-02 14:37:54 -07001897bool CFX_Edit::Clear(bool bAddUndo, bool bPaint) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001898 if (!m_pVT->IsValid() || m_SelState.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001899 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001900
thestig821d59e2016-05-11 12:59:22 -07001901 CPVT_WordRange range = m_SelState.ConvertToWordRange();
dsinclaira2919b32016-07-13 10:55:48 -07001902 if (bAddUndo && m_bEnableUndo)
Tom Sepez3509d162017-01-30 13:22:02 -08001903 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Clear>(this, range, GetSelText()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001904
thestig821d59e2016-05-11 12:59:22 -07001905 SelectNone();
1906 SetCaret(m_pVT->DeleteWords(range));
1907 m_SelState.Set(m_wpCaret, m_wpCaret);
thestig821d59e2016-05-11 12:59:22 -07001908 if (bPaint) {
1909 RearrangePart(range);
1910 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001911 Refresh();
thestig821d59e2016-05-11 12:59:22 -07001912 SetCaretOrigin();
1913 SetCaretInfo();
1914 }
thestig821d59e2016-05-11 12:59:22 -07001915 if (m_bOprNotify && m_pOprNotify)
1916 m_pOprNotify->OnClear(m_wpCaret, m_wpOldCaret);
1917
tsepez4cf55152016-11-02 14:37:54 -07001918 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001919}
1920
tsepez4cf55152016-11-02 14:37:54 -07001921bool CFX_Edit::InsertText(const CFX_WideString& sText,
1922 int32_t charset,
1923 bool bAddUndo,
1924 bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001925 if (IsTextOverflow())
tsepez4cf55152016-11-02 14:37:54 -07001926 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001927
1928 m_pVT->UpdateWordPlace(m_wpCaret);
tsepeza31da742016-09-08 11:28:14 -07001929 SetCaret(DoInsertText(m_wpCaret, sText, charset));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001930 m_SelState.Set(m_wpCaret, m_wpCaret);
tsepeza31da742016-09-08 11:28:14 -07001931 if (m_wpCaret == m_wpOldCaret)
tsepez4cf55152016-11-02 14:37:54 -07001932 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001933
tsepeza31da742016-09-08 11:28:14 -07001934 if (bAddUndo && m_bEnableUndo) {
Tom Sepez3509d162017-01-30 13:22:02 -08001935 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertText>(
1936 this, m_wpOldCaret, m_wpCaret, sText, charset));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001937 }
tsepeza31da742016-09-08 11:28:14 -07001938 if (bPaint)
1939 PaintInsertText(m_wpOldCaret, m_wpCaret);
1940
1941 if (m_bOprNotify && m_pOprNotify)
1942 m_pOprNotify->OnInsertText(m_wpCaret, m_wpOldCaret);
1943
tsepez4cf55152016-11-02 14:37:54 -07001944 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001945}
1946
1947void CFX_Edit::PaintInsertText(const CPVT_WordPlace& wpOld,
1948 const CPVT_WordPlace& wpNew) {
1949 if (m_pVT->IsValid()) {
1950 RearrangePart(CPVT_WordRange(wpOld, wpNew));
1951 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001952 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001953 SetCaretOrigin();
1954 SetCaretInfo();
1955 }
1956}
1957
tsepez4cf55152016-11-02 14:37:54 -07001958bool CFX_Edit::Redo() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001959 if (m_bEnableUndo) {
1960 if (m_Undo.CanRedo()) {
1961 m_Undo.Redo();
tsepez4cf55152016-11-02 14:37:54 -07001962 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001963 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001964 }
1965
tsepez4cf55152016-11-02 14:37:54 -07001966 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001967}
1968
tsepez4cf55152016-11-02 14:37:54 -07001969bool CFX_Edit::Undo() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001970 if (m_bEnableUndo) {
1971 if (m_Undo.CanUndo()) {
1972 m_Undo.Undo();
tsepez4cf55152016-11-02 14:37:54 -07001973 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001974 }
1975 }
1976
tsepez4cf55152016-11-02 14:37:54 -07001977 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001978}
1979
1980void CFX_Edit::SetCaretOrigin() {
thestig821d59e2016-05-11 12:59:22 -07001981 if (!m_pVT->IsValid())
1982 return;
1983
1984 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1985 pIterator->SetAt(m_wpCaret);
1986 CPVT_Word word;
1987 CPVT_Line line;
1988 if (pIterator->GetWord(word)) {
1989 m_ptCaret.x = word.ptWord.x + word.fWidth;
1990 m_ptCaret.y = word.ptWord.y;
1991 } else if (pIterator->GetLine(line)) {
1992 m_ptCaret.x = line.ptLine.x;
1993 m_ptCaret.y = line.ptLine.y;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001994 }
1995}
1996
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001997CPVT_WordPlace CFX_Edit::WordIndexToWordPlace(int32_t index) const {
1998 if (m_pVT->IsValid())
1999 return m_pVT->WordIndexToWordPlace(index);
2000
2001 return CPVT_WordPlace();
2002}
2003
tsepez4cf55152016-11-02 14:37:54 -07002004bool CFX_Edit::IsTextFull() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002005 int32_t nTotalWords = m_pVT->GetTotalWords();
2006 int32_t nLimitChar = m_pVT->GetLimitChar();
2007 int32_t nCharArray = m_pVT->GetCharArray();
2008
2009 return IsTextOverflow() || (nLimitChar > 0 && nTotalWords >= nLimitChar) ||
2010 (nCharArray > 0 && nTotalWords >= nCharArray);
2011}
2012
tsepez4cf55152016-11-02 14:37:54 -07002013bool CFX_Edit::IsTextOverflow() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002014 if (!m_bEnableScroll && !m_bEnableOverflow) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08002015 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
2016 CFX_FloatRect rcContent = m_pVT->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002017
dsinclair448c4332016-08-02 12:07:35 -07002018 if (m_pVT->IsMultiLine() && GetTotalLines() > 1 &&
2019 IsFloatBigger(rcContent.Height(), rcPlate.Height())) {
tsepez4cf55152016-11-02 14:37:54 -07002020 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002021 }
2022
dsinclair448c4332016-08-02 12:07:35 -07002023 if (IsFloatBigger(rcContent.Width(), rcPlate.Width()))
tsepez4cf55152016-11-02 14:37:54 -07002024 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002025 }
2026
tsepez4cf55152016-11-02 14:37:54 -07002027 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002028}
2029
tsepez4cf55152016-11-02 14:37:54 -07002030bool CFX_Edit::CanUndo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002031 if (m_bEnableUndo) {
2032 return m_Undo.CanUndo();
2033 }
2034
tsepez4cf55152016-11-02 14:37:54 -07002035 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002036}
2037
tsepez4cf55152016-11-02 14:37:54 -07002038bool CFX_Edit::CanRedo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002039 if (m_bEnableUndo) {
2040 return m_Undo.CanRedo();
2041 }
2042
tsepez4cf55152016-11-02 14:37:54 -07002043 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002044}
2045
tsepez4cf55152016-11-02 14:37:54 -07002046void CFX_Edit::EnableRefresh(bool bRefresh) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002047 m_bEnableRefresh = bRefresh;
2048}
2049
tsepez4cf55152016-11-02 14:37:54 -07002050void CFX_Edit::EnableUndo(bool bUndo) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002051 m_bEnableUndo = bUndo;
2052}
2053
tsepez4cf55152016-11-02 14:37:54 -07002054void CFX_Edit::EnableOprNotify(bool bNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002055 m_bOprNotify = bNotify;
2056}
2057
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002058CPVT_WordPlace CFX_Edit::DoInsertText(const CPVT_WordPlace& place,
tsepeza31da742016-09-08 11:28:14 -07002059 const CFX_WideString& sText,
dsinclairefd5a992016-07-18 10:04:07 -07002060 int32_t charset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002061 CPVT_WordPlace wp = place;
2062
2063 if (m_pVT->IsValid()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002064 for (int32_t i = 0, sz = sText.GetLength(); i < sz; i++) {
Tom Sepez62a70f92016-03-21 15:00:20 -07002065 uint16_t word = sText[i];
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002066 switch (word) {
2067 case 0x0D:
dsinclairefd5a992016-07-18 10:04:07 -07002068 wp = m_pVT->InsertSection(wp, nullptr, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002069 if (sText[i + 1] == 0x0A)
2070 i++;
2071 break;
2072 case 0x0A:
dsinclairefd5a992016-07-18 10:04:07 -07002073 wp = m_pVT->InsertSection(wp, nullptr, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002074 if (sText[i + 1] == 0x0D)
2075 i++;
2076 break;
2077 case 0x09:
2078 word = 0x20;
2079 default:
2080 wp = m_pVT->InsertWord(wp, word, GetCharSetFromUnicode(word, charset),
dsinclairefd5a992016-07-18 10:04:07 -07002081 nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002082 break;
2083 }
2084 }
2085 }
2086
2087 return wp;
2088}
2089
Tom Sepez62a70f92016-03-21 15:00:20 -07002090int32_t CFX_Edit::GetCharSetFromUnicode(uint16_t word, int32_t nOldCharset) {
dsinclairc7a73492016-04-05 12:01:42 -07002091 if (IPVT_FontMap* pFontMap = GetFontMap())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002092 return pFontMap->CharSetFromUnicode(word, nOldCharset);
2093 return nOldCharset;
2094}
2095
Tom Sepez3509d162017-01-30 13:22:02 -08002096void CFX_Edit::AddEditUndoItem(
2097 std::unique_ptr<CFX_Edit_UndoItem> pEditUndoItem) {
Lei Zhang1a89e362017-03-23 15:27:25 -07002098 m_Undo.AddItem(std::move(pEditUndoItem));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002099}
2100
weili625ad662016-06-15 11:21:33 -07002101CFX_Edit_LineRectArray::CFX_Edit_LineRectArray() {}
2102
Tom Sepez3509d162017-01-30 13:22:02 -08002103CFX_Edit_LineRectArray::~CFX_Edit_LineRectArray() {}
weili625ad662016-06-15 11:21:33 -07002104
Tom Sepez3509d162017-01-30 13:22:02 -08002105void CFX_Edit_LineRectArray::operator=(CFX_Edit_LineRectArray&& that) {
2106 m_LineRects = std::move(that.m_LineRects);
weili625ad662016-06-15 11:21:33 -07002107}
2108
2109void CFX_Edit_LineRectArray::Add(const CPVT_WordRange& wrLine,
2110 const CFX_FloatRect& rcLine) {
Tom Sepez3509d162017-01-30 13:22:02 -08002111 m_LineRects.push_back(pdfium::MakeUnique<CFX_Edit_LineRect>(wrLine, rcLine));
weili625ad662016-06-15 11:21:33 -07002112}
2113
2114int32_t CFX_Edit_LineRectArray::GetSize() const {
Tom Sepez3509d162017-01-30 13:22:02 -08002115 return pdfium::CollectionSize<int32_t>(m_LineRects);
weili625ad662016-06-15 11:21:33 -07002116}
2117
2118CFX_Edit_LineRect* CFX_Edit_LineRectArray::GetAt(int32_t nIndex) const {
Tom Sepez3509d162017-01-30 13:22:02 -08002119 if (nIndex < 0 || nIndex >= GetSize())
weili625ad662016-06-15 11:21:33 -07002120 return nullptr;
2121
Tom Sepez3509d162017-01-30 13:22:02 -08002122 return m_LineRects[nIndex].get();
weili625ad662016-06-15 11:21:33 -07002123}
2124
2125CFX_Edit_Select::CFX_Edit_Select() {}
2126
weili625ad662016-06-15 11:21:33 -07002127CFX_Edit_Select::CFX_Edit_Select(const CPVT_WordRange& range) {
2128 Set(range.BeginPos, range.EndPos);
2129}
2130
2131CPVT_WordRange CFX_Edit_Select::ConvertToWordRange() const {
2132 return CPVT_WordRange(BeginPos, EndPos);
2133}
2134
Tom Sepez52f69b32017-03-21 13:42:38 -07002135void CFX_Edit_Select::Reset() {
2136 BeginPos.Reset();
2137 EndPos.Reset();
weili625ad662016-06-15 11:21:33 -07002138}
2139
2140void CFX_Edit_Select::Set(const CPVT_WordPlace& begin,
2141 const CPVT_WordPlace& end) {
2142 BeginPos = begin;
2143 EndPos = end;
2144}
2145
weili625ad662016-06-15 11:21:33 -07002146void CFX_Edit_Select::SetEndPos(const CPVT_WordPlace& end) {
2147 EndPos = end;
2148}
2149
Tom Sepez52f69b32017-03-21 13:42:38 -07002150bool CFX_Edit_Select::IsEmpty() const {
2151 return BeginPos == EndPos;
weili625ad662016-06-15 11:21:33 -07002152}
2153
weili625ad662016-06-15 11:21:33 -07002154CFX_Edit_RectArray::CFX_Edit_RectArray() {}
2155
Tom Sepez3509d162017-01-30 13:22:02 -08002156CFX_Edit_RectArray::~CFX_Edit_RectArray() {}
weili625ad662016-06-15 11:21:33 -07002157
Tom Sepez3509d162017-01-30 13:22:02 -08002158void CFX_Edit_RectArray::Clear() {
2159 m_Rects.clear();
weili625ad662016-06-15 11:21:33 -07002160}
2161
2162void CFX_Edit_RectArray::Add(const CFX_FloatRect& rect) {
2163 // check for overlapped area
Tom Sepez3509d162017-01-30 13:22:02 -08002164 for (const auto& pRect : m_Rects) {
weili625ad662016-06-15 11:21:33 -07002165 if (pRect && pRect->Contains(rect))
2166 return;
2167 }
Tom Sepez3509d162017-01-30 13:22:02 -08002168 m_Rects.push_back(pdfium::MakeUnique<CFX_FloatRect>(rect));
weili625ad662016-06-15 11:21:33 -07002169}
2170
2171int32_t CFX_Edit_RectArray::GetSize() const {
Tom Sepez3509d162017-01-30 13:22:02 -08002172 return pdfium::CollectionSize<int32_t>(m_Rects);
weili625ad662016-06-15 11:21:33 -07002173}
2174
2175CFX_FloatRect* CFX_Edit_RectArray::GetAt(int32_t nIndex) const {
Tom Sepez3509d162017-01-30 13:22:02 -08002176 if (nIndex < 0 || nIndex >= GetSize())
weili625ad662016-06-15 11:21:33 -07002177 return nullptr;
2178
Tom Sepez3509d162017-01-30 13:22:02 -08002179 return m_Rects[nIndex].get();
weili625ad662016-06-15 11:21:33 -07002180}