7#include "xfa/fwl/cfwl_edit.h"
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/numerics/safe_conversions.h"
19#include "v8/include/cppgc/visitor.h"
20#include "xfa/fde/cfde_textout.h"
21#include "xfa/fgas/font/cfgas_gefont.h"
22#include "xfa/fgas/graphics/cfgas_gegraphics.h"
23#include "xfa/fgas/graphics/cfgas_gepath.h"
24#include "xfa/fwl/cfwl_app.h"
25#include "xfa/fwl/cfwl_caret.h"
26#include "xfa/fwl/cfwl_event.h"
27#include "xfa/fwl/cfwl_eventtextwillchange.h"
28#include "xfa/fwl/cfwl_eventvalidate.h"
29#include "xfa/fwl/cfwl_messagekey.h"
30#include "xfa/fwl/cfwl_messagemouse.h"
31#include "xfa/fwl/cfwl_themebackground.h"
32#include "xfa/fwl/cfwl_themepart.h"
33#include "xfa/fwl/cfwl_widgetmgr.h"
34#include "xfa/fwl/fwl_widgetdef.h"
35#include "xfa/fwl/ifwl_themeprovider.h"
36#include "xfa/fwl/theme/cfwl_utils.h"
40constexpr int kEditMargin = 3;
42#if BUILDFLAG(IS_APPLE)
43constexpr XFA_FWL_KeyFlag kEditingModifier = XFA_FWL_KeyFlag::kCommand;
55 m_pEditEngine->SetDelegate(
this);
61 m_pEditEngine->SetDelegate(
nullptr);
68 CFWL_Widget::Trace(visitor);
69 visitor->Trace(m_pVertScrollBar);
70 visitor->Trace(m_pCaret);
81 if (IsShowVertScrollBar()) {
82 rect
.width += scrollbarWidth;
91 if (m_pEditEngine->GetLength() > 0) {
92 CFX_SizeF size = CalcTextSize(
93 m_pEditEngine->GetText(),
125 if (IsShowVertScrollBar()) {
126 if (m_pVertScrollBar->GetWidgetRect().Contains(point))
143 DrawContent(pGraphics, matrix);
149 m_pEditEngine->Clear();
150 m_pEditEngine->Insert(0, wsText,
151 CFDE_TextEditEngine::RecordOperation::kInsertRecord);
155 m_pEditEngine->Clear();
156 m_pEditEngine->Insert(0, wsText,
157 CFDE_TextEditEngine::RecordOperation::kSkipNotify);
161 return m_pEditEngine->GetLength();
165 return m_pEditEngine->GetText();
169 m_pEditEngine->Clear();
173 m_pEditEngine->SelectAll();
177 return m_pEditEngine->HasSelection();
181 return m_pEditEngine->GetSelection();
185 return m_pEditEngine->ClearSelection();
196 m_pEditEngine->SetHasCharacterLimit(
true);
197 m_pEditEngine->SetCharacterLimit(nLimit);
199 m_pEditEngine->SetHasCharacterLimit(
false);
204 m_pEditEngine->SetAliasChar(wAlias);
208 if (!m_pEditEngine->HasSelection())
209 return absl::nullopt;
211 return m_pEditEngine->GetSelectedText();
215 if (!m_pEditEngine->HasSelection())
216 return absl::nullopt;
218 WideString cut_text = m_pEditEngine->DeleteSelectedText();
224 if (m_pEditEngine->HasSelection())
225 m_pEditEngine->ReplaceSelectedText(wsPaste);
227 m_pEditEngine->Insert(m_CursorPosition, wsPaste);
233 return CanUndo() && m_pEditEngine->Undo();
237 return CanRedo() && m_pEditEngine->Redo();
241 return m_pEditEngine->CanUndo();
245 return m_pEditEngine->CanRedo();
259 bool bRepaintContent = UpdateOffset();
262 bool bRepaintScroll =
false;
264 CFWL_ScrollBar* pScroll = UpdateScroll();
267 bRepaintScroll =
true;
270 if (bRepaintContent || bRepaintScroll) {
278 CFWL_EventTextWillChange event(
this, change->text, change->previous_text,
279 change->selection_start,
280 change->selection_end);
284 change->selection_start = event.GetSelectionStart();
285 change->selection_end = event.GetSelectionEnd();
302 CFWL_EventValidate event(
this, wsText);
308 m_fScrollOffsetY = fScrollOffset;
313 DrawContentNonComb(pGraphics, mtMatrix);
317 const int32_t iLimit = m_nLimit > 0 ? m_nLimit : 1;
318 const float fStep = m_EngineRect
.width / iLimit;
319 float fLeft = m_EngineRect
.left + 1;
320 for (int32_t i = 1; i < iLimit; i++) {
337 float fOffSetX = m_EngineRect
.left - m_fScrollOffsetX;
338 float fOffSetY = m_EngineRect
.top - m_fScrollOffsetY + m_fVAlignOffset;
344 if (bShowSel && m_pEditEngine->HasSelection()) {
347 std::tie(sel_start, count) = m_pEditEngine->GetSelection();
348 std::vector<CFX_RectF> rects = m_pEditEngine->GetCharacterRectsInRange(
349 pdfium::base::checked_cast<int32_t>(sel_start),
350 pdfium::base::checked_cast<int32_t>(count));
353 for (
auto& rect : rects) {
354 rect.left += fOffSetX;
355 rect.top += fOffSetY;
356 path.AddRectangle(rect.left, rect.top, rect.width, rect.height);
368 RenderText(pRenderDev, rtClip, mt);
376 RetainPtr<CFGAS_GEFont> font = m_pEditEngine->GetFont();
391 for (
const FDE_TEXTEDITPIECE& info : m_pEditEngine->GetTextPieces()) {
393 if (!rtDocClip.IntersectWith(info.rtPiece))
396 std::vector<TextCharPos> char_pos = m_pEditEngine->GetDisplayPos(info);
397 if (char_pos.empty())
400 CFDE_TextOut::DrawString(pRenderDev, m_pEditEngine->GetFontColor(), font,
401 char_pos, m_pEditEngine->GetFontSize(), mt);
411 m_pEditEngine->SetAvailableWidth(m_EngineRect.width);
412 m_pEditEngine->SetCombText(
415 m_pEditEngine->EnableValidation(
417 m_pEditEngine->EnablePasswordMode(
420 uint32_t alignment = 0;
445 m_pEditEngine->SetAlignment(alignment);
450 m_pEditEngine->EnableMultiLine(
true);
451 m_pEditEngine->EnableLineWrap(!auto_hscroll);
452 m_pEditEngine->LimitVerticalScroll(
456 m_pEditEngine->EnableMultiLine(
false);
457 m_pEditEngine->EnableLineWrap(
false);
458 m_pEditEngine->LimitVerticalScroll(
false);
460 m_pEditEngine->LimitHorizontalScroll(!auto_hscroll);
466 RetainPtr<CFGAS_GEFont> pFont = theme->GetFont(part);
470 m_pEditEngine->SetFont(pFont);
471 m_pEditEngine->SetFontColor(theme->GetTextColor(part));
472 m_pEditEngine->SetFontSize(m_fFontSize);
473 m_pEditEngine->SetLineSpace(theme->GetLineHeight(part));
474 m_pEditEngine->SetTabWidth(m_fFontSize);
475 m_pEditEngine->SetVisibleLineCount(m_EngineRect.height /
476 theme->GetLineHeight(part));
480 m_pEditEngine->Layout();
486 float fOffSetX = m_EngineRect
.left - m_fScrollOffsetX;
487 float fOffSetY = m_EngineRect
.top - m_fScrollOffsetY + m_fVAlignOffset;
490 const CFX_RectF& edit_bounds = m_EngineRect;
492 CFX_RectF contents_bounds = m_pEditEngine->GetContentsBoundingBox();
494 if (contents_bounds
.right() < edit_bounds
.right() && m_fScrollOffsetX > 0) {
496 m_fScrollOffsetX =
std::max(m_fScrollOffsetX, 0.0f);
499 m_fScrollOffsetY > 0) {
501 m_fScrollOffsetY =
std::max(m_fScrollOffsetY, 0.0f);
513 offsetY = rtCaret
.top - edit_bounds
.top;
517 m_fScrollOffsetX += offsetX;
518 m_fScrollOffsetY += offsetY;
519 if (m_fFontSize > m_EngineRect
.height)
520 m_fScrollOffsetY = 0;
525bool CFWL_Edit::UpdateOffset(CFWL_ScrollBar* pScrollBar,
float fPosChanged) {
526 m_fScrollOffsetY += fPosChanged;
533 const CFX_SizeF pSpace = theme->GetSpaceAboveBelow(part);
534 const float fSpaceAbove = pSpace.width >= 0.1f ? pSpace.width : 0.0f;
535 const float fSpaceBelow = pSpace.height >= 0.1f ? pSpace.height : 0.0f;
536 float fOffsetY = 0.0f;
537 CFX_RectF contents_bounds = m_pEditEngine->GetContentsBoundingBox();
540 if (fOffsetY < (fSpaceAbove + fSpaceBelow) / 2.0f &&
541 fSpaceAbove < fSpaceBelow) {
544 fOffsetY += (fSpaceAbove - fSpaceBelow) / 2.0f;
547 fOffsetY -= fSpaceBelow;
549 fOffsetY += fSpaceAbove;
551 m_fVAlignOffset =
std::max(fOffsetY, 0.0f);
557 m_EngineRect
.top - m_fScrollOffsetY + m_fVAlignOffset
);
573CFWL_ScrollBar*
CFWL_Edit::UpdateScroll() {
574 bool bShowVert = m_pVertScrollBar && m_pVertScrollBar->IsVisible();
578 CFX_RectF contents_bounds = m_pEditEngine->GetContentsBoundingBox();
579 CFX_RectF rtScroll = m_pVertScrollBar->GetWidgetRect();
581 float fStep = m_pEditEngine->GetLineSpace();
584 m_pVertScrollBar->SetRange(0.0f, fRange);
585 float fPos =
std::clamp(m_fScrollOffsetY, 0.0f, fRange);
586 m_pVertScrollBar->SetPos(fPos);
587 m_pVertScrollBar->SetTrackPos(fPos);
588 m_pVertScrollBar->SetPageSize(rtScroll.height);
589 m_pVertScrollBar->SetStepSize(fStep);
591 m_pVertScrollBar->Update();
592 return m_pVertScrollBar;
595 m_pVertScrollBar->SetRange(0, -1);
597 m_pVertScrollBar->Update();
598 return m_pVertScrollBar;
603bool CFWL_Edit::IsShowVertScrollBar()
const {
609 IsContentHeightOverflow();
612bool CFWL_Edit::IsContentHeightOverflow()
const {
613 return m_pEditEngine->GetContentsBoundingBox().height >
614 m_EngineRect.height + 1.0f;
619 m_EngineRect
= m_ClientRect;
635 bool bShowVertScrollbar = IsShowVertScrollBar();
636 if (bShowVertScrollbar) {
637 InitVerticalScrollBar();
646 m_EngineRect
.width -= fWidth;
649 m_pVertScrollBar->SetWidgetRect(rtVertScr);
651 m_pVertScrollBar->Update();
652 }
else if (m_pVertScrollBar) {
661 bool bShowVertScrollbar = IsShowVertScrollBar();
664 if (bShowVertScrollbar) {
665 if (!m_pVertScrollBar) {
666 InitVerticalScrollBar();
675 m_pVertScrollBar->SetWidgetRect(rtVertScr);
676 m_pVertScrollBar->Update();
679 }
else if (m_pVertScrollBar) {
682 if (bShowVertScrollbar)
686CFX_PointF
CFWL_Edit::DeviceToEngine(
const CFX_PointF& pt) {
687 return pt + CFX_PointF(m_fScrollOffsetX - m_EngineRect
.left,
688 m_fScrollOffsetY - m_EngineRect
.top - m_fVAlignOffset);
692 if (m_pVertScrollBar)
695 m_pVertScrollBar = cppgc::MakeGarbageCollected<CFWL_ScrollBar>(
696 GetFWLApp()->GetHeap()->GetAllocationHandle(), GetFWLApp(),
704 m_pCaret->ShowCaret();
705 if (!pRect->IsEmpty())
706 m_pCaret->SetWidgetRect(*pRect);
729 m_pCaret->HideCaret();
749 m_pCaret = cppgc::MakeGarbageCollected<CFWL_Caret>(
750 GetFWLApp()->GetHeap()->GetAllocationHandle(), GetFWLApp(), Properties(),
752 m_pCaret->SetStates(m_Properties.m_dwStates);
758 if (m_pEditEngine->CanGenerateCharacterInfo()) {
759 std::tie(bidi_level, m_CaretRect) = m_pEditEngine->GetCharacterInfo(
760 pdfium::base::checked_cast<int32_t>(m_CursorPosition));
776void CFWL_Edit::SetCursorPosition(size_t position) {
777 if (m_CursorPosition == position)
780 m_CursorPosition = std::min(position, m_pEditEngine->GetLength());
794 CFWL_MessageMouse* pMsg =
static_cast<CFWL_MessageMouse*>(pMessage);
803 OnButtonDoubleClick(pMsg);
817 CFWL_MessageKey* pKey =
static_cast<CFWL_MessageKey*>(pMessage);
837 if ((pSrcTarget == m_pVertScrollBar && m_pVertScrollBar)) {
838 CFWL_EventScroll* pScrollEvent =
static_cast<CFWL_EventScroll*>(pEvent);
839 OnScroll(
static_cast<CFWL_ScrollBar*>(pSrcTarget),
849void CFWL_Edit::DoRButtonDown(CFWL_MessageMouse* pMsg) {
851 m_pEditEngine->GetIndexForPoint(DeviceToEngine(pMsg->m_pos)));
863 bool bRepaint =
false;
880void CFWL_Edit::OnLButtonDown(CFWL_MessageMouse* pMsg) {
884 m_bLButtonDown =
true;
887 bool bRepaint =
false;
888 if (m_pEditEngine->HasSelection()) {
889 m_pEditEngine->ClearSelection();
893 size_t index_at_click =
894 m_pEditEngine->GetIndexForPoint(DeviceToEngine(pMsg->m_pos));
896 if (index_at_click != m_CursorPosition &&
897 !!(pMsg->m_dwFlags & XFA_FWL_KeyFlag::kShift)) {
898 size_t start = std::min(m_CursorPosition, index_at_click);
899 size_t end = std::max(m_CursorPosition, index_at_click);
901 m_pEditEngine->SetSelection(start, end - start);
904 SetCursorPosition(index_at_click);
911void CFWL_Edit::OnLButtonUp(CFWL_MessageMouse* pMsg) {
912 m_bLButtonDown =
false;
916void CFWL_Edit::OnButtonDoubleClick(CFWL_MessageMouse* pMsg) {
918 m_pEditEngine->GetIndexForPoint(DeviceToEngine(pMsg->m_pos));
921 std::tie(start_idx, count) = m_pEditEngine->BoundsForWordAt(click_idx);
923 m_pEditEngine->SetSelection(start_idx, count);
924 m_CursorPosition = start_idx + count;
928void CFWL_Edit::OnMouseMove(CFWL_MessageMouse* pMsg) {
930 if (!m_bLButtonDown || !shift)
933 size_t old_cursor_pos = m_CursorPosition;
935 m_pEditEngine->GetIndexForPoint(DeviceToEngine(pMsg->m_pos)));
936 if (old_cursor_pos == m_CursorPosition)
939 size_t length = m_pEditEngine->GetLength();
940 if (m_CursorPosition > length)
941 SetCursorPosition(length);
943 size_t sel_start = 0;
945 if (m_pEditEngine->HasSelection())
946 std::tie(sel_start, count) = m_pEditEngine->GetSelection();
948 sel_start = old_cursor_pos;
950 size_t start_pos = std::min(sel_start, m_CursorPosition);
951 size_t end_pos = std::max(sel_start, m_CursorPosition);
952 m_pEditEngine->SetSelection(start_pos, end_pos - start_pos);
955void CFWL_Edit::OnKeyDown(CFWL_MessageKey* pMsg) {
959 size_t sel_start = m_CursorPosition;
960 if (m_pEditEngine->HasSelection()) {
963 std::tie(start_idx, count) = m_pEditEngine->GetSelection();
964 sel_start = start_idx;
968 case XFA_FWL_VKEY_Left:
969 SetCursorPosition(m_pEditEngine->GetIndexLeft(m_CursorPosition));
971 case XFA_FWL_VKEY_Right:
972 SetCursorPosition(m_pEditEngine->GetIndexRight(m_CursorPosition));
974 case XFA_FWL_VKEY_Up:
975 SetCursorPosition(m_pEditEngine->GetIndexUp(m_CursorPosition));
977 case XFA_FWL_VKEY_Down:
978 SetCursorPosition(m_pEditEngine->GetIndexDown(m_CursorPosition));
980 case XFA_FWL_VKEY_Home:
982 bCtrl ? 0 : m_pEditEngine->GetIndexAtStartOfLine(m_CursorPosition));
984 case XFA_FWL_VKEY_End:
986 bCtrl ? m_pEditEngine->GetLength()
987 : m_pEditEngine->GetIndexAtEndOfLine(m_CursorPosition));
995 m_pEditEngine->Delete(m_CursorPosition, 1);
1007 if (bShift && sel_start != m_CursorPosition) {
1008 m_pEditEngine->SetSelection(std::min(sel_start, m_CursorPosition),
1009 std::max(sel_start, m_CursorPosition));
1014void CFWL_Edit::OnChar(CFWL_MessageKey* pMsg) {
1023 if (m_CursorPosition > 0) {
1024 SetCursorPosition(m_CursorPosition - 1);
1025 m_pEditEngine->Delete(m_CursorPosition, 1);
1034 m_pEditEngine->Insert(m_CursorPosition, L"\t");
1035 SetCursorPosition(m_CursorPosition + 1);
1039 m_pEditEngine->Insert(m_CursorPosition, L"\n");
1040 SetCursorPosition(m_CursorPosition + 1);
1044 if (pMsg->m_dwFlags & kEditingModifier)
1047 m_pEditEngine->Insert(m_CursorPosition, WideString(c));
1048 SetCursorPosition(m_CursorPosition + 1);
1054bool CFWL_Edit::OnScroll(CFWL_ScrollBar* pScrollBar,
1055 CFWL_EventScroll::
Code dwCode,
1073 if (fPos < fMin + fStep / 2) {
1080 if (fPos > fMax - fStep / 2) {
1106 if (iCurPos == fPos)
1111 UpdateOffset(pScrollBar, fPos - iCurPos);
@ CFX_TxtLineAlignment_Justified
@ CFX_TxtLineAlignment_Right
@ CFX_TxtLineAlignment_Center
@ CFX_TxtLineAlignment_Left
#define FWL_STYLEEXT_EDT_MultiLine
#define FWL_STYLEEXT_EDT_HFar
#define FWL_STYLEEXT_EDT_VAlignMask
#define FWL_STYLEEXT_EDT_HAlignModeMask
#define FWL_STYLEEXT_EDT_HAlignMask
#define FWL_STYLEEXT_EDT_ShowScrollbarFocus
#define FWL_STYLEEXT_EDT_WantReturn
#define FWL_STYLEEXT_EDT_VCenter
#define FWL_STYLEEXT_EDT_OuterScrollbar
#define FWL_STYLEEXT_EDT_AutoHScroll
#define FWL_STYLEEXT_EDT_ReadOnly
#define FWL_STYLEEXT_EDT_VFar
#define FWL_STYLEEXT_EDT_AutoVScroll
#define FWL_STYLEEXT_EDT_Validate
#define FWL_STYLEEXT_EDT_HNear
#define FWL_STYLEEXT_EDT_Password
#define FWL_STYLEEXT_EDT_Justified
#define FWL_STYLEEXT_EDT_CombText
#define FWL_STYLEEXT_EDT_HCenter
StateRestorer(CFGAS_GEGraphics *graphics)
CFX_RenderDevice * GetRenderDevice()
void SetClipRect(const CFX_RectF &rect)
void AddLine(const CFX_PointF &p1, const CFX_PointF &p2)
CFX_RectF GetAutosizedWidgetRect() override
void OnTextChanged() override
CFX_RectF GetWidgetRect() override
virtual void SetText(const WideString &wsText)
void SetLimit(int32_t nLimit)
void OnProcessMessage(CFWL_Message *pMessage) override
void OnCaretChanged() override
void DrawWidget(CFGAS_GEGraphics *pGraphics, const CFX_Matrix &matrix) override
size_t GetTextLength() const
void Trace(cppgc::Visitor *visitor) const override
void SetScrollOffset(float fScrollOffset) override
CFWL_Edit(CFWL_App *app, const Properties &properties, CFWL_Widget *pOuter)
FWL_WidgetHit HitTest(const CFX_PointF &point) override
bool OnValidate(const WideString &wsText) override
void SetAliasChar(wchar_t wAlias)
bool Paste(const WideString &wsPaste)
void OnTextWillChange(CFDE_TextEditEngine::TextChange *change) override
absl::optional< WideString > Cut()
WideString GetText() const
FWL_Type GetClassID() const override
void PreFinalize() override
void ShowCaret(CFX_RectF *pRect)
void OnProcessEvent(CFWL_Event *pEvent) override
absl::optional< WideString > Copy()
void OnDrawWidget(CFGAS_GEGraphics *pGraphics, const CFX_Matrix &matrix) override
void SetStates(uint32_t dwStates) override
void HideCaret(CFX_RectF *pRect)
bool HasSelection() const
void OnSelChanged() override
void NotifyTextFull() override
std::pair< size_t, size_t > GetSelection() const
virtual void SetTextSkipNotify(const WideString &wsText)
WideString GetChangeText() const
bool GetCancelled() const
CFWL_Widget * GetSrcTarget() const
const uint32_t m_dwKeyCodeOrChar
const MouseCommand m_dwCmd
CFWL_Widget * GetDstTarget() const
void SetPath(const CFGAS_GEPath *pPath)
CFX_Matrix & operator=(const CFX_Matrix &other)=default
CFX_Matrix(float a1, float b1, float c1, float d1, float e1, float f1)
CFX_RectF TransformRect(const CFX_RectF &rect) const
CFX_Matrix GetInverse() const
void Concat(const CFX_Matrix &right)
void Offset(float dx, float dy)
constexpr CFX_RectF()=default
bool Contains(const CFX_RectF &rt) const
void Intersect(const CFX_RectF &rt)
FX_RECT GetOuterRect() const
void Deflate(float off_left, float off_top, float off_right, float off_bottom)
CFX_RectF & operator=(const CFX_RectF &other)=default
bool Contains(const PointType &p) const
constexpr CFX_RectF(float dst_left, float dst_top, float dst_width, float dst_height)
void Union(const CFX_RectF &rt)
bool SetClip_Rect(const FX_RECT &pRect)
virtual float GetFontSize(const CFWL_ThemePart &pThemePart) const =0
virtual CFX_RectF GetUIMargin(const CFWL_ThemePart &pThemePart) const =0
virtual float GetScrollBarWidth() const =0