blob: 1acc57795a06a0d9b429c52f1107663a8d2e81ba [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,
46 FX_FLOAT fFontSize) {
47 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,
62 FX_FLOAT fFontSize,
63 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() {
462 if (m_pEdit) {
463 m_pEdit->SelectNone();
464 m_pEdit->SetCaret(m_wpOld);
tsepez4cf55152016-11-02 14:37:54 -0700465 m_pEdit->Backspace(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700466 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700467}
468
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700469void CFXEU_Backspace::Undo() {
470 if (m_pEdit) {
471 m_pEdit->SelectNone();
472 m_pEdit->SetCaret(m_wpNew);
473 if (m_wpNew.SecCmp(m_wpOld) != 0) {
tsepez4cf55152016-11-02 14:37:54 -0700474 m_pEdit->InsertReturn(&m_SecProps, &m_WordProps, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700475 } else {
tsepez4cf55152016-11-02 14:37:54 -0700476 m_pEdit->InsertWord(m_Word, m_nCharset, &m_WordProps, false, true);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700477 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700478 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700479}
480
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700481CFXEU_Delete::CFXEU_Delete(CFX_Edit* pEdit,
482 const CPVT_WordPlace& wpOldPlace,
483 const CPVT_WordPlace& wpNewPlace,
Tom Sepez62a70f92016-03-21 15:00:20 -0700484 uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700485 int32_t charset,
486 const CPVT_SecProps& SecProps,
487 const CPVT_WordProps& WordProps,
tsepez4cf55152016-11-02 14:37:54 -0700488 bool bSecEnd)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700489 : m_pEdit(pEdit),
490 m_wpOld(wpOldPlace),
491 m_wpNew(wpNewPlace),
492 m_Word(word),
493 m_nCharset(charset),
494 m_SecProps(SecProps),
495 m_WordProps(WordProps),
496 m_bSecEnd(bSecEnd) {}
497
498CFXEU_Delete::~CFXEU_Delete() {}
499
500void CFXEU_Delete::Redo() {
501 if (m_pEdit) {
502 m_pEdit->SelectNone();
503 m_pEdit->SetCaret(m_wpOld);
tsepez4cf55152016-11-02 14:37:54 -0700504 m_pEdit->Delete(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700505 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700506}
507
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700508void CFXEU_Delete::Undo() {
509 if (m_pEdit) {
510 m_pEdit->SelectNone();
511 m_pEdit->SetCaret(m_wpNew);
512 if (m_bSecEnd) {
tsepez4cf55152016-11-02 14:37:54 -0700513 m_pEdit->InsertReturn(&m_SecProps, &m_WordProps, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700514 } else {
tsepez4cf55152016-11-02 14:37:54 -0700515 m_pEdit->InsertWord(m_Word, m_nCharset, &m_WordProps, false, true);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700516 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700517 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700518}
519
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700520CFXEU_Clear::CFXEU_Clear(CFX_Edit* pEdit,
521 const CPVT_WordRange& wrSel,
522 const CFX_WideString& swText)
523 : m_pEdit(pEdit), m_wrSel(wrSel), m_swText(swText) {}
524
525CFXEU_Clear::~CFXEU_Clear() {}
526
527void CFXEU_Clear::Redo() {
528 if (m_pEdit) {
529 m_pEdit->SelectNone();
530 m_pEdit->SetSel(m_wrSel.BeginPos, m_wrSel.EndPos);
tsepez4cf55152016-11-02 14:37:54 -0700531 m_pEdit->Clear(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700532 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700533}
534
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700535void CFXEU_Clear::Undo() {
536 if (m_pEdit) {
537 m_pEdit->SelectNone();
538 m_pEdit->SetCaret(m_wrSel.BeginPos);
tsepez4cf55152016-11-02 14:37:54 -0700539 m_pEdit->InsertText(m_swText, FXFONT_DEFAULT_CHARSET, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700540 m_pEdit->SetSel(m_wrSel.BeginPos, m_wrSel.EndPos);
541 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700542}
543
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700544CFXEU_InsertText::CFXEU_InsertText(CFX_Edit* pEdit,
545 const CPVT_WordPlace& wpOldPlace,
546 const CPVT_WordPlace& wpNewPlace,
547 const CFX_WideString& swText,
dsinclairefd5a992016-07-18 10:04:07 -0700548 int32_t charset)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700549 : m_pEdit(pEdit),
550 m_wpOld(wpOldPlace),
551 m_wpNew(wpNewPlace),
552 m_swText(swText),
dsinclairefd5a992016-07-18 10:04:07 -0700553 m_nCharset(charset) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700554
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700555CFXEU_InsertText::~CFXEU_InsertText() {}
556
557void CFXEU_InsertText::Redo() {
558 if (m_pEdit && IsLast()) {
559 m_pEdit->SelectNone();
560 m_pEdit->SetCaret(m_wpOld);
tsepez4cf55152016-11-02 14:37:54 -0700561 m_pEdit->InsertText(m_swText, m_nCharset, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700562 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700563}
564
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700565void CFXEU_InsertText::Undo() {
566 if (m_pEdit) {
567 m_pEdit->SelectNone();
568 m_pEdit->SetSel(m_wpOld, m_wpNew);
tsepez4cf55152016-11-02 14:37:54 -0700569 m_pEdit->Clear(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700570 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700571}
572
dsinclaire35af1e2016-07-13 11:26:20 -0700573// static
574CFX_ByteString CFX_Edit::GetEditAppearanceStream(CFX_Edit* pEdit,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500575 const CFX_PointF& ptOffset,
dsinclaire35af1e2016-07-13 11:26:20 -0700576 const CPVT_WordRange* pRange,
tsepez4cf55152016-11-02 14:37:54 -0700577 bool bContinuous,
dsinclaire35af1e2016-07-13 11:26:20 -0700578 uint16_t SubWord) {
579 CFX_Edit_Iterator* pIterator = pEdit->GetIterator();
580 if (pRange)
581 pIterator->SetAt(pRange->BeginPos);
582 else
583 pIterator->SetAt(0);
584
585 CFX_ByteTextBuf sEditStream;
586 CFX_ByteTextBuf sWords;
587 int32_t nCurFontIndex = -1;
Dan Sinclairf528eee2017-02-14 11:52:07 -0500588 CFX_PointF ptOld;
589 CFX_PointF ptNew;
dsinclaire35af1e2016-07-13 11:26:20 -0700590 CPVT_WordPlace oldplace;
tsepez63f545c2016-09-13 16:08:49 -0700591
dsinclaire35af1e2016-07-13 11:26:20 -0700592 while (pIterator->NextWord()) {
593 CPVT_WordPlace place = pIterator->GetAt();
dsinclaire35af1e2016-07-13 11:26:20 -0700594 if (pRange && place.WordCmp(pRange->EndPos) > 0)
595 break;
596
597 if (bContinuous) {
598 if (place.LineCmp(oldplace) != 0) {
599 if (sWords.GetSize() > 0) {
600 sEditStream << GetWordRenderString(sWords.MakeString());
601 sWords.Clear();
602 }
603
604 CPVT_Word word;
605 if (pIterator->GetWord(word)) {
Dan Sinclairf528eee2017-02-14 11:52:07 -0500606 ptNew = CFX_PointF(word.ptWord.x + ptOffset.x,
607 word.ptWord.y + ptOffset.y);
dsinclaire35af1e2016-07-13 11:26:20 -0700608 } else {
609 CPVT_Line line;
610 pIterator->GetLine(line);
Dan Sinclairf528eee2017-02-14 11:52:07 -0500611 ptNew = CFX_PointF(line.ptLine.x + ptOffset.x,
612 line.ptLine.y + ptOffset.y);
dsinclaire35af1e2016-07-13 11:26:20 -0700613 }
614
615 if (ptNew.x != ptOld.x || ptNew.y != ptOld.y) {
616 sEditStream << ptNew.x - ptOld.x << " " << ptNew.y - ptOld.y
617 << " Td\n";
618
619 ptOld = ptNew;
620 }
621 }
622
623 CPVT_Word word;
624 if (pIterator->GetWord(word)) {
625 if (word.nFontIndex != nCurFontIndex) {
626 if (sWords.GetSize() > 0) {
627 sEditStream << GetWordRenderString(sWords.MakeString());
628 sWords.Clear();
629 }
630 sEditStream << GetFontSetString(pEdit->GetFontMap(), word.nFontIndex,
631 word.fFontSize);
632 nCurFontIndex = word.nFontIndex;
633 }
634
635 sWords << GetPDFWordString(pEdit->GetFontMap(), nCurFontIndex,
636 word.Word, SubWord);
637 }
638
639 oldplace = place;
640 } else {
641 CPVT_Word word;
642 if (pIterator->GetWord(word)) {
Dan Sinclairf528eee2017-02-14 11:52:07 -0500643 ptNew =
644 CFX_PointF(word.ptWord.x + ptOffset.x, word.ptWord.y + ptOffset.y);
dsinclaire35af1e2016-07-13 11:26:20 -0700645
646 if (ptNew.x != ptOld.x || ptNew.y != ptOld.y) {
647 sEditStream << ptNew.x - ptOld.x << " " << ptNew.y - ptOld.y
648 << " Td\n";
649 ptOld = ptNew;
650 }
651
652 if (word.nFontIndex != nCurFontIndex) {
653 sEditStream << GetFontSetString(pEdit->GetFontMap(), word.nFontIndex,
654 word.fFontSize);
655 nCurFontIndex = word.nFontIndex;
656 }
657
658 sEditStream << GetWordRenderString(GetPDFWordString(
659 pEdit->GetFontMap(), nCurFontIndex, word.Word, SubWord));
660 }
661 }
662 }
663
664 if (sWords.GetSize() > 0) {
665 sEditStream << GetWordRenderString(sWords.MakeString());
666 sWords.Clear();
667 }
668
669 CFX_ByteTextBuf sAppStream;
670 if (sEditStream.GetSize() > 0) {
671 int32_t nHorzScale = pEdit->GetHorzScale();
672 if (nHorzScale != 100) {
673 sAppStream << nHorzScale << " Tz\n";
674 }
675
676 FX_FLOAT fCharSpace = pEdit->GetCharSpace();
dsinclair448c4332016-08-02 12:07:35 -0700677 if (!IsFloatZero(fCharSpace)) {
dsinclaire35af1e2016-07-13 11:26:20 -0700678 sAppStream << fCharSpace << " Tc\n";
679 }
680
681 sAppStream << sEditStream;
682 }
683
684 return sAppStream.MakeString();
685}
686
687// static
688CFX_ByteString CFX_Edit::GetSelectAppearanceStream(
689 CFX_Edit* pEdit,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500690 const CFX_PointF& ptOffset,
dsinclaire35af1e2016-07-13 11:26:20 -0700691 const CPVT_WordRange* pRange) {
692 if (!pRange || !pRange->IsExist())
693 return CFX_ByteString();
694
695 CFX_Edit_Iterator* pIterator = pEdit->GetIterator();
696 pIterator->SetAt(pRange->BeginPos);
697
698 CFX_ByteTextBuf sRet;
699 while (pIterator->NextWord()) {
700 CPVT_WordPlace place = pIterator->GetAt();
701 if (place.WordCmp(pRange->EndPos) > 0)
702 break;
703
704 CPVT_Word word;
705 CPVT_Line line;
706 if (pIterator->GetWord(word) && pIterator->GetLine(line)) {
707 sRet << word.ptWord.x + ptOffset.x << " "
708 << line.ptLine.y + line.fLineDescent << " " << word.fWidth << " "
709 << line.fLineAscent - line.fLineDescent << " re\nf\n";
710 }
711 }
712
713 return sRet.MakeString();
714}
715
716// static
dsinclaire35af1e2016-07-13 11:26:20 -0700717void CFX_Edit::DrawEdit(CFX_RenderDevice* pDevice,
718 CFX_Matrix* pUser2Device,
719 CFX_Edit* pEdit,
720 FX_COLORREF crTextFill,
dsinclaire35af1e2016-07-13 11:26:20 -0700721 const CFX_FloatRect& rcClip,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500722 const CFX_PointF& ptOffset,
dsinclaire35af1e2016-07-13 11:26:20 -0700723 const CPVT_WordRange* pRange,
724 CFX_SystemHandler* pSystemHandler,
dsinclair8faac622016-09-15 12:41:50 -0700725 CFFL_FormFiller* pFFLData) {
dsinclaire35af1e2016-07-13 11:26:20 -0700726 const bool bContinuous =
727 pEdit->GetCharArray() == 0 && pEdit->GetCharSpace() <= 0.0f;
728 uint16_t SubWord = pEdit->GetPasswordChar();
729 FX_FLOAT fFontSize = pEdit->GetFontSize();
730 CPVT_WordRange wrSelect = pEdit->GetSelectWordRange();
731 int32_t nHorzScale = pEdit->GetHorzScale();
732
733 FX_COLORREF crCurFill = crTextFill;
734 FX_COLORREF crOldFill = crCurFill;
735
tsepez4cf55152016-11-02 14:37:54 -0700736 bool bSelect = false;
dsinclaire35af1e2016-07-13 11:26:20 -0700737 const FX_COLORREF crWhite = ArgbEncode(255, 255, 255, 255);
738 const FX_COLORREF crSelBK = ArgbEncode(255, 0, 51, 113);
739
740 CFX_ByteTextBuf sTextBuf;
741 int32_t nFontIndex = -1;
Dan Sinclairf528eee2017-02-14 11:52:07 -0500742 CFX_PointF ptBT;
dsinclaire35af1e2016-07-13 11:26:20 -0700743 pDevice->SaveState();
dsinclaire35af1e2016-07-13 11:26:20 -0700744 if (!rcClip.IsEmpty()) {
745 CFX_FloatRect rcTemp = rcClip;
746 pUser2Device->TransformRect(rcTemp);
747 pDevice->SetClip_Rect(rcTemp.ToFxRect());
748 }
749
750 CFX_Edit_Iterator* pIterator = pEdit->GetIterator();
751 if (IPVT_FontMap* pFontMap = pEdit->GetFontMap()) {
752 if (pRange)
753 pIterator->SetAt(pRange->BeginPos);
754 else
755 pIterator->SetAt(0);
756
757 CPVT_WordPlace oldplace;
758 while (pIterator->NextWord()) {
759 CPVT_WordPlace place = pIterator->GetAt();
760 if (pRange && place.WordCmp(pRange->EndPos) > 0)
761 break;
762
763 if (wrSelect.IsExist()) {
764 bSelect = place.WordCmp(wrSelect.BeginPos) > 0 &&
765 place.WordCmp(wrSelect.EndPos) <= 0;
766 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
dsinclairefd5a992016-07-18 10:04:07 -0700924void CFX_Edit::SetCharSpace(FX_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
dsinclairefd5a992016-07-18 10:04:07 -0700947void CFX_Edit::SetFontSize(FX_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) {
983 if (m_pVT->IsValid()) {
984 SelectNone();
985
986 m_SelState.Set(begin, end);
987
988 SetCaret(m_SelState.EndPos);
989
990 if (m_SelState.IsExist()) {
991 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -0700992 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700993 SetCaretInfo();
994 } else {
995 ScrollToCaret();
996 SetCaretInfo();
997 }
998 }
999}
1000
1001void CFX_Edit::GetSel(int32_t& nStartChar, int32_t& nEndChar) const {
1002 nStartChar = -1;
1003 nEndChar = -1;
1004
1005 if (m_pVT->IsValid()) {
1006 if (m_SelState.IsExist()) {
1007 if (m_SelState.BeginPos.WordCmp(m_SelState.EndPos) < 0) {
1008 nStartChar = m_pVT->WordPlaceToWordIndex(m_SelState.BeginPos);
1009 nEndChar = m_pVT->WordPlaceToWordIndex(m_SelState.EndPos);
1010 } else {
1011 nStartChar = m_pVT->WordPlaceToWordIndex(m_SelState.EndPos);
1012 nEndChar = m_pVT->WordPlaceToWordIndex(m_SelState.BeginPos);
1013 }
1014 } else {
1015 nStartChar = m_pVT->WordPlaceToWordIndex(m_wpCaret);
1016 nEndChar = m_pVT->WordPlaceToWordIndex(m_wpCaret);
1017 }
1018 }
1019}
1020
1021int32_t CFX_Edit::GetCaret() const {
1022 if (m_pVT->IsValid())
1023 return m_pVT->WordPlaceToWordIndex(m_wpCaret);
1024
1025 return -1;
1026}
1027
1028CPVT_WordPlace CFX_Edit::GetCaretWordPlace() const {
1029 return m_wpCaret;
1030}
1031
1032CFX_WideString CFX_Edit::GetText() const {
1033 CFX_WideString swRet;
1034
thestig821d59e2016-05-11 12:59:22 -07001035 if (!m_pVT->IsValid())
1036 return swRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001037
thestig821d59e2016-05-11 12:59:22 -07001038 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1039 pIterator->SetAt(0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001040
thestig821d59e2016-05-11 12:59:22 -07001041 CPVT_Word wordinfo;
1042 CPVT_WordPlace oldplace = pIterator->GetAt();
1043 while (pIterator->NextWord()) {
1044 CPVT_WordPlace place = pIterator->GetAt();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001045
thestig821d59e2016-05-11 12:59:22 -07001046 if (pIterator->GetWord(wordinfo))
1047 swRet += wordinfo.Word;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001048
thestig821d59e2016-05-11 12:59:22 -07001049 if (oldplace.SecCmp(place) != 0)
1050 swRet += L"\r\n";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001051
thestig821d59e2016-05-11 12:59:22 -07001052 oldplace = place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001053 }
1054
1055 return swRet;
1056}
1057
1058CFX_WideString CFX_Edit::GetRangeText(const CPVT_WordRange& range) const {
1059 CFX_WideString swRet;
1060
thestig821d59e2016-05-11 12:59:22 -07001061 if (!m_pVT->IsValid())
1062 return swRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001063
thestig821d59e2016-05-11 12:59:22 -07001064 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1065 CPVT_WordRange wrTemp = range;
1066 m_pVT->UpdateWordPlace(wrTemp.BeginPos);
1067 m_pVT->UpdateWordPlace(wrTemp.EndPos);
1068 pIterator->SetAt(wrTemp.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001069
thestig821d59e2016-05-11 12:59:22 -07001070 CPVT_Word wordinfo;
1071 CPVT_WordPlace oldplace = wrTemp.BeginPos;
1072 while (pIterator->NextWord()) {
1073 CPVT_WordPlace place = pIterator->GetAt();
1074 if (place.WordCmp(wrTemp.EndPos) > 0)
1075 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001076
thestig821d59e2016-05-11 12:59:22 -07001077 if (pIterator->GetWord(wordinfo))
1078 swRet += wordinfo.Word;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001079
thestig821d59e2016-05-11 12:59:22 -07001080 if (oldplace.SecCmp(place) != 0)
1081 swRet += L"\r\n";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001082
thestig821d59e2016-05-11 12:59:22 -07001083 oldplace = place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001084 }
1085
1086 return swRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001087}
1088
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001089CFX_WideString CFX_Edit::GetSelText() const {
1090 return GetRangeText(m_SelState.ConvertToWordRange());
1091}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001092
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001093int32_t CFX_Edit::GetTotalWords() const {
1094 return m_pVT->GetTotalWords();
1095}
1096
1097int32_t CFX_Edit::GetTotalLines() const {
thestig821d59e2016-05-11 12:59:22 -07001098 int32_t nLines = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001099
thestig821d59e2016-05-11 12:59:22 -07001100 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1101 pIterator->SetAt(0);
1102 while (pIterator->NextLine())
1103 ++nLines;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001104
thestig821d59e2016-05-11 12:59:22 -07001105 return nLines;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001106}
1107
1108CPVT_WordRange CFX_Edit::GetSelectWordRange() const {
1109 return m_SelState.ConvertToWordRange();
1110}
1111
tsepeza31da742016-09-08 11:28:14 -07001112void CFX_Edit::SetText(const CFX_WideString& sText) {
dsinclairefd5a992016-07-18 10:04:07 -07001113 Empty();
npmea3c3be2016-09-19 07:24:33 -07001114 DoInsertText(CPVT_WordPlace(0, 0, -1), sText, FXFONT_DEFAULT_CHARSET);
dsinclairefd5a992016-07-18 10:04:07 -07001115 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001116}
1117
tsepez4cf55152016-11-02 14:37:54 -07001118bool CFX_Edit::InsertWord(uint16_t word, int32_t charset) {
1119 return InsertWord(word, charset, nullptr, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001120}
1121
tsepez4cf55152016-11-02 14:37:54 -07001122bool CFX_Edit::InsertReturn() {
1123 return InsertReturn(nullptr, nullptr, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001124}
1125
tsepez4cf55152016-11-02 14:37:54 -07001126bool CFX_Edit::Backspace() {
1127 return Backspace(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001128}
1129
tsepez4cf55152016-11-02 14:37:54 -07001130bool CFX_Edit::Delete() {
1131 return Delete(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001132}
1133
tsepez4cf55152016-11-02 14:37:54 -07001134bool CFX_Edit::Clear() {
1135 return Clear(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001136}
1137
tsepez4cf55152016-11-02 14:37:54 -07001138bool CFX_Edit::InsertText(const CFX_WideString& sText, int32_t charset) {
1139 return InsertText(sText, charset, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001140}
1141
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001142FX_FLOAT CFX_Edit::GetFontSize() const {
1143 return m_pVT->GetFontSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001144}
1145
Tom Sepez62a70f92016-03-21 15:00:20 -07001146uint16_t CFX_Edit::GetPasswordChar() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001147 return m_pVT->GetPasswordChar();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001148}
1149
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001150int32_t CFX_Edit::GetCharArray() const {
1151 return m_pVT->GetCharArray();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001152}
1153
Tom Sepez281a9ea2016-02-26 14:24:28 -08001154CFX_FloatRect CFX_Edit::GetContentRect() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001155 return VTToEdit(m_pVT->GetContentRect());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001156}
1157
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001158int32_t CFX_Edit::GetHorzScale() const {
1159 return m_pVT->GetHorzScale();
1160}
1161
1162FX_FLOAT CFX_Edit::GetCharSpace() const {
1163 return m_pVT->GetCharSpace();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001164}
1165
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001166CPVT_WordRange CFX_Edit::GetWholeWordRange() const {
1167 if (m_pVT->IsValid())
1168 return CPVT_WordRange(m_pVT->GetBeginWordPlace(), m_pVT->GetEndWordPlace());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001169
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001170 return CPVT_WordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001171}
1172
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001173CPVT_WordRange CFX_Edit::GetVisibleWordRange() const {
1174 if (m_bEnableOverflow)
1175 return GetWholeWordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001176
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001177 if (m_pVT->IsValid()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001178 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001179
Dan Sinclairf528eee2017-02-14 11:52:07 -05001180 CPVT_WordPlace place1 =
1181 m_pVT->SearchWordPlace(EditToVT(CFX_PointF(rcPlate.left, rcPlate.top)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001182 CPVT_WordPlace place2 = m_pVT->SearchWordPlace(
Dan Sinclairf528eee2017-02-14 11:52:07 -05001183 EditToVT(CFX_PointF(rcPlate.right, rcPlate.bottom)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001184
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001185 return CPVT_WordRange(place1, place2);
1186 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001187
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001188 return CPVT_WordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001189}
1190
Dan Sinclairf528eee2017-02-14 11:52:07 -05001191CPVT_WordPlace CFX_Edit::SearchWordPlace(const CFX_PointF& point) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001192 if (m_pVT->IsValid()) {
1193 return m_pVT->SearchWordPlace(EditToVT(point));
1194 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001195
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001196 return CPVT_WordPlace();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001197}
1198
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001199void CFX_Edit::Paint() {
1200 if (m_pVT->IsValid()) {
1201 RearrangeAll();
1202 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001203 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001204 SetCaretOrigin();
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001205 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001206 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001207}
1208
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001209void CFX_Edit::RearrangeAll() {
1210 if (m_pVT->IsValid()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001211 m_pVT->UpdateWordPlace(m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001212 m_pVT->RearrangeAll();
1213 m_pVT->UpdateWordPlace(m_wpCaret);
1214 SetScrollInfo();
1215 SetContentChanged();
1216 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001217}
1218
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001219void CFX_Edit::RearrangePart(const CPVT_WordRange& range) {
1220 if (m_pVT->IsValid()) {
1221 m_pVT->UpdateWordPlace(m_wpCaret);
1222 m_pVT->RearrangePart(range);
1223 m_pVT->UpdateWordPlace(m_wpCaret);
1224 SetScrollInfo();
1225 SetContentChanged();
1226 }
1227}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07001228
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001229void CFX_Edit::SetContentChanged() {
dsinclaira2919b32016-07-13 10:55:48 -07001230 if (m_pNotify) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001231 CFX_FloatRect rcContent = m_pVT->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001232 if (rcContent.Width() != m_rcOldContent.Width() ||
1233 rcContent.Height() != m_rcOldContent.Height()) {
1234 if (!m_bNotifyFlag) {
tsepez4cf55152016-11-02 14:37:54 -07001235 m_bNotifyFlag = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001236 m_pNotify->IOnContentChange(rcContent);
tsepez4cf55152016-11-02 14:37:54 -07001237 m_bNotifyFlag = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001238 }
1239 m_rcOldContent = rcContent;
1240 }
1241 }
1242}
1243
1244void CFX_Edit::SelectAll() {
1245 if (m_pVT->IsValid()) {
Lei Zhang375a8642016-01-11 11:59:17 -08001246 m_SelState = CFX_Edit_Select(GetWholeWordRange());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001247 SetCaret(m_SelState.EndPos);
1248
1249 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001250 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001251 SetCaretInfo();
1252 }
1253}
1254
1255void CFX_Edit::SelectNone() {
1256 if (m_pVT->IsValid()) {
1257 if (m_SelState.IsExist()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001258 m_SelState.Default();
dsinclairefd5a992016-07-18 10:04:07 -07001259 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001260 }
1261 }
1262}
1263
tsepez4cf55152016-11-02 14:37:54 -07001264bool CFX_Edit::IsSelected() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001265 return m_SelState.IsExist();
1266}
1267
Dan Sinclairf528eee2017-02-14 11:52:07 -05001268CFX_PointF CFX_Edit::VTToEdit(const CFX_PointF& point) const {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001269 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1270 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001271
1272 FX_FLOAT fPadding = 0.0f;
1273
1274 switch (m_nAlignment) {
1275 case 0:
1276 fPadding = 0.0f;
1277 break;
1278 case 1:
1279 fPadding = (rcPlate.Height() - rcContent.Height()) * 0.5f;
1280 break;
1281 case 2:
1282 fPadding = rcPlate.Height() - rcContent.Height();
1283 break;
1284 }
1285
Dan Sinclairf528eee2017-02-14 11:52:07 -05001286 return CFX_PointF(point.x - (m_ptScrollPos.x - rcPlate.left),
1287 point.y - (m_ptScrollPos.y + fPadding - rcPlate.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001288}
1289
Dan Sinclairf528eee2017-02-14 11:52:07 -05001290CFX_PointF CFX_Edit::EditToVT(const CFX_PointF& point) const {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001291 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1292 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001293
1294 FX_FLOAT fPadding = 0.0f;
1295
1296 switch (m_nAlignment) {
1297 case 0:
1298 fPadding = 0.0f;
1299 break;
1300 case 1:
1301 fPadding = (rcPlate.Height() - rcContent.Height()) * 0.5f;
1302 break;
1303 case 2:
1304 fPadding = rcPlate.Height() - rcContent.Height();
1305 break;
1306 }
1307
Dan Sinclairf528eee2017-02-14 11:52:07 -05001308 return CFX_PointF(point.x + (m_ptScrollPos.x - rcPlate.left),
1309 point.y + (m_ptScrollPos.y + fPadding - rcPlate.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001310}
1311
Tom Sepez281a9ea2016-02-26 14:24:28 -08001312CFX_FloatRect CFX_Edit::VTToEdit(const CFX_FloatRect& rect) const {
Dan Sinclairf528eee2017-02-14 11:52:07 -05001313 CFX_PointF ptLeftBottom = VTToEdit(CFX_PointF(rect.left, rect.bottom));
1314 CFX_PointF ptRightTop = VTToEdit(CFX_PointF(rect.right, rect.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001315
Tom Sepez281a9ea2016-02-26 14:24:28 -08001316 return CFX_FloatRect(ptLeftBottom.x, ptLeftBottom.y, ptRightTop.x,
1317 ptRightTop.y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001318}
1319
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001320void CFX_Edit::SetScrollInfo() {
dsinclaira2919b32016-07-13 10:55:48 -07001321 if (m_pNotify) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001322 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
1323 CFX_FloatRect rcContent = m_pVT->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001324
1325 if (!m_bNotifyFlag) {
tsepez4cf55152016-11-02 14:37:54 -07001326 m_bNotifyFlag = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001327 m_pNotify->IOnSetScrollInfoY(rcPlate.bottom, rcPlate.top,
1328 rcContent.bottom, rcContent.top,
1329 rcPlate.Height() / 3, rcPlate.Height());
tsepez4cf55152016-11-02 14:37:54 -07001330 m_bNotifyFlag = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001331 }
1332 }
1333}
1334
1335void CFX_Edit::SetScrollPosX(FX_FLOAT fx) {
1336 if (!m_bEnableScroll)
1337 return;
1338
1339 if (m_pVT->IsValid()) {
dsinclair448c4332016-08-02 12:07:35 -07001340 if (!IsFloatEqual(m_ptScrollPos.x, fx)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001341 m_ptScrollPos.x = fx;
dsinclairefd5a992016-07-18 10:04:07 -07001342 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001343 }
1344 }
1345}
1346
1347void CFX_Edit::SetScrollPosY(FX_FLOAT fy) {
1348 if (!m_bEnableScroll)
1349 return;
1350
1351 if (m_pVT->IsValid()) {
dsinclair448c4332016-08-02 12:07:35 -07001352 if (!IsFloatEqual(m_ptScrollPos.y, fy)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001353 m_ptScrollPos.y = fy;
dsinclairefd5a992016-07-18 10:04:07 -07001354 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001355
dsinclaira2919b32016-07-13 10:55:48 -07001356 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001357 if (!m_bNotifyFlag) {
tsepez4cf55152016-11-02 14:37:54 -07001358 m_bNotifyFlag = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001359 m_pNotify->IOnSetScrollPosY(fy);
tsepez4cf55152016-11-02 14:37:54 -07001360 m_bNotifyFlag = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001361 }
1362 }
1363 }
1364 }
1365}
1366
Dan Sinclairf528eee2017-02-14 11:52:07 -05001367void CFX_Edit::SetScrollPos(const CFX_PointF& point) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001368 SetScrollPosX(point.x);
1369 SetScrollPosY(point.y);
1370 SetScrollLimit();
1371 SetCaretInfo();
1372}
1373
Dan Sinclairf528eee2017-02-14 11:52:07 -05001374CFX_PointF CFX_Edit::GetScrollPos() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001375 return m_ptScrollPos;
1376}
1377
1378void CFX_Edit::SetScrollLimit() {
1379 if (m_pVT->IsValid()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001380 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1381 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001382
1383 if (rcPlate.Width() > rcContent.Width()) {
1384 SetScrollPosX(rcPlate.left);
1385 } else {
dsinclair448c4332016-08-02 12:07:35 -07001386 if (IsFloatSmaller(m_ptScrollPos.x, rcContent.left)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001387 SetScrollPosX(rcContent.left);
dsinclair448c4332016-08-02 12:07:35 -07001388 } else if (IsFloatBigger(m_ptScrollPos.x,
1389 rcContent.right - rcPlate.Width())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001390 SetScrollPosX(rcContent.right - rcPlate.Width());
1391 }
1392 }
1393
1394 if (rcPlate.Height() > rcContent.Height()) {
1395 SetScrollPosY(rcPlate.top);
1396 } else {
dsinclair448c4332016-08-02 12:07:35 -07001397 if (IsFloatSmaller(m_ptScrollPos.y,
1398 rcContent.bottom + rcPlate.Height())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001399 SetScrollPosY(rcContent.bottom + rcPlate.Height());
dsinclair448c4332016-08-02 12:07:35 -07001400 } else if (IsFloatBigger(m_ptScrollPos.y, rcContent.top)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001401 SetScrollPosY(rcContent.top);
1402 }
1403 }
1404 }
1405}
1406
1407void CFX_Edit::ScrollToCaret() {
1408 SetScrollLimit();
1409
thestig821d59e2016-05-11 12:59:22 -07001410 if (!m_pVT->IsValid())
1411 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001412
thestig821d59e2016-05-11 12:59:22 -07001413 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1414 pIterator->SetAt(m_wpCaret);
1415
Dan Sinclairf528eee2017-02-14 11:52:07 -05001416 CFX_PointF ptHead;
1417 CFX_PointF ptFoot;
thestig821d59e2016-05-11 12:59:22 -07001418 CPVT_Word word;
1419 CPVT_Line line;
1420 if (pIterator->GetWord(word)) {
1421 ptHead.x = word.ptWord.x + word.fWidth;
1422 ptHead.y = word.ptWord.y + word.fAscent;
1423 ptFoot.x = word.ptWord.x + word.fWidth;
1424 ptFoot.y = word.ptWord.y + word.fDescent;
1425 } else if (pIterator->GetLine(line)) {
1426 ptHead.x = line.ptLine.x;
1427 ptHead.y = line.ptLine.y + line.fLineAscent;
1428 ptFoot.x = line.ptLine.x;
1429 ptFoot.y = line.ptLine.y + line.fLineDescent;
1430 }
1431
Dan Sinclairf528eee2017-02-14 11:52:07 -05001432 CFX_PointF ptHeadEdit = VTToEdit(ptHead);
1433 CFX_PointF ptFootEdit = VTToEdit(ptFoot);
thestig821d59e2016-05-11 12:59:22 -07001434 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
dsinclair448c4332016-08-02 12:07:35 -07001435 if (!IsFloatEqual(rcPlate.left, rcPlate.right)) {
1436 if (IsFloatSmaller(ptHeadEdit.x, rcPlate.left) ||
1437 IsFloatEqual(ptHeadEdit.x, rcPlate.left)) {
thestig821d59e2016-05-11 12:59:22 -07001438 SetScrollPosX(ptHead.x);
dsinclair448c4332016-08-02 12:07:35 -07001439 } else if (IsFloatBigger(ptHeadEdit.x, rcPlate.right)) {
thestig821d59e2016-05-11 12:59:22 -07001440 SetScrollPosX(ptHead.x - rcPlate.Width());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001441 }
thestig821d59e2016-05-11 12:59:22 -07001442 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001443
dsinclair448c4332016-08-02 12:07:35 -07001444 if (!IsFloatEqual(rcPlate.top, rcPlate.bottom)) {
1445 if (IsFloatSmaller(ptFootEdit.y, rcPlate.bottom) ||
1446 IsFloatEqual(ptFootEdit.y, rcPlate.bottom)) {
1447 if (IsFloatSmaller(ptHeadEdit.y, rcPlate.top)) {
thestig821d59e2016-05-11 12:59:22 -07001448 SetScrollPosY(ptFoot.y + rcPlate.Height());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001449 }
dsinclair448c4332016-08-02 12:07:35 -07001450 } else if (IsFloatBigger(ptHeadEdit.y, rcPlate.top)) {
1451 if (IsFloatBigger(ptFootEdit.y, rcPlate.bottom)) {
thestig821d59e2016-05-11 12:59:22 -07001452 SetScrollPosY(ptHead.y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001453 }
1454 }
1455 }
1456}
1457
dsinclairefd5a992016-07-18 10:04:07 -07001458void CFX_Edit::Refresh() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001459 if (m_bEnableRefresh && m_pVT->IsValid()) {
1460 m_Refresh.BeginRefresh();
1461 RefreshPushLineRects(GetVisibleWordRange());
1462
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001463 m_Refresh.NoAnalyse();
1464 m_ptRefreshScrollPos = m_ptScrollPos;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001465
dsinclaira2919b32016-07-13 10:55:48 -07001466 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001467 if (!m_bNotifyFlag) {
tsepez4cf55152016-11-02 14:37:54 -07001468 m_bNotifyFlag = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001469 if (const CFX_Edit_RectArray* pRects = m_Refresh.GetRefreshRects()) {
1470 for (int32_t i = 0, sz = pRects->GetSize(); i < sz; i++)
1471 m_pNotify->IOnInvalidateRect(pRects->GetAt(i));
1472 }
tsepez4cf55152016-11-02 14:37:54 -07001473 m_bNotifyFlag = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001474 }
1475 }
1476
1477 m_Refresh.EndRefresh();
1478 }
1479}
1480
1481void CFX_Edit::RefreshPushLineRects(const CPVT_WordRange& wr) {
thestig821d59e2016-05-11 12:59:22 -07001482 if (!m_pVT->IsValid())
1483 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001484
thestig821d59e2016-05-11 12:59:22 -07001485 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1486 CPVT_WordPlace wpBegin = wr.BeginPos;
1487 m_pVT->UpdateWordPlace(wpBegin);
1488 CPVT_WordPlace wpEnd = wr.EndPos;
1489 m_pVT->UpdateWordPlace(wpEnd);
1490 pIterator->SetAt(wpBegin);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001491
thestig821d59e2016-05-11 12:59:22 -07001492 CPVT_Line lineinfo;
1493 do {
1494 if (!pIterator->GetLine(lineinfo))
1495 break;
1496 if (lineinfo.lineplace.LineCmp(wpEnd) > 0)
1497 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001498
thestig821d59e2016-05-11 12:59:22 -07001499 CFX_FloatRect rcLine(lineinfo.ptLine.x,
1500 lineinfo.ptLine.y + lineinfo.fLineDescent,
1501 lineinfo.ptLine.x + lineinfo.fLineWidth,
1502 lineinfo.ptLine.y + lineinfo.fLineAscent);
1503
1504 m_Refresh.Push(CPVT_WordRange(lineinfo.lineplace, lineinfo.lineEnd),
1505 VTToEdit(rcLine));
1506 } while (pIterator->NextLine());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001507}
1508
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001509void CFX_Edit::RefreshWordRange(const CPVT_WordRange& wr) {
thestig821d59e2016-05-11 12:59:22 -07001510 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1511 CPVT_WordRange wrTemp = wr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001512
thestig821d59e2016-05-11 12:59:22 -07001513 m_pVT->UpdateWordPlace(wrTemp.BeginPos);
1514 m_pVT->UpdateWordPlace(wrTemp.EndPos);
1515 pIterator->SetAt(wrTemp.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001516
thestig821d59e2016-05-11 12:59:22 -07001517 CPVT_Word wordinfo;
1518 CPVT_Line lineinfo;
1519 CPVT_WordPlace place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001520
thestig821d59e2016-05-11 12:59:22 -07001521 while (pIterator->NextWord()) {
1522 place = pIterator->GetAt();
1523 if (place.WordCmp(wrTemp.EndPos) > 0)
1524 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001525
thestig821d59e2016-05-11 12:59:22 -07001526 pIterator->GetWord(wordinfo);
1527 pIterator->GetLine(lineinfo);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001528
thestig821d59e2016-05-11 12:59:22 -07001529 if (place.LineCmp(wrTemp.BeginPos) == 0 ||
1530 place.LineCmp(wrTemp.EndPos) == 0) {
1531 CFX_FloatRect rcWord(wordinfo.ptWord.x,
1532 lineinfo.ptLine.y + lineinfo.fLineDescent,
1533 wordinfo.ptWord.x + wordinfo.fWidth,
1534 lineinfo.ptLine.y + lineinfo.fLineAscent);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001535
dsinclaira2919b32016-07-13 10:55:48 -07001536 if (m_pNotify) {
thestig821d59e2016-05-11 12:59:22 -07001537 if (!m_bNotifyFlag) {
tsepez4cf55152016-11-02 14:37:54 -07001538 m_bNotifyFlag = true;
thestig821d59e2016-05-11 12:59:22 -07001539 CFX_FloatRect rcRefresh = VTToEdit(rcWord);
1540 m_pNotify->IOnInvalidateRect(&rcRefresh);
tsepez4cf55152016-11-02 14:37:54 -07001541 m_bNotifyFlag = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001542 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001543 }
thestig821d59e2016-05-11 12:59:22 -07001544 } else {
1545 CFX_FloatRect rcLine(lineinfo.ptLine.x,
1546 lineinfo.ptLine.y + lineinfo.fLineDescent,
1547 lineinfo.ptLine.x + lineinfo.fLineWidth,
1548 lineinfo.ptLine.y + lineinfo.fLineAscent);
1549
dsinclaira2919b32016-07-13 10:55:48 -07001550 if (m_pNotify) {
thestig821d59e2016-05-11 12:59:22 -07001551 if (!m_bNotifyFlag) {
tsepez4cf55152016-11-02 14:37:54 -07001552 m_bNotifyFlag = true;
thestig821d59e2016-05-11 12:59:22 -07001553 CFX_FloatRect rcRefresh = VTToEdit(rcLine);
1554 m_pNotify->IOnInvalidateRect(&rcRefresh);
tsepez4cf55152016-11-02 14:37:54 -07001555 m_bNotifyFlag = false;
thestig821d59e2016-05-11 12:59:22 -07001556 }
1557 }
1558
1559 pIterator->NextLine();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001560 }
1561 }
1562}
1563
1564void CFX_Edit::SetCaret(const CPVT_WordPlace& place) {
1565 m_wpOldCaret = m_wpCaret;
1566 m_wpCaret = place;
1567}
1568
1569void CFX_Edit::SetCaretInfo() {
dsinclaira2919b32016-07-13 10:55:48 -07001570 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001571 if (!m_bNotifyFlag) {
thestig821d59e2016-05-11 12:59:22 -07001572 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1573 pIterator->SetAt(m_wpCaret);
tsepez63f545c2016-09-13 16:08:49 -07001574
Dan Sinclairf528eee2017-02-14 11:52:07 -05001575 CFX_PointF ptHead;
1576 CFX_PointF ptFoot;
thestig821d59e2016-05-11 12:59:22 -07001577 CPVT_Word word;
1578 CPVT_Line line;
1579 if (pIterator->GetWord(word)) {
1580 ptHead.x = word.ptWord.x + word.fWidth;
1581 ptHead.y = word.ptWord.y + word.fAscent;
1582 ptFoot.x = word.ptWord.x + word.fWidth;
1583 ptFoot.y = word.ptWord.y + word.fDescent;
1584 } else if (pIterator->GetLine(line)) {
1585 ptHead.x = line.ptLine.x;
1586 ptHead.y = line.ptLine.y + line.fLineAscent;
1587 ptFoot.x = line.ptLine.x;
1588 ptFoot.y = line.ptLine.y + line.fLineDescent;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001589 }
1590
tsepez4cf55152016-11-02 14:37:54 -07001591 m_bNotifyFlag = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001592 m_pNotify->IOnSetCaret(!m_SelState.IsExist(), VTToEdit(ptHead),
1593 VTToEdit(ptFoot), m_wpCaret);
tsepez4cf55152016-11-02 14:37:54 -07001594 m_bNotifyFlag = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001595 }
1596 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001597}
1598
1599void CFX_Edit::SetCaret(int32_t nPos) {
1600 if (m_pVT->IsValid()) {
1601 SelectNone();
1602 SetCaret(m_pVT->WordIndexToWordPlace(nPos));
1603 m_SelState.Set(m_wpCaret, m_wpCaret);
1604
1605 ScrollToCaret();
1606 SetCaretOrigin();
1607 SetCaretInfo();
1608 }
1609}
1610
Dan Sinclairf528eee2017-02-14 11:52:07 -05001611void CFX_Edit::OnMouseDown(const CFX_PointF& point, bool bShift, bool bCtrl) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001612 if (m_pVT->IsValid()) {
1613 SelectNone();
1614 SetCaret(m_pVT->SearchWordPlace(EditToVT(point)));
1615 m_SelState.Set(m_wpCaret, m_wpCaret);
1616
1617 ScrollToCaret();
1618 SetCaretOrigin();
1619 SetCaretInfo();
1620 }
1621}
1622
Dan Sinclairf528eee2017-02-14 11:52:07 -05001623void CFX_Edit::OnMouseMove(const CFX_PointF& point, bool bShift, bool bCtrl) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001624 if (m_pVT->IsValid()) {
1625 SetCaret(m_pVT->SearchWordPlace(EditToVT(point)));
1626
1627 if (m_wpCaret != m_wpOldCaret) {
1628 m_SelState.SetEndPos(m_wpCaret);
1629
1630 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001631 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001632 SetCaretOrigin();
1633 SetCaretInfo();
1634 }
1635 }
1636}
1637
tsepez4cf55152016-11-02 14:37:54 -07001638void CFX_Edit::OnVK_UP(bool bShift, bool bCtrl) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001639 if (m_pVT->IsValid()) {
1640 SetCaret(m_pVT->GetUpWordPlace(m_wpCaret, m_ptCaret));
1641
1642 if (bShift) {
1643 if (m_SelState.IsExist())
1644 m_SelState.SetEndPos(m_wpCaret);
1645 else
1646 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1647
1648 if (m_wpOldCaret != m_wpCaret) {
1649 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001650 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001651 SetCaretInfo();
1652 }
1653 } else {
1654 SelectNone();
1655
1656 ScrollToCaret();
1657 SetCaretInfo();
1658 }
1659 }
1660}
1661
tsepez4cf55152016-11-02 14:37:54 -07001662void CFX_Edit::OnVK_DOWN(bool bShift, bool bCtrl) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001663 if (m_pVT->IsValid()) {
1664 SetCaret(m_pVT->GetDownWordPlace(m_wpCaret, m_ptCaret));
1665
1666 if (bShift) {
1667 if (m_SelState.IsExist())
1668 m_SelState.SetEndPos(m_wpCaret);
1669 else
1670 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1671
1672 if (m_wpOldCaret != m_wpCaret) {
1673 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001674 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001675 SetCaretInfo();
1676 }
1677 } else {
1678 SelectNone();
1679
1680 ScrollToCaret();
1681 SetCaretInfo();
1682 }
1683 }
1684}
1685
tsepez4cf55152016-11-02 14:37:54 -07001686void CFX_Edit::OnVK_LEFT(bool bShift, bool bCtrl) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001687 if (m_pVT->IsValid()) {
1688 if (bShift) {
1689 if (m_wpCaret == m_pVT->GetLineBeginPlace(m_wpCaret) &&
1690 m_wpCaret != m_pVT->GetSectionBeginPlace(m_wpCaret))
1691 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1692
1693 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1694
1695 if (m_SelState.IsExist())
1696 m_SelState.SetEndPos(m_wpCaret);
1697 else
1698 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1699
1700 if (m_wpOldCaret != m_wpCaret) {
1701 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001702 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001703 SetCaretInfo();
1704 }
1705 } else {
1706 if (m_SelState.IsExist()) {
1707 if (m_SelState.BeginPos.WordCmp(m_SelState.EndPos) < 0)
1708 SetCaret(m_SelState.BeginPos);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001709 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001710 SetCaret(m_SelState.EndPos);
1711
1712 SelectNone();
1713 ScrollToCaret();
1714 SetCaretInfo();
1715 } else {
1716 if (m_wpCaret == m_pVT->GetLineBeginPlace(m_wpCaret) &&
1717 m_wpCaret != m_pVT->GetSectionBeginPlace(m_wpCaret))
1718 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1719
1720 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1721
1722 ScrollToCaret();
1723 SetCaretOrigin();
1724 SetCaretInfo();
1725 }
1726 }
1727 }
1728}
1729
tsepez4cf55152016-11-02 14:37:54 -07001730void CFX_Edit::OnVK_RIGHT(bool bShift, bool bCtrl) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001731 if (m_pVT->IsValid()) {
1732 if (bShift) {
1733 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1734
1735 if (m_wpCaret == m_pVT->GetLineEndPlace(m_wpCaret) &&
1736 m_wpCaret != m_pVT->GetSectionEndPlace(m_wpCaret))
1737 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1738
1739 if (m_SelState.IsExist())
1740 m_SelState.SetEndPos(m_wpCaret);
1741 else
1742 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1743
1744 if (m_wpOldCaret != m_wpCaret) {
1745 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001746 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001747 SetCaretInfo();
1748 }
1749 } else {
1750 if (m_SelState.IsExist()) {
1751 if (m_SelState.BeginPos.WordCmp(m_SelState.EndPos) > 0)
1752 SetCaret(m_SelState.BeginPos);
1753 else
1754 SetCaret(m_SelState.EndPos);
1755
1756 SelectNone();
1757 ScrollToCaret();
1758 SetCaretInfo();
1759 } else {
1760 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1761
1762 if (m_wpCaret == m_pVT->GetLineEndPlace(m_wpCaret) &&
1763 m_wpCaret != m_pVT->GetSectionEndPlace(m_wpCaret))
1764 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1765
1766 ScrollToCaret();
1767 SetCaretOrigin();
1768 SetCaretInfo();
1769 }
1770 }
1771 }
1772}
1773
tsepez4cf55152016-11-02 14:37:54 -07001774void CFX_Edit::OnVK_HOME(bool bShift, bool bCtrl) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001775 if (m_pVT->IsValid()) {
1776 if (bShift) {
1777 if (bCtrl)
1778 SetCaret(m_pVT->GetBeginWordPlace());
1779 else
1780 SetCaret(m_pVT->GetLineBeginPlace(m_wpCaret));
1781
1782 if (m_SelState.IsExist())
1783 m_SelState.SetEndPos(m_wpCaret);
1784 else
1785 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1786
1787 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001788 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001789 SetCaretInfo();
1790 } else {
1791 if (m_SelState.IsExist()) {
1792 if (m_SelState.BeginPos.WordCmp(m_SelState.EndPos) < 0)
1793 SetCaret(m_SelState.BeginPos);
1794 else
1795 SetCaret(m_SelState.EndPos);
1796
1797 SelectNone();
1798 ScrollToCaret();
1799 SetCaretInfo();
1800 } else {
1801 if (bCtrl)
1802 SetCaret(m_pVT->GetBeginWordPlace());
1803 else
1804 SetCaret(m_pVT->GetLineBeginPlace(m_wpCaret));
1805
1806 ScrollToCaret();
1807 SetCaretOrigin();
1808 SetCaretInfo();
1809 }
1810 }
1811 }
1812}
1813
tsepez4cf55152016-11-02 14:37:54 -07001814void CFX_Edit::OnVK_END(bool bShift, bool bCtrl) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001815 if (m_pVT->IsValid()) {
1816 if (bShift) {
1817 if (bCtrl)
1818 SetCaret(m_pVT->GetEndWordPlace());
1819 else
1820 SetCaret(m_pVT->GetLineEndPlace(m_wpCaret));
1821
1822 if (m_SelState.IsExist())
1823 m_SelState.SetEndPos(m_wpCaret);
1824 else
1825 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1826
1827 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001828 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001829 SetCaretInfo();
1830 } else {
1831 if (m_SelState.IsExist()) {
1832 if (m_SelState.BeginPos.WordCmp(m_SelState.EndPos) > 0)
1833 SetCaret(m_SelState.BeginPos);
1834 else
1835 SetCaret(m_SelState.EndPos);
1836
1837 SelectNone();
1838 ScrollToCaret();
1839 SetCaretInfo();
1840 } else {
1841 if (bCtrl)
1842 SetCaret(m_pVT->GetEndWordPlace());
1843 else
1844 SetCaret(m_pVT->GetLineEndPlace(m_wpCaret));
1845
1846 ScrollToCaret();
1847 SetCaretOrigin();
1848 SetCaretInfo();
1849 }
1850 }
1851 }
1852}
1853
tsepez4cf55152016-11-02 14:37:54 -07001854bool CFX_Edit::InsertWord(uint16_t word,
1855 int32_t charset,
1856 const CPVT_WordProps* pWordProps,
1857 bool bAddUndo,
1858 bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001859 if (IsTextOverflow() || !m_pVT->IsValid())
tsepez4cf55152016-11-02 14:37:54 -07001860 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001861
Tom Sepez3509d162017-01-30 13:22:02 -08001862 m_pVT->UpdateWordPlace(m_wpCaret);
1863 SetCaret(m_pVT->InsertWord(m_wpCaret, word,
1864 GetCharSetFromUnicode(word, charset), pWordProps));
1865 m_SelState.Set(m_wpCaret, m_wpCaret);
1866 if (m_wpCaret == m_wpOldCaret)
1867 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001868
Tom Sepez3509d162017-01-30 13:22:02 -08001869 if (bAddUndo && m_bEnableUndo) {
1870 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertWord>(
1871 this, m_wpOldCaret, m_wpCaret, word, charset, pWordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001872 }
Tom Sepez3509d162017-01-30 13:22:02 -08001873 if (bPaint)
1874 PaintInsertText(m_wpOldCaret, m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001875
Tom Sepez3509d162017-01-30 13:22:02 -08001876 if (m_bOprNotify && m_pOprNotify)
1877 m_pOprNotify->OnInsertWord(m_wpCaret, m_wpOldCaret);
1878
1879 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001880}
1881
tsepez4cf55152016-11-02 14:37:54 -07001882bool CFX_Edit::InsertReturn(const CPVT_SecProps* pSecProps,
1883 const CPVT_WordProps* pWordProps,
1884 bool bAddUndo,
1885 bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001886 if (IsTextOverflow() || !m_pVT->IsValid())
tsepez4cf55152016-11-02 14:37:54 -07001887 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001888
Tom Sepez3509d162017-01-30 13:22:02 -08001889 m_pVT->UpdateWordPlace(m_wpCaret);
1890 SetCaret(m_pVT->InsertSection(m_wpCaret, pSecProps, pWordProps));
1891 m_SelState.Set(m_wpCaret, m_wpCaret);
1892 if (m_wpCaret == m_wpOldCaret)
1893 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001894
Tom Sepez3509d162017-01-30 13:22:02 -08001895 if (bAddUndo && m_bEnableUndo) {
1896 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertReturn>(
1897 this, m_wpOldCaret, m_wpCaret, pSecProps, pWordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001898 }
Tom Sepez3509d162017-01-30 13:22:02 -08001899 if (bPaint) {
1900 RearrangePart(CPVT_WordRange(m_wpOldCaret, m_wpCaret));
1901 ScrollToCaret();
1902 Refresh();
1903 SetCaretOrigin();
1904 SetCaretInfo();
1905 }
1906 if (m_bOprNotify && m_pOprNotify)
1907 m_pOprNotify->OnInsertReturn(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001908
Tom Sepez3509d162017-01-30 13:22:02 -08001909 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001910}
1911
tsepez4cf55152016-11-02 14:37:54 -07001912bool CFX_Edit::Backspace(bool bAddUndo, bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001913 if (!m_pVT->IsValid() || m_wpCaret == m_pVT->GetBeginWordPlace())
1914 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001915
Tom Sepez3509d162017-01-30 13:22:02 -08001916 CPVT_Section section;
1917 CPVT_Word word;
1918 if (bAddUndo) {
1919 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1920 pIterator->SetAt(m_wpCaret);
1921 pIterator->GetSection(section);
1922 pIterator->GetWord(word);
1923 }
1924 m_pVT->UpdateWordPlace(m_wpCaret);
1925 SetCaret(m_pVT->BackSpaceWord(m_wpCaret));
1926 m_SelState.Set(m_wpCaret, m_wpCaret);
1927 if (m_wpCaret == m_wpOldCaret)
1928 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001929
Tom Sepez3509d162017-01-30 13:22:02 -08001930 if (bAddUndo && m_bEnableUndo) {
1931 if (m_wpCaret.SecCmp(m_wpOldCaret) != 0) {
1932 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Backspace>(
1933 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1934 section.SecProps, section.WordProps));
1935 } else {
1936 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Backspace>(
1937 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1938 section.SecProps, word.WordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001939 }
1940 }
Tom Sepez3509d162017-01-30 13:22:02 -08001941 if (bPaint) {
1942 RearrangePart(CPVT_WordRange(m_wpCaret, m_wpOldCaret));
1943 ScrollToCaret();
1944 Refresh();
1945 SetCaretOrigin();
1946 SetCaretInfo();
1947 }
1948 if (m_bOprNotify && m_pOprNotify)
1949 m_pOprNotify->OnBackSpace(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001950
Tom Sepez3509d162017-01-30 13:22:02 -08001951 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001952}
1953
tsepez4cf55152016-11-02 14:37:54 -07001954bool CFX_Edit::Delete(bool bAddUndo, bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001955 if (!m_pVT->IsValid() || m_wpCaret == m_pVT->GetEndWordPlace())
1956 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001957
Tom Sepez3509d162017-01-30 13:22:02 -08001958 CPVT_Section section;
1959 CPVT_Word word;
1960 if (bAddUndo) {
1961 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1962 pIterator->SetAt(m_pVT->GetNextWordPlace(m_wpCaret));
1963 pIterator->GetSection(section);
1964 pIterator->GetWord(word);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001965 }
Tom Sepez3509d162017-01-30 13:22:02 -08001966 m_pVT->UpdateWordPlace(m_wpCaret);
1967 bool bSecEnd = (m_wpCaret == m_pVT->GetSectionEndPlace(m_wpCaret));
1968 SetCaret(m_pVT->DeleteWord(m_wpCaret));
1969 m_SelState.Set(m_wpCaret, m_wpCaret);
1970 if (bAddUndo && m_bEnableUndo) {
1971 if (bSecEnd) {
1972 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Delete>(
1973 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1974 section.SecProps, section.WordProps, bSecEnd));
1975 } else {
1976 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Delete>(
1977 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1978 section.SecProps, word.WordProps, bSecEnd));
1979 }
1980 }
1981 if (bPaint) {
1982 RearrangePart(CPVT_WordRange(m_wpOldCaret, m_wpCaret));
1983 ScrollToCaret();
1984 Refresh();
1985 SetCaretOrigin();
1986 SetCaretInfo();
1987 }
1988 if (m_bOprNotify && m_pOprNotify)
1989 m_pOprNotify->OnDelete(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001990
Tom Sepez3509d162017-01-30 13:22:02 -08001991 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001992}
1993
tsepez4cf55152016-11-02 14:37:54 -07001994bool CFX_Edit::Empty() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001995 if (m_pVT->IsValid()) {
1996 m_pVT->DeleteWords(GetWholeWordRange());
1997 SetCaret(m_pVT->GetBeginWordPlace());
1998
tsepez4cf55152016-11-02 14:37:54 -07001999 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002000 }
2001
tsepez4cf55152016-11-02 14:37:54 -07002002 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002003}
2004
tsepez4cf55152016-11-02 14:37:54 -07002005bool CFX_Edit::Clear(bool bAddUndo, bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08002006 if (!m_pVT->IsValid() || !m_SelState.IsExist())
tsepez4cf55152016-11-02 14:37:54 -07002007 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002008
thestig821d59e2016-05-11 12:59:22 -07002009 CPVT_WordRange range = m_SelState.ConvertToWordRange();
dsinclaira2919b32016-07-13 10:55:48 -07002010 if (bAddUndo && m_bEnableUndo)
Tom Sepez3509d162017-01-30 13:22:02 -08002011 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Clear>(this, range, GetSelText()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002012
thestig821d59e2016-05-11 12:59:22 -07002013 SelectNone();
2014 SetCaret(m_pVT->DeleteWords(range));
2015 m_SelState.Set(m_wpCaret, m_wpCaret);
thestig821d59e2016-05-11 12:59:22 -07002016 if (bPaint) {
2017 RearrangePart(range);
2018 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07002019 Refresh();
thestig821d59e2016-05-11 12:59:22 -07002020 SetCaretOrigin();
2021 SetCaretInfo();
2022 }
thestig821d59e2016-05-11 12:59:22 -07002023 if (m_bOprNotify && m_pOprNotify)
2024 m_pOprNotify->OnClear(m_wpCaret, m_wpOldCaret);
2025
tsepez4cf55152016-11-02 14:37:54 -07002026 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002027}
2028
tsepez4cf55152016-11-02 14:37:54 -07002029bool CFX_Edit::InsertText(const CFX_WideString& sText,
2030 int32_t charset,
2031 bool bAddUndo,
2032 bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002033 if (IsTextOverflow())
tsepez4cf55152016-11-02 14:37:54 -07002034 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002035
2036 m_pVT->UpdateWordPlace(m_wpCaret);
tsepeza31da742016-09-08 11:28:14 -07002037 SetCaret(DoInsertText(m_wpCaret, sText, charset));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002038 m_SelState.Set(m_wpCaret, m_wpCaret);
tsepeza31da742016-09-08 11:28:14 -07002039 if (m_wpCaret == m_wpOldCaret)
tsepez4cf55152016-11-02 14:37:54 -07002040 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002041
tsepeza31da742016-09-08 11:28:14 -07002042 if (bAddUndo && m_bEnableUndo) {
Tom Sepez3509d162017-01-30 13:22:02 -08002043 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertText>(
2044 this, m_wpOldCaret, m_wpCaret, sText, charset));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002045 }
tsepeza31da742016-09-08 11:28:14 -07002046 if (bPaint)
2047 PaintInsertText(m_wpOldCaret, m_wpCaret);
2048
2049 if (m_bOprNotify && m_pOprNotify)
2050 m_pOprNotify->OnInsertText(m_wpCaret, m_wpOldCaret);
2051
tsepez4cf55152016-11-02 14:37:54 -07002052 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002053}
2054
2055void CFX_Edit::PaintInsertText(const CPVT_WordPlace& wpOld,
2056 const CPVT_WordPlace& wpNew) {
2057 if (m_pVT->IsValid()) {
2058 RearrangePart(CPVT_WordRange(wpOld, wpNew));
2059 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07002060 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002061 SetCaretOrigin();
2062 SetCaretInfo();
2063 }
2064}
2065
tsepez4cf55152016-11-02 14:37:54 -07002066bool CFX_Edit::Redo() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002067 if (m_bEnableUndo) {
2068 if (m_Undo.CanRedo()) {
2069 m_Undo.Redo();
tsepez4cf55152016-11-02 14:37:54 -07002070 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002071 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002072 }
2073
tsepez4cf55152016-11-02 14:37:54 -07002074 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002075}
2076
tsepez4cf55152016-11-02 14:37:54 -07002077bool CFX_Edit::Undo() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002078 if (m_bEnableUndo) {
2079 if (m_Undo.CanUndo()) {
2080 m_Undo.Undo();
tsepez4cf55152016-11-02 14:37:54 -07002081 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002082 }
2083 }
2084
tsepez4cf55152016-11-02 14:37:54 -07002085 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002086}
2087
2088void CFX_Edit::SetCaretOrigin() {
thestig821d59e2016-05-11 12:59:22 -07002089 if (!m_pVT->IsValid())
2090 return;
2091
2092 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
2093 pIterator->SetAt(m_wpCaret);
2094 CPVT_Word word;
2095 CPVT_Line line;
2096 if (pIterator->GetWord(word)) {
2097 m_ptCaret.x = word.ptWord.x + word.fWidth;
2098 m_ptCaret.y = word.ptWord.y;
2099 } else if (pIterator->GetLine(line)) {
2100 m_ptCaret.x = line.ptLine.x;
2101 m_ptCaret.y = line.ptLine.y;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002102 }
2103}
2104
2105int32_t CFX_Edit::WordPlaceToWordIndex(const CPVT_WordPlace& place) const {
2106 if (m_pVT->IsValid())
2107 return m_pVT->WordPlaceToWordIndex(place);
2108
2109 return -1;
2110}
2111
2112CPVT_WordPlace CFX_Edit::WordIndexToWordPlace(int32_t index) const {
2113 if (m_pVT->IsValid())
2114 return m_pVT->WordIndexToWordPlace(index);
2115
2116 return CPVT_WordPlace();
2117}
2118
tsepez4cf55152016-11-02 14:37:54 -07002119bool CFX_Edit::IsTextFull() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002120 int32_t nTotalWords = m_pVT->GetTotalWords();
2121 int32_t nLimitChar = m_pVT->GetLimitChar();
2122 int32_t nCharArray = m_pVT->GetCharArray();
2123
2124 return IsTextOverflow() || (nLimitChar > 0 && nTotalWords >= nLimitChar) ||
2125 (nCharArray > 0 && nTotalWords >= nCharArray);
2126}
2127
tsepez4cf55152016-11-02 14:37:54 -07002128bool CFX_Edit::IsTextOverflow() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002129 if (!m_bEnableScroll && !m_bEnableOverflow) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08002130 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
2131 CFX_FloatRect rcContent = m_pVT->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002132
dsinclair448c4332016-08-02 12:07:35 -07002133 if (m_pVT->IsMultiLine() && GetTotalLines() > 1 &&
2134 IsFloatBigger(rcContent.Height(), rcPlate.Height())) {
tsepez4cf55152016-11-02 14:37:54 -07002135 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002136 }
2137
dsinclair448c4332016-08-02 12:07:35 -07002138 if (IsFloatBigger(rcContent.Width(), rcPlate.Width()))
tsepez4cf55152016-11-02 14:37:54 -07002139 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002140 }
2141
tsepez4cf55152016-11-02 14:37:54 -07002142 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002143}
2144
tsepez4cf55152016-11-02 14:37:54 -07002145bool CFX_Edit::CanUndo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002146 if (m_bEnableUndo) {
2147 return m_Undo.CanUndo();
2148 }
2149
tsepez4cf55152016-11-02 14:37:54 -07002150 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002151}
2152
tsepez4cf55152016-11-02 14:37:54 -07002153bool CFX_Edit::CanRedo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002154 if (m_bEnableUndo) {
2155 return m_Undo.CanRedo();
2156 }
2157
tsepez4cf55152016-11-02 14:37:54 -07002158 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002159}
2160
tsepez4cf55152016-11-02 14:37:54 -07002161void CFX_Edit::EnableRefresh(bool bRefresh) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002162 m_bEnableRefresh = bRefresh;
2163}
2164
tsepez4cf55152016-11-02 14:37:54 -07002165void CFX_Edit::EnableUndo(bool bUndo) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002166 m_bEnableUndo = bUndo;
2167}
2168
tsepez4cf55152016-11-02 14:37:54 -07002169void CFX_Edit::EnableOprNotify(bool bNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002170 m_bOprNotify = bNotify;
2171}
2172
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002173CPVT_WordPlace CFX_Edit::DoInsertText(const CPVT_WordPlace& place,
tsepeza31da742016-09-08 11:28:14 -07002174 const CFX_WideString& sText,
dsinclairefd5a992016-07-18 10:04:07 -07002175 int32_t charset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002176 CPVT_WordPlace wp = place;
2177
2178 if (m_pVT->IsValid()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002179 for (int32_t i = 0, sz = sText.GetLength(); i < sz; i++) {
Tom Sepez62a70f92016-03-21 15:00:20 -07002180 uint16_t word = sText[i];
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002181 switch (word) {
2182 case 0x0D:
dsinclairefd5a992016-07-18 10:04:07 -07002183 wp = m_pVT->InsertSection(wp, nullptr, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002184 if (sText[i + 1] == 0x0A)
2185 i++;
2186 break;
2187 case 0x0A:
dsinclairefd5a992016-07-18 10:04:07 -07002188 wp = m_pVT->InsertSection(wp, nullptr, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002189 if (sText[i + 1] == 0x0D)
2190 i++;
2191 break;
2192 case 0x09:
2193 word = 0x20;
2194 default:
2195 wp = m_pVT->InsertWord(wp, word, GetCharSetFromUnicode(word, charset),
dsinclairefd5a992016-07-18 10:04:07 -07002196 nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002197 break;
2198 }
2199 }
2200 }
2201
2202 return wp;
2203}
2204
Tom Sepez62a70f92016-03-21 15:00:20 -07002205int32_t CFX_Edit::GetCharSetFromUnicode(uint16_t word, int32_t nOldCharset) {
dsinclairc7a73492016-04-05 12:01:42 -07002206 if (IPVT_FontMap* pFontMap = GetFontMap())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002207 return pFontMap->CharSetFromUnicode(word, nOldCharset);
2208 return nOldCharset;
2209}
2210
Tom Sepez3509d162017-01-30 13:22:02 -08002211void CFX_Edit::AddEditUndoItem(
2212 std::unique_ptr<CFX_Edit_UndoItem> pEditUndoItem) {
2213 if (m_pGroupUndoItem)
2214 m_pGroupUndoItem->AddUndoItem(std::move(pEditUndoItem));
2215 else
2216 m_Undo.AddItem(std::move(pEditUndoItem));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002217}
2218
weili625ad662016-06-15 11:21:33 -07002219CFX_Edit_LineRectArray::CFX_Edit_LineRectArray() {}
2220
Tom Sepez3509d162017-01-30 13:22:02 -08002221CFX_Edit_LineRectArray::~CFX_Edit_LineRectArray() {}
weili625ad662016-06-15 11:21:33 -07002222
Tom Sepez3509d162017-01-30 13:22:02 -08002223void CFX_Edit_LineRectArray::operator=(CFX_Edit_LineRectArray&& that) {
2224 m_LineRects = std::move(that.m_LineRects);
weili625ad662016-06-15 11:21:33 -07002225}
2226
2227void CFX_Edit_LineRectArray::Add(const CPVT_WordRange& wrLine,
2228 const CFX_FloatRect& rcLine) {
Tom Sepez3509d162017-01-30 13:22:02 -08002229 m_LineRects.push_back(pdfium::MakeUnique<CFX_Edit_LineRect>(wrLine, rcLine));
weili625ad662016-06-15 11:21:33 -07002230}
2231
2232int32_t CFX_Edit_LineRectArray::GetSize() const {
Tom Sepez3509d162017-01-30 13:22:02 -08002233 return pdfium::CollectionSize<int32_t>(m_LineRects);
weili625ad662016-06-15 11:21:33 -07002234}
2235
2236CFX_Edit_LineRect* CFX_Edit_LineRectArray::GetAt(int32_t nIndex) const {
Tom Sepez3509d162017-01-30 13:22:02 -08002237 if (nIndex < 0 || nIndex >= GetSize())
weili625ad662016-06-15 11:21:33 -07002238 return nullptr;
2239
Tom Sepez3509d162017-01-30 13:22:02 -08002240 return m_LineRects[nIndex].get();
weili625ad662016-06-15 11:21:33 -07002241}
2242
2243CFX_Edit_Select::CFX_Edit_Select() {}
2244
2245CFX_Edit_Select::CFX_Edit_Select(const CPVT_WordPlace& begin,
2246 const CPVT_WordPlace& end) {
2247 Set(begin, end);
2248}
2249
2250CFX_Edit_Select::CFX_Edit_Select(const CPVT_WordRange& range) {
2251 Set(range.BeginPos, range.EndPos);
2252}
2253
2254CPVT_WordRange CFX_Edit_Select::ConvertToWordRange() const {
2255 return CPVT_WordRange(BeginPos, EndPos);
2256}
2257
2258void CFX_Edit_Select::Default() {
2259 BeginPos.Default();
2260 EndPos.Default();
2261}
2262
2263void CFX_Edit_Select::Set(const CPVT_WordPlace& begin,
2264 const CPVT_WordPlace& end) {
2265 BeginPos = begin;
2266 EndPos = end;
2267}
2268
2269void CFX_Edit_Select::SetBeginPos(const CPVT_WordPlace& begin) {
2270 BeginPos = begin;
2271}
2272
2273void CFX_Edit_Select::SetEndPos(const CPVT_WordPlace& end) {
2274 EndPos = end;
2275}
2276
tsepez4cf55152016-11-02 14:37:54 -07002277bool CFX_Edit_Select::IsExist() const {
weili625ad662016-06-15 11:21:33 -07002278 return BeginPos != EndPos;
2279}
2280
weili625ad662016-06-15 11:21:33 -07002281CFX_Edit_RectArray::CFX_Edit_RectArray() {}
2282
Tom Sepez3509d162017-01-30 13:22:02 -08002283CFX_Edit_RectArray::~CFX_Edit_RectArray() {}
weili625ad662016-06-15 11:21:33 -07002284
Tom Sepez3509d162017-01-30 13:22:02 -08002285void CFX_Edit_RectArray::Clear() {
2286 m_Rects.clear();
weili625ad662016-06-15 11:21:33 -07002287}
2288
2289void CFX_Edit_RectArray::Add(const CFX_FloatRect& rect) {
2290 // check for overlapped area
Tom Sepez3509d162017-01-30 13:22:02 -08002291 for (const auto& pRect : m_Rects) {
weili625ad662016-06-15 11:21:33 -07002292 if (pRect && pRect->Contains(rect))
2293 return;
2294 }
Tom Sepez3509d162017-01-30 13:22:02 -08002295 m_Rects.push_back(pdfium::MakeUnique<CFX_FloatRect>(rect));
weili625ad662016-06-15 11:21:33 -07002296}
2297
2298int32_t CFX_Edit_RectArray::GetSize() const {
Tom Sepez3509d162017-01-30 13:22:02 -08002299 return pdfium::CollectionSize<int32_t>(m_Rects);
weili625ad662016-06-15 11:21:33 -07002300}
2301
2302CFX_FloatRect* CFX_Edit_RectArray::GetAt(int32_t nIndex) const {
Tom Sepez3509d162017-01-30 13:22:02 -08002303 if (nIndex < 0 || nIndex >= GetSize())
weili625ad662016-06-15 11:21:33 -07002304 return nullptr;
2305
Tom Sepez3509d162017-01-30 13:22:02 -08002306 return m_Rects[nIndex].get();
weili625ad662016-06-15 11:21:33 -07002307}