• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // 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.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "xfa/fwl/cfwl_edit.h"
8 
9 #include <algorithm>
10 #include <memory>
11 #include <utility>
12 #include <vector>
13 
14 #include "third_party/base/ptr_util.h"
15 #include "third_party/base/stl_util.h"
16 #include "xfa/fde/cfde_txtedtengine.h"
17 #include "xfa/fde/fde_gedevice.h"
18 #include "xfa/fde/fde_render.h"
19 #include "xfa/fde/ifde_txtedtpage.h"
20 #include "xfa/fgas/font/cfgas_gefont.h"
21 #include "xfa/fwl/cfwl_app.h"
22 #include "xfa/fwl/cfwl_caret.h"
23 #include "xfa/fwl/cfwl_event.h"
24 #include "xfa/fwl/cfwl_eventcheckword.h"
25 #include "xfa/fwl/cfwl_eventtextchanged.h"
26 #include "xfa/fwl/cfwl_eventvalidate.h"
27 #include "xfa/fwl/cfwl_messagekey.h"
28 #include "xfa/fwl/cfwl_messagemouse.h"
29 #include "xfa/fwl/cfwl_themebackground.h"
30 #include "xfa/fwl/cfwl_themepart.h"
31 #include "xfa/fwl/cfwl_widgetmgr.h"
32 #include "xfa/fwl/ifwl_themeprovider.h"
33 #include "xfa/fxfa/xfa_ffdoc.h"
34 #include "xfa/fxfa/xfa_ffwidget.h"
35 #include "xfa/fxgraphics/cfx_path.h"
36 
37 namespace {
38 
39 const int kEditMargin = 3;
40 
FX_EDIT_ISLATINWORD(FX_WCHAR c)41 bool FX_EDIT_ISLATINWORD(FX_WCHAR c) {
42   return c == 0x2D || (c <= 0x005A && c >= 0x0041) ||
43          (c <= 0x007A && c >= 0x0061) || (c <= 0x02AF && c >= 0x00C0) ||
44          c == 0x0027;
45 }
46 
AddSquigglyPath(CFX_Path * pPathData,FX_FLOAT fStartX,FX_FLOAT fEndX,FX_FLOAT fY,FX_FLOAT fStep)47 void AddSquigglyPath(CFX_Path* pPathData,
48                      FX_FLOAT fStartX,
49                      FX_FLOAT fEndX,
50                      FX_FLOAT fY,
51                      FX_FLOAT fStep) {
52   pPathData->MoveTo(CFX_PointF(fStartX, fY));
53   int i = 1;
54   for (FX_FLOAT fx = fStartX + fStep; fx < fEndX; fx += fStep, ++i)
55     pPathData->LineTo(CFX_PointF(fx, fY + (i & 1) * fStep));
56 }
57 
58 }  // namespace
59 
CFWL_Edit(const CFWL_App * app,std::unique_ptr<CFWL_WidgetProperties> properties,CFWL_Widget * pOuter)60 CFWL_Edit::CFWL_Edit(const CFWL_App* app,
61                      std::unique_ptr<CFWL_WidgetProperties> properties,
62                      CFWL_Widget* pOuter)
63     : CFWL_Widget(app, std::move(properties), pOuter),
64       m_fVAlignOffset(0.0f),
65       m_fScrollOffsetX(0.0f),
66       m_fScrollOffsetY(0.0f),
67       m_bLButtonDown(false),
68       m_nSelStart(0),
69       m_nLimit(-1),
70       m_fFontSize(0),
71       m_bSetRange(false),
72       m_iMax(0xFFFFFFF),
73       m_iCurRecord(-1),
74       m_iMaxRecord(128) {
75   m_rtClient.Reset();
76   m_rtEngine.Reset();
77   m_rtStatic.Reset();
78 
79   InitCaret();
80 }
81 
~CFWL_Edit()82 CFWL_Edit::~CFWL_Edit() {
83   if (m_pProperties->m_dwStates & FWL_WGTSTATE_Focused)
84     HideCaret(nullptr);
85   ClearRecord();
86 }
87 
GetClassID() const88 FWL_Type CFWL_Edit::GetClassID() const {
89   return FWL_Type::Edit;
90 }
91 
GetWidgetRect()92 CFX_RectF CFWL_Edit::GetWidgetRect() {
93   CFX_RectF rect = m_pProperties->m_rtWidget;
94   if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_OuterScrollbar) {
95     IFWL_ThemeProvider* theme = GetAvailableTheme();
96     float scrollbarWidth = theme ? theme->GetScrollBarWidth() : 0.0f;
97     if (IsShowScrollBar(true)) {
98       rect.width += scrollbarWidth;
99       rect.width += kEditMargin;
100     }
101     if (IsShowScrollBar(false)) {
102       rect.height += scrollbarWidth;
103       rect.height += kEditMargin;
104     }
105   }
106   return rect;
107 }
108 
GetAutosizedWidgetRect()109 CFX_RectF CFWL_Edit::GetAutosizedWidgetRect() {
110   CFX_RectF rect;
111   if (m_EdtEngine.GetTextLength() > 0) {
112     CFX_SizeF sz = CalcTextSize(
113         m_EdtEngine.GetText(0, -1), m_pProperties->m_pThemeProvider,
114         !!(m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_MultiLine));
115     rect = CFX_RectF(0, 0, sz);
116   }
117   InflateWidgetRect(rect);
118   return rect;
119 }
120 
SetStates(uint32_t dwStates)121 void CFWL_Edit::SetStates(uint32_t dwStates) {
122   if ((m_pProperties->m_dwStates & FWL_WGTSTATE_Invisible) ||
123       (m_pProperties->m_dwStates & FWL_WGTSTATE_Disabled)) {
124     HideCaret(nullptr);
125   }
126   CFWL_Widget::SetStates(dwStates);
127 }
128 
Update()129 void CFWL_Edit::Update() {
130   if (IsLocked())
131     return;
132   if (!m_pProperties->m_pThemeProvider)
133     m_pProperties->m_pThemeProvider = GetAvailableTheme();
134 
135   Layout();
136   if (m_rtClient.IsEmpty())
137     return;
138 
139   UpdateEditEngine();
140   UpdateVAlignment();
141   UpdateScroll();
142   InitCaret();
143 }
144 
HitTest(const CFX_PointF & point)145 FWL_WidgetHit CFWL_Edit::HitTest(const CFX_PointF& point) {
146   if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_OuterScrollbar) {
147     if (IsShowScrollBar(true)) {
148       if (m_pVertScrollBar->GetWidgetRect().Contains(point))
149         return FWL_WidgetHit::VScrollBar;
150     }
151     if (IsShowScrollBar(false)) {
152       if (m_pHorzScrollBar->GetWidgetRect().Contains(point))
153         return FWL_WidgetHit::HScrollBar;
154     }
155   }
156   if (m_rtClient.Contains(point))
157     return FWL_WidgetHit::Edit;
158   return FWL_WidgetHit::Unknown;
159 }
160 
AddSpellCheckObj(CFX_Path & PathData,int32_t nStart,int32_t nCount,FX_FLOAT fOffSetX,FX_FLOAT fOffSetY)161 void CFWL_Edit::AddSpellCheckObj(CFX_Path& PathData,
162                                  int32_t nStart,
163                                  int32_t nCount,
164                                  FX_FLOAT fOffSetX,
165                                  FX_FLOAT fOffSetY) {
166   FX_FLOAT fStartX = 0.0f;
167   FX_FLOAT fEndX = 0.0f;
168   FX_FLOAT fY = 0.0f;
169   FX_FLOAT fStep = 0.0f;
170   IFDE_TxtEdtPage* pPage = m_EdtEngine.GetPage(0);
171   const FDE_TXTEDTPARAMS* txtEdtParams = m_EdtEngine.GetEditParams();
172   FX_FLOAT fAsent = static_cast<FX_FLOAT>(txtEdtParams->pFont->GetAscent()) *
173                     txtEdtParams->fFontSize / 1000;
174 
175   std::vector<CFX_RectF> rectArray;
176   pPage->CalcRangeRectArray(nStart, nCount, &rectArray);
177 
178   for (const auto& rectText : rectArray) {
179     fY = rectText.top + fAsent + fOffSetY;
180     fStep = txtEdtParams->fFontSize / 16.0f;
181     fStartX = rectText.left + fOffSetX;
182     fEndX = fStartX + rectText.Width();
183     AddSquigglyPath(&PathData, fStartX, fEndX, fY, fStep);
184   }
185 }
186 
DrawSpellCheck(CFX_Graphics * pGraphics,const CFX_Matrix * pMatrix)187 void CFWL_Edit::DrawSpellCheck(CFX_Graphics* pGraphics,
188                                const CFX_Matrix* pMatrix) {
189   pGraphics->SaveGraphState();
190   if (pMatrix)
191     pGraphics->ConcatMatrix(const_cast<CFX_Matrix*>(pMatrix));
192 
193   CFX_Color crLine(0xFFFF0000);
194   CFWL_EventCheckWord checkWordEvent(this);
195   CFX_ByteString sLatinWord;
196   CFX_Path pathSpell;
197   int32_t nStart = 0;
198   FX_FLOAT fOffSetX = m_rtEngine.left - m_fScrollOffsetX;
199   FX_FLOAT fOffSetY = m_rtEngine.top - m_fScrollOffsetY + m_fVAlignOffset;
200   CFX_WideString wsSpell = GetText();
201   int32_t nContentLen = wsSpell.GetLength();
202   for (int i = 0; i < nContentLen; i++) {
203     if (FX_EDIT_ISLATINWORD(wsSpell[i])) {
204       if (sLatinWord.IsEmpty())
205         nStart = i;
206       sLatinWord += (FX_CHAR)wsSpell[i];
207       continue;
208     }
209     checkWordEvent.bsWord = sLatinWord;
210     checkWordEvent.bCheckWord = true;
211     DispatchEvent(&checkWordEvent);
212 
213     if (!sLatinWord.IsEmpty() && !checkWordEvent.bCheckWord) {
214       AddSpellCheckObj(pathSpell, nStart, sLatinWord.GetLength(), fOffSetX,
215                        fOffSetY);
216     }
217     sLatinWord.clear();
218   }
219 
220   checkWordEvent.bsWord = sLatinWord;
221   checkWordEvent.bCheckWord = true;
222   DispatchEvent(&checkWordEvent);
223 
224   if (!sLatinWord.IsEmpty() && !checkWordEvent.bCheckWord) {
225     AddSpellCheckObj(pathSpell, nStart, sLatinWord.GetLength(), fOffSetX,
226                      fOffSetY);
227   }
228   if (!pathSpell.IsEmpty()) {
229     CFX_RectF rtClip = m_rtEngine;
230     CFX_Matrix mt(1, 0, 0, 1, fOffSetX, fOffSetY);
231     if (pMatrix) {
232       pMatrix->TransformRect(rtClip);
233       mt.Concat(*pMatrix);
234     }
235     pGraphics->SetClipRect(rtClip);
236     pGraphics->SetStrokeColor(&crLine);
237     pGraphics->SetLineWidth(0);
238     pGraphics->StrokePath(&pathSpell, nullptr);
239   }
240   pGraphics->RestoreGraphState();
241 }
242 
DrawWidget(CFX_Graphics * pGraphics,const CFX_Matrix * pMatrix)243 void CFWL_Edit::DrawWidget(CFX_Graphics* pGraphics, const CFX_Matrix* pMatrix) {
244   if (!pGraphics)
245     return;
246   if (!m_pProperties->m_pThemeProvider)
247     return;
248   if (m_rtClient.IsEmpty())
249     return;
250 
251   IFWL_ThemeProvider* pTheme = m_pProperties->m_pThemeProvider;
252   if (!m_pWidgetMgr->IsFormDisabled())
253     DrawTextBk(pGraphics, pTheme, pMatrix);
254   DrawContent(pGraphics, pTheme, pMatrix);
255 
256   if ((m_pProperties->m_dwStates & FWL_WGTSTATE_Focused) &&
257       !(m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_ReadOnly)) {
258     DrawSpellCheck(pGraphics, pMatrix);
259   }
260   if (HasBorder())
261     DrawBorder(pGraphics, CFWL_Part::Border, pTheme, pMatrix);
262 }
263 
SetThemeProvider(IFWL_ThemeProvider * pThemeProvider)264 void CFWL_Edit::SetThemeProvider(IFWL_ThemeProvider* pThemeProvider) {
265   if (!pThemeProvider)
266     return;
267   if (m_pHorzScrollBar)
268     m_pHorzScrollBar->SetThemeProvider(pThemeProvider);
269   if (m_pVertScrollBar)
270     m_pVertScrollBar->SetThemeProvider(pThemeProvider);
271   if (m_pCaret)
272     m_pCaret->SetThemeProvider(pThemeProvider);
273   m_pProperties->m_pThemeProvider = pThemeProvider;
274 }
275 
SetText(const CFX_WideString & wsText)276 void CFWL_Edit::SetText(const CFX_WideString& wsText) {
277   m_EdtEngine.SetText(wsText);
278 }
279 
GetTextLength() const280 int32_t CFWL_Edit::GetTextLength() const {
281   return m_EdtEngine.GetTextLength();
282 }
283 
GetText() const284 CFX_WideString CFWL_Edit::GetText() const {
285   return m_EdtEngine.GetText(0, -1);
286 }
287 
ClearText()288 void CFWL_Edit::ClearText() {
289   m_EdtEngine.ClearText();
290 }
291 
AddSelRange(int32_t nStart)292 void CFWL_Edit::AddSelRange(int32_t nStart) {
293   m_EdtEngine.AddSelRange(nStart, -1);
294 }
295 
CountSelRanges() const296 int32_t CFWL_Edit::CountSelRanges() const {
297   return m_EdtEngine.CountSelRanges();
298 }
299 
GetSelRange(int32_t nIndex,int32_t * nStart) const300 int32_t CFWL_Edit::GetSelRange(int32_t nIndex, int32_t* nStart) const {
301   return m_EdtEngine.GetSelRange(nIndex, nStart);
302 }
303 
ClearSelections()304 void CFWL_Edit::ClearSelections() {
305   m_EdtEngine.ClearSelection();
306 }
307 
GetLimit() const308 int32_t CFWL_Edit::GetLimit() const {
309   return m_nLimit;
310 }
311 
SetLimit(int32_t nLimit)312 void CFWL_Edit::SetLimit(int32_t nLimit) {
313   m_nLimit = nLimit;
314   m_EdtEngine.SetLimit(nLimit);
315 }
316 
SetAliasChar(FX_WCHAR wAlias)317 void CFWL_Edit::SetAliasChar(FX_WCHAR wAlias) {
318   m_EdtEngine.SetAliasChar(wAlias);
319 }
320 
Copy(CFX_WideString & wsCopy)321 bool CFWL_Edit::Copy(CFX_WideString& wsCopy) {
322   int32_t nCount = m_EdtEngine.CountSelRanges();
323   if (nCount == 0)
324     return false;
325 
326   wsCopy.clear();
327   int32_t nStart;
328   int32_t nLength;
329   for (int32_t i = 0; i < nCount; i++) {
330     nLength = m_EdtEngine.GetSelRange(i, &nStart);
331     wsCopy += m_EdtEngine.GetText(nStart, nLength);
332   }
333   return true;
334 }
335 
Cut(CFX_WideString & wsCut)336 bool CFWL_Edit::Cut(CFX_WideString& wsCut) {
337   int32_t nCount = m_EdtEngine.CountSelRanges();
338   if (nCount == 0)
339     return false;
340 
341   wsCut.clear();
342   CFX_WideString wsTemp;
343   int32_t nStart, nLength;
344   for (int32_t i = 0; i < nCount; i++) {
345     nLength = m_EdtEngine.GetSelRange(i, &nStart);
346     wsTemp = m_EdtEngine.GetText(nStart, nLength);
347     wsCut += wsTemp;
348     wsTemp.clear();
349   }
350   m_EdtEngine.Delete(0);
351   return true;
352 }
353 
Paste(const CFX_WideString & wsPaste)354 bool CFWL_Edit::Paste(const CFX_WideString& wsPaste) {
355   int32_t nCaret = m_EdtEngine.GetCaretPos();
356   int32_t iError =
357       m_EdtEngine.Insert(nCaret, wsPaste.c_str(), wsPaste.GetLength());
358   if (iError < 0) {
359     ProcessInsertError(iError);
360     return false;
361   }
362   return true;
363 }
364 
Redo(const IFDE_TxtEdtDoRecord * pRecord)365 bool CFWL_Edit::Redo(const IFDE_TxtEdtDoRecord* pRecord) {
366   return m_EdtEngine.Redo(pRecord);
367 }
368 
Undo(const IFDE_TxtEdtDoRecord * pRecord)369 bool CFWL_Edit::Undo(const IFDE_TxtEdtDoRecord* pRecord) {
370   return m_EdtEngine.Undo(pRecord);
371 }
372 
Undo()373 bool CFWL_Edit::Undo() {
374   if (!CanUndo())
375     return false;
376   return Undo(m_DoRecords[m_iCurRecord--].get());
377 }
378 
Redo()379 bool CFWL_Edit::Redo() {
380   if (!CanRedo())
381     return false;
382   return Redo(m_DoRecords[++m_iCurRecord].get());
383 }
384 
CanUndo()385 bool CFWL_Edit::CanUndo() {
386   return m_iCurRecord >= 0;
387 }
388 
CanRedo()389 bool CFWL_Edit::CanRedo() {
390   return m_iCurRecord < pdfium::CollectionSize<int32_t>(m_DoRecords) - 1;
391 }
392 
SetOuter(CFWL_Widget * pOuter)393 void CFWL_Edit::SetOuter(CFWL_Widget* pOuter) {
394   m_pOuter = pOuter;
395 }
396 
OnCaretChanged()397 void CFWL_Edit::OnCaretChanged() {
398   if (m_rtEngine.IsEmpty())
399     return;
400   if ((m_pProperties->m_dwStates & FWL_WGTSTATE_Focused) == 0)
401     return;
402 
403   bool bRepaintContent = UpdateOffset();
404   UpdateCaret();
405   CFX_RectF rtInvalid;
406   bool bRepaintScroll = false;
407   if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_MultiLine) {
408     CFWL_ScrollBar* pScroll = UpdateScroll();
409     if (pScroll) {
410       rtInvalid = pScroll->GetWidgetRect();
411       bRepaintScroll = true;
412     }
413   }
414   if (bRepaintContent || bRepaintScroll) {
415     if (bRepaintContent)
416       rtInvalid.Union(m_rtEngine);
417     RepaintRect(rtInvalid);
418   }
419 }
420 
OnTextChanged(const FDE_TXTEDT_TEXTCHANGE_INFO & ChangeInfo)421 void CFWL_Edit::OnTextChanged(const FDE_TXTEDT_TEXTCHANGE_INFO& ChangeInfo) {
422   if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_VAlignMask)
423     UpdateVAlignment();
424 
425   CFWL_EventTextChanged event(this);
426   event.wsPrevText = ChangeInfo.wsPrevText;
427   DispatchEvent(&event);
428 
429   LayoutScrollBar();
430   RepaintRect(GetClientRect());
431 }
432 
OnSelChanged()433 void CFWL_Edit::OnSelChanged() {
434   RepaintRect(GetClientRect());
435 }
436 
OnPageLoad(int32_t nPageIndex)437 bool CFWL_Edit::OnPageLoad(int32_t nPageIndex) {
438   IFDE_TxtEdtPage* pPage = m_EdtEngine.GetPage(nPageIndex);
439   if (!pPage)
440     return false;
441 
442   pPage->LoadPage(nullptr, nullptr);
443   return true;
444 }
445 
OnPageUnload(int32_t nPageIndex)446 bool CFWL_Edit::OnPageUnload(int32_t nPageIndex) {
447   IFDE_TxtEdtPage* pPage = m_EdtEngine.GetPage(nPageIndex);
448   if (!pPage)
449     return false;
450 
451   pPage->UnloadPage(nullptr);
452   return true;
453 }
454 
OnAddDoRecord(std::unique_ptr<IFDE_TxtEdtDoRecord> pRecord)455 void CFWL_Edit::OnAddDoRecord(std::unique_ptr<IFDE_TxtEdtDoRecord> pRecord) {
456   AddDoRecord(std::move(pRecord));
457 }
458 
OnValidate(const CFX_WideString & wsText)459 bool CFWL_Edit::OnValidate(const CFX_WideString& wsText) {
460   CFWL_Widget* pDst = GetOuter();
461   if (!pDst)
462     pDst = this;
463 
464   CFWL_EventValidate event(this);
465   event.wsInsert = wsText;
466   event.bValidate = true;
467   DispatchEvent(&event);
468   return event.bValidate;
469 }
470 
SetScrollOffset(FX_FLOAT fScrollOffset)471 void CFWL_Edit::SetScrollOffset(FX_FLOAT fScrollOffset) {
472   m_fScrollOffsetY = fScrollOffset;
473 }
474 
DrawTextBk(CFX_Graphics * pGraphics,IFWL_ThemeProvider * pTheme,const CFX_Matrix * pMatrix)475 void CFWL_Edit::DrawTextBk(CFX_Graphics* pGraphics,
476                            IFWL_ThemeProvider* pTheme,
477                            const CFX_Matrix* pMatrix) {
478   CFWL_ThemeBackground param;
479   param.m_pWidget = this;
480   param.m_iPart = CFWL_Part::Background;
481   param.m_bStaticBackground = false;
482   param.m_dwStates = m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_ReadOnly
483                          ? CFWL_PartState_ReadOnly
484                          : CFWL_PartState_Normal;
485   uint32_t dwStates = (m_pProperties->m_dwStates & FWL_WGTSTATE_Disabled);
486   if (dwStates)
487     param.m_dwStates = CFWL_PartState_Disabled;
488   param.m_pGraphics = pGraphics;
489   param.m_matrix = *pMatrix;
490   param.m_rtPart = m_rtClient;
491   pTheme->DrawBackground(&param);
492 
493   if (!IsShowScrollBar(true) || !IsShowScrollBar(false))
494     return;
495 
496   CFX_RectF rtScroll = m_pHorzScrollBar->GetWidgetRect();
497 
498   CFX_RectF rtStatic(m_rtClient.right() - rtScroll.height,
499                      m_rtClient.bottom() - rtScroll.height, rtScroll.height,
500                      rtScroll.height);
501   param.m_bStaticBackground = true;
502   param.m_bMaximize = true;
503   param.m_rtPart = rtStatic;
504   pTheme->DrawBackground(&param);
505 }
506 
DrawContent(CFX_Graphics * pGraphics,IFWL_ThemeProvider * pTheme,const CFX_Matrix * pMatrix)507 void CFWL_Edit::DrawContent(CFX_Graphics* pGraphics,
508                             IFWL_ThemeProvider* pTheme,
509                             const CFX_Matrix* pMatrix) {
510   IFDE_TxtEdtPage* pPage = m_EdtEngine.GetPage(0);
511   if (!pPage)
512     return;
513 
514   pGraphics->SaveGraphState();
515   if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_CombText)
516     pGraphics->SaveGraphState();
517 
518   CFX_RectF rtClip = m_rtEngine;
519   FX_FLOAT fOffSetX = m_rtEngine.left - m_fScrollOffsetX;
520   FX_FLOAT fOffSetY = m_rtEngine.top - m_fScrollOffsetY + m_fVAlignOffset;
521   CFX_Matrix mt(1, 0, 0, 1, fOffSetX, fOffSetY);
522   if (pMatrix) {
523     pMatrix->TransformRect(rtClip);
524     mt.Concat(*pMatrix);
525   }
526 
527   bool bShowSel = !!(m_pProperties->m_dwStates & FWL_WGTSTATE_Focused);
528   int32_t nSelCount = m_EdtEngine.CountSelRanges();
529   if (bShowSel && nSelCount > 0) {
530     int32_t nPageCharStart = pPage->GetCharStart();
531     int32_t nPageCharCount = pPage->GetCharCount();
532     int32_t nPageCharEnd = nPageCharStart + nPageCharCount - 1;
533     int32_t nCharCount;
534     int32_t nCharStart;
535     std::vector<CFX_RectF> rectArr;
536     for (int32_t i = 0; i < nSelCount; i++) {
537       nCharCount = m_EdtEngine.GetSelRange(i, &nCharStart);
538       int32_t nCharEnd = nCharStart + nCharCount - 1;
539       if (nCharEnd < nPageCharStart || nCharStart > nPageCharEnd)
540         continue;
541 
542       int32_t nBgn = std::max(nCharStart, nPageCharStart);
543       int32_t nEnd = std::min(nCharEnd, nPageCharEnd);
544       pPage->CalcRangeRectArray(nBgn - nPageCharStart, nEnd - nBgn + 1,
545                                 &rectArr);
546     }
547 
548     CFX_Path path;
549     for (auto& rect : rectArr) {
550       rect.left += fOffSetX;
551       rect.top += fOffSetY;
552       path.AddRectangle(rect.left, rect.top, rect.width, rect.height);
553     }
554     pGraphics->SetClipRect(rtClip);
555 
556     CFWL_ThemeBackground param;
557     param.m_pGraphics = pGraphics;
558     param.m_matrix = *pMatrix;
559     param.m_pWidget = this;
560     param.m_iPart = CFWL_Part::Background;
561     param.m_pPath = &path;
562     pTheme->DrawBackground(&param);
563   }
564 
565   CFX_RenderDevice* pRenderDev = pGraphics->GetRenderDevice();
566   if (!pRenderDev)
567     return;
568 
569   std::unique_ptr<CFDE_RenderDevice> pRenderDevice(
570       new CFDE_RenderDevice(pRenderDev, false));
571   std::unique_ptr<CFDE_RenderContext> pRenderContext(new CFDE_RenderContext);
572   pRenderDevice->SetClipRect(rtClip);
573   pRenderContext->StartRender(pRenderDevice.get(), pPage, mt);
574   pRenderContext->DoRender(nullptr);
575 
576   if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_CombText) {
577     pGraphics->RestoreGraphState();
578     CFX_Path path;
579     int32_t iLimit = m_nLimit > 0 ? m_nLimit : 1;
580     FX_FLOAT fStep = m_rtEngine.width / iLimit;
581     FX_FLOAT fLeft = m_rtEngine.left + 1;
582     for (int32_t i = 1; i < iLimit; i++) {
583       fLeft += fStep;
584       path.AddLine(CFX_PointF(fLeft, m_rtClient.top),
585                    CFX_PointF(fLeft, m_rtClient.bottom()));
586     }
587 
588     CFWL_ThemeBackground param;
589     param.m_pGraphics = pGraphics;
590     param.m_matrix = *pMatrix;
591     param.m_pWidget = this;
592     param.m_iPart = CFWL_Part::CombTextLine;
593     param.m_pPath = &path;
594     pTheme->DrawBackground(&param);
595   }
596   pGraphics->RestoreGraphState();
597 }
598 
UpdateEditEngine()599 void CFWL_Edit::UpdateEditEngine() {
600   UpdateEditParams();
601   UpdateEditLayout();
602   if (m_nLimit > -1)
603     m_EdtEngine.SetLimit(m_nLimit);
604 }
605 
UpdateEditParams()606 void CFWL_Edit::UpdateEditParams() {
607   FDE_TXTEDTPARAMS params;
608   params.nHorzScale = 100;
609   params.fPlateWidth = m_rtEngine.width;
610   params.fPlateHeight = m_rtEngine.height;
611   if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_CombText)
612     params.dwLayoutStyles |= FDE_TEXTEDITLAYOUT_CombText;
613   if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_LastLineHeight)
614     params.dwLayoutStyles |= FDE_TEXTEDITLAYOUT_LastLineHeight;
615   if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_Validate)
616     params.dwMode |= FDE_TEXTEDITMODE_Validate;
617   if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_Password)
618     params.dwMode |= FDE_TEXTEDITMODE_Password;
619 
620   switch (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_HAlignMask) {
621     case FWL_STYLEEXT_EDT_HNear: {
622       params.dwAlignment |= FDE_TEXTEDITALIGN_Left;
623       break;
624     }
625     case FWL_STYLEEXT_EDT_HCenter: {
626       params.dwAlignment |= FDE_TEXTEDITALIGN_Center;
627       break;
628     }
629     case FWL_STYLEEXT_EDT_HFar: {
630       params.dwAlignment |= FDE_TEXTEDITALIGN_Right;
631       break;
632     }
633     default:
634       break;
635   }
636   switch (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_HAlignModeMask) {
637     case FWL_STYLEEXT_EDT_Justified: {
638       params.dwAlignment |= FDE_TEXTEDITALIGN_Justified;
639       break;
640     }
641     default: {
642       params.dwAlignment |= FDE_TEXTEDITALIGN_Normal;
643       break;
644     }
645   }
646   if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_MultiLine) {
647     params.dwMode |= FDE_TEXTEDITMODE_MultiLines;
648     if ((m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_AutoHScroll) == 0) {
649       params.dwMode |=
650           FDE_TEXTEDITMODE_AutoLineWrap | FDE_TEXTEDITMODE_LimitArea_Horz;
651     }
652     if ((m_pProperties->m_dwStyles & FWL_WGTSTYLE_VScroll) == 0 &&
653         (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_AutoVScroll) == 0) {
654       params.dwMode |= FDE_TEXTEDITMODE_LimitArea_Vert;
655     } else {
656       params.fPlateHeight = 0x00FFFFFF;
657     }
658   } else if ((m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_AutoHScroll) ==
659              0) {
660     params.dwMode |= FDE_TEXTEDITMODE_LimitArea_Horz;
661   }
662   if ((m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_ReadOnly) ||
663       (m_pProperties->m_dwStates & FWL_WGTSTATE_Disabled)) {
664     params.dwMode |= FDE_TEXTEDITMODE_ReadOnly;
665   }
666 
667   IFWL_ThemeProvider* theme = GetAvailableTheme();
668   CFWL_ThemePart part;
669   part.m_pWidget = this;
670   m_fFontSize = theme ? theme->GetFontSize(&part) : FWLTHEME_CAPACITY_FontSize;
671 
672   if (!theme)
673     return;
674 
675   params.dwFontColor = theme->GetTextColor(&part);
676   params.fLineSpace = theme->GetLineHeight(&part);
677 
678   CFX_RetainPtr<CFGAS_GEFont> pFont = theme->GetFont(&part);
679   if (!pFont)
680     return;
681 
682   params.pFont = pFont;
683   params.fFontSize = m_fFontSize;
684   params.nLineCount = (int32_t)(params.fPlateHeight / params.fLineSpace);
685   if (params.nLineCount <= 0)
686     params.nLineCount = 1;
687   params.fTabWidth = params.fFontSize * 1;
688   params.bTabEquidistant = true;
689   params.wLineBreakChar = L'\n';
690   params.nCharRotation = 0;
691   params.pEventSink = this;
692   m_EdtEngine.SetEditParams(params);
693 }
694 
UpdateEditLayout()695 void CFWL_Edit::UpdateEditLayout() {
696   if (m_EdtEngine.GetTextLength() <= 0)
697     m_EdtEngine.SetTextByStream(nullptr);
698 
699   IFDE_TxtEdtPage* pPage = m_EdtEngine.GetPage(0);
700   if (pPage)
701     pPage->UnloadPage(nullptr);
702 
703   m_EdtEngine.StartLayout();
704   m_EdtEngine.DoLayout(nullptr);
705   m_EdtEngine.EndLayout();
706   pPage = m_EdtEngine.GetPage(0);
707   if (pPage)
708     pPage->LoadPage(nullptr, nullptr);
709 }
710 
UpdateOffset()711 bool CFWL_Edit::UpdateOffset() {
712   CFX_RectF rtCaret;
713   m_EdtEngine.GetCaretRect(rtCaret);
714   FX_FLOAT fOffSetX = m_rtEngine.left - m_fScrollOffsetX;
715   FX_FLOAT fOffSetY = m_rtEngine.top - m_fScrollOffsetY + m_fVAlignOffset;
716   rtCaret.Offset(fOffSetX, fOffSetY);
717   const CFX_RectF& rtEidt = m_rtEngine;
718   if (rtEidt.Contains(rtCaret)) {
719     IFDE_TxtEdtPage* pPage = m_EdtEngine.GetPage(0);
720     if (!pPage)
721       return false;
722 
723     CFX_RectF rtFDE = pPage->GetContentsBox();
724     rtFDE.Offset(fOffSetX, fOffSetY);
725     if (rtFDE.right() < rtEidt.right() && m_fScrollOffsetX > 0) {
726       m_fScrollOffsetX += rtFDE.right() - rtEidt.right();
727       m_fScrollOffsetX = std::max(m_fScrollOffsetX, 0.0f);
728     }
729     if (rtFDE.bottom() < rtEidt.bottom() && m_fScrollOffsetY > 0) {
730       m_fScrollOffsetY += rtFDE.bottom() - rtEidt.bottom();
731       m_fScrollOffsetY = std::max(m_fScrollOffsetY, 0.0f);
732     }
733     return false;
734   }
735 
736   FX_FLOAT offsetX = 0.0;
737   FX_FLOAT offsetY = 0.0;
738   if (rtCaret.left < rtEidt.left)
739     offsetX = rtCaret.left - rtEidt.left;
740   if (rtCaret.right() > rtEidt.right())
741     offsetX = rtCaret.right() - rtEidt.right();
742   if (rtCaret.top < rtEidt.top)
743     offsetY = rtCaret.top - rtEidt.top;
744   if (rtCaret.bottom() > rtEidt.bottom())
745     offsetY = rtCaret.bottom() - rtEidt.bottom();
746   m_fScrollOffsetX += offsetX;
747   m_fScrollOffsetY += offsetY;
748   if (m_fFontSize > m_rtEngine.height)
749     m_fScrollOffsetY = 0;
750   return true;
751 }
752 
UpdateOffset(CFWL_ScrollBar * pScrollBar,FX_FLOAT fPosChanged)753 bool CFWL_Edit::UpdateOffset(CFWL_ScrollBar* pScrollBar, FX_FLOAT fPosChanged) {
754   if (pScrollBar == m_pHorzScrollBar.get())
755     m_fScrollOffsetX += fPosChanged;
756   else
757     m_fScrollOffsetY += fPosChanged;
758   return true;
759 }
760 
UpdateVAlignment()761 void CFWL_Edit::UpdateVAlignment() {
762   IFDE_TxtEdtPage* pPage = m_EdtEngine.GetPage(0);
763   if (!pPage)
764     return;
765 
766   const CFX_RectF& rtFDE = pPage->GetContentsBox();
767   FX_FLOAT fOffsetY = 0.0f;
768   FX_FLOAT fSpaceAbove = 0.0f;
769   FX_FLOAT fSpaceBelow = 0.0f;
770   IFWL_ThemeProvider* theme = GetAvailableTheme();
771   if (theme) {
772     CFWL_ThemePart part;
773     part.m_pWidget = this;
774 
775     CFX_SizeF pSpace = theme->GetSpaceAboveBelow(&part);
776     fSpaceAbove = pSpace.width;
777     fSpaceBelow = pSpace.height;
778   }
779   if (fSpaceAbove < 0.1f)
780     fSpaceAbove = 0;
781   if (fSpaceBelow < 0.1f)
782     fSpaceBelow = 0;
783 
784   if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_VCenter) {
785     fOffsetY = (m_rtEngine.height - rtFDE.height) / 2;
786     if (fOffsetY < (fSpaceAbove + fSpaceBelow) / 2 &&
787         fSpaceAbove < fSpaceBelow) {
788       return;
789     }
790     fOffsetY += (fSpaceAbove - fSpaceBelow) / 2;
791   } else if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_VFar) {
792     fOffsetY = (m_rtEngine.height - rtFDE.height);
793     fOffsetY -= fSpaceBelow;
794   } else {
795     fOffsetY += fSpaceAbove;
796   }
797   m_fVAlignOffset = std::max(fOffsetY, 0.0f);
798 }
799 
UpdateCaret()800 void CFWL_Edit::UpdateCaret() {
801   CFX_RectF rtFDE;
802   m_EdtEngine.GetCaretRect(rtFDE);
803 
804   rtFDE.Offset(m_rtEngine.left - m_fScrollOffsetX,
805                m_rtEngine.top - m_fScrollOffsetY + m_fVAlignOffset);
806   CFX_RectF rtCaret(rtFDE.left, rtFDE.top, rtFDE.width, rtFDE.height);
807 
808   CFX_RectF rtClient = GetClientRect();
809   rtCaret.Intersect(rtClient);
810   if (rtCaret.left > rtClient.right()) {
811     FX_FLOAT right = rtCaret.right();
812     rtCaret.left = rtClient.right() - 1;
813     rtCaret.width = right - rtCaret.left;
814   }
815 
816   if (m_pProperties->m_dwStates & FWL_WGTSTATE_Focused && !rtCaret.IsEmpty())
817     ShowCaret(&rtCaret);
818   else
819     HideCaret(&rtCaret);
820 }
821 
UpdateScroll()822 CFWL_ScrollBar* CFWL_Edit::UpdateScroll() {
823   bool bShowHorz =
824       m_pHorzScrollBar &&
825       ((m_pHorzScrollBar->GetStates() & FWL_WGTSTATE_Invisible) == 0);
826   bool bShowVert =
827       m_pVertScrollBar &&
828       ((m_pVertScrollBar->GetStates() & FWL_WGTSTATE_Invisible) == 0);
829   if (!bShowHorz && !bShowVert)
830     return nullptr;
831 
832   IFDE_TxtEdtPage* pPage = m_EdtEngine.GetPage(0);
833   if (!pPage)
834     return nullptr;
835 
836   const CFX_RectF& rtFDE = pPage->GetContentsBox();
837   CFWL_ScrollBar* pRepaint = nullptr;
838   if (bShowHorz) {
839     CFX_RectF rtScroll = m_pHorzScrollBar->GetWidgetRect();
840     if (rtScroll.width < rtFDE.width) {
841       m_pHorzScrollBar->LockUpdate();
842       FX_FLOAT fRange = rtFDE.width - rtScroll.width;
843       m_pHorzScrollBar->SetRange(0.0f, fRange);
844 
845       FX_FLOAT fPos = std::min(std::max(m_fScrollOffsetX, 0.0f), fRange);
846       m_pHorzScrollBar->SetPos(fPos);
847       m_pHorzScrollBar->SetTrackPos(fPos);
848       m_pHorzScrollBar->SetPageSize(rtScroll.width);
849       m_pHorzScrollBar->SetStepSize(rtScroll.width / 10);
850       m_pHorzScrollBar->RemoveStates(FWL_WGTSTATE_Disabled);
851       m_pHorzScrollBar->UnlockUpdate();
852       m_pHorzScrollBar->Update();
853       pRepaint = m_pHorzScrollBar.get();
854     } else if ((m_pHorzScrollBar->GetStates() & FWL_WGTSTATE_Disabled) == 0) {
855       m_pHorzScrollBar->LockUpdate();
856       m_pHorzScrollBar->SetRange(0, -1);
857       m_pHorzScrollBar->SetStates(FWL_WGTSTATE_Disabled);
858       m_pHorzScrollBar->UnlockUpdate();
859       m_pHorzScrollBar->Update();
860       pRepaint = m_pHorzScrollBar.get();
861     }
862   }
863 
864   if (bShowVert) {
865     CFX_RectF rtScroll = m_pVertScrollBar->GetWidgetRect();
866     if (rtScroll.height < rtFDE.height) {
867       m_pVertScrollBar->LockUpdate();
868       FX_FLOAT fStep = m_EdtEngine.GetEditParams()->fLineSpace;
869       FX_FLOAT fRange = std::max(rtFDE.height - m_rtEngine.height, fStep);
870 
871       m_pVertScrollBar->SetRange(0.0f, fRange);
872       FX_FLOAT fPos = std::min(std::max(m_fScrollOffsetY, 0.0f), fRange);
873       m_pVertScrollBar->SetPos(fPos);
874       m_pVertScrollBar->SetTrackPos(fPos);
875       m_pVertScrollBar->SetPageSize(rtScroll.height);
876       m_pVertScrollBar->SetStepSize(fStep);
877       m_pVertScrollBar->RemoveStates(FWL_WGTSTATE_Disabled);
878       m_pVertScrollBar->UnlockUpdate();
879       m_pVertScrollBar->Update();
880       pRepaint = m_pVertScrollBar.get();
881     } else if ((m_pVertScrollBar->GetStates() & FWL_WGTSTATE_Disabled) == 0) {
882       m_pVertScrollBar->LockUpdate();
883       m_pVertScrollBar->SetRange(0, -1);
884       m_pVertScrollBar->SetStates(FWL_WGTSTATE_Disabled);
885       m_pVertScrollBar->UnlockUpdate();
886       m_pVertScrollBar->Update();
887       pRepaint = m_pVertScrollBar.get();
888     }
889   }
890   return pRepaint;
891 }
892 
IsShowScrollBar(bool bVert)893 bool CFWL_Edit::IsShowScrollBar(bool bVert) {
894   bool bShow =
895       (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_ShowScrollbarFocus)
896           ? (m_pProperties->m_dwStates & FWL_WGTSTATE_Focused) ==
897                 FWL_WGTSTATE_Focused
898           : true;
899   if (bVert) {
900     return bShow && (m_pProperties->m_dwStyles & FWL_WGTSTYLE_VScroll) &&
901            (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_MultiLine) &&
902            IsContentHeightOverflow();
903   }
904   return false;
905 }
906 
IsContentHeightOverflow()907 bool CFWL_Edit::IsContentHeightOverflow() {
908   IFDE_TxtEdtPage* pPage = m_EdtEngine.GetPage(0);
909   if (!pPage)
910     return false;
911   return pPage->GetContentsBox().height > m_rtEngine.height + 1.0f;
912 }
913 
AddDoRecord(std::unique_ptr<IFDE_TxtEdtDoRecord> pRecord)914 int32_t CFWL_Edit::AddDoRecord(std::unique_ptr<IFDE_TxtEdtDoRecord> pRecord) {
915   int32_t nCount = pdfium::CollectionSize<int32_t>(m_DoRecords);
916   if (m_iCurRecord == nCount - 1) {
917     if (nCount == m_iMaxRecord) {
918       m_DoRecords.pop_front();
919       m_iCurRecord--;
920     }
921   } else {
922     m_DoRecords.erase(m_DoRecords.begin() + m_iCurRecord + 1,
923                       m_DoRecords.end());
924   }
925 
926   m_DoRecords.push_back(std::move(pRecord));
927   m_iCurRecord = pdfium::CollectionSize<int32_t>(m_DoRecords) - 1;
928   return m_iCurRecord;
929 }
930 
Layout()931 void CFWL_Edit::Layout() {
932   m_rtClient = GetClientRect();
933   m_rtEngine = m_rtClient;
934   IFWL_ThemeProvider* theme = GetAvailableTheme();
935   if (!theme)
936     return;
937 
938   FX_FLOAT fWidth = theme->GetScrollBarWidth();
939   CFWL_ThemePart part;
940   if (!m_pOuter) {
941     part.m_pWidget = this;
942     CFX_RectF pUIMargin = theme->GetUIMargin(&part);
943     m_rtEngine.Deflate(pUIMargin.left, pUIMargin.top, pUIMargin.width,
944                        pUIMargin.height);
945   } else if (m_pOuter->GetClassID() == FWL_Type::DateTimePicker) {
946     part.m_pWidget = m_pOuter;
947     CFX_RectF pUIMargin = theme->GetUIMargin(&part);
948     m_rtEngine.Deflate(pUIMargin.left, pUIMargin.top, pUIMargin.width,
949                        pUIMargin.height);
950   }
951 
952   bool bShowVertScrollbar = IsShowScrollBar(true);
953   bool bShowHorzScrollbar = IsShowScrollBar(false);
954   if (bShowVertScrollbar) {
955     InitVerticalScrollBar();
956 
957     CFX_RectF rtVertScr;
958     if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_OuterScrollbar) {
959       rtVertScr = CFX_RectF(m_rtClient.right() + kEditMargin, m_rtClient.top,
960                             fWidth, m_rtClient.height);
961     } else {
962       rtVertScr = CFX_RectF(m_rtClient.right() - fWidth, m_rtClient.top, fWidth,
963                             m_rtClient.height);
964       if (bShowHorzScrollbar)
965         rtVertScr.height -= fWidth;
966       m_rtEngine.width -= fWidth;
967     }
968 
969     m_pVertScrollBar->SetWidgetRect(rtVertScr);
970     m_pVertScrollBar->RemoveStates(FWL_WGTSTATE_Invisible);
971     m_pVertScrollBar->Update();
972   } else if (m_pVertScrollBar) {
973     m_pVertScrollBar->SetStates(FWL_WGTSTATE_Invisible);
974   }
975 
976   if (bShowHorzScrollbar) {
977     InitHorizontalScrollBar();
978 
979     CFX_RectF rtHoriScr;
980     if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_OuterScrollbar) {
981       rtHoriScr = CFX_RectF(m_rtClient.left, m_rtClient.bottom() + kEditMargin,
982                             m_rtClient.width, fWidth);
983     } else {
984       rtHoriScr = CFX_RectF(m_rtClient.left, m_rtClient.bottom() - fWidth,
985                             m_rtClient.width, fWidth);
986       if (bShowVertScrollbar)
987         rtHoriScr.width -= fWidth;
988       m_rtEngine.height -= fWidth;
989     }
990     m_pHorzScrollBar->SetWidgetRect(rtHoriScr);
991     m_pHorzScrollBar->RemoveStates(FWL_WGTSTATE_Invisible);
992     m_pHorzScrollBar->Update();
993   } else if (m_pHorzScrollBar) {
994     m_pHorzScrollBar->SetStates(FWL_WGTSTATE_Invisible);
995   }
996 }
997 
LayoutScrollBar()998 void CFWL_Edit::LayoutScrollBar() {
999   if ((m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_ShowScrollbarFocus) ==
1000       0) {
1001     return;
1002   }
1003 
1004   bool bShowVertScrollbar = IsShowScrollBar(true);
1005   bool bShowHorzScrollbar = IsShowScrollBar(false);
1006 
1007   IFWL_ThemeProvider* theme = GetAvailableTheme();
1008   FX_FLOAT fWidth = theme ? theme->GetScrollBarWidth() : 0;
1009   if (bShowVertScrollbar) {
1010     if (!m_pVertScrollBar) {
1011       InitVerticalScrollBar();
1012       CFX_RectF rtVertScr;
1013       if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_OuterScrollbar) {
1014         rtVertScr = CFX_RectF(m_rtClient.right() + kEditMargin, m_rtClient.top,
1015                               fWidth, m_rtClient.height);
1016       } else {
1017         rtVertScr = CFX_RectF(m_rtClient.right() - fWidth, m_rtClient.top,
1018                               fWidth, m_rtClient.height);
1019         if (bShowHorzScrollbar)
1020           rtVertScr.height -= fWidth;
1021       }
1022       m_pVertScrollBar->SetWidgetRect(rtVertScr);
1023       m_pVertScrollBar->Update();
1024     }
1025     m_pVertScrollBar->RemoveStates(FWL_WGTSTATE_Invisible);
1026   } else if (m_pVertScrollBar) {
1027     m_pVertScrollBar->SetStates(FWL_WGTSTATE_Invisible);
1028   }
1029 
1030   if (bShowHorzScrollbar) {
1031     if (!m_pHorzScrollBar) {
1032       InitHorizontalScrollBar();
1033       CFX_RectF rtHoriScr;
1034       if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_OuterScrollbar) {
1035         rtHoriScr =
1036             CFX_RectF(m_rtClient.left, m_rtClient.bottom() + kEditMargin,
1037                       m_rtClient.width, fWidth);
1038       } else {
1039         rtHoriScr = CFX_RectF(m_rtClient.left, m_rtClient.bottom() - fWidth,
1040                               m_rtClient.width, fWidth);
1041         if (bShowVertScrollbar)
1042           rtHoriScr.width -= (fWidth);
1043       }
1044       m_pHorzScrollBar->SetWidgetRect(rtHoriScr);
1045       m_pHorzScrollBar->Update();
1046     }
1047     m_pHorzScrollBar->RemoveStates(FWL_WGTSTATE_Invisible);
1048   } else if (m_pHorzScrollBar) {
1049     m_pHorzScrollBar->SetStates(FWL_WGTSTATE_Invisible);
1050   }
1051   if (bShowVertScrollbar || bShowHorzScrollbar)
1052     UpdateScroll();
1053 }
1054 
DeviceToEngine(const CFX_PointF & pt)1055 CFX_PointF CFWL_Edit::DeviceToEngine(const CFX_PointF& pt) {
1056   return pt + CFX_PointF(m_fScrollOffsetX - m_rtEngine.left,
1057                          m_fScrollOffsetY - m_rtEngine.top - m_fVAlignOffset);
1058 }
1059 
InitVerticalScrollBar()1060 void CFWL_Edit::InitVerticalScrollBar() {
1061   if (m_pVertScrollBar)
1062     return;
1063 
1064   auto prop = pdfium::MakeUnique<CFWL_WidgetProperties>();
1065   prop->m_dwStyleExes = FWL_STYLEEXT_SCB_Vert;
1066   prop->m_dwStates = FWL_WGTSTATE_Disabled | FWL_WGTSTATE_Invisible;
1067   prop->m_pParent = this;
1068   prop->m_pThemeProvider = m_pProperties->m_pThemeProvider;
1069   m_pVertScrollBar =
1070       pdfium::MakeUnique<CFWL_ScrollBar>(m_pOwnerApp, std::move(prop), this);
1071 }
1072 
InitHorizontalScrollBar()1073 void CFWL_Edit::InitHorizontalScrollBar() {
1074   if (m_pHorzScrollBar)
1075     return;
1076 
1077   auto prop = pdfium::MakeUnique<CFWL_WidgetProperties>();
1078   prop->m_dwStyleExes = FWL_STYLEEXT_SCB_Horz;
1079   prop->m_dwStates = FWL_WGTSTATE_Disabled | FWL_WGTSTATE_Invisible;
1080   prop->m_pParent = this;
1081   prop->m_pThemeProvider = m_pProperties->m_pThemeProvider;
1082   m_pHorzScrollBar =
1083       pdfium::MakeUnique<CFWL_ScrollBar>(m_pOwnerApp, std::move(prop), this);
1084 }
1085 
ShowCaret(CFX_RectF * pRect)1086 void CFWL_Edit::ShowCaret(CFX_RectF* pRect) {
1087   if (m_pCaret) {
1088     m_pCaret->ShowCaret();
1089     if (!pRect->IsEmpty())
1090       m_pCaret->SetWidgetRect(*pRect);
1091     RepaintRect(m_rtEngine);
1092     return;
1093   }
1094 
1095   CFWL_Widget* pOuter = this;
1096   pRect->Offset(m_pProperties->m_rtWidget.left, m_pProperties->m_rtWidget.top);
1097   while (pOuter->GetOuter()) {
1098     pOuter = pOuter->GetOuter();
1099 
1100     CFX_RectF rtOuter = pOuter->GetWidgetRect();
1101     pRect->Offset(rtOuter.left, rtOuter.top);
1102   }
1103 
1104   CXFA_FFWidget* pXFAWidget = pOuter->GetLayoutItem();
1105   if (!pXFAWidget)
1106     return;
1107 
1108   IXFA_DocEnvironment* pDocEnvironment =
1109       pXFAWidget->GetDoc()->GetDocEnvironment();
1110   if (!pDocEnvironment)
1111     return;
1112 
1113   CFX_RectF rt(*pRect);
1114   pXFAWidget->GetRotateMatrix().TransformRect(rt);
1115   pDocEnvironment->DisplayCaret(pXFAWidget, true, &rt);
1116 }
1117 
HideCaret(CFX_RectF * pRect)1118 void CFWL_Edit::HideCaret(CFX_RectF* pRect) {
1119   if (m_pCaret) {
1120     m_pCaret->HideCaret();
1121     RepaintRect(m_rtEngine);
1122     return;
1123   }
1124 
1125   CFWL_Widget* pOuter = this;
1126   while (pOuter->GetOuter())
1127     pOuter = pOuter->GetOuter();
1128 
1129   CXFA_FFWidget* pXFAWidget = pOuter->GetLayoutItem();
1130   if (!pXFAWidget)
1131     return;
1132 
1133   IXFA_DocEnvironment* pDocEnvironment =
1134       pXFAWidget->GetDoc()->GetDocEnvironment();
1135   if (!pDocEnvironment)
1136     return;
1137 
1138   pDocEnvironment->DisplayCaret(pXFAWidget, false, pRect);
1139 }
1140 
ValidateNumberChar(FX_WCHAR cNum)1141 bool CFWL_Edit::ValidateNumberChar(FX_WCHAR cNum) {
1142   if (!m_bSetRange)
1143     return true;
1144 
1145   CFX_WideString wsText = m_EdtEngine.GetText(0, -1);
1146   if (wsText.IsEmpty()) {
1147     if (cNum == L'0')
1148       return false;
1149     return true;
1150   }
1151 
1152   int32_t caretPos = m_EdtEngine.GetCaretPos();
1153   if (CountSelRanges() == 0) {
1154     if (cNum == L'0' && caretPos == 0)
1155       return false;
1156 
1157     int32_t nLen = wsText.GetLength();
1158     CFX_WideString l = wsText.Mid(0, caretPos);
1159     CFX_WideString r = wsText.Mid(caretPos, nLen - caretPos);
1160     CFX_WideString wsNew = l + cNum + r;
1161     if (wsNew.GetInteger() <= m_iMax)
1162       return true;
1163     return false;
1164   }
1165 
1166   if (wsText.GetInteger() <= m_iMax)
1167     return true;
1168   return false;
1169 }
1170 
InitCaret()1171 void CFWL_Edit::InitCaret() {
1172   if (!m_pCaret)
1173     return;
1174   m_pCaret.reset();
1175 }
1176 
ClearRecord()1177 void CFWL_Edit::ClearRecord() {
1178   m_iCurRecord = -1;
1179   m_DoRecords.clear();
1180 }
1181 
ProcessInsertError(int32_t iError)1182 void CFWL_Edit::ProcessInsertError(int32_t iError) {
1183   if (iError != -2)
1184     return;
1185 
1186   CFWL_Event textFullEvent(CFWL_Event::Type::TextFull, this);
1187   DispatchEvent(&textFullEvent);
1188 }
1189 
OnProcessMessage(CFWL_Message * pMessage)1190 void CFWL_Edit::OnProcessMessage(CFWL_Message* pMessage) {
1191   if (!pMessage)
1192     return;
1193 
1194   switch (pMessage->GetType()) {
1195     case CFWL_Message::Type::SetFocus:
1196       OnFocusChanged(pMessage, true);
1197       break;
1198     case CFWL_Message::Type::KillFocus:
1199       OnFocusChanged(pMessage, false);
1200       break;
1201     case CFWL_Message::Type::Mouse: {
1202       CFWL_MessageMouse* pMsg = static_cast<CFWL_MessageMouse*>(pMessage);
1203       switch (pMsg->m_dwCmd) {
1204         case FWL_MouseCommand::LeftButtonDown:
1205           OnLButtonDown(pMsg);
1206           break;
1207         case FWL_MouseCommand::LeftButtonUp:
1208           OnLButtonUp(pMsg);
1209           break;
1210         case FWL_MouseCommand::LeftButtonDblClk:
1211           OnButtonDblClk(pMsg);
1212           break;
1213         case FWL_MouseCommand::Move:
1214           OnMouseMove(pMsg);
1215           break;
1216         case FWL_MouseCommand::RightButtonDown:
1217           DoButtonDown(pMsg);
1218           break;
1219         default:
1220           break;
1221       }
1222       break;
1223     }
1224     case CFWL_Message::Type::Key: {
1225       CFWL_MessageKey* pKey = static_cast<CFWL_MessageKey*>(pMessage);
1226       if (pKey->m_dwCmd == FWL_KeyCommand::KeyDown)
1227         OnKeyDown(pKey);
1228       else if (pKey->m_dwCmd == FWL_KeyCommand::Char)
1229         OnChar(pKey);
1230       break;
1231     }
1232     default:
1233       break;
1234   }
1235   CFWL_Widget::OnProcessMessage(pMessage);
1236 }
1237 
OnProcessEvent(CFWL_Event * pEvent)1238 void CFWL_Edit::OnProcessEvent(CFWL_Event* pEvent) {
1239   if (!pEvent)
1240     return;
1241   if (pEvent->GetType() != CFWL_Event::Type::Scroll)
1242     return;
1243 
1244   CFWL_Widget* pSrcTarget = pEvent->m_pSrcTarget;
1245   if ((pSrcTarget == m_pVertScrollBar.get() && m_pVertScrollBar) ||
1246       (pSrcTarget == m_pHorzScrollBar.get() && m_pHorzScrollBar)) {
1247     CFWL_EventScroll* pScrollEvent = static_cast<CFWL_EventScroll*>(pEvent);
1248     OnScroll(static_cast<CFWL_ScrollBar*>(pSrcTarget),
1249              pScrollEvent->m_iScrollCode, pScrollEvent->m_fPos);
1250   }
1251 }
1252 
OnDrawWidget(CFX_Graphics * pGraphics,const CFX_Matrix * pMatrix)1253 void CFWL_Edit::OnDrawWidget(CFX_Graphics* pGraphics,
1254                              const CFX_Matrix* pMatrix) {
1255   DrawWidget(pGraphics, pMatrix);
1256 }
1257 
DoButtonDown(CFWL_MessageMouse * pMsg)1258 void CFWL_Edit::DoButtonDown(CFWL_MessageMouse* pMsg) {
1259   if ((m_pProperties->m_dwStates & FWL_WGTSTATE_Focused) == 0)
1260     SetFocus(true);
1261 
1262   IFDE_TxtEdtPage* pPage = m_EdtEngine.GetPage(0);
1263   if (!pPage)
1264     return;
1265 
1266   bool bBefore = true;
1267   int32_t nIndex = pPage->GetCharIndex(DeviceToEngine(pMsg->m_pos), bBefore);
1268   if (nIndex < 0)
1269     nIndex = 0;
1270 
1271   m_EdtEngine.SetCaretPos(nIndex, bBefore);
1272 }
1273 
OnFocusChanged(CFWL_Message * pMsg,bool bSet)1274 void CFWL_Edit::OnFocusChanged(CFWL_Message* pMsg, bool bSet) {
1275   bool bRepaint = false;
1276   if (bSet) {
1277     m_pProperties->m_dwStates |= FWL_WGTSTATE_Focused;
1278 
1279     UpdateVAlignment();
1280     UpdateOffset();
1281     UpdateCaret();
1282   } else if (m_pProperties->m_dwStates & FWL_WGTSTATE_Focused) {
1283     m_pProperties->m_dwStates &= ~FWL_WGTSTATE_Focused;
1284     HideCaret(nullptr);
1285 
1286     int32_t nSel = CountSelRanges();
1287     if (nSel > 0) {
1288       ClearSelections();
1289       bRepaint = true;
1290     }
1291     m_EdtEngine.SetCaretPos(0, true);
1292     UpdateOffset();
1293 
1294     ClearRecord();
1295   }
1296 
1297   LayoutScrollBar();
1298   if (!bRepaint)
1299     return;
1300 
1301   CFX_RectF rtInvalidate(0, 0, m_pProperties->m_rtWidget.width,
1302                          m_pProperties->m_rtWidget.height);
1303   RepaintRect(rtInvalidate);
1304 }
1305 
OnLButtonDown(CFWL_MessageMouse * pMsg)1306 void CFWL_Edit::OnLButtonDown(CFWL_MessageMouse* pMsg) {
1307   if (m_pProperties->m_dwStates & FWL_WGTSTATE_Disabled)
1308     return;
1309 
1310   m_bLButtonDown = true;
1311   SetGrab(true);
1312   DoButtonDown(pMsg);
1313   int32_t nIndex = m_EdtEngine.GetCaretPos();
1314   bool bRepaint = false;
1315   if (m_EdtEngine.CountSelRanges() > 0) {
1316     m_EdtEngine.ClearSelection();
1317     bRepaint = true;
1318   }
1319 
1320   if ((pMsg->m_dwFlags & FWL_KEYFLAG_Shift) && m_nSelStart != nIndex) {
1321     int32_t iStart = std::min(m_nSelStart, nIndex);
1322     int32_t iEnd = std::max(m_nSelStart, nIndex);
1323     m_EdtEngine.AddSelRange(iStart, iEnd - iStart);
1324     bRepaint = true;
1325   } else {
1326     m_nSelStart = nIndex;
1327   }
1328   if (bRepaint)
1329     RepaintRect(m_rtEngine);
1330 }
1331 
OnLButtonUp(CFWL_MessageMouse * pMsg)1332 void CFWL_Edit::OnLButtonUp(CFWL_MessageMouse* pMsg) {
1333   m_bLButtonDown = false;
1334   SetGrab(false);
1335 }
1336 
OnButtonDblClk(CFWL_MessageMouse * pMsg)1337 void CFWL_Edit::OnButtonDblClk(CFWL_MessageMouse* pMsg) {
1338   IFDE_TxtEdtPage* pPage = m_EdtEngine.GetPage(0);
1339   if (!pPage)
1340     return;
1341 
1342   int32_t nCount = 0;
1343   int32_t nIndex = pPage->SelectWord(DeviceToEngine(pMsg->m_pos), nCount);
1344   if (nIndex < 0)
1345     return;
1346 
1347   m_EdtEngine.AddSelRange(nIndex, nCount);
1348   m_EdtEngine.SetCaretPos(nIndex + nCount - 1, false);
1349   RepaintRect(m_rtEngine);
1350 }
1351 
OnMouseMove(CFWL_MessageMouse * pMsg)1352 void CFWL_Edit::OnMouseMove(CFWL_MessageMouse* pMsg) {
1353   if (m_nSelStart == -1 || !m_bLButtonDown)
1354     return;
1355 
1356   IFDE_TxtEdtPage* pPage = m_EdtEngine.GetPage(0);
1357   if (!pPage)
1358     return;
1359 
1360   bool bBefore = true;
1361   int32_t nIndex = pPage->GetCharIndex(DeviceToEngine(pMsg->m_pos), bBefore);
1362   m_EdtEngine.SetCaretPos(nIndex, bBefore);
1363   nIndex = m_EdtEngine.GetCaretPos();
1364   m_EdtEngine.ClearSelection();
1365 
1366   if (nIndex == m_nSelStart)
1367     return;
1368 
1369   int32_t nLen = m_EdtEngine.GetTextLength();
1370   if (m_nSelStart >= nLen)
1371     m_nSelStart = nLen;
1372 
1373   m_EdtEngine.AddSelRange(std::min(m_nSelStart, nIndex),
1374                           FXSYS_abs(nIndex - m_nSelStart));
1375 }
1376 
OnKeyDown(CFWL_MessageKey * pMsg)1377 void CFWL_Edit::OnKeyDown(CFWL_MessageKey* pMsg) {
1378   FDE_TXTEDTMOVECARET MoveCaret = MC_MoveNone;
1379   bool bShift = !!(pMsg->m_dwFlags & FWL_KEYFLAG_Shift);
1380   bool bCtrl = !!(pMsg->m_dwFlags & FWL_KEYFLAG_Ctrl);
1381   uint32_t dwKeyCode = pMsg->m_dwKeyCode;
1382   switch (dwKeyCode) {
1383     case FWL_VKEY_Left: {
1384       MoveCaret = MC_Left;
1385       break;
1386     }
1387     case FWL_VKEY_Right: {
1388       MoveCaret = MC_Right;
1389       break;
1390     }
1391     case FWL_VKEY_Up: {
1392       MoveCaret = MC_Up;
1393       break;
1394     }
1395     case FWL_VKEY_Down: {
1396       MoveCaret = MC_Down;
1397       break;
1398     }
1399     case FWL_VKEY_Home: {
1400       MoveCaret = bCtrl ? MC_Home : MC_LineStart;
1401       break;
1402     }
1403     case FWL_VKEY_End: {
1404       MoveCaret = bCtrl ? MC_End : MC_LineEnd;
1405       break;
1406     }
1407     case FWL_VKEY_Insert:
1408       break;
1409     case FWL_VKEY_Delete: {
1410       if ((m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_ReadOnly) ||
1411           (m_pProperties->m_dwStates & FWL_WGTSTATE_Disabled)) {
1412         break;
1413       }
1414       int32_t nCaret = m_EdtEngine.GetCaretPos();
1415 #if (_FX_OS_ == _FX_MACOSX_)
1416       m_EdtEngine.Delete(nCaret, true);
1417 #else
1418       m_EdtEngine.Delete(nCaret);
1419 #endif
1420       break;
1421     }
1422     case FWL_VKEY_F2:
1423     case FWL_VKEY_Tab:
1424     default:
1425       break;
1426   }
1427   if (MoveCaret != MC_MoveNone)
1428     m_EdtEngine.MoveCaretPos(MoveCaret, bShift, bCtrl);
1429 }
1430 
OnChar(CFWL_MessageKey * pMsg)1431 void CFWL_Edit::OnChar(CFWL_MessageKey* pMsg) {
1432   if ((m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_ReadOnly) ||
1433       (m_pProperties->m_dwStates & FWL_WGTSTATE_Disabled)) {
1434     return;
1435   }
1436 
1437   int32_t iError = 0;
1438   FX_WCHAR c = static_cast<FX_WCHAR>(pMsg->m_dwKeyCode);
1439   int32_t nCaret = m_EdtEngine.GetCaretPos();
1440   switch (c) {
1441     case FWL_VKEY_Back:
1442       m_EdtEngine.Delete(nCaret, true);
1443       break;
1444     case FWL_VKEY_NewLine:
1445     case FWL_VKEY_Escape:
1446       break;
1447     case FWL_VKEY_Tab: {
1448       iError = m_EdtEngine.Insert(nCaret, L"\t", 1);
1449       break;
1450     }
1451     case FWL_VKEY_Return: {
1452       if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_WantReturn) {
1453         iError = m_EdtEngine.Insert(nCaret, L"\n", 1);
1454       }
1455       break;
1456     }
1457     default: {
1458       if (!m_pWidgetMgr->IsFormDisabled()) {
1459         if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_Number) {
1460           if (((pMsg->m_dwKeyCode < FWL_VKEY_0) &&
1461                (pMsg->m_dwKeyCode != 0x2E && pMsg->m_dwKeyCode != 0x2D)) ||
1462               pMsg->m_dwKeyCode > FWL_VKEY_9) {
1463             break;
1464           }
1465           if (!ValidateNumberChar(c))
1466             break;
1467         }
1468       }
1469 #if (_FX_OS_ == _FX_MACOSX_)
1470       if (pMsg->m_dwFlags & FWL_KEYFLAG_Command)
1471 #else
1472       if (pMsg->m_dwFlags & FWL_KEYFLAG_Ctrl)
1473 #endif
1474       {
1475         break;
1476       }
1477       iError = m_EdtEngine.Insert(nCaret, &c, 1);
1478       break;
1479     }
1480   }
1481   if (iError < 0)
1482     ProcessInsertError(iError);
1483 }
1484 
OnScroll(CFWL_ScrollBar * pScrollBar,CFWL_EventScroll::Code dwCode,FX_FLOAT fPos)1485 bool CFWL_Edit::OnScroll(CFWL_ScrollBar* pScrollBar,
1486                          CFWL_EventScroll::Code dwCode,
1487                          FX_FLOAT fPos) {
1488   CFX_SizeF fs;
1489   pScrollBar->GetRange(&fs.width, &fs.height);
1490   FX_FLOAT iCurPos = pScrollBar->GetPos();
1491   FX_FLOAT fStep = pScrollBar->GetStepSize();
1492   switch (dwCode) {
1493     case CFWL_EventScroll::Code::Min: {
1494       fPos = fs.width;
1495       break;
1496     }
1497     case CFWL_EventScroll::Code::Max: {
1498       fPos = fs.height;
1499       break;
1500     }
1501     case CFWL_EventScroll::Code::StepBackward: {
1502       fPos -= fStep;
1503       if (fPos < fs.width + fStep / 2) {
1504         fPos = fs.width;
1505       }
1506       break;
1507     }
1508     case CFWL_EventScroll::Code::StepForward: {
1509       fPos += fStep;
1510       if (fPos > fs.height - fStep / 2) {
1511         fPos = fs.height;
1512       }
1513       break;
1514     }
1515     case CFWL_EventScroll::Code::PageBackward: {
1516       fPos -= pScrollBar->GetPageSize();
1517       if (fPos < fs.width) {
1518         fPos = fs.width;
1519       }
1520       break;
1521     }
1522     case CFWL_EventScroll::Code::PageForward: {
1523       fPos += pScrollBar->GetPageSize();
1524       if (fPos > fs.height) {
1525         fPos = fs.height;
1526       }
1527       break;
1528     }
1529     case CFWL_EventScroll::Code::Pos:
1530     case CFWL_EventScroll::Code::TrackPos:
1531     case CFWL_EventScroll::Code::None:
1532       break;
1533     case CFWL_EventScroll::Code::EndScroll:
1534       return false;
1535   }
1536   if (iCurPos == fPos)
1537     return true;
1538 
1539   pScrollBar->SetPos(fPos);
1540   pScrollBar->SetTrackPos(fPos);
1541   UpdateOffset(pScrollBar, fPos - iCurPos);
1542   UpdateCaret();
1543 
1544   CFX_RectF rect = GetWidgetRect();
1545   RepaintRect(CFX_RectF(0, 0, rect.width + 2, rect.height + 2));
1546   return true;
1547 }
1548