xref: /aosp_15_r20/external/pdfium/fpdfsdk/pwl/cpwl_list_ctrl.h (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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 FPDFSDK_PWL_CPWL_LIST_CTRL_H_
8 #define FPDFSDK_PWL_CPWL_LIST_CTRL_H_
9 
10 #include <map>
11 #include <memory>
12 #include <vector>
13 
14 #include "core/fxcrt/fx_coordinates.h"
15 #include "core/fxcrt/unowned_ptr.h"
16 #include "core/fxcrt/widestring.h"
17 #include "fpdfsdk/pwl/cpwl_edit_impl.h"
18 
19 class IPVT_FontMap;
20 
21 class CPWL_ListCtrl {
22  public:
23   class NotifyIface {
24    public:
25     virtual ~NotifyIface();
26 
27     virtual void OnSetScrollInfoY(float fPlateMin,
28                                   float fPlateMax,
29                                   float fContentMin,
30                                   float fContentMax,
31                                   float fSmallStep,
32                                   float fBigStep) = 0;
33     virtual void OnSetScrollPosY(float fy) = 0;
34 
35     // Returns true if `this` is still allocated.
36     [[nodiscard]] virtual bool OnInvalidateRect(const CFX_FloatRect& rect) = 0;
37   };
38 
39   CPWL_ListCtrl();
40   ~CPWL_ListCtrl();
41 
SetNotify(NotifyIface * pNotify)42   void SetNotify(NotifyIface* pNotify) { m_pNotify = pNotify; }
43   void OnMouseDown(const CFX_PointF& point, bool bShift, bool bCtrl);
44   void OnMouseMove(const CFX_PointF& point, bool bShift, bool bCtrl);
45   void OnVK_UP(bool bShift, bool bCtrl);
46   void OnVK_DOWN(bool bShift, bool bCtrl);
47   void OnVK_LEFT(bool bShift, bool bCtrl);
48   void OnVK_RIGHT(bool bShift, bool bCtrl);
49   void OnVK_HOME(bool bShift, bool bCtrl);
50   void OnVK_END(bool bShift, bool bCtrl);
51   bool OnChar(uint16_t nChar, bool bShift, bool bCtrl);
52 
53   void SetScrollPos(const CFX_PointF& point);
54   void ScrollToListItem(int32_t nItemIndex);
55   CFX_FloatRect GetItemRect(int32_t nIndex) const;
GetCaret()56   int32_t GetCaret() const { return m_nCaretIndex; }
GetSelect()57   int32_t GetSelect() const { return m_nSelItem; }
58   int32_t GetTopItem() const;
59   CFX_FloatRect GetContentRect() const;
60 
61   int32_t GetItemIndex(const CFX_PointF& point) const;
62   void AddString(const WideString& str);
63   void SetTopItem(int32_t nIndex);
64   void Select(int32_t nItemIndex);
65   void Deselect(int32_t nItemIndex);
66   void SetCaret(int32_t nItemIndex);
67   WideString GetText() const;
68 
SetFontMap(IPVT_FontMap * pFontMap)69   void SetFontMap(IPVT_FontMap* pFontMap) { m_pFontMap = pFontMap; }
SetFontSize(float fFontSize)70   void SetFontSize(float fFontSize) { m_fFontSize = fFontSize; }
GetPlateRect()71   CFX_FloatRect GetPlateRect() const { return m_rcPlate; }
72   void SetPlateRect(const CFX_FloatRect& rect);
73 
GetFontSize()74   float GetFontSize() const { return m_fFontSize; }
75   CPWL_EditImpl* GetItemEdit(int32_t nIndex) const;
76   int32_t GetCount() const;
77   bool IsItemSelected(int32_t nIndex) const;
78   float GetFirstHeight() const;
SetMultipleSel(bool bMultiple)79   void SetMultipleSel(bool bMultiple) { m_bMultiple = bMultiple; }
IsMultipleSel()80   bool IsMultipleSel() const { return m_bMultiple; }
81   int32_t FindNext(int32_t nIndex, wchar_t nChar) const;
82   int32_t GetFirstSelected() const;
83 
84  private:
85   class Item {
86    public:
87     Item();
88     ~Item();
89 
90     void SetFontMap(IPVT_FontMap* pFontMap);
GetEdit()91     CPWL_EditImpl* GetEdit() const { return m_pEdit.get(); }
92 
SetRect(const CFX_FloatRect & rect)93     void SetRect(const CFX_FloatRect& rect) { m_rcListItem = rect; }
SetSelect(bool bSelected)94     void SetSelect(bool bSelected) { m_bSelected = bSelected; }
95     void SetText(const WideString& text);
96     void SetFontSize(float fFontSize);
97     WideString GetText() const;
98 
GetRect()99     CFX_FloatRect GetRect() const { return m_rcListItem; }
IsSelected()100     bool IsSelected() const { return m_bSelected; }
101     float GetItemHeight() const;
102     uint16_t GetFirstChar() const;
103 
104    private:
105     CPWL_EditImpl::Iterator* GetIterator() const;
106 
107     bool m_bSelected = false;
108     CFX_FloatRect m_rcListItem;
109     std::unique_ptr<CPWL_EditImpl> const m_pEdit;
110   };
111 
112   class SelectState {
113    public:
114     enum State { DESELECTING = -1, NORMAL = 0, SELECTING = 1 };
115     using const_iterator = std::map<int32_t, State>::const_iterator;
116 
117     SelectState();
118     ~SelectState();
119 
120     void Add(int32_t nItemIndex);
121     void Add(int32_t nBeginIndex, int32_t nEndIndex);
122     void Sub(int32_t nItemIndex);
123     void Sub(int32_t nBeginIndex, int32_t nEndIndex);
124     void DeselectAll();
125     void Done();
126 
begin()127     const_iterator begin() const { return m_Items.begin(); }
end()128     const_iterator end() const { return m_Items.end(); }
129 
130    private:
131     std::map<int32_t, State> m_Items;
132   };
133 
134   CFX_PointF InToOut(const CFX_PointF& point) const;
135   CFX_PointF OutToIn(const CFX_PointF& point) const;
136   CFX_FloatRect InToOut(const CFX_FloatRect& rect) const;
137   CFX_FloatRect OutToIn(const CFX_FloatRect& rect) const;
138 
139   CFX_PointF InnerToOuter(const CFX_PointF& point) const;
140   CFX_PointF OuterToInner(const CFX_PointF& point) const;
141   CFX_FloatRect InnerToOuter(const CFX_FloatRect& rect) const;
142   CFX_FloatRect OuterToInner(const CFX_FloatRect& rect) const;
143 
144   void OnVK(int32_t nItemIndex, bool bShift, bool bCtrl);
145   bool IsValid(int32_t nItemIndex) const;
146 
147   void ReArrange(int32_t nItemIndex);
148   CFX_FloatRect GetItemRectInternal(int32_t nIndex) const;
149   CFX_FloatRect GetContentRectInternal() const;
150   void SetMultipleSelect(int32_t nItemIndex, bool bSelected);
151   void SetSingleSelect(int32_t nItemIndex);
152   void InvalidateItem(int32_t nItemIndex);
153   void SelectItems();
154   bool IsItemVisible(int32_t nItemIndex) const;
155   void SetScrollInfo();
156   void SetScrollPosY(float fy);
157   void AddItem(const WideString& str);
158   WideString GetItemText(int32_t nIndex) const;
159   void SetItemSelect(int32_t nIndex, bool bSelected);
160   int32_t GetLastSelected() const;
GetBTPoint()161   CFX_PointF GetBTPoint() const {
162     return CFX_PointF(m_rcPlate.left, m_rcPlate.top);
163   }
164 
165   CFX_FloatRect m_rcPlate;
166   CFX_FloatRect m_rcContent;
167   CFX_PointF m_ptScrollPos;
168   float m_fFontSize = 0.0f;
169 
170   // For single:
171   int32_t m_nSelItem = -1;
172 
173   // For multiple:
174   SelectState m_SelectState;
175   int32_t m_nFootIndex = -1;
176   int32_t m_nCaretIndex = -1;
177   bool m_bCtrlSel = false;
178 
179   bool m_bMultiple = false;
180   bool m_bNotifyFlag = false;
181   UnownedPtr<NotifyIface> m_pNotify;
182   std::vector<std::unique_ptr<Item>> m_ListItems;
183   UnownedPtr<IPVT_FontMap> m_pFontMap;
184 };
185 
186 #endif  // FPDFSDK_PWL_CPWL_LIST_CTRL_H_
187