blob: d00b4d6a61bbdafd165c789f52dbcbfca56cdb40 [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();
Diana Gage4d02e902017-07-20 17:20:31 -0700452 m_pEdit->SetSelection(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);
Diana Gage4d02e902017-07-20 17:20:31 -0700462 m_pEdit->SetSelection(m_wrSel.BeginPos, m_wrSel.EndPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700463 }
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();
Diana Gage4d02e902017-07-20 17:20:31 -0700490 m_pEdit->SetSelection(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
Diana Gage4d02e902017-07-20 17:20:31 -0700736void CFX_Edit::SetSelection(int32_t nStartChar, int32_t nEndChar) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700737 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) {
Diana Gage4d02e902017-07-20 17:20:31 -0700744 SetSelection(m_pVT->WordIndexToWordPlace(nStartChar),
745 m_pVT->WordIndexToWordPlace(nEndChar));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700746 } else {
Diana Gage4d02e902017-07-20 17:20:31 -0700747 SetSelection(m_pVT->WordIndexToWordPlace(nEndChar),
748 m_pVT->WordIndexToWordPlace(nStartChar));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700749 }
750 }
751 }
752}
753
Diana Gage4d02e902017-07-20 17:20:31 -0700754void CFX_Edit::SetSelection(const CPVT_WordPlace& begin,
755 const CPVT_WordPlace& end) {
Tom Sepez52f69b32017-03-21 13:42:38 -0700756 if (!m_pVT->IsValid())
757 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700758
Tom Sepez52f69b32017-03-21 13:42:38 -0700759 SelectNone();
760 m_SelState.Set(begin, end);
761 SetCaret(m_SelState.EndPos);
762 ScrollToCaret();
763 if (!m_SelState.IsEmpty())
764 Refresh();
765 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700766}
767
Diana Gage4d02e902017-07-20 17:20:31 -0700768void CFX_Edit::GetSelection(int32_t& nStartChar, int32_t& nEndChar) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700769 nStartChar = -1;
770 nEndChar = -1;
Tom Sepez52f69b32017-03-21 13:42:38 -0700771 if (!m_pVT->IsValid())
772 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700773
Tom Sepez52f69b32017-03-21 13:42:38 -0700774 if (m_SelState.IsEmpty()) {
775 nStartChar = m_pVT->WordPlaceToWordIndex(m_wpCaret);
776 nEndChar = m_pVT->WordPlaceToWordIndex(m_wpCaret);
777 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700778 }
Tom Sepez52f69b32017-03-21 13:42:38 -0700779 if (m_SelState.BeginPos < m_SelState.EndPos) {
780 nStartChar = m_pVT->WordPlaceToWordIndex(m_SelState.BeginPos);
781 nEndChar = m_pVT->WordPlaceToWordIndex(m_SelState.EndPos);
782 return;
783 }
784 nStartChar = m_pVT->WordPlaceToWordIndex(m_SelState.EndPos);
785 nEndChar = m_pVT->WordPlaceToWordIndex(m_SelState.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700786}
787
788int32_t CFX_Edit::GetCaret() const {
789 if (m_pVT->IsValid())
790 return m_pVT->WordPlaceToWordIndex(m_wpCaret);
791
792 return -1;
793}
794
795CPVT_WordPlace CFX_Edit::GetCaretWordPlace() const {
796 return m_wpCaret;
797}
798
799CFX_WideString CFX_Edit::GetText() const {
800 CFX_WideString swRet;
thestig821d59e2016-05-11 12:59:22 -0700801 if (!m_pVT->IsValid())
802 return swRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700803
thestig821d59e2016-05-11 12:59:22 -0700804 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
805 pIterator->SetAt(0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700806
thestig821d59e2016-05-11 12:59:22 -0700807 CPVT_Word wordinfo;
808 CPVT_WordPlace oldplace = pIterator->GetAt();
809 while (pIterator->NextWord()) {
810 CPVT_WordPlace place = pIterator->GetAt();
thestig821d59e2016-05-11 12:59:22 -0700811 if (pIterator->GetWord(wordinfo))
812 swRet += wordinfo.Word;
Tom Sepez52f69b32017-03-21 13:42:38 -0700813 if (oldplace.nSecIndex != place.nSecIndex)
thestig821d59e2016-05-11 12:59:22 -0700814 swRet += L"\r\n";
thestig821d59e2016-05-11 12:59:22 -0700815 oldplace = place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700816 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700817 return swRet;
818}
819
820CFX_WideString CFX_Edit::GetRangeText(const CPVT_WordRange& range) const {
821 CFX_WideString swRet;
thestig821d59e2016-05-11 12:59:22 -0700822 if (!m_pVT->IsValid())
823 return swRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700824
thestig821d59e2016-05-11 12:59:22 -0700825 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
826 CPVT_WordRange wrTemp = range;
827 m_pVT->UpdateWordPlace(wrTemp.BeginPos);
828 m_pVT->UpdateWordPlace(wrTemp.EndPos);
829 pIterator->SetAt(wrTemp.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700830
thestig821d59e2016-05-11 12:59:22 -0700831 CPVT_Word wordinfo;
832 CPVT_WordPlace oldplace = wrTemp.BeginPos;
833 while (pIterator->NextWord()) {
834 CPVT_WordPlace place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -0700835 if (place > wrTemp.EndPos)
thestig821d59e2016-05-11 12:59:22 -0700836 break;
thestig821d59e2016-05-11 12:59:22 -0700837 if (pIterator->GetWord(wordinfo))
838 swRet += wordinfo.Word;
Tom Sepez52f69b32017-03-21 13:42:38 -0700839 if (oldplace.nSecIndex != place.nSecIndex)
thestig821d59e2016-05-11 12:59:22 -0700840 swRet += L"\r\n";
thestig821d59e2016-05-11 12:59:22 -0700841 oldplace = place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700842 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700843 return swRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700844}
845
Diana Gage89e65622017-07-20 18:09:31 -0700846CFX_WideString CFX_Edit::GetSelectedText() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700847 return GetRangeText(m_SelState.ConvertToWordRange());
848}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700849
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700850int32_t CFX_Edit::GetTotalLines() const {
thestig821d59e2016-05-11 12:59:22 -0700851 int32_t nLines = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700852
thestig821d59e2016-05-11 12:59:22 -0700853 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
854 pIterator->SetAt(0);
855 while (pIterator->NextLine())
856 ++nLines;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700857
thestig821d59e2016-05-11 12:59:22 -0700858 return nLines;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700859}
860
861CPVT_WordRange CFX_Edit::GetSelectWordRange() const {
862 return m_SelState.ConvertToWordRange();
863}
864
tsepeza31da742016-09-08 11:28:14 -0700865void CFX_Edit::SetText(const CFX_WideString& sText) {
dsinclairefd5a992016-07-18 10:04:07 -0700866 Empty();
Dan Sinclairf51a02a2017-04-19 12:46:53 -0400867 DoInsertText(CPVT_WordPlace(0, 0, -1), sText, FX_CHARSET_Default);
dsinclairefd5a992016-07-18 10:04:07 -0700868 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700869}
870
tsepez4cf55152016-11-02 14:37:54 -0700871bool CFX_Edit::InsertWord(uint16_t word, int32_t charset) {
872 return InsertWord(word, charset, nullptr, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700873}
874
tsepez4cf55152016-11-02 14:37:54 -0700875bool CFX_Edit::InsertReturn() {
876 return InsertReturn(nullptr, nullptr, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700877}
878
tsepez4cf55152016-11-02 14:37:54 -0700879bool CFX_Edit::Backspace() {
880 return Backspace(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700881}
882
tsepez4cf55152016-11-02 14:37:54 -0700883bool CFX_Edit::Delete() {
884 return Delete(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700885}
886
tsepez4cf55152016-11-02 14:37:54 -0700887bool CFX_Edit::Clear() {
888 return Clear(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700889}
890
tsepez4cf55152016-11-02 14:37:54 -0700891bool CFX_Edit::InsertText(const CFX_WideString& sText, int32_t charset) {
892 return InsertText(sText, charset, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700893}
894
Dan Sinclair05df0752017-03-14 14:43:42 -0400895float CFX_Edit::GetFontSize() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700896 return m_pVT->GetFontSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700897}
898
Tom Sepez62a70f92016-03-21 15:00:20 -0700899uint16_t CFX_Edit::GetPasswordChar() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700900 return m_pVT->GetPasswordChar();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700901}
902
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700903int32_t CFX_Edit::GetCharArray() const {
904 return m_pVT->GetCharArray();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700905}
906
Tom Sepez281a9ea2016-02-26 14:24:28 -0800907CFX_FloatRect CFX_Edit::GetContentRect() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700908 return VTToEdit(m_pVT->GetContentRect());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700909}
910
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700911int32_t CFX_Edit::GetHorzScale() const {
912 return m_pVT->GetHorzScale();
913}
914
Dan Sinclair05df0752017-03-14 14:43:42 -0400915float CFX_Edit::GetCharSpace() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700916 return m_pVT->GetCharSpace();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700917}
918
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700919CPVT_WordRange CFX_Edit::GetWholeWordRange() const {
920 if (m_pVT->IsValid())
921 return CPVT_WordRange(m_pVT->GetBeginWordPlace(), m_pVT->GetEndWordPlace());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700922
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700923 return CPVT_WordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700924}
925
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700926CPVT_WordRange CFX_Edit::GetVisibleWordRange() const {
927 if (m_bEnableOverflow)
928 return GetWholeWordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700929
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700930 if (m_pVT->IsValid()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800931 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700932
Dan Sinclairf528eee2017-02-14 11:52:07 -0500933 CPVT_WordPlace place1 =
934 m_pVT->SearchWordPlace(EditToVT(CFX_PointF(rcPlate.left, rcPlate.top)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700935 CPVT_WordPlace place2 = m_pVT->SearchWordPlace(
Dan Sinclairf528eee2017-02-14 11:52:07 -0500936 EditToVT(CFX_PointF(rcPlate.right, rcPlate.bottom)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700937
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700938 return CPVT_WordRange(place1, place2);
939 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700940
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700941 return CPVT_WordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700942}
943
Dan Sinclairf528eee2017-02-14 11:52:07 -0500944CPVT_WordPlace CFX_Edit::SearchWordPlace(const CFX_PointF& point) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700945 if (m_pVT->IsValid()) {
946 return m_pVT->SearchWordPlace(EditToVT(point));
947 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700948
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700949 return CPVT_WordPlace();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700950}
951
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700952void CFX_Edit::Paint() {
953 if (m_pVT->IsValid()) {
954 RearrangeAll();
955 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -0700956 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700957 SetCaretOrigin();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700958 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700959 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700960}
961
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700962void CFX_Edit::RearrangeAll() {
963 if (m_pVT->IsValid()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700964 m_pVT->UpdateWordPlace(m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700965 m_pVT->RearrangeAll();
966 m_pVT->UpdateWordPlace(m_wpCaret);
967 SetScrollInfo();
968 SetContentChanged();
969 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700970}
971
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700972void CFX_Edit::RearrangePart(const CPVT_WordRange& range) {
973 if (m_pVT->IsValid()) {
974 m_pVT->UpdateWordPlace(m_wpCaret);
975 m_pVT->RearrangePart(range);
976 m_pVT->UpdateWordPlace(m_wpCaret);
977 SetScrollInfo();
978 SetContentChanged();
979 }
980}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700981
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700982void CFX_Edit::SetContentChanged() {
dsinclaira2919b32016-07-13 10:55:48 -0700983 if (m_pNotify) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800984 CFX_FloatRect rcContent = m_pVT->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700985 if (rcContent.Width() != m_rcOldContent.Width() ||
986 rcContent.Height() != m_rcOldContent.Height()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700987 m_rcOldContent = rcContent;
988 }
989 }
990}
991
992void CFX_Edit::SelectAll() {
Tom Sepez52f69b32017-03-21 13:42:38 -0700993 if (!m_pVT->IsValid())
994 return;
995 m_SelState = CFX_Edit_Select(GetWholeWordRange());
996 SetCaret(m_SelState.EndPos);
997 ScrollToCaret();
998 Refresh();
999 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001000}
1001
1002void CFX_Edit::SelectNone() {
Tom Sepez52f69b32017-03-21 13:42:38 -07001003 if (!m_pVT->IsValid() || m_SelState.IsEmpty())
1004 return;
1005
1006 m_SelState.Reset();
1007 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001008}
1009
tsepez4cf55152016-11-02 14:37:54 -07001010bool CFX_Edit::IsSelected() const {
Tom Sepez52f69b32017-03-21 13:42:38 -07001011 return !m_SelState.IsEmpty();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001012}
1013
Dan Sinclairf528eee2017-02-14 11:52:07 -05001014CFX_PointF CFX_Edit::VTToEdit(const CFX_PointF& point) const {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001015 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1016 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001017
Dan Sinclair05df0752017-03-14 14:43:42 -04001018 float fPadding = 0.0f;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001019
1020 switch (m_nAlignment) {
1021 case 0:
1022 fPadding = 0.0f;
1023 break;
1024 case 1:
1025 fPadding = (rcPlate.Height() - rcContent.Height()) * 0.5f;
1026 break;
1027 case 2:
1028 fPadding = rcPlate.Height() - rcContent.Height();
1029 break;
1030 }
1031
Dan Sinclairf528eee2017-02-14 11:52:07 -05001032 return CFX_PointF(point.x - (m_ptScrollPos.x - rcPlate.left),
1033 point.y - (m_ptScrollPos.y + fPadding - rcPlate.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001034}
1035
Dan Sinclairf528eee2017-02-14 11:52:07 -05001036CFX_PointF CFX_Edit::EditToVT(const CFX_PointF& point) const {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001037 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1038 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001039
Dan Sinclair05df0752017-03-14 14:43:42 -04001040 float fPadding = 0.0f;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001041
1042 switch (m_nAlignment) {
1043 case 0:
1044 fPadding = 0.0f;
1045 break;
1046 case 1:
1047 fPadding = (rcPlate.Height() - rcContent.Height()) * 0.5f;
1048 break;
1049 case 2:
1050 fPadding = rcPlate.Height() - rcContent.Height();
1051 break;
1052 }
1053
Dan Sinclairf528eee2017-02-14 11:52:07 -05001054 return CFX_PointF(point.x + (m_ptScrollPos.x - rcPlate.left),
1055 point.y + (m_ptScrollPos.y + fPadding - rcPlate.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001056}
1057
Tom Sepez281a9ea2016-02-26 14:24:28 -08001058CFX_FloatRect CFX_Edit::VTToEdit(const CFX_FloatRect& rect) const {
Dan Sinclairf528eee2017-02-14 11:52:07 -05001059 CFX_PointF ptLeftBottom = VTToEdit(CFX_PointF(rect.left, rect.bottom));
1060 CFX_PointF ptRightTop = VTToEdit(CFX_PointF(rect.right, rect.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001061
Tom Sepez281a9ea2016-02-26 14:24:28 -08001062 return CFX_FloatRect(ptLeftBottom.x, ptLeftBottom.y, ptRightTop.x,
1063 ptRightTop.y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001064}
1065
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001066void CFX_Edit::SetScrollInfo() {
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001067 if (!m_pNotify)
1068 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001069
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001070 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
1071 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1072 if (m_bNotifyFlag)
1073 return;
1074
1075 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
1076 m_bNotifyFlag = true;
1077
1078 PWL_SCROLL_INFO Info;
1079 Info.fPlateWidth = rcPlate.top - rcPlate.bottom;
1080 Info.fContentMin = rcContent.bottom;
1081 Info.fContentMax = rcContent.top;
1082 Info.fSmallStep = rcPlate.Height() / 3;
1083 Info.fBigStep = rcPlate.Height();
1084 m_pNotify->SetScrollInfo(Info);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001085}
1086
Dan Sinclair05df0752017-03-14 14:43:42 -04001087void CFX_Edit::SetScrollPosX(float fx) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001088 if (!m_bEnableScroll)
1089 return;
1090
1091 if (m_pVT->IsValid()) {
dsinclair448c4332016-08-02 12:07:35 -07001092 if (!IsFloatEqual(m_ptScrollPos.x, fx)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001093 m_ptScrollPos.x = fx;
dsinclairefd5a992016-07-18 10:04:07 -07001094 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001095 }
1096 }
1097}
1098
Dan Sinclair05df0752017-03-14 14:43:42 -04001099void CFX_Edit::SetScrollPosY(float fy) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001100 if (!m_bEnableScroll)
1101 return;
1102
1103 if (m_pVT->IsValid()) {
dsinclair448c4332016-08-02 12:07:35 -07001104 if (!IsFloatEqual(m_ptScrollPos.y, fy)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001105 m_ptScrollPos.y = fy;
dsinclairefd5a992016-07-18 10:04:07 -07001106 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001107
dsinclaira2919b32016-07-13 10:55:48 -07001108 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001109 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001110 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001111 m_bNotifyFlag = true;
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001112 m_pNotify->SetScrollPosition(fy);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001113 }
1114 }
1115 }
1116 }
1117}
1118
Dan Sinclairf528eee2017-02-14 11:52:07 -05001119void CFX_Edit::SetScrollPos(const CFX_PointF& point) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001120 SetScrollPosX(point.x);
1121 SetScrollPosY(point.y);
1122 SetScrollLimit();
1123 SetCaretInfo();
1124}
1125
Dan Sinclairf528eee2017-02-14 11:52:07 -05001126CFX_PointF CFX_Edit::GetScrollPos() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001127 return m_ptScrollPos;
1128}
1129
1130void CFX_Edit::SetScrollLimit() {
1131 if (m_pVT->IsValid()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001132 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1133 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001134
1135 if (rcPlate.Width() > rcContent.Width()) {
1136 SetScrollPosX(rcPlate.left);
1137 } else {
dsinclair448c4332016-08-02 12:07:35 -07001138 if (IsFloatSmaller(m_ptScrollPos.x, rcContent.left)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001139 SetScrollPosX(rcContent.left);
dsinclair448c4332016-08-02 12:07:35 -07001140 } else if (IsFloatBigger(m_ptScrollPos.x,
1141 rcContent.right - rcPlate.Width())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001142 SetScrollPosX(rcContent.right - rcPlate.Width());
1143 }
1144 }
1145
1146 if (rcPlate.Height() > rcContent.Height()) {
1147 SetScrollPosY(rcPlate.top);
1148 } else {
dsinclair448c4332016-08-02 12:07:35 -07001149 if (IsFloatSmaller(m_ptScrollPos.y,
1150 rcContent.bottom + rcPlate.Height())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001151 SetScrollPosY(rcContent.bottom + rcPlate.Height());
dsinclair448c4332016-08-02 12:07:35 -07001152 } else if (IsFloatBigger(m_ptScrollPos.y, rcContent.top)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001153 SetScrollPosY(rcContent.top);
1154 }
1155 }
1156 }
1157}
1158
1159void CFX_Edit::ScrollToCaret() {
1160 SetScrollLimit();
1161
thestig821d59e2016-05-11 12:59:22 -07001162 if (!m_pVT->IsValid())
1163 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001164
thestig821d59e2016-05-11 12:59:22 -07001165 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1166 pIterator->SetAt(m_wpCaret);
1167
Dan Sinclairf528eee2017-02-14 11:52:07 -05001168 CFX_PointF ptHead;
1169 CFX_PointF ptFoot;
thestig821d59e2016-05-11 12:59:22 -07001170 CPVT_Word word;
1171 CPVT_Line line;
1172 if (pIterator->GetWord(word)) {
1173 ptHead.x = word.ptWord.x + word.fWidth;
1174 ptHead.y = word.ptWord.y + word.fAscent;
1175 ptFoot.x = word.ptWord.x + word.fWidth;
1176 ptFoot.y = word.ptWord.y + word.fDescent;
1177 } else if (pIterator->GetLine(line)) {
1178 ptHead.x = line.ptLine.x;
1179 ptHead.y = line.ptLine.y + line.fLineAscent;
1180 ptFoot.x = line.ptLine.x;
1181 ptFoot.y = line.ptLine.y + line.fLineDescent;
1182 }
1183
Dan Sinclairf528eee2017-02-14 11:52:07 -05001184 CFX_PointF ptHeadEdit = VTToEdit(ptHead);
1185 CFX_PointF ptFootEdit = VTToEdit(ptFoot);
thestig821d59e2016-05-11 12:59:22 -07001186 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
dsinclair448c4332016-08-02 12:07:35 -07001187 if (!IsFloatEqual(rcPlate.left, rcPlate.right)) {
1188 if (IsFloatSmaller(ptHeadEdit.x, rcPlate.left) ||
1189 IsFloatEqual(ptHeadEdit.x, rcPlate.left)) {
thestig821d59e2016-05-11 12:59:22 -07001190 SetScrollPosX(ptHead.x);
dsinclair448c4332016-08-02 12:07:35 -07001191 } else if (IsFloatBigger(ptHeadEdit.x, rcPlate.right)) {
thestig821d59e2016-05-11 12:59:22 -07001192 SetScrollPosX(ptHead.x - rcPlate.Width());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001193 }
thestig821d59e2016-05-11 12:59:22 -07001194 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001195
dsinclair448c4332016-08-02 12:07:35 -07001196 if (!IsFloatEqual(rcPlate.top, rcPlate.bottom)) {
1197 if (IsFloatSmaller(ptFootEdit.y, rcPlate.bottom) ||
1198 IsFloatEqual(ptFootEdit.y, rcPlate.bottom)) {
1199 if (IsFloatSmaller(ptHeadEdit.y, rcPlate.top)) {
thestig821d59e2016-05-11 12:59:22 -07001200 SetScrollPosY(ptFoot.y + rcPlate.Height());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001201 }
dsinclair448c4332016-08-02 12:07:35 -07001202 } else if (IsFloatBigger(ptHeadEdit.y, rcPlate.top)) {
1203 if (IsFloatBigger(ptFootEdit.y, rcPlate.bottom)) {
thestig821d59e2016-05-11 12:59:22 -07001204 SetScrollPosY(ptHead.y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001205 }
1206 }
1207 }
1208}
1209
dsinclairefd5a992016-07-18 10:04:07 -07001210void CFX_Edit::Refresh() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001211 if (m_bEnableRefresh && m_pVT->IsValid()) {
1212 m_Refresh.BeginRefresh();
1213 RefreshPushLineRects(GetVisibleWordRange());
1214
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001215 m_Refresh.NoAnalyse();
1216 m_ptRefreshScrollPos = m_ptScrollPos;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001217
dsinclaira2919b32016-07-13 10:55:48 -07001218 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001219 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001220 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001221 m_bNotifyFlag = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001222 if (const CFX_Edit_RectArray* pRects = m_Refresh.GetRefreshRects()) {
1223 for (int32_t i = 0, sz = pRects->GetSize(); i < sz; i++)
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001224 m_pNotify->InvalidateRect(pRects->GetAt(i));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001225 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001226 }
1227 }
1228
1229 m_Refresh.EndRefresh();
1230 }
1231}
1232
1233void CFX_Edit::RefreshPushLineRects(const CPVT_WordRange& wr) {
thestig821d59e2016-05-11 12:59:22 -07001234 if (!m_pVT->IsValid())
1235 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001236
thestig821d59e2016-05-11 12:59:22 -07001237 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1238 CPVT_WordPlace wpBegin = wr.BeginPos;
1239 m_pVT->UpdateWordPlace(wpBegin);
1240 CPVT_WordPlace wpEnd = wr.EndPos;
1241 m_pVT->UpdateWordPlace(wpEnd);
1242 pIterator->SetAt(wpBegin);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001243
thestig821d59e2016-05-11 12:59:22 -07001244 CPVT_Line lineinfo;
1245 do {
1246 if (!pIterator->GetLine(lineinfo))
1247 break;
1248 if (lineinfo.lineplace.LineCmp(wpEnd) > 0)
1249 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001250
thestig821d59e2016-05-11 12:59:22 -07001251 CFX_FloatRect rcLine(lineinfo.ptLine.x,
1252 lineinfo.ptLine.y + lineinfo.fLineDescent,
1253 lineinfo.ptLine.x + lineinfo.fLineWidth,
1254 lineinfo.ptLine.y + lineinfo.fLineAscent);
1255
1256 m_Refresh.Push(CPVT_WordRange(lineinfo.lineplace, lineinfo.lineEnd),
1257 VTToEdit(rcLine));
1258 } while (pIterator->NextLine());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001259}
1260
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001261void CFX_Edit::RefreshWordRange(const CPVT_WordRange& wr) {
thestig821d59e2016-05-11 12:59:22 -07001262 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1263 CPVT_WordRange wrTemp = wr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001264
thestig821d59e2016-05-11 12:59:22 -07001265 m_pVT->UpdateWordPlace(wrTemp.BeginPos);
1266 m_pVT->UpdateWordPlace(wrTemp.EndPos);
1267 pIterator->SetAt(wrTemp.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001268
thestig821d59e2016-05-11 12:59:22 -07001269 CPVT_Word wordinfo;
1270 CPVT_Line lineinfo;
1271 CPVT_WordPlace place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001272
thestig821d59e2016-05-11 12:59:22 -07001273 while (pIterator->NextWord()) {
1274 place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -07001275 if (place > wrTemp.EndPos)
thestig821d59e2016-05-11 12:59:22 -07001276 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001277
thestig821d59e2016-05-11 12:59:22 -07001278 pIterator->GetWord(wordinfo);
1279 pIterator->GetLine(lineinfo);
thestig821d59e2016-05-11 12:59:22 -07001280 if (place.LineCmp(wrTemp.BeginPos) == 0 ||
1281 place.LineCmp(wrTemp.EndPos) == 0) {
1282 CFX_FloatRect rcWord(wordinfo.ptWord.x,
1283 lineinfo.ptLine.y + lineinfo.fLineDescent,
1284 wordinfo.ptWord.x + wordinfo.fWidth,
1285 lineinfo.ptLine.y + lineinfo.fLineAscent);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001286
dsinclaira2919b32016-07-13 10:55:48 -07001287 if (m_pNotify) {
thestig821d59e2016-05-11 12:59:22 -07001288 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001289 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001290 m_bNotifyFlag = true;
thestig821d59e2016-05-11 12:59:22 -07001291 CFX_FloatRect rcRefresh = VTToEdit(rcWord);
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001292 m_pNotify->InvalidateRect(&rcRefresh);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001293 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001294 }
thestig821d59e2016-05-11 12:59:22 -07001295 } else {
1296 CFX_FloatRect rcLine(lineinfo.ptLine.x,
1297 lineinfo.ptLine.y + lineinfo.fLineDescent,
1298 lineinfo.ptLine.x + lineinfo.fLineWidth,
1299 lineinfo.ptLine.y + lineinfo.fLineAscent);
1300
dsinclaira2919b32016-07-13 10:55:48 -07001301 if (m_pNotify) {
thestig821d59e2016-05-11 12:59:22 -07001302 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001303 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001304 m_bNotifyFlag = true;
thestig821d59e2016-05-11 12:59:22 -07001305 CFX_FloatRect rcRefresh = VTToEdit(rcLine);
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001306 m_pNotify->InvalidateRect(&rcRefresh);
thestig821d59e2016-05-11 12:59:22 -07001307 }
1308 }
1309
1310 pIterator->NextLine();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001311 }
1312 }
1313}
1314
1315void CFX_Edit::SetCaret(const CPVT_WordPlace& place) {
1316 m_wpOldCaret = m_wpCaret;
1317 m_wpCaret = place;
1318}
1319
1320void CFX_Edit::SetCaretInfo() {
dsinclaira2919b32016-07-13 10:55:48 -07001321 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001322 if (!m_bNotifyFlag) {
thestig821d59e2016-05-11 12:59:22 -07001323 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1324 pIterator->SetAt(m_wpCaret);
tsepez63f545c2016-09-13 16:08:49 -07001325
Dan Sinclairf528eee2017-02-14 11:52:07 -05001326 CFX_PointF ptHead;
1327 CFX_PointF ptFoot;
thestig821d59e2016-05-11 12:59:22 -07001328 CPVT_Word word;
1329 CPVT_Line line;
1330 if (pIterator->GetWord(word)) {
1331 ptHead.x = word.ptWord.x + word.fWidth;
1332 ptHead.y = word.ptWord.y + word.fAscent;
1333 ptFoot.x = word.ptWord.x + word.fWidth;
1334 ptFoot.y = word.ptWord.y + word.fDescent;
1335 } else if (pIterator->GetLine(line)) {
1336 ptHead.x = line.ptLine.x;
1337 ptHead.y = line.ptLine.y + line.fLineAscent;
1338 ptFoot.x = line.ptLine.x;
1339 ptFoot.y = line.ptLine.y + line.fLineDescent;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001340 }
1341
Lei Zhanga8c2b912017-03-22 17:41:02 -07001342 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001343 m_bNotifyFlag = true;
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001344 m_pNotify->SetCaret(m_SelState.IsEmpty(), VTToEdit(ptHead),
1345 VTToEdit(ptFoot));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001346 }
1347 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001348}
1349
Dan Sinclairf528eee2017-02-14 11:52:07 -05001350void CFX_Edit::OnMouseDown(const CFX_PointF& point, bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001351 if (!m_pVT->IsValid())
1352 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001353
Tom Sepez52f69b32017-03-21 13:42:38 -07001354 SelectNone();
1355 SetCaret(m_pVT->SearchWordPlace(EditToVT(point)));
1356 m_SelState.Set(m_wpCaret, m_wpCaret);
1357 ScrollToCaret();
1358 SetCaretOrigin();
1359 SetCaretInfo();
1360}
1361
1362void CFX_Edit::OnMouseMove(const CFX_PointF& point, bool bShift, bool bCtrl) {
1363 if (!m_pVT->IsValid())
1364 return;
1365
1366 SetCaret(m_pVT->SearchWordPlace(EditToVT(point)));
1367 if (m_wpCaret == m_wpOldCaret)
1368 return;
1369
1370 m_SelState.SetEndPos(m_wpCaret);
1371 ScrollToCaret();
1372 Refresh();
1373 SetCaretOrigin();
1374 SetCaretInfo();
1375}
1376
1377void CFX_Edit::OnVK_UP(bool bShift, bool bCtrl) {
1378 if (!m_pVT->IsValid())
1379 return;
1380
1381 SetCaret(m_pVT->GetUpWordPlace(m_wpCaret, m_ptCaret));
1382 if (bShift) {
1383 if (m_SelState.IsEmpty())
1384 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1385 else
1386 m_SelState.SetEndPos(m_wpCaret);
1387
1388 if (m_wpOldCaret != m_wpCaret) {
1389 ScrollToCaret();
1390 Refresh();
1391 SetCaretInfo();
1392 }
1393 } else {
1394 SelectNone();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001395 ScrollToCaret();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001396 SetCaretInfo();
1397 }
1398}
1399
Tom Sepez52f69b32017-03-21 13:42:38 -07001400void CFX_Edit::OnVK_DOWN(bool bShift, bool bCtrl) {
1401 if (!m_pVT->IsValid())
1402 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001403
Tom Sepez52f69b32017-03-21 13:42:38 -07001404 SetCaret(m_pVT->GetDownWordPlace(m_wpCaret, m_ptCaret));
1405 if (bShift) {
1406 if (m_SelState.IsEmpty())
1407 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1408 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001409 m_SelState.SetEndPos(m_wpCaret);
1410
Tom Sepez52f69b32017-03-21 13:42:38 -07001411 if (m_wpOldCaret != m_wpCaret) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001412 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001413 Refresh();
Tom Sepez52f69b32017-03-21 13:42:38 -07001414 SetCaretInfo();
1415 }
1416 } else {
1417 SelectNone();
1418 ScrollToCaret();
1419 SetCaretInfo();
1420 }
1421}
1422
1423void CFX_Edit::OnVK_LEFT(bool bShift, bool bCtrl) {
1424 if (!m_pVT->IsValid())
1425 return;
1426
1427 if (bShift) {
1428 if (m_wpCaret == m_pVT->GetLineBeginPlace(m_wpCaret) &&
1429 m_wpCaret != m_pVT->GetSectionBeginPlace(m_wpCaret)) {
1430 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1431 }
1432 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1433 if (m_SelState.IsEmpty())
1434 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1435 else
1436 m_SelState.SetEndPos(m_wpCaret);
1437
1438 if (m_wpOldCaret != m_wpCaret) {
1439 ScrollToCaret();
1440 Refresh();
1441 SetCaretInfo();
1442 }
1443 } else {
1444 if (!m_SelState.IsEmpty()) {
1445 if (m_SelState.BeginPos < m_SelState.EndPos)
1446 SetCaret(m_SelState.BeginPos);
1447 else
1448 SetCaret(m_SelState.EndPos);
1449
1450 SelectNone();
1451 ScrollToCaret();
1452 SetCaretInfo();
1453 } else {
1454 if (m_wpCaret == m_pVT->GetLineBeginPlace(m_wpCaret) &&
1455 m_wpCaret != m_pVT->GetSectionBeginPlace(m_wpCaret)) {
1456 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1457 }
1458 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1459 ScrollToCaret();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001460 SetCaretOrigin();
1461 SetCaretInfo();
1462 }
1463 }
1464}
1465
tsepez4cf55152016-11-02 14:37:54 -07001466void CFX_Edit::OnVK_RIGHT(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001467 if (!m_pVT->IsValid())
1468 return;
1469
1470 if (bShift) {
1471 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1472 if (m_wpCaret == m_pVT->GetLineEndPlace(m_wpCaret) &&
1473 m_wpCaret != m_pVT->GetSectionEndPlace(m_wpCaret))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001474 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1475
Tom Sepez52f69b32017-03-21 13:42:38 -07001476 if (m_SelState.IsEmpty())
1477 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1478 else
1479 m_SelState.SetEndPos(m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001480
Tom Sepez52f69b32017-03-21 13:42:38 -07001481 if (m_wpOldCaret != m_wpCaret) {
1482 ScrollToCaret();
1483 Refresh();
1484 SetCaretInfo();
1485 }
1486 } else {
1487 if (!m_SelState.IsEmpty()) {
1488 if (m_SelState.BeginPos > m_SelState.EndPos)
1489 SetCaret(m_SelState.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001490 else
Tom Sepez52f69b32017-03-21 13:42:38 -07001491 SetCaret(m_SelState.EndPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001492
Tom Sepez52f69b32017-03-21 13:42:38 -07001493 SelectNone();
1494 ScrollToCaret();
1495 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001496 } else {
Tom Sepez52f69b32017-03-21 13:42:38 -07001497 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1498 if (m_wpCaret == m_pVT->GetLineEndPlace(m_wpCaret) &&
1499 m_wpCaret != m_pVT->GetSectionEndPlace(m_wpCaret)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001500 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001501 }
Tom Sepez52f69b32017-03-21 13:42:38 -07001502 ScrollToCaret();
1503 SetCaretOrigin();
1504 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001505 }
1506 }
1507}
1508
tsepez4cf55152016-11-02 14:37:54 -07001509void CFX_Edit::OnVK_HOME(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001510 if (!m_pVT->IsValid())
1511 return;
1512
1513 if (bShift) {
1514 if (bCtrl)
1515 SetCaret(m_pVT->GetBeginWordPlace());
1516 else
1517 SetCaret(m_pVT->GetLineBeginPlace(m_wpCaret));
1518
1519 if (m_SelState.IsEmpty())
1520 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1521 else
1522 m_SelState.SetEndPos(m_wpCaret);
1523
1524 ScrollToCaret();
1525 Refresh();
1526 SetCaretInfo();
1527 } else {
1528 if (!m_SelState.IsEmpty()) {
1529 SetCaret(std::min(m_SelState.BeginPos, m_SelState.EndPos));
1530 SelectNone();
1531 ScrollToCaret();
1532 SetCaretInfo();
1533 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001534 if (bCtrl)
1535 SetCaret(m_pVT->GetBeginWordPlace());
1536 else
1537 SetCaret(m_pVT->GetLineBeginPlace(m_wpCaret));
1538
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001539 ScrollToCaret();
Tom Sepez52f69b32017-03-21 13:42:38 -07001540 SetCaretOrigin();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001541 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001542 }
1543 }
1544}
1545
tsepez4cf55152016-11-02 14:37:54 -07001546void CFX_Edit::OnVK_END(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001547 if (!m_pVT->IsValid())
1548 return;
1549
1550 if (bShift) {
1551 if (bCtrl)
1552 SetCaret(m_pVT->GetEndWordPlace());
1553 else
1554 SetCaret(m_pVT->GetLineEndPlace(m_wpCaret));
1555
1556 if (m_SelState.IsEmpty())
1557 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1558 else
1559 m_SelState.SetEndPos(m_wpCaret);
1560
1561 ScrollToCaret();
1562 Refresh();
1563 SetCaretInfo();
1564 } else {
1565 if (!m_SelState.IsEmpty()) {
1566 SetCaret(std::max(m_SelState.BeginPos, m_SelState.EndPos));
1567 SelectNone();
1568 ScrollToCaret();
1569 SetCaretInfo();
1570 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001571 if (bCtrl)
1572 SetCaret(m_pVT->GetEndWordPlace());
1573 else
1574 SetCaret(m_pVT->GetLineEndPlace(m_wpCaret));
1575
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001576 ScrollToCaret();
Tom Sepez52f69b32017-03-21 13:42:38 -07001577 SetCaretOrigin();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001578 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001579 }
1580 }
1581}
1582
tsepez4cf55152016-11-02 14:37:54 -07001583bool CFX_Edit::InsertWord(uint16_t word,
1584 int32_t charset,
1585 const CPVT_WordProps* pWordProps,
1586 bool bAddUndo,
1587 bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001588 if (IsTextOverflow() || !m_pVT->IsValid())
tsepez4cf55152016-11-02 14:37:54 -07001589 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001590
Tom Sepez3509d162017-01-30 13:22:02 -08001591 m_pVT->UpdateWordPlace(m_wpCaret);
1592 SetCaret(m_pVT->InsertWord(m_wpCaret, word,
1593 GetCharSetFromUnicode(word, charset), pWordProps));
1594 m_SelState.Set(m_wpCaret, m_wpCaret);
1595 if (m_wpCaret == m_wpOldCaret)
1596 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001597
Tom Sepez3509d162017-01-30 13:22:02 -08001598 if (bAddUndo && m_bEnableUndo) {
1599 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertWord>(
1600 this, m_wpOldCaret, m_wpCaret, word, charset, pWordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001601 }
Tom Sepez3509d162017-01-30 13:22:02 -08001602 if (bPaint)
1603 PaintInsertText(m_wpOldCaret, m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001604
Tom Sepez3509d162017-01-30 13:22:02 -08001605 if (m_bOprNotify && m_pOprNotify)
1606 m_pOprNotify->OnInsertWord(m_wpCaret, m_wpOldCaret);
1607
1608 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001609}
1610
tsepez4cf55152016-11-02 14:37:54 -07001611bool CFX_Edit::InsertReturn(const CPVT_SecProps* pSecProps,
1612 const CPVT_WordProps* pWordProps,
1613 bool bAddUndo,
1614 bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001615 if (IsTextOverflow() || !m_pVT->IsValid())
tsepez4cf55152016-11-02 14:37:54 -07001616 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001617
Tom Sepez3509d162017-01-30 13:22:02 -08001618 m_pVT->UpdateWordPlace(m_wpCaret);
1619 SetCaret(m_pVT->InsertSection(m_wpCaret, pSecProps, pWordProps));
1620 m_SelState.Set(m_wpCaret, m_wpCaret);
1621 if (m_wpCaret == m_wpOldCaret)
1622 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001623
Tom Sepez3509d162017-01-30 13:22:02 -08001624 if (bAddUndo && m_bEnableUndo) {
1625 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertReturn>(
1626 this, m_wpOldCaret, m_wpCaret, pSecProps, pWordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001627 }
Tom Sepez3509d162017-01-30 13:22:02 -08001628 if (bPaint) {
1629 RearrangePart(CPVT_WordRange(m_wpOldCaret, m_wpCaret));
1630 ScrollToCaret();
1631 Refresh();
1632 SetCaretOrigin();
1633 SetCaretInfo();
1634 }
1635 if (m_bOprNotify && m_pOprNotify)
1636 m_pOprNotify->OnInsertReturn(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001637
Tom Sepez3509d162017-01-30 13:22:02 -08001638 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001639}
1640
tsepez4cf55152016-11-02 14:37:54 -07001641bool CFX_Edit::Backspace(bool bAddUndo, bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001642 if (!m_pVT->IsValid() || m_wpCaret == m_pVT->GetBeginWordPlace())
1643 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001644
Tom Sepez3509d162017-01-30 13:22:02 -08001645 CPVT_Section section;
1646 CPVT_Word word;
1647 if (bAddUndo) {
1648 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1649 pIterator->SetAt(m_wpCaret);
1650 pIterator->GetSection(section);
1651 pIterator->GetWord(word);
1652 }
1653 m_pVT->UpdateWordPlace(m_wpCaret);
1654 SetCaret(m_pVT->BackSpaceWord(m_wpCaret));
1655 m_SelState.Set(m_wpCaret, m_wpCaret);
1656 if (m_wpCaret == m_wpOldCaret)
1657 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001658
Tom Sepez3509d162017-01-30 13:22:02 -08001659 if (bAddUndo && m_bEnableUndo) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001660 if (m_wpCaret.nSecIndex != m_wpOldCaret.nSecIndex) {
Tom Sepez3509d162017-01-30 13:22:02 -08001661 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Backspace>(
1662 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1663 section.SecProps, section.WordProps));
1664 } else {
1665 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Backspace>(
1666 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1667 section.SecProps, word.WordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001668 }
1669 }
Tom Sepez3509d162017-01-30 13:22:02 -08001670 if (bPaint) {
1671 RearrangePart(CPVT_WordRange(m_wpCaret, m_wpOldCaret));
1672 ScrollToCaret();
1673 Refresh();
1674 SetCaretOrigin();
1675 SetCaretInfo();
1676 }
1677 if (m_bOprNotify && m_pOprNotify)
1678 m_pOprNotify->OnBackSpace(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001679
Tom Sepez3509d162017-01-30 13:22:02 -08001680 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001681}
1682
tsepez4cf55152016-11-02 14:37:54 -07001683bool CFX_Edit::Delete(bool bAddUndo, bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001684 if (!m_pVT->IsValid() || m_wpCaret == m_pVT->GetEndWordPlace())
1685 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001686
Tom Sepez3509d162017-01-30 13:22:02 -08001687 CPVT_Section section;
1688 CPVT_Word word;
1689 if (bAddUndo) {
1690 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1691 pIterator->SetAt(m_pVT->GetNextWordPlace(m_wpCaret));
1692 pIterator->GetSection(section);
1693 pIterator->GetWord(word);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001694 }
Tom Sepez3509d162017-01-30 13:22:02 -08001695 m_pVT->UpdateWordPlace(m_wpCaret);
1696 bool bSecEnd = (m_wpCaret == m_pVT->GetSectionEndPlace(m_wpCaret));
1697 SetCaret(m_pVT->DeleteWord(m_wpCaret));
1698 m_SelState.Set(m_wpCaret, m_wpCaret);
1699 if (bAddUndo && m_bEnableUndo) {
1700 if (bSecEnd) {
1701 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Delete>(
1702 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1703 section.SecProps, section.WordProps, bSecEnd));
1704 } else {
1705 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Delete>(
1706 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1707 section.SecProps, word.WordProps, bSecEnd));
1708 }
1709 }
1710 if (bPaint) {
1711 RearrangePart(CPVT_WordRange(m_wpOldCaret, m_wpCaret));
1712 ScrollToCaret();
1713 Refresh();
1714 SetCaretOrigin();
1715 SetCaretInfo();
1716 }
1717 if (m_bOprNotify && m_pOprNotify)
1718 m_pOprNotify->OnDelete(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001719
Tom Sepez3509d162017-01-30 13:22:02 -08001720 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001721}
1722
tsepez4cf55152016-11-02 14:37:54 -07001723bool CFX_Edit::Empty() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001724 if (m_pVT->IsValid()) {
1725 m_pVT->DeleteWords(GetWholeWordRange());
1726 SetCaret(m_pVT->GetBeginWordPlace());
1727
tsepez4cf55152016-11-02 14:37:54 -07001728 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001729 }
1730
tsepez4cf55152016-11-02 14:37:54 -07001731 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001732}
1733
tsepez4cf55152016-11-02 14:37:54 -07001734bool CFX_Edit::Clear(bool bAddUndo, bool bPaint) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001735 if (!m_pVT->IsValid() || m_SelState.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001736 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001737
thestig821d59e2016-05-11 12:59:22 -07001738 CPVT_WordRange range = m_SelState.ConvertToWordRange();
dsinclaira2919b32016-07-13 10:55:48 -07001739 if (bAddUndo && m_bEnableUndo)
Diana Gage89e65622017-07-20 18:09:31 -07001740 AddEditUndoItem(
1741 pdfium::MakeUnique<CFXEU_Clear>(this, range, GetSelectedText()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001742
thestig821d59e2016-05-11 12:59:22 -07001743 SelectNone();
1744 SetCaret(m_pVT->DeleteWords(range));
1745 m_SelState.Set(m_wpCaret, m_wpCaret);
thestig821d59e2016-05-11 12:59:22 -07001746 if (bPaint) {
1747 RearrangePart(range);
1748 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001749 Refresh();
thestig821d59e2016-05-11 12:59:22 -07001750 SetCaretOrigin();
1751 SetCaretInfo();
1752 }
thestig821d59e2016-05-11 12:59:22 -07001753 if (m_bOprNotify && m_pOprNotify)
1754 m_pOprNotify->OnClear(m_wpCaret, m_wpOldCaret);
1755
tsepez4cf55152016-11-02 14:37:54 -07001756 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001757}
1758
tsepez4cf55152016-11-02 14:37:54 -07001759bool CFX_Edit::InsertText(const CFX_WideString& sText,
1760 int32_t charset,
1761 bool bAddUndo,
1762 bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001763 if (IsTextOverflow())
tsepez4cf55152016-11-02 14:37:54 -07001764 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001765
1766 m_pVT->UpdateWordPlace(m_wpCaret);
tsepeza31da742016-09-08 11:28:14 -07001767 SetCaret(DoInsertText(m_wpCaret, sText, charset));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001768 m_SelState.Set(m_wpCaret, m_wpCaret);
tsepeza31da742016-09-08 11:28:14 -07001769 if (m_wpCaret == m_wpOldCaret)
tsepez4cf55152016-11-02 14:37:54 -07001770 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001771
tsepeza31da742016-09-08 11:28:14 -07001772 if (bAddUndo && m_bEnableUndo) {
Tom Sepez3509d162017-01-30 13:22:02 -08001773 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertText>(
1774 this, m_wpOldCaret, m_wpCaret, sText, charset));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001775 }
tsepeza31da742016-09-08 11:28:14 -07001776 if (bPaint)
1777 PaintInsertText(m_wpOldCaret, m_wpCaret);
1778
1779 if (m_bOprNotify && m_pOprNotify)
1780 m_pOprNotify->OnInsertText(m_wpCaret, m_wpOldCaret);
1781
tsepez4cf55152016-11-02 14:37:54 -07001782 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001783}
1784
1785void CFX_Edit::PaintInsertText(const CPVT_WordPlace& wpOld,
1786 const CPVT_WordPlace& wpNew) {
1787 if (m_pVT->IsValid()) {
1788 RearrangePart(CPVT_WordRange(wpOld, wpNew));
1789 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001790 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001791 SetCaretOrigin();
1792 SetCaretInfo();
1793 }
1794}
1795
tsepez4cf55152016-11-02 14:37:54 -07001796bool CFX_Edit::Redo() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001797 if (m_bEnableUndo) {
1798 if (m_Undo.CanRedo()) {
1799 m_Undo.Redo();
tsepez4cf55152016-11-02 14:37:54 -07001800 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001801 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001802 }
1803
tsepez4cf55152016-11-02 14:37:54 -07001804 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001805}
1806
tsepez4cf55152016-11-02 14:37:54 -07001807bool CFX_Edit::Undo() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001808 if (m_bEnableUndo) {
1809 if (m_Undo.CanUndo()) {
1810 m_Undo.Undo();
tsepez4cf55152016-11-02 14:37:54 -07001811 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001812 }
1813 }
1814
tsepez4cf55152016-11-02 14:37:54 -07001815 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001816}
1817
1818void CFX_Edit::SetCaretOrigin() {
thestig821d59e2016-05-11 12:59:22 -07001819 if (!m_pVT->IsValid())
1820 return;
1821
1822 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1823 pIterator->SetAt(m_wpCaret);
1824 CPVT_Word word;
1825 CPVT_Line line;
1826 if (pIterator->GetWord(word)) {
1827 m_ptCaret.x = word.ptWord.x + word.fWidth;
1828 m_ptCaret.y = word.ptWord.y;
1829 } else if (pIterator->GetLine(line)) {
1830 m_ptCaret.x = line.ptLine.x;
1831 m_ptCaret.y = line.ptLine.y;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001832 }
1833}
1834
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001835CPVT_WordPlace CFX_Edit::WordIndexToWordPlace(int32_t index) const {
1836 if (m_pVT->IsValid())
1837 return m_pVT->WordIndexToWordPlace(index);
1838
1839 return CPVT_WordPlace();
1840}
1841
tsepez4cf55152016-11-02 14:37:54 -07001842bool CFX_Edit::IsTextFull() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001843 int32_t nTotalWords = m_pVT->GetTotalWords();
1844 int32_t nLimitChar = m_pVT->GetLimitChar();
1845 int32_t nCharArray = m_pVT->GetCharArray();
1846
1847 return IsTextOverflow() || (nLimitChar > 0 && nTotalWords >= nLimitChar) ||
1848 (nCharArray > 0 && nTotalWords >= nCharArray);
1849}
1850
tsepez4cf55152016-11-02 14:37:54 -07001851bool CFX_Edit::IsTextOverflow() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001852 if (!m_bEnableScroll && !m_bEnableOverflow) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001853 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
1854 CFX_FloatRect rcContent = m_pVT->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001855
dsinclair448c4332016-08-02 12:07:35 -07001856 if (m_pVT->IsMultiLine() && GetTotalLines() > 1 &&
1857 IsFloatBigger(rcContent.Height(), rcPlate.Height())) {
tsepez4cf55152016-11-02 14:37:54 -07001858 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001859 }
1860
dsinclair448c4332016-08-02 12:07:35 -07001861 if (IsFloatBigger(rcContent.Width(), rcPlate.Width()))
tsepez4cf55152016-11-02 14:37:54 -07001862 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001863 }
1864
tsepez4cf55152016-11-02 14:37:54 -07001865 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001866}
1867
tsepez4cf55152016-11-02 14:37:54 -07001868bool CFX_Edit::CanUndo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001869 if (m_bEnableUndo) {
1870 return m_Undo.CanUndo();
1871 }
1872
tsepez4cf55152016-11-02 14:37:54 -07001873 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001874}
1875
tsepez4cf55152016-11-02 14:37:54 -07001876bool CFX_Edit::CanRedo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001877 if (m_bEnableUndo) {
1878 return m_Undo.CanRedo();
1879 }
1880
tsepez4cf55152016-11-02 14:37:54 -07001881 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001882}
1883
tsepez4cf55152016-11-02 14:37:54 -07001884void CFX_Edit::EnableRefresh(bool bRefresh) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001885 m_bEnableRefresh = bRefresh;
1886}
1887
tsepez4cf55152016-11-02 14:37:54 -07001888void CFX_Edit::EnableUndo(bool bUndo) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001889 m_bEnableUndo = bUndo;
1890}
1891
tsepez4cf55152016-11-02 14:37:54 -07001892void CFX_Edit::EnableOprNotify(bool bNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001893 m_bOprNotify = bNotify;
1894}
1895
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001896CPVT_WordPlace CFX_Edit::DoInsertText(const CPVT_WordPlace& place,
tsepeza31da742016-09-08 11:28:14 -07001897 const CFX_WideString& sText,
dsinclairefd5a992016-07-18 10:04:07 -07001898 int32_t charset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001899 CPVT_WordPlace wp = place;
1900
1901 if (m_pVT->IsValid()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001902 for (int32_t i = 0, sz = sText.GetLength(); i < sz; i++) {
Tom Sepez62a70f92016-03-21 15:00:20 -07001903 uint16_t word = sText[i];
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001904 switch (word) {
1905 case 0x0D:
dsinclairefd5a992016-07-18 10:04:07 -07001906 wp = m_pVT->InsertSection(wp, nullptr, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001907 if (sText[i + 1] == 0x0A)
1908 i++;
1909 break;
1910 case 0x0A:
dsinclairefd5a992016-07-18 10:04:07 -07001911 wp = m_pVT->InsertSection(wp, nullptr, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001912 if (sText[i + 1] == 0x0D)
1913 i++;
1914 break;
1915 case 0x09:
1916 word = 0x20;
1917 default:
1918 wp = m_pVT->InsertWord(wp, word, GetCharSetFromUnicode(word, charset),
dsinclairefd5a992016-07-18 10:04:07 -07001919 nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001920 break;
1921 }
1922 }
1923 }
1924
1925 return wp;
1926}
1927
Tom Sepez62a70f92016-03-21 15:00:20 -07001928int32_t CFX_Edit::GetCharSetFromUnicode(uint16_t word, int32_t nOldCharset) {
dsinclairc7a73492016-04-05 12:01:42 -07001929 if (IPVT_FontMap* pFontMap = GetFontMap())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001930 return pFontMap->CharSetFromUnicode(word, nOldCharset);
1931 return nOldCharset;
1932}
1933
Tom Sepez3509d162017-01-30 13:22:02 -08001934void CFX_Edit::AddEditUndoItem(
1935 std::unique_ptr<CFX_Edit_UndoItem> pEditUndoItem) {
Lei Zhang1a89e362017-03-23 15:27:25 -07001936 m_Undo.AddItem(std::move(pEditUndoItem));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001937}
1938
weili625ad662016-06-15 11:21:33 -07001939CFX_Edit_LineRectArray::CFX_Edit_LineRectArray() {}
1940
Tom Sepez3509d162017-01-30 13:22:02 -08001941CFX_Edit_LineRectArray::~CFX_Edit_LineRectArray() {}
weili625ad662016-06-15 11:21:33 -07001942
Tom Sepez3509d162017-01-30 13:22:02 -08001943void CFX_Edit_LineRectArray::operator=(CFX_Edit_LineRectArray&& that) {
1944 m_LineRects = std::move(that.m_LineRects);
weili625ad662016-06-15 11:21:33 -07001945}
1946
1947void CFX_Edit_LineRectArray::Add(const CPVT_WordRange& wrLine,
1948 const CFX_FloatRect& rcLine) {
Tom Sepez3509d162017-01-30 13:22:02 -08001949 m_LineRects.push_back(pdfium::MakeUnique<CFX_Edit_LineRect>(wrLine, rcLine));
weili625ad662016-06-15 11:21:33 -07001950}
1951
1952int32_t CFX_Edit_LineRectArray::GetSize() const {
Tom Sepez3509d162017-01-30 13:22:02 -08001953 return pdfium::CollectionSize<int32_t>(m_LineRects);
weili625ad662016-06-15 11:21:33 -07001954}
1955
1956CFX_Edit_LineRect* CFX_Edit_LineRectArray::GetAt(int32_t nIndex) const {
Tom Sepez3509d162017-01-30 13:22:02 -08001957 if (nIndex < 0 || nIndex >= GetSize())
weili625ad662016-06-15 11:21:33 -07001958 return nullptr;
1959
Tom Sepez3509d162017-01-30 13:22:02 -08001960 return m_LineRects[nIndex].get();
weili625ad662016-06-15 11:21:33 -07001961}
1962
1963CFX_Edit_Select::CFX_Edit_Select() {}
1964
weili625ad662016-06-15 11:21:33 -07001965CFX_Edit_Select::CFX_Edit_Select(const CPVT_WordRange& range) {
1966 Set(range.BeginPos, range.EndPos);
1967}
1968
1969CPVT_WordRange CFX_Edit_Select::ConvertToWordRange() const {
1970 return CPVT_WordRange(BeginPos, EndPos);
1971}
1972
Tom Sepez52f69b32017-03-21 13:42:38 -07001973void CFX_Edit_Select::Reset() {
1974 BeginPos.Reset();
1975 EndPos.Reset();
weili625ad662016-06-15 11:21:33 -07001976}
1977
1978void CFX_Edit_Select::Set(const CPVT_WordPlace& begin,
1979 const CPVT_WordPlace& end) {
1980 BeginPos = begin;
1981 EndPos = end;
1982}
1983
weili625ad662016-06-15 11:21:33 -07001984void CFX_Edit_Select::SetEndPos(const CPVT_WordPlace& end) {
1985 EndPos = end;
1986}
1987
Tom Sepez52f69b32017-03-21 13:42:38 -07001988bool CFX_Edit_Select::IsEmpty() const {
1989 return BeginPos == EndPos;
weili625ad662016-06-15 11:21:33 -07001990}
1991
weili625ad662016-06-15 11:21:33 -07001992CFX_Edit_RectArray::CFX_Edit_RectArray() {}
1993
Tom Sepez3509d162017-01-30 13:22:02 -08001994CFX_Edit_RectArray::~CFX_Edit_RectArray() {}
weili625ad662016-06-15 11:21:33 -07001995
Tom Sepez3509d162017-01-30 13:22:02 -08001996void CFX_Edit_RectArray::Clear() {
1997 m_Rects.clear();
weili625ad662016-06-15 11:21:33 -07001998}
1999
2000void CFX_Edit_RectArray::Add(const CFX_FloatRect& rect) {
2001 // check for overlapped area
Tom Sepez3509d162017-01-30 13:22:02 -08002002 for (const auto& pRect : m_Rects) {
weili625ad662016-06-15 11:21:33 -07002003 if (pRect && pRect->Contains(rect))
2004 return;
2005 }
Tom Sepez3509d162017-01-30 13:22:02 -08002006 m_Rects.push_back(pdfium::MakeUnique<CFX_FloatRect>(rect));
weili625ad662016-06-15 11:21:33 -07002007}
2008
2009int32_t CFX_Edit_RectArray::GetSize() const {
Tom Sepez3509d162017-01-30 13:22:02 -08002010 return pdfium::CollectionSize<int32_t>(m_Rects);
weili625ad662016-06-15 11:21:33 -07002011}
2012
2013CFX_FloatRect* CFX_Edit_RectArray::GetAt(int32_t nIndex) const {
Tom Sepez3509d162017-01-30 13:22:02 -08002014 if (nIndex < 0 || nIndex >= GetSize())
weili625ad662016-06-15 11:21:33 -07002015 return nullptr;
2016
Tom Sepez3509d162017-01-30 13:22:02 -08002017 return m_Rects[nIndex].get();
weili625ad662016-06-15 11:21:33 -07002018}