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/fwl/cfwl_event.h"
15 #include "xfa/fwl/cfwl_scrollbar.h"
16 #include "xfa/fwl/cfwl_widget.h"
17 #include "xfa/fxgraphics/cxfa_gepath.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_WidgetProperties;
44 class CFWL_Caret;
45 
46 class CFWL_Edit : public CFWL_Widget, public CFDE_TextEditEngine::Delegate {
47  public:
48   CFWL_Edit(const CFWL_App* app,
49             std::unique_ptr<CFWL_WidgetProperties> properties,
50             CFWL_Widget* pOuter);
51   ~CFWL_Edit() override;
52 
53   // CFWL_Widget:
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(CXFA_Graphics* pGraphics, const CFX_Matrix& matrix) override;
61   void SetThemeProvider(IFWL_ThemeProvider* pThemeProvider) override;
62   void OnProcessMessage(CFWL_Message* pMessage) override;
63   void OnProcessEvent(CFWL_Event* pEvent) override;
64   void OnDrawWidget(CXFA_Graphics* pGraphics,
65                     const CFX_Matrix& matrix) override;
66 
67   virtual void SetText(const WideString& wsText);
68   virtual void SetTextSkipNotify(const WideString& wsText);
69 
70   int32_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   Optional<WideString> Copy();
84   Optional<WideString> Cut();
85   bool Paste(const WideString& wsPaste);
86   bool Undo();
87   bool Redo();
88   bool CanUndo();
89   bool CanRedo();
90 
91   void SetOuter(CFWL_Widget* pOuter);
92 
93   // CFDE_TextEditEngine::Delegate
94   void NotifyTextFull() override;
95   void OnCaretChanged() override;
96   void OnTextWillChange(CFDE_TextEditEngine::TextChange* change) override;
97   void OnTextChanged() override;
98   void OnSelChanged() override;
99   bool OnValidate(const WideString& wsText) override;
100   void SetScrollOffset(float fScrollOffset) override;
101 
102  protected:
103   void ShowCaret(CFX_RectF* pRect);
104   void HideCaret(CFX_RectF* pRect);
GetRTClient()105   const CFX_RectF& GetRTClient() const { return m_rtClient; }
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 DrawTextBk(CXFA_Graphics* pGraphics,
113                   IFWL_ThemeProvider* pTheme,
114                   const CFX_Matrix* pMatrix);
115   void DrawContent(CXFA_Graphics* pGraphics,
116                    IFWL_ThemeProvider* pTheme,
117                    const CFX_Matrix* pMatrix);
118 
119   void UpdateEditEngine();
120   void UpdateEditParams();
121   void UpdateEditLayout();
122   bool UpdateOffset();
123   bool UpdateOffset(CFWL_ScrollBar* pScrollBar, float fPosChanged);
124   void UpdateVAlignment();
125   void UpdateCaret();
126   CFWL_ScrollBar* UpdateScroll();
127   void Layout();
128   void LayoutScrollBar();
129   CFX_PointF DeviceToEngine(const CFX_PointF& pt);
130   void InitVerticalScrollBar();
131   void InitHorizontalScrollBar();
132   void InitEngine();
133   void InitCaret();
134   bool ValidateNumberChar(wchar_t cNum);
135   bool IsShowScrollBar(bool bVert);
136   bool IsContentHeightOverflow();
137   void SetCursorPosition(size_t position);
138   void UpdateCursorRect();
139 
140   void DoRButtonDown(CFWL_MessageMouse* pMsg);
141   void OnFocusChanged(CFWL_Message* pMsg, bool bSet);
142   void OnLButtonDown(CFWL_MessageMouse* pMsg);
143   void OnLButtonUp(CFWL_MessageMouse* pMsg);
144   void OnButtonDoubleClick(CFWL_MessageMouse* pMsg);
145   void OnMouseMove(CFWL_MessageMouse* pMsg);
146   void OnKeyDown(CFWL_MessageKey* pMsg);
147   void OnChar(CFWL_MessageKey* pMsg);
148   bool OnScroll(CFWL_ScrollBar* pScrollBar,
149                 CFWL_EventScroll::Code dwCode,
150                 float fPos);
151 
152   CFX_RectF m_rtClient;
153   CFX_RectF m_rtEngine;
154   CFX_RectF m_rtStatic;
155   CFX_RectF m_rtCaret;
156   bool m_bLButtonDown = false;
157   bool m_bSetRange = false;
158   int32_t m_nLimit = -1;
159   int32_t m_iMax = 0xFFFFFFF;
160   float m_fVAlignOffset = 0.0f;
161   float m_fScrollOffsetX = 0.0f;
162   float m_fScrollOffsetY = 0.0f;
163   float m_fFontSize = 0.0f;
164   size_t m_CursorPosition = 0;
165   std::unique_ptr<CFDE_TextEditEngine> const m_pEditEngine;
166   std::unique_ptr<CFWL_ScrollBar> m_pVertScrollBar;
167   std::unique_ptr<CFWL_ScrollBar> m_pHorzScrollBar;
168   std::unique_ptr<CFWL_Caret> m_pCaret;
169   WideString m_wsCache;
170   WideString m_wsFont;
171 };
172 
173 #endif  // XFA_FWL_CFWL_EDIT_H_
174