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 XFA_FWL_CFWL_LISTBOX_H_ 8 #define XFA_FWL_CFWL_LISTBOX_H_ 9 10 #include <memory> 11 #include <vector> 12 13 #include "xfa/fwl/cfwl_edit.h" 14 #include "xfa/fwl/cfwl_event.h" 15 #include "xfa/fwl/cfwl_listbox.h" 16 #include "xfa/fwl/cfwl_widget.h" 17 #include "xfa/fwl/fwl_widgetdef.h" 18 19 #define FWL_STYLEEXT_LTB_MultiSelection (1L << 0) 20 #define FWL_STYLEEXT_LTB_LeftAlign (0L << 4) 21 #define FWL_STYLEEXT_LTB_CenterAlign (1L << 4) 22 #define FWL_STYLEEXT_LTB_RightAlign (2L << 4) 23 #define FWL_STYLEEXT_LTB_AlignMask (3L << 4) 24 #define FWL_STYLEEXT_LTB_ShowScrollBarFocus (1L << 10) 25 26 class CFWL_MessageMouse; 27 class CFWL_MessageMouseWheel; 28 29 class CFWL_ListBox : public CFWL_Widget { 30 public: 31 class Item { 32 public: 33 explicit Item(const WideString& text); 34 ~Item(); 35 IsSelected()36 bool IsSelected() const { return m_bIsSelected; } SetSelected(bool enable)37 void SetSelected(bool enable) { m_bIsSelected = enable; } IsFocused()38 bool IsFocused() const { return m_bIsFocused; } SetFocused(bool enable)39 void SetFocused(bool enable) { m_bIsFocused = enable; } GetRect()40 CFX_RectF GetRect() const { return m_ItemRect; } SetRect(const CFX_RectF & rect)41 void SetRect(const CFX_RectF& rect) { m_ItemRect = rect; } GetText()42 WideString GetText() const { return m_wsText; } 43 44 private: 45 bool m_bIsSelected = false; 46 bool m_bIsFocused = false; 47 CFX_RectF m_ItemRect; 48 const WideString m_wsText; 49 }; 50 51 CONSTRUCT_VIA_MAKE_GARBAGE_COLLECTED; 52 ~CFWL_ListBox() override; 53 54 // CFWL_Widget: 55 void Trace(cppgc::Visitor* visitor) const override; 56 FWL_Type GetClassID() const override; 57 void Update() override; 58 FWL_WidgetHit HitTest(const CFX_PointF& point) override; 59 void DrawWidget(CFGAS_GEGraphics* pGraphics, 60 const CFX_Matrix& matrix) override; 61 void OnProcessMessage(CFWL_Message* pMessage) override; 62 void OnProcessEvent(CFWL_Event* pEvent) override; 63 void OnDrawWidget(CFGAS_GEGraphics* pGraphics, 64 const CFX_Matrix& matrix) override; 65 66 int32_t CountItems(const CFWL_Widget* pWidget) const; 67 Item* GetItem(const CFWL_Widget* pWidget, int32_t nIndex) const; 68 int32_t GetItemIndex(CFWL_Widget* pWidget, Item* pItem); 69 Item* AddString(const WideString& wsAdd); 70 void RemoveAt(int32_t iIndex); 71 void DeleteString(Item* pItem); 72 void DeleteAll(); 73 int32_t CountSelItems(); 74 Item* GetSelItem(int32_t nIndexSel); 75 int32_t GetSelIndex(int32_t nIndex); 76 void SetSelItem(Item* hItem, bool bSelect); 77 float CalcItemHeight(); 78 79 protected: 80 CFWL_ListBox(CFWL_App* pApp, 81 const Properties& properties, 82 CFWL_Widget* pOuter); 83 84 Item* GetListItem(Item* hItem, XFA_FWL_VKEYCODE dwKeyCode); 85 void SetSelection(Item* hStart, Item* hEnd, bool bSelected); 86 Item* GetItemAtPoint(const CFX_PointF& point); 87 bool ScrollToVisible(Item* hItem); 88 void InitVerticalScrollBar(); 89 void InitHorizontalScrollBar(); 90 bool IsShowVertScrollBar() const; 91 bool IsShowHorzScrollBar() const; 92 bool ScrollBarPropertiesPresent() const; GetVertScrollBar()93 CFWL_ScrollBar* GetVertScrollBar() const { return m_pVertScrollBar; } GetRTClient()94 const CFX_RectF& GetRTClient() const { return m_ClientRect; } 95 96 private: 97 bool IsMultiSelection() const; 98 void ClearSelection(); 99 void SelectAll(); 100 Item* GetFocusedItem(); 101 void SetFocusItem(Item* hItem); 102 void DrawBkground(CFGAS_GEGraphics* pGraphics, const CFX_Matrix& mtMatrix); 103 void DrawItems(CFGAS_GEGraphics* pGraphics, const CFX_Matrix& mtMatrix); 104 void DrawItem(CFGAS_GEGraphics* pGraphics, 105 Item* hItem, 106 int32_t Index, 107 const CFX_RectF& rtItem, 108 const CFX_Matrix& pMatrix); 109 void DrawStatic(CFGAS_GEGraphics* pGraphics); 110 CFX_SizeF CalcSize(); 111 void UpdateItemSize(Item* hItem, 112 CFX_SizeF& size, 113 float fWidth, 114 float fHeight) const; 115 float GetMaxTextWidth(); 116 float GetScrollWidth(); 117 118 void OnFocusGained(); 119 void OnFocusLost(); 120 void OnLButtonDown(CFWL_MessageMouse* pMsg); 121 void OnLButtonUp(CFWL_MessageMouse* pMsg); 122 void OnMouseWheel(CFWL_MessageMouseWheel* pMsg); 123 void OnKeyDown(CFWL_MessageKey* pMsg); 124 void OnVK(Item* hItem, bool bShift, bool bCtrl); 125 bool OnScroll(CFWL_ScrollBar* pScrollBar, 126 CFWL_EventScroll::Code dwCode, 127 float fPos); 128 129 CFX_RectF m_ClientRect; 130 CFX_RectF m_StaticRect; 131 CFX_RectF m_ContentRect; 132 cppgc::Member<CFWL_ScrollBar> m_pHorzScrollBar; 133 cppgc::Member<CFWL_ScrollBar> m_pVertScrollBar; 134 FDE_TextStyle m_TTOStyles; 135 FDE_TextAlignment m_iTTOAligns = FDE_TextAlignment::kTopLeft; 136 bool m_bLButtonDown = false; 137 float m_fItemHeight = 0.0f; 138 float m_fScorllBarWidth = 0.0f; 139 Item* m_hAnchor = nullptr; 140 std::vector<std::unique_ptr<Item>> m_ItemArray; 141 }; 142 143 #endif // XFA_FWL_CFWL_LISTBOX_H_ 144