blob: 89b19de63dc36a2fb0d4d1759778791169e4c322 [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
Dan Sinclairc411eb92017-07-25 09:39:30 -04007#include "fpdfsdk/pwl/cpwl_edit_impl.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 Sinclairb8966992017-09-21 14:51:57 -040025#include "core/fxcrt/autorestorer.h"
Dan Sinclairf51a02a2017-04-19 12:46:53 -040026#include "core/fxcrt/fx_codepage.h"
dsinclair74a34fc2016-09-29 16:41:42 -070027#include "core/fxge/cfx_graphstatedata.h"
28#include "core/fxge/cfx_pathdata.h"
29#include "core/fxge/cfx_renderdevice.h"
dsinclaire35af1e2016-07-13 11:26:20 -070030#include "fpdfsdk/cfx_systemhandler.h"
Dan Sinclairc411eb92017-07-25 09:39:30 -040031#include "fpdfsdk/pwl/cpwl_edit.h"
32#include "fpdfsdk/pwl/cpwl_edit_ctrl.h"
33#include "fpdfsdk/pwl/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,
Lei Zhangeb14e042017-08-15 13:56:43 -070045 const CFX_Matrix& mtUser2Device,
Ryan Harrison275e2602017-09-18 14:23:18 -040046 const ByteString& str,
dsinclaire35af1e2016-07-13 11:26:20 -070047 FX_ARGB crTextFill,
dsinclaire35af1e2016-07-13 11:26:20 -070048 int32_t nHorzScale) {
Lei Zhangeb14e042017-08-15 13:56:43 -070049 if (!pFont)
50 return;
dsinclaire35af1e2016-07-13 11:26:20 -070051
Lei Zhangeb14e042017-08-15 13:56:43 -070052 CFX_PointF pos = mtUser2Device.Transform(pt);
53 CFX_Matrix mt;
54 if (nHorzScale == 100) {
55 mt = mtUser2Device;
56 } else {
57 mt = CFX_Matrix(nHorzScale / 100.0f, 0, 0, 1, 0, 0);
58 mt.Concat(mtUser2Device);
dsinclaire35af1e2016-07-13 11:26:20 -070059 }
Lei Zhangeb14e042017-08-15 13:56:43 -070060
61 CPDF_RenderOptions ro;
62 ro.m_Flags = RENDER_CLEARTYPE;
63 ro.m_ColorMode = CPDF_RenderOptions::kNormal;
64 CPDF_TextRenderer::DrawTextString(pDevice, pos.x, pos.y, pFont, fFontSize,
65 &mt, str, crTextFill, nullptr, &ro);
dsinclaire35af1e2016-07-13 11:26:20 -070066}
67
dsinclair8f4bf9a2016-05-04 13:51:51 -070068} // namespace
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070069
Dan Sinclair6b0158f2017-07-24 09:42:55 -040070CPWL_EditImpl_Iterator::CPWL_EditImpl_Iterator(
71 CPWL_EditImpl* pEdit,
72 CPDF_VariableText::Iterator* pVTIterator)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 : m_pEdit(pEdit), m_pVTIterator(pVTIterator) {}
74
Dan Sinclair6b0158f2017-07-24 09:42:55 -040075CPWL_EditImpl_Iterator::~CPWL_EditImpl_Iterator() {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076
Dan Sinclair6b0158f2017-07-24 09:42:55 -040077bool CPWL_EditImpl_Iterator::NextWord() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 return m_pVTIterator->NextWord();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070079}
80
Dan Sinclair6b0158f2017-07-24 09:42:55 -040081bool CPWL_EditImpl_Iterator::PrevWord() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082 return m_pVTIterator->PrevWord();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070083}
84
Dan Sinclair6b0158f2017-07-24 09:42:55 -040085bool CPWL_EditImpl_Iterator::GetWord(CPVT_Word& word) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 ASSERT(m_pEdit);
87
88 if (m_pVTIterator->GetWord(word)) {
89 word.ptWord = m_pEdit->VTToEdit(word.ptWord);
tsepez4cf55152016-11-02 14:37:54 -070090 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 }
tsepez4cf55152016-11-02 14:37:54 -070092 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070093}
94
Dan Sinclair6b0158f2017-07-24 09:42:55 -040095bool CPWL_EditImpl_Iterator::GetLine(CPVT_Line& line) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 ASSERT(m_pEdit);
97
98 if (m_pVTIterator->GetLine(line)) {
99 line.ptLine = m_pEdit->VTToEdit(line.ptLine);
tsepez4cf55152016-11-02 14:37:54 -0700100 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 }
tsepez4cf55152016-11-02 14:37:54 -0700102 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700103}
104
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400105void CPWL_EditImpl_Iterator::SetAt(int32_t nWordIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106 m_pVTIterator->SetAt(nWordIndex);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700107}
108
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400109void CPWL_EditImpl_Iterator::SetAt(const CPVT_WordPlace& place) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110 m_pVTIterator->SetAt(place);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700111}
112
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400113const CPVT_WordPlace& CPWL_EditImpl_Iterator::GetAt() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114 return m_pVTIterator->GetAt();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700115}
116
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400117CPWL_EditImpl_Provider::CPWL_EditImpl_Provider(IPVT_FontMap* pFontMap)
dsinclairc7a73492016-04-05 12:01:42 -0700118 : CPDF_VariableText::Provider(pFontMap), m_pFontMap(pFontMap) {
Lei Zhang96660d62015-12-14 18:27:25 -0800119 ASSERT(m_pFontMap);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700120}
121
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400122CPWL_EditImpl_Provider::~CPWL_EditImpl_Provider() {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700123
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400124IPVT_FontMap* CPWL_EditImpl_Provider::GetFontMap() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125 return m_pFontMap;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700126}
127
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400128int32_t CPWL_EditImpl_Provider::GetCharWidth(int32_t nFontIndex,
129 uint16_t word) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex)) {
tsepezc3255f52016-03-25 14:52:27 -0700131 uint32_t charcode = word;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700132
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133 if (pPDFFont->IsUnicodeCompatible())
134 charcode = pPDFFont->CharCodeFromUnicode(word);
135 else
136 charcode = m_pFontMap->CharCodeFromUnicode(nFontIndex, word);
137
Wei Li89409932016-03-28 10:33:33 -0700138 if (charcode != CPDF_Font::kInvalidCharCode)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139 return pPDFFont->GetCharWidthF(charcode);
140 }
141
142 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700143}
144
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400145int32_t CPWL_EditImpl_Provider::GetTypeAscent(int32_t nFontIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146 if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex))
147 return pPDFFont->GetTypeAscent();
148
149 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700150}
151
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400152int32_t CPWL_EditImpl_Provider::GetTypeDescent(int32_t nFontIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153 if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex))
154 return pPDFFont->GetTypeDescent();
155
156 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700157}
158
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400159int32_t CPWL_EditImpl_Provider::GetWordFontIndex(uint16_t word,
160 int32_t charset,
161 int32_t nFontIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162 return m_pFontMap->GetWordFontIndex(word, charset, nFontIndex);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700163}
164
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400165int32_t CPWL_EditImpl_Provider::GetDefaultFontIndex() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700166 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700167}
168
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400169bool CPWL_EditImpl_Provider::IsLatinWord(uint16_t word) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700170 return FX_EDIT_ISLATINWORD(word);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700171}
172
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400173CPWL_EditImpl_Refresh::CPWL_EditImpl_Refresh() {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700174
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400175CPWL_EditImpl_Refresh::~CPWL_EditImpl_Refresh() {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700176
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400177void CPWL_EditImpl_Refresh::BeginRefresh() {
Tom Sepez3509d162017-01-30 13:22:02 -0800178 m_RefreshRects.Clear();
179 m_OldLineRects = std::move(m_NewLineRects);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700180}
181
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400182void CPWL_EditImpl_Refresh::Push(const CPVT_WordRange& linerange,
183 const CFX_FloatRect& rect) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700184 m_NewLineRects.Add(linerange, rect);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700185}
186
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400187void CPWL_EditImpl_Refresh::NoAnalyse() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188 {
189 for (int32_t i = 0, sz = m_OldLineRects.GetSize(); i < sz; i++)
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400190 if (CPWL_EditImpl_LineRect* pOldRect = m_OldLineRects.GetAt(i))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191 m_RefreshRects.Add(pOldRect->m_rcLine);
192 }
193
194 {
195 for (int32_t i = 0, sz = m_NewLineRects.GetSize(); i < sz; i++)
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400196 if (CPWL_EditImpl_LineRect* pNewRect = m_NewLineRects.GetAt(i))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700197 m_RefreshRects.Add(pNewRect->m_rcLine);
198 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700199}
200
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400201const CPWL_EditImpl_RectArray* CPWL_EditImpl_Refresh::GetRefreshRects() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202 return &m_RefreshRects;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700203}
204
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400205void CPWL_EditImpl_Refresh::EndRefresh() {
Tom Sepez3509d162017-01-30 13:22:02 -0800206 m_RefreshRects.Clear();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700207}
208
Lei Zhangbff66f12017-08-15 13:26:44 -0700209CPWL_EditImpl_Undo::CPWL_EditImpl_Undo()
210 : m_nCurUndoPos(0), m_bWorking(false) {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211
Lei Zhangbff66f12017-08-15 13:26:44 -0700212CPWL_EditImpl_Undo::~CPWL_EditImpl_Undo() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700213
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400214bool CPWL_EditImpl_Undo::CanUndo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215 return m_nCurUndoPos > 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700216}
217
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400218void CPWL_EditImpl_Undo::Undo() {
tsepez4cf55152016-11-02 14:37:54 -0700219 m_bWorking = true;
Lei Zhangbff66f12017-08-15 13:26:44 -0700220 if (CanUndo()) {
Tom Sepez3509d162017-01-30 13:22:02 -0800221 m_UndoItemStack[m_nCurUndoPos - 1]->Undo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222 m_nCurUndoPos--;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223 }
tsepez4cf55152016-11-02 14:37:54 -0700224 m_bWorking = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700225}
226
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400227bool CPWL_EditImpl_Undo::CanRedo() const {
Tom Sepez3509d162017-01-30 13:22:02 -0800228 return m_nCurUndoPos < m_UndoItemStack.size();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700229}
230
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400231void CPWL_EditImpl_Undo::Redo() {
tsepez4cf55152016-11-02 14:37:54 -0700232 m_bWorking = true;
Lei Zhangbff66f12017-08-15 13:26:44 -0700233 if (CanRedo()) {
Tom Sepez3509d162017-01-30 13:22:02 -0800234 m_UndoItemStack[m_nCurUndoPos]->Redo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700235 m_nCurUndoPos++;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236 }
tsepez4cf55152016-11-02 14:37:54 -0700237 m_bWorking = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700238}
239
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400240void CPWL_EditImpl_Undo::AddItem(std::unique_ptr<IFX_Edit_UndoItem> pItem) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241 ASSERT(!m_bWorking);
Lei Zhang96660d62015-12-14 18:27:25 -0800242 ASSERT(pItem);
Lei Zhangbff66f12017-08-15 13:26:44 -0700243 if (CanRedo())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244 RemoveTails();
245
Lei Zhangbff66f12017-08-15 13:26:44 -0700246 if (m_UndoItemStack.size() >= kEditUndoMaxItems)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700247 RemoveHeads();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248
Tom Sepez3509d162017-01-30 13:22:02 -0800249 m_UndoItemStack.push_back(std::move(pItem));
250 m_nCurUndoPos = m_UndoItemStack.size();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700251}
252
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400253void CPWL_EditImpl_Undo::RemoveHeads() {
Tom Sepez3509d162017-01-30 13:22:02 -0800254 ASSERT(m_UndoItemStack.size() > 1);
255 m_UndoItemStack.pop_front();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700256}
257
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400258void CPWL_EditImpl_Undo::RemoveTails() {
Lei Zhangbff66f12017-08-15 13:26:44 -0700259 while (CanRedo())
Tom Sepez3509d162017-01-30 13:22:02 -0800260 m_UndoItemStack.pop_back();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261}
262
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400263CFXEU_InsertWord::CFXEU_InsertWord(CPWL_EditImpl* pEdit,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264 const CPVT_WordPlace& wpOldPlace,
265 const CPVT_WordPlace& wpNewPlace,
Tom Sepez62a70f92016-03-21 15:00:20 -0700266 uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 int32_t charset,
268 const CPVT_WordProps* pWordProps)
269 : m_pEdit(pEdit),
270 m_wpOld(wpOldPlace),
271 m_wpNew(wpNewPlace),
272 m_Word(word),
273 m_nCharset(charset),
274 m_WordProps() {
Lei Zhangf5a06672017-08-12 08:01:37 -0700275 ASSERT(m_pEdit);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276 if (pWordProps)
277 m_WordProps = *pWordProps;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700278}
279
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280CFXEU_InsertWord::~CFXEU_InsertWord() {}
281
282void CFXEU_InsertWord::Redo() {
Lei Zhangf5a06672017-08-12 08:01:37 -0700283 m_pEdit->SelectNone();
284 m_pEdit->SetCaret(m_wpOld);
285 m_pEdit->InsertWord(m_Word, m_nCharset, &m_WordProps, false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700286}
287
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700288void CFXEU_InsertWord::Undo() {
Lei Zhangf5a06672017-08-12 08:01:37 -0700289 m_pEdit->SelectNone();
290 m_pEdit->SetCaret(m_wpNew);
291 m_pEdit->Backspace(false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700292}
293
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400294CFXEU_InsertReturn::CFXEU_InsertReturn(CPWL_EditImpl* pEdit,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700295 const CPVT_WordPlace& wpOldPlace,
296 const CPVT_WordPlace& wpNewPlace,
297 const CPVT_SecProps* pSecProps,
298 const CPVT_WordProps* pWordProps)
299 : m_pEdit(pEdit),
300 m_wpOld(wpOldPlace),
301 m_wpNew(wpNewPlace),
302 m_SecProps(),
303 m_WordProps() {
Lei Zhangf5a06672017-08-12 08:01:37 -0700304 ASSERT(m_pEdit);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305 if (pSecProps)
306 m_SecProps = *pSecProps;
307 if (pWordProps)
308 m_WordProps = *pWordProps;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700309}
310
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700311CFXEU_InsertReturn::~CFXEU_InsertReturn() {}
312
313void CFXEU_InsertReturn::Redo() {
Lei Zhangf5a06672017-08-12 08:01:37 -0700314 m_pEdit->SelectNone();
315 m_pEdit->SetCaret(m_wpOld);
316 m_pEdit->InsertReturn(&m_SecProps, &m_WordProps, false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700317}
318
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319void CFXEU_InsertReturn::Undo() {
Lei Zhangf5a06672017-08-12 08:01:37 -0700320 m_pEdit->SelectNone();
321 m_pEdit->SetCaret(m_wpNew);
322 m_pEdit->Backspace(false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700323}
324
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400325CFXEU_Backspace::CFXEU_Backspace(CPWL_EditImpl* pEdit,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700326 const CPVT_WordPlace& wpOldPlace,
327 const CPVT_WordPlace& wpNewPlace,
Tom Sepez62a70f92016-03-21 15:00:20 -0700328 uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700329 int32_t charset,
330 const CPVT_SecProps& SecProps,
331 const CPVT_WordProps& WordProps)
332 : m_pEdit(pEdit),
333 m_wpOld(wpOldPlace),
334 m_wpNew(wpNewPlace),
335 m_Word(word),
336 m_nCharset(charset),
337 m_SecProps(SecProps),
Lei Zhangf5a06672017-08-12 08:01:37 -0700338 m_WordProps(WordProps) {
339 ASSERT(m_pEdit);
340}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700341
342CFXEU_Backspace::~CFXEU_Backspace() {}
343
344void CFXEU_Backspace::Redo() {
Tom Sepez52f69b32017-03-21 13:42:38 -0700345 m_pEdit->SelectNone();
346 m_pEdit->SetCaret(m_wpOld);
347 m_pEdit->Backspace(false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700348}
349
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700350void CFXEU_Backspace::Undo() {
Tom Sepez52f69b32017-03-21 13:42:38 -0700351 m_pEdit->SelectNone();
352 m_pEdit->SetCaret(m_wpNew);
353 if (m_wpNew.nSecIndex != m_wpOld.nSecIndex)
354 m_pEdit->InsertReturn(&m_SecProps, &m_WordProps, false, true);
355 else
356 m_pEdit->InsertWord(m_Word, m_nCharset, &m_WordProps, false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700357}
358
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400359CFXEU_Delete::CFXEU_Delete(CPWL_EditImpl* pEdit,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700360 const CPVT_WordPlace& wpOldPlace,
361 const CPVT_WordPlace& wpNewPlace,
Tom Sepez62a70f92016-03-21 15:00:20 -0700362 uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700363 int32_t charset,
364 const CPVT_SecProps& SecProps,
365 const CPVT_WordProps& WordProps,
tsepez4cf55152016-11-02 14:37:54 -0700366 bool bSecEnd)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700367 : m_pEdit(pEdit),
368 m_wpOld(wpOldPlace),
369 m_wpNew(wpNewPlace),
370 m_Word(word),
371 m_nCharset(charset),
372 m_SecProps(SecProps),
373 m_WordProps(WordProps),
Lei Zhangf5a06672017-08-12 08:01:37 -0700374 m_bSecEnd(bSecEnd) {
375 ASSERT(m_pEdit);
376}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700377
378CFXEU_Delete::~CFXEU_Delete() {}
379
380void CFXEU_Delete::Redo() {
Lei Zhangf5a06672017-08-12 08:01:37 -0700381 m_pEdit->SelectNone();
382 m_pEdit->SetCaret(m_wpOld);
383 m_pEdit->Delete(false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700384}
385
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700386void CFXEU_Delete::Undo() {
Lei Zhangf5a06672017-08-12 08:01:37 -0700387 m_pEdit->SelectNone();
388 m_pEdit->SetCaret(m_wpNew);
389 if (m_bSecEnd)
390 m_pEdit->InsertReturn(&m_SecProps, &m_WordProps, false, true);
391 else
392 m_pEdit->InsertWord(m_Word, m_nCharset, &m_WordProps, false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700393}
394
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400395CFXEU_Clear::CFXEU_Clear(CPWL_EditImpl* pEdit,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396 const CPVT_WordRange& wrSel,
Ryan Harrison275e2602017-09-18 14:23:18 -0400397 const WideString& swText)
Lei Zhangf5a06672017-08-12 08:01:37 -0700398 : m_pEdit(pEdit), m_wrSel(wrSel), m_swText(swText) {
399 ASSERT(m_pEdit);
400}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700401
402CFXEU_Clear::~CFXEU_Clear() {}
403
404void CFXEU_Clear::Redo() {
Lei Zhangf5a06672017-08-12 08:01:37 -0700405 m_pEdit->SelectNone();
406 m_pEdit->SetSelection(m_wrSel.BeginPos, m_wrSel.EndPos);
407 m_pEdit->Clear(false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700408}
409
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700410void CFXEU_Clear::Undo() {
Lei Zhangf5a06672017-08-12 08:01:37 -0700411 m_pEdit->SelectNone();
412 m_pEdit->SetCaret(m_wrSel.BeginPos);
413 m_pEdit->InsertText(m_swText, FX_CHARSET_Default, false, true);
414 m_pEdit->SetSelection(m_wrSel.BeginPos, m_wrSel.EndPos);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700415}
416
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400417CFXEU_InsertText::CFXEU_InsertText(CPWL_EditImpl* pEdit,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700418 const CPVT_WordPlace& wpOldPlace,
419 const CPVT_WordPlace& wpNewPlace,
Ryan Harrison275e2602017-09-18 14:23:18 -0400420 const WideString& swText,
dsinclairefd5a992016-07-18 10:04:07 -0700421 int32_t charset)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700422 : m_pEdit(pEdit),
423 m_wpOld(wpOldPlace),
424 m_wpNew(wpNewPlace),
425 m_swText(swText),
Lei Zhangf5a06672017-08-12 08:01:37 -0700426 m_nCharset(charset) {
427 ASSERT(m_pEdit);
428}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700429
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700430CFXEU_InsertText::~CFXEU_InsertText() {}
431
432void CFXEU_InsertText::Redo() {
Lei Zhangf5a06672017-08-12 08:01:37 -0700433 m_pEdit->SelectNone();
434 m_pEdit->SetCaret(m_wpOld);
435 m_pEdit->InsertText(m_swText, m_nCharset, false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700436}
437
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700438void CFXEU_InsertText::Undo() {
Lei Zhangf5a06672017-08-12 08:01:37 -0700439 m_pEdit->SelectNone();
440 m_pEdit->SetSelection(m_wpOld, m_wpNew);
441 m_pEdit->Clear(false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700442}
443
dsinclaire35af1e2016-07-13 11:26:20 -0700444// static
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400445void CPWL_EditImpl::DrawEdit(CFX_RenderDevice* pDevice,
Lei Zhangeb14e042017-08-15 13:56:43 -0700446 const CFX_Matrix& mtUser2Device,
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400447 CPWL_EditImpl* pEdit,
448 FX_COLORREF crTextFill,
449 const CFX_FloatRect& rcClip,
450 const CFX_PointF& ptOffset,
451 const CPVT_WordRange* pRange,
452 CFX_SystemHandler* pSystemHandler,
453 CFFL_FormFiller* pFFLData) {
dsinclaire35af1e2016-07-13 11:26:20 -0700454 const bool bContinuous =
455 pEdit->GetCharArray() == 0 && pEdit->GetCharSpace() <= 0.0f;
456 uint16_t SubWord = pEdit->GetPasswordChar();
Dan Sinclair05df0752017-03-14 14:43:42 -0400457 float fFontSize = pEdit->GetFontSize();
dsinclaire35af1e2016-07-13 11:26:20 -0700458 CPVT_WordRange wrSelect = pEdit->GetSelectWordRange();
459 int32_t nHorzScale = pEdit->GetHorzScale();
460
461 FX_COLORREF crCurFill = crTextFill;
462 FX_COLORREF crOldFill = crCurFill;
463
tsepez4cf55152016-11-02 14:37:54 -0700464 bool bSelect = false;
Lei Zhangeb14e042017-08-15 13:56:43 -0700465 static const FX_COLORREF crWhite = ArgbEncode(255, 255, 255, 255);
466 static const FX_COLORREF crSelBK = ArgbEncode(255, 0, 51, 113);
dsinclaire35af1e2016-07-13 11:26:20 -0700467
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400468 std::ostringstream sTextBuf;
dsinclaire35af1e2016-07-13 11:26:20 -0700469 int32_t nFontIndex = -1;
Dan Sinclairf528eee2017-02-14 11:52:07 -0500470 CFX_PointF ptBT;
Tom Sepez1629f602017-04-21 14:11:26 -0700471 CFX_RenderDevice::StateRestorer restorer(pDevice);
Jane Liu878b27d2017-08-22 10:50:06 -0400472 if (!rcClip.IsEmpty())
473 pDevice->SetClip_Rect(mtUser2Device.TransformRect(rcClip).ToFxRect());
dsinclaire35af1e2016-07-13 11:26:20 -0700474
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400475 CPWL_EditImpl_Iterator* pIterator = pEdit->GetIterator();
Lei Zhangeb14e042017-08-15 13:56:43 -0700476 IPVT_FontMap* pFontMap = pEdit->GetFontMap();
477 if (!pFontMap)
478 return;
dsinclaire35af1e2016-07-13 11:26:20 -0700479
Lei Zhangeb14e042017-08-15 13:56:43 -0700480 if (pRange)
481 pIterator->SetAt(pRange->BeginPos);
482 else
483 pIterator->SetAt(0);
dsinclaire35af1e2016-07-13 11:26:20 -0700484
Lei Zhangeb14e042017-08-15 13:56:43 -0700485 CPVT_WordPlace oldplace;
486 while (pIterator->NextWord()) {
487 CPVT_WordPlace place = pIterator->GetAt();
488 if (pRange && place > pRange->EndPos)
489 break;
dsinclaire35af1e2016-07-13 11:26:20 -0700490
Lei Zhangeb14e042017-08-15 13:56:43 -0700491 if (!wrSelect.IsEmpty()) {
492 bSelect = place > wrSelect.BeginPos && place <= wrSelect.EndPos;
493 crCurFill = bSelect ? crWhite : crTextFill;
494 }
495 if (pSystemHandler && pSystemHandler->IsSelectionImplemented()) {
496 crCurFill = crTextFill;
497 crOldFill = crCurFill;
498 }
499 CPVT_Word word;
500 if (pIterator->GetWord(word)) {
501 if (bSelect) {
502 CPVT_Line line;
503 pIterator->GetLine(line);
dsinclaire35af1e2016-07-13 11:26:20 -0700504
Lei Zhangeb14e042017-08-15 13:56:43 -0700505 if (pSystemHandler && pSystemHandler->IsSelectionImplemented()) {
506 CFX_FloatRect rc(word.ptWord.x, line.ptLine.y + line.fLineDescent,
507 word.ptWord.x + word.fWidth,
508 line.ptLine.y + line.fLineAscent);
509 rc.Intersect(rcClip);
510 pSystemHandler->OutputSelectedRect(pFFLData, rc);
dsinclaire35af1e2016-07-13 11:26:20 -0700511 } else {
Lei Zhangeb14e042017-08-15 13:56:43 -0700512 CFX_PathData pathSelBK;
513 pathSelBK.AppendRect(word.ptWord.x, line.ptLine.y + line.fLineDescent,
514 word.ptWord.x + word.fWidth,
515 line.ptLine.y + line.fLineAscent);
dsinclaire35af1e2016-07-13 11:26:20 -0700516
Lei Zhangeb14e042017-08-15 13:56:43 -0700517 pDevice->DrawPath(&pathSelBK, &mtUser2Device, nullptr, crSelBK, 0,
518 FXFILL_WINDING);
519 }
520 }
521
522 if (bContinuous) {
523 if (place.LineCmp(oldplace) != 0 || word.nFontIndex != nFontIndex ||
524 crOldFill != crCurFill) {
525 if (sTextBuf.tellp() > 0) {
526 DrawTextString(
527 pDevice, CFX_PointF(ptBT.x + ptOffset.x, ptBT.y + ptOffset.y),
528 pFontMap->GetPDFFont(nFontIndex), fFontSize, mtUser2Device,
Ryan Harrison275e2602017-09-18 14:23:18 -0400529 ByteString(sTextBuf), crOldFill, nHorzScale);
Lei Zhangeb14e042017-08-15 13:56:43 -0700530
531 sTextBuf.str("");
532 }
533 nFontIndex = word.nFontIndex;
534 ptBT = word.ptWord;
535 crOldFill = crCurFill;
536 }
537
538 sTextBuf << pEdit->GetPDFWordString(word.nFontIndex, word.Word,
539 SubWord);
540 } else {
541 DrawTextString(
542 pDevice,
543 CFX_PointF(word.ptWord.x + ptOffset.x, word.ptWord.y + ptOffset.y),
544 pFontMap->GetPDFFont(word.nFontIndex), fFontSize, mtUser2Device,
545 pEdit->GetPDFWordString(word.nFontIndex, word.Word, SubWord),
546 crCurFill, nHorzScale);
547 }
548 oldplace = place;
dsinclaire35af1e2016-07-13 11:26:20 -0700549 }
550 }
Lei Zhangeb14e042017-08-15 13:56:43 -0700551
552 if (sTextBuf.tellp() > 0) {
553 DrawTextString(pDevice,
554 CFX_PointF(ptBT.x + ptOffset.x, ptBT.y + ptOffset.y),
555 pFontMap->GetPDFFont(nFontIndex), fFontSize, mtUser2Device,
Ryan Harrison275e2602017-09-18 14:23:18 -0400556 ByteString(sTextBuf), crOldFill, nHorzScale);
Lei Zhangeb14e042017-08-15 13:56:43 -0700557 }
dsinclaire35af1e2016-07-13 11:26:20 -0700558}
559
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400560CPWL_EditImpl::CPWL_EditImpl()
Lei Zhang5688d622017-08-12 07:04:19 -0700561 : m_pVT(pdfium::MakeUnique<CPDF_VariableText>()),
tsepez4cf55152016-11-02 14:37:54 -0700562 m_bEnableScroll(false),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700563 m_nAlignment(0),
tsepez4cf55152016-11-02 14:37:54 -0700564 m_bNotifyFlag(false),
565 m_bEnableOverflow(false),
566 m_bEnableRefresh(true),
Lei Zhang5688d622017-08-12 07:04:19 -0700567 m_bEnableUndo(true) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700568
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400569CPWL_EditImpl::~CPWL_EditImpl() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700570
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400571void CPWL_EditImpl::Initialize() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700572 m_pVT->Initialize();
573 SetCaret(m_pVT->GetBeginWordPlace());
574 SetCaretOrigin();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700575}
576
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400577void CPWL_EditImpl::SetFontMap(IPVT_FontMap* pFontMap) {
578 m_pVTProvider = pdfium::MakeUnique<CPWL_EditImpl_Provider>(pFontMap);
thestig821d59e2016-05-11 12:59:22 -0700579 m_pVT->SetProvider(m_pVTProvider.get());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700580}
581
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400582void CPWL_EditImpl::SetNotify(CPWL_EditCtrl* pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700583 m_pNotify = pNotify;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700584}
585
Lei Zhang5688d622017-08-12 07:04:19 -0700586void CPWL_EditImpl::SetOperationNotify(CPWL_Edit* pOperationNotify) {
587 m_pOperationNotify = pOperationNotify;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700588}
589
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400590CPWL_EditImpl_Iterator* CPWL_EditImpl::GetIterator() {
tsepez36eb4bd2016-10-03 15:24:27 -0700591 if (!m_pIterator) {
592 m_pIterator =
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400593 pdfium::MakeUnique<CPWL_EditImpl_Iterator>(this, m_pVT->GetIterator());
tsepez36eb4bd2016-10-03 15:24:27 -0700594 }
thestig821d59e2016-05-11 12:59:22 -0700595 return m_pIterator.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700596}
597
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400598IPVT_FontMap* CPWL_EditImpl::GetFontMap() {
thestig821d59e2016-05-11 12:59:22 -0700599 return m_pVTProvider ? m_pVTProvider->GetFontMap() : nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700600}
601
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400602void CPWL_EditImpl::SetPlateRect(const CFX_FloatRect& rect) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700603 m_pVT->SetPlateRect(rect);
Dan Sinclairf528eee2017-02-14 11:52:07 -0500604 m_ptScrollPos = CFX_PointF(rect.left, rect.top);
dsinclairefd5a992016-07-18 10:04:07 -0700605 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700606}
607
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400608void CPWL_EditImpl::SetAlignmentH(int32_t nFormat, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700609 m_pVT->SetAlignment(nFormat);
610 if (bPaint)
611 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700612}
613
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400614void CPWL_EditImpl::SetAlignmentV(int32_t nFormat, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700615 m_nAlignment = nFormat;
616 if (bPaint)
617 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700618}
619
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400620void CPWL_EditImpl::SetPasswordChar(uint16_t wSubWord, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700621 m_pVT->SetPasswordChar(wSubWord);
622 if (bPaint)
623 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700624}
625
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400626void CPWL_EditImpl::SetLimitChar(int32_t nLimitChar) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700627 m_pVT->SetLimitChar(nLimitChar);
dsinclairefd5a992016-07-18 10:04:07 -0700628 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700629}
630
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400631void CPWL_EditImpl::SetCharArray(int32_t nCharArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700632 m_pVT->SetCharArray(nCharArray);
dsinclairefd5a992016-07-18 10:04:07 -0700633 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700634}
635
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400636void CPWL_EditImpl::SetCharSpace(float fCharSpace) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700637 m_pVT->SetCharSpace(fCharSpace);
dsinclairefd5a992016-07-18 10:04:07 -0700638 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700639}
640
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400641void CPWL_EditImpl::SetMultiLine(bool bMultiLine, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700642 m_pVT->SetMultiLine(bMultiLine);
643 if (bPaint)
644 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700645}
646
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400647void CPWL_EditImpl::SetAutoReturn(bool bAuto, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700648 m_pVT->SetAutoReturn(bAuto);
649 if (bPaint)
650 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700651}
652
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400653void CPWL_EditImpl::SetAutoFontSize(bool bAuto, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700654 m_pVT->SetAutoFontSize(bAuto);
655 if (bPaint)
656 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700657}
658
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400659void CPWL_EditImpl::SetFontSize(float fFontSize) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700660 m_pVT->SetFontSize(fFontSize);
dsinclairefd5a992016-07-18 10:04:07 -0700661 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700662}
663
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400664void CPWL_EditImpl::SetAutoScroll(bool bAuto, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700665 m_bEnableScroll = bAuto;
666 if (bPaint)
667 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700668}
669
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400670void CPWL_EditImpl::SetTextOverflow(bool bAllowed, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700671 m_bEnableOverflow = bAllowed;
672 if (bPaint)
673 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700674}
675
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400676void CPWL_EditImpl::SetSelection(int32_t nStartChar, int32_t nEndChar) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700677 if (m_pVT->IsValid()) {
678 if (nStartChar == 0 && nEndChar < 0) {
679 SelectAll();
680 } else if (nStartChar < 0) {
681 SelectNone();
682 } else {
683 if (nStartChar < nEndChar) {
Diana Gage4d02e902017-07-20 17:20:31 -0700684 SetSelection(m_pVT->WordIndexToWordPlace(nStartChar),
685 m_pVT->WordIndexToWordPlace(nEndChar));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700686 } else {
Diana Gage4d02e902017-07-20 17:20:31 -0700687 SetSelection(m_pVT->WordIndexToWordPlace(nEndChar),
688 m_pVT->WordIndexToWordPlace(nStartChar));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700689 }
690 }
691 }
692}
693
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400694void CPWL_EditImpl::SetSelection(const CPVT_WordPlace& begin,
695 const CPVT_WordPlace& end) {
Tom Sepez52f69b32017-03-21 13:42:38 -0700696 if (!m_pVT->IsValid())
697 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700698
Tom Sepez52f69b32017-03-21 13:42:38 -0700699 SelectNone();
700 m_SelState.Set(begin, end);
701 SetCaret(m_SelState.EndPos);
702 ScrollToCaret();
703 if (!m_SelState.IsEmpty())
704 Refresh();
705 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700706}
707
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400708void CPWL_EditImpl::GetSelection(int32_t& nStartChar, int32_t& nEndChar) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700709 nStartChar = -1;
710 nEndChar = -1;
Tom Sepez52f69b32017-03-21 13:42:38 -0700711 if (!m_pVT->IsValid())
712 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700713
Tom Sepez52f69b32017-03-21 13:42:38 -0700714 if (m_SelState.IsEmpty()) {
715 nStartChar = m_pVT->WordPlaceToWordIndex(m_wpCaret);
716 nEndChar = m_pVT->WordPlaceToWordIndex(m_wpCaret);
717 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700718 }
Tom Sepez52f69b32017-03-21 13:42:38 -0700719 if (m_SelState.BeginPos < m_SelState.EndPos) {
720 nStartChar = m_pVT->WordPlaceToWordIndex(m_SelState.BeginPos);
721 nEndChar = m_pVT->WordPlaceToWordIndex(m_SelState.EndPos);
722 return;
723 }
724 nStartChar = m_pVT->WordPlaceToWordIndex(m_SelState.EndPos);
725 nEndChar = m_pVT->WordPlaceToWordIndex(m_SelState.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700726}
727
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400728int32_t CPWL_EditImpl::GetCaret() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700729 if (m_pVT->IsValid())
730 return m_pVT->WordPlaceToWordIndex(m_wpCaret);
731
732 return -1;
733}
734
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400735CPVT_WordPlace CPWL_EditImpl::GetCaretWordPlace() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700736 return m_wpCaret;
737}
738
Ryan Harrison275e2602017-09-18 14:23:18 -0400739WideString CPWL_EditImpl::GetText() const {
740 WideString swRet;
thestig821d59e2016-05-11 12:59:22 -0700741 if (!m_pVT->IsValid())
742 return swRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700743
thestig821d59e2016-05-11 12:59:22 -0700744 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
745 pIterator->SetAt(0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700746
thestig821d59e2016-05-11 12:59:22 -0700747 CPVT_Word wordinfo;
748 CPVT_WordPlace oldplace = pIterator->GetAt();
749 while (pIterator->NextWord()) {
750 CPVT_WordPlace place = pIterator->GetAt();
thestig821d59e2016-05-11 12:59:22 -0700751 if (pIterator->GetWord(wordinfo))
752 swRet += wordinfo.Word;
Tom Sepez52f69b32017-03-21 13:42:38 -0700753 if (oldplace.nSecIndex != place.nSecIndex)
thestig821d59e2016-05-11 12:59:22 -0700754 swRet += L"\r\n";
thestig821d59e2016-05-11 12:59:22 -0700755 oldplace = place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700756 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700757 return swRet;
758}
759
Ryan Harrison275e2602017-09-18 14:23:18 -0400760WideString CPWL_EditImpl::GetRangeText(const CPVT_WordRange& range) const {
761 WideString swRet;
thestig821d59e2016-05-11 12:59:22 -0700762 if (!m_pVT->IsValid())
763 return swRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700764
thestig821d59e2016-05-11 12:59:22 -0700765 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
766 CPVT_WordRange wrTemp = range;
767 m_pVT->UpdateWordPlace(wrTemp.BeginPos);
768 m_pVT->UpdateWordPlace(wrTemp.EndPos);
769 pIterator->SetAt(wrTemp.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700770
thestig821d59e2016-05-11 12:59:22 -0700771 CPVT_Word wordinfo;
772 CPVT_WordPlace oldplace = wrTemp.BeginPos;
773 while (pIterator->NextWord()) {
774 CPVT_WordPlace place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -0700775 if (place > wrTemp.EndPos)
thestig821d59e2016-05-11 12:59:22 -0700776 break;
thestig821d59e2016-05-11 12:59:22 -0700777 if (pIterator->GetWord(wordinfo))
778 swRet += wordinfo.Word;
Tom Sepez52f69b32017-03-21 13:42:38 -0700779 if (oldplace.nSecIndex != place.nSecIndex)
thestig821d59e2016-05-11 12:59:22 -0700780 swRet += L"\r\n";
thestig821d59e2016-05-11 12:59:22 -0700781 oldplace = place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700782 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700783 return swRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700784}
785
Ryan Harrison275e2602017-09-18 14:23:18 -0400786WideString CPWL_EditImpl::GetSelectedText() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700787 return GetRangeText(m_SelState.ConvertToWordRange());
788}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700789
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400790int32_t CPWL_EditImpl::GetTotalLines() const {
thestig821d59e2016-05-11 12:59:22 -0700791 int32_t nLines = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700792
thestig821d59e2016-05-11 12:59:22 -0700793 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
794 pIterator->SetAt(0);
795 while (pIterator->NextLine())
796 ++nLines;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700797
thestig821d59e2016-05-11 12:59:22 -0700798 return nLines;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700799}
800
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400801CPVT_WordRange CPWL_EditImpl::GetSelectWordRange() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700802 return m_SelState.ConvertToWordRange();
803}
804
Ryan Harrison275e2602017-09-18 14:23:18 -0400805void CPWL_EditImpl::SetText(const WideString& sText) {
dsinclairefd5a992016-07-18 10:04:07 -0700806 Empty();
Dan Sinclairf51a02a2017-04-19 12:46:53 -0400807 DoInsertText(CPVT_WordPlace(0, 0, -1), sText, FX_CHARSET_Default);
dsinclairefd5a992016-07-18 10:04:07 -0700808 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700809}
810
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400811bool CPWL_EditImpl::InsertWord(uint16_t word, int32_t charset) {
tsepez4cf55152016-11-02 14:37:54 -0700812 return InsertWord(word, charset, nullptr, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700813}
814
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400815bool CPWL_EditImpl::InsertReturn() {
tsepez4cf55152016-11-02 14:37:54 -0700816 return InsertReturn(nullptr, nullptr, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700817}
818
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400819bool CPWL_EditImpl::Backspace() {
tsepez4cf55152016-11-02 14:37:54 -0700820 return Backspace(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700821}
822
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400823bool CPWL_EditImpl::Delete() {
tsepez4cf55152016-11-02 14:37:54 -0700824 return Delete(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700825}
826
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400827bool CPWL_EditImpl::ClearSelection() {
tsepez4cf55152016-11-02 14:37:54 -0700828 return Clear(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700829}
830
Ryan Harrison275e2602017-09-18 14:23:18 -0400831bool CPWL_EditImpl::InsertText(const WideString& sText, int32_t charset) {
tsepez4cf55152016-11-02 14:37:54 -0700832 return InsertText(sText, charset, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700833}
834
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400835float CPWL_EditImpl::GetFontSize() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700836 return m_pVT->GetFontSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700837}
838
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400839uint16_t CPWL_EditImpl::GetPasswordChar() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700840 return m_pVT->GetPasswordChar();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700841}
842
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400843int32_t CPWL_EditImpl::GetCharArray() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700844 return m_pVT->GetCharArray();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700845}
846
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400847CFX_FloatRect CPWL_EditImpl::GetContentRect() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700848 return VTToEdit(m_pVT->GetContentRect());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700849}
850
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400851int32_t CPWL_EditImpl::GetHorzScale() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700852 return m_pVT->GetHorzScale();
853}
854
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400855float CPWL_EditImpl::GetCharSpace() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700856 return m_pVT->GetCharSpace();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700857}
858
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400859CPVT_WordRange CPWL_EditImpl::GetWholeWordRange() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700860 if (m_pVT->IsValid())
861 return CPVT_WordRange(m_pVT->GetBeginWordPlace(), m_pVT->GetEndWordPlace());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700862
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700863 return CPVT_WordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700864}
865
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400866CPVT_WordRange CPWL_EditImpl::GetVisibleWordRange() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700867 if (m_bEnableOverflow)
868 return GetWholeWordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700869
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700870 if (m_pVT->IsValid()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800871 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700872
Dan Sinclairf528eee2017-02-14 11:52:07 -0500873 CPVT_WordPlace place1 =
874 m_pVT->SearchWordPlace(EditToVT(CFX_PointF(rcPlate.left, rcPlate.top)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700875 CPVT_WordPlace place2 = m_pVT->SearchWordPlace(
Dan Sinclairf528eee2017-02-14 11:52:07 -0500876 EditToVT(CFX_PointF(rcPlate.right, rcPlate.bottom)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700877
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700878 return CPVT_WordRange(place1, place2);
879 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700880
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700881 return CPVT_WordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700882}
883
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400884CPVT_WordPlace CPWL_EditImpl::SearchWordPlace(const CFX_PointF& point) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700885 if (m_pVT->IsValid()) {
886 return m_pVT->SearchWordPlace(EditToVT(point));
887 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700888
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700889 return CPVT_WordPlace();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700890}
891
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400892void CPWL_EditImpl::Paint() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700893 if (m_pVT->IsValid()) {
894 RearrangeAll();
895 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -0700896 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700897 SetCaretOrigin();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700898 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700899 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700900}
901
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400902void CPWL_EditImpl::RearrangeAll() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700903 if (m_pVT->IsValid()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700904 m_pVT->UpdateWordPlace(m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700905 m_pVT->RearrangeAll();
906 m_pVT->UpdateWordPlace(m_wpCaret);
907 SetScrollInfo();
908 SetContentChanged();
909 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700910}
911
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400912void CPWL_EditImpl::RearrangePart(const CPVT_WordRange& range) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700913 if (m_pVT->IsValid()) {
914 m_pVT->UpdateWordPlace(m_wpCaret);
915 m_pVT->RearrangePart(range);
916 m_pVT->UpdateWordPlace(m_wpCaret);
917 SetScrollInfo();
918 SetContentChanged();
919 }
920}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700921
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400922void CPWL_EditImpl::SetContentChanged() {
dsinclaira2919b32016-07-13 10:55:48 -0700923 if (m_pNotify) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800924 CFX_FloatRect rcContent = m_pVT->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700925 if (rcContent.Width() != m_rcOldContent.Width() ||
926 rcContent.Height() != m_rcOldContent.Height()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700927 m_rcOldContent = rcContent;
928 }
929 }
930}
931
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400932void CPWL_EditImpl::SelectAll() {
Tom Sepez52f69b32017-03-21 13:42:38 -0700933 if (!m_pVT->IsValid())
934 return;
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400935 m_SelState = CPWL_EditImpl_Select(GetWholeWordRange());
Tom Sepez52f69b32017-03-21 13:42:38 -0700936 SetCaret(m_SelState.EndPos);
937 ScrollToCaret();
938 Refresh();
939 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700940}
941
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400942void CPWL_EditImpl::SelectNone() {
Tom Sepez52f69b32017-03-21 13:42:38 -0700943 if (!m_pVT->IsValid() || m_SelState.IsEmpty())
944 return;
945
946 m_SelState.Reset();
947 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700948}
949
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400950bool CPWL_EditImpl::IsSelected() const {
Tom Sepez52f69b32017-03-21 13:42:38 -0700951 return !m_SelState.IsEmpty();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700952}
953
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400954CFX_PointF CPWL_EditImpl::VTToEdit(const CFX_PointF& point) const {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800955 CFX_FloatRect rcContent = m_pVT->GetContentRect();
956 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700957
Dan Sinclair05df0752017-03-14 14:43:42 -0400958 float fPadding = 0.0f;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700959
960 switch (m_nAlignment) {
961 case 0:
962 fPadding = 0.0f;
963 break;
964 case 1:
965 fPadding = (rcPlate.Height() - rcContent.Height()) * 0.5f;
966 break;
967 case 2:
968 fPadding = rcPlate.Height() - rcContent.Height();
969 break;
970 }
971
Dan Sinclairf528eee2017-02-14 11:52:07 -0500972 return CFX_PointF(point.x - (m_ptScrollPos.x - rcPlate.left),
973 point.y - (m_ptScrollPos.y + fPadding - rcPlate.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700974}
975
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400976CFX_PointF CPWL_EditImpl::EditToVT(const CFX_PointF& point) const {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800977 CFX_FloatRect rcContent = m_pVT->GetContentRect();
978 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700979
Dan Sinclair05df0752017-03-14 14:43:42 -0400980 float fPadding = 0.0f;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700981
982 switch (m_nAlignment) {
983 case 0:
984 fPadding = 0.0f;
985 break;
986 case 1:
987 fPadding = (rcPlate.Height() - rcContent.Height()) * 0.5f;
988 break;
989 case 2:
990 fPadding = rcPlate.Height() - rcContent.Height();
991 break;
992 }
993
Dan Sinclairf528eee2017-02-14 11:52:07 -0500994 return CFX_PointF(point.x + (m_ptScrollPos.x - rcPlate.left),
995 point.y + (m_ptScrollPos.y + fPadding - rcPlate.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700996}
997
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400998CFX_FloatRect CPWL_EditImpl::VTToEdit(const CFX_FloatRect& rect) const {
Dan Sinclairf528eee2017-02-14 11:52:07 -0500999 CFX_PointF ptLeftBottom = VTToEdit(CFX_PointF(rect.left, rect.bottom));
1000 CFX_PointF ptRightTop = VTToEdit(CFX_PointF(rect.right, rect.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001001
Tom Sepez281a9ea2016-02-26 14:24:28 -08001002 return CFX_FloatRect(ptLeftBottom.x, ptLeftBottom.y, ptRightTop.x,
1003 ptRightTop.y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001004}
1005
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001006void CPWL_EditImpl::SetScrollInfo() {
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001007 if (!m_pNotify)
1008 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001009
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001010 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
1011 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1012 if (m_bNotifyFlag)
1013 return;
1014
Dan Sinclairb8966992017-09-21 14:51:57 -04001015 AutoRestorer<bool> restorer(&m_bNotifyFlag);
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001016 m_bNotifyFlag = true;
1017
1018 PWL_SCROLL_INFO Info;
1019 Info.fPlateWidth = rcPlate.top - rcPlate.bottom;
1020 Info.fContentMin = rcContent.bottom;
1021 Info.fContentMax = rcContent.top;
1022 Info.fSmallStep = rcPlate.Height() / 3;
1023 Info.fBigStep = rcPlate.Height();
1024 m_pNotify->SetScrollInfo(Info);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001025}
1026
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001027void CPWL_EditImpl::SetScrollPosX(float fx) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001028 if (!m_bEnableScroll)
1029 return;
1030
1031 if (m_pVT->IsValid()) {
dsinclair448c4332016-08-02 12:07:35 -07001032 if (!IsFloatEqual(m_ptScrollPos.x, fx)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001033 m_ptScrollPos.x = fx;
dsinclairefd5a992016-07-18 10:04:07 -07001034 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001035 }
1036 }
1037}
1038
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001039void CPWL_EditImpl::SetScrollPosY(float fy) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001040 if (!m_bEnableScroll)
1041 return;
1042
1043 if (m_pVT->IsValid()) {
dsinclair448c4332016-08-02 12:07:35 -07001044 if (!IsFloatEqual(m_ptScrollPos.y, fy)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001045 m_ptScrollPos.y = fy;
dsinclairefd5a992016-07-18 10:04:07 -07001046 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001047
dsinclaira2919b32016-07-13 10:55:48 -07001048 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001049 if (!m_bNotifyFlag) {
Dan Sinclairb8966992017-09-21 14:51:57 -04001050 AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001051 m_bNotifyFlag = true;
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001052 m_pNotify->SetScrollPosition(fy);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001053 }
1054 }
1055 }
1056 }
1057}
1058
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001059void CPWL_EditImpl::SetScrollPos(const CFX_PointF& point) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001060 SetScrollPosX(point.x);
1061 SetScrollPosY(point.y);
1062 SetScrollLimit();
1063 SetCaretInfo();
1064}
1065
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001066CFX_PointF CPWL_EditImpl::GetScrollPos() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001067 return m_ptScrollPos;
1068}
1069
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001070void CPWL_EditImpl::SetScrollLimit() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001071 if (m_pVT->IsValid()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001072 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1073 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001074
1075 if (rcPlate.Width() > rcContent.Width()) {
1076 SetScrollPosX(rcPlate.left);
1077 } else {
dsinclair448c4332016-08-02 12:07:35 -07001078 if (IsFloatSmaller(m_ptScrollPos.x, rcContent.left)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001079 SetScrollPosX(rcContent.left);
dsinclair448c4332016-08-02 12:07:35 -07001080 } else if (IsFloatBigger(m_ptScrollPos.x,
1081 rcContent.right - rcPlate.Width())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001082 SetScrollPosX(rcContent.right - rcPlate.Width());
1083 }
1084 }
1085
1086 if (rcPlate.Height() > rcContent.Height()) {
1087 SetScrollPosY(rcPlate.top);
1088 } else {
dsinclair448c4332016-08-02 12:07:35 -07001089 if (IsFloatSmaller(m_ptScrollPos.y,
1090 rcContent.bottom + rcPlate.Height())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001091 SetScrollPosY(rcContent.bottom + rcPlate.Height());
dsinclair448c4332016-08-02 12:07:35 -07001092 } else if (IsFloatBigger(m_ptScrollPos.y, rcContent.top)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001093 SetScrollPosY(rcContent.top);
1094 }
1095 }
1096 }
1097}
1098
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001099void CPWL_EditImpl::ScrollToCaret() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001100 SetScrollLimit();
1101
thestig821d59e2016-05-11 12:59:22 -07001102 if (!m_pVT->IsValid())
1103 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001104
thestig821d59e2016-05-11 12:59:22 -07001105 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1106 pIterator->SetAt(m_wpCaret);
1107
Dan Sinclairf528eee2017-02-14 11:52:07 -05001108 CFX_PointF ptHead;
1109 CFX_PointF ptFoot;
thestig821d59e2016-05-11 12:59:22 -07001110 CPVT_Word word;
1111 CPVT_Line line;
1112 if (pIterator->GetWord(word)) {
1113 ptHead.x = word.ptWord.x + word.fWidth;
1114 ptHead.y = word.ptWord.y + word.fAscent;
1115 ptFoot.x = word.ptWord.x + word.fWidth;
1116 ptFoot.y = word.ptWord.y + word.fDescent;
1117 } else if (pIterator->GetLine(line)) {
1118 ptHead.x = line.ptLine.x;
1119 ptHead.y = line.ptLine.y + line.fLineAscent;
1120 ptFoot.x = line.ptLine.x;
1121 ptFoot.y = line.ptLine.y + line.fLineDescent;
1122 }
1123
Dan Sinclairf528eee2017-02-14 11:52:07 -05001124 CFX_PointF ptHeadEdit = VTToEdit(ptHead);
1125 CFX_PointF ptFootEdit = VTToEdit(ptFoot);
thestig821d59e2016-05-11 12:59:22 -07001126 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
dsinclair448c4332016-08-02 12:07:35 -07001127 if (!IsFloatEqual(rcPlate.left, rcPlate.right)) {
1128 if (IsFloatSmaller(ptHeadEdit.x, rcPlate.left) ||
1129 IsFloatEqual(ptHeadEdit.x, rcPlate.left)) {
thestig821d59e2016-05-11 12:59:22 -07001130 SetScrollPosX(ptHead.x);
dsinclair448c4332016-08-02 12:07:35 -07001131 } else if (IsFloatBigger(ptHeadEdit.x, rcPlate.right)) {
thestig821d59e2016-05-11 12:59:22 -07001132 SetScrollPosX(ptHead.x - rcPlate.Width());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001133 }
thestig821d59e2016-05-11 12:59:22 -07001134 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001135
dsinclair448c4332016-08-02 12:07:35 -07001136 if (!IsFloatEqual(rcPlate.top, rcPlate.bottom)) {
1137 if (IsFloatSmaller(ptFootEdit.y, rcPlate.bottom) ||
1138 IsFloatEqual(ptFootEdit.y, rcPlate.bottom)) {
1139 if (IsFloatSmaller(ptHeadEdit.y, rcPlate.top)) {
thestig821d59e2016-05-11 12:59:22 -07001140 SetScrollPosY(ptFoot.y + rcPlate.Height());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001141 }
dsinclair448c4332016-08-02 12:07:35 -07001142 } else if (IsFloatBigger(ptHeadEdit.y, rcPlate.top)) {
1143 if (IsFloatBigger(ptFootEdit.y, rcPlate.bottom)) {
thestig821d59e2016-05-11 12:59:22 -07001144 SetScrollPosY(ptHead.y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001145 }
1146 }
1147 }
1148}
1149
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001150void CPWL_EditImpl::Refresh() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001151 if (m_bEnableRefresh && m_pVT->IsValid()) {
1152 m_Refresh.BeginRefresh();
1153 RefreshPushLineRects(GetVisibleWordRange());
1154
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001155 m_Refresh.NoAnalyse();
1156 m_ptRefreshScrollPos = m_ptScrollPos;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001157
dsinclaira2919b32016-07-13 10:55:48 -07001158 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001159 if (!m_bNotifyFlag) {
Dan Sinclairb8966992017-09-21 14:51:57 -04001160 AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001161 m_bNotifyFlag = true;
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001162 if (const CPWL_EditImpl_RectArray* pRects =
1163 m_Refresh.GetRefreshRects()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001164 for (int32_t i = 0, sz = pRects->GetSize(); i < sz; i++)
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001165 m_pNotify->InvalidateRect(pRects->GetAt(i));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001166 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001167 }
1168 }
1169
1170 m_Refresh.EndRefresh();
1171 }
1172}
1173
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001174void CPWL_EditImpl::RefreshPushLineRects(const CPVT_WordRange& wr) {
thestig821d59e2016-05-11 12:59:22 -07001175 if (!m_pVT->IsValid())
1176 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001177
thestig821d59e2016-05-11 12:59:22 -07001178 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1179 CPVT_WordPlace wpBegin = wr.BeginPos;
1180 m_pVT->UpdateWordPlace(wpBegin);
1181 CPVT_WordPlace wpEnd = wr.EndPos;
1182 m_pVT->UpdateWordPlace(wpEnd);
1183 pIterator->SetAt(wpBegin);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001184
thestig821d59e2016-05-11 12:59:22 -07001185 CPVT_Line lineinfo;
1186 do {
1187 if (!pIterator->GetLine(lineinfo))
1188 break;
1189 if (lineinfo.lineplace.LineCmp(wpEnd) > 0)
1190 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001191
thestig821d59e2016-05-11 12:59:22 -07001192 CFX_FloatRect rcLine(lineinfo.ptLine.x,
1193 lineinfo.ptLine.y + lineinfo.fLineDescent,
1194 lineinfo.ptLine.x + lineinfo.fLineWidth,
1195 lineinfo.ptLine.y + lineinfo.fLineAscent);
1196
1197 m_Refresh.Push(CPVT_WordRange(lineinfo.lineplace, lineinfo.lineEnd),
1198 VTToEdit(rcLine));
1199 } while (pIterator->NextLine());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001200}
1201
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001202void CPWL_EditImpl::RefreshWordRange(const CPVT_WordRange& wr) {
thestig821d59e2016-05-11 12:59:22 -07001203 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1204 CPVT_WordRange wrTemp = wr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001205
thestig821d59e2016-05-11 12:59:22 -07001206 m_pVT->UpdateWordPlace(wrTemp.BeginPos);
1207 m_pVT->UpdateWordPlace(wrTemp.EndPos);
1208 pIterator->SetAt(wrTemp.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001209
thestig821d59e2016-05-11 12:59:22 -07001210 CPVT_Word wordinfo;
1211 CPVT_Line lineinfo;
1212 CPVT_WordPlace place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001213
thestig821d59e2016-05-11 12:59:22 -07001214 while (pIterator->NextWord()) {
1215 place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -07001216 if (place > wrTemp.EndPos)
thestig821d59e2016-05-11 12:59:22 -07001217 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001218
thestig821d59e2016-05-11 12:59:22 -07001219 pIterator->GetWord(wordinfo);
1220 pIterator->GetLine(lineinfo);
thestig821d59e2016-05-11 12:59:22 -07001221 if (place.LineCmp(wrTemp.BeginPos) == 0 ||
1222 place.LineCmp(wrTemp.EndPos) == 0) {
1223 CFX_FloatRect rcWord(wordinfo.ptWord.x,
1224 lineinfo.ptLine.y + lineinfo.fLineDescent,
1225 wordinfo.ptWord.x + wordinfo.fWidth,
1226 lineinfo.ptLine.y + lineinfo.fLineAscent);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001227
dsinclaira2919b32016-07-13 10:55:48 -07001228 if (m_pNotify) {
thestig821d59e2016-05-11 12:59:22 -07001229 if (!m_bNotifyFlag) {
Dan Sinclairb8966992017-09-21 14:51:57 -04001230 AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001231 m_bNotifyFlag = true;
thestig821d59e2016-05-11 12:59:22 -07001232 CFX_FloatRect rcRefresh = VTToEdit(rcWord);
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001233 m_pNotify->InvalidateRect(&rcRefresh);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001234 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001235 }
thestig821d59e2016-05-11 12:59:22 -07001236 } else {
1237 CFX_FloatRect rcLine(lineinfo.ptLine.x,
1238 lineinfo.ptLine.y + lineinfo.fLineDescent,
1239 lineinfo.ptLine.x + lineinfo.fLineWidth,
1240 lineinfo.ptLine.y + lineinfo.fLineAscent);
1241
dsinclaira2919b32016-07-13 10:55:48 -07001242 if (m_pNotify) {
thestig821d59e2016-05-11 12:59:22 -07001243 if (!m_bNotifyFlag) {
Dan Sinclairb8966992017-09-21 14:51:57 -04001244 AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001245 m_bNotifyFlag = true;
thestig821d59e2016-05-11 12:59:22 -07001246 CFX_FloatRect rcRefresh = VTToEdit(rcLine);
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001247 m_pNotify->InvalidateRect(&rcRefresh);
thestig821d59e2016-05-11 12:59:22 -07001248 }
1249 }
1250
1251 pIterator->NextLine();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001252 }
1253 }
1254}
1255
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001256void CPWL_EditImpl::SetCaret(const CPVT_WordPlace& place) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001257 m_wpOldCaret = m_wpCaret;
1258 m_wpCaret = place;
1259}
1260
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001261void CPWL_EditImpl::SetCaretInfo() {
dsinclaira2919b32016-07-13 10:55:48 -07001262 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001263 if (!m_bNotifyFlag) {
thestig821d59e2016-05-11 12:59:22 -07001264 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1265 pIterator->SetAt(m_wpCaret);
tsepez63f545c2016-09-13 16:08:49 -07001266
Dan Sinclairf528eee2017-02-14 11:52:07 -05001267 CFX_PointF ptHead;
1268 CFX_PointF ptFoot;
thestig821d59e2016-05-11 12:59:22 -07001269 CPVT_Word word;
1270 CPVT_Line line;
1271 if (pIterator->GetWord(word)) {
1272 ptHead.x = word.ptWord.x + word.fWidth;
1273 ptHead.y = word.ptWord.y + word.fAscent;
1274 ptFoot.x = word.ptWord.x + word.fWidth;
1275 ptFoot.y = word.ptWord.y + word.fDescent;
1276 } else if (pIterator->GetLine(line)) {
1277 ptHead.x = line.ptLine.x;
1278 ptHead.y = line.ptLine.y + line.fLineAscent;
1279 ptFoot.x = line.ptLine.x;
1280 ptFoot.y = line.ptLine.y + line.fLineDescent;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001281 }
1282
Dan Sinclairb8966992017-09-21 14:51:57 -04001283 AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001284 m_bNotifyFlag = true;
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001285 m_pNotify->SetCaret(m_SelState.IsEmpty(), VTToEdit(ptHead),
1286 VTToEdit(ptFoot));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001287 }
1288 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001289}
1290
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001291void CPWL_EditImpl::OnMouseDown(const CFX_PointF& point,
1292 bool bShift,
1293 bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001294 if (!m_pVT->IsValid())
1295 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001296
Tom Sepez52f69b32017-03-21 13:42:38 -07001297 SelectNone();
1298 SetCaret(m_pVT->SearchWordPlace(EditToVT(point)));
1299 m_SelState.Set(m_wpCaret, m_wpCaret);
1300 ScrollToCaret();
1301 SetCaretOrigin();
1302 SetCaretInfo();
1303}
1304
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001305void CPWL_EditImpl::OnMouseMove(const CFX_PointF& point,
1306 bool bShift,
1307 bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001308 if (!m_pVT->IsValid())
1309 return;
1310
1311 SetCaret(m_pVT->SearchWordPlace(EditToVT(point)));
1312 if (m_wpCaret == m_wpOldCaret)
1313 return;
1314
1315 m_SelState.SetEndPos(m_wpCaret);
1316 ScrollToCaret();
1317 Refresh();
1318 SetCaretOrigin();
1319 SetCaretInfo();
1320}
1321
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001322void CPWL_EditImpl::OnVK_UP(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001323 if (!m_pVT->IsValid())
1324 return;
1325
1326 SetCaret(m_pVT->GetUpWordPlace(m_wpCaret, m_ptCaret));
1327 if (bShift) {
1328 if (m_SelState.IsEmpty())
1329 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1330 else
1331 m_SelState.SetEndPos(m_wpCaret);
1332
1333 if (m_wpOldCaret != m_wpCaret) {
1334 ScrollToCaret();
1335 Refresh();
1336 SetCaretInfo();
1337 }
1338 } else {
1339 SelectNone();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001340 ScrollToCaret();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001341 SetCaretInfo();
1342 }
1343}
1344
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001345void CPWL_EditImpl::OnVK_DOWN(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001346 if (!m_pVT->IsValid())
1347 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001348
Tom Sepez52f69b32017-03-21 13:42:38 -07001349 SetCaret(m_pVT->GetDownWordPlace(m_wpCaret, m_ptCaret));
1350 if (bShift) {
1351 if (m_SelState.IsEmpty())
1352 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1353 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001354 m_SelState.SetEndPos(m_wpCaret);
1355
Tom Sepez52f69b32017-03-21 13:42:38 -07001356 if (m_wpOldCaret != m_wpCaret) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001357 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001358 Refresh();
Tom Sepez52f69b32017-03-21 13:42:38 -07001359 SetCaretInfo();
1360 }
1361 } else {
1362 SelectNone();
1363 ScrollToCaret();
1364 SetCaretInfo();
1365 }
1366}
1367
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001368void CPWL_EditImpl::OnVK_LEFT(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001369 if (!m_pVT->IsValid())
1370 return;
1371
1372 if (bShift) {
1373 if (m_wpCaret == m_pVT->GetLineBeginPlace(m_wpCaret) &&
1374 m_wpCaret != m_pVT->GetSectionBeginPlace(m_wpCaret)) {
1375 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1376 }
1377 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1378 if (m_SelState.IsEmpty())
1379 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1380 else
1381 m_SelState.SetEndPos(m_wpCaret);
1382
1383 if (m_wpOldCaret != m_wpCaret) {
1384 ScrollToCaret();
1385 Refresh();
1386 SetCaretInfo();
1387 }
1388 } else {
1389 if (!m_SelState.IsEmpty()) {
1390 if (m_SelState.BeginPos < m_SelState.EndPos)
1391 SetCaret(m_SelState.BeginPos);
1392 else
1393 SetCaret(m_SelState.EndPos);
1394
1395 SelectNone();
1396 ScrollToCaret();
1397 SetCaretInfo();
1398 } else {
1399 if (m_wpCaret == m_pVT->GetLineBeginPlace(m_wpCaret) &&
1400 m_wpCaret != m_pVT->GetSectionBeginPlace(m_wpCaret)) {
1401 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1402 }
1403 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1404 ScrollToCaret();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001405 SetCaretOrigin();
1406 SetCaretInfo();
1407 }
1408 }
1409}
1410
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001411void CPWL_EditImpl::OnVK_RIGHT(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001412 if (!m_pVT->IsValid())
1413 return;
1414
1415 if (bShift) {
1416 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1417 if (m_wpCaret == m_pVT->GetLineEndPlace(m_wpCaret) &&
1418 m_wpCaret != m_pVT->GetSectionEndPlace(m_wpCaret))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001419 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1420
Tom Sepez52f69b32017-03-21 13:42:38 -07001421 if (m_SelState.IsEmpty())
1422 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1423 else
1424 m_SelState.SetEndPos(m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001425
Tom Sepez52f69b32017-03-21 13:42:38 -07001426 if (m_wpOldCaret != m_wpCaret) {
1427 ScrollToCaret();
1428 Refresh();
1429 SetCaretInfo();
1430 }
1431 } else {
1432 if (!m_SelState.IsEmpty()) {
1433 if (m_SelState.BeginPos > m_SelState.EndPos)
1434 SetCaret(m_SelState.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001435 else
Tom Sepez52f69b32017-03-21 13:42:38 -07001436 SetCaret(m_SelState.EndPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001437
Tom Sepez52f69b32017-03-21 13:42:38 -07001438 SelectNone();
1439 ScrollToCaret();
1440 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001441 } else {
Tom Sepez52f69b32017-03-21 13:42:38 -07001442 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1443 if (m_wpCaret == m_pVT->GetLineEndPlace(m_wpCaret) &&
1444 m_wpCaret != m_pVT->GetSectionEndPlace(m_wpCaret)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001445 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001446 }
Tom Sepez52f69b32017-03-21 13:42:38 -07001447 ScrollToCaret();
1448 SetCaretOrigin();
1449 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001450 }
1451 }
1452}
1453
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001454void CPWL_EditImpl::OnVK_HOME(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001455 if (!m_pVT->IsValid())
1456 return;
1457
1458 if (bShift) {
1459 if (bCtrl)
1460 SetCaret(m_pVT->GetBeginWordPlace());
1461 else
1462 SetCaret(m_pVT->GetLineBeginPlace(m_wpCaret));
1463
1464 if (m_SelState.IsEmpty())
1465 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1466 else
1467 m_SelState.SetEndPos(m_wpCaret);
1468
1469 ScrollToCaret();
1470 Refresh();
1471 SetCaretInfo();
1472 } else {
1473 if (!m_SelState.IsEmpty()) {
1474 SetCaret(std::min(m_SelState.BeginPos, m_SelState.EndPos));
1475 SelectNone();
1476 ScrollToCaret();
1477 SetCaretInfo();
1478 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001479 if (bCtrl)
1480 SetCaret(m_pVT->GetBeginWordPlace());
1481 else
1482 SetCaret(m_pVT->GetLineBeginPlace(m_wpCaret));
1483
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001484 ScrollToCaret();
Tom Sepez52f69b32017-03-21 13:42:38 -07001485 SetCaretOrigin();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001486 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001487 }
1488 }
1489}
1490
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001491void CPWL_EditImpl::OnVK_END(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001492 if (!m_pVT->IsValid())
1493 return;
1494
1495 if (bShift) {
1496 if (bCtrl)
1497 SetCaret(m_pVT->GetEndWordPlace());
1498 else
1499 SetCaret(m_pVT->GetLineEndPlace(m_wpCaret));
1500
1501 if (m_SelState.IsEmpty())
1502 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1503 else
1504 m_SelState.SetEndPos(m_wpCaret);
1505
1506 ScrollToCaret();
1507 Refresh();
1508 SetCaretInfo();
1509 } else {
1510 if (!m_SelState.IsEmpty()) {
1511 SetCaret(std::max(m_SelState.BeginPos, m_SelState.EndPos));
1512 SelectNone();
1513 ScrollToCaret();
1514 SetCaretInfo();
1515 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001516 if (bCtrl)
1517 SetCaret(m_pVT->GetEndWordPlace());
1518 else
1519 SetCaret(m_pVT->GetLineEndPlace(m_wpCaret));
1520
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001521 ScrollToCaret();
Tom Sepez52f69b32017-03-21 13:42:38 -07001522 SetCaretOrigin();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001523 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001524 }
1525 }
1526}
1527
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001528bool CPWL_EditImpl::InsertWord(uint16_t word,
1529 int32_t charset,
1530 const CPVT_WordProps* pWordProps,
1531 bool bAddUndo,
1532 bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001533 if (IsTextOverflow() || !m_pVT->IsValid())
tsepez4cf55152016-11-02 14:37:54 -07001534 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001535
Tom Sepez3509d162017-01-30 13:22:02 -08001536 m_pVT->UpdateWordPlace(m_wpCaret);
1537 SetCaret(m_pVT->InsertWord(m_wpCaret, word,
1538 GetCharSetFromUnicode(word, charset), pWordProps));
1539 m_SelState.Set(m_wpCaret, m_wpCaret);
1540 if (m_wpCaret == m_wpOldCaret)
1541 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001542
Tom Sepez3509d162017-01-30 13:22:02 -08001543 if (bAddUndo && m_bEnableUndo) {
1544 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertWord>(
1545 this, m_wpOldCaret, m_wpCaret, word, charset, pWordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001546 }
Tom Sepez3509d162017-01-30 13:22:02 -08001547 if (bPaint)
1548 PaintInsertText(m_wpOldCaret, m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001549
Lei Zhang5688d622017-08-12 07:04:19 -07001550 if (m_pOperationNotify)
1551 m_pOperationNotify->OnInsertWord(m_wpCaret, m_wpOldCaret);
Tom Sepez3509d162017-01-30 13:22:02 -08001552
1553 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001554}
1555
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001556bool CPWL_EditImpl::InsertReturn(const CPVT_SecProps* pSecProps,
1557 const CPVT_WordProps* pWordProps,
1558 bool bAddUndo,
1559 bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001560 if (IsTextOverflow() || !m_pVT->IsValid())
tsepez4cf55152016-11-02 14:37:54 -07001561 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001562
Tom Sepez3509d162017-01-30 13:22:02 -08001563 m_pVT->UpdateWordPlace(m_wpCaret);
1564 SetCaret(m_pVT->InsertSection(m_wpCaret, pSecProps, pWordProps));
1565 m_SelState.Set(m_wpCaret, m_wpCaret);
1566 if (m_wpCaret == m_wpOldCaret)
1567 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001568
Tom Sepez3509d162017-01-30 13:22:02 -08001569 if (bAddUndo && m_bEnableUndo) {
1570 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertReturn>(
1571 this, m_wpOldCaret, m_wpCaret, pSecProps, pWordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001572 }
Tom Sepez3509d162017-01-30 13:22:02 -08001573 if (bPaint) {
1574 RearrangePart(CPVT_WordRange(m_wpOldCaret, m_wpCaret));
1575 ScrollToCaret();
1576 Refresh();
1577 SetCaretOrigin();
1578 SetCaretInfo();
1579 }
Lei Zhang5688d622017-08-12 07:04:19 -07001580 if (m_pOperationNotify)
1581 m_pOperationNotify->OnInsertReturn(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001582
Tom Sepez3509d162017-01-30 13:22:02 -08001583 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001584}
1585
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001586bool CPWL_EditImpl::Backspace(bool bAddUndo, bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001587 if (!m_pVT->IsValid() || m_wpCaret == m_pVT->GetBeginWordPlace())
1588 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001589
Tom Sepez3509d162017-01-30 13:22:02 -08001590 CPVT_Section section;
1591 CPVT_Word word;
1592 if (bAddUndo) {
1593 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1594 pIterator->SetAt(m_wpCaret);
1595 pIterator->GetSection(section);
1596 pIterator->GetWord(word);
1597 }
1598 m_pVT->UpdateWordPlace(m_wpCaret);
1599 SetCaret(m_pVT->BackSpaceWord(m_wpCaret));
1600 m_SelState.Set(m_wpCaret, m_wpCaret);
1601 if (m_wpCaret == m_wpOldCaret)
1602 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001603
Tom Sepez3509d162017-01-30 13:22:02 -08001604 if (bAddUndo && m_bEnableUndo) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001605 if (m_wpCaret.nSecIndex != m_wpOldCaret.nSecIndex) {
Tom Sepez3509d162017-01-30 13:22:02 -08001606 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Backspace>(
1607 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1608 section.SecProps, section.WordProps));
1609 } else {
1610 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Backspace>(
1611 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1612 section.SecProps, word.WordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001613 }
1614 }
Tom Sepez3509d162017-01-30 13:22:02 -08001615 if (bPaint) {
1616 RearrangePart(CPVT_WordRange(m_wpCaret, m_wpOldCaret));
1617 ScrollToCaret();
1618 Refresh();
1619 SetCaretOrigin();
1620 SetCaretInfo();
1621 }
Lei Zhang5688d622017-08-12 07:04:19 -07001622 if (m_pOperationNotify)
1623 m_pOperationNotify->OnBackSpace(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001624
Tom Sepez3509d162017-01-30 13:22:02 -08001625 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001626}
1627
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001628bool CPWL_EditImpl::Delete(bool bAddUndo, bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001629 if (!m_pVT->IsValid() || m_wpCaret == m_pVT->GetEndWordPlace())
1630 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001631
Tom Sepez3509d162017-01-30 13:22:02 -08001632 CPVT_Section section;
1633 CPVT_Word word;
1634 if (bAddUndo) {
1635 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1636 pIterator->SetAt(m_pVT->GetNextWordPlace(m_wpCaret));
1637 pIterator->GetSection(section);
1638 pIterator->GetWord(word);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001639 }
Tom Sepez3509d162017-01-30 13:22:02 -08001640 m_pVT->UpdateWordPlace(m_wpCaret);
1641 bool bSecEnd = (m_wpCaret == m_pVT->GetSectionEndPlace(m_wpCaret));
1642 SetCaret(m_pVT->DeleteWord(m_wpCaret));
1643 m_SelState.Set(m_wpCaret, m_wpCaret);
1644 if (bAddUndo && m_bEnableUndo) {
1645 if (bSecEnd) {
1646 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Delete>(
1647 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1648 section.SecProps, section.WordProps, bSecEnd));
1649 } else {
1650 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Delete>(
1651 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1652 section.SecProps, word.WordProps, bSecEnd));
1653 }
1654 }
1655 if (bPaint) {
1656 RearrangePart(CPVT_WordRange(m_wpOldCaret, m_wpCaret));
1657 ScrollToCaret();
1658 Refresh();
1659 SetCaretOrigin();
1660 SetCaretInfo();
1661 }
Lei Zhang5688d622017-08-12 07:04:19 -07001662 if (m_pOperationNotify)
1663 m_pOperationNotify->OnDelete(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001664
Tom Sepez3509d162017-01-30 13:22:02 -08001665 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001666}
1667
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001668bool CPWL_EditImpl::Empty() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001669 if (m_pVT->IsValid()) {
1670 m_pVT->DeleteWords(GetWholeWordRange());
1671 SetCaret(m_pVT->GetBeginWordPlace());
1672
tsepez4cf55152016-11-02 14:37:54 -07001673 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001674 }
1675
tsepez4cf55152016-11-02 14:37:54 -07001676 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001677}
1678
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001679bool CPWL_EditImpl::Clear(bool bAddUndo, bool bPaint) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001680 if (!m_pVT->IsValid() || m_SelState.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001681 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001682
thestig821d59e2016-05-11 12:59:22 -07001683 CPVT_WordRange range = m_SelState.ConvertToWordRange();
Diana Gage22bf7a52017-07-21 11:33:18 -07001684 if (bAddUndo && m_bEnableUndo) {
Diana Gage89e65622017-07-20 18:09:31 -07001685 AddEditUndoItem(
1686 pdfium::MakeUnique<CFXEU_Clear>(this, range, GetSelectedText()));
Diana Gage22bf7a52017-07-21 11:33:18 -07001687 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001688
thestig821d59e2016-05-11 12:59:22 -07001689 SelectNone();
1690 SetCaret(m_pVT->DeleteWords(range));
1691 m_SelState.Set(m_wpCaret, m_wpCaret);
thestig821d59e2016-05-11 12:59:22 -07001692 if (bPaint) {
1693 RearrangePart(range);
1694 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001695 Refresh();
thestig821d59e2016-05-11 12:59:22 -07001696 SetCaretOrigin();
1697 SetCaretInfo();
1698 }
Lei Zhang5688d622017-08-12 07:04:19 -07001699 if (m_pOperationNotify)
1700 m_pOperationNotify->OnClear(m_wpCaret, m_wpOldCaret);
thestig821d59e2016-05-11 12:59:22 -07001701
tsepez4cf55152016-11-02 14:37:54 -07001702 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001703}
1704
Ryan Harrison275e2602017-09-18 14:23:18 -04001705bool CPWL_EditImpl::InsertText(const WideString& sText,
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001706 int32_t charset,
1707 bool bAddUndo,
1708 bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001709 if (IsTextOverflow())
tsepez4cf55152016-11-02 14:37:54 -07001710 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001711
1712 m_pVT->UpdateWordPlace(m_wpCaret);
tsepeza31da742016-09-08 11:28:14 -07001713 SetCaret(DoInsertText(m_wpCaret, sText, charset));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001714 m_SelState.Set(m_wpCaret, m_wpCaret);
tsepeza31da742016-09-08 11:28:14 -07001715 if (m_wpCaret == m_wpOldCaret)
tsepez4cf55152016-11-02 14:37:54 -07001716 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001717
tsepeza31da742016-09-08 11:28:14 -07001718 if (bAddUndo && m_bEnableUndo) {
Tom Sepez3509d162017-01-30 13:22:02 -08001719 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertText>(
1720 this, m_wpOldCaret, m_wpCaret, sText, charset));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001721 }
tsepeza31da742016-09-08 11:28:14 -07001722 if (bPaint)
1723 PaintInsertText(m_wpOldCaret, m_wpCaret);
1724
Lei Zhang5688d622017-08-12 07:04:19 -07001725 if (m_pOperationNotify)
1726 m_pOperationNotify->OnInsertText(m_wpCaret, m_wpOldCaret);
tsepeza31da742016-09-08 11:28:14 -07001727
tsepez4cf55152016-11-02 14:37:54 -07001728 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001729}
1730
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001731void CPWL_EditImpl::PaintInsertText(const CPVT_WordPlace& wpOld,
1732 const CPVT_WordPlace& wpNew) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001733 if (m_pVT->IsValid()) {
1734 RearrangePart(CPVT_WordRange(wpOld, wpNew));
1735 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001736 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001737 SetCaretOrigin();
1738 SetCaretInfo();
1739 }
1740}
1741
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001742bool CPWL_EditImpl::Redo() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001743 if (m_bEnableUndo) {
1744 if (m_Undo.CanRedo()) {
1745 m_Undo.Redo();
tsepez4cf55152016-11-02 14:37:54 -07001746 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001747 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001748 }
1749
tsepez4cf55152016-11-02 14:37:54 -07001750 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001751}
1752
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001753bool CPWL_EditImpl::Undo() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001754 if (m_bEnableUndo) {
1755 if (m_Undo.CanUndo()) {
1756 m_Undo.Undo();
tsepez4cf55152016-11-02 14:37:54 -07001757 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001758 }
1759 }
1760
tsepez4cf55152016-11-02 14:37:54 -07001761 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001762}
1763
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001764void CPWL_EditImpl::SetCaretOrigin() {
thestig821d59e2016-05-11 12:59:22 -07001765 if (!m_pVT->IsValid())
1766 return;
1767
1768 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1769 pIterator->SetAt(m_wpCaret);
1770 CPVT_Word word;
1771 CPVT_Line line;
1772 if (pIterator->GetWord(word)) {
1773 m_ptCaret.x = word.ptWord.x + word.fWidth;
1774 m_ptCaret.y = word.ptWord.y;
1775 } else if (pIterator->GetLine(line)) {
1776 m_ptCaret.x = line.ptLine.x;
1777 m_ptCaret.y = line.ptLine.y;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001778 }
1779}
1780
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001781CPVT_WordPlace CPWL_EditImpl::WordIndexToWordPlace(int32_t index) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001782 if (m_pVT->IsValid())
1783 return m_pVT->WordIndexToWordPlace(index);
1784
1785 return CPVT_WordPlace();
1786}
1787
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001788bool CPWL_EditImpl::IsTextFull() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001789 int32_t nTotalWords = m_pVT->GetTotalWords();
1790 int32_t nLimitChar = m_pVT->GetLimitChar();
1791 int32_t nCharArray = m_pVT->GetCharArray();
1792
1793 return IsTextOverflow() || (nLimitChar > 0 && nTotalWords >= nLimitChar) ||
1794 (nCharArray > 0 && nTotalWords >= nCharArray);
1795}
1796
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001797bool CPWL_EditImpl::IsTextOverflow() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001798 if (!m_bEnableScroll && !m_bEnableOverflow) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001799 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
1800 CFX_FloatRect rcContent = m_pVT->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001801
dsinclair448c4332016-08-02 12:07:35 -07001802 if (m_pVT->IsMultiLine() && GetTotalLines() > 1 &&
1803 IsFloatBigger(rcContent.Height(), rcPlate.Height())) {
tsepez4cf55152016-11-02 14:37:54 -07001804 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001805 }
1806
dsinclair448c4332016-08-02 12:07:35 -07001807 if (IsFloatBigger(rcContent.Width(), rcPlate.Width()))
tsepez4cf55152016-11-02 14:37:54 -07001808 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001809 }
1810
tsepez4cf55152016-11-02 14:37:54 -07001811 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001812}
1813
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001814bool CPWL_EditImpl::CanUndo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001815 if (m_bEnableUndo) {
1816 return m_Undo.CanUndo();
1817 }
1818
tsepez4cf55152016-11-02 14:37:54 -07001819 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001820}
1821
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001822bool CPWL_EditImpl::CanRedo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001823 if (m_bEnableUndo) {
1824 return m_Undo.CanRedo();
1825 }
1826
tsepez4cf55152016-11-02 14:37:54 -07001827 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001828}
1829
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001830void CPWL_EditImpl::EnableRefresh(bool bRefresh) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001831 m_bEnableRefresh = bRefresh;
1832}
1833
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001834void CPWL_EditImpl::EnableUndo(bool bUndo) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001835 m_bEnableUndo = bUndo;
1836}
1837
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001838CPVT_WordPlace CPWL_EditImpl::DoInsertText(const CPVT_WordPlace& place,
Ryan Harrison275e2602017-09-18 14:23:18 -04001839 const WideString& sText,
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001840 int32_t charset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001841 CPVT_WordPlace wp = place;
1842
1843 if (m_pVT->IsValid()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001844 for (int32_t i = 0, sz = sText.GetLength(); i < sz; i++) {
Tom Sepez62a70f92016-03-21 15:00:20 -07001845 uint16_t word = sText[i];
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001846 switch (word) {
1847 case 0x0D:
dsinclairefd5a992016-07-18 10:04:07 -07001848 wp = m_pVT->InsertSection(wp, nullptr, nullptr);
Ryan Harrisonf8763bb2017-08-31 14:22:39 -04001849 if (i + 1 < sz && sText[i + 1] == 0x0A)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001850 i++;
1851 break;
1852 case 0x0A:
dsinclairefd5a992016-07-18 10:04:07 -07001853 wp = m_pVT->InsertSection(wp, nullptr, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001854 break;
1855 case 0x09:
1856 word = 0x20;
1857 default:
1858 wp = m_pVT->InsertWord(wp, word, GetCharSetFromUnicode(word, charset),
dsinclairefd5a992016-07-18 10:04:07 -07001859 nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001860 break;
1861 }
1862 }
1863 }
1864
1865 return wp;
1866}
1867
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001868int32_t CPWL_EditImpl::GetCharSetFromUnicode(uint16_t word,
1869 int32_t nOldCharset) {
dsinclairc7a73492016-04-05 12:01:42 -07001870 if (IPVT_FontMap* pFontMap = GetFontMap())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001871 return pFontMap->CharSetFromUnicode(word, nOldCharset);
1872 return nOldCharset;
1873}
1874
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001875void CPWL_EditImpl::AddEditUndoItem(
Lei Zhangae9c5ca2017-08-12 07:15:14 -07001876 std::unique_ptr<IFX_Edit_UndoItem> pEditUndoItem) {
Lei Zhang1a89e362017-03-23 15:27:25 -07001877 m_Undo.AddItem(std::move(pEditUndoItem));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001878}
1879
Ryan Harrison275e2602017-09-18 14:23:18 -04001880ByteString CPWL_EditImpl::GetPDFWordString(int32_t nFontIndex,
1881 uint16_t Word,
1882 uint16_t SubWord) {
Dan Sinclairc08dc392017-07-24 08:57:35 -04001883 IPVT_FontMap* pFontMap = GetFontMap();
1884 CPDF_Font* pPDFFont = pFontMap->GetPDFFont(nFontIndex);
1885 if (!pPDFFont)
Ryan Harrison275e2602017-09-18 14:23:18 -04001886 return ByteString();
Dan Sinclairc08dc392017-07-24 08:57:35 -04001887
Ryan Harrison275e2602017-09-18 14:23:18 -04001888 ByteString sWord;
Dan Sinclairc08dc392017-07-24 08:57:35 -04001889 if (SubWord > 0) {
1890 Word = SubWord;
1891 } else {
1892 uint32_t dwCharCode = pPDFFont->IsUnicodeCompatible()
1893 ? pPDFFont->CharCodeFromUnicode(Word)
1894 : pFontMap->CharCodeFromUnicode(nFontIndex, Word);
1895 if (dwCharCode > 0) {
1896 pPDFFont->AppendChar(&sWord, dwCharCode);
1897 return sWord;
1898 }
1899 }
1900 pPDFFont->AppendChar(&sWord, Word);
1901 return sWord;
1902}
1903
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001904CPWL_EditImpl_LineRectArray::CPWL_EditImpl_LineRectArray() {}
weili625ad662016-06-15 11:21:33 -07001905
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001906CPWL_EditImpl_LineRectArray::~CPWL_EditImpl_LineRectArray() {}
weili625ad662016-06-15 11:21:33 -07001907
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001908void CPWL_EditImpl_LineRectArray::operator=(
1909 CPWL_EditImpl_LineRectArray&& that) {
Tom Sepez3509d162017-01-30 13:22:02 -08001910 m_LineRects = std::move(that.m_LineRects);
weili625ad662016-06-15 11:21:33 -07001911}
1912
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001913void CPWL_EditImpl_LineRectArray::Add(const CPVT_WordRange& wrLine,
1914 const CFX_FloatRect& rcLine) {
1915 m_LineRects.push_back(
1916 pdfium::MakeUnique<CPWL_EditImpl_LineRect>(wrLine, rcLine));
weili625ad662016-06-15 11:21:33 -07001917}
1918
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001919int32_t CPWL_EditImpl_LineRectArray::GetSize() const {
Tom Sepez3509d162017-01-30 13:22:02 -08001920 return pdfium::CollectionSize<int32_t>(m_LineRects);
weili625ad662016-06-15 11:21:33 -07001921}
1922
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001923CPWL_EditImpl_LineRect* CPWL_EditImpl_LineRectArray::GetAt(
1924 int32_t nIndex) const {
Tom Sepez3509d162017-01-30 13:22:02 -08001925 if (nIndex < 0 || nIndex >= GetSize())
weili625ad662016-06-15 11:21:33 -07001926 return nullptr;
1927
Tom Sepez3509d162017-01-30 13:22:02 -08001928 return m_LineRects[nIndex].get();
weili625ad662016-06-15 11:21:33 -07001929}
1930
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001931CPWL_EditImpl_Select::CPWL_EditImpl_Select() {}
weili625ad662016-06-15 11:21:33 -07001932
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001933CPWL_EditImpl_Select::CPWL_EditImpl_Select(const CPVT_WordRange& range) {
weili625ad662016-06-15 11:21:33 -07001934 Set(range.BeginPos, range.EndPos);
1935}
1936
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001937CPVT_WordRange CPWL_EditImpl_Select::ConvertToWordRange() const {
weili625ad662016-06-15 11:21:33 -07001938 return CPVT_WordRange(BeginPos, EndPos);
1939}
1940
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001941void CPWL_EditImpl_Select::Reset() {
Tom Sepez52f69b32017-03-21 13:42:38 -07001942 BeginPos.Reset();
1943 EndPos.Reset();
weili625ad662016-06-15 11:21:33 -07001944}
1945
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001946void CPWL_EditImpl_Select::Set(const CPVT_WordPlace& begin,
1947 const CPVT_WordPlace& end) {
weili625ad662016-06-15 11:21:33 -07001948 BeginPos = begin;
1949 EndPos = end;
1950}
1951
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001952void CPWL_EditImpl_Select::SetEndPos(const CPVT_WordPlace& end) {
weili625ad662016-06-15 11:21:33 -07001953 EndPos = end;
1954}
1955
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001956bool CPWL_EditImpl_Select::IsEmpty() const {
Tom Sepez52f69b32017-03-21 13:42:38 -07001957 return BeginPos == EndPos;
weili625ad662016-06-15 11:21:33 -07001958}
1959
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001960CPWL_EditImpl_RectArray::CPWL_EditImpl_RectArray() {}
weili625ad662016-06-15 11:21:33 -07001961
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001962CPWL_EditImpl_RectArray::~CPWL_EditImpl_RectArray() {}
weili625ad662016-06-15 11:21:33 -07001963
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001964void CPWL_EditImpl_RectArray::Clear() {
Tom Sepez3509d162017-01-30 13:22:02 -08001965 m_Rects.clear();
weili625ad662016-06-15 11:21:33 -07001966}
1967
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001968void CPWL_EditImpl_RectArray::Add(const CFX_FloatRect& rect) {
weili625ad662016-06-15 11:21:33 -07001969 // check for overlapped area
Tom Sepez3509d162017-01-30 13:22:02 -08001970 for (const auto& pRect : m_Rects) {
weili625ad662016-06-15 11:21:33 -07001971 if (pRect && pRect->Contains(rect))
1972 return;
1973 }
Tom Sepez3509d162017-01-30 13:22:02 -08001974 m_Rects.push_back(pdfium::MakeUnique<CFX_FloatRect>(rect));
weili625ad662016-06-15 11:21:33 -07001975}
1976
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001977int32_t CPWL_EditImpl_RectArray::GetSize() const {
Tom Sepez3509d162017-01-30 13:22:02 -08001978 return pdfium::CollectionSize<int32_t>(m_Rects);
weili625ad662016-06-15 11:21:33 -07001979}
1980
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001981CFX_FloatRect* CPWL_EditImpl_RectArray::GetAt(int32_t nIndex) const {
Tom Sepez3509d162017-01-30 13:22:02 -08001982 if (nIndex < 0 || nIndex >= GetSize())
weili625ad662016-06-15 11:21:33 -07001983 return nullptr;
1984
Tom Sepez3509d162017-01-30 13:22:02 -08001985 return m_Rects[nIndex].get();
weili625ad662016-06-15 11:21:33 -07001986}