blob: e193d0f46909628a5639b7402c65d3910a72e441 [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 sinclair89e904b2016-03-23 19:29:15 -04007#include "fpdfsdk/pdfwindow/PWL_Edit.h"
Lei Zhangc2fb35f2016-01-05 16:46:58 -08008
Dan Sinclair3ebd1212016-03-09 09:59:23 -05009#include <vector>
10
dan sinclair61b2fc72016-03-23 19:21:44 -040011#include "core/fpdfapi/fpdf_font/include/cpdf_font.h"
dsinclairc7a73492016-04-05 12:01:42 -070012#include "core/fpdfdoc/include/cpvt_word.h"
Dan Sinclaira8a28e02016-03-23 15:41:39 -040013#include "core/fxcrt/include/fx_safe_types.h"
14#include "core/fxcrt/include/fx_xml.h"
dsinclair48baa5f2016-04-06 10:00:40 -070015#include "core/fxge/include/fx_ge.h"
dan sinclair89e904b2016-03-23 19:29:15 -040016#include "fpdfsdk/pdfwindow/PWL_Caret.h"
17#include "fpdfsdk/pdfwindow/PWL_EditCtrl.h"
18#include "fpdfsdk/pdfwindow/PWL_FontMap.h"
19#include "fpdfsdk/pdfwindow/PWL_ScrollBar.h"
20#include "fpdfsdk/pdfwindow/PWL_Utils.h"
21#include "fpdfsdk/pdfwindow/PWL_Wnd.h"
Lei Zhangc2fb35f2016-01-05 16:46:58 -080022#include "public/fpdf_fwlevent.h"
Tom Sepezab277682016-02-17 10:07:21 -080023#include "third_party/base/stl_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070024
Nico Weber9d8ec5a2015-08-04 13:00:21 -070025CPWL_Edit::CPWL_Edit()
26 : m_pFillerNotify(NULL), m_pSpellCheck(NULL), m_bFocus(FALSE) {
27 m_pFormFiller = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070028}
29
Nico Weber9d8ec5a2015-08-04 13:00:21 -070030CPWL_Edit::~CPWL_Edit() {
31 ASSERT(m_bFocus == FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070032}
33
Nico Weber9d8ec5a2015-08-04 13:00:21 -070034CFX_ByteString CPWL_Edit::GetClassName() const {
35 return PWL_CLASSNAME_EDIT;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070036}
37
Nico Weber9d8ec5a2015-08-04 13:00:21 -070038void CPWL_Edit::OnDestroy() {}
39
40void CPWL_Edit::SetText(const FX_WCHAR* csText) {
41 CFX_WideString swText = csText;
42
43 if (HasFlag(PES_RICH)) {
44 CFX_ByteString sValue = CFX_ByteString::FromUnicode(swText);
45
46 if (CXML_Element* pXML =
47 CXML_Element::Parse(sValue.c_str(), sValue.GetLength())) {
48 int32_t nCount = pXML->CountChildren();
49 FX_BOOL bFirst = TRUE;
50
51 swText.Empty();
52
53 for (int32_t i = 0; i < nCount; i++) {
54 if (CXML_Element* pSubElement = pXML->GetElement(i)) {
55 CFX_ByteString tag = pSubElement->GetTagName();
56 if (tag.EqualNoCase("p")) {
57 int nChild = pSubElement->CountChildren();
58 CFX_WideString swSection;
59 for (int32_t j = 0; j < nChild; j++) {
60 swSection += pSubElement->GetContent(j);
61 }
62
63 if (bFirst)
64 bFirst = FALSE;
65 else
66 swText += FWL_VKEY_Return;
67 swText += swSection;
68 }
69 }
70 }
71
72 delete pXML;
73 }
74 }
75
76 m_pEdit->SetText(swText.c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070077}
78
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079void CPWL_Edit::RePosChildWnd() {
80 if (CPWL_ScrollBar* pVSB = GetVScrollBar()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -080081 CFX_FloatRect rcWindow = m_rcOldWindow;
82 CFX_FloatRect rcVScroll =
83 CFX_FloatRect(rcWindow.right, rcWindow.bottom,
84 rcWindow.right + PWL_SCROLLBAR_WIDTH, rcWindow.top);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 pVSB->Move(rcVScroll, TRUE, FALSE);
86 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070087
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088 if (m_pEditCaret && !HasFlag(PES_TEXTOVERFLOW))
89 m_pEditCaret->SetClipRect(CPWL_Utils::InflateRect(
Dan Sinclair50cce602016-02-24 09:51:16 -050090 GetClientRect(), 1.0f)); // +1 for caret beside border
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070091
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092 CPWL_EditCtrl::RePosChildWnd();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070093}
94
Tom Sepez281a9ea2016-02-26 14:24:28 -080095CFX_FloatRect CPWL_Edit::GetClientRect() const {
96 CFX_FloatRect rcClient = CPWL_Utils::DeflateRect(
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097 GetWindowRect(), (FX_FLOAT)(GetBorderWidth() + GetInnerBorderWidth()));
98
99 if (CPWL_ScrollBar* pVSB = GetVScrollBar()) {
100 if (pVSB->IsVisible()) {
101 rcClient.right -= PWL_SCROLLBAR_WIDTH;
102 }
103 }
104
105 return rcClient;
106}
107
108void CPWL_Edit::SetAlignFormatH(PWL_EDIT_ALIGNFORMAT_H nFormat,
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800109 FX_BOOL bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110 m_pEdit->SetAlignmentH((int32_t)nFormat, bPaint);
111}
112
113void CPWL_Edit::SetAlignFormatV(PWL_EDIT_ALIGNFORMAT_V nFormat,
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800114 FX_BOOL bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700115 m_pEdit->SetAlignmentV((int32_t)nFormat, bPaint);
116}
117
118FX_BOOL CPWL_Edit::CanSelectAll() const {
119 return GetSelectWordRange() != m_pEdit->GetWholeWordRange();
120}
121
122FX_BOOL CPWL_Edit::CanClear() const {
123 return !IsReadOnly() && m_pEdit->IsSelected();
124}
125
126FX_BOOL CPWL_Edit::CanCopy() const {
127 return !HasFlag(PES_PASSWORD) && !HasFlag(PES_NOREAD) &&
128 m_pEdit->IsSelected();
129}
130
131FX_BOOL CPWL_Edit::CanCut() const {
132 return CanCopy() && !IsReadOnly();
133}
134
135FX_BOOL CPWL_Edit::CanPaste() const {
136 if (IsReadOnly())
137 return FALSE;
138
139 CFX_WideString swClipboard;
140 if (IFX_SystemHandler* pSH = GetSystemHandler())
141 swClipboard = pSH->GetClipboardText(GetAttachedHWnd());
142
143 return !swClipboard.IsEmpty();
144}
145
146void CPWL_Edit::CopyText() {
147 if (!CanCopy())
148 return;
149
150 CFX_WideString str = m_pEdit->GetSelText();
151
152 if (IFX_SystemHandler* pSH = GetSystemHandler())
153 pSH->SetClipboardText(GetAttachedHWnd(), str);
154}
155
156void CPWL_Edit::PasteText() {
157 if (!CanPaste())
158 return;
159
160 CFX_WideString swClipboard;
161 if (IFX_SystemHandler* pSH = GetSystemHandler())
162 swClipboard = pSH->GetClipboardText(GetAttachedHWnd());
163
164 if (m_pFillerNotify) {
165 FX_BOOL bRC = TRUE;
166 FX_BOOL bExit = FALSE;
167 CFX_WideString strChangeEx;
168 int nSelStart = 0;
169 int nSelEnd = 0;
170 GetSel(nSelStart, nSelEnd);
Lei Zhanga5b47042015-10-19 14:32:16 -0700171 m_pFillerNotify->OnBeforeKeyStroke(GetAttachedData(), swClipboard,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172 strChangeEx, nSelStart, nSelEnd, TRUE,
173 bRC, bExit, 0);
174 if (!bRC)
175 return;
176 if (bExit)
177 return;
178 }
179
180 if (swClipboard.GetLength() > 0) {
181 Clear();
182 InsertText(swClipboard.c_str());
183 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700184}
185
186void CPWL_Edit::CutText() {
187 if (!CanCut())
188 return;
189
190 CFX_WideString str = m_pEdit->GetSelText();
191
192 if (IFX_SystemHandler* pSH = GetSystemHandler())
193 pSH->SetClipboardText(GetAttachedHWnd(), str);
194
195 m_pEdit->Clear();
196}
197
198void CPWL_Edit::OnCreated() {
199 CPWL_EditCtrl::OnCreated();
200
201 if (CPWL_ScrollBar* pScroll = GetVScrollBar()) {
202 pScroll->RemoveFlag(PWS_AUTOTRANSPARENT);
203 pScroll->SetTransparency(255);
204 }
205
206 SetParamByFlag();
207
208 m_rcOldWindow = GetWindowRect();
209
210 m_pEdit->SetOprNotify(this);
211 m_pEdit->EnableOprNotify(TRUE);
212}
213
214void CPWL_Edit::SetParamByFlag() {
215 if (HasFlag(PES_RIGHT)) {
216 m_pEdit->SetAlignmentH(2, FALSE);
217 } else if (HasFlag(PES_MIDDLE)) {
218 m_pEdit->SetAlignmentH(1, FALSE);
219 } else {
220 m_pEdit->SetAlignmentH(0, FALSE);
221 }
222
223 if (HasFlag(PES_BOTTOM)) {
224 m_pEdit->SetAlignmentV(2, FALSE);
225 } else if (HasFlag(PES_CENTER)) {
226 m_pEdit->SetAlignmentV(1, FALSE);
227 } else {
228 m_pEdit->SetAlignmentV(0, FALSE);
229 }
230
231 if (HasFlag(PES_PASSWORD)) {
232 m_pEdit->SetPasswordChar('*', FALSE);
233 }
234
235 m_pEdit->SetMultiLine(HasFlag(PES_MULTILINE), FALSE);
236 m_pEdit->SetAutoReturn(HasFlag(PES_AUTORETURN), FALSE);
237 m_pEdit->SetAutoFontSize(HasFlag(PWS_AUTOFONTSIZE), FALSE);
238 m_pEdit->SetAutoScroll(HasFlag(PES_AUTOSCROLL), FALSE);
239 m_pEdit->EnableUndo(HasFlag(PES_UNDO));
240
241 if (HasFlag(PES_TEXTOVERFLOW)) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800242 SetClipRect(CFX_FloatRect(0.0f, 0.0f, 0.0f, 0.0f));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 m_pEdit->SetTextOverflow(TRUE, FALSE);
244 } else {
245 if (m_pEditCaret) {
246 m_pEditCaret->SetClipRect(CPWL_Utils::InflateRect(
Dan Sinclair50cce602016-02-24 09:51:16 -0500247 GetClientRect(), 1.0f)); // +1 for caret beside border
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248 }
249 }
250
251 if (HasFlag(PES_SPELLCHECK)) {
252 m_pSpellCheck = GetCreationParam().pSpellCheck;
253 }
254}
255
256void CPWL_Edit::GetThisAppearanceStream(CFX_ByteTextBuf& sAppStream) {
257 CPWL_Wnd::GetThisAppearanceStream(sAppStream);
258
Tom Sepez281a9ea2016-02-26 14:24:28 -0800259 CFX_FloatRect rcClient = GetClientRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260 CFX_ByteTextBuf sLine;
261
262 int32_t nCharArray = m_pEdit->GetCharArray();
263
264 if (nCharArray > 0) {
265 switch (GetBorderStyle()) {
266 case PBS_SOLID: {
267 sLine << "q\n" << GetBorderWidth() << " w\n"
268 << CPWL_Utils::GetColorAppStream(GetBorderColor(), FALSE)
tsepez28f97ff2016-04-04 16:41:35 -0700269 .AsByteStringC()
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 << " 2 J 0 j\n";
271
272 for (int32_t i = 1; i < nCharArray; i++) {
273 sLine << rcClient.left +
274 ((rcClient.right - rcClient.left) / nCharArray) * i
275 << " " << rcClient.bottom << " m\n"
276 << rcClient.left +
277 ((rcClient.right - rcClient.left) / nCharArray) * i
278 << " " << rcClient.top << " l S\n";
279 }
280
281 sLine << "Q\n";
282 } break;
283 case PBS_DASH: {
284 sLine << "q\n" << GetBorderWidth() << " w\n"
285 << CPWL_Utils::GetColorAppStream(GetBorderColor(), FALSE)
tsepez28f97ff2016-04-04 16:41:35 -0700286 .AsByteStringC()
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287 << " 2 J 0 j\n"
288 << "[" << GetBorderDash().nDash << " " << GetBorderDash().nGap
289 << "] " << GetBorderDash().nPhase << " d\n";
290
291 for (int32_t i = 1; i < nCharArray; i++) {
292 sLine << rcClient.left +
293 ((rcClient.right - rcClient.left) / nCharArray) * i
294 << " " << rcClient.bottom << " m\n"
295 << rcClient.left +
296 ((rcClient.right - rcClient.left) / nCharArray) * i
297 << " " << rcClient.top << " l S\n";
298 }
299
300 sLine << "Q\n";
301 } break;
302 }
303 }
304
305 sAppStream << sLine;
306
307 CFX_ByteTextBuf sText;
308
Tom Sepez281a9ea2016-02-26 14:24:28 -0800309 CFX_FloatPoint ptOffset = CFX_FloatPoint(0.0f, 0.0f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700310
311 CPVT_WordRange wrWhole = m_pEdit->GetWholeWordRange();
312 CPVT_WordRange wrSelect = GetSelectWordRange();
313 CPVT_WordRange wrVisible =
314 (HasFlag(PES_TEXTOVERFLOW) ? wrWhole : m_pEdit->GetVisibleWordRange());
315 CPVT_WordRange wrSelBefore(wrWhole.BeginPos, wrSelect.BeginPos);
316 CPVT_WordRange wrSelAfter(wrSelect.EndPos, wrWhole.EndPos);
317
318 CPVT_WordRange wrTemp =
319 CPWL_Utils::OverlapWordRange(GetSelectWordRange(), wrVisible);
320 CFX_ByteString sEditSel =
321 CPWL_Utils::GetEditSelAppStream(m_pEdit, ptOffset, &wrTemp);
322
323 if (sEditSel.GetLength() > 0)
324 sText << CPWL_Utils::GetColorAppStream(PWL_DEFAULT_SELBACKCOLOR)
tsepez28f97ff2016-04-04 16:41:35 -0700325 .AsByteStringC()
326 << sEditSel.AsByteStringC();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700327
328 wrTemp = CPWL_Utils::OverlapWordRange(wrVisible, wrSelBefore);
329 CFX_ByteString sEditBefore = CPWL_Utils::GetEditAppStream(
330 m_pEdit, ptOffset, &wrTemp, !HasFlag(PES_CHARARRAY),
331 m_pEdit->GetPasswordChar());
332
333 if (sEditBefore.GetLength() > 0)
tsepez28f97ff2016-04-04 16:41:35 -0700334 sText << "BT\n"
335 << CPWL_Utils::GetColorAppStream(GetTextColor()).AsByteStringC()
336 << sEditBefore.AsByteStringC() << "ET\n";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337
338 wrTemp = CPWL_Utils::OverlapWordRange(wrVisible, wrSelect);
339 CFX_ByteString sEditMid = CPWL_Utils::GetEditAppStream(
340 m_pEdit, ptOffset, &wrTemp, !HasFlag(PES_CHARARRAY),
341 m_pEdit->GetPasswordChar());
342
343 if (sEditMid.GetLength() > 0)
344 sText << "BT\n"
345 << CPWL_Utils::GetColorAppStream(CPWL_Color(COLORTYPE_GRAY, 1))
tsepez28f97ff2016-04-04 16:41:35 -0700346 .AsByteStringC()
347 << sEditMid.AsByteStringC() << "ET\n";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700348
349 wrTemp = CPWL_Utils::OverlapWordRange(wrVisible, wrSelAfter);
350 CFX_ByteString sEditAfter = CPWL_Utils::GetEditAppStream(
351 m_pEdit, ptOffset, &wrTemp, !HasFlag(PES_CHARARRAY),
352 m_pEdit->GetPasswordChar());
353
354 if (sEditAfter.GetLength() > 0)
tsepez28f97ff2016-04-04 16:41:35 -0700355 sText << "BT\n"
356 << CPWL_Utils::GetColorAppStream(GetTextColor()).AsByteStringC()
357 << sEditAfter.AsByteStringC() << "ET\n";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700358
359 if (HasFlag(PES_SPELLCHECK)) {
360 CFX_ByteString sSpellCheck = CPWL_Utils::GetSpellCheckAppStream(
361 m_pEdit, m_pSpellCheck, ptOffset, &wrVisible);
362 if (sSpellCheck.GetLength() > 0)
363 sText << CPWL_Utils::GetColorAppStream(CPWL_Color(COLORTYPE_RGB, 1, 0, 0),
364 FALSE)
tsepez28f97ff2016-04-04 16:41:35 -0700365 .AsByteStringC()
366 << sSpellCheck.AsByteStringC();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700367 }
368
369 if (sText.GetLength() > 0) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800370 CFX_FloatRect rcClient = GetClientRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700371 sAppStream << "q\n/Tx BMC\n";
372
373 if (!HasFlag(PES_TEXTOVERFLOW))
374 sAppStream << rcClient.left << " " << rcClient.bottom << " "
375 << rcClient.right - rcClient.left << " "
376 << rcClient.top - rcClient.bottom << " re W n\n";
377
378 sAppStream << sText;
379
380 sAppStream << "EMC\nQ\n";
381 }
382}
383
384void CPWL_Edit::DrawThisAppearance(CFX_RenderDevice* pDevice,
Tom Sepez60d909e2015-12-10 15:34:55 -0800385 CFX_Matrix* pUser2Device) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700386 CPWL_Wnd::DrawThisAppearance(pDevice, pUser2Device);
387
Tom Sepez281a9ea2016-02-26 14:24:28 -0800388 CFX_FloatRect rcClient = GetClientRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700389 CFX_ByteTextBuf sLine;
390
391 int32_t nCharArray = m_pEdit->GetCharArray();
392 FX_SAFE_INT32 nCharArraySafe = nCharArray;
393 nCharArraySafe -= 1;
394 nCharArraySafe *= 2;
395
396 if (nCharArray > 0 && nCharArraySafe.IsValid()) {
397 switch (GetBorderStyle()) {
398 case PBS_SOLID: {
399 CFX_GraphStateData gsd;
400 gsd.m_LineWidth = (FX_FLOAT)GetBorderWidth();
401
402 CFX_PathData path;
403 path.SetPointCount(nCharArraySafe.ValueOrDie());
404
405 for (int32_t i = 0; i < nCharArray - 1; i++) {
406 path.SetPoint(
407 i * 2,
408 rcClient.left +
409 ((rcClient.right - rcClient.left) / nCharArray) * (i + 1),
410 rcClient.bottom, FXPT_MOVETO);
411 path.SetPoint(
412 i * 2 + 1,
413 rcClient.left +
414 ((rcClient.right - rcClient.left) / nCharArray) * (i + 1),
415 rcClient.top, FXPT_LINETO);
416 }
417 if (path.GetPointCount() > 0)
418 pDevice->DrawPath(
419 &path, pUser2Device, &gsd, 0,
420 CPWL_Utils::PWLColorToFXColor(GetBorderColor(), 255),
421 FXFILL_ALTERNATE);
422 } break;
423 case PBS_DASH: {
424 CFX_GraphStateData gsd;
425 gsd.m_LineWidth = (FX_FLOAT)GetBorderWidth();
426
427 gsd.SetDashCount(2);
428 gsd.m_DashArray[0] = (FX_FLOAT)GetBorderDash().nDash;
429 gsd.m_DashArray[1] = (FX_FLOAT)GetBorderDash().nGap;
430 gsd.m_DashPhase = (FX_FLOAT)GetBorderDash().nPhase;
431
432 CFX_PathData path;
433 path.SetPointCount(nCharArraySafe.ValueOrDie());
434
435 for (int32_t i = 0; i < nCharArray - 1; i++) {
436 path.SetPoint(
437 i * 2,
438 rcClient.left +
439 ((rcClient.right - rcClient.left) / nCharArray) * (i + 1),
440 rcClient.bottom, FXPT_MOVETO);
441 path.SetPoint(
442 i * 2 + 1,
443 rcClient.left +
444 ((rcClient.right - rcClient.left) / nCharArray) * (i + 1),
445 rcClient.top, FXPT_LINETO);
446 }
447 if (path.GetPointCount() > 0)
448 pDevice->DrawPath(
449 &path, pUser2Device, &gsd, 0,
450 CPWL_Utils::PWLColorToFXColor(GetBorderColor(), 255),
451 FXFILL_ALTERNATE);
452 } break;
453 }
454 }
455
Tom Sepez281a9ea2016-02-26 14:24:28 -0800456 CFX_FloatRect rcClip;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700457 CPVT_WordRange wrRange = m_pEdit->GetVisibleWordRange();
458 CPVT_WordRange* pRange = NULL;
459
460 if (!HasFlag(PES_TEXTOVERFLOW)) {
461 rcClip = GetClientRect();
462 pRange = &wrRange;
463 }
464 IFX_SystemHandler* pSysHandler = GetSystemHandler();
465 IFX_Edit::DrawEdit(
466 pDevice, pUser2Device, m_pEdit,
467 CPWL_Utils::PWLColorToFXColor(GetTextColor(), GetTransparency()),
468 CPWL_Utils::PWLColorToFXColor(GetTextStrokeColor(), GetTransparency()),
Tom Sepez281a9ea2016-02-26 14:24:28 -0800469 rcClip, CFX_FloatPoint(0.0f, 0.0f), pRange, pSysHandler, m_pFormFiller);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700470
471 if (HasFlag(PES_SPELLCHECK)) {
472 CPWL_Utils::DrawEditSpellCheck(pDevice, pUser2Device, m_pEdit, rcClip,
Tom Sepez281a9ea2016-02-26 14:24:28 -0800473 CFX_FloatPoint(0.0f, 0.0f), pRange,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700474 GetCreationParam().pSpellCheck);
475 }
476}
477
tsepezc3255f52016-03-25 14:52:27 -0700478FX_BOOL CPWL_Edit::OnLButtonDown(const CFX_FloatPoint& point, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700479 CPWL_Wnd::OnLButtonDown(point, nFlag);
480
481 if (HasFlag(PES_TEXTOVERFLOW) || ClientHitTest(point)) {
482 if (m_bMouseDown)
483 InvalidateRect();
484
485 m_bMouseDown = TRUE;
486 SetCapture();
487
488 m_pEdit->OnMouseDown(point, IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
489 }
490
491 return TRUE;
492}
493
Tom Sepez281a9ea2016-02-26 14:24:28 -0800494FX_BOOL CPWL_Edit::OnLButtonDblClk(const CFX_FloatPoint& point,
tsepezc3255f52016-03-25 14:52:27 -0700495 uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700496 CPWL_Wnd::OnLButtonDblClk(point, nFlag);
497
498 if (HasFlag(PES_TEXTOVERFLOW) || ClientHitTest(point)) {
499 m_pEdit->SelectAll();
500 }
501
502 return TRUE;
503}
504
505#define WM_PWLEDIT_UNDO 0x01
506#define WM_PWLEDIT_REDO 0x02
507#define WM_PWLEDIT_CUT 0x03
508#define WM_PWLEDIT_COPY 0x04
509#define WM_PWLEDIT_PASTE 0x05
510#define WM_PWLEDIT_DELETE 0x06
511#define WM_PWLEDIT_SELECTALL 0x07
512#define WM_PWLEDIT_SUGGEST 0x08
513
tsepezc3255f52016-03-25 14:52:27 -0700514FX_BOOL CPWL_Edit::OnRButtonUp(const CFX_FloatPoint& point, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700515 if (m_bMouseDown)
516 return FALSE;
517
518 CPWL_Wnd::OnRButtonUp(point, nFlag);
519
520 if (!HasFlag(PES_TEXTOVERFLOW) && !ClientHitTest(point))
521 return TRUE;
522
523 IFX_SystemHandler* pSH = GetSystemHandler();
524 if (!pSH)
525 return FALSE;
526
527 SetFocus();
528
529 CPVT_WordRange wrLatin = GetLatinWordsRange(point);
530 CFX_WideString swLatin = m_pEdit->GetRangeText(wrLatin);
531
532 FX_HMENU hPopup = pSH->CreatePopupMenu();
533 if (!hPopup)
534 return FALSE;
535
Tom Sepezab277682016-02-17 10:07:21 -0800536 std::vector<CFX_ByteString> sSuggestWords;
Tom Sepez281a9ea2016-02-26 14:24:28 -0800537 CFX_FloatPoint ptPopup = point;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700538
539 if (!IsReadOnly()) {
540 if (HasFlag(PES_SPELLCHECK) && !swLatin.IsEmpty()) {
541 if (m_pSpellCheck) {
542 CFX_ByteString sLatin = CFX_ByteString::FromUnicode(swLatin);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700543 if (!m_pSpellCheck->CheckWord(sLatin)) {
544 m_pSpellCheck->SuggestWords(sLatin, sSuggestWords);
545
Tom Sepezab277682016-02-17 10:07:21 -0800546 int32_t nSuggest = pdfium::CollectionSize<int32_t>(sSuggestWords);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700547 for (int32_t nWord = 0; nWord < nSuggest; nWord++) {
548 pSH->AppendMenuItem(hPopup, WM_PWLEDIT_SUGGEST + nWord,
549 sSuggestWords[nWord].UTF8Decode());
550 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700551 if (nSuggest > 0)
552 pSH->AppendMenuItem(hPopup, 0, L"");
553
554 ptPopup = GetWordRightBottomPoint(wrLatin.EndPos);
555 }
556 }
557 }
558 }
559
560 IPWL_Provider* pProvider = GetProvider();
561
562 if (HasFlag(PES_UNDO)) {
563 pSH->AppendMenuItem(
564 hPopup, WM_PWLEDIT_UNDO,
565 pProvider ? pProvider->LoadPopupMenuString(0) : L"&Undo");
566 pSH->AppendMenuItem(
567 hPopup, WM_PWLEDIT_REDO,
568 pProvider ? pProvider->LoadPopupMenuString(1) : L"&Redo");
569 pSH->AppendMenuItem(hPopup, 0, L"");
570
571 if (!m_pEdit->CanUndo())
572 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_UNDO, FALSE);
573 if (!m_pEdit->CanRedo())
574 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_REDO, FALSE);
575 }
576
577 pSH->AppendMenuItem(hPopup, WM_PWLEDIT_CUT,
578 pProvider ? pProvider->LoadPopupMenuString(2) : L"Cu&t");
579 pSH->AppendMenuItem(hPopup, WM_PWLEDIT_COPY,
580 pProvider ? pProvider->LoadPopupMenuString(3) : L"&Copy");
581 pSH->AppendMenuItem(
582 hPopup, WM_PWLEDIT_PASTE,
583 pProvider ? pProvider->LoadPopupMenuString(4) : L"&Paste");
584 pSH->AppendMenuItem(
585 hPopup, WM_PWLEDIT_DELETE,
586 pProvider ? pProvider->LoadPopupMenuString(5) : L"&Delete");
587
588 CFX_WideString swText = pSH->GetClipboardText(GetAttachedHWnd());
589 if (swText.IsEmpty())
590 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_PASTE, FALSE);
591
592 if (!m_pEdit->IsSelected()) {
593 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_CUT, FALSE);
594 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_COPY, FALSE);
595 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_DELETE, FALSE);
596 }
597
598 if (IsReadOnly()) {
599 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_CUT, FALSE);
600 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_DELETE, FALSE);
601 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_PASTE, FALSE);
602 }
603
604 if (HasFlag(PES_PASSWORD)) {
605 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_CUT, FALSE);
606 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_COPY, FALSE);
607 }
608
609 if (HasFlag(PES_NOREAD)) {
610 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_CUT, FALSE);
611 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_COPY, FALSE);
612 }
613
614 pSH->AppendMenuItem(hPopup, 0, L"");
615 pSH->AppendMenuItem(
616 hPopup, WM_PWLEDIT_SELECTALL,
617 pProvider ? pProvider->LoadPopupMenuString(6) : L"&Select All");
618
619 if (m_pEdit->GetTotalWords() == 0) {
620 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_SELECTALL, FALSE);
621 }
622
623 int32_t x, y;
624 PWLtoWnd(ptPopup, x, y);
625 pSH->ClientToScreen(GetAttachedHWnd(), x, y);
626 pSH->SetCursor(FXCT_ARROW);
627 int32_t nCmd = pSH->TrackPopupMenu(hPopup, x, y, GetAttachedHWnd());
628
629 switch (nCmd) {
630 case WM_PWLEDIT_UNDO:
631 Undo();
632 break;
633 case WM_PWLEDIT_REDO:
634 Redo();
635 break;
636 case WM_PWLEDIT_CUT:
637 CutText();
638 break;
639 case WM_PWLEDIT_COPY:
640 CopyText();
641 break;
642 case WM_PWLEDIT_PASTE:
643 PasteText();
644 break;
645 case WM_PWLEDIT_DELETE:
646 Clear();
647 break;
648 case WM_PWLEDIT_SELECTALL:
649 SelectAll();
650 break;
651 case WM_PWLEDIT_SUGGEST + 0:
652 SetSel(m_pEdit->WordPlaceToWordIndex(wrLatin.BeginPos),
653 m_pEdit->WordPlaceToWordIndex(wrLatin.EndPos));
654 ReplaceSel(sSuggestWords[0].UTF8Decode().c_str());
655 break;
656 case WM_PWLEDIT_SUGGEST + 1:
657 SetSel(m_pEdit->WordPlaceToWordIndex(wrLatin.BeginPos),
658 m_pEdit->WordPlaceToWordIndex(wrLatin.EndPos));
659 ReplaceSel(sSuggestWords[1].UTF8Decode().c_str());
660 break;
661 case WM_PWLEDIT_SUGGEST + 2:
662 SetSel(m_pEdit->WordPlaceToWordIndex(wrLatin.BeginPos),
663 m_pEdit->WordPlaceToWordIndex(wrLatin.EndPos));
664 ReplaceSel(sSuggestWords[2].UTF8Decode().c_str());
665 break;
666 case WM_PWLEDIT_SUGGEST + 3:
667 SetSel(m_pEdit->WordPlaceToWordIndex(wrLatin.BeginPos),
668 m_pEdit->WordPlaceToWordIndex(wrLatin.EndPos));
669 ReplaceSel(sSuggestWords[3].UTF8Decode().c_str());
670 break;
671 case WM_PWLEDIT_SUGGEST + 4:
672 SetSel(m_pEdit->WordPlaceToWordIndex(wrLatin.BeginPos),
673 m_pEdit->WordPlaceToWordIndex(wrLatin.EndPos));
674 ReplaceSel(sSuggestWords[4].UTF8Decode().c_str());
675 break;
676 default:
677 break;
678 }
679
680 pSH->DestroyMenu(hPopup);
681
682 return TRUE;
683}
684
685void CPWL_Edit::OnSetFocus() {
686 SetEditCaret(TRUE);
687
688 if (!IsReadOnly()) {
689 if (IPWL_FocusHandler* pFocusHandler = GetFocusHandler())
690 pFocusHandler->OnSetFocus(this);
691 }
692
693 m_bFocus = TRUE;
694}
695
696void CPWL_Edit::OnKillFocus() {
697 ShowVScrollBar(FALSE);
698
699 m_pEdit->SelectNone();
Tom Sepez281a9ea2016-02-26 14:24:28 -0800700 SetCaret(FALSE, CFX_FloatPoint(0.0f, 0.0f), CFX_FloatPoint(0.0f, 0.0f));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700701
702 SetCharSet(0);
703
704 if (!IsReadOnly()) {
705 if (IPWL_FocusHandler* pFocusHandler = GetFocusHandler())
706 pFocusHandler->OnKillFocus(this);
707 }
708
709 m_bFocus = FALSE;
710}
711
712void CPWL_Edit::SetHorzScale(int32_t nHorzScale, FX_BOOL bPaint /* = TRUE*/) {
713 m_pEdit->SetHorzScale(nHorzScale, bPaint);
714}
715
716void CPWL_Edit::SetCharSpace(FX_FLOAT fCharSpace, FX_BOOL bPaint /* = TRUE*/) {
717 m_pEdit->SetCharSpace(fCharSpace, bPaint);
718}
719
720void CPWL_Edit::SetLineLeading(FX_FLOAT fLineLeading,
721 FX_BOOL bPaint /* = TRUE*/) {
722 m_pEdit->SetLineLeading(fLineLeading, bPaint);
723}
724
725CFX_ByteString CPWL_Edit::GetSelectAppearanceStream(
Tom Sepez281a9ea2016-02-26 14:24:28 -0800726 const CFX_FloatPoint& ptOffset) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700727 CPVT_WordRange wr = GetSelectWordRange();
728 return CPWL_Utils::GetEditSelAppStream(m_pEdit, ptOffset, &wr);
729}
730
731CPVT_WordRange CPWL_Edit::GetSelectWordRange() const {
732 if (m_pEdit->IsSelected()) {
733 int32_t nStart = -1;
734 int32_t nEnd = -1;
735
736 m_pEdit->GetSel(nStart, nEnd);
737
738 CPVT_WordPlace wpStart = m_pEdit->WordIndexToWordPlace(nStart);
739 CPVT_WordPlace wpEnd = m_pEdit->WordIndexToWordPlace(nEnd);
740
741 return CPVT_WordRange(wpStart, wpEnd);
742 }
743
744 return CPVT_WordRange();
745}
746
747CFX_ByteString CPWL_Edit::GetTextAppearanceStream(
Tom Sepez281a9ea2016-02-26 14:24:28 -0800748 const CFX_FloatPoint& ptOffset) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700749 CFX_ByteTextBuf sRet;
750 CFX_ByteString sEdit = CPWL_Utils::GetEditAppStream(m_pEdit, ptOffset);
751
752 if (sEdit.GetLength() > 0) {
tsepez28f97ff2016-04-04 16:41:35 -0700753 sRet << "BT\n"
754 << CPWL_Utils::GetColorAppStream(GetTextColor()).AsByteStringC()
755 << sEdit.AsByteStringC() << "ET\n";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700756 }
757
758 return sRet.GetByteString();
759}
760
761CFX_ByteString CPWL_Edit::GetCaretAppearanceStream(
Tom Sepez281a9ea2016-02-26 14:24:28 -0800762 const CFX_FloatPoint& ptOffset) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700763 if (m_pEditCaret)
764 return m_pEditCaret->GetCaretAppearanceStream(ptOffset);
765
766 return CFX_ByteString();
767}
768
Tom Sepez281a9ea2016-02-26 14:24:28 -0800769CFX_FloatPoint CPWL_Edit::GetWordRightBottomPoint(
770 const CPVT_WordPlace& wpWord) {
771 CFX_FloatPoint pt(0.0f, 0.0f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700772
773 if (IFX_Edit_Iterator* pIterator = m_pEdit->GetIterator()) {
774 CPVT_WordPlace wpOld = pIterator->GetAt();
775 pIterator->SetAt(wpWord);
776 CPVT_Word word;
777 if (pIterator->GetWord(word)) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800778 pt = CFX_FloatPoint(word.ptWord.x + word.fWidth,
779 word.ptWord.y + word.fDescent);
Lei Zhang60f507b2015-06-13 00:41:00 -0700780 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700781
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700782 pIterator->SetAt(wpOld);
783 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700784
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700785 return pt;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700786}
787
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700788FX_BOOL CPWL_Edit::IsTextFull() const {
789 return m_pEdit->IsTextFull();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700790}
791
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700792FX_FLOAT CPWL_Edit::GetCharArrayAutoFontSize(CPDF_Font* pFont,
Tom Sepez281a9ea2016-02-26 14:24:28 -0800793 const CFX_FloatRect& rcPlate,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700794 int32_t nCharArray) {
795 if (pFont && !pFont->IsStandardFont()) {
796 FX_RECT rcBBox;
797 pFont->GetFontBBox(rcBBox);
798
Tom Sepez281a9ea2016-02-26 14:24:28 -0800799 CFX_FloatRect rcCell = rcPlate;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700800 FX_FLOAT xdiv = rcCell.Width() / nCharArray * 1000.0f / rcBBox.Width();
801 FX_FLOAT ydiv = -rcCell.Height() * 1000.0f / rcBBox.Height();
802
803 return xdiv < ydiv ? xdiv : ydiv;
804 }
805
806 return 0.0f;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700807}
808
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700809void CPWL_Edit::SetCharArray(int32_t nCharArray) {
810 if (HasFlag(PES_CHARARRAY) && nCharArray > 0) {
811 m_pEdit->SetCharArray(nCharArray);
812 m_pEdit->SetTextOverflow(TRUE);
813
814 if (HasFlag(PWS_AUTOFONTSIZE)) {
dsinclairc7a73492016-04-05 12:01:42 -0700815 if (IPVT_FontMap* pFontMap = GetFontMap()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700816 FX_FLOAT fFontSize = GetCharArrayAutoFontSize(
817 pFontMap->GetPDFFont(0), GetClientRect(), nCharArray);
818 if (fFontSize > 0.0f) {
819 m_pEdit->SetAutoFontSize(FALSE);
820 m_pEdit->SetFontSize(fFontSize);
821 }
822 }
823 }
824 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700825}
826
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700827void CPWL_Edit::SetLimitChar(int32_t nLimitChar) {
828 m_pEdit->SetLimitChar(nLimitChar);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700829}
830
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700831void CPWL_Edit::ReplaceSel(const FX_WCHAR* csText) {
832 m_pEdit->Clear();
833 m_pEdit->InsertText(csText);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700834}
835
Tom Sepez281a9ea2016-02-26 14:24:28 -0800836CFX_FloatRect CPWL_Edit::GetFocusRect() const {
837 return CFX_FloatRect();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700838}
839
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700840void CPWL_Edit::ShowVScrollBar(FX_BOOL bShow) {
841 if (CPWL_ScrollBar* pScroll = GetVScrollBar()) {
842 if (bShow) {
843 if (!pScroll->IsVisible()) {
844 pScroll->SetVisible(TRUE);
Tom Sepez281a9ea2016-02-26 14:24:28 -0800845 CFX_FloatRect rcWindow = GetWindowRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700846 m_rcOldWindow = rcWindow;
847 rcWindow.right += PWL_SCROLLBAR_WIDTH;
848 Move(rcWindow, TRUE, TRUE);
849 }
850 } else {
851 if (pScroll->IsVisible()) {
852 pScroll->SetVisible(FALSE);
853 Move(m_rcOldWindow, TRUE, TRUE);
854 }
855 }
856 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700857}
858
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700859FX_BOOL CPWL_Edit::IsVScrollBarVisible() const {
860 if (CPWL_ScrollBar* pScroll = GetVScrollBar()) {
861 return pScroll->IsVisible();
862 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700863
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700864 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700865}
866
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700867void CPWL_Edit::EnableSpellCheck(FX_BOOL bEnabled) {
868 if (bEnabled)
869 AddFlag(PES_SPELLCHECK);
870 else
871 RemoveFlag(PES_SPELLCHECK);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700872}
873
tsepezc3255f52016-03-25 14:52:27 -0700874FX_BOOL CPWL_Edit::OnKeyDown(uint16_t nChar, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700875 if (m_bMouseDown)
876 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700877
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700878 if (nChar == FWL_VKEY_Delete) {
879 if (m_pFillerNotify) {
880 FX_BOOL bRC = TRUE;
881 FX_BOOL bExit = FALSE;
882 CFX_WideString strChange;
883 CFX_WideString strChangeEx;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700884
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700885 int nSelStart = 0;
886 int nSelEnd = 0;
887 GetSel(nSelStart, nSelEnd);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700888
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700889 if (nSelStart == nSelEnd)
890 nSelEnd = nSelStart + 1;
Lei Zhanga5b47042015-10-19 14:32:16 -0700891 m_pFillerNotify->OnBeforeKeyStroke(GetAttachedData(), strChange,
892 strChangeEx, nSelStart, nSelEnd, TRUE,
893 bRC, bExit, nFlag);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700894 if (!bRC)
895 return FALSE;
896 if (bExit)
897 return FALSE;
898 }
899 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700900
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700901 FX_BOOL bRet = CPWL_EditCtrl::OnKeyDown(nChar, nFlag);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700902
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700903 // In case of implementation swallow the OnKeyDown event.
904 if (IsProceedtoOnChar(nChar, nFlag))
905 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700906
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700907 return bRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700908}
909
910/**
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700911*In case of implementation swallow the OnKeyDown event.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700912*If the event is swallowed, implementation may do other unexpected things, which
913*is not the control means to do.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700914*/
tsepezc3255f52016-03-25 14:52:27 -0700915FX_BOOL CPWL_Edit::IsProceedtoOnChar(uint16_t nKeyCode, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700916 FX_BOOL bCtrl = IsCTRLpressed(nFlag);
917 FX_BOOL bAlt = IsALTpressed(nFlag);
918 if (bCtrl && !bAlt) {
919 // hot keys for edit control.
920 switch (nKeyCode) {
921 case 'C':
922 case 'V':
923 case 'X':
924 case 'A':
925 case 'Z':
926 return TRUE;
927 default:
928 break;
929 }
930 }
931 // control characters.
932 switch (nKeyCode) {
933 case FWL_VKEY_Escape:
934 case FWL_VKEY_Back:
935 case FWL_VKEY_Return:
936 case FWL_VKEY_Space:
937 return TRUE;
938 default:
Lei Zhanga5b47042015-10-19 14:32:16 -0700939 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700940 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700941}
942
tsepezc3255f52016-03-25 14:52:27 -0700943FX_BOOL CPWL_Edit::OnChar(uint16_t nChar, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700944 if (m_bMouseDown)
945 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700946
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700947 FX_BOOL bRC = TRUE;
948 FX_BOOL bExit = FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700949
Lei Zhanga5b47042015-10-19 14:32:16 -0700950 if (!IsCTRLpressed(nFlag)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700951 if (m_pFillerNotify) {
952 CFX_WideString swChange;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700953
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700954 int nSelStart = 0;
955 int nSelEnd = 0;
956 GetSel(nSelStart, nSelEnd);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700957
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700958 switch (nChar) {
959 case FWL_VKEY_Back:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700960 if (nSelStart == nSelEnd)
961 nSelStart = nSelEnd - 1;
962 break;
963 case FWL_VKEY_Return:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700964 break;
965 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700966 swChange += nChar;
967 break;
968 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700969
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700970 CFX_WideString strChangeEx;
Lei Zhanga5b47042015-10-19 14:32:16 -0700971 m_pFillerNotify->OnBeforeKeyStroke(GetAttachedData(), swChange,
972 strChangeEx, nSelStart, nSelEnd, TRUE,
973 bRC, bExit, nFlag);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700974 }
975 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700976
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700977 if (!bRC)
978 return TRUE;
979 if (bExit)
980 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700981
dsinclairc7a73492016-04-05 12:01:42 -0700982 if (IPVT_FontMap* pFontMap = GetFontMap()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700983 int32_t nOldCharSet = GetCharSet();
984 int32_t nNewCharSet = pFontMap->CharSetFromUnicode(nChar, DEFAULT_CHARSET);
985 if (nOldCharSet != nNewCharSet) {
986 SetCharSet(nNewCharSet);
987 }
988 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700989
Lei Zhanga5b47042015-10-19 14:32:16 -0700990 return CPWL_EditCtrl::OnChar(nChar, nFlag);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700991}
992
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700993FX_BOOL CPWL_Edit::OnMouseWheel(short zDelta,
Tom Sepez281a9ea2016-02-26 14:24:28 -0800994 const CFX_FloatPoint& point,
tsepezc3255f52016-03-25 14:52:27 -0700995 uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700996 if (HasFlag(PES_MULTILINE)) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800997 CFX_FloatPoint ptScroll = GetScrollPos();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700998
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700999 if (zDelta > 0) {
1000 ptScroll.y += GetFontSize();
1001 } else {
1002 ptScroll.y -= GetFontSize();
1003 }
1004 SetScrollPos(ptScroll);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001005
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001006 return TRUE;
1007 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001008
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001009 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001010}
1011
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001012void CPWL_Edit::OnInsertReturn(const CPVT_WordPlace& place,
1013 const CPVT_WordPlace& oldplace) {
1014 if (HasFlag(PES_SPELLCHECK)) {
1015 m_pEdit->RefreshWordRange(CombineWordRange(GetLatinWordsRange(oldplace),
1016 GetLatinWordsRange(place)));
1017 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001018
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001019 if (m_pEditNotify) {
1020 m_pEditNotify->OnInsertReturn(place, oldplace);
1021 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001022}
1023
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001024void CPWL_Edit::OnBackSpace(const CPVT_WordPlace& place,
1025 const CPVT_WordPlace& oldplace) {
1026 if (HasFlag(PES_SPELLCHECK)) {
1027 m_pEdit->RefreshWordRange(CombineWordRange(GetLatinWordsRange(oldplace),
1028 GetLatinWordsRange(place)));
1029 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001030
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001031 if (m_pEditNotify) {
1032 m_pEditNotify->OnBackSpace(place, oldplace);
1033 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001034}
1035
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001036void CPWL_Edit::OnDelete(const CPVT_WordPlace& place,
1037 const CPVT_WordPlace& oldplace) {
1038 if (HasFlag(PES_SPELLCHECK)) {
1039 m_pEdit->RefreshWordRange(CombineWordRange(GetLatinWordsRange(oldplace),
1040 GetLatinWordsRange(place)));
1041 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001042
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001043 if (m_pEditNotify) {
1044 m_pEditNotify->OnDelete(place, oldplace);
1045 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001046}
1047
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001048void CPWL_Edit::OnClear(const CPVT_WordPlace& place,
1049 const CPVT_WordPlace& oldplace) {
1050 if (HasFlag(PES_SPELLCHECK)) {
1051 m_pEdit->RefreshWordRange(CombineWordRange(GetLatinWordsRange(oldplace),
1052 GetLatinWordsRange(place)));
1053 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001054
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001055 if (m_pEditNotify) {
1056 m_pEditNotify->OnClear(place, oldplace);
1057 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001058}
1059
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001060void CPWL_Edit::OnInsertWord(const CPVT_WordPlace& place,
1061 const CPVT_WordPlace& oldplace) {
1062 if (HasFlag(PES_SPELLCHECK)) {
1063 m_pEdit->RefreshWordRange(CombineWordRange(GetLatinWordsRange(oldplace),
1064 GetLatinWordsRange(place)));
1065 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001066
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001067 if (m_pEditNotify) {
1068 m_pEditNotify->OnInsertWord(place, oldplace);
1069 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001070}
1071
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001072void CPWL_Edit::OnSetText(const CPVT_WordPlace& place,
1073 const CPVT_WordPlace& oldplace) {}
1074
1075void CPWL_Edit::OnInsertText(const CPVT_WordPlace& place,
1076 const CPVT_WordPlace& oldplace) {
1077 if (HasFlag(PES_SPELLCHECK)) {
1078 m_pEdit->RefreshWordRange(CombineWordRange(GetLatinWordsRange(oldplace),
1079 GetLatinWordsRange(place)));
1080 }
1081
1082 if (m_pEditNotify) {
1083 m_pEditNotify->OnInsertText(place, oldplace);
1084 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001085}
1086
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001087void CPWL_Edit::OnAddUndo(IFX_Edit_UndoItem* pUndoItem) {
1088 if (m_pEditNotify) {
1089 m_pEditNotify->OnAddUndo(this);
1090 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001091}
1092
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001093CPVT_WordRange CPWL_Edit::CombineWordRange(const CPVT_WordRange& wr1,
1094 const CPVT_WordRange& wr2) {
1095 CPVT_WordRange wrRet;
1096
1097 if (wr1.BeginPos.WordCmp(wr2.BeginPos) < 0) {
1098 wrRet.BeginPos = wr1.BeginPos;
1099 } else {
1100 wrRet.BeginPos = wr2.BeginPos;
1101 }
1102
1103 if (wr1.EndPos.WordCmp(wr2.EndPos) < 0) {
1104 wrRet.EndPos = wr2.EndPos;
1105 } else {
1106 wrRet.EndPos = wr1.EndPos;
1107 }
1108
1109 return wrRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001110}
1111
Tom Sepez281a9ea2016-02-26 14:24:28 -08001112CPVT_WordRange CPWL_Edit::GetLatinWordsRange(
1113 const CFX_FloatPoint& point) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001114 return GetSameWordsRange(m_pEdit->SearchWordPlace(point), TRUE, FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001115}
1116
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001117CPVT_WordRange CPWL_Edit::GetLatinWordsRange(
1118 const CPVT_WordPlace& place) const {
1119 return GetSameWordsRange(place, TRUE, FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001120}
1121
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001122CPVT_WordRange CPWL_Edit::GetArabicWordsRange(
1123 const CPVT_WordPlace& place) const {
1124 return GetSameWordsRange(place, FALSE, TRUE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001125}
1126
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001127#define PWL_ISARABICWORD(word) \
1128 ((word >= 0x0600 && word <= 0x06FF) || (word >= 0xFB50 && word <= 0xFEFC))
1129
1130CPVT_WordRange CPWL_Edit::GetSameWordsRange(const CPVT_WordPlace& place,
1131 FX_BOOL bLatin,
1132 FX_BOOL bArabic) const {
1133 CPVT_WordRange range;
1134
1135 if (IFX_Edit_Iterator* pIterator = m_pEdit->GetIterator()) {
1136 CPVT_Word wordinfo;
1137 CPVT_WordPlace wpStart(place), wpEnd(place);
1138 pIterator->SetAt(place);
1139
1140 if (bLatin) {
1141 while (pIterator->NextWord()) {
Lei Zhangc2fb35f2016-01-05 16:46:58 -08001142 if (!pIterator->GetWord(wordinfo) ||
1143 !FX_EDIT_ISLATINWORD(wordinfo.Word)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001144 break;
Lei Zhangc2fb35f2016-01-05 16:46:58 -08001145 }
1146
1147 wpEnd = pIterator->GetAt();
1148 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001149 } else if (bArabic) {
1150 while (pIterator->NextWord()) {
Lei Zhangc2fb35f2016-01-05 16:46:58 -08001151 if (!pIterator->GetWord(wordinfo) || !PWL_ISARABICWORD(wordinfo.Word))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001152 break;
Lei Zhangc2fb35f2016-01-05 16:46:58 -08001153
1154 wpEnd = pIterator->GetAt();
1155 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001156 }
1157
1158 pIterator->SetAt(place);
1159
1160 if (bLatin) {
1161 do {
Lei Zhangc2fb35f2016-01-05 16:46:58 -08001162 if (!pIterator->GetWord(wordinfo) ||
1163 !FX_EDIT_ISLATINWORD(wordinfo.Word)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001164 break;
1165 }
Lei Zhangc2fb35f2016-01-05 16:46:58 -08001166
1167 wpStart = pIterator->GetAt();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001168 } while (pIterator->PrevWord());
1169 } else if (bArabic) {
1170 do {
Lei Zhangc2fb35f2016-01-05 16:46:58 -08001171 if (!pIterator->GetWord(wordinfo) || !PWL_ISARABICWORD(wordinfo.Word))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001172 break;
Lei Zhangc2fb35f2016-01-05 16:46:58 -08001173
1174 wpStart = pIterator->GetAt();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001175 } while (pIterator->PrevWord());
1176 }
1177
1178 range.Set(wpStart, wpEnd);
1179 }
1180
1181 return range;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001182}
1183
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001184void CPWL_Edit::GeneratePageObjects(
Tom Sepez2398d892016-02-17 16:46:26 -08001185 CPDF_PageObjectHolder* pObjectHolder,
Tom Sepez281a9ea2016-02-26 14:24:28 -08001186 const CFX_FloatPoint& ptOffset,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001187 CFX_ArrayTemplate<CPDF_TextObject*>& ObjArray) {
1188 IFX_Edit::GeneratePageObjects(
Tom Sepez2398d892016-02-17 16:46:26 -08001189 pObjectHolder, m_pEdit, ptOffset, NULL,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001190 CPWL_Utils::PWLColorToFXColor(GetTextColor(), GetTransparency()),
1191 ObjArray);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001192}
1193
Tom Sepez2398d892016-02-17 16:46:26 -08001194void CPWL_Edit::GeneratePageObjects(CPDF_PageObjectHolder* pObjectHolder,
Tom Sepez281a9ea2016-02-26 14:24:28 -08001195 const CFX_FloatPoint& ptOffset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001196 CFX_ArrayTemplate<CPDF_TextObject*> ObjArray;
1197 IFX_Edit::GeneratePageObjects(
Tom Sepez2398d892016-02-17 16:46:26 -08001198 pObjectHolder, m_pEdit, ptOffset, NULL,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001199 CPWL_Utils::PWLColorToFXColor(GetTextColor(), GetTransparency()),
1200 ObjArray);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001201}