blob: 884232dc76cadbe2d4e874c1bcfaa311225c0c56 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
dsinclair0bb385b2016-09-29 17:03:59 -07007#include "fpdfsdk/fxedit/fxet_edit.h"
Lei Zhangc2fb35f2016-01-05 16:46:58 -08008
Lei Zhang375a8642016-01-11 11:59:17 -08009#include <algorithm>
thestigd4c34f22016-09-28 17:04:51 -070010#include <memory>
11#include <utility>
Lei Zhang375a8642016-01-11 11:59:17 -080012
dsinclairbc5e6d22016-10-04 11:08:49 -070013#include "core/fpdfapi/font/cpdf_font.h"
dsinclair41872fa2016-10-04 11:29:35 -070014#include "core/fpdfapi/page/cpdf_pageobject.h"
15#include "core/fpdfapi/page/cpdf_pageobjectholder.h"
16#include "core/fpdfapi/page/cpdf_pathobject.h"
17#include "core/fpdfapi/page/cpdf_textobject.h"
dsinclair488b7ad2016-10-04 11:55:50 -070018#include "core/fpdfapi/parser/fpdf_parser_decode.h"
dsinclair69d9c682016-10-04 12:18:35 -070019#include "core/fpdfapi/render/cpdf_renderoptions.h"
20#include "core/fpdfapi/render/cpdf_textrenderer.h"
dsinclair1727aee2016-09-29 13:12:56 -070021#include "core/fpdfdoc/cpvt_section.h"
22#include "core/fpdfdoc/cpvt_word.h"
23#include "core/fpdfdoc/ipvt_fontmap.h"
dsinclair74a34fc2016-09-29 16:41:42 -070024#include "core/fxge/cfx_graphstatedata.h"
25#include "core/fxge/cfx_pathdata.h"
26#include "core/fxge/cfx_renderdevice.h"
dsinclaire35af1e2016-07-13 11:26:20 -070027#include "fpdfsdk/cfx_systemhandler.h"
dsinclair0bb385b2016-09-29 17:03:59 -070028#include "fpdfsdk/fxedit/fx_edit.h"
dsinclaire35af1e2016-07-13 11:26:20 -070029#include "fpdfsdk/pdfwindow/PWL_Edit.h"
30#include "fpdfsdk/pdfwindow/PWL_EditCtrl.h"
tsepez36eb4bd2016-10-03 15:24:27 -070031#include "third_party/base/ptr_util.h"
Tom Sepez3509d162017-01-30 13:22:02 -080032#include "third_party/base/stl_util.h"
dsinclaire35af1e2016-07-13 11:26:20 -070033
dsinclair8f4bf9a2016-05-04 13:51:51 -070034namespace {
35
36const int kEditUndoMaxItems = 10000;
37
dsinclaire35af1e2016-07-13 11:26:20 -070038CFX_ByteString GetWordRenderString(const CFX_ByteString& strWords) {
39 if (strWords.GetLength() > 0)
40 return PDF_EncodeString(strWords) + " Tj\n";
41 return CFX_ByteString();
42}
43
44CFX_ByteString GetFontSetString(IPVT_FontMap* pFontMap,
45 int32_t nFontIndex,
Dan Sinclair05df0752017-03-14 14:43:42 -040046 float fFontSize) {
dsinclaire35af1e2016-07-13 11:26:20 -070047 if (!pFontMap)
48 return CFX_ByteString();
49
50 CFX_ByteString sFontAlias = pFontMap->GetPDFFontAlias(nFontIndex);
51 if (sFontAlias.GetLength() <= 0 || fFontSize <= 0)
52 return CFX_ByteString();
53
54 CFX_ByteTextBuf sRet;
55 sRet << "/" << sFontAlias << " " << fFontSize << " Tf\n";
56 return sRet.MakeString();
57}
58
dsinclaire35af1e2016-07-13 11:26:20 -070059void DrawTextString(CFX_RenderDevice* pDevice,
Dan Sinclairf528eee2017-02-14 11:52:07 -050060 const CFX_PointF& pt,
dsinclaire35af1e2016-07-13 11:26:20 -070061 CPDF_Font* pFont,
Dan Sinclair05df0752017-03-14 14:43:42 -040062 float fFontSize,
dsinclaire35af1e2016-07-13 11:26:20 -070063 CFX_Matrix* pUser2Device,
64 const CFX_ByteString& str,
65 FX_ARGB crTextFill,
dsinclaire35af1e2016-07-13 11:26:20 -070066 int32_t nHorzScale) {
Dan Sinclaira0061af2017-02-23 09:25:17 -050067 CFX_PointF pos = pUser2Device->Transform(pt);
dsinclaire35af1e2016-07-13 11:26:20 -070068
69 if (pFont) {
70 if (nHorzScale != 100) {
71 CFX_Matrix mt(nHorzScale / 100.0f, 0, 0, 1, 0, 0);
72 mt.Concat(*pUser2Device);
73
74 CPDF_RenderOptions ro;
75 ro.m_Flags = RENDER_CLEARTYPE;
76 ro.m_ColorMode = RENDER_COLOR_NORMAL;
77
Dan Sinclaira0061af2017-02-23 09:25:17 -050078 CPDF_TextRenderer::DrawTextString(pDevice, pos.x, pos.y, pFont, fFontSize,
79 &mt, str, crTextFill, nullptr, &ro);
dsinclaire35af1e2016-07-13 11:26:20 -070080 } else {
81 CPDF_RenderOptions ro;
82 ro.m_Flags = RENDER_CLEARTYPE;
83 ro.m_ColorMode = RENDER_COLOR_NORMAL;
84
Dan Sinclaira0061af2017-02-23 09:25:17 -050085 CPDF_TextRenderer::DrawTextString(pDevice, pos.x, pos.y, pFont, fFontSize,
86 pUser2Device, str, crTextFill, nullptr,
87 &ro);
dsinclaire35af1e2016-07-13 11:26:20 -070088 }
89 }
90}
91
dsinclair8f4bf9a2016-05-04 13:51:51 -070092} // namespace
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070093
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094CFX_Edit_Iterator::CFX_Edit_Iterator(CFX_Edit* pEdit,
dsinclairc7a73492016-04-05 12:01:42 -070095 CPDF_VariableText::Iterator* pVTIterator)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 : m_pEdit(pEdit), m_pVTIterator(pVTIterator) {}
97
98CFX_Edit_Iterator::~CFX_Edit_Iterator() {}
99
tsepez4cf55152016-11-02 14:37:54 -0700100bool CFX_Edit_Iterator::NextWord() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 return m_pVTIterator->NextWord();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700102}
103
tsepez4cf55152016-11-02 14:37:54 -0700104bool CFX_Edit_Iterator::PrevWord() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105 return m_pVTIterator->PrevWord();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700106}
107
tsepez4cf55152016-11-02 14:37:54 -0700108bool CFX_Edit_Iterator::GetWord(CPVT_Word& word) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700109 ASSERT(m_pEdit);
110
111 if (m_pVTIterator->GetWord(word)) {
112 word.ptWord = m_pEdit->VTToEdit(word.ptWord);
tsepez4cf55152016-11-02 14:37:54 -0700113 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114 }
tsepez4cf55152016-11-02 14:37:54 -0700115 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700116}
117
tsepez4cf55152016-11-02 14:37:54 -0700118bool CFX_Edit_Iterator::GetLine(CPVT_Line& line) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700119 ASSERT(m_pEdit);
120
121 if (m_pVTIterator->GetLine(line)) {
122 line.ptLine = m_pEdit->VTToEdit(line.ptLine);
tsepez4cf55152016-11-02 14:37:54 -0700123 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 }
tsepez4cf55152016-11-02 14:37:54 -0700125 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700126}
127
tsepez4cf55152016-11-02 14:37:54 -0700128bool CFX_Edit_Iterator::GetSection(CPVT_Section& section) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129 ASSERT(m_pEdit);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700130
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131 if (m_pVTIterator->GetSection(section)) {
132 section.rcSection = m_pEdit->VTToEdit(section.rcSection);
tsepez4cf55152016-11-02 14:37:54 -0700133 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134 }
tsepez4cf55152016-11-02 14:37:54 -0700135 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700136}
137
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138void CFX_Edit_Iterator::SetAt(int32_t nWordIndex) {
139 m_pVTIterator->SetAt(nWordIndex);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700140}
141
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700142void CFX_Edit_Iterator::SetAt(const CPVT_WordPlace& place) {
143 m_pVTIterator->SetAt(place);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700144}
145
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146const CPVT_WordPlace& CFX_Edit_Iterator::GetAt() const {
147 return m_pVTIterator->GetAt();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700148}
149
dsinclairc7a73492016-04-05 12:01:42 -0700150CFX_Edit_Provider::CFX_Edit_Provider(IPVT_FontMap* pFontMap)
151 : CPDF_VariableText::Provider(pFontMap), m_pFontMap(pFontMap) {
Lei Zhang96660d62015-12-14 18:27:25 -0800152 ASSERT(m_pFontMap);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700153}
154
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700155CFX_Edit_Provider::~CFX_Edit_Provider() {}
156
dsinclairc7a73492016-04-05 12:01:42 -0700157IPVT_FontMap* CFX_Edit_Provider::GetFontMap() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158 return m_pFontMap;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700159}
160
npm41d6bbe2016-09-14 11:54:44 -0700161int32_t CFX_Edit_Provider::GetCharWidth(int32_t nFontIndex, uint16_t word) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162 if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex)) {
tsepezc3255f52016-03-25 14:52:27 -0700163 uint32_t charcode = word;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700164
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165 if (pPDFFont->IsUnicodeCompatible())
166 charcode = pPDFFont->CharCodeFromUnicode(word);
167 else
168 charcode = m_pFontMap->CharCodeFromUnicode(nFontIndex, word);
169
Wei Li89409932016-03-28 10:33:33 -0700170 if (charcode != CPDF_Font::kInvalidCharCode)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171 return pPDFFont->GetCharWidthF(charcode);
172 }
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::GetTypeAscent(int32_t nFontIndex) {
178 if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex))
179 return pPDFFont->GetTypeAscent();
180
181 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700182}
183
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700184int32_t CFX_Edit_Provider::GetTypeDescent(int32_t nFontIndex) {
185 if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex))
186 return pPDFFont->GetTypeDescent();
187
188 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700189}
190
Tom Sepez62a70f92016-03-21 15:00:20 -0700191int32_t CFX_Edit_Provider::GetWordFontIndex(uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700192 int32_t charset,
193 int32_t nFontIndex) {
194 return m_pFontMap->GetWordFontIndex(word, charset, nFontIndex);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700195}
196
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700197int32_t CFX_Edit_Provider::GetDefaultFontIndex() {
198 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700199}
200
tsepez4cf55152016-11-02 14:37:54 -0700201bool CFX_Edit_Provider::IsLatinWord(uint16_t word) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202 return FX_EDIT_ISLATINWORD(word);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700203}
204
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205CFX_Edit_Refresh::CFX_Edit_Refresh() {}
206
207CFX_Edit_Refresh::~CFX_Edit_Refresh() {}
208
209void CFX_Edit_Refresh::BeginRefresh() {
Tom Sepez3509d162017-01-30 13:22:02 -0800210 m_RefreshRects.Clear();
211 m_OldLineRects = std::move(m_NewLineRects);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700212}
213
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700214void CFX_Edit_Refresh::Push(const CPVT_WordRange& linerange,
Tom Sepez281a9ea2016-02-26 14:24:28 -0800215 const CFX_FloatRect& rect) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216 m_NewLineRects.Add(linerange, rect);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700217}
218
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219void CFX_Edit_Refresh::NoAnalyse() {
220 {
221 for (int32_t i = 0, sz = m_OldLineRects.GetSize(); i < sz; i++)
222 if (CFX_Edit_LineRect* pOldRect = m_OldLineRects.GetAt(i))
223 m_RefreshRects.Add(pOldRect->m_rcLine);
224 }
225
226 {
227 for (int32_t i = 0, sz = m_NewLineRects.GetSize(); i < sz; i++)
228 if (CFX_Edit_LineRect* pNewRect = m_NewLineRects.GetAt(i))
229 m_RefreshRects.Add(pNewRect->m_rcLine);
230 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700231}
232
Tom Sepez281a9ea2016-02-26 14:24:28 -0800233void CFX_Edit_Refresh::AddRefresh(const CFX_FloatRect& rect) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234 m_RefreshRects.Add(rect);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700235}
236
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237const CFX_Edit_RectArray* CFX_Edit_Refresh::GetRefreshRects() const {
238 return &m_RefreshRects;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700239}
240
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241void CFX_Edit_Refresh::EndRefresh() {
Tom Sepez3509d162017-01-30 13:22:02 -0800242 m_RefreshRects.Clear();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700243}
244
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245CFX_Edit_Undo::CFX_Edit_Undo(int32_t nBufsize)
246 : m_nCurUndoPos(0),
247 m_nBufSize(nBufsize),
tsepez4cf55152016-11-02 14:37:54 -0700248 m_bModified(false),
249 m_bVirgin(true),
250 m_bWorking(false) {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251
252CFX_Edit_Undo::~CFX_Edit_Undo() {
253 Reset();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700254}
255
tsepez4cf55152016-11-02 14:37:54 -0700256bool CFX_Edit_Undo::CanUndo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257 return m_nCurUndoPos > 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700258}
259
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260void CFX_Edit_Undo::Undo() {
tsepez4cf55152016-11-02 14:37:54 -0700261 m_bWorking = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262 if (m_nCurUndoPos > 0) {
Tom Sepez3509d162017-01-30 13:22:02 -0800263 m_UndoItemStack[m_nCurUndoPos - 1]->Undo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264 m_nCurUndoPos--;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700265 m_bModified = (m_nCurUndoPos != 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266 }
tsepez4cf55152016-11-02 14:37:54 -0700267 m_bWorking = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700268}
269
tsepez4cf55152016-11-02 14:37:54 -0700270bool CFX_Edit_Undo::CanRedo() const {
Tom Sepez3509d162017-01-30 13:22:02 -0800271 return m_nCurUndoPos < m_UndoItemStack.size();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700272}
273
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274void CFX_Edit_Undo::Redo() {
tsepez4cf55152016-11-02 14:37:54 -0700275 m_bWorking = true;
Tom Sepez3509d162017-01-30 13:22:02 -0800276 if (m_nCurUndoPos < m_UndoItemStack.size()) {
277 m_UndoItemStack[m_nCurUndoPos]->Redo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278 m_nCurUndoPos++;
Tom Sepez3509d162017-01-30 13:22:02 -0800279 m_bModified = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280 }
tsepez4cf55152016-11-02 14:37:54 -0700281 m_bWorking = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700282}
283
Tom Sepez3509d162017-01-30 13:22:02 -0800284void CFX_Edit_Undo::AddItem(std::unique_ptr<IFX_Edit_UndoItem> pItem) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285 ASSERT(!m_bWorking);
Lei Zhang96660d62015-12-14 18:27:25 -0800286 ASSERT(pItem);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287 ASSERT(m_nBufSize > 1);
Tom Sepez3509d162017-01-30 13:22:02 -0800288 if (m_nCurUndoPos < m_UndoItemStack.size())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700289 RemoveTails();
290
Tom Sepez3509d162017-01-30 13:22:02 -0800291 if (m_UndoItemStack.size() >= m_nBufSize) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700292 RemoveHeads();
tsepez4cf55152016-11-02 14:37:54 -0700293 m_bVirgin = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700294 }
295
Tom Sepez3509d162017-01-30 13:22:02 -0800296 m_UndoItemStack.push_back(std::move(pItem));
297 m_nCurUndoPos = m_UndoItemStack.size();
298 m_bModified = true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700299}
300
tsepez4cf55152016-11-02 14:37:54 -0700301bool CFX_Edit_Undo::IsModified() const {
302 return m_bVirgin ? m_bModified : true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700303}
304
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305void CFX_Edit_Undo::RemoveHeads() {
Tom Sepez3509d162017-01-30 13:22:02 -0800306 ASSERT(m_UndoItemStack.size() > 1);
307 m_UndoItemStack.pop_front();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700308}
309
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700310void CFX_Edit_Undo::RemoveTails() {
Tom Sepez3509d162017-01-30 13:22:02 -0800311 while (m_UndoItemStack.size() > m_nCurUndoPos)
312 m_UndoItemStack.pop_back();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313}
314
315void CFX_Edit_Undo::Reset() {
Tom Sepez3509d162017-01-30 13:22:02 -0800316 m_UndoItemStack.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700317 m_nCurUndoPos = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700318}
319
tsepez4cf55152016-11-02 14:37:54 -0700320CFX_Edit_UndoItem::CFX_Edit_UndoItem() : m_bFirst(true), m_bLast(true) {}
weili625ad662016-06-15 11:21:33 -0700321
322CFX_Edit_UndoItem::~CFX_Edit_UndoItem() {}
323
Tom Sepez3509d162017-01-30 13:22:02 -0800324CFX_WideString CFX_Edit_UndoItem::GetUndoTitle() const {
325 return CFX_WideString();
weili625ad662016-06-15 11:21:33 -0700326}
327
tsepez4cf55152016-11-02 14:37:54 -0700328void CFX_Edit_UndoItem::SetFirst(bool bFirst) {
weili625ad662016-06-15 11:21:33 -0700329 m_bFirst = bFirst;
330}
331
tsepez4cf55152016-11-02 14:37:54 -0700332void CFX_Edit_UndoItem::SetLast(bool bLast) {
weili625ad662016-06-15 11:21:33 -0700333 m_bLast = bLast;
334}
335
tsepez4cf55152016-11-02 14:37:54 -0700336bool CFX_Edit_UndoItem::IsLast() {
weili625ad662016-06-15 11:21:33 -0700337 return m_bLast;
338}
339
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340CFX_Edit_GroupUndoItem::CFX_Edit_GroupUndoItem(const CFX_WideString& sTitle)
341 : m_sTitle(sTitle) {}
342
Tom Sepez3509d162017-01-30 13:22:02 -0800343CFX_Edit_GroupUndoItem::~CFX_Edit_GroupUndoItem() {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700344
Tom Sepez3509d162017-01-30 13:22:02 -0800345void CFX_Edit_GroupUndoItem::AddUndoItem(
346 std::unique_ptr<CFX_Edit_UndoItem> pUndoItem) {
tsepez4cf55152016-11-02 14:37:54 -0700347 pUndoItem->SetFirst(false);
348 pUndoItem->SetLast(false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700349 if (m_sTitle.IsEmpty())
350 m_sTitle = pUndoItem->GetUndoTitle();
Tom Sepez3509d162017-01-30 13:22:02 -0800351
352 m_Items.push_back(std::move(pUndoItem));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700353}
354
355void CFX_Edit_GroupUndoItem::UpdateItems() {
Tom Sepez3509d162017-01-30 13:22:02 -0800356 if (!m_Items.empty()) {
357 m_Items.front()->SetFirst(true);
358 m_Items.back()->SetLast(true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700359 }
360}
361
362void CFX_Edit_GroupUndoItem::Undo() {
Tom Sepez3509d162017-01-30 13:22:02 -0800363 for (auto iter = m_Items.rbegin(); iter != m_Items.rend(); ++iter)
364 (*iter)->Undo();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700365}
366
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700367void CFX_Edit_GroupUndoItem::Redo() {
Tom Sepez3509d162017-01-30 13:22:02 -0800368 for (auto iter = m_Items.begin(); iter != m_Items.end(); ++iter)
369 (*iter)->Redo();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700370}
371
Tom Sepez3509d162017-01-30 13:22:02 -0800372CFX_WideString CFX_Edit_GroupUndoItem::GetUndoTitle() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700373 return m_sTitle;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700374}
375
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700376CFXEU_InsertWord::CFXEU_InsertWord(CFX_Edit* pEdit,
377 const CPVT_WordPlace& wpOldPlace,
378 const CPVT_WordPlace& wpNewPlace,
Tom Sepez62a70f92016-03-21 15:00:20 -0700379 uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700380 int32_t charset,
381 const CPVT_WordProps* pWordProps)
382 : m_pEdit(pEdit),
383 m_wpOld(wpOldPlace),
384 m_wpNew(wpNewPlace),
385 m_Word(word),
386 m_nCharset(charset),
387 m_WordProps() {
388 if (pWordProps)
389 m_WordProps = *pWordProps;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700390}
391
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700392CFXEU_InsertWord::~CFXEU_InsertWord() {}
393
394void CFXEU_InsertWord::Redo() {
395 if (m_pEdit) {
396 m_pEdit->SelectNone();
397 m_pEdit->SetCaret(m_wpOld);
tsepez4cf55152016-11-02 14:37:54 -0700398 m_pEdit->InsertWord(m_Word, m_nCharset, &m_WordProps, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700399 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700400}
401
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700402void CFXEU_InsertWord::Undo() {
403 if (m_pEdit) {
404 m_pEdit->SelectNone();
405 m_pEdit->SetCaret(m_wpNew);
tsepez4cf55152016-11-02 14:37:54 -0700406 m_pEdit->Backspace(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700407 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700408}
409
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700410CFXEU_InsertReturn::CFXEU_InsertReturn(CFX_Edit* pEdit,
411 const CPVT_WordPlace& wpOldPlace,
412 const CPVT_WordPlace& wpNewPlace,
413 const CPVT_SecProps* pSecProps,
414 const CPVT_WordProps* pWordProps)
415 : m_pEdit(pEdit),
416 m_wpOld(wpOldPlace),
417 m_wpNew(wpNewPlace),
418 m_SecProps(),
419 m_WordProps() {
420 if (pSecProps)
421 m_SecProps = *pSecProps;
422 if (pWordProps)
423 m_WordProps = *pWordProps;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700424}
425
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700426CFXEU_InsertReturn::~CFXEU_InsertReturn() {}
427
428void CFXEU_InsertReturn::Redo() {
429 if (m_pEdit) {
430 m_pEdit->SelectNone();
431 m_pEdit->SetCaret(m_wpOld);
tsepez4cf55152016-11-02 14:37:54 -0700432 m_pEdit->InsertReturn(&m_SecProps, &m_WordProps, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700433 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700434}
435
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700436void CFXEU_InsertReturn::Undo() {
437 if (m_pEdit) {
438 m_pEdit->SelectNone();
439 m_pEdit->SetCaret(m_wpNew);
tsepez4cf55152016-11-02 14:37:54 -0700440 m_pEdit->Backspace(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700441 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700442}
443
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700444CFXEU_Backspace::CFXEU_Backspace(CFX_Edit* pEdit,
445 const CPVT_WordPlace& wpOldPlace,
446 const CPVT_WordPlace& wpNewPlace,
Tom Sepez62a70f92016-03-21 15:00:20 -0700447 uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700448 int32_t charset,
449 const CPVT_SecProps& SecProps,
450 const CPVT_WordProps& WordProps)
451 : m_pEdit(pEdit),
452 m_wpOld(wpOldPlace),
453 m_wpNew(wpNewPlace),
454 m_Word(word),
455 m_nCharset(charset),
456 m_SecProps(SecProps),
457 m_WordProps(WordProps) {}
458
459CFXEU_Backspace::~CFXEU_Backspace() {}
460
461void CFXEU_Backspace::Redo() {
Tom Sepez52f69b32017-03-21 13:42:38 -0700462 if (!m_pEdit)
463 return;
464
465 m_pEdit->SelectNone();
466 m_pEdit->SetCaret(m_wpOld);
467 m_pEdit->Backspace(false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700468}
469
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700470void CFXEU_Backspace::Undo() {
Tom Sepez52f69b32017-03-21 13:42:38 -0700471 if (!m_pEdit)
472 return;
473
474 m_pEdit->SelectNone();
475 m_pEdit->SetCaret(m_wpNew);
476 if (m_wpNew.nSecIndex != m_wpOld.nSecIndex)
477 m_pEdit->InsertReturn(&m_SecProps, &m_WordProps, false, true);
478 else
479 m_pEdit->InsertWord(m_Word, m_nCharset, &m_WordProps, false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700480}
481
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700482CFXEU_Delete::CFXEU_Delete(CFX_Edit* pEdit,
483 const CPVT_WordPlace& wpOldPlace,
484 const CPVT_WordPlace& wpNewPlace,
Tom Sepez62a70f92016-03-21 15:00:20 -0700485 uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700486 int32_t charset,
487 const CPVT_SecProps& SecProps,
488 const CPVT_WordProps& WordProps,
tsepez4cf55152016-11-02 14:37:54 -0700489 bool bSecEnd)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700490 : m_pEdit(pEdit),
491 m_wpOld(wpOldPlace),
492 m_wpNew(wpNewPlace),
493 m_Word(word),
494 m_nCharset(charset),
495 m_SecProps(SecProps),
496 m_WordProps(WordProps),
497 m_bSecEnd(bSecEnd) {}
498
499CFXEU_Delete::~CFXEU_Delete() {}
500
501void CFXEU_Delete::Redo() {
502 if (m_pEdit) {
503 m_pEdit->SelectNone();
504 m_pEdit->SetCaret(m_wpOld);
tsepez4cf55152016-11-02 14:37:54 -0700505 m_pEdit->Delete(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700506 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700507}
508
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700509void CFXEU_Delete::Undo() {
510 if (m_pEdit) {
511 m_pEdit->SelectNone();
512 m_pEdit->SetCaret(m_wpNew);
513 if (m_bSecEnd) {
tsepez4cf55152016-11-02 14:37:54 -0700514 m_pEdit->InsertReturn(&m_SecProps, &m_WordProps, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700515 } else {
tsepez4cf55152016-11-02 14:37:54 -0700516 m_pEdit->InsertWord(m_Word, m_nCharset, &m_WordProps, false, true);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700517 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700518 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700519}
520
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700521CFXEU_Clear::CFXEU_Clear(CFX_Edit* pEdit,
522 const CPVT_WordRange& wrSel,
523 const CFX_WideString& swText)
524 : m_pEdit(pEdit), m_wrSel(wrSel), m_swText(swText) {}
525
526CFXEU_Clear::~CFXEU_Clear() {}
527
528void CFXEU_Clear::Redo() {
529 if (m_pEdit) {
530 m_pEdit->SelectNone();
531 m_pEdit->SetSel(m_wrSel.BeginPos, m_wrSel.EndPos);
tsepez4cf55152016-11-02 14:37:54 -0700532 m_pEdit->Clear(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700533 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700534}
535
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700536void CFXEU_Clear::Undo() {
537 if (m_pEdit) {
538 m_pEdit->SelectNone();
539 m_pEdit->SetCaret(m_wrSel.BeginPos);
tsepez4cf55152016-11-02 14:37:54 -0700540 m_pEdit->InsertText(m_swText, FXFONT_DEFAULT_CHARSET, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700541 m_pEdit->SetSel(m_wrSel.BeginPos, m_wrSel.EndPos);
542 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700543}
544
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700545CFXEU_InsertText::CFXEU_InsertText(CFX_Edit* pEdit,
546 const CPVT_WordPlace& wpOldPlace,
547 const CPVT_WordPlace& wpNewPlace,
548 const CFX_WideString& swText,
dsinclairefd5a992016-07-18 10:04:07 -0700549 int32_t charset)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700550 : m_pEdit(pEdit),
551 m_wpOld(wpOldPlace),
552 m_wpNew(wpNewPlace),
553 m_swText(swText),
dsinclairefd5a992016-07-18 10:04:07 -0700554 m_nCharset(charset) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700555
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700556CFXEU_InsertText::~CFXEU_InsertText() {}
557
558void CFXEU_InsertText::Redo() {
559 if (m_pEdit && IsLast()) {
560 m_pEdit->SelectNone();
561 m_pEdit->SetCaret(m_wpOld);
tsepez4cf55152016-11-02 14:37:54 -0700562 m_pEdit->InsertText(m_swText, m_nCharset, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700563 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700564}
565
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700566void CFXEU_InsertText::Undo() {
567 if (m_pEdit) {
568 m_pEdit->SelectNone();
569 m_pEdit->SetSel(m_wpOld, m_wpNew);
tsepez4cf55152016-11-02 14:37:54 -0700570 m_pEdit->Clear(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700571 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700572}
573
dsinclaire35af1e2016-07-13 11:26:20 -0700574// static
575CFX_ByteString CFX_Edit::GetEditAppearanceStream(CFX_Edit* pEdit,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500576 const CFX_PointF& ptOffset,
dsinclaire35af1e2016-07-13 11:26:20 -0700577 const CPVT_WordRange* pRange,
tsepez4cf55152016-11-02 14:37:54 -0700578 bool bContinuous,
dsinclaire35af1e2016-07-13 11:26:20 -0700579 uint16_t SubWord) {
580 CFX_Edit_Iterator* pIterator = pEdit->GetIterator();
581 if (pRange)
582 pIterator->SetAt(pRange->BeginPos);
583 else
584 pIterator->SetAt(0);
585
586 CFX_ByteTextBuf sEditStream;
587 CFX_ByteTextBuf sWords;
588 int32_t nCurFontIndex = -1;
Dan Sinclairf528eee2017-02-14 11:52:07 -0500589 CFX_PointF ptOld;
590 CFX_PointF ptNew;
dsinclaire35af1e2016-07-13 11:26:20 -0700591 CPVT_WordPlace oldplace;
tsepez63f545c2016-09-13 16:08:49 -0700592
dsinclaire35af1e2016-07-13 11:26:20 -0700593 while (pIterator->NextWord()) {
594 CPVT_WordPlace place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -0700595 if (pRange && place > pRange->EndPos)
dsinclaire35af1e2016-07-13 11:26:20 -0700596 break;
597
598 if (bContinuous) {
599 if (place.LineCmp(oldplace) != 0) {
600 if (sWords.GetSize() > 0) {
601 sEditStream << GetWordRenderString(sWords.MakeString());
602 sWords.Clear();
603 }
604
605 CPVT_Word word;
606 if (pIterator->GetWord(word)) {
Dan Sinclairf528eee2017-02-14 11:52:07 -0500607 ptNew = CFX_PointF(word.ptWord.x + ptOffset.x,
608 word.ptWord.y + ptOffset.y);
dsinclaire35af1e2016-07-13 11:26:20 -0700609 } else {
610 CPVT_Line line;
611 pIterator->GetLine(line);
Dan Sinclairf528eee2017-02-14 11:52:07 -0500612 ptNew = CFX_PointF(line.ptLine.x + ptOffset.x,
613 line.ptLine.y + ptOffset.y);
dsinclaire35af1e2016-07-13 11:26:20 -0700614 }
615
616 if (ptNew.x != ptOld.x || ptNew.y != ptOld.y) {
617 sEditStream << ptNew.x - ptOld.x << " " << ptNew.y - ptOld.y
618 << " Td\n";
619
620 ptOld = ptNew;
621 }
622 }
623
624 CPVT_Word word;
625 if (pIterator->GetWord(word)) {
626 if (word.nFontIndex != nCurFontIndex) {
627 if (sWords.GetSize() > 0) {
628 sEditStream << GetWordRenderString(sWords.MakeString());
629 sWords.Clear();
630 }
631 sEditStream << GetFontSetString(pEdit->GetFontMap(), word.nFontIndex,
632 word.fFontSize);
633 nCurFontIndex = word.nFontIndex;
634 }
635
636 sWords << GetPDFWordString(pEdit->GetFontMap(), nCurFontIndex,
637 word.Word, SubWord);
638 }
639
640 oldplace = place;
641 } else {
642 CPVT_Word word;
643 if (pIterator->GetWord(word)) {
Dan Sinclairf528eee2017-02-14 11:52:07 -0500644 ptNew =
645 CFX_PointF(word.ptWord.x + ptOffset.x, word.ptWord.y + ptOffset.y);
dsinclaire35af1e2016-07-13 11:26:20 -0700646
647 if (ptNew.x != ptOld.x || ptNew.y != ptOld.y) {
648 sEditStream << ptNew.x - ptOld.x << " " << ptNew.y - ptOld.y
649 << " Td\n";
650 ptOld = ptNew;
651 }
652
653 if (word.nFontIndex != nCurFontIndex) {
654 sEditStream << GetFontSetString(pEdit->GetFontMap(), word.nFontIndex,
655 word.fFontSize);
656 nCurFontIndex = word.nFontIndex;
657 }
658
659 sEditStream << GetWordRenderString(GetPDFWordString(
660 pEdit->GetFontMap(), nCurFontIndex, word.Word, SubWord));
661 }
662 }
663 }
664
665 if (sWords.GetSize() > 0) {
666 sEditStream << GetWordRenderString(sWords.MakeString());
667 sWords.Clear();
668 }
669
670 CFX_ByteTextBuf sAppStream;
671 if (sEditStream.GetSize() > 0) {
672 int32_t nHorzScale = pEdit->GetHorzScale();
673 if (nHorzScale != 100) {
674 sAppStream << nHorzScale << " Tz\n";
675 }
676
Dan Sinclair05df0752017-03-14 14:43:42 -0400677 float fCharSpace = pEdit->GetCharSpace();
dsinclair448c4332016-08-02 12:07:35 -0700678 if (!IsFloatZero(fCharSpace)) {
dsinclaire35af1e2016-07-13 11:26:20 -0700679 sAppStream << fCharSpace << " Tc\n";
680 }
681
682 sAppStream << sEditStream;
683 }
684
685 return sAppStream.MakeString();
686}
687
688// static
689CFX_ByteString CFX_Edit::GetSelectAppearanceStream(
690 CFX_Edit* pEdit,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500691 const CFX_PointF& ptOffset,
dsinclaire35af1e2016-07-13 11:26:20 -0700692 const CPVT_WordRange* pRange) {
Tom Sepez52f69b32017-03-21 13:42:38 -0700693 if (!pRange || pRange->IsEmpty())
dsinclaire35af1e2016-07-13 11:26:20 -0700694 return CFX_ByteString();
695
696 CFX_Edit_Iterator* pIterator = pEdit->GetIterator();
697 pIterator->SetAt(pRange->BeginPos);
698
699 CFX_ByteTextBuf sRet;
700 while (pIterator->NextWord()) {
701 CPVT_WordPlace place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -0700702 if (place > pRange->EndPos)
dsinclaire35af1e2016-07-13 11:26:20 -0700703 break;
704
705 CPVT_Word word;
706 CPVT_Line line;
707 if (pIterator->GetWord(word) && pIterator->GetLine(line)) {
708 sRet << word.ptWord.x + ptOffset.x << " "
709 << line.ptLine.y + line.fLineDescent << " " << word.fWidth << " "
710 << line.fLineAscent - line.fLineDescent << " re\nf\n";
711 }
712 }
713
714 return sRet.MakeString();
715}
716
717// static
dsinclaire35af1e2016-07-13 11:26:20 -0700718void CFX_Edit::DrawEdit(CFX_RenderDevice* pDevice,
719 CFX_Matrix* pUser2Device,
720 CFX_Edit* pEdit,
721 FX_COLORREF crTextFill,
dsinclaire35af1e2016-07-13 11:26:20 -0700722 const CFX_FloatRect& rcClip,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500723 const CFX_PointF& ptOffset,
dsinclaire35af1e2016-07-13 11:26:20 -0700724 const CPVT_WordRange* pRange,
725 CFX_SystemHandler* pSystemHandler,
dsinclair8faac622016-09-15 12:41:50 -0700726 CFFL_FormFiller* pFFLData) {
dsinclaire35af1e2016-07-13 11:26:20 -0700727 const bool bContinuous =
728 pEdit->GetCharArray() == 0 && pEdit->GetCharSpace() <= 0.0f;
729 uint16_t SubWord = pEdit->GetPasswordChar();
Dan Sinclair05df0752017-03-14 14:43:42 -0400730 float fFontSize = pEdit->GetFontSize();
dsinclaire35af1e2016-07-13 11:26:20 -0700731 CPVT_WordRange wrSelect = pEdit->GetSelectWordRange();
732 int32_t nHorzScale = pEdit->GetHorzScale();
733
734 FX_COLORREF crCurFill = crTextFill;
735 FX_COLORREF crOldFill = crCurFill;
736
tsepez4cf55152016-11-02 14:37:54 -0700737 bool bSelect = false;
dsinclaire35af1e2016-07-13 11:26:20 -0700738 const FX_COLORREF crWhite = ArgbEncode(255, 255, 255, 255);
739 const FX_COLORREF crSelBK = ArgbEncode(255, 0, 51, 113);
740
741 CFX_ByteTextBuf sTextBuf;
742 int32_t nFontIndex = -1;
Dan Sinclairf528eee2017-02-14 11:52:07 -0500743 CFX_PointF ptBT;
dsinclaire35af1e2016-07-13 11:26:20 -0700744 pDevice->SaveState();
dsinclaire35af1e2016-07-13 11:26:20 -0700745 if (!rcClip.IsEmpty()) {
746 CFX_FloatRect rcTemp = rcClip;
747 pUser2Device->TransformRect(rcTemp);
748 pDevice->SetClip_Rect(rcTemp.ToFxRect());
749 }
750
751 CFX_Edit_Iterator* pIterator = pEdit->GetIterator();
752 if (IPVT_FontMap* pFontMap = pEdit->GetFontMap()) {
753 if (pRange)
754 pIterator->SetAt(pRange->BeginPos);
755 else
756 pIterator->SetAt(0);
757
758 CPVT_WordPlace oldplace;
759 while (pIterator->NextWord()) {
760 CPVT_WordPlace place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -0700761 if (pRange && place > pRange->EndPos)
dsinclaire35af1e2016-07-13 11:26:20 -0700762 break;
763
Tom Sepez52f69b32017-03-21 13:42:38 -0700764 if (!wrSelect.IsEmpty()) {
765 bSelect = place > wrSelect.BeginPos && place <= wrSelect.EndPos;
dsinclaire35af1e2016-07-13 11:26:20 -0700766 crCurFill = bSelect ? crWhite : crTextFill;
767 }
768 if (pSystemHandler && pSystemHandler->IsSelectionImplemented()) {
769 crCurFill = crTextFill;
770 crOldFill = crCurFill;
771 }
772 CPVT_Word word;
773 if (pIterator->GetWord(word)) {
774 if (bSelect) {
775 CPVT_Line line;
776 pIterator->GetLine(line);
777
778 if (pSystemHandler && pSystemHandler->IsSelectionImplemented()) {
779 CFX_FloatRect rc(word.ptWord.x, line.ptLine.y + line.fLineDescent,
780 word.ptWord.x + word.fWidth,
781 line.ptLine.y + line.fLineAscent);
782 rc.Intersect(rcClip);
783 pSystemHandler->OutputSelectedRect(pFFLData, rc);
784 } else {
785 CFX_PathData pathSelBK;
786 pathSelBK.AppendRect(
787 word.ptWord.x, line.ptLine.y + line.fLineDescent,
788 word.ptWord.x + word.fWidth, line.ptLine.y + line.fLineAscent);
789
790 pDevice->DrawPath(&pathSelBK, pUser2Device, nullptr, crSelBK, 0,
791 FXFILL_WINDING);
792 }
793 }
794
795 if (bContinuous) {
796 if (place.LineCmp(oldplace) != 0 || word.nFontIndex != nFontIndex ||
797 crOldFill != crCurFill) {
798 if (sTextBuf.GetLength() > 0) {
799 DrawTextString(
Dan Sinclairf528eee2017-02-14 11:52:07 -0500800 pDevice, CFX_PointF(ptBT.x + ptOffset.x, ptBT.y + ptOffset.y),
dsinclaire35af1e2016-07-13 11:26:20 -0700801 pFontMap->GetPDFFont(nFontIndex), fFontSize, pUser2Device,
Dan Sinclaira0061af2017-02-23 09:25:17 -0500802 sTextBuf.MakeString(), crOldFill, nHorzScale);
dsinclaire35af1e2016-07-13 11:26:20 -0700803
804 sTextBuf.Clear();
805 }
806 nFontIndex = word.nFontIndex;
807 ptBT = word.ptWord;
808 crOldFill = crCurFill;
809 }
810
811 sTextBuf << GetPDFWordString(pFontMap, word.nFontIndex, word.Word,
812 SubWord)
813 .AsStringC();
814 } else {
815 DrawTextString(
Dan Sinclairf528eee2017-02-14 11:52:07 -0500816 pDevice, CFX_PointF(word.ptWord.x + ptOffset.x,
817 word.ptWord.y + ptOffset.y),
dsinclaire35af1e2016-07-13 11:26:20 -0700818 pFontMap->GetPDFFont(word.nFontIndex), fFontSize, pUser2Device,
819 GetPDFWordString(pFontMap, word.nFontIndex, word.Word, SubWord),
Dan Sinclaira0061af2017-02-23 09:25:17 -0500820 crCurFill, nHorzScale);
dsinclaire35af1e2016-07-13 11:26:20 -0700821 }
822 oldplace = place;
823 }
824 }
825
826 if (sTextBuf.GetLength() > 0) {
Dan Sinclaira0061af2017-02-23 09:25:17 -0500827 DrawTextString(pDevice,
828 CFX_PointF(ptBT.x + ptOffset.x, ptBT.y + ptOffset.y),
829 pFontMap->GetPDFFont(nFontIndex), fFontSize, pUser2Device,
830 sTextBuf.MakeString(), crOldFill, nHorzScale);
dsinclaire35af1e2016-07-13 11:26:20 -0700831 }
832 }
833
834 pDevice->RestoreState(false);
835}
836
dsinclaire35af1e2016-07-13 11:26:20 -0700837CFX_Edit::CFX_Edit()
838 : m_pVT(new CPDF_VariableText),
thestig821d59e2016-05-11 12:59:22 -0700839 m_pNotify(nullptr),
840 m_pOprNotify(nullptr),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700841 m_wpCaret(-1, -1, -1),
842 m_wpOldCaret(-1, -1, -1),
843 m_SelState(),
tsepez4cf55152016-11-02 14:37:54 -0700844 m_bEnableScroll(false),
dsinclair8f4bf9a2016-05-04 13:51:51 -0700845 m_Undo(kEditUndoMaxItems),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700846 m_nAlignment(0),
tsepez4cf55152016-11-02 14:37:54 -0700847 m_bNotifyFlag(false),
848 m_bEnableOverflow(false),
849 m_bEnableRefresh(true),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700850 m_rcOldContent(0.0f, 0.0f, 0.0f, 0.0f),
tsepez4cf55152016-11-02 14:37:54 -0700851 m_bEnableUndo(true),
852 m_bOprNotify(false),
dsinclaire35af1e2016-07-13 11:26:20 -0700853 m_pGroupUndoItem(nullptr) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700854
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700855CFX_Edit::~CFX_Edit() {
Lei Zhang412e9082015-12-14 18:34:00 -0800856 ASSERT(!m_pGroupUndoItem);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700857}
858
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700859void CFX_Edit::Initialize() {
860 m_pVT->Initialize();
861 SetCaret(m_pVT->GetBeginWordPlace());
862 SetCaretOrigin();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700863}
864
dsinclairc7a73492016-04-05 12:01:42 -0700865void CFX_Edit::SetFontMap(IPVT_FontMap* pFontMap) {
tsepez36eb4bd2016-10-03 15:24:27 -0700866 m_pVTProvider = pdfium::MakeUnique<CFX_Edit_Provider>(pFontMap);
thestig821d59e2016-05-11 12:59:22 -0700867 m_pVT->SetProvider(m_pVTProvider.get());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700868}
869
dsinclaire35af1e2016-07-13 11:26:20 -0700870void CFX_Edit::SetNotify(CPWL_EditCtrl* pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700871 m_pNotify = pNotify;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700872}
873
dsinclaire35af1e2016-07-13 11:26:20 -0700874void CFX_Edit::SetOprNotify(CPWL_Edit* pOprNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700875 m_pOprNotify = pOprNotify;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700876}
877
dsinclaire35af1e2016-07-13 11:26:20 -0700878CFX_Edit_Iterator* CFX_Edit::GetIterator() {
tsepez36eb4bd2016-10-03 15:24:27 -0700879 if (!m_pIterator) {
880 m_pIterator =
881 pdfium::MakeUnique<CFX_Edit_Iterator>(this, m_pVT->GetIterator());
882 }
thestig821d59e2016-05-11 12:59:22 -0700883 return m_pIterator.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700884}
885
dsinclairc7a73492016-04-05 12:01:42 -0700886IPVT_FontMap* CFX_Edit::GetFontMap() {
thestig821d59e2016-05-11 12:59:22 -0700887 return m_pVTProvider ? m_pVTProvider->GetFontMap() : nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700888}
889
dsinclairefd5a992016-07-18 10:04:07 -0700890void CFX_Edit::SetPlateRect(const CFX_FloatRect& rect) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700891 m_pVT->SetPlateRect(rect);
Dan Sinclairf528eee2017-02-14 11:52:07 -0500892 m_ptScrollPos = CFX_PointF(rect.left, rect.top);
dsinclairefd5a992016-07-18 10:04:07 -0700893 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700894}
895
tsepez4cf55152016-11-02 14:37:54 -0700896void CFX_Edit::SetAlignmentH(int32_t nFormat, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700897 m_pVT->SetAlignment(nFormat);
898 if (bPaint)
899 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700900}
901
tsepez4cf55152016-11-02 14:37:54 -0700902void CFX_Edit::SetAlignmentV(int32_t nFormat, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700903 m_nAlignment = nFormat;
904 if (bPaint)
905 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700906}
907
tsepez4cf55152016-11-02 14:37:54 -0700908void CFX_Edit::SetPasswordChar(uint16_t wSubWord, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700909 m_pVT->SetPasswordChar(wSubWord);
910 if (bPaint)
911 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700912}
913
dsinclairefd5a992016-07-18 10:04:07 -0700914void CFX_Edit::SetLimitChar(int32_t nLimitChar) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700915 m_pVT->SetLimitChar(nLimitChar);
dsinclairefd5a992016-07-18 10:04:07 -0700916 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700917}
918
dsinclairefd5a992016-07-18 10:04:07 -0700919void CFX_Edit::SetCharArray(int32_t nCharArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700920 m_pVT->SetCharArray(nCharArray);
dsinclairefd5a992016-07-18 10:04:07 -0700921 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700922}
923
Dan Sinclair05df0752017-03-14 14:43:42 -0400924void CFX_Edit::SetCharSpace(float fCharSpace) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700925 m_pVT->SetCharSpace(fCharSpace);
dsinclairefd5a992016-07-18 10:04:07 -0700926 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700927}
928
tsepez4cf55152016-11-02 14:37:54 -0700929void CFX_Edit::SetMultiLine(bool bMultiLine, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700930 m_pVT->SetMultiLine(bMultiLine);
931 if (bPaint)
932 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700933}
934
tsepez4cf55152016-11-02 14:37:54 -0700935void CFX_Edit::SetAutoReturn(bool bAuto, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700936 m_pVT->SetAutoReturn(bAuto);
937 if (bPaint)
938 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700939}
940
tsepez4cf55152016-11-02 14:37:54 -0700941void CFX_Edit::SetAutoFontSize(bool bAuto, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700942 m_pVT->SetAutoFontSize(bAuto);
943 if (bPaint)
944 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700945}
946
Dan Sinclair05df0752017-03-14 14:43:42 -0400947void CFX_Edit::SetFontSize(float fFontSize) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700948 m_pVT->SetFontSize(fFontSize);
dsinclairefd5a992016-07-18 10:04:07 -0700949 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700950}
951
tsepez4cf55152016-11-02 14:37:54 -0700952void CFX_Edit::SetAutoScroll(bool bAuto, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700953 m_bEnableScroll = bAuto;
954 if (bPaint)
955 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700956}
957
tsepez4cf55152016-11-02 14:37:54 -0700958void CFX_Edit::SetTextOverflow(bool bAllowed, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700959 m_bEnableOverflow = bAllowed;
960 if (bPaint)
961 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700962}
963
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700964void CFX_Edit::SetSel(int32_t nStartChar, int32_t nEndChar) {
965 if (m_pVT->IsValid()) {
966 if (nStartChar == 0 && nEndChar < 0) {
967 SelectAll();
968 } else if (nStartChar < 0) {
969 SelectNone();
970 } else {
971 if (nStartChar < nEndChar) {
972 SetSel(m_pVT->WordIndexToWordPlace(nStartChar),
973 m_pVT->WordIndexToWordPlace(nEndChar));
974 } else {
975 SetSel(m_pVT->WordIndexToWordPlace(nEndChar),
976 m_pVT->WordIndexToWordPlace(nStartChar));
977 }
978 }
979 }
980}
981
982void CFX_Edit::SetSel(const CPVT_WordPlace& begin, const CPVT_WordPlace& end) {
Tom Sepez52f69b32017-03-21 13:42:38 -0700983 if (!m_pVT->IsValid())
984 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700985
Tom Sepez52f69b32017-03-21 13:42:38 -0700986 SelectNone();
987 m_SelState.Set(begin, end);
988 SetCaret(m_SelState.EndPos);
989 ScrollToCaret();
990 if (!m_SelState.IsEmpty())
991 Refresh();
992 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700993}
994
995void CFX_Edit::GetSel(int32_t& nStartChar, int32_t& nEndChar) const {
996 nStartChar = -1;
997 nEndChar = -1;
Tom Sepez52f69b32017-03-21 13:42:38 -0700998 if (!m_pVT->IsValid())
999 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001000
Tom Sepez52f69b32017-03-21 13:42:38 -07001001 if (m_SelState.IsEmpty()) {
1002 nStartChar = m_pVT->WordPlaceToWordIndex(m_wpCaret);
1003 nEndChar = m_pVT->WordPlaceToWordIndex(m_wpCaret);
1004 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001005 }
Tom Sepez52f69b32017-03-21 13:42:38 -07001006 if (m_SelState.BeginPos < m_SelState.EndPos) {
1007 nStartChar = m_pVT->WordPlaceToWordIndex(m_SelState.BeginPos);
1008 nEndChar = m_pVT->WordPlaceToWordIndex(m_SelState.EndPos);
1009 return;
1010 }
1011 nStartChar = m_pVT->WordPlaceToWordIndex(m_SelState.EndPos);
1012 nEndChar = m_pVT->WordPlaceToWordIndex(m_SelState.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001013}
1014
1015int32_t CFX_Edit::GetCaret() const {
1016 if (m_pVT->IsValid())
1017 return m_pVT->WordPlaceToWordIndex(m_wpCaret);
1018
1019 return -1;
1020}
1021
1022CPVT_WordPlace CFX_Edit::GetCaretWordPlace() const {
1023 return m_wpCaret;
1024}
1025
1026CFX_WideString CFX_Edit::GetText() const {
1027 CFX_WideString swRet;
thestig821d59e2016-05-11 12:59:22 -07001028 if (!m_pVT->IsValid())
1029 return swRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001030
thestig821d59e2016-05-11 12:59:22 -07001031 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1032 pIterator->SetAt(0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001033
thestig821d59e2016-05-11 12:59:22 -07001034 CPVT_Word wordinfo;
1035 CPVT_WordPlace oldplace = pIterator->GetAt();
1036 while (pIterator->NextWord()) {
1037 CPVT_WordPlace place = pIterator->GetAt();
thestig821d59e2016-05-11 12:59:22 -07001038 if (pIterator->GetWord(wordinfo))
1039 swRet += wordinfo.Word;
Tom Sepez52f69b32017-03-21 13:42:38 -07001040 if (oldplace.nSecIndex != place.nSecIndex)
thestig821d59e2016-05-11 12:59:22 -07001041 swRet += L"\r\n";
thestig821d59e2016-05-11 12:59:22 -07001042 oldplace = place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001043 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001044 return swRet;
1045}
1046
1047CFX_WideString CFX_Edit::GetRangeText(const CPVT_WordRange& range) const {
1048 CFX_WideString swRet;
thestig821d59e2016-05-11 12:59:22 -07001049 if (!m_pVT->IsValid())
1050 return swRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001051
thestig821d59e2016-05-11 12:59:22 -07001052 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1053 CPVT_WordRange wrTemp = range;
1054 m_pVT->UpdateWordPlace(wrTemp.BeginPos);
1055 m_pVT->UpdateWordPlace(wrTemp.EndPos);
1056 pIterator->SetAt(wrTemp.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001057
thestig821d59e2016-05-11 12:59:22 -07001058 CPVT_Word wordinfo;
1059 CPVT_WordPlace oldplace = wrTemp.BeginPos;
1060 while (pIterator->NextWord()) {
1061 CPVT_WordPlace place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -07001062 if (place > wrTemp.EndPos)
thestig821d59e2016-05-11 12:59:22 -07001063 break;
thestig821d59e2016-05-11 12:59:22 -07001064 if (pIterator->GetWord(wordinfo))
1065 swRet += wordinfo.Word;
Tom Sepez52f69b32017-03-21 13:42:38 -07001066 if (oldplace.nSecIndex != place.nSecIndex)
thestig821d59e2016-05-11 12:59:22 -07001067 swRet += L"\r\n";
thestig821d59e2016-05-11 12:59:22 -07001068 oldplace = place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001069 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001070 return swRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001071}
1072
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001073CFX_WideString CFX_Edit::GetSelText() const {
1074 return GetRangeText(m_SelState.ConvertToWordRange());
1075}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001076
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001077int32_t CFX_Edit::GetTotalLines() const {
thestig821d59e2016-05-11 12:59:22 -07001078 int32_t nLines = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001079
thestig821d59e2016-05-11 12:59:22 -07001080 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1081 pIterator->SetAt(0);
1082 while (pIterator->NextLine())
1083 ++nLines;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001084
thestig821d59e2016-05-11 12:59:22 -07001085 return nLines;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001086}
1087
1088CPVT_WordRange CFX_Edit::GetSelectWordRange() const {
1089 return m_SelState.ConvertToWordRange();
1090}
1091
tsepeza31da742016-09-08 11:28:14 -07001092void CFX_Edit::SetText(const CFX_WideString& sText) {
dsinclairefd5a992016-07-18 10:04:07 -07001093 Empty();
npmea3c3be2016-09-19 07:24:33 -07001094 DoInsertText(CPVT_WordPlace(0, 0, -1), sText, FXFONT_DEFAULT_CHARSET);
dsinclairefd5a992016-07-18 10:04:07 -07001095 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001096}
1097
tsepez4cf55152016-11-02 14:37:54 -07001098bool CFX_Edit::InsertWord(uint16_t word, int32_t charset) {
1099 return InsertWord(word, charset, nullptr, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001100}
1101
tsepez4cf55152016-11-02 14:37:54 -07001102bool CFX_Edit::InsertReturn() {
1103 return InsertReturn(nullptr, nullptr, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001104}
1105
tsepez4cf55152016-11-02 14:37:54 -07001106bool CFX_Edit::Backspace() {
1107 return Backspace(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001108}
1109
tsepez4cf55152016-11-02 14:37:54 -07001110bool CFX_Edit::Delete() {
1111 return Delete(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001112}
1113
tsepez4cf55152016-11-02 14:37:54 -07001114bool CFX_Edit::Clear() {
1115 return Clear(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001116}
1117
tsepez4cf55152016-11-02 14:37:54 -07001118bool CFX_Edit::InsertText(const CFX_WideString& sText, int32_t charset) {
1119 return InsertText(sText, charset, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001120}
1121
Dan Sinclair05df0752017-03-14 14:43:42 -04001122float CFX_Edit::GetFontSize() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001123 return m_pVT->GetFontSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001124}
1125
Tom Sepez62a70f92016-03-21 15:00:20 -07001126uint16_t CFX_Edit::GetPasswordChar() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001127 return m_pVT->GetPasswordChar();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001128}
1129
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001130int32_t CFX_Edit::GetCharArray() const {
1131 return m_pVT->GetCharArray();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001132}
1133
Tom Sepez281a9ea2016-02-26 14:24:28 -08001134CFX_FloatRect CFX_Edit::GetContentRect() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001135 return VTToEdit(m_pVT->GetContentRect());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001136}
1137
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001138int32_t CFX_Edit::GetHorzScale() const {
1139 return m_pVT->GetHorzScale();
1140}
1141
Dan Sinclair05df0752017-03-14 14:43:42 -04001142float CFX_Edit::GetCharSpace() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001143 return m_pVT->GetCharSpace();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001144}
1145
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001146CPVT_WordRange CFX_Edit::GetWholeWordRange() const {
1147 if (m_pVT->IsValid())
1148 return CPVT_WordRange(m_pVT->GetBeginWordPlace(), m_pVT->GetEndWordPlace());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001149
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001150 return CPVT_WordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001151}
1152
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001153CPVT_WordRange CFX_Edit::GetVisibleWordRange() const {
1154 if (m_bEnableOverflow)
1155 return GetWholeWordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001156
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001157 if (m_pVT->IsValid()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001158 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001159
Dan Sinclairf528eee2017-02-14 11:52:07 -05001160 CPVT_WordPlace place1 =
1161 m_pVT->SearchWordPlace(EditToVT(CFX_PointF(rcPlate.left, rcPlate.top)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001162 CPVT_WordPlace place2 = m_pVT->SearchWordPlace(
Dan Sinclairf528eee2017-02-14 11:52:07 -05001163 EditToVT(CFX_PointF(rcPlate.right, rcPlate.bottom)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001164
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001165 return CPVT_WordRange(place1, place2);
1166 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001167
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001168 return CPVT_WordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001169}
1170
Dan Sinclairf528eee2017-02-14 11:52:07 -05001171CPVT_WordPlace CFX_Edit::SearchWordPlace(const CFX_PointF& point) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001172 if (m_pVT->IsValid()) {
1173 return m_pVT->SearchWordPlace(EditToVT(point));
1174 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001175
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001176 return CPVT_WordPlace();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001177}
1178
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001179void CFX_Edit::Paint() {
1180 if (m_pVT->IsValid()) {
1181 RearrangeAll();
1182 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001183 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001184 SetCaretOrigin();
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001185 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001186 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001187}
1188
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001189void CFX_Edit::RearrangeAll() {
1190 if (m_pVT->IsValid()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001191 m_pVT->UpdateWordPlace(m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001192 m_pVT->RearrangeAll();
1193 m_pVT->UpdateWordPlace(m_wpCaret);
1194 SetScrollInfo();
1195 SetContentChanged();
1196 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001197}
1198
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001199void CFX_Edit::RearrangePart(const CPVT_WordRange& range) {
1200 if (m_pVT->IsValid()) {
1201 m_pVT->UpdateWordPlace(m_wpCaret);
1202 m_pVT->RearrangePart(range);
1203 m_pVT->UpdateWordPlace(m_wpCaret);
1204 SetScrollInfo();
1205 SetContentChanged();
1206 }
1207}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001208
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001209void CFX_Edit::SetContentChanged() {
dsinclaira2919b32016-07-13 10:55:48 -07001210 if (m_pNotify) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001211 CFX_FloatRect rcContent = m_pVT->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001212 if (rcContent.Width() != m_rcOldContent.Width() ||
1213 rcContent.Height() != m_rcOldContent.Height()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001214 m_rcOldContent = rcContent;
1215 }
1216 }
1217}
1218
1219void CFX_Edit::SelectAll() {
Tom Sepez52f69b32017-03-21 13:42:38 -07001220 if (!m_pVT->IsValid())
1221 return;
1222 m_SelState = CFX_Edit_Select(GetWholeWordRange());
1223 SetCaret(m_SelState.EndPos);
1224 ScrollToCaret();
1225 Refresh();
1226 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001227}
1228
1229void CFX_Edit::SelectNone() {
Tom Sepez52f69b32017-03-21 13:42:38 -07001230 if (!m_pVT->IsValid() || m_SelState.IsEmpty())
1231 return;
1232
1233 m_SelState.Reset();
1234 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001235}
1236
tsepez4cf55152016-11-02 14:37:54 -07001237bool CFX_Edit::IsSelected() const {
Tom Sepez52f69b32017-03-21 13:42:38 -07001238 return !m_SelState.IsEmpty();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001239}
1240
Dan Sinclairf528eee2017-02-14 11:52:07 -05001241CFX_PointF CFX_Edit::VTToEdit(const CFX_PointF& point) const {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001242 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1243 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001244
Dan Sinclair05df0752017-03-14 14:43:42 -04001245 float fPadding = 0.0f;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001246
1247 switch (m_nAlignment) {
1248 case 0:
1249 fPadding = 0.0f;
1250 break;
1251 case 1:
1252 fPadding = (rcPlate.Height() - rcContent.Height()) * 0.5f;
1253 break;
1254 case 2:
1255 fPadding = rcPlate.Height() - rcContent.Height();
1256 break;
1257 }
1258
Dan Sinclairf528eee2017-02-14 11:52:07 -05001259 return CFX_PointF(point.x - (m_ptScrollPos.x - rcPlate.left),
1260 point.y - (m_ptScrollPos.y + fPadding - rcPlate.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001261}
1262
Dan Sinclairf528eee2017-02-14 11:52:07 -05001263CFX_PointF CFX_Edit::EditToVT(const CFX_PointF& point) const {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001264 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1265 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001266
Dan Sinclair05df0752017-03-14 14:43:42 -04001267 float fPadding = 0.0f;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001268
1269 switch (m_nAlignment) {
1270 case 0:
1271 fPadding = 0.0f;
1272 break;
1273 case 1:
1274 fPadding = (rcPlate.Height() - rcContent.Height()) * 0.5f;
1275 break;
1276 case 2:
1277 fPadding = rcPlate.Height() - rcContent.Height();
1278 break;
1279 }
1280
Dan Sinclairf528eee2017-02-14 11:52:07 -05001281 return CFX_PointF(point.x + (m_ptScrollPos.x - rcPlate.left),
1282 point.y + (m_ptScrollPos.y + fPadding - rcPlate.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001283}
1284
Tom Sepez281a9ea2016-02-26 14:24:28 -08001285CFX_FloatRect CFX_Edit::VTToEdit(const CFX_FloatRect& rect) const {
Dan Sinclairf528eee2017-02-14 11:52:07 -05001286 CFX_PointF ptLeftBottom = VTToEdit(CFX_PointF(rect.left, rect.bottom));
1287 CFX_PointF ptRightTop = VTToEdit(CFX_PointF(rect.right, rect.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001288
Tom Sepez281a9ea2016-02-26 14:24:28 -08001289 return CFX_FloatRect(ptLeftBottom.x, ptLeftBottom.y, ptRightTop.x,
1290 ptRightTop.y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001291}
1292
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001293void CFX_Edit::SetScrollInfo() {
dsinclaira2919b32016-07-13 10:55:48 -07001294 if (m_pNotify) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001295 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
1296 CFX_FloatRect rcContent = m_pVT->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001297
1298 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001299 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001300 m_bNotifyFlag = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001301 m_pNotify->IOnSetScrollInfoY(rcPlate.bottom, rcPlate.top,
1302 rcContent.bottom, rcContent.top,
1303 rcPlate.Height() / 3, rcPlate.Height());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001304 }
1305 }
1306}
1307
Dan Sinclair05df0752017-03-14 14:43:42 -04001308void CFX_Edit::SetScrollPosX(float fx) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001309 if (!m_bEnableScroll)
1310 return;
1311
1312 if (m_pVT->IsValid()) {
dsinclair448c4332016-08-02 12:07:35 -07001313 if (!IsFloatEqual(m_ptScrollPos.x, fx)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001314 m_ptScrollPos.x = fx;
dsinclairefd5a992016-07-18 10:04:07 -07001315 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001316 }
1317 }
1318}
1319
Dan Sinclair05df0752017-03-14 14:43:42 -04001320void CFX_Edit::SetScrollPosY(float fy) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001321 if (!m_bEnableScroll)
1322 return;
1323
1324 if (m_pVT->IsValid()) {
dsinclair448c4332016-08-02 12:07:35 -07001325 if (!IsFloatEqual(m_ptScrollPos.y, fy)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001326 m_ptScrollPos.y = fy;
dsinclairefd5a992016-07-18 10:04:07 -07001327 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001328
dsinclaira2919b32016-07-13 10:55:48 -07001329 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001330 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001331 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001332 m_bNotifyFlag = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001333 m_pNotify->IOnSetScrollPosY(fy);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001334 }
1335 }
1336 }
1337 }
1338}
1339
Dan Sinclairf528eee2017-02-14 11:52:07 -05001340void CFX_Edit::SetScrollPos(const CFX_PointF& point) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001341 SetScrollPosX(point.x);
1342 SetScrollPosY(point.y);
1343 SetScrollLimit();
1344 SetCaretInfo();
1345}
1346
Dan Sinclairf528eee2017-02-14 11:52:07 -05001347CFX_PointF CFX_Edit::GetScrollPos() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001348 return m_ptScrollPos;
1349}
1350
1351void CFX_Edit::SetScrollLimit() {
1352 if (m_pVT->IsValid()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001353 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1354 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001355
1356 if (rcPlate.Width() > rcContent.Width()) {
1357 SetScrollPosX(rcPlate.left);
1358 } else {
dsinclair448c4332016-08-02 12:07:35 -07001359 if (IsFloatSmaller(m_ptScrollPos.x, rcContent.left)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001360 SetScrollPosX(rcContent.left);
dsinclair448c4332016-08-02 12:07:35 -07001361 } else if (IsFloatBigger(m_ptScrollPos.x,
1362 rcContent.right - rcPlate.Width())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001363 SetScrollPosX(rcContent.right - rcPlate.Width());
1364 }
1365 }
1366
1367 if (rcPlate.Height() > rcContent.Height()) {
1368 SetScrollPosY(rcPlate.top);
1369 } else {
dsinclair448c4332016-08-02 12:07:35 -07001370 if (IsFloatSmaller(m_ptScrollPos.y,
1371 rcContent.bottom + rcPlate.Height())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001372 SetScrollPosY(rcContent.bottom + rcPlate.Height());
dsinclair448c4332016-08-02 12:07:35 -07001373 } else if (IsFloatBigger(m_ptScrollPos.y, rcContent.top)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001374 SetScrollPosY(rcContent.top);
1375 }
1376 }
1377 }
1378}
1379
1380void CFX_Edit::ScrollToCaret() {
1381 SetScrollLimit();
1382
thestig821d59e2016-05-11 12:59:22 -07001383 if (!m_pVT->IsValid())
1384 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001385
thestig821d59e2016-05-11 12:59:22 -07001386 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1387 pIterator->SetAt(m_wpCaret);
1388
Dan Sinclairf528eee2017-02-14 11:52:07 -05001389 CFX_PointF ptHead;
1390 CFX_PointF ptFoot;
thestig821d59e2016-05-11 12:59:22 -07001391 CPVT_Word word;
1392 CPVT_Line line;
1393 if (pIterator->GetWord(word)) {
1394 ptHead.x = word.ptWord.x + word.fWidth;
1395 ptHead.y = word.ptWord.y + word.fAscent;
1396 ptFoot.x = word.ptWord.x + word.fWidth;
1397 ptFoot.y = word.ptWord.y + word.fDescent;
1398 } else if (pIterator->GetLine(line)) {
1399 ptHead.x = line.ptLine.x;
1400 ptHead.y = line.ptLine.y + line.fLineAscent;
1401 ptFoot.x = line.ptLine.x;
1402 ptFoot.y = line.ptLine.y + line.fLineDescent;
1403 }
1404
Dan Sinclairf528eee2017-02-14 11:52:07 -05001405 CFX_PointF ptHeadEdit = VTToEdit(ptHead);
1406 CFX_PointF ptFootEdit = VTToEdit(ptFoot);
thestig821d59e2016-05-11 12:59:22 -07001407 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
dsinclair448c4332016-08-02 12:07:35 -07001408 if (!IsFloatEqual(rcPlate.left, rcPlate.right)) {
1409 if (IsFloatSmaller(ptHeadEdit.x, rcPlate.left) ||
1410 IsFloatEqual(ptHeadEdit.x, rcPlate.left)) {
thestig821d59e2016-05-11 12:59:22 -07001411 SetScrollPosX(ptHead.x);
dsinclair448c4332016-08-02 12:07:35 -07001412 } else if (IsFloatBigger(ptHeadEdit.x, rcPlate.right)) {
thestig821d59e2016-05-11 12:59:22 -07001413 SetScrollPosX(ptHead.x - rcPlate.Width());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001414 }
thestig821d59e2016-05-11 12:59:22 -07001415 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001416
dsinclair448c4332016-08-02 12:07:35 -07001417 if (!IsFloatEqual(rcPlate.top, rcPlate.bottom)) {
1418 if (IsFloatSmaller(ptFootEdit.y, rcPlate.bottom) ||
1419 IsFloatEqual(ptFootEdit.y, rcPlate.bottom)) {
1420 if (IsFloatSmaller(ptHeadEdit.y, rcPlate.top)) {
thestig821d59e2016-05-11 12:59:22 -07001421 SetScrollPosY(ptFoot.y + rcPlate.Height());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001422 }
dsinclair448c4332016-08-02 12:07:35 -07001423 } else if (IsFloatBigger(ptHeadEdit.y, rcPlate.top)) {
1424 if (IsFloatBigger(ptFootEdit.y, rcPlate.bottom)) {
thestig821d59e2016-05-11 12:59:22 -07001425 SetScrollPosY(ptHead.y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001426 }
1427 }
1428 }
1429}
1430
dsinclairefd5a992016-07-18 10:04:07 -07001431void CFX_Edit::Refresh() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001432 if (m_bEnableRefresh && m_pVT->IsValid()) {
1433 m_Refresh.BeginRefresh();
1434 RefreshPushLineRects(GetVisibleWordRange());
1435
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001436 m_Refresh.NoAnalyse();
1437 m_ptRefreshScrollPos = m_ptScrollPos;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001438
dsinclaira2919b32016-07-13 10:55:48 -07001439 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001440 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001441 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001442 m_bNotifyFlag = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001443 if (const CFX_Edit_RectArray* pRects = m_Refresh.GetRefreshRects()) {
1444 for (int32_t i = 0, sz = pRects->GetSize(); i < sz; i++)
1445 m_pNotify->IOnInvalidateRect(pRects->GetAt(i));
1446 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001447 }
1448 }
1449
1450 m_Refresh.EndRefresh();
1451 }
1452}
1453
1454void CFX_Edit::RefreshPushLineRects(const CPVT_WordRange& wr) {
thestig821d59e2016-05-11 12:59:22 -07001455 if (!m_pVT->IsValid())
1456 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001457
thestig821d59e2016-05-11 12:59:22 -07001458 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1459 CPVT_WordPlace wpBegin = wr.BeginPos;
1460 m_pVT->UpdateWordPlace(wpBegin);
1461 CPVT_WordPlace wpEnd = wr.EndPos;
1462 m_pVT->UpdateWordPlace(wpEnd);
1463 pIterator->SetAt(wpBegin);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001464
thestig821d59e2016-05-11 12:59:22 -07001465 CPVT_Line lineinfo;
1466 do {
1467 if (!pIterator->GetLine(lineinfo))
1468 break;
1469 if (lineinfo.lineplace.LineCmp(wpEnd) > 0)
1470 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001471
thestig821d59e2016-05-11 12:59:22 -07001472 CFX_FloatRect rcLine(lineinfo.ptLine.x,
1473 lineinfo.ptLine.y + lineinfo.fLineDescent,
1474 lineinfo.ptLine.x + lineinfo.fLineWidth,
1475 lineinfo.ptLine.y + lineinfo.fLineAscent);
1476
1477 m_Refresh.Push(CPVT_WordRange(lineinfo.lineplace, lineinfo.lineEnd),
1478 VTToEdit(rcLine));
1479 } while (pIterator->NextLine());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001480}
1481
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001482void CFX_Edit::RefreshWordRange(const CPVT_WordRange& wr) {
thestig821d59e2016-05-11 12:59:22 -07001483 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1484 CPVT_WordRange wrTemp = wr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001485
thestig821d59e2016-05-11 12:59:22 -07001486 m_pVT->UpdateWordPlace(wrTemp.BeginPos);
1487 m_pVT->UpdateWordPlace(wrTemp.EndPos);
1488 pIterator->SetAt(wrTemp.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001489
thestig821d59e2016-05-11 12:59:22 -07001490 CPVT_Word wordinfo;
1491 CPVT_Line lineinfo;
1492 CPVT_WordPlace place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001493
thestig821d59e2016-05-11 12:59:22 -07001494 while (pIterator->NextWord()) {
1495 place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -07001496 if (place > wrTemp.EndPos)
thestig821d59e2016-05-11 12:59:22 -07001497 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001498
thestig821d59e2016-05-11 12:59:22 -07001499 pIterator->GetWord(wordinfo);
1500 pIterator->GetLine(lineinfo);
thestig821d59e2016-05-11 12:59:22 -07001501 if (place.LineCmp(wrTemp.BeginPos) == 0 ||
1502 place.LineCmp(wrTemp.EndPos) == 0) {
1503 CFX_FloatRect rcWord(wordinfo.ptWord.x,
1504 lineinfo.ptLine.y + lineinfo.fLineDescent,
1505 wordinfo.ptWord.x + wordinfo.fWidth,
1506 lineinfo.ptLine.y + lineinfo.fLineAscent);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001507
dsinclaira2919b32016-07-13 10:55:48 -07001508 if (m_pNotify) {
thestig821d59e2016-05-11 12:59:22 -07001509 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001510 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001511 m_bNotifyFlag = true;
thestig821d59e2016-05-11 12:59:22 -07001512 CFX_FloatRect rcRefresh = VTToEdit(rcWord);
1513 m_pNotify->IOnInvalidateRect(&rcRefresh);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001514 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001515 }
thestig821d59e2016-05-11 12:59:22 -07001516 } else {
1517 CFX_FloatRect rcLine(lineinfo.ptLine.x,
1518 lineinfo.ptLine.y + lineinfo.fLineDescent,
1519 lineinfo.ptLine.x + lineinfo.fLineWidth,
1520 lineinfo.ptLine.y + lineinfo.fLineAscent);
1521
dsinclaira2919b32016-07-13 10:55:48 -07001522 if (m_pNotify) {
thestig821d59e2016-05-11 12:59:22 -07001523 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001524 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001525 m_bNotifyFlag = true;
thestig821d59e2016-05-11 12:59:22 -07001526 CFX_FloatRect rcRefresh = VTToEdit(rcLine);
1527 m_pNotify->IOnInvalidateRect(&rcRefresh);
thestig821d59e2016-05-11 12:59:22 -07001528 }
1529 }
1530
1531 pIterator->NextLine();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001532 }
1533 }
1534}
1535
1536void CFX_Edit::SetCaret(const CPVT_WordPlace& place) {
1537 m_wpOldCaret = m_wpCaret;
1538 m_wpCaret = place;
1539}
1540
1541void CFX_Edit::SetCaretInfo() {
dsinclaira2919b32016-07-13 10:55:48 -07001542 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001543 if (!m_bNotifyFlag) {
thestig821d59e2016-05-11 12:59:22 -07001544 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1545 pIterator->SetAt(m_wpCaret);
tsepez63f545c2016-09-13 16:08:49 -07001546
Dan Sinclairf528eee2017-02-14 11:52:07 -05001547 CFX_PointF ptHead;
1548 CFX_PointF ptFoot;
thestig821d59e2016-05-11 12:59:22 -07001549 CPVT_Word word;
1550 CPVT_Line line;
1551 if (pIterator->GetWord(word)) {
1552 ptHead.x = word.ptWord.x + word.fWidth;
1553 ptHead.y = word.ptWord.y + word.fAscent;
1554 ptFoot.x = word.ptWord.x + word.fWidth;
1555 ptFoot.y = word.ptWord.y + word.fDescent;
1556 } else if (pIterator->GetLine(line)) {
1557 ptHead.x = line.ptLine.x;
1558 ptHead.y = line.ptLine.y + line.fLineAscent;
1559 ptFoot.x = line.ptLine.x;
1560 ptFoot.y = line.ptLine.y + line.fLineDescent;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001561 }
1562
Lei Zhanga8c2b912017-03-22 17:41:02 -07001563 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001564 m_bNotifyFlag = true;
Tom Sepez52f69b32017-03-21 13:42:38 -07001565 m_pNotify->IOnSetCaret(m_SelState.IsEmpty(), VTToEdit(ptHead),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001566 VTToEdit(ptFoot), m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001567 }
1568 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001569}
1570
1571void CFX_Edit::SetCaret(int32_t nPos) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001572 if (!m_pVT->IsValid())
1573 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001574
Tom Sepez52f69b32017-03-21 13:42:38 -07001575 SelectNone();
1576 SetCaret(m_pVT->WordIndexToWordPlace(nPos));
1577 m_SelState.Set(m_wpCaret, m_wpCaret);
1578 ScrollToCaret();
1579 SetCaretOrigin();
1580 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001581}
1582
Dan Sinclairf528eee2017-02-14 11:52:07 -05001583void CFX_Edit::OnMouseDown(const CFX_PointF& point, bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001584 if (!m_pVT->IsValid())
1585 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001586
Tom Sepez52f69b32017-03-21 13:42:38 -07001587 SelectNone();
1588 SetCaret(m_pVT->SearchWordPlace(EditToVT(point)));
1589 m_SelState.Set(m_wpCaret, m_wpCaret);
1590 ScrollToCaret();
1591 SetCaretOrigin();
1592 SetCaretInfo();
1593}
1594
1595void CFX_Edit::OnMouseMove(const CFX_PointF& point, bool bShift, bool bCtrl) {
1596 if (!m_pVT->IsValid())
1597 return;
1598
1599 SetCaret(m_pVT->SearchWordPlace(EditToVT(point)));
1600 if (m_wpCaret == m_wpOldCaret)
1601 return;
1602
1603 m_SelState.SetEndPos(m_wpCaret);
1604 ScrollToCaret();
1605 Refresh();
1606 SetCaretOrigin();
1607 SetCaretInfo();
1608}
1609
1610void CFX_Edit::OnVK_UP(bool bShift, bool bCtrl) {
1611 if (!m_pVT->IsValid())
1612 return;
1613
1614 SetCaret(m_pVT->GetUpWordPlace(m_wpCaret, m_ptCaret));
1615 if (bShift) {
1616 if (m_SelState.IsEmpty())
1617 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1618 else
1619 m_SelState.SetEndPos(m_wpCaret);
1620
1621 if (m_wpOldCaret != m_wpCaret) {
1622 ScrollToCaret();
1623 Refresh();
1624 SetCaretInfo();
1625 }
1626 } else {
1627 SelectNone();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001628 ScrollToCaret();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001629 SetCaretInfo();
1630 }
1631}
1632
Tom Sepez52f69b32017-03-21 13:42:38 -07001633void CFX_Edit::OnVK_DOWN(bool bShift, bool bCtrl) {
1634 if (!m_pVT->IsValid())
1635 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001636
Tom Sepez52f69b32017-03-21 13:42:38 -07001637 SetCaret(m_pVT->GetDownWordPlace(m_wpCaret, m_ptCaret));
1638 if (bShift) {
1639 if (m_SelState.IsEmpty())
1640 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1641 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001642 m_SelState.SetEndPos(m_wpCaret);
1643
Tom Sepez52f69b32017-03-21 13:42:38 -07001644 if (m_wpOldCaret != m_wpCaret) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001645 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001646 Refresh();
Tom Sepez52f69b32017-03-21 13:42:38 -07001647 SetCaretInfo();
1648 }
1649 } else {
1650 SelectNone();
1651 ScrollToCaret();
1652 SetCaretInfo();
1653 }
1654}
1655
1656void CFX_Edit::OnVK_LEFT(bool bShift, bool bCtrl) {
1657 if (!m_pVT->IsValid())
1658 return;
1659
1660 if (bShift) {
1661 if (m_wpCaret == m_pVT->GetLineBeginPlace(m_wpCaret) &&
1662 m_wpCaret != m_pVT->GetSectionBeginPlace(m_wpCaret)) {
1663 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1664 }
1665 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1666 if (m_SelState.IsEmpty())
1667 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1668 else
1669 m_SelState.SetEndPos(m_wpCaret);
1670
1671 if (m_wpOldCaret != m_wpCaret) {
1672 ScrollToCaret();
1673 Refresh();
1674 SetCaretInfo();
1675 }
1676 } else {
1677 if (!m_SelState.IsEmpty()) {
1678 if (m_SelState.BeginPos < m_SelState.EndPos)
1679 SetCaret(m_SelState.BeginPos);
1680 else
1681 SetCaret(m_SelState.EndPos);
1682
1683 SelectNone();
1684 ScrollToCaret();
1685 SetCaretInfo();
1686 } else {
1687 if (m_wpCaret == m_pVT->GetLineBeginPlace(m_wpCaret) &&
1688 m_wpCaret != m_pVT->GetSectionBeginPlace(m_wpCaret)) {
1689 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1690 }
1691 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1692 ScrollToCaret();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001693 SetCaretOrigin();
1694 SetCaretInfo();
1695 }
1696 }
1697}
1698
tsepez4cf55152016-11-02 14:37:54 -07001699void CFX_Edit::OnVK_RIGHT(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001700 if (!m_pVT->IsValid())
1701 return;
1702
1703 if (bShift) {
1704 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1705 if (m_wpCaret == m_pVT->GetLineEndPlace(m_wpCaret) &&
1706 m_wpCaret != m_pVT->GetSectionEndPlace(m_wpCaret))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001707 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1708
Tom Sepez52f69b32017-03-21 13:42:38 -07001709 if (m_SelState.IsEmpty())
1710 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1711 else
1712 m_SelState.SetEndPos(m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001713
Tom Sepez52f69b32017-03-21 13:42:38 -07001714 if (m_wpOldCaret != m_wpCaret) {
1715 ScrollToCaret();
1716 Refresh();
1717 SetCaretInfo();
1718 }
1719 } else {
1720 if (!m_SelState.IsEmpty()) {
1721 if (m_SelState.BeginPos > m_SelState.EndPos)
1722 SetCaret(m_SelState.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001723 else
Tom Sepez52f69b32017-03-21 13:42:38 -07001724 SetCaret(m_SelState.EndPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001725
Tom Sepez52f69b32017-03-21 13:42:38 -07001726 SelectNone();
1727 ScrollToCaret();
1728 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001729 } else {
Tom Sepez52f69b32017-03-21 13:42:38 -07001730 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1731 if (m_wpCaret == m_pVT->GetLineEndPlace(m_wpCaret) &&
1732 m_wpCaret != m_pVT->GetSectionEndPlace(m_wpCaret)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001733 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001734 }
Tom Sepez52f69b32017-03-21 13:42:38 -07001735 ScrollToCaret();
1736 SetCaretOrigin();
1737 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001738 }
1739 }
1740}
1741
tsepez4cf55152016-11-02 14:37:54 -07001742void CFX_Edit::OnVK_HOME(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001743 if (!m_pVT->IsValid())
1744 return;
1745
1746 if (bShift) {
1747 if (bCtrl)
1748 SetCaret(m_pVT->GetBeginWordPlace());
1749 else
1750 SetCaret(m_pVT->GetLineBeginPlace(m_wpCaret));
1751
1752 if (m_SelState.IsEmpty())
1753 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1754 else
1755 m_SelState.SetEndPos(m_wpCaret);
1756
1757 ScrollToCaret();
1758 Refresh();
1759 SetCaretInfo();
1760 } else {
1761 if (!m_SelState.IsEmpty()) {
1762 SetCaret(std::min(m_SelState.BeginPos, m_SelState.EndPos));
1763 SelectNone();
1764 ScrollToCaret();
1765 SetCaretInfo();
1766 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001767 if (bCtrl)
1768 SetCaret(m_pVT->GetBeginWordPlace());
1769 else
1770 SetCaret(m_pVT->GetLineBeginPlace(m_wpCaret));
1771
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001772 ScrollToCaret();
Tom Sepez52f69b32017-03-21 13:42:38 -07001773 SetCaretOrigin();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001774 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001775 }
1776 }
1777}
1778
tsepez4cf55152016-11-02 14:37:54 -07001779void CFX_Edit::OnVK_END(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001780 if (!m_pVT->IsValid())
1781 return;
1782
1783 if (bShift) {
1784 if (bCtrl)
1785 SetCaret(m_pVT->GetEndWordPlace());
1786 else
1787 SetCaret(m_pVT->GetLineEndPlace(m_wpCaret));
1788
1789 if (m_SelState.IsEmpty())
1790 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1791 else
1792 m_SelState.SetEndPos(m_wpCaret);
1793
1794 ScrollToCaret();
1795 Refresh();
1796 SetCaretInfo();
1797 } else {
1798 if (!m_SelState.IsEmpty()) {
1799 SetCaret(std::max(m_SelState.BeginPos, m_SelState.EndPos));
1800 SelectNone();
1801 ScrollToCaret();
1802 SetCaretInfo();
1803 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001804 if (bCtrl)
1805 SetCaret(m_pVT->GetEndWordPlace());
1806 else
1807 SetCaret(m_pVT->GetLineEndPlace(m_wpCaret));
1808
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001809 ScrollToCaret();
Tom Sepez52f69b32017-03-21 13:42:38 -07001810 SetCaretOrigin();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001811 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001812 }
1813 }
1814}
1815
tsepez4cf55152016-11-02 14:37:54 -07001816bool CFX_Edit::InsertWord(uint16_t word,
1817 int32_t charset,
1818 const CPVT_WordProps* pWordProps,
1819 bool bAddUndo,
1820 bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001821 if (IsTextOverflow() || !m_pVT->IsValid())
tsepez4cf55152016-11-02 14:37:54 -07001822 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001823
Tom Sepez3509d162017-01-30 13:22:02 -08001824 m_pVT->UpdateWordPlace(m_wpCaret);
1825 SetCaret(m_pVT->InsertWord(m_wpCaret, word,
1826 GetCharSetFromUnicode(word, charset), pWordProps));
1827 m_SelState.Set(m_wpCaret, m_wpCaret);
1828 if (m_wpCaret == m_wpOldCaret)
1829 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001830
Tom Sepez3509d162017-01-30 13:22:02 -08001831 if (bAddUndo && m_bEnableUndo) {
1832 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertWord>(
1833 this, m_wpOldCaret, m_wpCaret, word, charset, pWordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001834 }
Tom Sepez3509d162017-01-30 13:22:02 -08001835 if (bPaint)
1836 PaintInsertText(m_wpOldCaret, m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001837
Tom Sepez3509d162017-01-30 13:22:02 -08001838 if (m_bOprNotify && m_pOprNotify)
1839 m_pOprNotify->OnInsertWord(m_wpCaret, m_wpOldCaret);
1840
1841 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001842}
1843
tsepez4cf55152016-11-02 14:37:54 -07001844bool CFX_Edit::InsertReturn(const CPVT_SecProps* pSecProps,
1845 const CPVT_WordProps* pWordProps,
1846 bool bAddUndo,
1847 bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001848 if (IsTextOverflow() || !m_pVT->IsValid())
tsepez4cf55152016-11-02 14:37:54 -07001849 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001850
Tom Sepez3509d162017-01-30 13:22:02 -08001851 m_pVT->UpdateWordPlace(m_wpCaret);
1852 SetCaret(m_pVT->InsertSection(m_wpCaret, pSecProps, pWordProps));
1853 m_SelState.Set(m_wpCaret, m_wpCaret);
1854 if (m_wpCaret == m_wpOldCaret)
1855 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001856
Tom Sepez3509d162017-01-30 13:22:02 -08001857 if (bAddUndo && m_bEnableUndo) {
1858 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertReturn>(
1859 this, m_wpOldCaret, m_wpCaret, pSecProps, pWordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001860 }
Tom Sepez3509d162017-01-30 13:22:02 -08001861 if (bPaint) {
1862 RearrangePart(CPVT_WordRange(m_wpOldCaret, m_wpCaret));
1863 ScrollToCaret();
1864 Refresh();
1865 SetCaretOrigin();
1866 SetCaretInfo();
1867 }
1868 if (m_bOprNotify && m_pOprNotify)
1869 m_pOprNotify->OnInsertReturn(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001870
Tom Sepez3509d162017-01-30 13:22:02 -08001871 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001872}
1873
tsepez4cf55152016-11-02 14:37:54 -07001874bool CFX_Edit::Backspace(bool bAddUndo, bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001875 if (!m_pVT->IsValid() || m_wpCaret == m_pVT->GetBeginWordPlace())
1876 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001877
Tom Sepez3509d162017-01-30 13:22:02 -08001878 CPVT_Section section;
1879 CPVT_Word word;
1880 if (bAddUndo) {
1881 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1882 pIterator->SetAt(m_wpCaret);
1883 pIterator->GetSection(section);
1884 pIterator->GetWord(word);
1885 }
1886 m_pVT->UpdateWordPlace(m_wpCaret);
1887 SetCaret(m_pVT->BackSpaceWord(m_wpCaret));
1888 m_SelState.Set(m_wpCaret, m_wpCaret);
1889 if (m_wpCaret == m_wpOldCaret)
1890 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001891
Tom Sepez3509d162017-01-30 13:22:02 -08001892 if (bAddUndo && m_bEnableUndo) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001893 if (m_wpCaret.nSecIndex != m_wpOldCaret.nSecIndex) {
Tom Sepez3509d162017-01-30 13:22:02 -08001894 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Backspace>(
1895 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1896 section.SecProps, section.WordProps));
1897 } else {
1898 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Backspace>(
1899 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1900 section.SecProps, word.WordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001901 }
1902 }
Tom Sepez3509d162017-01-30 13:22:02 -08001903 if (bPaint) {
1904 RearrangePart(CPVT_WordRange(m_wpCaret, m_wpOldCaret));
1905 ScrollToCaret();
1906 Refresh();
1907 SetCaretOrigin();
1908 SetCaretInfo();
1909 }
1910 if (m_bOprNotify && m_pOprNotify)
1911 m_pOprNotify->OnBackSpace(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001912
Tom Sepez3509d162017-01-30 13:22:02 -08001913 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001914}
1915
tsepez4cf55152016-11-02 14:37:54 -07001916bool CFX_Edit::Delete(bool bAddUndo, bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001917 if (!m_pVT->IsValid() || m_wpCaret == m_pVT->GetEndWordPlace())
1918 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001919
Tom Sepez3509d162017-01-30 13:22:02 -08001920 CPVT_Section section;
1921 CPVT_Word word;
1922 if (bAddUndo) {
1923 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1924 pIterator->SetAt(m_pVT->GetNextWordPlace(m_wpCaret));
1925 pIterator->GetSection(section);
1926 pIterator->GetWord(word);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001927 }
Tom Sepez3509d162017-01-30 13:22:02 -08001928 m_pVT->UpdateWordPlace(m_wpCaret);
1929 bool bSecEnd = (m_wpCaret == m_pVT->GetSectionEndPlace(m_wpCaret));
1930 SetCaret(m_pVT->DeleteWord(m_wpCaret));
1931 m_SelState.Set(m_wpCaret, m_wpCaret);
1932 if (bAddUndo && m_bEnableUndo) {
1933 if (bSecEnd) {
1934 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Delete>(
1935 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1936 section.SecProps, section.WordProps, bSecEnd));
1937 } else {
1938 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Delete>(
1939 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1940 section.SecProps, word.WordProps, bSecEnd));
1941 }
1942 }
1943 if (bPaint) {
1944 RearrangePart(CPVT_WordRange(m_wpOldCaret, m_wpCaret));
1945 ScrollToCaret();
1946 Refresh();
1947 SetCaretOrigin();
1948 SetCaretInfo();
1949 }
1950 if (m_bOprNotify && m_pOprNotify)
1951 m_pOprNotify->OnDelete(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001952
Tom Sepez3509d162017-01-30 13:22:02 -08001953 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001954}
1955
tsepez4cf55152016-11-02 14:37:54 -07001956bool CFX_Edit::Empty() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001957 if (m_pVT->IsValid()) {
1958 m_pVT->DeleteWords(GetWholeWordRange());
1959 SetCaret(m_pVT->GetBeginWordPlace());
1960
tsepez4cf55152016-11-02 14:37:54 -07001961 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001962 }
1963
tsepez4cf55152016-11-02 14:37:54 -07001964 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001965}
1966
tsepez4cf55152016-11-02 14:37:54 -07001967bool CFX_Edit::Clear(bool bAddUndo, bool bPaint) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001968 if (!m_pVT->IsValid() || m_SelState.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001969 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001970
thestig821d59e2016-05-11 12:59:22 -07001971 CPVT_WordRange range = m_SelState.ConvertToWordRange();
dsinclaira2919b32016-07-13 10:55:48 -07001972 if (bAddUndo && m_bEnableUndo)
Tom Sepez3509d162017-01-30 13:22:02 -08001973 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Clear>(this, range, GetSelText()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001974
thestig821d59e2016-05-11 12:59:22 -07001975 SelectNone();
1976 SetCaret(m_pVT->DeleteWords(range));
1977 m_SelState.Set(m_wpCaret, m_wpCaret);
thestig821d59e2016-05-11 12:59:22 -07001978 if (bPaint) {
1979 RearrangePart(range);
1980 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001981 Refresh();
thestig821d59e2016-05-11 12:59:22 -07001982 SetCaretOrigin();
1983 SetCaretInfo();
1984 }
thestig821d59e2016-05-11 12:59:22 -07001985 if (m_bOprNotify && m_pOprNotify)
1986 m_pOprNotify->OnClear(m_wpCaret, m_wpOldCaret);
1987
tsepez4cf55152016-11-02 14:37:54 -07001988 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001989}
1990
tsepez4cf55152016-11-02 14:37:54 -07001991bool CFX_Edit::InsertText(const CFX_WideString& sText,
1992 int32_t charset,
1993 bool bAddUndo,
1994 bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001995 if (IsTextOverflow())
tsepez4cf55152016-11-02 14:37:54 -07001996 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001997
1998 m_pVT->UpdateWordPlace(m_wpCaret);
tsepeza31da742016-09-08 11:28:14 -07001999 SetCaret(DoInsertText(m_wpCaret, sText, charset));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002000 m_SelState.Set(m_wpCaret, m_wpCaret);
tsepeza31da742016-09-08 11:28:14 -07002001 if (m_wpCaret == m_wpOldCaret)
tsepez4cf55152016-11-02 14:37:54 -07002002 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002003
tsepeza31da742016-09-08 11:28:14 -07002004 if (bAddUndo && m_bEnableUndo) {
Tom Sepez3509d162017-01-30 13:22:02 -08002005 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertText>(
2006 this, m_wpOldCaret, m_wpCaret, sText, charset));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002007 }
tsepeza31da742016-09-08 11:28:14 -07002008 if (bPaint)
2009 PaintInsertText(m_wpOldCaret, m_wpCaret);
2010
2011 if (m_bOprNotify && m_pOprNotify)
2012 m_pOprNotify->OnInsertText(m_wpCaret, m_wpOldCaret);
2013
tsepez4cf55152016-11-02 14:37:54 -07002014 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002015}
2016
2017void CFX_Edit::PaintInsertText(const CPVT_WordPlace& wpOld,
2018 const CPVT_WordPlace& wpNew) {
2019 if (m_pVT->IsValid()) {
2020 RearrangePart(CPVT_WordRange(wpOld, wpNew));
2021 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07002022 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002023 SetCaretOrigin();
2024 SetCaretInfo();
2025 }
2026}
2027
tsepez4cf55152016-11-02 14:37:54 -07002028bool CFX_Edit::Redo() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002029 if (m_bEnableUndo) {
2030 if (m_Undo.CanRedo()) {
2031 m_Undo.Redo();
tsepez4cf55152016-11-02 14:37:54 -07002032 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002033 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002034 }
2035
tsepez4cf55152016-11-02 14:37:54 -07002036 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002037}
2038
tsepez4cf55152016-11-02 14:37:54 -07002039bool CFX_Edit::Undo() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002040 if (m_bEnableUndo) {
2041 if (m_Undo.CanUndo()) {
2042 m_Undo.Undo();
tsepez4cf55152016-11-02 14:37:54 -07002043 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002044 }
2045 }
2046
tsepez4cf55152016-11-02 14:37:54 -07002047 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002048}
2049
2050void CFX_Edit::SetCaretOrigin() {
thestig821d59e2016-05-11 12:59:22 -07002051 if (!m_pVT->IsValid())
2052 return;
2053
2054 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
2055 pIterator->SetAt(m_wpCaret);
2056 CPVT_Word word;
2057 CPVT_Line line;
2058 if (pIterator->GetWord(word)) {
2059 m_ptCaret.x = word.ptWord.x + word.fWidth;
2060 m_ptCaret.y = word.ptWord.y;
2061 } else if (pIterator->GetLine(line)) {
2062 m_ptCaret.x = line.ptLine.x;
2063 m_ptCaret.y = line.ptLine.y;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002064 }
2065}
2066
2067int32_t CFX_Edit::WordPlaceToWordIndex(const CPVT_WordPlace& place) const {
2068 if (m_pVT->IsValid())
2069 return m_pVT->WordPlaceToWordIndex(place);
2070
2071 return -1;
2072}
2073
2074CPVT_WordPlace CFX_Edit::WordIndexToWordPlace(int32_t index) const {
2075 if (m_pVT->IsValid())
2076 return m_pVT->WordIndexToWordPlace(index);
2077
2078 return CPVT_WordPlace();
2079}
2080
tsepez4cf55152016-11-02 14:37:54 -07002081bool CFX_Edit::IsTextFull() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002082 int32_t nTotalWords = m_pVT->GetTotalWords();
2083 int32_t nLimitChar = m_pVT->GetLimitChar();
2084 int32_t nCharArray = m_pVT->GetCharArray();
2085
2086 return IsTextOverflow() || (nLimitChar > 0 && nTotalWords >= nLimitChar) ||
2087 (nCharArray > 0 && nTotalWords >= nCharArray);
2088}
2089
tsepez4cf55152016-11-02 14:37:54 -07002090bool CFX_Edit::IsTextOverflow() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002091 if (!m_bEnableScroll && !m_bEnableOverflow) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08002092 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
2093 CFX_FloatRect rcContent = m_pVT->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002094
dsinclair448c4332016-08-02 12:07:35 -07002095 if (m_pVT->IsMultiLine() && GetTotalLines() > 1 &&
2096 IsFloatBigger(rcContent.Height(), rcPlate.Height())) {
tsepez4cf55152016-11-02 14:37:54 -07002097 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002098 }
2099
dsinclair448c4332016-08-02 12:07:35 -07002100 if (IsFloatBigger(rcContent.Width(), rcPlate.Width()))
tsepez4cf55152016-11-02 14:37:54 -07002101 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002102 }
2103
tsepez4cf55152016-11-02 14:37:54 -07002104 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002105}
2106
tsepez4cf55152016-11-02 14:37:54 -07002107bool CFX_Edit::CanUndo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002108 if (m_bEnableUndo) {
2109 return m_Undo.CanUndo();
2110 }
2111
tsepez4cf55152016-11-02 14:37:54 -07002112 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002113}
2114
tsepez4cf55152016-11-02 14:37:54 -07002115bool CFX_Edit::CanRedo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002116 if (m_bEnableUndo) {
2117 return m_Undo.CanRedo();
2118 }
2119
tsepez4cf55152016-11-02 14:37:54 -07002120 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002121}
2122
tsepez4cf55152016-11-02 14:37:54 -07002123void CFX_Edit::EnableRefresh(bool bRefresh) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002124 m_bEnableRefresh = bRefresh;
2125}
2126
tsepez4cf55152016-11-02 14:37:54 -07002127void CFX_Edit::EnableUndo(bool bUndo) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002128 m_bEnableUndo = bUndo;
2129}
2130
tsepez4cf55152016-11-02 14:37:54 -07002131void CFX_Edit::EnableOprNotify(bool bNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002132 m_bOprNotify = bNotify;
2133}
2134
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002135CPVT_WordPlace CFX_Edit::DoInsertText(const CPVT_WordPlace& place,
tsepeza31da742016-09-08 11:28:14 -07002136 const CFX_WideString& sText,
dsinclairefd5a992016-07-18 10:04:07 -07002137 int32_t charset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002138 CPVT_WordPlace wp = place;
2139
2140 if (m_pVT->IsValid()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002141 for (int32_t i = 0, sz = sText.GetLength(); i < sz; i++) {
Tom Sepez62a70f92016-03-21 15:00:20 -07002142 uint16_t word = sText[i];
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002143 switch (word) {
2144 case 0x0D:
dsinclairefd5a992016-07-18 10:04:07 -07002145 wp = m_pVT->InsertSection(wp, nullptr, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002146 if (sText[i + 1] == 0x0A)
2147 i++;
2148 break;
2149 case 0x0A:
dsinclairefd5a992016-07-18 10:04:07 -07002150 wp = m_pVT->InsertSection(wp, nullptr, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002151 if (sText[i + 1] == 0x0D)
2152 i++;
2153 break;
2154 case 0x09:
2155 word = 0x20;
2156 default:
2157 wp = m_pVT->InsertWord(wp, word, GetCharSetFromUnicode(word, charset),
dsinclairefd5a992016-07-18 10:04:07 -07002158 nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002159 break;
2160 }
2161 }
2162 }
2163
2164 return wp;
2165}
2166
Tom Sepez62a70f92016-03-21 15:00:20 -07002167int32_t CFX_Edit::GetCharSetFromUnicode(uint16_t word, int32_t nOldCharset) {
dsinclairc7a73492016-04-05 12:01:42 -07002168 if (IPVT_FontMap* pFontMap = GetFontMap())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002169 return pFontMap->CharSetFromUnicode(word, nOldCharset);
2170 return nOldCharset;
2171}
2172
Tom Sepez3509d162017-01-30 13:22:02 -08002173void CFX_Edit::AddEditUndoItem(
2174 std::unique_ptr<CFX_Edit_UndoItem> pEditUndoItem) {
2175 if (m_pGroupUndoItem)
2176 m_pGroupUndoItem->AddUndoItem(std::move(pEditUndoItem));
2177 else
2178 m_Undo.AddItem(std::move(pEditUndoItem));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002179}
2180
weili625ad662016-06-15 11:21:33 -07002181CFX_Edit_LineRectArray::CFX_Edit_LineRectArray() {}
2182
Tom Sepez3509d162017-01-30 13:22:02 -08002183CFX_Edit_LineRectArray::~CFX_Edit_LineRectArray() {}
weili625ad662016-06-15 11:21:33 -07002184
Tom Sepez3509d162017-01-30 13:22:02 -08002185void CFX_Edit_LineRectArray::operator=(CFX_Edit_LineRectArray&& that) {
2186 m_LineRects = std::move(that.m_LineRects);
weili625ad662016-06-15 11:21:33 -07002187}
2188
2189void CFX_Edit_LineRectArray::Add(const CPVT_WordRange& wrLine,
2190 const CFX_FloatRect& rcLine) {
Tom Sepez3509d162017-01-30 13:22:02 -08002191 m_LineRects.push_back(pdfium::MakeUnique<CFX_Edit_LineRect>(wrLine, rcLine));
weili625ad662016-06-15 11:21:33 -07002192}
2193
2194int32_t CFX_Edit_LineRectArray::GetSize() const {
Tom Sepez3509d162017-01-30 13:22:02 -08002195 return pdfium::CollectionSize<int32_t>(m_LineRects);
weili625ad662016-06-15 11:21:33 -07002196}
2197
2198CFX_Edit_LineRect* CFX_Edit_LineRectArray::GetAt(int32_t nIndex) const {
Tom Sepez3509d162017-01-30 13:22:02 -08002199 if (nIndex < 0 || nIndex >= GetSize())
weili625ad662016-06-15 11:21:33 -07002200 return nullptr;
2201
Tom Sepez3509d162017-01-30 13:22:02 -08002202 return m_LineRects[nIndex].get();
weili625ad662016-06-15 11:21:33 -07002203}
2204
2205CFX_Edit_Select::CFX_Edit_Select() {}
2206
2207CFX_Edit_Select::CFX_Edit_Select(const CPVT_WordPlace& begin,
2208 const CPVT_WordPlace& end) {
2209 Set(begin, end);
2210}
2211
2212CFX_Edit_Select::CFX_Edit_Select(const CPVT_WordRange& range) {
2213 Set(range.BeginPos, range.EndPos);
2214}
2215
2216CPVT_WordRange CFX_Edit_Select::ConvertToWordRange() const {
2217 return CPVT_WordRange(BeginPos, EndPos);
2218}
2219
Tom Sepez52f69b32017-03-21 13:42:38 -07002220void CFX_Edit_Select::Reset() {
2221 BeginPos.Reset();
2222 EndPos.Reset();
weili625ad662016-06-15 11:21:33 -07002223}
2224
2225void CFX_Edit_Select::Set(const CPVT_WordPlace& begin,
2226 const CPVT_WordPlace& end) {
2227 BeginPos = begin;
2228 EndPos = end;
2229}
2230
2231void CFX_Edit_Select::SetBeginPos(const CPVT_WordPlace& begin) {
2232 BeginPos = begin;
2233}
2234
2235void CFX_Edit_Select::SetEndPos(const CPVT_WordPlace& end) {
2236 EndPos = end;
2237}
2238
Tom Sepez52f69b32017-03-21 13:42:38 -07002239bool CFX_Edit_Select::IsEmpty() const {
2240 return BeginPos == EndPos;
weili625ad662016-06-15 11:21:33 -07002241}
2242
weili625ad662016-06-15 11:21:33 -07002243CFX_Edit_RectArray::CFX_Edit_RectArray() {}
2244
Tom Sepez3509d162017-01-30 13:22:02 -08002245CFX_Edit_RectArray::~CFX_Edit_RectArray() {}
weili625ad662016-06-15 11:21:33 -07002246
Tom Sepez3509d162017-01-30 13:22:02 -08002247void CFX_Edit_RectArray::Clear() {
2248 m_Rects.clear();
weili625ad662016-06-15 11:21:33 -07002249}
2250
2251void CFX_Edit_RectArray::Add(const CFX_FloatRect& rect) {
2252 // check for overlapped area
Tom Sepez3509d162017-01-30 13:22:02 -08002253 for (const auto& pRect : m_Rects) {
weili625ad662016-06-15 11:21:33 -07002254 if (pRect && pRect->Contains(rect))
2255 return;
2256 }
Tom Sepez3509d162017-01-30 13:22:02 -08002257 m_Rects.push_back(pdfium::MakeUnique<CFX_FloatRect>(rect));
weili625ad662016-06-15 11:21:33 -07002258}
2259
2260int32_t CFX_Edit_RectArray::GetSize() const {
Tom Sepez3509d162017-01-30 13:22:02 -08002261 return pdfium::CollectionSize<int32_t>(m_Rects);
weili625ad662016-06-15 11:21:33 -07002262}
2263
2264CFX_FloatRect* CFX_Edit_RectArray::GetAt(int32_t nIndex) const {
Tom Sepez3509d162017-01-30 13:22:02 -08002265 if (nIndex < 0 || nIndex >= GetSize())
weili625ad662016-06-15 11:21:33 -07002266 return nullptr;
2267
Tom Sepez3509d162017-01-30 13:22:02 -08002268 return m_Rects[nIndex].get();
weili625ad662016-06-15 11:21:33 -07002269}