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 XFA_FWL_CFWL_COMBOBOX_H_
8 #define XFA_FWL_CFWL_COMBOBOX_H_
9 
10 #include <memory>
11 
12 #include "xfa/fwl/cfwl_comboboxproxy.h"
13 #include "xfa/fwl/cfwl_comboedit.h"
14 #include "xfa/fwl/cfwl_combolist.h"
15 #include "xfa/fwl/cfwl_form.h"
16 #include "xfa/fwl/cfwl_listbox.h"
17 #include "xfa/fxgraphics/cxfa_graphics.h"
18 
19 class CFWL_WidgetProperties;
20 class CFWL_ComboBox;
21 class CFWL_ComboBoxProxy;
22 class CFWL_FormProxy;
23 class CFWL_ListBox;
24 class CFWL_Widget;
25 
26 #define FWL_STYLEEXT_CMB_DropDown (1L << 0)
27 #define FWL_STYLEEXT_CMB_Sort (1L << 1)
28 #define FWL_STYLEEXT_CMB_OwnerDraw (1L << 3)
29 #define FWL_STYLEEXT_CMB_EditHNear 0
30 #define FWL_STYLEEXT_CMB_EditHCenter (1L << 4)
31 #define FWL_STYLEEXT_CMB_EditVNear 0
32 #define FWL_STYLEEXT_CMB_EditVCenter (1L << 6)
33 #define FWL_STYLEEXT_CMB_EditVFar (2L << 6)
34 #define FWL_STYLEEXT_CMB_EditJustified (1L << 8)
35 #define FWL_STYLEEXT_CMB_EditHAlignMask (3L << 4)
36 #define FWL_STYLEEXT_CMB_EditVAlignMask (3L << 6)
37 #define FWL_STYLEEXT_CMB_ListItemLeftAlign 0
38 #define FWL_STYLEEXT_CMB_ListItemCenterAlign (1L << 10)
39 #define FWL_STYLEEXT_CMB_ListItemAlignMask (3L << 10)
40 #define FWL_STYLEEXT_CMB_ReadOnly (1L << 13)
41 
42 class CFWL_ComboBox : public CFWL_Widget {
43  public:
44   explicit CFWL_ComboBox(const CFWL_App* pApp);
45   ~CFWL_ComboBox() override;
46 
47   // CFWL_Widget
48   FWL_Type GetClassID() const override;
49   void ModifyStylesEx(uint32_t dwStylesExAdded,
50                       uint32_t dwStylesExRemoved) override;
51   void SetStates(uint32_t dwStates) override;
52   void RemoveStates(uint32_t dwStates) override;
53   void Update() override;
54   FWL_WidgetHit HitTest(const CFX_PointF& point) override;
55   void DrawWidget(CXFA_Graphics* pGraphics, const CFX_Matrix& matrix) override;
56   void SetThemeProvider(IFWL_ThemeProvider* pThemeProvider) override;
57   void OnProcessMessage(CFWL_Message* pMessage) override;
58   void OnProcessEvent(CFWL_Event* pEvent) override;
59   void OnDrawWidget(CXFA_Graphics* pGraphics,
60                     const CFX_Matrix& matrix) override;
61 
62   WideString GetTextByIndex(int32_t iIndex) const;
GetCurSel()63   int32_t GetCurSel() const { return m_iCurSel; }
64   void SetCurSel(int32_t iSel);
65 
66   void AddString(const WideStringView& wsText);
67   void RemoveAt(int32_t iIndex);
68   void RemoveAll();
69 
70   void SetEditText(const WideString& wsText);
71   WideString GetEditText() const;
72 
73   void OpenDropDownList(bool bActivate);
74 
EditCanUndo()75   bool EditCanUndo() const { return m_pEdit->CanUndo(); }
EditCanRedo()76   bool EditCanRedo() const { return m_pEdit->CanRedo(); }
EditUndo()77   bool EditUndo() { return m_pEdit->Undo(); }
EditRedo()78   bool EditRedo() { return m_pEdit->Redo(); }
EditCanCopy()79   bool EditCanCopy() const { return m_pEdit->HasSelection(); }
EditCanCut()80   bool EditCanCut() const {
81     if (m_pEdit->GetStylesEx() & FWL_STYLEEXT_EDT_ReadOnly)
82       return false;
83     return EditCanCopy();
84   }
EditCanSelectAll()85   bool EditCanSelectAll() const { return m_pEdit->GetTextLength() > 0; }
EditCopy()86   Optional<WideString> EditCopy() const { return m_pEdit->Copy(); }
EditCut()87   Optional<WideString> EditCut() { return m_pEdit->Cut(); }
EditPaste(const WideString & wsPaste)88   bool EditPaste(const WideString& wsPaste) { return m_pEdit->Paste(wsPaste); }
EditSelectAll()89   void EditSelectAll() { m_pEdit->SelectAll(); }
EditDelete()90   void EditDelete() { m_pEdit->ClearText(); }
EditDeSelect()91   void EditDeSelect() { m_pEdit->ClearSelection(); }
92 
93   CFX_RectF GetBBox() const;
94   void EditModifyStylesEx(uint32_t dwStylesExAdded, uint32_t dwStylesExRemoved);
95 
96   void DrawStretchHandler(CXFA_Graphics* pGraphics, const CFX_Matrix* pMatrix);
IsDropListVisible()97   bool IsDropListVisible() const {
98     return m_pComboBoxProxy &&
99            !(m_pComboBoxProxy->GetStates() & FWL_WGTSTATE_Invisible);
100   }
101   void ShowDropList(bool bActivate);
102 
GetComboEdit()103   CFWL_ComboEdit* GetComboEdit() const { return m_pEdit.get(); }
104 
105   void ProcessSelChanged(bool bLButtonUp);
GetCurrentSelection()106   int32_t GetCurrentSelection() const { return m_iCurSel; }
107 
108  private:
IsDropDownStyle()109   bool IsDropDownStyle() const {
110     return !!(m_pProperties->m_dwStyleExes & FWL_STYLEEXT_CMB_DropDown);
111   }
112   void MatchEditText();
113   void SyncEditText(int32_t iListItem);
114   void Layout();
115   void ResetTheme();
116   void ResetEditAlignment();
117   void ResetListItemAlignment();
118   void InitProxyForm();
119   void OnFocusChanged(CFWL_Message* pMsg, bool bSet);
120   void OnLButtonDown(CFWL_MessageMouse* pMsg);
121   void OnLButtonUp(CFWL_MessageMouse* pMsg);
122   void OnMouseMove(CFWL_MessageMouse* pMsg);
123   void OnMouseLeave(CFWL_MessageMouse* pMsg);
124   void OnKey(CFWL_MessageKey* pMsg);
125   void DoSubCtrlKey(CFWL_MessageKey* pMsg);
126 
127   void DisForm_InitComboList();
128   void DisForm_InitComboEdit();
129   void DisForm_ShowDropList(bool bActivate);
DisForm_IsDropListVisible()130   bool DisForm_IsDropListVisible() const {
131     return !(m_pListBox->GetStates() & FWL_WGTSTATE_Invisible);
132   }
133   void DisForm_ModifyStylesEx(uint32_t dwStylesExAdded,
134                               uint32_t dwStylesExRemoved);
135   void DisForm_Update();
136   FWL_WidgetHit DisForm_HitTest(const CFX_PointF& point);
137   void DisForm_DrawWidget(CXFA_Graphics* pGraphics, const CFX_Matrix* pMatrix);
138   CFX_RectF DisForm_GetBBox() const;
139   void DisForm_Layout();
140   void DisForm_OnProcessMessage(CFWL_Message* pMessage);
141   void DisForm_OnLButtonDown(CFWL_MessageMouse* pMsg);
142   void DisForm_OnFocusChanged(CFWL_Message* pMsg, bool bSet);
143   void DisForm_OnKey(CFWL_MessageKey* pMsg);
144 
145   CFX_RectF m_rtClient;
146   CFX_RectF m_rtContent;
147   CFX_RectF m_rtBtn;
148   CFX_RectF m_rtList;
149   CFX_RectF m_rtProxy;
150   CFX_RectF m_rtHandler;
151   std::unique_ptr<CFWL_ComboEdit> m_pEdit;
152   std::unique_ptr<CFWL_ComboList> m_pListBox;
153   CFWL_ComboBoxProxy* m_pComboBoxProxy;  // Can this be a unique_ptr?
154   bool m_bLButtonDown;
155   int32_t m_iCurSel;
156   int32_t m_iBtnState;
157 };
158 
159 #endif  // XFA_FWL_CFWL_COMBOBOX_H_
160