blob: 20f584e183e71c9074a24fa488e932d92875def0 [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::GetTotalWords() const {
1078 return m_pVT->GetTotalWords();
1079}
1080
1081int32_t CFX_Edit::GetTotalLines() const {
thestig821d59e2016-05-11 12:59:22 -07001082 int32_t nLines = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001083
thestig821d59e2016-05-11 12:59:22 -07001084 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1085 pIterator->SetAt(0);
1086 while (pIterator->NextLine())
1087 ++nLines;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001088
thestig821d59e2016-05-11 12:59:22 -07001089 return nLines;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001090}
1091
1092CPVT_WordRange CFX_Edit::GetSelectWordRange() const {
1093 return m_SelState.ConvertToWordRange();
1094}
1095
tsepeza31da742016-09-08 11:28:14 -07001096void CFX_Edit::SetText(const CFX_WideString& sText) {
dsinclairefd5a992016-07-18 10:04:07 -07001097 Empty();
npmea3c3be2016-09-19 07:24:33 -07001098 DoInsertText(CPVT_WordPlace(0, 0, -1), sText, FXFONT_DEFAULT_CHARSET);
dsinclairefd5a992016-07-18 10:04:07 -07001099 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001100}
1101
tsepez4cf55152016-11-02 14:37:54 -07001102bool CFX_Edit::InsertWord(uint16_t word, int32_t charset) {
1103 return InsertWord(word, charset, nullptr, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001104}
1105
tsepez4cf55152016-11-02 14:37:54 -07001106bool CFX_Edit::InsertReturn() {
1107 return InsertReturn(nullptr, nullptr, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001108}
1109
tsepez4cf55152016-11-02 14:37:54 -07001110bool CFX_Edit::Backspace() {
1111 return Backspace(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001112}
1113
tsepez4cf55152016-11-02 14:37:54 -07001114bool CFX_Edit::Delete() {
1115 return Delete(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001116}
1117
tsepez4cf55152016-11-02 14:37:54 -07001118bool CFX_Edit::Clear() {
1119 return Clear(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001120}
1121
tsepez4cf55152016-11-02 14:37:54 -07001122bool CFX_Edit::InsertText(const CFX_WideString& sText, int32_t charset) {
1123 return InsertText(sText, charset, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001124}
1125
Dan Sinclair05df0752017-03-14 14:43:42 -04001126float CFX_Edit::GetFontSize() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001127 return m_pVT->GetFontSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001128}
1129
Tom Sepez62a70f92016-03-21 15:00:20 -07001130uint16_t CFX_Edit::GetPasswordChar() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001131 return m_pVT->GetPasswordChar();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001132}
1133
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001134int32_t CFX_Edit::GetCharArray() const {
1135 return m_pVT->GetCharArray();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001136}
1137
Tom Sepez281a9ea2016-02-26 14:24:28 -08001138CFX_FloatRect CFX_Edit::GetContentRect() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001139 return VTToEdit(m_pVT->GetContentRect());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001140}
1141
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001142int32_t CFX_Edit::GetHorzScale() const {
1143 return m_pVT->GetHorzScale();
1144}
1145
Dan Sinclair05df0752017-03-14 14:43:42 -04001146float CFX_Edit::GetCharSpace() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001147 return m_pVT->GetCharSpace();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001148}
1149
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001150CPVT_WordRange CFX_Edit::GetWholeWordRange() const {
1151 if (m_pVT->IsValid())
1152 return CPVT_WordRange(m_pVT->GetBeginWordPlace(), m_pVT->GetEndWordPlace());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001153
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001154 return CPVT_WordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001155}
1156
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001157CPVT_WordRange CFX_Edit::GetVisibleWordRange() const {
1158 if (m_bEnableOverflow)
1159 return GetWholeWordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001160
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001161 if (m_pVT->IsValid()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001162 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001163
Dan Sinclairf528eee2017-02-14 11:52:07 -05001164 CPVT_WordPlace place1 =
1165 m_pVT->SearchWordPlace(EditToVT(CFX_PointF(rcPlate.left, rcPlate.top)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001166 CPVT_WordPlace place2 = m_pVT->SearchWordPlace(
Dan Sinclairf528eee2017-02-14 11:52:07 -05001167 EditToVT(CFX_PointF(rcPlate.right, rcPlate.bottom)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001168
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001169 return CPVT_WordRange(place1, place2);
1170 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001171
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001172 return CPVT_WordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001173}
1174
Dan Sinclairf528eee2017-02-14 11:52:07 -05001175CPVT_WordPlace CFX_Edit::SearchWordPlace(const CFX_PointF& point) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001176 if (m_pVT->IsValid()) {
1177 return m_pVT->SearchWordPlace(EditToVT(point));
1178 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001179
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001180 return CPVT_WordPlace();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001181}
1182
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001183void CFX_Edit::Paint() {
1184 if (m_pVT->IsValid()) {
1185 RearrangeAll();
1186 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001187 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001188 SetCaretOrigin();
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001189 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001190 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001191}
1192
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001193void CFX_Edit::RearrangeAll() {
1194 if (m_pVT->IsValid()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001195 m_pVT->UpdateWordPlace(m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001196 m_pVT->RearrangeAll();
1197 m_pVT->UpdateWordPlace(m_wpCaret);
1198 SetScrollInfo();
1199 SetContentChanged();
1200 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001201}
1202
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001203void CFX_Edit::RearrangePart(const CPVT_WordRange& range) {
1204 if (m_pVT->IsValid()) {
1205 m_pVT->UpdateWordPlace(m_wpCaret);
1206 m_pVT->RearrangePart(range);
1207 m_pVT->UpdateWordPlace(m_wpCaret);
1208 SetScrollInfo();
1209 SetContentChanged();
1210 }
1211}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001212
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001213void CFX_Edit::SetContentChanged() {
dsinclaira2919b32016-07-13 10:55:48 -07001214 if (m_pNotify) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001215 CFX_FloatRect rcContent = m_pVT->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001216 if (rcContent.Width() != m_rcOldContent.Width() ||
1217 rcContent.Height() != m_rcOldContent.Height()) {
1218 if (!m_bNotifyFlag) {
tsepez4cf55152016-11-02 14:37:54 -07001219 m_bNotifyFlag = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001220 m_pNotify->IOnContentChange(rcContent);
tsepez4cf55152016-11-02 14:37:54 -07001221 m_bNotifyFlag = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001222 }
1223 m_rcOldContent = rcContent;
1224 }
1225 }
1226}
1227
1228void CFX_Edit::SelectAll() {
Tom Sepez52f69b32017-03-21 13:42:38 -07001229 if (!m_pVT->IsValid())
1230 return;
1231 m_SelState = CFX_Edit_Select(GetWholeWordRange());
1232 SetCaret(m_SelState.EndPos);
1233 ScrollToCaret();
1234 Refresh();
1235 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001236}
1237
1238void CFX_Edit::SelectNone() {
Tom Sepez52f69b32017-03-21 13:42:38 -07001239 if (!m_pVT->IsValid() || m_SelState.IsEmpty())
1240 return;
1241
1242 m_SelState.Reset();
1243 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001244}
1245
tsepez4cf55152016-11-02 14:37:54 -07001246bool CFX_Edit::IsSelected() const {
Tom Sepez52f69b32017-03-21 13:42:38 -07001247 return !m_SelState.IsEmpty();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001248}
1249
Dan Sinclairf528eee2017-02-14 11:52:07 -05001250CFX_PointF CFX_Edit::VTToEdit(const CFX_PointF& point) const {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001251 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1252 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001253
Dan Sinclair05df0752017-03-14 14:43:42 -04001254 float fPadding = 0.0f;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001255
1256 switch (m_nAlignment) {
1257 case 0:
1258 fPadding = 0.0f;
1259 break;
1260 case 1:
1261 fPadding = (rcPlate.Height() - rcContent.Height()) * 0.5f;
1262 break;
1263 case 2:
1264 fPadding = rcPlate.Height() - rcContent.Height();
1265 break;
1266 }
1267
Dan Sinclairf528eee2017-02-14 11:52:07 -05001268 return CFX_PointF(point.x - (m_ptScrollPos.x - rcPlate.left),
1269 point.y - (m_ptScrollPos.y + fPadding - rcPlate.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001270}
1271
Dan Sinclairf528eee2017-02-14 11:52:07 -05001272CFX_PointF CFX_Edit::EditToVT(const CFX_PointF& point) const {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001273 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1274 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001275
Dan Sinclair05df0752017-03-14 14:43:42 -04001276 float fPadding = 0.0f;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001277
1278 switch (m_nAlignment) {
1279 case 0:
1280 fPadding = 0.0f;
1281 break;
1282 case 1:
1283 fPadding = (rcPlate.Height() - rcContent.Height()) * 0.5f;
1284 break;
1285 case 2:
1286 fPadding = rcPlate.Height() - rcContent.Height();
1287 break;
1288 }
1289
Dan Sinclairf528eee2017-02-14 11:52:07 -05001290 return CFX_PointF(point.x + (m_ptScrollPos.x - rcPlate.left),
1291 point.y + (m_ptScrollPos.y + fPadding - rcPlate.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001292}
1293
Tom Sepez281a9ea2016-02-26 14:24:28 -08001294CFX_FloatRect CFX_Edit::VTToEdit(const CFX_FloatRect& rect) const {
Dan Sinclairf528eee2017-02-14 11:52:07 -05001295 CFX_PointF ptLeftBottom = VTToEdit(CFX_PointF(rect.left, rect.bottom));
1296 CFX_PointF ptRightTop = VTToEdit(CFX_PointF(rect.right, rect.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001297
Tom Sepez281a9ea2016-02-26 14:24:28 -08001298 return CFX_FloatRect(ptLeftBottom.x, ptLeftBottom.y, ptRightTop.x,
1299 ptRightTop.y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001300}
1301
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001302void CFX_Edit::SetScrollInfo() {
dsinclaira2919b32016-07-13 10:55:48 -07001303 if (m_pNotify) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001304 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
1305 CFX_FloatRect rcContent = m_pVT->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001306
1307 if (!m_bNotifyFlag) {
tsepez4cf55152016-11-02 14:37:54 -07001308 m_bNotifyFlag = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001309 m_pNotify->IOnSetScrollInfoY(rcPlate.bottom, rcPlate.top,
1310 rcContent.bottom, rcContent.top,
1311 rcPlate.Height() / 3, rcPlate.Height());
tsepez4cf55152016-11-02 14:37:54 -07001312 m_bNotifyFlag = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001313 }
1314 }
1315}
1316
Dan Sinclair05df0752017-03-14 14:43:42 -04001317void CFX_Edit::SetScrollPosX(float fx) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001318 if (!m_bEnableScroll)
1319 return;
1320
1321 if (m_pVT->IsValid()) {
dsinclair448c4332016-08-02 12:07:35 -07001322 if (!IsFloatEqual(m_ptScrollPos.x, fx)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001323 m_ptScrollPos.x = fx;
dsinclairefd5a992016-07-18 10:04:07 -07001324 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001325 }
1326 }
1327}
1328
Dan Sinclair05df0752017-03-14 14:43:42 -04001329void CFX_Edit::SetScrollPosY(float fy) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001330 if (!m_bEnableScroll)
1331 return;
1332
1333 if (m_pVT->IsValid()) {
dsinclair448c4332016-08-02 12:07:35 -07001334 if (!IsFloatEqual(m_ptScrollPos.y, fy)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001335 m_ptScrollPos.y = fy;
dsinclairefd5a992016-07-18 10:04:07 -07001336 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001337
dsinclaira2919b32016-07-13 10:55:48 -07001338 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001339 if (!m_bNotifyFlag) {
tsepez4cf55152016-11-02 14:37:54 -07001340 m_bNotifyFlag = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001341 m_pNotify->IOnSetScrollPosY(fy);
tsepez4cf55152016-11-02 14:37:54 -07001342 m_bNotifyFlag = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001343 }
1344 }
1345 }
1346 }
1347}
1348
Dan Sinclairf528eee2017-02-14 11:52:07 -05001349void CFX_Edit::SetScrollPos(const CFX_PointF& point) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001350 SetScrollPosX(point.x);
1351 SetScrollPosY(point.y);
1352 SetScrollLimit();
1353 SetCaretInfo();
1354}
1355
Dan Sinclairf528eee2017-02-14 11:52:07 -05001356CFX_PointF CFX_Edit::GetScrollPos() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001357 return m_ptScrollPos;
1358}
1359
1360void CFX_Edit::SetScrollLimit() {
1361 if (m_pVT->IsValid()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001362 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1363 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001364
1365 if (rcPlate.Width() > rcContent.Width()) {
1366 SetScrollPosX(rcPlate.left);
1367 } else {
dsinclair448c4332016-08-02 12:07:35 -07001368 if (IsFloatSmaller(m_ptScrollPos.x, rcContent.left)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001369 SetScrollPosX(rcContent.left);
dsinclair448c4332016-08-02 12:07:35 -07001370 } else if (IsFloatBigger(m_ptScrollPos.x,
1371 rcContent.right - rcPlate.Width())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001372 SetScrollPosX(rcContent.right - rcPlate.Width());
1373 }
1374 }
1375
1376 if (rcPlate.Height() > rcContent.Height()) {
1377 SetScrollPosY(rcPlate.top);
1378 } else {
dsinclair448c4332016-08-02 12:07:35 -07001379 if (IsFloatSmaller(m_ptScrollPos.y,
1380 rcContent.bottom + rcPlate.Height())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001381 SetScrollPosY(rcContent.bottom + rcPlate.Height());
dsinclair448c4332016-08-02 12:07:35 -07001382 } else if (IsFloatBigger(m_ptScrollPos.y, rcContent.top)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001383 SetScrollPosY(rcContent.top);
1384 }
1385 }
1386 }
1387}
1388
1389void CFX_Edit::ScrollToCaret() {
1390 SetScrollLimit();
1391
thestig821d59e2016-05-11 12:59:22 -07001392 if (!m_pVT->IsValid())
1393 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001394
thestig821d59e2016-05-11 12:59:22 -07001395 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1396 pIterator->SetAt(m_wpCaret);
1397
Dan Sinclairf528eee2017-02-14 11:52:07 -05001398 CFX_PointF ptHead;
1399 CFX_PointF ptFoot;
thestig821d59e2016-05-11 12:59:22 -07001400 CPVT_Word word;
1401 CPVT_Line line;
1402 if (pIterator->GetWord(word)) {
1403 ptHead.x = word.ptWord.x + word.fWidth;
1404 ptHead.y = word.ptWord.y + word.fAscent;
1405 ptFoot.x = word.ptWord.x + word.fWidth;
1406 ptFoot.y = word.ptWord.y + word.fDescent;
1407 } else if (pIterator->GetLine(line)) {
1408 ptHead.x = line.ptLine.x;
1409 ptHead.y = line.ptLine.y + line.fLineAscent;
1410 ptFoot.x = line.ptLine.x;
1411 ptFoot.y = line.ptLine.y + line.fLineDescent;
1412 }
1413
Dan Sinclairf528eee2017-02-14 11:52:07 -05001414 CFX_PointF ptHeadEdit = VTToEdit(ptHead);
1415 CFX_PointF ptFootEdit = VTToEdit(ptFoot);
thestig821d59e2016-05-11 12:59:22 -07001416 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
dsinclair448c4332016-08-02 12:07:35 -07001417 if (!IsFloatEqual(rcPlate.left, rcPlate.right)) {
1418 if (IsFloatSmaller(ptHeadEdit.x, rcPlate.left) ||
1419 IsFloatEqual(ptHeadEdit.x, rcPlate.left)) {
thestig821d59e2016-05-11 12:59:22 -07001420 SetScrollPosX(ptHead.x);
dsinclair448c4332016-08-02 12:07:35 -07001421 } else if (IsFloatBigger(ptHeadEdit.x, rcPlate.right)) {
thestig821d59e2016-05-11 12:59:22 -07001422 SetScrollPosX(ptHead.x - rcPlate.Width());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001423 }
thestig821d59e2016-05-11 12:59:22 -07001424 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001425
dsinclair448c4332016-08-02 12:07:35 -07001426 if (!IsFloatEqual(rcPlate.top, rcPlate.bottom)) {
1427 if (IsFloatSmaller(ptFootEdit.y, rcPlate.bottom) ||
1428 IsFloatEqual(ptFootEdit.y, rcPlate.bottom)) {
1429 if (IsFloatSmaller(ptHeadEdit.y, rcPlate.top)) {
thestig821d59e2016-05-11 12:59:22 -07001430 SetScrollPosY(ptFoot.y + rcPlate.Height());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001431 }
dsinclair448c4332016-08-02 12:07:35 -07001432 } else if (IsFloatBigger(ptHeadEdit.y, rcPlate.top)) {
1433 if (IsFloatBigger(ptFootEdit.y, rcPlate.bottom)) {
thestig821d59e2016-05-11 12:59:22 -07001434 SetScrollPosY(ptHead.y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001435 }
1436 }
1437 }
1438}
1439
dsinclairefd5a992016-07-18 10:04:07 -07001440void CFX_Edit::Refresh() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001441 if (m_bEnableRefresh && m_pVT->IsValid()) {
1442 m_Refresh.BeginRefresh();
1443 RefreshPushLineRects(GetVisibleWordRange());
1444
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001445 m_Refresh.NoAnalyse();
1446 m_ptRefreshScrollPos = m_ptScrollPos;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001447
dsinclaira2919b32016-07-13 10:55:48 -07001448 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001449 if (!m_bNotifyFlag) {
tsepez4cf55152016-11-02 14:37:54 -07001450 m_bNotifyFlag = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001451 if (const CFX_Edit_RectArray* pRects = m_Refresh.GetRefreshRects()) {
1452 for (int32_t i = 0, sz = pRects->GetSize(); i < sz; i++)
1453 m_pNotify->IOnInvalidateRect(pRects->GetAt(i));
1454 }
tsepez4cf55152016-11-02 14:37:54 -07001455 m_bNotifyFlag = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001456 }
1457 }
1458
1459 m_Refresh.EndRefresh();
1460 }
1461}
1462
1463void CFX_Edit::RefreshPushLineRects(const CPVT_WordRange& wr) {
thestig821d59e2016-05-11 12:59:22 -07001464 if (!m_pVT->IsValid())
1465 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001466
thestig821d59e2016-05-11 12:59:22 -07001467 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1468 CPVT_WordPlace wpBegin = wr.BeginPos;
1469 m_pVT->UpdateWordPlace(wpBegin);
1470 CPVT_WordPlace wpEnd = wr.EndPos;
1471 m_pVT->UpdateWordPlace(wpEnd);
1472 pIterator->SetAt(wpBegin);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001473
thestig821d59e2016-05-11 12:59:22 -07001474 CPVT_Line lineinfo;
1475 do {
1476 if (!pIterator->GetLine(lineinfo))
1477 break;
1478 if (lineinfo.lineplace.LineCmp(wpEnd) > 0)
1479 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001480
thestig821d59e2016-05-11 12:59:22 -07001481 CFX_FloatRect rcLine(lineinfo.ptLine.x,
1482 lineinfo.ptLine.y + lineinfo.fLineDescent,
1483 lineinfo.ptLine.x + lineinfo.fLineWidth,
1484 lineinfo.ptLine.y + lineinfo.fLineAscent);
1485
1486 m_Refresh.Push(CPVT_WordRange(lineinfo.lineplace, lineinfo.lineEnd),
1487 VTToEdit(rcLine));
1488 } while (pIterator->NextLine());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001489}
1490
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001491void CFX_Edit::RefreshWordRange(const CPVT_WordRange& wr) {
thestig821d59e2016-05-11 12:59:22 -07001492 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1493 CPVT_WordRange wrTemp = wr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001494
thestig821d59e2016-05-11 12:59:22 -07001495 m_pVT->UpdateWordPlace(wrTemp.BeginPos);
1496 m_pVT->UpdateWordPlace(wrTemp.EndPos);
1497 pIterator->SetAt(wrTemp.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001498
thestig821d59e2016-05-11 12:59:22 -07001499 CPVT_Word wordinfo;
1500 CPVT_Line lineinfo;
1501 CPVT_WordPlace place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001502
thestig821d59e2016-05-11 12:59:22 -07001503 while (pIterator->NextWord()) {
1504 place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -07001505 if (place > wrTemp.EndPos)
thestig821d59e2016-05-11 12:59:22 -07001506 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001507
thestig821d59e2016-05-11 12:59:22 -07001508 pIterator->GetWord(wordinfo);
1509 pIterator->GetLine(lineinfo);
thestig821d59e2016-05-11 12:59:22 -07001510 if (place.LineCmp(wrTemp.BeginPos) == 0 ||
1511 place.LineCmp(wrTemp.EndPos) == 0) {
1512 CFX_FloatRect rcWord(wordinfo.ptWord.x,
1513 lineinfo.ptLine.y + lineinfo.fLineDescent,
1514 wordinfo.ptWord.x + wordinfo.fWidth,
1515 lineinfo.ptLine.y + lineinfo.fLineAscent);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001516
dsinclaira2919b32016-07-13 10:55:48 -07001517 if (m_pNotify) {
thestig821d59e2016-05-11 12:59:22 -07001518 if (!m_bNotifyFlag) {
tsepez4cf55152016-11-02 14:37:54 -07001519 m_bNotifyFlag = true;
thestig821d59e2016-05-11 12:59:22 -07001520 CFX_FloatRect rcRefresh = VTToEdit(rcWord);
1521 m_pNotify->IOnInvalidateRect(&rcRefresh);
tsepez4cf55152016-11-02 14:37:54 -07001522 m_bNotifyFlag = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001523 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001524 }
thestig821d59e2016-05-11 12:59:22 -07001525 } else {
1526 CFX_FloatRect rcLine(lineinfo.ptLine.x,
1527 lineinfo.ptLine.y + lineinfo.fLineDescent,
1528 lineinfo.ptLine.x + lineinfo.fLineWidth,
1529 lineinfo.ptLine.y + lineinfo.fLineAscent);
1530
dsinclaira2919b32016-07-13 10:55:48 -07001531 if (m_pNotify) {
thestig821d59e2016-05-11 12:59:22 -07001532 if (!m_bNotifyFlag) {
tsepez4cf55152016-11-02 14:37:54 -07001533 m_bNotifyFlag = true;
thestig821d59e2016-05-11 12:59:22 -07001534 CFX_FloatRect rcRefresh = VTToEdit(rcLine);
1535 m_pNotify->IOnInvalidateRect(&rcRefresh);
tsepez4cf55152016-11-02 14:37:54 -07001536 m_bNotifyFlag = false;
thestig821d59e2016-05-11 12:59:22 -07001537 }
1538 }
1539
1540 pIterator->NextLine();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001541 }
1542 }
1543}
1544
1545void CFX_Edit::SetCaret(const CPVT_WordPlace& place) {
1546 m_wpOldCaret = m_wpCaret;
1547 m_wpCaret = place;
1548}
1549
1550void CFX_Edit::SetCaretInfo() {
dsinclaira2919b32016-07-13 10:55:48 -07001551 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001552 if (!m_bNotifyFlag) {
thestig821d59e2016-05-11 12:59:22 -07001553 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1554 pIterator->SetAt(m_wpCaret);
tsepez63f545c2016-09-13 16:08:49 -07001555
Dan Sinclairf528eee2017-02-14 11:52:07 -05001556 CFX_PointF ptHead;
1557 CFX_PointF ptFoot;
thestig821d59e2016-05-11 12:59:22 -07001558 CPVT_Word word;
1559 CPVT_Line line;
1560 if (pIterator->GetWord(word)) {
1561 ptHead.x = word.ptWord.x + word.fWidth;
1562 ptHead.y = word.ptWord.y + word.fAscent;
1563 ptFoot.x = word.ptWord.x + word.fWidth;
1564 ptFoot.y = word.ptWord.y + word.fDescent;
1565 } else if (pIterator->GetLine(line)) {
1566 ptHead.x = line.ptLine.x;
1567 ptHead.y = line.ptLine.y + line.fLineAscent;
1568 ptFoot.x = line.ptLine.x;
1569 ptFoot.y = line.ptLine.y + line.fLineDescent;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001570 }
1571
tsepez4cf55152016-11-02 14:37:54 -07001572 m_bNotifyFlag = true;
Tom Sepez52f69b32017-03-21 13:42:38 -07001573 m_pNotify->IOnSetCaret(m_SelState.IsEmpty(), VTToEdit(ptHead),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001574 VTToEdit(ptFoot), m_wpCaret);
tsepez4cf55152016-11-02 14:37:54 -07001575 m_bNotifyFlag = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001576 }
1577 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001578}
1579
1580void CFX_Edit::SetCaret(int32_t nPos) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001581 if (!m_pVT->IsValid())
1582 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001583
Tom Sepez52f69b32017-03-21 13:42:38 -07001584 SelectNone();
1585 SetCaret(m_pVT->WordIndexToWordPlace(nPos));
1586 m_SelState.Set(m_wpCaret, m_wpCaret);
1587 ScrollToCaret();
1588 SetCaretOrigin();
1589 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001590}
1591
Dan Sinclairf528eee2017-02-14 11:52:07 -05001592void CFX_Edit::OnMouseDown(const CFX_PointF& point, bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001593 if (!m_pVT->IsValid())
1594 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001595
Tom Sepez52f69b32017-03-21 13:42:38 -07001596 SelectNone();
1597 SetCaret(m_pVT->SearchWordPlace(EditToVT(point)));
1598 m_SelState.Set(m_wpCaret, m_wpCaret);
1599 ScrollToCaret();
1600 SetCaretOrigin();
1601 SetCaretInfo();
1602}
1603
1604void CFX_Edit::OnMouseMove(const CFX_PointF& point, bool bShift, bool bCtrl) {
1605 if (!m_pVT->IsValid())
1606 return;
1607
1608 SetCaret(m_pVT->SearchWordPlace(EditToVT(point)));
1609 if (m_wpCaret == m_wpOldCaret)
1610 return;
1611
1612 m_SelState.SetEndPos(m_wpCaret);
1613 ScrollToCaret();
1614 Refresh();
1615 SetCaretOrigin();
1616 SetCaretInfo();
1617}
1618
1619void CFX_Edit::OnVK_UP(bool bShift, bool bCtrl) {
1620 if (!m_pVT->IsValid())
1621 return;
1622
1623 SetCaret(m_pVT->GetUpWordPlace(m_wpCaret, m_ptCaret));
1624 if (bShift) {
1625 if (m_SelState.IsEmpty())
1626 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1627 else
1628 m_SelState.SetEndPos(m_wpCaret);
1629
1630 if (m_wpOldCaret != m_wpCaret) {
1631 ScrollToCaret();
1632 Refresh();
1633 SetCaretInfo();
1634 }
1635 } else {
1636 SelectNone();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001637 ScrollToCaret();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001638 SetCaretInfo();
1639 }
1640}
1641
Tom Sepez52f69b32017-03-21 13:42:38 -07001642void CFX_Edit::OnVK_DOWN(bool bShift, bool bCtrl) {
1643 if (!m_pVT->IsValid())
1644 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001645
Tom Sepez52f69b32017-03-21 13:42:38 -07001646 SetCaret(m_pVT->GetDownWordPlace(m_wpCaret, m_ptCaret));
1647 if (bShift) {
1648 if (m_SelState.IsEmpty())
1649 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1650 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001651 m_SelState.SetEndPos(m_wpCaret);
1652
Tom Sepez52f69b32017-03-21 13:42:38 -07001653 if (m_wpOldCaret != m_wpCaret) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001654 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001655 Refresh();
Tom Sepez52f69b32017-03-21 13:42:38 -07001656 SetCaretInfo();
1657 }
1658 } else {
1659 SelectNone();
1660 ScrollToCaret();
1661 SetCaretInfo();
1662 }
1663}
1664
1665void CFX_Edit::OnVK_LEFT(bool bShift, bool bCtrl) {
1666 if (!m_pVT->IsValid())
1667 return;
1668
1669 if (bShift) {
1670 if (m_wpCaret == m_pVT->GetLineBeginPlace(m_wpCaret) &&
1671 m_wpCaret != m_pVT->GetSectionBeginPlace(m_wpCaret)) {
1672 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1673 }
1674 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1675 if (m_SelState.IsEmpty())
1676 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1677 else
1678 m_SelState.SetEndPos(m_wpCaret);
1679
1680 if (m_wpOldCaret != m_wpCaret) {
1681 ScrollToCaret();
1682 Refresh();
1683 SetCaretInfo();
1684 }
1685 } else {
1686 if (!m_SelState.IsEmpty()) {
1687 if (m_SelState.BeginPos < m_SelState.EndPos)
1688 SetCaret(m_SelState.BeginPos);
1689 else
1690 SetCaret(m_SelState.EndPos);
1691
1692 SelectNone();
1693 ScrollToCaret();
1694 SetCaretInfo();
1695 } else {
1696 if (m_wpCaret == m_pVT->GetLineBeginPlace(m_wpCaret) &&
1697 m_wpCaret != m_pVT->GetSectionBeginPlace(m_wpCaret)) {
1698 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1699 }
1700 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1701 ScrollToCaret();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001702 SetCaretOrigin();
1703 SetCaretInfo();
1704 }
1705 }
1706}
1707
tsepez4cf55152016-11-02 14:37:54 -07001708void CFX_Edit::OnVK_RIGHT(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001709 if (!m_pVT->IsValid())
1710 return;
1711
1712 if (bShift) {
1713 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1714 if (m_wpCaret == m_pVT->GetLineEndPlace(m_wpCaret) &&
1715 m_wpCaret != m_pVT->GetSectionEndPlace(m_wpCaret))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001716 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1717
Tom Sepez52f69b32017-03-21 13:42:38 -07001718 if (m_SelState.IsEmpty())
1719 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1720 else
1721 m_SelState.SetEndPos(m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001722
Tom Sepez52f69b32017-03-21 13:42:38 -07001723 if (m_wpOldCaret != m_wpCaret) {
1724 ScrollToCaret();
1725 Refresh();
1726 SetCaretInfo();
1727 }
1728 } else {
1729 if (!m_SelState.IsEmpty()) {
1730 if (m_SelState.BeginPos > m_SelState.EndPos)
1731 SetCaret(m_SelState.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001732 else
Tom Sepez52f69b32017-03-21 13:42:38 -07001733 SetCaret(m_SelState.EndPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001734
Tom Sepez52f69b32017-03-21 13:42:38 -07001735 SelectNone();
1736 ScrollToCaret();
1737 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001738 } else {
Tom Sepez52f69b32017-03-21 13:42:38 -07001739 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1740 if (m_wpCaret == m_pVT->GetLineEndPlace(m_wpCaret) &&
1741 m_wpCaret != m_pVT->GetSectionEndPlace(m_wpCaret)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001742 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001743 }
Tom Sepez52f69b32017-03-21 13:42:38 -07001744 ScrollToCaret();
1745 SetCaretOrigin();
1746 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001747 }
1748 }
1749}
1750
tsepez4cf55152016-11-02 14:37:54 -07001751void CFX_Edit::OnVK_HOME(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001752 if (!m_pVT->IsValid())
1753 return;
1754
1755 if (bShift) {
1756 if (bCtrl)
1757 SetCaret(m_pVT->GetBeginWordPlace());
1758 else
1759 SetCaret(m_pVT->GetLineBeginPlace(m_wpCaret));
1760
1761 if (m_SelState.IsEmpty())
1762 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1763 else
1764 m_SelState.SetEndPos(m_wpCaret);
1765
1766 ScrollToCaret();
1767 Refresh();
1768 SetCaretInfo();
1769 } else {
1770 if (!m_SelState.IsEmpty()) {
1771 SetCaret(std::min(m_SelState.BeginPos, m_SelState.EndPos));
1772 SelectNone();
1773 ScrollToCaret();
1774 SetCaretInfo();
1775 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001776 if (bCtrl)
1777 SetCaret(m_pVT->GetBeginWordPlace());
1778 else
1779 SetCaret(m_pVT->GetLineBeginPlace(m_wpCaret));
1780
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001781 ScrollToCaret();
Tom Sepez52f69b32017-03-21 13:42:38 -07001782 SetCaretOrigin();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001783 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001784 }
1785 }
1786}
1787
tsepez4cf55152016-11-02 14:37:54 -07001788void CFX_Edit::OnVK_END(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001789 if (!m_pVT->IsValid())
1790 return;
1791
1792 if (bShift) {
1793 if (bCtrl)
1794 SetCaret(m_pVT->GetEndWordPlace());
1795 else
1796 SetCaret(m_pVT->GetLineEndPlace(m_wpCaret));
1797
1798 if (m_SelState.IsEmpty())
1799 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1800 else
1801 m_SelState.SetEndPos(m_wpCaret);
1802
1803 ScrollToCaret();
1804 Refresh();
1805 SetCaretInfo();
1806 } else {
1807 if (!m_SelState.IsEmpty()) {
1808 SetCaret(std::max(m_SelState.BeginPos, m_SelState.EndPos));
1809 SelectNone();
1810 ScrollToCaret();
1811 SetCaretInfo();
1812 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001813 if (bCtrl)
1814 SetCaret(m_pVT->GetEndWordPlace());
1815 else
1816 SetCaret(m_pVT->GetLineEndPlace(m_wpCaret));
1817
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001818 ScrollToCaret();
Tom Sepez52f69b32017-03-21 13:42:38 -07001819 SetCaretOrigin();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001820 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001821 }
1822 }
1823}
1824
tsepez4cf55152016-11-02 14:37:54 -07001825bool CFX_Edit::InsertWord(uint16_t word,
1826 int32_t charset,
1827 const CPVT_WordProps* pWordProps,
1828 bool bAddUndo,
1829 bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001830 if (IsTextOverflow() || !m_pVT->IsValid())
tsepez4cf55152016-11-02 14:37:54 -07001831 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001832
Tom Sepez3509d162017-01-30 13:22:02 -08001833 m_pVT->UpdateWordPlace(m_wpCaret);
1834 SetCaret(m_pVT->InsertWord(m_wpCaret, word,
1835 GetCharSetFromUnicode(word, charset), pWordProps));
1836 m_SelState.Set(m_wpCaret, m_wpCaret);
1837 if (m_wpCaret == m_wpOldCaret)
1838 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001839
Tom Sepez3509d162017-01-30 13:22:02 -08001840 if (bAddUndo && m_bEnableUndo) {
1841 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertWord>(
1842 this, m_wpOldCaret, m_wpCaret, word, charset, pWordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001843 }
Tom Sepez3509d162017-01-30 13:22:02 -08001844 if (bPaint)
1845 PaintInsertText(m_wpOldCaret, m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001846
Tom Sepez3509d162017-01-30 13:22:02 -08001847 if (m_bOprNotify && m_pOprNotify)
1848 m_pOprNotify->OnInsertWord(m_wpCaret, m_wpOldCaret);
1849
1850 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001851}
1852
tsepez4cf55152016-11-02 14:37:54 -07001853bool CFX_Edit::InsertReturn(const CPVT_SecProps* pSecProps,
1854 const CPVT_WordProps* pWordProps,
1855 bool bAddUndo,
1856 bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001857 if (IsTextOverflow() || !m_pVT->IsValid())
tsepez4cf55152016-11-02 14:37:54 -07001858 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001859
Tom Sepez3509d162017-01-30 13:22:02 -08001860 m_pVT->UpdateWordPlace(m_wpCaret);
1861 SetCaret(m_pVT->InsertSection(m_wpCaret, pSecProps, pWordProps));
1862 m_SelState.Set(m_wpCaret, m_wpCaret);
1863 if (m_wpCaret == m_wpOldCaret)
1864 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001865
Tom Sepez3509d162017-01-30 13:22:02 -08001866 if (bAddUndo && m_bEnableUndo) {
1867 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertReturn>(
1868 this, m_wpOldCaret, m_wpCaret, pSecProps, pWordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001869 }
Tom Sepez3509d162017-01-30 13:22:02 -08001870 if (bPaint) {
1871 RearrangePart(CPVT_WordRange(m_wpOldCaret, m_wpCaret));
1872 ScrollToCaret();
1873 Refresh();
1874 SetCaretOrigin();
1875 SetCaretInfo();
1876 }
1877 if (m_bOprNotify && m_pOprNotify)
1878 m_pOprNotify->OnInsertReturn(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001879
Tom Sepez3509d162017-01-30 13:22:02 -08001880 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001881}
1882
tsepez4cf55152016-11-02 14:37:54 -07001883bool CFX_Edit::Backspace(bool bAddUndo, bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001884 if (!m_pVT->IsValid() || m_wpCaret == m_pVT->GetBeginWordPlace())
1885 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001886
Tom Sepez3509d162017-01-30 13:22:02 -08001887 CPVT_Section section;
1888 CPVT_Word word;
1889 if (bAddUndo) {
1890 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1891 pIterator->SetAt(m_wpCaret);
1892 pIterator->GetSection(section);
1893 pIterator->GetWord(word);
1894 }
1895 m_pVT->UpdateWordPlace(m_wpCaret);
1896 SetCaret(m_pVT->BackSpaceWord(m_wpCaret));
1897 m_SelState.Set(m_wpCaret, m_wpCaret);
1898 if (m_wpCaret == m_wpOldCaret)
1899 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001900
Tom Sepez3509d162017-01-30 13:22:02 -08001901 if (bAddUndo && m_bEnableUndo) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001902 if (m_wpCaret.nSecIndex != m_wpOldCaret.nSecIndex) {
Tom Sepez3509d162017-01-30 13:22:02 -08001903 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Backspace>(
1904 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1905 section.SecProps, section.WordProps));
1906 } else {
1907 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Backspace>(
1908 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1909 section.SecProps, word.WordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001910 }
1911 }
Tom Sepez3509d162017-01-30 13:22:02 -08001912 if (bPaint) {
1913 RearrangePart(CPVT_WordRange(m_wpCaret, m_wpOldCaret));
1914 ScrollToCaret();
1915 Refresh();
1916 SetCaretOrigin();
1917 SetCaretInfo();
1918 }
1919 if (m_bOprNotify && m_pOprNotify)
1920 m_pOprNotify->OnBackSpace(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001921
Tom Sepez3509d162017-01-30 13:22:02 -08001922 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001923}
1924
tsepez4cf55152016-11-02 14:37:54 -07001925bool CFX_Edit::Delete(bool bAddUndo, bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001926 if (!m_pVT->IsValid() || m_wpCaret == m_pVT->GetEndWordPlace())
1927 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001928
Tom Sepez3509d162017-01-30 13:22:02 -08001929 CPVT_Section section;
1930 CPVT_Word word;
1931 if (bAddUndo) {
1932 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1933 pIterator->SetAt(m_pVT->GetNextWordPlace(m_wpCaret));
1934 pIterator->GetSection(section);
1935 pIterator->GetWord(word);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001936 }
Tom Sepez3509d162017-01-30 13:22:02 -08001937 m_pVT->UpdateWordPlace(m_wpCaret);
1938 bool bSecEnd = (m_wpCaret == m_pVT->GetSectionEndPlace(m_wpCaret));
1939 SetCaret(m_pVT->DeleteWord(m_wpCaret));
1940 m_SelState.Set(m_wpCaret, m_wpCaret);
1941 if (bAddUndo && m_bEnableUndo) {
1942 if (bSecEnd) {
1943 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Delete>(
1944 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1945 section.SecProps, section.WordProps, bSecEnd));
1946 } else {
1947 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Delete>(
1948 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1949 section.SecProps, word.WordProps, bSecEnd));
1950 }
1951 }
1952 if (bPaint) {
1953 RearrangePart(CPVT_WordRange(m_wpOldCaret, m_wpCaret));
1954 ScrollToCaret();
1955 Refresh();
1956 SetCaretOrigin();
1957 SetCaretInfo();
1958 }
1959 if (m_bOprNotify && m_pOprNotify)
1960 m_pOprNotify->OnDelete(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001961
Tom Sepez3509d162017-01-30 13:22:02 -08001962 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001963}
1964
tsepez4cf55152016-11-02 14:37:54 -07001965bool CFX_Edit::Empty() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001966 if (m_pVT->IsValid()) {
1967 m_pVT->DeleteWords(GetWholeWordRange());
1968 SetCaret(m_pVT->GetBeginWordPlace());
1969
tsepez4cf55152016-11-02 14:37:54 -07001970 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001971 }
1972
tsepez4cf55152016-11-02 14:37:54 -07001973 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001974}
1975
tsepez4cf55152016-11-02 14:37:54 -07001976bool CFX_Edit::Clear(bool bAddUndo, bool bPaint) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001977 if (!m_pVT->IsValid() || m_SelState.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001978 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001979
thestig821d59e2016-05-11 12:59:22 -07001980 CPVT_WordRange range = m_SelState.ConvertToWordRange();
dsinclaira2919b32016-07-13 10:55:48 -07001981 if (bAddUndo && m_bEnableUndo)
Tom Sepez3509d162017-01-30 13:22:02 -08001982 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Clear>(this, range, GetSelText()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001983
thestig821d59e2016-05-11 12:59:22 -07001984 SelectNone();
1985 SetCaret(m_pVT->DeleteWords(range));
1986 m_SelState.Set(m_wpCaret, m_wpCaret);
thestig821d59e2016-05-11 12:59:22 -07001987 if (bPaint) {
1988 RearrangePart(range);
1989 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001990 Refresh();
thestig821d59e2016-05-11 12:59:22 -07001991 SetCaretOrigin();
1992 SetCaretInfo();
1993 }
thestig821d59e2016-05-11 12:59:22 -07001994 if (m_bOprNotify && m_pOprNotify)
1995 m_pOprNotify->OnClear(m_wpCaret, m_wpOldCaret);
1996
tsepez4cf55152016-11-02 14:37:54 -07001997 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001998}
1999
tsepez4cf55152016-11-02 14:37:54 -07002000bool CFX_Edit::InsertText(const CFX_WideString& sText,
2001 int32_t charset,
2002 bool bAddUndo,
2003 bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002004 if (IsTextOverflow())
tsepez4cf55152016-11-02 14:37:54 -07002005 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002006
2007 m_pVT->UpdateWordPlace(m_wpCaret);
tsepeza31da742016-09-08 11:28:14 -07002008 SetCaret(DoInsertText(m_wpCaret, sText, charset));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002009 m_SelState.Set(m_wpCaret, m_wpCaret);
tsepeza31da742016-09-08 11:28:14 -07002010 if (m_wpCaret == m_wpOldCaret)
tsepez4cf55152016-11-02 14:37:54 -07002011 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002012
tsepeza31da742016-09-08 11:28:14 -07002013 if (bAddUndo && m_bEnableUndo) {
Tom Sepez3509d162017-01-30 13:22:02 -08002014 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertText>(
2015 this, m_wpOldCaret, m_wpCaret, sText, charset));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002016 }
tsepeza31da742016-09-08 11:28:14 -07002017 if (bPaint)
2018 PaintInsertText(m_wpOldCaret, m_wpCaret);
2019
2020 if (m_bOprNotify && m_pOprNotify)
2021 m_pOprNotify->OnInsertText(m_wpCaret, m_wpOldCaret);
2022
tsepez4cf55152016-11-02 14:37:54 -07002023 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002024}
2025
2026void CFX_Edit::PaintInsertText(const CPVT_WordPlace& wpOld,
2027 const CPVT_WordPlace& wpNew) {
2028 if (m_pVT->IsValid()) {
2029 RearrangePart(CPVT_WordRange(wpOld, wpNew));
2030 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07002031 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002032 SetCaretOrigin();
2033 SetCaretInfo();
2034 }
2035}
2036
tsepez4cf55152016-11-02 14:37:54 -07002037bool CFX_Edit::Redo() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002038 if (m_bEnableUndo) {
2039 if (m_Undo.CanRedo()) {
2040 m_Undo.Redo();
tsepez4cf55152016-11-02 14:37:54 -07002041 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002042 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002043 }
2044
tsepez4cf55152016-11-02 14:37:54 -07002045 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002046}
2047
tsepez4cf55152016-11-02 14:37:54 -07002048bool CFX_Edit::Undo() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002049 if (m_bEnableUndo) {
2050 if (m_Undo.CanUndo()) {
2051 m_Undo.Undo();
tsepez4cf55152016-11-02 14:37:54 -07002052 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002053 }
2054 }
2055
tsepez4cf55152016-11-02 14:37:54 -07002056 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002057}
2058
2059void CFX_Edit::SetCaretOrigin() {
thestig821d59e2016-05-11 12:59:22 -07002060 if (!m_pVT->IsValid())
2061 return;
2062
2063 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
2064 pIterator->SetAt(m_wpCaret);
2065 CPVT_Word word;
2066 CPVT_Line line;
2067 if (pIterator->GetWord(word)) {
2068 m_ptCaret.x = word.ptWord.x + word.fWidth;
2069 m_ptCaret.y = word.ptWord.y;
2070 } else if (pIterator->GetLine(line)) {
2071 m_ptCaret.x = line.ptLine.x;
2072 m_ptCaret.y = line.ptLine.y;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002073 }
2074}
2075
2076int32_t CFX_Edit::WordPlaceToWordIndex(const CPVT_WordPlace& place) const {
2077 if (m_pVT->IsValid())
2078 return m_pVT->WordPlaceToWordIndex(place);
2079
2080 return -1;
2081}
2082
2083CPVT_WordPlace CFX_Edit::WordIndexToWordPlace(int32_t index) const {
2084 if (m_pVT->IsValid())
2085 return m_pVT->WordIndexToWordPlace(index);
2086
2087 return CPVT_WordPlace();
2088}
2089
tsepez4cf55152016-11-02 14:37:54 -07002090bool CFX_Edit::IsTextFull() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002091 int32_t nTotalWords = m_pVT->GetTotalWords();
2092 int32_t nLimitChar = m_pVT->GetLimitChar();
2093 int32_t nCharArray = m_pVT->GetCharArray();
2094
2095 return IsTextOverflow() || (nLimitChar > 0 && nTotalWords >= nLimitChar) ||
2096 (nCharArray > 0 && nTotalWords >= nCharArray);
2097}
2098
tsepez4cf55152016-11-02 14:37:54 -07002099bool CFX_Edit::IsTextOverflow() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002100 if (!m_bEnableScroll && !m_bEnableOverflow) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08002101 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
2102 CFX_FloatRect rcContent = m_pVT->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002103
dsinclair448c4332016-08-02 12:07:35 -07002104 if (m_pVT->IsMultiLine() && GetTotalLines() > 1 &&
2105 IsFloatBigger(rcContent.Height(), rcPlate.Height())) {
tsepez4cf55152016-11-02 14:37:54 -07002106 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002107 }
2108
dsinclair448c4332016-08-02 12:07:35 -07002109 if (IsFloatBigger(rcContent.Width(), rcPlate.Width()))
tsepez4cf55152016-11-02 14:37:54 -07002110 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002111 }
2112
tsepez4cf55152016-11-02 14:37:54 -07002113 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002114}
2115
tsepez4cf55152016-11-02 14:37:54 -07002116bool CFX_Edit::CanUndo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002117 if (m_bEnableUndo) {
2118 return m_Undo.CanUndo();
2119 }
2120
tsepez4cf55152016-11-02 14:37:54 -07002121 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002122}
2123
tsepez4cf55152016-11-02 14:37:54 -07002124bool CFX_Edit::CanRedo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002125 if (m_bEnableUndo) {
2126 return m_Undo.CanRedo();
2127 }
2128
tsepez4cf55152016-11-02 14:37:54 -07002129 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002130}
2131
tsepez4cf55152016-11-02 14:37:54 -07002132void CFX_Edit::EnableRefresh(bool bRefresh) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002133 m_bEnableRefresh = bRefresh;
2134}
2135
tsepez4cf55152016-11-02 14:37:54 -07002136void CFX_Edit::EnableUndo(bool bUndo) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002137 m_bEnableUndo = bUndo;
2138}
2139
tsepez4cf55152016-11-02 14:37:54 -07002140void CFX_Edit::EnableOprNotify(bool bNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002141 m_bOprNotify = bNotify;
2142}
2143
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002144CPVT_WordPlace CFX_Edit::DoInsertText(const CPVT_WordPlace& place,
tsepeza31da742016-09-08 11:28:14 -07002145 const CFX_WideString& sText,
dsinclairefd5a992016-07-18 10:04:07 -07002146 int32_t charset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002147 CPVT_WordPlace wp = place;
2148
2149 if (m_pVT->IsValid()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002150 for (int32_t i = 0, sz = sText.GetLength(); i < sz; i++) {
Tom Sepez62a70f92016-03-21 15:00:20 -07002151 uint16_t word = sText[i];
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002152 switch (word) {
2153 case 0x0D:
dsinclairefd5a992016-07-18 10:04:07 -07002154 wp = m_pVT->InsertSection(wp, nullptr, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002155 if (sText[i + 1] == 0x0A)
2156 i++;
2157 break;
2158 case 0x0A:
dsinclairefd5a992016-07-18 10:04:07 -07002159 wp = m_pVT->InsertSection(wp, nullptr, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002160 if (sText[i + 1] == 0x0D)
2161 i++;
2162 break;
2163 case 0x09:
2164 word = 0x20;
2165 default:
2166 wp = m_pVT->InsertWord(wp, word, GetCharSetFromUnicode(word, charset),
dsinclairefd5a992016-07-18 10:04:07 -07002167 nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002168 break;
2169 }
2170 }
2171 }
2172
2173 return wp;
2174}
2175
Tom Sepez62a70f92016-03-21 15:00:20 -07002176int32_t CFX_Edit::GetCharSetFromUnicode(uint16_t word, int32_t nOldCharset) {
dsinclairc7a73492016-04-05 12:01:42 -07002177 if (IPVT_FontMap* pFontMap = GetFontMap())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002178 return pFontMap->CharSetFromUnicode(word, nOldCharset);
2179 return nOldCharset;
2180}
2181
Tom Sepez3509d162017-01-30 13:22:02 -08002182void CFX_Edit::AddEditUndoItem(
2183 std::unique_ptr<CFX_Edit_UndoItem> pEditUndoItem) {
2184 if (m_pGroupUndoItem)
2185 m_pGroupUndoItem->AddUndoItem(std::move(pEditUndoItem));
2186 else
2187 m_Undo.AddItem(std::move(pEditUndoItem));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002188}
2189
weili625ad662016-06-15 11:21:33 -07002190CFX_Edit_LineRectArray::CFX_Edit_LineRectArray() {}
2191
Tom Sepez3509d162017-01-30 13:22:02 -08002192CFX_Edit_LineRectArray::~CFX_Edit_LineRectArray() {}
weili625ad662016-06-15 11:21:33 -07002193
Tom Sepez3509d162017-01-30 13:22:02 -08002194void CFX_Edit_LineRectArray::operator=(CFX_Edit_LineRectArray&& that) {
2195 m_LineRects = std::move(that.m_LineRects);
weili625ad662016-06-15 11:21:33 -07002196}
2197
2198void CFX_Edit_LineRectArray::Add(const CPVT_WordRange& wrLine,
2199 const CFX_FloatRect& rcLine) {
Tom Sepez3509d162017-01-30 13:22:02 -08002200 m_LineRects.push_back(pdfium::MakeUnique<CFX_Edit_LineRect>(wrLine, rcLine));
weili625ad662016-06-15 11:21:33 -07002201}
2202
2203int32_t CFX_Edit_LineRectArray::GetSize() const {
Tom Sepez3509d162017-01-30 13:22:02 -08002204 return pdfium::CollectionSize<int32_t>(m_LineRects);
weili625ad662016-06-15 11:21:33 -07002205}
2206
2207CFX_Edit_LineRect* CFX_Edit_LineRectArray::GetAt(int32_t nIndex) const {
Tom Sepez3509d162017-01-30 13:22:02 -08002208 if (nIndex < 0 || nIndex >= GetSize())
weili625ad662016-06-15 11:21:33 -07002209 return nullptr;
2210
Tom Sepez3509d162017-01-30 13:22:02 -08002211 return m_LineRects[nIndex].get();
weili625ad662016-06-15 11:21:33 -07002212}
2213
2214CFX_Edit_Select::CFX_Edit_Select() {}
2215
2216CFX_Edit_Select::CFX_Edit_Select(const CPVT_WordPlace& begin,
2217 const CPVT_WordPlace& end) {
2218 Set(begin, end);
2219}
2220
2221CFX_Edit_Select::CFX_Edit_Select(const CPVT_WordRange& range) {
2222 Set(range.BeginPos, range.EndPos);
2223}
2224
2225CPVT_WordRange CFX_Edit_Select::ConvertToWordRange() const {
2226 return CPVT_WordRange(BeginPos, EndPos);
2227}
2228
Tom Sepez52f69b32017-03-21 13:42:38 -07002229void CFX_Edit_Select::Reset() {
2230 BeginPos.Reset();
2231 EndPos.Reset();
weili625ad662016-06-15 11:21:33 -07002232}
2233
2234void CFX_Edit_Select::Set(const CPVT_WordPlace& begin,
2235 const CPVT_WordPlace& end) {
2236 BeginPos = begin;
2237 EndPos = end;
2238}
2239
2240void CFX_Edit_Select::SetBeginPos(const CPVT_WordPlace& begin) {
2241 BeginPos = begin;
2242}
2243
2244void CFX_Edit_Select::SetEndPos(const CPVT_WordPlace& end) {
2245 EndPos = end;
2246}
2247
Tom Sepez52f69b32017-03-21 13:42:38 -07002248bool CFX_Edit_Select::IsEmpty() const {
2249 return BeginPos == EndPos;
weili625ad662016-06-15 11:21:33 -07002250}
2251
weili625ad662016-06-15 11:21:33 -07002252CFX_Edit_RectArray::CFX_Edit_RectArray() {}
2253
Tom Sepez3509d162017-01-30 13:22:02 -08002254CFX_Edit_RectArray::~CFX_Edit_RectArray() {}
weili625ad662016-06-15 11:21:33 -07002255
Tom Sepez3509d162017-01-30 13:22:02 -08002256void CFX_Edit_RectArray::Clear() {
2257 m_Rects.clear();
weili625ad662016-06-15 11:21:33 -07002258}
2259
2260void CFX_Edit_RectArray::Add(const CFX_FloatRect& rect) {
2261 // check for overlapped area
Tom Sepez3509d162017-01-30 13:22:02 -08002262 for (const auto& pRect : m_Rects) {
weili625ad662016-06-15 11:21:33 -07002263 if (pRect && pRect->Contains(rect))
2264 return;
2265 }
Tom Sepez3509d162017-01-30 13:22:02 -08002266 m_Rects.push_back(pdfium::MakeUnique<CFX_FloatRect>(rect));
weili625ad662016-06-15 11:21:33 -07002267}
2268
2269int32_t CFX_Edit_RectArray::GetSize() const {
Tom Sepez3509d162017-01-30 13:22:02 -08002270 return pdfium::CollectionSize<int32_t>(m_Rects);
weili625ad662016-06-15 11:21:33 -07002271}
2272
2273CFX_FloatRect* CFX_Edit_RectArray::GetAt(int32_t nIndex) const {
Tom Sepez3509d162017-01-30 13:22:02 -08002274 if (nIndex < 0 || nIndex >= GetSize())
weili625ad662016-06-15 11:21:33 -07002275 return nullptr;
2276
Tom Sepez3509d162017-01-30 13:22:02 -08002277 return m_Rects[nIndex].get();
weili625ad662016-06-15 11:21:33 -07002278}