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 #include <memory>
8 
9 #include "xfa/src/foxitlib.h"
10 
Create()11 CFWL_ComboBox* CFWL_ComboBox::Create() {
12   return new CFWL_ComboBox;
13 }
Initialize(const CFWL_WidgetProperties * pProperties)14 FWL_ERR CFWL_ComboBox::Initialize(const CFWL_WidgetProperties* pProperties) {
15   if (m_pIface)
16     return FWL_ERR_Indefinite;
17   if (pProperties) {
18     *m_pProperties = *pProperties;
19   }
20   std::unique_ptr<IFWL_ComboBox> pComboBox(IFWL_ComboBox::Create(
21       m_pProperties->MakeWidgetImpProperties(&m_comboBoxData)));
22   FWL_ERR ret = pComboBox->Initialize();
23   if (ret != FWL_ERR_Succeeded) {
24     return ret;
25   }
26   m_pIface = pComboBox.release();
27   CFWL_Widget::Initialize();
28   return FWL_ERR_Succeeded;
29 }
AddString(const CFX_WideStringC & wsText)30 int32_t CFWL_ComboBox::AddString(const CFX_WideStringC& wsText) {
31   CFWL_ComboBoxItem* pItem = new CFWL_ComboBoxItem;
32   pItem->m_wsText = wsText;
33   pItem->m_dwStyles = 0;
34   return m_comboBoxData.m_arrItem.Add(pItem);
35 }
AddString(const CFX_WideStringC & wsText,CFX_DIBitmap * pIcon)36 int32_t CFWL_ComboBox::AddString(const CFX_WideStringC& wsText,
37                                  CFX_DIBitmap* pIcon) {
38   CFWL_ComboBoxItem* pItem = new CFWL_ComboBoxItem;
39   pItem->m_wsText = wsText;
40   pItem->m_dwStyles = 0;
41   pItem->m_pDIB = pIcon;
42   return m_comboBoxData.m_arrItem.Add(pItem);
43 }
RemoveAt(int32_t iIndex)44 int32_t CFWL_ComboBox::RemoveAt(int32_t iIndex) {
45   return m_comboBoxData.m_arrItem.RemoveAt(iIndex);
46 }
RemoveAll()47 int32_t CFWL_ComboBox::RemoveAll() {
48   m_comboBoxData.m_arrItem.RemoveAll();
49   return 0;
50 }
CountItems()51 int32_t CFWL_ComboBox::CountItems() {
52   return m_comboBoxData.CountItems(GetWidget());
53 }
GetTextByIndex(int32_t iIndex,CFX_WideString & wsText)54 FWL_ERR CFWL_ComboBox::GetTextByIndex(int32_t iIndex, CFX_WideString& wsText) {
55   CFWL_ComboBoxItem* pItem = reinterpret_cast<CFWL_ComboBoxItem*>(
56       m_comboBoxData.GetItem(m_pIface, iIndex));
57   if (!pItem)
58     return FWL_ERR_Indefinite;
59   wsText = pItem->m_wsText;
60   return FWL_ERR_Succeeded;
61 }
GetCurSel()62 int32_t CFWL_ComboBox::GetCurSel() {
63   if (!m_pIface)
64     return -1;
65   return static_cast<IFWL_ComboBox*>(m_pIface)->GetCurSel();
66 }
SetCurSel(int32_t iSel)67 FWL_ERR CFWL_ComboBox::SetCurSel(int32_t iSel) {
68   if (!m_pIface)
69     return FWL_ERR_Indefinite;
70   return static_cast<IFWL_ComboBox*>(m_pIface)->SetCurSel(iSel);
71 }
SetEditText(const CFX_WideStringC & wsText)72 FWL_ERR CFWL_ComboBox::SetEditText(const CFX_WideStringC& wsText) {
73   if (!m_pIface)
74     return FWL_ERR_Indefinite;
75   return static_cast<IFWL_ComboBox*>(m_pIface)->SetEditText(wsText);
76 }
GetEditTextLength() const77 int32_t CFWL_ComboBox::GetEditTextLength() const {
78   if (!m_pIface)
79     return 0;
80   return static_cast<IFWL_ComboBox*>(m_pIface)->GetEditTextLength();
81 }
GetEditText(CFX_WideString & wsText,int32_t nStart,int32_t nCount) const82 FWL_ERR CFWL_ComboBox::GetEditText(CFX_WideString& wsText,
83                                    int32_t nStart,
84                                    int32_t nCount) const {
85   if (!m_pIface)
86     return FWL_ERR_Indefinite;
87   return static_cast<IFWL_ComboBox*>(m_pIface)
88       ->GetEditText(wsText, nStart, nCount);
89 }
SetEditSelRange(int32_t nStart,int32_t nCount)90 FWL_ERR CFWL_ComboBox::SetEditSelRange(int32_t nStart, int32_t nCount) {
91   if (!m_pIface)
92     return FWL_ERR_Indefinite;
93   return static_cast<IFWL_ComboBox*>(m_pIface)->SetEditSelRange(nStart, nCount);
94 }
GetEditSelRange(int32_t nIndex,int32_t & nStart)95 int32_t CFWL_ComboBox::GetEditSelRange(int32_t nIndex, int32_t& nStart) {
96   if (!m_pIface)
97     return 0;
98   return static_cast<IFWL_ComboBox*>(m_pIface)->GetEditSelRange(nIndex, nStart);
99 }
GetEditLimit()100 int32_t CFWL_ComboBox::GetEditLimit() {
101   if (!m_pIface)
102     return 0;
103   return static_cast<IFWL_ComboBox*>(m_pIface)->GetEditLimit();
104 }
SetEditLimit(int32_t nLimit)105 FWL_ERR CFWL_ComboBox::SetEditLimit(int32_t nLimit) {
106   if (!m_pIface)
107     return FWL_ERR_Indefinite;
108   return static_cast<IFWL_ComboBox*>(m_pIface)->SetEditLimit(nLimit);
109 }
EditDoClipboard(int32_t iCmd)110 FWL_ERR CFWL_ComboBox::EditDoClipboard(int32_t iCmd) {
111   if (!m_pIface)
112     return FWL_ERR_Indefinite;
113   return static_cast<IFWL_ComboBox*>(m_pIface)->EditDoClipboard(iCmd);
114 }
EditRedo(const CFX_ByteStringC & bsRecord)115 FX_BOOL CFWL_ComboBox::EditRedo(const CFX_ByteStringC& bsRecord) {
116   if (!m_pIface)
117     return FALSE;
118   return static_cast<IFWL_ComboBox*>(m_pIface)->EditRedo(bsRecord);
119 }
EditUndo(const CFX_ByteStringC & bsRecord)120 FX_BOOL CFWL_ComboBox::EditUndo(const CFX_ByteStringC& bsRecord) {
121   if (!m_pIface)
122     return FALSE;
123   return static_cast<IFWL_ComboBox*>(m_pIface)->EditUndo(bsRecord);
124 }
SetMaxListHeight(FX_FLOAT fMaxHeight)125 FWL_ERR CFWL_ComboBox::SetMaxListHeight(FX_FLOAT fMaxHeight) {
126   m_comboBoxData.m_fMaxListHeight = fMaxHeight;
127   return FWL_ERR_Succeeded;
128 }
SetItemData(int32_t iIndex,void * pData)129 FWL_ERR CFWL_ComboBox::SetItemData(int32_t iIndex, void* pData) {
130   CFWL_ComboBoxItem* pItem = reinterpret_cast<CFWL_ComboBoxItem*>(
131       m_comboBoxData.GetItem(m_pIface, iIndex));
132   if (!pItem)
133     return FWL_ERR_Indefinite;
134   pItem->m_pData = pData;
135   return FWL_ERR_Succeeded;
136 }
GetItemData(int32_t iIndex)137 void* CFWL_ComboBox::GetItemData(int32_t iIndex) {
138   CFWL_ComboBoxItem* pItem = reinterpret_cast<CFWL_ComboBoxItem*>(
139       m_comboBoxData.GetItem(m_pIface, iIndex));
140   if (!pItem)
141     return NULL;
142   return pItem->m_pData;
143 }
SetListTheme(IFWL_ThemeProvider * pTheme)144 FWL_ERR CFWL_ComboBox::SetListTheme(IFWL_ThemeProvider* pTheme) {
145   return static_cast<IFWL_ComboBox*>(m_pIface)->GetListBoxt()->SetThemeProvider(
146       pTheme);
147 }
AfterFocusShowDropList()148 FX_BOOL CFWL_ComboBox::AfterFocusShowDropList() {
149   return static_cast<IFWL_ComboBox*>(m_pIface)->AfterFocusShowDropList();
150 }
OpenDropDownList(FX_BOOL bActivate)151 FWL_ERR CFWL_ComboBox::OpenDropDownList(FX_BOOL bActivate) {
152   return static_cast<IFWL_ComboBox*>(m_pIface)->OpenDropDownList(bActivate);
153 }
EditCanUndo()154 FX_BOOL CFWL_ComboBox::EditCanUndo() {
155   if (!m_pIface)
156     return FALSE;
157   return static_cast<IFWL_ComboBox*>(m_pIface)->EditCanUndo();
158 }
EditCanRedo()159 FX_BOOL CFWL_ComboBox::EditCanRedo() {
160   if (!m_pIface)
161     return FALSE;
162   return static_cast<IFWL_ComboBox*>(m_pIface)->EditCanRedo();
163 }
EditUndo()164 FX_BOOL CFWL_ComboBox::EditUndo() {
165   if (!m_pIface)
166     return FALSE;
167   return static_cast<IFWL_ComboBox*>(m_pIface)->EditUndo();
168 }
EditRedo()169 FX_BOOL CFWL_ComboBox::EditRedo() {
170   if (!m_pIface)
171     return FALSE;
172   return static_cast<IFWL_ComboBox*>(m_pIface)->EditRedo();
173 }
EditCanCopy()174 FX_BOOL CFWL_ComboBox::EditCanCopy() {
175   if (!m_pIface)
176     return FALSE;
177   return static_cast<IFWL_ComboBox*>(m_pIface)->EditCanCopy();
178 }
EditCanCut()179 FX_BOOL CFWL_ComboBox::EditCanCut() {
180   if (!m_pIface)
181     return FALSE;
182   return static_cast<IFWL_ComboBox*>(m_pIface)->EditCanCut();
183 }
EditCanSelectAll()184 FX_BOOL CFWL_ComboBox::EditCanSelectAll() {
185   if (!m_pIface)
186     return FALSE;
187   return static_cast<IFWL_ComboBox*>(m_pIface)->EditCanSelectAll();
188 }
EditCopy(CFX_WideString & wsCopy)189 FX_BOOL CFWL_ComboBox::EditCopy(CFX_WideString& wsCopy) {
190   if (!m_pIface)
191     return FALSE;
192   return static_cast<IFWL_ComboBox*>(m_pIface)->EditCopy(wsCopy);
193 }
EditCut(CFX_WideString & wsCut)194 FX_BOOL CFWL_ComboBox::EditCut(CFX_WideString& wsCut) {
195   if (!m_pIface)
196     return FALSE;
197   return static_cast<IFWL_ComboBox*>(m_pIface)->EditCut(wsCut);
198 }
EditPaste(const CFX_WideString & wsPaste)199 FX_BOOL CFWL_ComboBox::EditPaste(const CFX_WideString& wsPaste) {
200   if (!m_pIface)
201     return FALSE;
202   return static_cast<IFWL_ComboBox*>(m_pIface)->EditPaste(wsPaste);
203 }
EditSelectAll()204 FX_BOOL CFWL_ComboBox::EditSelectAll() {
205   if (!m_pIface)
206     return FALSE;
207   return static_cast<IFWL_ComboBox*>(m_pIface)->EditSelectAll();
208 }
EditDelete()209 FX_BOOL CFWL_ComboBox::EditDelete() {
210   if (!m_pIface)
211     return FALSE;
212   return static_cast<IFWL_ComboBox*>(m_pIface)->EditDelete();
213 }
EditDeSelect()214 FX_BOOL CFWL_ComboBox::EditDeSelect() {
215   if (!m_pIface)
216     return FALSE;
217   return static_cast<IFWL_ComboBox*>(m_pIface)->EditDeSelect();
218 }
GetBBox(CFX_RectF & rect)219 FWL_ERR CFWL_ComboBox::GetBBox(CFX_RectF& rect) {
220   if (!m_pIface)
221     return FALSE;
222   return static_cast<IFWL_ComboBox*>(m_pIface)->GetBBox(rect);
223 }
EditModifyStylesEx(FX_DWORD dwStylesExAdded,FX_DWORD dwStylesExRemoved)224 FWL_ERR CFWL_ComboBox::EditModifyStylesEx(FX_DWORD dwStylesExAdded,
225                                           FX_DWORD dwStylesExRemoved) {
226   if (!m_pIface)
227     return FALSE;
228   return static_cast<IFWL_ComboBox*>(m_pIface)
229       ->EditModifyStylesEx(dwStylesExAdded, dwStylesExRemoved);
230 }
CFWL_ComboBox()231 CFWL_ComboBox::CFWL_ComboBox() {}
~CFWL_ComboBox()232 CFWL_ComboBox::~CFWL_ComboBox() {}
CFWL_ComboBoxDP()233 CFWL_ComboBox::CFWL_ComboBoxDP::CFWL_ComboBoxDP() {
234   m_fItemHeight = 0;
235   m_fMaxListHeight = 0;
236 }
~CFWL_ComboBoxDP()237 CFWL_ComboBox::CFWL_ComboBoxDP::~CFWL_ComboBoxDP() {
238   int32_t nCount = m_arrItem.GetSize();
239   for (int32_t i = 0; i < nCount; i++) {
240     delete static_cast<CFWL_ComboBoxItem*>(m_arrItem[i]);
241   }
242   m_arrItem.RemoveAll();
243 }
CountItems(IFWL_Widget * pWidget)244 int32_t CFWL_ComboBox::CFWL_ComboBoxDP::CountItems(IFWL_Widget* pWidget) {
245   return m_arrItem.GetSize();
246 }
GetItem(IFWL_Widget * pWidget,int32_t nIndex)247 FWL_HLISTITEM CFWL_ComboBox::CFWL_ComboBoxDP::GetItem(IFWL_Widget* pWidget,
248                                                       int32_t nIndex) {
249   int32_t iCount = m_arrItem.GetSize();
250   if (nIndex >= iCount || nIndex < 0) {
251     return NULL;
252   }
253   return (FWL_HLISTITEM)m_arrItem[nIndex];
254 }
GetItemIndex(IFWL_Widget * pWidget,FWL_HLISTITEM hItem)255 int32_t CFWL_ComboBox::CFWL_ComboBoxDP::GetItemIndex(IFWL_Widget* pWidget,
256                                                      FWL_HLISTITEM hItem) {
257   return m_arrItem.Find(hItem);
258 }
SetItemIndex(IFWL_Widget * pWidget,FWL_HLISTITEM hItem,int32_t nIndex)259 FX_BOOL CFWL_ComboBox::CFWL_ComboBoxDP::SetItemIndex(IFWL_Widget* pWidget,
260                                                      FWL_HLISTITEM hItem,
261                                                      int32_t nIndex) {
262   return m_arrItem.SetAt(nIndex, hItem);
263 }
GetItemStyles(IFWL_Widget * pWidget,FWL_HLISTITEM hItem)264 FX_DWORD CFWL_ComboBox::CFWL_ComboBoxDP::GetItemStyles(IFWL_Widget* pWidget,
265                                                        FWL_HLISTITEM hItem) {
266   if (!hItem)
267     return 0;
268   return reinterpret_cast<CFWL_ComboBoxItem*>(hItem)->m_dwStyles;
269 }
GetItemText(IFWL_Widget * pWidget,FWL_HLISTITEM hItem,CFX_WideString & wsText)270 FWL_ERR CFWL_ComboBox::CFWL_ComboBoxDP::GetItemText(IFWL_Widget* pWidget,
271                                                     FWL_HLISTITEM hItem,
272                                                     CFX_WideString& wsText) {
273   if (!hItem)
274     return FWL_ERR_Indefinite;
275   wsText = reinterpret_cast<CFWL_ComboBoxItem*>(hItem)->m_wsText;
276   return FWL_ERR_Succeeded;
277 }
GetItemRect(IFWL_Widget * pWidget,FWL_HLISTITEM hItem,CFX_RectF & rtItem)278 FWL_ERR CFWL_ComboBox::CFWL_ComboBoxDP::GetItemRect(IFWL_Widget* pWidget,
279                                                     FWL_HLISTITEM hItem,
280                                                     CFX_RectF& rtItem) {
281   if (!hItem)
282     return FWL_ERR_Indefinite;
283   CFWL_ComboBoxItem* pItem = reinterpret_cast<CFWL_ComboBoxItem*>(hItem);
284   rtItem.Set(pItem->m_rtItem.left, pItem->m_rtItem.top, pItem->m_rtItem.width,
285              pItem->m_rtItem.height);
286   return FWL_ERR_Succeeded;
287 }
GetItemData(IFWL_Widget * pWidget,FWL_HLISTITEM hItem)288 void* CFWL_ComboBox::CFWL_ComboBoxDP::GetItemData(IFWL_Widget* pWidget,
289                                                   FWL_HLISTITEM hItem) {
290   if (!hItem)
291     return NULL;
292   CFWL_ComboBoxItem* pItem = reinterpret_cast<CFWL_ComboBoxItem*>(hItem);
293   return pItem->m_pData;
294 }
SetItemStyles(IFWL_Widget * pWidget,FWL_HLISTITEM hItem,FX_DWORD dwStyle)295 FWL_ERR CFWL_ComboBox::CFWL_ComboBoxDP::SetItemStyles(IFWL_Widget* pWidget,
296                                                       FWL_HLISTITEM hItem,
297                                                       FX_DWORD dwStyle) {
298   if (!hItem)
299     return FWL_ERR_Indefinite;
300   reinterpret_cast<CFWL_ComboBoxItem*>(hItem)->m_dwStyles = dwStyle;
301   return FWL_ERR_Succeeded;
302 }
SetItemText(IFWL_Widget * pWidget,FWL_HLISTITEM hItem,const FX_WCHAR * pszText)303 FWL_ERR CFWL_ComboBox::CFWL_ComboBoxDP::SetItemText(IFWL_Widget* pWidget,
304                                                     FWL_HLISTITEM hItem,
305                                                     const FX_WCHAR* pszText) {
306   if (!hItem)
307     return FWL_ERR_Indefinite;
308   reinterpret_cast<CFWL_ComboBoxItem*>(hItem)->m_wsText = pszText;
309   return FWL_ERR_Succeeded;
310 }
SetItemRect(IFWL_Widget * pWidget,FWL_HLISTITEM hItem,const CFX_RectF & rtItem)311 FWL_ERR CFWL_ComboBox::CFWL_ComboBoxDP::SetItemRect(IFWL_Widget* pWidget,
312                                                     FWL_HLISTITEM hItem,
313                                                     const CFX_RectF& rtItem) {
314   if (!hItem)
315     return FWL_ERR_Indefinite;
316   reinterpret_cast<CFWL_ComboBoxItem*>(hItem)->m_rtItem = rtItem;
317   return FWL_ERR_Succeeded;
318 }
GetItemHeight(IFWL_Widget * pWidget)319 FX_FLOAT CFWL_ComboBox::CFWL_ComboBoxDP::GetItemHeight(IFWL_Widget* pWidget) {
320   return m_fItemHeight;
321 }
GetItemIcon(IFWL_Widget * pWidget,FWL_HLISTITEM hItem)322 CFX_DIBitmap* CFWL_ComboBox::CFWL_ComboBoxDP::GetItemIcon(IFWL_Widget* pWidget,
323                                                           FWL_HLISTITEM hItem) {
324   if (!hItem)
325     return NULL;
326   return reinterpret_cast<CFWL_ComboBoxItem*>(hItem)->m_pDIB;
327 }
GetItemCheckRect(IFWL_Widget * pWidget,FWL_HLISTITEM hItem,CFX_RectF & rtCheck)328 FWL_ERR CFWL_ComboBox::CFWL_ComboBoxDP::GetItemCheckRect(IFWL_Widget* pWidget,
329                                                          FWL_HLISTITEM hItem,
330                                                          CFX_RectF& rtCheck) {
331   CFWL_ComboBoxItem* pItem = reinterpret_cast<CFWL_ComboBoxItem*>(hItem);
332   rtCheck = pItem->m_rtCheckBox;
333   return FWL_ERR_Succeeded;
334 }
SetItemCheckRect(IFWL_Widget * pWidget,FWL_HLISTITEM hItem,const CFX_RectF & rtCheck)335 FWL_ERR CFWL_ComboBox::CFWL_ComboBoxDP::SetItemCheckRect(
336     IFWL_Widget* pWidget,
337     FWL_HLISTITEM hItem,
338     const CFX_RectF& rtCheck) {
339   CFWL_ComboBoxItem* pItem = reinterpret_cast<CFWL_ComboBoxItem*>(hItem);
340   pItem->m_rtCheckBox = rtCheck;
341   return FWL_ERR_Succeeded;
342 }
GetItemCheckState(IFWL_Widget * pWidget,FWL_HLISTITEM hItem)343 FX_DWORD CFWL_ComboBox::CFWL_ComboBoxDP::GetItemCheckState(
344     IFWL_Widget* pWidget,
345     FWL_HLISTITEM hItem) {
346   CFWL_ComboBoxItem* pItem = reinterpret_cast<CFWL_ComboBoxItem*>(hItem);
347   return pItem->m_dwCheckState;
348 }
SetItemCheckState(IFWL_Widget * pWidget,FWL_HLISTITEM hItem,FX_DWORD dwCheckState)349 FWL_ERR CFWL_ComboBox::CFWL_ComboBoxDP::SetItemCheckState(
350     IFWL_Widget* pWidget,
351     FWL_HLISTITEM hItem,
352     FX_DWORD dwCheckState) {
353   CFWL_ComboBoxItem* pItem = reinterpret_cast<CFWL_ComboBoxItem*>(hItem);
354   pItem->m_dwCheckState = dwCheckState;
355   return FWL_ERR_Succeeded;
356 }
GetListHeight(IFWL_Widget * pWidget)357 FX_FLOAT CFWL_ComboBox::CFWL_ComboBoxDP::GetListHeight(IFWL_Widget* pWidget) {
358   return m_fMaxListHeight;
359 }
360