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 #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_Edit;
42 class CFWL_MessageMouse;
43 class CFWL_Caret;
44 
45 class CFWL_Edit : public CFWL_Widget, public CFDE_TextEditEngine::Delegate {
46  public:
47   CONSTRUCT_VIA_MAKE_GARBAGE_COLLECTED;
48   ~CFWL_Edit() override;
49 
50   // CFWL_Widget:
51   void PreFinalize() override;
52   void Trace(cppgc::Visitor* visitor) const override;
53   FWL_Type GetClassID() const override;
54   CFX_RectF GetAutosizedWidgetRect() override;
55   CFX_RectF GetWidgetRect() override;
56   void Update() override;
57   FWL_WidgetHit HitTest(const CFX_PointF& point) override;
58   void SetStates(uint32_t dwStates) override;
59   void DrawWidget(CFGAS_GEGraphics* pGraphics,
60                   const CFX_Matrix& matrix) override;
61   void OnProcessMessage(CFWL_Message* pMessage) override;
62   void OnProcessEvent(CFWL_Event* pEvent) override;
63   void OnDrawWidget(CFGAS_GEGraphics* pGraphics,
64                     const CFX_Matrix& matrix) override;
65 
66   virtual void SetText(const WideString& wsText);
67   virtual void SetTextSkipNotify(const WideString& wsText);
68 
69   int32_t GetTextLength() const;
70   WideString GetText() const;
71   void ClearText();
72 
73   void SelectAll();
74   void ClearSelection();
75   bool HasSelection() const;
76   // Returns <start, count> of the selection.
77   std::pair<size_t, size_t> GetSelection() const;
78 
79   int32_t GetLimit() const;
80   void SetLimit(int32_t nLimit);
81   void SetAliasChar(wchar_t wAlias);
82   Optional<WideString> Copy();
83   Optional<WideString> Cut();
84   bool Paste(const WideString& wsPaste);
85   bool Undo();
86   bool Redo();
87   bool CanUndo();
88   bool CanRedo();
89 
90   // CFDE_TextEditEngine::Delegate
91   void NotifyTextFull() override;
92   void OnCaretChanged() override;
93   void OnTextWillChange(CFDE_TextEditEngine::TextChange* change) override;
94   void OnTextChanged() override;
95   void OnSelChanged() override;
96   bool OnValidate(const WideString& wsText) override;
97   void SetScrollOffset(float fScrollOffset) override;
98 
99  protected:
100   CFWL_Edit(CFWL_App* app, const Properties& properties, CFWL_Widget* pOuter);
101 
102   void ShowCaret(CFX_RectF* pRect);
103   void HideCaret(CFX_RectF* pRect);
GetRTClient()104   const CFX_RectF& GetRTClient() const { return m_ClientRect; }
GetTxtEdtEngine()105   CFDE_TextEditEngine* GetTxtEdtEngine() { return m_pEditEngine.get(); }
106 
107  private:
108   void RenderText(CFX_RenderDevice* pRenderDev,
109                   const CFX_RectF& clipRect,
110                   const CFX_Matrix& mt);
111   void DrawContent(CFGAS_GEGraphics* pGraphics, const CFX_Matrix* pMatrix);
112 
113   void UpdateEditEngine();
114   void UpdateEditParams();
115   void UpdateEditLayout();
116   bool UpdateOffset();
117   bool UpdateOffset(CFWL_ScrollBar* pScrollBar, float fPosChanged);
118   void UpdateVAlignment();
119   void UpdateCaret();
120   CFWL_ScrollBar* UpdateScroll();
121   void Layout();
122   void LayoutScrollBar();
123   CFX_PointF DeviceToEngine(const CFX_PointF& pt);
124   void InitVerticalScrollBar();
125   void InitHorizontalScrollBar();
126   void InitEngine();
127   void InitCaret();
128   bool IsShowScrollBar(bool bVert);
129   bool IsContentHeightOverflow();
130   void SetCursorPosition(size_t position);
131   void UpdateCursorRect();
132 
133   void DoRButtonDown(CFWL_MessageMouse* pMsg);
134   void OnFocusChanged(CFWL_Message* pMsg, bool bSet);
135   void OnLButtonDown(CFWL_MessageMouse* pMsg);
136   void OnLButtonUp(CFWL_MessageMouse* pMsg);
137   void OnButtonDoubleClick(CFWL_MessageMouse* pMsg);
138   void OnMouseMove(CFWL_MessageMouse* pMsg);
139   void OnKeyDown(CFWL_MessageKey* pMsg);
140   void OnChar(CFWL_MessageKey* pMsg);
141   bool OnScroll(CFWL_ScrollBar* pScrollBar,
142                 CFWL_EventScroll::Code dwCode,
143                 float fPos);
144 
145   CFX_RectF m_ClientRect;
146   CFX_RectF m_EngineRect;
147   CFX_RectF m_StaticRect;
148   CFX_RectF m_CaretRect;
149   bool m_bLButtonDown = false;
150   int32_t m_nLimit = -1;
151   float m_fVAlignOffset = 0.0f;
152   float m_fScrollOffsetX = 0.0f;
153   float m_fScrollOffsetY = 0.0f;
154   float m_fFontSize = 0.0f;
155   size_t m_CursorPosition = 0;
156   std::unique_ptr<CFDE_TextEditEngine> const m_pEditEngine;
157   cppgc::Member<CFWL_ScrollBar> m_pVertScrollBar;
158   cppgc::Member<CFWL_ScrollBar> m_pHorzScrollBar;
159   cppgc::Member<CFWL_Caret> m_pCaret;
160   WideString m_wsCache;
161   WideString m_wsFont;
162 };
163 
164 #endif  // XFA_FWL_CFWL_EDIT_H_
165