• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The PDFium Authors
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 "build/build_config.h"
15 #include "core/fxge/cfx_renderdevice.h"
16 #include "core/fxge/text_char_pos.h"
17 #include "third_party/base/check.h"
18 #include "third_party/base/cxx17_backports.h"
19 #include "third_party/base/numerics/safe_conversions.h"
20 #include "v8/include/cppgc/visitor.h"
21 #include "xfa/fde/cfde_textout.h"
22 #include "xfa/fgas/font/cfgas_gefont.h"
23 #include "xfa/fgas/graphics/cfgas_gegraphics.h"
24 #include "xfa/fgas/graphics/cfgas_gepath.h"
25 #include "xfa/fwl/cfwl_app.h"
26 #include "xfa/fwl/cfwl_caret.h"
27 #include "xfa/fwl/cfwl_event.h"
28 #include "xfa/fwl/cfwl_eventtextwillchange.h"
29 #include "xfa/fwl/cfwl_eventvalidate.h"
30 #include "xfa/fwl/cfwl_messagekey.h"
31 #include "xfa/fwl/cfwl_messagemouse.h"
32 #include "xfa/fwl/cfwl_themebackground.h"
33 #include "xfa/fwl/cfwl_themepart.h"
34 #include "xfa/fwl/cfwl_widgetmgr.h"
35 #include "xfa/fwl/fwl_widgetdef.h"
36 #include "xfa/fwl/ifwl_themeprovider.h"
37 #include "xfa/fwl/theme/cfwl_utils.h"
38 
39 namespace {
40 
41 constexpr int kEditMargin = 3;
42 
43 #if BUILDFLAG(IS_APPLE)
44 constexpr XFA_FWL_KeyFlag kEditingModifier = XFA_FWL_KeyFlag::kCommand;
45 #else
46 constexpr XFA_FWL_KeyFlag kEditingModifier = XFA_FWL_KeyFlag::kCtrl;
47 #endif
48 
49 }  // namespace
50 
CFWL_Edit(CFWL_App * app,const Properties & properties,CFWL_Widget * pOuter)51 CFWL_Edit::CFWL_Edit(CFWL_App* app,
52                      const Properties& properties,
53                      CFWL_Widget* pOuter)
54     : CFWL_Widget(app, properties, pOuter),
55       m_pEditEngine(std::make_unique<CFDE_TextEditEngine>()) {
56   m_pEditEngine->SetDelegate(this);
57 }
58 
59 CFWL_Edit::~CFWL_Edit() = default;
60 
PreFinalize()61 void CFWL_Edit::PreFinalize() {
62   m_pEditEngine->SetDelegate(nullptr);
63   if (m_Properties.m_dwStates & FWL_STATE_WGT_Focused)
64     HideCaret(nullptr);
65   CFWL_Widget::PreFinalize();
66 }
67 
Trace(cppgc::Visitor * visitor) const68 void CFWL_Edit::Trace(cppgc::Visitor* visitor) const {
69   CFWL_Widget::Trace(visitor);
70   visitor->Trace(m_pVertScrollBar);
71   visitor->Trace(m_pCaret);
72 }
73 
GetClassID() const74 FWL_Type CFWL_Edit::GetClassID() const {
75   return FWL_Type::Edit;
76 }
77 
GetWidgetRect()78 CFX_RectF CFWL_Edit::GetWidgetRect() {
79   CFX_RectF rect = m_WidgetRect;
80   if (m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_OuterScrollbar) {
81     float scrollbarWidth = GetThemeProvider()->GetScrollBarWidth();
82     if (IsShowVertScrollBar()) {
83       rect.width += scrollbarWidth;
84       rect.width += kEditMargin;
85     }
86   }
87   return rect;
88 }
89 
GetAutosizedWidgetRect()90 CFX_RectF CFWL_Edit::GetAutosizedWidgetRect() {
91   CFX_RectF rect;
92   if (m_pEditEngine->GetLength() > 0) {
93     CFX_SizeF size = CalcTextSize(
94         m_pEditEngine->GetText(),
95         !!(m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_MultiLine));
96     rect = CFX_RectF(0, 0, size);
97   }
98   InflateWidgetRect(rect);
99   return rect;
100 }
101 
SetStates(uint32_t dwStates)102 void CFWL_Edit::SetStates(uint32_t dwStates) {
103   if ((m_Properties.m_dwStates & FWL_STATE_WGT_Invisible) ||
104       (m_Properties.m_dwStates & FWL_STATE_WGT_Disabled)) {
105     HideCaret(nullptr);
106   }
107   CFWL_Widget::SetStates(dwStates);
108 }
109 
Update()110 void CFWL_Edit::Update() {
111   if (IsLocked())
112     return;
113 
114   Layout();
115   if (m_ClientRect.IsEmpty())
116     return;
117 
118   UpdateEditEngine();
119   UpdateVAlignment();
120   UpdateScroll();
121   InitCaret();
122 }
123 
HitTest(const CFX_PointF & point)124 FWL_WidgetHit CFWL_Edit::HitTest(const CFX_PointF& point) {
125   if (m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_OuterScrollbar) {
126     if (IsShowVertScrollBar()) {
127       if (m_pVertScrollBar->GetWidgetRect().Contains(point))
128         return FWL_WidgetHit::VScrollBar;
129     }
130   }
131   if (m_ClientRect.Contains(point))
132     return FWL_WidgetHit::Edit;
133   return FWL_WidgetHit::Unknown;
134 }
135 
DrawWidget(CFGAS_GEGraphics * pGraphics,const CFX_Matrix & matrix)136 void CFWL_Edit::DrawWidget(CFGAS_GEGraphics* pGraphics,
137                            const CFX_Matrix& matrix) {
138   if (!pGraphics)
139     return;
140 
141   if (m_ClientRect.IsEmpty())
142     return;
143 
144   DrawContent(pGraphics, matrix);
145   if (HasBorder())
146     DrawBorder(pGraphics, CFWL_ThemePart::Part::kBorder, matrix);
147 }
148 
SetText(const WideString & wsText)149 void CFWL_Edit::SetText(const WideString& wsText) {
150   m_pEditEngine->Clear();
151   m_pEditEngine->Insert(0, wsText,
152                         CFDE_TextEditEngine::RecordOperation::kInsertRecord);
153 }
154 
SetTextSkipNotify(const WideString & wsText)155 void CFWL_Edit::SetTextSkipNotify(const WideString& wsText) {
156   m_pEditEngine->Clear();
157   m_pEditEngine->Insert(0, wsText,
158                         CFDE_TextEditEngine::RecordOperation::kSkipNotify);
159 }
160 
GetTextLength() const161 size_t CFWL_Edit::GetTextLength() const {
162   return m_pEditEngine->GetLength();
163 }
164 
GetText() const165 WideString CFWL_Edit::GetText() const {
166   return m_pEditEngine->GetText();
167 }
168 
ClearText()169 void CFWL_Edit::ClearText() {
170   m_pEditEngine->Clear();
171 }
172 
SelectAll()173 void CFWL_Edit::SelectAll() {
174   m_pEditEngine->SelectAll();
175 }
176 
HasSelection() const177 bool CFWL_Edit::HasSelection() const {
178   return m_pEditEngine->HasSelection();
179 }
180 
GetSelection() const181 std::pair<size_t, size_t> CFWL_Edit::GetSelection() const {
182   return m_pEditEngine->GetSelection();
183 }
184 
ClearSelection()185 void CFWL_Edit::ClearSelection() {
186   return m_pEditEngine->ClearSelection();
187 }
188 
GetLimit() const189 int32_t CFWL_Edit::GetLimit() const {
190   return m_nLimit;
191 }
192 
SetLimit(int32_t nLimit)193 void CFWL_Edit::SetLimit(int32_t nLimit) {
194   m_nLimit = nLimit;
195 
196   if (m_nLimit > 0) {
197     m_pEditEngine->SetHasCharacterLimit(true);
198     m_pEditEngine->SetCharacterLimit(nLimit);
199   } else {
200     m_pEditEngine->SetHasCharacterLimit(false);
201   }
202 }
203 
SetAliasChar(wchar_t wAlias)204 void CFWL_Edit::SetAliasChar(wchar_t wAlias) {
205   m_pEditEngine->SetAliasChar(wAlias);
206 }
207 
Copy()208 absl::optional<WideString> CFWL_Edit::Copy() {
209   if (!m_pEditEngine->HasSelection())
210     return absl::nullopt;
211 
212   return m_pEditEngine->GetSelectedText();
213 }
214 
Cut()215 absl::optional<WideString> CFWL_Edit::Cut() {
216   if (!m_pEditEngine->HasSelection())
217     return absl::nullopt;
218 
219   WideString cut_text = m_pEditEngine->DeleteSelectedText();
220   UpdateCaret();
221   return cut_text;
222 }
223 
Paste(const WideString & wsPaste)224 bool CFWL_Edit::Paste(const WideString& wsPaste) {
225   if (m_pEditEngine->HasSelection())
226     m_pEditEngine->ReplaceSelectedText(wsPaste);
227   else
228     m_pEditEngine->Insert(m_CursorPosition, wsPaste);
229 
230   return true;
231 }
232 
Undo()233 bool CFWL_Edit::Undo() {
234   return CanUndo() && m_pEditEngine->Undo();
235 }
236 
Redo()237 bool CFWL_Edit::Redo() {
238   return CanRedo() && m_pEditEngine->Redo();
239 }
240 
CanUndo()241 bool CFWL_Edit::CanUndo() {
242   return m_pEditEngine->CanUndo();
243 }
244 
CanRedo()245 bool CFWL_Edit::CanRedo() {
246   return m_pEditEngine->CanRedo();
247 }
248 
NotifyTextFull()249 void CFWL_Edit::NotifyTextFull() {
250   CFWL_Event evt(CFWL_Event::Type::TextFull, this);
251   DispatchEvent(&evt);
252 }
253 
OnCaretChanged()254 void CFWL_Edit::OnCaretChanged() {
255   if (m_EngineRect.IsEmpty())
256     return;
257   if ((m_Properties.m_dwStates & FWL_STATE_WGT_Focused) == 0)
258     return;
259 
260   bool bRepaintContent = UpdateOffset();
261   UpdateCaret();
262   CFX_RectF rtInvalid;
263   bool bRepaintScroll = false;
264   if (m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_MultiLine) {
265     CFWL_ScrollBar* pScroll = UpdateScroll();
266     if (pScroll) {
267       rtInvalid = pScroll->GetWidgetRect();
268       bRepaintScroll = true;
269     }
270   }
271   if (bRepaintContent || bRepaintScroll) {
272     if (bRepaintContent)
273       rtInvalid.Union(m_EngineRect);
274     RepaintRect(rtInvalid);
275   }
276 }
277 
OnTextWillChange(CFDE_TextEditEngine::TextChange * change)278 void CFWL_Edit::OnTextWillChange(CFDE_TextEditEngine::TextChange* change) {
279   CFWL_EventTextWillChange event(this, change->text, change->previous_text,
280                                  change->selection_start,
281                                  change->selection_end);
282   DispatchEvent(&event);
283 
284   change->text = event.GetChangeText();
285   change->selection_start = event.GetSelectionStart();
286   change->selection_end = event.GetSelectionEnd();
287   change->cancelled = event.GetCancelled();
288 }
289 
OnTextChanged()290 void CFWL_Edit::OnTextChanged() {
291   if (m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_VAlignMask)
292     UpdateVAlignment();
293 
294   LayoutScrollBar();
295   RepaintRect(GetClientRect());
296 }
297 
OnSelChanged()298 void CFWL_Edit::OnSelChanged() {
299   RepaintRect(GetClientRect());
300 }
301 
OnValidate(const WideString & wsText)302 bool CFWL_Edit::OnValidate(const WideString& wsText) {
303   CFWL_EventValidate event(this, wsText);
304   DispatchEvent(&event);
305   return event.GetValidate();
306 }
307 
SetScrollOffset(float fScrollOffset)308 void CFWL_Edit::SetScrollOffset(float fScrollOffset) {
309   m_fScrollOffsetY = fScrollOffset;
310 }
311 
DrawContent(CFGAS_GEGraphics * pGraphics,const CFX_Matrix & mtMatrix)312 void CFWL_Edit::DrawContent(CFGAS_GEGraphics* pGraphics,
313                             const CFX_Matrix& mtMatrix) {
314   DrawContentNonComb(pGraphics, mtMatrix);
315   if (m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_CombText) {
316     CFGAS_GEGraphics::StateRestorer restorer(pGraphics);
317     CFGAS_GEPath path;
318     const int32_t iLimit = m_nLimit > 0 ? m_nLimit : 1;
319     const float fStep = m_EngineRect.width / iLimit;
320     float fLeft = m_EngineRect.left + 1;
321     for (int32_t i = 1; i < iLimit; i++) {
322       fLeft += fStep;
323       path.AddLine(CFX_PointF(fLeft, m_ClientRect.top),
324                    CFX_PointF(fLeft, m_ClientRect.bottom()));
325     }
326     CFWL_ThemeBackground param(CFWL_ThemePart::Part::kCombTextLine, this,
327                                pGraphics);
328     param.m_matrix = mtMatrix;
329     param.SetPath(&path);
330     GetThemeProvider()->DrawBackground(param);
331   }
332 }
333 
DrawContentNonComb(CFGAS_GEGraphics * pGraphics,const CFX_Matrix & mtMatrix)334 void CFWL_Edit::DrawContentNonComb(CFGAS_GEGraphics* pGraphics,
335                                    const CFX_Matrix& mtMatrix) {
336   CFGAS_GEGraphics::StateRestorer restorer(pGraphics);
337   CFX_RectF rtClip = m_EngineRect;
338   float fOffSetX = m_EngineRect.left - m_fScrollOffsetX;
339   float fOffSetY = m_EngineRect.top - m_fScrollOffsetY + m_fVAlignOffset;
340   CFX_Matrix mt(1, 0, 0, 1, fOffSetX, fOffSetY);
341   rtClip = mtMatrix.TransformRect(rtClip);
342   mt.Concat(mtMatrix);
343 
344   bool bShowSel = !!(m_Properties.m_dwStates & FWL_STATE_WGT_Focused);
345   if (bShowSel && m_pEditEngine->HasSelection()) {
346     size_t sel_start;
347     size_t count;
348     std::tie(sel_start, count) = m_pEditEngine->GetSelection();
349     std::vector<CFX_RectF> rects = m_pEditEngine->GetCharacterRectsInRange(
350         pdfium::base::checked_cast<int32_t>(sel_start),
351         pdfium::base::checked_cast<int32_t>(count));
352 
353     CFGAS_GEPath path;
354     for (auto& rect : rects) {
355       rect.left += fOffSetX;
356       rect.top += fOffSetY;
357       path.AddRectangle(rect.left, rect.top, rect.width, rect.height);
358     }
359     pGraphics->SetClipRect(rtClip);
360 
361     CFWL_ThemeBackground param(CFWL_ThemePart::Part::kBackground, this,
362                                pGraphics);
363     param.m_matrix = mtMatrix;
364     param.SetPath(&path);
365     GetThemeProvider()->DrawBackground(param);
366   }
367 
368   CFX_RenderDevice* pRenderDev = pGraphics->GetRenderDevice();
369   RenderText(pRenderDev, rtClip, mt);
370 }
371 
RenderText(CFX_RenderDevice * pRenderDev,const CFX_RectF & clipRect,const CFX_Matrix & mt)372 void CFWL_Edit::RenderText(CFX_RenderDevice* pRenderDev,
373                            const CFX_RectF& clipRect,
374                            const CFX_Matrix& mt) {
375   DCHECK(pRenderDev);
376 
377   RetainPtr<CFGAS_GEFont> font = m_pEditEngine->GetFont();
378   if (!font)
379     return;
380 
381   pRenderDev->SetClip_Rect(clipRect.GetOuterRect());
382 
383   CFX_RectF rtDocClip = clipRect;
384   if (rtDocClip.IsEmpty()) {
385     rtDocClip.left = 0;
386     rtDocClip.top = 0;
387     rtDocClip.width = static_cast<float>(pRenderDev->GetWidth());
388     rtDocClip.height = static_cast<float>(pRenderDev->GetHeight());
389   }
390   rtDocClip = mt.GetInverse().TransformRect(rtDocClip);
391 
392   for (const FDE_TEXTEDITPIECE& info : m_pEditEngine->GetTextPieces()) {
393     // If this character is outside the clip, skip it.
394     if (!rtDocClip.IntersectWith(info.rtPiece))
395       continue;
396 
397     std::vector<TextCharPos> char_pos = m_pEditEngine->GetDisplayPos(info);
398     if (char_pos.empty())
399       continue;
400 
401     CFDE_TextOut::DrawString(pRenderDev, m_pEditEngine->GetFontColor(), font,
402                              char_pos, m_pEditEngine->GetFontSize(), mt);
403   }
404 }
405 
UpdateEditEngine()406 void CFWL_Edit::UpdateEditEngine() {
407   UpdateEditParams();
408   UpdateEditLayout();
409 }
410 
UpdateEditParams()411 void CFWL_Edit::UpdateEditParams() {
412   m_pEditEngine->SetAvailableWidth(m_EngineRect.width);
413   m_pEditEngine->SetCombText(
414       !!(m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_CombText));
415 
416   m_pEditEngine->EnableValidation(
417       !!(m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_Validate));
418   m_pEditEngine->EnablePasswordMode(
419       !!(m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_Password));
420 
421   uint32_t alignment = 0;
422   switch (m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_HAlignMask) {
423     case FWL_STYLEEXT_EDT_HNear: {
424       alignment |= CFX_TxtLineAlignment_Left;
425       break;
426     }
427     case FWL_STYLEEXT_EDT_HCenter: {
428       alignment |= CFX_TxtLineAlignment_Center;
429       break;
430     }
431     case FWL_STYLEEXT_EDT_HFar: {
432       alignment |= CFX_TxtLineAlignment_Right;
433       break;
434     }
435     default:
436       break;
437   }
438   switch (m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_HAlignModeMask) {
439     case FWL_STYLEEXT_EDT_Justified: {
440       alignment |= CFX_TxtLineAlignment_Justified;
441       break;
442     }
443     default:
444       break;
445   }
446   m_pEditEngine->SetAlignment(alignment);
447 
448   bool auto_hscroll =
449       !!(m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_AutoHScroll);
450   if (m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_MultiLine) {
451     m_pEditEngine->EnableMultiLine(true);
452     m_pEditEngine->EnableLineWrap(!auto_hscroll);
453     m_pEditEngine->LimitVerticalScroll(
454         (m_Properties.m_dwStyles & FWL_STYLE_WGT_VScroll) == 0 &&
455         (m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_AutoVScroll) == 0);
456   } else {
457     m_pEditEngine->EnableMultiLine(false);
458     m_pEditEngine->EnableLineWrap(false);
459     m_pEditEngine->LimitVerticalScroll(false);
460   }
461   m_pEditEngine->LimitHorizontalScroll(!auto_hscroll);
462 
463   IFWL_ThemeProvider* theme = GetThemeProvider();
464   CFWL_ThemePart part(CFWL_ThemePart::Part::kNone, this);
465   m_fFontSize = theme->GetFontSize(part);
466 
467   RetainPtr<CFGAS_GEFont> pFont = theme->GetFont(part);
468   if (!pFont)
469     return;
470 
471   m_pEditEngine->SetFont(pFont);
472   m_pEditEngine->SetFontColor(theme->GetTextColor(part));
473   m_pEditEngine->SetFontSize(m_fFontSize);
474   m_pEditEngine->SetLineSpace(theme->GetLineHeight(part));
475   m_pEditEngine->SetTabWidth(m_fFontSize);
476   m_pEditEngine->SetVisibleLineCount(m_EngineRect.height /
477                                      theme->GetLineHeight(part));
478 }
479 
UpdateEditLayout()480 void CFWL_Edit::UpdateEditLayout() {
481   m_pEditEngine->Layout();
482 }
483 
UpdateOffset()484 bool CFWL_Edit::UpdateOffset() {
485   CFX_RectF rtCaret = m_CaretRect;
486 
487   float fOffSetX = m_EngineRect.left - m_fScrollOffsetX;
488   float fOffSetY = m_EngineRect.top - m_fScrollOffsetY + m_fVAlignOffset;
489   rtCaret.Offset(fOffSetX, fOffSetY);
490 
491   const CFX_RectF& edit_bounds = m_EngineRect;
492   if (edit_bounds.Contains(rtCaret)) {
493     CFX_RectF contents_bounds = m_pEditEngine->GetContentsBoundingBox();
494     contents_bounds.Offset(fOffSetX, fOffSetY);
495     if (contents_bounds.right() < edit_bounds.right() && m_fScrollOffsetX > 0) {
496       m_fScrollOffsetX += contents_bounds.right() - edit_bounds.right();
497       m_fScrollOffsetX = std::max(m_fScrollOffsetX, 0.0f);
498     }
499     if (contents_bounds.bottom() < edit_bounds.bottom() &&
500         m_fScrollOffsetY > 0) {
501       m_fScrollOffsetY += contents_bounds.bottom() - edit_bounds.bottom();
502       m_fScrollOffsetY = std::max(m_fScrollOffsetY, 0.0f);
503     }
504     return false;
505   }
506 
507   float offsetX = 0.0;
508   float offsetY = 0.0;
509   if (rtCaret.left < edit_bounds.left)
510     offsetX = rtCaret.left - edit_bounds.left;
511   if (rtCaret.right() > edit_bounds.right())
512     offsetX = rtCaret.right() - edit_bounds.right();
513   if (rtCaret.top < edit_bounds.top)
514     offsetY = rtCaret.top - edit_bounds.top;
515   if (rtCaret.bottom() > edit_bounds.bottom())
516     offsetY = rtCaret.bottom() - edit_bounds.bottom();
517 
518   m_fScrollOffsetX += offsetX;
519   m_fScrollOffsetY += offsetY;
520   if (m_fFontSize > m_EngineRect.height)
521     m_fScrollOffsetY = 0;
522 
523   return true;
524 }
525 
UpdateOffset(CFWL_ScrollBar * pScrollBar,float fPosChanged)526 bool CFWL_Edit::UpdateOffset(CFWL_ScrollBar* pScrollBar, float fPosChanged) {
527   m_fScrollOffsetY += fPosChanged;
528   return true;
529 }
530 
UpdateVAlignment()531 void CFWL_Edit::UpdateVAlignment() {
532   IFWL_ThemeProvider* theme = GetThemeProvider();
533   CFWL_ThemePart part(CFWL_ThemePart::Part::kNone, this);
534   const CFX_SizeF pSpace = theme->GetSpaceAboveBelow(part);
535   const float fSpaceAbove = pSpace.width >= 0.1f ? pSpace.width : 0.0f;
536   const float fSpaceBelow = pSpace.height >= 0.1f ? pSpace.height : 0.0f;
537   float fOffsetY = 0.0f;
538   CFX_RectF contents_bounds = m_pEditEngine->GetContentsBoundingBox();
539   if (m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_VCenter) {
540     fOffsetY = (m_EngineRect.height - contents_bounds.height) / 2.0f;
541     if (fOffsetY < (fSpaceAbove + fSpaceBelow) / 2.0f &&
542         fSpaceAbove < fSpaceBelow) {
543       return;
544     }
545     fOffsetY += (fSpaceAbove - fSpaceBelow) / 2.0f;
546   } else if (m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_VFar) {
547     fOffsetY = (m_EngineRect.height - contents_bounds.height);
548     fOffsetY -= fSpaceBelow;
549   } else {
550     fOffsetY += fSpaceAbove;
551   }
552   m_fVAlignOffset = std::max(fOffsetY, 0.0f);
553 }
554 
UpdateCaret()555 void CFWL_Edit::UpdateCaret() {
556   CFX_RectF rtCaret = m_CaretRect;
557   rtCaret.Offset(m_EngineRect.left - m_fScrollOffsetX,
558                  m_EngineRect.top - m_fScrollOffsetY + m_fVAlignOffset);
559 
560   CFX_RectF rtClient = GetClientRect();
561   rtCaret.Intersect(rtClient);
562   if (rtCaret.left > rtClient.right()) {
563     float right = rtCaret.right();
564     rtCaret.left = rtClient.right() - 1;
565     rtCaret.width = right - rtCaret.left;
566   }
567 
568   if (m_Properties.m_dwStates & FWL_STATE_WGT_Focused && !rtCaret.IsEmpty())
569     ShowCaret(&rtCaret);
570   else
571     HideCaret(&rtCaret);
572 }
573 
UpdateScroll()574 CFWL_ScrollBar* CFWL_Edit::UpdateScroll() {
575   bool bShowVert = m_pVertScrollBar && m_pVertScrollBar->IsVisible();
576   if (!bShowVert)
577     return nullptr;
578 
579   CFX_RectF contents_bounds = m_pEditEngine->GetContentsBoundingBox();
580   CFX_RectF rtScroll = m_pVertScrollBar->GetWidgetRect();
581   if (rtScroll.height < contents_bounds.height) {
582     float fStep = m_pEditEngine->GetLineSpace();
583     float fRange =
584         std::max(contents_bounds.height - m_EngineRect.height, fStep);
585     m_pVertScrollBar->SetRange(0.0f, fRange);
586     float fPos = pdfium::clamp(m_fScrollOffsetY, 0.0f, fRange);
587     m_pVertScrollBar->SetPos(fPos);
588     m_pVertScrollBar->SetTrackPos(fPos);
589     m_pVertScrollBar->SetPageSize(rtScroll.height);
590     m_pVertScrollBar->SetStepSize(fStep);
591     m_pVertScrollBar->RemoveStates(FWL_STATE_WGT_Disabled);
592     m_pVertScrollBar->Update();
593     return m_pVertScrollBar;
594   }
595   if ((m_pVertScrollBar->GetStates() & FWL_STATE_WGT_Disabled) == 0) {
596     m_pVertScrollBar->SetRange(0, -1);
597     m_pVertScrollBar->SetStates(FWL_STATE_WGT_Disabled);
598     m_pVertScrollBar->Update();
599     return m_pVertScrollBar;
600   }
601   return nullptr;
602 }
603 
IsShowVertScrollBar() const604 bool CFWL_Edit::IsShowVertScrollBar() const {
605   const bool bShow =
606       !(m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_ShowScrollbarFocus) ||
607       (m_Properties.m_dwStates & FWL_STATE_WGT_Focused);
608   return bShow && (m_Properties.m_dwStyles & FWL_STYLE_WGT_VScroll) &&
609          (m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_MultiLine) &&
610          IsContentHeightOverflow();
611 }
612 
IsContentHeightOverflow() const613 bool CFWL_Edit::IsContentHeightOverflow() const {
614   return m_pEditEngine->GetContentsBoundingBox().height >
615          m_EngineRect.height + 1.0f;
616 }
617 
Layout()618 void CFWL_Edit::Layout() {
619   m_ClientRect = GetClientRect();
620   m_EngineRect = m_ClientRect;
621 
622   IFWL_ThemeProvider* theme = GetThemeProvider();
623   float fWidth = theme->GetScrollBarWidth();
624   if (!GetOuter()) {
625     CFWL_ThemePart part(CFWL_ThemePart::Part::kNone, this);
626     CFX_RectF pUIMargin = theme->GetUIMargin(part);
627     m_EngineRect.Deflate(pUIMargin.left, pUIMargin.top, pUIMargin.width,
628                          pUIMargin.height);
629   } else if (GetOuter()->GetClassID() == FWL_Type::DateTimePicker) {
630     CFWL_ThemePart part(CFWL_ThemePart::Part::kNone, GetOuter());
631     CFX_RectF pUIMargin = theme->GetUIMargin(part);
632     m_EngineRect.Deflate(pUIMargin.left, pUIMargin.top, pUIMargin.width,
633                          pUIMargin.height);
634   }
635 
636   bool bShowVertScrollbar = IsShowVertScrollBar();
637   if (bShowVertScrollbar) {
638     InitVerticalScrollBar();
639 
640     CFX_RectF rtVertScr;
641     if (m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_OuterScrollbar) {
642       rtVertScr = CFX_RectF(m_ClientRect.right() + kEditMargin,
643                             m_ClientRect.top, fWidth, m_ClientRect.height);
644     } else {
645       rtVertScr = CFX_RectF(m_ClientRect.right() - fWidth, m_ClientRect.top,
646                             fWidth, m_ClientRect.height);
647       m_EngineRect.width -= fWidth;
648     }
649 
650     m_pVertScrollBar->SetWidgetRect(rtVertScr);
651     m_pVertScrollBar->RemoveStates(FWL_STATE_WGT_Invisible);
652     m_pVertScrollBar->Update();
653   } else if (m_pVertScrollBar) {
654     m_pVertScrollBar->SetStates(FWL_STATE_WGT_Invisible);
655   }
656 }
657 
LayoutScrollBar()658 void CFWL_Edit::LayoutScrollBar() {
659   if (!(m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_ShowScrollbarFocus))
660     return;
661 
662   bool bShowVertScrollbar = IsShowVertScrollBar();
663   IFWL_ThemeProvider* theme = GetThemeProvider();
664   float fWidth = theme->GetScrollBarWidth();
665   if (bShowVertScrollbar) {
666     if (!m_pVertScrollBar) {
667       InitVerticalScrollBar();
668       CFX_RectF rtVertScr;
669       if (m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_OuterScrollbar) {
670         rtVertScr = CFX_RectF(m_ClientRect.right() + kEditMargin,
671                               m_ClientRect.top, fWidth, m_ClientRect.height);
672       } else {
673         rtVertScr = CFX_RectF(m_ClientRect.right() - fWidth, m_ClientRect.top,
674                               fWidth, m_ClientRect.height);
675       }
676       m_pVertScrollBar->SetWidgetRect(rtVertScr);
677       m_pVertScrollBar->Update();
678     }
679     m_pVertScrollBar->RemoveStates(FWL_STATE_WGT_Invisible);
680   } else if (m_pVertScrollBar) {
681     m_pVertScrollBar->SetStates(FWL_STATE_WGT_Invisible);
682   }
683   if (bShowVertScrollbar)
684     UpdateScroll();
685 }
686 
DeviceToEngine(const CFX_PointF & pt)687 CFX_PointF CFWL_Edit::DeviceToEngine(const CFX_PointF& pt) {
688   return pt + CFX_PointF(m_fScrollOffsetX - m_EngineRect.left,
689                          m_fScrollOffsetY - m_EngineRect.top - m_fVAlignOffset);
690 }
691 
InitVerticalScrollBar()692 void CFWL_Edit::InitVerticalScrollBar() {
693   if (m_pVertScrollBar)
694     return;
695 
696   m_pVertScrollBar = cppgc::MakeGarbageCollected<CFWL_ScrollBar>(
697       GetFWLApp()->GetHeap()->GetAllocationHandle(), GetFWLApp(),
698       Properties{0, FWL_STYLEEXT_SCB_Vert,
699                  FWL_STATE_WGT_Disabled | FWL_STATE_WGT_Invisible},
700       this);
701 }
702 
ShowCaret(CFX_RectF * pRect)703 void CFWL_Edit::ShowCaret(CFX_RectF* pRect) {
704   if (m_pCaret) {
705     m_pCaret->ShowCaret();
706     if (!pRect->IsEmpty())
707       m_pCaret->SetWidgetRect(*pRect);
708     RepaintRect(m_EngineRect);
709     return;
710   }
711 
712   CFWL_Widget* pOuter = this;
713   pRect->Offset(m_WidgetRect.left, m_WidgetRect.top);
714   while (pOuter->GetOuter()) {
715     pOuter = pOuter->GetOuter();
716     CFX_RectF rtOuter = pOuter->GetWidgetRect();
717     pRect->Offset(rtOuter.left, rtOuter.top);
718   }
719 
720   CFWL_Widget::AdapterIface* pXFAWidget = pOuter->GetAdapterIface();
721   if (!pXFAWidget)
722     return;
723 
724   CFX_RectF rt = pXFAWidget->GetRotateMatrix().TransformRect(*pRect);
725   pXFAWidget->DisplayCaret(true, &rt);
726 }
727 
HideCaret(CFX_RectF * pRect)728 void CFWL_Edit::HideCaret(CFX_RectF* pRect) {
729   if (m_pCaret) {
730     m_pCaret->HideCaret();
731     RepaintRect(m_EngineRect);
732     return;
733   }
734 
735   CFWL_Widget* pOuter = this;
736   while (pOuter->GetOuter())
737     pOuter = pOuter->GetOuter();
738 
739   CFWL_Widget::AdapterIface* pXFAWidget = pOuter->GetAdapterIface();
740   if (!pXFAWidget)
741     return;
742 
743   pXFAWidget->DisplayCaret(false, pRect);
744 }
745 
InitCaret()746 void CFWL_Edit::InitCaret() {
747   if (m_pCaret)
748     return;
749 
750   m_pCaret = cppgc::MakeGarbageCollected<CFWL_Caret>(
751       GetFWLApp()->GetHeap()->GetAllocationHandle(), GetFWLApp(), Properties(),
752       this);
753   m_pCaret->SetStates(m_Properties.m_dwStates);
754   UpdateCursorRect();
755 }
756 
UpdateCursorRect()757 void CFWL_Edit::UpdateCursorRect() {
758   int32_t bidi_level;
759   if (m_pEditEngine->CanGenerateCharacterInfo()) {
760     std::tie(bidi_level, m_CaretRect) = m_pEditEngine->GetCharacterInfo(
761         pdfium::base::checked_cast<int32_t>(m_CursorPosition));
762   } else {
763     bidi_level = 0;
764     m_CaretRect = CFX_RectF();
765   }
766 
767   // TODO(dsinclair): This should handle bidi level  ...
768 
769   m_CaretRect.width = 1.0f;
770 
771   // TODO(hnakashima): Handle correctly edits with empty text instead of using
772   // these defaults.
773   if (m_CaretRect.height == 0)
774     m_CaretRect.height = 8.0f;
775 }
776 
SetCursorPosition(size_t position)777 void CFWL_Edit::SetCursorPosition(size_t position) {
778   if (m_CursorPosition == position)
779     return;
780 
781   m_CursorPosition = std::min(position, m_pEditEngine->GetLength());
782   UpdateCursorRect();
783   OnCaretChanged();
784 }
785 
OnProcessMessage(CFWL_Message * pMessage)786 void CFWL_Edit::OnProcessMessage(CFWL_Message* pMessage) {
787   switch (pMessage->GetType()) {
788     case CFWL_Message::Type::kSetFocus:
789       OnFocusGained();
790       break;
791     case CFWL_Message::Type::kKillFocus:
792       OnFocusLost();
793       break;
794     case CFWL_Message::Type::kMouse: {
795       CFWL_MessageMouse* pMsg = static_cast<CFWL_MessageMouse*>(pMessage);
796       switch (pMsg->m_dwCmd) {
797         case CFWL_MessageMouse::MouseCommand::kLeftButtonDown:
798           OnLButtonDown(pMsg);
799           break;
800         case CFWL_MessageMouse::MouseCommand::kLeftButtonUp:
801           OnLButtonUp(pMsg);
802           break;
803         case CFWL_MessageMouse::MouseCommand::kLeftButtonDblClk:
804           OnButtonDoubleClick(pMsg);
805           break;
806         case CFWL_MessageMouse::MouseCommand::kMove:
807           OnMouseMove(pMsg);
808           break;
809         case CFWL_MessageMouse::MouseCommand::kRightButtonDown:
810           DoRButtonDown(pMsg);
811           break;
812         default:
813           break;
814       }
815       break;
816     }
817     case CFWL_Message::Type::kKey: {
818       CFWL_MessageKey* pKey = static_cast<CFWL_MessageKey*>(pMessage);
819       if (pKey->m_dwCmd == CFWL_MessageKey::KeyCommand::kKeyDown)
820         OnKeyDown(pKey);
821       else if (pKey->m_dwCmd == CFWL_MessageKey::KeyCommand::kChar)
822         OnChar(pKey);
823       break;
824     }
825     default:
826       break;
827   }
828   // Dst target could be |this|, continue only if not destroyed by above.
829   if (pMessage->GetDstTarget())
830     CFWL_Widget::OnProcessMessage(pMessage);
831 }
832 
OnProcessEvent(CFWL_Event * pEvent)833 void CFWL_Edit::OnProcessEvent(CFWL_Event* pEvent) {
834   if (!pEvent || pEvent->GetType() != CFWL_Event::Type::Scroll)
835     return;
836 
837   CFWL_Widget* pSrcTarget = pEvent->GetSrcTarget();
838   if ((pSrcTarget == m_pVertScrollBar && m_pVertScrollBar)) {
839     CFWL_EventScroll* pScrollEvent = static_cast<CFWL_EventScroll*>(pEvent);
840     OnScroll(static_cast<CFWL_ScrollBar*>(pSrcTarget),
841              pScrollEvent->GetScrollCode(), pScrollEvent->GetPos());
842   }
843 }
844 
OnDrawWidget(CFGAS_GEGraphics * pGraphics,const CFX_Matrix & matrix)845 void CFWL_Edit::OnDrawWidget(CFGAS_GEGraphics* pGraphics,
846                              const CFX_Matrix& matrix) {
847   DrawWidget(pGraphics, matrix);
848 }
849 
DoRButtonDown(CFWL_MessageMouse * pMsg)850 void CFWL_Edit::DoRButtonDown(CFWL_MessageMouse* pMsg) {
851   SetCursorPosition(
852       m_pEditEngine->GetIndexForPoint(DeviceToEngine(pMsg->m_pos)));
853 }
854 
OnFocusGained()855 void CFWL_Edit::OnFocusGained() {
856   m_Properties.m_dwStates |= FWL_STATE_WGT_Focused;
857   UpdateVAlignment();
858   UpdateOffset();
859   UpdateCaret();
860   LayoutScrollBar();
861 }
862 
OnFocusLost()863 void CFWL_Edit::OnFocusLost() {
864   bool bRepaint = false;
865   if (m_Properties.m_dwStates & FWL_STATE_WGT_Focused) {
866     m_Properties.m_dwStates &= ~FWL_STATE_WGT_Focused;
867     HideCaret(nullptr);
868     if (HasSelection()) {
869       ClearSelection();
870       bRepaint = true;
871     }
872     UpdateOffset();
873   }
874   LayoutScrollBar();
875   if (!bRepaint)
876     return;
877 
878   RepaintRect(CFX_RectF(0, 0, m_WidgetRect.width, m_WidgetRect.height));
879 }
880 
OnLButtonDown(CFWL_MessageMouse * pMsg)881 void CFWL_Edit::OnLButtonDown(CFWL_MessageMouse* pMsg) {
882   if (m_Properties.m_dwStates & FWL_STATE_WGT_Disabled)
883     return;
884 
885   m_bLButtonDown = true;
886   SetGrab(true);
887 
888   bool bRepaint = false;
889   if (m_pEditEngine->HasSelection()) {
890     m_pEditEngine->ClearSelection();
891     bRepaint = true;
892   }
893 
894   size_t index_at_click =
895       m_pEditEngine->GetIndexForPoint(DeviceToEngine(pMsg->m_pos));
896 
897   if (index_at_click != m_CursorPosition &&
898       !!(pMsg->m_dwFlags & XFA_FWL_KeyFlag::kShift)) {
899     size_t start = std::min(m_CursorPosition, index_at_click);
900     size_t end = std::max(m_CursorPosition, index_at_click);
901 
902     m_pEditEngine->SetSelection(start, end - start);
903     bRepaint = true;
904   } else {
905     SetCursorPosition(index_at_click);
906   }
907 
908   if (bRepaint)
909     RepaintRect(m_EngineRect);
910 }
911 
OnLButtonUp(CFWL_MessageMouse * pMsg)912 void CFWL_Edit::OnLButtonUp(CFWL_MessageMouse* pMsg) {
913   m_bLButtonDown = false;
914   SetGrab(false);
915 }
916 
OnButtonDoubleClick(CFWL_MessageMouse * pMsg)917 void CFWL_Edit::OnButtonDoubleClick(CFWL_MessageMouse* pMsg) {
918   size_t click_idx =
919       m_pEditEngine->GetIndexForPoint(DeviceToEngine(pMsg->m_pos));
920   size_t start_idx;
921   size_t count;
922   std::tie(start_idx, count) = m_pEditEngine->BoundsForWordAt(click_idx);
923 
924   m_pEditEngine->SetSelection(start_idx, count);
925   m_CursorPosition = start_idx + count;
926   RepaintRect(m_EngineRect);
927 }
928 
OnMouseMove(CFWL_MessageMouse * pMsg)929 void CFWL_Edit::OnMouseMove(CFWL_MessageMouse* pMsg) {
930   bool shift = !!(pMsg->m_dwFlags & XFA_FWL_KeyFlag::kShift);
931   if (!m_bLButtonDown || !shift)
932     return;
933 
934   size_t old_cursor_pos = m_CursorPosition;
935   SetCursorPosition(
936       m_pEditEngine->GetIndexForPoint(DeviceToEngine(pMsg->m_pos)));
937   if (old_cursor_pos == m_CursorPosition)
938     return;
939 
940   size_t length = m_pEditEngine->GetLength();
941   if (m_CursorPosition > length)
942     SetCursorPosition(length);
943 
944   size_t sel_start = 0;
945   size_t count = 0;
946   if (m_pEditEngine->HasSelection())
947     std::tie(sel_start, count) = m_pEditEngine->GetSelection();
948   else
949     sel_start = old_cursor_pos;
950 
951   size_t start_pos = std::min(sel_start, m_CursorPosition);
952   size_t end_pos = std::max(sel_start, m_CursorPosition);
953   m_pEditEngine->SetSelection(start_pos, end_pos - start_pos);
954 }
955 
OnKeyDown(CFWL_MessageKey * pMsg)956 void CFWL_Edit::OnKeyDown(CFWL_MessageKey* pMsg) {
957   bool bShift = !!(pMsg->m_dwFlags & XFA_FWL_KeyFlag::kShift);
958   bool bCtrl = !!(pMsg->m_dwFlags & XFA_FWL_KeyFlag::kCtrl);
959 
960   size_t sel_start = m_CursorPosition;
961   if (m_pEditEngine->HasSelection()) {
962     size_t start_idx;
963     size_t count;
964     std::tie(start_idx, count) = m_pEditEngine->GetSelection();
965     sel_start = start_idx;
966   }
967 
968   switch (pMsg->m_dwKeyCodeOrChar) {
969     case XFA_FWL_VKEY_Left:
970       SetCursorPosition(m_pEditEngine->GetIndexLeft(m_CursorPosition));
971       break;
972     case XFA_FWL_VKEY_Right:
973       SetCursorPosition(m_pEditEngine->GetIndexRight(m_CursorPosition));
974       break;
975     case XFA_FWL_VKEY_Up:
976       SetCursorPosition(m_pEditEngine->GetIndexUp(m_CursorPosition));
977       break;
978     case XFA_FWL_VKEY_Down:
979       SetCursorPosition(m_pEditEngine->GetIndexDown(m_CursorPosition));
980       break;
981     case XFA_FWL_VKEY_Home:
982       SetCursorPosition(
983           bCtrl ? 0 : m_pEditEngine->GetIndexAtStartOfLine(m_CursorPosition));
984       break;
985     case XFA_FWL_VKEY_End:
986       SetCursorPosition(
987           bCtrl ? m_pEditEngine->GetLength()
988                 : m_pEditEngine->GetIndexAtEndOfLine(m_CursorPosition));
989       break;
990     case XFA_FWL_VKEY_Delete: {
991       if ((m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_ReadOnly) ||
992           (m_Properties.m_dwStates & FWL_STATE_WGT_Disabled)) {
993         break;
994       }
995 
996       m_pEditEngine->Delete(m_CursorPosition, 1);
997       UpdateCaret();
998       break;
999     }
1000     case XFA_FWL_VKEY_Insert:
1001     case XFA_FWL_VKEY_F2:
1002     case XFA_FWL_VKEY_Tab:
1003     default:
1004       break;
1005   }
1006 
1007   // Update the selection.
1008   if (bShift && sel_start != m_CursorPosition) {
1009     m_pEditEngine->SetSelection(std::min(sel_start, m_CursorPosition),
1010                                 std::max(sel_start, m_CursorPosition));
1011     RepaintRect(m_EngineRect);
1012   }
1013 }
1014 
OnChar(CFWL_MessageKey * pMsg)1015 void CFWL_Edit::OnChar(CFWL_MessageKey* pMsg) {
1016   if ((m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_ReadOnly) ||
1017       (m_Properties.m_dwStates & FWL_STATE_WGT_Disabled)) {
1018     return;
1019   }
1020 
1021   wchar_t c = static_cast<wchar_t>(pMsg->m_dwKeyCodeOrChar);
1022   switch (c) {
1023     case L'\b':
1024       if (m_CursorPosition > 0) {
1025         SetCursorPosition(m_CursorPosition - 1);
1026         m_pEditEngine->Delete(m_CursorPosition, 1);
1027         UpdateCaret();
1028       }
1029       break;
1030     case L'\n':
1031     case 27:   // Esc
1032     case 127:  // Delete
1033       break;
1034     case L'\t':
1035       m_pEditEngine->Insert(m_CursorPosition, L"\t");
1036       SetCursorPosition(m_CursorPosition + 1);
1037       break;
1038     case L'\r':
1039       if (m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_WantReturn) {
1040         m_pEditEngine->Insert(m_CursorPosition, L"\n");
1041         SetCursorPosition(m_CursorPosition + 1);
1042       }
1043       break;
1044     default: {
1045       if (pMsg->m_dwFlags & kEditingModifier)
1046         break;
1047 
1048       m_pEditEngine->Insert(m_CursorPosition, WideString(c));
1049       SetCursorPosition(m_CursorPosition + 1);
1050       break;
1051     }
1052   }
1053 }
1054 
OnScroll(CFWL_ScrollBar * pScrollBar,CFWL_EventScroll::Code dwCode,float fPos)1055 bool CFWL_Edit::OnScroll(CFWL_ScrollBar* pScrollBar,
1056                          CFWL_EventScroll::Code dwCode,
1057                          float fPos) {
1058   float fMin;
1059   float fMax;
1060   pScrollBar->GetRange(&fMin, &fMax);
1061   float iCurPos = pScrollBar->GetPos();
1062   float fStep = pScrollBar->GetStepSize();
1063   switch (dwCode) {
1064     case CFWL_EventScroll::Code::Min: {
1065       fPos = fMin;
1066       break;
1067     }
1068     case CFWL_EventScroll::Code::Max: {
1069       fPos = fMax;
1070       break;
1071     }
1072     case CFWL_EventScroll::Code::StepBackward: {
1073       fPos -= fStep;
1074       if (fPos < fMin + fStep / 2) {
1075         fPos = fMin;
1076       }
1077       break;
1078     }
1079     case CFWL_EventScroll::Code::StepForward: {
1080       fPos += fStep;
1081       if (fPos > fMax - fStep / 2) {
1082         fPos = fMax;
1083       }
1084       break;
1085     }
1086     case CFWL_EventScroll::Code::PageBackward: {
1087       fPos -= pScrollBar->GetPageSize();
1088       if (fPos < fMin) {
1089         fPos = fMin;
1090       }
1091       break;
1092     }
1093     case CFWL_EventScroll::Code::PageForward: {
1094       fPos += pScrollBar->GetPageSize();
1095       if (fPos > fMax) {
1096         fPos = fMax;
1097       }
1098       break;
1099     }
1100     case CFWL_EventScroll::Code::Pos:
1101     case CFWL_EventScroll::Code::TrackPos:
1102     case CFWL_EventScroll::Code::None:
1103       break;
1104     case CFWL_EventScroll::Code::EndScroll:
1105       return false;
1106   }
1107   if (iCurPos == fPos)
1108     return true;
1109 
1110   pScrollBar->SetPos(fPos);
1111   pScrollBar->SetTrackPos(fPos);
1112   UpdateOffset(pScrollBar, fPos - iCurPos);
1113   UpdateCaret();
1114 
1115   CFX_RectF rect = GetWidgetRect();
1116   RepaintRect(CFX_RectF(0, 0, rect.width + 2, rect.height + 2));
1117   return true;
1118 }
1119