Cleanup: Get this rid of "this->" in fpdfsdk/
[pdfium.git] / fpdfsdk / include / fxedit / fxet_list.h
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_FXEDIT_FXET_LIST_H_
8 #define FPDFSDK_INCLUDE_FXEDIT_FXET_LIST_H_
9
10 #include "../../../core/include/fpdfapi/fpdf_parser.h"  // For CPDF_Point.
11 #include "fx_edit.h"
12
13 class IFX_Edit;
14
15 class CLST_Size
16 {
17 public:
18         CLST_Size() : x(0.0f), y(0.0f)
19         {
20         }
21
22         CLST_Size(FX_FLOAT other_x, FX_FLOAT other_y)
23         {
24                 x = other_x;
25                 y = other_y;
26         }
27
28         void Default()
29         {
30                 x = 0.0f;
31                 y = 0.0f;
32         }
33
34         FX_BOOL operator != (const CLST_Size & size) const
35         {
36                 return FXSYS_memcmp(this, &size, sizeof(CLST_Size)) != 0;
37         }
38
39         FX_FLOAT x,y;
40 };
41
42 class CLST_Rect : public CPDF_Rect
43 {
44 public:
45         CLST_Rect()
46         {
47                 left = top = right = bottom = 0.0f;
48         }
49
50     CLST_Rect(FX_FLOAT other_left,
51               FX_FLOAT other_top,
52               FX_FLOAT other_right,
53               FX_FLOAT other_bottom)
54     {
55         left = other_left;
56         top = other_top;
57         right = other_right;
58         bottom = other_bottom;
59     }
60
61     CLST_Rect(const CPDF_Rect & rect)
62     {
63         left = rect.left;
64         top = rect.top;
65         right = rect.right;
66         bottom = rect.bottom;
67     }
68
69         void Default()
70         {
71                 left = top = right = bottom = 0.0f;
72         }
73
74         const CLST_Rect operator = (const CPDF_Rect & rect)
75         {
76                 left = rect.left;
77                 top = rect.top;
78                 right = rect.right;
79                 bottom = rect.bottom;
80
81                 return *this;
82         }
83
84         FX_BOOL operator == (const CLST_Rect & rect) const
85         {
86                 return FXSYS_memcmp(this, &rect, sizeof(CLST_Rect)) == 0;
87         }
88
89         FX_BOOL operator != (const CLST_Rect & rect) const
90         {
91                 return FXSYS_memcmp(this, &rect, sizeof(CLST_Rect)) != 0;
92         }
93
94         FX_FLOAT Width() const
95         {
96                 return right - left;
97         }
98
99         FX_FLOAT Height() const
100         {
101                 if (top > bottom)
102                         return top - bottom;
103                 return bottom - top;
104         }
105
106         CPDF_Point LeftTop() const
107         {
108                 return CPDF_Point(left,top);
109         }
110
111         CPDF_Point RightBottom() const
112         {
113                 return CPDF_Point(right,bottom);
114         }
115
116         const CLST_Rect operator += (const CPDF_Point & point)
117         {
118                 left += point.x;
119                 right += point.x;
120                 top += point.y;
121                 bottom += point.y;
122
123                 return *this;
124         }
125
126         const CLST_Rect operator -= (const CPDF_Point & point)
127         {
128                 left -= point.x;
129                 right -= point.x;
130                 top -= point.y;
131                 bottom -= point.y;
132
133                 return *this;
134         }
135
136         CLST_Rect operator + (const CPDF_Point & point) const
137         {
138                 return CLST_Rect(left + point.x,
139                                         top + point.y,
140                                         right + point.x,
141                                         bottom + point.y);
142         }
143
144         CLST_Rect operator - (const CPDF_Point & point) const
145         {
146                 return CLST_Rect(left - point.x,
147                                         top - point.y,
148                                         right - point.x,
149                                         bottom - point.y);
150         }
151 };
152
153 class CFX_ListItem
154 {
155 public:
156         CFX_ListItem();
157         virtual ~CFX_ListItem();
158
159         void                                                    SetFontMap(IFX_Edit_FontMap * pFontMap);
160         IFX_Edit_Iterator*                              GetIterator() const;
161         IFX_Edit*                                               GetEdit() const;
162
163 public:
164         void                                                    SetRect(const CLST_Rect & rect);
165         void                                                    SetSelect(FX_BOOL bSelected);
166         void                                                    SetCaret(FX_BOOL bCaret);
167         void                                                    SetText(const FX_WCHAR* text);
168         void                                                    SetFontSize(FX_FLOAT fFontSize);
169         CFX_WideString                                  GetText() const;
170
171         CLST_Rect                                               GetRect() const;
172         FX_BOOL                                                 IsSelected() const;
173         FX_BOOL                                                 IsCaret() const;
174         FX_FLOAT                                                GetItemHeight() const;
175         FX_WORD                                                 GetFirstChar() const;
176
177 private:
178         IFX_Edit*                                               m_pEdit;
179         FX_BOOL                                                 m_bSelected;
180         FX_BOOL                                                 m_bCaret;
181         CLST_Rect                                               m_rcListItem;
182 };
183
184 class CFX_ListContainer
185 {
186 public:
187         CFX_ListContainer() : m_rcPlate(0.0f,0.0f,0.0f,0.0f), m_rcContent(0.0f,0.0f,0.0f,0.0f){}
188         virtual ~CFX_ListContainer(){}
189         virtual void                                    SetPlateRect(const CPDF_Rect & rect){m_rcPlate = rect;}
190         CPDF_Rect                                               GetPlateRect() const{return m_rcPlate;}
191         void                                                    SetContentRect(const CLST_Rect & rect){m_rcContent = rect;}
192         CLST_Rect                                               GetContentRect() const{return m_rcContent;}
193         CPDF_Point                                              GetBTPoint() const{return CPDF_Point(m_rcPlate.left,m_rcPlate.top);}
194         CPDF_Point                                              GetETPoint() const{return CPDF_Point(m_rcPlate.right,m_rcPlate.bottom);}
195 public:
196         CPDF_Point                                              InnerToOuter(const CPDF_Point & point) const{return CPDF_Point(point.x + GetBTPoint().x,GetBTPoint().y - point.y);}
197         CPDF_Point                                              OuterToInner(const CPDF_Point & point) const{return CPDF_Point(point.x - GetBTPoint().x,GetBTPoint().y - point.y);}
198         CPDF_Rect                                               InnerToOuter(const CLST_Rect & rect) const{CPDF_Point ptLeftTop = InnerToOuter(CPDF_Point(rect.left,rect.top));
199                                                                                                                                                         CPDF_Point ptRightBottom = InnerToOuter(CPDF_Point(rect.right,rect.bottom));
200                                                                                                                                                         return CPDF_Rect(ptLeftTop.x,ptRightBottom.y,ptRightBottom.x,ptLeftTop.y);}
201         CLST_Rect                                               OuterToInner(const CPDF_Rect & rect) const{CPDF_Point ptLeftTop = OuterToInner(CPDF_Point(rect.left,rect.top));
202                                                                                                                                                         CPDF_Point ptRightBottom = OuterToInner(CPDF_Point(rect.right,rect.bottom));
203                                                                                                                                                         return CLST_Rect(ptLeftTop.x,ptLeftTop.y,ptRightBottom.x,ptRightBottom.y);}
204 private:
205         CPDF_Rect                                               m_rcPlate;
206         CLST_Rect                                               m_rcContent;            //positive forever!
207 };
208
209 template<class TYPE> class CLST_ArrayTemplate : public CFX_ArrayTemplate<TYPE>
210 {
211 public:
212         FX_BOOL IsEmpty() { return CFX_ArrayTemplate<TYPE>::GetSize() <= 0; }
213         TYPE GetAt(int32_t nIndex) const { if (nIndex >= 0 && nIndex < CFX_ArrayTemplate<TYPE>::GetSize()) return CFX_ArrayTemplate<TYPE>::GetAt(nIndex); return NULL;}
214         void RemoveAt(int32_t nIndex){if (nIndex >= 0 && nIndex < CFX_ArrayTemplate<TYPE>::GetSize()) CFX_ArrayTemplate<TYPE>::RemoveAt(nIndex);}
215 };
216
217 class CFX_List : protected CFX_ListContainer , public IFX_List
218 {
219 public:
220         CFX_List();
221         virtual ~CFX_List();
222
223 public:
224         virtual void                                    SetFontMap(IFX_Edit_FontMap * pFontMap);
225         virtual void                                    SetFontSize(FX_FLOAT fFontSize);
226
227         virtual CPDF_Rect                               GetPlateRect() const;
228         virtual CPDF_Rect                               GetContentRect() const;
229
230         virtual FX_FLOAT                                GetFontSize() const;
231         virtual IFX_Edit*                               GetItemEdit(int32_t nIndex) const;
232         virtual int32_t                         GetCount() const;
233         virtual FX_BOOL                                 IsItemSelected(int32_t nIndex) const;
234         virtual FX_FLOAT                                GetFirstHeight() const;
235
236         virtual void                                    SetMultipleSel(FX_BOOL bMultiple);
237         virtual FX_BOOL                                 IsMultipleSel() const;
238         virtual FX_BOOL                                 IsValid(int32_t nItemIndex) const;
239         virtual int32_t                         FindNext(int32_t nIndex,FX_WCHAR nChar) const;
240
241 protected:
242         virtual void                                    Empty();
243
244         void                                                    AddItem(const FX_WCHAR* str);
245         virtual void                                    ReArrange(int32_t nItemIndex);
246
247         virtual CPDF_Rect                               GetItemRect(int32_t nIndex) const;
248         CFX_WideString                                  GetItemText(int32_t nIndex) const;
249
250         void                                                    SetItemSelect(int32_t nItemIndex, FX_BOOL bSelected);
251         void                                                    SetItemCaret(int32_t nItemIndex, FX_BOOL bCaret);
252
253         virtual int32_t                         GetItemIndex(const CPDF_Point & point) const;
254         int32_t                                         GetFirstSelected() const;
255         int32_t                                         GetLastSelected() const;
256         FX_WCHAR                                                Toupper(FX_WCHAR c) const;
257
258 private:
259         CLST_ArrayTemplate<CFX_ListItem*>       m_aListItems;
260         FX_FLOAT                                                        m_fFontSize;
261         IFX_Edit_FontMap*                                       m_pFontMap;
262         FX_BOOL                                                         m_bMultiple;
263 };
264
265 struct CPLST_Select_Item
266 {
267         CPLST_Select_Item(int32_t other_nItemIndex, int32_t other_nState)
268         {
269                 nItemIndex = other_nItemIndex;
270                 nState = other_nState;
271         }
272
273         int32_t         nItemIndex;
274         int32_t         nState; //0:normal select -1:to deselect 1: to select
275 };
276
277 class CPLST_Select
278 {
279 public:
280         CPLST_Select();
281         virtual ~CPLST_Select();
282
283 public:
284         void                                                    Add(int32_t nItemIndex);
285         void                                                    Add(int32_t nBeginIndex, int32_t nEndIndex);
286         void                                                    Sub(int32_t nItemIndex);
287         void                                                    Sub(int32_t nBeginIndex, int32_t nEndIndex);
288         FX_BOOL                                                 IsExist(int32_t nItemIndex) const;
289         int32_t                                         Find(int32_t nItemIndex) const;
290         int32_t                                         GetCount() const;
291         int32_t                                         GetItemIndex(int32_t nIndex) const;
292         int32_t                                         GetState(int32_t nIndex) const;
293         void                                                    Done();
294         void                                                    DeselectAll();
295
296 private:
297         CFX_ArrayTemplate<CPLST_Select_Item*>   m_aItems;
298 };
299
300 class CFX_ListCtrl : public CFX_List
301 {
302 public:
303         CFX_ListCtrl();
304         virtual ~CFX_ListCtrl();
305
306 public:
307         void                                                    SetNotify(IFX_List_Notify * pNotify);
308
309         void                                                    OnMouseDown(const CPDF_Point & point,FX_BOOL bShift,FX_BOOL bCtrl);
310         void                                                    OnMouseMove(const CPDF_Point & point,FX_BOOL bShift,FX_BOOL bCtrl);
311         void                                                    OnVK_UP(FX_BOOL bShift,FX_BOOL bCtrl);
312         void                                                    OnVK_DOWN(FX_BOOL bShift,FX_BOOL bCtrl);
313         void                                                    OnVK_LEFT(FX_BOOL bShift,FX_BOOL bCtrl);
314         void                                                    OnVK_RIGHT(FX_BOOL bShift,FX_BOOL bCtrl);
315         void                                                    OnVK_HOME(FX_BOOL bShift,FX_BOOL bCtrl);
316         void                                                    OnVK_END(FX_BOOL bShift,FX_BOOL bCtrl);
317         void                                                    OnVK(int32_t nItemIndex,FX_BOOL bShift,FX_BOOL bCtrl);
318         FX_BOOL                                                 OnChar(FX_WORD nChar,FX_BOOL bShift,FX_BOOL bCtrl);
319
320         virtual CPDF_Point                              InToOut(const CPDF_Point & point) const;
321         virtual CPDF_Point                              OutToIn(const CPDF_Point & point) const;
322         virtual CPDF_Rect                               InToOut(const CPDF_Rect & rect) const;
323         virtual CPDF_Rect                               OutToIn(const CPDF_Rect & rect) const;
324
325         virtual void                                    SetPlateRect(const CPDF_Rect & rect);
326         void                                                    SetScrollPos(const CPDF_Point & point);
327         void                                                    ScrollToListItem(int32_t nItemIndex);
328         virtual CPDF_Rect                               GetItemRect(int32_t nIndex) const;
329         int32_t                                         GetCaret() const {return m_nCaretIndex;}
330         int32_t                                         GetSelect() const {return m_nSelItem;}
331         int32_t                                         GetTopItem() const;
332         virtual CPDF_Rect                               GetContentRect() const;
333         virtual int32_t                         GetItemIndex(const CPDF_Point & point) const;
334
335         void                                                    AddString(const FX_WCHAR* string);
336         void                                                    SetTopItem(int32_t nIndex);
337         void                                                    Select(int32_t nItemIndex);
338         virtual void                                    SetCaret(int32_t nItemIndex);
339         virtual void                                    Empty();
340         virtual void                                    Cancel();
341         CFX_WideString                                  GetText() const;
342
343 private:
344         void                                                    SetMultipleSelect(int32_t nItemIndex, FX_BOOL bSelected);
345         void                                                    SetSingleSelect(int32_t nItemIndex);
346         void                                                    InvalidateItem(int32_t nItemIndex);
347         void                                                    SelectItems();
348         FX_BOOL                                                 IsItemVisible(int32_t nItemIndex) const;
349         void                                                    SetScrollInfo();
350         void                                                    SetScrollPosY(FX_FLOAT fy);
351         virtual void                                    ReArrange(int32_t nItemIndex);
352
353 private:
354         IFX_List_Notify*                                m_pNotify;
355         FX_BOOL                                                 m_bNotifyFlag;
356         CPDF_Point                                              m_ptScrollPos;
357         CPLST_Select                                    m_aSelItems;    //for multiple
358         int32_t                                         m_nSelItem;             //for single
359         int32_t                                         m_nFootIndex;   //for multiple
360         FX_BOOL                                                 m_bCtrlSel;             //for multiple
361         int32_t                                         m_nCaretIndex;  //for multiple
362 };
363
364 #endif  // FPDFSDK_INCLUDE_FXEDIT_FXET_LIST_H_