summaryrefslogtreecommitdiff
path: root/fpdfsdk/fxedit/fxet_list.h
blob: 65426e06810859cb69e01b9b473edc06af1a1c59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#ifndef FPDFSDK_FXEDIT_FXET_LIST_H_
#define FPDFSDK_FXEDIT_FXET_LIST_H_

#include <memory>
#include <vector>

#include "core/fxcrt/fx_coordinates.h"
#include "fpdfsdk/fxedit/fx_edit.h"

class CFX_Edit;
class CFX_Edit_Iterator;
class CPWL_List_Notify;

class CLST_Rect : public CFX_FloatRect {
 public:
  CLST_Rect() { left = top = right = bottom = 0.0f; }

  CLST_Rect(float other_left,
            float other_top,
            float other_right,
            float other_bottom) {
    left = other_left;
    top = other_top;
    right = other_right;
    bottom = other_bottom;
  }

  explicit CLST_Rect(const CFX_FloatRect& rect) {
    left = rect.left;
    top = rect.top;
    right = rect.right;
    bottom = rect.bottom;
  }

  ~CLST_Rect() {}

  void Default() { left = top = right = bottom = 0.0f; }

  const CLST_Rect operator=(const CFX_FloatRect& rect) {
    left = rect.left;
    top = rect.top;
    right = rect.right;
    bottom = rect.bottom;

    return *this;
  }

  bool operator==(const CLST_Rect& rect) const {
    return FXSYS_memcmp(this, &rect, sizeof(CLST_Rect)) == 0;
  }

  bool operator!=(const CLST_Rect& rect) const { return !(*this == rect); }

  float Width() const { return right - left; }

  float Height() const {
    if (top > bottom)
      return top - bottom;
    return bottom - top;
  }

  CFX_PointF LeftTop() const { return CFX_PointF(left, top); }

  CFX_PointF RightBottom() const { return CFX_PointF(right, bottom); }

  const CLST_Rect operator+=(const CFX_PointF& point) {
    left += point.x;
    right += point.x;
    top += point.y;
    bottom += point.y;

    return *this;
  }

  const CLST_Rect operator-=(const CFX_PointF& point) {
    left -= point.x;
    right -= point.x;
    top -= point.y;
    bottom -= point.y;

    return *this;
  }

  CLST_Rect operator+(const CFX_PointF& point) const {
    return CLST_Rect(left + point.x, top + point.y, right + point.x,
                     bottom + point.y);
  }

  CLST_Rect operator-(const CFX_PointF& point) const {
    return CLST_Rect(left - point.x, top - point.y, right - point.x,
                     bottom - point.y);
  }
};

class CFX_ListItem final {
 public:
  CFX_ListItem();
  ~CFX_ListItem();

  void SetFontMap(IPVT_FontMap* pFontMap);
  CFX_Edit* GetEdit() const;

  void SetRect(const CLST_Rect& rect);
  void SetSelect(bool bSelected);
  void SetText(const CFX_WideString& text);
  void SetFontSize(float fFontSize);
  CFX_WideString GetText() const;

  CLST_Rect GetRect() const;
  bool IsSelected() const;
  float GetItemHeight() const;
  uint16_t GetFirstChar() const;

 private:
  CFX_Edit_Iterator* GetIterator() const;

  std::unique_ptr<CFX_Edit> m_pEdit;
  bool m_bSelected;
  CLST_Rect m_rcListItem;
};

class CFX_ListContainer {
 public:
  CFX_ListContainer();
  virtual ~CFX_ListContainer();

  virtual void SetPlateRect(const CFX_FloatRect& rect);

  CFX_FloatRect GetPlateRect() const { return m_rcPlate; }
  void SetContentRect(const CLST_Rect& rect) { m_rcContent = rect; }
  CLST_Rect GetContentRect() const { return m_rcContent; }
  CFX_PointF GetBTPoint() const {
    return CFX_PointF(m_rcPlate.left, m_rcPlate.top);
  }
  CFX_PointF GetETPoint() const {
    return CFX_PointF(m_rcPlate.right, m_rcPlate.bottom);
  }

 public:
  CFX_PointF InnerToOuter(const CFX_PointF& point) const {
    return CFX_PointF(point.x + GetBTPoint().x, GetBTPoint().y - point.y);
  }
  CFX_PointF OuterToInner(const CFX_PointF& point) const {
    return CFX_PointF(point.x - GetBTPoint().x, GetBTPoint().y - point.y);
  }
  CFX_FloatRect InnerToOuter(const CLST_Rect& rect) const {
    CFX_PointF ptLeftTop = InnerToOuter(CFX_PointF(rect.left, rect.top));
    CFX_PointF ptRightBottom =
        InnerToOuter(CFX_PointF(rect.right, rect.bottom));
    return CFX_FloatRect(ptLeftTop.x, ptRightBottom.y, ptRightBottom.x,
                         ptLeftTop.y);
  }
  CLST_Rect OuterToInner(const CFX_FloatRect& rect) const {
    CFX_PointF ptLeftTop = OuterToInner(CFX_PointF(rect.left, rect.top));
    CFX_PointF ptRightBottom =
        OuterToInner(CFX_PointF(rect.right, rect.bottom));
    return CLST_Rect(ptLeftTop.x, ptLeftTop.y, ptRightBottom.x,
                     ptRightBottom.y);
  }

 private:
  CFX_FloatRect m_rcPlate;
  CLST_Rect m_rcContent;  // positive forever!
};

struct CPLST_Select_Item {
  CPLST_Select_Item(int32_t other_nItemIndex, int32_t other_nState) {
    nItemIndex = other_nItemIndex;
    nState = other_nState;
  }

  int32_t nItemIndex;
  int32_t nState;  // 0:normal select -1:to deselect 1: to select
};

class CPLST_Select {
 public:
  CPLST_Select();
  virtual ~CPLST_Select();

 public:
  void Add(int32_t nItemIndex);
  void Add(int32_t nBeginIndex, int32_t nEndIndex);
  void Sub(int32_t nItemIndex);
  void Sub(int32_t nBeginIndex, int32_t nEndIndex);
  bool IsExist(int32_t nItemIndex) const;
  int32_t Find(int32_t nItemIndex) const;
  int32_t GetCount() const;
  int32_t GetItemIndex(int32_t nIndex) const;
  int32_t GetState(int32_t nIndex) const;
  void Done();
  void DeselectAll();

 private:
  CFX_ArrayTemplate<CPLST_Select_Item*> m_aItems;
};

class CFX_ListCtrl : protected CFX_ListContainer {
 public:
  CFX_ListCtrl();
  ~CFX_ListCtrl() override;

  // CFX_ListContainer
  void SetPlateRect(const CFX_FloatRect& rect) override;

  void SetNotify(CPWL_List_Notify* pNotify);
  void OnMouseDown(const CFX_PointF& point, bool bShift, bool bCtrl);
  void OnMouseMove(const CFX_PointF& point, bool bShift, bool bCtrl);
  void OnVK_UP(bool bShift, bool bCtrl);
  void OnVK_DOWN(bool bShift, bool bCtrl);
  void OnVK_LEFT(bool bShift, bool bCtrl);
  void OnVK_RIGHT(bool bShift, bool bCtrl);
  void OnVK_HOME(bool bShift, bool bCtrl);
  void OnVK_END(bool bShift, bool bCtrl);
  void OnVK(int32_t nItemIndex, bool bShift, bool bCtrl);
  bool OnChar(uint16_t nChar, bool bShift, bool bCtrl);

  void SetScrollPos(const CFX_PointF& point);
  void ScrollToListItem(int32_t nItemIndex);
  CFX_FloatRect GetItemRect(int32_t nIndex) const;
  int32_t GetCaret() const;
  int32_t GetSelect() const;
  int32_t GetTopItem() const;
  CFX_FloatRect GetContentRect() const;
  int32_t GetItemIndex(const CFX_PointF& point) const;
  void AddString(const CFX_WideString& str);
  void SetTopItem(int32_t nIndex);
  void Select(int32_t nItemIndex);
  void SetCaret(int32_t nItemIndex);
  void Empty();
  void Cancel();
  CFX_WideString GetText() const;

  void SetFontMap(IPVT_FontMap* pFontMap);
  void SetFontSize(float fFontSize);
  CFX_FloatRect GetPlateRect() const;
  float GetFontSize() const;
  CFX_Edit* GetItemEdit(int32_t nIndex) const;
  int32_t GetCount() const;
  bool IsItemSelected(int32_t nIndex) const;
  float GetFirstHeight() const;
  void SetMultipleSel(bool bMultiple);
  bool IsMultipleSel() const;
  bool IsValid(int32_t nItemIndex) const;
  int32_t FindNext(int32_t nIndex, wchar_t nChar) const;
  int32_t GetFirstSelected() const;

  CFX_PointF InToOut(const CFX_PointF& point) const;
  CFX_PointF OutToIn(const CFX_PointF& point) const;
  CFX_FloatRect InToOut(const CFX_FloatRect& rect) const;
  CFX_FloatRect OutToIn(const CFX_FloatRect& rect) const;

 private:
  void ReArrange(int32_t nItemIndex);
  CFX_FloatRect GetItemRectInternal(int32_t nIndex) const;
  CFX_FloatRect GetContentRectInternal() const;
  void SetMultipleSelect(int32_t nItemIndex, bool bSelected);
  void SetSingleSelect(int32_t nItemIndex);
  void InvalidateItem(int32_t nItemIndex);
  void SelectItems();
  bool IsItemVisible(int32_t nItemIndex) const;
  void SetScrollInfo();
  void SetScrollPosY(float fy);
  void AddItem(const CFX_WideString& str);
  CFX_WideString GetItemText(int32_t nIndex) const;
  void SetItemSelect(int32_t nItemIndex, bool bSelected);
  int32_t GetLastSelected() const;
  wchar_t Toupper(wchar_t c) const;

  CPWL_List_Notify* m_pNotify;
  bool m_bNotifyFlag;
  CFX_PointF m_ptScrollPos;
  CPLST_Select m_aSelItems;  // for multiple
  int32_t m_nSelItem;        // for single
  int32_t m_nFootIndex;      // for multiple
  bool m_bCtrlSel;           // for multiple
  int32_t m_nCaretIndex;     // for multiple
  std::vector<std::unique_ptr<CFX_ListItem>> m_ListItems;
  float m_fFontSize;
  IPVT_FontMap* m_pFontMap;
  bool m_bMultiple;
};

#endif  // FPDFSDK_FXEDIT_FXET_LIST_H_