blob: f26811e1377ed5afc59a22d74181d3254fe986d4 [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 Sinclairf51a02a2017-04-19 12:46:53 -040025#include "core/fxcrt/fx_codepage.h"
dsinclair74a34fc2016-09-29 16:41:42 -070026#include "core/fxge/cfx_graphstatedata.h"
27#include "core/fxge/cfx_pathdata.h"
28#include "core/fxge/cfx_renderdevice.h"
dsinclaire35af1e2016-07-13 11:26:20 -070029#include "fpdfsdk/cfx_systemhandler.h"
Dan Sinclairc411eb92017-07-25 09:39:30 -040030#include "fpdfsdk/pwl/cpwl_edit.h"
31#include "fpdfsdk/pwl/cpwl_edit_ctrl.h"
32#include "fpdfsdk/pwl/cpwl_scroll_bar.h"
tsepez36eb4bd2016-10-03 15:24:27 -070033#include "third_party/base/ptr_util.h"
Tom Sepez3509d162017-01-30 13:22:02 -080034#include "third_party/base/stl_util.h"
dsinclaire35af1e2016-07-13 11:26:20 -070035
dsinclair8f4bf9a2016-05-04 13:51:51 -070036namespace {
37
38const int kEditUndoMaxItems = 10000;
39
dsinclaire35af1e2016-07-13 11:26:20 -070040void DrawTextString(CFX_RenderDevice* pDevice,
Dan Sinclairf528eee2017-02-14 11:52:07 -050041 const CFX_PointF& pt,
dsinclaire35af1e2016-07-13 11:26:20 -070042 CPDF_Font* pFont,
Dan Sinclair05df0752017-03-14 14:43:42 -040043 float fFontSize,
Lei Zhangeb14e042017-08-15 13:56:43 -070044 const CFX_Matrix& mtUser2Device,
dsinclaire35af1e2016-07-13 11:26:20 -070045 const CFX_ByteString& str,
46 FX_ARGB crTextFill,
dsinclaire35af1e2016-07-13 11:26:20 -070047 int32_t nHorzScale) {
Lei Zhangeb14e042017-08-15 13:56:43 -070048 if (!pFont)
49 return;
dsinclaire35af1e2016-07-13 11:26:20 -070050
Lei Zhangeb14e042017-08-15 13:56:43 -070051 CFX_PointF pos = mtUser2Device.Transform(pt);
52 CFX_Matrix mt;
53 if (nHorzScale == 100) {
54 mt = mtUser2Device;
55 } else {
56 mt = CFX_Matrix(nHorzScale / 100.0f, 0, 0, 1, 0, 0);
57 mt.Concat(mtUser2Device);
dsinclaire35af1e2016-07-13 11:26:20 -070058 }
Lei Zhangeb14e042017-08-15 13:56:43 -070059
60 CPDF_RenderOptions ro;
61 ro.m_Flags = RENDER_CLEARTYPE;
62 ro.m_ColorMode = CPDF_RenderOptions::kNormal;
63 CPDF_TextRenderer::DrawTextString(pDevice, pos.x, pos.y, pFont, fFontSize,
64 &mt, str, crTextFill, nullptr, &ro);
dsinclaire35af1e2016-07-13 11:26:20 -070065}
66
dsinclair8f4bf9a2016-05-04 13:51:51 -070067} // namespace
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070068
Dan Sinclair6b0158f2017-07-24 09:42:55 -040069CPWL_EditImpl_Iterator::CPWL_EditImpl_Iterator(
70 CPWL_EditImpl* pEdit,
71 CPDF_VariableText::Iterator* pVTIterator)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072 : m_pEdit(pEdit), m_pVTIterator(pVTIterator) {}
73
Dan Sinclair6b0158f2017-07-24 09:42:55 -040074CPWL_EditImpl_Iterator::~CPWL_EditImpl_Iterator() {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075
Dan Sinclair6b0158f2017-07-24 09:42:55 -040076bool CPWL_EditImpl_Iterator::NextWord() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077 return m_pVTIterator->NextWord();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070078}
79
Dan Sinclair6b0158f2017-07-24 09:42:55 -040080bool CPWL_EditImpl_Iterator::PrevWord() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 return m_pVTIterator->PrevWord();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070082}
83
Dan Sinclair6b0158f2017-07-24 09:42:55 -040084bool CPWL_EditImpl_Iterator::GetWord(CPVT_Word& word) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 ASSERT(m_pEdit);
86
87 if (m_pVTIterator->GetWord(word)) {
88 word.ptWord = m_pEdit->VTToEdit(word.ptWord);
tsepez4cf55152016-11-02 14:37:54 -070089 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090 }
tsepez4cf55152016-11-02 14:37:54 -070091 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070092}
93
Dan Sinclair6b0158f2017-07-24 09:42:55 -040094bool CPWL_EditImpl_Iterator::GetLine(CPVT_Line& line) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095 ASSERT(m_pEdit);
96
97 if (m_pVTIterator->GetLine(line)) {
98 line.ptLine = m_pEdit->VTToEdit(line.ptLine);
tsepez4cf55152016-11-02 14:37:54 -070099 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 }
tsepez4cf55152016-11-02 14:37:54 -0700101 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700102}
103
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400104void CPWL_EditImpl_Iterator::SetAt(int32_t nWordIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105 m_pVTIterator->SetAt(nWordIndex);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700106}
107
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400108void CPWL_EditImpl_Iterator::SetAt(const CPVT_WordPlace& place) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700109 m_pVTIterator->SetAt(place);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700110}
111
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400112const CPVT_WordPlace& CPWL_EditImpl_Iterator::GetAt() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113 return m_pVTIterator->GetAt();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700114}
115
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400116CPWL_EditImpl_Provider::CPWL_EditImpl_Provider(IPVT_FontMap* pFontMap)
dsinclairc7a73492016-04-05 12:01:42 -0700117 : CPDF_VariableText::Provider(pFontMap), m_pFontMap(pFontMap) {
Lei Zhang96660d62015-12-14 18:27:25 -0800118 ASSERT(m_pFontMap);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700119}
120
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400121CPWL_EditImpl_Provider::~CPWL_EditImpl_Provider() {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700122
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400123IPVT_FontMap* CPWL_EditImpl_Provider::GetFontMap() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 return m_pFontMap;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700125}
126
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400127int32_t CPWL_EditImpl_Provider::GetCharWidth(int32_t nFontIndex,
128 uint16_t word) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129 if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex)) {
tsepezc3255f52016-03-25 14:52:27 -0700130 uint32_t charcode = word;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700131
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132 if (pPDFFont->IsUnicodeCompatible())
133 charcode = pPDFFont->CharCodeFromUnicode(word);
134 else
135 charcode = m_pFontMap->CharCodeFromUnicode(nFontIndex, word);
136
Wei Li89409932016-03-28 10:33:33 -0700137 if (charcode != CPDF_Font::kInvalidCharCode)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138 return pPDFFont->GetCharWidthF(charcode);
139 }
140
141 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700142}
143
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400144int32_t CPWL_EditImpl_Provider::GetTypeAscent(int32_t nFontIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145 if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex))
146 return pPDFFont->GetTypeAscent();
147
148 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700149}
150
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400151int32_t CPWL_EditImpl_Provider::GetTypeDescent(int32_t nFontIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152 if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex))
153 return pPDFFont->GetTypeDescent();
154
155 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700156}
157
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400158int32_t CPWL_EditImpl_Provider::GetWordFontIndex(uint16_t word,
159 int32_t charset,
160 int32_t nFontIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 return m_pFontMap->GetWordFontIndex(word, charset, nFontIndex);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700162}
163
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400164int32_t CPWL_EditImpl_Provider::GetDefaultFontIndex() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700166}
167
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400168bool CPWL_EditImpl_Provider::IsLatinWord(uint16_t word) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169 return FX_EDIT_ISLATINWORD(word);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700170}
171
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400172CPWL_EditImpl_Refresh::CPWL_EditImpl_Refresh() {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700173
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400174CPWL_EditImpl_Refresh::~CPWL_EditImpl_Refresh() {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400176void CPWL_EditImpl_Refresh::BeginRefresh() {
Tom Sepez3509d162017-01-30 13:22:02 -0800177 m_RefreshRects.Clear();
178 m_OldLineRects = std::move(m_NewLineRects);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700179}
180
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400181void CPWL_EditImpl_Refresh::Push(const CPVT_WordRange& linerange,
182 const CFX_FloatRect& rect) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183 m_NewLineRects.Add(linerange, rect);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700184}
185
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400186void CPWL_EditImpl_Refresh::NoAnalyse() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700187 {
188 for (int32_t i = 0, sz = m_OldLineRects.GetSize(); i < sz; i++)
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400189 if (CPWL_EditImpl_LineRect* pOldRect = m_OldLineRects.GetAt(i))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 m_RefreshRects.Add(pOldRect->m_rcLine);
191 }
192
193 {
194 for (int32_t i = 0, sz = m_NewLineRects.GetSize(); i < sz; i++)
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400195 if (CPWL_EditImpl_LineRect* pNewRect = m_NewLineRects.GetAt(i))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196 m_RefreshRects.Add(pNewRect->m_rcLine);
197 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700198}
199
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400200const CPWL_EditImpl_RectArray* CPWL_EditImpl_Refresh::GetRefreshRects() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201 return &m_RefreshRects;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700202}
203
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400204void CPWL_EditImpl_Refresh::EndRefresh() {
Tom Sepez3509d162017-01-30 13:22:02 -0800205 m_RefreshRects.Clear();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700206}
207
Lei Zhangbff66f12017-08-15 13:26:44 -0700208CPWL_EditImpl_Undo::CPWL_EditImpl_Undo()
209 : m_nCurUndoPos(0), m_bWorking(false) {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210
Lei Zhangbff66f12017-08-15 13:26:44 -0700211CPWL_EditImpl_Undo::~CPWL_EditImpl_Undo() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700212
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400213bool CPWL_EditImpl_Undo::CanUndo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700214 return m_nCurUndoPos > 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700215}
216
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400217void CPWL_EditImpl_Undo::Undo() {
tsepez4cf55152016-11-02 14:37:54 -0700218 m_bWorking = true;
Lei Zhangbff66f12017-08-15 13:26:44 -0700219 if (CanUndo()) {
Tom Sepez3509d162017-01-30 13:22:02 -0800220 m_UndoItemStack[m_nCurUndoPos - 1]->Undo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221 m_nCurUndoPos--;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222 }
tsepez4cf55152016-11-02 14:37:54 -0700223 m_bWorking = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700224}
225
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400226bool CPWL_EditImpl_Undo::CanRedo() const {
Tom Sepez3509d162017-01-30 13:22:02 -0800227 return m_nCurUndoPos < m_UndoItemStack.size();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700228}
229
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400230void CPWL_EditImpl_Undo::Redo() {
tsepez4cf55152016-11-02 14:37:54 -0700231 m_bWorking = true;
Lei Zhangbff66f12017-08-15 13:26:44 -0700232 if (CanRedo()) {
Tom Sepez3509d162017-01-30 13:22:02 -0800233 m_UndoItemStack[m_nCurUndoPos]->Redo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234 m_nCurUndoPos++;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700235 }
tsepez4cf55152016-11-02 14:37:54 -0700236 m_bWorking = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700237}
238
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400239void CPWL_EditImpl_Undo::AddItem(std::unique_ptr<IFX_Edit_UndoItem> pItem) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240 ASSERT(!m_bWorking);
Lei Zhang96660d62015-12-14 18:27:25 -0800241 ASSERT(pItem);
Lei Zhangbff66f12017-08-15 13:26:44 -0700242 if (CanRedo())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 RemoveTails();
244
Lei Zhangbff66f12017-08-15 13:26:44 -0700245 if (m_UndoItemStack.size() >= kEditUndoMaxItems)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700246 RemoveHeads();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700247
Tom Sepez3509d162017-01-30 13:22:02 -0800248 m_UndoItemStack.push_back(std::move(pItem));
249 m_nCurUndoPos = m_UndoItemStack.size();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700250}
251
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400252void CPWL_EditImpl_Undo::RemoveHeads() {
Tom Sepez3509d162017-01-30 13:22:02 -0800253 ASSERT(m_UndoItemStack.size() > 1);
254 m_UndoItemStack.pop_front();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700255}
256
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400257void CPWL_EditImpl_Undo::RemoveTails() {
Lei Zhangbff66f12017-08-15 13:26:44 -0700258 while (CanRedo())
Tom Sepez3509d162017-01-30 13:22:02 -0800259 m_UndoItemStack.pop_back();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260}
261
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400262CFXEU_InsertWord::CFXEU_InsertWord(CPWL_EditImpl* pEdit,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263 const CPVT_WordPlace& wpOldPlace,
264 const CPVT_WordPlace& wpNewPlace,
Tom Sepez62a70f92016-03-21 15:00:20 -0700265 uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266 int32_t charset,
267 const CPVT_WordProps* pWordProps)
268 : m_pEdit(pEdit),
269 m_wpOld(wpOldPlace),
270 m_wpNew(wpNewPlace),
271 m_Word(word),
272 m_nCharset(charset),
273 m_WordProps() {
Lei Zhangf5a06672017-08-12 08:01:37 -0700274 ASSERT(m_pEdit);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275 if (pWordProps)
276 m_WordProps = *pWordProps;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700277}
278
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279CFXEU_InsertWord::~CFXEU_InsertWord() {}
280
281void CFXEU_InsertWord::Redo() {
Lei Zhangf5a06672017-08-12 08:01:37 -0700282 m_pEdit->SelectNone();
283 m_pEdit->SetCaret(m_wpOld);
284 m_pEdit->InsertWord(m_Word, m_nCharset, &m_WordProps, false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700285}
286
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287void CFXEU_InsertWord::Undo() {
Lei Zhangf5a06672017-08-12 08:01:37 -0700288 m_pEdit->SelectNone();
289 m_pEdit->SetCaret(m_wpNew);
290 m_pEdit->Backspace(false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700291}
292
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400293CFXEU_InsertReturn::CFXEU_InsertReturn(CPWL_EditImpl* pEdit,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700294 const CPVT_WordPlace& wpOldPlace,
295 const CPVT_WordPlace& wpNewPlace,
296 const CPVT_SecProps* pSecProps,
297 const CPVT_WordProps* pWordProps)
298 : m_pEdit(pEdit),
299 m_wpOld(wpOldPlace),
300 m_wpNew(wpNewPlace),
301 m_SecProps(),
302 m_WordProps() {
Lei Zhangf5a06672017-08-12 08:01:37 -0700303 ASSERT(m_pEdit);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700304 if (pSecProps)
305 m_SecProps = *pSecProps;
306 if (pWordProps)
307 m_WordProps = *pWordProps;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700308}
309
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700310CFXEU_InsertReturn::~CFXEU_InsertReturn() {}
311
312void CFXEU_InsertReturn::Redo() {
Lei Zhangf5a06672017-08-12 08:01:37 -0700313 m_pEdit->SelectNone();
314 m_pEdit->SetCaret(m_wpOld);
315 m_pEdit->InsertReturn(&m_SecProps, &m_WordProps, false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700316}
317
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700318void CFXEU_InsertReturn::Undo() {
Lei Zhangf5a06672017-08-12 08:01:37 -0700319 m_pEdit->SelectNone();
320 m_pEdit->SetCaret(m_wpNew);
321 m_pEdit->Backspace(false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700322}
323
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400324CFXEU_Backspace::CFXEU_Backspace(CPWL_EditImpl* pEdit,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700325 const CPVT_WordPlace& wpOldPlace,
326 const CPVT_WordPlace& wpNewPlace,
Tom Sepez62a70f92016-03-21 15:00:20 -0700327 uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700328 int32_t charset,
329 const CPVT_SecProps& SecProps,
330 const CPVT_WordProps& WordProps)
331 : m_pEdit(pEdit),
332 m_wpOld(wpOldPlace),
333 m_wpNew(wpNewPlace),
334 m_Word(word),
335 m_nCharset(charset),
336 m_SecProps(SecProps),
Lei Zhangf5a06672017-08-12 08:01:37 -0700337 m_WordProps(WordProps) {
338 ASSERT(m_pEdit);
339}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340
341CFXEU_Backspace::~CFXEU_Backspace() {}
342
343void CFXEU_Backspace::Redo() {
Tom Sepez52f69b32017-03-21 13:42:38 -0700344 m_pEdit->SelectNone();
345 m_pEdit->SetCaret(m_wpOld);
346 m_pEdit->Backspace(false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700347}
348
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700349void CFXEU_Backspace::Undo() {
Tom Sepez52f69b32017-03-21 13:42:38 -0700350 m_pEdit->SelectNone();
351 m_pEdit->SetCaret(m_wpNew);
352 if (m_wpNew.nSecIndex != m_wpOld.nSecIndex)
353 m_pEdit->InsertReturn(&m_SecProps, &m_WordProps, false, true);
354 else
355 m_pEdit->InsertWord(m_Word, m_nCharset, &m_WordProps, false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700356}
357
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400358CFXEU_Delete::CFXEU_Delete(CPWL_EditImpl* pEdit,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700359 const CPVT_WordPlace& wpOldPlace,
360 const CPVT_WordPlace& wpNewPlace,
Tom Sepez62a70f92016-03-21 15:00:20 -0700361 uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700362 int32_t charset,
363 const CPVT_SecProps& SecProps,
364 const CPVT_WordProps& WordProps,
tsepez4cf55152016-11-02 14:37:54 -0700365 bool bSecEnd)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700366 : m_pEdit(pEdit),
367 m_wpOld(wpOldPlace),
368 m_wpNew(wpNewPlace),
369 m_Word(word),
370 m_nCharset(charset),
371 m_SecProps(SecProps),
372 m_WordProps(WordProps),
Lei Zhangf5a06672017-08-12 08:01:37 -0700373 m_bSecEnd(bSecEnd) {
374 ASSERT(m_pEdit);
375}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700376
377CFXEU_Delete::~CFXEU_Delete() {}
378
379void CFXEU_Delete::Redo() {
Lei Zhangf5a06672017-08-12 08:01:37 -0700380 m_pEdit->SelectNone();
381 m_pEdit->SetCaret(m_wpOld);
382 m_pEdit->Delete(false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700383}
384
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700385void CFXEU_Delete::Undo() {
Lei Zhangf5a06672017-08-12 08:01:37 -0700386 m_pEdit->SelectNone();
387 m_pEdit->SetCaret(m_wpNew);
388 if (m_bSecEnd)
389 m_pEdit->InsertReturn(&m_SecProps, &m_WordProps, false, true);
390 else
391 m_pEdit->InsertWord(m_Word, m_nCharset, &m_WordProps, false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700392}
393
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400394CFXEU_Clear::CFXEU_Clear(CPWL_EditImpl* pEdit,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700395 const CPVT_WordRange& wrSel,
396 const CFX_WideString& swText)
Lei Zhangf5a06672017-08-12 08:01:37 -0700397 : m_pEdit(pEdit), m_wrSel(wrSel), m_swText(swText) {
398 ASSERT(m_pEdit);
399}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400
401CFXEU_Clear::~CFXEU_Clear() {}
402
403void CFXEU_Clear::Redo() {
Lei Zhangf5a06672017-08-12 08:01:37 -0700404 m_pEdit->SelectNone();
405 m_pEdit->SetSelection(m_wrSel.BeginPos, m_wrSel.EndPos);
406 m_pEdit->Clear(false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700407}
408
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409void CFXEU_Clear::Undo() {
Lei Zhangf5a06672017-08-12 08:01:37 -0700410 m_pEdit->SelectNone();
411 m_pEdit->SetCaret(m_wrSel.BeginPos);
412 m_pEdit->InsertText(m_swText, FX_CHARSET_Default, false, true);
413 m_pEdit->SetSelection(m_wrSel.BeginPos, m_wrSel.EndPos);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700414}
415
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400416CFXEU_InsertText::CFXEU_InsertText(CPWL_EditImpl* pEdit,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700417 const CPVT_WordPlace& wpOldPlace,
418 const CPVT_WordPlace& wpNewPlace,
419 const CFX_WideString& swText,
dsinclairefd5a992016-07-18 10:04:07 -0700420 int32_t charset)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700421 : m_pEdit(pEdit),
422 m_wpOld(wpOldPlace),
423 m_wpNew(wpNewPlace),
424 m_swText(swText),
Lei Zhangf5a06672017-08-12 08:01:37 -0700425 m_nCharset(charset) {
426 ASSERT(m_pEdit);
427}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700428
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700429CFXEU_InsertText::~CFXEU_InsertText() {}
430
431void CFXEU_InsertText::Redo() {
Lei Zhangf5a06672017-08-12 08:01:37 -0700432 m_pEdit->SelectNone();
433 m_pEdit->SetCaret(m_wpOld);
434 m_pEdit->InsertText(m_swText, m_nCharset, false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700435}
436
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700437void CFXEU_InsertText::Undo() {
Lei Zhangf5a06672017-08-12 08:01:37 -0700438 m_pEdit->SelectNone();
439 m_pEdit->SetSelection(m_wpOld, m_wpNew);
440 m_pEdit->Clear(false, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700441}
442
dsinclaire35af1e2016-07-13 11:26:20 -0700443// static
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400444void CPWL_EditImpl::DrawEdit(CFX_RenderDevice* pDevice,
Lei Zhangeb14e042017-08-15 13:56:43 -0700445 const CFX_Matrix& mtUser2Device,
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400446 CPWL_EditImpl* pEdit,
447 FX_COLORREF crTextFill,
448 const CFX_FloatRect& rcClip,
449 const CFX_PointF& ptOffset,
450 const CPVT_WordRange* pRange,
451 CFX_SystemHandler* pSystemHandler,
452 CFFL_FormFiller* pFFLData) {
dsinclaire35af1e2016-07-13 11:26:20 -0700453 const bool bContinuous =
454 pEdit->GetCharArray() == 0 && pEdit->GetCharSpace() <= 0.0f;
455 uint16_t SubWord = pEdit->GetPasswordChar();
Dan Sinclair05df0752017-03-14 14:43:42 -0400456 float fFontSize = pEdit->GetFontSize();
dsinclaire35af1e2016-07-13 11:26:20 -0700457 CPVT_WordRange wrSelect = pEdit->GetSelectWordRange();
458 int32_t nHorzScale = pEdit->GetHorzScale();
459
460 FX_COLORREF crCurFill = crTextFill;
461 FX_COLORREF crOldFill = crCurFill;
462
tsepez4cf55152016-11-02 14:37:54 -0700463 bool bSelect = false;
Lei Zhangeb14e042017-08-15 13:56:43 -0700464 static const FX_COLORREF crWhite = ArgbEncode(255, 255, 255, 255);
465 static const FX_COLORREF crSelBK = ArgbEncode(255, 0, 51, 113);
dsinclaire35af1e2016-07-13 11:26:20 -0700466
Henrique Nakashima2e2da132017-06-27 13:43:22 -0400467 std::ostringstream sTextBuf;
dsinclaire35af1e2016-07-13 11:26:20 -0700468 int32_t nFontIndex = -1;
Dan Sinclairf528eee2017-02-14 11:52:07 -0500469 CFX_PointF ptBT;
Tom Sepez1629f602017-04-21 14:11:26 -0700470 CFX_RenderDevice::StateRestorer restorer(pDevice);
dsinclaire35af1e2016-07-13 11:26:20 -0700471 if (!rcClip.IsEmpty()) {
472 CFX_FloatRect rcTemp = rcClip;
Lei Zhangeb14e042017-08-15 13:56:43 -0700473 mtUser2Device.TransformRect(rcTemp);
dsinclaire35af1e2016-07-13 11:26:20 -0700474 pDevice->SetClip_Rect(rcTemp.ToFxRect());
475 }
476
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400477 CPWL_EditImpl_Iterator* pIterator = pEdit->GetIterator();
Lei Zhangeb14e042017-08-15 13:56:43 -0700478 IPVT_FontMap* pFontMap = pEdit->GetFontMap();
479 if (!pFontMap)
480 return;
dsinclaire35af1e2016-07-13 11:26:20 -0700481
Lei Zhangeb14e042017-08-15 13:56:43 -0700482 if (pRange)
483 pIterator->SetAt(pRange->BeginPos);
484 else
485 pIterator->SetAt(0);
dsinclaire35af1e2016-07-13 11:26:20 -0700486
Lei Zhangeb14e042017-08-15 13:56:43 -0700487 CPVT_WordPlace oldplace;
488 while (pIterator->NextWord()) {
489 CPVT_WordPlace place = pIterator->GetAt();
490 if (pRange && place > pRange->EndPos)
491 break;
dsinclaire35af1e2016-07-13 11:26:20 -0700492
Lei Zhangeb14e042017-08-15 13:56:43 -0700493 if (!wrSelect.IsEmpty()) {
494 bSelect = place > wrSelect.BeginPos && place <= wrSelect.EndPos;
495 crCurFill = bSelect ? crWhite : crTextFill;
496 }
497 if (pSystemHandler && pSystemHandler->IsSelectionImplemented()) {
498 crCurFill = crTextFill;
499 crOldFill = crCurFill;
500 }
501 CPVT_Word word;
502 if (pIterator->GetWord(word)) {
503 if (bSelect) {
504 CPVT_Line line;
505 pIterator->GetLine(line);
dsinclaire35af1e2016-07-13 11:26:20 -0700506
Lei Zhangeb14e042017-08-15 13:56:43 -0700507 if (pSystemHandler && pSystemHandler->IsSelectionImplemented()) {
508 CFX_FloatRect rc(word.ptWord.x, line.ptLine.y + line.fLineDescent,
509 word.ptWord.x + word.fWidth,
510 line.ptLine.y + line.fLineAscent);
511 rc.Intersect(rcClip);
512 pSystemHandler->OutputSelectedRect(pFFLData, rc);
dsinclaire35af1e2016-07-13 11:26:20 -0700513 } else {
Lei Zhangeb14e042017-08-15 13:56:43 -0700514 CFX_PathData pathSelBK;
515 pathSelBK.AppendRect(word.ptWord.x, line.ptLine.y + line.fLineDescent,
516 word.ptWord.x + word.fWidth,
517 line.ptLine.y + line.fLineAscent);
dsinclaire35af1e2016-07-13 11:26:20 -0700518
Lei Zhangeb14e042017-08-15 13:56:43 -0700519 pDevice->DrawPath(&pathSelBK, &mtUser2Device, nullptr, crSelBK, 0,
520 FXFILL_WINDING);
521 }
522 }
523
524 if (bContinuous) {
525 if (place.LineCmp(oldplace) != 0 || word.nFontIndex != nFontIndex ||
526 crOldFill != crCurFill) {
527 if (sTextBuf.tellp() > 0) {
528 DrawTextString(
529 pDevice, CFX_PointF(ptBT.x + ptOffset.x, ptBT.y + ptOffset.y),
530 pFontMap->GetPDFFont(nFontIndex), fFontSize, mtUser2Device,
531 CFX_ByteString(sTextBuf), crOldFill, nHorzScale);
532
533 sTextBuf.str("");
534 }
535 nFontIndex = word.nFontIndex;
536 ptBT = word.ptWord;
537 crOldFill = crCurFill;
538 }
539
540 sTextBuf << pEdit->GetPDFWordString(word.nFontIndex, word.Word,
541 SubWord);
542 } else {
543 DrawTextString(
544 pDevice,
545 CFX_PointF(word.ptWord.x + ptOffset.x, word.ptWord.y + ptOffset.y),
546 pFontMap->GetPDFFont(word.nFontIndex), fFontSize, mtUser2Device,
547 pEdit->GetPDFWordString(word.nFontIndex, word.Word, SubWord),
548 crCurFill, nHorzScale);
549 }
550 oldplace = place;
dsinclaire35af1e2016-07-13 11:26:20 -0700551 }
552 }
Lei Zhangeb14e042017-08-15 13:56:43 -0700553
554 if (sTextBuf.tellp() > 0) {
555 DrawTextString(pDevice,
556 CFX_PointF(ptBT.x + ptOffset.x, ptBT.y + ptOffset.y),
557 pFontMap->GetPDFFont(nFontIndex), fFontSize, mtUser2Device,
558 CFX_ByteString(sTextBuf), crOldFill, nHorzScale);
559 }
dsinclaire35af1e2016-07-13 11:26:20 -0700560}
561
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400562CPWL_EditImpl::CPWL_EditImpl()
Lei Zhang5688d622017-08-12 07:04:19 -0700563 : m_pVT(pdfium::MakeUnique<CPDF_VariableText>()),
tsepez4cf55152016-11-02 14:37:54 -0700564 m_bEnableScroll(false),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700565 m_nAlignment(0),
tsepez4cf55152016-11-02 14:37:54 -0700566 m_bNotifyFlag(false),
567 m_bEnableOverflow(false),
568 m_bEnableRefresh(true),
Lei Zhang5688d622017-08-12 07:04:19 -0700569 m_bEnableUndo(true) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700570
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400571CPWL_EditImpl::~CPWL_EditImpl() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700572
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400573void CPWL_EditImpl::Initialize() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700574 m_pVT->Initialize();
575 SetCaret(m_pVT->GetBeginWordPlace());
576 SetCaretOrigin();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700577}
578
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400579void CPWL_EditImpl::SetFontMap(IPVT_FontMap* pFontMap) {
580 m_pVTProvider = pdfium::MakeUnique<CPWL_EditImpl_Provider>(pFontMap);
thestig821d59e2016-05-11 12:59:22 -0700581 m_pVT->SetProvider(m_pVTProvider.get());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700582}
583
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400584void CPWL_EditImpl::SetNotify(CPWL_EditCtrl* pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700585 m_pNotify = pNotify;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700586}
587
Lei Zhang5688d622017-08-12 07:04:19 -0700588void CPWL_EditImpl::SetOperationNotify(CPWL_Edit* pOperationNotify) {
589 m_pOperationNotify = pOperationNotify;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700590}
591
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400592CPWL_EditImpl_Iterator* CPWL_EditImpl::GetIterator() {
tsepez36eb4bd2016-10-03 15:24:27 -0700593 if (!m_pIterator) {
594 m_pIterator =
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400595 pdfium::MakeUnique<CPWL_EditImpl_Iterator>(this, m_pVT->GetIterator());
tsepez36eb4bd2016-10-03 15:24:27 -0700596 }
thestig821d59e2016-05-11 12:59:22 -0700597 return m_pIterator.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700598}
599
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400600IPVT_FontMap* CPWL_EditImpl::GetFontMap() {
thestig821d59e2016-05-11 12:59:22 -0700601 return m_pVTProvider ? m_pVTProvider->GetFontMap() : nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700602}
603
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400604void CPWL_EditImpl::SetPlateRect(const CFX_FloatRect& rect) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700605 m_pVT->SetPlateRect(rect);
Dan Sinclairf528eee2017-02-14 11:52:07 -0500606 m_ptScrollPos = CFX_PointF(rect.left, rect.top);
dsinclairefd5a992016-07-18 10:04:07 -0700607 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700608}
609
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400610void CPWL_EditImpl::SetAlignmentH(int32_t nFormat, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700611 m_pVT->SetAlignment(nFormat);
612 if (bPaint)
613 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700614}
615
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400616void CPWL_EditImpl::SetAlignmentV(int32_t nFormat, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700617 m_nAlignment = nFormat;
618 if (bPaint)
619 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700620}
621
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400622void CPWL_EditImpl::SetPasswordChar(uint16_t wSubWord, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700623 m_pVT->SetPasswordChar(wSubWord);
624 if (bPaint)
625 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700626}
627
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400628void CPWL_EditImpl::SetLimitChar(int32_t nLimitChar) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700629 m_pVT->SetLimitChar(nLimitChar);
dsinclairefd5a992016-07-18 10:04:07 -0700630 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700631}
632
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400633void CPWL_EditImpl::SetCharArray(int32_t nCharArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700634 m_pVT->SetCharArray(nCharArray);
dsinclairefd5a992016-07-18 10:04:07 -0700635 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700636}
637
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400638void CPWL_EditImpl::SetCharSpace(float fCharSpace) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700639 m_pVT->SetCharSpace(fCharSpace);
dsinclairefd5a992016-07-18 10:04:07 -0700640 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700641}
642
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400643void CPWL_EditImpl::SetMultiLine(bool bMultiLine, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700644 m_pVT->SetMultiLine(bMultiLine);
645 if (bPaint)
646 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700647}
648
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400649void CPWL_EditImpl::SetAutoReturn(bool bAuto, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700650 m_pVT->SetAutoReturn(bAuto);
651 if (bPaint)
652 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700653}
654
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400655void CPWL_EditImpl::SetAutoFontSize(bool bAuto, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700656 m_pVT->SetAutoFontSize(bAuto);
657 if (bPaint)
658 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700659}
660
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400661void CPWL_EditImpl::SetFontSize(float fFontSize) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700662 m_pVT->SetFontSize(fFontSize);
dsinclairefd5a992016-07-18 10:04:07 -0700663 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700664}
665
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400666void CPWL_EditImpl::SetAutoScroll(bool bAuto, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700667 m_bEnableScroll = bAuto;
668 if (bPaint)
669 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700670}
671
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400672void CPWL_EditImpl::SetTextOverflow(bool bAllowed, bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700673 m_bEnableOverflow = bAllowed;
674 if (bPaint)
675 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700676}
677
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400678void CPWL_EditImpl::SetSelection(int32_t nStartChar, int32_t nEndChar) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700679 if (m_pVT->IsValid()) {
680 if (nStartChar == 0 && nEndChar < 0) {
681 SelectAll();
682 } else if (nStartChar < 0) {
683 SelectNone();
684 } else {
685 if (nStartChar < nEndChar) {
Diana Gage4d02e902017-07-20 17:20:31 -0700686 SetSelection(m_pVT->WordIndexToWordPlace(nStartChar),
687 m_pVT->WordIndexToWordPlace(nEndChar));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700688 } else {
Diana Gage4d02e902017-07-20 17:20:31 -0700689 SetSelection(m_pVT->WordIndexToWordPlace(nEndChar),
690 m_pVT->WordIndexToWordPlace(nStartChar));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700691 }
692 }
693 }
694}
695
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400696void CPWL_EditImpl::SetSelection(const CPVT_WordPlace& begin,
697 const CPVT_WordPlace& end) {
Tom Sepez52f69b32017-03-21 13:42:38 -0700698 if (!m_pVT->IsValid())
699 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700700
Tom Sepez52f69b32017-03-21 13:42:38 -0700701 SelectNone();
702 m_SelState.Set(begin, end);
703 SetCaret(m_SelState.EndPos);
704 ScrollToCaret();
705 if (!m_SelState.IsEmpty())
706 Refresh();
707 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700708}
709
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400710void CPWL_EditImpl::GetSelection(int32_t& nStartChar, int32_t& nEndChar) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700711 nStartChar = -1;
712 nEndChar = -1;
Tom Sepez52f69b32017-03-21 13:42:38 -0700713 if (!m_pVT->IsValid())
714 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700715
Tom Sepez52f69b32017-03-21 13:42:38 -0700716 if (m_SelState.IsEmpty()) {
717 nStartChar = m_pVT->WordPlaceToWordIndex(m_wpCaret);
718 nEndChar = m_pVT->WordPlaceToWordIndex(m_wpCaret);
719 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700720 }
Tom Sepez52f69b32017-03-21 13:42:38 -0700721 if (m_SelState.BeginPos < m_SelState.EndPos) {
722 nStartChar = m_pVT->WordPlaceToWordIndex(m_SelState.BeginPos);
723 nEndChar = m_pVT->WordPlaceToWordIndex(m_SelState.EndPos);
724 return;
725 }
726 nStartChar = m_pVT->WordPlaceToWordIndex(m_SelState.EndPos);
727 nEndChar = m_pVT->WordPlaceToWordIndex(m_SelState.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700728}
729
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400730int32_t CPWL_EditImpl::GetCaret() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700731 if (m_pVT->IsValid())
732 return m_pVT->WordPlaceToWordIndex(m_wpCaret);
733
734 return -1;
735}
736
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400737CPVT_WordPlace CPWL_EditImpl::GetCaretWordPlace() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700738 return m_wpCaret;
739}
740
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400741CFX_WideString CPWL_EditImpl::GetText() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700742 CFX_WideString swRet;
thestig821d59e2016-05-11 12:59:22 -0700743 if (!m_pVT->IsValid())
744 return swRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700745
thestig821d59e2016-05-11 12:59:22 -0700746 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
747 pIterator->SetAt(0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700748
thestig821d59e2016-05-11 12:59:22 -0700749 CPVT_Word wordinfo;
750 CPVT_WordPlace oldplace = pIterator->GetAt();
751 while (pIterator->NextWord()) {
752 CPVT_WordPlace place = pIterator->GetAt();
thestig821d59e2016-05-11 12:59:22 -0700753 if (pIterator->GetWord(wordinfo))
754 swRet += wordinfo.Word;
Tom Sepez52f69b32017-03-21 13:42:38 -0700755 if (oldplace.nSecIndex != place.nSecIndex)
thestig821d59e2016-05-11 12:59:22 -0700756 swRet += L"\r\n";
thestig821d59e2016-05-11 12:59:22 -0700757 oldplace = place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700758 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700759 return swRet;
760}
761
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400762CFX_WideString CPWL_EditImpl::GetRangeText(const CPVT_WordRange& range) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700763 CFX_WideString swRet;
thestig821d59e2016-05-11 12:59:22 -0700764 if (!m_pVT->IsValid())
765 return swRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700766
thestig821d59e2016-05-11 12:59:22 -0700767 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
768 CPVT_WordRange wrTemp = range;
769 m_pVT->UpdateWordPlace(wrTemp.BeginPos);
770 m_pVT->UpdateWordPlace(wrTemp.EndPos);
771 pIterator->SetAt(wrTemp.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700772
thestig821d59e2016-05-11 12:59:22 -0700773 CPVT_Word wordinfo;
774 CPVT_WordPlace oldplace = wrTemp.BeginPos;
775 while (pIterator->NextWord()) {
776 CPVT_WordPlace place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -0700777 if (place > wrTemp.EndPos)
thestig821d59e2016-05-11 12:59:22 -0700778 break;
thestig821d59e2016-05-11 12:59:22 -0700779 if (pIterator->GetWord(wordinfo))
780 swRet += wordinfo.Word;
Tom Sepez52f69b32017-03-21 13:42:38 -0700781 if (oldplace.nSecIndex != place.nSecIndex)
thestig821d59e2016-05-11 12:59:22 -0700782 swRet += L"\r\n";
thestig821d59e2016-05-11 12:59:22 -0700783 oldplace = place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700784 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700785 return swRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700786}
787
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400788CFX_WideString CPWL_EditImpl::GetSelectedText() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700789 return GetRangeText(m_SelState.ConvertToWordRange());
790}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700791
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400792int32_t CPWL_EditImpl::GetTotalLines() const {
thestig821d59e2016-05-11 12:59:22 -0700793 int32_t nLines = 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700794
thestig821d59e2016-05-11 12:59:22 -0700795 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
796 pIterator->SetAt(0);
797 while (pIterator->NextLine())
798 ++nLines;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700799
thestig821d59e2016-05-11 12:59:22 -0700800 return nLines;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700801}
802
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400803CPVT_WordRange CPWL_EditImpl::GetSelectWordRange() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700804 return m_SelState.ConvertToWordRange();
805}
806
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400807void CPWL_EditImpl::SetText(const CFX_WideString& sText) {
dsinclairefd5a992016-07-18 10:04:07 -0700808 Empty();
Dan Sinclairf51a02a2017-04-19 12:46:53 -0400809 DoInsertText(CPVT_WordPlace(0, 0, -1), sText, FX_CHARSET_Default);
dsinclairefd5a992016-07-18 10:04:07 -0700810 Paint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700811}
812
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400813bool CPWL_EditImpl::InsertWord(uint16_t word, int32_t charset) {
tsepez4cf55152016-11-02 14:37:54 -0700814 return InsertWord(word, charset, nullptr, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700815}
816
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400817bool CPWL_EditImpl::InsertReturn() {
tsepez4cf55152016-11-02 14:37:54 -0700818 return InsertReturn(nullptr, nullptr, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700819}
820
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400821bool CPWL_EditImpl::Backspace() {
tsepez4cf55152016-11-02 14:37:54 -0700822 return Backspace(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700823}
824
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400825bool CPWL_EditImpl::Delete() {
tsepez4cf55152016-11-02 14:37:54 -0700826 return Delete(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700827}
828
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400829bool CPWL_EditImpl::ClearSelection() {
tsepez4cf55152016-11-02 14:37:54 -0700830 return Clear(true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700831}
832
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400833bool CPWL_EditImpl::InsertText(const CFX_WideString& sText, int32_t charset) {
tsepez4cf55152016-11-02 14:37:54 -0700834 return InsertText(sText, charset, true, true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700835}
836
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400837float CPWL_EditImpl::GetFontSize() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700838 return m_pVT->GetFontSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700839}
840
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400841uint16_t CPWL_EditImpl::GetPasswordChar() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700842 return m_pVT->GetPasswordChar();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700843}
844
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400845int32_t CPWL_EditImpl::GetCharArray() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700846 return m_pVT->GetCharArray();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700847}
848
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400849CFX_FloatRect CPWL_EditImpl::GetContentRect() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700850 return VTToEdit(m_pVT->GetContentRect());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700851}
852
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400853int32_t CPWL_EditImpl::GetHorzScale() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700854 return m_pVT->GetHorzScale();
855}
856
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400857float CPWL_EditImpl::GetCharSpace() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700858 return m_pVT->GetCharSpace();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700859}
860
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400861CPVT_WordRange CPWL_EditImpl::GetWholeWordRange() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700862 if (m_pVT->IsValid())
863 return CPVT_WordRange(m_pVT->GetBeginWordPlace(), m_pVT->GetEndWordPlace());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700864
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700865 return CPVT_WordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700866}
867
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400868CPVT_WordRange CPWL_EditImpl::GetVisibleWordRange() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700869 if (m_bEnableOverflow)
870 return GetWholeWordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700871
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700872 if (m_pVT->IsValid()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800873 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700874
Dan Sinclairf528eee2017-02-14 11:52:07 -0500875 CPVT_WordPlace place1 =
876 m_pVT->SearchWordPlace(EditToVT(CFX_PointF(rcPlate.left, rcPlate.top)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700877 CPVT_WordPlace place2 = m_pVT->SearchWordPlace(
Dan Sinclairf528eee2017-02-14 11:52:07 -0500878 EditToVT(CFX_PointF(rcPlate.right, rcPlate.bottom)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700879
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700880 return CPVT_WordRange(place1, place2);
881 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700882
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700883 return CPVT_WordRange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700884}
885
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400886CPVT_WordPlace CPWL_EditImpl::SearchWordPlace(const CFX_PointF& point) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700887 if (m_pVT->IsValid()) {
888 return m_pVT->SearchWordPlace(EditToVT(point));
889 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700890
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700891 return CPVT_WordPlace();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700892}
893
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400894void CPWL_EditImpl::Paint() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700895 if (m_pVT->IsValid()) {
896 RearrangeAll();
897 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -0700898 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700899 SetCaretOrigin();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700900 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700901 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700902}
903
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400904void CPWL_EditImpl::RearrangeAll() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700905 if (m_pVT->IsValid()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700906 m_pVT->UpdateWordPlace(m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700907 m_pVT->RearrangeAll();
908 m_pVT->UpdateWordPlace(m_wpCaret);
909 SetScrollInfo();
910 SetContentChanged();
911 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700912}
913
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400914void CPWL_EditImpl::RearrangePart(const CPVT_WordRange& range) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700915 if (m_pVT->IsValid()) {
916 m_pVT->UpdateWordPlace(m_wpCaret);
917 m_pVT->RearrangePart(range);
918 m_pVT->UpdateWordPlace(m_wpCaret);
919 SetScrollInfo();
920 SetContentChanged();
921 }
922}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700923
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400924void CPWL_EditImpl::SetContentChanged() {
dsinclaira2919b32016-07-13 10:55:48 -0700925 if (m_pNotify) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800926 CFX_FloatRect rcContent = m_pVT->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700927 if (rcContent.Width() != m_rcOldContent.Width() ||
928 rcContent.Height() != m_rcOldContent.Height()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700929 m_rcOldContent = rcContent;
930 }
931 }
932}
933
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400934void CPWL_EditImpl::SelectAll() {
Tom Sepez52f69b32017-03-21 13:42:38 -0700935 if (!m_pVT->IsValid())
936 return;
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400937 m_SelState = CPWL_EditImpl_Select(GetWholeWordRange());
Tom Sepez52f69b32017-03-21 13:42:38 -0700938 SetCaret(m_SelState.EndPos);
939 ScrollToCaret();
940 Refresh();
941 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700942}
943
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400944void CPWL_EditImpl::SelectNone() {
Tom Sepez52f69b32017-03-21 13:42:38 -0700945 if (!m_pVT->IsValid() || m_SelState.IsEmpty())
946 return;
947
948 m_SelState.Reset();
949 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700950}
951
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400952bool CPWL_EditImpl::IsSelected() const {
Tom Sepez52f69b32017-03-21 13:42:38 -0700953 return !m_SelState.IsEmpty();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700954}
955
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400956CFX_PointF CPWL_EditImpl::VTToEdit(const CFX_PointF& point) const {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800957 CFX_FloatRect rcContent = m_pVT->GetContentRect();
958 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700959
Dan Sinclair05df0752017-03-14 14:43:42 -0400960 float fPadding = 0.0f;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700961
962 switch (m_nAlignment) {
963 case 0:
964 fPadding = 0.0f;
965 break;
966 case 1:
967 fPadding = (rcPlate.Height() - rcContent.Height()) * 0.5f;
968 break;
969 case 2:
970 fPadding = rcPlate.Height() - rcContent.Height();
971 break;
972 }
973
Dan Sinclairf528eee2017-02-14 11:52:07 -0500974 return CFX_PointF(point.x - (m_ptScrollPos.x - rcPlate.left),
975 point.y - (m_ptScrollPos.y + fPadding - rcPlate.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700976}
977
Dan Sinclair6b0158f2017-07-24 09:42:55 -0400978CFX_PointF CPWL_EditImpl::EditToVT(const CFX_PointF& point) const {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800979 CFX_FloatRect rcContent = m_pVT->GetContentRect();
980 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700981
Dan Sinclair05df0752017-03-14 14:43:42 -0400982 float fPadding = 0.0f;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700983
984 switch (m_nAlignment) {
985 case 0:
986 fPadding = 0.0f;
987 break;
988 case 1:
989 fPadding = (rcPlate.Height() - rcContent.Height()) * 0.5f;
990 break;
991 case 2:
992 fPadding = rcPlate.Height() - rcContent.Height();
993 break;
994 }
995
Dan Sinclairf528eee2017-02-14 11:52:07 -0500996 return CFX_PointF(point.x + (m_ptScrollPos.x - rcPlate.left),
997 point.y + (m_ptScrollPos.y + fPadding - rcPlate.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700998}
999
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001000CFX_FloatRect CPWL_EditImpl::VTToEdit(const CFX_FloatRect& rect) const {
Dan Sinclairf528eee2017-02-14 11:52:07 -05001001 CFX_PointF ptLeftBottom = VTToEdit(CFX_PointF(rect.left, rect.bottom));
1002 CFX_PointF ptRightTop = VTToEdit(CFX_PointF(rect.right, rect.top));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001003
Tom Sepez281a9ea2016-02-26 14:24:28 -08001004 return CFX_FloatRect(ptLeftBottom.x, ptLeftBottom.y, ptRightTop.x,
1005 ptRightTop.y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001006}
1007
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001008void CPWL_EditImpl::SetScrollInfo() {
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001009 if (!m_pNotify)
1010 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001011
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001012 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
1013 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1014 if (m_bNotifyFlag)
1015 return;
1016
1017 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
1018 m_bNotifyFlag = true;
1019
1020 PWL_SCROLL_INFO Info;
1021 Info.fPlateWidth = rcPlate.top - rcPlate.bottom;
1022 Info.fContentMin = rcContent.bottom;
1023 Info.fContentMax = rcContent.top;
1024 Info.fSmallStep = rcPlate.Height() / 3;
1025 Info.fBigStep = rcPlate.Height();
1026 m_pNotify->SetScrollInfo(Info);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001027}
1028
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001029void CPWL_EditImpl::SetScrollPosX(float fx) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001030 if (!m_bEnableScroll)
1031 return;
1032
1033 if (m_pVT->IsValid()) {
dsinclair448c4332016-08-02 12:07:35 -07001034 if (!IsFloatEqual(m_ptScrollPos.x, fx)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001035 m_ptScrollPos.x = fx;
dsinclairefd5a992016-07-18 10:04:07 -07001036 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001037 }
1038 }
1039}
1040
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001041void CPWL_EditImpl::SetScrollPosY(float fy) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001042 if (!m_bEnableScroll)
1043 return;
1044
1045 if (m_pVT->IsValid()) {
dsinclair448c4332016-08-02 12:07:35 -07001046 if (!IsFloatEqual(m_ptScrollPos.y, fy)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001047 m_ptScrollPos.y = fy;
dsinclairefd5a992016-07-18 10:04:07 -07001048 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001049
dsinclaira2919b32016-07-13 10:55:48 -07001050 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001051 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001052 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001053 m_bNotifyFlag = true;
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001054 m_pNotify->SetScrollPosition(fy);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001055 }
1056 }
1057 }
1058 }
1059}
1060
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001061void CPWL_EditImpl::SetScrollPos(const CFX_PointF& point) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001062 SetScrollPosX(point.x);
1063 SetScrollPosY(point.y);
1064 SetScrollLimit();
1065 SetCaretInfo();
1066}
1067
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001068CFX_PointF CPWL_EditImpl::GetScrollPos() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001069 return m_ptScrollPos;
1070}
1071
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001072void CPWL_EditImpl::SetScrollLimit() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001073 if (m_pVT->IsValid()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001074 CFX_FloatRect rcContent = m_pVT->GetContentRect();
1075 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001076
1077 if (rcPlate.Width() > rcContent.Width()) {
1078 SetScrollPosX(rcPlate.left);
1079 } else {
dsinclair448c4332016-08-02 12:07:35 -07001080 if (IsFloatSmaller(m_ptScrollPos.x, rcContent.left)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001081 SetScrollPosX(rcContent.left);
dsinclair448c4332016-08-02 12:07:35 -07001082 } else if (IsFloatBigger(m_ptScrollPos.x,
1083 rcContent.right - rcPlate.Width())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001084 SetScrollPosX(rcContent.right - rcPlate.Width());
1085 }
1086 }
1087
1088 if (rcPlate.Height() > rcContent.Height()) {
1089 SetScrollPosY(rcPlate.top);
1090 } else {
dsinclair448c4332016-08-02 12:07:35 -07001091 if (IsFloatSmaller(m_ptScrollPos.y,
1092 rcContent.bottom + rcPlate.Height())) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001093 SetScrollPosY(rcContent.bottom + rcPlate.Height());
dsinclair448c4332016-08-02 12:07:35 -07001094 } else if (IsFloatBigger(m_ptScrollPos.y, rcContent.top)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001095 SetScrollPosY(rcContent.top);
1096 }
1097 }
1098 }
1099}
1100
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001101void CPWL_EditImpl::ScrollToCaret() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001102 SetScrollLimit();
1103
thestig821d59e2016-05-11 12:59:22 -07001104 if (!m_pVT->IsValid())
1105 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001106
thestig821d59e2016-05-11 12:59:22 -07001107 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1108 pIterator->SetAt(m_wpCaret);
1109
Dan Sinclairf528eee2017-02-14 11:52:07 -05001110 CFX_PointF ptHead;
1111 CFX_PointF ptFoot;
thestig821d59e2016-05-11 12:59:22 -07001112 CPVT_Word word;
1113 CPVT_Line line;
1114 if (pIterator->GetWord(word)) {
1115 ptHead.x = word.ptWord.x + word.fWidth;
1116 ptHead.y = word.ptWord.y + word.fAscent;
1117 ptFoot.x = word.ptWord.x + word.fWidth;
1118 ptFoot.y = word.ptWord.y + word.fDescent;
1119 } else if (pIterator->GetLine(line)) {
1120 ptHead.x = line.ptLine.x;
1121 ptHead.y = line.ptLine.y + line.fLineAscent;
1122 ptFoot.x = line.ptLine.x;
1123 ptFoot.y = line.ptLine.y + line.fLineDescent;
1124 }
1125
Dan Sinclairf528eee2017-02-14 11:52:07 -05001126 CFX_PointF ptHeadEdit = VTToEdit(ptHead);
1127 CFX_PointF ptFootEdit = VTToEdit(ptFoot);
thestig821d59e2016-05-11 12:59:22 -07001128 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
dsinclair448c4332016-08-02 12:07:35 -07001129 if (!IsFloatEqual(rcPlate.left, rcPlate.right)) {
1130 if (IsFloatSmaller(ptHeadEdit.x, rcPlate.left) ||
1131 IsFloatEqual(ptHeadEdit.x, rcPlate.left)) {
thestig821d59e2016-05-11 12:59:22 -07001132 SetScrollPosX(ptHead.x);
dsinclair448c4332016-08-02 12:07:35 -07001133 } else if (IsFloatBigger(ptHeadEdit.x, rcPlate.right)) {
thestig821d59e2016-05-11 12:59:22 -07001134 SetScrollPosX(ptHead.x - rcPlate.Width());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001135 }
thestig821d59e2016-05-11 12:59:22 -07001136 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001137
dsinclair448c4332016-08-02 12:07:35 -07001138 if (!IsFloatEqual(rcPlate.top, rcPlate.bottom)) {
1139 if (IsFloatSmaller(ptFootEdit.y, rcPlate.bottom) ||
1140 IsFloatEqual(ptFootEdit.y, rcPlate.bottom)) {
1141 if (IsFloatSmaller(ptHeadEdit.y, rcPlate.top)) {
thestig821d59e2016-05-11 12:59:22 -07001142 SetScrollPosY(ptFoot.y + rcPlate.Height());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001143 }
dsinclair448c4332016-08-02 12:07:35 -07001144 } else if (IsFloatBigger(ptHeadEdit.y, rcPlate.top)) {
1145 if (IsFloatBigger(ptFootEdit.y, rcPlate.bottom)) {
thestig821d59e2016-05-11 12:59:22 -07001146 SetScrollPosY(ptHead.y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001147 }
1148 }
1149 }
1150}
1151
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001152void CPWL_EditImpl::Refresh() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001153 if (m_bEnableRefresh && m_pVT->IsValid()) {
1154 m_Refresh.BeginRefresh();
1155 RefreshPushLineRects(GetVisibleWordRange());
1156
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001157 m_Refresh.NoAnalyse();
1158 m_ptRefreshScrollPos = m_ptScrollPos;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001159
dsinclaira2919b32016-07-13 10:55:48 -07001160 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001161 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001162 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001163 m_bNotifyFlag = true;
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001164 if (const CPWL_EditImpl_RectArray* pRects =
1165 m_Refresh.GetRefreshRects()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001166 for (int32_t i = 0, sz = pRects->GetSize(); i < sz; i++)
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001167 m_pNotify->InvalidateRect(pRects->GetAt(i));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001168 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001169 }
1170 }
1171
1172 m_Refresh.EndRefresh();
1173 }
1174}
1175
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001176void CPWL_EditImpl::RefreshPushLineRects(const CPVT_WordRange& wr) {
thestig821d59e2016-05-11 12:59:22 -07001177 if (!m_pVT->IsValid())
1178 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001179
thestig821d59e2016-05-11 12:59:22 -07001180 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1181 CPVT_WordPlace wpBegin = wr.BeginPos;
1182 m_pVT->UpdateWordPlace(wpBegin);
1183 CPVT_WordPlace wpEnd = wr.EndPos;
1184 m_pVT->UpdateWordPlace(wpEnd);
1185 pIterator->SetAt(wpBegin);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001186
thestig821d59e2016-05-11 12:59:22 -07001187 CPVT_Line lineinfo;
1188 do {
1189 if (!pIterator->GetLine(lineinfo))
1190 break;
1191 if (lineinfo.lineplace.LineCmp(wpEnd) > 0)
1192 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001193
thestig821d59e2016-05-11 12:59:22 -07001194 CFX_FloatRect rcLine(lineinfo.ptLine.x,
1195 lineinfo.ptLine.y + lineinfo.fLineDescent,
1196 lineinfo.ptLine.x + lineinfo.fLineWidth,
1197 lineinfo.ptLine.y + lineinfo.fLineAscent);
1198
1199 m_Refresh.Push(CPVT_WordRange(lineinfo.lineplace, lineinfo.lineEnd),
1200 VTToEdit(rcLine));
1201 } while (pIterator->NextLine());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001202}
1203
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001204void CPWL_EditImpl::RefreshWordRange(const CPVT_WordRange& wr) {
thestig821d59e2016-05-11 12:59:22 -07001205 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1206 CPVT_WordRange wrTemp = wr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001207
thestig821d59e2016-05-11 12:59:22 -07001208 m_pVT->UpdateWordPlace(wrTemp.BeginPos);
1209 m_pVT->UpdateWordPlace(wrTemp.EndPos);
1210 pIterator->SetAt(wrTemp.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001211
thestig821d59e2016-05-11 12:59:22 -07001212 CPVT_Word wordinfo;
1213 CPVT_Line lineinfo;
1214 CPVT_WordPlace place;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001215
thestig821d59e2016-05-11 12:59:22 -07001216 while (pIterator->NextWord()) {
1217 place = pIterator->GetAt();
Tom Sepez52f69b32017-03-21 13:42:38 -07001218 if (place > wrTemp.EndPos)
thestig821d59e2016-05-11 12:59:22 -07001219 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001220
thestig821d59e2016-05-11 12:59:22 -07001221 pIterator->GetWord(wordinfo);
1222 pIterator->GetLine(lineinfo);
thestig821d59e2016-05-11 12:59:22 -07001223 if (place.LineCmp(wrTemp.BeginPos) == 0 ||
1224 place.LineCmp(wrTemp.EndPos) == 0) {
1225 CFX_FloatRect rcWord(wordinfo.ptWord.x,
1226 lineinfo.ptLine.y + lineinfo.fLineDescent,
1227 wordinfo.ptWord.x + wordinfo.fWidth,
1228 lineinfo.ptLine.y + lineinfo.fLineAscent);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001229
dsinclaira2919b32016-07-13 10:55:48 -07001230 if (m_pNotify) {
thestig821d59e2016-05-11 12:59:22 -07001231 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001232 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001233 m_bNotifyFlag = true;
thestig821d59e2016-05-11 12:59:22 -07001234 CFX_FloatRect rcRefresh = VTToEdit(rcWord);
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001235 m_pNotify->InvalidateRect(&rcRefresh);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001236 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001237 }
thestig821d59e2016-05-11 12:59:22 -07001238 } else {
1239 CFX_FloatRect rcLine(lineinfo.ptLine.x,
1240 lineinfo.ptLine.y + lineinfo.fLineDescent,
1241 lineinfo.ptLine.x + lineinfo.fLineWidth,
1242 lineinfo.ptLine.y + lineinfo.fLineAscent);
1243
dsinclaira2919b32016-07-13 10:55:48 -07001244 if (m_pNotify) {
thestig821d59e2016-05-11 12:59:22 -07001245 if (!m_bNotifyFlag) {
Lei Zhanga8c2b912017-03-22 17:41:02 -07001246 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001247 m_bNotifyFlag = true;
thestig821d59e2016-05-11 12:59:22 -07001248 CFX_FloatRect rcRefresh = VTToEdit(rcLine);
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001249 m_pNotify->InvalidateRect(&rcRefresh);
thestig821d59e2016-05-11 12:59:22 -07001250 }
1251 }
1252
1253 pIterator->NextLine();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001254 }
1255 }
1256}
1257
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001258void CPWL_EditImpl::SetCaret(const CPVT_WordPlace& place) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001259 m_wpOldCaret = m_wpCaret;
1260 m_wpCaret = place;
1261}
1262
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001263void CPWL_EditImpl::SetCaretInfo() {
dsinclaira2919b32016-07-13 10:55:48 -07001264 if (m_pNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001265 if (!m_bNotifyFlag) {
thestig821d59e2016-05-11 12:59:22 -07001266 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1267 pIterator->SetAt(m_wpCaret);
tsepez63f545c2016-09-13 16:08:49 -07001268
Dan Sinclairf528eee2017-02-14 11:52:07 -05001269 CFX_PointF ptHead;
1270 CFX_PointF ptFoot;
thestig821d59e2016-05-11 12:59:22 -07001271 CPVT_Word word;
1272 CPVT_Line line;
1273 if (pIterator->GetWord(word)) {
1274 ptHead.x = word.ptWord.x + word.fWidth;
1275 ptHead.y = word.ptWord.y + word.fAscent;
1276 ptFoot.x = word.ptWord.x + word.fWidth;
1277 ptFoot.y = word.ptWord.y + word.fDescent;
1278 } else if (pIterator->GetLine(line)) {
1279 ptHead.x = line.ptLine.x;
1280 ptHead.y = line.ptLine.y + line.fLineAscent;
1281 ptFoot.x = line.ptLine.x;
1282 ptFoot.y = line.ptLine.y + line.fLineDescent;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001283 }
1284
Lei Zhanga8c2b912017-03-22 17:41:02 -07001285 CFX_AutoRestorer<bool> restorer(&m_bNotifyFlag);
tsepez4cf55152016-11-02 14:37:54 -07001286 m_bNotifyFlag = true;
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -04001287 m_pNotify->SetCaret(m_SelState.IsEmpty(), VTToEdit(ptHead),
1288 VTToEdit(ptFoot));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001289 }
1290 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001291}
1292
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001293void CPWL_EditImpl::OnMouseDown(const CFX_PointF& point,
1294 bool bShift,
1295 bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001296 if (!m_pVT->IsValid())
1297 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001298
Tom Sepez52f69b32017-03-21 13:42:38 -07001299 SelectNone();
1300 SetCaret(m_pVT->SearchWordPlace(EditToVT(point)));
1301 m_SelState.Set(m_wpCaret, m_wpCaret);
1302 ScrollToCaret();
1303 SetCaretOrigin();
1304 SetCaretInfo();
1305}
1306
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001307void CPWL_EditImpl::OnMouseMove(const CFX_PointF& point,
1308 bool bShift,
1309 bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001310 if (!m_pVT->IsValid())
1311 return;
1312
1313 SetCaret(m_pVT->SearchWordPlace(EditToVT(point)));
1314 if (m_wpCaret == m_wpOldCaret)
1315 return;
1316
1317 m_SelState.SetEndPos(m_wpCaret);
1318 ScrollToCaret();
1319 Refresh();
1320 SetCaretOrigin();
1321 SetCaretInfo();
1322}
1323
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001324void CPWL_EditImpl::OnVK_UP(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001325 if (!m_pVT->IsValid())
1326 return;
1327
1328 SetCaret(m_pVT->GetUpWordPlace(m_wpCaret, m_ptCaret));
1329 if (bShift) {
1330 if (m_SelState.IsEmpty())
1331 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1332 else
1333 m_SelState.SetEndPos(m_wpCaret);
1334
1335 if (m_wpOldCaret != m_wpCaret) {
1336 ScrollToCaret();
1337 Refresh();
1338 SetCaretInfo();
1339 }
1340 } else {
1341 SelectNone();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001342 ScrollToCaret();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001343 SetCaretInfo();
1344 }
1345}
1346
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001347void CPWL_EditImpl::OnVK_DOWN(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001348 if (!m_pVT->IsValid())
1349 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001350
Tom Sepez52f69b32017-03-21 13:42:38 -07001351 SetCaret(m_pVT->GetDownWordPlace(m_wpCaret, m_ptCaret));
1352 if (bShift) {
1353 if (m_SelState.IsEmpty())
1354 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1355 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001356 m_SelState.SetEndPos(m_wpCaret);
1357
Tom Sepez52f69b32017-03-21 13:42:38 -07001358 if (m_wpOldCaret != m_wpCaret) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001359 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001360 Refresh();
Tom Sepez52f69b32017-03-21 13:42:38 -07001361 SetCaretInfo();
1362 }
1363 } else {
1364 SelectNone();
1365 ScrollToCaret();
1366 SetCaretInfo();
1367 }
1368}
1369
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001370void CPWL_EditImpl::OnVK_LEFT(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001371 if (!m_pVT->IsValid())
1372 return;
1373
1374 if (bShift) {
1375 if (m_wpCaret == m_pVT->GetLineBeginPlace(m_wpCaret) &&
1376 m_wpCaret != m_pVT->GetSectionBeginPlace(m_wpCaret)) {
1377 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1378 }
1379 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1380 if (m_SelState.IsEmpty())
1381 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1382 else
1383 m_SelState.SetEndPos(m_wpCaret);
1384
1385 if (m_wpOldCaret != m_wpCaret) {
1386 ScrollToCaret();
1387 Refresh();
1388 SetCaretInfo();
1389 }
1390 } else {
1391 if (!m_SelState.IsEmpty()) {
1392 if (m_SelState.BeginPos < m_SelState.EndPos)
1393 SetCaret(m_SelState.BeginPos);
1394 else
1395 SetCaret(m_SelState.EndPos);
1396
1397 SelectNone();
1398 ScrollToCaret();
1399 SetCaretInfo();
1400 } else {
1401 if (m_wpCaret == m_pVT->GetLineBeginPlace(m_wpCaret) &&
1402 m_wpCaret != m_pVT->GetSectionBeginPlace(m_wpCaret)) {
1403 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1404 }
1405 SetCaret(m_pVT->GetPrevWordPlace(m_wpCaret));
1406 ScrollToCaret();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001407 SetCaretOrigin();
1408 SetCaretInfo();
1409 }
1410 }
1411}
1412
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001413void CPWL_EditImpl::OnVK_RIGHT(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001414 if (!m_pVT->IsValid())
1415 return;
1416
1417 if (bShift) {
1418 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1419 if (m_wpCaret == m_pVT->GetLineEndPlace(m_wpCaret) &&
1420 m_wpCaret != m_pVT->GetSectionEndPlace(m_wpCaret))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001421 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1422
Tom Sepez52f69b32017-03-21 13:42:38 -07001423 if (m_SelState.IsEmpty())
1424 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1425 else
1426 m_SelState.SetEndPos(m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001427
Tom Sepez52f69b32017-03-21 13:42:38 -07001428 if (m_wpOldCaret != m_wpCaret) {
1429 ScrollToCaret();
1430 Refresh();
1431 SetCaretInfo();
1432 }
1433 } else {
1434 if (!m_SelState.IsEmpty()) {
1435 if (m_SelState.BeginPos > m_SelState.EndPos)
1436 SetCaret(m_SelState.BeginPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001437 else
Tom Sepez52f69b32017-03-21 13:42:38 -07001438 SetCaret(m_SelState.EndPos);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001439
Tom Sepez52f69b32017-03-21 13:42:38 -07001440 SelectNone();
1441 ScrollToCaret();
1442 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001443 } else {
Tom Sepez52f69b32017-03-21 13:42:38 -07001444 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
1445 if (m_wpCaret == m_pVT->GetLineEndPlace(m_wpCaret) &&
1446 m_wpCaret != m_pVT->GetSectionEndPlace(m_wpCaret)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001447 SetCaret(m_pVT->GetNextWordPlace(m_wpCaret));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001448 }
Tom Sepez52f69b32017-03-21 13:42:38 -07001449 ScrollToCaret();
1450 SetCaretOrigin();
1451 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001452 }
1453 }
1454}
1455
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001456void CPWL_EditImpl::OnVK_HOME(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001457 if (!m_pVT->IsValid())
1458 return;
1459
1460 if (bShift) {
1461 if (bCtrl)
1462 SetCaret(m_pVT->GetBeginWordPlace());
1463 else
1464 SetCaret(m_pVT->GetLineBeginPlace(m_wpCaret));
1465
1466 if (m_SelState.IsEmpty())
1467 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1468 else
1469 m_SelState.SetEndPos(m_wpCaret);
1470
1471 ScrollToCaret();
1472 Refresh();
1473 SetCaretInfo();
1474 } else {
1475 if (!m_SelState.IsEmpty()) {
1476 SetCaret(std::min(m_SelState.BeginPos, m_SelState.EndPos));
1477 SelectNone();
1478 ScrollToCaret();
1479 SetCaretInfo();
1480 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001481 if (bCtrl)
1482 SetCaret(m_pVT->GetBeginWordPlace());
1483 else
1484 SetCaret(m_pVT->GetLineBeginPlace(m_wpCaret));
1485
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001486 ScrollToCaret();
Tom Sepez52f69b32017-03-21 13:42:38 -07001487 SetCaretOrigin();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001488 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001489 }
1490 }
1491}
1492
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001493void CPWL_EditImpl::OnVK_END(bool bShift, bool bCtrl) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001494 if (!m_pVT->IsValid())
1495 return;
1496
1497 if (bShift) {
1498 if (bCtrl)
1499 SetCaret(m_pVT->GetEndWordPlace());
1500 else
1501 SetCaret(m_pVT->GetLineEndPlace(m_wpCaret));
1502
1503 if (m_SelState.IsEmpty())
1504 m_SelState.Set(m_wpOldCaret, m_wpCaret);
1505 else
1506 m_SelState.SetEndPos(m_wpCaret);
1507
1508 ScrollToCaret();
1509 Refresh();
1510 SetCaretInfo();
1511 } else {
1512 if (!m_SelState.IsEmpty()) {
1513 SetCaret(std::max(m_SelState.BeginPos, m_SelState.EndPos));
1514 SelectNone();
1515 ScrollToCaret();
1516 SetCaretInfo();
1517 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001518 if (bCtrl)
1519 SetCaret(m_pVT->GetEndWordPlace());
1520 else
1521 SetCaret(m_pVT->GetLineEndPlace(m_wpCaret));
1522
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001523 ScrollToCaret();
Tom Sepez52f69b32017-03-21 13:42:38 -07001524 SetCaretOrigin();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001525 SetCaretInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001526 }
1527 }
1528}
1529
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001530bool CPWL_EditImpl::InsertWord(uint16_t word,
1531 int32_t charset,
1532 const CPVT_WordProps* pWordProps,
1533 bool bAddUndo,
1534 bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001535 if (IsTextOverflow() || !m_pVT->IsValid())
tsepez4cf55152016-11-02 14:37:54 -07001536 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001537
Tom Sepez3509d162017-01-30 13:22:02 -08001538 m_pVT->UpdateWordPlace(m_wpCaret);
1539 SetCaret(m_pVT->InsertWord(m_wpCaret, word,
1540 GetCharSetFromUnicode(word, charset), pWordProps));
1541 m_SelState.Set(m_wpCaret, m_wpCaret);
1542 if (m_wpCaret == m_wpOldCaret)
1543 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001544
Tom Sepez3509d162017-01-30 13:22:02 -08001545 if (bAddUndo && m_bEnableUndo) {
1546 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertWord>(
1547 this, m_wpOldCaret, m_wpCaret, word, charset, pWordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001548 }
Tom Sepez3509d162017-01-30 13:22:02 -08001549 if (bPaint)
1550 PaintInsertText(m_wpOldCaret, m_wpCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001551
Lei Zhang5688d622017-08-12 07:04:19 -07001552 if (m_pOperationNotify)
1553 m_pOperationNotify->OnInsertWord(m_wpCaret, m_wpOldCaret);
Tom Sepez3509d162017-01-30 13:22:02 -08001554
1555 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001556}
1557
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001558bool CPWL_EditImpl::InsertReturn(const CPVT_SecProps* pSecProps,
1559 const CPVT_WordProps* pWordProps,
1560 bool bAddUndo,
1561 bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001562 if (IsTextOverflow() || !m_pVT->IsValid())
tsepez4cf55152016-11-02 14:37:54 -07001563 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001564
Tom Sepez3509d162017-01-30 13:22:02 -08001565 m_pVT->UpdateWordPlace(m_wpCaret);
1566 SetCaret(m_pVT->InsertSection(m_wpCaret, pSecProps, pWordProps));
1567 m_SelState.Set(m_wpCaret, m_wpCaret);
1568 if (m_wpCaret == m_wpOldCaret)
1569 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001570
Tom Sepez3509d162017-01-30 13:22:02 -08001571 if (bAddUndo && m_bEnableUndo) {
1572 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertReturn>(
1573 this, m_wpOldCaret, m_wpCaret, pSecProps, pWordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001574 }
Tom Sepez3509d162017-01-30 13:22:02 -08001575 if (bPaint) {
1576 RearrangePart(CPVT_WordRange(m_wpOldCaret, m_wpCaret));
1577 ScrollToCaret();
1578 Refresh();
1579 SetCaretOrigin();
1580 SetCaretInfo();
1581 }
Lei Zhang5688d622017-08-12 07:04:19 -07001582 if (m_pOperationNotify)
1583 m_pOperationNotify->OnInsertReturn(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001584
Tom Sepez3509d162017-01-30 13:22:02 -08001585 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001586}
1587
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001588bool CPWL_EditImpl::Backspace(bool bAddUndo, bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001589 if (!m_pVT->IsValid() || m_wpCaret == m_pVT->GetBeginWordPlace())
1590 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001591
Tom Sepez3509d162017-01-30 13:22:02 -08001592 CPVT_Section section;
1593 CPVT_Word word;
1594 if (bAddUndo) {
1595 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1596 pIterator->SetAt(m_wpCaret);
1597 pIterator->GetSection(section);
1598 pIterator->GetWord(word);
1599 }
1600 m_pVT->UpdateWordPlace(m_wpCaret);
1601 SetCaret(m_pVT->BackSpaceWord(m_wpCaret));
1602 m_SelState.Set(m_wpCaret, m_wpCaret);
1603 if (m_wpCaret == m_wpOldCaret)
1604 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001605
Tom Sepez3509d162017-01-30 13:22:02 -08001606 if (bAddUndo && m_bEnableUndo) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001607 if (m_wpCaret.nSecIndex != m_wpOldCaret.nSecIndex) {
Tom Sepez3509d162017-01-30 13:22:02 -08001608 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Backspace>(
1609 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1610 section.SecProps, section.WordProps));
1611 } else {
1612 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Backspace>(
1613 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1614 section.SecProps, word.WordProps));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001615 }
1616 }
Tom Sepez3509d162017-01-30 13:22:02 -08001617 if (bPaint) {
1618 RearrangePart(CPVT_WordRange(m_wpCaret, m_wpOldCaret));
1619 ScrollToCaret();
1620 Refresh();
1621 SetCaretOrigin();
1622 SetCaretInfo();
1623 }
Lei Zhang5688d622017-08-12 07:04:19 -07001624 if (m_pOperationNotify)
1625 m_pOperationNotify->OnBackSpace(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001626
Tom Sepez3509d162017-01-30 13:22:02 -08001627 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001628}
1629
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001630bool CPWL_EditImpl::Delete(bool bAddUndo, bool bPaint) {
Tom Sepez3509d162017-01-30 13:22:02 -08001631 if (!m_pVT->IsValid() || m_wpCaret == m_pVT->GetEndWordPlace())
1632 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001633
Tom Sepez3509d162017-01-30 13:22:02 -08001634 CPVT_Section section;
1635 CPVT_Word word;
1636 if (bAddUndo) {
1637 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1638 pIterator->SetAt(m_pVT->GetNextWordPlace(m_wpCaret));
1639 pIterator->GetSection(section);
1640 pIterator->GetWord(word);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001641 }
Tom Sepez3509d162017-01-30 13:22:02 -08001642 m_pVT->UpdateWordPlace(m_wpCaret);
1643 bool bSecEnd = (m_wpCaret == m_pVT->GetSectionEndPlace(m_wpCaret));
1644 SetCaret(m_pVT->DeleteWord(m_wpCaret));
1645 m_SelState.Set(m_wpCaret, m_wpCaret);
1646 if (bAddUndo && m_bEnableUndo) {
1647 if (bSecEnd) {
1648 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Delete>(
1649 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1650 section.SecProps, section.WordProps, bSecEnd));
1651 } else {
1652 AddEditUndoItem(pdfium::MakeUnique<CFXEU_Delete>(
1653 this, m_wpOldCaret, m_wpCaret, word.Word, word.nCharset,
1654 section.SecProps, word.WordProps, bSecEnd));
1655 }
1656 }
1657 if (bPaint) {
1658 RearrangePart(CPVT_WordRange(m_wpOldCaret, m_wpCaret));
1659 ScrollToCaret();
1660 Refresh();
1661 SetCaretOrigin();
1662 SetCaretInfo();
1663 }
Lei Zhang5688d622017-08-12 07:04:19 -07001664 if (m_pOperationNotify)
1665 m_pOperationNotify->OnDelete(m_wpCaret, m_wpOldCaret);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001666
Tom Sepez3509d162017-01-30 13:22:02 -08001667 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001668}
1669
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001670bool CPWL_EditImpl::Empty() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001671 if (m_pVT->IsValid()) {
1672 m_pVT->DeleteWords(GetWholeWordRange());
1673 SetCaret(m_pVT->GetBeginWordPlace());
1674
tsepez4cf55152016-11-02 14:37:54 -07001675 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001676 }
1677
tsepez4cf55152016-11-02 14:37:54 -07001678 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001679}
1680
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001681bool CPWL_EditImpl::Clear(bool bAddUndo, bool bPaint) {
Tom Sepez52f69b32017-03-21 13:42:38 -07001682 if (!m_pVT->IsValid() || m_SelState.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001683 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001684
thestig821d59e2016-05-11 12:59:22 -07001685 CPVT_WordRange range = m_SelState.ConvertToWordRange();
Diana Gage22bf7a52017-07-21 11:33:18 -07001686 if (bAddUndo && m_bEnableUndo) {
Diana Gage89e65622017-07-20 18:09:31 -07001687 AddEditUndoItem(
1688 pdfium::MakeUnique<CFXEU_Clear>(this, range, GetSelectedText()));
Diana Gage22bf7a52017-07-21 11:33:18 -07001689 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001690
thestig821d59e2016-05-11 12:59:22 -07001691 SelectNone();
1692 SetCaret(m_pVT->DeleteWords(range));
1693 m_SelState.Set(m_wpCaret, m_wpCaret);
thestig821d59e2016-05-11 12:59:22 -07001694 if (bPaint) {
1695 RearrangePart(range);
1696 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001697 Refresh();
thestig821d59e2016-05-11 12:59:22 -07001698 SetCaretOrigin();
1699 SetCaretInfo();
1700 }
Lei Zhang5688d622017-08-12 07:04:19 -07001701 if (m_pOperationNotify)
1702 m_pOperationNotify->OnClear(m_wpCaret, m_wpOldCaret);
thestig821d59e2016-05-11 12:59:22 -07001703
tsepez4cf55152016-11-02 14:37:54 -07001704 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001705}
1706
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001707bool CPWL_EditImpl::InsertText(const CFX_WideString& sText,
1708 int32_t charset,
1709 bool bAddUndo,
1710 bool bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001711 if (IsTextOverflow())
tsepez4cf55152016-11-02 14:37:54 -07001712 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001713
1714 m_pVT->UpdateWordPlace(m_wpCaret);
tsepeza31da742016-09-08 11:28:14 -07001715 SetCaret(DoInsertText(m_wpCaret, sText, charset));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001716 m_SelState.Set(m_wpCaret, m_wpCaret);
tsepeza31da742016-09-08 11:28:14 -07001717 if (m_wpCaret == m_wpOldCaret)
tsepez4cf55152016-11-02 14:37:54 -07001718 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001719
tsepeza31da742016-09-08 11:28:14 -07001720 if (bAddUndo && m_bEnableUndo) {
Tom Sepez3509d162017-01-30 13:22:02 -08001721 AddEditUndoItem(pdfium::MakeUnique<CFXEU_InsertText>(
1722 this, m_wpOldCaret, m_wpCaret, sText, charset));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001723 }
tsepeza31da742016-09-08 11:28:14 -07001724 if (bPaint)
1725 PaintInsertText(m_wpOldCaret, m_wpCaret);
1726
Lei Zhang5688d622017-08-12 07:04:19 -07001727 if (m_pOperationNotify)
1728 m_pOperationNotify->OnInsertText(m_wpCaret, m_wpOldCaret);
tsepeza31da742016-09-08 11:28:14 -07001729
tsepez4cf55152016-11-02 14:37:54 -07001730 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001731}
1732
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001733void CPWL_EditImpl::PaintInsertText(const CPVT_WordPlace& wpOld,
1734 const CPVT_WordPlace& wpNew) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001735 if (m_pVT->IsValid()) {
1736 RearrangePart(CPVT_WordRange(wpOld, wpNew));
1737 ScrollToCaret();
dsinclairefd5a992016-07-18 10:04:07 -07001738 Refresh();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001739 SetCaretOrigin();
1740 SetCaretInfo();
1741 }
1742}
1743
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001744bool CPWL_EditImpl::Redo() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001745 if (m_bEnableUndo) {
1746 if (m_Undo.CanRedo()) {
1747 m_Undo.Redo();
tsepez4cf55152016-11-02 14:37:54 -07001748 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001749 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001750 }
1751
tsepez4cf55152016-11-02 14:37:54 -07001752 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001753}
1754
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001755bool CPWL_EditImpl::Undo() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001756 if (m_bEnableUndo) {
1757 if (m_Undo.CanUndo()) {
1758 m_Undo.Undo();
tsepez4cf55152016-11-02 14:37:54 -07001759 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001760 }
1761 }
1762
tsepez4cf55152016-11-02 14:37:54 -07001763 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001764}
1765
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001766void CPWL_EditImpl::SetCaretOrigin() {
thestig821d59e2016-05-11 12:59:22 -07001767 if (!m_pVT->IsValid())
1768 return;
1769
1770 CPDF_VariableText::Iterator* pIterator = m_pVT->GetIterator();
1771 pIterator->SetAt(m_wpCaret);
1772 CPVT_Word word;
1773 CPVT_Line line;
1774 if (pIterator->GetWord(word)) {
1775 m_ptCaret.x = word.ptWord.x + word.fWidth;
1776 m_ptCaret.y = word.ptWord.y;
1777 } else if (pIterator->GetLine(line)) {
1778 m_ptCaret.x = line.ptLine.x;
1779 m_ptCaret.y = line.ptLine.y;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001780 }
1781}
1782
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001783CPVT_WordPlace CPWL_EditImpl::WordIndexToWordPlace(int32_t index) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001784 if (m_pVT->IsValid())
1785 return m_pVT->WordIndexToWordPlace(index);
1786
1787 return CPVT_WordPlace();
1788}
1789
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001790bool CPWL_EditImpl::IsTextFull() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001791 int32_t nTotalWords = m_pVT->GetTotalWords();
1792 int32_t nLimitChar = m_pVT->GetLimitChar();
1793 int32_t nCharArray = m_pVT->GetCharArray();
1794
1795 return IsTextOverflow() || (nLimitChar > 0 && nTotalWords >= nLimitChar) ||
1796 (nCharArray > 0 && nTotalWords >= nCharArray);
1797}
1798
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001799bool CPWL_EditImpl::IsTextOverflow() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001800 if (!m_bEnableScroll && !m_bEnableOverflow) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001801 CFX_FloatRect rcPlate = m_pVT->GetPlateRect();
1802 CFX_FloatRect rcContent = m_pVT->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001803
dsinclair448c4332016-08-02 12:07:35 -07001804 if (m_pVT->IsMultiLine() && GetTotalLines() > 1 &&
1805 IsFloatBigger(rcContent.Height(), rcPlate.Height())) {
tsepez4cf55152016-11-02 14:37:54 -07001806 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001807 }
1808
dsinclair448c4332016-08-02 12:07:35 -07001809 if (IsFloatBigger(rcContent.Width(), rcPlate.Width()))
tsepez4cf55152016-11-02 14:37:54 -07001810 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001811 }
1812
tsepez4cf55152016-11-02 14:37:54 -07001813 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001814}
1815
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001816bool CPWL_EditImpl::CanUndo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001817 if (m_bEnableUndo) {
1818 return m_Undo.CanUndo();
1819 }
1820
tsepez4cf55152016-11-02 14:37:54 -07001821 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001822}
1823
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001824bool CPWL_EditImpl::CanRedo() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001825 if (m_bEnableUndo) {
1826 return m_Undo.CanRedo();
1827 }
1828
tsepez4cf55152016-11-02 14:37:54 -07001829 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001830}
1831
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001832void CPWL_EditImpl::EnableRefresh(bool bRefresh) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001833 m_bEnableRefresh = bRefresh;
1834}
1835
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001836void CPWL_EditImpl::EnableUndo(bool bUndo) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001837 m_bEnableUndo = bUndo;
1838}
1839
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001840CPVT_WordPlace CPWL_EditImpl::DoInsertText(const CPVT_WordPlace& place,
1841 const CFX_WideString& sText,
1842 int32_t charset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001843 CPVT_WordPlace wp = place;
1844
1845 if (m_pVT->IsValid()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001846 for (int32_t i = 0, sz = sText.GetLength(); i < sz; i++) {
Tom Sepez62a70f92016-03-21 15:00:20 -07001847 uint16_t word = sText[i];
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001848 switch (word) {
1849 case 0x0D:
dsinclairefd5a992016-07-18 10:04:07 -07001850 wp = m_pVT->InsertSection(wp, nullptr, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001851 if (sText[i + 1] == 0x0A)
1852 i++;
1853 break;
1854 case 0x0A:
dsinclairefd5a992016-07-18 10:04:07 -07001855 wp = m_pVT->InsertSection(wp, nullptr, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001856 if (sText[i + 1] == 0x0D)
1857 i++;
1858 break;
1859 case 0x09:
1860 word = 0x20;
1861 default:
1862 wp = m_pVT->InsertWord(wp, word, GetCharSetFromUnicode(word, charset),
dsinclairefd5a992016-07-18 10:04:07 -07001863 nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001864 break;
1865 }
1866 }
1867 }
1868
1869 return wp;
1870}
1871
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001872int32_t CPWL_EditImpl::GetCharSetFromUnicode(uint16_t word,
1873 int32_t nOldCharset) {
dsinclairc7a73492016-04-05 12:01:42 -07001874 if (IPVT_FontMap* pFontMap = GetFontMap())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001875 return pFontMap->CharSetFromUnicode(word, nOldCharset);
1876 return nOldCharset;
1877}
1878
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001879void CPWL_EditImpl::AddEditUndoItem(
Lei Zhangae9c5ca2017-08-12 07:15:14 -07001880 std::unique_ptr<IFX_Edit_UndoItem> pEditUndoItem) {
Lei Zhang1a89e362017-03-23 15:27:25 -07001881 m_Undo.AddItem(std::move(pEditUndoItem));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001882}
1883
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001884CFX_ByteString CPWL_EditImpl::GetPDFWordString(int32_t nFontIndex,
1885 uint16_t Word,
1886 uint16_t SubWord) {
Dan Sinclairc08dc392017-07-24 08:57:35 -04001887 IPVT_FontMap* pFontMap = GetFontMap();
1888 CPDF_Font* pPDFFont = pFontMap->GetPDFFont(nFontIndex);
1889 if (!pPDFFont)
1890 return CFX_ByteString();
1891
1892 CFX_ByteString sWord;
1893 if (SubWord > 0) {
1894 Word = SubWord;
1895 } else {
1896 uint32_t dwCharCode = pPDFFont->IsUnicodeCompatible()
1897 ? pPDFFont->CharCodeFromUnicode(Word)
1898 : pFontMap->CharCodeFromUnicode(nFontIndex, Word);
1899 if (dwCharCode > 0) {
1900 pPDFFont->AppendChar(&sWord, dwCharCode);
1901 return sWord;
1902 }
1903 }
1904 pPDFFont->AppendChar(&sWord, Word);
1905 return sWord;
1906}
1907
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001908CPWL_EditImpl_LineRectArray::CPWL_EditImpl_LineRectArray() {}
weili625ad662016-06-15 11:21:33 -07001909
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001910CPWL_EditImpl_LineRectArray::~CPWL_EditImpl_LineRectArray() {}
weili625ad662016-06-15 11:21:33 -07001911
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001912void CPWL_EditImpl_LineRectArray::operator=(
1913 CPWL_EditImpl_LineRectArray&& that) {
Tom Sepez3509d162017-01-30 13:22:02 -08001914 m_LineRects = std::move(that.m_LineRects);
weili625ad662016-06-15 11:21:33 -07001915}
1916
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001917void CPWL_EditImpl_LineRectArray::Add(const CPVT_WordRange& wrLine,
1918 const CFX_FloatRect& rcLine) {
1919 m_LineRects.push_back(
1920 pdfium::MakeUnique<CPWL_EditImpl_LineRect>(wrLine, rcLine));
weili625ad662016-06-15 11:21:33 -07001921}
1922
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001923int32_t CPWL_EditImpl_LineRectArray::GetSize() const {
Tom Sepez3509d162017-01-30 13:22:02 -08001924 return pdfium::CollectionSize<int32_t>(m_LineRects);
weili625ad662016-06-15 11:21:33 -07001925}
1926
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001927CPWL_EditImpl_LineRect* CPWL_EditImpl_LineRectArray::GetAt(
1928 int32_t nIndex) const {
Tom Sepez3509d162017-01-30 13:22:02 -08001929 if (nIndex < 0 || nIndex >= GetSize())
weili625ad662016-06-15 11:21:33 -07001930 return nullptr;
1931
Tom Sepez3509d162017-01-30 13:22:02 -08001932 return m_LineRects[nIndex].get();
weili625ad662016-06-15 11:21:33 -07001933}
1934
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001935CPWL_EditImpl_Select::CPWL_EditImpl_Select() {}
weili625ad662016-06-15 11:21:33 -07001936
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001937CPWL_EditImpl_Select::CPWL_EditImpl_Select(const CPVT_WordRange& range) {
weili625ad662016-06-15 11:21:33 -07001938 Set(range.BeginPos, range.EndPos);
1939}
1940
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001941CPVT_WordRange CPWL_EditImpl_Select::ConvertToWordRange() const {
weili625ad662016-06-15 11:21:33 -07001942 return CPVT_WordRange(BeginPos, EndPos);
1943}
1944
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001945void CPWL_EditImpl_Select::Reset() {
Tom Sepez52f69b32017-03-21 13:42:38 -07001946 BeginPos.Reset();
1947 EndPos.Reset();
weili625ad662016-06-15 11:21:33 -07001948}
1949
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001950void CPWL_EditImpl_Select::Set(const CPVT_WordPlace& begin,
1951 const CPVT_WordPlace& end) {
weili625ad662016-06-15 11:21:33 -07001952 BeginPos = begin;
1953 EndPos = end;
1954}
1955
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001956void CPWL_EditImpl_Select::SetEndPos(const CPVT_WordPlace& end) {
weili625ad662016-06-15 11:21:33 -07001957 EndPos = end;
1958}
1959
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001960bool CPWL_EditImpl_Select::IsEmpty() const {
Tom Sepez52f69b32017-03-21 13:42:38 -07001961 return BeginPos == EndPos;
weili625ad662016-06-15 11:21:33 -07001962}
1963
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001964CPWL_EditImpl_RectArray::CPWL_EditImpl_RectArray() {}
weili625ad662016-06-15 11:21:33 -07001965
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001966CPWL_EditImpl_RectArray::~CPWL_EditImpl_RectArray() {}
weili625ad662016-06-15 11:21:33 -07001967
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001968void CPWL_EditImpl_RectArray::Clear() {
Tom Sepez3509d162017-01-30 13:22:02 -08001969 m_Rects.clear();
weili625ad662016-06-15 11:21:33 -07001970}
1971
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001972void CPWL_EditImpl_RectArray::Add(const CFX_FloatRect& rect) {
weili625ad662016-06-15 11:21:33 -07001973 // check for overlapped area
Tom Sepez3509d162017-01-30 13:22:02 -08001974 for (const auto& pRect : m_Rects) {
weili625ad662016-06-15 11:21:33 -07001975 if (pRect && pRect->Contains(rect))
1976 return;
1977 }
Tom Sepez3509d162017-01-30 13:22:02 -08001978 m_Rects.push_back(pdfium::MakeUnique<CFX_FloatRect>(rect));
weili625ad662016-06-15 11:21:33 -07001979}
1980
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001981int32_t CPWL_EditImpl_RectArray::GetSize() const {
Tom Sepez3509d162017-01-30 13:22:02 -08001982 return pdfium::CollectionSize<int32_t>(m_Rects);
weili625ad662016-06-15 11:21:33 -07001983}
1984
Dan Sinclair6b0158f2017-07-24 09:42:55 -04001985CFX_FloatRect* CPWL_EditImpl_RectArray::GetAt(int32_t nIndex) const {
Tom Sepez3509d162017-01-30 13:22:02 -08001986 if (nIndex < 0 || nIndex >= GetSize())
weili625ad662016-06-15 11:21:33 -07001987 return nullptr;
1988
Tom Sepez3509d162017-01-30 13:22:02 -08001989 return m_Rects[nIndex].get();
weili625ad662016-06-15 11:21:33 -07001990}