blob: affb1be87ebbbdca5ee3583433e96c56844ae4fc [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
Lei Zhangc2fb35f2016-01-05 16:46:58 -08007#include "fpdfsdk/include/pdfwindow/PWL_Edit.h"
8
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"
Lei Zhangbde53d22015-11-12 22:21:30 -080015#include "fpdfsdk/include/pdfwindow/PWL_Caret.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080016#include "fpdfsdk/include/pdfwindow/PWL_EditCtrl.h"
17#include "fpdfsdk/include/pdfwindow/PWL_FontMap.h"
18#include "fpdfsdk/include/pdfwindow/PWL_ScrollBar.h"
19#include "fpdfsdk/include/pdfwindow/PWL_Utils.h"
20#include "fpdfsdk/include/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)
268 << " 2 J 0 j\n";
269
270 for (int32_t i = 1; i < nCharArray; i++) {
271 sLine << rcClient.left +
272 ((rcClient.right - rcClient.left) / nCharArray) * i
273 << " " << rcClient.bottom << " m\n"
274 << rcClient.left +
275 ((rcClient.right - rcClient.left) / nCharArray) * i
276 << " " << rcClient.top << " l S\n";
277 }
278
279 sLine << "Q\n";
280 } break;
281 case PBS_DASH: {
282 sLine << "q\n" << GetBorderWidth() << " w\n"
283 << CPWL_Utils::GetColorAppStream(GetBorderColor(), FALSE)
284 << " 2 J 0 j\n"
285 << "[" << GetBorderDash().nDash << " " << GetBorderDash().nGap
286 << "] " << GetBorderDash().nPhase << " d\n";
287
288 for (int32_t i = 1; i < nCharArray; i++) {
289 sLine << rcClient.left +
290 ((rcClient.right - rcClient.left) / nCharArray) * i
291 << " " << rcClient.bottom << " m\n"
292 << rcClient.left +
293 ((rcClient.right - rcClient.left) / nCharArray) * i
294 << " " << rcClient.top << " l S\n";
295 }
296
297 sLine << "Q\n";
298 } break;
299 }
300 }
301
302 sAppStream << sLine;
303
304 CFX_ByteTextBuf sText;
305
Tom Sepez281a9ea2016-02-26 14:24:28 -0800306 CFX_FloatPoint ptOffset = CFX_FloatPoint(0.0f, 0.0f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700307
308 CPVT_WordRange wrWhole = m_pEdit->GetWholeWordRange();
309 CPVT_WordRange wrSelect = GetSelectWordRange();
310 CPVT_WordRange wrVisible =
311 (HasFlag(PES_TEXTOVERFLOW) ? wrWhole : m_pEdit->GetVisibleWordRange());
312 CPVT_WordRange wrSelBefore(wrWhole.BeginPos, wrSelect.BeginPos);
313 CPVT_WordRange wrSelAfter(wrSelect.EndPos, wrWhole.EndPos);
314
315 CPVT_WordRange wrTemp =
316 CPWL_Utils::OverlapWordRange(GetSelectWordRange(), wrVisible);
317 CFX_ByteString sEditSel =
318 CPWL_Utils::GetEditSelAppStream(m_pEdit, ptOffset, &wrTemp);
319
320 if (sEditSel.GetLength() > 0)
321 sText << CPWL_Utils::GetColorAppStream(PWL_DEFAULT_SELBACKCOLOR)
322 << sEditSel;
323
324 wrTemp = CPWL_Utils::OverlapWordRange(wrVisible, wrSelBefore);
325 CFX_ByteString sEditBefore = CPWL_Utils::GetEditAppStream(
326 m_pEdit, ptOffset, &wrTemp, !HasFlag(PES_CHARARRAY),
327 m_pEdit->GetPasswordChar());
328
329 if (sEditBefore.GetLength() > 0)
330 sText << "BT\n" << CPWL_Utils::GetColorAppStream(GetTextColor())
331 << sEditBefore << "ET\n";
332
333 wrTemp = CPWL_Utils::OverlapWordRange(wrVisible, wrSelect);
334 CFX_ByteString sEditMid = CPWL_Utils::GetEditAppStream(
335 m_pEdit, ptOffset, &wrTemp, !HasFlag(PES_CHARARRAY),
336 m_pEdit->GetPasswordChar());
337
338 if (sEditMid.GetLength() > 0)
339 sText << "BT\n"
340 << CPWL_Utils::GetColorAppStream(CPWL_Color(COLORTYPE_GRAY, 1))
341 << sEditMid << "ET\n";
342
343 wrTemp = CPWL_Utils::OverlapWordRange(wrVisible, wrSelAfter);
344 CFX_ByteString sEditAfter = CPWL_Utils::GetEditAppStream(
345 m_pEdit, ptOffset, &wrTemp, !HasFlag(PES_CHARARRAY),
346 m_pEdit->GetPasswordChar());
347
348 if (sEditAfter.GetLength() > 0)
349 sText << "BT\n" << CPWL_Utils::GetColorAppStream(GetTextColor())
350 << sEditAfter << "ET\n";
351
352 if (HasFlag(PES_SPELLCHECK)) {
353 CFX_ByteString sSpellCheck = CPWL_Utils::GetSpellCheckAppStream(
354 m_pEdit, m_pSpellCheck, ptOffset, &wrVisible);
355 if (sSpellCheck.GetLength() > 0)
356 sText << CPWL_Utils::GetColorAppStream(CPWL_Color(COLORTYPE_RGB, 1, 0, 0),
357 FALSE)
358 << sSpellCheck;
359 }
360
361 if (sText.GetLength() > 0) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800362 CFX_FloatRect rcClient = GetClientRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700363 sAppStream << "q\n/Tx BMC\n";
364
365 if (!HasFlag(PES_TEXTOVERFLOW))
366 sAppStream << rcClient.left << " " << rcClient.bottom << " "
367 << rcClient.right - rcClient.left << " "
368 << rcClient.top - rcClient.bottom << " re W n\n";
369
370 sAppStream << sText;
371
372 sAppStream << "EMC\nQ\n";
373 }
374}
375
376void CPWL_Edit::DrawThisAppearance(CFX_RenderDevice* pDevice,
Tom Sepez60d909e2015-12-10 15:34:55 -0800377 CFX_Matrix* pUser2Device) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700378 CPWL_Wnd::DrawThisAppearance(pDevice, pUser2Device);
379
Tom Sepez281a9ea2016-02-26 14:24:28 -0800380 CFX_FloatRect rcClient = GetClientRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700381 CFX_ByteTextBuf sLine;
382
383 int32_t nCharArray = m_pEdit->GetCharArray();
384 FX_SAFE_INT32 nCharArraySafe = nCharArray;
385 nCharArraySafe -= 1;
386 nCharArraySafe *= 2;
387
388 if (nCharArray > 0 && nCharArraySafe.IsValid()) {
389 switch (GetBorderStyle()) {
390 case PBS_SOLID: {
391 CFX_GraphStateData gsd;
392 gsd.m_LineWidth = (FX_FLOAT)GetBorderWidth();
393
394 CFX_PathData path;
395 path.SetPointCount(nCharArraySafe.ValueOrDie());
396
397 for (int32_t i = 0; i < nCharArray - 1; i++) {
398 path.SetPoint(
399 i * 2,
400 rcClient.left +
401 ((rcClient.right - rcClient.left) / nCharArray) * (i + 1),
402 rcClient.bottom, FXPT_MOVETO);
403 path.SetPoint(
404 i * 2 + 1,
405 rcClient.left +
406 ((rcClient.right - rcClient.left) / nCharArray) * (i + 1),
407 rcClient.top, FXPT_LINETO);
408 }
409 if (path.GetPointCount() > 0)
410 pDevice->DrawPath(
411 &path, pUser2Device, &gsd, 0,
412 CPWL_Utils::PWLColorToFXColor(GetBorderColor(), 255),
413 FXFILL_ALTERNATE);
414 } break;
415 case PBS_DASH: {
416 CFX_GraphStateData gsd;
417 gsd.m_LineWidth = (FX_FLOAT)GetBorderWidth();
418
419 gsd.SetDashCount(2);
420 gsd.m_DashArray[0] = (FX_FLOAT)GetBorderDash().nDash;
421 gsd.m_DashArray[1] = (FX_FLOAT)GetBorderDash().nGap;
422 gsd.m_DashPhase = (FX_FLOAT)GetBorderDash().nPhase;
423
424 CFX_PathData path;
425 path.SetPointCount(nCharArraySafe.ValueOrDie());
426
427 for (int32_t i = 0; i < nCharArray - 1; i++) {
428 path.SetPoint(
429 i * 2,
430 rcClient.left +
431 ((rcClient.right - rcClient.left) / nCharArray) * (i + 1),
432 rcClient.bottom, FXPT_MOVETO);
433 path.SetPoint(
434 i * 2 + 1,
435 rcClient.left +
436 ((rcClient.right - rcClient.left) / nCharArray) * (i + 1),
437 rcClient.top, FXPT_LINETO);
438 }
439 if (path.GetPointCount() > 0)
440 pDevice->DrawPath(
441 &path, pUser2Device, &gsd, 0,
442 CPWL_Utils::PWLColorToFXColor(GetBorderColor(), 255),
443 FXFILL_ALTERNATE);
444 } break;
445 }
446 }
447
Tom Sepez281a9ea2016-02-26 14:24:28 -0800448 CFX_FloatRect rcClip;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700449 CPVT_WordRange wrRange = m_pEdit->GetVisibleWordRange();
450 CPVT_WordRange* pRange = NULL;
451
452 if (!HasFlag(PES_TEXTOVERFLOW)) {
453 rcClip = GetClientRect();
454 pRange = &wrRange;
455 }
456 IFX_SystemHandler* pSysHandler = GetSystemHandler();
457 IFX_Edit::DrawEdit(
458 pDevice, pUser2Device, m_pEdit,
459 CPWL_Utils::PWLColorToFXColor(GetTextColor(), GetTransparency()),
460 CPWL_Utils::PWLColorToFXColor(GetTextStrokeColor(), GetTransparency()),
Tom Sepez281a9ea2016-02-26 14:24:28 -0800461 rcClip, CFX_FloatPoint(0.0f, 0.0f), pRange, pSysHandler, m_pFormFiller);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700462
463 if (HasFlag(PES_SPELLCHECK)) {
464 CPWL_Utils::DrawEditSpellCheck(pDevice, pUser2Device, m_pEdit, rcClip,
Tom Sepez281a9ea2016-02-26 14:24:28 -0800465 CFX_FloatPoint(0.0f, 0.0f), pRange,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700466 GetCreationParam().pSpellCheck);
467 }
468}
469
Tom Sepez281a9ea2016-02-26 14:24:28 -0800470FX_BOOL CPWL_Edit::OnLButtonDown(const CFX_FloatPoint& point, FX_DWORD nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700471 CPWL_Wnd::OnLButtonDown(point, nFlag);
472
473 if (HasFlag(PES_TEXTOVERFLOW) || ClientHitTest(point)) {
474 if (m_bMouseDown)
475 InvalidateRect();
476
477 m_bMouseDown = TRUE;
478 SetCapture();
479
480 m_pEdit->OnMouseDown(point, IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
481 }
482
483 return TRUE;
484}
485
Tom Sepez281a9ea2016-02-26 14:24:28 -0800486FX_BOOL CPWL_Edit::OnLButtonDblClk(const CFX_FloatPoint& point,
487 FX_DWORD nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700488 CPWL_Wnd::OnLButtonDblClk(point, nFlag);
489
490 if (HasFlag(PES_TEXTOVERFLOW) || ClientHitTest(point)) {
491 m_pEdit->SelectAll();
492 }
493
494 return TRUE;
495}
496
497#define WM_PWLEDIT_UNDO 0x01
498#define WM_PWLEDIT_REDO 0x02
499#define WM_PWLEDIT_CUT 0x03
500#define WM_PWLEDIT_COPY 0x04
501#define WM_PWLEDIT_PASTE 0x05
502#define WM_PWLEDIT_DELETE 0x06
503#define WM_PWLEDIT_SELECTALL 0x07
504#define WM_PWLEDIT_SUGGEST 0x08
505
Tom Sepez281a9ea2016-02-26 14:24:28 -0800506FX_BOOL CPWL_Edit::OnRButtonUp(const CFX_FloatPoint& point, FX_DWORD nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700507 if (m_bMouseDown)
508 return FALSE;
509
510 CPWL_Wnd::OnRButtonUp(point, nFlag);
511
512 if (!HasFlag(PES_TEXTOVERFLOW) && !ClientHitTest(point))
513 return TRUE;
514
515 IFX_SystemHandler* pSH = GetSystemHandler();
516 if (!pSH)
517 return FALSE;
518
519 SetFocus();
520
521 CPVT_WordRange wrLatin = GetLatinWordsRange(point);
522 CFX_WideString swLatin = m_pEdit->GetRangeText(wrLatin);
523
524 FX_HMENU hPopup = pSH->CreatePopupMenu();
525 if (!hPopup)
526 return FALSE;
527
Tom Sepezab277682016-02-17 10:07:21 -0800528 std::vector<CFX_ByteString> sSuggestWords;
Tom Sepez281a9ea2016-02-26 14:24:28 -0800529 CFX_FloatPoint ptPopup = point;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700530
531 if (!IsReadOnly()) {
532 if (HasFlag(PES_SPELLCHECK) && !swLatin.IsEmpty()) {
533 if (m_pSpellCheck) {
534 CFX_ByteString sLatin = CFX_ByteString::FromUnicode(swLatin);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700535 if (!m_pSpellCheck->CheckWord(sLatin)) {
536 m_pSpellCheck->SuggestWords(sLatin, sSuggestWords);
537
Tom Sepezab277682016-02-17 10:07:21 -0800538 int32_t nSuggest = pdfium::CollectionSize<int32_t>(sSuggestWords);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700539 for (int32_t nWord = 0; nWord < nSuggest; nWord++) {
540 pSH->AppendMenuItem(hPopup, WM_PWLEDIT_SUGGEST + nWord,
541 sSuggestWords[nWord].UTF8Decode());
542 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700543 if (nSuggest > 0)
544 pSH->AppendMenuItem(hPopup, 0, L"");
545
546 ptPopup = GetWordRightBottomPoint(wrLatin.EndPos);
547 }
548 }
549 }
550 }
551
552 IPWL_Provider* pProvider = GetProvider();
553
554 if (HasFlag(PES_UNDO)) {
555 pSH->AppendMenuItem(
556 hPopup, WM_PWLEDIT_UNDO,
557 pProvider ? pProvider->LoadPopupMenuString(0) : L"&Undo");
558 pSH->AppendMenuItem(
559 hPopup, WM_PWLEDIT_REDO,
560 pProvider ? pProvider->LoadPopupMenuString(1) : L"&Redo");
561 pSH->AppendMenuItem(hPopup, 0, L"");
562
563 if (!m_pEdit->CanUndo())
564 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_UNDO, FALSE);
565 if (!m_pEdit->CanRedo())
566 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_REDO, FALSE);
567 }
568
569 pSH->AppendMenuItem(hPopup, WM_PWLEDIT_CUT,
570 pProvider ? pProvider->LoadPopupMenuString(2) : L"Cu&t");
571 pSH->AppendMenuItem(hPopup, WM_PWLEDIT_COPY,
572 pProvider ? pProvider->LoadPopupMenuString(3) : L"&Copy");
573 pSH->AppendMenuItem(
574 hPopup, WM_PWLEDIT_PASTE,
575 pProvider ? pProvider->LoadPopupMenuString(4) : L"&Paste");
576 pSH->AppendMenuItem(
577 hPopup, WM_PWLEDIT_DELETE,
578 pProvider ? pProvider->LoadPopupMenuString(5) : L"&Delete");
579
580 CFX_WideString swText = pSH->GetClipboardText(GetAttachedHWnd());
581 if (swText.IsEmpty())
582 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_PASTE, FALSE);
583
584 if (!m_pEdit->IsSelected()) {
585 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_CUT, FALSE);
586 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_COPY, FALSE);
587 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_DELETE, FALSE);
588 }
589
590 if (IsReadOnly()) {
591 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_CUT, FALSE);
592 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_DELETE, FALSE);
593 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_PASTE, FALSE);
594 }
595
596 if (HasFlag(PES_PASSWORD)) {
597 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_CUT, FALSE);
598 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_COPY, FALSE);
599 }
600
601 if (HasFlag(PES_NOREAD)) {
602 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_CUT, FALSE);
603 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_COPY, FALSE);
604 }
605
606 pSH->AppendMenuItem(hPopup, 0, L"");
607 pSH->AppendMenuItem(
608 hPopup, WM_PWLEDIT_SELECTALL,
609 pProvider ? pProvider->LoadPopupMenuString(6) : L"&Select All");
610
611 if (m_pEdit->GetTotalWords() == 0) {
612 pSH->EnableMenuItem(hPopup, WM_PWLEDIT_SELECTALL, FALSE);
613 }
614
615 int32_t x, y;
616 PWLtoWnd(ptPopup, x, y);
617 pSH->ClientToScreen(GetAttachedHWnd(), x, y);
618 pSH->SetCursor(FXCT_ARROW);
619 int32_t nCmd = pSH->TrackPopupMenu(hPopup, x, y, GetAttachedHWnd());
620
621 switch (nCmd) {
622 case WM_PWLEDIT_UNDO:
623 Undo();
624 break;
625 case WM_PWLEDIT_REDO:
626 Redo();
627 break;
628 case WM_PWLEDIT_CUT:
629 CutText();
630 break;
631 case WM_PWLEDIT_COPY:
632 CopyText();
633 break;
634 case WM_PWLEDIT_PASTE:
635 PasteText();
636 break;
637 case WM_PWLEDIT_DELETE:
638 Clear();
639 break;
640 case WM_PWLEDIT_SELECTALL:
641 SelectAll();
642 break;
643 case WM_PWLEDIT_SUGGEST + 0:
644 SetSel(m_pEdit->WordPlaceToWordIndex(wrLatin.BeginPos),
645 m_pEdit->WordPlaceToWordIndex(wrLatin.EndPos));
646 ReplaceSel(sSuggestWords[0].UTF8Decode().c_str());
647 break;
648 case WM_PWLEDIT_SUGGEST + 1:
649 SetSel(m_pEdit->WordPlaceToWordIndex(wrLatin.BeginPos),
650 m_pEdit->WordPlaceToWordIndex(wrLatin.EndPos));
651 ReplaceSel(sSuggestWords[1].UTF8Decode().c_str());
652 break;
653 case WM_PWLEDIT_SUGGEST + 2:
654 SetSel(m_pEdit->WordPlaceToWordIndex(wrLatin.BeginPos),
655 m_pEdit->WordPlaceToWordIndex(wrLatin.EndPos));
656 ReplaceSel(sSuggestWords[2].UTF8Decode().c_str());
657 break;
658 case WM_PWLEDIT_SUGGEST + 3:
659 SetSel(m_pEdit->WordPlaceToWordIndex(wrLatin.BeginPos),
660 m_pEdit->WordPlaceToWordIndex(wrLatin.EndPos));
661 ReplaceSel(sSuggestWords[3].UTF8Decode().c_str());
662 break;
663 case WM_PWLEDIT_SUGGEST + 4:
664 SetSel(m_pEdit->WordPlaceToWordIndex(wrLatin.BeginPos),
665 m_pEdit->WordPlaceToWordIndex(wrLatin.EndPos));
666 ReplaceSel(sSuggestWords[4].UTF8Decode().c_str());
667 break;
668 default:
669 break;
670 }
671
672 pSH->DestroyMenu(hPopup);
673
674 return TRUE;
675}
676
677void CPWL_Edit::OnSetFocus() {
678 SetEditCaret(TRUE);
679
680 if (!IsReadOnly()) {
681 if (IPWL_FocusHandler* pFocusHandler = GetFocusHandler())
682 pFocusHandler->OnSetFocus(this);
683 }
684
685 m_bFocus = TRUE;
686}
687
688void CPWL_Edit::OnKillFocus() {
689 ShowVScrollBar(FALSE);
690
691 m_pEdit->SelectNone();
Tom Sepez281a9ea2016-02-26 14:24:28 -0800692 SetCaret(FALSE, CFX_FloatPoint(0.0f, 0.0f), CFX_FloatPoint(0.0f, 0.0f));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700693
694 SetCharSet(0);
695
696 if (!IsReadOnly()) {
697 if (IPWL_FocusHandler* pFocusHandler = GetFocusHandler())
698 pFocusHandler->OnKillFocus(this);
699 }
700
701 m_bFocus = FALSE;
702}
703
704void CPWL_Edit::SetHorzScale(int32_t nHorzScale, FX_BOOL bPaint /* = TRUE*/) {
705 m_pEdit->SetHorzScale(nHorzScale, bPaint);
706}
707
708void CPWL_Edit::SetCharSpace(FX_FLOAT fCharSpace, FX_BOOL bPaint /* = TRUE*/) {
709 m_pEdit->SetCharSpace(fCharSpace, bPaint);
710}
711
712void CPWL_Edit::SetLineLeading(FX_FLOAT fLineLeading,
713 FX_BOOL bPaint /* = TRUE*/) {
714 m_pEdit->SetLineLeading(fLineLeading, bPaint);
715}
716
717CFX_ByteString CPWL_Edit::GetSelectAppearanceStream(
Tom Sepez281a9ea2016-02-26 14:24:28 -0800718 const CFX_FloatPoint& ptOffset) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700719 CPVT_WordRange wr = GetSelectWordRange();
720 return CPWL_Utils::GetEditSelAppStream(m_pEdit, ptOffset, &wr);
721}
722
723CPVT_WordRange CPWL_Edit::GetSelectWordRange() const {
724 if (m_pEdit->IsSelected()) {
725 int32_t nStart = -1;
726 int32_t nEnd = -1;
727
728 m_pEdit->GetSel(nStart, nEnd);
729
730 CPVT_WordPlace wpStart = m_pEdit->WordIndexToWordPlace(nStart);
731 CPVT_WordPlace wpEnd = m_pEdit->WordIndexToWordPlace(nEnd);
732
733 return CPVT_WordRange(wpStart, wpEnd);
734 }
735
736 return CPVT_WordRange();
737}
738
739CFX_ByteString CPWL_Edit::GetTextAppearanceStream(
Tom Sepez281a9ea2016-02-26 14:24:28 -0800740 const CFX_FloatPoint& ptOffset) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700741 CFX_ByteTextBuf sRet;
742 CFX_ByteString sEdit = CPWL_Utils::GetEditAppStream(m_pEdit, ptOffset);
743
744 if (sEdit.GetLength() > 0) {
745 sRet << "BT\n" << CPWL_Utils::GetColorAppStream(GetTextColor()) << sEdit
746 << "ET\n";
747 }
748
749 return sRet.GetByteString();
750}
751
752CFX_ByteString CPWL_Edit::GetCaretAppearanceStream(
Tom Sepez281a9ea2016-02-26 14:24:28 -0800753 const CFX_FloatPoint& ptOffset) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700754 if (m_pEditCaret)
755 return m_pEditCaret->GetCaretAppearanceStream(ptOffset);
756
757 return CFX_ByteString();
758}
759
Tom Sepez281a9ea2016-02-26 14:24:28 -0800760CFX_FloatPoint CPWL_Edit::GetWordRightBottomPoint(
761 const CPVT_WordPlace& wpWord) {
762 CFX_FloatPoint pt(0.0f, 0.0f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700763
764 if (IFX_Edit_Iterator* pIterator = m_pEdit->GetIterator()) {
765 CPVT_WordPlace wpOld = pIterator->GetAt();
766 pIterator->SetAt(wpWord);
767 CPVT_Word word;
768 if (pIterator->GetWord(word)) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800769 pt = CFX_FloatPoint(word.ptWord.x + word.fWidth,
770 word.ptWord.y + word.fDescent);
Lei Zhang60f507b2015-06-13 00:41:00 -0700771 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700772
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700773 pIterator->SetAt(wpOld);
774 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700775
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700776 return pt;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700777}
778
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700779FX_BOOL CPWL_Edit::IsTextFull() const {
780 return m_pEdit->IsTextFull();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700781}
782
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700783FX_FLOAT CPWL_Edit::GetCharArrayAutoFontSize(CPDF_Font* pFont,
Tom Sepez281a9ea2016-02-26 14:24:28 -0800784 const CFX_FloatRect& rcPlate,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700785 int32_t nCharArray) {
786 if (pFont && !pFont->IsStandardFont()) {
787 FX_RECT rcBBox;
788 pFont->GetFontBBox(rcBBox);
789
Tom Sepez281a9ea2016-02-26 14:24:28 -0800790 CFX_FloatRect rcCell = rcPlate;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700791 FX_FLOAT xdiv = rcCell.Width() / nCharArray * 1000.0f / rcBBox.Width();
792 FX_FLOAT ydiv = -rcCell.Height() * 1000.0f / rcBBox.Height();
793
794 return xdiv < ydiv ? xdiv : ydiv;
795 }
796
797 return 0.0f;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700798}
799
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700800void CPWL_Edit::SetCharArray(int32_t nCharArray) {
801 if (HasFlag(PES_CHARARRAY) && nCharArray > 0) {
802 m_pEdit->SetCharArray(nCharArray);
803 m_pEdit->SetTextOverflow(TRUE);
804
805 if (HasFlag(PWS_AUTOFONTSIZE)) {
806 if (IFX_Edit_FontMap* pFontMap = GetFontMap()) {
807 FX_FLOAT fFontSize = GetCharArrayAutoFontSize(
808 pFontMap->GetPDFFont(0), GetClientRect(), nCharArray);
809 if (fFontSize > 0.0f) {
810 m_pEdit->SetAutoFontSize(FALSE);
811 m_pEdit->SetFontSize(fFontSize);
812 }
813 }
814 }
815 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700816}
817
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700818void CPWL_Edit::SetLimitChar(int32_t nLimitChar) {
819 m_pEdit->SetLimitChar(nLimitChar);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700820}
821
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700822void CPWL_Edit::ReplaceSel(const FX_WCHAR* csText) {
823 m_pEdit->Clear();
824 m_pEdit->InsertText(csText);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700825}
826
Tom Sepez281a9ea2016-02-26 14:24:28 -0800827CFX_FloatRect CPWL_Edit::GetFocusRect() const {
828 return CFX_FloatRect();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700829}
830
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700831void CPWL_Edit::ShowVScrollBar(FX_BOOL bShow) {
832 if (CPWL_ScrollBar* pScroll = GetVScrollBar()) {
833 if (bShow) {
834 if (!pScroll->IsVisible()) {
835 pScroll->SetVisible(TRUE);
Tom Sepez281a9ea2016-02-26 14:24:28 -0800836 CFX_FloatRect rcWindow = GetWindowRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700837 m_rcOldWindow = rcWindow;
838 rcWindow.right += PWL_SCROLLBAR_WIDTH;
839 Move(rcWindow, TRUE, TRUE);
840 }
841 } else {
842 if (pScroll->IsVisible()) {
843 pScroll->SetVisible(FALSE);
844 Move(m_rcOldWindow, TRUE, TRUE);
845 }
846 }
847 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700848}
849
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700850FX_BOOL CPWL_Edit::IsVScrollBarVisible() const {
851 if (CPWL_ScrollBar* pScroll = GetVScrollBar()) {
852 return pScroll->IsVisible();
853 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700854
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700855 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700856}
857
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700858void CPWL_Edit::EnableSpellCheck(FX_BOOL bEnabled) {
859 if (bEnabled)
860 AddFlag(PES_SPELLCHECK);
861 else
862 RemoveFlag(PES_SPELLCHECK);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700863}
864
Tom Sepez62a70f92016-03-21 15:00:20 -0700865FX_BOOL CPWL_Edit::OnKeyDown(uint16_t nChar, FX_DWORD nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700866 if (m_bMouseDown)
867 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700868
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700869 if (nChar == FWL_VKEY_Delete) {
870 if (m_pFillerNotify) {
871 FX_BOOL bRC = TRUE;
872 FX_BOOL bExit = FALSE;
873 CFX_WideString strChange;
874 CFX_WideString strChangeEx;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700875
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700876 int nSelStart = 0;
877 int nSelEnd = 0;
878 GetSel(nSelStart, nSelEnd);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700879
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700880 if (nSelStart == nSelEnd)
881 nSelEnd = nSelStart + 1;
Lei Zhanga5b47042015-10-19 14:32:16 -0700882 m_pFillerNotify->OnBeforeKeyStroke(GetAttachedData(), strChange,
883 strChangeEx, nSelStart, nSelEnd, TRUE,
884 bRC, bExit, nFlag);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700885 if (!bRC)
886 return FALSE;
887 if (bExit)
888 return FALSE;
889 }
890 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700891
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700892 FX_BOOL bRet = CPWL_EditCtrl::OnKeyDown(nChar, nFlag);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700893
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700894 // In case of implementation swallow the OnKeyDown event.
895 if (IsProceedtoOnChar(nChar, nFlag))
896 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700897
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700898 return bRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700899}
900
901/**
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700902*In case of implementation swallow the OnKeyDown event.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700903*If the event is swallowed, implementation may do other unexpected things, which
904*is not the control means to do.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700905*/
Tom Sepez62a70f92016-03-21 15:00:20 -0700906FX_BOOL CPWL_Edit::IsProceedtoOnChar(uint16_t nKeyCode, FX_DWORD nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700907 FX_BOOL bCtrl = IsCTRLpressed(nFlag);
908 FX_BOOL bAlt = IsALTpressed(nFlag);
909 if (bCtrl && !bAlt) {
910 // hot keys for edit control.
911 switch (nKeyCode) {
912 case 'C':
913 case 'V':
914 case 'X':
915 case 'A':
916 case 'Z':
917 return TRUE;
918 default:
919 break;
920 }
921 }
922 // control characters.
923 switch (nKeyCode) {
924 case FWL_VKEY_Escape:
925 case FWL_VKEY_Back:
926 case FWL_VKEY_Return:
927 case FWL_VKEY_Space:
928 return TRUE;
929 default:
Lei Zhanga5b47042015-10-19 14:32:16 -0700930 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700931 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700932}
933
Tom Sepez62a70f92016-03-21 15:00:20 -0700934FX_BOOL CPWL_Edit::OnChar(uint16_t nChar, FX_DWORD nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700935 if (m_bMouseDown)
936 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700937
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700938 FX_BOOL bRC = TRUE;
939 FX_BOOL bExit = FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700940
Lei Zhanga5b47042015-10-19 14:32:16 -0700941 if (!IsCTRLpressed(nFlag)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700942 if (m_pFillerNotify) {
943 CFX_WideString swChange;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700944
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700945 int nSelStart = 0;
946 int nSelEnd = 0;
947 GetSel(nSelStart, nSelEnd);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700948
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700949 switch (nChar) {
950 case FWL_VKEY_Back:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700951 if (nSelStart == nSelEnd)
952 nSelStart = nSelEnd - 1;
953 break;
954 case FWL_VKEY_Return:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700955 break;
956 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700957 swChange += nChar;
958 break;
959 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700960
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700961 CFX_WideString strChangeEx;
Lei Zhanga5b47042015-10-19 14:32:16 -0700962 m_pFillerNotify->OnBeforeKeyStroke(GetAttachedData(), swChange,
963 strChangeEx, nSelStart, nSelEnd, TRUE,
964 bRC, bExit, nFlag);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700965 }
966 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700967
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700968 if (!bRC)
969 return TRUE;
970 if (bExit)
971 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700972
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700973 if (IFX_Edit_FontMap* pFontMap = GetFontMap()) {
974 int32_t nOldCharSet = GetCharSet();
975 int32_t nNewCharSet = pFontMap->CharSetFromUnicode(nChar, DEFAULT_CHARSET);
976 if (nOldCharSet != nNewCharSet) {
977 SetCharSet(nNewCharSet);
978 }
979 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700980
Lei Zhanga5b47042015-10-19 14:32:16 -0700981 return CPWL_EditCtrl::OnChar(nChar, nFlag);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700982}
983
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700984FX_BOOL CPWL_Edit::OnMouseWheel(short zDelta,
Tom Sepez281a9ea2016-02-26 14:24:28 -0800985 const CFX_FloatPoint& point,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700986 FX_DWORD nFlag) {
987 if (HasFlag(PES_MULTILINE)) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800988 CFX_FloatPoint ptScroll = GetScrollPos();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700989
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700990 if (zDelta > 0) {
991 ptScroll.y += GetFontSize();
992 } else {
993 ptScroll.y -= GetFontSize();
994 }
995 SetScrollPos(ptScroll);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700996
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700997 return TRUE;
998 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700999
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001000 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001001}
1002
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001003void CPWL_Edit::OnInsertReturn(const CPVT_WordPlace& place,
1004 const CPVT_WordPlace& oldplace) {
1005 if (HasFlag(PES_SPELLCHECK)) {
1006 m_pEdit->RefreshWordRange(CombineWordRange(GetLatinWordsRange(oldplace),
1007 GetLatinWordsRange(place)));
1008 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001009
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001010 if (m_pEditNotify) {
1011 m_pEditNotify->OnInsertReturn(place, oldplace);
1012 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001013}
1014
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001015void CPWL_Edit::OnBackSpace(const CPVT_WordPlace& place,
1016 const CPVT_WordPlace& oldplace) {
1017 if (HasFlag(PES_SPELLCHECK)) {
1018 m_pEdit->RefreshWordRange(CombineWordRange(GetLatinWordsRange(oldplace),
1019 GetLatinWordsRange(place)));
1020 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001021
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001022 if (m_pEditNotify) {
1023 m_pEditNotify->OnBackSpace(place, oldplace);
1024 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001025}
1026
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001027void CPWL_Edit::OnDelete(const CPVT_WordPlace& place,
1028 const CPVT_WordPlace& oldplace) {
1029 if (HasFlag(PES_SPELLCHECK)) {
1030 m_pEdit->RefreshWordRange(CombineWordRange(GetLatinWordsRange(oldplace),
1031 GetLatinWordsRange(place)));
1032 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001033
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001034 if (m_pEditNotify) {
1035 m_pEditNotify->OnDelete(place, oldplace);
1036 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001037}
1038
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001039void CPWL_Edit::OnClear(const CPVT_WordPlace& place,
1040 const CPVT_WordPlace& oldplace) {
1041 if (HasFlag(PES_SPELLCHECK)) {
1042 m_pEdit->RefreshWordRange(CombineWordRange(GetLatinWordsRange(oldplace),
1043 GetLatinWordsRange(place)));
1044 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001045
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001046 if (m_pEditNotify) {
1047 m_pEditNotify->OnClear(place, oldplace);
1048 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001049}
1050
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001051void CPWL_Edit::OnInsertWord(const CPVT_WordPlace& place,
1052 const CPVT_WordPlace& oldplace) {
1053 if (HasFlag(PES_SPELLCHECK)) {
1054 m_pEdit->RefreshWordRange(CombineWordRange(GetLatinWordsRange(oldplace),
1055 GetLatinWordsRange(place)));
1056 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001057
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001058 if (m_pEditNotify) {
1059 m_pEditNotify->OnInsertWord(place, oldplace);
1060 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001061}
1062
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001063void CPWL_Edit::OnSetText(const CPVT_WordPlace& place,
1064 const CPVT_WordPlace& oldplace) {}
1065
1066void CPWL_Edit::OnInsertText(const CPVT_WordPlace& place,
1067 const CPVT_WordPlace& oldplace) {
1068 if (HasFlag(PES_SPELLCHECK)) {
1069 m_pEdit->RefreshWordRange(CombineWordRange(GetLatinWordsRange(oldplace),
1070 GetLatinWordsRange(place)));
1071 }
1072
1073 if (m_pEditNotify) {
1074 m_pEditNotify->OnInsertText(place, oldplace);
1075 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001076}
1077
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001078void CPWL_Edit::OnAddUndo(IFX_Edit_UndoItem* pUndoItem) {
1079 if (m_pEditNotify) {
1080 m_pEditNotify->OnAddUndo(this);
1081 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001082}
1083
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001084CPVT_WordRange CPWL_Edit::CombineWordRange(const CPVT_WordRange& wr1,
1085 const CPVT_WordRange& wr2) {
1086 CPVT_WordRange wrRet;
1087
1088 if (wr1.BeginPos.WordCmp(wr2.BeginPos) < 0) {
1089 wrRet.BeginPos = wr1.BeginPos;
1090 } else {
1091 wrRet.BeginPos = wr2.BeginPos;
1092 }
1093
1094 if (wr1.EndPos.WordCmp(wr2.EndPos) < 0) {
1095 wrRet.EndPos = wr2.EndPos;
1096 } else {
1097 wrRet.EndPos = wr1.EndPos;
1098 }
1099
1100 return wrRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001101}
1102
Tom Sepez281a9ea2016-02-26 14:24:28 -08001103CPVT_WordRange CPWL_Edit::GetLatinWordsRange(
1104 const CFX_FloatPoint& point) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001105 return GetSameWordsRange(m_pEdit->SearchWordPlace(point), TRUE, FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001106}
1107
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001108CPVT_WordRange CPWL_Edit::GetLatinWordsRange(
1109 const CPVT_WordPlace& place) const {
1110 return GetSameWordsRange(place, TRUE, FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001111}
1112
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001113CPVT_WordRange CPWL_Edit::GetArabicWordsRange(
1114 const CPVT_WordPlace& place) const {
1115 return GetSameWordsRange(place, FALSE, TRUE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001116}
1117
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001118#define PWL_ISARABICWORD(word) \
1119 ((word >= 0x0600 && word <= 0x06FF) || (word >= 0xFB50 && word <= 0xFEFC))
1120
1121CPVT_WordRange CPWL_Edit::GetSameWordsRange(const CPVT_WordPlace& place,
1122 FX_BOOL bLatin,
1123 FX_BOOL bArabic) const {
1124 CPVT_WordRange range;
1125
1126 if (IFX_Edit_Iterator* pIterator = m_pEdit->GetIterator()) {
1127 CPVT_Word wordinfo;
1128 CPVT_WordPlace wpStart(place), wpEnd(place);
1129 pIterator->SetAt(place);
1130
1131 if (bLatin) {
1132 while (pIterator->NextWord()) {
Lei Zhangc2fb35f2016-01-05 16:46:58 -08001133 if (!pIterator->GetWord(wordinfo) ||
1134 !FX_EDIT_ISLATINWORD(wordinfo.Word)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001135 break;
Lei Zhangc2fb35f2016-01-05 16:46:58 -08001136 }
1137
1138 wpEnd = pIterator->GetAt();
1139 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001140 } else if (bArabic) {
1141 while (pIterator->NextWord()) {
Lei Zhangc2fb35f2016-01-05 16:46:58 -08001142 if (!pIterator->GetWord(wordinfo) || !PWL_ISARABICWORD(wordinfo.Word))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001143 break;
Lei Zhangc2fb35f2016-01-05 16:46:58 -08001144
1145 wpEnd = pIterator->GetAt();
1146 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001147 }
1148
1149 pIterator->SetAt(place);
1150
1151 if (bLatin) {
1152 do {
Lei Zhangc2fb35f2016-01-05 16:46:58 -08001153 if (!pIterator->GetWord(wordinfo) ||
1154 !FX_EDIT_ISLATINWORD(wordinfo.Word)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001155 break;
1156 }
Lei Zhangc2fb35f2016-01-05 16:46:58 -08001157
1158 wpStart = pIterator->GetAt();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001159 } while (pIterator->PrevWord());
1160 } else if (bArabic) {
1161 do {
Lei Zhangc2fb35f2016-01-05 16:46:58 -08001162 if (!pIterator->GetWord(wordinfo) || !PWL_ISARABICWORD(wordinfo.Word))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001163 break;
Lei Zhangc2fb35f2016-01-05 16:46:58 -08001164
1165 wpStart = pIterator->GetAt();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001166 } while (pIterator->PrevWord());
1167 }
1168
1169 range.Set(wpStart, wpEnd);
1170 }
1171
1172 return range;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001173}
1174
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001175void CPWL_Edit::GeneratePageObjects(
Tom Sepez2398d892016-02-17 16:46:26 -08001176 CPDF_PageObjectHolder* pObjectHolder,
Tom Sepez281a9ea2016-02-26 14:24:28 -08001177 const CFX_FloatPoint& ptOffset,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001178 CFX_ArrayTemplate<CPDF_TextObject*>& ObjArray) {
1179 IFX_Edit::GeneratePageObjects(
Tom Sepez2398d892016-02-17 16:46:26 -08001180 pObjectHolder, m_pEdit, ptOffset, NULL,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001181 CPWL_Utils::PWLColorToFXColor(GetTextColor(), GetTransparency()),
1182 ObjArray);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001183}
1184
Tom Sepez2398d892016-02-17 16:46:26 -08001185void CPWL_Edit::GeneratePageObjects(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}