blob: 64588f2dff1bc5713a881994453d83fcf5067be0 [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"
Dan Sinclaira8a28e02016-03-23 15:41:39 -040012#include "core/fxcrt/include/fx_safe_types.h"
13#include "core/fxcrt/include/fx_xml.h"
Dan Sinclairaa403d32016-03-15 14:57:22 -040014#include "core/include/fxge/fx_ge.h"
dan sinclair89e904b2016-03-23 19:29:15 -040015#include "fpdfsdk/pdfwindow/PWL_Caret.h"
16#include "fpdfsdk/pdfwindow/PWL_EditCtrl.h"
17#include "fpdfsdk/pdfwindow/PWL_FontMap.h"
18#include "fpdfsdk/pdfwindow/PWL_ScrollBar.h"
19#include "fpdfsdk/pdfwindow/PWL_Utils.h"
20#include "fpdfsdk/pdfwindow/PWL_Wnd.h"
Lei Zhangc2fb35f2016-01-05 16:46:58 -080021#include "public/fpdf_fwlevent.h"
Tom Sepezab277682016-02-17 10:07:21 -080022#include "third_party/base/stl_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070023
Nico Weber9d8ec5a2015-08-04 13:00:21 -070024CPWL_Edit::CPWL_Edit()
25 : m_pFillerNotify(NULL), m_pSpellCheck(NULL), m_bFocus(FALSE) {
26 m_pFormFiller = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027}
28
Nico Weber9d8ec5a2015-08-04 13:00:21 -070029CPWL_Edit::~CPWL_Edit() {
30 ASSERT(m_bFocus == FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070031}
32
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033CFX_ByteString CPWL_Edit::GetClassName() const {
34 return PWL_CLASSNAME_EDIT;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070035}
36
Nico Weber9d8ec5a2015-08-04 13:00:21 -070037void CPWL_Edit::OnDestroy() {}
38
39void CPWL_Edit::SetText(const FX_WCHAR* csText) {
40 CFX_WideString swText = csText;
41
42 if (HasFlag(PES_RICH)) {
43 CFX_ByteString sValue = CFX_ByteString::FromUnicode(swText);
44
45 if (CXML_Element* pXML =
46 CXML_Element::Parse(sValue.c_str(), sValue.GetLength())) {
47 int32_t nCount = pXML->CountChildren();
48 FX_BOOL bFirst = TRUE;
49
50 swText.Empty();
51
52 for (int32_t i = 0; i < nCount; i++) {
53 if (CXML_Element* pSubElement = pXML->GetElement(i)) {
54 CFX_ByteString tag = pSubElement->GetTagName();
55 if (tag.EqualNoCase("p")) {
56 int nChild = pSubElement->CountChildren();
57 CFX_WideString swSection;
58 for (int32_t j = 0; j < nChild; j++) {
59 swSection += pSubElement->GetContent(j);
60 }
61
62 if (bFirst)
63 bFirst = FALSE;
64 else
65 swText += FWL_VKEY_Return;
66 swText += swSection;
67 }
68 }
69 }
70
71 delete pXML;
72 }
73 }
74
75 m_pEdit->SetText(swText.c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070076}
77
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078void CPWL_Edit::RePosChildWnd() {
79 if (CPWL_ScrollBar* pVSB = GetVScrollBar()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -080080 CFX_FloatRect rcWindow = m_rcOldWindow;
81 CFX_FloatRect rcVScroll =
82 CFX_FloatRect(rcWindow.right, rcWindow.bottom,
83 rcWindow.right + PWL_SCROLLBAR_WIDTH, rcWindow.top);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084 pVSB->Move(rcVScroll, TRUE, FALSE);
85 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070086
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087 if (m_pEditCaret && !HasFlag(PES_TEXTOVERFLOW))
88 m_pEditCaret->SetClipRect(CPWL_Utils::InflateRect(
Dan Sinclair50cce602016-02-24 09:51:16 -050089 GetClientRect(), 1.0f)); // +1 for caret beside border
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070090
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 CPWL_EditCtrl::RePosChildWnd();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070092}
93
Tom Sepez281a9ea2016-02-26 14:24:28 -080094CFX_FloatRect CPWL_Edit::GetClientRect() const {
95 CFX_FloatRect rcClient = CPWL_Utils::DeflateRect(
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 GetWindowRect(), (FX_FLOAT)(GetBorderWidth() + GetInnerBorderWidth()));
97
98 if (CPWL_ScrollBar* pVSB = GetVScrollBar()) {
99 if (pVSB->IsVisible()) {
100 rcClient.right -= PWL_SCROLLBAR_WIDTH;
101 }
102 }
103
104 return rcClient;
105}
106
107void CPWL_Edit::SetAlignFormatH(PWL_EDIT_ALIGNFORMAT_H nFormat,
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800108 FX_BOOL bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700109 m_pEdit->SetAlignmentH((int32_t)nFormat, bPaint);
110}
111
112void CPWL_Edit::SetAlignFormatV(PWL_EDIT_ALIGNFORMAT_V nFormat,
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800113 FX_BOOL bPaint) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114 m_pEdit->SetAlignmentV((int32_t)nFormat, bPaint);
115}
116
117FX_BOOL CPWL_Edit::CanSelectAll() const {
118 return GetSelectWordRange() != m_pEdit->GetWholeWordRange();
119}
120
121FX_BOOL CPWL_Edit::CanClear() const {
122 return !IsReadOnly() && m_pEdit->IsSelected();
123}
124
125FX_BOOL CPWL_Edit::CanCopy() const {
126 return !HasFlag(PES_PASSWORD) && !HasFlag(PES_NOREAD) &&
127 m_pEdit->IsSelected();
128}
129
130FX_BOOL CPWL_Edit::CanCut() const {
131 return CanCopy() && !IsReadOnly();
132}
133
134FX_BOOL CPWL_Edit::CanPaste() const {
135 if (IsReadOnly())
136 return FALSE;
137
138 CFX_WideString swClipboard;
139 if (IFX_SystemHandler* pSH = GetSystemHandler())
140 swClipboard = pSH->GetClipboardText(GetAttachedHWnd());
141
142 return !swClipboard.IsEmpty();
143}
144
145void CPWL_Edit::CopyText() {
146 if (!CanCopy())
147 return;
148
149 CFX_WideString str = m_pEdit->GetSelText();
150
151 if (IFX_SystemHandler* pSH = GetSystemHandler())
152 pSH->SetClipboardText(GetAttachedHWnd(), str);
153}
154
155void CPWL_Edit::PasteText() {
156 if (!CanPaste())
157 return;
158
159 CFX_WideString swClipboard;
160 if (IFX_SystemHandler* pSH = GetSystemHandler())
161 swClipboard = pSH->GetClipboardText(GetAttachedHWnd());
162
163 if (m_pFillerNotify) {
164 FX_BOOL bRC = TRUE;
165 FX_BOOL bExit = FALSE;
166 CFX_WideString strChangeEx;
167 int nSelStart = 0;
168 int nSelEnd = 0;
169 GetSel(nSelStart, nSelEnd);
Lei Zhanga5b47042015-10-19 14:32:16 -0700170 m_pFillerNotify->OnBeforeKeyStroke(GetAttachedData(), swClipboard,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171 strChangeEx, nSelStart, nSelEnd, TRUE,
172 bRC, bExit, 0);
173 if (!bRC)
174 return;
175 if (bExit)
176 return;
177 }
178
179 if (swClipboard.GetLength() > 0) {
180 Clear();
181 InsertText(swClipboard.c_str());
182 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183}
184
185void CPWL_Edit::CutText() {
186 if (!CanCut())
187 return;
188
189 CFX_WideString str = m_pEdit->GetSelText();
190
191 if (IFX_SystemHandler* pSH = GetSystemHandler())
192 pSH->SetClipboardText(GetAttachedHWnd(), str);
193
194 m_pEdit->Clear();
195}
196
197void CPWL_Edit::OnCreated() {
198 CPWL_EditCtrl::OnCreated();
199
200 if (CPWL_ScrollBar* pScroll = GetVScrollBar()) {
201 pScroll->RemoveFlag(PWS_AUTOTRANSPARENT);
202 pScroll->SetTransparency(255);
203 }
204
205 SetParamByFlag();
206
207 m_rcOldWindow = GetWindowRect();
208
209 m_pEdit->SetOprNotify(this);
210 m_pEdit->EnableOprNotify(TRUE);
211}
212
213void CPWL_Edit::SetParamByFlag() {
214 if (HasFlag(PES_RIGHT)) {
215 m_pEdit->SetAlignmentH(2, FALSE);
216 } else if (HasFlag(PES_MIDDLE)) {
217 m_pEdit->SetAlignmentH(1, FALSE);
218 } else {
219 m_pEdit->SetAlignmentH(0, FALSE);
220 }
221
222 if (HasFlag(PES_BOTTOM)) {
223 m_pEdit->SetAlignmentV(2, FALSE);
224 } else if (HasFlag(PES_CENTER)) {
225 m_pEdit->SetAlignmentV(1, FALSE);
226 } else {
227 m_pEdit->SetAlignmentV(0, FALSE);
228 }
229
230 if (HasFlag(PES_PASSWORD)) {
231 m_pEdit->SetPasswordChar('*', FALSE);
232 }
233
234 m_pEdit->SetMultiLine(HasFlag(PES_MULTILINE), FALSE);
235 m_pEdit->SetAutoReturn(HasFlag(PES_AUTORETURN), FALSE);
236 m_pEdit->SetAutoFontSize(HasFlag(PWS_AUTOFONTSIZE), FALSE);
237 m_pEdit->SetAutoScroll(HasFlag(PES_AUTOSCROLL), FALSE);
238 m_pEdit->EnableUndo(HasFlag(PES_UNDO));
239
240 if (HasFlag(PES_TEXTOVERFLOW)) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800241 SetClipRect(CFX_FloatRect(0.0f, 0.0f, 0.0f, 0.0f));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 m_pEdit->SetTextOverflow(TRUE, FALSE);
243 } else {
244 if (m_pEditCaret) {
245 m_pEditCaret->SetClipRect(CPWL_Utils::InflateRect(
Dan Sinclair50cce602016-02-24 09:51:16 -0500246 GetClientRect(), 1.0f)); // +1 for caret beside border
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700247 }
248 }
249
250 if (HasFlag(PES_SPELLCHECK)) {
251 m_pSpellCheck = GetCreationParam().pSpellCheck;
252 }
253}
254
255void CPWL_Edit::GetThisAppearanceStream(CFX_ByteTextBuf& sAppStream) {
256 CPWL_Wnd::GetThisAppearanceStream(sAppStream);
257
Tom Sepez281a9ea2016-02-26 14:24:28 -0800258 CFX_FloatRect rcClient = GetClientRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700259 CFX_ByteTextBuf sLine;
260
261 int32_t nCharArray = m_pEdit->GetCharArray();
262
263 if (nCharArray > 0) {
264 switch (GetBorderStyle()) {
265 case PBS_SOLID: {
266 sLine << "q\n" << GetBorderWidth() << " w\n"
267 << CPWL_Utils::GetColorAppStream(GetBorderColor(), FALSE)
tsepez28f97ff2016-04-04 16:41:35 -0700268 .AsByteStringC()
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269 << " 2 J 0 j\n";
270
271 for (int32_t i = 1; i < nCharArray; i++) {
272 sLine << rcClient.left +
273 ((rcClient.right - rcClient.left) / nCharArray) * i
274 << " " << rcClient.bottom << " m\n"
275 << rcClient.left +
276 ((rcClient.right - rcClient.left) / nCharArray) * i
277 << " " << rcClient.top << " l S\n";
278 }
279
280 sLine << "Q\n";
281 } break;
282 case PBS_DASH: {
283 sLine << "q\n" << GetBorderWidth() << " w\n"
284 << CPWL_Utils::GetColorAppStream(GetBorderColor(), FALSE)
tsepez28f97ff2016-04-04 16:41:35 -0700285 .AsByteStringC()
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700286 << " 2 J 0 j\n"
287 << "[" << GetBorderDash().nDash << " " << GetBorderDash().nGap
288 << "] " << GetBorderDash().nPhase << " d\n";
289
290 for (int32_t i = 1; i < nCharArray; i++) {
291 sLine << rcClient.left +
292 ((rcClient.right - rcClient.left) / nCharArray) * i
293 << " " << rcClient.bottom << " m\n"
294 << rcClient.left +
295 ((rcClient.right - rcClient.left) / nCharArray) * i
296 << " " << rcClient.top << " l S\n";
297 }
298
299 sLine << "Q\n";
300 } break;
301 }
302 }
303
304 sAppStream << sLine;
305
306 CFX_ByteTextBuf sText;
307
Tom Sepez281a9ea2016-02-26 14:24:28 -0800308 CFX_FloatPoint ptOffset = CFX_FloatPoint(0.0f, 0.0f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700309
310 CPVT_WordRange wrWhole = m_pEdit->GetWholeWordRange();
311 CPVT_WordRange wrSelect = GetSelectWordRange();
312 CPVT_WordRange wrVisible =
313 (HasFlag(PES_TEXTOVERFLOW) ? wrWhole : m_pEdit->GetVisibleWordRange());
314 CPVT_WordRange wrSelBefore(wrWhole.BeginPos, wrSelect.BeginPos);
315 CPVT_WordRange wrSelAfter(wrSelect.EndPos, wrWhole.EndPos);
316
317 CPVT_WordRange wrTemp =
318 CPWL_Utils::OverlapWordRange(GetSelectWordRange(), wrVisible);
319 CFX_ByteString sEditSel =
320 CPWL_Utils::GetEditSelAppStream(m_pEdit, ptOffset, &wrTemp);
321
322 if (sEditSel.GetLength() > 0)
323 sText << CPWL_Utils::GetColorAppStream(PWL_DEFAULT_SELBACKCOLOR)
tsepez28f97ff2016-04-04 16:41:35 -0700324 .AsByteStringC()
325 << sEditSel.AsByteStringC();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700326
327 wrTemp = CPWL_Utils::OverlapWordRange(wrVisible, wrSelBefore);
328 CFX_ByteString sEditBefore = CPWL_Utils::GetEditAppStream(
329 m_pEdit, ptOffset, &wrTemp, !HasFlag(PES_CHARARRAY),
330 m_pEdit->GetPasswordChar());
331
332 if (sEditBefore.GetLength() > 0)
tsepez28f97ff2016-04-04 16:41:35 -0700333 sText << "BT\n"
334 << CPWL_Utils::GetColorAppStream(GetTextColor()).AsByteStringC()
335 << sEditBefore.AsByteStringC() << "ET\n";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336
337 wrTemp = CPWL_Utils::OverlapWordRange(wrVisible, wrSelect);
338 CFX_ByteString sEditMid = CPWL_Utils::GetEditAppStream(
339 m_pEdit, ptOffset, &wrTemp, !HasFlag(PES_CHARARRAY),
340 m_pEdit->GetPasswordChar());
341
342 if (sEditMid.GetLength() > 0)
343 sText << "BT\n"
344 << CPWL_Utils::GetColorAppStream(CPWL_Color(COLORTYPE_GRAY, 1))
tsepez28f97ff2016-04-04 16:41:35 -0700345 .AsByteStringC()
346 << sEditMid.AsByteStringC() << "ET\n";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700347
348 wrTemp = CPWL_Utils::OverlapWordRange(wrVisible, wrSelAfter);
349 CFX_ByteString sEditAfter = CPWL_Utils::GetEditAppStream(
350 m_pEdit, ptOffset, &wrTemp, !HasFlag(PES_CHARARRAY),
351 m_pEdit->GetPasswordChar());
352
353 if (sEditAfter.GetLength() > 0)
tsepez28f97ff2016-04-04 16:41:35 -0700354 sText << "BT\n"
355 << CPWL_Utils::GetColorAppStream(GetTextColor()).AsByteStringC()
356 << sEditAfter.AsByteStringC() << "ET\n";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700357
358 if (HasFlag(PES_SPELLCHECK)) {
359 CFX_ByteString sSpellCheck = CPWL_Utils::GetSpellCheckAppStream(
360 m_pEdit, m_pSpellCheck, ptOffset, &wrVisible);
361 if (sSpellCheck.GetLength() > 0)
362 sText << CPWL_Utils::GetColorAppStream(CPWL_Color(COLORTYPE_RGB, 1, 0, 0),
363 FALSE)
tsepez28f97ff2016-04-04 16:41:35 -0700364 .AsByteStringC()
365 << sSpellCheck.AsByteStringC();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700366 }
367
368 if (sText.GetLength() > 0) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800369 CFX_FloatRect rcClient = GetClientRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700370 sAppStream << "q\n/Tx BMC\n";
371
372 if (!HasFlag(PES_TEXTOVERFLOW))
373 sAppStream << rcClient.left << " " << rcClient.bottom << " "
374 << rcClient.right - rcClient.left << " "
375 << rcClient.top - rcClient.bottom << " re W n\n";
376
377 sAppStream << sText;
378
379 sAppStream << "EMC\nQ\n";
380 }
381}
382
383void CPWL_Edit::DrawThisAppearance(CFX_RenderDevice* pDevice,
Tom Sepez60d909e2015-12-10 15:34:55 -0800384 CFX_Matrix* pUser2Device) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700385 CPWL_Wnd::DrawThisAppearance(pDevice, pUser2Device);
386
Tom Sepez281a9ea2016-02-26 14:24:28 -0800387 CFX_FloatRect rcClient = GetClientRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700388 CFX_ByteTextBuf sLine;
389
390 int32_t nCharArray = m_pEdit->GetCharArray();
391 FX_SAFE_INT32 nCharArraySafe = nCharArray;
392 nCharArraySafe -= 1;
393 nCharArraySafe *= 2;
394
395 if (nCharArray > 0 && nCharArraySafe.IsValid()) {
396 switch (GetBorderStyle()) {
397 case PBS_SOLID: {
398 CFX_GraphStateData gsd;
399 gsd.m_LineWidth = (FX_FLOAT)GetBorderWidth();
400
401 CFX_PathData path;
402 path.SetPointCount(nCharArraySafe.ValueOrDie());
403
404 for (int32_t i = 0; i < nCharArray - 1; i++) {
405 path.SetPoint(
406 i * 2,
407 rcClient.left +
408 ((rcClient.right - rcClient.left) / nCharArray) * (i + 1),
409 rcClient.bottom, FXPT_MOVETO);
410 path.SetPoint(
411 i * 2 + 1,
412 rcClient.left +
413 ((rcClient.right - rcClient.left) / nCharArray) * (i + 1),
414 rcClient.top, FXPT_LINETO);
415 }
416 if (path.GetPointCount() > 0)
417 pDevice->DrawPath(
418 &path, pUser2Device, &gsd, 0,
419 CPWL_Utils::PWLColorToFXColor(GetBorderColor(), 255),
420 FXFILL_ALTERNATE);
421 } break;
422 case PBS_DASH: {
423 CFX_GraphStateData gsd;
424 gsd.m_LineWidth = (FX_FLOAT)GetBorderWidth();
425
426 gsd.SetDashCount(2);
427 gsd.m_DashArray[0] = (FX_FLOAT)GetBorderDash().nDash;
428 gsd.m_DashArray[1] = (FX_FLOAT)GetBorderDash().nGap;
429 gsd.m_DashPhase = (FX_FLOAT)GetBorderDash().nPhase;
430
431 CFX_PathData path;
432 path.SetPointCount(nCharArraySafe.ValueOrDie());
433
434 for (int32_t i = 0; i < nCharArray - 1; i++) {
435 path.SetPoint(
436 i * 2,
437 rcClient.left +
438 ((rcClient.right - rcClient.left) / nCharArray) * (i + 1),
439 rcClient.bottom, FXPT_MOVETO);
440 path.SetPoint(
441 i * 2 + 1,
442 rcClient.left +
443 ((rcClient.right - rcClient.left) / nCharArray) * (i + 1),
444 rcClient.top, FXPT_LINETO);
445 }
446 if (path.GetPointCount() > 0)
447 pDevice->DrawPath(
448 &path, pUser2Device, &gsd, 0,
449 CPWL_Utils::PWLColorToFXColor(GetBorderColor(), 255),
450 FXFILL_ALTERNATE);
451 } break;
452 }
453 }
454
Tom Sepez281a9ea2016-02-26 14:24:28 -0800455 CFX_FloatRect rcClip;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700456 CPVT_WordRange wrRange = m_pEdit->GetVisibleWordRange();
457 CPVT_WordRange* pRange = NULL;
458
459 if (!HasFlag(PES_TEXTOVERFLOW)) {
460 rcClip = GetClientRect();
461 pRange = &wrRange;
462 }
463 IFX_SystemHandler* pSysHandler = GetSystemHandler();
464 IFX_Edit::DrawEdit(
465 pDevice, pUser2Device, m_pEdit,
466 CPWL_Utils::PWLColorToFXColor(GetTextColor(), GetTransparency()),
467 CPWL_Utils::PWLColorToFXColor(GetTextStrokeColor(), GetTransparency()),
Tom Sepez281a9ea2016-02-26 14:24:28 -0800468 rcClip, CFX_FloatPoint(0.0f, 0.0f), pRange, pSysHandler, m_pFormFiller);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700469
470 if (HasFlag(PES_SPELLCHECK)) {
471 CPWL_Utils::DrawEditSpellCheck(pDevice, pUser2Device, m_pEdit, rcClip,
Tom Sepez281a9ea2016-02-26 14:24:28 -0800472 CFX_FloatPoint(0.0f, 0.0f), pRange,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700473 GetCreationParam().pSpellCheck);
474 }
475}
476
tsepezc3255f52016-03-25 14:52:27 -0700477FX_BOOL CPWL_Edit::OnLButtonDown(const CFX_FloatPoint& point, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700478 CPWL_Wnd::OnLButtonDown(point, nFlag);
479
480 if (HasFlag(PES_TEXTOVERFLOW) || ClientHitTest(point)) {
481 if (m_bMouseDown)
482 InvalidateRect();
483
484 m_bMouseDown = TRUE;
485 SetCapture();
486
487 m_pEdit->OnMouseDown(point, IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
488 }
489
490 return TRUE;
491}
492
Tom Sepez281a9ea2016-02-26 14:24:28 -0800493FX_BOOL CPWL_Edit::OnLButtonDblClk(const CFX_FloatPoint& point,
tsepezc3255f52016-03-25 14:52:27 -0700494 uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700495 CPWL_Wnd::OnLButtonDblClk(point, nFlag);
496
497 if (HasFlag(PES_TEXTOVERFLOW) || ClientHitTest(point)) {
498 m_pEdit->SelectAll();
499 }
500
501 return TRUE;
502}
503
504#define WM_PWLEDIT_UNDO 0x01
505#define WM_PWLEDIT_REDO 0x02
506#define WM_PWLEDIT_CUT 0x03
507#define WM_PWLEDIT_COPY 0x04
508#define WM_PWLEDIT_PASTE 0x05
509#define WM_PWLEDIT_DELETE 0x06
510#define WM_PWLEDIT_SELECTALL 0x07
511#define WM_PWLEDIT_SUGGEST 0x08
512
tsepezc3255f52016-03-25 14:52:27 -0700513FX_BOOL CPWL_Edit::OnRButtonUp(const CFX_FloatPoint& point, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700514 if (m_bMouseDown)
515 return FALSE;
516
517 CPWL_Wnd::OnRButtonUp(point, nFlag);
518
519 if (!HasFlag(PES_TEXTOVERFLOW) && !ClientHitTest(point))
520 return TRUE;
521
522 IFX_SystemHandler* pSH = GetSystemHandler();
523 if (!pSH)
524 return FALSE;
525
526 SetFocus();
527
528 CPVT_WordRange wrLatin = GetLatinWordsRange(point);
529 CFX_WideString swLatin = m_pEdit->GetRangeText(wrLatin);
530
531 FX_HMENU hPopup = pSH->CreatePopupMenu();
532 if (!hPopup)
533 return FALSE;
534
Tom Sepezab277682016-02-17 10:07:21 -0800535 std::vector<CFX_ByteString> sSuggestWords;
Tom Sepez281a9ea2016-02-26 14:24:28 -0800536 CFX_FloatPoint ptPopup = point;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700537
538 if (!IsReadOnly()) {
539 if (HasFlag(PES_SPELLCHECK) && !swLatin.IsEmpty()) {
540 if (m_pSpellCheck) {
541 CFX_ByteString sLatin = CFX_ByteString::FromUnicode(swLatin);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700542 if (!m_pSpellCheck->CheckWord(sLatin)) {
543 m_pSpellCheck->SuggestWords(sLatin, sSuggestWords);
544
Tom Sepezab277682016-02-17 10:07:21 -0800545 int32_t nSuggest = pdfium::CollectionSize<int32_t>(sSuggestWords);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700546 for (int32_t nWord = 0; nWord < nSuggest; nWord++) {
547 pSH->AppendMenuItem(hPopup, WM_PWLEDIT_SUGGEST + nWord,
548 sSuggestWords[nWord].UTF8Decode());
549 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700550 if (nSuggest > 0)
551 pSH->AppendMenuItem(hPopup, 0, L"");
552
553 ptPopup = GetWordRightBottomPoint(wrLatin.EndPos);
554 }
555 }
556 }
557 }
558
559 IPWL_Provider* pProvider = GetProvider();
560
561 if (HasFlag(PES_UNDO)) {
562 pSH->AppendMenuItem(
563 hPopup, WM_PWLEDIT_UNDO,
564 pProvider ? pProvider->LoadPopupMenuString(0) : L"&Undo");
565 pSH->AppendMenuItem(
566 hPopup, WM_PWLEDIT_REDO,
567 pProvider ? pProvider->LoadPopupMenuString(1) : L"&Redo");
568 pSH->AppendMenuItem(hPopup, 0, L"");
569
570 if (!m_pEdit->CanUndo())
571 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_UNDO, FALSE);
572 if (!m_pEdit->CanRedo())
573 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_REDO, FALSE);
574 }
575
576 pSH->AppendMenuItem(hPopup, WM_PWLEDIT_CUT,
577 pProvider ? pProvider->LoadPopupMenuString(2) : L"Cu&t");
578 pSH->AppendMenuItem(hPopup, WM_PWLEDIT_COPY,
579 pProvider ? pProvider->LoadPopupMenuString(3) : L"&Copy");
580 pSH->AppendMenuItem(
581 hPopup, WM_PWLEDIT_PASTE,
582 pProvider ? pProvider->LoadPopupMenuString(4) : L"&Paste");
583 pSH->AppendMenuItem(
584 hPopup, WM_PWLEDIT_DELETE,
585 pProvider ? pProvider->LoadPopupMenuString(5) : L"&Delete");
586
587 CFX_WideString swText = pSH->GetClipboardText(GetAttachedHWnd());
588 if (swText.IsEmpty())
589 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_PASTE, FALSE);
590
591 if (!m_pEdit->IsSelected()) {
592 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_CUT, FALSE);
593 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_COPY, FALSE);
594 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_DELETE, FALSE);
595 }
596
597 if (IsReadOnly()) {
598 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_CUT, FALSE);
599 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_DELETE, FALSE);
600 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_PASTE, FALSE);
601 }
602
603 if (HasFlag(PES_PASSWORD)) {
604 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_CUT, FALSE);
605 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_COPY, FALSE);
606 }
607
608 if (HasFlag(PES_NOREAD)) {
609 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_CUT, FALSE);
610 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_COPY, FALSE);
611 }
612
613 pSH->AppendMenuItem(hPopup, 0, L"");
614 pSH->AppendMenuItem(
615 hPopup, WM_PWLEDIT_SELECTALL,
616 pProvider ? pProvider->LoadPopupMenuString(6) : L"&Select All");
617
618 if (m_pEdit->GetTotalWords() == 0) {
619 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_SELECTALL, FALSE);
620 }
621
622 int32_t x, y;
623 PWLtoWnd(ptPopup, x, y);
624 pSH->ClientToScreen(GetAttachedHWnd(), x, y);
625 pSH->SetCursor(FXCT_ARROW);
626 int32_t nCmd = pSH->TrackPopupMenu(hPopup, x, y, GetAttachedHWnd());
627
628 switch (nCmd) {
629 case WM_PWLEDIT_UNDO:
630 Undo();
631 break;
632 case WM_PWLEDIT_REDO:
633 Redo();
634 break;
635 case WM_PWLEDIT_CUT:
636 CutText();
637 break;
638 case WM_PWLEDIT_COPY:
639 CopyText();
640 break;
641 case WM_PWLEDIT_PASTE:
642 PasteText();
643 break;
644 case WM_PWLEDIT_DELETE:
645 Clear();
646 break;
647 case WM_PWLEDIT_SELECTALL:
648 SelectAll();
649 break;
650 case WM_PWLEDIT_SUGGEST + 0:
651 SetSel(m_pEdit->WordPlaceToWordIndex(wrLatin.BeginPos),
652 m_pEdit->WordPlaceToWordIndex(wrLatin.EndPos));
653 ReplaceSel(sSuggestWords[0].UTF8Decode().c_str());
654 break;
655 case WM_PWLEDIT_SUGGEST + 1:
656 SetSel(m_pEdit->WordPlaceToWordIndex(wrLatin.BeginPos),
657 m_pEdit->WordPlaceToWordIndex(wrLatin.EndPos));
658 ReplaceSel(sSuggestWords[1].UTF8Decode().c_str());
659 break;
660 case WM_PWLEDIT_SUGGEST + 2:
661 SetSel(m_pEdit->WordPlaceToWordIndex(wrLatin.BeginPos),
662 m_pEdit->WordPlaceToWordIndex(wrLatin.EndPos));
663 ReplaceSel(sSuggestWords[2].UTF8Decode().c_str());
664 break;
665 case WM_PWLEDIT_SUGGEST + 3:
666 SetSel(m_pEdit->WordPlaceToWordIndex(wrLatin.BeginPos),
667 m_pEdit->WordPlaceToWordIndex(wrLatin.EndPos));
668 ReplaceSel(sSuggestWords[3].UTF8Decode().c_str());
669 break;
670 case WM_PWLEDIT_SUGGEST + 4:
671 SetSel(m_pEdit->WordPlaceToWordIndex(wrLatin.BeginPos),
672 m_pEdit->WordPlaceToWordIndex(wrLatin.EndPos));
673 ReplaceSel(sSuggestWords[4].UTF8Decode().c_str());
674 break;
675 default:
676 break;
677 }
678
679 pSH->DestroyMenu(hPopup);
680
681 return TRUE;
682}
683
684void CPWL_Edit::OnSetFocus() {
685 SetEditCaret(TRUE);
686
687 if (!IsReadOnly()) {
688 if (IPWL_FocusHandler* pFocusHandler = GetFocusHandler())
689 pFocusHandler->OnSetFocus(this);
690 }
691
692 m_bFocus = TRUE;
693}
694
695void CPWL_Edit::OnKillFocus() {
696 ShowVScrollBar(FALSE);
697
698 m_pEdit->SelectNone();
Tom Sepez281a9ea2016-02-26 14:24:28 -0800699 SetCaret(FALSE, CFX_FloatPoint(0.0f, 0.0f), CFX_FloatPoint(0.0f, 0.0f));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700700
701 SetCharSet(0);
702
703 if (!IsReadOnly()) {
704 if (IPWL_FocusHandler* pFocusHandler = GetFocusHandler())
705 pFocusHandler->OnKillFocus(this);
706 }
707
708 m_bFocus = FALSE;
709}
710
711void CPWL_Edit::SetHorzScale(int32_t nHorzScale, FX_BOOL bPaint /* = TRUE*/) {
712 m_pEdit->SetHorzScale(nHorzScale, bPaint);
713}
714
715void CPWL_Edit::SetCharSpace(FX_FLOAT fCharSpace, FX_BOOL bPaint /* = TRUE*/) {
716 m_pEdit->SetCharSpace(fCharSpace, bPaint);
717}
718
719void CPWL_Edit::SetLineLeading(FX_FLOAT fLineLeading,
720 FX_BOOL bPaint /* = TRUE*/) {
721 m_pEdit->SetLineLeading(fLineLeading, bPaint);
722}
723
724CFX_ByteString CPWL_Edit::GetSelectAppearanceStream(
Tom Sepez281a9ea2016-02-26 14:24:28 -0800725 const CFX_FloatPoint& ptOffset) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700726 CPVT_WordRange wr = GetSelectWordRange();
727 return CPWL_Utils::GetEditSelAppStream(m_pEdit, ptOffset, &wr);
728}
729
730CPVT_WordRange CPWL_Edit::GetSelectWordRange() const {
731 if (m_pEdit->IsSelected()) {
732 int32_t nStart = -1;
733 int32_t nEnd = -1;
734
735 m_pEdit->GetSel(nStart, nEnd);
736
737 CPVT_WordPlace wpStart = m_pEdit->WordIndexToWordPlace(nStart);
738 CPVT_WordPlace wpEnd = m_pEdit->WordIndexToWordPlace(nEnd);
739
740 return CPVT_WordRange(wpStart, wpEnd);
741 }
742
743 return CPVT_WordRange();
744}
745
746CFX_ByteString CPWL_Edit::GetTextAppearanceStream(
Tom Sepez281a9ea2016-02-26 14:24:28 -0800747 const CFX_FloatPoint& ptOffset) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700748 CFX_ByteTextBuf sRet;
749 CFX_ByteString sEdit = CPWL_Utils::GetEditAppStream(m_pEdit, ptOffset);
750
751 if (sEdit.GetLength() > 0) {
tsepez28f97ff2016-04-04 16:41:35 -0700752 sRet << "BT\n"
753 << CPWL_Utils::GetColorAppStream(GetTextColor()).AsByteStringC()
754 << sEdit.AsByteStringC() << "ET\n";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700755 }
756
757 return sRet.GetByteString();
758}
759
760CFX_ByteString CPWL_Edit::GetCaretAppearanceStream(
Tom Sepez281a9ea2016-02-26 14:24:28 -0800761 const CFX_FloatPoint& ptOffset) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700762 if (m_pEditCaret)
763 return m_pEditCaret->GetCaretAppearanceStream(ptOffset);
764
765 return CFX_ByteString();
766}
767
Tom Sepez281a9ea2016-02-26 14:24:28 -0800768CFX_FloatPoint CPWL_Edit::GetWordRightBottomPoint(
769 const CPVT_WordPlace& wpWord) {
770 CFX_FloatPoint pt(0.0f, 0.0f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700771
772 if (IFX_Edit_Iterator* pIterator = m_pEdit->GetIterator()) {
773 CPVT_WordPlace wpOld = pIterator->GetAt();
774 pIterator->SetAt(wpWord);
775 CPVT_Word word;
776 if (pIterator->GetWord(word)) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800777 pt = CFX_FloatPoint(word.ptWord.x + word.fWidth,
778 word.ptWord.y + word.fDescent);
Lei Zhang60f507b2015-06-13 00:41:00 -0700779 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700780
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700781 pIterator->SetAt(wpOld);
782 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700783
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700784 return pt;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700785}
786
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700787FX_BOOL CPWL_Edit::IsTextFull() const {
788 return m_pEdit->IsTextFull();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700789}
790
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700791FX_FLOAT CPWL_Edit::GetCharArrayAutoFontSize(CPDF_Font* pFont,
Tom Sepez281a9ea2016-02-26 14:24:28 -0800792 const CFX_FloatRect& rcPlate,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700793 int32_t nCharArray) {
794 if (pFont && !pFont->IsStandardFont()) {
795 FX_RECT rcBBox;
796 pFont->GetFontBBox(rcBBox);
797
Tom Sepez281a9ea2016-02-26 14:24:28 -0800798 CFX_FloatRect rcCell = rcPlate;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700799 FX_FLOAT xdiv = rcCell.Width() / nCharArray * 1000.0f / rcBBox.Width();
800 FX_FLOAT ydiv = -rcCell.Height() * 1000.0f / rcBBox.Height();
801
802 return xdiv < ydiv ? xdiv : ydiv;
803 }
804
805 return 0.0f;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700806}
807
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700808void CPWL_Edit::SetCharArray(int32_t nCharArray) {
809 if (HasFlag(PES_CHARARRAY) && nCharArray > 0) {
810 m_pEdit->SetCharArray(nCharArray);
811 m_pEdit->SetTextOverflow(TRUE);
812
813 if (HasFlag(PWS_AUTOFONTSIZE)) {
814 if (IFX_Edit_FontMap* pFontMap = GetFontMap()) {
815 FX_FLOAT fFontSize = GetCharArrayAutoFontSize(
816 pFontMap->GetPDFFont(0), GetClientRect(), nCharArray);
817 if (fFontSize > 0.0f) {
818 m_pEdit->SetAutoFontSize(FALSE);
819 m_pEdit->SetFontSize(fFontSize);
820 }
821 }
822 }
823 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700824}
825
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700826void CPWL_Edit::SetLimitChar(int32_t nLimitChar) {
827 m_pEdit->SetLimitChar(nLimitChar);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700828}
829
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700830void CPWL_Edit::ReplaceSel(const FX_WCHAR* csText) {
831 m_pEdit->Clear();
832 m_pEdit->InsertText(csText);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700833}
834
Tom Sepez281a9ea2016-02-26 14:24:28 -0800835CFX_FloatRect CPWL_Edit::GetFocusRect() const {
836 return CFX_FloatRect();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700837}
838
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700839void CPWL_Edit::ShowVScrollBar(FX_BOOL bShow) {
840 if (CPWL_ScrollBar* pScroll = GetVScrollBar()) {
841 if (bShow) {
842 if (!pScroll->IsVisible()) {
843 pScroll->SetVisible(TRUE);
Tom Sepez281a9ea2016-02-26 14:24:28 -0800844 CFX_FloatRect rcWindow = GetWindowRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700845 m_rcOldWindow = rcWindow;
846 rcWindow.right += PWL_SCROLLBAR_WIDTH;
847 Move(rcWindow, TRUE, TRUE);
848 }
849 } else {
850 if (pScroll->IsVisible()) {
851 pScroll->SetVisible(FALSE);
852 Move(m_rcOldWindow, TRUE, TRUE);
853 }
854 }
855 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700856}
857
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700858FX_BOOL CPWL_Edit::IsVScrollBarVisible() const {
859 if (CPWL_ScrollBar* pScroll = GetVScrollBar()) {
860 return pScroll->IsVisible();
861 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700862
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700863 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700864}
865
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700866void CPWL_Edit::EnableSpellCheck(FX_BOOL bEnabled) {
867 if (bEnabled)
868 AddFlag(PES_SPELLCHECK);
869 else
870 RemoveFlag(PES_SPELLCHECK);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700871}
872
tsepezc3255f52016-03-25 14:52:27 -0700873FX_BOOL CPWL_Edit::OnKeyDown(uint16_t nChar, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700874 if (m_bMouseDown)
875 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700876
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700877 if (nChar == FWL_VKEY_Delete) {
878 if (m_pFillerNotify) {
879 FX_BOOL bRC = TRUE;
880 FX_BOOL bExit = FALSE;
881 CFX_WideString strChange;
882 CFX_WideString strChangeEx;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700883
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700884 int nSelStart = 0;
885 int nSelEnd = 0;
886 GetSel(nSelStart, nSelEnd);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700887
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700888 if (nSelStart == nSelEnd)
889 nSelEnd = nSelStart + 1;
Lei Zhanga5b47042015-10-19 14:32:16 -0700890 m_pFillerNotify->OnBeforeKeyStroke(GetAttachedData(), strChange,
891 strChangeEx, nSelStart, nSelEnd, TRUE,
892 bRC, bExit, nFlag);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700893 if (!bRC)
894 return FALSE;
895 if (bExit)
896 return FALSE;
897 }
898 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700899
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700900 FX_BOOL bRet = CPWL_EditCtrl::OnKeyDown(nChar, nFlag);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700901
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700902 // In case of implementation swallow the OnKeyDown event.
903 if (IsProceedtoOnChar(nChar, nFlag))
904 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700905
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700906 return bRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700907}
908
909/**
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700910*In case of implementation swallow the OnKeyDown event.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700911*If the event is swallowed, implementation may do other unexpected things, which
912*is not the control means to do.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700913*/
tsepezc3255f52016-03-25 14:52:27 -0700914FX_BOOL CPWL_Edit::IsProceedtoOnChar(uint16_t nKeyCode, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700915 FX_BOOL bCtrl = IsCTRLpressed(nFlag);
916 FX_BOOL bAlt = IsALTpressed(nFlag);
917 if (bCtrl && !bAlt) {
918 // hot keys for edit control.
919 switch (nKeyCode) {
920 case 'C':
921 case 'V':
922 case 'X':
923 case 'A':
924 case 'Z':
925 return TRUE;
926 default:
927 break;
928 }
929 }
930 // control characters.
931 switch (nKeyCode) {
932 case FWL_VKEY_Escape:
933 case FWL_VKEY_Back:
934 case FWL_VKEY_Return:
935 case FWL_VKEY_Space:
936 return TRUE;
937 default:
Lei Zhanga5b47042015-10-19 14:32:16 -0700938 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700939 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700940}
941
tsepezc3255f52016-03-25 14:52:27 -0700942FX_BOOL CPWL_Edit::OnChar(uint16_t nChar, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700943 if (m_bMouseDown)
944 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700945
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700946 FX_BOOL bRC = TRUE;
947 FX_BOOL bExit = FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700948
Lei Zhanga5b47042015-10-19 14:32:16 -0700949 if (!IsCTRLpressed(nFlag)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700950 if (m_pFillerNotify) {
951 CFX_WideString swChange;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700952
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700953 int nSelStart = 0;
954 int nSelEnd = 0;
955 GetSel(nSelStart, nSelEnd);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700956
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700957 switch (nChar) {
958 case FWL_VKEY_Back:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700959 if (nSelStart == nSelEnd)
960 nSelStart = nSelEnd - 1;
961 break;
962 case FWL_VKEY_Return:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700963 break;
964 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700965 swChange += nChar;
966 break;
967 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700968
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700969 CFX_WideString strChangeEx;
Lei Zhanga5b47042015-10-19 14:32:16 -0700970 m_pFillerNotify->OnBeforeKeyStroke(GetAttachedData(), swChange,
971 strChangeEx, nSelStart, nSelEnd, TRUE,
972 bRC, bExit, nFlag);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700973 }
974 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700975
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700976 if (!bRC)
977 return TRUE;
978 if (bExit)
979 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700980
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700981 if (IFX_Edit_FontMap* pFontMap = GetFontMap()) {
982 int32_t nOldCharSet = GetCharSet();
983 int32_t nNewCharSet = pFontMap->CharSetFromUnicode(nChar, DEFAULT_CHARSET);
984 if (nOldCharSet != nNewCharSet) {
985 SetCharSet(nNewCharSet);
986 }
987 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700988
Lei Zhanga5b47042015-10-19 14:32:16 -0700989 return CPWL_EditCtrl::OnChar(nChar, nFlag);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700990}
991
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700992FX_BOOL CPWL_Edit::OnMouseWheel(short zDelta,
Tom Sepez281a9ea2016-02-26 14:24:28 -0800993 const CFX_FloatPoint& point,
tsepezc3255f52016-03-25 14:52:27 -0700994 uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700995 if (HasFlag(PES_MULTILINE)) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800996 CFX_FloatPoint ptScroll = GetScrollPos();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700997
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700998 if (zDelta > 0) {
999 ptScroll.y += GetFontSize();
1000 } else {
1001 ptScroll.y -= GetFontSize();
1002 }
1003 SetScrollPos(ptScroll);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001004
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001005 return TRUE;
1006 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001007
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001008 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001009}
1010
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001011void CPWL_Edit::OnInsertReturn(const CPVT_WordPlace& place,
1012 const CPVT_WordPlace& oldplace) {
1013 if (HasFlag(PES_SPELLCHECK)) {
1014 m_pEdit->RefreshWordRange(CombineWordRange(GetLatinWordsRange(oldplace),
1015 GetLatinWordsRange(place)));
1016 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001017
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001018 if (m_pEditNotify) {
1019 m_pEditNotify->OnInsertReturn(place, oldplace);
1020 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001021}
1022
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001023void CPWL_Edit::OnBackSpace(const CPVT_WordPlace& place,
1024 const CPVT_WordPlace& oldplace) {
1025 if (HasFlag(PES_SPELLCHECK)) {
1026 m_pEdit->RefreshWordRange(CombineWordRange(GetLatinWordsRange(oldplace),
1027 GetLatinWordsRange(place)));
1028 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001029
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001030 if (m_pEditNotify) {
1031 m_pEditNotify->OnBackSpace(place, oldplace);
1032 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001033}
1034
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001035void CPWL_Edit::OnDelete(const CPVT_WordPlace& place,
1036 const CPVT_WordPlace& oldplace) {
1037 if (HasFlag(PES_SPELLCHECK)) {
1038 m_pEdit->RefreshWordRange(CombineWordRange(GetLatinWordsRange(oldplace),
1039 GetLatinWordsRange(place)));
1040 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001041
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001042 if (m_pEditNotify) {
1043 m_pEditNotify->OnDelete(place, oldplace);
1044 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001045}
1046
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001047void CPWL_Edit::OnClear(const CPVT_WordPlace& place,
1048 const CPVT_WordPlace& oldplace) {
1049 if (HasFlag(PES_SPELLCHECK)) {
1050 m_pEdit->RefreshWordRange(CombineWordRange(GetLatinWordsRange(oldplace),
1051 GetLatinWordsRange(place)));
1052 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001053
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001054 if (m_pEditNotify) {
1055 m_pEditNotify->OnClear(place, oldplace);
1056 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001057}
1058
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001059void CPWL_Edit::OnInsertWord(const CPVT_WordPlace& place,
1060 const CPVT_WordPlace& oldplace) {
1061 if (HasFlag(PES_SPELLCHECK)) {
1062 m_pEdit->RefreshWordRange(CombineWordRange(GetLatinWordsRange(oldplace),
1063 GetLatinWordsRange(place)));
1064 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001065
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001066 if (m_pEditNotify) {
1067 m_pEditNotify->OnInsertWord(place, oldplace);
1068 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001069}
1070
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001071void CPWL_Edit::OnSetText(const CPVT_WordPlace& place,
1072 const CPVT_WordPlace& oldplace) {}
1073
1074void CPWL_Edit::OnInsertText(const CPVT_WordPlace& place,
1075 const CPVT_WordPlace& oldplace) {
1076 if (HasFlag(PES_SPELLCHECK)) {
1077 m_pEdit->RefreshWordRange(CombineWordRange(GetLatinWordsRange(oldplace),
1078 GetLatinWordsRange(place)));
1079 }
1080
1081 if (m_pEditNotify) {
1082 m_pEditNotify->OnInsertText(place, oldplace);
1083 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001084}
1085
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001086void CPWL_Edit::OnAddUndo(IFX_Edit_UndoItem* pUndoItem) {
1087 if (m_pEditNotify) {
1088 m_pEditNotify->OnAddUndo(this);
1089 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001090}
1091
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001092CPVT_WordRange CPWL_Edit::CombineWordRange(const CPVT_WordRange& wr1,
1093 const CPVT_WordRange& wr2) {
1094 CPVT_WordRange wrRet;
1095
1096 if (wr1.BeginPos.WordCmp(wr2.BeginPos) < 0) {
1097 wrRet.BeginPos = wr1.BeginPos;
1098 } else {
1099 wrRet.BeginPos = wr2.BeginPos;
1100 }
1101
1102 if (wr1.EndPos.WordCmp(wr2.EndPos) < 0) {
1103 wrRet.EndPos = wr2.EndPos;
1104 } else {
1105 wrRet.EndPos = wr1.EndPos;
1106 }
1107
1108 return wrRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001109}
1110
Tom Sepez281a9ea2016-02-26 14:24:28 -08001111CPVT_WordRange CPWL_Edit::GetLatinWordsRange(
1112 const CFX_FloatPoint& point) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001113 return GetSameWordsRange(m_pEdit->SearchWordPlace(point), TRUE, FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001114}
1115
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001116CPVT_WordRange CPWL_Edit::GetLatinWordsRange(
1117 const CPVT_WordPlace& place) const {
1118 return GetSameWordsRange(place, TRUE, FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001119}
1120
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001121CPVT_WordRange CPWL_Edit::GetArabicWordsRange(
1122 const CPVT_WordPlace& place) const {
1123 return GetSameWordsRange(place, FALSE, TRUE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001124}
1125
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001126#define PWL_ISARABICWORD(word) \
1127 ((word >= 0x0600 && word <= 0x06FF) || (word >= 0xFB50 && word <= 0xFEFC))
1128
1129CPVT_WordRange CPWL_Edit::GetSameWordsRange(const CPVT_WordPlace& place,
1130 FX_BOOL bLatin,
1131 FX_BOOL bArabic) const {
1132 CPVT_WordRange range;
1133
1134 if (IFX_Edit_Iterator* pIterator = m_pEdit->GetIterator()) {
1135 CPVT_Word wordinfo;
1136 CPVT_WordPlace wpStart(place), wpEnd(place);
1137 pIterator->SetAt(place);
1138
1139 if (bLatin) {
1140 while (pIterator->NextWord()) {
Lei Zhangc2fb35f2016-01-05 16:46:58 -08001141 if (!pIterator->GetWord(wordinfo) ||
1142 !FX_EDIT_ISLATINWORD(wordinfo.Word)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001143 break;
Lei Zhangc2fb35f2016-01-05 16:46:58 -08001144 }
1145
1146 wpEnd = pIterator->GetAt();
1147 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001148 } else if (bArabic) {
1149 while (pIterator->NextWord()) {
Lei Zhangc2fb35f2016-01-05 16:46:58 -08001150 if (!pIterator->GetWord(wordinfo) || !PWL_ISARABICWORD(wordinfo.Word))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001151 break;
Lei Zhangc2fb35f2016-01-05 16:46:58 -08001152
1153 wpEnd = pIterator->GetAt();
1154 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001155 }
1156
1157 pIterator->SetAt(place);
1158
1159 if (bLatin) {
1160 do {
Lei Zhangc2fb35f2016-01-05 16:46:58 -08001161 if (!pIterator->GetWord(wordinfo) ||
1162 !FX_EDIT_ISLATINWORD(wordinfo.Word)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001163 break;
1164 }
Lei Zhangc2fb35f2016-01-05 16:46:58 -08001165
1166 wpStart = pIterator->GetAt();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001167 } while (pIterator->PrevWord());
1168 } else if (bArabic) {
1169 do {
Lei Zhangc2fb35f2016-01-05 16:46:58 -08001170 if (!pIterator->GetWord(wordinfo) || !PWL_ISARABICWORD(wordinfo.Word))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001171 break;
Lei Zhangc2fb35f2016-01-05 16:46:58 -08001172
1173 wpStart = pIterator->GetAt();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001174 } while (pIterator->PrevWord());
1175 }
1176
1177 range.Set(wpStart, wpEnd);
1178 }
1179
1180 return range;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001181}
1182
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001183void CPWL_Edit::GeneratePageObjects(
Tom Sepez2398d892016-02-17 16:46:26 -08001184 CPDF_PageObjectHolder* pObjectHolder,
Tom Sepez281a9ea2016-02-26 14:24:28 -08001185 const CFX_FloatPoint& ptOffset,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001186 CFX_ArrayTemplate<CPDF_TextObject*>& ObjArray) {
1187 IFX_Edit::GeneratePageObjects(
Tom Sepez2398d892016-02-17 16:46:26 -08001188 pObjectHolder, m_pEdit, ptOffset, NULL,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001189 CPWL_Utils::PWLColorToFXColor(GetTextColor(), GetTransparency()),
1190 ObjArray);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001191}
1192
Tom Sepez2398d892016-02-17 16:46:26 -08001193void CPWL_Edit::GeneratePageObjects(CPDF_PageObjectHolder* pObjectHolder,
Tom Sepez281a9ea2016-02-26 14:24:28 -08001194 const CFX_FloatPoint& ptOffset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001195 CFX_ArrayTemplate<CPDF_TextObject*> ObjArray;
1196 IFX_Edit::GeneratePageObjects(
Tom Sepez2398d892016-02-17 16:46:26 -08001197 pObjectHolder, m_pEdit, ptOffset, NULL,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001198 CPWL_Utils::PWLColorToFXColor(GetTextColor(), GetTransparency()),
1199 ObjArray);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001200}