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 #ifndef XFA_FWL_CFWL_EDIT_H_ 8 #define XFA_FWL_CFWL_EDIT_H_ 9 10 #include <memory> 11 #include <utility> 12 13 #include "xfa/fde/cfde_texteditengine.h" 14 #include "xfa/fgas/graphics/cfgas_gepath.h" 15 #include "xfa/fwl/cfwl_event.h" 16 #include "xfa/fwl/cfwl_scrollbar.h" 17 #include "xfa/fwl/cfwl_widget.h" 18 19 #define FWL_STYLEEXT_EDT_ReadOnly (1L << 0) 20 #define FWL_STYLEEXT_EDT_MultiLine (1L << 1) 21 #define FWL_STYLEEXT_EDT_WantReturn (1L << 2) 22 #define FWL_STYLEEXT_EDT_AutoHScroll (1L << 4) 23 #define FWL_STYLEEXT_EDT_AutoVScroll (1L << 5) 24 #define FWL_STYLEEXT_EDT_Validate (1L << 7) 25 #define FWL_STYLEEXT_EDT_Password (1L << 8) 26 #define FWL_STYLEEXT_EDT_Number (1L << 9) 27 #define FWL_STYLEEXT_EDT_CombText (1L << 17) 28 #define FWL_STYLEEXT_EDT_HNear 0 29 #define FWL_STYLEEXT_EDT_HCenter (1L << 18) 30 #define FWL_STYLEEXT_EDT_HFar (2L << 18) 31 #define FWL_STYLEEXT_EDT_VNear 0 32 #define FWL_STYLEEXT_EDT_VCenter (1L << 20) 33 #define FWL_STYLEEXT_EDT_VFar (2L << 20) 34 #define FWL_STYLEEXT_EDT_Justified (1L << 22) 35 #define FWL_STYLEEXT_EDT_HAlignMask (3L << 18) 36 #define FWL_STYLEEXT_EDT_VAlignMask (3L << 20) 37 #define FWL_STYLEEXT_EDT_HAlignModeMask (3L << 22) 38 #define FWL_STYLEEXT_EDT_ShowScrollbarFocus (1L << 25) 39 #define FWL_STYLEEXT_EDT_OuterScrollbar (1L << 26) 40 41 class CFWL_MessageKey; 42 class CFWL_MessageMouse; 43 class CFWL_Caret; 44 class CFX_RenderDevice; 45 46 class CFWL_Edit : public CFWL_Widget, public CFDE_TextEditEngine::Delegate { 47 public: 48 CONSTRUCT_VIA_MAKE_GARBAGE_COLLECTED; 49 ~CFWL_Edit() override; 50 51 // CFWL_Widget: 52 void PreFinalize() override; 53 void Trace(cppgc::Visitor* visitor) const override; 54 FWL_Type GetClassID() const override; 55 CFX_RectF GetAutosizedWidgetRect() override; 56 CFX_RectF GetWidgetRect() override; 57 void Update() override; 58 FWL_WidgetHit HitTest(const CFX_PointF& point) override; 59 void SetStates(uint32_t dwStates) override; 60 void DrawWidget(CFGAS_GEGraphics* pGraphics, 61 const CFX_Matrix& matrix) override; 62 void OnProcessMessage(CFWL_Message* pMessage) override; 63 void OnProcessEvent(CFWL_Event* pEvent) override; 64 void OnDrawWidget(CFGAS_GEGraphics* pGraphics, 65 const CFX_Matrix& matrix) override; 66 67 virtual void SetText(const WideString& wsText); 68 virtual void SetTextSkipNotify(const WideString& wsText); 69 70 size_t GetTextLength() const; 71 WideString GetText() const; 72 void ClearText(); 73 74 void SelectAll(); 75 void ClearSelection(); 76 bool HasSelection() const; 77 // Returns <start, count> of the selection. 78 std::pair<size_t, size_t> GetSelection() const; 79 80 int32_t GetLimit() const; 81 void SetLimit(int32_t nLimit); 82 void SetAliasChar(wchar_t wAlias); 83 absl::optional<WideString> Copy(); 84 absl::optional<WideString> Cut(); 85 bool Paste(const WideString& wsPaste); 86 bool Undo(); 87 bool Redo(); 88 bool CanUndo(); 89 bool CanRedo(); 90 91 // CFDE_TextEditEngine::Delegate 92 void NotifyTextFull() override; 93 void OnCaretChanged() override; 94 void OnTextWillChange(CFDE_TextEditEngine::TextChange* change) override; 95 void OnTextChanged() override; 96 void OnSelChanged() override; 97 bool OnValidate(const WideString& wsText) override; 98 void SetScrollOffset(float fScrollOffset) override; 99 100 protected: 101 CFWL_Edit(CFWL_App* app, const Properties& properties, CFWL_Widget* pOuter); 102 103 void ShowCaret(CFX_RectF* pRect); 104 void HideCaret(CFX_RectF* pRect); GetRTClient()105 const CFX_RectF& GetRTClient() const { return m_ClientRect; } GetTxtEdtEngine()106 CFDE_TextEditEngine* GetTxtEdtEngine() { return m_pEditEngine.get(); } 107 108 private: 109 void RenderText(CFX_RenderDevice* pRenderDev, 110 const CFX_RectF& clipRect, 111 const CFX_Matrix& mt); 112 void DrawContent(CFGAS_GEGraphics* pGraphics, const CFX_Matrix& mtMatrix); 113 void DrawContentNonComb(CFGAS_GEGraphics* pGraphics, 114 const CFX_Matrix& mtMatrix); 115 116 void UpdateEditEngine(); 117 void UpdateEditParams(); 118 void UpdateEditLayout(); 119 bool UpdateOffset(); 120 bool UpdateOffset(CFWL_ScrollBar* pScrollBar, float fPosChanged); 121 void UpdateVAlignment(); 122 void UpdateCaret(); 123 CFWL_ScrollBar* UpdateScroll(); 124 void Layout(); 125 void LayoutScrollBar(); 126 CFX_PointF DeviceToEngine(const CFX_PointF& pt); 127 void InitVerticalScrollBar(); 128 void InitEngine(); 129 void InitCaret(); 130 bool IsShowVertScrollBar() const; 131 bool IsContentHeightOverflow() const; 132 void SetCursorPosition(size_t position); 133 void UpdateCursorRect(); 134 135 void DoRButtonDown(CFWL_MessageMouse* pMsg); 136 void OnFocusGained(); 137 void OnFocusLost(); 138 void OnLButtonDown(CFWL_MessageMouse* pMsg); 139 void OnLButtonUp(CFWL_MessageMouse* pMsg); 140 void OnButtonDoubleClick(CFWL_MessageMouse* pMsg); 141 void OnMouseMove(CFWL_MessageMouse* pMsg); 142 void OnKeyDown(CFWL_MessageKey* pMsg); 143 void OnChar(CFWL_MessageKey* pMsg); 144 bool OnScroll(CFWL_ScrollBar* pScrollBar, 145 CFWL_EventScroll::Code dwCode, 146 float fPos); 147 148 CFX_RectF m_ClientRect; 149 CFX_RectF m_EngineRect; 150 CFX_RectF m_StaticRect; 151 CFX_RectF m_CaretRect; 152 bool m_bLButtonDown = false; 153 int32_t m_nLimit = -1; 154 float m_fVAlignOffset = 0.0f; 155 float m_fScrollOffsetX = 0.0f; 156 float m_fScrollOffsetY = 0.0f; 157 float m_fFontSize = 0.0f; 158 size_t m_CursorPosition = 0; 159 std::unique_ptr<CFDE_TextEditEngine> const m_pEditEngine; 160 cppgc::Member<CFWL_ScrollBar> m_pVertScrollBar; 161 cppgc::Member<CFWL_Caret> m_pCaret; 162 WideString m_wsCache; 163 WideString m_wsFont; 164 }; 165 166 #endif // XFA_FWL_CFWL_EDIT_H_ 167