blob: 41019b3459eab136a30ff6e28ec632c4a0448593 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
dsinclair0bb385b2016-09-29 17:03:59 -07007#include "fpdfsdk/fxedit/fxet_edit.h"
Lei Zhangc2fb35f2016-01-05 16:46:58 -08008
Lei Zhang375a8642016-01-11 11:59:17 -08009#include <algorithm>
thestigd4c34f22016-09-28 17:04:51 -070010#include <memory>
Henrique Nakashima2e2da132017-06-27 13:43:22 -040011#include <sstream>
thestigd4c34f22016-09-28 17:04:51 -070012#include <utility>
Lei Zhang375a8642016-01-11 11:59:17 -080013
dsinclairbc5e6d22016-10-04 11:08:49 -070014#include "core/fpdfapi/font/cpdf_font.h"
dsinclair41872fa2016-10-04 11:29:35 -070015#include "core/fpdfapi/page/cpdf_pageobject.h"
16#include "core/fpdfapi/page/cpdf_pageobjectholder.h"
17#include "core/fpdfapi/page/cpdf_pathobject.h"
18#include "core/fpdfapi/page/cpdf_textobject.h"
dsinclair488b7ad2016-10-04 11:55:50 -070019#include "core/fpdfapi/parser/fpdf_parser_decode.h"
dsinclair69d9c682016-10-04 12:18:35 -070020#include "core/fpdfapi/render/cpdf_renderoptions.h"
21#include "core/fpdfapi/render/cpdf_textrenderer.h"
dsinclair1727aee2016-09-29 13:12:56 -070022#include "core/fpdfdoc/cpvt_section.h"
23#include "core/fpdfdoc/cpvt_word.h"
24#include "core/fpdfdoc/ipvt_fontmap.h"
Dan Sinclairf51a02a2017-04-19 12:46:53 -040025#include "core/fxcrt/fx_codepage.h"
dsinclair74a34fc2016-09-29 16:41:42 -070026#include "core/fxge/cfx_graphstatedata.h"
27#include "core/fxge/cfx_pathdata.h"
28#include "core/fxge/cfx_renderdevice.h"
dsinclaire35af1e2016-07-13 11:26:20 -070029#include "fpdfsdk/cfx_systemhandler.h"
dsinclair0bb385b2016-09-29 17:03:59 -070030#include "fpdfsdk/fxedit/fx_edit.h"
Lei Zhang633a3b72017-06-02 15:27:22 -070031#include "fpdfsdk/pdfwindow/cpwl_edit.h"
32#include "fpdfsdk/pdfwindow/cpwl_edit_ctrl.h"
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -040033#include "fpdfsdk/pdfwindow/cpwl_scroll_bar.h"
tsepez36eb4bd2016-10-03 15:24:27 -070034#include "third_party/base/ptr_util.h"
Tom Sepez3509d162017-01-30 13:22:02 -080035#include "third_party/base/stl_util.h"
dsinclaire35af1e2016-07-13 11:26:20 -070036
dsinclair8f4bf9a2016-05-04 13:51:51 -070037namespace {
38
39const int kEditUndoMaxItems = 10000;
40
dsinclaire35af1e2016-07-13 11:26:20 -070041void DrawTextString(CFX_RenderDevice* pDevice,
Dan Sinclairf528eee2017-02-14 11:52:07 -050042 const CFX_PointF& pt,
dsinclaire35af1e2016-07-13 11:26:20 -070043 CPDF_Font* pFont,
Dan Sinclair05df0752017-03-14 14:43:42 -040044 float fFontSize,
dsinclaire35af1e2016-07-13 11:26:20 -070045 CFX_Matrix* pUser2Device,
46 const CFX_ByteString& str,
47 FX_ARGB crTextFill,
dsinclaire35af1e2016-07-13 11:26:20 -070048 int32_t nHorzScale) {
Dan Sinclaira0061af2017-02-23 09:25:17 -050049 CFX_PointF pos = pUser2Device->Transform(pt);
dsinclaire35af1e2016-07-13 11:26:20 -070050
51 if (pFont) {
52 if (nHorzScale != 100) {
53 CFX_Matrix mt(nHorzScale / 100.0f, 0, 0, 1, 0, 0);
54 mt.Concat(*pUser2Device);
55
56 CPDF_RenderOptions ro;
57 ro.m_Flags = RENDER_CLEARTYPE;
Dan Sinclairf55e72e2017-07-13 14:53:28 -040058 ro.m_ColorMode = CPDF_RenderOptions::kNormal;
dsinclaire35af1e2016-07-13 11:26:20 -070059
Dan Sinclaira0061af2017-02-23 09:25:17 -050060 CPDF_TextRenderer::DrawTextString(pDevice, pos.x, pos.y, pFont, fFontSize,
61 &mt, str, crTextFill, nullptr, &ro);
dsinclaire35af1e2016-07-13 11:26:20 -070062 } else {
63 CPDF_RenderOptions ro;
64 ro.m_Flags = RENDER_CLEARTYPE;
Dan Sinclairf55e72e2017-07-13 14:53:28 -040065 ro.m_ColorMode = CPDF_RenderOptions::kNormal;
dsinclaire35af1e2016-07-13 11:26:20 -070066
Dan Sinclaira0061af2017-02-23 09:25:17 -050067 CPDF_TextRenderer::DrawTextString(pDevice, pos.x, pos.y, pFont, fFontSize,
68 pUser2Device, str, crTextFill, nullptr,
69 &ro);
dsinclaire35af1e2016-07-13 11:26:20 -070070 }
71 }
72}
73
dsinclair8f4bf9a2016-05-04 13:51:51 -070074} // namespace
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070075
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076CFX_Edit_Iterator::CFX_Edit_Iterator(CFX_Edit* pEdit,
dsinclairc7a73492016-04-05 12:01:42 -070077 CPDF_VariableText::Iterator* pVTIterator)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 : m_pEdit(pEdit), m_pVTIterator(pVTIterator) {}
79
80CFX_Edit_Iterator::~CFX_Edit_Iterator() {}
81
tsepez4cf55152016-11-02 14:37:54 -070082bool CFX_Edit_Iterator::NextWord() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083 return m_pVTIterator->NextWord();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070084}
85
tsepez4cf55152016-11-02 14:37:54 -070086bool CFX_Edit_Iterator::PrevWord() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087 return m_pVTIterator->PrevWord();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088}
89
tsepez4cf55152016-11-02 14:37:54 -070090bool CFX_Edit_Iterator::GetWord(CPVT_Word& word) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 ASSERT(m_pEdit);
92
93 if (m_pVTIterator->GetWord(word)) {
94 word.ptWord = m_pEdit->VTToEdit(word.ptWord);
tsepez4cf55152016-11-02 14:37:54 -070095 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 }
tsepez4cf55152016-11-02 14:37:54 -070097 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070098}
99
tsepez4cf55152016-11-02 14:37:54 -0700100bool CFX_Edit_Iterator::GetLine(CPVT_Line& line) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 ASSERT(m_pEdit);
102
103 if (m_pVTIterator->GetLine(line)) {
104 line.ptLine = m_pEdit->VTToEdit(line.ptLine);
tsepez4cf55152016-11-02 14:37:54 -0700105 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106 }
tsepez4cf55152016-11-02 14:37:54 -0700107 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700108}
109
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110void CFX_Edit_Iterator::SetAt(int32_t nWordIndex) {
111 m_pVTIterator->SetAt(nWordIndex);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700112}
113
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114void CFX_Edit_Iterator::SetAt(const CPVT_WordPlace& place) {
115 m_pVTIterator->SetAt(place);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700116}
117
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118const CPVT_WordPlace& CFX_Edit_Iterator::GetAt() const {
119 return m_pVTIterator->GetAt();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700120}
121
dsinclairc7a73492016-04-05 12:01:42 -0700122CFX_Edit_Provider::CFX_Edit_Provider(IPVT_FontMap* pFontMap)
123 : CPDF_VariableText::Provider(pFontMap), m_pFontMap(pFontMap) {
Lei Zhang96660d62015-12-14 18:27:25 -0800124 ASSERT(m_pFontMap);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700125}
126
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127CFX_Edit_Provider::~CFX_Edit_Provider() {}
128
Tom Sepezd0409af2017-05-25 15:53:57 -0700129IPVT_FontMap* CFX_Edit_Provider::GetFontMap() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 return m_pFontMap;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700131}
132
npm41d6bbe2016-09-14 11:54:44 -0700133int32_t CFX_Edit_Provider::GetCharWidth(int32_t nFontIndex, uint16_t word) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134 if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex)) {
tsepezc3255f52016-03-25 14:52:27 -0700135 uint32_t charcode = word;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700136
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700137 if (pPDFFont->IsUnicodeCompatible())
138 charcode = pPDFFont->CharCodeFromUnicode(word);
139 else
140 charcode = m_pFontMap->CharCodeFromUnicode(nFontIndex, word);
141
Wei Li89409932016-03-28 10:33:33 -0700142 if (charcode != CPDF_Font::kInvalidCharCode)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700143 return pPDFFont->GetCharWidthF(charcode);
144 }
145
146 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700147}
148
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149int32_t CFX_Edit_Provider::GetTypeAscent(int32_t nFontIndex) {
150 if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex))
151 return pPDFFont->GetTypeAscent();
152
153 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700154}
155
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156int32_t CFX_Edit_Provider::GetTypeDescent(int32_t nFontIndex) {
157 if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex))
158 return pPDFFont->GetTypeDescent();
159
160 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700161}
162
Tom Sepez62a70f92016-03-21 15:00:20 -0700163int32_t CFX_Edit_Provider::GetWordFontIndex(uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700164 int32_t charset,
165 int32_t nFontIndex) {
166 return m_pFontMap->GetWordFontIndex(word, charset, nFontIndex);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700167}
168
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169int32_t CFX_Edit_Provider::GetDefaultFontIndex() {
170 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700171}
172
tsepez4cf55152016-11-02 14:37:54 -0700173bool CFX_Edit_Provider::IsLatinWord(uint16_t word) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700174 return FX_EDIT_ISLATINWORD(word);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700175}
176
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177CFX_Edit_Refresh::CFX_Edit_Refresh() {}
178
179CFX_Edit_Refresh::~CFX_Edit_Refresh() {}
180
181void CFX_Edit_Refresh::BeginRefresh() {
Tom Sepez3509d162017-01-30 13:22:02 -0800182 m_RefreshRects.Clear();
183 m_OldLineRects = std::move(m_NewLineRects);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700184}
185
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186void CFX_Edit_Refresh::Push(const CPVT_WordRange& linerange,
Tom Sepez281a9ea2016-02-26 14:24:28 -0800187 const CFX_FloatRect& rect) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188 m_NewLineRects.Add(linerange, rect);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700189}
190
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191void CFX_Edit_Refresh::NoAnalyse() {
192 {
193 for (int32_t i = 0, sz = m_OldLineRects.GetSize(); i < sz; i++)
194 if (CFX_Edit_LineRect* pOldRect = m_OldLineRects.GetAt(i))
195 m_RefreshRects.Add(pOldRect->m_rcLine);
196 }
197
198 {
199 for (int32_t i = 0, sz = m_NewLineRects.GetSize(); i < sz; i++)
200 if (CFX_Edit_LineRect* pNewRect = m_NewLineRects.GetAt(i))
201 m_RefreshRects.Add(pNewRect->m_rcLine);
202 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700203}
204
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205const CFX_Edit_RectArray* CFX_Edit_Refresh::GetRefreshRects() const {
206 return &m_RefreshRects;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700207}
208
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209void CFX_Edit_Refresh::EndRefresh() {
Tom Sepez3509d162017-01-30 13:22:02 -0800210 m_RefreshRects.Clear();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700211}
212
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213CFX_Edit_Undo::CFX_Edit_Undo(int32_t nBufsize)
214 : m_nCurUndoPos(0),
215 m_nBufSize(nBufsize),
tsepez4cf55152016-11-02 14:37:54 -0700216 m_bWorking(false) {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700217
218CFX_Edit_Undo::~CFX_Edit_Undo() {
219 Reset();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700220}
221
tsepez4cf55152016-11-02 14:37:54 -0700222bool CFX_Edit_Undo::CanUndo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223 return m_nCurUndoPos > 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700224}
225
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700226void CFX_Edit_Undo::Undo() {
tsepez4cf55152016-11-02 14:37:54 -0700227 m_bWorking = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228 if (m_nCurUndoPos > 0) {
Tom Sepez3509d162017-01-30 13:22:02 -0800229 m_UndoItemStack[m_nCurUndoPos - 1]->Undo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700230 m_nCurUndoPos--;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231 }
tsepez4cf55152016-11-02 14:37:54 -0700232 m_bWorking = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700233}
234
tsepez4cf55152016-11-02 14:37:54 -0700235bool CFX_Edit_Undo::CanRedo() const {
Tom Sepez3509d162017-01-30 13:22:02 -0800236 return m_nCurUndoPos < m_UndoItemStack.size();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700237}
238
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239void CFX_Edit_Undo::Redo() {
tsepez4cf55152016-11-02 14:37:54 -0700240 m_bWorking = true;
Tom Sepez3509d162017-01-30 13:22:02 -0800241 if (m_nCurUndoPos < m_UndoItemStack.size()) {
242 m_UndoItemStack[m_nCurUndoPos]->Redo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 m_nCurUndoPos++;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244 }
tsepez4cf55152016-11-02 14:37:54 -0700245 m_bWorking = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700246}
247
Tom Sepez3509d162017-01-30 13:22:02 -0800248void CFX_Edit_Undo::AddItem(std::unique_ptr<IFX_Edit_UndoItem> pItem) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249 ASSERT(!m_bWorking);
Lei Zhang96660d62015-12-14 18:27:25 -0800250 ASSERT(pItem);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251 ASSERT(m_nBufSize > 1);
Tom Sepez3509d162017-01-30 13:22:02 -0800252 if (m_nCurUndoPos < m_UndoItemStack.size())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253 RemoveTails();
254
Lei Zhang1a89e362017-03-23 15:27:25 -0700255 if (m_UndoItemStack.size() >= m_nBufSize)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700256 RemoveHeads();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257
Tom Sepez3509d162017-01-30 13:22:02 -0800258 m_UndoItemStack.push_back(std::move(pItem));
259 m_nCurUndoPos = m_UndoItemStack.size();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700260}
261
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262void CFX_Edit_Undo::RemoveHeads() {
Tom Sepez3509d162017-01-30 13:22:02 -0800263 ASSERT(m_UndoItemStack.size() > 1);
264 m_UndoItemStack.pop_front();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700265}
266
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267void CFX_Edit_Undo::RemoveTails() {
Tom Sepez3509d162017-01-30 13:22:02 -0800268 while (m_UndoItemStack.size() > m_nCurUndoPos)
269 m_UndoItemStack.pop_back();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270}
271
272void CFX_Edit_Undo::Reset() {
Tom Sepez3509d162017-01-30 13:22:02 -0800273 m_UndoItemStack.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274 m_nCurUndoPos = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275}
276
tsepez4cf55152016-11-02 14:37:54 -0700277CFX_Edit_UndoItem::CFX_Edit_UndoItem() : m_bFirst(true), m_bLast(true) {}
weili625ad662016-06-15 11:21:33 -0700278
279CFX_Edit_UndoItem::~CFX_Edit_UndoItem() {}
280
Tom Sepez3509d162017-01-30 13:22:02 -0800281CFX_WideString CFX_Edit_UndoItem::GetUndoTitle() const {
282 return CFX_WideString();
weili625ad662016-06-15 11:21:33 -0700283}
284
tsepez4cf55152016-11-02 14:37:54 -0700285void CFX_Edit_UndoItem::SetFirst(bool bFirst) {
weili625ad662016-06-15 11:21:33 -0700286 m_bFirst = bFirst;
287}
288
tsepez4cf55152016-11-02 14:37:54 -0700289void CFX_Edit_UndoItem::SetLast(bool bLast) {
weili625ad662016-06-15 11:21:33 -0700290 m_bLast = bLast;
291}
292
tsepez4cf55152016-11-02 14:37:54 -0700293bool CFX_Edit_UndoItem::IsLast() {
weili625ad662016-06-15 11:21:33 -0700294 return m_bLast;
295}
296
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700297CFXEU_InsertWord::CFXEU_InsertWord(CFX_Edit* pEdit,
298 const CPVT_WordPlace& wpOldPlace,
299 const CPVT_WordPlace& wpNewPlace,
Tom Sepez62a70f92016-03-21 15:00:20 -0700300 uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700301 int32_t charset,
302 const CPVT_WordProps* pWordProps)
303 : m_pEdit(pEdit),
304 m_wpOld(wpOldPlace),
305 m_wpNew(wpNewPlace),
306 m_Word(word),
307 m_nCharset(charset),
308 m_WordProps() {
309 if (pWordProps)
310 m_WordProps = *pWordProps;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700311}
312
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313CFXEU_InsertWord::~CFXEU_InsertWord() {}
314
315void CFXEU_InsertWord::Redo() {
316 if (m_pEdit) {
317 m_pEdit->SelectNone();
318 m_pEdit->SetCaret(m_wpOld);
tsepez4cf55152016-11-02 14:37:54 -0700319 m_pEdit->InsertWord(m_Word, m_nCharset, &m_WordProps, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700321}
322
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700323void CFXEU_InsertWord::Undo() {
324 if (m_pEdit) {
325 m_pEdit->SelectNone();
326 m_pEdit->SetCaret(m_wpNew);
tsepez4cf55152016-11-02 14:37:54 -0700327 m_pEdit->Backspace(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700328 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700329}
330
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700331CFXEU_InsertReturn::CFXEU_InsertReturn(CFX_Edit* pEdit,
332 const CPVT_WordPlace& wpOldPlace,
333 const CPVT_WordPlace& wpNewPlace,
334 const CPVT_SecProps* pSecProps,
335 const CPVT_WordProps* pWordProps)
336 : m_pEdit(pEdit),
337 m_wpOld(wpOldPlace),
338 m_wpNew(wpNewPlace),
339 m_SecProps(),
340 m_WordProps() {
341 if (pSecProps)
342 m_SecProps = *pSecProps;
343 if (pWordProps)
344 m_WordProps = *pWordProps;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700345}
346
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700347CFXEU_InsertReturn::~CFXEU_InsertReturn() {}
348
349void CFXEU_InsertReturn::Redo() {
350 if (m_pEdit) {
351 m_pEdit->SelectNone();
352 m_pEdit->SetCaret(m_wpOld);
tsepez4cf55152016-11-02 14:37:54 -0700353 m_pEdit->InsertReturn(&m_SecProps, &m_WordProps, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700354 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700355}
356
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700357void CFXEU_InsertReturn::Undo() {
358 if (m_pEdit) {
359 m_pEdit->SelectNone();
360 m_pEdit->SetCaret(m_wpNew);
tsepez4cf55152016-11-02 14:37:54 -0700361 m_pEdit->Backspace(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700362 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700363}
364
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700365CFXEU_Backspace::CFXEU_Backspace(CFX_Edit* pEdit,
366 const CPVT_WordPlace& wpOldPlace,
367 const CPVT_WordPlace& wpNewPlace,
Tom Sepez62a70f92016-03-21 15:00:20 -0700368 uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700369 int32_t charset,
370 const CPVT_SecProps& SecProps,
371 const CPVT_WordProps& WordProps)
372 : m_pEdit(pEdit),
373 m_wpOld(wpOldPlace),
374 m_wpNew(wpNewPlace),
375 m_Word(word),
376 m_nCharset(charset),
377 m_SecProps(SecProps),
378 m_WordProps(WordProps) {}
379
380CFXEU_Backspace::~CFXEU_Backspace() {}
381
382void CFXEU_Backspace::Redo() {
Tom Sepez52f69b32017-03-21 13:42:38 -0700383 if (!m_pEdit)
384 return;
385
386 m_pEdit->SelectNone();
387 m_pEdit->SetCaret(m_wpOld);
388 m_pEdit->Backspace(false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700389}
390
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700391void CFXEU_Backspace::Undo() {
Tom Sepez52f69b32017-03-21 13:42:38 -0700392 if (!m_pEdit)
393 return;
394
395 m_pEdit->SelectNone();
396 m_pEdit->SetCaret(m_wpNew);
397 if (m_wpNew.nSecIndex != m_wpOld.nSecIndex)
398 m_pEdit->InsertReturn(&m_SecProps, &m_WordProps, false, true);
399 else
400 m_pEdit->InsertWord(m_Word, m_nCharset, &m_WordProps, false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700401}
402
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403CFXEU_Delete::CFXEU_Delete(CFX_Edit* pEdit,
404 const CPVT_WordPlace& wpOldPlace,
405 const CPVT_WordPlace& wpNewPlace,
Tom Sepez62a70f92016-03-21 15:00:20 -0700406 uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700407 int32_t charset,
408 const CPVT_SecProps& SecProps,
409 const CPVT_WordProps& WordProps,
tsepez4cf55152016-11-02 14:37:54 -0700410 bool bSecEnd)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700411 : m_pEdit(pEdit),
412 m_wpOld(wpOldPlace),
413 m_wpNew(wpNewPlace),
414 m_Word(word),
415 m_nCharset(charset),
416 m_SecProps(SecProps),
417 m_WordProps(WordProps),
418 m_bSecEnd(bSecEnd) {}
419
420CFXEU_Delete::~CFXEU_Delete() {}
421
422void CFXEU_Delete::Redo() {
423 if (m_pEdit) {
424 m_pEdit->SelectNone();
425 m_pEdit->SetCaret(m_wpOld);
tsepez4cf55152016-11-02 14:37:54 -0700426 m_pEdit->Delete(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700427 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700428}
429
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700430void CFXEU_Delete::Undo() {
431 if (m_pEdit) {
432 m_pEdit->SelectNone();
433 m_pEdit->SetCaret(m_wpNew);
434 if (m_bSecEnd) {
tsepez4cf55152016-11-02 14:37:54 -0700435 m_pEdit->InsertReturn(&m_SecProps, &m_WordProps, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700436 } else {
tsepez4cf55152016-11-02 14:37:54 -0700437 m_pEdit->InsertWord(m_Word, m_nCharset, &m_WordProps, false, true);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700438 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700439 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700440}
441
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700442CFXEU_Clear::CFXEU_Clear(CFX_Edit* pEdit,
443 const CPVT_WordRange& wrSel,
444 const CFX_WideString& swText)
445 : m_pEdit(pEdit), m_wrSel(wrSel), m_swText(swText) {}
446
447CFXEU_Clear::~CFXEU_Clear() {}
448
449void CFXEU_Clear::Redo() {
450 if (m_pEdit) {
451 m_pEdit->SelectNone();
452 m_pEdit->SetSel(m_wrSel.BeginPos, m_wrSel.EndPos);
tsepez4cf55152016-11-02 14:37:54 -0700453 m_pEdit->Clear(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700454 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700455}
456
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700457void CFXEU_Clear::Undo() {
458 if (m_pEdit) {
459 m_pEdit->SelectNone();
460 m_pEdit->SetCaret(m_wrSel.BeginPos);
Dan Sinclairf51a02a2017-04-19 12:46:53 -0400461 m_pEdit->InsertText(m_swText, FX_CHARSET_Default, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700462 m_pEdit->SetSel(m_wrSel.BeginPos, m_wrSel.EndPos);
463 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700464}
465
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700466CFXEU_InsertText::CFXEU_InsertText(CFX_Edit* pEdit,
467 const CPVT_WordPlace& wpOldPlace,
468 const CPVT_WordPlace& wpNewPlace,
469 const CFX_WideString& swText,
dsinclairefd5a992016-07-18 10:04:07 -0700470 int32_t charset)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700471 : m_pEdit(pEdit),
472 m_wpOld(wpOldPlace),
473 m_wpNew(wpNewPlace),
474 m_swText(swText),
dsinclairefd5a992016-07-18 10:04:07 -0700475 m_nCharset(charset) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700476
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700477CFXEU_InsertText::~CFXEU_InsertText() {}
478
479void CFXEU_InsertText::Redo() {
480 if (m_pEdit && IsLast()) {
481 m_pEdit->SelectNone();
482 m_pEdit->SetCaret(m_wpOld);
tsepez4cf55152016-11-02 14:37:54 -0700483 m_pEdit->InsertText(m_swText, m_nCharset, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700484 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700485}
486
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700487void CFXEU_InsertText::Undo() {
488 if (m_pEdit) {
489 m_pEdit->SelectNone();
490 m_pEdit->SetSel(m_wpOld, m_wpNew);
tsepez4cf55152016-11-02 14:37:54 -0700491 m_pEdit->Clear(false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700492 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700493}
494
dsinclaire35af1e2016-07-13 11:26:20 -0700495// static
dsinclaire35af1e2016-07-13 11:26:20 -0700496void CFX_Edit::DrawEdit(CFX_RenderDevice* pDevice,
497 CFX_Matrix* pUser2Device,
498 CFX_Edit* pEdit,
499 FX_COLORREF crTextFill,
dsinclaire35af1e2016-07-13 11:26:20 -0700500 const CFX_FloatRect& rcClip,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500501 const CFX_PointF& ptOffset,
dsinclaire35af1e2016-07-13 11:26:20 -0700502 const CPVT_WordRange* pRange,
503 CFX_SystemHandler* pSystemHandler,
dsinclair8faac622016-09-15 12:41:50 -0700504 CFFL_FormFiller* pFFLData) {
dsinclaire35af1e2016-07-13 11:26:20 -0700505 const bool bContinuous =
506 pEdit->GetCharArray() == 0 && pEdit->GetCharSpace() <= 0.0f;
507 uint16_t SubWord = pEdit->GetPasswordChar();
Dan Sinclair05df0752017-03-14 14:43:42 -0400508 float fFontSize = pEdit->GetFontSize();
dsinclaire35af1e2016-07-13 11:26:20 -0700509 CPVT_WordRange wrSelect = pEdit->GetSelectWordRange();
510 int32_t nHorzScale = pEdit->GetHorzScale();
511
512 FX_COLORREF crCurFill = crTextFill;
513 FX_COLORREF crOldFill = crCurFill;
514
tsepez4cf55152016-11-02 14:37:54 -0700515 bool bSelect = false;
dsinclaire35af1e2016-07-13 11:26:20 -0700516 const FX_COLORREF crWhite = ArgbEncode(255, 255, 255, 255);
517 const FX_COLORREF crSelBK = ArgbEncode(255, 0, 51, 113);
518
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400519 std::ostringstream sTextBuf;
dsinclaire35af1e2016-07-13 11:26:20 -0700520 int32_t nFontIndex = -1;
Dan Sinclairf528eee2017-02-14 11:52:07 -0500521 CFX_PointF ptBT;
Tom Sepez1629f602017-04-21 14:11:26 -0700522 CFX_RenderDevice::StateRestorer restorer(pDevice);
dsinclaire35af1e2016-07-13 11:26:20 -0700523 if (!rcClip.IsEmpty()) {
524 CFX_FloatRect rcTemp = rcClip;
525 pUser2Device->TransformRect(rcTemp);
526 pDevice->SetClip_Rect(rcTemp.ToFxRect());
527 }
528
529 CFX_Edit_Iterator* pIterator = pEdit->GetIterator();
530 if (IPVT_FontMap* pFontMap = pEdit->GetFontMap()) {
531 if (pRange)
532 pIterator->SetAt(pRange->BeginPos);
533 else
534 pIterator->SetAt(0);
535
536 CPVT_WordPlace oldplace;
537 while (pIterator->NextWord()) {
538 CPVT_WordPlace place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -0700539 if (pRange && place > pRange->EndPos)
dsinclaire35af1e2016-07-13 11:26:20 -0700540 break;
541
Tom Sepez52f69b32017-03-21 13:42:38 -0700542 if (!wrSelect.IsEmpty()) {
543 bSelect = place > wrSelect.BeginPos && place <= wrSelect.EndPos;
dsinclaire35af1e2016-07-13 11:26:20 -0700544 crCurFill = bSelect ? crWhite : crTextFill;
545 }
546 if (pSystemHandler && pSystemHandler->IsSelectionImplemented()) {
547 crCurFill = crTextFill;
548 crOldFill = crCurFill;
549 }
550 CPVT_Word word;
551 if (pIterator->GetWord(word)) {
552 if (bSelect) {
553 CPVT_Line line;
554 pIterator->GetLine(line);
555
556 if (pSystemHandler && pSystemHandler->IsSelectionImplemented()) {
557 CFX_FloatRect rc(word.ptWord.x, line.ptLine.y + line.fLineDescent,
558 word.ptWord.x + word.fWidth,
559 line.ptLine.y + line.fLineAscent);
560 rc.Intersect(rcClip);
561 pSystemHandler->OutputSelectedRect(pFFLData, rc);
562 } else {
563 CFX_PathData pathSelBK;
564 pathSelBK.AppendRect(
565 word.ptWord.x, line.ptLine.y + line.fLineDescent,
566 word.ptWord.x + word.fWidth, line.ptLine.y + line.fLineAscent);
567
568 pDevice->DrawPath(&pathSelBK, pUser2Device, nullptr, crSelBK, 0,
569 FXFILL_WINDING);
570 }
571 }
572
573 if (bContinuous) {
574 if (place.LineCmp(oldplace) != 0 || word.nFontIndex != nFontIndex ||
575 crOldFill != crCurFill) {
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400576 if (sTextBuf.tellp() > 0) {
dsinclaire35af1e2016-07-13 11:26:20 -0700577 DrawTextString(
Dan Sinclairf528eee2017-02-14 11:52:07 -0500578 pDevice, CFX_PointF(ptBT.x + ptOffset.x, ptBT.y + ptOffset.y),
dsinclaire35af1e2016-07-13 11:26:20 -0700579 pFontMap->GetPDFFont(nFontIndex), fFontSize, pUser2Device,
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400580 CFX_ByteString(sTextBuf), crOldFill, nHorzScale);
dsinclaire35af1e2016-07-13 11:26:20 -0700581
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400582 sTextBuf.str("");
dsinclaire35af1e2016-07-13 11:26:20 -0700583 }
584 nFontIndex = word.nFontIndex;
585 ptBT = word.ptWord;
586 crOldFill = crCurFill;
587 }
588
589 sTextBuf << GetPDFWordString(pFontMap, word.nFontIndex, word.Word,
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400590 SubWord);
dsinclaire35af1e2016-07-13 11:26:20 -0700591 } else {
592 DrawTextString(
Dan Sinclairf528eee2017-02-14 11:52:07 -0500593 pDevice, CFX_PointF(word.ptWord.x + ptOffset.x,
594 word.ptWord.y + ptOffset.y),
dsinclaire35af1e2016-07-13 11:26:20 -0700595 pFontMap->GetPDFFont(word.nFontIndex), fFontSize, pUser2Device,
596 GetPDFWordString(pFontMap, word.nFontIndex, word.Word, SubWord),
Dan Sinclaira0061af2017-02-23 09:25:17 -0500597 crCurFill, nHorzScale);
dsinclaire35af1e2016-07-13 11:26:20 -0700598 }
599 oldplace = place;
600 }
601 }
602
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400603 if (sTextBuf.tellp() > 0) {
Dan Sinclaira0061af2017-02-23 09:25:17 -0500604 DrawTextString(pDevice,
605 CFX_PointF(ptBT.x + ptOffset.x, ptBT.y + ptOffset.y),
606 pFontMap->GetPDFFont(nFontIndex), fFontSize, pUser2Device,
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400607 CFX_ByteString(sTextBuf), crOldFill, nHorzScale);
dsinclaire35af1e2016-07-13 11:26:20 -0700608 }
609 }
dsinclaire35af1e2016-07-13 11:26:20 -0700610}
611
dsinclaire35af1e2016-07-13 11:26:20 -0700612CFX_Edit::CFX_Edit()
613 : m_pVT(new CPDF_VariableText),
thestig821d59e2016-05-11 12:59:22 -0700614 m_pNotify(nullptr),
615 m_pOprNotify(nullptr),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700616 m_wpCaret(-1, -1, -1),
617 m_wpOldCaret(-1, -1, -1),
618 m_SelState(),
tsepez4cf55152016-11-02 14:37:54 -0700619 m_bEnableScroll(false),
dsinclair8f4bf9a2016-05-04 13:51:51 -0700620 m_Undo(kEditUndoMaxItems),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700621 m_nAlignment(0),
tsepez4cf55152016-11-02 14:37:54 -0700622 m_bNotifyFlag(false),
623 m_bEnableOverflow(false),
624 m_bEnableRefresh(true),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700625 m_rcOldContent(0.0f, 0.0f, 0.0f, 0.0f),
tsepez4cf55152016-11-02 14:37:54 -0700626 m_bEnableUndo(true),
Lei Zhang1a89e362017-03-23 15:27:25 -0700627 m_bOprNotify(false) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700628
Lei Zhang1a89e362017-03-23 15:27:25 -0700629CFX_Edit::~CFX_Edit() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700630
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700631void CFX_Edit::Initialize() {
632 m_pVT->Initialize();
633 SetCaret(m_pVT->GetBeginWordPlace());
634 SetCaretOrigin();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700635}
636
dsinclairc7a73492016-04-05 12:01:42 -0700637void CFX_Edit::SetFontMap(IPVT_FontMap* pFontMap) {
tsepez36eb4bd2016-10-03 15:24:27 -0700638 m_pVTProvider = pdfium::MakeUnique<CFX_Edit_Provider>(pFontMap);
thestig821d59e2016-05-11 12:59:22 -0700639 m_pVT->SetProvider(m_pVTProvider.get());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700640}
641
dsinclaire35af1e2016-07-13 11:26:20 -0700642void CFX_Edit::SetNotify(CPWL_EditCtrl* pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700643 m_pNotify = pNotify;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700644}
645
dsinclaire35af1e2016-07-13 11:26:20 -0700646void CFX_Edit::SetOprNotify(CPWL_Edit* pOprNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700647 m_pOprNotify = pOprNotify;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700648}
649
dsinclaire35af1e2016-07-13 11:26:20 -0700650CFX_Edit_Iterator* CFX_Edit::GetIterator() {
tsepez36eb4bd2016-10-03 15:24:27 -0700651 if (!m_pIterator) {
652 m_pIterator =
653 pdfium::MakeUnique<CFX_Edit_Iterator>(this, m_pVT->GetIterator());
654 }
thestig821d59e2016-05-11 12:59:22 -0700655 return m_pIterator.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700656}
657
dsinclairc7a73492016-04-05 12:01:42 -0700658IPVT_FontMap* CFX_Edit::GetFontMap() {
thestig821d59e2016-05-11 12:59:22 -0700659 return m_pVTProvider ? m_pVTProvider->GetFontMap() : nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700660}
661
dsinclairefd5a992016-07-18 10:04:07 -0700662void CFX_Edit::SetPlateRect(const CFX_FloatRect& rect) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700663 m_pVT->SetPlateRect(rect);
Dan Sinclairf528eee2017-02-14 11:52:07 -0500664 m_ptScrollPos = CFX_PointF(rect.left, rect.top);
dsinclairefd5a992016-07-18 10:04:07 -0700665 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700666}
667
tsepez4cf55152016-11-02 14:37:54 -0700668void CFX_Edit::SetAlignmentH(int32_t nFormat, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700669 m_pVT->SetAlignment(nFormat);
670 if (bPaint)
671 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700672}
673
tsepez4cf55152016-11-02 14:37:54 -0700674void CFX_Edit::SetAlignmentV(int32_t nFormat, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700675 m_nAlignment = nFormat;
676 if (bPaint)
677 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700678}
679
tsepez4cf55152016-11-02 14:37:54 -0700680void CFX_Edit::SetPasswordChar(uint16_t wSubWord, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700681 m_pVT->SetPasswordChar(wSubWord);
682 if (bPaint)
683 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700684}
685
dsinclairefd5a992016-07-18 10:04:07 -0700686void CFX_Edit::SetLimitChar(int32_t nLimitChar) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700687 m_pVT->SetLimitChar(nLimitChar);
dsinclairefd5a992016-07-18 10:04:07 -0700688 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700689}
690
dsinclairefd5a992016-07-18 10:04:07 -0700691void CFX_Edit::SetCharArray(int32_t nCharArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700692 m_pVT->SetCharArray(nCharArray);
dsinclairefd5a992016-07-18 10:04:07 -0700693 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700694}
695
Dan Sinclair05df0752017-03-14 14:43:42 -0400696void CFX_Edit::SetCharSpace(float fCharSpace) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700697 m_pVT->SetCharSpace(fCharSpace);
dsinclairefd5a992016-07-18 10:04:07 -0700698 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700699}
700
tsepez4cf55152016-11-02 14:37:54 -0700701void CFX_Edit::SetMultiLine(bool bMultiLine, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700702 m_pVT->SetMultiLine(bMultiLine);
703 if (bPaint)
704 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700705}
706
tsepez4cf55152016-11-02 14:37:54 -0700707void CFX_Edit::SetAutoReturn(bool bAuto, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700708 m_pVT->SetAutoReturn(bAuto);
709 if (bPaint)
710 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700711}
712
tsepez4cf55152016-11-02 14:37:54 -0700713void CFX_Edit::SetAutoFontSize(bool bAuto, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700714 m_pVT->SetAutoFontSize(bAuto);
715 if (bPaint)
716 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700717}
718
Dan Sinclair05df0752017-03-14 14:43:42 -0400719void CFX_Edit::SetFontSize(float fFontSize) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700720 m_pVT->SetFontSize(fFontSize);
dsinclairefd5a992016-07-18 10:04:07 -0700721 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700722}
723
tsepez4cf55152016-11-02 14:37:54 -0700724void CFX_Edit::SetAutoScroll(bool bAuto, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700725 m_bEnableScroll = bAuto;
726 if (bPaint)
727 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700728}
729
tsepez4cf55152016-11-02 14:37:54 -0700730void CFX_Edit::SetTextOverflow(bool bAllowed, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700731 m_bEnableOverflow = bAllowed;
732 if (bPaint)
733 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700734}
735
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700736void CFX_Edit::SetSel(int32_t nStartChar, int32_t nEndChar) {
737 if (m_pVT->IsValid()) {
738 if (nStartChar == 0 && nEndChar < 0) {
739 SelectAll();
740 } else if (nStartChar < 0) {
741 SelectNone();
742 } else {
743 if (nStartChar < nEndChar) {
744 SetSel(m_pVT->WordIndexToWordPlace(nStartChar),
745 m_pVT->WordIndexToWordPlace(nEndChar));
746 } else {
747 SetSel(m_pVT->WordIndexToWordPlace(nEndChar),
748 m_pVT->WordIndexToWordPlace(nStartChar));
749 }
750 }
751 }
752}
753
754void CFX_Edit::SetSel(const CPVT_WordPlace& begin, const CPVT_WordPlace& end) {
Tom Sepez52f69b32017-03-21 13:42:38 -0700755 if (!m_pVT->IsValid())
756 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700757
Tom Sepez52f69b32017-03-21 13:42:38 -0700758 SelectNone();
759 m_SelState.Set(begin, end);
760 SetCaret(m_SelState.EndPos);
761 ScrollToCaret();
762 if (!m_SelState.IsEmpty())
763 Refresh();
764 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700765}
766
767void CFX_Edit::GetSel(int32_t& nStartChar, int32_t& nEndChar) const {
768 nStartChar = -1;
769 nEndChar = -1;
Tom Sepez52f69b32017-03-21 13:42:38 -0700770 if (!m_pVT->IsValid())
771 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700772
Tom Sepez52f69b32017-03-21 13:42:38 -0700773 if (m_SelState.IsEmpty()) {
774 nStartChar = m_pVT->WordPlaceToWordIndex(m_wpCaret);
775 nEndChar = m_pVT->WordPlaceToWordIndex(m_wpCaret);
776 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700777 }
Tom Sepez52f69b32017-03-21 13:42:38 -0700778 if (m_SelState.BeginPos < m_SelState.EndPos) {
779 nStartChar = m_pVT->WordPlaceToWordIndex(m_SelState.BeginPos);
780 nEndChar = m_pVT->WordPlaceToWordIndex(m_SelState.EndPos);
781 return;
782 }
783 nStartChar = m_pVT->WordPlaceToWordIndex(m_SelState.EndPos);
784 nEndChar = m_pVT->WordPlaceToWordIndex(m_SelState.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700785}
786
787int32_t CFX_Edit::GetCaret() const {
788 if (m_pVT->IsValid())
789 return m_pVT->WordPlaceToWordIndex(m_wpCaret);
790
791 return -1;
792}
793
794CPVT_WordPlace CFX_Edit::GetCaretWordPlace() const {
795 return m_wpCaret;
796}
797
798CFX_WideString CFX_Edit::GetText() const {
799 CFX_WideString swRet;
thestig821d59e2016-05-11 12:59:22 -0700800 if (!m_pVT->IsValid())
801 return swRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700802
thestig821d59e2016-05-11 12:59:22 -0700803 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
804 pIterator->SetAt(0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700805
thestig821d59e2016-05-11 12:59:22 -0700806 CPVT_Word wordinfo;
807 CPVT_WordPlace oldplace = pIterator->GetAt();
808 while (pIterator->NextWord()) {
809 CPVT_WordPlace place = pIterator->GetAt();
thestig821d59e2016-05-11 12:59:22 -0700810 if (pIterator->GetWord(wordinfo))
811 swRet += wordinfo.Word;
Tom Sepez52f69b32017-03-21 13:42:38 -0700812 if (oldplace.nSecIndex != place.nSecIndex)
thestig821d59e2016-05-11 12:59:22 -0700813 swRet += L"\r\n";
thestig821d59e2016-05-11 12:59:22 -0700814 oldplace = place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700815 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700816 return swRet;
817}
818
819CFX_WideString CFX_Edit::GetRangeText(const CPVT_WordRange& range) const {
820 CFX_WideString swRet;
thestig821d59e2016-05-11 12:59:22 -0700821 if (!m_pVT->IsValid())
822 return swRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700823
thestig821d59e2016-05-11 12:59:22 -0700824 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
825 CPVT_WordRange wrTemp = range;
826 m_pVT->UpdateWordPlace(wrTemp.BeginPos);
827 m_pVT->UpdateWordPlace(wrTemp.EndPos);
828 pIterator->SetAt(wrTemp.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700829
thestig821d59e2016-05-11 12:59:22 -0700830 CPVT_Word wordinfo;
831 CPVT_WordPlace oldplace = wrTemp.BeginPos;
832 while (pIterator->NextWord()) {
833 CPVT_WordPlace place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -0700834 if (place > wrTemp.EndPos)
thestig821d59e2016-05-11 12:59:22 -0700835 break;
thestig821d59e2016-05-11 12:59:22 -0700836 if (pIterator->GetWord(wordinfo))
837 swRet += wordinfo.Word;
Tom Sepez52f69b32017-03-21 13:42:38 -0700838 if (oldplace.nSecIndex != place.nSecIndex)
thestig821d59e2016-05-11 12:59:22 -0700839 swRet += L"\r\n";
thestig821d59e2016-05-11 12:59:22 -0700840 oldplace = place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700841 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700842 return swRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700843}
844
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700845CFX_WideString CFX_Edit::GetSelText() const {
846 return GetRangeText(m_SelState.ConvertToWordRange());
847}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700848
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700849int32_t CFX_Edit::GetTotalLines() const {
thestig821d59e2016-05-11 12:59:22 -0700850 int32_t nLines = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700851
thestig821d59e2016-05-11 12:59:22 -0700852 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
853 pIterator->SetAt(0);
854 while (pIterator->NextLine())
855 ++nLines;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700856
thestig821d59e2016-05-11 12:59:22 -0700857 return nLines;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700858}
859
860CPVT_WordRange CFX_Edit::GetSelectWordRange() const {
861 return m_SelState.ConvertToWordRange();
862}
863
tsepeza31da742016-09-08 11:28:14 -0700864void CFX_Edit::SetText(const CFX_WideString& sText) {
dsinclairefd5a992016-07-18 10:04:07 -0700865 Empty();
Dan Sinclairf51a02a2017-04-19 12:46:53 -0400866 DoInsertText(CPVT_WordPlace(0, 0, -1), sText, FX_CHARSET_Default);
dsinclairefd5a992016-07-18 10:04:07 -0700867 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700868}
869
tsepez4cf55152016-11-02 14:37:54 -0700870bool CFX_Edit::InsertWord(uint16_t word, int32_t charset) {
871 return InsertWord(word, charset, nullptr, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700872}
873
tsepez4cf55152016-11-02 14:37:54 -0700874bool CFX_Edit::InsertReturn() {
875 return InsertReturn(nullptr, nullptr, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700876}
877
tsepez4cf55152016-11-02 14:37:54 -0700878bool CFX_Edit::Backspace() {
879 return Backspace(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700880}
881
tsepez4cf55152016-11-02 14:37:54 -0700882bool CFX_Edit::Delete() {
883 return Delete(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700884}
885
tsepez4cf55152016-11-02 14:37:54 -0700886bool CFX_Edit::Clear() {
887 return Clear(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700888}
889
tsepez4cf55152016-11-02 14:37:54 -0700890bool CFX_Edit::InsertText(const CFX_WideString& sText, int32_t charset) {
891 return InsertText(sText, charset, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700892}
893
Dan Sinclair05df0752017-03-14 14:43:42 -0400894float CFX_Edit::GetFontSize() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700895 return m_pVT->GetFontSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700896}
897
Tom Sepez62a70f92016-03-21 15:00:20 -0700898uint16_t CFX_Edit::GetPasswordChar() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700899 return m_pVT->GetPasswordChar();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700900}
901
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700902int32_t CFX_Edit::GetCharArray() const {
903 return m_pVT->GetCharArray();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700904}
905
Tom Sepez281a9ea2016-02-26 14:24:28 -0800906CFX_FloatRect CFX_Edit::GetContentRect() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700907 return VTToEdit(m_pVT->GetContentRect());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700908}
909
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700910int32_t CFX_Edit::GetHorzScale() const {
911 return m_pVT->GetHorzScale();
912}
913
Dan Sinclair05df0752017-03-14 14:43:42 -0400914float CFX_Edit::GetCharSpace() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700915 return m_pVT->GetCharSpace();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700916}
917
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700918CPVT_WordRange CFX_Edit::GetWholeWordRange() const {
919 if (m_pVT->IsValid())
920 return CPVT_WordRange(m_pVT->GetBeginWordPlace(), m_pVT->GetEndWordPlace());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700921
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700922 return CPVT_WordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700923}
924
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700925CPVT_WordRange CFX_Edit::GetVisibleWordRange() const {
926 if (m_bEnableOverflow)
927 return GetWholeWordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700928
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700929 if (m_pVT->IsValid()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800930 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700931
Dan Sinclairf528eee2017-02-14 11:52:07 -0500932 CPVT_WordPlace place1 =
933 m_pVT->SearchWordPlace(EditToVT(CFX_PointF(rcPlate.left, rcPlate.top)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700934 CPVT_WordPlace place2 = m_pVT->SearchWordPlace(
Dan Sinclairf528eee2017-02-14 11:52:07 -0500935 EditToVT(CFX_PointF(rcPlate.right, rcPlate.bottom)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700936
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700937 return CPVT_WordRange(place1, place2);
938 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700939
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700940 return CPVT_WordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700941}
942
Dan Sinclairf528eee2017-02-14 11:52:07 -0500943CPVT_WordPlace CFX_Edit::SearchWordPlace(const CFX_PointF& point) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700944 if (m_pVT->IsValid()) {
945 return m_pVT->SearchWordPlace(EditToVT(point));
946 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700947
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700948 return CPVT_WordPlace();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700949}
950
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700951void CFX_Edit::Paint() {
952 if (m_pVT->IsValid()) {
953 RearrangeAll();
954 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -0700955 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700956 SetCaretOrigin();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700957 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700958 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700959}
960
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700961void CFX_Edit::RearrangeAll() {
962 if (m_pVT->IsValid()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700963 m_pVT->UpdateWordPlace(m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700964 m_pVT->RearrangeAll();
965 m_pVT->UpdateWordPlace(m_wpCaret);
966 SetScrollInfo();
967 SetContentChanged();
968 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700969}
970
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700971void CFX_Edit::RearrangePart(const CPVT_WordRange& range) {
972 if (m_pVT->IsValid()) {
973 m_pVT->UpdateWordPlace(m_wpCaret);
974 m_pVT->RearrangePart(range);
975 m_pVT->UpdateWordPlace(m_wpCaret);
976 SetScrollInfo();
977 SetContentChanged();
978 }
979}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700980
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700981void CFX_Edit::SetContentChanged() {
dsinclaira2919b32016-07-13 10:55:48 -0700982 if (m_pNotify) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800983 CFX_FloatRect rcContent = m_pVT->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700984 if (rcContent.Width() != m_rcOldContent.Width() ||
985 rcContent.Height() != m_rcOldContent.Height()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700986 m_rcOldContent = rcContent;
987 }
988 }
989}
990
991void CFX_Edit::SelectAll() {
Tom Sepez52f69b32017-03-21 13:42:38 -0700992 if (!m_pVT->IsValid())
993 return;
994 m_SelState = CFX_Edit_Select(GetWholeWordRange());
995 SetCaret(m_SelState.EndPos);
996 ScrollToCaret();
997 Refresh();
998 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700999}
1000
1001void CFX_Edit::SelectNone() {
Tom Sepez52f69b32017-03-21 13:42:38 -07001002 if (!m_pVT->IsValid() || m_SelState.IsEmpty())
1003 return;
1004
1005 m_SelState.Reset();
1006 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001007}
1008
tsepez4cf55152016-11-02 14:37:54 -07001009bool CFX_Edit::IsSelected() const {
Tom Sepez52f69b32017-03-21 13:42:38 -07001010 return !m_SelState.IsEmpty();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001011}
1012
Dan Sinclairf528eee2017-02-14 11:52:07 -05001013CFX_PointF CFX_Edit::VTToEdit(const CFX_PointF& point) const {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001014 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1015 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001016
Dan Sinclair05df0752017-03-14 14:43:42 -04001017 float fPadding = 0.0f;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001018
1019 switch (m_nAlignment) {
1020 case 0:
1021 fPadding = 0.0f;
1022 break;
1023 case 1:
1024 fPadding = (rcPlate.Height() - rcContent.Height()) * 0.5f;
1025 break;
1026 case 2:
1027 fPadding = rcPlate.Height() - rcContent.Height();
1028 break;
1029 }
1030
Dan Sinclairf528eee2017-02-14 11:52:07 -05001031 return CFX_PointF(point.x - (m_ptScrollPos.x - rcPlate.left),
1032 point.y - (m_ptScrollPos.y + fPadding - rcPlate.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001033}
1034
Dan Sinclairf528eee2017-02-14 11:52:07 -05001035CFX_PointF CFX_Edit::EditToVT(const CFX_PointF& point) const {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001036 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1037 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001038
Dan Sinclair05df0752017-03-14 14:43:42 -04001039 float fPadding = 0.0f;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001040
1041 switch (m_nAlignment) {
1042 case 0:
1043 fPadding = 0.0f;
1044 break;
1045 case 1:
1046 fPadding = (rcPlate.Height() - rcContent.Height()) * 0.5f;
1047 break;
1048 case 2:
1049 fPadding = rcPlate.Height() - rcContent.Height();
1050 break;
1051 }
1052
Dan Sinclairf528eee2017-02-14 11:52:07 -05001053 return CFX_PointF(point.x + (m_ptScrollPos.x - rcPlate.left),
1054 point.y + (m_ptScrollPos.y + fPadding - rcPlate.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001055}
1056
Tom Sepez281a9ea2016-02-26 14:24:28 -08001057CFX_FloatRect CFX_Edit::VTToEdit(const CFX_FloatRect& rect) const {
Dan Sinclairf528eee2017-02-14 11:52:07 -05001058 CFX_PointF ptLeftBottom = VTToEdit(CFX_PointF(rect.left, rect.bottom));
1059 CFX_PointF ptRightTop = VTToEdit(CFX_PointF(rect.right, rect.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001060
Tom Sepez281a9ea2016-02-26 14:24:28 -08001061 return CFX_FloatRect(ptLeftBottom.x, ptLeftBottom.y, ptRightTop.x,
1062 ptRightTop.y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001063}
1064
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001065void CFX_Edit::SetScrollInfo() {
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001066 if (!m_pNotify)
1067 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001068
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001069 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
1070 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1071 if (m_bNotifyFlag)
1072 return;
1073
1074 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
1075 m_bNotifyFlag = true;
1076
1077 PWL_SCROLL_INFO Info;
1078 Info.fPlateWidth = rcPlate.top - rcPlate.bottom;
1079 Info.fContentMin = rcContent.bottom;
1080 Info.fContentMax = rcContent.top;
1081 Info.fSmallStep = rcPlate.Height() / 3;
1082 Info.fBigStep = rcPlate.Height();
1083 m_pNotify->SetScrollInfo(Info);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001084}
1085
Dan Sinclair05df0752017-03-14 14:43:42 -04001086void CFX_Edit::SetScrollPosX(float fx) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001087 if (!m_bEnableScroll)
1088 return;
1089
1090 if (m_pVT->IsValid()) {
dsinclair448c4332016-08-02 12:07:35 -07001091 if (!IsFloatEqual(m_ptScrollPos.x, fx)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001092 m_ptScrollPos.x = fx;
dsinclairefd5a992016-07-18 10:04:07 -07001093 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001094 }
1095 }
1096}
1097
Dan Sinclair05df0752017-03-14 14:43:42 -04001098void CFX_Edit::SetScrollPosY(float fy) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001099 if (!m_bEnableScroll)
1100 return;
1101
1102 if (m_pVT->IsValid()) {
dsinclair448c4332016-08-02 12:07:35 -07001103 if (!IsFloatEqual(m_ptScrollPos.y, fy)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001104 m_ptScrollPos.y = fy;
dsinclairefd5a992016-07-18 10:04:07 -07001105 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001106
dsinclaira2919b32016-07-13 10:55:48 -07001107 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001108 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001109 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001110 m_bNotifyFlag = true;
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001111 m_pNotify->SetScrollPosition(fy);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001112 }
1113 }
1114 }
1115 }
1116}
1117
Dan Sinclairf528eee2017-02-14 11:52:07 -05001118void CFX_Edit::SetScrollPos(const CFX_PointF& point) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001119 SetScrollPosX(point.x);
1120 SetScrollPosY(point.y);
1121 SetScrollLimit();
1122 SetCaretInfo();
1123}
1124
Dan Sinclairf528eee2017-02-14 11:52:07 -05001125CFX_PointF CFX_Edit::GetScrollPos() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001126 return m_ptScrollPos;
1127}
1128
1129void CFX_Edit::SetScrollLimit() {
1130 if (m_pVT->IsValid()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001131 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1132 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001133
1134 if (rcPlate.Width() > rcContent.Width()) {
1135 SetScrollPosX(rcPlate.left);
1136 } else {
dsinclair448c4332016-08-02 12:07:35 -07001137 if (IsFloatSmaller(m_ptScrollPos.x, rcContent.left)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001138 SetScrollPosX(rcContent.left);
dsinclair448c4332016-08-02 12:07:35 -07001139 } else if (IsFloatBigger(m_ptScrollPos.x,
1140 rcContent.right - rcPlate.Width())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001141 SetScrollPosX(rcContent.right - rcPlate.Width());
1142 }
1143 }
1144
1145 if (rcPlate.Height() > rcContent.Height()) {
1146 SetScrollPosY(rcPlate.top);
1147 } else {
dsinclair448c4332016-08-02 12:07:35 -07001148 if (IsFloatSmaller(m_ptScrollPos.y,
1149 rcContent.bottom + rcPlate.Height())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001150 SetScrollPosY(rcContent.bottom + rcPlate.Height());
dsinclair448c4332016-08-02 12:07:35 -07001151 } else if (IsFloatBigger(m_ptScrollPos.y, rcContent.top)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001152 SetScrollPosY(rcContent.top);
1153 }
1154 }
1155 }
1156}
1157
1158void CFX_Edit::ScrollToCaret() {
1159 SetScrollLimit();
1160
thestig821d59e2016-05-11 12:59:22 -07001161 if (!m_pVT->IsValid())
1162 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001163
thestig821d59e2016-05-11 12:59:22 -07001164 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1165 pIterator->SetAt(m_wpCaret);
1166
Dan Sinclairf528eee2017-02-14 11:52:07 -05001167 CFX_PointF ptHead;
1168 CFX_PointF ptFoot;
thestig821d59e2016-05-11 12:59:22 -07001169 CPVT_Word word;
1170 CPVT_Line line;
1171 if (pIterator->GetWord(word)) {
1172 ptHead.x = word.ptWord.x + word.fWidth;
1173 ptHead.y = word.ptWord.y + word.fAscent;
1174 ptFoot.x = word.ptWord.x + word.fWidth;
1175 ptFoot.y = word.ptWord.y + word.fDescent;
1176 } else if (pIterator->GetLine(line)) {
1177 ptHead.x = line.ptLine.x;
1178 ptHead.y = line.ptLine.y + line.fLineAscent;
1179 ptFoot.x = line.ptLine.x;
1180 ptFoot.y = line.ptLine.y + line.fLineDescent;
1181 }
1182
Dan Sinclairf528eee2017-02-14 11:52:07 -05001183 CFX_PointF ptHeadEdit = VTToEdit(ptHead);
1184 CFX_PointF ptFootEdit = VTToEdit(ptFoot);
thestig821d59e2016-05-11 12:59:22 -07001185 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
dsinclair448c4332016-08-02 12:07:35 -07001186 if (!IsFloatEqual(rcPlate.left, rcPlate.right)) {
1187 if (IsFloatSmaller(ptHeadEdit.x, rcPlate.left) ||
1188 IsFloatEqual(ptHeadEdit.x, rcPlate.left)) {
thestig821d59e2016-05-11 12:59:22 -07001189 SetScrollPosX(ptHead.x);
dsinclair448c4332016-08-02 12:07:35 -07001190 } else if (IsFloatBigger(ptHeadEdit.x, rcPlate.right)) {
thestig821d59e2016-05-11 12:59:22 -07001191 SetScrollPosX(ptHead.x - rcPlate.Width());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001192 }
thestig821d59e2016-05-11 12:59:22 -07001193 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001194
dsinclair448c4332016-08-02 12:07:35 -07001195 if (!IsFloatEqual(rcPlate.top, rcPlate.bottom)) {
1196 if (IsFloatSmaller(ptFootEdit.y, rcPlate.bottom) ||
1197 IsFloatEqual(ptFootEdit.y, rcPlate.bottom)) {
1198 if (IsFloatSmaller(ptHeadEdit.y, rcPlate.top)) {
thestig821d59e2016-05-11 12:59:22 -07001199 SetScrollPosY(ptFoot.y + rcPlate.Height());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001200 }
dsinclair448c4332016-08-02 12:07:35 -07001201 } else if (IsFloatBigger(ptHeadEdit.y, rcPlate.top)) {
1202 if (IsFloatBigger(ptFootEdit.y, rcPlate.bottom)) {
thestig821d59e2016-05-11 12:59:22 -07001203 SetScrollPosY(ptHead.y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001204 }
1205 }
1206 }
1207}
1208
dsinclairefd5a992016-07-18 10:04:07 -07001209void CFX_Edit::Refresh() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001210 if (m_bEnableRefresh && m_pVT->IsValid()) {
1211 m_Refresh.BeginRefresh();
1212 RefreshPushLineRects(GetVisibleWordRange());
1213
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001214 m_Refresh.NoAnalyse();
1215 m_ptRefreshScrollPos = m_ptScrollPos;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001216
dsinclaira2919b32016-07-13 10:55:48 -07001217 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001218 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001219 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001220 m_bNotifyFlag = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001221 if (const CFX_Edit_RectArray* pRects = m_Refresh.GetRefreshRects()) {
1222 for (int32_t i = 0, sz = pRects->GetSize(); i < sz; i++)
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001223 m_pNotify->InvalidateRect(pRects->GetAt(i));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001224 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001225 }
1226 }
1227
1228 m_Refresh.EndRefresh();
1229 }
1230}
1231
1232void CFX_Edit::RefreshPushLineRects(const CPVT_WordRange& wr) {
thestig821d59e2016-05-11 12:59:22 -07001233 if (!m_pVT->IsValid())
1234 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001235
thestig821d59e2016-05-11 12:59:22 -07001236 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1237 CPVT_WordPlace wpBegin = wr.BeginPos;
1238 m_pVT->UpdateWordPlace(wpBegin);
1239 CPVT_WordPlace wpEnd = wr.EndPos;
1240 m_pVT->UpdateWordPlace(wpEnd);
1241 pIterator->SetAt(wpBegin);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001242
thestig821d59e2016-05-11 12:59:22 -07001243 CPVT_Line lineinfo;
1244 do {
1245 if (!pIterator->GetLine(lineinfo))
1246 break;
1247 if (lineinfo.lineplace.LineCmp(wpEnd) > 0)
1248 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001249
thestig821d59e2016-05-11 12:59:22 -07001250 CFX_FloatRect rcLine(lineinfo.ptLine.x,
1251 lineinfo.ptLine.y + lineinfo.fLineDescent,
1252 lineinfo.ptLine.x + lineinfo.fLineWidth,
1253 lineinfo.ptLine.y + lineinfo.fLineAscent);
1254
1255 m_Refresh.Push(CPVT_WordRange(lineinfo.lineplace, lineinfo.lineEnd),
1256 VTToEdit(rcLine));
1257 } while (pIterator->NextLine());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001258}
1259
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001260void CFX_Edit::RefreshWordRange(const CPVT_WordRange& wr) {
thestig821d59e2016-05-11 12:59:22 -07001261 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1262 CPVT_WordRange wrTemp = wr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001263
thestig821d59e2016-05-11 12:59:22 -07001264 m_pVT->UpdateWordPlace(wrTemp.BeginPos);
1265 m_pVT->UpdateWordPlace(wrTemp.EndPos);
1266 pIterator->SetAt(wrTemp.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001267
thestig821d59e2016-05-11 12:59:22 -07001268 CPVT_Word wordinfo;
1269 CPVT_Line lineinfo;
1270 CPVT_WordPlace place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001271
thestig821d59e2016-05-11 12:59:22 -07001272 while (pIterator->NextWord()) {
1273 place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -07001274 if (place > wrTemp.EndPos)
thestig821d59e2016-05-11 12:59:22 -07001275 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001276
thestig821d59e2016-05-11 12:59:22 -07001277 pIterator->GetWord(wordinfo);
1278 pIterator->GetLine(lineinfo);
thestig821d59e2016-05-11 12:59:22 -07001279 if (place.LineCmp(wrTemp.BeginPos) == 0 ||
1280 place.LineCmp(wrTemp.EndPos) == 0) {
1281 CFX_FloatRect rcWord(wordinfo.ptWord.x,
1282 lineinfo.ptLine.y + lineinfo.fLineDescent,
1283 wordinfo.ptWord.x + wordinfo.fWidth,
1284 lineinfo.ptLine.y + lineinfo.fLineAscent);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001285
dsinclaira2919b32016-07-13 10:55:48 -07001286 if (m_pNotify) {
thestig821d59e2016-05-11 12:59:22 -07001287 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001288 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001289 m_bNotifyFlag = true;
thestig821d59e2016-05-11 12:59:22 -07001290 CFX_FloatRect rcRefresh = VTToEdit(rcWord);
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001291 m_pNotify->InvalidateRect(&rcRefresh);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001292 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001293 }
thestig821d59e2016-05-11 12:59:22 -07001294 } else {
1295 CFX_FloatRect rcLine(lineinfo.ptLine.x,
1296 lineinfo.ptLine.y + lineinfo.fLineDescent,
1297 lineinfo.ptLine.x + lineinfo.fLineWidth,
1298 lineinfo.ptLine.y + lineinfo.fLineAscent);
1299
dsinclaira2919b32016-07-13 10:55:48 -07001300 if (m_pNotify) {
thestig821d59e2016-05-11 12:59:22 -07001301 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001302 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001303 m_bNotifyFlag = true;
thestig821d59e2016-05-11 12:59:22 -07001304 CFX_FloatRect rcRefresh = VTToEdit(rcLine);
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001305 m_pNotify->InvalidateRect(&rcRefresh);
thestig821d59e2016-05-11 12:59:22 -07001306 }
1307 }
1308
1309 pIterator->NextLine();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001310 }
1311 }
1312}
1313
1314void CFX_Edit::SetCaret(const CPVT_WordPlace& place) {
1315 m_wpOldCaret = m_wpCaret;
1316 m_wpCaret = place;
1317}
1318
1319void CFX_Edit::SetCaretInfo() {
dsinclaira2919b32016-07-13 10:55:48 -07001320 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001321 if (!m_bNotifyFlag) {
thestig821d59e2016-05-11 12:59:22 -07001322 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1323 pIterator->SetAt(m_wpCaret);
tsepez63f545c2016-09-13 16:08:49 -07001324
Dan Sinclairf528eee2017-02-14 11:52:07 -05001325 CFX_PointF ptHead;
1326 CFX_PointF ptFoot;
thestig821d59e2016-05-11 12:59:22 -07001327 CPVT_Word word;
1328 CPVT_Line line;
1329 if (pIterator->GetWord(word)) {
1330 ptHead.x = word.ptWord.x + word.fWidth;
1331 ptHead.y = word.ptWord.y + word.fAscent;
1332 ptFoot.x = word.ptWord.x + word.fWidth;
1333 ptFoot.y = word.ptWord.y + word.fDescent;
1334 } else if (pIterator->GetLine(line)) {
1335 ptHead.x = line.ptLine.x;
1336 ptHead.y = line.ptLine.y + line.fLineAscent;
1337 ptFoot.x = line.ptLine.x;
1338 ptFoot.y = line.ptLine.y + line.fLineDescent;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001339 }
1340
Lei Zhanga8c2b912017-03-22 17:41:02 -07001341 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001342 m_bNotifyFlag = true;
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001343 m_pNotify->SetCaret(m_SelState.IsEmpty(), VTToEdit(ptHead),
1344 VTToEdit(ptFoot));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001345 }
1346 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001347}
1348
Dan Sinclairf528eee2017-02-14 11:52:07 -05001349void CFX_Edit::OnMouseDown(const CFX_PointF& point, bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001350 if (!m_pVT->IsValid())
1351 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001352
Tom Sepez52f69b32017-03-21 13:42:38 -07001353 SelectNone();
1354 SetCaret(m_pVT->SearchWordPlace(EditToVT(point)));
1355 m_SelState.Set(m_wpCaret, m_wpCaret);
1356 ScrollToCaret();
1357 SetCaretOrigin();
1358 SetCaretInfo();
1359}
1360
1361void CFX_Edit::OnMouseMove(const CFX_PointF& point, bool bShift, bool bCtrl) {
1362 if (!m_pVT->IsValid())
1363 return;
1364
1365 SetCaret(m_pVT->SearchWordPlace(EditToVT(point)));
1366 if (m_wpCaret == m_wpOldCaret)
1367 return;
1368
1369 m_SelState.SetEndPos(m_wpCaret);
1370 ScrollToCaret();
1371 Refresh();
1372 SetCaretOrigin();
1373 SetCaretInfo();
1374}
1375
1376void CFX_Edit::OnVK_UP(bool bShift, bool bCtrl) {
1377 if (!m_pVT->IsValid())
1378 return;
1379
1380 SetCaret(m_pVT->GetUpWordPlace(m_wpCaret, m_ptCaret));
1381 if (bShift) {
1382 if (m_SelState.IsEmpty())
1383 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1384 else
1385 m_SelState.SetEndPos(m_wpCaret);
1386
1387 if (m_wpOldCaret != m_wpCaret) {
1388 ScrollToCaret();
1389 Refresh();
1390 SetCaretInfo();
1391 }
1392 } else {
1393 SelectNone();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001394 ScrollToCaret();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001395 SetCaretInfo();
1396 }
1397}
1398
Tom Sepez52f69b32017-03-21 13:42:38 -07001399void CFX_Edit::OnVK_DOWN(bool bShift, bool bCtrl) {
1400 if (!m_pVT->IsValid())
1401 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001402
Tom Sepez52f69b32017-03-21 13:42:38 -07001403 SetCaret(m_pVT->GetDownWordPlace(m_wpCaret, m_ptCaret));
1404 if (bShift) {
1405 if (m_SelState.IsEmpty())
1406 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1407 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001408 m_SelState.SetEndPos(m_wpCaret);
1409
Tom Sepez52f69b32017-03-21 13:42:38 -07001410 if (m_wpOldCaret != m_wpCaret) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001411 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001412 Refresh();
Tom Sepez52f69b32017-03-21 13:42:38 -07001413 SetCaretInfo();
1414 }
1415 } else {
1416 SelectNone();
1417 ScrollToCaret();
1418 SetCaretInfo();
1419 }
1420}
1421
1422void CFX_Edit::OnVK_LEFT(bool bShift, bool bCtrl) {
1423 if (!m_pVT->IsValid())
1424 return;
1425
1426 if (bShift) {
1427 if (m_wpCaret == m_pVT->GetLineBeginPlace(m_wpCaret) &&
1428 m_wpCaret != m_pVT->GetSectionBeginPlace(m_wpCaret)) {
1429 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1430 }
1431 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1432 if (m_SelState.IsEmpty())
1433 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1434 else
1435 m_SelState.SetEndPos(m_wpCaret);
1436
1437 if (m_wpOldCaret != m_wpCaret) {
1438 ScrollToCaret();
1439 Refresh();
1440 SetCaretInfo();
1441 }
1442 } else {
1443 if (!m_SelState.IsEmpty()) {
1444 if (m_SelState.BeginPos < m_SelState.EndPos)
1445 SetCaret(m_SelState.BeginPos);
1446 else
1447 SetCaret(m_SelState.EndPos);
1448
1449 SelectNone();
1450 ScrollToCaret();
1451 SetCaretInfo();
1452 } else {
1453 if (m_wpCaret == m_pVT->GetLineBeginPlace(m_wpCaret) &&
1454 m_wpCaret != m_pVT->GetSectionBeginPlace(m_wpCaret)) {
1455 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1456 }
1457 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1458 ScrollToCaret();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001459 SetCaretOrigin();
1460 SetCaretInfo();
1461 }
1462 }
1463}
1464
tsepez4cf55152016-11-02 14:37:54 -07001465void CFX_Edit::OnVK_RIGHT(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001466 if (!m_pVT->IsValid())
1467 return;
1468
1469 if (bShift) {
1470 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1471 if (m_wpCaret == m_pVT->GetLineEndPlace(m_wpCaret) &&
1472 m_wpCaret != m_pVT->GetSectionEndPlace(m_wpCaret))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001473 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1474
Tom Sepez52f69b32017-03-21 13:42:38 -07001475 if (m_SelState.IsEmpty())
1476 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1477 else
1478 m_SelState.SetEndPos(m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001479
Tom Sepez52f69b32017-03-21 13:42:38 -07001480 if (m_wpOldCaret != m_wpCaret) {
1481 ScrollToCaret();
1482 Refresh();
1483 SetCaretInfo();
1484 }
1485 } else {
1486 if (!m_SelState.IsEmpty()) {
1487 if (m_SelState.BeginPos > m_SelState.EndPos)
1488 SetCaret(m_SelState.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001489 else
Tom Sepez52f69b32017-03-21 13:42:38 -07001490 SetCaret(m_SelState.EndPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001491
Tom Sepez52f69b32017-03-21 13:42:38 -07001492 SelectNone();
1493 ScrollToCaret();
1494 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001495 } else {
Tom Sepez52f69b32017-03-21 13:42:38 -07001496 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1497 if (m_wpCaret == m_pVT->GetLineEndPlace(m_wpCaret) &&
1498 m_wpCaret != m_pVT->GetSectionEndPlace(m_wpCaret)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001499 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001500 }
Tom Sepez52f69b32017-03-21 13:42:38 -07001501 ScrollToCaret();
1502 SetCaretOrigin();
1503 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001504 }
1505 }
1506}
1507
tsepez4cf55152016-11-02 14:37:54 -07001508void CFX_Edit::OnVK_HOME(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001509 if (!m_pVT->IsValid())
1510 return;
1511
1512 if (bShift) {
1513 if (bCtrl)
1514 SetCaret(m_pVT->GetBeginWordPlace());
1515 else
1516 SetCaret(m_pVT->GetLineBeginPlace(m_wpCaret));
1517
1518 if (m_SelState.IsEmpty())
1519 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1520 else
1521 m_SelState.SetEndPos(m_wpCaret);
1522
1523 ScrollToCaret();
1524 Refresh();
1525 SetCaretInfo();
1526 } else {
1527 if (!m_SelState.IsEmpty()) {
1528 SetCaret(std::min(m_SelState.BeginPos, m_SelState.EndPos));
1529 SelectNone();
1530 ScrollToCaret();
1531 SetCaretInfo();
1532 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001533 if (bCtrl)
1534 SetCaret(m_pVT->GetBeginWordPlace());
1535 else
1536 SetCaret(m_pVT->GetLineBeginPlace(m_wpCaret));
1537
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001538 ScrollToCaret();
Tom Sepez52f69b32017-03-21 13:42:38 -07001539 SetCaretOrigin();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001540 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001541 }
1542 }
1543}
1544
tsepez4cf55152016-11-02 14:37:54 -07001545void CFX_Edit::OnVK_END(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001546 if (!m_pVT->IsValid())
1547 return;
1548
1549 if (bShift) {
1550 if (bCtrl)
1551 SetCaret(m_pVT->GetEndWordPlace());
1552 else
1553 SetCaret(m_pVT->GetLineEndPlace(m_wpCaret));
1554
1555 if (m_SelState.IsEmpty())
1556 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1557 else
1558 m_SelState.SetEndPos(m_wpCaret);
1559
1560 ScrollToCaret();
1561 Refresh();
1562 SetCaretInfo();
1563 } else {
1564 if (!m_SelState.IsEmpty()) {
1565 SetCaret(std::max(m_SelState.BeginPos, m_SelState.EndPos));
1566 SelectNone();
1567 ScrollToCaret();
1568 SetCaretInfo();
1569 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001570 if (bCtrl)
1571 SetCaret(m_pVT->GetEndWordPlace());
1572 else
1573 SetCaret(m_pVT->GetLineEndPlace(m_wpCaret));
1574
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001575 ScrollToCaret();
Tom Sepez52f69b32017-03-21 13:42:38 -07001576 SetCaretOrigin();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001577 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001578 }
1579 }
1580}
1581
tsepez4cf55152016-11-02 14:37:54 -07001582bool CFX_Edit::InsertWord(uint16_t word,
1583 int32_t charset,
1584 const CPVT_WordProps* pWordProps,
1585 bool bAddUndo,
1586 bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001587 if (IsTextOverflow() || !m_pVT->IsValid())
tsepez4cf55152016-11-02 14:37:54 -07001588 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001589
Tom Sepez3509d162017-01-30 13:22:02 -08001590 m_pVT->UpdateWordPlace(m_wpCaret);
1591 SetCaret(m_pVT->InsertWord(m_wpCaret, word,
1592 GetCharSetFromUnicode(word, charset), pWordProps));
1593 m_SelState.Set(m_wpCaret, m_wpCaret);
1594 if (m_wpCaret == m_wpOldCaret)
1595 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001596
Tom Sepez3509d162017-01-30 13:22:02 -08001597 if (bAddUndo && m_bEnableUndo) {
1598 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertWord>(
1599 this, m_wpOldCaret, m_wpCaret, word, charset, pWordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001600 }
Tom Sepez3509d162017-01-30 13:22:02 -08001601 if (bPaint)
1602 PaintInsertText(m_wpOldCaret, m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001603
Tom Sepez3509d162017-01-30 13:22:02 -08001604 if (m_bOprNotify && m_pOprNotify)
1605 m_pOprNotify->OnInsertWord(m_wpCaret, m_wpOldCaret);
1606
1607 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001608}
1609
tsepez4cf55152016-11-02 14:37:54 -07001610bool CFX_Edit::InsertReturn(const CPVT_SecProps* pSecProps,
1611 const CPVT_WordProps* pWordProps,
1612 bool bAddUndo,
1613 bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001614 if (IsTextOverflow() || !m_pVT->IsValid())
tsepez4cf55152016-11-02 14:37:54 -07001615 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001616
Tom Sepez3509d162017-01-30 13:22:02 -08001617 m_pVT->UpdateWordPlace(m_wpCaret);
1618 SetCaret(m_pVT->InsertSection(m_wpCaret, pSecProps, pWordProps));
1619 m_SelState.Set(m_wpCaret, m_wpCaret);
1620 if (m_wpCaret == m_wpOldCaret)
1621 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001622
Tom Sepez3509d162017-01-30 13:22:02 -08001623 if (bAddUndo && m_bEnableUndo) {
1624 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertReturn>(
1625 this, m_wpOldCaret, m_wpCaret, pSecProps, pWordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001626 }
Tom Sepez3509d162017-01-30 13:22:02 -08001627 if (bPaint) {
1628 RearrangePart(CPVT_WordRange(m_wpOldCaret, m_wpCaret));
1629 ScrollToCaret();
1630 Refresh();
1631 SetCaretOrigin();
1632 SetCaretInfo();
1633 }
1634 if (m_bOprNotify && m_pOprNotify)
1635 m_pOprNotify->OnInsertReturn(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001636
Tom Sepez3509d162017-01-30 13:22:02 -08001637 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001638}
1639
tsepez4cf55152016-11-02 14:37:54 -07001640bool CFX_Edit::Backspace(bool bAddUndo, bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001641 if (!m_pVT->IsValid() || m_wpCaret == m_pVT->GetBeginWordPlace())
1642 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001643
Tom Sepez3509d162017-01-30 13:22:02 -08001644 CPVT_Section section;
1645 CPVT_Word word;
1646 if (bAddUndo) {
1647 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1648 pIterator->SetAt(m_wpCaret);
1649 pIterator->GetSection(section);
1650 pIterator->GetWord(word);
1651 }
1652 m_pVT->UpdateWordPlace(m_wpCaret);
1653 SetCaret(m_pVT->BackSpaceWord(m_wpCaret));
1654 m_SelState.Set(m_wpCaret, m_wpCaret);
1655 if (m_wpCaret == m_wpOldCaret)
1656 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001657
Tom Sepez3509d162017-01-30 13:22:02 -08001658 if (bAddUndo && m_bEnableUndo) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001659 if (m_wpCaret.nSecIndex != m_wpOldCaret.nSecIndex) {
Tom Sepez3509d162017-01-30 13:22:02 -08001660 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Backspace>(
1661 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1662 section.SecProps, section.WordProps));
1663 } else {
1664 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Backspace>(
1665 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1666 section.SecProps, word.WordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001667 }
1668 }
Tom Sepez3509d162017-01-30 13:22:02 -08001669 if (bPaint) {
1670 RearrangePart(CPVT_WordRange(m_wpCaret, m_wpOldCaret));
1671 ScrollToCaret();
1672 Refresh();
1673 SetCaretOrigin();
1674 SetCaretInfo();
1675 }
1676 if (m_bOprNotify && m_pOprNotify)
1677 m_pOprNotify->OnBackSpace(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001678
Tom Sepez3509d162017-01-30 13:22:02 -08001679 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001680}
1681
tsepez4cf55152016-11-02 14:37:54 -07001682bool CFX_Edit::Delete(bool bAddUndo, bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001683 if (!m_pVT->IsValid() || m_wpCaret == m_pVT->GetEndWordPlace())
1684 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001685
Tom Sepez3509d162017-01-30 13:22:02 -08001686 CPVT_Section section;
1687 CPVT_Word word;
1688 if (bAddUndo) {
1689 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1690 pIterator->SetAt(m_pVT->GetNextWordPlace(m_wpCaret));
1691 pIterator->GetSection(section);
1692 pIterator->GetWord(word);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001693 }
Tom Sepez3509d162017-01-30 13:22:02 -08001694 m_pVT->UpdateWordPlace(m_wpCaret);
1695 bool bSecEnd = (m_wpCaret == m_pVT->GetSectionEndPlace(m_wpCaret));
1696 SetCaret(m_pVT->DeleteWord(m_wpCaret));
1697 m_SelState.Set(m_wpCaret, m_wpCaret);
1698 if (bAddUndo && m_bEnableUndo) {
1699 if (bSecEnd) {
1700 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Delete>(
1701 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1702 section.SecProps, section.WordProps, bSecEnd));
1703 } else {
1704 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Delete>(
1705 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1706 section.SecProps, word.WordProps, bSecEnd));
1707 }
1708 }
1709 if (bPaint) {
1710 RearrangePart(CPVT_WordRange(m_wpOldCaret, m_wpCaret));
1711 ScrollToCaret();
1712 Refresh();
1713 SetCaretOrigin();
1714 SetCaretInfo();
1715 }
1716 if (m_bOprNotify && m_pOprNotify)
1717 m_pOprNotify->OnDelete(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001718
Tom Sepez3509d162017-01-30 13:22:02 -08001719 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001720}
1721
tsepez4cf55152016-11-02 14:37:54 -07001722bool CFX_Edit::Empty() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001723 if (m_pVT->IsValid()) {
1724 m_pVT->DeleteWords(GetWholeWordRange());
1725 SetCaret(m_pVT->GetBeginWordPlace());
1726
tsepez4cf55152016-11-02 14:37:54 -07001727 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001728 }
1729
tsepez4cf55152016-11-02 14:37:54 -07001730 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001731}
1732
tsepez4cf55152016-11-02 14:37:54 -07001733bool CFX_Edit::Clear(bool bAddUndo, bool bPaint) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001734 if (!m_pVT->IsValid() || m_SelState.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001735 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001736
thestig821d59e2016-05-11 12:59:22 -07001737 CPVT_WordRange range = m_SelState.ConvertToWordRange();
dsinclaira2919b32016-07-13 10:55:48 -07001738 if (bAddUndo && m_bEnableUndo)
Tom Sepez3509d162017-01-30 13:22:02 -08001739 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Clear>(this, range, GetSelText()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001740
thestig821d59e2016-05-11 12:59:22 -07001741 SelectNone();
1742 SetCaret(m_pVT->DeleteWords(range));
1743 m_SelState.Set(m_wpCaret, m_wpCaret);
thestig821d59e2016-05-11 12:59:22 -07001744 if (bPaint) {
1745 RearrangePart(range);
1746 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001747 Refresh();
thestig821d59e2016-05-11 12:59:22 -07001748 SetCaretOrigin();
1749 SetCaretInfo();
1750 }
thestig821d59e2016-05-11 12:59:22 -07001751 if (m_bOprNotify && m_pOprNotify)
1752 m_pOprNotify->OnClear(m_wpCaret, m_wpOldCaret);
1753
tsepez4cf55152016-11-02 14:37:54 -07001754 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001755}
1756
tsepez4cf55152016-11-02 14:37:54 -07001757bool CFX_Edit::InsertText(const CFX_WideString& sText,
1758 int32_t charset,
1759 bool bAddUndo,
1760 bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001761 if (IsTextOverflow())
tsepez4cf55152016-11-02 14:37:54 -07001762 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001763
1764 m_pVT->UpdateWordPlace(m_wpCaret);
tsepeza31da742016-09-08 11:28:14 -07001765 SetCaret(DoInsertText(m_wpCaret, sText, charset));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001766 m_SelState.Set(m_wpCaret, m_wpCaret);
tsepeza31da742016-09-08 11:28:14 -07001767 if (m_wpCaret == m_wpOldCaret)
tsepez4cf55152016-11-02 14:37:54 -07001768 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001769
tsepeza31da742016-09-08 11:28:14 -07001770 if (bAddUndo && m_bEnableUndo) {
Tom Sepez3509d162017-01-30 13:22:02 -08001771 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertText>(
1772 this, m_wpOldCaret, m_wpCaret, sText, charset));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001773 }
tsepeza31da742016-09-08 11:28:14 -07001774 if (bPaint)
1775 PaintInsertText(m_wpOldCaret, m_wpCaret);
1776
1777 if (m_bOprNotify && m_pOprNotify)
1778 m_pOprNotify->OnInsertText(m_wpCaret, m_wpOldCaret);
1779
tsepez4cf55152016-11-02 14:37:54 -07001780 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001781}
1782
1783void CFX_Edit::PaintInsertText(const CPVT_WordPlace& wpOld,
1784 const CPVT_WordPlace& wpNew) {
1785 if (m_pVT->IsValid()) {
1786 RearrangePart(CPVT_WordRange(wpOld, wpNew));
1787 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001788 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001789 SetCaretOrigin();
1790 SetCaretInfo();
1791 }
1792}
1793
tsepez4cf55152016-11-02 14:37:54 -07001794bool CFX_Edit::Redo() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001795 if (m_bEnableUndo) {
1796 if (m_Undo.CanRedo()) {
1797 m_Undo.Redo();
tsepez4cf55152016-11-02 14:37:54 -07001798 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001799 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001800 }
1801
tsepez4cf55152016-11-02 14:37:54 -07001802 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001803}
1804
tsepez4cf55152016-11-02 14:37:54 -07001805bool CFX_Edit::Undo() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001806 if (m_bEnableUndo) {
1807 if (m_Undo.CanUndo()) {
1808 m_Undo.Undo();
tsepez4cf55152016-11-02 14:37:54 -07001809 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001810 }
1811 }
1812
tsepez4cf55152016-11-02 14:37:54 -07001813 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001814}
1815
1816void CFX_Edit::SetCaretOrigin() {
thestig821d59e2016-05-11 12:59:22 -07001817 if (!m_pVT->IsValid())
1818 return;
1819
1820 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1821 pIterator->SetAt(m_wpCaret);
1822 CPVT_Word word;
1823 CPVT_Line line;
1824 if (pIterator->GetWord(word)) {
1825 m_ptCaret.x = word.ptWord.x + word.fWidth;
1826 m_ptCaret.y = word.ptWord.y;
1827 } else if (pIterator->GetLine(line)) {
1828 m_ptCaret.x = line.ptLine.x;
1829 m_ptCaret.y = line.ptLine.y;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001830 }
1831}
1832
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001833CPVT_WordPlace CFX_Edit::WordIndexToWordPlace(int32_t index) const {
1834 if (m_pVT->IsValid())
1835 return m_pVT->WordIndexToWordPlace(index);
1836
1837 return CPVT_WordPlace();
1838}
1839
tsepez4cf55152016-11-02 14:37:54 -07001840bool CFX_Edit::IsTextFull() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001841 int32_t nTotalWords = m_pVT->GetTotalWords();
1842 int32_t nLimitChar = m_pVT->GetLimitChar();
1843 int32_t nCharArray = m_pVT->GetCharArray();
1844
1845 return IsTextOverflow() || (nLimitChar > 0 && nTotalWords >= nLimitChar) ||
1846 (nCharArray > 0 && nTotalWords >= nCharArray);
1847}
1848
tsepez4cf55152016-11-02 14:37:54 -07001849bool CFX_Edit::IsTextOverflow() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001850 if (!m_bEnableScroll && !m_bEnableOverflow) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001851 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
1852 CFX_FloatRect rcContent = m_pVT->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001853
dsinclair448c4332016-08-02 12:07:35 -07001854 if (m_pVT->IsMultiLine() && GetTotalLines() > 1 &&
1855 IsFloatBigger(rcContent.Height(), rcPlate.Height())) {
tsepez4cf55152016-11-02 14:37:54 -07001856 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001857 }
1858
dsinclair448c4332016-08-02 12:07:35 -07001859 if (IsFloatBigger(rcContent.Width(), rcPlate.Width()))
tsepez4cf55152016-11-02 14:37:54 -07001860 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001861 }
1862
tsepez4cf55152016-11-02 14:37:54 -07001863 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001864}
1865
tsepez4cf55152016-11-02 14:37:54 -07001866bool CFX_Edit::CanUndo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001867 if (m_bEnableUndo) {
1868 return m_Undo.CanUndo();
1869 }
1870
tsepez4cf55152016-11-02 14:37:54 -07001871 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001872}
1873
tsepez4cf55152016-11-02 14:37:54 -07001874bool CFX_Edit::CanRedo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001875 if (m_bEnableUndo) {
1876 return m_Undo.CanRedo();
1877 }
1878
tsepez4cf55152016-11-02 14:37:54 -07001879 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001880}
1881
tsepez4cf55152016-11-02 14:37:54 -07001882void CFX_Edit::EnableRefresh(bool bRefresh) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001883 m_bEnableRefresh = bRefresh;
1884}
1885
tsepez4cf55152016-11-02 14:37:54 -07001886void CFX_Edit::EnableUndo(bool bUndo) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001887 m_bEnableUndo = bUndo;
1888}
1889
tsepez4cf55152016-11-02 14:37:54 -07001890void CFX_Edit::EnableOprNotify(bool bNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001891 m_bOprNotify = bNotify;
1892}
1893
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001894CPVT_WordPlace CFX_Edit::DoInsertText(const CPVT_WordPlace& place,
tsepeza31da742016-09-08 11:28:14 -07001895 const CFX_WideString& sText,
dsinclairefd5a992016-07-18 10:04:07 -07001896 int32_t charset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001897 CPVT_WordPlace wp = place;
1898
1899 if (m_pVT->IsValid()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001900 for (int32_t i = 0, sz = sText.GetLength(); i < sz; i++) {
Tom Sepez62a70f92016-03-21 15:00:20 -07001901 uint16_t word = sText[i];
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001902 switch (word) {
1903 case 0x0D:
dsinclairefd5a992016-07-18 10:04:07 -07001904 wp = m_pVT->InsertSection(wp, nullptr, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001905 if (sText[i + 1] == 0x0A)
1906 i++;
1907 break;
1908 case 0x0A:
dsinclairefd5a992016-07-18 10:04:07 -07001909 wp = m_pVT->InsertSection(wp, nullptr, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001910 if (sText[i + 1] == 0x0D)
1911 i++;
1912 break;
1913 case 0x09:
1914 word = 0x20;
1915 default:
1916 wp = m_pVT->InsertWord(wp, word, GetCharSetFromUnicode(word, charset),
dsinclairefd5a992016-07-18 10:04:07 -07001917 nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001918 break;
1919 }
1920 }
1921 }
1922
1923 return wp;
1924}
1925
Tom Sepez62a70f92016-03-21 15:00:20 -07001926int32_t CFX_Edit::GetCharSetFromUnicode(uint16_t word, int32_t nOldCharset) {
dsinclairc7a73492016-04-05 12:01:42 -07001927 if (IPVT_FontMap* pFontMap = GetFontMap())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001928 return pFontMap->CharSetFromUnicode(word, nOldCharset);
1929 return nOldCharset;
1930}
1931
Tom Sepez3509d162017-01-30 13:22:02 -08001932void CFX_Edit::AddEditUndoItem(
1933 std::unique_ptr<CFX_Edit_UndoItem> pEditUndoItem) {
Lei Zhang1a89e362017-03-23 15:27:25 -07001934 m_Undo.AddItem(std::move(pEditUndoItem));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001935}
1936
weili625ad662016-06-15 11:21:33 -07001937CFX_Edit_LineRectArray::CFX_Edit_LineRectArray() {}
1938
Tom Sepez3509d162017-01-30 13:22:02 -08001939CFX_Edit_LineRectArray::~CFX_Edit_LineRectArray() {}
weili625ad662016-06-15 11:21:33 -07001940
Tom Sepez3509d162017-01-30 13:22:02 -08001941void CFX_Edit_LineRectArray::operator=(CFX_Edit_LineRectArray&& that) {
1942 m_LineRects = std::move(that.m_LineRects);
weili625ad662016-06-15 11:21:33 -07001943}
1944
1945void CFX_Edit_LineRectArray::Add(const CPVT_WordRange& wrLine,
1946 const CFX_FloatRect& rcLine) {
Tom Sepez3509d162017-01-30 13:22:02 -08001947 m_LineRects.push_back(pdfium::MakeUnique<CFX_Edit_LineRect>(wrLine, rcLine));
weili625ad662016-06-15 11:21:33 -07001948}
1949
1950int32_t CFX_Edit_LineRectArray::GetSize() const {
Tom Sepez3509d162017-01-30 13:22:02 -08001951 return pdfium::CollectionSize<int32_t>(m_LineRects);
weili625ad662016-06-15 11:21:33 -07001952}
1953
1954CFX_Edit_LineRect* CFX_Edit_LineRectArray::GetAt(int32_t nIndex) const {
Tom Sepez3509d162017-01-30 13:22:02 -08001955 if (nIndex < 0 || nIndex >= GetSize())
weili625ad662016-06-15 11:21:33 -07001956 return nullptr;
1957
Tom Sepez3509d162017-01-30 13:22:02 -08001958 return m_LineRects[nIndex].get();
weili625ad662016-06-15 11:21:33 -07001959}
1960
1961CFX_Edit_Select::CFX_Edit_Select() {}
1962
weili625ad662016-06-15 11:21:33 -07001963CFX_Edit_Select::CFX_Edit_Select(const CPVT_WordRange& range) {
1964 Set(range.BeginPos, range.EndPos);
1965}
1966
1967CPVT_WordRange CFX_Edit_Select::ConvertToWordRange() const {
1968 return CPVT_WordRange(BeginPos, EndPos);
1969}
1970
Tom Sepez52f69b32017-03-21 13:42:38 -07001971void CFX_Edit_Select::Reset() {
1972 BeginPos.Reset();
1973 EndPos.Reset();
weili625ad662016-06-15 11:21:33 -07001974}
1975
1976void CFX_Edit_Select::Set(const CPVT_WordPlace& begin,
1977 const CPVT_WordPlace& end) {
1978 BeginPos = begin;
1979 EndPos = end;
1980}
1981
weili625ad662016-06-15 11:21:33 -07001982void CFX_Edit_Select::SetEndPos(const CPVT_WordPlace& end) {
1983 EndPos = end;
1984}
1985
Tom Sepez52f69b32017-03-21 13:42:38 -07001986bool CFX_Edit_Select::IsEmpty() const {
1987 return BeginPos == EndPos;
weili625ad662016-06-15 11:21:33 -07001988}
1989
weili625ad662016-06-15 11:21:33 -07001990CFX_Edit_RectArray::CFX_Edit_RectArray() {}
1991
Tom Sepez3509d162017-01-30 13:22:02 -08001992CFX_Edit_RectArray::~CFX_Edit_RectArray() {}
weili625ad662016-06-15 11:21:33 -07001993
Tom Sepez3509d162017-01-30 13:22:02 -08001994void CFX_Edit_RectArray::Clear() {
1995 m_Rects.clear();
weili625ad662016-06-15 11:21:33 -07001996}
1997
1998void CFX_Edit_RectArray::Add(const CFX_FloatRect& rect) {
1999 // check for overlapped area
Tom Sepez3509d162017-01-30 13:22:02 -08002000 for (const auto& pRect : m_Rects) {
weili625ad662016-06-15 11:21:33 -07002001 if (pRect && pRect->Contains(rect))
2002 return;
2003 }
Tom Sepez3509d162017-01-30 13:22:02 -08002004 m_Rects.push_back(pdfium::MakeUnique<CFX_FloatRect>(rect));
weili625ad662016-06-15 11:21:33 -07002005}
2006
2007int32_t CFX_Edit_RectArray::GetSize() const {
Tom Sepez3509d162017-01-30 13:22:02 -08002008 return pdfium::CollectionSize<int32_t>(m_Rects);
weili625ad662016-06-15 11:21:33 -07002009}
2010
2011CFX_FloatRect* CFX_Edit_RectArray::GetAt(int32_t nIndex) const {
Tom Sepez3509d162017-01-30 13:22:02 -08002012 if (nIndex < 0 || nIndex >= GetSize())
weili625ad662016-06-15 11:21:33 -07002013 return nullptr;
2014
Tom Sepez3509d162017-01-30 13:22:02 -08002015 return m_Rects[nIndex].get();
weili625ad662016-06-15 11:21:33 -07002016}