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