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 FPDFSDK_INCLUDE_PDFWINDOW_PWL_EDITCTRL_H_
8#define FPDFSDK_INCLUDE_PDFWINDOW_PWL_EDITCTRL_H_
9
10#include "PWL_Wnd.h"
11#include "core/include/fxcrt/fx_string.h"
12#include "fpdfsdk/include/fxedit/fx_edit.h"
13
14class CPWL_Caret;
15class CPWL_Edit;
16class CPWL_EditCtrl;
17class IFX_Edit;
18class IPWL_Edit_Notify;
19struct CPVT_WordPlace;
20
21enum PWL_EDIT_ALIGNFORMAT_H { PEAH_LEFT = 0, PEAH_MIDDLE, PEAH_RIGHT };
22
23enum PWL_EDIT_ALIGNFORMAT_V { PEAV_TOP = 0, PEAV_CENTER, PEAV_BOTTOM };
24
25class IPWL_Edit_Notify {
26 public:
27  virtual ~IPWL_Edit_Notify() {}
28  // when the position of caret is changed in edit
29  virtual void OnCaretMove(int32_t x1, int32_t y1, int32_t x2, int32_t y2) {}
30  virtual void OnContentChange(const CPDF_Rect& rcContent) {}
31  // OprType: 0 InsertWord
32  // 1 InsertReturn
33  // 2 BackSpace
34  // 3 Delete
35  // 4 Clear
36  // 5 InsertText
37  // 6 SetText
38  virtual void OnInsertWord(const CPVT_WordPlace& place,
39                            const CPVT_WordPlace& oldplace) {}
40  virtual void OnInsertReturn(const CPVT_WordPlace& place,
41                              const CPVT_WordPlace& oldplace) {}
42  virtual void OnBackSpace(const CPVT_WordPlace& place,
43                           const CPVT_WordPlace& oldplace) {}
44  virtual void OnDelete(const CPVT_WordPlace& place,
45                        const CPVT_WordPlace& oldplace) {}
46  virtual void OnClear(const CPVT_WordPlace& place,
47                       const CPVT_WordPlace& oldplace) {}
48  virtual void OnInsertText(const CPVT_WordPlace& place,
49                            const CPVT_WordPlace& oldplace) {}
50  virtual void OnSetText(const CPVT_WordPlace& place,
51                         const CPVT_WordPlace& oldplace) {}
52  virtual void OnAddUndo(CPWL_Edit* pEdit) {}
53};
54
55class CPWL_EditCtrl : public CPWL_Wnd, public IFX_Edit_Notify {
56  friend class CPWL_Edit_Notify;
57
58 public:
59  CPWL_EditCtrl();
60  ~CPWL_EditCtrl() override;
61
62  CPDF_Rect GetContentRect() const;
63  void GetCaretPos(int32_t& x, int32_t& y) const;
64
65  CFX_WideString GetText() const;
66  void SetSel(int32_t nStartChar, int32_t nEndChar);
67  void GetSel(int32_t& nStartChar, int32_t& nEndChar) const;
68  void GetTextRange(const CPDF_Rect& rect,
69                    int32_t& nStartChar,
70                    int32_t& nEndChar) const;
71  CFX_WideString GetText(int32_t& nStartChar, int32_t& nEndChar) const;
72  void Clear();
73  void SelectAll();
74
75  int32_t GetCaret() const;
76  void SetCaret(int32_t nPos);
77  int32_t GetTotalWords() const;
78
79  void Paint();
80
81  void EnableRefresh(FX_BOOL bRefresh);
82  CPDF_Point GetScrollPos() const;
83  void SetScrollPos(const CPDF_Point& point);
84
85  void SetEditNotify(IPWL_Edit_Notify* pNotify) { m_pEditNotify = pNotify; }
86
87  void SetCharSet(uint8_t nCharSet) { m_nCharSet = nCharSet; }
88  int32_t GetCharSet() const;
89
90  void SetCodePage(int32_t nCodePage) { m_nCodePage = nCodePage; }
91  int32_t GetCodePage() const { return m_nCodePage; }
92
93  CPDF_Font* GetCaretFont() const;
94  FX_FLOAT GetCaretFontSize() const;
95
96  FX_BOOL CanUndo() const;
97  FX_BOOL CanRedo() const;
98  void Redo();
99  void Undo();
100
101  void SetReadyToInput();
102
103  // CPWL_Wnd
104  void OnCreate(PWL_CREATEPARAM& cp) override;
105  void OnCreated() override;
106  FX_BOOL OnKeyDown(FX_WORD nChar, FX_DWORD nFlag) override;
107  FX_BOOL OnChar(FX_WORD nChar, FX_DWORD nFlag) override;
108  FX_BOOL OnLButtonDown(const CPDF_Point& point, FX_DWORD nFlag) override;
109  FX_BOOL OnLButtonUp(const CPDF_Point& point, FX_DWORD nFlag) override;
110  FX_BOOL OnMouseMove(const CPDF_Point& point, FX_DWORD nFlag) override;
111  void OnNotify(CPWL_Wnd* pWnd,
112                FX_DWORD msg,
113                intptr_t wParam = 0,
114                intptr_t lParam = 0) override;
115  void CreateChildWnd(const PWL_CREATEPARAM& cp) override;
116  void RePosChildWnd() override;
117  void SetFontSize(FX_FLOAT fFontSize) override;
118  FX_FLOAT GetFontSize() const override;
119  void SetCursor() override;
120
121 protected:
122  // IFX_Edit_Notify
123  void IOnSetScrollInfoX(FX_FLOAT fPlateMin,
124                         FX_FLOAT fPlateMax,
125                         FX_FLOAT fContentMin,
126                         FX_FLOAT fContentMax,
127                         FX_FLOAT fSmallStep,
128                         FX_FLOAT fBigStep) override {}
129  void IOnSetScrollInfoY(FX_FLOAT fPlateMin,
130                         FX_FLOAT fPlateMax,
131                         FX_FLOAT fContentMin,
132                         FX_FLOAT fContentMax,
133                         FX_FLOAT fSmallStep,
134                         FX_FLOAT fBigStep) override;
135  void IOnSetScrollPosX(FX_FLOAT fx) override {}
136  void IOnSetScrollPosY(FX_FLOAT fy) override;
137  void IOnSetCaret(FX_BOOL bVisible,
138                   const CPDF_Point& ptHead,
139                   const CPDF_Point& ptFoot,
140                   const CPVT_WordPlace& place) override;
141  void IOnCaretChange(const CPVT_SecProps& secProps,
142                      const CPVT_WordProps& wordProps) override;
143  void IOnContentChange(const CPDF_Rect& rcContent) override;
144  void IOnInvalidateRect(CPDF_Rect* pRect) override;
145
146  void InsertText(const FX_WCHAR* csText);
147  void SetText(const FX_WCHAR* csText);
148  void CopyText();
149  void PasteText();
150  void CutText();
151  void ShowVScrollBar(FX_BOOL bShow);
152  void InsertWord(FX_WORD word, int32_t nCharset);
153  void InsertReturn();
154
155  FX_BOOL IsWndHorV();
156
157  void Delete();
158  void Backspace();
159
160  void GetCaretInfo(CPDF_Point& ptHead, CPDF_Point& ptFoot) const;
161  void SetCaret(FX_BOOL bVisible,
162                const CPDF_Point& ptHead,
163                const CPDF_Point& ptFoot);
164
165  void SetEditCaret(FX_BOOL bVisible);
166
167 private:
168  void CreateEditCaret(const PWL_CREATEPARAM& cp);
169
170 protected:
171  IFX_Edit* m_pEdit;
172  CPWL_Caret* m_pEditCaret;
173  FX_BOOL m_bMouseDown;
174  IPWL_Edit_Notify* m_pEditNotify;
175
176 private:
177  int32_t m_nCharSet;
178  int32_t m_nCodePage;
179};
180
181#endif  // FPDFSDK_INCLUDE_PDFWINDOW_PWL_EDITCTRL_H_
182