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