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